CMS 3D CMS Logo

ElectronMVAEstimatorRun2Spring16GeneralPurpose.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 
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 eta = eleRecoPtr->superCluster()->eta();
144 
145  //
146  // Determine the category
147  //
148  int iCategory = UNDEFINED;
149  const float ebSplit = 0.800;// barrel is split into two regions
150  const float ebeeSplit = 1.479; // division between barrel and endcap
151 
152 
153  if (std::abs(eta) < ebSplit)
154  iCategory = CAT_EB1_PT10plus;
155 
156  if (std::abs(eta) >= ebSplit && std::abs(eta) < ebeeSplit)
157  iCategory = CAT_EB2_PT10plus;
158 
159  if (std::abs(eta) >= ebeeSplit)
160  iCategory = CAT_EE_PT10plus;
161 
162  return iCategory;
163 }
164 
167 
168  bool isEndcap = false;
169  if( category == CAT_EE_PT10plus )
170  isEndcap = true;
171 
172  return isEndcap;
173 }
174 
175 
176 // A function that should work on both pat and reco objects
179  const edm::Event& iEvent ) const {
180 
181  //
182  // Declare all value maps corresponding to the products we defined earlier
183  //
184  edm::Handle<reco::BeamSpot> theBeamSpot;
186 
187  // Get data needed for conversion rejection
188  iEvent.getByLabel(beamSpotLabel_, theBeamSpot);
189 
190  // Conversions in miniAOD and AOD have different names,
191  // but the same type, so we use the same handle with different tokens.
192  iEvent.getByLabel(conversionsLabelAOD_, conversions);
193  if( !conversions.isValid() )
194  iEvent.getByLabel(conversionsLabelMiniAOD_, conversions);
195 
196  // Make sure everything is retrieved successfully
197  if(! (theBeamSpot.isValid()
198  && conversions.isValid() )
199  )
200  throw cms::Exception("MVA failure: ")
201  << "Failed to retrieve event content needed for this MVA"
202  << std::endl
203  << "Check python MVA configuration file."
204  << std::endl;
205 
206  // Try to cast the particle into a reco particle.
207  // This should work for both reco and pat.
208  const edm::Ptr<reco::GsfElectron> eleRecoPtr = ( edm::Ptr<reco::GsfElectron> )particle;
209  if( eleRecoPtr.get() == nullptr )
210  throw cms::Exception("MVA failure: ")
211  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
212  << " but appears to be neither" << std::endl;
213  return fillMVAVariables(eleRecoPtr.get(), conversions, theBeamSpot.product());
214 }
215 
216 // A function that should work on both pat and reco objects
219  const edm::Handle<reco::ConversionCollection> conversions, const reco::BeamSpot *theBeamSpot ) const {
220 
221 
222  // Both pat and reco particles have exactly the same accessors, so we use a reco ptr
223  // throughout the code, with a single exception as of this writing, handled separately below.
224  auto superCluster = eleRecoPtr->superCluster();
225 
226  AllVariables allMVAVars;
227 
228  // Pure ECAL -> shower shapes
229  allMVAVars.see = eleRecoPtr->full5x5_sigmaIetaIeta();
230  allMVAVars.spp = eleRecoPtr->full5x5_sigmaIphiIphi();
231  allMVAVars.OneMinusE1x5E5x5 = 1. - eleRecoPtr->full5x5_e1x5() / eleRecoPtr->full5x5_e5x5();
232  allMVAVars.R9 = eleRecoPtr->full5x5_r9();
233  allMVAVars.etawidth = superCluster->etaWidth();
234  allMVAVars.phiwidth = superCluster->phiWidth();
235  allMVAVars.HoE = eleRecoPtr->full5x5_hcalOverEcal(); //hadronicOverEm();
236  // Endcap only variables
237  allMVAVars.PreShowerOverRaw = superCluster->preshowerEnergy() / superCluster->rawEnergy();
238 
239  // To get to CTF track information in pat::Electron, we have to have the pointer
240  // to pat::Electron, it is not accessible from the pointer to reco::GsfElectron.
241  // This behavior is reported and is expected to change in the future (post-7.4.5 some time).
242  bool validKF= false;
243  reco::TrackRef myTrackRef = eleRecoPtr->closestCtfTrackRef();
244  const pat::Electron * elePatPtr = dynamic_cast<const pat::Electron *>(eleRecoPtr);
245  // Check if this is really a pat::Electron, and if yes, get the track ref from this new
246  // pointer instead
247  if( elePatPtr != nullptr )
248  myTrackRef = elePatPtr->closestCtfTrackRef();
249  validKF = (myTrackRef.isAvailable() && (myTrackRef.isNonnull()) );
250 
251  //Pure tracking variables
252  allMVAVars.kfhits = (validKF) ? myTrackRef->hitPattern().trackerLayersWithMeasurement() : -1. ;
253  allMVAVars.kfchi2 = (validKF) ? myTrackRef->normalizedChi2() : 0;
254  allMVAVars.gsfchi2 = eleRecoPtr->gsfTrack()->normalizedChi2();
255 
256  // Energy matching
257  allMVAVars.fbrem = eleRecoPtr->fbrem();
258 
259  allMVAVars.gsfhits = eleRecoPtr->gsfTrack()->hitPattern().trackerLayersWithMeasurement();
260  allMVAVars.expectedMissingInnerHits = eleRecoPtr->gsfTrack()
261  ->hitPattern().numberOfLostHits(reco::HitPattern::MISSING_INNER_HITS);
262 
264  conversions,
265  theBeamSpot->position());
266  double vertexFitProbability = -1.;
267  if(!conv_ref.isNull()) {
268  const reco::Vertex &vtx = conv_ref.get()->conversionVertex(); if (vtx.isValid()) {
269  vertexFitProbability = TMath::Prob( vtx.chi2(), vtx.ndof());
270  }
271  }
272  allMVAVars.convVtxFitProbability = vertexFitProbability;
273 
274  allMVAVars.EoP = eleRecoPtr->eSuperClusterOverP();
275  allMVAVars.eleEoPout = eleRecoPtr->eEleClusterOverPout();
276  float pAtVertex = eleRecoPtr->trackMomentumAtVtx().R();
277  allMVAVars.IoEmIoP = (1.0/eleRecoPtr->ecalEnergy()) - (1.0 / pAtVertex );
278 
279  // Geometrical matchings
280  allMVAVars.deta = eleRecoPtr->deltaEtaSuperClusterTrackAtVtx();
281  allMVAVars.dphi = eleRecoPtr->deltaPhiSuperClusterTrackAtVtx();
282  allMVAVars.detacalo = eleRecoPtr->deltaEtaSeedClusterTrackAtCalo();
283 
284  // Spectator variables
285  allMVAVars.pt = eleRecoPtr->pt();
286  float scEta = superCluster->eta();
287  constexpr float ebeeSplit = 1.479;
288  allMVAVars.isBarrel = ( std::abs(scEta) < ebeeSplit );
289  allMVAVars.isEndcap = ( std::abs(scEta) >= ebeeSplit );
290  allMVAVars.SCeta = scEta;
291  allMVAVars.pt = eleRecoPtr->pt();
292 
293  constrainMVAVariables(allMVAVars);
294 
295  std::vector<float> vars;
296 
297  if( isEndcapCategory( findCategory( eleRecoPtr ) ) ) {
298  vars = packMVAVariables(allMVAVars.see,
299  allMVAVars.spp,
300  allMVAVars.OneMinusE1x5E5x5,
301  allMVAVars.R9,
302  allMVAVars.etawidth,
303  allMVAVars.phiwidth,
304  allMVAVars.HoE,
305  //Pure tracking variables
306  allMVAVars.kfhits,
307  allMVAVars.kfchi2,
308  allMVAVars.gsfchi2,
309  // Energy matching
310  allMVAVars.fbrem,
311  allMVAVars.gsfhits,
312  allMVAVars.expectedMissingInnerHits,
313  allMVAVars.convVtxFitProbability,
314  allMVAVars.EoP,
315  allMVAVars.eleEoPout,
316  allMVAVars.IoEmIoP,
317  // Geometrical matchings
318  allMVAVars.deta,
319  allMVAVars.dphi,
320  allMVAVars.detacalo,
321 
322  allMVAVars.pt,
323  allMVAVars.SCeta,
324  // Endcap only variables
325  allMVAVars.PreShowerOverRaw);
326  } else {
327  vars = packMVAVariables(allMVAVars.see,
328  allMVAVars.spp,
329  allMVAVars.OneMinusE1x5E5x5,
330  allMVAVars.R9,
331  allMVAVars.etawidth,
332  allMVAVars.phiwidth,
333  allMVAVars.HoE,
334  //Pure tracking variables
335  allMVAVars.kfhits,
336  allMVAVars.kfchi2,
337  allMVAVars.gsfchi2,
338  // Energy matching
339  allMVAVars.fbrem,
340  allMVAVars.gsfhits,
341  allMVAVars.expectedMissingInnerHits,
342  allMVAVars.convVtxFitProbability,
343  allMVAVars.EoP,
344  allMVAVars.eleEoPout,
345  allMVAVars.IoEmIoP,
346  // Geometrical matchings
347  allMVAVars.deta,
348  allMVAVars.dphi,
349  allMVAVars.detacalo,
350  allMVAVars.pt,
351  allMVAVars.SCeta);
352  }
353  return vars;
354 }
355 
357 
358  // Check that variables do not have crazy values
359 
360  if(allMVAVars.fbrem < -1.)
361  allMVAVars.fbrem = -1.;
362 
363  allMVAVars.deta = fabs(allMVAVars.deta);
364  if(allMVAVars.deta > 0.06)
365  allMVAVars.deta = 0.06;
366 
367 
368  allMVAVars.dphi = fabs(allMVAVars.dphi);
369  if(allMVAVars.dphi > 0.6)
370  allMVAVars.dphi = 0.6;
371 
372 
373  if(allMVAVars.EoP > 20.)
374  allMVAVars.EoP = 20.;
375 
376  if(allMVAVars.eleEoPout > 20.)
377  allMVAVars.eleEoPout = 20.;
378 
379 
380  allMVAVars.detacalo = fabs(allMVAVars.detacalo);
381  if(allMVAVars.detacalo > 0.2)
382  allMVAVars.detacalo = 0.2;
383 
384  if(allMVAVars.OneMinusE1x5E5x5 < -1.)
385  allMVAVars.OneMinusE1x5E5x5 = -1;
386 
387  if(allMVAVars.OneMinusE1x5E5x5 > 2.)
388  allMVAVars.OneMinusE1x5E5x5 = 2.;
389 
390 
391  if(allMVAVars.R9 > 5)
392  allMVAVars.R9 = 5;
393 
394  if(allMVAVars.gsfchi2 > 200.)
395  allMVAVars.gsfchi2 = 200;
396 
397 
398  if(allMVAVars.kfchi2 > 10.)
399  allMVAVars.kfchi2 = 10.;
400 }
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
void init(const std::vector< std::string > weightFileNames)
virtual TrackRef closestCtfTrackRef() const
Definition: GsfElectron.h:201
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:253
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:750
#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
float mvaValue(const edm::Ptr< reco::Candidate > &particle, const edm::Event &) const override
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
int findCategory(const edm::Ptr< reco::Candidate > &particle) const override
bool isValid() const
Definition: HandleBase.h:74
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:464
bool isEndcap(GeomDetEnumerators::SubDetector m)
bool isNull() const
Checks for null.
Definition: Ref.h:250
double ndof() const
Definition: Vertex.h:105
#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:837
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.
SuperClusterRef superCluster() const override
reference to a SuperCluster
Definition: GsfElectron.h:184
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)