1#ifndef RIVET_Cutflow_HH
2#define RIVET_Cutflow_HH
4#include "Rivet/Tools/Utils.hh"
18 Cutflow(
const string& cfname,
const vector<string>& cutnames)
19 : name(cfname), ncuts(cutnames.size()), cuts(cutnames), counts(ncuts+1, 0), icurr(0)
33 bool fill(
size_t icut,
bool cutresult=
true,
double weight=1.) {
35 throw RangeError(
"Cut number must be greater than 0");
36 if (cutresult) counts.at(icut) += weight;
47 bool fill(
size_t icut,
double weight) {
48 return fill(icut,
true, weight);
54 bool fill(
size_t icut,
const vector<bool>& cutresults,
double weight=1.) {
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");
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();
70 bool fillnext(
bool cutresult,
double weight=1.) {
71 return fill(icurr, cutresult, weight);
78 return fill(icurr,
true, weight);
84 bool fillnext(
const vector<bool>& cutresults,
double weight=1.) {
85 return fill(icurr, cutresults, weight);
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");
96 return fill(1, cutresults, weight);
108 bool filltail(
const vector<bool>& cutresults,
double weight=1.) {
116 return fill(ncuts+1-cutresults.size(), cutresults, weight);
122 for (
double& x : counts) x *= factor;
127 scale(norm/counts.at(icut));
135 ss << fixed << std::setprecision(1) << counts.front();
136 const size_t count0len = ss.str().length();
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)));
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();
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);
166 void print(std::ostream& os)
const {
167 os <<
str() << std::flush;
173 vector<double> counts;
181 return os << cf.
str();
193 Cutflows(
const vector<Cutflow>& cutflows) : cfs(cutflows) { }
202 void addCutflow(
const string& cfname,
const vector<string>& cutnames) {
203 cfs.push_back(
Cutflow(cfname, cutnames));
215 if (cf.name == name)
return cf;
216 throw UserError(
"Requested cut-flow name '" + name +
"' does not exist");
221 if (cf.name == name)
return cf;
222 throw UserError(
"Requested cut-flow name '" + name +
"' does not exist");
228 for (
Cutflow& cf : cfs) cf.fillinit(weight);
233 bool fill(
size_t icut,
bool cutresult=
true,
double weight=1.) {
234 for (
Cutflow& cf : cfs) cf.fill(icut, cutresult, weight);
244 bool fill(
size_t icut,
double weight) {
245 return fill(icut,
true, weight);
251 bool fill(
size_t icut,
const vector<bool>& cutresults,
double weight=1.) {
253 for (
Cutflow& cf : cfs) rtn = cf.fill(icut, cutresults, weight);
262 for (
Cutflow& cf : cfs) cf.fillnext(cutresult, weight);
270 for (
Cutflow& cf : cfs) cf.fillnext(weight);
277 bool fillnext(
const vector<bool>& cutresults,
double weight=1.) {
279 for (
Cutflow& cf : cfs) rtn = cf.fillnext(cutresults, weight);
287 bool fillall(
const vector<bool>& cutresults,
double weight=1.) {
289 for (
Cutflow& cf : cfs) rtn = cf.fillall(cutresults, weight);
296 for (
Cutflow& cf : cfs) cf.scale(factor);
302 for (
Cutflow& cf : cfs) cf.normalize(norm, icut);
315 void print(std::ostream& os)
const {
316 os <<
str() << std::flush;
327 return os << cfs.
str();
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
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