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 (const 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 30 of file MVAExpert.cc.

Constructor & Destructor Documentation

◆ Impl()

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

constructor

Definition at line 82 of file MVAExpert.cc.

84 : m_allNamedVariables(std::move(namedVariables))
85 , m_identifier(identifier)
86{
87}
std::vector< Named< Float_t * > > m_allNamedVariables
References to the all named values from the source variable set.
Definition MVAExpert.cc:44
std::string m_identifier
DB identifier of the expert or file name.
Definition MVAExpert.cc:65

Member Function Documentation

◆ beginRun()

void beginRun ( )

Called once before a new run begins.

Definition at line 102 of file MVAExpert.cc.

103{
104 std::unique_ptr<MVA::Weightfile> weightfile = getWeightFile();
105 // cppcheck-suppress knownConditionTrueFalse ; defensive check on the weightfile representation
106 if (weightfile) {
107 if ((weightfile->getElement<std::string>("method") == "FastBDT" and
108 (weightfile->getElement<int>("FastBDT_version") == 1 or
109 weightfile->getElement<int>("FastBDT_version") == 2)) or
110 (weightfile->getElement<std::string>("method") == "Python")) {
111
112 int nExpectedVars = weightfile->getElement<int>("number_feature_variables");
113
115 for (int iVar = 0; iVar < nExpectedVars; ++iVar) {
116 std::string variableElementName = "variable" + std::to_string(iVar);
117 std::string expectedName = weightfile->getElement<std::string>(variableElementName);
118 auto itNamedVariable = std::find_if(m_allNamedVariables.begin(),
120 [expectedName](const Named<Float_t*>& namedVariable) {
121 return namedVariable.getName() == expectedName;
122 });
123
124 if (itNamedVariable == m_allNamedVariables.end()) {
125 B2ERROR("Variable name " << iVar << " mismatch for FastBDT. " <<
126 "Could not find expected variable '" << expectedName << "'");
127 }
128 m_selectedNamedVariables.push_back(*itNamedVariable);
129 }
130 B2ASSERT("Number of variables mismatch", nExpectedVars == static_cast<int>(m_selectedNamedVariables.size()));
131 } else {
132 B2WARNING("Unpacked new kind of classifier. Consider to extend the feature variable check. Identifier name: " << m_identifier
133 << "; method name: " << weightfile->getElement<std::string>("method"));
135 }
136
137 const std::map<std::string, MVA::AbstractInterface*>& supportedInterfaces =
139 weightfile->getOptions(m_generalOptions);
140 m_expert = supportedInterfaces.at(m_generalOptions.m_method)->getExpert();
141 m_expert->load(*weightfile);
142
143 std::vector<float> dummy;
144 dummy.resize(m_selectedNamedVariables.size(), 0);
145 m_dataset = std::make_unique<MVA::SingleDataset>(m_generalOptions, std::move(dummy), 0);
146 } else {
147 B2ERROR("Could not find weight file for identifier " << m_identifier);
148 }
149}
static const 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:151
std::unique_ptr< MVA::Expert > m_expert
Pointer to the current MVA Expert.
Definition MVAExpert.cc:54
std::vector< Named< Float_t * > > m_selectedNamedVariables
References to the selected named values from the source variable set.
Definition MVAExpert.cc:48
std::unique_ptr< MVA::Dataset > m_dataset
Pointer to the current dataset.
Definition MVAExpert.cc:57
MVA::GeneralOptions m_generalOptions
General options.
Definition MVAExpert.cc:61

◆ getVariableNames()

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

Get selected variable names.

Definition at line 193 of file MVAExpert.cc.

194{
195 std::vector<std::string> out(m_selectedNamedVariables.size());
196 for (size_t iName = 0; iName < m_selectedNamedVariables.size(); iName += 1) {
197 out[iName] = m_selectedNamedVariables[iName].getName();
198 }
199 return out;
200}

◆ getWeightFile()

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

Get the weight file.

Definition at line 151 of file MVAExpert.cc.

152{
154 std::stringstream ss((*m_weightfileRepresentation)->m_data);
155 return std::make_unique<MVA::Weightfile>(MVA::Weightfile::loadFromStream(ss));
156 } else {
157 std::string weightFilePath = FileSystem::findFile(m_identifier);
158 return std::make_unique<MVA::Weightfile>(MVA::Weightfile::loadFromFile(weightFilePath));
159 }
160}
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:51

◆ initialize()

void initialize ( )

Signal the beginning of the event processing.

Definition at line 89 of file MVAExpert.cc.

90{
93 not(m_identifier.ends_with(".root") or m_identifier.ends_with(".xml"))) {
94 using DBWeightFileRepresentation = DBObjPtr<DatabaseRepresentationOfWeightfile>;
95 m_weightfileRepresentation = std::make_unique<DBWeightFileRepresentation>(m_identifier);
96 }
97 if ((not m_weightfileRepresentation) or (not m_weightfileRepresentation->isValid())) {
98 B2FATAL("No weight file could be loaded in tracking/trackingUtilities/mva/MVAExpert.");
99 }
100}
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 162 of file MVAExpert.cc.

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

◆ predict() [2/2]

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

Get predictions for several inputs.

Definition at line 176 of file MVAExpert.cc.

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

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 44 of file MVAExpert.cc.

◆ m_dataset

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

Pointer to the current dataset.

Definition at line 57 of file MVAExpert.cc.

◆ m_expert

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

Pointer to the current MVA Expert.

Definition at line 54 of file MVAExpert.cc.

◆ m_generalOptions

MVA::GeneralOptions m_generalOptions
private

General options.

Definition at line 61 of file MVAExpert.cc.

◆ m_identifier

std::string m_identifier
private

DB identifier of the expert or file name.

Definition at line 65 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 48 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 51 of file MVAExpert.cc.


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