CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ElectronMVAEstimatorRun2Spring15NonTrig.cc
Go to the documentation of this file.
2 
5 
7 
9 
10 #include "TMath.h"
11 #include "TMVA/MethodBDT.h"
12 
15  _tag(conf.getParameter<std::string>("mvaTag")),
16  _MethodName("BDTG method"),
17  _beamSpotLabel(conf.getParameter<edm::InputTag>("beamSpot")),
18  _conversionsLabelAOD(conf.getParameter<edm::InputTag>("conversionsAOD")),
19  _conversionsLabelMiniAOD(conf.getParameter<edm::InputTag>("conversionsMiniAOD")) {
20 
21  const std::vector <std::string> weightFileNames
22  = conf.getParameter<std::vector<std::string> >("weightFileNames");
23 
24  if( (int)(weightFileNames.size()) != nCategories )
25  throw cms::Exception("MVA config failure: ")
26  << "wrong number of weightfiles" << std::endl;
27 
28  _gbrForests.clear();
29  // Create a TMVA reader object for each category
30  for(int i=0; i<nCategories; i++){
31 
32  // Use unique_ptr so that all readers are properly cleaned up
33  // when the vector clear() is called in the destructor
34 
35  edm::FileInPath weightFile( weightFileNames[i] );
36  _gbrForests.push_back( createSingleReader(i, weightFile ) );
37 
38  }
39 
40 }
41 
44 }
45 
46 
48 
49  // All tokens for event content needed by this MVA
50 
51  // Beam spot (same for AOD and miniAOD)
52  cc.consumes<reco::BeamSpot>(_beamSpotLabel);
53 
54  // Conversions collection (different names in AOD and miniAOD)
57 
58 
59 }
60 
62 mvaValue( const edm::Ptr<reco::Candidate>& particle, const edm::Event& iEvent) const {
63 
64  const int iCategory = findCategory( particle );
65  const std::vector<float> vars = std::move( fillMVAVariables( particle, iEvent ) );
66  const float result = _gbrForests.at(iCategory)->GetClassifier(vars.data());
67 
68  const bool debug = false;
69  if(debug) {
70  std::cout << " *** Inside the class _MethodName " << _MethodName << std::endl;
71  std::cout << " bin " << iCategory
72  << " fbrem " << vars[11]
73  << " kfchi2 " << vars[9]
74  << " mykfhits " << vars[8]
75  << " gsfchi2 " << vars[10]
76  << " deta " << vars[18]
77  << " dphi " << vars[19]
78  << " detacalo " << vars[20]
79  << " see " << vars[0]
80  << " spp " << vars[1]
81  << " etawidth " << vars[4]
82  << " phiwidth " << vars[5]
83  << " OneMinusE1x5E5x5 " << vars[2]
84  << " R9 " << vars[3]
85  << " HoE " << vars[6]
86  << " EoP " << vars[15]
87  << " IoEmIoP " << vars[17]
88  << " eleEoPout " << vars[16]
89  << " eta " << vars[24]
90  << " pt " << vars[21] << std::endl;
91  std::cout << " ### MVA " << result << std::endl;
92  }
93 
94  return result;
95 }
96 
98 
99  // Try to cast the particle into a reco particle.
100  // This should work for both reco and pat.
101  const edm::Ptr<reco::GsfElectron> eleRecoPtr = ( edm::Ptr<reco::GsfElectron> )particle;
102  if( eleRecoPtr.get() == NULL )
103  throw cms::Exception("MVA failure: ")
104  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
105  << " but appears to be neither" << std::endl;
106 
107  float pt = eleRecoPtr->pt();
108  float eta = eleRecoPtr->superCluster()->eta();
109 
110  //
111  // Determine the category
112  //
113  int iCategory = UNDEFINED;
114  const float ptSplit = 10; // we have above and below 10 GeV categories
115  const float ebSplit = 0.800;// barrel is split into two regions
116  const float ebeeSplit = 1.479; // division between barrel and endcap
117 
118  if (pt < ptSplit && std::abs(eta) < ebSplit)
119  iCategory = CAT_EB1_PT5to10;
120 
121  if (pt < ptSplit && std::abs(eta) >= ebSplit && std::abs(eta) < ebeeSplit)
122  iCategory = CAT_EB2_PT5to10;
123 
124  if (pt < ptSplit && std::abs(eta) >= ebeeSplit)
125  iCategory = CAT_EE_PT5to10;
126 
127  if (pt >= ptSplit && std::abs(eta) < ebSplit)
128  iCategory = CAT_EB1_PT10plus;
129 
130  if (pt >= ptSplit && std::abs(eta) >= ebSplit && std::abs(eta) < ebeeSplit)
131  iCategory = CAT_EB2_PT10plus;
132 
133  if (pt >= ptSplit && std::abs(eta) >= ebeeSplit)
134  iCategory = CAT_EE_PT10plus;
135 
136  return iCategory;
137 }
138 
141 
142  bool isEndcap = false;
143  if( category == CAT_EE_PT5to10 || category == CAT_EE_PT10plus )
144  isEndcap = true;
145 
146  return isEndcap;
147 }
148 
149 
150 std::unique_ptr<const GBRForest> ElectronMVAEstimatorRun2Spring15NonTrig::
151 createSingleReader(const int iCategory, const edm::FileInPath &weightFile){
152 
153  //
154  // Create the reader
155  //
156  TMVA::Reader tmpTMVAReader( "!Color:Silent:!Error" );
157 
158  //
159  // Configure all variables and spectators. Note: the order and names
160  // must match what is found in the xml weights file!
161  //
162  // Pure ECAL -> shower shapes
163  tmpTMVAReader.AddVariable("ele_oldsigmaietaieta", &_allMVAVars.see);
164  tmpTMVAReader.AddVariable("ele_oldsigmaiphiiphi", &_allMVAVars.spp);
165  tmpTMVAReader.AddVariable("ele_oldcircularity", &_allMVAVars.OneMinusE1x5E5x5);
166  tmpTMVAReader.AddVariable("ele_oldr9", &_allMVAVars.R9);
167  tmpTMVAReader.AddVariable("ele_scletawidth", &_allMVAVars.etawidth);
168  tmpTMVAReader.AddVariable("ele_sclphiwidth", &_allMVAVars.phiwidth);
169  tmpTMVAReader.AddVariable("ele_he", &_allMVAVars.HoE);
170  // Endcap only variables
171  if( isEndcapCategory(iCategory) )
172  tmpTMVAReader.AddVariable("ele_psEoverEraw", &_allMVAVars.PreShowerOverRaw);
173 
174  //Pure tracking variables
175  tmpTMVAReader.AddVariable("ele_kfhits", &_allMVAVars.kfhits);
176  tmpTMVAReader.AddVariable("ele_kfchi2", &_allMVAVars.kfchi2);
177  tmpTMVAReader.AddVariable("ele_gsfchi2", &_allMVAVars.gsfchi2);
178 
179  // Energy matching
180  tmpTMVAReader.AddVariable("ele_fbrem", &_allMVAVars.fbrem);
181 
182  tmpTMVAReader.AddVariable("ele_gsfhits", &_allMVAVars.gsfhits);
183  tmpTMVAReader.AddVariable("ele_expected_inner_hits", &_allMVAVars.expectedMissingInnerHits);
184  tmpTMVAReader.AddVariable("ele_conversionVertexFitProbability", &_allMVAVars.convVtxFitProbability);
185 
186  tmpTMVAReader.AddVariable("ele_ep", &_allMVAVars.EoP);
187  tmpTMVAReader.AddVariable("ele_eelepout", &_allMVAVars.eleEoPout);
188  tmpTMVAReader.AddVariable("ele_IoEmIop", &_allMVAVars.IoEmIoP);
189 
190  // Geometrical matchings
191  tmpTMVAReader.AddVariable("ele_deltaetain", &_allMVAVars.deta);
192  tmpTMVAReader.AddVariable("ele_deltaphiin", &_allMVAVars.dphi);
193  tmpTMVAReader.AddVariable("ele_deltaetaseed", &_allMVAVars.detacalo);
194 
195  // Spectator variables
196  tmpTMVAReader.AddSpectator("ele_pT", &_allMVAVars.pt);
197  tmpTMVAReader.AddSpectator("ele_isbarrel", &_allMVAVars.isBarrel);
198  tmpTMVAReader.AddSpectator("ele_isendcap", &_allMVAVars.isEndcap);
199  tmpTMVAReader.AddSpectator("scl_eta", &_allMVAVars.SCeta);
200 
201  tmpTMVAReader.AddSpectator("ele_eClass", &_allMVAVars.eClass);
202  tmpTMVAReader.AddSpectator("ele_pfRelIso", &_allMVAVars.pfRelIso);
203  tmpTMVAReader.AddSpectator("ele_expected_inner_hits", &_allMVAVars.expectedInnerHits);
204  tmpTMVAReader.AddSpectator("ele_vtxconv", &_allMVAVars.vtxconv);
205  tmpTMVAReader.AddSpectator("mc_event_weight", &_allMVAVars.mcEventWeight);
206  tmpTMVAReader.AddSpectator("mc_ele_CBmatching_category", &_allMVAVars.mcCBmatchingCategory);
207 
208  //
209  // Book the method and set up the weights file
210  //
211  std::unique_ptr<TMVA::IMethod> temp( tmpTMVAReader.BookMVA(_MethodName , weightFile.fullPath() ) );
212 
213  return std::unique_ptr<const GBRForest> ( new GBRForest( dynamic_cast<TMVA::MethodBDT*>( tmpTMVAReader.FindMVA(_MethodName) ) ) );
214 }
215 
216 // A function that should work on both pat and reco objects
219  const edm::Event& iEvent ) const {
220 
221  //
222  // Declare all value maps corresponding to the products we defined earlier
223  //
224  edm::Handle<reco::BeamSpot> theBeamSpot;
226 
227  // Get data needed for conversion rejection
228  iEvent.getByLabel(_beamSpotLabel, theBeamSpot);
229 
230  // Conversions in miniAOD and AOD have different names,
231  // but the same type, so we use the same handle with different tokens.
232  iEvent.getByLabel(_conversionsLabelAOD, conversions);
233  if( !conversions.isValid() )
234  iEvent.getByLabel(_conversionsLabelMiniAOD, conversions);
235 
236  // Make sure everything is retrieved successfully
237  if(! (theBeamSpot.isValid()
238  && conversions.isValid() )
239  )
240  throw cms::Exception("MVA failure: ")
241  << "Failed to retrieve event content needed for this MVA"
242  << std::endl
243  << "Check python MVA configuration file."
244  << std::endl;
245 
246  // Try to cast the particle into a reco particle.
247  // This should work for both reco and pat.
248  const edm::Ptr<reco::GsfElectron> eleRecoPtr = ( edm::Ptr<reco::GsfElectron> )particle;
249  if( eleRecoPtr.get() == NULL )
250  throw cms::Exception("MVA failure: ")
251  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
252  << " but appears to be neither" << std::endl;
253 
254  // Both pat and reco particles have exactly the same accessors, so we use a reco ptr
255  // throughout the code, with a single exception as of this writing, handled separately below.
256  auto superCluster = eleRecoPtr->superCluster();
257 
258  AllVariables allMVAVars;
259 
260  // Pure ECAL -> shower shapes
261  allMVAVars.see = eleRecoPtr->full5x5_sigmaIetaIeta();
262  allMVAVars.spp = eleRecoPtr->full5x5_sigmaIphiIphi();
263  allMVAVars.OneMinusE1x5E5x5 = 1. - eleRecoPtr->full5x5_e1x5() / eleRecoPtr->full5x5_e5x5();
264  allMVAVars.R9 = eleRecoPtr->full5x5_r9();
265  allMVAVars.etawidth = superCluster->etaWidth();
266  allMVAVars.phiwidth = superCluster->phiWidth();
267  allMVAVars.HoE = eleRecoPtr->hadronicOverEm();
268  // Endcap only variables
269  allMVAVars.PreShowerOverRaw = superCluster->preshowerEnergy() / superCluster->rawEnergy();
270 
271  // To get to CTF track information in pat::Electron, we have to have the pointer
272  // to pat::Electron, it is not accessible from the pointer to reco::GsfElectron.
273  // This behavior is reported and is expected to change in the future (post-7.4.5 some time).
274  bool validKF= false;
275  reco::TrackRef myTrackRef = eleRecoPtr->closestCtfTrackRef();
276  const edm::Ptr<pat::Electron> elePatPtr(eleRecoPtr);
277  // Check if this is really a pat::Electron, and if yes, get the track ref from this new
278  // pointer instead
279  if( elePatPtr.get() != NULL )
280  myTrackRef = elePatPtr->closestCtfTrackRef();
281  validKF = (myTrackRef.isAvailable() && (myTrackRef.isNonnull()) );
282 
283  //Pure tracking variables
284  allMVAVars.kfhits = (validKF) ? myTrackRef->hitPattern().trackerLayersWithMeasurement() : -1. ;
285  allMVAVars.kfchi2 = (validKF) ? myTrackRef->normalizedChi2() : 0;
286  allMVAVars.gsfchi2 = eleRecoPtr->gsfTrack()->normalizedChi2();
287 
288  // Energy matching
289  allMVAVars.fbrem = eleRecoPtr->fbrem();
290 
291  allMVAVars.gsfhits = eleRecoPtr->gsfTrack()->hitPattern().trackerLayersWithMeasurement();
292  allMVAVars.expectedMissingInnerHits = eleRecoPtr->gsfTrack()
293  ->hitPattern().numberOfHits(reco::HitPattern::MISSING_INNER_HITS);
294 
296  conversions,
297  theBeamSpot->position());
298  double vertexFitProbability = -1.;
299  if(!conv_ref.isNull()) {
300  const reco::Vertex &vtx = conv_ref.get()->conversionVertex(); if (vtx.isValid()) {
301  vertexFitProbability = TMath::Prob( vtx.chi2(), vtx.ndof());
302  }
303  }
304  allMVAVars.convVtxFitProbability = vertexFitProbability;
305 
306  allMVAVars.EoP = eleRecoPtr->eSuperClusterOverP();
307  allMVAVars.eleEoPout = eleRecoPtr->eEleClusterOverPout();
308  float pAtVertex = eleRecoPtr->trackMomentumAtVtx().R();
309  allMVAVars.IoEmIoP = (1.0/eleRecoPtr->ecalEnergy()) - (1.0 / pAtVertex );
310 
311  // Geometrical matchings
312  allMVAVars.deta = eleRecoPtr->deltaEtaSuperClusterTrackAtVtx();
313  allMVAVars.dphi = eleRecoPtr->deltaPhiSuperClusterTrackAtVtx();
314  allMVAVars.detacalo = eleRecoPtr->deltaEtaSeedClusterTrackAtCalo();
315 
316  // Spectator variables
317  allMVAVars.pt = eleRecoPtr->pt();
318  float scEta = superCluster->eta();
319  constexpr float ebeeSplit = 1.479;
320  allMVAVars.isBarrel = ( std::abs(scEta) < ebeeSplit );
321  allMVAVars.isEndcap = ( std::abs(scEta) >= ebeeSplit );
322  allMVAVars.SCeta = scEta;
323  // The spectator variables below were examined for training, but
324  // are not necessary for evaluating the discriminator, so they are
325  // given dummy values (the specator variables above are also unimportant).
326  // They are introduced only to match the definition of the discriminator
327  // in the weights file.
328  constexpr unsigned nines = 999;
329  allMVAVars.eClass = nines;
330  allMVAVars.pfRelIso = nines;
331  allMVAVars.expectedInnerHits = nines;
332  allMVAVars.vtxconv = nines;
333  allMVAVars.mcEventWeight = nines;
334  allMVAVars.mcCBmatchingCategory = nines;
335 
336  constrainMVAVariables(allMVAVars);
337 
338  std::vector<float> vars;
339 
340  if( isEndcapCategory( findCategory( particle ) ) ) {
341  vars = std::move( packMVAVariables(allMVAVars.see,
342  allMVAVars.spp,
343  allMVAVars.OneMinusE1x5E5x5,
344  allMVAVars.R9,
345  allMVAVars.etawidth,
346  allMVAVars.phiwidth,
347  allMVAVars.HoE,
348  // Endcap only variables
349  allMVAVars.PreShowerOverRaw,
350  //Pure tracking variables
351  allMVAVars.kfhits,
352  allMVAVars.kfchi2,
353  allMVAVars.gsfchi2,
354  // Energy matching
355  allMVAVars.fbrem,
356  allMVAVars.gsfhits,
357  allMVAVars.expectedMissingInnerHits,
358  allMVAVars.convVtxFitProbability,
359  allMVAVars.EoP,
360  allMVAVars.eleEoPout,
361  allMVAVars.IoEmIoP,
362  // Geometrical matchings
363  allMVAVars.deta,
364  allMVAVars.dphi,
365  allMVAVars.detacalo,
366  // Spectator variables
367  allMVAVars.pt,
368  allMVAVars.isBarrel,
369  allMVAVars.isEndcap,
370  allMVAVars.SCeta,
371  allMVAVars.eClass,
372  allMVAVars.pfRelIso,
373  allMVAVars.expectedInnerHits,
374  allMVAVars.vtxconv,
375  allMVAVars.mcEventWeight,
376  allMVAVars.mcCBmatchingCategory)
377  );
378  } else {
379  vars = std::move( packMVAVariables(allMVAVars.see,
380  allMVAVars.spp,
381  allMVAVars.OneMinusE1x5E5x5,
382  allMVAVars.R9,
383  allMVAVars.etawidth,
384  allMVAVars.phiwidth,
385  allMVAVars.HoE,
386  //Pure tracking variables
387  allMVAVars.kfhits,
388  allMVAVars.kfchi2,
389  allMVAVars.gsfchi2,
390  // Energy matching
391  allMVAVars.fbrem,
392  allMVAVars.gsfhits,
393  allMVAVars.expectedMissingInnerHits,
394  allMVAVars.convVtxFitProbability,
395  allMVAVars.EoP,
396  allMVAVars.eleEoPout,
397  allMVAVars.IoEmIoP,
398  // Geometrical matchings
399  allMVAVars.deta,
400  allMVAVars.dphi,
401  allMVAVars.detacalo,
402  // Spectator variables
403  allMVAVars.pt,
404  allMVAVars.isBarrel,
405  allMVAVars.isEndcap,
406  allMVAVars.SCeta,
407  allMVAVars.eClass,
408  allMVAVars.pfRelIso,
409  allMVAVars.expectedInnerHits,
410  allMVAVars.vtxconv,
411  allMVAVars.mcEventWeight,
412  allMVAVars.mcCBmatchingCategory)
413  );
414  }
415  return vars;
416 }
417 
419 
420  // Check that variables do not have crazy values
421 
422  if(allMVAVars.fbrem < -1.)
423  allMVAVars.fbrem = -1.;
424 
425  allMVAVars.deta = fabs(allMVAVars.deta);
426  if(allMVAVars.deta > 0.06)
427  allMVAVars.deta = 0.06;
428 
429 
430  allMVAVars.dphi = fabs(allMVAVars.dphi);
431  if(allMVAVars.dphi > 0.6)
432  allMVAVars.dphi = 0.6;
433 
434 
435  if(allMVAVars.EoP > 20.)
436  allMVAVars.EoP = 20.;
437 
438  if(allMVAVars.eleEoPout > 20.)
439  allMVAVars.eleEoPout = 20.;
440 
441 
442  allMVAVars.detacalo = fabs(allMVAVars.detacalo);
443  if(allMVAVars.detacalo > 0.2)
444  allMVAVars.detacalo = 0.2;
445 
446  if(allMVAVars.OneMinusE1x5E5x5 < -1.)
447  allMVAVars.OneMinusE1x5E5x5 = -1;
448 
449  if(allMVAVars.OneMinusE1x5E5x5 > 2.)
450  allMVAVars.OneMinusE1x5E5x5 = 2.;
451 
452 
453 
454  if(allMVAVars.R9 > 5)
455  allMVAVars.R9 = 5;
456 
457  if(allMVAVars.gsfchi2 > 200.)
458  allMVAVars.gsfchi2 = 200;
459 
460 
461  if(allMVAVars.kfchi2 > 10.)
462  allMVAVars.kfchi2 = 10.;
463 
464 
465 }
466 
bool isAvailable() const
Definition: Ref.h:576
T getParameter(std::string const &) const
std::vector< std::unique_ptr< const GBRForest > > _gbrForests
int i
Definition: DBlmapReader.cc:9
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:252
std::unique_ptr< const GBRForest > createSingleReader(const int iCategory, const edm::FileInPath &weightFile)
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:160
bool isValid() const
Tells whether the vertex is valid.
Definition: Vertex.h:60
int findCategory(const edm::Ptr< reco::Candidate > &particle) const override
#define NULL
Definition: scimark2.h:8
std::vector< float > fillMVAVariables(const edm::Ptr< reco::Candidate > &particle, const edm::Event &) const override
#define constexpr
std::vector< Conversion > ConversionCollection
collectin of Conversion objects
Definition: ConversionFwd.h:9
std::vector< float > packMVAVariables(const Args...args) const
int iEvent
Definition: GenABIO.cc:230
tuple result
Definition: query.py:137
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double chi2() const
chi-squares
Definition: Vertex.h:95
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:244
bool isValid() const
Definition: HandleBase.h:75
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:420
bool isEndcap(GeomDetEnumerators::SubDetector m)
bool isNull() const
Checks for null.
Definition: Ref.h:249
double ndof() const
Definition: Vertex.h:102
tuple conf
Definition: dbtoconf.py:185
#define debug
Definition: HDRShower.cc:19
void setConsumes(edm::ConsumesCollector &&) const overridefinal
tuple cout
Definition: gather_cfg.py:121
std::string fullPath() const
Definition: FileInPath.cc:165
float mvaValue(const edm::Ptr< reco::Candidate > &particle, const edm::Event &) const override
static reco::ConversionRef matchedConversion(const reco::GsfElectron &ele, const edm::Handle< reco::ConversionCollection > &convCol, const math::XYZPoint &beamspot, bool allowCkfMatch=true, float lxyMin=2.0, float probMin=1e-6, unsigned int nHitsBeforeVtxMax=0)