CMS 3D CMS Logo

PhotonMVAEstimatorRun2Spring15NonTrig.cc
Go to the documentation of this file.
4 
5 #include "TMVA/MethodBDT.h"
6 
9  _MethodName("BDTG method"),
10  _useValueMaps(conf.getParameter<bool>("useValueMaps")),
11  _full5x5SigmaIEtaIEtaMapLabel(conf.getParameter<edm::InputTag>("full5x5SigmaIEtaIEtaMap")),
12  _full5x5SigmaIEtaIPhiMapLabel(conf.getParameter<edm::InputTag>("full5x5SigmaIEtaIPhiMap")),
13  _full5x5E1x3MapLabel(conf.getParameter<edm::InputTag>("full5x5E1x3Map")),
14  _full5x5E2x2MapLabel(conf.getParameter<edm::InputTag>("full5x5E2x2Map")),
15  _full5x5E2x5MaxMapLabel(conf.getParameter<edm::InputTag>("full5x5E2x5MaxMap")),
16  _full5x5E5x5MapLabel(conf.getParameter<edm::InputTag>("full5x5E5x5Map")),
17  _esEffSigmaRRMapLabel(conf.getParameter<edm::InputTag>("esEffSigmaRRMap")),
18  _phoChargedIsolationLabel(conf.getParameter<edm::InputTag>("phoChargedIsolation")),
19  _phoPhotonIsolationLabel(conf.getParameter<edm::InputTag>("phoPhotonIsolation")),
20  _phoWorstChargedIsolationLabel(conf.getParameter<edm::InputTag>("phoWorstChargedIsolation")),
21  _rhoLabel(conf.getParameter<edm::InputTag>("rho"))
22 {
23 
24  //
25  // Construct the MVA estimators
26  //
27  _tag = conf.getParameter<std::string>("mvaTag");
28 
29  const std::vector <std::string> weightFileNames
30  = conf.getParameter<std::vector<std::string> >("weightFileNames");
31 
32  if( (int)(weightFileNames.size()) != nCategories )
33  throw cms::Exception("MVA config failure: ")
34  << "wrong number of weightfiles" << std::endl;
35 
36  _gbrForests.clear();
37  // The method name is just a key to retrieve this method later, it is not
38  // a control parameter for a reader (the full definition of the MVA type and
39  // everything else comes from the xml weight files).
40 
41  // Create a TMVA reader object for each category
42  for(int i=0; i<nCategories; i++){
43 
44  // Use unique_ptr so that all readers are properly cleaned up
45  // when the vector clear() is called in the destructor
46 
47  edm::FileInPath weightFile( weightFileNames[i] );
48  _gbrForests.push_back( GBRForestTools::createGBRForest( weightFile ) );
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)->GetResponse(vars.data()); // The BDT score
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 // A function that should work on both pat and reco objects
141 
142  //
143  // Declare all value maps corresponding to the above tokens
144  //
152  //
153  edm::Handle<edm::ValueMap<float> > phoChargedIsolationMap;
154  edm::Handle<edm::ValueMap<float> > phoPhotonIsolationMap;
155  edm::Handle<edm::ValueMap<float> > phoWorstChargedIsolationMap;
156 
157  // Rho will be pulled from the event content
159 
160  // Get the full5x5 and ES maps
161  if( _useValueMaps ) {
162  iEvent.getByLabel(_full5x5SigmaIEtaIEtaMapLabel, full5x5SigmaIEtaIEtaMap);
163  iEvent.getByLabel(_full5x5SigmaIEtaIPhiMapLabel, full5x5SigmaIEtaIPhiMap);
164  iEvent.getByLabel(_full5x5E1x3MapLabel, full5x5E1x3Map);
165  iEvent.getByLabel(_full5x5E2x2MapLabel, full5x5E2x2Map);
166  iEvent.getByLabel(_full5x5E2x5MaxMapLabel, full5x5E2x5MaxMap);
167  iEvent.getByLabel(_full5x5E5x5MapLabel, full5x5E5x5Map);
168  iEvent.getByLabel(_esEffSigmaRRMapLabel, esEffSigmaRRMap);
169  }
170 
171  // Get the isolation maps
172  iEvent.getByLabel(_phoChargedIsolationLabel, phoChargedIsolationMap);
173  iEvent.getByLabel(_phoPhotonIsolationLabel, phoPhotonIsolationMap);
174  iEvent.getByLabel(_phoWorstChargedIsolationLabel, phoWorstChargedIsolationMap);
175 
176  // Get rho
177  iEvent.getByLabel(_rhoLabel,rho);
178 
179  // Make sure everything is retrieved successfully
180  if(! ( ( !_useValueMaps || ( full5x5SigmaIEtaIEtaMap.isValid()
181  && full5x5SigmaIEtaIPhiMap.isValid()
182  && full5x5E1x3Map.isValid()
183  && full5x5E2x2Map.isValid()
184  && full5x5E2x5MaxMap.isValid()
185  && full5x5E5x5Map.isValid()
186  && esEffSigmaRRMap.isValid() ) )
187  && phoChargedIsolationMap.isValid()
188  && phoPhotonIsolationMap.isValid()
189  && phoWorstChargedIsolationMap.isValid()
190  && rho.isValid() ) )
191  throw cms::Exception("MVA failure: ")
192  << "Failed to retrieve event content needed for this MVA"
193  << std::endl
194  << "Check python MVA configuration file and make sure all needed"
195  << std::endl
196  << "producers are running upstream" << std::endl;
197 
198  // Try to cast the particle into a reco particle.
199  // This should work for both reco and pat.
200  const edm::Ptr<reco::Photon> phoRecoPtr(particle);
201  if( phoRecoPtr.isNull() )
202  throw cms::Exception("MVA failure: ")
203  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
204  << " but appears to be neither" << std::endl;
205 
206  // Both pat and reco particles have exactly the same accessors.
207  auto superCluster = phoRecoPtr->superCluster();
208  // Full 5x5 cluster shapes. We could take some of this directly from
209  // the photon object, but some of these are not available.
210  float e1x3 = std::numeric_limits<float>::max();
211  float e2x2 = std::numeric_limits<float>::max();
214  float full5x5_sigmaIetaIeta = std::numeric_limits<float>::max();
215  float full5x5_sigmaIetaIphi = std::numeric_limits<float>::max();
216  float effSigmaRR = std::numeric_limits<float>::max();
217 
218  AllVariables allMVAVars;
219 
220  if( _useValueMaps ) { //(before 752)
221  // in principle, in the photon object as well
222  // not in the photon object
223  e1x3 = (*full5x5E1x3Map )[ phoRecoPtr ];
224  e2x2 = (*full5x5E2x2Map )[ phoRecoPtr ];
225  e2x5 = (*full5x5E2x5MaxMap)[ phoRecoPtr ];
226  e5x5 = (*full5x5E5x5Map )[ phoRecoPtr ];
227  full5x5_sigmaIetaIeta = (*full5x5SigmaIEtaIEtaMap)[ phoRecoPtr ];
228  full5x5_sigmaIetaIphi = (*full5x5SigmaIEtaIPhiMap)[ phoRecoPtr ];
229  effSigmaRR = (*esEffSigmaRRMap)[ phoRecoPtr ];
230  } else {
231  // from 753
232  const auto& full5x5_pss = phoRecoPtr->full5x5_showerShapeVariables();
233  e1x3 = full5x5_pss.e1x3;
234  e2x2 = full5x5_pss.e2x2;
235  e2x5 = full5x5_pss.e2x5Max;
236  e5x5 = full5x5_pss.e5x5;
237  full5x5_sigmaIetaIeta = full5x5_pss.sigmaIetaIeta;
238  full5x5_sigmaIetaIphi = full5x5_pss.sigmaIetaIphi;
239  effSigmaRR = full5x5_pss.effSigmaRR;
240  }
241 
242  allMVAVars.varPhi = phoRecoPtr->phi();
243  allMVAVars.varR9 = phoRecoPtr->r9() ;
244  allMVAVars.varSieie = full5x5_sigmaIetaIeta;
245  allMVAVars.varSieip = full5x5_sigmaIetaIphi;
246  allMVAVars.varE1x3overE5x5 = e1x3/e5x5;
247  allMVAVars.varE2x2overE5x5 = e2x2/e5x5;
248  allMVAVars.varE2x5overE5x5 = e2x5/e5x5;
249  allMVAVars.varSCEta = superCluster->eta();
250  allMVAVars.varRawE = superCluster->rawEnergy();
251  allMVAVars.varSCEtaWidth = superCluster->etaWidth();
252  allMVAVars.varSCPhiWidth = superCluster->phiWidth();
253  allMVAVars.varESEnOverRawE = superCluster->preshowerEnergy() / superCluster->rawEnergy();
254  allMVAVars.varESEffSigmaRR = effSigmaRR;
255  allMVAVars.varRho = *rho;
256  allMVAVars.varPhoIsoRaw = (*phoPhotonIsolationMap)[phoRecoPtr];
257  allMVAVars.varChIsoRaw = (*phoChargedIsolationMap)[phoRecoPtr];
258  allMVAVars.varWorstChRaw = (*phoWorstChargedIsolationMap)[phoRecoPtr];
259  // Declare spectator vars
260  allMVAVars.varPt = phoRecoPtr->pt();
261  allMVAVars.varEta = phoRecoPtr->eta();
262 
263  constrainMVAVariables(allMVAVars);
264 
265  std::vector<float> vars;
266  if( isEndcapCategory( findCategory( particle ) ) ) {
267  vars = std::move( packMVAVariables(allMVAVars.varPhi,
268  allMVAVars.varR9,
269  allMVAVars.varSieie,
270  allMVAVars.varSieip,
271  allMVAVars.varE1x3overE5x5,
272  allMVAVars.varE2x2overE5x5,
273  allMVAVars.varE2x5overE5x5,
274  allMVAVars.varSCEta,
275  allMVAVars.varRawE,
276  allMVAVars.varSCEtaWidth,
277  allMVAVars.varSCPhiWidth,
278  allMVAVars.varESEnOverRawE,
279  allMVAVars.varESEffSigmaRR,
280  allMVAVars.varRho,
281  allMVAVars.varPhoIsoRaw,
282  allMVAVars.varChIsoRaw,
283  allMVAVars.varWorstChRaw,
284  // Declare spectator vars
285  allMVAVars.varPt,
286  allMVAVars.varEta)
287  );
288  } else {
289  vars = std::move( packMVAVariables(allMVAVars.varPhi,
290  allMVAVars.varR9,
291  allMVAVars.varSieie,
292  allMVAVars.varSieip,
293  allMVAVars.varE1x3overE5x5,
294  allMVAVars.varE2x2overE5x5,
295  allMVAVars.varE2x5overE5x5,
296  allMVAVars.varSCEta,
297  allMVAVars.varRawE,
298  allMVAVars.varSCEtaWidth,
299  allMVAVars.varSCPhiWidth,
300  allMVAVars.varRho,
301  allMVAVars.varPhoIsoRaw,
302  allMVAVars.varChIsoRaw,
303  allMVAVars.varWorstChRaw,
304  // Declare spectator vars
305  allMVAVars.varPt,
306  allMVAVars.varEta)
307  );
308  }
309  return vars;
310 }
311 
313 
314  // Check that variables do not have crazy values
315 
316  // This function is currently empty as this specific MVA was not
317  // developed with restricting variables to specific physical ranges.
318  return;
319 
320 }
321 
323  if( _useValueMaps ) {
331  }
335  cc.consumes<double>(_rhoLabel);
336 }
337 
340  "PhotonMVAEstimatorRun2Spring15NonTrig");
T getParameter(std::string const &) const
double eta() const final
momentum pseudorapidity
double pt() const final
transverse momentum
reco::SuperClusterRef superCluster() const override
Ref to SuperCluster.
std::vector< float > packMVAVariables(const Args...args) const
int iEvent
Definition: GenABIO.cc:230
PhotonMVAEstimatorRun2Spring15NonTrig(const edm::ParameterSet &conf)
static std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightFile)
bool isNull() const
Checks for null.
Definition: Ptr.h:164
void setConsumes(edm::ConsumesCollector &&) const override
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
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)
#define debug
Definition: HDRShower.cc:19
float mvaValue(const edm::Ptr< reco::Candidate > &particle, const edm::Event &) const override
HLT enums.
const ShowerShape & full5x5_showerShapeVariables() const
Definition: Photon.h:198
float r9() const
Definition: Photon.h:228
int findCategory(const edm::Ptr< reco::Candidate > &particle) const override
#define DEFINE_EDM_PLUGIN(factory, type, name)
std::vector< std::unique_ptr< const GBRForest > > _gbrForests
std::vector< float > fillMVAVariables(const edm::Ptr< reco::Candidate > &particle, const edm::Event &iEvent) const override
double phi() const final
momentum azimuthal angle
def move(src, dest)
Definition: eostools.py:510