CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ElectronMVAEstimatorRun2Phys14NonTrig.cc
Go to the documentation of this file.
2 
5 
7 
9 
12 
13  const std::vector <std::string> weightFileNames
14  = conf.getParameter<std::vector<std::string> >("weightFileNames");
15 
16  if( (int)(weightFileNames.size()) != nCategories )
17  throw cms::Exception("MVA config failure: ")
18  << "wrong number of weightfiles" << std::endl;
19 
20  _tmvaReaders.clear();
21  _MethodName = "BDTG method";
22  // Create a TMVA reader object for each category
23  for(int i=0; i<nCategories; i++){
24 
25  // Use unique_ptr so that all readers are properly cleaned up
26  // when the vector clear() is called in the destructor
27 
28  edm::FileInPath weightFile( weightFileNames[i] );
29  _tmvaReaders.push_back( std::unique_ptr<TMVA::Reader> ( createSingleReader(i, weightFile ) ) );
30 
31  }
32 
33 }
34 
37 
38  _tmvaReaders.clear();
39 }
40 
43 
44  int iCategory = findCategory( particle );
45  fillMVAVariables( particle );
47  float result = _tmvaReaders.at(iCategory)->EvaluateMVA(_MethodName);
48 
49  bool debug = false;
50  if(debug) {
51  std::cout << " *** Inside the class _MethodName " << _MethodName << std::endl;
52  std::cout << " bin " << iCategory
53  << " fbrem " << _allMVAVars.fbrem
54  << " kfchi2 " << _allMVAVars.kfchi2
55  << " mykfhits " << _allMVAVars.kfhits
56  << " gsfchi2 " << _allMVAVars.gsfchi2
57  << " deta " << _allMVAVars.deta
58  << " dphi " << _allMVAVars.dphi
59  << " detacalo " << _allMVAVars.detacalo
60  << " see " << _allMVAVars.see
61  << " spp " << _allMVAVars.spp
62  << " etawidth " << _allMVAVars.etawidth
63  << " phiwidth " << _allMVAVars.phiwidth
64  << " OneMinusE1x5E5x5 " << _allMVAVars.OneMinusE1x5E5x5
65  << " R9 " << _allMVAVars.R9
66  << " HoE " << _allMVAVars.HoE
67  << " EoP " << _allMVAVars.EoP
68  << " IoEmIoP " << _allMVAVars.IoEmIoP
69  << " eleEoPout " << _allMVAVars.eleEoPout
70  //<< " d0 " << _allMVAVars.d0
71  // << " ip3d " << _allMVAVars.ip3d
72  << " eta " << _allMVAVars.SCeta
73  << " pt " << _allMVAVars.pt << std::endl;
74  std::cout << " ### MVA " << result << std::endl;
75  }
76 
77  return result;
78 }
79 
81 
82  // Try to cast the particle into a reco particle.
83  // This should work for both reco and pat.
84  const edm::Ptr<reco::GsfElectron> eleRecoPtr = ( edm::Ptr<reco::GsfElectron> )particle;
85  if( eleRecoPtr.get() == NULL )
86  throw cms::Exception("MVA failure: ")
87  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
88  << " but appears to be neither" << std::endl;
89 
90  float pt = eleRecoPtr->pt();
91  float eta = eleRecoPtr->superCluster()->eta();
92 
93  //
94  // Determine the category
95  //
96  int iCategory = UNDEFINED;
97  const float ptSplit = 10; // we have above and below 10 GeV categories
98  const float ebSplit = 0.800;// barrel is split into two regions
99  const float ebeeSplit = 1.479; // division between barrel and endcap
100 
101  if (pt < ptSplit && std::abs(eta) < ebSplit)
102  iCategory = CAT_EB1_PT5to10;
103 
104  if (pt < ptSplit && std::abs(eta) >= ebSplit && std::abs(eta) < ebeeSplit)
105  iCategory = CAT_EB2_PT5to10;
106 
107  if (pt < ptSplit && std::abs(eta) >= ebeeSplit)
108  iCategory = CAT_EE_PT5to10;
109 
110  if (pt >= ptSplit && std::abs(eta) < ebSplit)
111  iCategory = CAT_EB1_PT10plus;
112 
113  if (pt >= ptSplit && std::abs(eta) >= ebSplit && std::abs(eta) < ebeeSplit)
114  iCategory = CAT_EB2_PT10plus;
115 
116  if (pt >= ptSplit && std::abs(eta) >= ebeeSplit)
117  iCategory = CAT_EE_PT10plus;
118 
119  return iCategory;
120 }
121 
124 
125  bool isEndcap = false;
126  if( category == CAT_EE_PT5to10 || category == CAT_EE_PT10plus )
127  isEndcap = true;
128 
129  return isEndcap;
130 }
131 
132 
134 createSingleReader(const int iCategory, const edm::FileInPath &weightFile){
135 
136  //
137  // Create the reader
138  //
139  TMVA::Reader *tmpTMVAReader = new TMVA::Reader( "!Color:Silent:Error" );
140 
141  //
142  // Configure all variables and spectators. Note: the order and names
143  // must match what is found in the xml weights file!
144  //
145 
146  tmpTMVAReader->AddVariable("ele_kfhits", &_allMVAVars.kfhits);
147 
148  // Pure ECAL -> shower shapes
149  tmpTMVAReader->AddVariable("ele_oldsigmaietaieta", &_allMVAVars.see);
150  tmpTMVAReader->AddVariable("ele_oldsigmaiphiiphi", &_allMVAVars.spp);
151  tmpTMVAReader->AddVariable("ele_oldcircularity", &_allMVAVars.OneMinusE1x5E5x5);
152  tmpTMVAReader->AddVariable("ele_oldr9", &_allMVAVars.R9);
153  tmpTMVAReader->AddVariable("ele_scletawidth", &_allMVAVars.etawidth);
154  tmpTMVAReader->AddVariable("ele_sclphiwidth", &_allMVAVars.phiwidth);
155  tmpTMVAReader->AddVariable("ele_he", &_allMVAVars.HoE);
156  // Endcap only variables
157  if( isEndcapCategory(iCategory) )
158  tmpTMVAReader->AddVariable("ele_psEoverEraw", &_allMVAVars.PreShowerOverRaw);
159 
160  //Pure tracking variables
161  tmpTMVAReader->AddVariable("ele_kfchi2", &_allMVAVars.kfchi2);
162  tmpTMVAReader->AddVariable("ele_chi2_hits", &_allMVAVars.gsfchi2);
163 
164  // Energy matching
165  tmpTMVAReader->AddVariable("ele_fbrem", &_allMVAVars.fbrem);
166  tmpTMVAReader->AddVariable("ele_ep", &_allMVAVars.EoP);
167  tmpTMVAReader->AddVariable("ele_eelepout", &_allMVAVars.eleEoPout);
168  tmpTMVAReader->AddVariable("ele_IoEmIop", &_allMVAVars.IoEmIoP);
169 
170  // Geometrical matchings
171  tmpTMVAReader->AddVariable("ele_deltaetain", &_allMVAVars.deta);
172  tmpTMVAReader->AddVariable("ele_deltaphiin", &_allMVAVars.dphi);
173  tmpTMVAReader->AddVariable("ele_deltaetaseed", &_allMVAVars.detacalo);
174 
175  // Spectator variables
176  tmpTMVAReader->AddSpectator("ele_pT", &_allMVAVars.pt);
177  tmpTMVAReader->AddSpectator("ele_isbarrel", &_allMVAVars.isBarrel);
178  tmpTMVAReader->AddSpectator("ele_isendcap", &_allMVAVars.isEndcap);
179  tmpTMVAReader->AddSpectator("scl_eta", &_allMVAVars.SCeta);
180 
181  //
182  // Book the method and set up the weights file
183  //
184  tmpTMVAReader->BookMVA(_MethodName , weightFile.fullPath() );
185 
186  return tmpTMVAReader;
187 }
188 
189 // A function that should work on both pat and reco objects
191 
192  // Try to cast the particle into a reco particle.
193  // This should work for both reco and pat.
194  const edm::Ptr<reco::GsfElectron> eleRecoPtr = ( edm::Ptr<reco::GsfElectron> )particle;
195  if( eleRecoPtr.get() == NULL )
196  throw cms::Exception("MVA failure: ")
197  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
198  << " but appears to be neither" << std::endl;
199 
200  // Both pat and reco particles have exactly the same accessors, so we use a reco ptr
201  // throughout the code, with a single exception as of this writing, handled separately below.
202  auto superCluster = eleRecoPtr->superCluster();
203 
204  // To get to CTF track information in pat::Electron, we have to have the pointer
205  // to pat::Electron, it is not accessible from the pointer to reco::GsfElectron.
206  // This behavior is reported and is expected to change in the future (post-7.4.5 some time).
207  bool validKF= false;
208  reco::TrackRef myTrackRef = eleRecoPtr->closestCtfTrackRef();
209  const edm::Ptr<pat::Electron> elePatPtr(eleRecoPtr);
210  // Check if this is really a pat::Electron, and if yes, get the track ref from this new
211  // pointer instead
212  if( elePatPtr.get() != NULL )
213  myTrackRef = elePatPtr->closestCtfTrackRef();
214  validKF = (myTrackRef.isAvailable() && (myTrackRef.isNonnull()) );
215 
216  _allMVAVars.kfhits = (validKF) ? myTrackRef->hitPattern().trackerLayersWithMeasurement() : -1. ;
217  // Pure ECAL -> shower shapes
218  _allMVAVars.see = eleRecoPtr->full5x5_sigmaIetaIeta();
219  _allMVAVars.spp = eleRecoPtr->full5x5_sigmaIphiIphi();
220  _allMVAVars.OneMinusE1x5E5x5 = 1. - eleRecoPtr->full5x5_e1x5() / eleRecoPtr->full5x5_e5x5();
221  _allMVAVars.R9 = eleRecoPtr->full5x5_r9();
222  _allMVAVars.etawidth = superCluster->etaWidth();
223  _allMVAVars.phiwidth = superCluster->phiWidth();
224  _allMVAVars.HoE = eleRecoPtr->hadronicOverEm();
225  // Endcap only variables
226  _allMVAVars.PreShowerOverRaw = superCluster->preshowerEnergy() / superCluster->rawEnergy();
227  //Pure tracking variables
228  _allMVAVars.kfchi2 = (validKF) ? myTrackRef->normalizedChi2() : 0;
229  _allMVAVars.gsfchi2 = eleRecoPtr->gsfTrack()->normalizedChi2();
230  // Energy matching
231  _allMVAVars.fbrem = eleRecoPtr->fbrem();
232  _allMVAVars.EoP = eleRecoPtr->eSuperClusterOverP();
233  _allMVAVars.eleEoPout = eleRecoPtr->eEleClusterOverPout();
234  _allMVAVars.IoEmIoP = (1.0/eleRecoPtr->ecalEnergy()) - (1.0 / eleRecoPtr->p());
235  // Geometrical matchings
236  _allMVAVars.deta = eleRecoPtr->deltaEtaSuperClusterTrackAtVtx();
237  _allMVAVars.dphi = eleRecoPtr->deltaPhiSuperClusterTrackAtVtx();
238  _allMVAVars.detacalo = eleRecoPtr->deltaEtaSeedClusterTrackAtCalo();
239  // Spectator variables
240  _allMVAVars.pt = eleRecoPtr->pt();
241  float scEta = superCluster->eta();
242  _allMVAVars.isBarrel = ( std::abs(scEta) < 1.479 );
243  _allMVAVars.isEndcap = ( std::abs(scEta) >= 1.479);
244  _allMVAVars.SCeta = scEta;
245 
246 
247 }
248 
250 
251  // Check that variables do not have crazy values
252 
253  if(_allMVAVars.fbrem < -1.)
254  _allMVAVars.fbrem = -1.;
255 
257  if(_allMVAVars.deta > 0.06)
258  _allMVAVars.deta = 0.06;
259 
260 
262  if(_allMVAVars.dphi > 0.6)
263  _allMVAVars.dphi = 0.6;
264 
265 
266  if(_allMVAVars.EoP > 20.)
267  _allMVAVars.EoP = 20.;
268 
269  if(_allMVAVars.eleEoPout > 20.)
270  _allMVAVars.eleEoPout = 20.;
271 
272 
274  if(_allMVAVars.detacalo > 0.2)
275  _allMVAVars.detacalo = 0.2;
276 
277  if(_allMVAVars.OneMinusE1x5E5x5 < -1.)
279 
282 
283 
284 
285  if(_allMVAVars.R9 > 5)
286  _allMVAVars.R9 = 5;
287 
288  if(_allMVAVars.gsfchi2 > 200.)
289  _allMVAVars.gsfchi2 = 200;
290 
291 
292  if(_allMVAVars.kfchi2 > 10.)
293  _allMVAVars.kfchi2 = 10.;
294 
295 
296 }
297 
bool isAvailable() const
Definition: Ref.h:614
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
std::vector< std::unique_ptr< TMVA::Reader > > _tmvaReaders
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:250
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:143
#define NULL
Definition: scimark2.h:8
T eta() const
tuple result
Definition: query.py:137
ElectronMVAEstimatorRun2Phys14NonTrig(const edm::ParameterSet &conf)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool isEndcap(GeomDetEnumerators::SubDetector m)
tuple conf
Definition: dbtoconf.py:185
TMVA::Reader * createSingleReader(const int iCategory, const edm::FileInPath &weightFile)
#define debug
Definition: HDRShower.cc:19
int findCategory(const edm::Ptr< reco::Candidate > &particle)
float mvaValue(const edm::Ptr< reco::Candidate > &particle)
tuple cout
Definition: gather_cfg.py:121
void fillMVAVariables(const edm::Ptr< reco::Candidate > &particle)
std::string fullPath() const
Definition: FileInPath.cc:165