rivet is hosted by Hepforge, IPPP Durham
Rivet 3.1.6
Cutflow.hh
1#ifndef RIVET_Cutflow_HH
2#define RIVET_Cutflow_HH
3
4#include "Rivet/Tools/Utils.hh"
5
6namespace Rivet {
7
8
10 struct Cutflow {
11
16
18 Cutflow(const string& cfname, const vector<string>& cutnames)
19 : name(cfname), ncuts(cutnames.size()), cuts(cutnames), counts(ncuts+1, 0), icurr(0)
20 { }
21
22
24 void fillinit(double weight=1.) {
25 counts[0] += weight;
26 icurr = 1;
27 }
28
29
33 bool fill(size_t icut, bool cutresult=true, double weight=1.) {
34 if (icut == 0)
35 throw RangeError("Cut number must be greater than 0");
36 if (cutresult) counts.at(icut) += weight;
37 icurr = icut + 1;
38 return cutresult;
39 }
40
47 bool fill(size_t icut, double weight) {
48 return fill(icut, true, weight);
49 }
50
54 bool fill(size_t icut, const vector<bool>& cutresults, double weight=1.) {
55 if (icut == 0)
56 throw RangeError("Cut number must be greater than 0");
57 if (icut+cutresults.size() > ncuts+1)
58 throw RangeError("Number of filled cut results needs to match the Cutflow construction");
59 bool rtn = true;
60 for (size_t i = 0; i < cutresults.size(); ++i)
61 if (!fill(icut+i, cutresults[i], weight)) { rtn = false; break; }
62 icurr = icut + cutresults.size();
63 return rtn;
64 }
65
66
70 bool fillnext(bool cutresult, double weight=1.) {
71 return fill(icurr, cutresult, weight);
72 }
73
77 bool fillnext(double weight=1.) {
78 return fill(icurr, true, weight);
79 }
80
84 bool fillnext(const vector<bool>& cutresults, double weight=1.) {
85 return fill(icurr, cutresults, weight);
86 }
87
88
92 bool fillall(const vector<bool>& cutresults, double weight=1.) {
93 if (cutresults.size() != ncuts)
94 throw RangeError("Number of filled cut results needs to match the Cutflow construction");
95 // if (icut == 0) { fillinit(weight); icut = 1; }
96 return fill(1, cutresults, weight);
97 }
98
108 bool filltail(const vector<bool>& cutresults, double weight=1.) {
109 // if (cutresults.size() > ncuts)
110 // throw RangeError("Number of filled cut results needs to match the Cutflow construction");
111 // const size_t offset = counts.size() - cutresults.size();
112 // for (size_t i = 0; i < cutresults.size(); ++i) {
113 // if (cutresults[i]) counts.at(offset+i) += weight; else break;
114 // }
115 // return all(cutresults);
116 return fill(ncuts+1-cutresults.size(), cutresults, weight);
117 }
118
119
121 void scale(double factor) {
122 for (double& x : counts) x *= factor;
123 }
124
126 void normalize(double norm, size_t icut=0) {
127 scale(norm/counts.at(icut));
128 }
129
130
132 string str() const {
133 using namespace std;
134 stringstream ss;
135 ss << fixed << std::setprecision(1) << counts.front();
136 const size_t count0len = ss.str().length();
137 ss.str("");
138 ss << name << " cut-flow:\n";
139 size_t maxnamelen = 0;
140 for (const string& t : cuts)
141 maxnamelen = max(t.length(), maxnamelen);
142 ss << setw(maxnamelen+5) << "" << " "
143 << setw(count0len) << right << "Count" << " "
144 << setw(6) << right << "A_cumu" << " "
145 << setw(6) << right << "A_incr";
146 for (size_t i = 0; i <= ncuts; ++i) {
147 const int pcttot = (counts.front() == 0) ? -1 : round(100*counts.at(i)/double(counts.front()));
148 const int pctinc = (i == 0 || counts.at(i-1) == 0) ? -1 : round(100*counts.at(i)/double(counts.at(i-1)));
149 stringstream ss2;
150 ss2 << fixed << setprecision(1) << counts.at(i);
151 const string countstr = ss2.str(); ss2.str("");
152 ss2 << fixed << setprecision(3) << pcttot << "%";
153 const string pcttotstr = ss2.str(); ss2.str("");
154 ss2 << fixed << setprecision(3) << pctinc << "%";
155 const string pctincstr = ss2.str();
156 ss << "\n"
157 << setw(maxnamelen+5) << left << (i == 0 ? "" : "Pass "+cuts.at(i-1)) << " "
158 << setw(count0len) << right << countstr << " "
159 << setw(6) << right << (pcttot < 0 ? "- " : pcttotstr) << " "
160 << setw(6) << right << (pctinc < 0 ? "- " : pctincstr);
161 }
162 return ss.str();
163 }
164
166 void print(std::ostream& os) const {
167 os << str() << std::flush;
168 }
169
170 string name;
171 size_t ncuts;
172 vector<string> cuts;
173 vector<double> counts;
174 size_t icurr;
175
176 };
177
178
180 inline std::ostream& operator << (std::ostream& os, const Cutflow& cf) {
181 return os << cf.str();
182 }
183
184
185
187 struct Cutflows {
188
191
193 Cutflows(const vector<Cutflow>& cutflows) : cfs(cutflows) { }
194
195
197 void addCutflow(const Cutflow& cf) {
198 cfs.push_back(cf);
199 }
200
202 void addCutflow(const string& cfname, const vector<string>& cutnames) {
203 cfs.push_back(Cutflow(cfname, cutnames));
204 }
205
206
208 Cutflow& operator [] (size_t i) { return cfs[i]; }
210 const Cutflow& operator [] (size_t i) const { return cfs[i]; }
211
213 Cutflow& operator [] (const string& name) {
214 for (Cutflow& cf : cfs)
215 if (cf.name == name) return cf;
216 throw UserError("Requested cut-flow name '" + name + "' does not exist");
217 }
219 const Cutflow& operator [] (const string& name) const {
220 for (const Cutflow& cf : cfs)
221 if (cf.name == name) return cf;
222 throw UserError("Requested cut-flow name '" + name + "' does not exist");
223 }
224
225
227 void fillinit(double weight=1.) {
228 for (Cutflow& cf : cfs) cf.fillinit(weight);
229 }
230
231
233 bool fill(size_t icut, bool cutresult=true, double weight=1.) {
234 for (Cutflow& cf : cfs) cf.fill(icut, cutresult, weight);
235 return cutresult;
236 }
237
244 bool fill(size_t icut, double weight) {
245 return fill(icut, true, weight);
246 }
247
251 bool fill(size_t icut, const vector<bool>& cutresults, double weight=1.) {
252 bool rtn;
253 for (Cutflow& cf : cfs) rtn = cf.fill(icut, cutresults, weight);
254 return rtn;
255 }
256
257
261 bool fillnext(bool cutresult, double weight=1.) {
262 for (Cutflow& cf : cfs) cf.fillnext(cutresult, weight);
263 return cutresult;
264 }
265
269 bool fillnext(double weight=1.) {
270 for (Cutflow& cf : cfs) cf.fillnext(weight);
271 return true;
272 }
273
277 bool fillnext(const vector<bool>& cutresults, double weight=1.) {
278 bool rtn;
279 for (Cutflow& cf : cfs) rtn = cf.fillnext(cutresults, weight);
280 return rtn;
281 }
282
283
287 bool fillall(const vector<bool>& cutresults, double weight=1.) {
288 bool rtn;
289 for (Cutflow& cf : cfs) rtn = cf.fillall(cutresults, weight);
290 return rtn;
291 }
292
293
295 void scale(double factor) {
296 for (Cutflow& cf : cfs) cf.scale(factor);
297 }
298
301 void normalize(double norm, size_t icut=0) {
302 for (Cutflow& cf : cfs) cf.normalize(norm, icut);
303 }
304
305
307 string str() const {
308 stringstream ss;
309 for (const Cutflow& cf : cfs)
310 ss << cf << "\n\n";
311 return ss.str();
312 }
313
315 void print(std::ostream& os) const {
316 os << str() << std::flush;
317 }
318
319
320 vector<Cutflow> cfs;
321
322 };
323
324
326 inline std::ostream& operator << (std::ostream& os, const Cutflows& cfs) {
327 return os << cfs.str();
328 }
329
330
331}
332
333#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:362
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:10
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:92
bool fillnext(bool cutresult, double weight=1.)
Fill the next post-cut counter.
Definition: Cutflow.hh:70
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:54
bool fillnext(double weight=1.)
Fill the next post-cut counter, assuming a true result.
Definition: Cutflow.hh:77
void scale(double factor)
Scale the cutflow weights by the given factor.
Definition: Cutflow.hh:121
Cutflow()
Default constructor.
Definition: Cutflow.hh:15
string str() const
Create a string representation.
Definition: Cutflow.hh:132
void fillinit(double weight=1.)
Fill the pre-cut counter.
Definition: Cutflow.hh:24
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:33
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:47
Cutflow(const string &cfname, const vector< string > &cutnames)
Proper constructor.
Definition: Cutflow.hh:18
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:126
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:108
void print(std::ostream &os) const
Print string representation to a stream.
Definition: Cutflow.hh:166
bool fillnext(const vector< bool > &cutresults, double weight=1.)
Fill the next cut-state counters from an n-element results vector.
Definition: Cutflow.hh:84
A container for several Cutflow objects, with some convenient batch access.
Definition: Cutflow.hh:187
string str() const
Create a string representation.
Definition: Cutflow.hh:307
bool fillnext(bool cutresult, double weight=1.)
Fill the next post-cut counter.
Definition: Cutflow.hh:261
Cutflow & operator[](size_t i)
Access the i'th Cutflow.
Definition: Cutflow.hh:208
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:251
Cutflows()
Do-nothing default constructor.
Definition: Cutflow.hh:190
void addCutflow(const string &cfname, const vector< string > &cutnames)
Append a newly constructed Cutflow to the list.
Definition: Cutflow.hh:202
void addCutflow(const Cutflow &cf)
Append a provided Cutflow to the list.
Definition: Cutflow.hh:197
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:233
void fillinit(double weight=1.)
Fill the pre-cuts state counter for all contained {Cutflow}s.
Definition: Cutflow.hh:227
bool fillnext(double weight=1.)
Fill the next post-cut counter, assuming a true result.
Definition: Cutflow.hh:269
void print(std::ostream &os) const
Print string representation to a stream.
Definition: Cutflow.hh:315
void scale(double factor)
Scale the contained {Cutflow}s by the given factor.
Definition: Cutflow.hh:295
void normalize(double norm, size_t icut=0)
Definition: Cutflow.hh:301
Cutflows(const vector< Cutflow > &cutflows)
Populating constructor.
Definition: Cutflow.hh:193
bool fillnext(const vector< bool > &cutresults, double weight=1.)
Fill the next cut-state counters from an n-element results vector.
Definition: Cutflow.hh:277
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:244
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:287
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