2#ifndef RIVET_PERCENTILEPROJECTION_HH
3#define RIVET_PERCENTILEPROJECTION_HH
5#include "Rivet/Projections/SingleValueProjection.hh"
6#include "Rivet/Tools/RivetYODA.hh"
11 enum class PercentileOrder { INCREASING, DECREASING };
22 using SingleValueProjection::operator =;
30 PercentileOrder pctorder=PercentileOrder::DECREASING)
32 _increasing(pctorder == PercentileOrder::INCREASING)
34 setName(
"PercentileProjection");
37 MSG_DEBUG(
"Constructing PercentileProjection from " << calhist.path());
38 _calhist = calhist.path();
39 int N = calhist.numBins();
40 double sum = calhist.sumW();
43 for (
int i = 0; i <= N; ++i) {
44 acc += calhist.bin(i).sumW();
45 _table.insert(make_pair(calhist.bin(i).xMax(), 100.0*acc/
sum));
50 for (
int i = N+1; i > 0; --i) {
51 acc += calhist.bin(i).sumW();
52 _table.insert(make_pair(calhist.bin(i).xMin(), 100.0*acc/
sum));
55 if (
getLog().isActive(Log::DEBUG)) {
56 MSG_DEBUG(
"Mapping from observable to percentile:");
57 for (
auto p : _table) {
58 std::cout << std::setw(16) << p.first <<
" -> "
59 << std::setw(16) << p.second <<
"%" << std::endl;
60 if (not _increasing and p.second <= 0)
break;
61 if (_increasing and p.second >= 100)
break;
71 PercentileOrder pctorder=PercentileOrder::DECREASING)
73 _increasing(pctorder == PercentileOrder::INCREASING)
78 MSG_DEBUG(
"Constructing PercentileProjection from " << calest.path());
79 _calhist = calest.path();
80 int N = calest.numBins();
82 for (
const auto &b : calest.bins() )
sum += b.val();
86 _table.insert(make_pair(calest.bin(1).xMin(), 100.0*acc/
sum));
87 for (
int i = 0; i < N; ++i ) {
88 acc += calest.bin(i+1).val();
89 _table.insert(make_pair(calest.bin(i+1).xMax(), 100.0*acc/
sum));
93 _table.insert(make_pair(calest.bin(N).xMax(), 100.0*acc/
sum));
94 for (
int i = N - 1; i >= 0; --i) {
95 acc += calest.bin(i+1).val();
96 _table.insert(make_pair(calest.bin(i+1).xMin(), 100.0*acc/
sum));
102 RIVET_DEFAULT_PROJ_CLONE(PercentileProjection);
105 using Projection::operator =;
115 if ( _table.empty() )
return;
116 auto& pobs = apply<SingleValueProjection>(e,
"OBSERVABLE");
118 double pcnt = lookup(obs);
120 MSG_DEBUG(
"Observable(" << pobs.name() <<
")="
121 << std::setw(16) << obs
122 <<
"-> Percentile=" << std::setw(16) << pcnt <<
"%");
129 cmp(_increasing, pp._increasing) ||
130 cmp(_calhist, pp._calhist);
137 double lookup(
double obs)
const {
138 auto low = _table.upper_bound(obs);
139 if ( low == _table.end() )
return _increasing? 100.0: 0.0;
140 if ( low == _table.begin() )
return _increasing? 0.0: 100.0;
142 return low->second + (obs - low->first)*(high->second - low->second)/
143 (high->first - low->first);
151 map<double,double> _table;
Representation of a HepMC event, and enabler of Projection caching.
Definition Event.hh:22
class for projections that reports the percentile for a given SingleValueProjection when initialized ...
Definition PercentileProjection.hh:19
void project(const Event &e)
Definition PercentileProjection.hh:113
CmpState compare(const Projection &p) const
Definition PercentileProjection.hh:126
PercentileProjection(const SingleValueProjection &sv, const Histo1D &calhist, PercentileOrder pctorder=PercentileOrder::DECREASING)
Definition PercentileProjection.hh:29
const PROJ & declare(const PROJ &proj, const std::string &name) const
Register a contained projection (user-facing version)
Definition ProjectionApplier.hh:175
Base class for all Rivet projections.
Definition Projection.hh:29
void setName(const std::string &name)
Used by derived classes to set their name.
Definition Projection.hh:148
Log & getLog() const
Get a Log object based on the getName() property of the calling projection object.
Definition Projection.hh:142
Cmp< Projection > mkNamedPCmp(const Projection &otherparent, const std::string &pname) const
Base class for projections returning a single floating point value.
Definition SingleValueProjection.hh:17
void setValue(double v)
Set the value.
Definition SingleValueProjection.hh:48
void clear()
Unset the value.
Definition SingleValueProjection.hh:54
#define MSG_DEBUG(x)
Debug messaging, not enabled by default, using MSG_LVL.
Definition Logging.hh:182
Definition MC_CENT_PPB_Projections.hh:10
T sum(const DressedLeptons &c, FN &&fn, const T &start=T())
Generic sum function, adding fn(x) for all x in container c, starting with start.
Definition DressedLepton.hh:60
Cmp< T > cmp(const T &t1, const T &t2)
Global helper function for easy creation of Cmp objects.
Definition Cmp.hh:255