Rivet 3.1.9
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
18 public:
19
25 PercentileProjection(const SingleValueProjection & sv, const Histo1D& calhist,
26 bool increasing = false)
27 : _calhist("EMPTY"), _increasing(increasing) {
28 setName("PercentileProjection");
29 declare(sv, "OBSERVABLE");
30 //if ( !calhist ) return;
31 MSG_DEBUG("Constructing PercentileProjection from " << calhist.path());
32 _calhist = calhist.path();
33 int N = calhist.numBins();
34 double sum = calhist.sumW();
35
36 if ( increasing ) {
37 double acc = calhist.underflow().sumW();
38 _table.insert(make_pair(calhist.bin(0).xEdges().first, 100.0*acc/sum));
39 for ( int i = 0; i < N; ++i ) {
40 acc += calhist.bin(i).sumW();
41 _table.insert(make_pair(calhist.bin(i).xEdges().second, 100.0*acc/sum));
42 }
43 } else {
44 double acc = calhist.overflow().sumW();
45 _table.insert(make_pair(calhist.bin(N - 1).xEdges().second, 100.0*acc/sum));
46 for ( int i = N - 1; i >= 0; --i ) {
47 acc += calhist.bin(i).sumW();
48 _table.insert(make_pair(calhist.bin(i).xEdges().first, 100.0*acc/sum));
49 }
50 }
51 if (getLog().isActive(Log::DEBUG)) {
52 MSG_DEBUG("Mapping from observable to percentile:");
53 for (auto p : _table) {
54 std::cout << std::setw(16) << p.first << " -> "
55 << std::setw(16) << p.second << "%" << std::endl;
56 if (not increasing and p.second <= 0) break;
57 if (increasing and p.second >= 100) break;
58 }
59 }
60 }
61
62 // Constructor taking a SingleValueProjection and a calibration
63 // histogram. If increasing it means that low values corresponds to
64 // lower percentiles.
65 PercentileProjection(const SingleValueProjection & sv, const Scatter2D& calscat,
66 bool increasing = false)
67 : _calhist("EMPTY"), _increasing(increasing) {
68 declare(sv, "OBSERVABLE");
69
70 //if ( !calscat ) return;
71 MSG_DEBUG("Constructing PercentileProjection from " << calscat.path());
72 _calhist = calscat.path();
73 int N = calscat.numPoints();
74 double sum = 0.0;
75 for ( const auto & p : calscat.points() ) sum += p.y();
76
77 double acc = 0.0;
78 if ( increasing ) {
79 _table.insert(make_pair(calscat.point(0).xMin(), 100.0*acc/sum));
80 for ( int i = 0; i < N; ++i ) {
81 acc += calscat.point(i).y();
82 _table.insert(make_pair(calscat.point(i).xMax(), 100.0*acc/sum));
83 }
84 } else {
85 _table.insert(make_pair(calscat.point(N - 1).xMax(), 100.0*acc/sum));
86 for ( int i = N - 1; i >= 0; --i ) {
87 acc += calscat.point(i).y();
88 _table.insert(make_pair(calscat.point(i).xMin(), 100.0*acc/sum));
89 }
90 }
91 }
92
93 DEFAULT_RIVET_PROJ_CLONE(PercentileProjection);
94
96 using Projection::operator =;
97
98
99 // The projection function takes the assigned SingeValueProjection
100 // and sets the value of this projection to the corresponding
101 // percentile. If no calibration has been provided, -1 will be
102 // returned. If values are outside of the calibration histogram, 0
103 // or 100 will be returned.
104 void project(const Event& e) {
105 clear();
106 if ( _table.empty() ) return;
107 auto& pobs = apply<SingleValueProjection>(e, "OBSERVABLE");
108 double obs = pobs();
109 double pcnt = lookup(obs);
110 if ( pcnt >= 0.0 ) set(pcnt);
111 MSG_DEBUG("Observable(" << pobs.name() << ")="
112 << std::setw(16) << obs
113 << "-> Percentile=" << std::setw(16) << pcnt << "%");
114 }
115
116
117 // Standard comparison function.
118 CmpState compare(const Projection& p) const {
119 const PercentileProjection pp = dynamic_cast<const PercentileProjection&>(p);
120 return mkNamedPCmp(p, "OBSERVABLE") ||
121 cmp(_increasing, pp._increasing) ||
122 cmp(_calhist, pp._calhist);
123 }
124
125
126 protected:
127
128 // The (interpolated) lookup table
129 double lookup(double obs) const {
130 auto low = _table.upper_bound(obs);
131 if ( low == _table.end() ) return _increasing? 100.0: 0.0;
132 if ( low == _table.begin() ) return _increasing? 0.0: 100.0;
133 auto high = low--;
134 return low->second + (obs - low->first)*(high->second - low->second)/
135 (high->first - low->first);
136 }
137
138 // Astring identifying the calibration histogram.
139 string _calhist;
140
141 // A lookup table to find (by interpolation) the percentile given
142 // the value of the underlying SingleValueProjection.
143 map<double,double> _table;
144
145 // A flag to say whether the distribution should be integrated from
146 // below or above.
147 bool _increasing;
148
149 };
150
151
152}
153
154#endif
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:17
PercentileProjection(const SingleValueProjection &sv, const Histo1D &calhist, bool increasing=false)
Definition PercentileProjection.hh:25
void project(const Event &e)
Definition PercentileProjection.hh:104
CmpState compare(const Projection &p) const
Definition PercentileProjection.hh:118
const PROJ & declare(const PROJ &proj, const std::string &name)
Register a contained projection (user-facing version)
Definition ProjectionApplier.hh:170
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:142
Log & getLog() const
Get a Log object based on the getName() property of the calling projection object.
Definition Projection.hh:136
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 set(double v)
Definition SingleValueProjection.hh:55
void clear()
Unset the value.
Definition SingleValueProjection.hh:58
CONTAINER::value_type sum(const CONTAINER &c)
Generic sum function, adding x for all x in container c.
Definition Utils.hh:436
#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:684
Definition MC_Cent_pPb.hh:10
Cmp< T > cmp(const T &t1, const T &t2)
Global helper function for easy creation of Cmp objects.
Definition Cmp.hh:255