Rivet 3.1.9
Cutflow.hh
1#ifndef RIVET_Cutflow_HH
2#define RIVET_Cutflow_HH
3
4#include "Rivet/Tools/Utils.hh"
5
6#include <sstream> // std::stringstream
7#include <iostream>
8#include <iomanip>
9
10namespace Rivet {
11
12
14 struct Cutflow {
15
20
22 Cutflow(const string& cfname, const vector<string>& cutnames)
23 : name(cfname), ncuts(cutnames.size()), cuts(cutnames), counts(ncuts+1, 0), icurr(0)
24 { }
25
26
28 void fillinit(double weight=1.) {
29 counts[0] += weight;
30 icurr = 1;
31 }
32
33
37 bool fill(size_t icut, bool cutresult=true, double weight=1.) {
38 if (icut == 0)
39 throw RangeError("Cut number must be greater than 0");
40 if (cutresult) counts.at(icut) += weight;
41 icurr = icut + 1;
42 return cutresult;
43 }
44
51 bool fill(size_t icut, double weight) {
52 return fill(icut, true, weight);
53 }
54
58 bool fill(size_t icut, const vector<bool>& cutresults, double weight=1.) {
59 if (icut == 0)
60 throw RangeError("Cut number must be greater than 0");
61 if (icut+cutresults.size() > ncuts+1)
62 throw RangeError("Number of filled cut results needs to match the Cutflow construction (in cutflow '"+name+"')");
63 bool rtn = true;
64 for (size_t i = 0; i < cutresults.size(); ++i)
65 if (!fill(icut+i, cutresults[i], weight)) { rtn = false; break; }
66 icurr = icut + cutresults.size();
67 return rtn;
68 }
69
70
74 bool fillnext(bool cutresult, double weight=1.) {
75 return fill(icurr, cutresult, weight);
76 }
77
81 bool fillnext(double weight=1.) {
82 return fill(icurr, true, weight);
83 }
84
88 bool fillnext(const vector<bool>& cutresults, double weight=1.) {
89 return fill(icurr, cutresults, weight);
90 }
91
92
96 bool fillall(const vector<bool>& cutresults, double weight=1.) {
97 if (cutresults.size() != ncuts)
98 throw RangeError("Number of filled cut results needs to match the Cutflow construction (in cutflow '"+name+"')");
99 // if (icut == 0) { fillinit(weight); icut = 1; }
100 return fill(1, cutresults, weight);
101 }
102
112 bool filltail(const vector<bool>& cutresults, double weight=1.) {
113 // if (cutresults.size() > ncuts)
114 // throw RangeError("Number of filled cut results needs to match the Cutflow construction");
115 // const size_t offset = counts.size() - cutresults.size();
116 // for (size_t i = 0; i < cutresults.size(); ++i) {
117 // if (cutresults[i]) counts.at(offset+i) += weight; else break;
118 // }
119 // return all(cutresults);
120 return fill(ncuts+1-cutresults.size(), cutresults, weight);
121 }
122
123
125 void scale(double factor) {
126 for (double& x : counts) x *= factor;
127 }
128
130 void normalize(double norm, size_t icut=0) {
131 scale(norm/counts.at(icut));
132 }
133
134
136 string str() const {
137 using namespace std;
138 stringstream ss;
139 ss << fixed << std::setprecision(1) << counts.front();
140 const size_t count0len = ss.str().length();
141 ss.str("");
142 ss << name << " cut-flow:\n";
143 size_t maxnamelen = 0;
144 for (const string& t : cuts)
145 maxnamelen = max(t.length(), maxnamelen);
146 ss << setw(maxnamelen+5) << "" << " "
147 << setw(count0len) << right << "Count" << " "
148 << setw(6) << right << "A_cumu" << " "
149 << setw(6) << right << "A_incr";
150 for (size_t i = 0; i <= ncuts; ++i) {
151 const int pcttot = (counts.front() == 0) ? -1 : round(100*counts.at(i)/double(counts.front()));
152 const int pctinc = (i == 0 || counts.at(i-1) == 0) ? -1 : round(100*counts.at(i)/double(counts.at(i-1)));
153 stringstream ss2;
154 ss2 << fixed << setprecision(1) << counts.at(i);
155 const string countstr = ss2.str(); ss2.str("");
156 ss2 << fixed << setprecision(3) << pcttot << "%";
157 const string pcttotstr = ss2.str(); ss2.str("");
158 ss2 << fixed << setprecision(3) << pctinc << "%";
159 const string pctincstr = ss2.str();
160 ss << "\n"
161 << setw(maxnamelen+5) << left << (i == 0 ? "" : "Pass "+cuts.at(i-1)) << " "
162 << setw(count0len) << right << countstr << " "
163 << setw(6) << right << (pcttot < 0 ? "- " : pcttotstr) << " "
164 << setw(6) << right << (pctinc < 0 ? "- " : pctincstr);
165 }
166 return ss.str();
167 }
168
170 void print(std::ostream& os) const {
171 os << str() << std::flush;
172 }
173
174 string name;
175 size_t ncuts;
176 vector<string> cuts;
177 vector<double> counts;
178 size_t icurr;
179
180 };
181
182
184 inline std::ostream& operator << (std::ostream& os, const Cutflow& cf) {
185 return os << cf.str();
186 }
187
188
189
191 struct Cutflows {
192
195
197 Cutflows(const vector<Cutflow>& cutflows) : cfs(cutflows) { }
198
199
201 void addCutflow(const Cutflow& cf) {
202 cfs.push_back(cf);
203 }
204
206 void addCutflow(const string& cfname, const vector<string>& cutnames) {
207 cfs.push_back(Cutflow(cfname, cutnames));
208 }
209
210
212 Cutflow& operator [] (size_t i) { return cfs[i]; }
214 const Cutflow& operator [] (size_t i) const { return cfs[i]; }
215
217 Cutflow& operator [] (const string& name) {
218 for (Cutflow& cf : cfs)
219 if (cf.name == name) return cf;
220 throw UserError("Requested cut-flow name '" + name + "' does not exist");
221 }
223 const Cutflow& operator [] (const string& name) const {
224 for (const Cutflow& cf : cfs)
225 if (cf.name == name) return cf;
226 throw UserError("Requested cut-flow name '" + name + "' does not exist");
227 }
228
229
231 void fillinit(double weight=1.) {
232 for (Cutflow& cf : cfs) cf.fillinit(weight);
233 }
234
235
237 bool fill(size_t icut, bool cutresult=true, double weight=1.) {
238 for (Cutflow& cf : cfs) cf.fill(icut, cutresult, weight);
239 return cutresult;
240 }
241
248 bool fill(size_t icut, double weight) {
249 return fill(icut, true, weight);
250 }
251
255 bool fill(size_t icut, const vector<bool>& cutresults, double weight=1.) {
256 bool rtn;
257 for (Cutflow& cf : cfs) rtn = cf.fill(icut, cutresults, weight);
258 return rtn;
259 }
260
261
265 bool fillnext(bool cutresult, double weight=1.) {
266 for (Cutflow& cf : cfs) cf.fillnext(cutresult, weight);
267 return cutresult;
268 }
269
273 bool fillnext(double weight=1.) {
274 for (Cutflow& cf : cfs) cf.fillnext(weight);
275 return true;
276 }
277
281 bool fillnext(const vector<bool>& cutresults, double weight=1.) {
282 bool rtn;
283 for (Cutflow& cf : cfs) rtn = cf.fillnext(cutresults, weight);
284 return rtn;
285 }
286
287
291 bool fillall(const vector<bool>& cutresults, double weight=1.) {
292 bool rtn;
293 for (Cutflow& cf : cfs) rtn = cf.fillall(cutresults, weight);
294 return rtn;
295 }
296
297
299 void scale(double factor) {
300 for (Cutflow& cf : cfs) cf.scale(factor);
301 }
302
305 void normalize(double norm, size_t icut=0) {
306 for (Cutflow& cf : cfs) cf.normalize(norm, icut);
307 }
308
309
311 string str() const {
312 std::stringstream ss;
313 for (const Cutflow& cf : cfs)
314 ss << cf << "\n\n";
315 return ss.str();
316 }
317
319 void print(std::ostream& os) const {
320 os << str() << std::flush;
321 }
322
323
324 vector<Cutflow> cfs;
325
326 };
327
328
330 inline std::ostream& operator << (std::ostream& os, const Cutflows& cfs) {
331 return os << cfs.str();
332 }
333
334
335}
336
337#endif
Definition MC_Cent_pPb.hh:10
std::ostream & operator<<(std::ostream &os, const AnalysisInfo &ai)
Stream an AnalysisInfo as a text description.
Definition AnalysisInfo.hh:368
std::enable_if< std::is_arithmetic< N1 >::value &&std::is_arithmetic< N2 >::value, typenamestd::common_type< N1, N2 >::type >::type max(N1 a, N2 b)
Get the maximum of two numbers.
Definition MathUtils.hh:111
STL namespace.
A tracker of numbers & fractions of events passing sequential cuts.
Definition Cutflow.hh:14
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.hh:96
bool fillnext(bool cutresult, double weight=1.)
Fill the next post-cut counter.
Definition Cutflow.hh:74
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.hh:58
bool fillnext(double weight=1.)
Fill the next post-cut counter, assuming a true result.
Definition Cutflow.hh:81
void scale(double factor)
Scale the cutflow weights by the given factor.
Definition Cutflow.hh:125
Cutflow()
Default constructor.
Definition Cutflow.hh:19
string str() const
Create a string representation.
Definition Cutflow.hh:136
void fillinit(double weight=1.)
Fill the pre-cut counter.
Definition Cutflow.hh:28
bool fill(size_t icut, bool cutresult=true, double weight=1.)
Fill the {icut}'th post-cut counter, starting at icut=1 for first cut.
Definition Cutflow.hh:37
bool fill(size_t icut, double weight)
Fill the {icut}'th post-cut counter, starting at icut=1 for first cut (cutvalue=true overload)
Definition Cutflow.hh:51
Cutflow(const string &cfname, const vector< string > &cutnames)
Proper constructor.
Definition Cutflow.hh:22
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:130
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:112
void print(std::ostream &os) const
Print string representation to a stream.
Definition Cutflow.hh:170
bool fillnext(const vector< bool > &cutresults, double weight=1.)
Fill the next cut-state counters from an n-element results vector.
Definition Cutflow.hh:88
A container for several Cutflow objects, with some convenient batch access.
Definition Cutflow.hh:191
string str() const
Create a string representation.
Definition Cutflow.hh:311
bool fillnext(bool cutresult, double weight=1.)
Fill the next post-cut counter.
Definition Cutflow.hh:265
Cutflow & operator[](size_t i)
Access the i'th Cutflow.
Definition Cutflow.hh:212
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.hh:255
Cutflows()
Do-nothing default constructor.
Definition Cutflow.hh:194
void addCutflow(const string &cfname, const vector< string > &cutnames)
Append a newly constructed Cutflow to the list.
Definition Cutflow.hh:206
void addCutflow(const Cutflow &cf)
Append a provided Cutflow to the list.
Definition Cutflow.hh:201
bool fill(size_t icut, bool cutresult=true, double weight=1.)
Fill the {icut}'th post-cut counter, starting at icut=1 for first cut, with the same result for all {...
Definition Cutflow.hh:237
void fillinit(double weight=1.)
Fill the pre-cuts state counter for all contained {Cutflow}s.
Definition Cutflow.hh:231
bool fillnext(double weight=1.)
Fill the next post-cut counter, assuming a true result.
Definition Cutflow.hh:273
void print(std::ostream &os) const
Print string representation to a stream.
Definition Cutflow.hh:319
void scale(double factor)
Scale the contained {Cutflow}s by the given factor.
Definition Cutflow.hh:299
void normalize(double norm, size_t icut=0)
Definition Cutflow.hh:305
Cutflows(const vector< Cutflow > &cutflows)
Populating constructor.
Definition Cutflow.hh:197
bool fillnext(const vector< bool > &cutresults, double weight=1.)
Fill the next cut-state counters from an n-element results vector.
Definition Cutflow.hh:281
bool fill(size_t icut, double weight)
Fill the {icut}'th post-cut counter, starting at icut=1 for first cut, with the same result for all {...
Definition Cutflow.hh:248
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.hh:291
Error for e.g. use of invalid bin ranges.
Definition Exceptions.hh:22
Error specialisation for where the problem is between the chair and the computer.
Definition Exceptions.hh:55