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< 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 15 of file ElectronMVAEstimatorRun2.h.

Constructor & Destructor Documentation

◆ ElectronMVAEstimatorRun2() [1/2]

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

Definition at line 5 of file ElectronMVAEstimatorRun2.cc.

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
21 }

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 19 of file ElectronMVAEstimatorRun2.h.

19 {};

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

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);
39 }

References categoryFunctions_, PA_MinBiasSkim_cff::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 122 of file ElectronMVAEstimatorRun2.cc.

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 }

References HPSPFTauProducerPuppi_cfi::electron, and Exception.

Referenced by mvaValue().

◆ findCategory() [2/2]

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

Definition at line 134 of file ElectronMVAEstimatorRun2.cc.

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 }

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

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 }

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 35 of file ElectronMVAEstimatorRun2.h.

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

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

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 }

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<ThreadSafeFunctor<StringCutObjectSelector<reco::GsfElectron> > > ElectronMVAEstimatorRun2::categoryFunctions_
private

Definition at line 46 of file ElectronMVAEstimatorRun2.h.

Referenced by ElectronMVAEstimatorRun2(), and findCategory().

◆ gbrForests_

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

Definition at line 50 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

◆ mvaVarMngr_

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

Definition at line 56 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

◆ nVariables_

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

Definition at line 47 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

◆ variables_

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

Definition at line 54 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:428
MVAVariableManager::getValue
float getValue(int index, const ParticleType &particle, const std::vector< float > &auxVariables) const
Definition: MVAVariableManager.h:50
gather_cfg.cout
cout
Definition: gather_cfg.py:144
createGBRForest
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)
Definition: GBRForestTools.cc:257
ElectronMVAEstimatorRun2::nVariables_
std::vector< int > nVariables_
Definition: ElectronMVAEstimatorRun2.h:47
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
MVAVariableManager::getName
const std::string & getName(int index) const
Definition: MVAVariableManager.h:46
ElectronMVAEstimatorRun2::mvaValue
float mvaValue(const reco::Candidate *candidate, std::vector< float > const &auxVariables, int &iCategory) const override
Definition: ElectronMVAEstimatorRun2.cc:83
debug
#define debug
Definition: HDRShower.cc:19
vars
vars
Definition: DeepTauId.cc:164
taus_updatedMVAIds_cff.mvaName
mvaName
Definition: taus_updatedMVAIds_cff.py:18
ElectronMVAEstimatorRun2::variables_
std::vector< std::vector< int > > variables_
Definition: ElectronMVAEstimatorRun2.h:54
AnyMVAEstimatorRun2Base::isDebug
bool isDebug() const
Definition: AnyMVAEstimatorRun2Base.h:46
reco::GsfElectron
Definition: GsfElectron.h:35
ElectronMVAEstimatorRun2::categoryFunctions_
std::vector< ThreadSafeFunctor< StringCutObjectSelector< reco::GsfElectron > > > categoryFunctions_
Definition: ElectronMVAEstimatorRun2.h:46
DDAxes::rho
MVAVariableManager::getVarIndex
int getVarIndex(const std::string &name)
Definition: MVAVariableManager.h:37
MVAVariableHelper::indexMap
static std::unordered_map< std::string, int > indexMap()
Definition: MVAVariableHelper.h:22
ElectronMVAEstimatorRun2::gbrForests_
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
Definition: ElectronMVAEstimatorRun2.h:50
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:122
ElectronMVAEstimatorRun2::init
void init(const std::vector< std::string > &weightFileNames)
Definition: ElectronMVAEstimatorRun2.cc:41
HPSPFTauProducerPuppi_cfi.electron
electron
Definition: HPSPFTauProducerPuppi_cfi.py:13
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
mvaElectronID_Fall17_iso_V1_cff.nCategories
nCategories
Definition: mvaElectronID_Fall17_iso_V1_cff.py:86
ElectronMVAEstimatorRun2::mvaVarMngr_
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
Definition: ElectronMVAEstimatorRun2.h:56
mvaElectronID_Fall17_iso_V1_cff.weightFileNames
weightFileNames
Definition: mvaElectronID_Fall17_iso_V1_cff.py:89
PA_MinBiasSkim_cff.cut
cut
Definition: PA_MinBiasSkim_cff.py:13
Exception
Definition: hltDiff.cc:245
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
cms::Exception
Definition: Exception.h:70
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
AnyMVAEstimatorRun2Base::getName
const std::string & getName() const
Definition: AnyMVAEstimatorRun2Base.h:40