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

11 : 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 146 of file TMVAEvaluator.cc.

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

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

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

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

85  {
86  // default value
87  float value = -99.;
88 
89  // TMVA::Reader is not thread safe
90  std::lock_guard<std::mutex> lock(m_mutex);
91 
92  // set the input variable values
93  for (auto it = mVariables.begin(); it != mVariables.end(); ++it) {
94  if (inputs.count(it->first) > 0)
95  it->second.second = inputs.at(it->first);
96  else
97  edm::LogError("MissingInputVariable")
98  << "Input variable " << it->first
99  << " is missing from the list of inputs. The returned discriminator value might not be sensible.";
100  }
101 
102  // if using spectator variables
103  if (useSpectators) {
104  // set the spectator variable values
105  for (auto it = mSpectators.begin(); it != mSpectators.end(); ++it) {
106  if (inputs.count(it->first) > 0)
107  it->second.second = inputs.at(it->first);
108  else
109  edm::LogError("MissingSpectatorVariable")
110  << "Spectator variable " << it->first
111  << " is missing from the list of inputs. The returned discriminator value might not be sensible.";
112  }
113  }
114 
115  // evaluate the MVA
116  value = mReader->EvaluateMVA(mMethod.c_str());
117 
118  return value;
119 }

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

19  {
20  // initialize the TMVA reader
21  mReader = std::make_unique<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 }

References createGBRForest(), reco::details::loadTMVAWeights(), AlcaSiPixelAliHarvester0T_cff::method, mGBRForest, mIsInitialized, mMethod, mReader, mSpectators, mUseAdaBoost, mUsingGBRForest, mVariables, candidateCombinedMVAV2Computer_cfi::spectators, HLT_FULL_cff::useAdaBoost, HLT_FULL_cff::useGBRForest, L1TEGammaDiff_cfi::variables, and HLT_FULL_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 53 of file TMVAEvaluator.cc.

56  {
57  // add input variables
58  for (std::vector<std::string>::const_iterator it = variables.begin(); it != variables.end(); ++it)
59  mVariables.insert(std::make_pair(*it, std::make_pair(it - variables.begin(), 0.)));
60 
61  // add spectator variables
62  for (std::vector<std::string>::const_iterator it = spectators.begin(); it != spectators.end(); ++it)
63  mSpectators.insert(std::make_pair(*it, std::make_pair(it - spectators.begin(), 0.)));
64 
65  // do not take ownership if getting GBRForest from an external source
66  mGBRForest = std::shared_ptr<const GBRForest>(gbrForest, [](const GBRForest*) {});
67 
68  mIsInitialized = true;
69  mUsingGBRForest = true;
71 }

References mGBRForest, mIsInitialized, mSpectators, mUseAdaBoost, mUsingGBRForest, mVariables, candidateCombinedMVAV2Computer_cfi::spectators, HLT_FULL_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:15
GBRForest
Definition: GBRForest.h:25
AlcaSiPixelAliHarvester0T_cff.method
method
Definition: AlcaSiPixelAliHarvester0T_cff.py:41
HLT_FULL_cff.weightFile
weightFile
Definition: HLT_FULL_cff.py:6777
createGBRForest
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)
Definition: GBRForestTools.cc:244
TMVAEvaluator::mIsInitialized
bool mIsInitialized
Definition: TMVAEvaluator.h:44
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
options
Definition: options.py:1
TMVAEvaluator::evaluateGBRForest
float evaluateGBRForest(const std::map< std::string, float > &inputs) const
Definition: TMVAEvaluator.cc:121
edm::EventSetup::get
T get() const
Definition: EventSetup.h:80
vars
vars
Definition: DeepTauId.cc:159
HLT_FULL_cff.useAdaBoost
useAdaBoost
Definition: HLT_FULL_cff.py:6780
edm::ESHandle< GBRForest >
TMVAEvaluator::mVariables
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:53
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
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
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:53
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:85
HLT_FULL_cff.useGBRForest
useGBRForest
Definition: HLT_FULL_cff.py:6779
TMVAEvaluator::mSpectators
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:54
label
const char * label
Definition: PFTauDecayModeTools.cc:11