Belle II Software development
MVAExpert::Impl Class Reference

Implementation of the class to interact with the MVA package. More...

Public Member Functions

 Impl (const std::string &identifier, std::vector< Named< Float_t * > > namedVariables)
 constructor
 
void initialize ()
 Signal the beginning of the event processing.
 
void beginRun ()
 Called once before a new run begins.
 
std::unique_ptr< MVA::WeightfilegetWeightFile ()
 Get the weight file.
 
double predict ()
 Get the MVA prediction.
 
std::vector< float > predict (float *, int, int)
 Get predictions for several inputs.
 
std::vector< std::string > getVariableNames ()
 Get selected variable names.
 

Private Attributes

std::vector< Named< Float_t * > > m_allNamedVariables
 References to the all named values from the source variable set.
 
std::vector< Named< Float_t * > > m_selectedNamedVariables
 References to the selected named values from the source variable set.
 
std::unique_ptr< DBObjPtr< DatabaseRepresentationOfWeightfile > > m_weightfileRepresentation
 Database pointer to the Database representation of the weightfile.
 
std::unique_ptr< MVA::Expertm_expert
 Pointer to the current MVA Expert.
 
std::unique_ptr< MVA::Datasetm_dataset
 Pointer to the current dataset.
 
MVA::GeneralOptions m_generalOptions
 General options.
 
std::string m_identifier
 DB identifier of the expert or file name.
 

Detailed Description

Implementation of the class to interact with the MVA package.

Definition at line 31 of file MVAExpert.cc.

Constructor & Destructor Documentation

◆ Impl()

Impl ( const std::string & identifier,
std::vector< Named< Float_t * > > namedVariables )

constructor

Definition at line 78 of file MVAExpert.cc.

80 : m_allNamedVariables(std::move(namedVariables))
81 , m_identifier(identifier)
82{
83}
std::vector< Named< Float_t * > > m_allNamedVariables
References to the all named values from the source variable set.
Definition MVAExpert.cc:43
std::string m_identifier
DB identifier of the expert or file name.
Definition MVAExpert.cc:61

Member Function Documentation

◆ beginRun()

void beginRun ( )

Called once before a new run begins.

Definition at line 99 of file MVAExpert.cc.

100{
101 std::unique_ptr<MVA::Weightfile> weightfile = getWeightFile();
102 if (weightfile) {
103 if ((weightfile->getElement<std::string>("method") == "FastBDT" and
104 (weightfile->getElement<int>("FastBDT_version") == 1 or
105 weightfile->getElement<int>("FastBDT_version") == 2)) or
106 (weightfile->getElement<std::string>("method") == "Python")) {
107
108 int nExpectedVars = weightfile->getElement<int>("number_feature_variables");
109
111 for (int iVar = 0; iVar < nExpectedVars; ++iVar) {
112 std::string variableElementName = "variable" + std::to_string(iVar);
113 std::string expectedName = weightfile->getElement<std::string>(variableElementName);
114 auto itNamedVariable = std::find_if(m_allNamedVariables.begin(),
116 [expectedName](const Named<Float_t*>& namedVariable) {
117 return namedVariable.getName() == expectedName;
118 });
119
120 if (itNamedVariable == m_allNamedVariables.end()) {
121 B2ERROR("Variable name " << iVar << " mismatch for FastBDT. " <<
122 "Could not find expected variable '" << expectedName << "'");
123 }
124 m_selectedNamedVariables.push_back(*itNamedVariable);
125 }
126 B2ASSERT("Number of variables mismatch", nExpectedVars == static_cast<int>(m_selectedNamedVariables.size()));
127 } else {
128 B2WARNING("Unpacked new kind of classifier. Consider to extend the feature variable check. Identifier name: " << m_identifier
129 << "; method name: " << weightfile->getElement<std::string>("method"));
131 }
132
133 std::map<std::string, MVA::AbstractInterface*> supportedInterfaces =
135 weightfile->getOptions(m_generalOptions);
136 m_expert = supportedInterfaces[m_generalOptions.m_method]->getExpert();
137 m_expert->load(*weightfile);
138
139 std::vector<float> dummy;
140 dummy.resize(m_selectedNamedVariables.size(), 0);
141 m_dataset = std::make_unique<MVA::SingleDataset>(m_generalOptions, std::move(dummy), 0);
142 } else {
143 B2ERROR("Could not find weight file for identifier " << m_identifier);
144 }
145}
static std::map< std::string, AbstractInterface * > getSupportedInterfaces()
Returns interfaces supported by the MVA Interface.
Definition Interface.h:53
std::unique_ptr< MVA::Weightfile > getWeightFile()
Get the weight file.
Definition MVAExpert.cc:147
std::unique_ptr< MVA::Expert > m_expert
Pointer to the current MVA Expert.
Definition MVAExpert.cc:52
std::vector< Named< Float_t * > > m_selectedNamedVariables
References to the selected named values from the source variable set.
Definition MVAExpert.cc:46
std::unique_ptr< MVA::Dataset > m_dataset
Pointer to the current dataset.
Definition MVAExpert.cc:55
MVA::GeneralOptions m_generalOptions
General options.
Definition MVAExpert.cc:58

◆ getVariableNames()

std::vector< std::string > getVariableNames ( )

Get selected variable names.

Definition at line 188 of file MVAExpert.cc.

189{
190 std::vector<std::string> out(m_selectedNamedVariables.size());
191 for (size_t iName = 0; iName < m_selectedNamedVariables.size(); iName += 1) {
192 out[iName] = m_selectedNamedVariables[iName].getName();
193 }
194 return out;
195}

◆ getWeightFile()

std::unique_ptr< MVA::Weightfile > getWeightFile ( )

Get the weight file.

Definition at line 147 of file MVAExpert.cc.

148{
150 std::stringstream ss((*m_weightfileRepresentation)->m_data);
151 return std::make_unique<MVA::Weightfile>(MVA::Weightfile::loadFromStream(ss));
152 } else {
153 std::string weightFilePath = FileSystem::findFile(m_identifier);
154 return std::make_unique<MVA::Weightfile>(MVA::Weightfile::loadFromFile(weightFilePath));
155 }
156}
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
static Weightfile loadFromStream(std::istream &stream)
Static function which deserializes a Weightfile from a stream.
static Weightfile loadFromFile(const std::string &filename)
Static function which loads a Weightfile from a file.
std::unique_ptr< DBObjPtr< DatabaseRepresentationOfWeightfile > > m_weightfileRepresentation
Database pointer to the Database representation of the weightfile.
Definition MVAExpert.cc:49

◆ initialize()

void initialize ( )

Signal the beginning of the event processing.

Definition at line 85 of file MVAExpert.cc.

86{
88 using boost::algorithm::ends_with;
90 not(ends_with(m_identifier, ".root") or ends_with(m_identifier, ".xml"))) {
91 using DBWeightFileRepresentation = DBObjPtr<DatabaseRepresentationOfWeightfile>;
92 m_weightfileRepresentation = std::make_unique<DBWeightFileRepresentation>(m_identifier);
93 }
94 if ((not m_weightfileRepresentation) or (not m_weightfileRepresentation->isValid())) {
95 B2FATAL("No weight file could be loaded in tracking/trackingUtilities/mva/MVAExpert.");
96 }
97}
static void initSupportedInterfaces()
Static function which initializes all supported interfaces, has to be called once before getSupported...
Definition Interface.cc:46

◆ predict() [1/2]

double predict ( )

Get the MVA prediction.

Definition at line 158 of file MVAExpert.cc.

159{
160 if (not m_expert) {
161 B2ERROR("MVA Expert is not loaded! I will return 0");
162 return NAN;
163 }
164
165 // Transfer the extracted values to the data set were the expert can find them
166 for (unsigned int i = 0; i < m_selectedNamedVariables.size(); ++i) {
167 m_dataset->m_input[i] = *m_selectedNamedVariables[i];
168 }
169 return m_expert->apply(*m_dataset)[0];
170}

◆ predict() [2/2]

std::vector< float > predict ( float * test_data,
int nFeature,
int nRows )

Get predictions for several inputs.

Definition at line 172 of file MVAExpert.cc.

173{
174 std::vector<std::vector<float>> spectators;
175 std::vector<std::vector <float> > data;
176 data.resize(nRows);
177 for (int iRow = 0; iRow < nRows; iRow += 1) {
178 data[iRow].resize(nFeature);
179 for (int iFeature = 0; iFeature < nFeature; iFeature += 1) {
180 data[iRow][iFeature] = test_data[nFeature * iRow + iFeature];
181 }
182 }
183
184 MVA::MultiDataset dataSet(m_generalOptions, data, spectators);
185 return m_expert->apply(dataSet);
186}

Member Data Documentation

◆ m_allNamedVariables

std::vector<Named<Float_t*> > m_allNamedVariables
private

References to the all named values from the source variable set.

Definition at line 43 of file MVAExpert.cc.

◆ m_dataset

std::unique_ptr<MVA::Dataset> m_dataset
private

Pointer to the current dataset.

Definition at line 55 of file MVAExpert.cc.

◆ m_expert

std::unique_ptr<MVA::Expert> m_expert
private

Pointer to the current MVA Expert.

Definition at line 52 of file MVAExpert.cc.

◆ m_generalOptions

MVA::GeneralOptions m_generalOptions
private

General options.

Definition at line 58 of file MVAExpert.cc.

◆ m_identifier

std::string m_identifier
private

DB identifier of the expert or file name.

Definition at line 61 of file MVAExpert.cc.

◆ m_selectedNamedVariables

std::vector<Named<Float_t*> > m_selectedNamedVariables
private

References to the selected named values from the source variable set.

Definition at line 46 of file MVAExpert.cc.

◆ m_weightfileRepresentation

std::unique_ptr<DBObjPtr<DatabaseRepresentationOfWeightfile> > m_weightfileRepresentation
private

Database pointer to the Database representation of the weightfile.

Definition at line 49 of file MVAExpert.cc.


The documentation for this class was generated from the following file: