CMS 3D CMS Logo

ElectronMVAEstimatorRun2.cc
Go to the documentation of this file.
4 
7  mvaVarMngr_(conf.getParameter<std::string>("variableDefinition"), MVAVariableHelper::indexMap()) {
8  const auto weightFileNames = conf.getParameter<std::vector<std::string> >("weightFileNames");
9  const auto categoryCutStrings = conf.getParameter<std::vector<std::string> >("categoryCuts");
10 
11  if ((int)(categoryCutStrings.size()) != getNCategories())
12  throw cms::Exception("MVA config failure: ")
13  << "wrong number of category cuts in ElectronMVAEstimatorRun2" << getTag() << std::endl;
14 
15  for (int i = 0; i < getNCategories(); ++i) {
16  categoryFunctions_.emplace_back(categoryCutStrings[i]);
17  }
18 
19  // Initialize GBRForests from weight files
21 }
22 
24  const std::string& mvaName,
25  int nCategories,
27  const std::vector<std::string>& categoryCutStrings,
28  const std::vector<std::string>& weightFileNames,
29  bool debug)
31  mvaVarMngr_(variableDefinition, MVAVariableHelper::indexMap()) {
32  if ((int)(categoryCutStrings.size()) != getNCategories())
33  throw cms::Exception("MVA config failure: ")
34  << "wrong number of category cuts in " << getName() << getTag() << std::endl;
35 
36  for (auto const& cut : categoryCutStrings)
37  categoryFunctions_.emplace_back(cut);
39 }
40 
41 void ElectronMVAEstimatorRun2::init(const std::vector<std::string>& weightFileNames) {
42  if (isDebug()) {
43  std::cout << " *** Inside ElectronMVAEstimatorRun2" << getTag() << std::endl;
44  }
45 
46  // Initialize GBRForests
47  if ((int)(weightFileNames.size()) != getNCategories())
48  throw cms::Exception("MVA config failure: ")
49  << "wrong number of weightfiles in ElectronMVAEstimatorRun2" << getTag() << std::endl;
50 
51  gbrForests_.clear();
52  // Create a TMVA reader object for each category
53  for (int i = 0; i < getNCategories(); i++) {
54  std::vector<int> variablesInCategory;
55 
56  // Use unique_ptr so that all readers are properly cleaned up
57  // when the vector clear() is called in the destructor
58 
59  std::vector<std::string> variableNamesInCategory;
60  gbrForests_.push_back(createGBRForest(weightFileNames[i], variableNamesInCategory));
61 
62  nVariables_.push_back(variableNamesInCategory.size());
63 
64  variables_.push_back(variablesInCategory);
65 
66  if (isDebug()) {
67  std::cout << " *** Inside ElectronMVAEstimatorRun2" << getTag() << std::endl;
68  std::cout << " category " << i << " with nVariables " << nVariables_[i] << std::endl;
69  }
70 
71  for (int j = 0; j < nVariables_[i]; ++j) {
72  int index = mvaVarMngr_.getVarIndex(variableNamesInCategory[j]);
73  if (index == -1) {
74  throw cms::Exception("MVA config failure: ")
75  << "Concerning ElectronMVAEstimatorRun2" << getTag() << std::endl
76  << "Variable " << variableNamesInCategory[j] << " not found in variable definition file!" << std::endl;
77  }
78  variables_[i].push_back(index);
79  }
80  }
81 }
82 
84  const std::vector<float>& auxVariables,
85  int& iCategory) const {
86  const reco::GsfElectron* electron = dynamic_cast<const reco::GsfElectron*>(candidate);
87 
88  if (electron == nullptr) {
89  throw cms::Exception("MVA failure: ")
90  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
91  << " but appears to be neither" << std::endl;
92  }
93 
94  iCategory = findCategory(electron);
95 
96  if (iCategory < 0)
97  return -999;
98 
99  std::vector<float> vars;
100 
101  vars.reserve(nVariables_[iCategory]);
102  for (int i = 0; i < nVariables_[iCategory]; ++i) {
103  vars.push_back(mvaVarMngr_.getValue(variables_[iCategory][i], *electron, auxVariables));
104  }
105 
106  if (isDebug()) {
107  std::cout << " *** Inside ElectronMVAEstimatorRun2" << getTag() << std::endl;
108  std::cout << " category " << iCategory << std::endl;
109  for (int i = 0; i < nVariables_[iCategory]; ++i) {
110  std::cout << " " << mvaVarMngr_.getName(variables_[iCategory][i]) << " " << vars[i] << std::endl;
111  }
112  }
113  const float response = gbrForests_.at(iCategory)->GetResponse(vars.data()); // The BDT score
114 
115  if (isDebug()) {
116  std::cout << " ### MVA " << response << std::endl << std::endl;
117  }
118 
119  return response;
120 }
121 
123  const reco::GsfElectron* electron = dynamic_cast<reco::GsfElectron const*>(candidate);
124 
125  if (electron == nullptr) {
126  throw cms::Exception("MVA failure: ")
127  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
128  << " but appears to be neither" << std::endl;
129  }
130 
131  return findCategory(*electron);
132 }
133 
135  for (int i = 0; i < getNCategories(); ++i) {
137  return i;
138  }
139 
140  edm::LogWarning("MVA warning") << "category not defined for particle with pt " << electron.pt() << " GeV, eta "
141  << electron.superCluster()->eta() << " in ElectronMVAEstimatorRun2" << getTag();
142 
143  return -1;
144 }
mvaElectronID_Fall17_iso_V1_cff.mvaTag
mvaTag
Definition: mvaElectronID_Fall17_iso_V1_cff.py:16
mvaElectronID_Fall17_iso_V1_cff.variableDefinition
variableDefinition
Definition: mvaElectronID_Fall17_iso_V1_cff.py:90
mps_fire.i
i
Definition: mps_fire.py:428
MessageLogger.h
MVAVariableManager::getValue
float getValue(int index, const ParticleType &particle, const std::vector< float > &auxVariables) const
Definition: MVAVariableManager.h:50
AnyMVAEstimatorRun2Base
Definition: AnyMVAEstimatorRun2Base.h:11
gather_cfg.cout
cout
Definition: gather_cfg.py:144
createGBRForest
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)
Definition: GBRForestTools.cc:257
ElectronMVAEstimatorRun2::nVariables_
std::vector< int > nVariables_
Definition: ElectronMVAEstimatorRun2.h:47
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
MVAVariableManager::getName
const std::string & getName(int index) const
Definition: MVAVariableManager.h:46
MVAVariableHelper.h
ElectronMVAEstimatorRun2::mvaValue
float mvaValue(const reco::Candidate *candidate, std::vector< float > const &auxVariables, int &iCategory) const override
Definition: ElectronMVAEstimatorRun2.cc:83
debug
#define debug
Definition: HDRShower.cc:19
vars
vars
Definition: DeepTauId.cc:164
taus_updatedMVAIds_cff.mvaName
mvaName
Definition: taus_updatedMVAIds_cff.py:18
ElectronMVAEstimatorRun2::variables_
std::vector< std::vector< int > > variables_
Definition: ElectronMVAEstimatorRun2.h:54
AnyMVAEstimatorRun2Base::isDebug
bool isDebug() const
Definition: AnyMVAEstimatorRun2Base.h:46
reco::GsfElectron
Definition: GsfElectron.h:35
ElectronMVAEstimatorRun2::categoryFunctions_
std::vector< ThreadSafeFunctor< StringCutObjectSelector< reco::GsfElectron > > > categoryFunctions_
Definition: ElectronMVAEstimatorRun2.h:46
MVAVariableManager::getVarIndex
int getVarIndex(const std::string &name)
Definition: MVAVariableManager.h:37
ElectronMVAEstimatorRun2::ElectronMVAEstimatorRun2
ElectronMVAEstimatorRun2(const edm::ParameterSet &conf)
Definition: ElectronMVAEstimatorRun2.cc:5
edm::ParameterSet
Definition: ParameterSet.h:47
ElectronMVAEstimatorRun2::gbrForests_
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
Definition: ElectronMVAEstimatorRun2.h:50
AnyMVAEstimatorRun2Base::getNCategories
int getNCategories() const
Definition: AnyMVAEstimatorRun2Base.h:39
AnyMVAEstimatorRun2Base::getTag
const std::string & getTag() const
Definition: AnyMVAEstimatorRun2Base.h:44
ElectronMVAEstimatorRun2::findCategory
int findCategory(const reco::Candidate *candidate) const override
Definition: ElectronMVAEstimatorRun2.cc:122
ElectronMVAEstimatorRun2::init
void init(const std::vector< std::string > &weightFileNames)
Definition: ElectronMVAEstimatorRun2.cc:41
HPSPFTauProducerPuppi_cfi.electron
electron
Definition: HPSPFTauProducerPuppi_cfi.py:13
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
mvaElectronID_Fall17_iso_V1_cff.nCategories
nCategories
Definition: mvaElectronID_Fall17_iso_V1_cff.py:86
reco::Candidate
Definition: Candidate.h:27
ElectronMVAEstimatorRun2::mvaVarMngr_
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
Definition: ElectronMVAEstimatorRun2.h:56
ElectronMVAEstimatorRun2.h
std
Definition: JetResolutionObject.h:76
mvaElectronID_Fall17_iso_V1_cff.weightFileNames
weightFileNames
Definition: mvaElectronID_Fall17_iso_V1_cff.py:89
PA_MinBiasSkim_cff.cut
cut
Definition: PA_MinBiasSkim_cff.py:13
Exception
Definition: hltDiff.cc:245
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
cms::Exception
Definition: Exception.h:70
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
MVAVariableHelper
Definition: MVAVariableHelper.h:12
AnyMVAEstimatorRun2Base::getName
const std::string & getName() const
Definition: AnyMVAEstimatorRun2Base.h:40