CMS 3D CMS Logo

PhotonMVAEstimator.cc
Go to the documentation of this file.
4 
7  , mvaVarMngr_ (conf.getParameter<std::string>("variableDefinition"))
8 {
9 
10  //
11  // Construct the MVA estimators
12  //
13  if (getTag() == "Run2Spring16NonTrigV1") {
14  effectiveAreas_ = std::make_unique<EffectiveAreas>((conf.getParameter<edm::FileInPath>("effAreasConfigFile")).fullPath());
15  phoIsoPtScalingCoeff_ = conf.getParameter<std::vector<double >>("phoIsoPtScalingCoeff");
16  phoIsoCutoff_ = conf.getParameter<double>("phoIsoCutoff");
17  }
18 
19  const std::vector <std::string> weightFileNames
20  = conf.getParameter<std::vector<std::string> >("weightFileNames");
21 
22  const std::vector <std::string> categoryCutStrings
23  = conf.getParameter<std::vector<std::string> >("categoryCuts");
24 
25  if( (int)(categoryCutStrings.size()) != getNCategories() )
26  throw cms::Exception("MVA config failure: ")
27  << "wrong number of category cuts in " << getName() << getTag() << std::endl;
28 
29  for (int i = 0; i < getNCategories(); ++i) {
30  StringCutObjectSelector<reco::Photon> select(categoryCutStrings[i]);
31  categoryFunctions_.push_back(select);
32  }
33 
34  // Initialize GBRForests
35  if( static_cast<int>(weightFileNames.size()) != getNCategories() )
36  throw cms::Exception("MVA config failure: ")
37  << "wrong number of weightfiles in " << getName() << getTag() << std::endl;
38 
39  gbrForests_.clear();
40  // Create a TMVA reader object for each category
41  for(int i=0; i<getNCategories(); i++){
42 
43  std::vector<int> variablesInCategory;
44 
45  std::vector<std::string> variableNamesInCategory;
46  gbrForests_.push_back(createGBRForest(weightFileNames[i], variableNamesInCategory));
47 
48  nVariables_.push_back(variableNamesInCategory.size());
49 
50  variables_.push_back(variablesInCategory);
51 
52  for (int j=0; j<nVariables_[i];++j) {
53  int index = mvaVarMngr_.getVarIndex(variableNamesInCategory[j]);
54  if(index == -1) {
55  throw cms::Exception("MVA config failure: ")
56  << "Concerning " << getName() << getTag() << std::endl
57  << "Variable " << variableNamesInCategory[j]
58  << " not found in variable definition file!" << std::endl;
59  }
60  variables_[i].push_back(index);
61 
62  }
63  }
64 }
65 
67 mvaValue(const edm::Ptr<reco::Candidate>& candPtr, const edm::EventBase& iEvent, int &iCategory) const {
68 
69  const edm::Ptr<reco::Photon> phoPtr{ candPtr };
70  if( phoPtr.get() == nullptr) {
71  throw cms::Exception("MVA failure: ")
72  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
73  << " but appears to be neither" << std::endl;
74  }
75 
76  iCategory = findCategory( phoPtr );
77 
78  std::vector<float> vars;
79 
80  for (int i = 0; i < nVariables_[iCategory]; ++i) {
81  vars.push_back(mvaVarMngr_.getValue(variables_[iCategory][i], phoPtr, iEvent));
82  }
83 
84  // Special case for Spring16!
85  if (getTag() == "Run2Spring16NonTrigV1" and iCategory == 1) { // Endcap category
86  // Raw value for EB only, because of loss of transparency in EE
87  // for endcap MVA only in 2016
88  double eA = effectiveAreas_->getEffectiveArea( std::abs(phoPtr->superCluster()->eta()) );
89  double phoIsoCorr = vars[10] - eA*(double)vars[9] - phoIsoPtScalingCoeff_.at(1) * phoPtr->pt();
90  vars[10] = TMath::Max( phoIsoCorr, phoIsoCutoff_);
91  }
92 
93  if(isDebug()) {
94  std::cout << " *** Inside " << getName() << getTag() << std::endl;
95  std::cout << " category " << iCategory << std::endl;
96  for (int i = 0; i < nVariables_[iCategory]; ++i) {
97  std::cout << " " << mvaVarMngr_.getName(variables_[iCategory][i]) << " " << vars[i] << std::endl;
98  }
99  }
100 
101  const float response = gbrForests_.at(iCategory)->GetResponse(vars.data());
102 
103  if(isDebug()) {
104  std::cout << " ### MVA " << response << std::endl << std::endl;
105  }
106 
107  return response;
108 }
109 
111 
112  const edm::Ptr<reco::Photon> phoPtr{ candPtr };
113  if( phoPtr.get() == nullptr ) {
114  throw cms::Exception("MVA failure: ")
115  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
116  << " but appears to be neither" << std::endl;
117  }
118 
119  return findCategory(phoPtr);
120 
121 }
122 
124 
125  for (int i = 0; i < getNCategories(); ++i) {
126  if (categoryFunctions_[i](*phoPtr)) return i;
127  }
128 
129  edm::LogWarning ("MVA warning") <<
130  "category not defined for particle with pt " << phoPtr->pt() << " GeV, eta " <<
131  phoPtr->superCluster()->eta() << " in " << getName() << getTag();
132 
133  return -1;
134 
135 }
136 
138  // All tokens for event content needed by this MVA
139  // Tags from the variable helper
141 }
142 
145  "PhotonMVAEstimator");
void setConsumes(edm::ConsumesCollector &&cc)
T getParameter(std::string const &) const
const std::string & getName(int index) const
PhotonMVAEstimator(const edm::ParameterSet &conf)
MVAVariableManager< reco::Photon > mvaVarMngr_
double pt() const final
transverse momentum
#define nullptr
reco::SuperClusterRef superCluster() const override
Ref to SuperCluster.
const std::string & getName() const
const std::string & getTag() const
int iEvent
Definition: GenABIO.cc:230
int findCategory(const edm::Ptr< reco::Candidate > &candPtr) const override
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void setConsumes(edm::ConsumesCollector &&) override
std::vector< StringCutObjectSelector< reco::Photon > > categoryFunctions_
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
T Max(T a, T b)
Definition: MathUtil.h:44
float mvaValue(const edm::Ptr< reco::Candidate > &candPtr, const edm::EventBase &iEvent, int &iCategory) const override
std::vector< int > nVariables_
std::vector< double > phoIsoPtScalingCoeff_
int getVarIndex(const std::string &name)
float getValue(int index, const edm::Ptr< ParticleType > &ptclPtr, const edm::EventBase &iEvent) const
std::unique_ptr< EffectiveAreas > effectiveAreas_
#define DEFINE_EDM_PLUGIN(factory, type, name)
std::vector< std::vector< int > > variables_
def move(src, dest)
Definition: eostools.py:511
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)