Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9674 from cms-btv-pog/TMVAEvaluator_from-CMSSW_7_4_5
Added TMVAEvaluator class
- Loading branch information
Showing
7 changed files
with
132 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef CommonTools_Utils_TMVAEvaluator_h | ||
#define CommonTools_Utils_TMVAEvaluator_h | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
#include <map> | ||
|
||
#include "TMVA/Reader.h" | ||
|
||
|
||
class TMVAEvaluator { | ||
|
||
public: | ||
TMVAEvaluator(); | ||
~TMVAEvaluator(); | ||
|
||
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); | ||
float evaluate(const std::map<std::string,float> & inputs); | ||
|
||
private: | ||
bool mIsInitialized; | ||
|
||
std::string mMethod; | ||
std::unique_ptr<TMVA::Reader> mReader; | ||
|
||
std::map<std::string,float> mVariables; | ||
std::map<std::string,float> mSpectators; | ||
}; | ||
|
||
#endif // CommonTools_Utils_TMVAEvaluator_h | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
RecoBTag/SecondaryVertex/interface/MvaBoostedDoubleSecondaryVertexEstimator.h
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
RecoBTag/SecondaryVertex/src/MvaBoostedDoubleSecondaryVertexEstimator.cc
This file was deleted.
Oops, something went wrong.