gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
Cutflow.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
15 
16 #pragma once
17 
18 #include <string>
19 #include <vector>
20 #include <sstream>
21 #include <iostream>
22 #include <iomanip>
23 
25 
26 
27 namespace Gambit
28 {
29  namespace ColliderBit
30  {
31 
32  using namespace std;
33 
34 
36  struct Cutflow
37  {
38 
42  Cutflow() {}
43 
45  Cutflow(const string& cfname, const vector<string>& cutnames)
46  : name(cfname), ncuts(cutnames.size()), cuts(cutnames), counts(ncuts+1, 0), icurr(0)
47  { }
48 
50  void fillinit(double weight=1.)
51  {
52  counts[0] += weight;
53  icurr = 1;
54  }
55 
59  bool fill(size_t icut, bool cutresult=true, double weight=1.)
60  {
61  // if (icut == 0)
62  // throw RangeError("Cut number must be greater than 0");
63  if (cutresult) counts.at(icut) += weight;
64  icurr = icut + 1;
65  return cutresult;
66  }
67 
74  bool fill(size_t icut, double weight)
75  {
76  return fill(icut, true, weight);
77  }
78 
82  bool fill(size_t icut, const vector<bool>& cutresults, double weight=1.)
83  {
84  // throw RangeError("Cut number must be greater than 0");
85  // if (cutresults.size() > ncuts-icut)
86  // throw RangeError("Number of filled cut results needs to match the Cutflow construction");
87  bool rtn = true;
88  for (size_t i = 0; i < cutresults.size(); ++i)
89  if (!fill(icut+i, cutresults[i], weight)) { rtn = false; break; }
90  icurr = icut + cutresults.size();
91  return rtn;
92  }
93 
94 
96  bool fillall(const vector<bool>& cutresults, double weight=1.)
97  {
98  // if (cutresults.size() != ncuts)
99  // throw RangeError("Number of filled cut results needs to match the Cutflow construction");
100  // if (icut == 0) { fillinit(weight); icut = 1; }
101  return fill(1, cutresults, weight);
102  }
103 
107  bool fillnext(bool cutresult, double weight=1.)
108  {
109  return fill(icurr, cutresult, weight);
110  }
111 
115  bool fillnext(double weight=1.)
116  {
117  return fill(icurr, true, weight);
118  }
119 
123  bool fillnext(const vector<bool>& cutresults, double weight=1.)
124  {
125  return fill(icurr, cutresults, weight);
126  }
127 
128 
138  bool filltail(const vector<bool>& cutresults, double weight=1.)
139  {
140  return fill(ncuts+1-cutresults.size(), cutresults, weight);
141  }
142 
144  void scale(double factor)
145  {
146  for (double& x : counts) x *= factor;
147  }
148 
150  void normalize(double norm, size_t icut=0)
151  {
152  scale(norm/counts.at(icut));
153  }
154 
156  string str() const
157  {
158  using namespace std;
159  stringstream ss;
160  ss << fixed << std::setprecision(1) << counts.front();
161  const size_t count0len = ss.str().length();
162  ss.str("");
163  ss << name << " cut-flow:\n";
164  size_t maxnamelen = 0;
165  for (const string& t : cuts)
166  maxnamelen = max(t.length(), maxnamelen);
167  ss << setw(maxnamelen+5) << "" << " "
168  << setw(count0len) << right << "Count" << " "
169  << setw(6) << right << "A_cumu" << " "
170  << setw(6) << right << "A_incr";
171  for (size_t i = 0; i <= ncuts; ++i)
172  {
173  const int pcttot = (counts.front() == 0) ? -1 : round(100*counts.at(i)/double(counts.front()));
174  const int pctinc = (i == 0 || counts.at(i-1) == 0) ? -1 : round(100*counts.at(i)/double(counts.at(i-1)));
175  stringstream ss2;
176  ss2 << fixed << setprecision(1) << counts.at(i);
177  const string countstr = ss2.str(); ss2.str("");
178  ss2 << fixed << setprecision(3) << pcttot << "%";
179  const string pcttotstr = ss2.str(); ss2.str("");
180  ss2 << fixed << setprecision(3) << pctinc << "%";
181  const string pctincstr = ss2.str();
182  ss << "\n"
183  << setw(maxnamelen+5) << left << (i == 0 ? "" : "Pass "+cuts.at(i-1)) << " "
184  << setw(count0len) << right << countstr << " "
185  << setw(6) << right << (pcttot < 0 ? "- " : pcttotstr) << " "
186  << setw(6) << right << (pctinc < 0 ? "- " : pctincstr);
187  }
188  return ss.str();
189  }
190 
192  void print(std::ostream& os) const
193  {
194  os << str() << std::flush;
195  }
196 
197  string name;
198  size_t ncuts;
199  vector<string> cuts;
200  vector<double> counts;
201  size_t icurr;
202 
203  };
204 
205 
207  inline std::ostream& operator << (std::ostream& os, const Cutflow& cf)
208  {
209  return os << cf.str();
210  }
211 
212 
213 
215  struct Cutflows
216  {
217 
219  Cutflows() { }
220 
222  Cutflows(const vector<Cutflow>& cutflows) : cfs(cutflows) { }
223 
225  void addCutflow(const Cutflow& cf)
226  {
227  cfs.push_back(cf);
228  }
229 
231  void addCutflow(const string& cfname, const vector<string>& cutnames)
232  {
233  cfs.push_back(Cutflow(cfname, cutnames));
234  }
235 
237  Cutflow& operator [] (size_t i) { return cfs[i]; }
239  const Cutflow& operator [] (size_t i) const { return cfs[i]; }
240 
242  Cutflow& operator [] (const string& name)
243  {
244  for (Cutflow& cf : cfs)
245  if (cf.name == name) return cf;
246  // throw UserError("Requested cut-flow name '" + name + "' does not exist");
247  throw 0;
248  }
250  const Cutflow& operator [] (const string& name) const
251  {
252  for (const Cutflow& cf : cfs)
253  if (cf.name == name) return cf;
254  // throw UserError("Requested cut-flow name '" + name + "' does not exist");
255  throw 0;
256  }
257 
259  void fillinit(double weight=1.)
260  {
261  for (Cutflow& cf : cfs) cf.fillinit(weight);
262  }
263 
265  bool fill(size_t icut, bool cutresult=true, double weight=1.)
266  {
267  for (Cutflow& cf : cfs) cf.fill(icut, cutresult, weight);
268  return cutresult;
269  }
270 
277  bool fill(size_t icut, double weight)
278  {
279  return fill(icut, true, weight);
280  }
281 
285  bool fill(size_t icut, const vector<bool>& cutresults, double weight=1.)
286  {
287  bool rtn = true;
288  for (Cutflow& cf : cfs) rtn = cf.fill(icut, cutresults, weight);
289  return rtn;
290  }
291 
292 
294  bool fillall(const vector<bool>& cutresults, double weight=1.)
295  {
296  bool rtn = true;
297  for (Cutflow& cf : cfs) rtn = cf.fillall(cutresults, weight);
298  return rtn;
299  }
300 
304  bool fillnext(bool cutresult, double weight=1.) {
305  for (Cutflow& cf : cfs) cf.fillnext(cutresult, weight);
306  return cutresult;
307  }
308 
312  bool fillnext(double weight=1.)
313  {
314  for (Cutflow& cf : cfs) cf.fillnext(weight);
315  return true;
316  }
317 
321  bool fillnext(const vector<bool>& cutresults, double weight=1.)
322  {
323  bool rtn = true;
324  for (Cutflow& cf : cfs) rtn = cf.fillnext(cutresults, weight);
325  return rtn;
326  }
327 
328 
330  void scale(double factor)
331  {
332  for (Cutflow& cf : cfs) cf.scale(factor);
333  }
334 
337  void normalize(double norm, size_t icut=0)
338  {
339  for (Cutflow& cf : cfs) cf.normalize(norm, icut);
340  }
341 
343  string str() const
344  {
345  stringstream ss;
346  for (const Cutflow& cf : cfs)
347  ss << cf << "\n\n";
348  return ss.str();
349  }
350 
352  void print(std::ostream& os) const
353  {
354  os << str() << std::flush;
355  }
356 
357 
358  vector<Cutflow> cfs;
359 
360  };
361 
362 
364  inline std::ostream& operator << (std::ostream& os, const Cutflows& cfs)
365  {
366  return os << cfs.str();
367  }
368 
369 
370  }
371 }
vector< string > cuts
Definition: Cutflow.hpp:199
DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry double
A container for several Cutflow objects, with some convenient batch access.
Definition: Cutflow.hpp:215
void addCutflow(const string &cfname, const vector< string > &cutnames)
Append a newly constructed Cutflow to the list.
Definition: Cutflow.hpp:231
void print(std::ostream &os) const
Print string representation to a stream.
Definition: Cutflow.hpp:352
bool fillnext(bool cutresult, double weight=1.)
Fill the next post-cut counter.
Definition: Cutflow.hpp:304
string str() const
Create a string representation.
Definition: Cutflow.hpp:343
void fillinit(double weight=1.)
Fill the pre-cut counter.
Definition: Cutflow.hpp:50
Cutflow()
Default constructor.
Definition: Cutflow.hpp:42
std::ostream & operator<<(std::ostream &os, const Cutflow &cf)
Print a Cutflow to a stream.
Definition: Cutflow.hpp:207
bool fill(size_t icut, const vector< bool > &cutresults, double weight=1.)
Fill cut-state counters from an n-element results vector, starting at icut.
Definition: Cutflow.hpp:82
bool fillnext(const vector< bool > &cutresults, double weight=1.)
Fill the next cut-state counters from an n-element results vector.
Definition: Cutflow.hpp:123
bool fill(size_t icut, bool cutresult=true, double weight=1.)
Fill the {icut}&#39;th post-cut counter, starting at icut=1 for first cut.
Definition: Cutflow.hpp:59
STL namespace.
void scale(double factor)
Scale the cutflow weights by the given factor.
Definition: Cutflow.hpp:144
bool fillnext(bool cutresult, double weight=1.)
Fill the next post-cut counter.
Definition: Cutflow.hpp:107
Cutflows(const vector< Cutflow > &cutflows)
Populating constructor.
Definition: Cutflow.hpp:222
void fillinit(double weight=1.)
Fill the pre-cuts state counter for all contained {Cutflow}s.
Definition: Cutflow.hpp:259
bool fillall(const vector< bool > &cutresults, double weight=1.)
Fill all cut-state counters from an Ncut-element results vector, starting at icut=1.
Definition: Cutflow.hpp:96
bool filltail(const vector< bool > &cutresults, double weight=1.)
Fill the N trailing post-cut counters, when supplied with an N-element results vector.
Definition: Cutflow.hpp:138
Cutflows()
Do-nothing default constructor.
Definition: Cutflow.hpp:219
Util variables and functions for ColliderBit.
void addCutflow(const Cutflow &cf)
Append a provided Cutflow to the list.
Definition: Cutflow.hpp:225
bool fill(size_t icut, double weight)
Fill the {icut}&#39;th post-cut counter, starting at icut=1 for first cut (cutvalue=true overload) ...
Definition: Cutflow.hpp:74
void scale(double factor)
Scale the contained {Cutflow}s by the given factor.
Definition: Cutflow.hpp:330
bool fillnext(double weight=1.)
Fill the next post-cut counter, assuming a true result.
Definition: Cutflow.hpp:312
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
bool fillall(const vector< bool > &cutresults, double weight=1.)
Fill all cut-state counters from an Ncut-element results vector, starting at icut=1.
Definition: Cutflow.hpp:294
bool fill(size_t icut, double weight)
Fill the {icut}&#39;th post-cut counter, starting at icut=1 for first cut, with the same result for all {...
Definition: Cutflow.hpp:277
bool fillnext(const vector< bool > &cutresults, double weight=1.)
Fill the next cut-state counters from an n-element results vector.
Definition: Cutflow.hpp:321
void print(std::ostream &os) const
Print string representation to a stream.
Definition: Cutflow.hpp:192
void normalize(double norm, size_t icut=0)
Scale the cutflow weights so that the weight count after cut icut is norm.
Definition: Cutflow.hpp:150
string str() const
Create a string representation.
Definition: Cutflow.hpp:156
Cutflow(const string &cfname, const vector< string > &cutnames)
Proper constructor.
Definition: Cutflow.hpp:45
vector< Cutflow > cfs
Definition: Cutflow.hpp:358
void normalize(double norm, size_t icut=0)
Scale the cutflow weights so that all the weight counts after cut icut are norm.
Definition: Cutflow.hpp:337
bool fill(size_t icut, bool cutresult=true, double weight=1.)
Fill the {icut}&#39;th post-cut counter, starting at icut=1 for first cut, with the same result for all {...
Definition: Cutflow.hpp:265
bool fill(size_t icut, const vector< bool > &cutresults, double weight=1.)
Fill cut-state counters from an n-element results vector, starting at icut.
Definition: Cutflow.hpp:285
TODO: see if we can use this one:
Definition: Analysis.hpp:33
vector< double > counts
Definition: Cutflow.hpp:200
A tracker of numbers & fractions of events passing sequential cuts.
Definition: Cutflow.hpp:36
bool fillnext(double weight=1.)
Fill the next post-cut counter, assuming a true result.
Definition: Cutflow.hpp:115