CMS 3D CMS Logo

MuonMvaEstimator.cc
Go to the documentation of this file.
2 
12 
13 #include "TMVA/Reader.h"
14 #include "TMVA/MethodBDT.h"
15 
16 using namespace pat;
17 
18 namespace {
19  constexpr char muon_mva_name[] = "BDTG";
20 }
21 
22 MuonMvaEstimator::MuonMvaEstimator(const std::string& weightsfile, float dRmax):
23  dRmax_(dRmax)
24 {
25  TMVA::Reader tmvaReader("!Color:!Silent:Error");
26  tmvaReader.AddVariable("LepGood_pt", &pt_ );
27  tmvaReader.AddVariable("LepGood_eta", &eta_ );
28  tmvaReader.AddVariable("LepGood_jetNDauChargedMVASel", &jetNDauCharged_ );
29  tmvaReader.AddVariable("LepGood_miniRelIsoCharged", &miniRelIsoCharged_);
30  tmvaReader.AddVariable("LepGood_miniRelIsoNeutral", &miniRelIsoNeutral_);
31  tmvaReader.AddVariable("LepGood_jetPtRelv2", &jetPtRel_ );
32  tmvaReader.AddVariable("max(LepGood_jetBTagCSV,0)", &jetBTagCSV_ );
33  tmvaReader.AddVariable("(LepGood_jetBTagCSV>-5)*min(LepGood_jetPtRatiov2,1.5)+(LepGood_jetBTagCSV<-5)/(1+LepGood_relIso04)", &jetPtRatio_ );
34  tmvaReader.AddVariable("LepGood_sip3d", &sip_ );
35  tmvaReader.AddVariable("log(abs(LepGood_dxy))", &log_abs_dxyBS_ );
36  tmvaReader.AddVariable("log(abs(LepGood_dz))", &log_abs_dzPV_ );
37  tmvaReader.AddVariable("LepGood_segmentCompatibility", &segmentCompatibility_);
38 
39  auto temp{ tmvaReader.BookMVA(muon_mva_name, weightsfile.c_str()) };
40  gbrForest_ = std::make_unique<GBRForest>( dynamic_cast<TMVA::MethodBDT*>( temp ) );
41 }
42 
44 
45 namespace {
46 
47  enum inputIndexes {
48  kPt,
49  kEta,
50  kJetNDauCharged,
51  kMiniRelIsoCharged,
52  kMiniRelIsoNeutral,
53  kJetPtRel,
54  kJetPtRatio,
55  kJetBTagCSV,
56  kSip,
57  kLog_abs_dxyBS,
58  kLog_abs_dzPV,
59  kSegmentCompatibility,
60  kLast
61  };
62 
64  bool subtractMuon=true)
65  {
67  if (subtractMuon) jp4-=muP4;
68  float dot = muP4.Vect().Dot( jp4.Vect() );
69  float ptrel = muP4.P2() - dot*dot/jp4.P2();
70  ptrel = ptrel>0 ? sqrt(ptrel) : 0.0;
71  return ptrel;
72  }
73 }
74 
76  const reco::Vertex& vertex,
78  float& jetPtRatio,
79  float& jetPtRel,
80  const reco::JetCorrector* correctorL1,
81  const reco::JetCorrector* correctorL1L2L3Res) const
82 {
83  float var[kLast]{};
84 
85  var[kPt] = muon.pt();
86  var[kEta] = muon.eta();
87  var[kSegmentCompatibility] = muon.segmentCompatibility();
88  var[kMiniRelIsoCharged] = muon.miniPFIsolation().chargedHadronIso();
89  var[kMiniRelIsoNeutral] = muon.miniPFIsolation().neutralHadronIso();
90 
91  double dB2D = fabs(muon.dB(pat::Muon::BS2D));
92  double dB3D = muon.dB(pat::Muon::PV3D);
93  double edB3D = muon.edB(pat::Muon::PV3D);
94  double dz = fabs(muon.muonBestTrack()->dz(vertex.position()));
95  var[kSip] = edB3D>0?fabs(dB3D/edB3D):0.0;
96  var[kLog_abs_dxyBS] = dB2D>0?log(dB2D):0;
97  var[kLog_abs_dzPV] = dz>0?log(dz):0;
98 
99  //Initialise loop variables
100  double minDr = 9999;
101  double jecL1L2L3Res = 1.;
102  double jecL1 = 1.;
103 
104  // Compute corrected isolation variables
105  double chIso = muon.pfIsolationR04().sumChargedHadronPt;
106  double nIso = muon.pfIsolationR04().sumNeutralHadronEt;
107  double phoIso = muon.pfIsolationR04().sumPhotonEt;
108  double puIso = muon.pfIsolationR04().sumPUPt;
109  double dbCorrectedIsolation = chIso + std::max( nIso + phoIso - .5*puIso, 0. ) ;
110  double dbCorrectedRelIso = dbCorrectedIsolation/muon.pt();
111 
112  var[kJetPtRatio] = 1./(1+dbCorrectedRelIso);
113  var[kJetPtRel] = 0;
114  var[kJetBTagCSV] = -999;
115  var[kJetNDauCharged] = -1;
116 
117 
118  for (const auto& tagI: bTags){
119  // for each muon with the lepton
120  double dr = deltaR(*(tagI.first), muon);
121  if(dr > minDr) continue;
122  minDr = dr;
123 
124  const reco::Candidate::LorentzVector& muP4(muon.p4());
125  reco::Candidate::LorentzVector jetP4(tagI.first->p4());
126 
127  if (correctorL1 && correctorL1L2L3Res){
128  jecL1L2L3Res = correctorL1L2L3Res->correction(*(tagI.first));
129  jecL1 = correctorL1->correction(*(tagI.first));
130  }
131 
132  // Get b-jet info
133  var[kJetBTagCSV] = tagI.second;
134  var[kJetNDauCharged] = 0;
135  for (auto jet: tagI.first->getJetConstituentsQuick()){
136  const reco::PFCandidate *pfcand = dynamic_cast<const reco::PFCandidate*>(jet);
137  if (pfcand==nullptr) throw cms::Exception("ConfigurationError") << "Cannot get jet constituents";
138  if (pfcand->charge()==0) continue;
139  auto bestTrackPtr = pfcand->bestTrack();
140  if (!bestTrackPtr) continue;
141  if (!bestTrackPtr->quality(reco::Track::highPurity)) continue;
142  if (bestTrackPtr->pt()<1.) continue;
143  if (bestTrackPtr->hitPattern().numberOfValidHits()<8) continue;
144  if (bestTrackPtr->hitPattern().numberOfValidPixelHits()<2) continue;
145  if (bestTrackPtr->normalizedChi2()>=5) continue;
146 
147  if (std::fabs(bestTrackPtr->dxy(vertex.position())) > 0.2) continue;
148  if (std::fabs(bestTrackPtr->dz(vertex.position())) > 17) continue;
149  var[kJetNDauCharged]++;
150  }
151 
152  if(minDr < dRmax_){
153  if ((jetP4-muP4).Rho()<0.0001){
154  var[kJetPtRel] = 0;
155  var[kJetPtRatio] = 1;
156  } else {
157  jetP4 -= muP4/jecL1;
158  jetP4 *= jecL1L2L3Res;
159  jetP4 += muP4;
160 
161  var[kJetPtRatio] = muP4.pt()/jetP4.pt();
162  var[kJetPtRel] = ptRel(muP4,jetP4);
163  }
164  }
165  }
166 
167  if (var[kJetPtRatio] > 1.5) var[kJetPtRatio] = 1.5;
168  if (var[kJetBTagCSV] < 0) var[kJetBTagCSV] = 0;
169  jetPtRatio = var[kJetPtRatio];
170  jetPtRel = var[kJetPtRel];
171  return gbrForest_->GetClassifier(var);
172 };
double eta() const final
momentum pseudorapidity
example_track example_track const char *const kPt
Definition: TSelector.cc:34
double dB(IPTYPE type) const
double pt() const final
transverse momentum
int charge() const final
electric charge
Definition: LeafCandidate.h:91
double correction(const LorentzVector &fJet) const
get correction using Jet information only
Definition: JetCorrector.h:49
float sumPhotonEt
sum pt of PF photons
const Point & position() const
position
Definition: Vertex.h:109
#define constexpr
float sumNeutralHadronEt
sum pt of neutral hadrons
Definition: HeavyIon.h:7
const PFIsolation & miniPFIsolation() const
Definition: Lepton.h:196
inputIndexes
double segmentCompatibility(reco::Muon::ArbitrationType arbitrationType=reco::Muon::SegmentAndTrackArbitration) const
Returns the segment compatibility, using muon::segmentCompatibility (DataFormats/MuonReco/interface/M...
float sumPUPt
sum pt of charged Particles not from PV (for Pu corrections)
T sqrt(T t)
Definition: SSEVec.h:18
const LorentzVector & p4() const final
four-momentum Lorentz vector
Definition: LeafCandidate.h:99
double edB(IPTYPE type) const
float pt_
MVA VAriables.
float computeMva(const pat::Muon &imuon, const reco::Vertex &vertex, const reco::JetTagCollection &bTags, float &jetPtRatio, float &jetPtRel, const reco::JetCorrector *correctorL1=0, const reco::JetCorrector *correctorL1L2L3Res=0) const
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
const MuonPFIsolation & pfIsolationR04() const
Definition: Muon.h:168
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
T dot(const Basic3DVector &v) const
Scalar product, or "dot" product, with a vector of same type.
std::unique_ptr< const GBRForest > gbrForest_
reco::TrackRef muonBestTrack() const override
Track selected to be the best measurement of the muon parameters (including PFlow global information)...
MuonMvaEstimator(const std::string &weightsfile, float dRmax)
float chargedHadronIso() const
Definition: PFIsolation.h:33
Analysis-level muon class.
Definition: Muon.h:50
const reco::Track * bestTrack() const override
Definition: PFCandidate.h:162
float neutralHadronIso() const
Definition: PFIsolation.h:34
float sumChargedHadronPt
sum-pt of charged Hadron