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 
5 #include "TMVA/MethodBDT.h"
6 
9  _MethodName("BDTG method"),
10  _full5x5SigmaIEtaIEtaMapLabel(conf.getParameter<edm::InputTag>("full5x5SigmaIEtaIEtaMap")),
11  _full5x5SigmaIEtaIPhiMapLabel(conf.getParameter<edm::InputTag>("full5x5SigmaIEtaIPhiMap")),
12  _full5x5E1x3MapLabel(conf.getParameter<edm::InputTag>("full5x5E1x3Map")),
13  _full5x5E2x2MapLabel(conf.getParameter<edm::InputTag>("full5x5E2x2Map")),
14  _full5x5E2x5MaxMapLabel(conf.getParameter<edm::InputTag>("full5x5E2x5MaxMap")),
15  _full5x5E5x5MapLabel(conf.getParameter<edm::InputTag>("full5x5E5x5Map")),
16  _esEffSigmaRRMapLabel(conf.getParameter<edm::InputTag>("esEffSigmaRRMap")),
17  _phoChargedIsolationLabel(conf.getParameter<edm::InputTag>("phoChargedIsolation")),
18  _phoPhotonIsolationLabel(conf.getParameter<edm::InputTag>("phoPhotonIsolation")),
19  _phoWorstChargedIsolationLabel(conf.getParameter<edm::InputTag>("phoWorstChargedIsolation")),
20  _rhoLabel(conf.getParameter<edm::InputTag>("rho"))
21 {
22 
23  //
24  // Construct the MVA estimators
25  //
26  _tag = conf.getParameter<std::string>("mvaTag");
27 
28  const std::vector <std::string> weightFileNames
29  = conf.getParameter<std::vector<std::string> >("weightFileNames");
30 
31  if( (int)(weightFileNames.size()) != nCategories )
32  throw cms::Exception("MVA config failure: ")
33  << "wrong number of weightfiles" << std::endl;
34 
35  _gbrForests.clear();
36  // The method name is just a key to retrieve this method later, it is not
37  // a control parameter for a reader (the full definition of the MVA type and
38  // everything else comes from the xml weight files).
39 
40  // Create a TMVA reader object for each category
41  for(int i=0; i<nCategories; i++){
42 
43  // Use unique_ptr so that all readers are properly cleaned up
44  // when the vector clear() is called in the destructor
45 
46  edm::FileInPath weightFile( weightFileNames[i] );
47  _gbrForests.push_back( createSingleReader(i, weightFile ) );
48 
49  }
50 
51 }
52 
55 }
56 
58 mvaValue(const edm::Ptr<reco::Candidate>& particle, const edm::Event& iEvent) const {
59 
60  const int iCategory = findCategory( particle );
61  const std::vector<float> vars = std::move( fillMVAVariables( particle, iEvent ) );
62 
63  const float result = _gbrForests.at(iCategory)->GetClassifier(vars.data());
64 
65  // DEBUG
66  const bool debug = false;
67  if( debug ){
68  printf("Printout of the photon variable inputs for MVA:\n");
69  printf(" varPhi_ %f\n", vars[0] );
70  printf(" varR9_ %f\n", vars[1] );
71  printf(" varSieie_ %f\n", vars[2] );
72  printf(" varSieip_ %f\n", vars[3] );
73  printf(" varE1x3overE5x5_ %f\n", vars[4] );
74  printf(" varE2x2overE5x5_ %f\n", vars[5] );
75  printf(" varE2x5overE5x5_ %f\n", vars[6] );
76  printf(" varSCEta_ %f\n", vars[7] );
77  printf(" varRawE_ %f\n", vars[8] );
78  printf(" varSCEtaWidth_ %f\n", vars[9] );
79  printf(" varSCPhiWidth_ %f\n", vars[10] );
80  printf(" varRho_ %f\n", vars[11] );
81  printf(" varPhoIsoRaw_ %f\n", vars[12] );
82  printf(" varChIsoRaw_ %f\n", vars[13] );
83  printf(" varWorstChRaw_ %f\n", vars[14] );
84  if( isEndcapCategory( iCategory ) ) {
85  printf(" varESEnOverRawE_ %f\n", vars[15] ); // for endcap MVA only
86  printf(" varESEffSigmaRR_ %f\n", vars[16] ); // for endcap MVA only
87  // The spectators
88  printf(" varPt_ %f\n", vars[17] );
89  printf(" varEta_ %f\n", vars[18] );
90  } else {
91  // The spectators
92  printf(" varPt_ %f\n", vars[15] );
93  printf(" varEta_ %f\n", vars[16] );
94  }
95  }
96 
97  return result;
98 }
99 
101 
102  // Try to cast the particle into a reco particle.
103  // This should work for both reco and pat.
104  const edm::Ptr<reco::Photon> phoRecoPtr = ( edm::Ptr<reco::Photon> )particle;
105  if( phoRecoPtr.isNull() )
106  throw cms::Exception("MVA failure: ")
107  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
108  << " but appears to be neither" << std::endl;
109 
110  float eta = phoRecoPtr->superCluster()->eta();
111 
112  //
113  // Determine the category
114  //
115  int iCategory = UNDEFINED;
116  const float ebeeSplit = 1.479; // division between barrel and endcap
117 
118  if ( std::abs(eta) < ebeeSplit)
119  iCategory = CAT_EB;
120 
121  if (std::abs(eta) >= ebeeSplit)
122  iCategory = CAT_EE;
123 
124  return iCategory;
125 }
126 
129 
130  // For this specific MVA the function is trivial, but kept for possible
131  // future evolution to an MVA with more categories in eta
132  bool isEndcap = false;
133  if( category == CAT_EE )
134  isEndcap = true;
135 
136  return isEndcap;
137 }
138 
139 
140 std::unique_ptr<const GBRForest> PhotonMVAEstimatorRun2Spring15NonTrig::
141 createSingleReader(const int iCategory, const edm::FileInPath &weightFile){
142 
143  //
144  // Create the reader
145  //
146  TMVA::Reader tmpTMVAReader( "!Color:Silent:Error" );
147 
148  //
149  // Configure all variables and spectators. Note: the order and names
150  // must match what is found in the xml weights file!
151  //
152 
153  tmpTMVAReader.AddVariable("recoPhi" , &_allMVAVars.varPhi);
154  tmpTMVAReader.AddVariable("r9" , &_allMVAVars.varR9);
155  tmpTMVAReader.AddVariable("sieieFull5x5", &_allMVAVars.varSieie);
156  tmpTMVAReader.AddVariable("sieipFull5x5", &_allMVAVars.varSieip);
157  tmpTMVAReader.AddVariable("e1x3Full5x5/e5x5Full5x5", &_allMVAVars.varE1x3overE5x5);
158  tmpTMVAReader.AddVariable("e2x2Full5x5/e5x5Full5x5", &_allMVAVars.varE2x2overE5x5);
159  tmpTMVAReader.AddVariable("e2x5Full5x5/e5x5Full5x5", &_allMVAVars.varE2x5overE5x5);
160  tmpTMVAReader.AddVariable("recoSCEta" , &_allMVAVars.varSCEta);
161  tmpTMVAReader.AddVariable("rawE" , &_allMVAVars.varRawE);
162  tmpTMVAReader.AddVariable("scEtaWidth", &_allMVAVars.varSCEtaWidth);
163  tmpTMVAReader.AddVariable("scPhiWidth", &_allMVAVars.varSCPhiWidth);
164 
165  // Endcap only variables
166  if( isEndcapCategory(iCategory) ){
167  tmpTMVAReader.AddVariable("esEn/rawE" , &_allMVAVars.varESEnOverRawE);
168  tmpTMVAReader.AddVariable("esRR" , &_allMVAVars.varESEffSigmaRR);
169  }
170 
171  // Pileup
172  tmpTMVAReader.AddVariable("rho" , &_allMVAVars.varRho);
173 
174  // Isolations
175  tmpTMVAReader.AddVariable("phoIsoRaw" , &_allMVAVars.varPhoIsoRaw);
176  tmpTMVAReader.AddVariable("chIsoRaw" , &_allMVAVars.varChIsoRaw);
177  tmpTMVAReader.AddVariable("chWorstRaw", &_allMVAVars.varWorstChRaw);
178 
179  // Spectators
180  tmpTMVAReader.AddSpectator("recoPt" , &_allMVAVars.varPt);
181  tmpTMVAReader.AddSpectator("recoEta", &_allMVAVars.varEta);
182 
183  //
184  // Book the method and set up the weights file
185  //
186  std::unique_ptr<TMVA::IMethod> temp( tmpTMVAReader.BookMVA(_MethodName , weightFile.fullPath() ) );
187 
188  return std::unique_ptr<const GBRForest>( new GBRForest( dynamic_cast<TMVA::MethodBDT*>( tmpTMVAReader.FindMVA(_MethodName) ) ) );
189 
190 }
191 
192 // A function that should work on both pat and reco objects
194 
195  //
196  // Declare all value maps corresponding to the above tokens
197  //
198  edm::Handle<edm::ValueMap<float> > full5x5SigmaIEtaIEtaMap;
199  edm::Handle<edm::ValueMap<float> > full5x5SigmaIEtaIPhiMap;
200  edm::Handle<edm::ValueMap<float> > full5x5E1x3Map;
201  edm::Handle<edm::ValueMap<float> > full5x5E2x2Map;
202  edm::Handle<edm::ValueMap<float> > full5x5E2x5MaxMap;
203  edm::Handle<edm::ValueMap<float> > full5x5E5x5Map;
204  edm::Handle<edm::ValueMap<float> > esEffSigmaRRMap;
205  //
206  edm::Handle<edm::ValueMap<float> > phoChargedIsolationMap;
207  edm::Handle<edm::ValueMap<float> > phoPhotonIsolationMap;
208  edm::Handle<edm::ValueMap<float> > phoWorstChargedIsolationMap;
209 
210  // Rho will be pulled from the event content
212 
213  // Get the full5x5 and ES maps
214  iEvent.getByLabel(_full5x5SigmaIEtaIEtaMapLabel, full5x5SigmaIEtaIEtaMap);
215  iEvent.getByLabel(_full5x5SigmaIEtaIPhiMapLabel, full5x5SigmaIEtaIPhiMap);
216  iEvent.getByLabel(_full5x5E1x3MapLabel, full5x5E1x3Map);
217  iEvent.getByLabel(_full5x5E2x2MapLabel, full5x5E2x2Map);
218  iEvent.getByLabel(_full5x5E2x5MaxMapLabel, full5x5E2x5MaxMap);
219  iEvent.getByLabel(_full5x5E5x5MapLabel, full5x5E5x5Map);
220  iEvent.getByLabel(_esEffSigmaRRMapLabel, esEffSigmaRRMap);
221 
222  // Get the isolation maps
223  iEvent.getByLabel(_phoChargedIsolationLabel, phoChargedIsolationMap);
224  iEvent.getByLabel(_phoPhotonIsolationLabel, phoPhotonIsolationMap);
225  iEvent.getByLabel(_phoWorstChargedIsolationLabel, phoWorstChargedIsolationMap);
226 
227  // Get rho
228  iEvent.getByLabel(_rhoLabel,rho);
229 
230  // Make sure everything is retrieved successfully
231  if(! (full5x5SigmaIEtaIEtaMap.isValid()
232  && full5x5SigmaIEtaIPhiMap.isValid()
233  && full5x5E1x3Map.isValid()
234  && full5x5E2x2Map.isValid()
235  && full5x5E2x5MaxMap.isValid()
236  && full5x5E5x5Map.isValid()
237  && esEffSigmaRRMap.isValid()
238  && phoChargedIsolationMap.isValid()
239  && phoPhotonIsolationMap.isValid()
240  && phoWorstChargedIsolationMap.isValid()
241  && rho.isValid() ) )
242  throw cms::Exception("MVA failure: ")
243  << "Failed to retrieve event content needed for this MVA"
244  << std::endl
245  << "Check python MVA configuration file and make sure all needed"
246  << std::endl
247  << "producers are running upstream" << std::endl;
248 
249  // Try to cast the particle into a reco particle.
250  // This should work for both reco and pat.
251  const edm::Ptr<reco::Photon> phoRecoPtr(particle);
252  if( phoRecoPtr.isNull() )
253  throw cms::Exception("MVA failure: ")
254  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
255  << " but appears to be neither" << std::endl;
256 
257  // Both pat and reco particles have exactly the same accessors.
258  auto superCluster = phoRecoPtr->superCluster();
259  // Full 5x5 cluster shapes. We could take some of this directly from
260  // the photon object, but some of these are not available.
261  const float e1x3 = (*full5x5E1x3Map )[ phoRecoPtr ];
262  const float e2x2 = (*full5x5E2x2Map )[ phoRecoPtr ];
263  const float e2x5 = (*full5x5E2x5MaxMap)[ phoRecoPtr ];
264  const float e5x5 = (*full5x5E5x5Map )[ phoRecoPtr ];
265 
266  AllVariables allMVAVars;
267 
268  allMVAVars.varPhi = phoRecoPtr->phi();
269  allMVAVars.varR9 = phoRecoPtr->r9() ;
270  allMVAVars.varSieie = (*full5x5SigmaIEtaIEtaMap)[ phoRecoPtr ]; // in principle, in the photon object as well
271  allMVAVars.varSieip = (*full5x5SigmaIEtaIPhiMap)[ phoRecoPtr ]; // not in the photon object
272  allMVAVars.varE1x3overE5x5 = e1x3/e5x5;
273  allMVAVars.varE2x2overE5x5 = e2x2/e5x5;
274  allMVAVars.varE2x5overE5x5 = e2x5/e5x5;
275  allMVAVars.varSCEta = superCluster->eta();
276  allMVAVars.varRawE = superCluster->rawEnergy();
277  allMVAVars.varSCEtaWidth = superCluster->etaWidth();
278  allMVAVars.varSCPhiWidth = superCluster->phiWidth();
279  allMVAVars.varESEnOverRawE = superCluster->preshowerEnergy() / superCluster->rawEnergy();
280  allMVAVars.varESEffSigmaRR = (*esEffSigmaRRMap)[ phoRecoPtr ];
281  allMVAVars.varRho = *rho;
282  allMVAVars.varPhoIsoRaw = (*phoPhotonIsolationMap)[phoRecoPtr];
283  allMVAVars.varChIsoRaw = (*phoChargedIsolationMap)[phoRecoPtr];
284  allMVAVars.varWorstChRaw = (*phoWorstChargedIsolationMap)[phoRecoPtr];
285  // Declare spectator vars
286  allMVAVars.varPt = phoRecoPtr->pt();
287  allMVAVars.varEta = phoRecoPtr->eta();
288 
289  constrainMVAVariables(allMVAVars);
290 
291  std::vector<float> vars;
292  if( isEndcapCategory( findCategory( particle ) ) ) {
293  vars = std::move( packMVAVariables(allMVAVars.varPhi,
294  allMVAVars.varR9,
295  allMVAVars.varSieie,
296  allMVAVars.varSieip,
297  allMVAVars.varE1x3overE5x5,
298  allMVAVars.varE2x2overE5x5,
299  allMVAVars.varE2x5overE5x5,
300  allMVAVars.varSCEta,
301  allMVAVars.varRawE,
302  allMVAVars.varSCEtaWidth,
303  allMVAVars.varSCPhiWidth,
304  allMVAVars.varESEnOverRawE,
305  allMVAVars.varESEffSigmaRR,
306  allMVAVars.varRho,
307  allMVAVars.varPhoIsoRaw,
308  allMVAVars.varChIsoRaw,
309  allMVAVars.varWorstChRaw,
310  // Declare spectator vars
311  allMVAVars.varPt,
312  allMVAVars.varEta)
313  );
314  } else {
315  vars = std::move( packMVAVariables(allMVAVars.varPhi,
316  allMVAVars.varR9,
317  allMVAVars.varSieie,
318  allMVAVars.varSieip,
319  allMVAVars.varE1x3overE5x5,
320  allMVAVars.varE2x2overE5x5,
321  allMVAVars.varE2x5overE5x5,
322  allMVAVars.varSCEta,
323  allMVAVars.varRawE,
324  allMVAVars.varSCEtaWidth,
325  allMVAVars.varSCPhiWidth,
326  allMVAVars.varRho,
327  allMVAVars.varPhoIsoRaw,
328  allMVAVars.varChIsoRaw,
329  allMVAVars.varWorstChRaw,
330  // Declare spectator vars
331  allMVAVars.varPt,
332  allMVAVars.varEta)
333  );
334  }
335  return vars;
336 }
337 
339 
340  // Check that variables do not have crazy values
341 
342  // This function is currently empty as this specific MVA was not
343  // developed with restricting variables to specific physical ranges.
344  return;
345 
346 }
347 
359  cc.consumes<double>(_rhoLabel);
360 }
361 
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
float mvaValue(const edm::Ptr< reco::Candidate > &particle, const edm::Event &) const
std::unique_ptr< const GBRForest > createSingleReader(const int iCategory, const edm::FileInPath &weightFile)
std::vector< float > packMVAVariables(const Args...args) const
int iEvent
Definition: GenABIO.cc:230
PhotonMVAEstimatorRun2Spring15NonTrig(const edm::ParameterSet &conf)
tuple result
Definition: query.py:137
bool isNull() const
Checks for null.
Definition: Ptr.h:165
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool isValid() const
Definition: HandleBase.h:75
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:413
bool isEndcap(GeomDetEnumerators::SubDetector m)
tuple conf
Definition: dbtoconf.py:185
#define debug
Definition: HDRShower.cc:19
std::vector< float > fillMVAVariables(const edm::Ptr< reco::Candidate > &particle, const edm::Event &iEvent) const override
int findCategory(const edm::Ptr< reco::Candidate > &particle) const override
void setConsumes(edm::ConsumesCollector &&) const override
std::vector< std::unique_ptr< const GBRForest > > _gbrForests
std::string fullPath() const
Definition: FileInPath.cc:165