Rivet API documentation

Rivet 4.1.3
SmearedJets.hh
1// -*- C++ -*-
2#ifndef RIVET_SmearedJets_HH
3#define RIVET_SmearedJets_HH
4
5#include "Rivet/Jet.hh"
6#include "Rivet/Particle.hh"
7#include "Rivet/Projection.hh"
8#include "Rivet/Projections/JetFinder.hh"
9#include "Rivet/Tools/SmearingFunctions.hh"
10#include <functional>
11
12namespace Rivet {
13
14
16
17
19 class SmearedJets : public JetFinder {
20 public:
21
24
29 const JetSmearFn& smearFn,
30 const JetEffFn& bTagEffFn = JET_BTAG_PERFECT,
31 const JetEffFn& cTagEffFn = JET_CTAG_PERFECT)
32 : SmearedJets(ja, bTagEffFn, cTagEffFn, smearFn) { }
33
34
39 template <typename... Args, typename = std::enable_if_t<allArgumentsOf<JetEffSmearFn, Args...>::value>>
41 const JetEffFn& bTagEffFn,
42 const JetEffFn& cTagEffFn,
43 Args&&... effSmearFns)
44 : _detFns({JetEffSmearFn(std::forward<Args>(effSmearFns))...}),
45 _bTagEffFn(bTagEffFn),
46 _cTagEffFn(cTagEffFn) {
47 setName("SmearedJets");
48 declare(ja, "TruthJets");
49 _noSmear = getEnvParam<bool>("RIVET_DISABLE_SMEARING", false);
50 }
51
55
56
59
61
63 using Projection::operator=;
64
65
67 CmpState compare(const Projection& p) const {
68 // Compare truth jets definitions
69 const CmpState teq = mkPCmp(p, "TruthJets");
70 if (teq != CmpState::EQ) return teq;
71
72 // Short-circuit if smearing is disabled
73 if (_noSmear) return CmpState::EQ;
74
75 // Compare lists of detector functions
76 const SmearedJets& other = dynamic_cast<const SmearedJets&>(p);
77 const CmpState nfeq = cmp(_detFns.size(), other._detFns.size());
78 if (nfeq != CmpState::EQ) return nfeq;
79 for (size_t i = 0; i < _detFns.size(); ++i) {
80 const CmpState feq = _detFns[i].cmp(other._detFns[i]);
81 if (feq != CmpState::EQ) return feq;
82 }
83 return Rivet::cmp(get_address(_bTagEffFn), get_address(other._bTagEffFn))
84 || Rivet::cmp(get_address(_cTagEffFn), get_address(other._cTagEffFn));
85 }
86
87
89 void project(const Event& e) {
90 const Jets& truthjets = apply<JetFinder>(e, "TruthJets").jetsByPt(); //truthJets();
91
92 // Short-circuit if smearing is disabled
93 if (_noSmear) {
94 _recojets = truthjets;
95 return;
96 }
97
98 // Apply jet smearing and efficiency transforms
99 _recojets.clear();
100 _recojets.reserve(truthjets.size());
101 for (const Jet& j : truthjets) {
102 Jet jdet = j;
103 bool keep = true;
104 MSG_DEBUG("Truth jet: " << "mom=" << jdet.mom() / GeV << " GeV, pT=" << jdet.pT() / GeV
105 << ", eta=" << jdet.eta());
106 for (const JetEffSmearFn& fn : _detFns) {
107 double jeff = -1;
108 std::tie(jdet, jeff) = fn(jdet); // smear & eff
109 // Re-add constituents & tags if (we assume accidentally) they were lost by the smearing function
110 if (jdet.particles().empty() && !j.particles().empty()) jdet.particles() = j.particles();
111 if (jdet.tags().empty() && !j.tags().empty()) jdet.tags() = j.tags();
112 MSG_DEBUG(" ->" << "mom=" << jdet.mom() / GeV << " GeV, pT=" << jdet.pT() / GeV
113 << ", eta=" << jdet.eta());
114 // MSG_DEBUG("New det jet: "
115 // << "mom=" << jdet.mom()/GeV << " GeV, pT=" << jdet.pT()/GeV << ", eta=" << jdet.eta()
116 // << ", b-tag=" << boolalpha << jdet.bTagged()
117 // << ", c-tag=" << boolalpha << jdet.cTagged()
118 // << " : eff=" << 100*jeff << "%");
119 if (jeff <= 0) {
120 keep = false;
121 break;
122 } //< no need to roll expensive dice (and we deal with -ve probabilities, just in case)
123 if (jeff < 1 && rand01() > jeff) {
124 keep = false;
125 break;
126 } //< roll dice (and deal with >1 probabilities, just in case)
127 }
128 if (keep) _recojets.push_back(jdet);
129 }
130 // Apply tagging efficiencies, using smeared kinematics as input to the tag eff functions
131 for (Jet& j : _recojets) {
132 // Decide whether or not there should be a b-tag on this jet
133 const double beff = _bTagEffFn ? _bTagEffFn(j) : j.bTagged();
134 const bool btag = beff == 1 || (beff != 0 && rand01() < beff);
135 // Remove b-tags if needed, and add a dummy one if needed
136 if (!btag && j.bTagged())
137 j.tags().erase(std::remove_if(j.tags().begin(), j.tags().end(), hasBottom), j.tags().end());
138 if (btag && !j.bTagged())
139 j.tags().push_back(
140 Particle(PID::BQUARK,
141 j.mom()));
142 // Decide whether or not there should be a c-tag on this jet
143 const double ceff = _cTagEffFn ? _cTagEffFn(j) : j.cTagged();
144 const bool ctag = ceff == 1 || (ceff != 0 && rand01() < beff);
145 // Remove c-tags if needed, and add a dummy one if needed
146 if (!ctag && j.cTagged())
147 j.tags().erase(std::remove_if(j.tags().begin(), j.tags().end(), hasCharm), j.tags().end());
148 if (ctag && !j.cTagged()) j.tags().push_back(Particle(PID::CQUARK, j.mom()));
149 }
150 }
151
152
154 Jets _jets() const {
155 return _recojets;
156 }
157
159 const Jets truthJets() const {
160 return getProjection<JetFinder>("TruthJets").jetsByPt();
161 }
162
164 void reset() {
165 _recojets.clear();
166 }
167
168
169 protected:
170
172 Jets _recojets;
173
175 vector<JetEffSmearFn> _detFns;
176
178 JetEffFn _bTagEffFn, _cTagEffFn;
179
183 bool _noSmear{false};
184 };
185
186
187}
188
189#endif
Representation of a HepMC event, and enabler of Projection caching.
Definition Event.hh:22
size_t size() const
Count the jets.
Definition JetFinder.hh:166
JetFinder(const FinalState &fs, JetMuons usemuons=JetMuons::ALL, JetInvisibles useinvis=JetInvisibles::NONE)
Constructor.
Representation of a clustered jet of particles.
Definition Jet.hh:47
Particles & particles()
Get the particles in this jet.
Definition Jet.hh:84
Particles & tags()
Particles which have been tag-matched to this jet.
Definition Jet.hh:148
Specialised vector of Jet objects.
Definition Jet.hh:21
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
Particle representation, either from a HepMC::GenEvent or reconstructed.
Definition Particle.hh:50
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
void project(const Event &e)
Perform the jet finding & smearing calculation.
Definition SmearedJets.hh:89
SmearedJets(const JetFinder &ja, const JetSmearFn &smearFn, const JetEffFn &bTagEffFn=JET_BTAG_PERFECT, const JetEffFn &cTagEffFn=JET_CTAG_PERFECT)
Constructor with a reco efficiency and optional tagging efficiencies.
Definition SmearedJets.hh:28
SmearedJets(const JetFinder &ja, const JetEffFn &bTagEffFn, const JetEffFn &cTagEffFn, Args &&... effSmearFns)
Constructor with a parameter pack of efficiency and smearing functions, plus optional tagging efficie...
Definition SmearedJets.hh:40
CmpState compare(const Projection &p) const
Compare to another SmearedJets.
Definition SmearedJets.hh:67
const Jets truthJets() const
Get the truth jets (sorted by pT).
Definition SmearedJets.hh:159
void reset()
Reset the projection. Smearing functions will be unchanged.
Definition SmearedJets.hh:164
RIVET_DEFAULT_PROJ_CLONE(SmearedJets)
Clone on the heap.
#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
double JET_CTAG_PERFECT(const Jet &j)
Return 1 if the given Jet contains a c, otherwise 0.
Definition JetSmearingFunctions.hh:85
double JET_BTAG_PERFECT(const Jet &j)
Return 1 if the given Jet contains a b, otherwise 0.
Definition JetSmearingFunctions.hh:74
function< Jet(const Jet &)> JetSmearFn
Typedef for Jet smearing functions/functors.
Definition JetSmearingFunctions.hh:20
function< double(const Jet &)> JetEffFn
Typedef for Jet efficiency functions/functors.
Definition JetSmearingFunctions.hh:23
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
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
uintptr_t get_address(std::function< T(U...)> f)
Get a function pointer / hash integer from an std::function.
Definition RivetSTL.hh:338
Functor for simultaneous efficiency-filtering and smearing of Jets.
Definition JetSmearingFunctions.hh:142