CMS 3D CMS Logo

MatchJet.cc
Go to the documentation of this file.
1 #include <functional>
2 #include <algorithm>
3 #include <vector>
4 #include <memory>
5 #include <string>
6 
7 #include <Math/GenVector/Cartesian3D.h>
8 #include <Math/GenVector/VectorUtil.h>
9 
11 
12 #include "Matching.h"
13 
14 using namespace btag;
15 
16 namespace {
17  inline double sqr(double val) { return val * val; }
18 }
19 
21  maxChi2(pSet.getParameter<double>("maxChi2")),
22  sigmaDeltaR2(sqr(pSet.getParameter<double>("sigmaDeltaR"))),
23  sigmaDeltaE2(sqr(pSet.getParameter<double>("sigmaDeltaE"))),
24  threshold(1.0)
25 {
26 }
27 
29  const edm::RefToBaseVector<reco::Jet> & refJets_,
30  const edm::RefToBaseVector<reco::Jet> & recJets_,
32  )
33 {
34  refJetCorrector.setCorrector(corrector);
35  recJetCorrector.setCorrector(corrector);
36 
37  typedef ROOT::Math::Cartesian3D<double> Vector;
38 
39  std::vector<Vector> corrRefJets;
40  refJets.clear();
42  iter != refJets_.end(); ++iter) {
43  edm::RefToBase<reco::Jet> jetRef = *iter;
44  reco::Jet jet = refJetCorrector(*jetRef);
45  if (jet.energy() < threshold)
46  continue;
47 
48  corrRefJets.push_back(Vector(jet.px(), jet.py(), jet.pz()));
49  refJets.push_back(jetRef);
50  }
51 
52  std::vector<Vector> corrRecJets;
53  recJets.clear();
55  iter != recJets_.end(); ++iter) {
56  edm::RefToBase<reco::Jet> jetRec = *iter;
57  reco::Jet jet = recJetCorrector(*jetRec);
58  if (jet.energy() < threshold)
59  continue;
60 
61  corrRecJets.push_back(Vector(jet.px(), jet.py(), jet.pz()));
62  recJets.push_back(jetRec);
63  }
64 
65  refToRec.clear();
66  refToRec.resize(refJets.size(), -1);
67 
68  recToRef.clear();
69  recToRef.resize(recJets.size(), -1);
70 
71  Matching<double> matching(corrRefJets, corrRecJets,
72  [this](auto& v1, auto& v2)
73  {
74  double x = ROOT::Math::VectorUtil::DeltaR2(v1, v2) / this->sigmaDeltaR2 +
75  sqr(2. * (v1.R() - v2.R()) / (v1.R() + v2.R())) / this->sigmaDeltaE2;
76  // Possible debug output
77  // std::cout << "xxx " << ROOT::Math::VectorUtil::DeltaPhi(v1, v2) << " " << (v1.Eta() - v2.Eta())
78  // << " " << (v1.R() - v2.R()) / (v1.R() + v2.R()) << " " << x << std::endl;
79  return x;
80  });
81 
83 
84  const std::vector<Match>& matches =
85  matching.match(std::less<double>(),
86  [&](auto &c){ return c < this->maxChi2;});
87  for(std::vector<Match>::const_iterator iter = matches.begin();
88  iter != matches.end(); ++iter) {
89  refToRec[iter->index1] = iter->index2;
90  recToRef[iter->index2] = iter->index1;
91  }
92 }
93 
96 {
98  if (recJet.id() != recJets.id())
99  return result;
100 
101  for(unsigned int i = 0; i != recJets.size(); ++i) {
102  if (recJets[i] == recJet) {
103  int match = recToRef[i];
104  if (match >= 0)
105  result = refJets[match];
106  break;
107  }
108  }
109 
110  return result;
111 }
double sigmaDeltaE2
Definition: MatchJet.h:47
ROOT::Math::Plane3D::Vector Vector
Definition: EcalHitMaker.cc:29
Base class for all types of Jets.
Definition: Jet.h:20
double px() const final
x coordinate of momentum vector
edm::RefToBaseVector< reco::Jet > refJets
Definition: MatchJet.h:39
const_iterator end() const
std::vector< int > recToRef
Definition: MatchJet.h:38
edm::RefToBaseVector< reco::Jet > recJets
Definition: MatchJet.h:40
ProductID id() const
Definition: RefToBase.h:242
Definition: Matching.h:10
void matchCollections(const edm::RefToBaseVector< reco::Jet > &refJets, const edm::RefToBaseVector< reco::Jet > &recJets, const reco::JetCorrector *corrector)
match the collections
Definition: MatchJet.cc:28
std::vector< int > refToRec
Definition: MatchJet.h:38
double maxChi2
Definition: MatchJet.h:45
double pz() const final
z coordinate of momentum vector
double energy() const final
energy
double sigmaDeltaR2
Definition: MatchJet.h:46
size_type size() const
edm::RefToBase< reco::Jet > operator()(const edm::RefToBase< reco::Jet > &recJet) const
Returns the matched "reference" jet.
Definition: MatchJet.cc:95
CorrectJet refJetCorrector
Definition: MatchJet.h:42
CorrectJet recJetCorrector
Definition: MatchJet.h:43
void setCorrector(const reco::JetCorrector *corrector)
Returns the corrected jet.
Definition: CorrectJet.h:22
double py() const final
y coordinate of momentum vector
const_iterator begin() const
def Match(required, got)
Square< F >::type sqr(const F &f)
Definition: Square.h:13
void push_back(const RefToBase< T > &)
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
double threshold
Definition: MatchJet.h:48
MatchJet()
Definition: MatchJet.h:22
ProductID id() const