CMS 3D CMS Logo

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

Private Attributes

std::vector< StringCutObjectSelector< reco::GsfElectron > > categoryFunctions_
 
bool debug_
 
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
 
const std::string methodName_
 
MVAVariableManager< reco::GsfElectronmvaVarMngr_
 
const std::string name_
 
int nCategories_
 
std::vector< int > nVariables_
 
const std::string tag_
 
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_, Exception, edm::ParameterSet::getParameter(), mps_fire::i, init(), name_, nCategories_, TauGenJetsDecayModeSelectorAllHadrons_cfi::select, tag_, and mvaElectronID_Fall17_iso_V1_cff::weightFileNames.

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

Definition at line 89 of file ElectronMVAEstimatorRun2.cc.

Referenced by init().

89  {
90 }
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 140 of file ElectronMVAEstimatorRun2.cc.

References categoryFunctions_, Exception, edm::Ptr< T >::get(), mps_fire::i, name_, nCategories_, and tag_.

Referenced by getTag(), and mvaValue().

140  {
141 
142  auto gsfEle = dynamic_cast<reco::GsfElectron const*>(candPtr.get());
143 
144  if( gsfEle == nullptr ) {
145  throw cms::Exception("MVA failure: ")
146  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
147  << " but appears to be neither" << std::endl;
148  }
149 
150  for (int i = 0; i < nCategories_; ++i) {
151  if (categoryFunctions_[i](*gsfEle)) return i;
152  }
153 
154  edm::LogWarning ("MVA warning") <<
155  "category not defined for particle with pt " << gsfEle->pt() << " GeV, eta " <<
156  gsfEle->superCluster()->eta() << " in " << name_ << tag_;
157 
158  return -1;
159 
160 }
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:159
std::vector< StringCutObjectSelector< reco::GsfElectron > > categoryFunctions_
const std::string& ElectronMVAEstimatorRun2::getName ( void  ) const
inlinefinalvirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 28 of file ElectronMVAEstimatorRun2.h.

References name_.

Referenced by plotting.Plot::draw().

28 { return name_; }
int ElectronMVAEstimatorRun2::getNCategories ( ) const
inlineoverridevirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 27 of file ElectronMVAEstimatorRun2.h.

References nCategories_.

const std::string& ElectronMVAEstimatorRun2::getTag ( ) const
inlinefinalvirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 29 of file ElectronMVAEstimatorRun2.h.

References findCategory(), setConsumes(), and tag_.

29 { return tag_; }
void ElectronMVAEstimatorRun2::init ( const std::vector< std::string > &  weightFileNames)

Definition at line 42 of file ElectronMVAEstimatorRun2.cc.

References gather_cfg::cout, GBRForestTools::createGBRForest(), debug_, Exception, gbrForests_, MVAVariableManager< ParticleType >::getVarIndex(), mps_fire::i, mvaVarMngr_, name_, nCategories_, nVariables_, tag_, variables_, and ~ElectronMVAEstimatorRun2().

Referenced by ElectronMVAEstimatorRun2().

42  {
43 
44  if(debug_) {
45  std::cout << " *** Inside " << name_ << tag_ << std::endl;
46  }
47 
48  // Initialize GBRForests
49  if( (int)(weightFileNames.size()) != nCategories_ )
50  throw cms::Exception("MVA config failure: ")
51  << "wrong number of weightfiles in " << name_ << tag_ << std::endl;
52 
53  gbrForests_.clear();
54  // Create a TMVA reader object for each category
55  for(int i=0; i<nCategories_; i++){
56 
57  std::vector<std::string> variableNamesInCategory;
58  std::vector<int> variablesInCategory;
59 
60  // Use unique_ptr so that all readers are properly cleaned up
61  // when the vector clear() is called in the destructor
62 
63  gbrForests_.push_back( GBRForestTools::createGBRForest( weightFileNames[i], variableNamesInCategory ) );
64 
65  nVariables_.push_back(variableNamesInCategory.size());
66 
67  variables_.push_back(variablesInCategory);
68 
69  if(debug_) {
70  std::cout << " *** Inside " << name_ << tag_ << 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 " << name_ << tag_ << std::endl
79  << "Variable " << variableNamesInCategory[j]
80  << " not found in variable definition file!" << std::endl;
81  }
82  variables_[i].push_back(index);
83 
84  }
85  }
86 }
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
static std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightFile)
int getVarIndex(std::string &name)
std::vector< std::vector< int > > variables_
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
float ElectronMVAEstimatorRun2::mvaValue ( const edm::Ptr< reco::Candidate > &  candPtr,
const edm::EventBase iEvent 
) const
overridevirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 104 of file ElectronMVAEstimatorRun2.cc.

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

Referenced by setConsumes().

104  {
105 
106  const int iCategory = findCategory( candPtr );
107 
108  if (iCategory < 0) return -999;
109 
110  std::vector<float> vars;
111 
112  const edm::Ptr<reco::GsfElectron> gsfPtr{ candPtr };
113 
114  if( gsfPtr.get() == nullptr ) {
115  throw cms::Exception("MVA failure: ")
116  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
117  << " but appears to be neither" << std::endl;
118  }
119 
120  for (int i = 0; i < nVariables_[iCategory]; ++i) {
121  vars.push_back(mvaVarMngr_.getValue(variables_[iCategory][i], gsfPtr, iEvent));
122  }
123 
124  if(debug_) {
125  std::cout << " *** Inside " << name_ << tag_ << std::endl;
126  std::cout << " category " << iCategory << std::endl;
127  for (int i = 0; i < nVariables_[iCategory]; ++i) {
128  std::cout << " " << mvaVarMngr_.getName(variables_[iCategory][i]) << " " << vars[i] << std::endl;
129  }
130  }
131  const float response = gbrForests_.at(iCategory)->GetResponse(vars.data()); // The BDT score
132 
133  if(debug_) {
134  std::cout << " ### MVA " << response << std::endl << std::endl;
135  }
136 
137  return response;
138 }
const std::string getName(int index) const
int findCategory(const edm::Ptr< reco::Candidate > &candPtr) const override
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
#define nullptr
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
vars
Definition: DeepTauId.cc:77
void ElectronMVAEstimatorRun2::setConsumes ( edm::ConsumesCollector &&  cc) const
finalvirtual

Reimplemented from AnyMVAEstimatorRun2Base.

Definition at line 92 of file ElectronMVAEstimatorRun2.cc.

References MVAVariableManager< ParticleType >::getGlobalInputTags(), MVAVariableManager< ParticleType >::getHelperInputTags(), mvaValue(), mvaVarMngr_, and GlobalPosition_Frontier_DevDB_cff::tag.

Referenced by getTag().

92  {
93  // All tokens for event content needed by this MVA
94  // Tags from the variable helper
95  for (auto &tag : mvaVarMngr_.getHelperInputTags()) {
97  }
98  for (auto &tag : mvaVarMngr_.getGlobalInputTags()) {
99  cc.consumes<double>(tag);
100  }
101 }
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
std::vector< edm::InputTag > getHelperInputTags() const
std::vector< edm::InputTag > getGlobalInputTags() const

Member Data Documentation

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

Definition at line 51 of file ElectronMVAEstimatorRun2.h.

Referenced by ElectronMVAEstimatorRun2(), and findCategory().

bool ElectronMVAEstimatorRun2::debug_
private

Definition at line 66 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

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

Definition at line 55 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

const std::string ElectronMVAEstimatorRun2::methodName_
private

Definition at line 57 of file ElectronMVAEstimatorRun2.h.

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

Definition at line 64 of file ElectronMVAEstimatorRun2.h.

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

const std::string ElectronMVAEstimatorRun2::name_
private
int ElectronMVAEstimatorRun2::nCategories_
private
std::vector<int> ElectronMVAEstimatorRun2::nVariables_
private

Definition at line 52 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

const std::string ElectronMVAEstimatorRun2::tag_
private
std::vector<std::vector<int> > ElectronMVAEstimatorRun2::variables_
private

Definition at line 62 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().