CombineHarvester
CombineHarvester_Creation.cc
Go to the documentation of this file.
2 #include <vector>
3 #include <map>
4 #include <string>
5 #include <iomanip>
6 #include <iostream>
7 #include <utility>
8 #include <algorithm>
9 #include "TDirectory.h"
10 #include "TH1.h"
18 
19 namespace ch {
21  std::vector<std::string> mass,
22  std::vector<std::string> analysis,
23  std::vector<std::string> era,
24  std::vector<std::string> channel,
25  ch::Categories bin) {
26  std::vector<unsigned> lengths = {
27  unsigned(mass.size()),
28  unsigned(analysis.size()),
29  unsigned(era.size()),
30  unsigned(channel.size()),
31  unsigned(bin.size())};
32  auto comb = ch::GenerateCombinations(lengths);
33  for (auto const& c : comb) {
34  auto obs = std::make_shared<Observation>();
35  obs->set_mass(mass[c[0]]);
36  obs->set_analysis(analysis[c[1]]);
37  obs->set_era(era[c[2]]);
38  obs->set_channel(channel[c[3]]);
39  obs->set_bin_id(bin[c[4]].first);
40  obs->set_bin(bin[c[4]].second);
41  obs_.push_back(obs);
42  }
43 }
44 
46  std::vector<std::string> mass,
47  std::vector<std::string> analysis,
48  std::vector<std::string> era,
49  std::vector<std::string> channel,
50  std::vector<std::string> procs,
51  ch::Categories bin,
52  bool signal) {
53  std::vector<unsigned> lengths = {
54  unsigned(mass.size()),
55  unsigned(analysis.size()),
56  unsigned(era.size()),
57  unsigned(channel.size()),
58  unsigned(bin.size())};
59  auto comb = ch::GenerateCombinations(lengths);
60  for (auto const& c : comb) {
61  for (unsigned i = 0; i < procs.size(); ++i) {
62  auto proc = std::make_shared<Process>();
63  proc->set_mass(mass[c[0]]);
64  proc->set_analysis(analysis[c[1]]);
65  proc->set_era(era[c[2]]);
66  proc->set_channel(channel[c[3]]);
67  proc->set_bin_id(bin[c[4]].first);
68  proc->set_bin(bin[c[4]].second);
69  proc->set_process(procs[i]);
70  proc->set_signal(signal);
71  procs_.push_back(proc);
72  }
73  }
74 }
75 
77  std::string const& name,
78  std::string const& type, bool asymm,
79  double val_u, double val_d,
80  std::string const& formula,
81  std::string const& args) {
82  std::string subbed_name = name;
83  boost::replace_all(subbed_name, "$BINID",
84  boost::lexical_cast<std::string>(proc.bin_id()));
85  boost::replace_all(subbed_name, "$BIN", proc.bin());
86  boost::replace_all(subbed_name, "$PROCESS", proc.process());
87  boost::replace_all(subbed_name, "$MASS", proc.mass());
88  boost::replace_all(subbed_name, "$ERA", proc.era());
89  boost::replace_all(subbed_name, "$CHANNEL", proc.channel());
90  boost::replace_all(subbed_name, "$ANALYSIS", proc.analysis());
91  std::map<std::string,std::string> attrs = proc.all_attributes();
92  for( const auto it : attrs){
93  boost::replace_all(subbed_name, "$ATTR("+it.first+")",proc.attribute(it.first));
94  }
95  auto sys = std::make_shared<Systematic>();
96  ch::SetProperties(sys.get(), &proc);
97  sys->set_name(subbed_name);
98  sys->set_type(type);
99  if (type == "lnN" || type == "lnU") {
100  sys->set_asymm(asymm);
101  sys->set_value_u(val_u);
102  sys->set_value_d(val_d);
103  CreateParameterIfEmpty(sys->name());
104  } else if (type == "shape" || type == "shapeN2" || type == "shapeU") {
105  sys->set_asymm(true);
106  sys->set_value_u(1.0);
107  sys->set_value_d(1.0);
108  sys->set_scale(val_u);
109  CreateParameterIfEmpty(sys->name());
110  } else if (type == "rateParam") {
111  sys->set_asymm(false);
112  if (formula == "" && args == "") {
113  SetupRateParamVar(subbed_name, val_u);
114  } else {
115  std::string subbed_args = args;
116  boost::replace_all(subbed_args, "$BINID",
117  boost::lexical_cast<std::string>(proc.bin_id()));
118  boost::replace_all(subbed_args, "$BIN", proc.bin());
119  boost::replace_all(subbed_args, "$PROCESS", proc.process());
120  boost::replace_all(subbed_args, "$MASS", proc.mass());
121  boost::replace_all(subbed_args, "$ERA", proc.era());
122  boost::replace_all(subbed_args, "$CHANNEL", proc.channel());
123  boost::replace_all(subbed_args, "$ANALYSIS", proc.analysis());
124  for( const auto it : attrs){
125  boost::replace_all(subbed_args, "$ATTR("+it.first+")",proc.attribute(it.first));
126  }
127  SetupRateParamFunc(subbed_name, formula, subbed_args);
128  }
129  }
130  if (sys->type() == "lnU" || sys->type() == "shapeU") {
131  params_.at(sys->name())->set_err_d(0.);
132  params_.at(sys->name())->set_err_u(0.);
133  }
134  systs_.push_back(sys);
135 }
136 
137 void CombineHarvester::AddSystVar(std::string const& name,
138  double val, double err) {
139  Parameter * param = SetupRateParamVar(name,val);
140  param->set_val(val);
141  param->set_err_u(+1.0 *err);
142  param->set_err_d(-1.0 *err);
143  auto sys = std::make_shared<Systematic>();
144  sys->set_name(name);
145  sys->set_type("param");
146  systs_.push_back(sys);
147 }
148 
149 void CombineHarvester::RenameSystematic(CombineHarvester &target, std::string const& old_name,
150  std::string const& new_name) {
151  for(unsigned i = 0; i<systs_.size(); ++i){
152  if(systs_[i]->name()==old_name){
153  systs_[i]->set_name(new_name);
154  target.CreateParameterIfEmpty(systs_[i]->name());
155  }
156  }
157 }
158 
159 void CombineHarvester::ExtractShapes(std::string const& file,
160  std::string const& rule,
161  std::string const& syst_rule) {
162  std::vector<HistMapping> mapping(1);
163  mapping[0].process = "*";
164  mapping[0].category = "*";
165  mapping[0].file = std::make_shared<TFile>(file.c_str());
166  mapping[0].pattern = rule;
167  mapping[0].syst_pattern = syst_rule;
168 
169  // Note that these LoadShapes calls will fail if we encounter
170  // any object that already has shapes
171  for (unsigned i = 0; i < obs_.size(); ++i) {
172  if (obs_[i]->shape() || obs_[i]->data()) continue;
173  LoadShapes(obs_[i].get(), mapping);
174  }
175  for (unsigned i = 0; i < procs_.size(); ++i) {
176  if (procs_[i]->shape() || procs_[i]->pdf()) continue;
177  LoadShapes(procs_[i].get(), mapping);
178  }
179  if (syst_rule == "") return;
180  for (unsigned i = 0; i < systs_.size(); ++i) {
181  if (systs_[i]->type() != "shape" && systs_[i]->type() != "shapeN2" &&
182  systs_[i]->type() != "shapeU")
183  continue;
184  LoadShapes(systs_[i].get(), mapping);
185  }
186 }
187 
188 void CombineHarvester::AddWorkspace(RooWorkspace const& ws,
189  bool can_rename) {
190  SetupWorkspace(ws, can_rename);
191 }
192 
194  std::string const& ws_name,
195  std::string const& rule,
196  std::string norm_rule) {
197  std::vector<HistMapping> mapping(1);
198  mapping[0].process = "*";
199  mapping[0].category = "*";
200  mapping[0].pattern = ws_name+":"+rule;
201  if (norm_rule != "") mapping[0].syst_pattern = ws_name + ":" + norm_rule;
202  if (!wspaces_.count(ws_name)) return;
203  mapping[0].ws = wspaces_.at(ws_name);
204  for (unsigned i = 0; i < procs_.size(); ++i) {
205  if (!procs_[i]->pdf()) {
206  target.LoadShapes(procs_[i].get(), mapping);
207  }
208  }
209 }
210 
211 void CombineHarvester::ExtractData(std::string const &ws_name,
212  std::string const &rule) {
213  std::vector<HistMapping> mapping(1);
214  mapping[0].process = "*";
215  mapping[0].category = "*";
216  mapping[0].pattern = ws_name+":"+rule;
217  if (!wspaces_.count(ws_name)) return;
218  mapping[0].ws = wspaces_.at(ws_name);
219  for (unsigned i = 0; i < obs_.size(); ++i) {
220  if (!obs_[i]->data()) {
221  LoadShapes(obs_[i].get(), mapping);
222  }
223  }
224 }
225 
226 void CombineHarvester::AddBinByBin(double threshold, bool fixed_norm,
227  CombineHarvester &other) {
228  AddBinByBin(threshold, fixed_norm, &other);
229 }
230 
231 void CombineHarvester::AddBinByBin(double threshold, bool fixed_norm,
232  CombineHarvester *other) {
233  auto bbb_factory = ch::BinByBinFactory()
234  .SetAddThreshold(threshold)
235  .SetFixNorm(fixed_norm)
236  .SetVerbosity(verbosity_);
237  bbb_factory.AddBinByBin(*this, *other);
238 }
239 
240 void CombineHarvester::CreateParameterIfEmpty(std::string const &name) {
241  if (!params_.count(name)) {
242  auto param = std::make_shared<Parameter>(Parameter());
243  param->set_name(name);
244  params_.insert({name, param});
245  }
246 }
247 
248 void CombineHarvester::MergeBinErrors(double bbb_threshold,
249  double merge_threshold) {
250  auto bbb_factory = ch::BinByBinFactory()
251  .SetAddThreshold(bbb_threshold)
252  .SetMergeThreshold(merge_threshold)
253  .SetVerbosity(verbosity_);
254  bbb_factory.MergeBinErrors(*this);
255 }
256 
258  obs_.push_back(std::make_shared<ch::Observation>(obs));
259 }
260 
262  procs_.push_back(std::make_shared<ch::Process>(proc));
263 }
264 
266  systs_.push_back(std::make_shared<ch::Systematic>(sys));
267 }
268 }
Merges bin uncertainties and creates bin-by-bin statistical uncertainties.
Definition: BinByBin.h:21
BinByBinFactory & SetAddThreshold(double val)
Set the fractional bin error threshold for bin-by-bin creation and for participation in the merging a...
Definition: BinByBin.h:99
BinByBinFactory & SetFixNorm(bool fix)
Whether or not the bin-by-bin systematics are allowed to vary the process normalisation.
Definition: BinByBin.h:124
void MergeBinErrors(CombineHarvester &cb)
Merges histogram bin errors between processes.
Definition: BinByBin.cc:22
BinByBinFactory & SetVerbosity(unsigned verbosity)
By default this class only produces output on the screen when an error occurs, set to a value greater...
Definition: BinByBin.h:90
BinByBinFactory & SetMergeThreshold(double val)
The threshold for the merging algorithm.
Definition: BinByBin.h:107
void AddBinByBin(CombineHarvester &src, CombineHarvester &dest)
Create bin-by-bin shape uncertainties for every process in src, and add these to dest
Definition: BinByBin.cc:105
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)
void AddWorkspace(RooWorkspace const &ws, bool can_rename=false)
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)
void AddBinByBin(double threshold, bool fixed_norm, CombineHarvester *other)
Create bin-by-bin uncertainties.
CombineHarvester & mass(std::vector< std::string > const &vec, bool cond=true)
void AddSystVar(std::string const &name, double val, double err)
void CreateParameterIfEmpty(std::string const &name)
void MergeBinErrors(double bbb_threshold, double merge_threshold)
Merge bin errors within a bin.
CombineHarvester & analysis(std::vector< std::string > const &vec, bool cond=true)
CombineHarvester & bin(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 InsertSystematic(ch::Systematic const &sys)
void ExtractData(std::string const &ws_name, std::string const &rule)
void InsertObservation(ch::Observation const &obs)
void ExtractShapes(std::string const &file, std::string const &rule, std::string const &syst_rule)
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="")
virtual std::string const attribute(std::string const &attr_label) const
Definition: Object.h:44
virtual std::string const & process() const
Definition: Object.h:20
virtual std::string const & bin() const
Definition: Object.h:17
virtual int bin_id() const
Definition: Object.h:35
virtual std::string const & analysis() const
Definition: Object.h:26
virtual std::string const & era() const
Definition: Object.h:29
virtual std::string const & mass() const
Definition: Object.h:38
virtual std::string const & channel() const
Definition: Object.h:32
virtual std::map< std::string, std::string > const & all_attributes() const
Definition: Object.h:43
void set_err_u(double const &err_u)
Definition: Parameter.h:33
void set_err_d(double const &err_d)
Definition: Parameter.h:36
void set_val(double const &val)
Definition: Parameter.h:23
Definition: Algorithm.h:10
void SetProperties(T *first, U const *second)
Definition: Utilities.h:59
std::vector< std::vector< unsigned > > GenerateCombinations(std::vector< unsigned > vec)
Definition: Utilities.cc:191
std::vector< std::pair< int, std::string > > Categories