Rivet API documentation

Rivet 4.1.3
PercentileProjection.hh
1// -*- C++ -*-
2#ifndef RIVET_PERCENTILEPROJECTION_HH
3#define RIVET_PERCENTILEPROJECTION_HH
4
5#include "Rivet/Projections/SingleValueProjection.hh"
6#include "Rivet/Tools/RivetYODA.hh"
7#include <map>
8
9namespace Rivet {
10
11 enum class PercentileOrder { INCREASING, DECREASING };
12
13
20 public:
21
22 using SingleValueProjection::operator=;
23
30 const Histo1D& calhist,
31 PercentileOrder pctorder = PercentileOrder::DECREASING)
32 : _calhist("EMPTY"), _increasing(pctorder == PercentileOrder::INCREASING) {
33 setName("PercentileProjection");
34 declare(sv, "OBSERVABLE");
35 //if ( !calhist ) return;
36 MSG_DEBUG("Constructing PercentileProjection from " << calhist.path());
37 _calhist = calhist.path();
38 int N = calhist.numBins();
39 double sum = calhist.sumW();
40 if (_increasing) {
41 double acc = 0.0;
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));
45 }
46 }
47 else {
48 double acc = 0.0;
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));
52 }
53 }
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;
60 }
61 }
62 }
63
64
65 // Constructor taking a SingleValueProjection and a calibration
66 // histogram. If increasing it means that low values corresponds to
67 // lower percentiles.
69 const Estimate1D& calest,
70 PercentileOrder pctorder = PercentileOrder::DECREASING)
71 : _calhist("EMPTY"), _increasing(pctorder == PercentileOrder::INCREASING) {
72 declare(sv, "OBSERVABLE");
73
74 //if ( !calest ) return;
75 MSG_DEBUG("Constructing PercentileProjection from " << calest.path());
76 _calhist = calest.path();
77 int N = calest.numBins();
78 double sum = 0.0;
79 for (const auto& b : calest.bins()) sum += b.val();
80
81 double acc = 0.0;
82 if (_increasing) {
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));
87 }
88 }
89 else {
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));
94 }
95 }
96 }
97
98
99 RIVET_DEFAULT_PROJ_CLONE(PercentileProjection);
100
102 using Projection::operator=;
103
104
105 // The projection function takes the assigned SingeValueProjection
106 // and sets the value of this projection to the corresponding
107 // percentile. If no calibration has been provided, -1 will be
108 // returned. If values are outside of the calibration histogram, 0
109 // or 100 will be returned.
110 void project(const Event& e) {
111 clear();
112 if (_table.empty()) return;
113 auto& pobs = apply<SingleValueProjection>(e, "OBSERVABLE");
114 double obs = pobs();
115 double pcnt = lookup(obs);
116 if (pcnt >= 0.0) setValue(pcnt);
117 MSG_DEBUG("Observable(" << pobs.name() << ")=" << std::setw(16) << obs
118 << "-> Percentile=" << std::setw(16) << pcnt << "%");
119 }
120
121 // Standard comparison function.
122 CmpState compare(const Projection& p) const {
123 const PercentileProjection pp = dynamic_cast<const PercentileProjection&>(p);
124 return mkNamedPCmp(p, "OBSERVABLE") || cmp(_increasing, pp._increasing) || cmp(_calhist, pp._calhist);
125 }
126
127
128 protected:
129
130 // The (interpolated) lookup table
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;
135 auto high = low--;
136 return low->second + (obs - low->first) * (high->second - low->second) / (high->first - low->first);
137 }
138
139 // Astring identifying the calibration histogram.
140 string _calhist;
141
142 // A lookup table to find (by interpolation) the percentile given
143 // the value of the underlying SingleValueProjection.
144 map<double, double> _table;
145
146 // A flag to say whether the distribution should be integrated from
147 // below or above.
148 bool _increasing;
149 };
150
151
152}
153
154#endif
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