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  this->refJets = refJets;
66  refToRec.clear();
67  refToRec.resize(refJets.size(), -1);
68 
69  this->recJets = recJets;
70  recToRef.clear();
71  recToRef.resize(recJets.size(), -1);
72 
73  Matching<double> matching(corrRefJets, corrRecJets,
74  [this](auto& v1, auto& v2)
75  {
76  double x = ROOT::Math::VectorUtil::DeltaR2(v1, v2) / this->sigmaDeltaR2 +
77  sqr(2. * (v1.R() - v2.R()) / (v1.R() + v2.R())) / this->sigmaDeltaE2;
78  // Possible debug output
79  // std::cout << "xxx " << ROOT::Math::VectorUtil::DeltaPhi(v1, v2) << " " << (v1.Eta() - v2.Eta())
80  // << " " << (v1.R() - v2.R()) / (v1.R() + v2.R()) << " " << x << std::endl;
81  return x;
82  });
83 
85 
86  const std::vector<Match>& matches =
87  matching.match(std::less<double>(),
88  [&](auto &c){ return c < this->maxChi2;});
89  for(std::vector<Match>::const_iterator iter = matches.begin();
90  iter != matches.end(); ++iter) {
91  refToRec[iter->index1] = iter->index2;
92  recToRef[iter->index2] = iter->index1;
93  }
94 }
95 
98 {
100  if (recJet.id() != recJets.id())
101  return result;
102 
103  for(unsigned int i = 0; i != recJets.size(); ++i) {
104  if (recJets[i] == recJet) {
105  int match = recToRef[i];
106  if (match >= 0)
107  result = refJets[match];
108  break;
109  }
110  }
111 
112  return result;
113 }
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:97
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