1 #ifndef CombineTools_CombineHarvester_h
2 #define CombineTools_CombineHarvester_h
8 #include <unordered_map>
13 #include "boost/range/algorithm_ext/erase.hpp"
16 #include "RooWorkspace.h"
28 typedef std::vector<std::pair<int, std::string>>
Categories;
68 void SetFlag(std::string
const& flag,
bool const& value);
69 bool GetFlag(std::string
const& flag)
const;
95 inline void SetVerbosity(
unsigned verbosity) { verbosity_ = verbosity; }
111 std::string
const&
era,
114 std::string
const&
mass);
116 std::string parse_rule =
"");
118 void WriteDatacard(std::string
const& name, std::string
const& root_file);
119 void WriteDatacard(std::string
const& name, TFile & root_file);
148 CombineHarvester&
attr(std::vector<std::string>
const& vec,std::string attr_label,
bool cond =
true);
160 template<
typename Function>
162 template<
typename Function>
164 template<
typename Function>
166 template<
typename Function>
179 std::set<std::string>
bin_set();
183 std::set<std::string>
era_set();
202 template <
typename T,
203 typename R =
typename std::decay<
204 typename std::result_of<T(
Object const*)>::type>::type>
212 template <
typename T,
213 typename R =
typename std::decay<
214 typename std::result_of<T(
Observation const*)>::type>::type>
222 template <
typename T,
223 typename R =
typename std::decay<
224 typename std::result_of<T(
Process const*)>::type>::type>
232 template <
typename T,
233 typename R =
typename std::decay<
234 typename std::result_of<T(
Systematic const*)>::type>::type>
262 void RenameParameter(std::string
const& oldname, std::string
const& newname);
264 template<
typename Function>
267 template<
typename Function>
270 template<
typename Function>
273 template<
typename Function>
277 void ZeroBins(
double min,
double max);
282 void setParInWs(
const std::string name,
double value) ;
283 void renameParInWs(
const std::string& name,
const std::string& newName,
const std::string& wsName=
"");
295 void SetGroup(std::string
const& name, std::vector<std::string>
const& patterns);
306 void RemoveGroup(std::string
const& name, std::vector<std::string>
const& patterns);
314 void RenameGroup(std::string
const& oldname, std::string
const& newname);
352 double GetUncertainty(RooFitResult
const* fit,
unsigned n_samples);
353 double GetUncertainty(RooFitResult
const& fit,
unsigned n_samples);
399 std::vector<std::string>
era,
400 std::vector<std::string>
channel,
405 std::vector<std::string>
era,
406 std::vector<std::string>
channel,
407 std::vector<std::string> procs,
409 void AddSystVar(std::string
const& name,
double val,
double err);
411 std::string
const& type,
bool asymm,
double val_u,
412 double val_d, std::string
const& formula,
413 std::string
const& args);
417 std::string
const& type, Map
const& valmap);
419 void ExtractShapes(std::string
const& file, std::string
const& rule,
420 std::string
const& syst_rule);
422 std::string
const& rule, std::string norm_rule =
"");
423 void ExtractData(std::string
const& ws_name, std::string
const& rule);
425 void AddWorkspace(RooWorkspace
const& ws,
bool can_rename =
false);
464 void MergeBinErrors(
double bbb_threshold,
double merge_threshold);
471 void AddExtArgValue(std::string
const& name,
double const& value);
478 std::vector<std::shared_ptr<Observation>> obs_;
479 std::vector<std::shared_ptr<Process>> procs_;
480 std::vector<std::shared_ptr<Systematic>> systs_;
481 std::map<std::string, std::shared_ptr<Parameter>> params_;
482 std::map<std::string, std::shared_ptr<RooWorkspace>> wspaces_;
484 std::unordered_map<std::string, bool> flags_;
486 struct AutoMCStatsSettings {
487 double event_threshold;
491 AutoMCStatsSettings(
double thresh,
bool sig=
false,
int mode=1) {
492 event_threshold = thresh;
493 include_signal = sig;
497 AutoMCStatsSettings() : AutoMCStatsSettings(0.) {}
500 std::map<std::string, AutoMCStatsSettings> auto_stats_settings_;
501 std::vector<std::string> post_lines_;
506 typedef std::pair<std::string, std::string> StrPair;
507 typedef std::vector<StrPair> StrPairVec;
508 typedef std::vector<std::string> StrVec;
516 std::ostream& log()
const {
return *log_; }
522 void LoadShapes(Observation* entry,
523 std::vector<HistMapping>
const& mappings);
524 void LoadShapes(Process* entry,
525 std::vector<HistMapping>
const& mappings);
526 void LoadShapes(Systematic* entry,
527 std::vector<HistMapping>
const& mappings);
529 HistMapping
const& ResolveMapping(std::string
const&
process,
530 std::string
const&
bin,
531 std::vector<HistMapping>
const& mappings);
533 StrPairVec GenerateShapeMapAttempts(std::string
process,
534 std::string category);
536 std::shared_ptr<RooWorkspace> SetupWorkspace(RooWorkspace
const& ws,
537 bool can_rename =
false);
539 void ImportParameters(RooArgSet *vars);
541 RooAbsData
const* FindMatchingData(Process
const* proc);
543 ch::Parameter * SetupRateParamVar(std::string
const& name,
double val,
bool is_ext_arg =
false);
544 void SetupRateParamFunc(std::string
const& name, std::string
const& formula,
545 std::string
const& pars);
546 void SetupRateParamWspObj(std::string
const& name, std::string
const& obj,
bool is_ext_arg =
false);
547 bool SetupRateParamWspObjFromWsStore(std::string
const& name, std::string
const& obj, std::map<std::string, std::shared_ptr<RooWorkspace>>
const& ws_store);
551 void WriteHistToFile(
554 std::vector<HistMapping>
const& mappings,
555 std::string
const&
bin,
557 std::string
const&
mass,
558 std::string
const& nuisance,
561 void FillHistMappings(std::vector<HistMapping> & mappings);
567 typedef std::vector<std::vector<Systematic const*>> ProcSystMap;
568 ProcSystMap GenerateProcSystMap();
570 double GetRateInternal(ProcSystMap
const& lookup,
571 std::string
const& single_sys =
"");
573 TH1F GetShapeInternal(ProcSystMap
const& lookup,
574 std::string
const& single_sys =
"");
576 inline double smoothStepFunc(
double x)
const {
577 if (std::fabs(x) >= 1.0)
return x > 0 ? +1 : -1;
578 double xnorm = x/1.0;
579 double xnorm2 = xnorm*xnorm;
580 return 0.125 * xnorm * (xnorm2 * (3.*xnorm2 - 10.) + 15);
583 double logKappaForX(
double x,
double k_low,
double k_high)
const;
585 void ShapeDiff(
double x, TH1F* target, TH1
const* nom, TH1
const* low,
586 TH1
const* high,
bool linear);
587 void ShapeDiff(
double x, TH1F* target, RooDataHist
const* nom,
588 RooDataHist
const* low, RooDataHist
const* high);
591 RooWorkspace* fixRooConstVar(RooWorkspace *win,
bool useRooRealVar=
true,
bool clean=
true);
598 template <
typename T,
typename R>
601 for (
auto const& item : obs_) ret.insert(func(item.get()));
602 for (
auto const& item : procs_) ret.insert(func(item.get()));
603 for (
auto const& item : systs_) ret.insert(func(item.get()));
607 template <
typename T,
typename R>
610 for (
auto const& item : obs_) ret.insert(func(item.get()));
614 template <
typename T,
typename R>
617 for (
auto const& item : procs_) ret.insert(func(item.get()));
621 template <
typename T,
typename R>
624 for (
auto const& item : systs_) ret.insert(func(item.get()));
628 template<
typename Function>
635 template<
typename Function>
637 for (
auto & item: procs_) func(item.get());
640 template<
typename Function>
642 for (
auto & item: obs_) func(item.get());
645 template<
typename Function>
647 for (
auto & item: systs_) func(item.get());
650 template<
typename Function>
658 template<
typename Function>
660 boost::remove_erase_if(
661 obs_, [&](std::shared_ptr<Observation> ptr) {
return func(ptr.get());
666 template<
typename Function>
668 boost::remove_erase_if(
669 procs_, [&](std::shared_ptr<Process> ptr) {
return func(ptr.get());
673 template<
typename Function>
675 boost::remove_erase_if(
676 systs_, [&](std::shared_ptr<Systematic> ptr) {
return func(ptr.get());
683 std::string
const& name, std::string
const& type,
687 std::vector<ch::Process *> not_added_procs;
688 std::vector<ch::Process *> added_procs;
692 auto tuples = valmap.GetTupleSet();
693 if (verbosity_ >= 1) {
694 log() << (name +
":" + type) <<
"\n";
696 for (
unsigned i = 0; i < procs_.size(); ++i) {
697 if (!valmap.Contains(procs_[i].get())) {
698 not_added_procs.push_back(procs_[i].get());
701 tuples.erase(valmap.GetTuple(procs_[i].get()));
702 added_procs.push_back(procs_[i].get());
703 double val_u = valmap.ValU(procs_[i].get());
704 double val_d = valmap.ValD(procs_[i].get());
705 std::string formula = valmap.Formula(procs_[i].get());
706 std::string args = valmap.Args(procs_[i].get());
708 val_u, val_d, formula, args);
710 if (tuples.size() && verbosity_ >= 1) {
711 log() <<
">> Map keys that were not used to create a Systematic:\n";
712 for (
auto s : tuples) {
716 if (verbosity_ >= 2) {
718 log() <<
">> Process entries that did not get a Systematic:\n";
719 for (
auto p : not_added_procs) {
722 log() <<
">> Process entries that did get a Systematic:\n";
723 for (
auto p : added_procs) {
void SetVerbosity(unsigned verbosity)
void AddSystFromProc(Process const &proc, std::string const &name, std::string const &type, bool asymm, double val_u, double val_d, std::string const &formula, std::string const &args)
TH2F GetRateCovariance(RooFitResult const &fit, unsigned n_samples)
void ZeroBins(double min, double max)
CombineHarvester & process_rgx(std::vector< std::string > const &vec, bool cond=true)
void AddWorkspace(RooWorkspace const &ws, bool can_rename=false)
CombineHarvester & bin_id(std::vector< int > const &vec, bool cond=true)
void AddProcesses(std::vector< std::string > mass, std::vector< std::string > analysis, std::vector< std::string > era, std::vector< std::string > channel, std::vector< std::string > procs, ch::Categories bin, bool signal)
CombineHarvester & backgrounds()
void AddBinByBin(double threshold, bool fixed_norm, CombineHarvester *other)
Create bin-by-bin uncertainties.
CombineHarvester & signals()
CombineHarvester & mass(std::vector< std::string > const &vec, bool cond=true)
void RenameParameter(std::string const &oldname, std::string const &newname)
CombineHarvester & PrintObs()
CombineHarvester & pdfs()
void AddSystVar(std::string const &name, double val, double err)
void CreateParameterIfEmpty(std::string const &name)
TH2F GetRateCorrelation(RooFitResult const &fit, unsigned n_samples)
CombineHarvester & FilterObs(Function func)
void MergeBinErrors(double bbb_threshold, double merge_threshold)
Merge bin errors within a bin.
CombineHarvester & process(std::vector< std::string > const &vec, bool cond=true)
void ForEachSyst(Function func)
void ForEachProc(Function func)
void RenameAutoMCStatsBin(std::string const &oldname, std::string const &newname)
std::set< R > SetFromObs(T func)
Fill an std::set using only the Observation entries.
void renameParInWs(const std::string &name, const std::string &newName, const std::string &wsName="")
CombineHarvester & data()
void SetFlag(std::string const &flag, bool const &value)
Set a named flag value.
CombineHarvester & era(std::vector< std::string > const &vec, bool cond=true)
std::set< int > bin_id_set()
CombineHarvester & operator=(CombineHarvester other)
std::set< std::string > process_set()
void AddDatacardLineAtEnd(std::string const &line)
Add a line of text at the end of all datacards.
void SetGroup(std::string const &name, std::vector< std::string > const &patterns)
Add parameters to a given group.
void WriteDatacard(std::string const &name, std::string const &root_file)
CombineHarvester & attr(std::vector< std::string > const &vec, std::string attr_label, bool cond=true)
CombineHarvester & PrintAll()
CombineHarvester & analysis(std::vector< std::string > const &vec, bool cond=true)
friend void swap(CombineHarvester &first, CombineHarvester &second)
std::set< R > SetFromSysts(T func)
Fill an std::set using only the Systematic entries.
std::set< std::string > syst_type_set()
TH1F GetShapeWithUncertainty()
void RenameGroup(std::string const &oldname, std::string const &newname)
Rename a nuisance parameter group.
std::set< R > SetFromProcs(T func)
Fill an std::set using only the Process entries.
CombineHarvester & bin(std::vector< std::string > const &vec, bool cond=true)
void SetAutoMCStats(CombineHarvester &target, double thresh, bool sig=false, int mode=1)
void UpdateParameters(std::vector< ch::Parameter > const ¶ms)
std::set< std::string > mass_set()
std::set< std::string > bin_set()
CombineHarvester & syst_name(std::vector< std::string > const &vec, bool cond=true)
void InsertProcess(ch::Process const &proc)
void AddObservations(std::vector< std::string > mass, std::vector< std::string > analysis, std::vector< std::string > era, std::vector< std::string > channel, ch::Categories bin)
void ForEachObs(Function func)
CombineHarvester & FilterSysts(Function func)
bool GetFlag(std::string const &flag) const
void VariableRebin(std::vector< double > bins)
std::set< std::string > era_set()
int ParseDatacard(std::string const &filename, std::string const &analysis, std::string const &era, std::string const &channel, int bin_id, std::string const &mass)
std::set< std::string > channel_set()
CombineHarvester & FilterAll(Function func)
void AddSyst(CombineHarvester &target, std::string const &name, std::string const &type, Map const &valmap)
CombineHarvester & FilterProcs(Function func)
CombineHarvester & PrintParams()
CombineHarvester & histograms()
void InsertSystematic(ch::Systematic const &sys)
std::set< std::string > GetAutoMCStatsBins() const
void RemoveGroup(std::string const &name, std::vector< std::string > const &patterns)
Remove parameters to a given group.
std::set< std::string > syst_name_set()
void ExtractData(std::string const &ws_name, std::string const &rule)
void ClearDatacardLinesAtEnd()
Clear all added datacard lines.
std::set< R > SetFromAll(T func)
Fill an std::set with the return values from an arbitrary function.
CombineHarvester & PrintProcs()
CombineHarvester deep()
Creates and retunrs a deep copy of the CombineHarvester instance.
void InsertObservation(ch::Observation const &obs)
CombineHarvester & PrintSysts()
std::vector< ch::Parameter > GetParameters() const
std::set< std::string > analysis_set()
double getParFromWs(const std::string name)
void ExtractShapes(std::string const &file, std::string const &rule, std::string const &syst_rule)
void SetPdfBins(unsigned nbins)
CombineHarvester cp()
Creates and returns a shallow copy of the CombineHarvester instance.
void ForEachObj(Function func)
void AddExtArgValue(std::string const &name, double const &value)
ch::Parameter const * GetParameter(std::string const &name) const
CombineHarvester & channel(std::vector< std::string > const &vec, bool cond=true)
CombineHarvester & syst_type(std::vector< std::string > const &vec, bool cond=true)
void setParInWs(const std::string name, double value)
void RenameSystematic(CombineHarvester &target, std::string const &old_name, std::string const &new_name)
Rename a systematic from 'old_name' to 'new_name' and add a parameter 'new_name' to CH instance 'targ...
void ExtractPdfs(CombineHarvester &target, std::string const &ws_name, std::string const &rule, std::string norm_rule="")
static std::ostream & PrintHeader(std::ostream &out)
std::string Tuple2String(const std::tuple< Args... > &t)
Format any std::tuple as a string.
std::vector< std::pair< int, std::string > > Categories