CMS 3D CMS Logo

ElectronMVAEstimatorRun2.cc
Go to the documentation of this file.
2 
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 }
22 
24  const std::string& mvaName,
25  int nCategories,
27  const std::vector<std::string>& categoryCutStrings,
28  const std::vector<std::string> &weightFileNames,
29  bool debug)
30  : AnyMVAEstimatorRun2Base( mvaName, mvaTag, nCategories, debug )
31  , mvaVarMngr_ (variableDefinition)
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);
39  init(weightFileNames);
40 }
41 
42 void ElectronMVAEstimatorRun2::init(const std::vector<std::string> &weightFileNames) {
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 }
87 
89 mvaValue( const reco::Candidate* candidate, const std::vector<float>& auxVariables, int &iCategory) const {
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 }
124 
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 }
138 
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 }
T getParameter(std::string const &) const
const std::string & getName(int index) const
MVAVariableManager< reco::GsfElectron > mvaVarMngr_
double pt() const final
transverse momentum
float getValue(int index, const ParticleType &particle, const std::vector< float > &auxVariables) const
const std::string & getName() const
const std::string & getTag() const
std::vector< ThreadSafeStringCut< StringCutObjectSelector< reco::GsfElectron >, reco::GsfElectron > > categoryFunctions_
float mvaValue(const reco::Candidate *candidate, std::vector< float > const &auxVariables, int &iCategory) const override
ElectronMVAEstimatorRun2(const edm::ParameterSet &conf)
void init(const std::vector< std::string > &weightFileNames)
std::vector< std::vector< int > > variables_
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
#define debug
Definition: HDRShower.cc:19
int getVarIndex(const std::string &name)
int findCategory(const reco::Candidate *candidate) const override
SuperClusterRef superCluster() const override
reference to a SuperCluster
Definition: GsfElectron.h:185
vars
Definition: DeepTauId.cc:77
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)