CMS 3D CMS Logo

TMVAEvaluator.cc
Go to the documentation of this file.
1 #include <memory>
2 
6 
10 
11 TMVAEvaluator::TMVAEvaluator() : mIsInitialized(false), mUsingGBRForest(false), mUseAdaBoost(false) {}
12 
14  const std::string& method,
15  const std::string& weightFile,
16  const std::vector<std::string>& variables,
17  const std::vector<std::string>& spectators,
18  bool useGBRForest,
19  bool useAdaBoost) {
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 }
52 
54  const std::vector<std::string>& variables,
55  const std::vector<std::string>& spectators,
56  bool useAdaBoost) {
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 }
72 
73 float TMVAEvaluator::evaluateTMVA(const std::map<std::string, float>& inputs, bool useSpectators) const {
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 }
108 
109 float TMVAEvaluator::evaluateGBRForest(const std::map<std::string, float>& inputs) const {
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 }
133 
134 float TMVAEvaluator::evaluate(const std::map<std::string, float>& inputs, bool useSpectators) const {
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
void initializeGBRForest(const GBRForest *gbrForest, const std::vector< std::string > &variables, const std::vector< std::string > &spectators, bool useAdaBoost=false)
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
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)
std::shared_ptr< const GBRForest > mGBRForest
Definition: TMVAEvaluator.h:45
Definition: value.py:1
std::string mMethod
Definition: TMVAEvaluator.h:42
float evaluate(const std::map< std::string, float > &inputs, bool useSpectators=false) const
std::map< std::string, std::pair< size_t, float > > mSpectators
Definition: TMVAEvaluator.h:48
std::mutex m_mutex
Definition: TMVAEvaluator.h:43
float evaluateGBRForest(const std::map< std::string, float > &inputs) const
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
vars
Definition: DeepTauId.cc:166
Log< level::Warning, false > LogWarning
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)