CMS 3D CMS Logo

MuonMvaEstimator.cc
Go to the documentation of this file.
2 
13 
14 using namespace pat;
15 
16 MuonMvaEstimator::MuonMvaEstimator(const edm::FileInPath& weightsfile, float dRmax) : dRmax_(dRmax) {
17  gbrForest_ = createGBRForest(weightsfile);
18 }
19 
21 
22 namespace {
23 
24  enum inputIndexes {
25  kPt,
26  kEta,
27  kJetNDauCharged,
28  kMiniRelIsoCharged,
29  kMiniRelIsoNeutral,
30  kJetPtRel,
31  kJetBTagCSV,
32  kJetPtRatio,
33  kLog_abs_dxyBS,
34  kSip,
35  kLog_abs_dzPV,
36  kSegmentCompatibility,
37  kLast
38  };
39 
40  float ptRel(const reco::Candidate::LorentzVector& muP4,
41  const reco::Candidate::LorentzVector& jetP4,
42  bool subtractMuon = true) {
44  if (subtractMuon)
45  jp4 -= muP4;
46  float dot = muP4.Vect().Dot(jp4.Vect());
47  float ptrel = muP4.P2() - dot * dot / jp4.P2();
48  ptrel = ptrel > 0 ? sqrt(ptrel) : 0.0;
49  return ptrel;
50  }
51 } // namespace
52 
54  const reco::Vertex& vertex,
56  float& jetPtRatio,
57  float& jetPtRel,
58  float& miniIsoValue,
59  const reco::JetCorrector* correctorL1,
60  const reco::JetCorrector* correctorL1L2L3Res) const {
61  float var[kLast]{};
62 
63  var[kPt] = muon.pt();
64  var[kEta] = muon.eta();
65  var[kSegmentCompatibility] = muon.segmentCompatibility();
66  var[kMiniRelIsoCharged] = muon.miniPFIsolation().chargedHadronIso() / muon.pt();
67  var[kMiniRelIsoNeutral] = miniIsoValue - var[kMiniRelIsoCharged];
68 
69  double dB2D = fabs(muon.dB(pat::Muon::PV2D));
70  double dB3D = muon.dB(pat::Muon::PV3D);
71  double edB3D = muon.edB(pat::Muon::PV3D);
72  double dz = fabs(muon.muonBestTrack()->dz(vertex.position()));
73  var[kSip] = edB3D > 0 ? fabs(dB3D / edB3D) : 0.0;
74  var[kLog_abs_dxyBS] = dB2D > 0 ? log(dB2D) : 0;
75  var[kLog_abs_dzPV] = dz > 0 ? log(dz) : 0;
76 
77  //Initialise loop variables
78  double minDr = 9999;
79  double jecL1L2L3Res = 1.;
80  double jecL1 = 1.;
81 
82  // Compute corrected isolation variables
83  double chIso = muon.pfIsolationR04().sumChargedHadronPt;
84  double nIso = muon.pfIsolationR04().sumNeutralHadronEt;
85  double phoIso = muon.pfIsolationR04().sumPhotonEt;
86  double puIso = muon.pfIsolationR04().sumPUPt;
87  double dbCorrectedIsolation = chIso + std::max(nIso + phoIso - .5 * puIso, 0.);
88  double dbCorrectedRelIso = dbCorrectedIsolation / muon.pt();
89 
90  var[kJetPtRatio] = 1. / (1 + dbCorrectedRelIso);
91  var[kJetPtRel] = 0;
92  var[kJetBTagCSV] = -999;
93  var[kJetNDauCharged] = -1;
94 
95  for (const auto& tagI : bTags) {
96  // for each muon with the lepton
97  double dr = deltaR(*(tagI.first), muon);
98  if (dr > minDr)
99  continue;
100  minDr = dr;
101 
102  const reco::Candidate::LorentzVector& muP4(muon.p4());
103  reco::Candidate::LorentzVector jetP4(tagI.first->p4());
104 
105  if (correctorL1 && correctorL1L2L3Res) {
106  jecL1L2L3Res = correctorL1L2L3Res->correction(*(tagI.first));
107  jecL1 = correctorL1->correction(*(tagI.first));
108  }
109 
110  // Get b-jet info
111  var[kJetBTagCSV] = tagI.second;
112  var[kJetNDauCharged] = 0;
113  for (auto jet : tagI.first->getJetConstituentsQuick()) {
114  const reco::PFCandidate* pfcand = dynamic_cast<const reco::PFCandidate*>(jet);
115  if (pfcand == nullptr)
116  throw cms::Exception("ConfigurationError") << "Cannot get jet constituents";
117  if (pfcand->charge() == 0)
118  continue;
119  auto bestTrackPtr = pfcand->bestTrack();
120  if (!bestTrackPtr)
121  continue;
122  if (!bestTrackPtr->quality(reco::Track::highPurity))
123  continue;
124  if (bestTrackPtr->pt() < 1.)
125  continue;
126  if (bestTrackPtr->hitPattern().numberOfValidHits() < 8)
127  continue;
128  if (bestTrackPtr->hitPattern().numberOfValidPixelHits() < 2)
129  continue;
130  if (bestTrackPtr->normalizedChi2() >= 5)
131  continue;
132 
133  if (std::fabs(bestTrackPtr->dxy(vertex.position())) > 0.2)
134  continue;
135  if (std::fabs(bestTrackPtr->dz(vertex.position())) > 17)
136  continue;
137  var[kJetNDauCharged]++;
138  }
139 
140  if (minDr < dRmax_) {
141  if ((jetP4 - muP4).Rho() < 0.0001) {
142  var[kJetPtRel] = 0;
143  var[kJetPtRatio] = 1;
144  } else {
145  jetP4 -= muP4 / jecL1;
146  jetP4 *= jecL1L2L3Res;
147  jetP4 += muP4;
148 
149  var[kJetPtRatio] = muP4.pt() / jetP4.pt();
150  var[kJetPtRel] = ptRel(muP4, jetP4);
151  }
152  }
153  }
154 
155  if (var[kJetPtRatio] > 1.5)
156  var[kJetPtRatio] = 1.5;
157  if (var[kJetBTagCSV] < 0)
158  var[kJetBTagCSV] = 0;
159  jetPtRatio = var[kJetPtRatio];
160  jetPtRel = var[kJetPtRel];
161  return gbrForest_->GetClassifier(var);
162 };
example_track example_track example_trackconst char *const kPt
Definition: TSelector.cc:34
MuonMvaEstimator(const edm::FileInPath &weightsfile, float dRmax)
double correction(const LorentzVector &fJet) const
get correction using Jet information only
Definition: JetCorrector.h:46
T dot(const Basic3DVector &v) const
Scalar product, or "dot" product, with a vector of same type.
Definition: HeavyIon.h:7
float computeMva(const pat::Muon &imuon, const reco::Vertex &vertex, const reco::JetTagCollection &bTags, float &jetPtRatio, float &jetPtRel, float &miniIsoValue, const reco::JetCorrector *correctorL1=nullptr, const reco::JetCorrector *correctorL1L2L3Res=nullptr) const
inputIndexes
T sqrt(T t)
Definition: SSEVec.h:19
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
std::unique_ptr< const GBRForest > gbrForest_
Analysis-level muon class.
Definition: Muon.h:51
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)