rivet is hosted by Hepforge, IPPP Durham
Rivet  2.7.0
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/JetAlg.hh"
9 #include "Rivet/Tools/SmearingFunctions.hh"
10 #include <functional>
11 
12 namespace Rivet {
13 
14 
15  // // Recursive variadic template arg decoding
16  // namespace {
17  // template<typename T>
18  // vector<JetEffSmearFn>& toEffSmearFns(vector<JetEffSmearFn>& v, const T& t) {
19  // v.push_back(JetEffSmearFn(t));
20  // return v;
21  // }
22  // template<typename T, typename... ARGS>
23  // vector<JetEffSmearFn>& toEffSmearFns(vector<JetEffSmearFn>& v, const T& first, ARGS... args) {
24  // v.push_back(JetEffSmearFn(first));
25  // toEffSmearFns(v, args...);
26  // return v;
27  // }
28  // }
29 
30 
31 
33  class SmearedJets : public JetAlg {
34  public:
35 
37 
38 
40  SmearedJets(const JetAlg& ja,
41  const JetSmearFn& smearFn,
42  const JetEffFn& bTagEffFn=JET_BTAG_PERFECT,
43  const JetEffFn& cTagEffFn=JET_CTAG_PERFECT)
44  : SmearedJets(ja, vector<JetEffSmearFn>{smearFn}, bTagEffFn, cTagEffFn)
45  { }
46 
47 
49  SmearedJets(const JetAlg& ja,
50  const JetEffFn& bTagEffFn=JET_BTAG_PERFECT,
51  const JetEffFn& cTagEffFn=JET_CTAG_PERFECT,
52  const initializer_list<JetEffSmearFn>& effSmearFns={})
53  : SmearedJets(ja, vector<JetEffSmearFn>{effSmearFns}, bTagEffFn, cTagEffFn)
54  { }
55 
57  SmearedJets(const JetAlg& ja,
58  const JetEffFn& bTagEffFn=JET_BTAG_PERFECT,
59  const JetEffFn& cTagEffFn=JET_CTAG_PERFECT,
60  const vector<JetEffSmearFn>& effSmearFns={})
61  : SmearedJets(ja, effSmearFns, bTagEffFn, cTagEffFn)
62  { }
63 
64 
66  SmearedJets(const JetAlg& ja,
67  const initializer_list<JetEffSmearFn>& effSmearFns,
68  const JetEffFn& bTagEffFn=JET_BTAG_PERFECT,
69  const JetEffFn& cTagEffFn=JET_CTAG_PERFECT)
70  : SmearedJets(ja, vector<JetEffSmearFn>{effSmearFns}, bTagEffFn, cTagEffFn)
71  { }
72 
74  SmearedJets(const JetAlg& ja,
75  const vector<JetEffSmearFn>& effSmearFns,
76  const JetEffFn& bTagEffFn=JET_BTAG_PERFECT,
77  const JetEffFn& cTagEffFn=JET_CTAG_PERFECT)
78  : _detFns(effSmearFns), _bTagEffFn(bTagEffFn), _cTagEffFn(cTagEffFn)
79  {
80  setName("SmearedJets");
81  addProjection(ja, "TruthJets");
82  }
83 
84 
87  SmearedJets(const JetAlg& ja,
88  const JetSmearFn& smearFn,
89  const JetEffFn& bTagEffFn,
90  const JetEffFn& cTagEffFn,
91  const JetEffFn& jetEffFn)
92  : SmearedJets(ja, {jetEffFn,smearFn}, bTagEffFn, cTagEffFn)
93  { }
94 
95 
99 
100 
103 
105 
106 
108  int compare(const Projection& p) const {
109  // Compare truth jets definitions
110  const int teq = mkPCmp(p, "TruthJets");
111  if (teq != EQUIVALENT) return teq;
112 
113  // Compare lists of detector functions
114  const SmearedJets& other = dynamic_cast<const SmearedJets&>(p);
115  const int nfeq = cmp(_detFns.size(), other._detFns.size());
116  if (nfeq != EQUIVALENT) return nfeq;
117  for (size_t i = 0; i < _detFns.size(); ++i) {
118  const int feq = _detFns[i].cmp(other._detFns[i]);
119  if (feq != EQUIVALENT) return feq;
120  }
121 
122  // If we got this far, we're equal
123  return EQUIVALENT;
124  }
125 
126 
128  void project(const Event& e) {
129  // Copying and filtering
130  const Jets& truthjets = apply<JetAlg>(e, "TruthJets").jetsByPt();
131  _recojets.clear(); _recojets.reserve(truthjets.size());
132  // Apply jet smearing and efficiency transforms
133  for (const Jet& j : truthjets) {
134  Jet jdet = j;
135  bool keep = true;
136  MSG_DEBUG("Truth jet: " << "mom=" << jdet.mom()/GeV << " GeV, pT=" << jdet.pT()/GeV << ", eta=" << jdet.eta());
137  for (const JetEffSmearFn& fn : _detFns) {
138  double jeff = -1;
139  tie(jdet, jeff) = fn(jdet); // smear & eff
140  // Re-add constituents & tags if (we assume accidentally) they were lost by the smearing function
141  if (jdet.particles().empty() && !j.particles().empty()) jdet.particles() = j.particles();
142  if (jdet.tags().empty() && !j.tags().empty()) jdet.tags() = j.tags();
143  MSG_DEBUG(" ->" << "mom=" << jdet.mom()/GeV << " GeV, pT=" << jdet.pT()/GeV << ", eta=" << jdet.eta());
144  // MSG_DEBUG("New det jet: "
145  // << "mom=" << jdet.mom()/GeV << " GeV, pT=" << jdet.pT()/GeV << ", eta=" << jdet.eta()
146  // << ", b-tag=" << boolalpha << jdet.bTagged()
147  // << ", c-tag=" << boolalpha << jdet.cTagged()
148  // << " : eff=" << 100*jeff << "%");
149  if (jeff <= 0) { keep = false; break; } //< no need to roll expensive dice (and we deal with -ve probabilities, just in case)
150  if (jeff < 1 && rand01() > jeff) { keep = false; break; } //< roll dice (and deal with >1 probabilities, just in case)
151  }
152  if (keep) _recojets.push_back(jdet);
153  }
154  // Apply tagging efficiencies, using smeared kinematics as input to the tag eff functions
155  for (Jet& j : _recojets) {
156  // Decide whether or not there should be a b-tag on this jet
157  const double beff = _bTagEffFn ? _bTagEffFn(j) : j.bTagged();
158  const bool btag = beff == 1 || (beff != 0 && rand01() < beff);
159  // Remove b-tags if needed, and add a dummy one if needed
160  if (!btag && j.bTagged()) j.tags().erase(std::remove_if(j.tags().begin(), j.tags().end(), hasBottom), j.tags().end());
161  if (btag && !j.bTagged()) j.tags().push_back(Particle(PID::BQUARK, j.mom()));
162  // Decide whether or not there should be a c-tag on this jet
163  const double ceff = _cTagEffFn ? _cTagEffFn(j) : j.cTagged();
164  const bool ctag = ceff == 1 || (ceff != 0 && rand01() < beff);
165  // Remove c-tags if needed, and add a dummy one if needed
166  if (!ctag && j.cTagged()) j.tags().erase(std::remove_if(j.tags().begin(), j.tags().end(), hasCharm), j.tags().end());
167  if (ctag && !j.cTagged()) j.tags().push_back(Particle(PID::CQUARK, j.mom()));
168  }
169  }
170 
171 
173  Jets _jets() const { return _recojets; }
174 
175 
177  void reset() { _recojets.clear(); }
178 
179 
180  private:
181 
183  Jets _recojets;
184 
186  vector<JetEffSmearFn> _detFns;
187 
189  JetEffFn _bTagEffFn, _cTagEffFn;
190 
191  };
192 
193 
194 }
195 
196 #endif
void setName(const std::string &name)
Used by derived classes to set their name.
Definition: Projection.hh:133
Definition: ALICE_2010_I880049.cc:13
void reset()
Reset the projection. Smearing functions will be unchanged.
Definition: SmearedJets.hh:177
int compare(const Projection &p) const
Compare to another SmearedJets.
Definition: SmearedJets.hh:108
Functor for simultaneous efficiency-filtering and smearing of Jets.
Definition: JetSmearingFunctions.hh:73
const FourMomentum & mom() const
Get equivalent single momentum four-vector (const) (alias).
Definition: ParticleBase.hh:39
Particles & particles()
Get the particles in this jet.
Definition: Jet.hh:47
SmearedJets(const JetAlg &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:40
double JET_CTAG_PERFECT(const Jet &j)
Return 1 if the given Jet contains a c, otherwise 0.
Definition: JetSmearingFunctions.hh:41
Particle representation, either from a HepMC::GenEvent or reconstructed.
Definition: Particle.hh:18
SmearedJets(const JetAlg &ja, const JetEffFn &bTagEffFn=JET_BTAG_PERFECT, const JetEffFn &cTagEffFn=JET_CTAG_PERFECT, const initializer_list< JetEffSmearFn > &effSmearFns={})
Constructor with tagging efficiencies, plus an ordered init-list of efficiency and smearing functions...
Definition: SmearedJets.hh:49
DEFAULT_RIVET_PROJ_CLONE(SmearedJets)
Clone on the heap.
double JET_BTAG_PERFECT(const Jet &j)
Return 1 if the given Jet contains a b, otherwise 0.
Definition: JetSmearingFunctions.hh:38
Definition: Event.hh:22
SmearedJets(const JetAlg &ja, const initializer_list< JetEffSmearFn > &effSmearFns, const JetEffFn &bTagEffFn=JET_BTAG_PERFECT, const JetEffFn &cTagEffFn=JET_CTAG_PERFECT)
Constructor with an ordered init-list of efficiency and smearing functions, plus optional tagging eff...
Definition: SmearedJets.hh:66
void project(const Event &e)
Perform the jet finding & smearing calculation.
Definition: SmearedJets.hh:128
size_t size() const
Count the jets.
Definition: JetAlg.hh:178
Cmp< Projection > mkPCmp(const Projection &otherparent, const std::string &pname) const
Definition: Projection.cc:55
SmearedJets(const JetAlg &ja, const JetSmearFn &smearFn, const JetEffFn &bTagEffFn, const JetEffFn &cTagEffFn, const JetEffFn &jetEffFn)
Constructor with trailing efficiency arg.
Definition: SmearedJets.hh:87
double pT() const
Get the directly (alias).
Definition: ParticleBase.hh:63
SmearedJets(const JetAlg &ja, const vector< JetEffSmearFn > &effSmearFns, const JetEffFn &bTagEffFn=JET_BTAG_PERFECT, const JetEffFn &cTagEffFn=JET_CTAG_PERFECT)
Constructor with an ordered vector of efficiency and smearing functions, plus optional tagging effici...
Definition: SmearedJets.hh:74
const PROJ & addProjection(const PROJ &proj, const std::string &name)
Register a contained projection (user-facing version)
Definition: ProjectionApplier.hh:170
Particles & tags()
Particles which have been tag-matched to this jet.
Definition: Jet.hh:89
double eta() const
Get the directly (alias).
Definition: ParticleBase.hh:87
Representation of a clustered jet of particles.
Definition: Jet.hh:18
Base class for all Rivet projections.
Definition: Projection.hh:29
Jets jetsByPt(const Cut &c=Cuts::open()) const
Definition: JetAlg.hh:141
SmearedJets(const JetAlg &ja, const JetEffFn &bTagEffFn=JET_BTAG_PERFECT, const JetEffFn &cTagEffFn=JET_CTAG_PERFECT, const vector< JetEffSmearFn > &effSmearFns={})
Constructor with tagging efficiencies, plus an ordered vector of efficiency and smearing functions...
Definition: SmearedJets.hh:57
Abstract base class for projections which can return a set of Jets.
Definition: JetAlg.hh:15
Wrapper projection for smearing Jets with detector resolutions and efficiencies.
Definition: SmearedJets.hh:33
double rand01()
Return a uniformly sampled random number between 0 and 1.
Definition: Random.cc:39
Cmp< T > cmp(const T &t1, const T &t2)
Global helper function for easy creation of Cmp objects.
Definition: Cmp.hh:285