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 const Histo1D& calhist,
31 PercentileOrder pctorder = PercentileOrder::DECREASING)
32 : _calhist(
"EMPTY"), _increasing(pctorder == PercentileOrder::INCREASING) {
33 setName(
"PercentileProjection");
36 MSG_DEBUG(
"Constructing PercentileProjection from " << calhist.path());
37 _calhist = calhist.path();
38 int N = calhist.numBins();
39 double sum = calhist.sumW();
42 for (
int i = 0; i <= N; ++i) {
43 acc += calhist.bin(i).sumW();
44 _table.insert(make_pair(calhist.bin(i).xMax(), 100.0 * acc /
sum));
49 for (
int i = N + 1; i > 0; --i) {
50 acc += calhist.bin(i).sumW();
51 _table.insert(make_pair(calhist.bin(i).xMin(), 100.0 * acc /
sum));
54 if (
getLog().isActive(Log::DEBUG)) {
55 MSG_DEBUG(
"Mapping from observable to percentile:");
56 for (
auto p : _table) {
57 std::cout << std::setw(16) <<
p.first <<
" -> " << std::setw(16) <<
p.second <<
"%" << std::endl;
58 if (not _increasing and
p.second <= 0)
break;
59 if (_increasing and
p.second >= 100)
break;
69 const Estimate1D& calest,
70 PercentileOrder pctorder = PercentileOrder::DECREASING)
71 : _calhist(
"EMPTY"), _increasing(pctorder == PercentileOrder::INCREASING) {
75 MSG_DEBUG(
"Constructing PercentileProjection from " << calest.path());
76 _calhist = calest.path();
77 int N = calest.numBins();
79 for (
const auto& b : calest.bins())
sum += b.val();
83 _table.insert(make_pair(calest.bin(1).xMin(), 100.0 * acc /
sum));
84 for (
int i = 0; i < N; ++i) {
85 acc += calest.bin(i + 1).val();
86 _table.insert(make_pair(calest.bin(i + 1).xMax(), 100.0 * acc /
sum));
90 _table.insert(make_pair(calest.bin(N).xMax(), 100.0 * acc /
sum));
91 for (
int i = N - 1; i >= 0; --i) {
92 acc += calest.bin(i + 1).val();
93 _table.insert(make_pair(calest.bin(i + 1).xMin(), 100.0 * acc /
sum));
102 using Projection::operator=;
112 if (_table.empty())
return;
115 double pcnt = lookup(obs);
117 MSG_DEBUG(
"Observable(" << pobs.name() <<
")=" << std::setw(16) << obs
118 <<
"-> Percentile=" << std::setw(16) << pcnt <<
"%");
124 return mkNamedPCmp(
p,
"OBSERVABLE") ||
cmp(_increasing, pp._increasing) ||
cmp(_calhist, pp._calhist);
131 double lookup(
double obs)
const {
132 auto low = _table.upper_bound(obs);
133 if (low == _table.end())
return _increasing ? 100.0 : 0.0;
134 if (low == _table.begin())
return _increasing ? 0.0 : 100.0;
136 return low->second + (obs - low->first) * (high->second - low->second) / (high->first - low->first);
144 map<double, double> _table;
Representation of a HepMC event, and enabler of Projection caching.
Definition Event.hh:22
void project(const Event &e)
Definition PercentileProjection.hh:110
CmpState compare(const Projection &p) const
Definition PercentileProjection.hh:122
PercentileProjection(const SingleValueProjection &sv, const Histo1D &calhist, PercentileOrder pctorder=PercentileOrder::DECREASING)
Definition PercentileProjection.hh:29
std::enable_if_t< std::is_base_of< Projection, PROJ >::value, const PROJ & > apply(const Event &evt, const Projection &proj) const
Apply the supplied projection on event evt.
Definition ProjectionApplier.hh:124
const PROJ & declare(const PROJ &proj, const std::string &name) const
Register a contained projection (user-facing version).
Definition ProjectionApplier.hh:205
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:146
Log & getLog() const
Get a Log object based on the getName() property of the calling projection object.
Definition Projection.hh:140
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:49
SingleValueProjection()
The default constructor.
Definition SingleValueProjection.hh:21
void clear()
Unset the value.
Definition SingleValueProjection.hh:55
#define MSG_DEBUG(x)
Debug messaging, not enabled by default, using MSG_LVL.
Definition Logging.hh:195
double p(const ParticleBase &p)
Unbound function access to p.
Definition ParticleBaseUtils.hh:819
Definition LHCbCommon.hh:9
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:66
Cmp< T > cmp(const T &t1, const T &t2)
Global helper function for easy creation of Cmp objects.
Definition Cmp.hh:253