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, float rho, int &iCategory) const
 
float mvaValue (const reco::Candidate *candidate, std::vector< float > const &auxVariables, int &iCategory) const override
 
 ~ElectronMVAEstimatorRun2 () override
 
- Public Member Functions inherited from AnyMVAEstimatorRun2Base
 AnyMVAEstimatorRun2Base (const ::std::string &mvaName, const ::std::string &mvaTag, int nCategories, bool debug)
 
 AnyMVAEstimatorRun2Base (const edm::ParameterSet &conf)
 
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< ThreadSafeStringCut< StringCutObjectSelector< reco::GsfElectron >, 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 16 of file ElectronMVAEstimatorRun2.h.

Constructor & Destructor Documentation

◆ ElectronMVAEstimatorRun2() [1/2]

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

Definition at line 3 of file ElectronMVAEstimatorRun2.cc.

4  : AnyMVAEstimatorRun2Base(conf), mvaVarMngr_(conf.getParameter<std::string>("variableDefinition")) {
5  const auto weightFileNames = conf.getParameter<std::vector<std::string> >("weightFileNames");
6  const auto categoryCutStrings = conf.getParameter<std::vector<std::string> >("categoryCuts");
7 
8  if ((int)(categoryCutStrings.size()) != getNCategories())
9  throw cms::Exception("MVA config failure: ")
10  << "wrong number of category cuts in ElectronMVAEstimatorRun2" << getTag() << std::endl;
11 
12  for (int i = 0; i < getNCategories(); ++i) {
13  categoryFunctions_.emplace_back(categoryCutStrings[i]);
14  }
15 
16  // Initialize GBRForests from weight files
18 }

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

◆ ~ElectronMVAEstimatorRun2()

ElectronMVAEstimatorRun2::~ElectronMVAEstimatorRun2 ( )
inlineoverride

Definition at line 20 of file ElectronMVAEstimatorRun2.h.

20 {};

◆ 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 20 of file ElectronMVAEstimatorRun2.cc.

28  if ((int)(categoryCutStrings.size()) != getNCategories())
29  throw cms::Exception("MVA config failure: ")
30  << "wrong number of category cuts in " << getName() << getTag() << std::endl;
31 
32  for (auto const& cut : categoryCutStrings)
33  categoryFunctions_.emplace_back(cut);
35 }

References categoryFunctions_, TkAlMuonSelectors_cfi::cut, AnyMVAEstimatorRun2Base::getName(), AnyMVAEstimatorRun2Base::getNCategories(), AnyMVAEstimatorRun2Base::getTag(), init(), and mvaElectronID_Fall17_iso_V1_cff::weightFileNames.

Member Function Documentation

◆ findCategory() [1/2]

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

Implements AnyMVAEstimatorRun2Base.

Definition at line 117 of file ElectronMVAEstimatorRun2.cc.

117  {
118  const reco::GsfElectron* electron = dynamic_cast<reco::GsfElectron const*>(candidate);
119 
120  if (electron == nullptr) {
121  throw cms::Exception("MVA failure: ")
122  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
123  << " but appears to be neither" << std::endl;
124  }
125 
126  return findCategory(*electron);
127 }

References HPSPFTauProducerPuppi_cfi::electron, and Exception.

Referenced by mvaValue().

◆ findCategory() [2/2]

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

Definition at line 129 of file ElectronMVAEstimatorRun2.cc.

129  {
130  for (int i = 0; i < getNCategories(); ++i) {
132  return i;
133  }
134 
135  edm::LogWarning("MVA warning") << "category not defined for particle with pt " << electron.pt() << " GeV, eta "
136  << electron.superCluster()->eta() << " in ElectronMVAEstimatorRun2" << getTag();
137 
138  return -1;
139 }

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

◆ init()

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

Definition at line 37 of file ElectronMVAEstimatorRun2.cc.

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

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.

Referenced by ElectronMVAEstimatorRun2().

◆ mvaValue() [1/2]

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

Definition at line 36 of file ElectronMVAEstimatorRun2.h.

36  {
37  return mvaValue(candidate, std::vector<float>{rho}, iCategory);
38  }

References mvaValue(), and rho.

◆ mvaValue() [2/2]

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

Implements AnyMVAEstimatorRun2Base.

Definition at line 79 of file ElectronMVAEstimatorRun2.cc.

81  {
82  const reco::GsfElectron* electron = dynamic_cast<const reco::GsfElectron*>(candidate);
83 
84  if (electron == 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(electron);
91 
92  if (iCategory < 0)
93  return -999;
94 
95  std::vector<float> vars;
96 
97  for (int i = 0; i < nVariables_[iCategory]; ++i) {
98  vars.push_back(mvaVarMngr_.getValue(variables_[iCategory][i], *electron, auxVariables));
99  }
100 
101  if (isDebug()) {
102  std::cout << " *** Inside ElectronMVAEstimatorRun2" << getTag() << std::endl;
103  std::cout << " category " << iCategory << std::endl;
104  for (int i = 0; i < nVariables_[iCategory]; ++i) {
105  std::cout << " " << mvaVarMngr_.getName(variables_[iCategory][i]) << " " << vars[i] << std::endl;
106  }
107  }
108  const float response = gbrForests_.at(iCategory)->GetResponse(vars.data()); // The BDT score
109 
110  if (isDebug()) {
111  std::cout << " ### MVA " << response << std::endl << std::endl;
112  }
113 
114  return response;
115 }

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().

Member Data Documentation

◆ categoryFunctions_

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

Definition at line 47 of file ElectronMVAEstimatorRun2.h.

Referenced by ElectronMVAEstimatorRun2(), and findCategory().

◆ gbrForests_

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

Definition at line 51 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

◆ mvaVarMngr_

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

Definition at line 57 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

◆ nVariables_

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

Definition at line 48 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

◆ variables_

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

Definition at line 55 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

AnyMVAEstimatorRun2Base::AnyMVAEstimatorRun2Base
AnyMVAEstimatorRun2Base(const edm::ParameterSet &conf)
Definition: AnyMVAEstimatorRun2Base.h:14
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:355
TkAlMuonSelectors_cfi.cut
cut
Definition: TkAlMuonSelectors_cfi.py:5
MVAVariableManager::getValue
float getValue(int index, const ParticleType &particle, const std::vector< float > &auxVariables) const
Definition: MVAVariableManager.h:51
gather_cfg.cout
cout
Definition: gather_cfg.py:144
createGBRForest
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)
Definition: GBRForestTools.cc:244
ElectronMVAEstimatorRun2::nVariables_
std::vector< int > nVariables_
Definition: ElectronMVAEstimatorRun2.h:48
MVAVariableManager::getName
const std::string & getName(int index) const
Definition: MVAVariableManager.h:47
ElectronMVAEstimatorRun2::mvaValue
float mvaValue(const reco::Candidate *candidate, std::vector< float > const &auxVariables, int &iCategory) const override
Definition: ElectronMVAEstimatorRun2.cc:79
debug
#define debug
Definition: HDRShower.cc:19
vars
vars
Definition: DeepTauId.cc:163
ElectronMVAEstimatorRun2::variables_
std::vector< std::vector< int > > variables_
Definition: ElectronMVAEstimatorRun2.h:55
AnyMVAEstimatorRun2Base::isDebug
bool isDebug() const
Definition: AnyMVAEstimatorRun2Base.h:46
reco::GsfElectron
Definition: GsfElectron.h:35
DDAxes::rho
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::LogWarning
Definition: MessageLogger.h:141
MVAVariableManager::getVarIndex
int getVarIndex(const std::string &name)
Definition: MVAVariableManager.h:38
ElectronMVAEstimatorRun2::gbrForests_
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
Definition: ElectronMVAEstimatorRun2.h:51
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:117
ElectronMVAEstimatorRun2::init
void init(const std::vector< std::string > &weightFileNames)
Definition: ElectronMVAEstimatorRun2.cc:37
HPSPFTauProducerPuppi_cfi.electron
electron
Definition: HPSPFTauProducerPuppi_cfi.py:13
mvaElectronID_Fall17_iso_V1_cff.nCategories
nCategories
Definition: mvaElectronID_Fall17_iso_V1_cff.py:86
ElectronMVAEstimatorRun2::mvaVarMngr_
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
Definition: ElectronMVAEstimatorRun2.h:57
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
mvaElectronID_Fall17_iso_V1_cff.weightFileNames
weightFileNames
Definition: mvaElectronID_Fall17_iso_V1_cff.py:89
Exception
Definition: hltDiff.cc:246
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
cms::Exception
Definition: Exception.h:70
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
ElectronMVAEstimatorRun2::categoryFunctions_
std::vector< ThreadSafeStringCut< StringCutObjectSelector< reco::GsfElectron >, reco::GsfElectron > > categoryFunctions_
Definition: ElectronMVAEstimatorRun2.h:47
candidateCombinedMVAV2Computer_cfi.mvaName
mvaName
Definition: candidateCombinedMVAV2Computer_cfi.py:12
AnyMVAEstimatorRun2Base::getName
const std::string & getName() const
Definition: AnyMVAEstimatorRun2Base.h:40