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 { }
26
32 template<typename... Args,
33 typename = std::enable_if_t< allArgumentsOf<ParticleEffSmearFn, Args...>::value >>
34 SmearedParticles(const ParticleFinder& pf, const Cut& c, Args&& ... effSmearFns)
35 : ParticleFinder(c), _detFns({ParticleEffSmearFn(std::forward<Args>(effSmearFns))...})
36 {
37 setName("SmearedParticles");
38 declare(pf, "TruthParticles");
39 _noSmear = getEnvParam<bool>("RIVET_DISABLE_SMEARING", false);
40 }
41
42
45
47
49 using Projection::operator =;
50
51
56 CmpState compare(const Projection& p) const {
57 const SmearedParticles& other = dynamic_cast<const SmearedParticles&>(p);
58
59 // Compare truth particles definitions
60 const CmpState teq = mkPCmp(other, "TruthParticles");
61 if (teq != CmpState::EQ) return teq;
62
63 // Compare cuts
64 if (_cuts != other._cuts) return CmpState::NEQ;
65
66 // Compare lists of detector functions
67 if (!_noSmear) {
68 const CmpState nfeq = cmp(_detFns.size(), other._detFns.size());
69 MSG_TRACE("Numbers of detector functions = " << _detFns.size() << " VS " << other._detFns.size());
70 if (nfeq != CmpState::EQ) return nfeq;
71 for (size_t i = 0; i < _detFns.size(); ++i) {
72 const CmpState feq = _detFns[i].cmp(other._detFns[i]);
73 if (feq != CmpState::EQ) return feq;
74 }
75 }
76
77 // If we got this far, we're equal
78 MSG_DEBUG("Equivalent detected! " << p.name() << ", " << this->name());
79 return CmpState::EQ;
80 }
81
82
84 void project(const Event& e) {
85 const Particles& truthparticles = apply<ParticleFinder>(e, "TruthParticles").particlesByPt(); //truthParticles();
86
87 // Short-circuit (with cuts) if smearing is disabled
88 if (_noSmear) {
89 _theParticles = select(truthparticles, _cuts);
90 return;
91 }
92
93 // Apply the smearing and then reco-level cuts to each particle
94 _theParticles.clear(); _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()
105 << ", mom=" << pdet.mom()/GeV << " GeV, "
106 << "pT=" << pdet.pT()/GeV << ", eta=" << pdet.eta()
107 << " : eff=" << 100*peff << "%, discarded=" << std::boolalpha << !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() { _theParticles.clear(); }
128
129
130 protected:
131
133 vector<ParticleEffSmearFn> _detFns;
134
138 bool _noSmear{false};
139
140 };
141
142
143}
144
145#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:65
double eta() const
Get the directly (alias).
Definition ParticleBase.hh:89
size_t size() const
Count the final-state particles.
Definition ParticleFinder.hh:51
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:45
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:212
Specialised vector of Particle objects.
Definition Particle.hh:21
const PROJ & getProjection(const std::string &name) const
Definition ProjectionApplier.hh:71
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:119
const PROJ & declare(const PROJ &proj, const std::string &name) const
Register a contained projection (user-facing version).
Definition ProjectionApplier.hh:184
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:148
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:84
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:56
SmearedParticles(const ParticleFinder &pf, const Cut &c, Args &&... effSmearFns)
Constructor with a variadic ordered list of efficiency and smearing function args.
Definition SmearedParticles.hh:34
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:157
#define MSG_TRACE(x)
Lowest-level, most verbose messaging, using MSG_LVL.
Definition Logging.hh:180
#define MSG_DEBUG(x)
Debug messaging, not enabled by default, using MSG_LVL.
Definition Logging.hh:182
double p(const ParticleBase &p)
Unbound function access to p.
Definition ParticleBaseUtils.hh:653
T getEnvParam(const std::string name, const T &fallback)
Get a parameter from a named environment variable, with automatic type conversion.
Definition Utils.hh:874
Namespace used for ambiguous identifiers.
Definition Cuts.hh:55
Definition MC_CENT_PPB_Projections.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
STL namespace.
Functor for simultaneous efficiency-filtering and smearing of Particles.
Definition ParticleSmearingFunctions.hh:58