CMS 3D CMS Logo

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