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 edm::EventSetup &iSetup, const std::string &label, const std::vector< std::string > &variables, const std::vector< std::string > &spectators, bool useAdaBoost=false)
 
void initializeGBRForest (const GBRForest *gbrForest, 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::TMVAEvaluator ( )

Definition at line 9 of file TMVAEvaluator.cc.

9 : mIsInitialized(false), mUsingGBRForest(false), mUseAdaBoost(false) {}

Member Function Documentation

◆ evaluate()

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

Definition at line 144 of file TMVAEvaluator.cc.

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.";
169  } else
170  value = evaluateTMVA(inputs, useSpectators);
171 
172  return value;
173 }

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

◆ evaluateGBRForest()

float TMVAEvaluator::evaluateGBRForest ( const std::map< std::string, float > &  inputs) const

Definition at line 119 of file TMVAEvaluator.cc.

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 }

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

Referenced by evaluate().

◆ evaluateTMVA()

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

Definition at line 83 of file TMVAEvaluator.cc.

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 }

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

Referenced by evaluate().

◆ initialize()

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.

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 }

References createGBRForest(), reco::details::loadTMVAWeights(), AlcaSiPixelAliHarvester0T_cff::method, mGBRForest, mIsInitialized, mMethod, mReader, mSpectators, mUseAdaBoost, mUsingGBRForest, mVariables, candidateCombinedMVAV2Computer_cfi::spectators, HLT_2018_cff::useAdaBoost, HLT_2018_cff::useGBRForest, L1TEGammaDiff_cfi::variables, and HLT_2018_cff::weightFile.

◆ initializeGBRForest() [1/2]

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 
)

◆ initializeGBRForest() [2/2]

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.

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 }

References mGBRForest, mIsInitialized, mSpectators, mUseAdaBoost, mUsingGBRForest, mVariables, candidateCombinedMVAV2Computer_cfi::spectators, HLT_2018_cff::useAdaBoost, and L1TEGammaDiff_cfi::variables.

Referenced by initializeGBRForest().

Member Data Documentation

◆ m_mutex

std::mutex TMVAEvaluator::m_mutex
mutableprivate

Definition at line 49 of file TMVAEvaluator.h.

Referenced by evaluateTMVA().

◆ mGBRForest

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

Definition at line 51 of file TMVAEvaluator.h.

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

◆ mIsInitialized

bool TMVAEvaluator::mIsInitialized
private

Definition at line 44 of file TMVAEvaluator.h.

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

◆ mMethod

std::string TMVAEvaluator::mMethod
private

Definition at line 48 of file TMVAEvaluator.h.

Referenced by evaluateTMVA(), and initialize().

◆ mReader

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

Definition at line 50 of file TMVAEvaluator.h.

Referenced by evaluateTMVA(), and initialize().

◆ mSpectators

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().

◆ mUseAdaBoost

bool TMVAEvaluator::mUseAdaBoost
private

Definition at line 46 of file TMVAEvaluator.h.

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

◆ mUsingGBRForest

bool TMVAEvaluator::mUsingGBRForest
private

Definition at line 45 of file TMVAEvaluator.h.

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

◆ mVariables

std::map<std::string, std::pair<size_t, float> > TMVAEvaluator::mVariables
mutableprivate
TMVAEvaluator::mReader
std::unique_ptr< TMVA::Reader > mReader
Definition: TMVAEvaluator.h:50
edm::ESHandle::product
T const * product() const
Definition: ESHandle.h:86
TMVAEvaluator::mMethod
std::string mMethod
Definition: TMVAEvaluator.h:48
L1TEGammaDiff_cfi.variables
variables
Definition: L1TEGammaDiff_cfi.py:5
TMVAEvaluator::mUsingGBRForest
bool mUsingGBRForest
Definition: TMVAEvaluator.h:45
candidateCombinedMVAV2Computer_cfi.spectators
spectators
Definition: candidateCombinedMVAV2Computer_cfi.py:13
GBRForest
Definition: GBRForest.h:25
AlcaSiPixelAliHarvester0T_cff.method
method
Definition: AlcaSiPixelAliHarvester0T_cff.py:41
createGBRForest
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)
Definition: GBRForestTools.cc:244
TMVAEvaluator::mIsInitialized
bool mIsInitialized
Definition: TMVAEvaluator.h:44
options
Definition: options.py:1
TMVAEvaluator::evaluateGBRForest
float evaluateGBRForest(const std::map< std::string, float > &inputs) const
Definition: TMVAEvaluator.cc:119
edm::EventSetup::get
T get() const
Definition: EventSetup.h:73
vars
vars
Definition: DeepTauId.cc:163
edm::ESHandle< GBRForest >
HLT_2018_cff.useAdaBoost
useAdaBoost
Definition: HLT_2018_cff.py:5398
edm::LogWarning
Definition: MessageLogger.h:141
TMVAEvaluator::mVariables
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:53
edm::LogError
Definition: MessageLogger.h:183
CommonMethods.lock
def lock()
Definition: CommonMethods.py:82
TMVAEvaluator::m_mutex
std::mutex m_mutex
Definition: TMVAEvaluator.h:49
TMVAEvaluator::mGBRForest
std::shared_ptr< const GBRForest > mGBRForest
Definition: TMVAEvaluator.h:51
value
Definition: value.py:1
PixelMapPlotter.inputs
inputs
Definition: PixelMapPlotter.py:490
TMVAEvaluator::mUseAdaBoost
bool mUseAdaBoost
Definition: TMVAEvaluator.h:46
get
#define get
TMVAEvaluator::initializeGBRForest
void initializeGBRForest(const GBRForest *gbrForest, const std::vector< std::string > &variables, const std::vector< std::string > &spectators, bool useAdaBoost=false)
Definition: TMVAEvaluator.cc:51
HLT_2018_cff.weightFile
weightFile
Definition: HLT_2018_cff.py:5395
relativeConstraints.value
value
Definition: relativeConstraints.py:53
reco::details::loadTMVAWeights
TMVA::IMethod * loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)
Definition: TMVAZipReader.cc:52
GBRWrapperRcd
Definition: GBRWrapperRcd.h:24
TMVAEvaluator::evaluateTMVA
float evaluateTMVA(const std::map< std::string, float > &inputs, bool useSpectators) const
Definition: TMVAEvaluator.cc:83
HLT_2018_cff.useGBRForest
useGBRForest
Definition: HLT_2018_cff.py:5397
TMVAEvaluator::mSpectators
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:54
label
const char * label
Definition: PFTauDecayModeTools.cc:11