CMS 3D CMS Logo

PhotonMVAEstimator.cc
Go to the documentation of this file.
15 
17 public:
18  // Constructor and destructor
20  ~PhotonMVAEstimator() override{};
21 
22  // Calculation of the MVA value
23  float mvaValue(const reco::Candidate* candPtr, std::vector<float> const& auxVars, int& iCategory) const override;
24 
25  int findCategory(const reco::Candidate* candPtr) const override;
26 
27  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
28 
29 private:
30  int findCategory(reco::Photon const& photon) const;
31 
32  // The number of categories and number of variables per category
34  std::vector<ThreadSafeFunctor<StringCutObjectSelector<reco::Photon>>> categoryFunctions_;
35  std::vector<int> nVariables_;
36 
37  // Data members
38  std::vector<std::unique_ptr<const GBRForest>> gbrForests_;
39 
40  // There might be different variables for each category, so the variables
41  // names vector is itself a vector of length nCategories
42  std::vector<std::vector<int>> variables_;
43 
44  // The variable manager which stores how to obtain the variables
46 
47  // Other objects needed by the MVA
48  std::unique_ptr<EffectiveAreas> effectiveAreas_;
49  std::vector<double> phoIsoPtScalingCoeff_;
50  double phoIsoCutoff_;
51 };
52 
55  mvaVarMngr_(conf.getParameter<std::string>("variableDefinition"), MVAVariableHelper::indexMap()) {
56  //
57  // Construct the MVA estimators
58  //
59  if (getTag() == "Run2Spring16NonTrigV1") {
61  std::make_unique<EffectiveAreas>((conf.getParameter<edm::FileInPath>("effAreasConfigFile")).fullPath());
62  phoIsoPtScalingCoeff_ = conf.getParameter<std::vector<double>>("phoIsoPtScalingCoeff");
63  phoIsoCutoff_ = conf.getParameter<double>("phoIsoCutoff");
64  }
65 
66  const auto weightFileNames = conf.getParameter<std::vector<std::string>>("weightFileNames");
67  const auto categoryCutStrings = conf.getParameter<std::vector<std::string>>("categoryCuts");
68 
69  if ((int)(categoryCutStrings.size()) != getNCategories())
70  throw cms::Exception("MVA config failure: ")
71  << "wrong number of category cuts in PhotonMVAEstimator" << getTag() << std::endl;
72 
73  for (auto const& cut : categoryCutStrings)
74  categoryFunctions_.emplace_back(cut);
75 
76  // Initialize GBRForests
77  if (static_cast<int>(weightFileNames.size()) != getNCategories())
78  throw cms::Exception("MVA config failure: ")
79  << "wrong number of weightfiles in PhotonMVAEstimator" << getTag() << std::endl;
80 
81  gbrForests_.clear();
82  // Create a TMVA reader object for each category
83  for (int i = 0; i < getNCategories(); i++) {
84  std::vector<int> variablesInCategory;
85 
86  std::vector<std::string> variableNamesInCategory;
87  gbrForests_.push_back(createGBRForest(weightFileNames[i], variableNamesInCategory));
88 
89  nVariables_.push_back(variableNamesInCategory.size());
90 
91  variables_.push_back(variablesInCategory);
92 
93  for (int j = 0; j < nVariables_[i]; ++j) {
94  int index = mvaVarMngr_.getVarIndex(variableNamesInCategory[j]);
95  if (index == -1) {
96  throw cms::Exception("MVA config failure: ")
97  << "Concerning PhotonMVAEstimator" << getTag() << std::endl
98  << "Variable " << variableNamesInCategory[j] << " not found in variable definition file!" << std::endl;
99  }
100  variables_[i].push_back(index);
101  }
102  }
103 }
104 
106  std::vector<float> const& auxVars,
107  int& iCategory) const {
108  const reco::Photon* phoPtr = dynamic_cast<const reco::Photon*>(candPtr);
109  if (phoPtr == nullptr) {
110  throw cms::Exception("MVA failure: ")
111  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
112  << " but appears to be neither" << std::endl;
113  }
114 
115  iCategory = findCategory(phoPtr);
116 
117  std::vector<float> vars;
118 
119  vars.reserve(nVariables_[iCategory]);
120  for (int i = 0; i < nVariables_[iCategory]; ++i) {
121  vars.push_back(mvaVarMngr_.getValue(variables_[iCategory][i], *phoPtr, auxVars));
122  }
123 
124  // Special case for Spring16!
125  if (getTag() == "Run2Spring16NonTrigV1" and iCategory == 1) { // Endcap category
126  // Raw value for EB only, because of loss of transparency in EE
127  // for endcap MVA only in 2016
128  double eA = effectiveAreas_->getEffectiveArea(std::abs(phoPtr->superCluster()->eta()));
129  double phoIsoCorr = vars[10] - eA * (double)vars[9] - phoIsoPtScalingCoeff_.at(1) * phoPtr->pt();
130  vars[10] = std::max(phoIsoCorr, phoIsoCutoff_);
131  }
132 
133  if (isDebug()) {
134  std::cout << " *** Inside PhotonMVAEstimator" << getTag() << std::endl;
135  std::cout << " category " << iCategory << std::endl;
136  for (int i = 0; i < nVariables_[iCategory]; ++i) {
137  std::cout << " " << mvaVarMngr_.getName(variables_[iCategory][i]) << " " << vars[i] << std::endl;
138  }
139  }
140 
141  const float response = gbrForests_.at(iCategory)->GetResponse(vars.data());
142 
143  if (isDebug()) {
144  std::cout << " ### MVA " << response << std::endl << std::endl;
145  }
146 
147  return response;
148 }
149 
151  const reco::Photon* phoPtr = dynamic_cast<const reco::Photon*>(candPtr);
152  if (phoPtr == nullptr) {
153  throw cms::Exception("MVA failure: ")
154  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
155  << " but appears to be neither" << std::endl;
156  }
157 
158  return findCategory(*phoPtr);
159 }
160 
162  for (int i = 0; i < getNCategories(); ++i) {
164  return i;
165  }
166 
167  edm::LogWarning("MVA warning") << "category not defined for particle with pt " << photon.pt() << " GeV, eta "
168  << photon.superCluster()->eta() << " in PhotonMVAEstimator" << getTag();
169 
170  return -1;
171 }
172 
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
double pt() const final
transverse momentum
int findCategory(const reco::Candidate *candPtr) const override
PhotonMVAEstimator(const edm::ParameterSet &conf)
MVAVariableManager< reco::Photon > mvaVarMngr_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const std::string & getName(int index) const
reco::SuperClusterRef superCluster() const override
Ref to SuperCluster.
std::vector< ThreadSafeFunctor< StringCutObjectSelector< reco::Photon > > > categoryFunctions_
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const std::string & getTag() const
float getValue(int index, const ParticleType &particle, const std::vector< float > &auxVariables) const
std::vector< int > nVariables_
std::vector< double > phoIsoPtScalingCoeff_
int getVarIndex(const std::string &name)
float mvaValue(const reco::Candidate *candPtr, std::vector< float > const &auxVars, int &iCategory) const override
std::unique_ptr< EffectiveAreas > effectiveAreas_
#define DEFINE_EDM_PLUGIN(factory, type, name)
std::vector< std::vector< int > > variables_
~PhotonMVAEstimator() override
vars
Definition: DeepTauId.cc:166
Log< level::Warning, false > LogWarning
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)