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 predictions for several inputs.
 

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

97{
98 std::unique_ptr<MVA::Weightfile> weightfile = getWeightFile();
99 if (weightfile) {
100 if ((weightfile->getElement<std::string>("method") == "FastBDT" and
101 (weightfile->getElement<int>("FastBDT_version") == 1 or
102 weightfile->getElement<int>("FastBDT_version") == 2)) or
103 (weightfile->getElement<std::string>("method") == "Python")) {
104
105 int nExpectedVars = weightfile->getElement<int>("number_feature_variables");
106
108 for (int iVar = 0; iVar < nExpectedVars; ++iVar) {
109 std::string variableElementName = "variable" + std::to_string(iVar);
110 std::string expectedName = weightfile->getElement<std::string>(variableElementName);
111 auto itNamedVariable = std::find_if(m_allNamedVariables.begin(),
113 [expectedName](const Named<Float_t*>& namedVariable) {
114 return namedVariable.getName() == expectedName;
115 });
116
117 if (itNamedVariable == m_allNamedVariables.end()) {
118 B2ERROR("Variable name " << iVar << " mismatch for FastBDT. " <<
119 "Could not find expected variable '" << expectedName << "'");
120 }
121 m_selectedNamedVariables.push_back(*itNamedVariable);
122 }
123 B2ASSERT("Number of variables mismatch", nExpectedVars == static_cast<int>(m_selectedNamedVariables.size()));
124 } else {
125 B2WARNING("Unpacked new kind of classifier. Consider to extend the feature variable check. Identifier name: " << m_identifier
126 << "; method name: " << weightfile->getElement<std::string>("method"));
128 }
129
130 std::map<std::string, MVA::AbstractInterface*> supportedInterfaces =
132 weightfile->getOptions(m_generalOptions);
133 m_expert = supportedInterfaces[m_generalOptions.m_method]->getExpert();
134 m_expert->load(*weightfile);
135
136 std::vector<float> dummy;
137 dummy.resize(m_selectedNamedVariables.size(), 0);
138 m_dataset = std::make_unique<MVA::SingleDataset>(m_generalOptions, std::move(dummy), 0);
139 } else {
140 B2ERROR("Could not find weight file for identifier " << m_identifier);
141 }
142}
static std::map< std::string, AbstractInterface * > getSupportedInterfaces()
Returns interfaces supported by the MVA Interface.
Definition: Interface.h:53
std::string m_method
Name of the MVA method to use.
Definition: Options.h:82
std::unique_ptr< MVA::Weightfile > getWeightFile()
Get the weight file.
Definition: MVAExpert.cc:144
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
A mixin class to attach a name to an object.
Definition: Named.h:23

◆ getVariableNames()

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

Get predictions for several inputs.

Definition at line 185 of file MVAExpert.cc.

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

◆ getWeightFile()

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

Get the weight file.

Definition at line 144 of file MVAExpert.cc.

145{
147 std::stringstream ss((*m_weightfileRepresentation)->m_data);
148 return std::make_unique<MVA::Weightfile>(MVA::Weightfile::loadFromStream(ss));
149 } else {
150 std::string weightFilePath = FileSystem::findFile(m_identifier);
151 return std::make_unique<MVA::Weightfile>(MVA::Weightfile::loadFromFile(weightFilePath));
152 }
153}
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...
Definition: FileSystem.cc:151
static Weightfile loadFromStream(std::istream &stream)
Static function which deserializes a Weightfile from a stream.
Definition: Weightfile.cc:251
static Weightfile loadFromFile(const std::string &filename)
Static function which loads a Weightfile from a file.
Definition: Weightfile.cc:206
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}
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
static void initSupportedInterfaces()
Static function which initliazes all supported interfaces, has to be called once before getSupportedI...
Definition: Interface.cc:45

◆ predict() [1/2]

double predict ( )

Get the MVA prediction.

Definition at line 155 of file MVAExpert.cc.

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

◆ predict() [2/2]

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

Get predictions for several inputs.

Definition at line 169 of file MVAExpert.cc.

170{
171 std::vector<std::vector<float>> spectators;
172 std::vector<std::vector <float> > data;
173 data.resize(nRows);
174 for (int iRow = 0; iRow < nRows; iRow += 1) {
175 data[iRow].resize(nFeature);
176 for (int iFeature = 0; iFeature < nFeature; iFeature += 1) {
177 data[iRow][iFeature] = test_data[nFeature * iRow + iFeature];
178 }
179 }
180
181 MVA::MultiDataset dataSet(m_generalOptions, data, spectators);
182 return m_expert->apply(dataSet);
183}
Wraps the data of a multiple event into a Dataset.
Definition: Dataset.h: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: