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 
11 
12 #include <fstream>
13 
14 template <class ParticleType>
16 
17  public:
19  : nVars_ (0)
20  , nHelperVars_ (0)
21  , nGlobalVars_ (0)
22  {
23  };
24 
25  MVAVariableManager(const std::string &variableDefinitionFileName) {
26  init(variableDefinitionFileName);
27  };
28 
29  int init(const std::string &variableDefinitionFileName)
30  {
31  nVars_ = 0;
32  nHelperVars_ = 0;
33  nGlobalVars_ = 0;
34 
35  variableInfos_.clear();
36  functions_.clear();
37  formulas_.clear();
38  names_.clear();
39  helperInputTags_.clear();
40  globalInputTags_.clear();
41 
42  edm::FileInPath variableDefinitionFileEdm(variableDefinitionFileName);
43  std::ifstream file(variableDefinitionFileEdm.fullPath());
44 
45  std::string name, formula, upper, lower;
46  while( true ) {
47  file >> name;
48  if(file.eof()) {
49  break;
50  }
51  if (name.find("#") != std::string::npos) {
53  continue;
54  }
55  file >> formula >> lower >> upper;
56  if (file.eof()) {
57  break;
58  }
59  addVariable(name, formula, lower, upper);
60  }
61  return nVars_;
62  };
63 
65  {
66  std::map<std::string,int>::iterator it = indexMap_.find(name);
67  if (it == indexMap_.end()) {
68  return -1;
69  } else {
70  return it->second;
71  }
72  }
73 
74  const std::string& getName(int index) const {
75  return names_[index];
76  }
77 
78  int getNVars() const {
79  return nVars_;
80  }
81 
82  // For edm::EventBase with getByLabel
83  float getValue(int index, const edm::Ptr<ParticleType>& ptclPtr, const edm::EventBase& iEvent) const {
84  float value;
85  MVAVariableInfo varInfo = variableInfos_[index];
86  if (varInfo.fromVariableHelper >= 0) {
88  iEvent.getByLabel(edm::InputTag(formulas_[index]), vMap);
89  value = (*vMap)[ptclPtr];
90  } else if (varInfo.isGlobalVariable >= 0) {
91  edm::Handle<double> valueHandle;
92  iEvent.getByLabel(edm::InputTag(formulas_[index]), valueHandle);
93  value = *valueHandle;
94  } else {
95  value = functions_[index](*ptclPtr);
96  }
97  if (varInfo.hasLowerClip && value < varInfo.lowerClipValue) {
98  value = varInfo.lowerClipValue;
99  }
100  if (varInfo.hasUpperClip && value > varInfo.upperClipValue) {
101  value = varInfo.upperClipValue;
102  }
103  return value;
104  }
105 
106  // For edm::Event where getByToken is possible
107  float getValue(int index, const edm::Ptr<ParticleType>& ptclPtr, const edm::Event& iEvent) const {
108  float value;
109  MVAVariableInfo varInfo = variableInfos_[index];
110  if (varInfo.fromVariableHelper >= 0) {
112  iEvent.getByToken(helperTokens_[varInfo.fromVariableHelper], vMap);
113  value = (*vMap)[ptclPtr];
114  } else if (varInfo.isGlobalVariable >= 0) {
115  edm::Handle<double> valueHandle;
116  iEvent.getByToken(globalTokens_[varInfo.isGlobalVariable], valueHandle);
117  value = *valueHandle;
118  } else {
119  value = functions_[index](*ptclPtr);
120  }
121  if (varInfo.hasLowerClip && value < varInfo.lowerClipValue) {
122  value = varInfo.lowerClipValue;
123  }
124  if (varInfo.hasUpperClip && value > varInfo.upperClipValue) {
125  value = varInfo.upperClipValue;
126  }
127  return value;
128  }
129 
131  // All tokens for event content needed by the MVA
132  // Tags from the variable helper
133  for (auto &tag : helperInputTags_) {
134  helperTokens_.push_back(cc.consumes<edm::ValueMap<float>>(tag));
135  }
136  for (auto &tag : globalInputTags_) {
137  globalTokens_.push_back(cc.consumes<double>(tag));
138  }
139  }
140 
141  private:
142 
150  };
151 
153  const std::string &lowerClip, const std::string &upperClip)
154  {
155  bool hasLowerClip = lowerClip.find("None") == std::string::npos;
156  bool hasUpperClip = upperClip.find("None") == std::string::npos;
157  int fromVariableHelper = formula.find("MVAVariableHelper") != std::string::npos ||
158  formula.find("IDValueMapProducer") != std::string::npos ||
159  formula.find("egmPhotonIsolation") != std::string::npos;
160  float lowerClipValue = hasLowerClip ? (float)::atof(lowerClip.c_str()) : 0.;
161  float upperClipValue = hasUpperClip ? (float)::atof(upperClip.c_str()) : 0.;
162 
163  // *Rho* is the only global variable used ever, so its hardcoded...
164  int isGlobalVariable = formula.find("Rho") != std::string::npos;
165 
166  if ( !(fromVariableHelper || isGlobalVariable) ) {
168  } else {
169  // Push back a dummy function since we won't use the
170  // StringObjectFunction to evaluate a variable form the helper or a
171  // global variable
173  }
174 
175  formulas_.push_back(formula);
176  if (fromVariableHelper) {
177  helperInputTags_.push_back(edm::InputTag(formula));
178  }
179  if (isGlobalVariable) {
180  globalInputTags_.push_back(edm::InputTag(formula));
181  }
182 
183  // Switch from bool to int, corresponding to the token index
184  fromVariableHelper = fromVariableHelper ? nHelperVars_++ : -1;
185  isGlobalVariable = isGlobalVariable ? nGlobalVars_++ : - 1;
186 
187  MVAVariableInfo varInfo = {
188  .hasLowerClip = hasLowerClip,
189  .hasUpperClip = hasUpperClip,
190  .lowerClipValue = lowerClipValue,
191  .upperClipValue = upperClipValue,
192  .fromVariableHelper = fromVariableHelper,
193  .isGlobalVariable = isGlobalVariable
194  };
195  variableInfos_.push_back(varInfo);
196  names_.push_back(name);
197  indexMap_[name] = nVars_;
198  nVars_++;
199  };
200 
201  int nVars_;
204 
205  std::vector<MVAVariableInfo> variableInfos_;
206  std::vector<StringObjectFunction<ParticleType>> functions_;
207  std::vector<std::string> formulas_;
208  std::vector<std::string> names_;
209  std::map<std::string, int> indexMap_;
210 
211  // To store the MVAVariableHelper input tags needed for the variables in this container
212  std::vector<edm::InputTag> helperInputTags_;
213  std::vector<edm::InputTag> globalInputTags_;
214 
215  // Tokens
216  std::vector<edm::EDGetToken> helperTokens_;
217  std::vector<edm::EDGetToken> globalTokens_;
218 };
219 
220 #endif
void setConsumes(edm::ConsumesCollector &&cc)
const std::string & getName(int index) const
float getValue(int index, const edm::Ptr< ParticleType > &ptclPtr, const edm::Event &iEvent) const
MVAVariableManager(const std::string &variableDefinitionFileName)
std::vector< std::string > names_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
std::map< std::string, int > indexMap_
std::vector< std::string > formulas_
std::vector< edm::EDGetToken > globalTokens_
int iEvent
Definition: GenABIO.cc:230
void addVariable(const std::string &name, const std::string &formula, const std::string &lowerClip, const std::string &upperClip)
std::vector< edm::InputTag > helperInputTags_
int getVarIndex(const std::string &name)
std::vector< edm::EDGetToken > helperTokens_
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)