CMS 3D CMS Logo

MVAVariableManager.h
Go to the documentation of this file.
1 #ifndef RecoEgamma_EgammaTools_MVAVariableManager_H
2 #define RecoEgamma_EgammaTools_MVAVariableManager_H
3 
10 
11 #include <fstream>
12 
13 template <class ParticleType>
15 
16  public:
17 
18  MVAVariableManager(const std::string &variableDefinitionFileName)
19  : nVars_ (0)
20  {
21  edm::FileInPath variableDefinitionFileEdm(variableDefinitionFileName);
22  std::ifstream file(variableDefinitionFileEdm.fullPath());
23 
24  std::string name, formula, upper, lower;
25  while( true ) {
26  file >> name;
27  if(file.eof()) {
28  break;
29  }
30  if (name.find("#") != std::string::npos) {
32  continue;
33  }
34  file >> formula >> lower >> upper;
35  if (file.eof()) {
36  break;
37  }
38  addVariable(name, formula, lower, upper);
39  }
40  }
41 
43  {
44  std::map<std::string,int>::iterator it = indexMap_.find(name);
45  if (it == indexMap_.end()) {
46  return -1;
47  } else {
48  return it->second;
49  }
50  }
51 
52  const std::string& getName(int index) const {
53  return names_[index];
54  }
55 
56  int getNVars() const {
57  return nVars_;
58  }
59 
60  float getValue(int index, const ParticleType& particle, const std::vector<float>& auxVariables) const {
61  float value;
62 
63  MVAVariableInfo varInfo = variableInfos_[index];
64 
65  if (varInfo.auxIndex >= 0) value = auxVariables[varInfo.auxIndex];
66  else value = functions_[index](particle);
67 
68  if (varInfo.hasLowerClip && value < varInfo.lowerClipValue) {
69  value = varInfo.lowerClipValue;
70  }
71  if (varInfo.hasUpperClip && value > varInfo.upperClipValue) {
72  value = varInfo.upperClipValue;
73  }
74  return value;
75  }
76 
77  private:
78 
79  struct MVAVariableInfo {
84  int auxIndex;
85  };
86 
88  const std::string &lowerClip, const std::string &upperClip)
89  {
90  bool hasLowerClip = lowerClip.find("None") == std::string::npos;
91  bool hasUpperClip = upperClip.find("None") == std::string::npos;
92  bool isAuxiliary = formula.find("MVAVariableHelper") != std::string::npos ||
93  formula.find("IDValueMapProducer") != std::string::npos ||
94  formula.find("egmPhotonIsolation") != std::string::npos ||
95  formula.find("Rho") != std::string::npos;
96  // *Rho* is still hardcoded...
97  float lowerClipValue = hasLowerClip ? (float)::atof(lowerClip.c_str()) : 0.;
98  float upperClipValue = hasUpperClip ? (float)::atof(upperClip.c_str()) : 0.;
99 
100  if ( !isAuxiliary ) functions_.emplace_back(formula);
101  // Else push back a dummy function since we won't use the
102  // StringObjectFunction to evaluate an auxiliary variable
103  else functions_.emplace_back("pt");
104 
105  formulas_.push_back(formula);
106 
107  int auxIndex = isAuxiliary ? indexMap.getIndex(formula) : -1;
108 
109  MVAVariableInfo varInfo {
110  .hasLowerClip = hasLowerClip,
111  .hasUpperClip = hasUpperClip,
112  .lowerClipValue = lowerClipValue,
113  .upperClipValue = upperClipValue,
114  .auxIndex = auxIndex,
115  };
116 
117  variableInfos_.push_back(varInfo);
118  names_.push_back(name);
119  indexMap_[name] = nVars_;
120  nVars_++;
121  };
122 
123  int nVars_;
124 
125  std::vector<MVAVariableInfo> variableInfos_;
126  std::vector<ThreadSafeStringCut<StringObjectFunction<ParticleType>, ParticleType>> functions_;
127  std::vector<std::string> formulas_;
128  std::vector<std::string> names_;
129  std::map<std::string, int> indexMap_;
130 
132 };
133 
134 #endif
const std::string & getName(int index) const
MVAVariableManager(const std::string &variableDefinitionFileName)
std::vector< std::string > names_
std::map< std::string, int > indexMap_
std::vector< std::string > formulas_
float getValue(int index, const ParticleType &particle, const std::vector< float > &auxVariables) const
std::vector< ThreadSafeStringCut< StringObjectFunction< ParticleType >, ParticleType > > functions_
void addVariable(const std::string &name, const std::string &formula, const std::string &lowerClip, const std::string &upperClip)
const MVAVariableIndexMap< ParticleType > indexMap
int getVarIndex(const std::string &name)
std::string fullPath() const
Definition: FileInPath.cc:163
std::vector< MVAVariableInfo > variableInfos_
ParticleType
Definition of particle types.
Definition: ParticleCode.h:18