CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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
 
 ~ElectronMVAEstimatorRun2 () override
 
- 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::GsfElectron
mvaVarMngr_
 
std::vector< int > nVariables_
 
std::vector< std::vector< int > > variables_
 

Detailed Description

Definition at line 15 of file ElectronMVAEstimatorRun2.h.

Constructor & Destructor Documentation

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

Definition at line 5 of file ElectronMVAEstimatorRun2.cc.

References categoryFunctions_, AnyMVAEstimatorRun2Base::getNCategories(), edm::ParameterSet::getParameter(), AnyMVAEstimatorRun2Base::getTag(), mps_fire::i, and init().

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
20  init(weightFileNames);
21 }
AnyMVAEstimatorRun2Base(const edm::ParameterSet &conf)
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
const std::string & getTag() const
std::vector< ThreadSafeFunctor< StringCutObjectSelector< reco::GsfElectron > > > categoryFunctions_
void init(const std::vector< std::string > &weightFileNames)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
ElectronMVAEstimatorRun2::~ElectronMVAEstimatorRun2 ( )
inlineoverride

Definition at line 19 of file ElectronMVAEstimatorRun2.h.

19 {};
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 23 of file ElectronMVAEstimatorRun2.cc.

References categoryFunctions_, GOODCOLL_filter_cfg::cut, AnyMVAEstimatorRun2Base::getName(), AnyMVAEstimatorRun2Base::getNCategories(), AnyMVAEstimatorRun2Base::getTag(), and init().

30  : AnyMVAEstimatorRun2Base(mvaName, mvaTag, nCategories, 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);
38  init(weightFileNames);
39 }
AnyMVAEstimatorRun2Base(const edm::ParameterSet &conf)
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
const std::string & getName() const
const std::string & getTag() const
std::vector< ThreadSafeFunctor< StringCutObjectSelector< reco::GsfElectron > > > categoryFunctions_
void init(const std::vector< std::string > &weightFileNames)
#define debug
Definition: HDRShower.cc:19

Member Function Documentation

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

Implements AnyMVAEstimatorRun2Base.

Definition at line 122 of file ElectronMVAEstimatorRun2.cc.

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

Referenced by mvaValue().

122  {
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 }
int findCategory(const reco::Candidate *candidate) const override
int ElectronMVAEstimatorRun2::findCategory ( reco::GsfElectron const &  electron) const
private

Definition at line 134 of file ElectronMVAEstimatorRun2.cc.

References categoryFunctions_, AnyMVAEstimatorRun2Base::getNCategories(), AnyMVAEstimatorRun2Base::getTag(), mps_fire::i, reco::LeafCandidate::pt(), and reco::GsfElectron::superCluster().

134  {
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 }
const std::string & getTag() const
std::vector< ThreadSafeFunctor< StringCutObjectSelector< reco::GsfElectron > > > categoryFunctions_
Log< level::Warning, false > LogWarning
void ElectronMVAEstimatorRun2::init ( const std::vector< std::string > &  weightFileNames)
private

Definition at line 41 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_, and variables_.

Referenced by ElectronMVAEstimatorRun2().

41  {
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 }
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
const std::string & getTag() const
std::vector< std::vector< int > > variables_
int getVarIndex(const std::string &name)
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
tuple cout
Definition: gather_cfg.py:144
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)
float ElectronMVAEstimatorRun2::mvaValue ( const reco::Candidate candidate,
std::vector< float > const &  auxVariables,
int &  iCategory 
) const
overridevirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 83 of file ElectronMVAEstimatorRun2.cc.

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

Referenced by mvaValue().

85  {
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 }
const std::string & getName(int index) const
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
float getValue(int index, const ParticleType &particle, const std::vector< float > &auxVariables) const
const std::string & getTag() const
std::vector< std::vector< int > > variables_
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
tuple cout
Definition: gather_cfg.py:144
int findCategory(const reco::Candidate *candidate) const override
vars
Definition: DeepTauId.cc:164
float ElectronMVAEstimatorRun2::mvaValue ( const reco::Candidate candidate,
float  rho,
int &  iCategory 
) const
inline

Definition at line 35 of file ElectronMVAEstimatorRun2.h.

References mvaValue().

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

Member Data Documentation

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

Definition at line 46 of file ElectronMVAEstimatorRun2.h.

Referenced by ElectronMVAEstimatorRun2(), and findCategory().

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

Definition at line 50 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

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

Definition at line 56 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

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

Definition at line 47 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

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

Definition at line 54 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().