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
MVAVariableManager::nVars_
int nVars_
Definition: MVAVariableManager.h:113
MVAVariableManager::names_
std::vector< std::string > names_
Definition: MVAVariableManager.h:120
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
MVAVariableManager::MVAVariableManager
MVAVariableManager(const std::string &variableDefinitionFileName, IndexMap const &indexMap)
Definition: MVAVariableManager.h:15
MVAVariableManager::getValue
float getValue(int index, const ParticleType &particle, const std::vector< float > &auxVariables) const
Definition: MVAVariableManager.h:50
pfMETCorrectionType0_cfi.formula
formula
Definition: pfMETCorrectionType0_cfi.py:46
MVAVariableManager::MVAVariableInfo::upperClipValue
float upperClipValue
Definition: MVAVariableManager.h:74
MVAVariableManager::formulas_
std::vector< std::string > formulas_
Definition: MVAVariableManager.h:119
MVAVariableManager::addVariable
void addVariable(const std::string &name, const std::string &formula, const std::string &lowerClip, const std::string &upperClip, IndexMap const &indexMap)
Definition: MVAVariableManager.h:79
MVAVariableManager::variableInfos_
std::vector< MVAVariableInfo > variableInfos_
Definition: MVAVariableManager.h:117
MVAVariableManager::getName
const std::string & getName(int index) const
Definition: MVAVariableManager.h:46
FileInPath.h
edm::FileInPath
Definition: FileInPath.h:64
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
MVAVariableManager::getNVars
int getNVars() const
Definition: MVAVariableManager.h:48
MVAVariableManager::getVarIndex
int getVarIndex(const std::string &name)
Definition: MVAVariableManager.h:37
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
thread_safety_macros.h
MVAVariableManager::MVAVariableInfo
Definition: MVAVariableManager.h:70
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
value
Definition: value.py:1
ThreadSafeFunctor.h
pat::ParticleType
ParticleType
Definition of particle types.
Definition: ParticleCode.h:17
MVAVariableManager::MVAVariableInfo::auxIndex
int auxIndex
Definition: MVAVariableManager.h:75
MVAVariableManager
Definition: MVAVariableManager.h:12
MVAVariableManager::indexMap_
std::map< std::string, int > indexMap_
Definition: MVAVariableManager.h:121
MVAVariableManager::MVAVariableInfo::lowerClipValue
float lowerClipValue
Definition: MVAVariableManager.h:73
relativeConstraints.value
value
Definition: relativeConstraints.py:53
MVAVariableManager::MVAVariableInfo::hasUpperClip
bool hasUpperClip
Definition: MVAVariableManager.h:72
pileupCalc.upper
upper
Definition: pileupCalc.py:214
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
MVAVariableManager::MVAVariableInfo::hasLowerClip
bool hasLowerClip
Definition: MVAVariableManager.h:71
MVAVariableManager::functions_
std::vector< ThreadSafeFunctor< StringObjectFunction< ParticleType > > > functions_
Definition: MVAVariableManager.h:118
StringObjectFunction.h
edm::FileInPath::fullPath
std::string fullPath() const
Definition: FileInPath.cc:163