CMS 3D CMS Logo

ElectronMVAEstimatorRun2Spring15Trig.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( GBRForestTools::createGBRForest( 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 = fillMVAVariables( particle, iEvent );
66  const float result = _gbrForests.at(iCategory)->GetResponse(vars.data()); // The BDT score
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[22]
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() == nullptr )
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 eta = eleRecoPtr->superCluster()->eta();
108 
109  //
110  // Determine the category
111  //
112  int iCategory = UNDEFINED;
113  const float ebSplit = 0.800;// barrel is split into two regions
114  const float ebeeSplit = 1.479; // division between barrel and endcap
115 
116  if (std::abs(eta) < ebSplit)
117  iCategory = CAT_EB1;
118 
119  if (std::abs(eta) >= ebSplit && std::abs(eta) < ebeeSplit)
120  iCategory = CAT_EB2;
121 
122  if (std::abs(eta) >= ebeeSplit)
123  iCategory = CAT_EE;
124 
125  return iCategory;
126 }
127 
130 
131  bool isEndcap = false;
132  if( category == CAT_EE )
133  isEndcap = true;
134 
135  return isEndcap;
136 }
137 
138 // A function that should work on both pat and reco objects
141  const edm::Event& iEvent ) const {
142 
143  //
144  // Declare all value maps corresponding to the products we defined earlier
145  //
146  edm::Handle<reco::BeamSpot> theBeamSpot;
148 
149  // Get data needed for conversion rejection
150  iEvent.getByLabel(_beamSpotLabel, theBeamSpot);
151 
152  // Conversions in miniAOD and AOD have different names,
153  // but the same type, so we use the same handle with different tokens.
154  iEvent.getByLabel(_conversionsLabelAOD, conversions);
155  if( !conversions.isValid() )
156  iEvent.getByLabel(_conversionsLabelMiniAOD, conversions);
157 
158  // Make sure everything is retrieved successfully
159  if(! (theBeamSpot.isValid()
160  && conversions.isValid() )
161  )
162  throw cms::Exception("MVA failure: ")
163  << "Failed to retrieve event content needed for this MVA"
164  << std::endl
165  << "Check python MVA configuration file."
166  << std::endl;
167 
168  // Try to cast the particle into a reco particle.
169  // This should work for both reco and pat.
170  const edm::Ptr<reco::GsfElectron> eleRecoPtr = ( edm::Ptr<reco::GsfElectron> )particle;
171  if( eleRecoPtr.get() == nullptr )
172  throw cms::Exception("MVA failure: ")
173  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
174  << " but appears to be neither" << std::endl;
175 
176  // Both pat and reco particles have exactly the same accessors, so we use a reco ptr
177  // throughout the code, with a single exception as of this writing, handled separately below.
178  auto superCluster = eleRecoPtr->superCluster();
179 
180  AllVariables allMVAVars;
181 
182  // Pure ECAL -> shower shapes
183  allMVAVars.see = eleRecoPtr->full5x5_sigmaIetaIeta();
184  allMVAVars.spp = eleRecoPtr->full5x5_sigmaIphiIphi();
185  allMVAVars.OneMinusE1x5E5x5 = 1. - eleRecoPtr->full5x5_e1x5() / eleRecoPtr->full5x5_e5x5();
186  allMVAVars.R9 = eleRecoPtr->full5x5_r9();
187  allMVAVars.etawidth = superCluster->etaWidth();
188  allMVAVars.phiwidth = superCluster->phiWidth();
189  allMVAVars.HoE = eleRecoPtr->hadronicOverEm();
190  // Endcap only variables
191  allMVAVars.PreShowerOverRaw = superCluster->preshowerEnergy() / superCluster->rawEnergy();
192 
193  // To get to CTF track information in pat::Electron, we have to have the pointer
194  // to pat::Electron, it is not accessible from the pointer to reco::GsfElectron.
195  // This behavior is reported and is expected to change in the future (post-7.4.5 some time).
196  bool validKF= false;
197  reco::TrackRef myTrackRef = eleRecoPtr->closestCtfTrackRef();
198  const edm::Ptr<pat::Electron> elePatPtr(eleRecoPtr);
199  // Check if this is really a pat::Electron, and if yes, get the track ref from this new
200  // pointer instead
201  if( elePatPtr.get() != nullptr )
202  myTrackRef = elePatPtr->closestCtfTrackRef();
203  validKF = (myTrackRef.isAvailable() && (myTrackRef.isNonnull()) );
204 
205  //Pure tracking variables
206  allMVAVars.kfhits = (validKF) ? myTrackRef->hitPattern().trackerLayersWithMeasurement() : -1. ;
207  allMVAVars.kfchi2 = (validKF) ? myTrackRef->normalizedChi2() : 0;
208  allMVAVars.gsfchi2 = eleRecoPtr->gsfTrack()->normalizedChi2();
209 
210  // Energy matching
211  allMVAVars.fbrem = eleRecoPtr->fbrem();
212 
213  allMVAVars.gsfhits = eleRecoPtr->gsfTrack()->hitPattern().trackerLayersWithMeasurement();
214  allMVAVars.expectedMissingInnerHits = eleRecoPtr->gsfTrack()
215  ->hitPattern().numberOfLostHits(reco::HitPattern::MISSING_INNER_HITS);
216 
218  conversions,
219  theBeamSpot->position());
220  double vertexFitProbability = -1.;
221  if(!conv_ref.isNull()) {
222  const reco::Vertex &vtx = conv_ref.get()->conversionVertex(); if (vtx.isValid()) {
223  vertexFitProbability = TMath::Prob( vtx.chi2(), vtx.ndof());
224  }
225  }
226  allMVAVars.convVtxFitProbability = vertexFitProbability;
227 
228  allMVAVars.EoP = eleRecoPtr->eSuperClusterOverP();
229  allMVAVars.eleEoPout = eleRecoPtr->eEleClusterOverPout();
230  float pAtVertex = eleRecoPtr->trackMomentumAtVtx().R();
231  allMVAVars.IoEmIoP = (1.0/eleRecoPtr->ecalEnergy()) - (1.0 / pAtVertex );
232 
233  // Geometrical matchings
234  allMVAVars.deta = eleRecoPtr->deltaEtaSuperClusterTrackAtVtx();
235  allMVAVars.dphi = eleRecoPtr->deltaPhiSuperClusterTrackAtVtx();
236  allMVAVars.detacalo = eleRecoPtr->deltaEtaSeedClusterTrackAtCalo();
237 
238  // Spectator variables
239  allMVAVars.pt = eleRecoPtr->pt();
240  float scEta = superCluster->eta();
241  allMVAVars.SCeta = scEta;
242 
243  constrainMVAVariables(allMVAVars);
244 
245  std::vector<float> vars;
246 
247  if( isEndcapCategory( findCategory( particle ) ) ) {
248  vars = packMVAVariables(allMVAVars.see,
249  allMVAVars.spp,
250  allMVAVars.OneMinusE1x5E5x5,
251  allMVAVars.R9,
252  allMVAVars.etawidth,
253  allMVAVars.phiwidth,
254  allMVAVars.HoE,
255  // Endcap only variables
256  allMVAVars.PreShowerOverRaw,
257  //Pure tracking variables
258  allMVAVars.kfhits,
259  allMVAVars.kfchi2,
260  allMVAVars.gsfchi2,
261  // Energy matching
262  allMVAVars.fbrem,
263  allMVAVars.gsfhits,
264  allMVAVars.expectedMissingInnerHits,
265  allMVAVars.convVtxFitProbability,
266  allMVAVars.EoP,
267  allMVAVars.eleEoPout,
268  allMVAVars.IoEmIoP,
269  // Geometrical matchings
270  allMVAVars.deta,
271  allMVAVars.dphi,
272  allMVAVars.detacalo,
273  // Spectator variables
274  allMVAVars.pt,
275  allMVAVars.SCeta);
276  } else {
277  vars = packMVAVariables(allMVAVars.see,
278  allMVAVars.spp,
279  allMVAVars.OneMinusE1x5E5x5,
280  allMVAVars.R9,
281  allMVAVars.etawidth,
282  allMVAVars.phiwidth,
283  allMVAVars.HoE,
284  //Pure tracking variables
285  allMVAVars.kfhits,
286  allMVAVars.kfchi2,
287  allMVAVars.gsfchi2,
288  // Energy matching
289  allMVAVars.fbrem,
290  allMVAVars.gsfhits,
291  allMVAVars.expectedMissingInnerHits,
292  allMVAVars.convVtxFitProbability,
293  allMVAVars.EoP,
294  allMVAVars.eleEoPout,
295  allMVAVars.IoEmIoP,
296  // Geometrical matchings
297  allMVAVars.deta,
298  allMVAVars.dphi,
299  allMVAVars.detacalo,
300  // Spectator variables
301  allMVAVars.pt,
302  allMVAVars.SCeta);
303  }
304  return vars;
305 }
306 
308 
309  // Check that variables do not have crazy values
310 
311  if(allMVAVars.fbrem < -1.)
312  allMVAVars.fbrem = -1.;
313 
314  allMVAVars.deta = fabs(allMVAVars.deta);
315  if(allMVAVars.deta > 0.06)
316  allMVAVars.deta = 0.06;
317 
318 
319  allMVAVars.dphi = fabs(allMVAVars.dphi);
320  if(allMVAVars.dphi > 0.6)
321  allMVAVars.dphi = 0.6;
322 
323 
324  if(allMVAVars.EoP > 20.)
325  allMVAVars.EoP = 20.;
326 
327  if(allMVAVars.eleEoPout > 20.)
328  allMVAVars.eleEoPout = 20.;
329 
330 
331  allMVAVars.detacalo = fabs(allMVAVars.detacalo);
332  if(allMVAVars.detacalo > 0.2)
333  allMVAVars.detacalo = 0.2;
334 
335  if(allMVAVars.OneMinusE1x5E5x5 < -1.)
336  allMVAVars.OneMinusE1x5E5x5 = -1;
337 
338  if(allMVAVars.OneMinusE1x5E5x5 > 2.)
339  allMVAVars.OneMinusE1x5E5x5 = 2.;
340 
341 
342 
343  if(allMVAVars.R9 > 5)
344  allMVAVars.R9 = 5;
345 
346  if(allMVAVars.gsfchi2 > 200.)
347  allMVAVars.gsfchi2 = 200;
348 
349 
350  if(allMVAVars.kfchi2 > 10.)
351  allMVAVars.kfchi2 = 10.;
352 
353 
354 }
355 
358  "ElectronMVAEstimatorRun2Spring15Trig");
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
std::vector< float > fillMVAVariables(const edm::Ptr< reco::Candidate > &particle, const edm::Event &) const override
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 > > _gbrForests
float eSuperClusterOverP() const
Definition: GsfElectron.h:245
float full5x5_e5x5() const
Definition: GsfElectron.h:459
void setConsumes(edm::ConsumesCollector &&) const final
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
float mvaValue(const edm::Ptr< reco::Candidate > &particle, const edm::Event &) const override
#define nullptr
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
float hadronicOverEm() const
Definition: GsfElectron.h:487
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
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
ElectronMVAEstimatorRun2Spring15Trig(const edm::ParameterSet &conf)
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
HLT enums.
SuperClusterRef superCluster() const override
reference to a SuperCluster
Definition: GsfElectron.h:184
#define DEFINE_EDM_PLUGIN(factory, type, name)
const Point & position() const
position
Definition: BeamSpot.h:62
int findCategory(const edm::Ptr< reco::Candidate > &particle) 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)