CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Attributes
TMVAEvaluator Class Reference

#include <TMVAEvaluator.h>

Public Member Functions

float evaluate (const std::map< std::string, float > &inputs, bool useSpectators=false) const
 
float evaluateGBRForest (const std::map< std::string, float > &inputs) const
 
float evaluateTMVA (const std::map< std::string, float > &inputs, bool useSpectators) const
 
void initialize (const std::string &options, const std::string &method, const std::string &weightFile, const std::vector< std::string > &variables, const std::vector< std::string > &spectators, bool useGBRForest=false, bool useAdaBoost=false)
 
void initializeGBRForest (const GBRForest *gbrForest, const std::vector< std::string > &variables, const std::vector< std::string > &spectators, bool useAdaBoost=false)
 
void initializeGBRForest (const edm::EventSetup &iSetup, const std::string &label, const std::vector< std::string > &variables, const std::vector< std::string > &spectators, bool useAdaBoost=false)
 
 TMVAEvaluator ()
 

Private Attributes

std::mutex m_mutex
 
std::shared_ptr< const GBRForestmGBRForest
 
bool mIsInitialized
 
std::string mMethod
 
std::unique_ptr< TMVA::Reader > mReader
 
std::map< std::string,
std::pair< size_t, float > > 
mSpectators
 
bool mUseAdaBoost
 
bool mUsingGBRForest
 
std::map< std::string,
std::pair< size_t, float > > 
mVariables
 

Detailed Description

Definition at line 16 of file TMVAEvaluator.h.

Constructor & Destructor Documentation

TMVAEvaluator::TMVAEvaluator ( )

Definition at line 10 of file TMVAEvaluator.cc.

10  :
11  mIsInitialized(false), mUsingGBRForest(false), mUseAdaBoost(false)
12 {
13 }
bool mUsingGBRForest
Definition: TMVAEvaluator.h:33

Member Function Documentation

float TMVAEvaluator::evaluate ( const std::map< std::string, float > &  inputs,
bool  useSpectators = false 
) const

Definition at line 155 of file TMVAEvaluator.cc.

References evaluateGBRForest(), evaluateTMVA(), mIsInitialized, mSpectators, mUsingGBRForest, mVariables, and relativeConstraints::value.

156 {
157  // default value
158  float value = -99.;
159 
160  if(!mIsInitialized)
161  {
162  edm::LogError("InitializationError") << "TMVAEvaluator not properly initialized.";
163  return value;
164  }
165 
166  if( useSpectators && inputs.size() < ( mVariables.size() + mSpectators.size() ) )
167  {
168  edm::LogError("MissingInputs") << "Too few inputs provided (" << inputs.size() << " provided but " << mVariables.size() << " input and " << mSpectators.size() << " spectator variables expected).";
169  return value;
170  }
171  else if( inputs.size() < mVariables.size() )
172  {
173  edm::LogError("MissingInputVariable(s)") << "Too few input variables provided (" << inputs.size() << " provided but " << mVariables.size() << " expected).";
174  return value;
175  }
176 
177  if (mUsingGBRForest)
178  {
179  if(useSpectators)
180  edm::LogWarning("UnsupportedFunctionality") << "Use of spectator variables with GBRForest is not supported. Spectator variables will be ignored.";
181  value = evaluateGBRForest(inputs);
182  }
183  else
184  value = evaluateTMVA(inputs, useSpectators);
185 
186  return value;
187 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:44
bool mUsingGBRForest
Definition: TMVAEvaluator.h:33
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:45
float evaluateTMVA(const std::map< std::string, float > &inputs, bool useSpectators) const
float evaluateGBRForest(const std::map< std::string, float > &inputs) const
float TMVAEvaluator::evaluateGBRForest ( const std::map< std::string, float > &  inputs) const

Definition at line 130 of file TMVAEvaluator.cc.

References mGBRForest, mUseAdaBoost, mVariables, and relativeConstraints::value.

Referenced by evaluate().

131 {
132  // default value
133  float value = -99.;
134 
135  std::unique_ptr<float[]> vars(new float[mVariables.size()]); // allocate n floats
136 
137  // set the input variable values
138  for(auto it = mVariables.begin(); it!=mVariables.end(); ++it)
139  {
140  if (inputs.count(it->first)>0)
141  vars[it->second.first] = inputs.at(it->first);
142  else
143  edm::LogError("MissingInputVariable") << "Input variable " << it->first << " is missing from the list of inputs. The returned discriminator value might not be sensible.";
144  }
145 
146  // evaluate the MVA
147  if (mUseAdaBoost)
148  value = mGBRForest->GetAdaBoostClassifier(vars.get());
149  else
150  value = mGBRForest->GetGradBoostClassifier(vars.get());
151 
152  return value;
153 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:44
std::shared_ptr< const GBRForest > mGBRForest
Definition: TMVAEvaluator.h:42
float TMVAEvaluator::evaluateTMVA ( const std::map< std::string, float > &  inputs,
bool  useSpectators 
) const

Definition at line 93 of file TMVAEvaluator.cc.

References CommonMethods::lock(), m_mutex, mMethod, mReader, mSpectators, mVariables, and relativeConstraints::value.

Referenced by evaluate().

94 {
95  // default value
96  float value = -99.;
97 
98  // TMVA::Reader is not thread safe
99  std::lock_guard<std::mutex> lock(m_mutex);
100 
101  // set the input variable values
102  for(auto it = mVariables.begin(); it!=mVariables.end(); ++it)
103  {
104  if (inputs.count(it->first)>0)
105  it->second.second = inputs.at(it->first);
106  else
107  edm::LogError("MissingInputVariable") << "Input variable " << it->first << " is missing from the list of inputs. The returned discriminator value might not be sensible.";
108  }
109 
110  // if using spectator variables
111  if(useSpectators)
112  {
113  // set the spectator variable values
114  for(auto it = mSpectators.begin(); it!=mSpectators.end(); ++it)
115  {
116  if (inputs.count(it->first)>0)
117  it->second.second = inputs.at(it->first);
118  else
119  edm::LogError("MissingSpectatorVariable") << "Spectator variable " << it->first << " is missing from the list of inputs. The returned discriminator value might not be sensible.";
120  }
121  }
122 
123  // evaluate the MVA
124  value = mReader->EvaluateMVA(mMethod.c_str());
125 
126  return value;
127 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:44
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:45
std::string mMethod
Definition: TMVAEvaluator.h:36
std::mutex m_mutex
Definition: TMVAEvaluator.h:37
std::unique_ptr< TMVA::Reader > mReader
Definition: TMVAEvaluator.h:38
void TMVAEvaluator::initialize ( const std::string &  options,
const std::string &  method,
const std::string &  weightFile,
const std::vector< std::string > &  variables,
const std::vector< std::string > &  spectators,
bool  useGBRForest = false,
bool  useAdaBoost = false 
)

Definition at line 15 of file TMVAEvaluator.cc.

References reco::details::loadTMVAWeights(), PATTauDiscriminationAgainstElectronMVA6_cfi::method, mGBRForest, mIsInitialized, mMethod, mReader, mSpectators, mUseAdaBoost, mUsingGBRForest, and mVariables.

17 {
18  // initialize the TMVA reader
19  mReader.reset(new TMVA::Reader(options.c_str()));
20  mReader->SetVerbose(false);
21  mMethod = method;
22 
23  // add input variables
24  for(std::vector<std::string>::const_iterator it = variables.begin(); it!=variables.end(); ++it)
25  {
26  mVariables.insert( std::make_pair( *it, std::make_pair( it - variables.begin(), 0. ) ) );
27  mReader->AddVariable(it->c_str(), &(mVariables.at(*it).second));
28  }
29 
30  // add spectator variables
31  for(std::vector<std::string>::const_iterator it = spectators.begin(); it!=spectators.end(); ++it)
32  {
33  mSpectators.insert( std::make_pair( *it, std::make_pair( it - spectators.begin(), 0. ) ) );
34  mReader->AddSpectator(it->c_str(), &(mSpectators.at(*it).second));
35  }
36 
37  // load the TMVA weights
38  #if ROOT_VERSION_CODE < ROOT_VERSION(6,7,0)
39  mIMethod = std::unique_ptr<TMVA::IMethod>( reco::details::loadTMVAWeights(mReader.get(), mMethod.c_str(), weightFile.c_str()) );
40  #else
41  reco::details::loadTMVAWeights(mReader.get(), mMethod.c_str(), weightFile.c_str());
42  #endif
43 
44  if (useGBRForest)
45  {
46  mGBRForest.reset( new GBRForest( dynamic_cast<TMVA::MethodBDT*>( mReader->FindMVA(mMethod.c_str()) ) ) );
47 
48  // now can free some memory
49  mReader.reset(nullptr);
50  #if ROOT_VERSION_CODE < ROOT_VERSION(6,7,0)
51  mIMethod.reset(nullptr);
52  #endif
53 
54  mUsingGBRForest = true;
55  mUseAdaBoost = useAdaBoost;
56  }
57 
58  mIsInitialized = true;
59 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:44
bool mUsingGBRForest
Definition: TMVAEvaluator.h:33
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:45
std::shared_ptr< const GBRForest > mGBRForest
Definition: TMVAEvaluator.h:42
std::string mMethod
Definition: TMVAEvaluator.h:36
TMVA::IMethod * loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)
std::unique_ptr< TMVA::Reader > mReader
Definition: TMVAEvaluator.h:38
void TMVAEvaluator::initializeGBRForest ( const GBRForest gbrForest,
const std::vector< std::string > &  variables,
const std::vector< std::string > &  spectators,
bool  useAdaBoost = false 
)

Definition at line 62 of file TMVAEvaluator.cc.

References mGBRForest, mIsInitialized, mSpectators, mUseAdaBoost, mUsingGBRForest, and mVariables.

Referenced by initializeGBRForest().

64 {
65  // add input variables
66  for(std::vector<std::string>::const_iterator it = variables.begin(); it!=variables.end(); ++it)
67  mVariables.insert( std::make_pair( *it, std::make_pair( it - variables.begin(), 0. ) ) );
68 
69  // add spectator variables
70  for(std::vector<std::string>::const_iterator it = spectators.begin(); it!=spectators.end(); ++it)
71  mSpectators.insert( std::make_pair( *it, std::make_pair( it - spectators.begin(), 0. ) ) );
72 
73  // do not take ownership if getting GBRForest from an external source
74  mGBRForest = std::shared_ptr<const GBRForest>(gbrForest, [](const GBRForest*) {} );
75 
76  mIsInitialized = true;
77  mUsingGBRForest = true;
78  mUseAdaBoost = useAdaBoost;
79 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:44
bool mUsingGBRForest
Definition: TMVAEvaluator.h:33
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:45
std::shared_ptr< const GBRForest > mGBRForest
Definition: TMVAEvaluator.h:42
void TMVAEvaluator::initializeGBRForest ( const edm::EventSetup iSetup,
const std::string &  label,
const std::vector< std::string > &  variables,
const std::vector< std::string > &  spectators,
bool  useAdaBoost = false 
)

Definition at line 82 of file TMVAEvaluator.cc.

References edm::EventSetup::get(), initializeGBRForest(), edm::ESHandle< class >::product(), and makeLayoutFileForGui::variables.

84 {
85  edm::ESHandle<GBRForest> gbrForestHandle;
86 
87  iSetup.get<GBRWrapperRcd>().get(label.c_str(), gbrForestHandle);
88 
89  initializeGBRForest(gbrForestHandle.product(), variables, spectators, useAdaBoost);
90 }
void initializeGBRForest(const GBRForest *gbrForest, const std::vector< std::string > &variables, const std::vector< std::string > &spectators, bool useAdaBoost=false)
const T & get() const
Definition: EventSetup.h:56
T const * product() const
Definition: ESHandle.h:86

Member Data Documentation

std::mutex TMVAEvaluator::m_mutex
mutableprivate

Definition at line 37 of file TMVAEvaluator.h.

Referenced by evaluateTMVA().

std::shared_ptr<const GBRForest> TMVAEvaluator::mGBRForest
private

Definition at line 42 of file TMVAEvaluator.h.

Referenced by evaluateGBRForest(), initialize(), and initializeGBRForest().

bool TMVAEvaluator::mIsInitialized
private

Definition at line 32 of file TMVAEvaluator.h.

Referenced by evaluate(), initialize(), and initializeGBRForest().

std::string TMVAEvaluator::mMethod
private

Definition at line 36 of file TMVAEvaluator.h.

Referenced by evaluateTMVA(), and initialize().

std::unique_ptr<TMVA::Reader> TMVAEvaluator::mReader
private

Definition at line 38 of file TMVAEvaluator.h.

Referenced by evaluateTMVA(), and initialize().

std::map<std::string,std::pair<size_t,float> > TMVAEvaluator::mSpectators
mutableprivate

Definition at line 45 of file TMVAEvaluator.h.

Referenced by evaluate(), evaluateTMVA(), initialize(), and initializeGBRForest().

bool TMVAEvaluator::mUseAdaBoost
private

Definition at line 34 of file TMVAEvaluator.h.

Referenced by evaluateGBRForest(), initialize(), and initializeGBRForest().

bool TMVAEvaluator::mUsingGBRForest
private

Definition at line 33 of file TMVAEvaluator.h.

Referenced by evaluate(), initialize(), and initializeGBRForest().

std::map<std::string,std::pair<size_t,float> > TMVAEvaluator::mVariables
mutableprivate