CMS 3D CMS Logo

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