Rivet API documentation

Rivet 4.1.3
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
15 public:
16
19
21 template <typename... Args,
22 typename = std::enable_if_t<allArgumentsOf<ParticleEffSmearFn, Args...>::value>>
23 SmearedParticles(const ParticleFinder& pf, Args&&... effSmearFns)
24 : SmearedParticles(pf, Cuts::open(), std::forward<Args>(effSmearFns)...) { }
25
31 template <typename... Args,
32 typename = std::enable_if_t<allArgumentsOf<ParticleEffSmearFn, Args...>::value>>
33 SmearedParticles(const ParticleFinder& pf, const Cut& c, Args&&... effSmearFns)
34 : ParticleFinder(c), _detFns({ParticleEffSmearFn(std::forward<Args>(effSmearFns))...}) {
35 setName("SmearedParticles");
36 declare(pf, "TruthParticles");
37 _noSmear = getEnvParam<bool>("RIVET_DISABLE_SMEARING", false);
38 }
39
40
43
45
47 using Projection::operator=;
48
49
54 CmpState compare(const Projection& p) const {
55 const SmearedParticles& other = dynamic_cast<const SmearedParticles&>(p);
56
57 // Compare truth particles definitions
58 const CmpState teq = mkPCmp(other, "TruthParticles");
59 if (teq != CmpState::EQ) return teq;
60
61 // Compare cuts
62 if (_cuts != other._cuts) return CmpState::NEQ;
63
64 // Compare lists of detector functions
65 if (!_noSmear) {
66 const CmpState nfeq = cmp(_detFns.size(), other._detFns.size());
67 MSG_TRACE("Numbers of detector functions = " << _detFns.size() << " VS " << other._detFns.size());
68 if (nfeq != CmpState::EQ) return nfeq;
69 for (size_t i = 0; i < _detFns.size(); ++i) {
70 const CmpState feq = _detFns[i].cmp(other._detFns[i]);
71 if (feq != CmpState::EQ) return feq;
72 }
73 }
74
75 // If we got this far, we're equal
76 MSG_DEBUG("Equivalent detected! " << p.name() << ", " << this->name());
77 return CmpState::EQ;
78 }
79
80
82 void project(const Event& e) {
83 const Particles& truthparticles = apply<ParticleFinder>(e, "TruthParticles")
84 .particlesByPt(); //truthParticles();
85
86 // Short-circuit (with cuts) if smearing is disabled
87 if (_noSmear) {
88 _theParticles = select(truthparticles, _cuts);
89 return;
90 }
91
92 // Apply the smearing and then reco-level cuts to each particle
93 _theParticles.clear();
94 _theParticles.reserve(truthparticles.size());
95 for (const Particle& p : truthparticles) {
96 Particle pdet = p;
97 double peff = -1;
98 bool keep = true;
99 MSG_TRACE("Number of detector functions = " << _detFns.size());
100 for (const ParticleEffSmearFn& fn : _detFns) {
101 std::tie(pdet, peff) = fn(pdet); // smear & eff
102 // Test the short-circuit random numbers if possible; note handling of < 0 and > 1 probabilities
103 if (peff <= 0 || rand01() > peff) keep = false;
104 MSG_DEBUG("New det particle: pid=" << pdet.pid() << ", mom=" << pdet.mom() / GeV << " GeV, "
105 << "pT=" << pdet.pT() / GeV << ", eta=" << pdet.eta()
106 << " : eff=" << 100 * peff << "%, discarded=" << std::boolalpha
107 << !keep);
108 if (!keep) break; // discarded; no need to try more smear-eff functions
109 }
110 // If discarding, go straight to the next particle
111 if (!keep) continue;
112 // Ensure the smeared particle satisfies the cuts associated with this projection
113 if (!_cuts->accept(pdet)) continue;
114
115 // Store, recording where the smearing was built from
116 pdet.addConstituent(p);
117 _theParticles.push_back(pdet);
118 }
119 }
120
122 const Particles truthParticles() const {
123 return getProjection<ParticleFinder>("TruthParticles").particlesByPt();
124 }
125
127 void reset() {
128 _theParticles.clear();
129 }
130
131
132 protected:
133
135 vector<ParticleEffSmearFn> _detFns;
136
140 bool _noSmear{false};
141 };
142
143
144}
145
146#endif
Representation of a HepMC event, and enabler of Projection caching.
Definition Event.hh:22
const FourMomentum & mom() const
Get the equivalent momentum four-vector (const) (alias).
Definition ParticleBase.hh:39
double pT() const
Get the directly (alias).
Definition ParticleBase.hh:81
double eta() const
Get the directly (alias).
Definition ParticleBase.hh:125
size_t size() const
Count the final-state particles.
Definition ParticleFinder.hh:50
ParticleFinder(const Cut &c=Cuts::OPEN)
Construction using Cuts object.
Definition ParticleFinder.hh:20
Particle representation, either from a HepMC::GenEvent or reconstructed.
Definition Particle.hh:50
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:221
Specialised vector of Particle objects.
Definition Particle.hh:21
const PROJ & getProjection(const std::string &name) const
Definition ProjectionApplier.hh:72
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
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:146
SmearedParticles(const ParticleFinder &pf, Args &&... effSmearFns)
Constructor with a variadic ordered list of efficiency and smearing function args.
Definition SmearedParticles.hh:23
const Particles truthParticles() const
Get the truth particles (sorted by pT).
Definition SmearedParticles.hh:122
void project(const Event &e)
Perform the particle finding & smearing calculation.
Definition SmearedParticles.hh:82
RIVET_DEFAULT_PROJ_CLONE(SmearedParticles)
Clone on the heap.
void reset()
Reset the projection. Smearing functions will be unchanged.
Definition SmearedParticles.hh:127
CmpState compare(const Projection &p) const
Definition SmearedParticles.hh:54
SmearedParticles(const ParticleFinder &pf, const Cut &c, Args &&... effSmearFns)
Constructor with a variadic ordered list of efficiency and smearing function args.
Definition SmearedParticles.hh:33
Jets select(const Jets &jets, const Cut &c)
Filter a jet collection in-place to the subset that passes the supplied Cut.
Definition JetUtils.hh:183
#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:819
T getEnvParam(const std::string name, const T &fallback)
Get a parameter from a named environment variable, with automatic type conversion.
Definition Utils.hh:921
Namespace used for ambiguous identifiers.
Definition Cuts.hh:58
Definition LHCbCommon.hh:9
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:253
STL namespace.
Functor for simultaneous efficiency-filtering and smearing of Particles.
Definition ParticleSmearingFunctions.hh:79