CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
ElectronMVAEstimatorRun2 Class Reference

#include <ElectronMVAEstimatorRun2.h>

Inheritance diagram for ElectronMVAEstimatorRun2:
AnyMVAEstimatorRun2Base

Public Member Functions

 ElectronMVAEstimatorRun2 (const edm::ParameterSet &conf)
 
 ElectronMVAEstimatorRun2 (const std::string &mvaTag, const std::string &mvaName, int nCategories, const std::string &variableDefinition, const std::vector< std::string > &categoryCutStrings, const std::vector< std::string > &weightFileNames, bool debug=false)
 
int findCategory (const reco::Candidate *candidate) const override
 
float mvaValue (const reco::Candidate *candidate, std::vector< float > const &auxVariables, int &iCategory) const override
 
float mvaValue (const reco::Candidate *candidate, float rho, int &iCategory) const
 
- Public Member Functions inherited from AnyMVAEstimatorRun2Base
 AnyMVAEstimatorRun2Base (const edm::ParameterSet &conf)
 
 AnyMVAEstimatorRun2Base (const ::std::string &mvaName, const ::std::string &mvaTag, int nCategories, bool debug)
 
const std::string & getName () const
 
int getNCategories () const
 
const std::string & getTag () const
 
bool isDebug () const
 
float mvaValue (const reco::Candidate *candidate, std::vector< float > const &auxVariables) const
 
virtual ~AnyMVAEstimatorRun2Base ()
 

Private Member Functions

int findCategory (reco::GsfElectron const &electron) const
 
void init (const std::vector< std::string > &weightFileNames)
 

Private Attributes

std::vector< ThreadSafeFunctor< StringCutObjectSelector< reco::GsfElectron > > > categoryFunctions_
 
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
 
MVAVariableManager< reco::GsfElectronmvaVarMngr_
 
std::vector< int > nVariables_
 
std::vector< std::vector< int > > variables_
 

Detailed Description

Definition at line 32 of file ElectronMVAEstimatorRun2.h.

Constructor & Destructor Documentation

◆ ElectronMVAEstimatorRun2() [1/2]

ElectronMVAEstimatorRun2::ElectronMVAEstimatorRun2 ( const edm::ParameterSet conf)

Definition at line 9 of file ElectronMVAEstimatorRun2.cc.

References edm::ParameterSet::getParameter(), MVAVariableHelper::indexMap(), and AlCaHLTBitMon_QueryRunRegistry::string.

11  mvaVarMngr_{conf.getParameter<std::string>("variableDefinition"), MVAVariableHelper::indexMap()} {
12  const auto weightFileNames = conf.getParameter<std::vector<std::string> >("weightFileNames");
13  const auto categoryCutStrings = conf.getParameter<std::vector<std::string> >("categoryCuts");
14 
15  if ((int)(categoryCutStrings.size()) != getNCategories())
16  throw cms::Exception("MVA config failure: ")
17  << "wrong number of category cuts in ElectronMVAEstimatorRun2" << getTag() << std::endl;
18 
19  for (int i = 0; i < getNCategories(); ++i) {
20  categoryFunctions_.emplace_back(categoryCutStrings[i]);
21  }
22 
23  // Initialize GBRForests from weight files
25 }
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
AnyMVAEstimatorRun2Base(const edm::ParameterSet &conf)
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
static std::unordered_map< std::string, int > indexMap()
std::vector< ThreadSafeFunctor< StringCutObjectSelector< reco::GsfElectron > > > categoryFunctions_
void init(const std::vector< std::string > &weightFileNames)
const std::string & getTag() const

◆ ElectronMVAEstimatorRun2() [2/2]

ElectronMVAEstimatorRun2::ElectronMVAEstimatorRun2 ( const std::string &  mvaTag,
const std::string &  mvaName,
int  nCategories,
const std::string &  variableDefinition,
const std::vector< std::string > &  categoryCutStrings,
const std::vector< std::string > &  weightFileNames,
bool  debug = false 
)

Definition at line 27 of file ElectronMVAEstimatorRun2.cc.

References MVAVariableHelper::indexMap(), and mvaElectronID_Fall17_iso_V1_cff::variableDefinition.

36  if ((int)(categoryCutStrings.size()) != getNCategories())
37  throw cms::Exception("MVA config failure: ")
38  << "wrong number of category cuts in " << getName() << getTag() << std::endl;
39 
40  for (auto const& cut : categoryCutStrings)
41  categoryFunctions_.emplace_back(cut);
43 }
AnyMVAEstimatorRun2Base(const edm::ParameterSet &conf)
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
static std::unordered_map< std::string, int > indexMap()
std::vector< ThreadSafeFunctor< StringCutObjectSelector< reco::GsfElectron > > > categoryFunctions_
void init(const std::vector< std::string > &weightFileNames)
const std::string & getTag() const
#define debug
Definition: HDRShower.cc:19
const std::string & getName() const

Member Function Documentation

◆ findCategory() [1/2]

int ElectronMVAEstimatorRun2::findCategory ( const reco::Candidate candidate) const
overridevirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 125 of file ElectronMVAEstimatorRun2.cc.

References runTheMatrix::const, HPSPFTauProducerPuppi_cfi::electron, and Exception.

Referenced by mvaValue().

125  {
126  const reco::GsfElectron* electron = dynamic_cast<reco::GsfElectron const*>(candidate);
127 
128  if (electron == nullptr) {
129  throw cms::Exception("MVA failure: ")
130  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
131  << " but appears to be neither" << std::endl;
132  }
133 
134  return findCategory(*electron);
135 }
int findCategory(const reco::Candidate *candidate) const override

◆ findCategory() [2/2]

int ElectronMVAEstimatorRun2::findCategory ( reco::GsfElectron const &  electron) const
private

Definition at line 137 of file ElectronMVAEstimatorRun2.cc.

References categoryFunctions_, HPSPFTauProducerPuppi_cfi::electron, AnyMVAEstimatorRun2Base::getNCategories(), AnyMVAEstimatorRun2Base::getTag(), and mps_fire::i.

137  {
138  for (int i = 0; i < getNCategories(); ++i) {
140  return i;
141  }
142 
143  edm::LogWarning("MVA warning") << "category not defined for particle with pt " << electron.pt() << " GeV, eta "
144  << electron.superCluster()->eta() << " in ElectronMVAEstimatorRun2" << getTag();
145 
146  return -1;
147 }
std::vector< ThreadSafeFunctor< StringCutObjectSelector< reco::GsfElectron > > > categoryFunctions_
const std::string & getTag() const
Log< level::Warning, false > LogWarning

◆ init()

void ElectronMVAEstimatorRun2::init ( const std::vector< std::string > &  weightFileNames)
private

Definition at line 45 of file ElectronMVAEstimatorRun2.cc.

References gather_cfg::cout, createGBRForest(), Exception, gbrForests_, AnyMVAEstimatorRun2Base::getNCategories(), AnyMVAEstimatorRun2Base::getTag(), MVAVariableManager< ParticleType >::getVarIndex(), mps_fire::i, AnyMVAEstimatorRun2Base::isDebug(), dqmiolumiharvest::j, mvaVarMngr_, nVariables_, variables_, and mvaElectronID_Fall17_iso_V1_cff::weightFileNames.

45  {
46  if (isDebug()) {
47  std::cout << " *** Inside ElectronMVAEstimatorRun2" << getTag() << std::endl;
48  }
49 
50  // Initialize GBRForests
51  if ((int)(weightFileNames.size()) != getNCategories())
52  throw cms::Exception("MVA config failure: ")
53  << "wrong number of weightfiles in ElectronMVAEstimatorRun2" << getTag() << std::endl;
54 
55  // Create a TMVA reader object for each category
56  for (int i = 0; i < getNCategories(); i++) {
57  std::vector<int> variablesInCategory;
58 
59  // Use unique_ptr so that all readers are properly cleaned up
60  // when the vector clear() is called in the destructor
61 
62  std::vector<std::string> variableNamesInCategory;
63  gbrForests_.push_back(createGBRForest(weightFileNames[i], variableNamesInCategory));
64 
65  nVariables_.push_back(variableNamesInCategory.size());
66 
67  variables_.push_back(variablesInCategory);
68 
69  if (isDebug()) {
70  std::cout << " *** Inside ElectronMVAEstimatorRun2" << getTag() << std::endl;
71  std::cout << " category " << i << " with nVariables " << nVariables_[i] << std::endl;
72  }
73 
74  for (int j = 0; j < nVariables_[i]; ++j) {
75  int index = mvaVarMngr_.getVarIndex(variableNamesInCategory[j]);
76  if (index == -1) {
77  throw cms::Exception("MVA config failure: ")
78  << "Concerning ElectronMVAEstimatorRun2" << getTag() << std::endl
79  << "Variable " << variableNamesInCategory[j] << " not found in variable definition file!" << std::endl;
80  }
81  variables_[i].push_back(index);
82  }
83  }
84 }
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
std::vector< std::vector< int > > variables_
const std::string & getTag() const
int getVarIndex(const std::string &name)
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)

◆ mvaValue() [1/2]

float ElectronMVAEstimatorRun2::mvaValue ( const reco::Candidate candidate,
std::vector< float > const &  auxVariables,
int &  iCategory 
) const
overridevirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 86 of file ElectronMVAEstimatorRun2.cc.

References gather_cfg::cout, HPSPFTauProducerPuppi_cfi::electron, Exception, findCategory(), gbrForests_, MVAVariableManager< ParticleType >::getName(), AnyMVAEstimatorRun2Base::getTag(), MVAVariableManager< ParticleType >::getValue(), mps_fire::i, AnyMVAEstimatorRun2Base::isDebug(), mvaVarMngr_, nVariables_, and variables_.

Referenced by mvaValue().

88  {
89  const reco::GsfElectron* electron = dynamic_cast<const reco::GsfElectron*>(candidate);
90 
91  if (electron == nullptr) {
92  throw cms::Exception("MVA failure: ")
93  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
94  << " but appears to be neither" << std::endl;
95  }
96 
97  iCategory = findCategory(electron);
98 
99  if (iCategory < 0)
100  return -999;
101 
102  std::vector<float> vars;
103 
104  vars.reserve(nVariables_[iCategory]);
105  for (int i = 0; i < nVariables_[iCategory]; ++i) {
106  vars.push_back(mvaVarMngr_.getValue(variables_[iCategory][i], *electron, auxVariables));
107  }
108 
109  if (isDebug()) {
110  std::cout << " *** Inside ElectronMVAEstimatorRun2" << getTag() << std::endl;
111  std::cout << " category " << iCategory << std::endl;
112  for (int i = 0; i < nVariables_[iCategory]; ++i) {
113  std::cout << " " << mvaVarMngr_.getName(variables_[iCategory][i]) << " " << vars[i] << std::endl;
114  }
115  }
116  const float response = gbrForests_.at(iCategory)->GetResponse(vars.data()); // The BDT score
117 
118  if (isDebug()) {
119  std::cout << " ### MVA " << response << std::endl << std::endl;
120  }
121 
122  return response;
123 }
vars
Definition: DeepTauIdBase.h:60
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
const std::string & getName(int index) const
std::vector< std::vector< int > > variables_
const std::string & getTag() const
float getValue(int index, const ParticleType &particle, const std::vector< float > &auxVariables) const
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
int findCategory(const reco::Candidate *candidate) const override

◆ mvaValue() [2/2]

float ElectronMVAEstimatorRun2::mvaValue ( const reco::Candidate candidate,
float  rho,
int &  iCategory 
) const
inline

Definition at line 51 of file ElectronMVAEstimatorRun2.h.

References mvaValue(), and rho.

51  {
52  return mvaValue(candidate, std::vector<float>{rho}, iCategory);
53  }
float mvaValue(const reco::Candidate *candidate, std::vector< float > const &auxVariables, int &iCategory) const override

Member Data Documentation

◆ categoryFunctions_

std::vector<ThreadSafeFunctor<StringCutObjectSelector<reco::GsfElectron> > > ElectronMVAEstimatorRun2::categoryFunctions_
private

Definition at line 62 of file ElectronMVAEstimatorRun2.h.

Referenced by findCategory().

◆ gbrForests_

std::vector<std::unique_ptr<const GBRForest> > ElectronMVAEstimatorRun2::gbrForests_
private

Definition at line 66 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

◆ mvaVarMngr_

MVAVariableManager<reco::GsfElectron> ElectronMVAEstimatorRun2::mvaVarMngr_
private

Definition at line 72 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

◆ nVariables_

std::vector<int> ElectronMVAEstimatorRun2::nVariables_
private

Definition at line 63 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

◆ variables_

std::vector<std::vector<int> > ElectronMVAEstimatorRun2::variables_
private

Definition at line 70 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().