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 
4 #include <fstream>
5 
8 
12 
15 
17 
18 template <class ParticleType>
20 
21  public:
23  nVars_ = 0;
24  };
25 
26  MVAVariableManager(const std::string &variableDefinitionFileName) {
27  init(variableDefinitionFileName);
28  };
29 
30  int init(const std::string &variableDefinitionFileName) {
31  nVars_ = 0;
32 
33  variableInfos_.clear();
34  functions_.clear();
35  formulas_.clear();
36  names_.clear();
37  helperInputTags_.clear();
38  globalInputTags_.clear();
39 
40  edm::FileInPath variableDefinitionFileEdm(variableDefinitionFileName);
41  std::ifstream file(variableDefinitionFileEdm.fullPath());
42 
43  std::string name, formula, upper, lower;
44  while( true ) {
45  file >> name;
46  if (name.find("#") != std::string::npos) {
48  continue;
49  }
50  file >> formula >> lower >> upper;
51  if (file.eof()) {
52  break;
53  }
54  addVariable(name, formula, lower, upper);
55  }
56  return nVars_;
57  };
58 
60  std::map<std::string,int>::iterator it = indexMap_.find(name);
61  if (it == indexMap_.end()) {
62  return -1;
63  } else {
64  return it->second;
65  }
66  }
67 
68  const std::string getName(int index) const {
69  return names_[index];
70  }
71 
72  const int getNVars() const {
73  return nVars_;
74  }
75 
76  std::vector<edm::InputTag> getHelperInputTags() const {
77  return helperInputTags_;
78  }
79 
80  std::vector<edm::InputTag> getGlobalInputTags() const {
81  return globalInputTags_;
82  }
83 
84  float getValue(int index, const edm::Ptr<ParticleType>& ptclPtr, const edm::EventBase& iEvent) const {
85  float value;
86  MVAVariableInfo varInfo = variableInfos_[index];
87  if (varInfo.fromVariableHelper) {
89  iEvent.getByLabel(edm::InputTag(formulas_[index]), vMap);
90  value = (*vMap)[ptclPtr];
91  } else if (varInfo.isGlobalVariable) {
92  edm::Handle<double> valueHandle;
93  iEvent.getByLabel(edm::InputTag(formulas_[index]), valueHandle);
94  value = *valueHandle;
95  } else {
96  value = functions_[index](*ptclPtr);
97  }
98  if (varInfo.hasLowerClip && value < varInfo.lowerClipValue) {
99  value = varInfo.lowerClipValue;
100  }
101  if (varInfo.hasUpperClip && value > varInfo.upperClipValue) {
102  value = varInfo.upperClipValue;
103  }
104  return value;
105  }
106 
107  private:
108 
116  };
117 
119  bool hasLowerClip = lowerClip.find("None") == std::string::npos;
120  bool hasUpperClip = upperClip.find("None") == std::string::npos;
121  bool fromVariableHelper = formula.find("MVAVariableHelper") != std::string::npos ||
122  formula.find("IDValueMapProducer") != std::string::npos ||
123  formula.find("egmPhotonIsolation") != std::string::npos;
124  float lowerClipValue = hasLowerClip ? (float)::atof(lowerClip.c_str()) : 0.;
125  float upperClipValue = hasUpperClip ? (float)::atof(upperClip.c_str()) : 0.;
126 
127  // *Rho* is the only global variable used ever, so its hardcoded...
128  bool isGlobalVariable = formula.find("Rho") != std::string::npos;
129 
130  if ( !(fromVariableHelper || isGlobalVariable) ) {
132  } else {
133  // Push back a dummy function since we won't use the
134  // StringObjectFunction to evaluate a variable form the helper or a
135  // global variable
137  }
138 
139  formulas_.push_back(formula);
140  if (fromVariableHelper) {
141  helperInputTags_.push_back(edm::InputTag(formula));
142  }
143  if (isGlobalVariable) {
144  globalInputTags_.push_back(edm::InputTag(formula));
145  }
146  MVAVariableInfo varInfo = {
147  .hasLowerClip = hasLowerClip,
148  .hasUpperClip = hasUpperClip,
149  .lowerClipValue = lowerClipValue,
150  .upperClipValue = upperClipValue,
151  .fromVariableHelper = fromVariableHelper,
152  .isGlobalVariable = isGlobalVariable
153  };
154  variableInfos_.push_back(varInfo);
155  names_.push_back(name);
156  indexMap_[name] = nVars_;
157  nVars_++;
158  };
159 
160 
161  int nVars_;
162  std::vector<MVAVariableInfo> variableInfos_;
163  std::vector<StringObjectFunction<ParticleType>> functions_;
164  std::vector<std::string> formulas_;
165  std::vector<std::string> names_;
166  std::map<std::string, int> indexMap_;
167 
168  // To store the MVAVariableHelper input tags needed for the variables in this container
169  std::vector<edm::InputTag> helperInputTags_;
170 
171  std::vector<edm::InputTag> globalInputTags_;
172 };
173 
174 #endif
const std::string getName(int index) const
void addVariable(std::string &name, std::string &formula, std::string &lowerClip, std::string &upperClip)
MVAVariableManager(const std::string &variableDefinitionFileName)
std::vector< std::string > names_
std::map< std::string, int > indexMap_
std::vector< edm::InputTag > getHelperInputTags() const
std::vector< std::string > formulas_
int iEvent
Definition: GenABIO.cc:230
const int getNVars() const
int getVarIndex(std::string &name)
std::vector< edm::InputTag > helperInputTags_
float getValue(int index, const edm::Ptr< ParticleType > &ptclPtr, const edm::EventBase &iEvent) const
std::vector< edm::InputTag > globalInputTags_
std::vector< StringObjectFunction< ParticleType > > functions_
bool getByLabel(InputTag const &, Handle< T > &) const
Definition: EventBase.h:94
std::string fullPath() const
Definition: FileInPath.cc:197
std::vector< MVAVariableInfo > variableInfos_
int init(const std::string &variableDefinitionFileName)
std::vector< edm::InputTag > getGlobalInputTags() const