rivet is hosted by Hepforge, IPPP Durham
Rivet 3.1.6
SmearedParticles.hh
1// -*- C++ -*-
2#ifndef RIVET_SmearedParticles_HH
3#define RIVET_SmearedParticles_HH
4
5#include "Rivet/Particle.hh"
6#include "Rivet/Projection.hh"
7#include "Rivet/Projections/ParticleFinder.hh"
8#include "Rivet/Tools/SmearingFunctions.hh"
9
10namespace Rivet {
11
12
13 // Recursive variadic template arg decoding
14 namespace {
15 template<typename T>
16 vector<ParticleEffSmearFn>& toEffSmearFns(vector<ParticleEffSmearFn>& v, const T& t) {
17 v.push_back(ParticleEffSmearFn(t));
18 return v;
19 }
20 template<typename T, typename... ARGS>
21 vector<ParticleEffSmearFn>& toEffSmearFns(vector<ParticleEffSmearFn>& v, const T& first, ARGS... args) {
22 v.push_back(ParticleEffSmearFn(first));
23 toEffSmearFns(v, args...);
24 return v;
25 }
26 }
27
28
29
32 public:
33
35
36
39 double eff,
40 const Cut& c=Cuts::open())
41 : SmearedParticles(pf, {{eff}}, c)
42 { }
43
46 const ParticleEffFn& effFn,
47 const Cut& c=Cuts::open())
48 : SmearedParticles(pf, {{effFn}}, c)
49 { }
50
53 double eff, const ParticleSmearFn& smearFn,
54 const Cut& c=Cuts::open())
55 : SmearedParticles(pf, {eff, smearFn}, c)
56 { }
57
60 const ParticleSmearFn& smearFn, double eff,
61 const Cut& c=Cuts::open())
62 : SmearedParticles(pf, {smearFn, eff}, c)
63 { }
64
67 const ParticleEffFn& effFn, const ParticleSmearFn& smearFn,
68 const Cut& c=Cuts::open())
69 : SmearedParticles(pf, {effFn, smearFn}, c)
70 { }
71
74 const ParticleSmearFn& smearFn, const ParticleEffFn& effFn,
75 const Cut& c=Cuts::open())
76 : SmearedParticles(pf, {smearFn, effFn}, c)
77 { }
78
81 const vector<ParticleEffSmearFn>& effSmearFns,
82 const Cut& c=Cuts::open())
83 : ParticleFinder(c),
84 _detFns(effSmearFns)
85 {
86 setName("SmearedParticles");
87 declare(pf, "TruthParticles");
88 }
89
92 const initializer_list<ParticleEffSmearFn>& effSmearFns,
93 const Cut& c=Cuts::open())
94 : SmearedParticles(pf, vector<ParticleEffSmearFn>{effSmearFns}, c)
95 { }
96
100 template<typename... ARGS>
101 SmearedParticles(const ParticleFinder& pf, const Cut& c, ARGS... effSmearFns)
102 : SmearedParticles(pf, toEffSmearFns(_detFns, effSmearFns...), c)
103 { }
104
105
108
110
111
116 CmpState compare(const Projection& p) const {
117 const SmearedParticles& other = dynamic_cast<const SmearedParticles&>(p);
118
119 // Compare truth particles definitions
120 const CmpState teq = mkPCmp(other, "TruthParticles");
121 if (teq != CmpState::EQ) return teq;
122
123 // Compare lists of detector functions
124 const CmpState nfeq = cmp(_detFns.size(), other._detFns.size());
125 MSG_TRACE("Numbers of detector functions = " << _detFns.size() << " VS " << other._detFns.size());
126 if (nfeq != CmpState::EQ) return nfeq;
127 for (size_t i = 0; i < _detFns.size(); ++i) {
128 const CmpState feq = _detFns[i].cmp(other._detFns[i]);
129 if (feq != CmpState::EQ) return feq;
130 }
131
132 // If we got this far, we're equal
133 MSG_DEBUG("Equivalent detected! " << p.name() << ", " << this->name());
134 return CmpState::EQ;
135 }
136
137
139 void project(const Event& e) {
140 // Copying and filtering
141 const Particles& truthparticles = apply<ParticleFinder>(e, "TruthParticles").particlesByPt(); //truthParticles();
142 _theParticles.clear(); _theParticles.reserve(truthparticles.size());
143 for (const Particle& p : truthparticles) {
144 Particle pdet = p;
145 double peff = -1;
146 bool keep = true;
147 MSG_TRACE("Number of detector functions = " << _detFns.size());
148 for (const ParticleEffSmearFn& fn : _detFns) {
149 std::tie(pdet, peff) = fn(pdet); // smear & eff
150 // Test the short-circuit random numbers if possible; note handling of < 0 and > 1 probabilities
151 if (peff <= 0 || rand01() > peff) keep = false;
152 MSG_DEBUG("New det particle: pid=" << pdet.pid()
153 << ", mom=" << pdet.mom()/GeV << " GeV, "
154 << "pT=" << pdet.pT()/GeV << ", eta=" << pdet.eta()
155 << " : eff=" << 100*peff << "%, discarded=" << std::boolalpha << !keep);
156 if (!keep) break; // discarded; no need to try more smear-eff functions
157 }
158 // If discarding, go straight to the next particle
159 if (!keep) continue;
160 // Store, recording where the smearing was built from
161 pdet.addConstituent(p);
162 _theParticles.push_back(pdet);
163 }
164 }
165
167 const Particles truthParticles() const {
168 return getProjection<ParticleFinder>("TruthParticles").particlesByPt();
169 }
170
172 void reset() { _theParticles.clear(); }
173
174
175 private:
176
178 vector<ParticleEffSmearFn> _detFns;
179
180 };
181
182
183}
184
185#endif
Representation of a HepMC event, and enabler of Projection caching.
Definition: Event.hh:22
const FourMomentum & mom() const
Get equivalent single momentum four-vector (const) (alias).
Definition: ParticleBase.hh:39
double pT() const
Get the directly (alias).
Definition: ParticleBase.hh:63
double eta() const
Get the directly (alias).
Definition: ParticleBase.hh:87
Base class for projections which return subsets of an event's particles.
Definition: ParticleFinder.hh:11
size_t size() const
Count the final-state particles.
Definition: ParticleFinder.hh:35
Particle representation, either from a HepMC::GenEvent or reconstructed.
Definition: Particle.hh:53
virtual void addConstituent(const Particle &c, bool addmom=false)
Add a single direct constituent to this particle.
PdgId pid() const
This Particle's PDG ID code.
Definition: Particle.hh:197
Specialised vector of Particle objects.
Definition: Particle.hh:25
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
Cmp< Projection > mkPCmp(const Projection &otherparent, const std::string &pname) const
void setName(const std::string &name)
Used by derived classes to set their name.
Definition: Projection.hh:142
Wrapper projection for smearing Jets with detector resolutions and efficiencies.
Definition: SmearedParticles.hh:31
SmearedParticles(const ParticleFinder &pf, double eff, const ParticleSmearFn &smearFn, const Cut &c=Cuts::open())
Constructor with const efficiency followed by a smearing function.
Definition: SmearedParticles.hh:52
SmearedParticles(const ParticleFinder &pf, const Cut &c, ARGS... effSmearFns)
Constructor with a variadic ordered list of efficiency and smearing function args.
Definition: SmearedParticles.hh:101
SmearedParticles(const ParticleFinder &pf, const vector< ParticleEffSmearFn > &effSmearFns, const Cut &c=Cuts::open())
Constructor with an ordered list of efficiency and/or smearing functions.
Definition: SmearedParticles.hh:80
SmearedParticles(const ParticleFinder &pf, const ParticleEffFn &effFn, const ParticleSmearFn &smearFn, const Cut &c=Cuts::open())
Constructor with an efficiency function followed by a smearing function.
Definition: SmearedParticles.hh:66
const Particles truthParticles() const
Get the truth particles (sorted by pT)
Definition: SmearedParticles.hh:167
SmearedParticles(const ParticleFinder &pf, double eff, const Cut &c=Cuts::open())
Constructor with const efficiency.
Definition: SmearedParticles.hh:38
void project(const Event &e)
Perform the particle finding & smearing calculation.
Definition: SmearedParticles.hh:139
SmearedParticles(const ParticleFinder &pf, const initializer_list< ParticleEffSmearFn > &effSmearFns, const Cut &c=Cuts::open())
Constructor with an ordered list of efficiency and/or smearing functions.
Definition: SmearedParticles.hh:91
void reset()
Reset the projection. Smearing functions will be unchanged.
Definition: SmearedParticles.hh:172
SmearedParticles(const ParticleFinder &pf, const ParticleSmearFn &smearFn, const ParticleEffFn &effFn, const Cut &c=Cuts::open())
Constructor with a smearing function followed by an efficiency function.
Definition: SmearedParticles.hh:73
CmpState compare(const Projection &p) const
Definition: SmearedParticles.hh:116
SmearedParticles(const ParticleFinder &pf, const ParticleEffFn &effFn, const Cut &c=Cuts::open())
Constructor with an efficiency function.
Definition: SmearedParticles.hh:45
SmearedParticles(const ParticleFinder &pf, const ParticleSmearFn &smearFn, double eff, const Cut &c=Cuts::open())
Constructor with a smearing function followed by const efficiency.
Definition: SmearedParticles.hh:59
DEFAULT_RIVET_PROJ_CLONE(SmearedParticles)
Clone on the heap.
#define MSG_TRACE(x)
Lowest-level, most verbose messaging, using MSG_LVL.
Definition: Logging.hh:193
#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:653
function< Particle(const Particle &)> ParticleSmearFn
Typedef for Particle smearing functions/functors.
Definition: ParticleSmearingFunctions.hh:19
function< double(const Particle &)> ParticleEffFn
Typedef for Particle efficiency functions/functors.
Definition: ParticleSmearingFunctions.hh:22
const Cut & open()
Fully open cut singleton, accepts everything.
Definition: MC_Cent_pPb.hh:10
double rand01()
Return a uniformly sampled random number between 0 and 1.
Cmp< T > cmp(const T &t1, const T &t2)
Global helper function for easy creation of Cmp objects.
Definition: Cmp.hh:255
Functor for simultaneous efficiency-filtering and smearing of Particles.
Definition: ParticleSmearingFunctions.hh:58