1#ifndef RIVET_Cutflow_HH
2#define RIVET_Cutflow_HH
4#include "Rivet/Tools/Utils.hh"
22 Cutflow(
const string& cfname,
const vector<string>& cutnames)
23 : name(cfname), ncuts(cutnames.size()), cuts(cutnames), counts(ncuts+1, 0), icurr(0)
37 bool fill(
size_t icut,
bool cutresult=
true,
double weight=1.) {
39 throw RangeError(
"Cut number must be greater than 0");
40 if (cutresult) counts.at(icut) += weight;
51 bool fill(
size_t icut,
double weight) {
52 return fill(icut,
true, weight);
58 bool fill(
size_t icut,
const vector<bool>& cutresults,
double weight=1.) {
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+
"')");
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();
74 bool fillnext(
bool cutresult,
double weight=1.) {
75 return fill(icurr, cutresult, weight);
82 return fill(icurr,
true, weight);
88 bool fillnext(
const vector<bool>& cutresults,
double weight=1.) {
89 return fill(icurr, cutresults, weight);
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+
"')");
100 return fill(1, cutresults, weight);
112 bool filltail(
const vector<bool>& cutresults,
double weight=1.) {
120 return fill(ncuts+1-cutresults.size(), cutresults, weight);
126 for (
double& x : counts) x *= factor;
131 scale(norm/counts.at(icut));
139 ss << fixed << std::setprecision(1) << counts.front();
140 const size_t count0len = ss.str().length();
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)));
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();
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);
170 void print(std::ostream& os)
const {
171 os <<
str() << std::flush;
177 vector<double> counts;
185 return os << cf.
str();
197 Cutflows(
const vector<Cutflow>& cutflows) : cfs(cutflows) { }
206 void addCutflow(
const string& cfname,
const vector<string>& cutnames) {
207 cfs.push_back(
Cutflow(cfname, cutnames));
219 if (cf.name == name)
return cf;
220 throw UserError(
"Requested cut-flow name '" + name +
"' does not exist");
225 if (cf.name == name)
return cf;
226 throw UserError(
"Requested cut-flow name '" + name +
"' does not exist");
232 for (
Cutflow& cf : cfs) cf.fillinit(weight);
237 bool fill(
size_t icut,
bool cutresult=
true,
double weight=1.) {
238 for (
Cutflow& cf : cfs) cf.fill(icut, cutresult, weight);
248 bool fill(
size_t icut,
double weight) {
249 return fill(icut,
true, weight);
255 bool fill(
size_t icut,
const vector<bool>& cutresults,
double weight=1.) {
257 for (
Cutflow& cf : cfs) rtn = cf.fill(icut, cutresults, weight);
266 for (
Cutflow& cf : cfs) cf.fillnext(cutresult, weight);
274 for (
Cutflow& cf : cfs) cf.fillnext(weight);
281 bool fillnext(
const vector<bool>& cutresults,
double weight=1.) {
283 for (
Cutflow& cf : cfs) rtn = cf.fillnext(cutresults, weight);
291 bool fillall(
const vector<bool>& cutresults,
double weight=1.) {
293 for (
Cutflow& cf : cfs) rtn = cf.fillall(cutresults, weight);
300 for (
Cutflow& cf : cfs) cf.scale(factor);
306 for (
Cutflow& cf : cfs) cf.normalize(norm, icut);
312 std::stringstream ss;
319 void print(std::ostream& os)
const {
320 os <<
str() << std::flush;
331 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: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
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