CMS 3D CMS Logo

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