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, const bool debug=false)
 
int findCategory (const edm::Ptr< reco::Candidate > &candPtr) const override
 
void init (const std::vector< std::string > &weightFileNames)
 
float mvaValue (const edm::Ptr< reco::Candidate > &candPtr, const edm::EventBase &iEvent, int &iCategory) const override
 
void setConsumes (edm::ConsumesCollector &&) final
 
 ~ElectronMVAEstimatorRun2 () override
 
- Public Member Functions inherited from AnyMVAEstimatorRun2Base
 AnyMVAEstimatorRun2Base (const edm::ParameterSet &conf)
 
const std::string & getName () const
 
int getNCategories () const
 
const std::string & getTag () const
 
bool isDebug () const
 
float mvaValue (const edm::Ptr< reco::Candidate > &candPtr, const edm::EventBase &iEvent) const
 
virtual ~AnyMVAEstimatorRun2Base ()
 

Private Member Functions

int findCategory (const edm::Ptr< reco::GsfElectron > &gsfPtr) const
 

Private Attributes

std::vector< 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 9 of file ElectronMVAEstimatorRun2.h.

Constructor & Destructor Documentation

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

Definition at line 3 of file ElectronMVAEstimatorRun2.cc.

References categoryFunctions_, AnyMVAEstimatorRun2Base::getName(), AnyMVAEstimatorRun2Base::getNCategories(), edm::ParameterSet::getParameter(), AnyMVAEstimatorRun2Base::getTag(), mps_fire::i, init(), TauGenJetsDecayModeSelectorAllHadrons_cfi::select, and mvaElectronID_Fall17_iso_V1_cff::weightFileNames.

Referenced by ~ElectronMVAEstimatorRun2().

3  :
5  mvaVarMngr_(conf.getParameter<std::string>("variableDefinition"))
6 {
7 
8  const std::vector <std::string> weightFileNames
9  = conf.getParameter<std::vector<std::string> >("weightFileNames");
10 
11  const std::vector <std::string> categoryCutStrings
12  = conf.getParameter<std::vector<std::string> >("categoryCuts");
13 
14  if( (int)(categoryCutStrings.size()) != getNCategories() )
15  throw cms::Exception("MVA config failure: ")
16  << "wrong number of category cuts in " << getName() << getTag() << std::endl;
17 
18  for (int i = 0; i < getNCategories(); ++i) {
20  categoryFunctions_.push_back(select);
21  }
22 
23  // Initialize GBRForests from weight files
24  init(weightFileNames);
25 
26 }
T getParameter(std::string const &) const
AnyMVAEstimatorRun2Base(const edm::ParameterSet &conf)
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
const std::string & getName() const
const std::string & getTag() const
std::vector< StringCutObjectSelector< reco::GsfElectron > > categoryFunctions_
void init(const std::vector< std::string > &weightFileNames)
ElectronMVAEstimatorRun2::~ElectronMVAEstimatorRun2 ( )
inlineoverride
ElectronMVAEstimatorRun2::ElectronMVAEstimatorRun2 ( const std::string &  mvaTag,
const std::string &  mvaName,
const bool  debug = false 
)

Member Function Documentation

int ElectronMVAEstimatorRun2::findCategory ( const edm::Ptr< reco::Candidate > &  candPtr) const
overridevirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 116 of file ElectronMVAEstimatorRun2.cc.

References Exception, and nullptr.

Referenced by mvaValue(), and ~ElectronMVAEstimatorRun2().

116  {
117 
118  const edm::Ptr<reco::GsfElectron> gsfPtr{ candPtr };
119  if( gsfPtr.get() == nullptr ) {
120  throw cms::Exception("MVA failure: ")
121  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
122  << " but appears to be neither" << std::endl;
123  }
124 
125  return findCategory(gsfPtr);
126 
127 }
int findCategory(const edm::Ptr< reco::Candidate > &candPtr) const override
#define nullptr
int ElectronMVAEstimatorRun2::findCategory ( const edm::Ptr< reco::GsfElectron > &  gsfPtr) const
private

Definition at line 129 of file ElectronMVAEstimatorRun2.cc.

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

129  {
130 
131  for (int i = 0; i < getNCategories(); ++i) {
132  if (categoryFunctions_[i](*gsfPtr)) return i;
133  }
134 
135  edm::LogWarning ("MVA warning") <<
136  "category not defined for particle with pt " << gsfPtr->pt() << " GeV, eta " <<
137  gsfPtr->superCluster()->eta() << " in " << getName() << getTag();
138 
139  return -1;
140 
141 }
double pt() const final
transverse momentum
const std::string & getName() const
const std::string & getTag() const
std::vector< StringCutObjectSelector< reco::GsfElectron > > categoryFunctions_
SuperClusterRef superCluster() const override
reference to a SuperCluster
Definition: GsfElectron.h:184
void ElectronMVAEstimatorRun2::init ( const std::vector< std::string > &  weightFileNames)

Definition at line 28 of file ElectronMVAEstimatorRun2.cc.

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

Referenced by ElectronMVAEstimatorRun2(), and ~ElectronMVAEstimatorRun2().

28  {
29 
30  if(isDebug()) {
31  std::cout << " *** Inside " << getName() << getTag() << std::endl;
32  }
33 
34  // Initialize GBRForests
35  if( (int)(weightFileNames.size()) != getNCategories() )
36  throw cms::Exception("MVA config failure: ")
37  << "wrong number of weightfiles in " << getName() << getTag() << std::endl;
38 
39  gbrForests_.clear();
40  // Create a TMVA reader object for each category
41  for(int i=0; i<getNCategories(); i++){
42 
43  std::vector<int> variablesInCategory;
44 
45  // Use unique_ptr so that all readers are properly cleaned up
46  // when the vector clear() is called in the destructor
47 
48  std::vector<std::string> variableNamesInCategory;
49  gbrForests_.push_back(createGBRForest(weightFileNames[i], variableNamesInCategory));
50 
51  nVariables_.push_back(variableNamesInCategory.size());
52 
53  variables_.push_back(variablesInCategory);
54 
55  if(isDebug()) {
56  std::cout << " *** Inside " << getName() << getTag() << std::endl;
57  std::cout << " category " << i << " with nVariables " << nVariables_[i] << std::endl;
58  }
59 
60  for (int j=0; j<nVariables_[i];++j) {
61  int index = mvaVarMngr_.getVarIndex(variableNamesInCategory[j]);
62  if(index == -1) {
63  throw cms::Exception("MVA config failure: ")
64  << "Concerning " << getName() << getTag() << std::endl
65  << "Variable " << variableNamesInCategory[j]
66  << " not found in variable definition file!" << std::endl;
67  }
68  variables_[i].push_back(index);
69 
70  }
71  }
72 }
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
const std::string & getName() const
const std::string & getTag() const
std::vector< std::vector< int > > variables_
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
int getVarIndex(const std::string &name)
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)
float ElectronMVAEstimatorRun2::mvaValue ( const edm::Ptr< reco::Candidate > &  candPtr,
const edm::EventBase iEvent,
int &  iCategory 
) const
overridevirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 81 of file ElectronMVAEstimatorRun2.cc.

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

Referenced by setConsumes(), and ~ElectronMVAEstimatorRun2().

81  {
82 
83  const edm::Ptr<reco::GsfElectron> gsfPtr{ candPtr };
84  if( gsfPtr.get() == nullptr ) {
85  throw cms::Exception("MVA failure: ")
86  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
87  << " but appears to be neither" << std::endl;
88  }
89 
90  iCategory = findCategory( gsfPtr );
91 
92  if (iCategory < 0) return -999;
93 
94  std::vector<float> vars;
95 
96  for (int i = 0; i < nVariables_[iCategory]; ++i) {
97  vars.push_back(mvaVarMngr_.getValue(variables_[iCategory][i], gsfPtr, iEvent));
98  }
99 
100  if(isDebug()) {
101  std::cout << " *** Inside " << getName() << getTag() << std::endl;
102  std::cout << " category " << iCategory << std::endl;
103  for (int i = 0; i < nVariables_[iCategory]; ++i) {
104  std::cout << " " << mvaVarMngr_.getName(variables_[iCategory][i]) << " " << vars[i] << std::endl;
105  }
106  }
107  const float response = gbrForests_.at(iCategory)->GetResponse(vars.data()); // The BDT score
108 
109  if(isDebug()) {
110  std::cout << " ### MVA " << response << std::endl << std::endl;
111  }
112 
113  return response;
114 }
const std::string & getName(int index) const
int findCategory(const edm::Ptr< reco::Candidate > &candPtr) const override
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
#define nullptr
const std::string & getName() const
const std::string & getTag() const
std::vector< std::vector< int > > variables_
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
float getValue(int index, const edm::Ptr< ParticleType > &ptclPtr, const edm::EventBase &iEvent) const
void ElectronMVAEstimatorRun2::setConsumes ( edm::ConsumesCollector &&  cc)
finalvirtual

Reimplemented from AnyMVAEstimatorRun2Base.

Definition at line 74 of file ElectronMVAEstimatorRun2.cc.

References eostools::move(), mvaValue(), mvaVarMngr_, and MVAVariableManager< ParticleType >::setConsumes().

Referenced by ~ElectronMVAEstimatorRun2().

74  {
75  // All tokens for event content needed by this MVA
76  // Tags from the variable helper
78 }
void setConsumes(edm::ConsumesCollector &&cc)
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
def move(src, dest)
Definition: eostools.py:511

Member Data Documentation

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

Definition at line 36 of file ElectronMVAEstimatorRun2.h.

Referenced by ElectronMVAEstimatorRun2(), and findCategory().

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

Definition at line 40 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

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

Definition at line 47 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), mvaValue(), and setConsumes().

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

Definition at line 37 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

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

Definition at line 45 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().