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.

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

Member Function Documentation

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

Definition at line 145 of file TMVAEvaluator.cc.

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

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

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

Definition at line 82 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:46
Definition: value.py:1
std::string mMethod
Definition: TMVAEvaluator.h:41
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:47
std::mutex m_mutex
Definition: TMVAEvaluator.h:42
std::unique_ptr< TMVA::Reader > mReader
Definition: TMVAEvaluator.h:43
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 16 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.

19 {
20  // initialize the TMVA reader
21  mReader.reset(new TMVA::Reader(options.c_str()));
22  mReader->SetVerbose(false);
23  mMethod = method;
24 
25  // add input variables
26  for (std::vector<std::string>::const_iterator it = variables.begin(); it != variables.end(); ++it) {
27  mVariables.insert(std::make_pair(*it, std::make_pair(it - variables.begin(), 0.)));
28  mReader->AddVariable(it->c_str(), &(mVariables.at(*it).second));
29  }
30 
31  // add spectator variables
32  for (std::vector<std::string>::const_iterator it = spectators.begin(); it != spectators.end(); ++it) {
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
39 
40  if (useGBRForest) {
42 
43  // now can free some memory
44  mReader.reset(nullptr);
45 
46  mUsingGBRForest = true;
48  }
49 
50  mIsInitialized = true;
51 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:46
bool mUsingGBRForest
Definition: TMVAEvaluator.h:38
std::shared_ptr< const GBRForest > mGBRForest
Definition: TMVAEvaluator.h:44
std::string mMethod
Definition: TMVAEvaluator.h:41
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:47
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:43
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 53 of file TMVAEvaluator.cc.

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

Referenced by initializeGBRForest().

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

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

74 {
75  edm::ESHandle<GBRForest> gbrForestHandle;
76 
77  iSetup.get<GBRWrapperRcd>().get(label.c_str(), gbrForestHandle);
78 
80 }
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:68
T const * product() const
Definition: ESHandle.h:84

Member Data Documentation

std::mutex TMVAEvaluator::m_mutex
mutableprivate

Definition at line 42 of file TMVAEvaluator.h.

Referenced by evaluateTMVA().

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

Definition at line 44 of file TMVAEvaluator.h.

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

bool TMVAEvaluator::mIsInitialized
private

Definition at line 37 of file TMVAEvaluator.h.

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

std::string TMVAEvaluator::mMethod
private

Definition at line 41 of file TMVAEvaluator.h.

Referenced by evaluateTMVA(), and initialize().

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

Definition at line 43 of file TMVAEvaluator.h.

Referenced by evaluateTMVA(), and initialize().

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

Definition at line 47 of file TMVAEvaluator.h.

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

bool TMVAEvaluator::mUseAdaBoost
private

Definition at line 39 of file TMVAEvaluator.h.

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

bool TMVAEvaluator::mUsingGBRForest
private

Definition at line 38 of file TMVAEvaluator.h.

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

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