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