CMS 3D CMS Logo

PhotonMVAEstimatorRun2Phys14NonTrig.cc
Go to the documentation of this file.
4 
5 #include "TMVA/MethodBDT.h"
6 
9  // The method name is just a key to retrieve this method later, it is not
10  // a control parameter for a reader (the full definition of the MVA type and
11  // everything else comes from the xml weight files).
12  _MethodName("BDTG method"),
13  _useValueMaps(conf.getParameter<bool>("useValueMaps")),
14  _full5x5SigmaIEtaIEtaMapLabel(conf.getParameter<edm::InputTag>("full5x5SigmaIEtaIEtaMap")),
15  _full5x5SigmaIEtaIPhiMapLabel(conf.getParameter<edm::InputTag>("full5x5SigmaIEtaIPhiMap")),
16  _full5x5E1x3MapLabel(conf.getParameter<edm::InputTag>("full5x5E1x3Map")),
17  _full5x5E2x2MapLabel(conf.getParameter<edm::InputTag>("full5x5E2x2Map")),
18  _full5x5E2x5MaxMapLabel(conf.getParameter<edm::InputTag>("full5x5E2x5MaxMap")),
19  _full5x5E5x5MapLabel(conf.getParameter<edm::InputTag>("full5x5E5x5Map")),
20  _esEffSigmaRRMapLabel(conf.getParameter<edm::InputTag>("esEffSigmaRRMap")),
21  _phoChargedIsolationLabel(conf.getParameter<edm::InputTag>("phoChargedIsolation")),
22  _phoPhotonIsolationLabel(conf.getParameter<edm::InputTag>("phoPhotonIsolation")),
23  _phoWorstChargedIsolationLabel(conf.getParameter<edm::InputTag>("phoWorstChargedIsolation")),
24  _rhoLabel(conf.getParameter<edm::InputTag>("rho"))
25 {
26  //
27  // Construct the MVA estimators
28  //
29  _tag = conf.getParameter<std::string>("mvaTag");
30 
31  const std::vector <std::string> weightFileNames
32  = conf.getParameter<std::vector<std::string> >("weightFileNames");
33 
34  if( (int)(weightFileNames.size()) != nCategories )
35  throw cms::Exception("MVA config failure: ")
36  << "wrong number of weightfiles" << std::endl;
37 
38  // Create a TMVA reader object for each category
39  for(int i=0; i<nCategories; i++){
40  // Use unique_ptr so that all MVAs are properly cleaned up
41  // in the destructor
42  edm::FileInPath weightFile( weightFileNames[i] );
43  _gbrForests.push_back( GBRForestTools::createGBRForest( weightFile ) );
44  }
45 }
46 
49 }
50 
52 mvaValue( const edm::Ptr<reco::Candidate>& particle, const edm::Event& iEvent) const {
53 
54  const int iCategory = findCategory( particle );
55  const std::vector<float> vars = std::move( fillMVAVariables( particle, iEvent ) );
56  const float result = _gbrForests.at(iCategory)->GetResponse(vars.data()); // The BDT score
57 
58  // DEBUG
59  constexpr bool debug = false;
60  if( debug ){
61  printf("Printout of the photon variable inputs for MVA:\n");
62  printf(" varPhi_ %f\n", vars[0] );
63  printf(" varR9_ %f\n", vars[1] );
64  printf(" varSieie_ %f\n", vars[2] );
65  printf(" varSieip_ %f\n", vars[3] );
66  printf(" varE1x3overE5x5_ %f\n", vars[4] );
67  printf(" varE2x2overE5x5_ %f\n", vars[5] );
68  printf(" varE2x5overE5x5_ %f\n", vars[6] );
69  printf(" varSCEta_ %f\n", vars[7] );
70  printf(" varRawE_ %f\n", vars[8] );
71  printf(" varSCEtaWidth_ %f\n", vars[9] );
72  printf(" varSCPhiWidth_ %f\n", vars[10] );
73  printf(" varRho_ %f\n", vars[11] );
74  printf(" varPhoIsoRaw_ %f\n", vars[12] );
75  printf(" varChIsoRaw_ %f\n", vars[13] );
76  printf(" varWorstChRaw_ %f\n", vars[14] );
77  if( isEndcapCategory( iCategory ) ) {
78  printf(" varESEnOverRawE_ %f\n", vars[15] ); // for endcap MVA only
79  printf(" varESEffSigmaRR_ %f\n", vars[16] ); // for endcap MVA only
80  // The spectators
81  printf(" varPt_ %f\n", vars[17] );
82  printf(" varEta_ %f\n", vars[18] );
83  } else {
84  // The spectators
85  printf(" varPt_ %f\n", vars[15] );
86  printf(" varEta_ %f\n", vars[16] );
87  }
88  }
89 
90  return result;
91 }
92 
94 
95  // Try to cast the particle into a reco particle.
96  // This should work for both reco and pat.
97  const edm::Ptr<reco::Photon> phoRecoPtr(particle);
98  if( phoRecoPtr.isNull() )
99  throw cms::Exception("MVA failure: ")
100  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
101  << " but appears to be neither" << std::endl;
102 
103  const float eta = phoRecoPtr->superCluster()->eta();
104 
105  //
106  // Determine the category
107  //
108  int iCategory = UNDEFINED;
109  constexpr float ebeeSplit = 1.479; // division between barrel and endcap
110 
111  if( std::abs(eta) < ebeeSplit )
112  iCategory = CAT_EB;
113 
114  if( std::abs(eta) >= ebeeSplit )
115  iCategory = CAT_EE;
116 
117  return iCategory;
118 }
119 
122 
123  // For this specific MVA the function is trivial, but kept for possible
124  // future evolution to an MVA with more categories in eta
125  bool isEndcap = false;
126  if( category == CAT_EE )
127  isEndcap = true;
128 
129  return isEndcap;
130 }
131 
132 // A function that should work on both pat and reco objects
134  //
135  // Declare all value maps corresponding to the above tokens
136  //
144  //
145  edm::Handle<edm::ValueMap<float> > phoChargedIsolationMap;
146  edm::Handle<edm::ValueMap<float> > phoPhotonIsolationMap;
147  edm::Handle<edm::ValueMap<float> > phoWorstChargedIsolationMap;
148 
149  // Rho will be pulled from the event content
151 
152  // Get the full5x5 and ES maps
153  if( _useValueMaps ) {
154  iEvent.getByLabel(_full5x5SigmaIEtaIEtaMapLabel, full5x5SigmaIEtaIEtaMap);
155  iEvent.getByLabel(_full5x5SigmaIEtaIPhiMapLabel, full5x5SigmaIEtaIPhiMap);
156  iEvent.getByLabel(_full5x5E1x3MapLabel, full5x5E1x3Map);
157  iEvent.getByLabel(_full5x5E2x2MapLabel, full5x5E2x2Map);
158  iEvent.getByLabel(_full5x5E2x5MaxMapLabel, full5x5E2x5MaxMap);
159  iEvent.getByLabel(_full5x5E5x5MapLabel, full5x5E5x5Map);
160  iEvent.getByLabel(_esEffSigmaRRMapLabel, esEffSigmaRRMap);
161  }
162 
163  // Get the isolation maps
164  iEvent.getByLabel(_phoChargedIsolationLabel, phoChargedIsolationMap);
165  iEvent.getByLabel(_phoPhotonIsolationLabel, phoPhotonIsolationMap);
166  iEvent.getByLabel(_phoWorstChargedIsolationLabel, phoWorstChargedIsolationMap);
167 
168  // Get rho
169  iEvent.getByLabel(_rhoLabel,rho);
170 
171  // Make sure everything is retrieved successfully
172  if(! ( ( !_useValueMaps || ( full5x5SigmaIEtaIEtaMap.isValid()
173  && full5x5SigmaIEtaIPhiMap.isValid()
174  && full5x5E1x3Map.isValid()
175  && full5x5E2x2Map.isValid()
176  && full5x5E2x5MaxMap.isValid()
177  && full5x5E5x5Map.isValid()
178  && esEffSigmaRRMap.isValid() ) )
179  && phoChargedIsolationMap.isValid()
180  && phoPhotonIsolationMap.isValid()
181  && phoWorstChargedIsolationMap.isValid()
182  && rho.isValid() ) )
183  throw cms::Exception("MVA failure: ")
184  << "Failed to retrieve event content needed for this MVA"
185  << std::endl
186  << "Check python MVA configuration file and make sure all needed"
187  << std::endl
188  << "producers are running upstream" << std::endl;
189 
190  // Try to cast the particle into a reco particle.
191  // This should work for both reco and pat.
192  const edm::Ptr<reco::Photon> phoRecoPtr = ( edm::Ptr<reco::Photon> )particle;
193  AllVariables allMVAVars;
194  if( phoRecoPtr.isNull() )
195  throw cms::Exception("MVA failure: ")
196  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
197  << " but appears to be neither" << std::endl;
198 
199  // Both pat and reco particles have exactly the same accessors.
200  auto superCluster = phoRecoPtr->superCluster();
201  // Full 5x5 cluster shapes. We could take some of this directly from
202  // the photon object, but some of these are not available.
203  float e1x3 = std::numeric_limits<float>::max();
204  float e2x2 = std::numeric_limits<float>::max();
207  float full5x5_sigmaIetaIeta = std::numeric_limits<float>::max();
208  float full5x5_sigmaIetaIphi = std::numeric_limits<float>::max();
209  float effSigmaRR = std::numeric_limits<float>::max();
210 
211  if( _useValueMaps ) { //(before 752)
212  // in principle, in the photon object as well
213  // not in the photon object
214  e1x3 = (*full5x5E1x3Map )[ phoRecoPtr ];
215  e2x2 = (*full5x5E2x2Map )[ phoRecoPtr ];
216  e2x5 = (*full5x5E2x5MaxMap)[ phoRecoPtr ];
217  e5x5 = (*full5x5E5x5Map )[ phoRecoPtr ];
218  full5x5_sigmaIetaIeta = (*full5x5SigmaIEtaIEtaMap)[ phoRecoPtr ];
219  full5x5_sigmaIetaIphi = (*full5x5SigmaIEtaIPhiMap)[ phoRecoPtr ];
220  effSigmaRR = (*esEffSigmaRRMap)[ phoRecoPtr ];
221  } else {
222  // from 753
223  const auto& full5x5_pss = phoRecoPtr->full5x5_showerShapeVariables();
224  e1x3 = full5x5_pss.e1x3;
225  e2x2 = full5x5_pss.e2x2;
226  e2x5 = full5x5_pss.e2x5Max;
227  e5x5 = full5x5_pss.e5x5;
228  full5x5_sigmaIetaIeta = full5x5_pss.sigmaIetaIeta;
229  full5x5_sigmaIetaIphi = full5x5_pss.sigmaIetaIphi;
230  effSigmaRR = full5x5_pss.effSigmaRR;
231  }
232 
233  allMVAVars.varPhi = phoRecoPtr->phi();
234  allMVAVars.varR9 = phoRecoPtr->r9() ;
235  allMVAVars.varSieie = full5x5_sigmaIetaIeta;
236  allMVAVars.varSieip = full5x5_sigmaIetaIphi;
237  allMVAVars.varE1x3overE5x5 = e1x3/e5x5;
238  allMVAVars.varE2x2overE5x5 = e2x2/e5x5;
239  allMVAVars.varE2x5overE5x5 = e2x5/e5x5;
240  allMVAVars.varSCEta = superCluster->eta();
241  allMVAVars.varRawE = superCluster->rawEnergy();
242  allMVAVars.varSCEtaWidth = superCluster->etaWidth();
243  allMVAVars.varSCPhiWidth = superCluster->phiWidth();
244  allMVAVars.varESEnOverRawE = superCluster->preshowerEnergy() / superCluster->rawEnergy();
245  allMVAVars.varESEffSigmaRR = effSigmaRR;
246  allMVAVars.varRho = *rho;
247  allMVAVars.varPhoIsoRaw = (*phoPhotonIsolationMap)[phoRecoPtr];
248  allMVAVars.varChIsoRaw = (*phoChargedIsolationMap)[phoRecoPtr];
249  allMVAVars.varWorstChRaw = (*phoWorstChargedIsolationMap)[phoRecoPtr];
250  // Declare spectator vars
251  allMVAVars.varPt = phoRecoPtr->pt();
252  allMVAVars.varEta = phoRecoPtr->eta();
253 
254  constrainMVAVariables(allMVAVars);
255 
256  std::vector<float> vars;
257  if( isEndcapCategory( findCategory( particle ) ) ) {
258  vars = std::move( packMVAVariables(allMVAVars.varPhi,
259  allMVAVars.varR9,
260  allMVAVars.varSieie,
261  allMVAVars.varSieip,
262  allMVAVars.varE1x3overE5x5,
263  allMVAVars.varE2x2overE5x5,
264  allMVAVars.varE2x5overE5x5,
265  allMVAVars.varSCEta,
266  allMVAVars.varRawE,
267  allMVAVars.varSCEtaWidth,
268  allMVAVars.varSCPhiWidth,
269  allMVAVars.varESEnOverRawE,
270  allMVAVars.varESEffSigmaRR,
271  allMVAVars.varRho,
272  allMVAVars.varPhoIsoRaw,
273  allMVAVars.varChIsoRaw,
274  allMVAVars.varWorstChRaw,
275  // Declare spectator vars
276  allMVAVars.varPt,
277  allMVAVars.varEta)
278  );
279  } else {
280  vars = std::move( packMVAVariables(allMVAVars.varPhi,
281  allMVAVars.varR9,
282  allMVAVars.varSieie,
283  allMVAVars.varSieip,
284  allMVAVars.varE1x3overE5x5,
285  allMVAVars.varE2x2overE5x5,
286  allMVAVars.varE2x5overE5x5,
287  allMVAVars.varSCEta,
288  allMVAVars.varRawE,
289  allMVAVars.varSCEtaWidth,
290  allMVAVars.varSCPhiWidth,
291  allMVAVars.varRho,
292  allMVAVars.varPhoIsoRaw,
293  allMVAVars.varChIsoRaw,
294  allMVAVars.varWorstChRaw,
295  // Declare spectator vars
296  allMVAVars.varPt,
297  allMVAVars.varEta)
298  );
299  }
300 
301  return vars;
302 }
303 
305 
306  // Check that variables do not have crazy values
307 
308  // This function is currently empty as this specific MVA was not
309  // developed with restricting variables to specific physical ranges.
310  return;
311 
312 }
313 
315  if( _useValueMaps ) {
323  }
327  cc.consumes<double>(_rhoLabel);
328 }
329 
332  "PhotonMVAEstimatorRun2Phys14NonTrig");
T getParameter(std::string const &) const
double eta() const final
momentum pseudorapidity
void setConsumes(edm::ConsumesCollector &&) const override
double pt() const final
transverse momentum
reco::SuperClusterRef superCluster() const override
Ref to SuperCluster.
#define constexpr
std::vector< float > packMVAVariables(const Args...args) const
float mvaValue(const edm::Ptr< reco::Candidate > &particle, const edm::Event &) const override
int iEvent
Definition: GenABIO.cc:230
static std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightFile)
int findCategory(const edm::Ptr< reco::Candidate > &particle) const override
bool isNull() const
Checks for null.
Definition: Ptr.h:164
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)
PhotonMVAEstimatorRun2Phys14NonTrig(const edm::ParameterSet &conf)
#define debug
Definition: HDRShower.cc:19
HLT enums.
const ShowerShape & full5x5_showerShapeVariables() const
Definition: Photon.h:198
float r9() const
Definition: Photon.h:228
#define DEFINE_EDM_PLUGIN(factory, type, name)
std::vector< std::unique_ptr< const GBRForest > > _gbrForests
double phi() const final
momentum azimuthal angle
std::vector< float > fillMVAVariables(const edm::Ptr< reco::Candidate > &particle, const edm::Event &) const override
def move(src, dest)
Definition: eostools.py:510