Belle II Software  release-05-01-25
MVAExpert.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2017 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost, Jonas Wagner *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #include <tracking/trackFindingVXD/mva/MVAExpert.h>
11 
12 
13 #include <mva/interface/Interface.h>
14 
15 #include <framework/utilities/FileSystem.h>
16 #include <framework/logging/Logger.h>
17 
18 #include <boost/algorithm/string/predicate.hpp>
19 
20 using namespace Belle2;
21 
22 MVAExpert::MVAExpert(const std::string& identifier,
23  std::vector<Named<float*>> namedVariables)
24  : m_allNamedVariables(std::move(namedVariables))
25  , m_identifier(identifier)
26 {
27 }
28 
30 {
32  using boost::algorithm::ends_with;
33  if (not m_weightfileRepresentation and
34  not(ends_with(m_identifier, ".root") or ends_with(m_identifier, ".xml"))) {
35  using DBWeightFileRepresentation = DBObjPtr<DatabaseRepresentationOfWeightfile>;
36  m_weightfileRepresentation = std::make_unique<DBWeightFileRepresentation>(m_identifier);
37  }
38 }
39 
41 {
42  std::unique_ptr<MVA::Weightfile> weightfile = getWeightFile();
43  if (weightfile) {
44  // FastBDT_version refers to the weightfile version, only FastBDT_VERSION_MAJOR >= 5 can handle FastBDT_version==2
45  if (weightfile->getElement<std::string>("method") == "FastBDT" and
46  (weightfile->getElement<int>("FastBDT_version") == 1 or
47  weightfile->getElement<int>("FastBDT_version") == 2)) {
48 
49  int nExpectedVars = weightfile->getElement<int>("number_feature_variables");
50 
52  for (int iVar = 0; iVar < nExpectedVars; ++iVar) {
53  std::string variableElementName = "variable" + std::to_string(iVar);
54  std::string expectedName = weightfile->getElement<std::string>(variableElementName);
55 
56  auto itNamedVariable = std::find_if(m_allNamedVariables.begin(),
57  m_allNamedVariables.end(),
58  [expectedName](const Named<Float_t*>& namedVariable) {
59  return namedVariable.getName() == expectedName;
60  });
61 
62  if (itNamedVariable == m_allNamedVariables.end()) {
63  B2ERROR("Variable name " << iVar << " mismatch for FastBDT. " <<
64  "Could not find expected variable '" << expectedName << "'");
65  }
66  m_selectedNamedVariables.push_back(*itNamedVariable);
67  }
68  B2ASSERT("Number of variables mismatch", nExpectedVars == static_cast<int>(m_selectedNamedVariables.size()));
69  } else {
70  B2WARNING("Unpacked new kind of classifier. Consider to extend the feature variable check.");
72  }
73 
74  std::map<std::string, MVA::AbstractInterface*> supportedInterfaces =
76  MVA::GeneralOptions generalOptions;
77  weightfile->getOptions(generalOptions);
78  m_expert = supportedInterfaces[generalOptions.m_method]->getExpert();
79  m_expert->load(*weightfile);
80 
81  std::vector<float> dummy;
82  dummy.resize(m_selectedNamedVariables.size(), 0);
83  m_dataset = std::make_unique<MVA::SingleDataset>(generalOptions, std::move(dummy), 0);
84  } else {
85  B2ERROR("Could not find weight file for identifier " << m_identifier);
86  }
87 }
88 
89 std::unique_ptr<MVA::Weightfile> MVAExpert::getWeightFile()
90 {
92  std::stringstream ss((*m_weightfileRepresentation)->m_data);
93  return std::make_unique<MVA::Weightfile>(MVA::Weightfile::loadFromStream(ss));
94  } else {
95  std::string weightFilePath = FileSystem::findFile(m_identifier);
96  return std::make_unique<MVA::Weightfile>(MVA::Weightfile::loadFromFile(weightFilePath));
97  }
98 }
99 
100 float MVAExpert::predict()
101 {
102  if (not m_expert) {
103  B2ERROR("MVA Expert is not loaded! I will return 0");
104  return 0;
105  }
106 
107  // Transfer the extracted values to the data set were the expert can find them
108  for (unsigned int i = 0; i < m_selectedNamedVariables.size(); ++i) {
109  m_dataset->m_input[i] = *(m_selectedNamedVariables[i].getValue());
110  }
111  return m_expert->apply(*m_dataset)[0];
112 }
Belle2::MVAExpert::m_dataset
std::unique_ptr< MVA::Dataset > m_dataset
Pointer to the current dataset.
Definition: MVAExpert.h:79
Belle2::MVAExpert::MVAExpert
MVAExpert(const std::string &identifier, std::vector< Named< float * >> namedVariables)
Construct the Expert with the specified weight folder and the name of the training that was used in t...
Definition: MVAExpert.cc:22
Belle2::MVAExpert::getWeightFile
std::unique_ptr< MVA::Weightfile > getWeightFile()
Resolves the source of the weight file and unpacks it.
Definition: MVAExpert.cc:89
Belle2::MVA::AbstractInterface::getSupportedInterfaces
static std::map< std::string, AbstractInterface * > getSupportedInterfaces()
Returns interfaces supported by the MVA Interface.
Definition: Interface.h:55
Belle2::MVA::Weightfile::getOptions
void getOptions(Options &options) const
Fills an Option object from the xml tree.
Definition: Weightfile.cc:76
Belle2::MVAExpert::m_selectedNamedVariables
std::vector< Named< float * > > m_selectedNamedVariables
References to the selected named values from the source variable set.
Definition: MVAExpert.h:70
Belle2::MVAExpert::initialize
void initialize()
Initialise the mva method.
Definition: MVAExpert.cc:178
Belle2::MVAExpert::beginRun
void beginRun()
Update the mva method to the new run.
Definition: MVAExpert.cc:183
Belle2::MVAExpert::m_allNamedVariables
std::vector< Named< float * > > m_allNamedVariables
References to the named values from the source variable set.
Definition: MVAExpert.h:67
Belle2::DBObjPtr
Class for accessing objects in the database.
Definition: DBObjPtr.h:31
Belle2::Named< float * >
Belle2::MVA::Weightfile::getElement
T getElement(const std::string &identifier) const
Returns a stored element from the xml tree.
Definition: Weightfile.h:153
Belle2::MVA::GeneralOptions::m_method
std::string m_method
Name of the MVA method to use.
Definition: Options.h:84
Belle2::MVAExpert::m_weightfileRepresentation
std::unique_ptr< DBObjPtr< DatabaseRepresentationOfWeightfile > > m_weightfileRepresentation
Database pointer to the Database representation of the weightfile.
Definition: MVAExpert.h:73
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MVA::Weightfile::loadFromFile
static Weightfile loadFromFile(const std::string &filename)
Static function which loads a Weightfile from a file.
Definition: Weightfile.cc:215
Belle2::MVAExpert::m_expert
std::unique_ptr< MVA::Expert > m_expert
Pointer to the current MVA Expert.
Definition: MVAExpert.h:76
Belle2::MVAExpert::m_identifier
std::string m_identifier
DB identifier of the expert or file name.
Definition: MVAExpert.h:82
Belle2::MVA::GeneralOptions
General options which are shared by all MVA trainings.
Definition: Options.h:64
Belle2::MVA::AbstractInterface::initSupportedInterfaces
static void initSupportedInterfaces()
Static function which initliazes all supported interfaces, has to be called once before getSupportedI...
Definition: Interface.cc:55
Belle2::MVA::Weightfile::loadFromStream
static Weightfile loadFromStream(std::istream &stream)
Static function which deserializes a Weightfile from a stream.
Definition: Weightfile.cc:260
Belle2::FileSystem::findFile
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:147
Belle2::MVAExpert::predict
float predict()
Evaluate the MVA method and return the MVAOutput.
Definition: MVAExpert.cc:188