CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MuonIDMVA.cc
Go to the documentation of this file.
5 
6 #include "TFile.h"
7 #include "TRandom3.h"
8 #include "TVector3.h"
9 #include "TMVA/Tools.h"
10 #include "TMVA/Reader.h"
11 
12 using namespace HWWFunctions;
13 
14 //--------------------------------------------------------------------------------------------------
16  fMethodname("BDTG method"),
17  fIsInitialized(kFALSE)
18 {
19  // Constructor.
20  for(UInt_t i=0; i<6; ++i) {
21  fTMVAReader[i] = 0;
22  }
23 }
24 
25 
26 //--------------------------------------------------------------------------------------------------
28 {
29  for(UInt_t i=0; i<6; ++i) {
30  if (fTMVAReader[i]) delete fTMVAReader[i];
31  }
32 }
33 
34 //--------------------------------------------------------------------------------------------------
35 void MuonIDMVA::Initialize( TString methodName, unsigned int version,
36  TString Subdet0Pt10To14p5Weights ,
37  TString Subdet1Pt10To14p5Weights ,
38  TString Subdet0Pt14p5To20Weights,
39  TString Subdet1Pt14p5To20Weights,
40  TString Subdet0Pt20ToInfWeights,
41  TString Subdet1Pt20ToInfWeights) {
42 
43  if (version != 1) {
44  edm::LogError("InvalidInput") << "[MuonIDMVA::Initialize] Version must be 1. Aborting.";
45  return;
46  }
47 
48  fIsInitialized = kTRUE;
49  fMethodname = methodName;
50 
51  for(UInt_t i=0; i<6; ++i) {
52  if (fTMVAReader[i]) delete fTMVAReader[i];
53 
54  fTMVAReader[i] = new TMVA::Reader( "!Color:!Silent:Error" );
55  fTMVAReader[i]->SetVerbose(kTRUE);
56 
57  // order matters!
58  if (version == 1) {
59  fTMVAReader[i]->AddVariable( "TkNchi2", &fMVAVar_MuTkNchi2 );
60  fTMVAReader[i]->AddVariable( "GlobalNchi2", &fMVAVar_MuGlobalNchi2 );
61  fTMVAReader[i]->AddVariable( "NValidHits", &fMVAVar_MuNValidHits );
62  fTMVAReader[i]->AddVariable( "NTrackerHits", &fMVAVar_MuNTrackerHits );
63  fTMVAReader[i]->AddVariable( "NPixelHits", &fMVAVar_MuNPixelHits );
64  fTMVAReader[i]->AddVariable( "NMatches", &fMVAVar_MuNMatches );
65  fTMVAReader[i]->AddVariable( "D0", &fMVAVar_MuD0 );
66  fTMVAReader[i]->AddVariable( "IP3d", &fMVAVar_MuIP3d );
67  fTMVAReader[i]->AddVariable( "IP3dSig", &fMVAVar_MuIP3dSig );
68  fTMVAReader[i]->AddVariable( "TrkKink", &fMVAVar_MuTrkKink );
69  fTMVAReader[i]->AddVariable( "SegmentCompatibility", &fMVAVar_MuSegmentCompatibility );
70  fTMVAReader[i]->AddVariable( "CaloCompatibility", &fMVAVar_MuCaloCompatibility );
71  fTMVAReader[i]->AddVariable( "HadEnergyOverPt", &fMVAVar_MuHadEnergyOverPt );
72  fTMVAReader[i]->AddVariable( "EmEnergyOverPt", &fMVAVar_MuEmEnergyOverPt );
73  fTMVAReader[i]->AddVariable( "HadS9EnergyOverPt", &fMVAVar_MuHadS9EnergyOverPt );
74  fTMVAReader[i]->AddVariable( "EmS9EnergyOverPt", &fMVAVar_MuEmS9EnergyOverPt );
75  fTMVAReader[i]->AddVariable( "TrkIso03OverPt", &fMVAVar_MuTrkIso03OverPt );
76  fTMVAReader[i]->AddVariable( "EMIso03OverPt", &fMVAVar_MuEMIso03OverPt );
77  fTMVAReader[i]->AddVariable( "HadIso03OverPt", &fMVAVar_MuHadIso03OverPt );
78  fTMVAReader[i]->AddVariable( "TrkIso05OverPt", &fMVAVar_MuTrkIso05OverPt );
79  fTMVAReader[i]->AddVariable( "EMIso05OverPt", &fMVAVar_MuEMIso05OverPt );
80  fTMVAReader[i]->AddVariable( "HadIso05OverPt", &fMVAVar_MuHadIso05OverPt );
81  }
82 
83  if (i==0) fTMVAReader[i]->BookMVA(fMethodname , Subdet0Pt10To14p5Weights );
84  if (i==1) fTMVAReader[i]->BookMVA(fMethodname , Subdet1Pt10To14p5Weights );
85  if (i==2) fTMVAReader[i]->BookMVA(fMethodname , Subdet0Pt14p5To20Weights );
86  if (i==3) fTMVAReader[i]->BookMVA(fMethodname , Subdet1Pt14p5To20Weights );
87  if (i==4) fTMVAReader[i]->BookMVA(fMethodname , Subdet0Pt20ToInfWeights );
88  if (i==5) fTMVAReader[i]->BookMVA(fMethodname , Subdet1Pt20ToInfWeights );
89 
90  }
91 }
92 
93 //--------------------------------------------------------------------------------------------------
94 Double_t MuonIDMVA::MVAValue(Double_t MuPt , Double_t MuEta,
95  Double_t MuTkNchi2,
96  Double_t MuGlobalNchi2,
97  Double_t MuNValidHits,
98  Double_t MuNTrackerHits,
99  Double_t MuNPixelHits,
100  Double_t MuNMatches,
101  Double_t MuD0,
102  Double_t MuIP3d,
103  Double_t MuIP3dSig,
104  Double_t MuTrkKink,
105  Double_t MuSegmentCompatibility,
106  Double_t MuCaloCompatibility,
107  Double_t MuHadEnergyOverPt,
108  Double_t MuHoEnergyOverPt,
109  Double_t MuEmEnergyOverPt,
110  Double_t MuHadS9EnergyOverPt,
111  Double_t MuHoS9EnergyOverPt,
112  Double_t MuEmS9EnergyOverPt,
113  Double_t MuTrkIso03OverPt,
114  Double_t MuEMIso03OverPt,
115  Double_t MuHadIso03OverPt,
116  Double_t MuTrkIso05OverPt,
117  Double_t MuEMIso05OverPt,
118  Double_t MuHadIso05OverPt,
119  Bool_t printDebug
120  ) {
121 
122  if (!fIsInitialized) {
123  edm::LogError("NotInitialized") << "Error: MuonIDMVA not properly initialized.";
124  return -9999;
125  }
126 
127  Int_t subdet = 0;
128  if (fabs(MuEta) < 1.479) subdet = 0;
129  else subdet = 1;
130  Int_t ptBin = 0;
131  if (MuPt > 14.5) ptBin = 1;
132  if (MuPt > 20.0) ptBin = 2;
133 
134 
135  //set all input variables
136  fMVAVar_MuTkNchi2 = MuTkNchi2;
137  fMVAVar_MuGlobalNchi2 = MuGlobalNchi2;
138  fMVAVar_MuNValidHits = MuNValidHits;
139  fMVAVar_MuNTrackerHits = MuNTrackerHits;
140  fMVAVar_MuNPixelHits = MuNPixelHits;
141  fMVAVar_MuNMatches = MuNMatches;
142  fMVAVar_MuD0 = MuD0;
143  fMVAVar_MuIP3d = MuIP3d;
144  fMVAVar_MuIP3dSig = MuIP3dSig;
145  fMVAVar_MuTrkKink = MuTrkKink;
146  fMVAVar_MuSegmentCompatibility = MuSegmentCompatibility;
147  fMVAVar_MuCaloCompatibility = MuCaloCompatibility;
148  fMVAVar_MuHadEnergyOverPt = MuHadEnergyOverPt;
149  fMVAVar_MuHoEnergyOverPt = MuHoEnergyOverPt;
150  fMVAVar_MuEmEnergyOverPt = MuEmEnergyOverPt;
151  fMVAVar_MuHadS9EnergyOverPt = MuHadS9EnergyOverPt;
152  fMVAVar_MuHoS9EnergyOverPt = MuHoS9EnergyOverPt;
153  fMVAVar_MuEmS9EnergyOverPt = MuEmS9EnergyOverPt;
154  fMVAVar_MuTrkIso03OverPt = MuTrkIso03OverPt;
155  fMVAVar_MuEMIso03OverPt = MuEMIso03OverPt;
156  fMVAVar_MuHadIso03OverPt = MuHadIso03OverPt;
157  fMVAVar_MuTrkIso05OverPt = MuTrkIso05OverPt;
158  fMVAVar_MuEMIso05OverPt = MuEMIso05OverPt;
159  fMVAVar_MuHadIso05OverPt = MuHadIso05OverPt;
160 
161  Double_t mva = -9999;
162  TMVA::Reader *reader = 0;
163 
164  Int_t MVABin = -1;
165  if (subdet == 0 && ptBin == 0) MVABin = 0;
166  if (subdet == 1 && ptBin == 0) MVABin = 1;
167  if (subdet == 0 && ptBin == 1) MVABin = 2;
168  if (subdet == 1 && ptBin == 1) MVABin = 3;
169  if (subdet == 0 && ptBin == 2) MVABin = 4;
170  if (subdet == 1 && ptBin == 2) MVABin = 5;
171  assert(MVABin >= 0 && MVABin <= 5);
172  reader = fTMVAReader[MVABin];
173 
174  mva = reader->EvaluateMVA( fMethodname );
175 
176  if (printDebug) {
177  LogDebug("MuonIDMVA") << "Debug Muon MVA: "
178  << MuPt << " " << MuEta << " --> MVABin " << MVABin << " : "
179  << fMVAVar_MuTkNchi2 << " "
180  << fMVAVar_MuGlobalNchi2 << " "
181  << fMVAVar_MuNValidHits << " "
182  << fMVAVar_MuNTrackerHits << " "
183  << fMVAVar_MuNPixelHits << " "
184  << fMVAVar_MuNMatches << " "
185  << fMVAVar_MuD0 << " "
186  << fMVAVar_MuIP3d << " "
187  << fMVAVar_MuIP3dSig << " "
188  << fMVAVar_MuTrkKink << " "
190  << fMVAVar_MuCaloCompatibility << " "
191  << fMVAVar_MuHadEnergyOverPt << " "
192  << fMVAVar_MuHoEnergyOverPt << " "
193  << fMVAVar_MuEmEnergyOverPt << " "
194  << fMVAVar_MuHadS9EnergyOverPt << " "
195  << fMVAVar_MuHoS9EnergyOverPt << " "
196  << fMVAVar_MuEmS9EnergyOverPt << " "
197  << fMVAVar_MuTrkIso03OverPt << " "
198  << fMVAVar_MuEMIso03OverPt << " "
199  << fMVAVar_MuHadIso03OverPt << " "
200  << fMVAVar_MuTrkIso05OverPt << " "
201  << fMVAVar_MuEMIso05OverPt << " "
202  << fMVAVar_MuHadIso05OverPt << " "
203  << " === : === "
204  << mva;
205  }
206 
207  return mva;
208 }
209 
210 //--------------------------------------------------------------------------------------------------
211 Double_t MuonIDMVA::MVAValue(HWW& hww, const unsigned int mu, const unsigned int vertex) {
212 
213  if (!fIsInitialized) {
214  edm::LogError("NotInialized") << "Error: MuonIDMVA not properly initialized.";
215  return -9999;
216  }
217 
218  Double_t MuPt = hww.mus_trk_p4().at(mu).pt();
219  Double_t MuEta = hww.mus_trk_p4().at(mu).eta();
220  Double_t Rho = hww.evt_ww_rho_vor();
221 
222  Int_t subdet = 0;
223  if (fabs(MuEta) < 1.479) subdet = 0;
224  else subdet = 1;
225  Int_t ptBin = 0;
226  if (MuPt > 14.5) ptBin = 1;
227  if (MuPt > 20.0) ptBin = 2;
228 
229  //set all input variables
230  fMVAVar_MuTkNchi2 = hww.mus_chi2().at(mu) / hww.mus_ndof().at(mu);
231  fMVAVar_MuGlobalNchi2 = hww.mus_gfit_chi2().at(mu) / hww.mus_gfit_ndof().at(mu);
233  fMVAVar_MuNTrackerHits = hww.mus_validHits().at(mu);
234  fMVAVar_MuNPixelHits = hww.trks_valid_pixelhits().at(hww.mus_trkidx().at(mu));
235  fMVAVar_MuNMatches = hww.mus_nmatches().at(mu);
236  fMVAVar_MuD0 = mud0PV_smurfV3(hww, mu);
237 
238  const double mud0sign = ( (trks_d0_pv(hww, hww.mus_trkidx().at(mu),0).first) >=0 ) ? 1. : -1.;
239  fMVAVar_MuIP3d = hww.mus_ip3d().at(mu)*mud0sign;
240  if (hww.mus_ip3derr().at(mu) == 0.0) fMVAVar_MuIP3dSig = 0.0;
241  else fMVAVar_MuIP3dSig = hww.mus_ip3d().at(mu)*mud0sign / hww.mus_ip3derr().at(mu);
242  fMVAVar_MuTrkKink = hww.mus_trkKink().at(mu);
257 
258  Double_t mva = -9999;
259  TMVA::Reader *reader = 0;
260 
261  Int_t MVABin = -1;
262  if (subdet == 0 && ptBin == 0) MVABin = 0;
263  if (subdet == 1 && ptBin == 0) MVABin = 1;
264  if (subdet == 0 && ptBin == 1) MVABin = 2;
265  if (subdet == 1 && ptBin == 1) MVABin = 3;
266  if (subdet == 0 && ptBin == 2) MVABin = 4;
267  if (subdet == 1 && ptBin == 2) MVABin = 5;
268  assert(MVABin >= 0 && MVABin <= 5);
269  reader = fTMVAReader[MVABin];
270 
271  mva = reader->EvaluateMVA( fMethodname );
272 
273  if (0) {
274  LogDebug("MuonIDMVA") << "Debug Muon MVA: "
275  << hww.evt_run() << " " << hww.evt_lumiBlock() << " " << hww.evt_event() << " " << Rho << " "
276  << hww.mus_p4().at(mu).pt() << " " << hww.mus_p4().at(mu).eta() << " " << hww.mus_p4().at(mu).phi() << " : "
277  << MuPt << " " << MuEta << " --> MVABin " << MVABin << " : "
278  << fMVAVar_MuTkNchi2 << " "
279  << fMVAVar_MuGlobalNchi2 << " "
280  << fMVAVar_MuNValidHits << " "
281  << fMVAVar_MuNTrackerHits << " "
282  << fMVAVar_MuNPixelHits << " "
283  << fMVAVar_MuNMatches << " "
284  << fMVAVar_MuD0 << " "
285  << fMVAVar_MuIP3d << " "
286  << fMVAVar_MuIP3dSig << " "
287  << fMVAVar_MuTrkKink << " "
289  << fMVAVar_MuCaloCompatibility << " "
290  << fMVAVar_MuHadEnergyOverPt << " "
291  << fMVAVar_MuHoEnergyOverPt << " "
292  << fMVAVar_MuEmEnergyOverPt << " "
293  << fMVAVar_MuHadS9EnergyOverPt << " "
294  << fMVAVar_MuHoS9EnergyOverPt << " "
295  << fMVAVar_MuEmS9EnergyOverPt << " "
296  << fMVAVar_MuTrkIso03OverPt << " "
297  << fMVAVar_MuEMIso03OverPt << " "
298  << fMVAVar_MuHadIso03OverPt << " "
299  << fMVAVar_MuTrkIso05OverPt << " "
300  << fMVAVar_MuEMIso05OverPt << " "
301  << fMVAVar_MuHadIso05OverPt << " "
302  << " === : === "
303  << mva;
304  }
305 
306  return mva;
307 }
#define LogDebug(id)
Float_t fMVAVar_MuEMIso05OverPt
Definition: MuonIDMVA.h:211
Float_t fMVAVar_MuIP3dSig
Definition: MuonIDMVA.h:193
int i
Definition: DBlmapReader.cc:9
Float_t fMVAVar_MuGlobalNchi2
Definition: MuonIDMVA.h:186
Float_t fMVAVar_MuEMIso03OverPt
Definition: MuonIDMVA.h:208
TString fMethodname
Definition: MuonIDMVA.h:182
double mud0PV_smurfV3(HWW &, unsigned int index)
std::vector< int > & trks_valid_pixelhits()
Definition: HWW.cc:63
Bool_t fIsInitialized
Definition: MuonIDMVA.h:183
std::vector< float > & mus_iso05_hadEt()
Definition: HWW.cc:479
void Initialize(TString methodName, unsigned int version, TString Subdet0Pt10To14p5Weights, TString Subdet1Pt10To14p5Weights, TString Subdet0Pt14p5To20Weights, TString Subdet1Pt14p5To20Weights, TString Subdet0Pt20ToInfWeights, TString Subdet1Pt20ToInfWeights)
Definition: MuonIDMVA.cc:35
Float_t fMVAVar_MuHoEnergyOverPt
Definition: MuonIDMVA.h:198
std::vector< float > & mus_gfit_chi2()
Definition: HWW.cc:383
Float_t fMVAVar_MuCaloCompatibility
Definition: MuonIDMVA.h:196
Float_t fMVAVar_MuHadEnergyOverPt
Definition: MuonIDMVA.h:197
std::vector< int > & mus_trkidx()
Definition: HWW.cc:535
~MuonIDMVA()
Definition: MuonIDMVA.cc:27
Float_t fMVAVar_MuTrkIso03OverPt
Definition: MuonIDMVA.h:207
std::vector< LorentzVector > & mus_p4()
Definition: HWW.cc:367
std::vector< float > & mus_iso03_hadEt()
Definition: HWW.cc:467
float & evt_ww_rho_vor()
Definition: HWW.cc:623
std::vector< float > & mus_e_hoS9()
Definition: HWW.cc:451
std::vector< int > & mus_nmatches()
Definition: HWW.cc:547
std::vector< float > & mus_iso03_sumPt()
Definition: HWW.cc:459
Float_t fMVAVar_MuNPixelHits
Definition: MuonIDMVA.h:189
std::vector< int > & mus_gfit_validSTAHits()
Definition: HWW.cc:515
std::vector< float > & mus_ndof()
Definition: HWW.cc:415
std::vector< float > & mus_ip3derr()
Definition: HWW.cc:423
Float_t fMVAVar_MuNMatches
Definition: MuonIDMVA.h:190
std::vector< float > & mus_ip3d()
Definition: HWW.cc:419
std::vector< float > & mus_e_emS9()
Definition: HWW.cc:455
Float_t fMVAVar_MuEmS9EnergyOverPt
Definition: MuonIDMVA.h:202
edm::LuminosityBlockNumber_t & evt_lumiBlock()
Definition: HWW.cc:611
Float_t fMVAVar_MuHadIso05OverPt
Definition: MuonIDMVA.h:212
std::pair< double, double > trks_d0_pv(HWW &, int itrk, int ipv)
std::vector< LorentzVector > & mus_trk_p4()
Definition: HWW.cc:371
std::vector< int > & mus_validHits()
Definition: HWW.cc:531
Float_t fMVAVar_MuTrkKink
Definition: MuonIDMVA.h:194
Float_t fMVAVar_MuD0
Definition: MuonIDMVA.h:191
Float_t fMVAVar_MuTrkIso05OverPt
Definition: MuonIDMVA.h:210
Float_t fMVAVar_MuHadIso03OverPt
Definition: MuonIDMVA.h:209
std::vector< float > & mus_gfit_ndof()
Definition: HWW.cc:387
static const Double_t MuonEffectiveArea(EMuonEffectiveAreaType type, Double_t Eta)
Definition: MuonIDMVA.h:87
Float_t fMVAVar_MuSegmentCompatibility
Definition: MuonIDMVA.h:195
const int mu
Definition: Constants.h:22
std::vector< float > & mus_e_hadS9()
Definition: HWW.cc:447
bool first
Definition: L1TdeRCT.cc:75
Definition: HWW.h:12
std::vector< float > & mus_e_had()
Definition: HWW.cc:435
std::vector< float > & mus_trkKink()
Definition: HWW.cc:395
Float_t fMVAVar_MuEmEnergyOverPt
Definition: MuonIDMVA.h:199
Float_t fMVAVar_MuHadS9EnergyOverPt
Definition: MuonIDMVA.h:200
std::vector< float > & mus_segmCompatibility()
Definition: HWW.cc:427
TMVA::Reader * fTMVAReader[6]
Definition: MuonIDMVA.h:181
Float_t fMVAVar_MuIP3d
Definition: MuonIDMVA.h:192
std::vector< float > & mus_e_em()
Definition: HWW.cc:443
std::vector< float > & mus_iso05_sumPt()
Definition: HWW.cc:471
Float_t fMVAVar_MuNTrackerHits
Definition: MuonIDMVA.h:188
std::vector< float > & mus_caloCompatibility()
Definition: HWW.cc:431
std::vector< float > & mus_e_ho()
Definition: HWW.cc:439
Float_t fMVAVar_MuHoS9EnergyOverPt
Definition: MuonIDMVA.h:201
Float_t fMVAVar_MuNValidHits
Definition: MuonIDMVA.h:187
std::vector< float > & mus_iso05_emEt()
Definition: HWW.cc:475
Double_t MVAValue(HWW &, const unsigned int mu, const unsigned int vertex)
Definition: MuonIDMVA.cc:211
std::vector< float > & mus_chi2()
Definition: HWW.cc:411
Float_t fMVAVar_MuTkNchi2
Definition: MuonIDMVA.h:185
std::vector< float > & mus_iso03_emEt()
Definition: HWW.cc:463
edm::RunNumber_t & evt_run()
Definition: HWW.cc:607
edm::EventNumber_t & evt_event()
Definition: HWW.cc:615