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
MVAVariableManager::nVars_
int nVars_
Definition: MVAVariableManager.h:112
MVAVariableManager::names_
std::vector< std::string > names_
Definition: MVAVariableManager.h:119
MVAVariableManager::indexMap
const MVAVariableIndexMap indexMap
Definition: MVAVariableManager.h:122
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
MVAVariableManager::getValue
float getValue(int index, const ParticleType &particle, const std::vector< float > &auxVariables) const
Definition: MVAVariableManager.h:51
MVAVariableManager::MVAVariableManager
MVAVariableManager(const std::string &variableDefinitionFileName)
Definition: MVAVariableManager.h:16
pfMETCorrectionType0_cfi.formula
formula
Definition: pfMETCorrectionType0_cfi.py:46
MVAVariableManager::MVAVariableInfo::upperClipValue
float upperClipValue
Definition: MVAVariableManager.h:75
MVAVariableManager::formulas_
std::vector< std::string > formulas_
Definition: MVAVariableManager.h:118
MVAVariableManager::variableInfos_
std::vector< MVAVariableInfo > variableInfos_
Definition: MVAVariableManager.h:116
MVAVariableIndexMap
Definition: MVAVariableHelper.h:13
ThreadSafeStringCut.h
MVAVariableManager::getName
const std::string & getName(int index) const
Definition: MVAVariableManager.h:47
MVAVariableHelper.h
FileInPath.h
edm::FileInPath
Definition: FileInPath.h:64
MVAVariableManager::addVariable
void addVariable(const std::string &name, const std::string &formula, const std::string &lowerClip, const std::string &upperClip)
Definition: MVAVariableManager.h:79
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
MVAVariableManager::getNVars
int getNVars() const
Definition: MVAVariableManager.h:49
MVAVariableManager::getVarIndex
int getVarIndex(const std::string &name)
Definition: MVAVariableManager.h:38
Event.h
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
thread_safety_macros.h
MVAVariableManager::MVAVariableInfo
Definition: MVAVariableManager.h:71
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
value
Definition: value.py:1
pat::ParticleType
ParticleType
Definition of particle types.
Definition: ParticleCode.h:17
MVAVariableManager::MVAVariableInfo::auxIndex
int auxIndex
Definition: MVAVariableManager.h:76
MVAVariableManager
Definition: MVAVariableManager.h:14
MVAVariableManager::indexMap_
std::map< std::string, int > indexMap_
Definition: MVAVariableManager.h:120
MVAVariableIndexMap::getIndex
int getIndex(std::string const &name) const
Definition: MVAVariableHelper.h:17
MVAVariableManager::MVAVariableInfo::lowerClipValue
float lowerClipValue
Definition: MVAVariableManager.h:74
relativeConstraints.value
value
Definition: relativeConstraints.py:53
MVAVariableManager::MVAVariableInfo::hasUpperClip
bool hasUpperClip
Definition: MVAVariableManager.h:73
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
Candidate.h
MVAVariableManager::MVAVariableInfo::hasLowerClip
bool hasLowerClip
Definition: MVAVariableManager.h:72
MVAVariableManager::functions_
std::vector< ThreadSafeStringCut< StringObjectFunction< ParticleType >, ParticleType > > functions_
Definition: MVAVariableManager.h:117
edm::FileInPath::fullPath
std::string fullPath() const
Definition: FileInPath.cc:163