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)
 
 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) {}
bool mUsingGBRForest
Definition: TMVAEvaluator.h:39

Member Function Documentation

◆ evaluate()

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

Definition at line 134 of file TMVAEvaluator.cc.

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

134  {
135  // default value
136  float value = -99.;
137 
138  if (!mIsInitialized) {
139  edm::LogError("InitializationError") << "TMVAEvaluator not properly initialized.";
140  return value;
141  }
142 
143  if (useSpectators && inputs.size() < (mVariables.size() + mSpectators.size())) {
144  edm::LogError("MissingInputs") << "Too few inputs provided (" << inputs.size() << " provided but "
145  << mVariables.size() << " input and " << mSpectators.size()
146  << " spectator variables expected).";
147  return value;
148  } else if (inputs.size() < mVariables.size()) {
149  edm::LogError("MissingInputVariable(s)") << "Too few input variables provided (" << inputs.size()
150  << " provided but " << mVariables.size() << " expected).";
151  return value;
152  }
153 
154  if (mUsingGBRForest) {
155  if (useSpectators)
156  edm::LogWarning("UnsupportedFunctionality")
157  << "Use of spectator variables with GBRForest is not supported. Spectator variables will be ignored.";
159  } else
160  value = evaluateTMVA(inputs, useSpectators);
161 
162  return value;
163 }
float evaluateTMVA(const std::map< std::string, float > &inputs, bool useSpectators) const
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:47
bool mUsingGBRForest
Definition: TMVAEvaluator.h:39
Log< level::Error, false > LogError
Definition: value.py:1
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:48
float evaluateGBRForest(const std::map< std::string, float > &inputs) const
Log< level::Warning, false > LogWarning

◆ evaluateGBRForest()

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

Definition at line 109 of file TMVAEvaluator.cc.

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

Referenced by evaluate().

109  {
110  // default value
111  float value = -99.;
112 
113  std::unique_ptr<float[]> vars(new float[mVariables.size()]); // allocate n floats
114 
115  // set the input variable values
116  for (auto it = mVariables.begin(); it != mVariables.end(); ++it) {
117  if (inputs.count(it->first) > 0)
118  vars[it->second.first] = inputs.at(it->first);
119  else
120  edm::LogError("MissingInputVariable")
121  << "Input variable " << it->first
122  << " is missing from the list of inputs. The returned discriminator value might not be sensible.";
123  }
124 
125  // evaluate the MVA
126  if (mUseAdaBoost)
127  value = mGBRForest->GetAdaBoostClassifier(vars.get());
128  else
129  value = mGBRForest->GetGradBoostClassifier(vars.get());
130 
131  return value;
132 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:47
vars
Definition: DeepTauIdBase.h:60
Log< level::Error, false > LogError
std::shared_ptr< const GBRForest > mGBRForest
Definition: TMVAEvaluator.h:45
Definition: value.py:1

◆ evaluateTMVA()

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

Definition at line 73 of file TMVAEvaluator.cc.

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

Referenced by evaluate().

73  {
74  // default value
75  float value = -99.;
76 
77  // TMVA::Reader is not thread safe
78  std::lock_guard<std::mutex> lock(m_mutex);
79 
80  // set the input variable values
81  for (auto it = mVariables.begin(); it != mVariables.end(); ++it) {
82  if (inputs.count(it->first) > 0)
83  it->second.second = inputs.at(it->first);
84  else
85  edm::LogError("MissingInputVariable")
86  << "Input variable " << it->first
87  << " is missing from the list of inputs. The returned discriminator value might not be sensible.";
88  }
89 
90  // if using spectator variables
91  if (useSpectators) {
92  // set the spectator variable values
93  for (auto it = mSpectators.begin(); it != mSpectators.end(); ++it) {
94  if (inputs.count(it->first) > 0)
95  it->second.second = inputs.at(it->first);
96  else
97  edm::LogError("MissingSpectatorVariable")
98  << "Spectator variable " << it->first
99  << " is missing from the list of inputs. The returned discriminator value might not be sensible.";
100  }
101  }
102 
103  // evaluate the MVA
104  value = mReader->EvaluateMVA(mMethod.c_str());
105 
106  return value;
107 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:47
Log< level::Error, false > LogError
Definition: value.py:1
std::string mMethod
Definition: TMVAEvaluator.h:42
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:48
std::mutex m_mutex
Definition: TMVAEvaluator.h:43
std::unique_ptr< TMVA::Reader > mReader
Definition: TMVAEvaluator.h:44

◆ 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.

References createGBRForest(), ALPAKA_ACCELERATOR_NAMESPACE::vertexFinder::it, reco::details::loadTMVAWeights(), AlcaSiPixelAliHarvester0T_cff::method, mGBRForest, mIsInitialized, mMethod, mReader, mSpectators, mUseAdaBoost, mUsingGBRForest, mVariables, candidateCombinedMVAV2Computer_cfi::spectators, HLT_2024v12_cff::useAdaBoost, HLT_2024v12_cff::useGBRForest, hcalRecHitTable_cff::variables, and HLT_2024v12_cff::weightFile.

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 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:47
bool mUsingGBRForest
Definition: TMVAEvaluator.h:39
std::shared_ptr< const GBRForest > mGBRForest
Definition: TMVAEvaluator.h:45
std::string mMethod
Definition: TMVAEvaluator.h:42
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:48
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:44
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)

◆ initializeGBRForest()

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 ALPAKA_ACCELERATOR_NAMESPACE::vertexFinder::it, mGBRForest, mIsInitialized, mSpectators, mUseAdaBoost, mUsingGBRForest, mVariables, candidateCombinedMVAV2Computer_cfi::spectators, HLT_2024v12_cff::useAdaBoost, and hcalRecHitTable_cff::variables.

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 }
std::map< std::string, std::pair< size_t, float > > mVariables
Definition: TMVAEvaluator.h:47
bool mUsingGBRForest
Definition: TMVAEvaluator.h:39
std::shared_ptr< const GBRForest > mGBRForest
Definition: TMVAEvaluator.h:45
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:48

Member Data Documentation

◆ m_mutex

std::mutex TMVAEvaluator::m_mutex
mutableprivate

Definition at line 43 of file TMVAEvaluator.h.

Referenced by evaluateTMVA().

◆ mGBRForest

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

Definition at line 45 of file TMVAEvaluator.h.

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

◆ mIsInitialized

bool TMVAEvaluator::mIsInitialized
private

Definition at line 38 of file TMVAEvaluator.h.

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

◆ mMethod

std::string TMVAEvaluator::mMethod
private

Definition at line 42 of file TMVAEvaluator.h.

Referenced by evaluateTMVA(), and initialize().

◆ mReader

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

Definition at line 44 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 48 of file TMVAEvaluator.h.

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

◆ mUseAdaBoost

bool TMVAEvaluator::mUseAdaBoost
private

Definition at line 40 of file TMVAEvaluator.h.

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

◆ mUsingGBRForest

bool TMVAEvaluator::mUsingGBRForest
private

Definition at line 39 of file TMVAEvaluator.h.

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

◆ mVariables

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