CMS 3D CMS Logo

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