CMS 3D CMS Logo

List of all members | Public Member Functions | Static 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
 
 ~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 ()
 

Static Public Member Functions

static std::vector< float > getExtraVars (reco::GsfElectron const &ele, reco::ConversionCollection const *conversions, reco::BeamSpot const *beamSpot, double rho)
 

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::ElectronMVAEstimatorRun2 ( const edm::ParameterSet conf)

Definition at line 3 of file ElectronMVAEstimatorRun2.cc.

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

Referenced by ~ElectronMVAEstimatorRun2().

3  :
5  mvaVarMngr_(conf.getParameter<std::string>("variableDefinition"))
6 {
7 
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 }
T getParameter(std::string const &) const
AnyMVAEstimatorRun2Base(const edm::ParameterSet &conf)
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
const std::string & getTag() const
std::vector< ThreadSafeStringCut< StringCutObjectSelector< reco::GsfElectron >, reco::GsfElectron > > categoryFunctions_
void init(const std::vector< std::string > &weightFileNames)
ElectronMVAEstimatorRun2::~ElectronMVAEstimatorRun2 ( )
inlineoverride
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_, TkAlMuonSelectors_cfi::cut, AnyMVAEstimatorRun2Base::getName(), AnyMVAEstimatorRun2Base::getNCategories(), AnyMVAEstimatorRun2Base::getTag(), and init().

32 {
33 
34  if( (int)(categoryCutStrings.size()) != getNCategories() )
35  throw cms::Exception("MVA config failure: ")
36  << "wrong number of category cuts in " << getName() << getTag() << std::endl;
37 
38  for (auto const& cut : categoryCutStrings) categoryFunctions_.emplace_back(cut);
40 }
AnyMVAEstimatorRun2Base(const edm::ParameterSet &conf)
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
const std::string & getName() const
const std::string & getTag() const
std::vector< ThreadSafeStringCut< StringCutObjectSelector< reco::GsfElectron >, 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 125 of file ElectronMVAEstimatorRun2.cc.

References metsig::electron, and Exception.

Referenced by getExtraVars(), and mvaValue().

125  {
126 
127  const reco::GsfElectron* electron = dynamic_cast<reco::GsfElectron const*>(candidate);
128 
129  if( electron == nullptr ) {
130  throw cms::Exception("MVA failure: ")
131  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
132  << " but appears to be neither" << std::endl;
133  }
134 
135  return findCategory(*electron);
136 
137 }
int findCategory(const reco::Candidate *candidate) const override
int ElectronMVAEstimatorRun2::findCategory ( reco::GsfElectron const &  electron) const
private

Definition at line 139 of file ElectronMVAEstimatorRun2.cc.

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

139  {
140 
141  for (int i = 0; i < getNCategories(); ++i) {
142  if (categoryFunctions_[i](electron)) return i;
143  }
144 
145  edm::LogWarning ("MVA warning") <<
146  "category not defined for particle with pt " << electron.pt() << " GeV, eta " <<
147  electron.superCluster()->eta() << " in ElectronMVAEstimatorRun2" << getTag();
148 
149  return -1;
150 
151 }
const std::string & getTag() const
std::vector< ThreadSafeStringCut< StringCutObjectSelector< reco::GsfElectron >, reco::GsfElectron > > categoryFunctions_
static std::vector<float> ElectronMVAEstimatorRun2::getExtraVars ( reco::GsfElectron const &  ele,
reco::ConversionCollection const *  conversions,
reco::BeamSpot const *  beamSpot,
double  rho 
)
inlinestatic

Definition at line 33 of file ElectronMVAEstimatorRun2.h.

References reco::Vertex::chi2(), reco::GsfElectron::closestCtfTrackRef(), conv, reco::Conversion::conversionVertex(), metsig::electron, findCategory(), init(), edm::Ref< C, T, F >::isAvailable(), edm::Ref< C, T, F >::isNonnull(), reco::Vertex::isValid(), ConversionTools::matchedConversion(), mvaValue(), reco::Vertex::ndof(), reco::BeamSpot::position(), rho, and badGlobalMuonTaggersAOD_cff::vtx.

Referenced by MVAVariableHelper< reco::GsfElectron >::getAuxVariables().

37  {
38  // Conversion vertex fit
40 
41  float convVtxFitProb = -1.;
42  if(!(conv == nullptr)) {
43  const reco::Vertex &vtx = conv->conversionVertex();
44  if (vtx.isValid()) {
45  convVtxFitProb = TMath::Prob( vtx.chi2(), vtx.ndof());
46  }
47  }
48 
49  // kf track related variables
50  bool validKf=false;
51  reco::TrackRef trackRef = ele.closestCtfTrackRef();
52  validKf = trackRef.isAvailable();
53  validKf &= trackRef.isNonnull();
54  float kfchi2 = validKf ? trackRef->normalizedChi2() : 0 ; //ielectron->track()->normalizedChi2() : 0 ;
55  float kfhits = validKf ? trackRef->hitPattern().trackerLayersWithMeasurement() : -1. ;
56 
57  return std::vector<float>{kfhits, kfchi2, convVtxFitProb, static_cast<float>(rho)};
58  }
const reco::Vertex & conversionVertex() const
returns the reco conversion vertex
Definition: Conversion.h:97
bool isAvailable() const
Definition: Ref.h:575
static reco::Conversion const * matchedConversion(const reco::GsfElectron &ele, const reco::ConversionCollection &convCol, const math::XYZPoint &beamspot, bool allowCkfMatch=true, float lxyMin=2.0, float probMin=1e-6, unsigned int nHitsBeforeVtxMax=0)
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
static HepMC::IO_HEPEVT conv
bool isValid() const
Tells whether the vertex is valid.
Definition: Vertex.h:68
double chi2() const
chi-squares
Definition: Vertex.h:98
double ndof() const
Definition: Vertex.h:105
void ElectronMVAEstimatorRun2::init ( const std::vector< std::string > &  weightFileNames)
private

Definition at line 42 of file ElectronMVAEstimatorRun2.cc.

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

Referenced by ElectronMVAEstimatorRun2(), and getExtraVars().

42  {
43 
44  if(isDebug()) {
45  std::cout << " *** Inside ElectronMVAEstimatorRun2" << getTag() << std::endl;
46  }
47 
48  // Initialize GBRForests
49  if( (int)(weightFileNames.size()) != getNCategories() )
50  throw cms::Exception("MVA config failure: ")
51  << "wrong number of weightfiles in ElectronMVAEstimatorRun2" << getTag() << std::endl;
52 
53  gbrForests_.clear();
54  // Create a TMVA reader object for each category
55  for(int i=0; i<getNCategories(); i++){
56 
57  std::vector<int> variablesInCategory;
58 
59  // Use unique_ptr so that all readers are properly cleaned up
60  // when the vector clear() is called in the destructor
61 
62  std::vector<std::string> variableNamesInCategory;
63  gbrForests_.push_back(createGBRForest(weightFileNames[i], variableNamesInCategory));
64 
65  nVariables_.push_back(variableNamesInCategory.size());
66 
67  variables_.push_back(variablesInCategory);
68 
69  if(isDebug()) {
70  std::cout << " *** Inside ElectronMVAEstimatorRun2" << getTag() << 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 ElectronMVAEstimatorRun2" << getTag() << 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_
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 reco::Candidate candidate,
std::vector< float > const &  auxVariables,
int &  iCategory 
) const
overridevirtual

Implements AnyMVAEstimatorRun2Base.

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

Referenced by getExtraVars(), and init().

89  {
90 
91  const reco::GsfElectron* electron = dynamic_cast<const reco::GsfElectron*>(candidate);
92 
93  if( electron == nullptr ) {
94  throw cms::Exception("MVA failure: ")
95  << " given particle is expected to be reco::GsfElectron or pat::Electron," << std::endl
96  << " but appears to be neither" << std::endl;
97  }
98 
99  iCategory = findCategory( electron );
100 
101  if (iCategory < 0) return -999;
102 
103  std::vector<float> vars;
104 
105  for (int i = 0; i < nVariables_[iCategory]; ++i) {
106  vars.push_back(mvaVarMngr_.getValue(variables_[iCategory][i], *electron, auxVariables));
107  }
108 
109  if(isDebug()) {
110  std::cout << " *** Inside ElectronMVAEstimatorRun2" << getTag() << std::endl;
111  std::cout << " category " << iCategory << std::endl;
112  for (int i = 0; i < nVariables_[iCategory]; ++i) {
113  std::cout << " " << mvaVarMngr_.getName(variables_[iCategory][i]) << " " << vars[i] << std::endl;
114  }
115  }
116  const float response = gbrForests_.at(iCategory)->GetResponse(vars.data()); // The BDT score
117 
118  if(isDebug()) {
119  std::cout << " ### MVA " << response << std::endl << std::endl;
120  }
121 
122  return response;
123 }
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_
int findCategory(const reco::Candidate *candidate) const override

Member Data Documentation

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

Definition at line 71 of file ElectronMVAEstimatorRun2.h.

Referenced by ElectronMVAEstimatorRun2(), and findCategory().

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

Definition at line 75 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

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

Definition at line 82 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

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

Definition at line 72 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().

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

Definition at line 80 of file ElectronMVAEstimatorRun2.h.

Referenced by init(), and mvaValue().