DAS  3.0
Das Analysis System
JMEmatching.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <queue>
5 #include <utility>
6 #include <vector>
7 
9 
10 #include "Math/VectorUtil.h"
11 
12 namespace DAS {
13 
27 template<typename RecContainer = std::vector<RecJet>,
28  typename GenContainer = std::vector<GenJet>> class JMEmatching {
29 
30  static FourVector getP4 (const FourVector& p4)
31  {
32  return p4;
33  }
34 
35  static FourVector getP4 (const PhysicsObject& phys_obj)
36  {
37  return phys_obj.p4;
38  }
39 
40 public:
41 
42  using RecIt = RecContainer::iterator;
43  using GenIt = GenContainer::iterator;
44  using Couple = std::pair<RecIt, GenIt>;
45 
46  static float maxDR;
47 
48  std::vector<RecIt> fake_its;
49  std::vector<GenIt> miss_its;
50  std::vector<Couple> match_its;
51 
52  JMEmatching (RecContainer& recJets,
53  GenContainer& genJets)
54  {
55  using namespace std;
56 
57  // 1) get the size of the collections
58 
59  size_t nRec = distance(recJets.begin(), recJets.end()),
60  nGen = distance(genJets.begin(), genJets.end());
61 
62  // 2) declare a few utilities
63 
64  using Indices = pair<size_t,size_t>;
65 
66  auto DeltaR2 = [&recJets,&genJets](const Indices& indices) {
67  const FourVector& recJet = getP4(recJets[indices.first ]),
68  & genJet = getP4(genJets[indices.second]);
69  return ROOT::Math::VectorUtil::DeltaR2(recJet, genJet);
70  };
71 
72  auto ascending_DR2 = [&DeltaR2](const Indices& l, const Indices& r) {
73  return DeltaR2(l) > DeltaR2(r);
74  };
75 
76  // 3) make pair candidates
77 
78  // https://en.cppreference.com/w/cpp/container/priority_queue
79  priority_queue< Indices,
80  vector<Indices>, decltype(ascending_DR2)>
81  candidates(ascending_DR2);
82  for (size_t iRec = 0; iRec < nRec; ++iRec)
83  for (size_t iGen = 0; iGen < nGen; ++iGen) {
84  Indices indices {iRec, iGen};
85  auto DR2 = DeltaR2(indices);
86  const auto maxDR2 = pow(maxDR,2);
87  if (DR2 >= maxDR2) continue;
88  candidates.emplace( std::move(indices) );
89  }
90 
91  // 4) keep only the closest pairs (keep track of matched jets to avoid double matching)
92 
93  vector<bool> matchedRec(nRec, false),
94  matchedGen(nGen, false);
95  match_its.reserve(min(nRec,nGen));
96  while (!candidates.empty()) {
97  auto [iRec, iGen] = candidates.top();
98  candidates.pop();
99 
100  // skip already matched objects
101  if (matchedRec.at(iRec)) continue;
102  if (matchedGen.at(iGen)) continue;
103  matchedRec.at(iRec) = true;
104  matchedGen.at(iGen) = true;
105 
106  RecIt rec_it = recJets.begin();
107  GenIt gen_it = genJets.begin();
108  advance(rec_it, iRec);
109  advance(gen_it, iGen);
110 
111  match_its.push_back( make_pair(rec_it, gen_it) );
112  }
113 
114  // 5) fill the unmatched entries
115 
116  fake_its.reserve(nRec);
117  for (int iRec = 0; iRec < nRec; ++iRec) {
118  if (matchedRec.at(iRec)) continue;
119  RecIt fake_it = recJets.begin();
120  advance(fake_it, iRec);
121  fake_its.push_back(fake_it);
122  }
123 
124  miss_its.reserve(nGen);
125  for (int iGen = 0; iGen < nGen; ++iGen) {
126  if (matchedGen.at(iGen)) continue;
127  GenIt miss_it = genJets.begin();
128  advance(miss_it, iGen);
129  miss_its.push_back(miss_it);
130  }
131  }
132 };
133 template<typename RecContainer, typename GenContainer>
135 
136 } // end of DAS namespace
DAS
Definition: applyBTagSF.cc:31
DAS::JMEmatching::getP4
static FourVector getP4(const FourVector &p4)
Definition: JMEmatching.h:30
DAS::JMEmatching::match_its
std::vector< Couple > match_its
Definition: JMEmatching.h:50
DAS::JMEmatching::GenIt
GenContainer::iterator GenIt
Definition: JMEmatching.h:43
DAS::PhysicsObject::p4
FourVector p4
raw four-momentum directly after reconstruction
Definition: PhysicsObject.h:50
DAS::JMEmatching::fake_its
std::vector< RecIt > fake_its
Definition: JMEmatching.h:48
DAS::PhysicsObject
Definition: PhysicsObject.h:41
DAS::JMEmatching::maxDR
static float maxDR
Definition: JMEmatching.h:46
Jet.h
DAS::JMEmatching::JMEmatching
JMEmatching(RecContainer &recJets, GenContainer &genJets)
Definition: JMEmatching.h:52
DAS::JMEmatching::getP4
static FourVector getP4(const PhysicsObject &phys_obj)
Definition: JMEmatching.h:35
DAS::JMEmatching::Couple
std::pair< RecIt, GenIt > Couple
Definition: JMEmatching.h:44
DAS::JMEmatching::miss_its
std::vector< GenIt > miss_its
Definition: JMEmatching.h:49
DAS::FourVector
ROOT::Math::LorentzVector< ROOT::Math::PtEtaPhiM4D< float > > FourVector
Definition: PhysicsObject.h:15
DAS::JMEmatching::RecIt
RecContainer::iterator RecIt
Definition: JMEmatching.h:42
DAS::JMEmatching
Definition: JMEmatching.h:28