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 public:
16  MVAVariableManager(const std::string &variableDefinitionFileName) : nVars_(0) {
17  edm::FileInPath variableDefinitionFileEdm(variableDefinitionFileName);
18  std::ifstream file(variableDefinitionFileEdm.fullPath());
19 
20  std::string name, formula, upper, lower;
21  while (true) {
22  file >> name;
23  if (file.eof()) {
24  break;
25  }
26  if (name.find("#") != std::string::npos) {
28  continue;
29  }
30  file >> formula >> lower >> upper;
31  if (file.eof()) {
32  break;
33  }
34  addVariable(name, formula, lower, upper);
35  }
36  }
37 
39  std::map<std::string, int>::iterator it = indexMap_.find(name);
40  if (it == indexMap_.end()) {
41  return -1;
42  } else {
43  return it->second;
44  }
45  }
46 
47  const std::string &getName(int index) const { return names_[index]; }
48 
49  int getNVars() const { return nVars_; }
50 
51  float getValue(int index, const ParticleType &particle, const std::vector<float> &auxVariables) const {
52  float value;
53 
54  MVAVariableInfo varInfo = variableInfos_[index];
55 
56  if (varInfo.auxIndex >= 0)
57  value = auxVariables[varInfo.auxIndex];
58  else
59  value = functions_[index](particle);
60 
61  if (varInfo.hasLowerClip && value < varInfo.lowerClipValue) {
62  value = varInfo.lowerClipValue;
63  }
64  if (varInfo.hasUpperClip && value > varInfo.upperClipValue) {
65  value = varInfo.upperClipValue;
66  }
67  return value;
68  }
69 
70 private:
71  struct MVAVariableInfo {
76  int auxIndex;
77  };
78 
80  const std::string &formula,
81  const std::string &lowerClip,
82  const std::string &upperClip) {
83  bool hasLowerClip = lowerClip.find("None") == std::string::npos;
84  bool hasUpperClip = upperClip.find("None") == std::string::npos;
85  bool isAuxiliary = formula.find("Rho") != std::string::npos; // *Rho* is still hardcoded...
86  float lowerClipValue = hasLowerClip ? (float)::atof(lowerClip.c_str()) : 0.;
87  float upperClipValue = hasUpperClip ? (float)::atof(upperClip.c_str()) : 0.;
88 
89  if (!isAuxiliary)
90  functions_.emplace_back(formula);
91  // Else push back a dummy function since we won't use the
92  // StringObjectFunction to evaluate an auxiliary variable
93  else
94  functions_.emplace_back("pt");
95 
96  formulas_.push_back(formula);
97 
98  int auxIndex = isAuxiliary ? indexMap.getIndex(formula) : -1;
99 
100  MVAVariableInfo varInfo{
101  .hasLowerClip = hasLowerClip,
102  .hasUpperClip = hasUpperClip,
103  .lowerClipValue = lowerClipValue,
104  .upperClipValue = upperClipValue,
105  .auxIndex = auxIndex,
106  };
107 
108  variableInfos_.push_back(varInfo);
109  names_.push_back(name);
110  indexMap_[name] = nVars_;
111  nVars_++;
112  };
113 
114  int nVars_;
115 
116  std::vector<MVAVariableInfo> variableInfos_;
117  std::vector<ThreadSafeStringCut<StringObjectFunction<ParticleType>, ParticleType>> functions_;
118  std::vector<std::string> formulas_;
119  std::vector<std::string> names_;
120  std::map<std::string, int> indexMap_;
121 
123 };
124 
125 #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
int getIndex(std::string const &name) 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 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:17