CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PhotonMVAEstimatorRun2Spring15NonTrig.cc
Go to the documentation of this file.
2 
4 
7 {
8 
9  //
10  // Construct the MVA estimators
11  //
12  const std::vector <std::string> weightFileNames
13  = conf.getParameter<std::vector<std::string> >("weightFileNames");
14 
15  if( (int)(weightFileNames.size()) != nCategories )
16  throw cms::Exception("MVA config failure: ")
17  << "wrong number of weightfiles" << std::endl;
18 
19  _tmvaReaders.clear();
20  // The method name is just a key to retrieve this method later, it is not
21  // a control parameter for a reader (the full definition of the MVA type and
22  // everything else comes from the xml weight files).
23  _MethodName = "BDTG method";
24  // Create a TMVA reader object for each category
25  for(int i=0; i<nCategories; i++){
26 
27  // Use unique_ptr so that all readers are properly cleaned up
28  // when the vector clear() is called in the destructor
29 
30  edm::FileInPath weightFile( weightFileNames[i] );
31  _tmvaReaders.push_back( std::unique_ptr<TMVA::Reader>
32  ( createSingleReader(i, weightFile ) ) );
33 
34  }
35 
36 }
37 
40 
41  _tmvaReaders.clear();
42 }
43 
45 
46  // All tokens for event content needed by this MVA
47  // Cluster shapes
49  (_conf.getParameter<edm::InputTag>("full5x5SigmaIEtaIEtaMap"));
50 
52  (_conf.getParameter<edm::InputTag>("full5x5SigmaIEtaIPhiMap"));
53 
55  (_conf.getParameter<edm::InputTag>("full5x5E1x3Map"));
56 
58  (_conf.getParameter<edm::InputTag>("full5x5E2x2Map"));
59 
61  (_conf.getParameter<edm::InputTag>("full5x5E2x5MaxMap"));
62 
64  (_conf.getParameter<edm::InputTag>("full5x5E5x5Map"));
65 
67  (_conf.getParameter<edm::InputTag>("esEffSigmaRRMap"));
68 
69  // Isolations
71  (_conf.getParameter<edm::InputTag>("phoChargedIsolation"));
72 
74  (_conf.getParameter<edm::InputTag>("phoPhotonIsolation"));
75 
77  (_conf.getParameter<edm::InputTag>("phoWorstChargedIsolation"));
78 
79  // Pileup
80  _rhoToken = cc.consumes<double> (_conf.getParameter<edm::InputTag>("rho"));
81 
82 }
83 
85 
86  // Get the full5x5 and ES maps
94 
95  // Get the isolation maps
99 
100  // Get rho
101  iEvent.getByToken(_rhoToken,_rho);
102 
103  // Make sure everything is retrieved successfully
114  && _rho.isValid() ) )
115  throw cms::Exception("MVA failure: ")
116  << "Failed to retrieve event content needed for this MVA"
117  << std::endl
118  << "Check python MVA configuration file and make sure all needed"
119  << std::endl
120  << "producers are running upstream" << std::endl;
121 }
122 
123 
126 
127  int iCategory = findCategory( particle );
128  fillMVAVariables( particle );
130  float result = _tmvaReaders.at(iCategory)->EvaluateMVA(_MethodName);
131 
132  // DEBUG
133  const bool debug = false;
134  if( debug ){
135  printf("Printout of the photon variable inputs for MVA:\n");
136  printf(" varPhi_ %f\n", _allMVAVars.varPhi );
137  printf(" varR9_ %f\n", _allMVAVars.varR9 );
138  printf(" varSieie_ %f\n", _allMVAVars.varSieie );
139  printf(" varSieip_ %f\n", _allMVAVars.varSieip );
140  printf(" varE1x3overE5x5_ %f\n", _allMVAVars.varE1x3overE5x5);
141  printf(" varE2x2overE5x5_ %f\n", _allMVAVars.varE2x2overE5x5);
142  printf(" varE2x5overE5x5_ %f\n", _allMVAVars.varE2x5overE5x5);
143  printf(" varSCEta_ %f\n", _allMVAVars.varSCEta );
144  printf(" varRawE_ %f\n", _allMVAVars.varRawE );
145  printf(" varSCEtaWidth_ %f\n", _allMVAVars.varSCEtaWidth );
146  printf(" varSCPhiWidth_ %f\n", _allMVAVars.varSCPhiWidth );
147  printf(" varRho_ %f\n", _allMVAVars.varRho );
148  printf(" varPhoIsoRaw_ %f\n", _allMVAVars.varPhoIsoRaw );
149  printf(" varChIsoRaw_ %f\n", _allMVAVars.varChIsoRaw );
150  printf(" varWorstChRaw_ %f\n", _allMVAVars.varWorstChRaw );
151  printf(" varESEnOverRawE_ %f\n", _allMVAVars.varESEnOverRawE); // for endcap MVA only
152  printf(" varESEffSigmaRR_ %f\n", _allMVAVars.varESEffSigmaRR); // for endcap MVA only
153  // The spectators
154  printf(" varPt_ %f\n", _allMVAVars.varPt );
155  printf(" varEta_ %f\n", _allMVAVars.varEta );
156  }
157 
158  return result;
159 }
160 
162 
163  // Try to cast the particle into a reco particle.
164  // This should work for both reco and pat.
165  const edm::Ptr<reco::Photon> phoRecoPtr = ( edm::Ptr<reco::Photon> )particle;
166  if( phoRecoPtr.isNull() )
167  throw cms::Exception("MVA failure: ")
168  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
169  << " but appears to be neither" << std::endl;
170 
171  float eta = phoRecoPtr->superCluster()->eta();
172 
173  //
174  // Determine the category
175  //
176  int iCategory = UNDEFINED;
177  const float ebeeSplit = 1.479; // division between barrel and endcap
178 
179  if ( std::abs(eta) < ebeeSplit)
180  iCategory = CAT_EB;
181 
182  if (std::abs(eta) >= ebeeSplit)
183  iCategory = CAT_EE;
184 
185  return iCategory;
186 }
187 
190 
191  // For this specific MVA the function is trivial, but kept for possible
192  // future evolution to an MVA with more categories in eta
193  bool isEndcap = false;
194  if( category == CAT_EE )
195  isEndcap = true;
196 
197  return isEndcap;
198 }
199 
200 
202 createSingleReader(const int iCategory, const edm::FileInPath &weightFile){
203 
204  //
205  // Create the reader
206  //
207  TMVA::Reader *tmpTMVAReader = new TMVA::Reader( "!Color:Silent:Error" );
208 
209  //
210  // Configure all variables and spectators. Note: the order and names
211  // must match what is found in the xml weights file!
212  //
213 
214  tmpTMVAReader->AddVariable("recoPhi" , &_allMVAVars.varPhi);
215  tmpTMVAReader->AddVariable("r9" , &_allMVAVars.varR9);
216  tmpTMVAReader->AddVariable("sieieFull5x5", &_allMVAVars.varSieie);
217  tmpTMVAReader->AddVariable("sieipFull5x5", &_allMVAVars.varSieip);
218  tmpTMVAReader->AddVariable("e1x3Full5x5/e5x5Full5x5" , &_allMVAVars.varE1x3overE5x5);
219  tmpTMVAReader->AddVariable("e2x2Full5x5/e5x5Full5x5" , &_allMVAVars.varE2x2overE5x5);
220  tmpTMVAReader->AddVariable("e2x5Full5x5/e5x5Full5x5" , &_allMVAVars.varE2x5overE5x5);
221  tmpTMVAReader->AddVariable("recoSCEta" , &_allMVAVars.varSCEta);
222  tmpTMVAReader->AddVariable("rawE" , &_allMVAVars.varRawE);
223  tmpTMVAReader->AddVariable("scEtaWidth", &_allMVAVars.varSCEtaWidth);
224  tmpTMVAReader->AddVariable("scPhiWidth", &_allMVAVars.varSCPhiWidth);
225 
226  // Endcap only variables
227  if( isEndcapCategory(iCategory) ){
228  tmpTMVAReader->AddVariable("esEn/rawE" , &_allMVAVars.varESEnOverRawE);
229  tmpTMVAReader->AddVariable("esRR" , &_allMVAVars.varESEffSigmaRR);
230  }
231 
232  // Pileup
233  tmpTMVAReader->AddVariable("rho" , &_allMVAVars.varRho);
234 
235  // Isolations
236  tmpTMVAReader->AddVariable("phoIsoRaw" , &_allMVAVars.varPhoIsoRaw);
237  tmpTMVAReader->AddVariable("chIsoRaw" , &_allMVAVars.varChIsoRaw);
238  tmpTMVAReader->AddVariable("chWorstRaw", &_allMVAVars.varWorstChRaw);
239 
240  // Spectators
241  tmpTMVAReader->AddSpectator("recoPt" , &_allMVAVars.varPt);
242  tmpTMVAReader->AddSpectator("recoEta", &_allMVAVars.varEta);
243 
244  //
245  // Book the method and set up the weights file
246  //
247  tmpTMVAReader->BookMVA(_MethodName , weightFile.fullPath() );
248 
249  return tmpTMVAReader;
250 }
251 
252 // A function that should work on both pat and reco objects
254 
255  // Try to cast the particle into a reco particle.
256  // This should work for both reco and pat.
257  const edm::Ptr<reco::Photon> phoRecoPtr = ( edm::Ptr<reco::Photon> )particle;
258  if( phoRecoPtr.isNull() )
259  throw cms::Exception("MVA failure: ")
260  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
261  << " but appears to be neither" << std::endl;
262 
263  // Both pat and reco particles have exactly the same accessors.
264  auto superCluster = phoRecoPtr->superCluster();
265  // Full 5x5 cluster shapes. We could take some of this directly from
266  // the photon object, but some of these are not available.
267  float e1x3 = (*_full5x5E1x3Map )[ phoRecoPtr ];
268  float e2x2 = (*_full5x5E2x2Map )[ phoRecoPtr ];
269  float e2x5 = (*_full5x5E2x5MaxMap)[ phoRecoPtr ];
270  float e5x5 = (*_full5x5E5x5Map )[ phoRecoPtr ];
271 
272  _allMVAVars.varPhi = phoRecoPtr->phi();
273  _allMVAVars.varR9 = phoRecoPtr->r9() ;
274  _allMVAVars.varSieie = (*_full5x5SigmaIEtaIEtaMap)[ phoRecoPtr ]; // in principle, in the photon object as well
275  _allMVAVars.varSieip = (*_full5x5SigmaIEtaIPhiMap)[ phoRecoPtr ]; // not in the photon object
276  _allMVAVars.varE1x3overE5x5 = e1x3/e5x5;
277  _allMVAVars.varE2x2overE5x5 = e2x2/e5x5;
278  _allMVAVars.varE2x5overE5x5 = e2x5/e5x5;
279  _allMVAVars.varSCEta = superCluster->eta();
280  _allMVAVars.varRawE = superCluster->rawEnergy();
281  _allMVAVars.varSCEtaWidth = superCluster->etaWidth();
282  _allMVAVars.varSCPhiWidth = superCluster->phiWidth();
283  _allMVAVars.varESEnOverRawE = superCluster->preshowerEnergy() / superCluster->rawEnergy();
284  _allMVAVars.varESEffSigmaRR = (*_esEffSigmaRRMap)[ phoRecoPtr ];
285  _allMVAVars.varRho = *_rho;
286  _allMVAVars.varPhoIsoRaw = (*_phoPhotonIsolationMap)[phoRecoPtr];
287  _allMVAVars.varChIsoRaw = (*_phoChargedIsolationMap)[phoRecoPtr];
288  _allMVAVars.varWorstChRaw = (*_phoWorstChargedIsolationMap)[phoRecoPtr];
289  // Declare spectator vars
290  _allMVAVars.varPt = phoRecoPtr->pt();
291  _allMVAVars.varEta = phoRecoPtr->eta();
292 
293 }
294 
296 
297  // Check that variables do not have crazy values
298 
299  // This function is currently empty as this specific MVA was not
300  // developed with restricting variables to specific physical ranges.
301  return;
302 
303 }
304 
const edm::ParameterSet _conf
edm::Handle< edm::ValueMap< float > > _full5x5E2x5MaxMap
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
edm::Handle< edm::ValueMap< float > > _phoWorstChargedIsolationMap
edm::Handle< edm::ValueMap< float > > _phoChargedIsolationMap
edm::EDGetTokenT< edm::ValueMap< float > > _full5x5E1x3MapToken
edm::Handle< edm::ValueMap< float > > _full5x5E5x5Map
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
edm::Handle< edm::ValueMap< float > > _esEffSigmaRRMap
float mvaValue(const edm::Ptr< reco::Candidate > &particle)
edm::Handle< edm::ValueMap< float > > _full5x5E2x2Map
edm::EDGetTokenT< edm::ValueMap< float > > _phoWorstChargedIsolationToken
T eta() const
edm::EDGetTokenT< edm::ValueMap< float > > _esEffSigmaRRMapToken
edm::Handle< edm::ValueMap< float > > _full5x5SigmaIEtaIPhiMap
int iEvent
Definition: GenABIO.cc:230
PhotonMVAEstimatorRun2Spring15NonTrig(const edm::ParameterSet &conf)
tuple result
Definition: query.py:137
void fillMVAVariables(const edm::Ptr< reco::Candidate > &particle)
bool isNull() const
Checks for null.
Definition: Ptr.h:148
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void setConsumes(edm::ConsumesCollector &&) override
void getEventContent(const edm::Event &iEvent) override
edm::Handle< edm::ValueMap< float > > _full5x5E1x3Map
bool isValid() const
Definition: HandleBase.h:75
bool isEndcap(GeomDetEnumerators::SubDetector m)
tuple conf
Definition: dbtoconf.py:185
#define debug
Definition: HDRShower.cc:19
edm::EDGetTokenT< edm::ValueMap< float > > _full5x5E2x5MaxMapToken
TMVA::Reader * createSingleReader(const int iCategory, const edm::FileInPath &weightFile)
edm::EDGetTokenT< edm::ValueMap< float > > _full5x5SigmaIEtaIEtaMapToken
edm::EDGetTokenT< edm::ValueMap< float > > _full5x5SigmaIEtaIPhiMapToken
edm::Handle< edm::ValueMap< float > > _full5x5SigmaIEtaIEtaMap
edm::EDGetTokenT< edm::ValueMap< float > > _phoChargedIsolationToken
edm::EDGetTokenT< edm::ValueMap< float > > _full5x5E5x5MapToken
std::string fullPath() const
Definition: FileInPath.cc:165
int findCategory(const edm::Ptr< reco::Candidate > &particle)
std::vector< std::unique_ptr< TMVA::Reader > > _tmvaReaders
edm::EDGetTokenT< edm::ValueMap< float > > _full5x5E2x2MapToken
edm::EDGetTokenT< edm::ValueMap< float > > _phoPhotonIsolationToken
edm::Handle< edm::ValueMap< float > > _phoPhotonIsolationMap