CMS 3D CMS Logo

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 9 of file TMVAEvaluator.cc.

9 : mIsInitialized(false), mUsingGBRForest(false), mUseAdaBoost(false) {}
bool mUsingGBRForest
Definition: TMVAEvaluator.h:45

Member Function Documentation

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

Definition at line 144 of file TMVAEvaluator.cc.

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

144  {
145  // default value
146  float value = -99.;
147 
148  if (!mIsInitialized) {
149  edm::LogError("InitializationError") << "TMVAEvaluator not properly initialized.";
150  return value;
151  }
152 
153  if (useSpectators && inputs.size() < (mVariables.size() + mSpectators.size())) {
154  edm::LogError("MissingInputs") << "Too few inputs provided (" << inputs.size() << " provided but "
155  << mVariables.size() << " input and " << mSpectators.size()
156  << " spectator variables expected).";
157  return value;
158  } else if (inputs.size() < mVariables.size()) {
159  edm::LogError("MissingInputVariable(s)") << "Too few input variables provided (" << inputs.size()
160  << " provided but " << mVariables.size() << " expected).";
161  return value;
162  }
163 
164  if (mUsingGBRForest) {
165  if (useSpectators)
166  edm::LogWarning("UnsupportedFunctionality")
167  << "Use of spectator variables with GBRForest is not supported. Spectator variables will be ignored.";
168  value = evaluateGBRForest(inputs);
169  } else
170  value = evaluateTMVA(inputs, useSpectators);
171 
172  return value;
173 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:53
bool mUsingGBRForest
Definition: TMVAEvaluator.h:45
float evaluateTMVA(const std::map< std::string, float > &inputs, bool useSpectators) const
Definition: value.py:1
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:54
float evaluateGBRForest(const std::map< std::string, float > &inputs) const
float TMVAEvaluator::evaluateGBRForest ( const std::map< std::string, float > &  inputs) const

Definition at line 119 of file TMVAEvaluator.cc.

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

Referenced by evaluate().

119  {
120  // default value
121  float value = -99.;
122 
123  std::unique_ptr<float[]> vars(new float[mVariables.size()]); // allocate n floats
124 
125  // set the input variable values
126  for (auto it = mVariables.begin(); it != mVariables.end(); ++it) {
127  if (inputs.count(it->first) > 0)
128  vars[it->second.first] = inputs.at(it->first);
129  else
130  edm::LogError("MissingInputVariable")
131  << "Input variable " << it->first
132  << " is missing from the list of inputs. The returned discriminator value might not be sensible.";
133  }
134 
135  // evaluate the MVA
136  if (mUseAdaBoost)
137  value = mGBRForest->GetAdaBoostClassifier(vars.get());
138  else
139  value = mGBRForest->GetGradBoostClassifier(vars.get());
140 
141  return value;
142 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:53
std::shared_ptr< const GBRForest > mGBRForest
Definition: TMVAEvaluator.h:51
Definition: value.py:1
float TMVAEvaluator::evaluateTMVA ( const std::map< std::string, float > &  inputs,
bool  useSpectators 
) const

Definition at line 83 of file TMVAEvaluator.cc.

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

Referenced by evaluate().

83  {
84  // default value
85  float value = -99.;
86 
87  // TMVA::Reader is not thread safe
88  std::lock_guard<std::mutex> lock(m_mutex);
89 
90  // set the input variable values
91  for (auto it = mVariables.begin(); it != mVariables.end(); ++it) {
92  if (inputs.count(it->first) > 0)
93  it->second.second = inputs.at(it->first);
94  else
95  edm::LogError("MissingInputVariable")
96  << "Input variable " << it->first
97  << " is missing from the list of inputs. The returned discriminator value might not be sensible.";
98  }
99 
100  // if using spectator variables
101  if (useSpectators) {
102  // set the spectator variable values
103  for (auto it = mSpectators.begin(); it != mSpectators.end(); ++it) {
104  if (inputs.count(it->first) > 0)
105  it->second.second = inputs.at(it->first);
106  else
107  edm::LogError("MissingSpectatorVariable")
108  << "Spectator variable " << it->first
109  << " is missing from the list of inputs. The returned discriminator value might not be sensible.";
110  }
111  }
112 
113  // evaluate the MVA
114  value = mReader->EvaluateMVA(mMethod.c_str());
115 
116  return value;
117 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:53
Definition: value.py:1
std::string mMethod
Definition: TMVAEvaluator.h:48
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:54
std::mutex m_mutex
Definition: TMVAEvaluator.h:49
std::unique_ptr< TMVA::Reader > mReader
Definition: TMVAEvaluator.h:50
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 11 of file TMVAEvaluator.cc.

References createGBRForest(), reco::details::loadTMVAWeights(), AlcaSiPixelAliHarvester0T_cff::method, mGBRForest, mIsInitialized, mMethod, mReader, mSpectators, mUseAdaBoost, mUsingGBRForest, mVariables, candidateChargeBTagComputer_cfi::useAdaBoost, and electrons_cff::weightFile.

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  mVariables.insert(std::make_pair(*it, std::make_pair(it - variables.begin(), 0.)));
26  mReader->AddVariable(it->c_str(), &(mVariables.at(*it).second));
27  }
28 
29  // add spectator variables
30  for (std::vector<std::string>::const_iterator it = spectators.begin(); it != spectators.end(); ++it) {
31  mSpectators.insert(std::make_pair(*it, std::make_pair(it - spectators.begin(), 0.)));
32  mReader->AddSpectator(it->c_str(), &(mSpectators.at(*it).second));
33  }
34 
35  // load the TMVA weights
37 
38  if (useGBRForest) {
40 
41  // now can free some memory
42  mReader.reset(nullptr);
43 
44  mUsingGBRForest = true;
46  }
47 
48  mIsInitialized = true;
49 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:53
bool mUsingGBRForest
Definition: TMVAEvaluator.h:45
std::shared_ptr< const GBRForest > mGBRForest
Definition: TMVAEvaluator.h:51
std::string mMethod
Definition: TMVAEvaluator.h:48
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:54
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:50
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)
void TMVAEvaluator::initializeGBRForest ( const GBRForest gbrForest,
const std::vector< std::string > &  variables,
const std::vector< std::string > &  spectators,
bool  useAdaBoost = false 
)

Definition at line 51 of file TMVAEvaluator.cc.

References mGBRForest, mIsInitialized, mSpectators, mUseAdaBoost, mUsingGBRForest, mVariables, and candidateChargeBTagComputer_cfi::useAdaBoost.

Referenced by initializeGBRForest().

54  {
55  // add input variables
56  for (std::vector<std::string>::const_iterator it = variables.begin(); it != variables.end(); ++it)
57  mVariables.insert(std::make_pair(*it, std::make_pair(it - variables.begin(), 0.)));
58 
59  // add spectator variables
60  for (std::vector<std::string>::const_iterator it = spectators.begin(); it != spectators.end(); ++it)
61  mSpectators.insert(std::make_pair(*it, std::make_pair(it - spectators.begin(), 0.)));
62 
63  // do not take ownership if getting GBRForest from an external source
64  mGBRForest = std::shared_ptr<const GBRForest>(gbrForest, [](const GBRForest*) {});
65 
66  mIsInitialized = true;
67  mUsingGBRForest = true;
69 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:53
bool mUsingGBRForest
Definition: TMVAEvaluator.h:45
std::shared_ptr< const GBRForest > mGBRForest
Definition: TMVAEvaluator.h:51
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:54
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 71 of file TMVAEvaluator.cc.

References edm::EventSetup::get(), initializeGBRForest(), edm::ESHandle< T >::product(), candidateCombinedMVAV2Computer_cfi::spectators, candidateChargeBTagComputer_cfi::useAdaBoost, and objects.autophobj::variables.

75  {
76  edm::ESHandle<GBRForest> gbrForestHandle;
77 
78  iSetup.get<GBRWrapperRcd>().get(label.c_str(), gbrForestHandle);
79 
81 }
void initializeGBRForest(const GBRForest *gbrForest, const std::vector< std::string > &variables, const std::vector< std::string > &spectators, bool useAdaBoost=false)
char const * label
T get() const
Definition: EventSetup.h:71
T const * product() const
Definition: ESHandle.h:86

Member Data Documentation

std::mutex TMVAEvaluator::m_mutex
mutableprivate

Definition at line 49 of file TMVAEvaluator.h.

Referenced by evaluateTMVA().

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

Definition at line 51 of file TMVAEvaluator.h.

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

bool TMVAEvaluator::mIsInitialized
private

Definition at line 44 of file TMVAEvaluator.h.

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

std::string TMVAEvaluator::mMethod
private

Definition at line 48 of file TMVAEvaluator.h.

Referenced by evaluateTMVA(), and initialize().

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

Definition at line 50 of file TMVAEvaluator.h.

Referenced by evaluateTMVA(), and initialize().

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

Definition at line 54 of file TMVAEvaluator.h.

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

bool TMVAEvaluator::mUseAdaBoost
private

Definition at line 46 of file TMVAEvaluator.h.

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

bool TMVAEvaluator::mUsingGBRForest
private

Definition at line 45 of file TMVAEvaluator.h.

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

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