CMS 3D CMS Logo

L1MuonMatcherAlgo.h
Go to the documentation of this file.
1 #ifndef MuonAnalysis_MuonAssociators_interface_L1MuonMatcherAlgo_h
2 #define MuonAnalysis_MuonAssociators_interface_L1MuonMatcherAlgo_h
3 
10 #include <cmath>
11 #include <type_traits>
25 
26 template <edm::Transition Tr>
28 public:
30  ~L1MuonMatcherAlgoT() = default;
31 
33  void init(const edm::EventSetup &iSetup);
34 
37 
40 
43  return prop_.extrapolate(tk, vtx);
44  }
45 
48 
52  const PropagateToMuon &propagatorToMuon() const { return prop_; }
53 
56  bool match(const reco::Track &tk,
57  const l1extra::L1MuonParticle &l1,
58  float &deltaR,
59  float &deltaPhi,
60  TrajectoryStateOnSurface &propagated) const {
61  propagated = extrapolate(tk);
62  return propagated.isValid() ? match(propagated, l1, deltaR, deltaPhi) : false;
63  }
64 
67  bool match(const reco::Candidate &c,
68  const l1extra::L1MuonParticle &l1,
69  float &deltaR,
70  float &deltaPhi,
71  TrajectoryStateOnSurface &propagated) const {
72  propagated = extrapolate(c);
73  return propagated.isValid() ? match(propagated, l1, deltaR, deltaPhi) : false;
74  }
75 
78  bool match(const SimTrack &tk,
79  const edm::SimVertexContainer &vtxs,
80  const l1extra::L1MuonParticle &l1,
81  float &deltaR,
82  float &deltaPhi,
83  TrajectoryStateOnSurface &propagated) const {
84  propagated = extrapolate(tk, vtxs);
85  return propagated.isValid() ? match(propagated, l1, deltaR, deltaPhi) : false;
86  }
87 
90  bool match(TrajectoryStateOnSurface &propagated,
91  const l1extra::L1MuonParticle &l1,
92  float &deltaR,
93  float &deltaPhi) const;
94 
95  // Methods to match with vectors of legacy L1 muon trigger objects
96 
100  int match(const reco::Track &tk,
101  const std::vector<l1extra::L1MuonParticle> &l1,
102  float &deltaR,
103  float &deltaPhi,
104  TrajectoryStateOnSurface &propagated) const {
105  propagated = extrapolate(tk);
106  return propagated.isValid() ? match(propagated, l1, deltaR, deltaPhi) : -1;
107  }
108 
112  int match(const reco::Candidate &c,
113  const std::vector<l1extra::L1MuonParticle> &l1,
114  float &deltaR,
115  float &deltaPhi,
116  TrajectoryStateOnSurface &propagated) const {
117  propagated = extrapolate(c);
118  return propagated.isValid() ? match(propagated, l1, deltaR, deltaPhi) : -1;
119  }
120 
124  int match(const SimTrack &tk,
125  const edm::SimVertexContainer &vtxs,
126  const std::vector<l1extra::L1MuonParticle> &l1,
127  float &deltaR,
128  float &deltaPhi,
129  TrajectoryStateOnSurface &propagated) const {
130  propagated = extrapolate(tk, vtxs);
131  return propagated.isValid() ? match(propagated, l1, deltaR, deltaPhi) : -1;
132  }
133 
137  int match(TrajectoryStateOnSurface &propagated,
138  const std::vector<l1extra::L1MuonParticle> &l1,
139  float &deltaR,
140  float &deltaPhi) const;
141 
142  // Methods to match with vectors of stage2 L1 muon trigger objects
143 
147  int match(const reco::Track &tk,
148  const std::vector<l1t::Muon> &l1,
149  float &deltaR,
150  float &deltaPhi,
151  TrajectoryStateOnSurface &propagated) const {
152  propagated = extrapolate(tk);
153  return propagated.isValid() ? match(propagated, l1, deltaR, deltaPhi) : -1;
154  }
155 
159  int match(const reco::Candidate &c,
160  const std::vector<l1t::Muon> &l1,
161  float &deltaR,
162  float &deltaPhi,
163  TrajectoryStateOnSurface &propagated) const {
164  propagated = extrapolate(c);
165  return propagated.isValid() ? match(propagated, l1, deltaR, deltaPhi) : -1;
166  }
167 
171  int match(const SimTrack &tk,
172  const edm::SimVertexContainer &vtxs,
173  const std::vector<l1t::Muon> &l1,
174  float &deltaR,
175  float &deltaPhi,
176  TrajectoryStateOnSurface &propagated) const {
177  propagated = extrapolate(tk, vtxs);
178  return propagated.isValid() ? match(propagated, l1, deltaR, deltaPhi) : -1;
179  }
180 
184  int match(TrajectoryStateOnSurface &propagated,
185  const std::vector<l1t::Muon> &l1,
186  float &deltaR,
187  float &deltaPhi) const;
188 
189  // Generic matching
190 
195  template <typename Collection, typename Selector>
196  int matchGeneric(const reco::Track &tk,
197  const Collection &l1,
198  const Selector &sel,
199  float &deltaR,
200  float &deltaPhi,
201  TrajectoryStateOnSurface &propagated) const {
202  propagated = extrapolate(tk);
203  return propagated.isValid() ? matchGeneric(propagated, l1, sel, deltaR, deltaPhi) : -1;
204  }
205 
210  template <typename Collection, typename Selector>
212  const Collection &l1,
213  const Selector &sel,
214  float &deltaR,
215  float &deltaPhi,
216  TrajectoryStateOnSurface &propagated) const {
217  propagated = extrapolate(c);
218  return propagated.isValid() ? matchGeneric(propagated, l1, sel, deltaR, deltaPhi) : -1;
219  }
220 
225  template <typename Collection, typename Selector>
226  int matchGeneric(TrajectoryStateOnSurface &propagated,
227  const Collection &l1,
228  const Selector &sel,
229  float &deltaR,
230  float &deltaPhi) const;
231 
234 
235 private:
236  template <class T>
237  int genericQuality(T const &cand) const {
238  return 0;
239  }
240 
241  int genericQuality(l1extra::L1MuonParticle const &cand) const { return cand.gmtMuonCand().rank(); }
242  int genericQuality(l1t::Muon const &cand) const { return cand.hwQual(); }
243 
246 
248 
252 
255 
259 
261  double l1PhiOffset_;
262 };
263 
264 template <edm::Transition Tr>
265 template <typename Collection, typename Selector>
267  const Collection &l1s,
268  const Selector &sel,
269  float &deltaR,
270  float &deltaPhi) const {
271  typedef typename Collection::value_type obj;
272 
273  int match = -1;
274  double minDeltaPhi = deltaPhi_;
275  double minDeltaEta = deltaEta_;
276  double minDeltaR2 = deltaR2_;
277  double maxPt = -1.0;
278  int maxQual = 0;
279  GlobalPoint pos = propagated.globalPosition();
280  for (int i = 0, n = l1s.size(); i < n; ++i) {
281  const obj &l1 = l1s[i];
282  if (sel(l1)) {
283  double thisDeltaPhi = ::deltaPhi(double(pos.phi()), l1.phi() + l1PhiOffset_);
284  double thisDeltaEta = pos.eta() - l1.eta();
285  double thisDeltaR2 = ::deltaR2(double(pos.eta()), double(pos.phi()), l1.eta(), l1.phi() + l1PhiOffset_);
286  int thisQual = genericQuality<obj>(l1);
287  double thisPt = l1.pt();
288  if ((fabs(thisDeltaPhi) < deltaPhi_) && (fabs(thisDeltaEta) < deltaEta_) &&
289  (thisDeltaR2 < deltaR2_)) { // check all
290  bool betterMatch = (match == -1);
291  switch (sortBy_) {
292  case SortByDeltaR:
293  betterMatch = (thisDeltaR2 < minDeltaR2);
294  break;
295  case SortByDeltaEta:
296  betterMatch = (fabs(thisDeltaEta) < fabs(minDeltaEta));
297  break;
298  case SortByDeltaPhi:
299  betterMatch = (fabs(thisDeltaPhi) < fabs(minDeltaPhi));
300  break;
301  case SortByPt:
302  betterMatch = (thisPt > maxPt);
303  break;
304  // Quality is an int, adding sorting by pT in case of identical qualities
305  case SortByQual:
306  betterMatch = (thisQual > maxQual || ((thisQual == maxQual) && (thisPt > maxPt)));
307  break;
308  }
309  if (betterMatch) { // sort on one
310  match = i;
311  deltaR = std::sqrt(thisDeltaR2);
312  deltaPhi = thisDeltaPhi;
313  minDeltaR2 = thisDeltaR2;
314  minDeltaEta = thisDeltaEta;
315  minDeltaPhi = thisDeltaPhi;
316  maxQual = thisQual;
317  maxPt = thisPt;
318  }
319  }
320  }
321  }
322  return match;
323 }
324 
325 template <edm::Transition Tr>
327  : propSetup_(iConfig, iCollector),
328  useStage2L1_(iConfig.existsAs<bool>("useStage2L1") ? iConfig.getParameter<bool>("useStage2L1") : false),
329  preselectionCut_(
330  (iConfig.existsAs<std::string>("preselection")) ? iConfig.getParameter<std::string>("preselection") : ""),
331  deltaR2_(std::pow(iConfig.getParameter<double>("maxDeltaR"), 2)),
332  deltaPhi_(iConfig.existsAs<double>("maxDeltaPhi") ? iConfig.getParameter<double>("maxDeltaPhi") : 10),
333  deltaEta_(iConfig.existsAs<double>("maxDeltaEta") ? iConfig.getParameter<double>("maxDeltaEta") : 10),
334  l1PhiOffset_(iConfig.existsAs<double>("l1PhiOffset") ? iConfig.getParameter<double>("l1PhiOffset") : 0) {
335  bool reqQual = iConfig.existsAs<bool>("sortByQual") && iConfig.getParameter<bool>("sortByQual");
336  bool reqPhi = iConfig.existsAs<bool>("sortByDeltaPhi") && iConfig.getParameter<bool>("sortByDeltaPhi");
337  bool reqEta = iConfig.existsAs<bool>("sortByDeltaEta") && iConfig.getParameter<bool>("sortByDeltaEta");
338  bool reqPt = iConfig.existsAs<bool>("sortByPt") && iConfig.getParameter<bool>("sortByPt");
339  std::string sortBy = iConfig.existsAs<std::string>("sortBy") ? iConfig.getParameter<std::string>("sortBy") : "";
340  if (reqPhi + reqEta + reqPt + reqQual > 1)
341  throw cms::Exception("Configuration")
342  << "L1MuonMatcherAlgoT: Can't set more than one 'sortBy<XXX>' parameter to True.\n";
343  if (sortBy == "deltaPhi") {
344  if (reqEta || reqPt || reqQual)
345  throw cms::Exception("Configuration") << "L1MuonMatcherAlgoT: Can't set sortBy = 'deltaPhi' and set also another "
346  "'sortBy<XXX>' parameter to True.\n";
347  else
348  reqPhi = true;
349  }
350  if (sortBy == "deltaEta") {
351  if (reqPhi || reqPt || reqQual)
352  throw cms::Exception("Configuration") << "L1MuonMatcherAlgoT: Can't set sortBy = 'deltaEta' and set also another "
353  "'sortBy<XXX>' parameter to True.\n";
354  else
355  reqEta = true;
356  }
357  if (sortBy == "pt") {
358  if (reqPhi || reqEta || reqQual)
359  throw cms::Exception("Configuration")
360  << "L1MuonMatcherAlgoT: Can't set sortBy = 'pt' and set also another 'sortBy<XXX>' parameter to True.\n";
361  else
362  reqPt = true;
363  }
364  if (sortBy == "quality") {
365  if (reqPhi || reqEta || reqPt)
366  throw cms::Exception("Configuration")
367  << "L1MuonMatcherAlgoT: Can't set sortBy = 'quality' and set also another 'sortBy<XXX>' parameter to True.\n";
368  else
369  reqQual = true;
370  }
371  if (sortBy == "deltaR") {
372  if (reqPhi || reqEta || reqPt || reqQual)
373  throw cms::Exception("Configuration")
374  << "L1MuonMatcherAlgoT: Can't set sortBy = 'deltaR' and set also another 'sortBy<XXX>' parameter to True.\n";
375  }
376  // so, if we're here there's no ambiguity in what the user may want. either everything is false, or exactly one req is true.
377  if (reqEta)
379  else if (reqPhi)
381  else if (reqQual)
383  else if (reqPt)
384  sortBy_ = SortByPt;
385  else
387 }
388 
389 template <edm::Transition Tr>
391  prop_ = propSetup_.init(iSetup);
392 }
393 
394 template <edm::Transition Tr>
396  const l1extra::L1MuonParticle &l1,
397  float &deltaR,
398  float &deltaPhi) const {
399  if (preselectionCut_(l1)) {
400  GlobalPoint pos = propagated.globalPosition();
401  double thisDeltaPhi = ::deltaPhi(double(pos.phi()), l1.phi() + l1PhiOffset_);
402  double thisDeltaEta = pos.eta() - l1.eta();
403  double thisDeltaR2 = ::deltaR2(double(pos.eta()), double(pos.phi()), l1.eta(), l1.phi() + l1PhiOffset_);
404  if ((fabs(thisDeltaPhi) < deltaPhi_) && (fabs(thisDeltaEta) < deltaEta_) && (thisDeltaR2 < deltaR2_)) {
405  deltaR = std::sqrt(thisDeltaR2);
406  deltaPhi = thisDeltaPhi;
407  return true;
408  }
409  }
410  return false;
411 }
412 
413 template <edm::Transition Tr>
415  const std::vector<l1extra::L1MuonParticle> &l1s,
416  float &deltaR,
417  float &deltaPhi) const {
418  return matchGeneric(propagated, l1s, preselectionCut_, deltaR, deltaPhi);
419 }
420 
421 template <edm::Transition Tr>
423  const std::vector<l1t::Muon> &l1s,
424  float &deltaR,
425  float &deltaPhi) const {
426  return matchGeneric(propagated, l1s, preselectionCut_, deltaR, deltaPhi);
427 }
428 
430 
431 #endif
StringCutObjectSelector< reco::Candidate, true > L1Selector
~L1MuonMatcherAlgoT()=default
Definition: L1Scalers.h:16
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
int genericQuality(l1extra::L1MuonParticle const &cand) const
int match(const SimTrack &tk, const edm::SimVertexContainer &vtxs, const std::vector< l1extra::L1MuonParticle > &l1, float &deltaR, float &deltaPhi, TrajectoryStateOnSurface &propagated) const
const PropagateToMuon & propagatorToMuon() const
Return the propagator to second muon station (in case it&#39;s needed)
L1MuonMatcherAlgoT(const edm::ParameterSet &iConfig, edm::ConsumesCollector)
TrajectoryStateOnSurface extrapolate(const reco::Track &tk) const
Extrapolate reco::Track to the muon station 2, return an invalid TSOS if it fails.
int match(const SimTrack &tk, const edm::SimVertexContainer &vtxs, const std::vector< l1t::Muon > &l1, float &deltaR, float &deltaPhi, TrajectoryStateOnSurface &propagated) const
bool match(const reco::Track &tk, const l1extra::L1MuonParticle &l1, float &deltaR, float &deltaPhi, TrajectoryStateOnSurface &propagated) const
int genericQuality(l1t::Muon const &cand) const
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:172
L1Selector preselectionCut_
Preselection cut to apply to L1 candidates before matching.
int genericQuality(T const &cand) const
TrajectoryStateOnSurface extrapolate(const reco::Candidate &tk) const
Extrapolate reco::Candidate to the muon station 2, return an invalid TSOS if it fails.
int match(const reco::Track &tk, const std::vector< l1extra::L1MuonParticle > &l1, float &deltaR, float &deltaPhi, TrajectoryStateOnSurface &propagated) const
void init(const edm::EventSetup &iSetup)
Call this method at the beginning of each run, to initialize geometry, magnetic field and propagators...
double l1PhiOffset_
offset to be added to the L1 phi before the match
bool match(const SimTrack &tk, const edm::SimVertexContainer &vtxs, const l1extra::L1MuonParticle &l1, float &deltaR, float &deltaPhi, TrajectoryStateOnSurface &propagated) const
Propagate an object (usually a track) to the second (default) or first muon station.
GlobalPoint globalPosition() const
bool match(const reco::Candidate &c, const l1extra::L1MuonParticle &l1, float &deltaR, float &deltaPhi, TrajectoryStateOnSurface &propagated) const
TrajectoryStateOnSurface extrapolate(const SimTrack &tk, const edm::SimVertexContainer &vtx) const
Extrapolate a SimTrack to the muon station 2, return an invalid TSOS if it fails. Requires SimVertice...
T sqrt(T t)
Definition: SSEVec.h:23
double deltaR2_
Matching cuts.
void setL1PhiOffset(double l1PhiOffset)
Add this offset to the L1 phi before doing the match, to correct for different scales in L1 vs offlin...
int matchGeneric(const reco::Track &tk, const Collection &l1, const Selector &sel, float &deltaR, float &deltaPhi, TrajectoryStateOnSurface &propagated) const
Functor that operates on <T>
Definition: Selector.h:22
PropagateToMuon & propagatorToMuon()
Return the propagator to second muon station (in case it&#39;s needed)
Definition: Muon.h:21
int match(const reco::Track &tk, const std::vector< l1t::Muon > &l1, float &deltaR, float &deltaPhi, TrajectoryStateOnSurface &propagated) const
maxPt
Definition: PV_cfg.py:224
std::vector< SimVertex > SimVertexContainer
TrajectoryStateOnSurface extrapolate(const FreeTrajectoryState &state) const
Extrapolate a FreeTrajectoryState to the muon station 2, return an invalid TSOS if it fails...
int matchGeneric(const reco::Candidate &c, const Collection &l1, const Selector &sel, float &deltaR, float &deltaPhi, TrajectoryStateOnSurface &propagated) const
PropagateToMuonSetupT< Tr > propSetup_
Matcher of reconstructed objects to L1 Muons.
SortBy
Sort by deltaPhi or deltaEta instead of deltaR.
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
int match(const reco::Candidate &c, const std::vector< l1t::Muon > &l1, float &deltaR, float &deltaPhi, TrajectoryStateOnSurface &propagated) const
int match(const reco::Candidate &c, const std::vector< l1extra::L1MuonParticle > &l1, float &deltaR, float &deltaPhi, TrajectoryStateOnSurface &propagated) const
TrajectoryStateOnSurface extrapolate(const reco::Track &tk) const
Extrapolate reco::Track to the muon station 2, return an invalid TSOS if it fails.
long double T
double phi() const final
momentum azimuthal angle
PropagateToMuon prop_
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
double eta() const final
momentum pseudorapidity