rivet is hosted by Hepforge, IPPP Durham
Rivet  2.7.0
Cutflow.hh
1 #ifndef RIVET_Cutflow_HH
2 #define RIVET_Cutflow_HH
3 
4 #include "Rivet/Tools/Utils.hh"
5 
6 namespace Rivet {
7 
8 
10  struct Cutflow {
11 
15  Cutflow() {}
16 
18  Cutflow(const string& cfname, const vector<string>& cutnames)
19  : name(cfname), ncuts(cutnames.size()), cuts(cutnames), counts(ncuts+1, 0)
20  { }
21 
23  void fillinit(double weight=1.) {
24  counts.front() += weight;
25  }
26 
30  bool fill(size_t icut, bool cutresult=true, double weight=1.) {
31  if (cutresult) counts.at(icut) += weight;
32  return cutresult;
33  }
34 
41  bool fill(size_t icut, double weight) {
42  return fill(icut, true, weight);
43  }
44 
53  bool fill(const vector<bool>& cutresults, double weight=1.) {
54  if (cutresults.size() != ncuts)
55  throw RangeError("Number of filled cut results needs to match the Cutflow construction");
56  counts.front() += 1;
57  for (size_t i = 0; i < ncuts; ++i) {
58  if (cutresults[i]) counts.at(i+1) += weight; else break;
59  }
60  return all(cutresults);
61  }
62 
64 
66 
74  bool filltail(const vector<bool>& cutresults, double weight=1.) {
75  if (cutresults.size() > ncuts)
76  throw RangeError("Number of filled cut results needs to match the Cutflow construction");
77  const size_t offset = counts.size() - cutresults.size();
78  for (size_t i = 0; i < cutresults.size(); ++i) {
79  if (cutresults[i]) counts.at(offset+i) += weight; else break;
80  }
81  return all(cutresults);
82  }
83 
85  void scale(double factor) {
86  for (double& x : counts) x *= factor;
87  }
88 
90  void normalize(double norm, size_t icut=0) {
91  scale(norm/counts.at(icut));
92  }
93 
95  string str() const {
96  stringstream ss;
97  ss << fixed << setprecision(1) << counts.front();
98  const size_t count0len = ss.str().length();
99  ss.str("");
100  ss << name << " cut-flow:\n";
101  size_t maxnamelen = 0;
102  for (const string& t : cuts)
103  maxnamelen = max(t.length(), maxnamelen);
104  ss << setw(maxnamelen+5) << "" << " "
105  << setw(count0len) << right << "Count" << " "
106  << setw(6) << right << "A_cumu" << " "
107  << setw(6) << right << "A_incr";
108  for (size_t i = 0; i <= ncuts; ++i) {
109  const int pcttot = (counts.front() == 0) ? -1 : round(100*counts.at(i)/double(counts.front()));
110  const int pctinc = (i == 0 || counts.at(i-1) == 0) ? -1 : round(100*counts.at(i)/double(counts.at(i-1)));
111  stringstream ss2;
112  ss2 << fixed << setprecision(1) << counts.at(i);
113  const string countstr = ss2.str(); ss2.str("");
114  ss2 << fixed << setprecision(3) << pcttot << "%";
115  const string pcttotstr = ss2.str(); ss2.str("");
116  ss2 << fixed << setprecision(3) << pctinc << "%";
117  const string pctincstr = ss2.str();
118  ss << "\n"
119  << setw(maxnamelen+5) << left << (i == 0 ? "" : "Pass "+cuts.at(i-1)) << " "
120  << setw(count0len) << right << countstr << " "
121  << setw(6) << right << (pcttot < 0 ? "- " : pcttotstr) << " "
122  << setw(6) << right << (pctinc < 0 ? "- " : pctincstr);
123  }
124  return ss.str();
125  }
126 
128  void print(ostream& os) const {
129  os << str() << flush;
130  }
131 
132  string name;
133  size_t ncuts;
134  vector<string> cuts;
135  vector<double> counts;
136 
137  };
138 
139 
141  inline ostream& operator << (ostream& os, const Cutflow& cf) {
142  return os << cf.str();
143  }
144 
145 
146 
148  struct Cutflows {
149 
151  Cutflows() { }
152 
154  Cutflows(const vector<Cutflow>& cutflows) : cfs(cutflows) { }
155 
157  void addCutflow(const Cutflow& cf) {
158  cfs.push_back(cf);
159  }
160 
162  void addCutflow(const string& cfname, const vector<string>& cutnames) {
163  cfs.push_back(Cutflow(cfname, cutnames));
164  }
165 
167  Cutflow& operator [] (size_t i) { return cfs[i]; }
169  const Cutflow& operator [] (size_t i) const { return cfs[i]; }
170 
172  Cutflow& operator [] (const string& name) {
173  for (Cutflow& cf : cfs)
174  if (cf.name == name) return cf;
175  throw UserError("Requested cut-flow name '" + name + "' does not exist");
176  }
178  const Cutflow& operator [] (const string& name) const {
179  for (const Cutflow& cf : cfs)
180  if (cf.name == name) return cf;
181  throw UserError("Requested cut-flow name '" + name + "' does not exist");
182  }
183 
185  void fillinit(double weight=1.) {
186  for (Cutflow& cf : cfs) cf.fillinit(weight);
187  }
188 
190  bool fill(size_t icut, bool cutresult=true, double weight=1.) {
191  for (Cutflow& cf : cfs) cf.fill(icut, cutresult, weight);
192  return cutresult;
193  }
194 
201  bool fill(size_t icut, double weight) {
202  return fill(icut, true, weight);
203  }
204 
206 
208 
210  void scale(double factor) {
211  for (Cutflow& cf : cfs) cf.scale(factor);
212  }
213 
216  void normalize(double norm, size_t icut=0) {
217  for (Cutflow& cf : cfs) cf.normalize(norm, icut);
218  }
219 
221  string str() const {
222  stringstream ss;
223  for (const Cutflow& cf : cfs)
224  ss << cf << "\n\n";
225  return ss.str();
226  }
227 
229  void print(ostream& os) const {
230  os << str() << flush;
231  }
232 
233  vector<Cutflow> cfs;
234 
235  };
236 
238  inline ostream& operator << (ostream& os, const Cutflows& cfs) {
239  return os << cfs.str();
240  }
241 
242 
243 }
244 
245 #endif
Definition: ALICE_2010_I880049.cc:13
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.hh:190
void normalize(double norm, size_t icut=0)
Scale the cutflow weights so that the weight count after cut icut is norm.
Definition: Cutflow.hh:90
void scale(double factor)
Scale the cutflow weights by the given factor.
Definition: Cutflow.hh:85
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.hh:201
void fillinit(double weight=1.)
Fill the pre-cuts state counter for all contained {Cutflow}s.
Definition: Cutflow.hh:185
Error specialisation for where the problem is between the chair and the computer. ...
Definition: Exceptions.hh:55
void print(ostream &os) const
Print string representation to a stream.
Definition: Cutflow.hh:229
Cutflows()
Do-nothing default constructor.
Definition: Cutflow.hh:151
string str() const
Create a string representation.
Definition: Cutflow.hh:221
string str() const
Create a string representation.
Definition: Cutflow.hh:95
Cutflow(const string &cfname, const vector< string > &cutnames)
Proper constructor.
Definition: Cutflow.hh:18
Cutflows(const vector< Cutflow > &cutflows)
Populating constructor.
Definition: Cutflow.hh:154
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.hh:74
Cutflow()
Default constructor.
Definition: Cutflow.hh:15
void print(ostream &os) const
Print string representation to a stream.
Definition: Cutflow.hh:128
void scale(double factor)
Scale the contained {Cutflow}s by the given factor.
Definition: Cutflow.hh:210
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.hh:30
bool all(const CONTAINER &c)
Return true if x is true for all x in container c, otherwise false.
Definition: Utils.hh:301
double max(const vector< double > &in, double errval=DBL_NAN)
Find the maximum value in the vector.
Definition: Utils.hh:465
void addCutflow(const string &cfname, const vector< string > &cutnames)
Append a newly constructed Cutflow to the list.
Definition: Cutflow.hh:162
void addCutflow(const Cutflow &cf)
Append a provided Cutflow to the list.
Definition: Cutflow.hh:157
A tracker of numbers & fractions of events passing sequential cuts.
Definition: Cutflow.hh:10
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.hh:41
A container for several Cutflow objects, with some convenient batch access.
Definition: Cutflow.hh:148
bool fill(const vector< bool > &cutresults, double weight=1.)
Fill all cut-state counters from an Ncut-element results vector.
Definition: Cutflow.hh:53
void fillinit(double weight=1.)
Fill the pre-cut counter.
Definition: Cutflow.hh:23
Error for e.g. use of invalid bin ranges.
Definition: Exceptions.hh:22
void normalize(double norm, size_t icut=0)
Definition: Cutflow.hh:216