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
35
40 template <typename... Args,
41 typename = std::enable_if_t< allArgumentsOf<JetEffSmearFn, Args...>::value >>
42 SmearedJets(const JetFinder& ja, const JetEffFn& bTagEffFn, const JetEffFn& cTagEffFn, Args&& ... effSmearFns)
43 : _detFns({JetEffSmearFn(std::forward<Args>(effSmearFns))...}), _bTagEffFn(bTagEffFn), _cTagEffFn(cTagEffFn)
44 {
45 setName("SmearedJets");
46 declare(ja, "TruthJets");
47 _noSmear = getEnvParam<bool>("RIVET_DISABLE_SMEARING", false);
48 }
49
53
54
57
59
61 using Projection::operator =;
62
63
65 CmpState compare(const Projection& p) const {
66 // Compare truth jets definitions
67 const CmpState teq = mkPCmp(p, "TruthJets");
68 if (teq != CmpState::EQ) return teq;
69
70 // Short-circuit if smearing is disabled
71 if (_noSmear) return CmpState::EQ;
72
73 // Compare lists of detector functions
74 const SmearedJets& other = dynamic_cast<const SmearedJets&>(p);
75 const CmpState nfeq = cmp(_detFns.size(), other._detFns.size());
76 if (nfeq != CmpState::EQ) return nfeq;
77 for (size_t i = 0; i < _detFns.size(); ++i) {
78 const CmpState feq = _detFns[i].cmp(other._detFns[i]);
79 if (feq != CmpState::EQ) return feq;
80 }
81 return Rivet::cmp(get_address(_bTagEffFn), get_address(other._bTagEffFn)) ||
82 Rivet::cmp(get_address(_cTagEffFn), get_address(other._cTagEffFn));
83 }
84
85
87 void project(const Event& e) {
88 const Jets& truthjets = apply<JetFinder>(e, "TruthJets").jetsByPt(); //truthJets();
89
90 // Short-circuit if smearing is disabled
91 if (_noSmear) {
92 _recojets = truthjets;
93 return;
94 }
95
96 // Apply jet smearing and efficiency transforms
97 _recojets.clear(); _recojets.reserve(truthjets.size());
98 for (const Jet& j : truthjets) {
99 Jet jdet = j;
100 bool keep = true;
101 MSG_DEBUG("Truth jet: " << "mom=" << jdet.mom()/GeV << " GeV, pT=" << jdet.pT()/GeV << ", eta=" << jdet.eta());
102 for (const JetEffSmearFn& fn : _detFns) {
103 double jeff = -1;
104 std::tie(jdet, jeff) = fn(jdet); // smear & eff
105 // Re-add constituents & tags if (we assume accidentally) they were lost by the smearing function
106 if (jdet.particles().empty() && !j.particles().empty()) jdet.particles() = j.particles();
107 if (jdet.tags().empty() && !j.tags().empty()) jdet.tags() = j.tags();
108 MSG_DEBUG(" ->" << "mom=" << jdet.mom()/GeV << " GeV, pT=" << jdet.pT()/GeV << ", eta=" << jdet.eta());
109 // MSG_DEBUG("New det jet: "
110 // << "mom=" << jdet.mom()/GeV << " GeV, pT=" << jdet.pT()/GeV << ", eta=" << jdet.eta()
111 // << ", b-tag=" << boolalpha << jdet.bTagged()
112 // << ", c-tag=" << boolalpha << jdet.cTagged()
113 // << " : eff=" << 100*jeff << "%");
114 if (jeff <= 0) { keep = false; break; } //< no need to roll expensive dice (and we deal with -ve probabilities, just in case)
115 if (jeff < 1 && rand01() > jeff) { keep = false; break; } //< roll dice (and deal with >1 probabilities, just in case)
116 }
117 if (keep) _recojets.push_back(jdet);
118 }
119 // Apply tagging efficiencies, using smeared kinematics as input to the tag eff functions
120 for (Jet& j : _recojets) {
121 // Decide whether or not there should be a b-tag on this jet
122 const double beff = _bTagEffFn ? _bTagEffFn(j) : j.bTagged();
123 const bool btag = beff == 1 || (beff != 0 && rand01() < beff);
124 // Remove b-tags if needed, and add a dummy one if needed
125 if (!btag && j.bTagged()) j.tags().erase(std::remove_if(j.tags().begin(), j.tags().end(), hasBottom), j.tags().end());
126 if (btag && !j.bTagged()) j.tags().push_back(Particle(PID::BQUARK, j.mom()));
127 // Decide whether or not there should be a c-tag on this jet
128 const double ceff = _cTagEffFn ? _cTagEffFn(j) : j.cTagged();
129 const bool ctag = ceff == 1 || (ceff != 0 && rand01() < beff);
130 // Remove c-tags if needed, and add a dummy one if needed
131 if (!ctag && j.cTagged()) j.tags().erase(std::remove_if(j.tags().begin(), j.tags().end(), hasCharm), j.tags().end());
132 if (ctag && !j.cTagged()) j.tags().push_back(Particle(PID::CQUARK, j.mom()));
133 }
134 }
135
136
138 Jets _jets() const { return _recojets; }
139
141 const Jets truthJets() const {
142 return getProjection<JetFinder>("TruthJets").jetsByPt();
143 }
144
146 void reset() { _recojets.clear(); }
147
148
149 protected:
150
152 Jets _recojets;
153
155 vector<JetEffSmearFn> _detFns;
156
158 JetEffFn _bTagEffFn, _cTagEffFn;
159
163 bool _noSmear{false};
164
165 };
166
167
168}
169
170#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:165
JetFinder(const FinalState &fs, JetMuons usemuons=JetMuons::ALL, JetInvisibles useinvis=JetInvisibles::NONE)
Constructor.
Representation of a clustered jet of particles.
Definition Jet.hh:42
Particles & particles()
Get the particles in this jet.
Definition Jet.hh:71
Particles & tags()
Particles which have been tag-matched to this jet.
Definition Jet.hh:113
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:65
double eta() const
Get the directly (alias).
Definition ParticleBase.hh:89
Particle representation, either from a HepMC::GenEvent or reconstructed.
Definition Particle.hh:45
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
void project(const Event &e)
Perform the jet finding & smearing calculation.
Definition SmearedJets.hh:87
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:42
CmpState compare(const Projection &p) const
Compare to another SmearedJets.
Definition SmearedJets.hh:65
const Jets truthJets() const
Get the truth jets (sorted by pT).
Definition SmearedJets.hh:141
void reset()
Reset the projection. Smearing functions will be unchanged.
Definition SmearedJets.hh:146
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:182
double p(const ParticleBase &p)
Unbound function access to p.
Definition ParticleBaseUtils.hh:653
double JET_CTAG_PERFECT(const Jet &j)
Return 1 if the given Jet contains a c, otherwise 0.
Definition JetSmearingFunctions.hh:63
double JET_BTAG_PERFECT(const Jet &j)
Return 1 if the given Jet contains a b, otherwise 0.
Definition JetSmearingFunctions.hh:56
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:874
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
uintptr_t get_address(std::function< T(U...)> f)
Get a function pointer / hash integer from an std::function.
Definition RivetSTL.hh:320
Functor for simultaneous efficiency-filtering and smearing of Jets.
Definition JetSmearingFunctions.hh:105