-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
TMVAEvaluator.cc
74 lines (57 loc) · 2.2 KB
/
TMVAEvaluator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "CommonTools/Utils/interface/TMVAEvaluator.h"
#include "CommonTools/Utils/interface/TMVAZipReader.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
TMVAEvaluator::TMVAEvaluator() :
mIsInitialized(false)
{
}
TMVAEvaluator::~TMVAEvaluator()
{
}
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)
{
// initialize the TMVA reader
mReader.reset(new TMVA::Reader(options.c_str()));
mReader->SetVerbose(false);
mMethod = method;
// add input variables
for(std::vector<std::string>::const_iterator it = variables.begin(); it!=variables.end(); ++it)
{
mVariables.insert( std::pair<std::string,float>(*it,0.) );
mReader->AddVariable(it->c_str(), &mVariables.at(*it));
}
// add spectator variables
for(std::vector<std::string>::const_iterator it = spectators.begin(); it!=spectators.end(); ++it)
{
mSpectators.insert( std::pair<std::string,float>(*it,0.) );
mReader->AddSpectator(it->c_str(), &mSpectators.at(*it));
}
// load the TMVA weights
reco::details::loadTMVAWeights(mReader.get(), mMethod.c_str(), weightFile.c_str());
mIsInitialized = true;
}
float TMVAEvaluator::evaluate(const std::map<std::string,float> & inputs)
{
if(!mIsInitialized)
{
edm::LogError("InitializationError") << "TMVAEvaluator not properly initialized.";
return -99.;
}
if( inputs.size() < mVariables.size() )
{
edm::LogError("MissingInputVariable(s)") << "Too few input variables provided (" << inputs.size() << " provided but " << mVariables.size() << " expected).";
return -99.;
}
// set the input variable values
for(std::map<std::string,float>::iterator it = mVariables.begin(); it!=mVariables.end(); ++it)
{
if (inputs.count(it->first)>0)
it->second = inputs.at(it->first);
else
edm::LogError("MissingInputVariable") << "Variable " << it->first << " is missing from the list of input variables. The returned discriminator value might not be sensible.";
}
// evaluate the MVA
float value = mReader->EvaluateMVA(mMethod.c_str());
return value;
}