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