CMS 3D CMS Logo

List of all members | Classes | Public Member Functions | Private Member Functions | Private Attributes
MVAVariableManager< ParticleType > Class Template Reference

#include <MVAVariableManager.h>

Classes

struct  MVAVariableInfo
 

Public Member Functions

const std::string & getName (int index) const
 
int getNVars () const
 
float getValue (int index, const edm::Ptr< ParticleType > &ptclPtr, const edm::EventBase &iEvent) const
 
float getValue (int index, const edm::Ptr< ParticleType > &ptclPtr, const edm::Event &iEvent) const
 
int getVarIndex (const std::string &name)
 
int init (const std::string &variableDefinitionFileName)
 
 MVAVariableManager ()
 
 MVAVariableManager (const std::string &variableDefinitionFileName)
 
void setConsumes (edm::ConsumesCollector &&cc)
 

Private Member Functions

void addVariable (const std::string &name, const std::string &formula, const std::string &lowerClip, const std::string &upperClip)
 

Private Attributes

std::vector< std::string > formulas_
 
std::vector< StringObjectFunction< ParticleType > > functions_
 
std::vector< edm::InputTagglobalInputTags_
 
std::vector< edm::EDGetTokenglobalTokens_
 
std::vector< edm::InputTaghelperInputTags_
 
std::vector< edm::EDGetTokenhelperTokens_
 
std::map< std::string, int > indexMap_
 
std::vector< std::string > names_
 
int nGlobalVars_
 
int nHelperVars_
 
int nVars_
 
std::vector< MVAVariableInfovariableInfos_
 

Detailed Description

template<class ParticleType>
class MVAVariableManager< ParticleType >

Definition at line 15 of file MVAVariableManager.h.

Constructor & Destructor Documentation

template<class ParticleType>
MVAVariableManager< ParticleType >::MVAVariableManager ( )
inline

Definition at line 18 of file MVAVariableManager.h.

19  : nVars_ (0)
20  , nHelperVars_ (0)
21  , nGlobalVars_ (0)
22  {
23  };
template<class ParticleType>
MVAVariableManager< ParticleType >::MVAVariableManager ( const std::string &  variableDefinitionFileName)
inline

Definition at line 25 of file MVAVariableManager.h.

25  {
26  init(variableDefinitionFileName);
27  };
int init(const std::string &variableDefinitionFileName)

Member Function Documentation

template<class ParticleType>
void MVAVariableManager< ParticleType >::addVariable ( const std::string &  name,
const std::string &  formula,
const std::string &  lowerClip,
const std::string &  upperClip 
)
inlineprivate

Definition at line 152 of file MVAVariableManager.h.

Referenced by MVAVariableManager< reco::GsfElectron >::init().

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) {
178  }
179  if (isGlobalVariable) {
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  };
std::vector< std::string > names_
std::map< std::string, int > indexMap_
std::vector< std::string > formulas_
std::vector< edm::InputTag > helperInputTags_
std::vector< edm::InputTag > globalInputTags_
std::vector< StringObjectFunction< ParticleType > > functions_
std::vector< MVAVariableInfo > variableInfos_
template<class ParticleType>
const std::string& MVAVariableManager< ParticleType >::getName ( int  index) const
inline
template<class ParticleType>
int MVAVariableManager< ParticleType >::getNVars ( ) const
inline

Definition at line 78 of file MVAVariableManager.h.

Referenced by ElectronMVANtuplizer::ElectronMVANtuplizer().

78  {
79  return nVars_;
80  }
template<class ParticleType>
float MVAVariableManager< ParticleType >::getValue ( int  index,
const edm::Ptr< ParticleType > &  ptclPtr,
const edm::EventBase iEvent 
) const
inline

Definition at line 83 of file MVAVariableManager.h.

Referenced by ElectronMVANtuplizer::analyze(), PhotonMVAEstimator::mvaValue(), and ElectronMVAEstimatorRun2::mvaValue().

83  {
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  }
std::vector< std::string > formulas_
std::vector< StringObjectFunction< ParticleType > > functions_
bool getByLabel(InputTag const &, Handle< T > &) const
Definition: EventBase.h:94
std::vector< MVAVariableInfo > variableInfos_
template<class ParticleType>
float MVAVariableManager< ParticleType >::getValue ( int  index,
const edm::Ptr< ParticleType > &  ptclPtr,
const edm::Event iEvent 
) const
inline

Definition at line 107 of file MVAVariableManager.h.

107  {
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  }
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
std::vector< edm::EDGetToken > globalTokens_
std::vector< edm::EDGetToken > helperTokens_
std::vector< StringObjectFunction< ParticleType > > functions_
std::vector< MVAVariableInfo > variableInfos_
template<class ParticleType>
int MVAVariableManager< ParticleType >::getVarIndex ( const std::string &  name)
inline

Definition at line 64 of file MVAVariableManager.h.

Referenced by ElectronMVAEstimatorRun2::init(), and PhotonMVAEstimator::PhotonMVAEstimator().

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  }
std::map< std::string, int > indexMap_
template<class ParticleType>
int MVAVariableManager< ParticleType >::init ( const std::string &  variableDefinitionFileName)
inline

Definition at line 29 of file MVAVariableManager.h.

Referenced by MVAVariableManager< reco::GsfElectron >::MVAVariableManager().

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  };
std::vector< std::string > names_
std::vector< std::string > formulas_
void addVariable(const std::string &name, const std::string &formula, const std::string &lowerClip, const std::string &upperClip)
std::vector< edm::InputTag > helperInputTags_
std::vector< edm::InputTag > globalInputTags_
std::vector< StringObjectFunction< ParticleType > > functions_
std::vector< MVAVariableInfo > variableInfos_
template<class ParticleType>
void MVAVariableManager< ParticleType >::setConsumes ( edm::ConsumesCollector &&  cc)
inline

Definition at line 130 of file MVAVariableManager.h.

Referenced by ElectronMVANtuplizer::ElectronMVANtuplizer(), PhotonMVAEstimator::setConsumes(), and ElectronMVAEstimatorRun2::setConsumes().

130  {
131  // All tokens for event content needed by the MVA
132  // Tags from the variable helper
133  for (auto &tag : helperInputTags_) {
135  }
136  for (auto &tag : globalInputTags_) {
137  globalTokens_.push_back(cc.consumes<double>(tag));
138  }
139  }
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
std::vector< edm::EDGetToken > globalTokens_
std::vector< edm::InputTag > helperInputTags_
std::vector< edm::EDGetToken > helperTokens_
std::vector< edm::InputTag > globalInputTags_

Member Data Documentation

template<class ParticleType>
std::vector<std::string> MVAVariableManager< ParticleType >::formulas_
private
template<class ParticleType>
std::vector<StringObjectFunction<ParticleType> > MVAVariableManager< ParticleType >::functions_
private
template<class ParticleType>
std::vector<edm::InputTag> MVAVariableManager< ParticleType >::globalInputTags_
private
template<class ParticleType>
std::vector<edm::EDGetToken> MVAVariableManager< ParticleType >::globalTokens_
private
template<class ParticleType>
std::vector<edm::InputTag> MVAVariableManager< ParticleType >::helperInputTags_
private
template<class ParticleType>
std::vector<edm::EDGetToken> MVAVariableManager< ParticleType >::helperTokens_
private
template<class ParticleType>
std::map<std::string, int> MVAVariableManager< ParticleType >::indexMap_
private
template<class ParticleType>
std::vector<std::string> MVAVariableManager< ParticleType >::names_
private
template<class ParticleType>
int MVAVariableManager< ParticleType >::nGlobalVars_
private
template<class ParticleType>
int MVAVariableManager< ParticleType >::nHelperVars_
private
template<class ParticleType>
int MVAVariableManager< ParticleType >::nVars_
private
template<class ParticleType>
std::vector<MVAVariableInfo> MVAVariableManager< ParticleType >::variableInfos_
private