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