Belle II Software  release-05-02-19
Weightfile.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Keck *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 #ifndef INCLUDE_GUARD_BELLE2_MVA_WEIGHTFILE_HEADER
13 #define INCLUDE_GUARD_BELLE2_MVA_WEIGHTFILE_HEADER
14 
15 #include <mva/interface/Options.h>
16 
17 #include <framework/database/IntervalOfValidity.h>
18 #include <framework/dataobjects/EventMetaData.h>
19 
20 #include <boost/property_tree/ptree.hpp>
21 
22 #include <vector>
23 #include <string>
24 #include <fstream>
25 
26 namespace Belle2 {
32  namespace MVA {
33 
34  std::string makeSaveForDatabase(std::string str);
35 
40  class Weightfile {
41 
42  public:
46  Weightfile() {};
47 
51  ~Weightfile();
52 
57  void addFeatureImportance(const std::map<std::string, float>& importance);
58 
62  std::map<std::string, float> getFeatureImportance() const;
63 
68  void addOptions(const Options& options);
69 
74  void getOptions(Options& options) const;
75 
80  void addSignalFraction(float signal_fraction);
81 
86  float getSignalFraction() const;
87 
94  std::string generateFileName(const std::string& suffix = "");
95 
101  void addFile(const std::string& identifier, const std::string& custom_weightfile);
102 
108  void addStream(const std::string& identifier, std::istream& in);
109 
115  template<class T>
116  void addElement(const std::string& identifier, const T& element)
117  {
118  m_pt.put(identifier, element);
119  }
120 
126  template<class T>
127  void addVector(const std::string& identifier, const std::vector<T>& vector)
128  {
129  m_pt.put(identifier + "_size", vector.size());
130  for (unsigned int i = 0; i < vector.size(); ++i) {
131  m_pt.put(identifier + std::to_string(i), vector[i]);
132  }
133  }
134 
140  void getFile(const std::string& identifier, const std::string& custom_weightfile);
141 
146  std::string getStream(const std::string& identifier) const;
147 
152  template<class T>
153  T getElement(const std::string& identifier) const
154  {
155  return m_pt.get<T>(identifier);
156  }
157 
162  bool containsElement(const std::string& identifier) const
163  {
164  return m_pt.count(identifier) > 0;
165  }
166 
171  template<class T>
172  T getElement(const std::string& identifier, const T& default_value) const
173  {
174  return m_pt.get<T>(identifier, default_value);
175  }
176 
181  template<class T>
182  std::vector<T> getVector(const std::string& identifier) const
183  {
184  std::vector<T> vector;
185  vector.resize(m_pt.get<T>(identifier + "_size"));
186  for (unsigned int i = 0; i < vector.size(); ++i) {
187  vector[i] = m_pt.get<T>(identifier + std::to_string(i));
188  }
189  return vector;
190  }
191 
197  static void save(Weightfile& weightfile, const std::string& filename,
198  const Belle2::IntervalOfValidity& iov = Belle2::IntervalOfValidity(0, 0, -1, -1));
199 
205  static void saveToROOTFile(Weightfile& weightfile, const std::string& filename);
206 
212  static void saveToXMLFile(Weightfile& weightfile, const std::string& filename);
213 
219  static void saveToStream(Weightfile& weightfile, std::ostream& stream);
220 
225  static Weightfile load(const std::string& filename, const Belle2::EventMetaData& emd = Belle2::EventMetaData(0, 0, 0));
226 
231  static Weightfile loadFromFile(const std::string& filename);
232 
237  static Weightfile loadFromROOTFile(const std::string& filename);
238 
243  static Weightfile loadFromXMLFile(const std::string& filename);
244 
249  static Weightfile loadFromStream(std::istream& stream);
250 
257  static void saveToDatabase(Weightfile& weightfile, const std::string& identifier,
258  const Belle2::IntervalOfValidity& iov = Belle2::IntervalOfValidity(0, 0, -1, -1));
259 
266  static void saveArrayToDatabase(std::vector<Weightfile>& weightfiles, const std::string& identifier,
267  const Belle2::IntervalOfValidity& iov = Belle2::IntervalOfValidity(0, 0, -1, -1));
268 
274  static Weightfile loadFromDatabase(const std::string& identifier, const Belle2::EventMetaData& emd = Belle2::EventMetaData(0, 0,
275  0));
276 
281  void setRemoveTemporaryDirectories(bool remove_temporary_directories) { m_remove_temporary_directories = remove_temporary_directories; }
282 
286  void setTemporaryDirectory(const std::string& temporary_directory) { m_temporary_directory = temporary_directory; }
287 
291  const boost::property_tree::ptree& getXMLTree() const { return m_pt; };
292 
293  private:
294  boost::property_tree::ptree m_pt;
295  std::vector<std::string> m_filenames;
297  std::string m_temporary_directory = "/tmp";
298  };
299 
300  }
302 }
303 #endif
Belle2::IntervalOfValidity
A class that describes the interval of experiments/runs for which an object in the database is valid.
Definition: IntervalOfValidity.h:35
Belle2::MVA::Weightfile::getVector
std::vector< T > getVector(const std::string &identifier) const
Returns a stored vector from the xml tree.
Definition: Weightfile.h:182
Belle2::MVA::Weightfile::addSignalFraction
void addSignalFraction(float signal_fraction)
Saves the signal fraction in the xml tree.
Definition: Weightfile.cc:104
Belle2::MVA::Weightfile::getOptions
void getOptions(Options &options) const
Fills an Option object from the xml tree.
Definition: Weightfile.cc:76
Belle2::MVA::Weightfile::saveToDatabase
static void saveToDatabase(Weightfile &weightfile, const std::string &identifier, const Belle2::IntervalOfValidity &iov=Belle2::IntervalOfValidity(0, 0, -1, -1))
Static function which saves a Weightfile in the basf2 condition database.
Definition: Weightfile.cc:267
Belle2::MVA::Weightfile::m_pt
boost::property_tree::ptree m_pt
xml tree containing all the saved information of this weightfile
Definition: Weightfile.h:291
Belle2::MVA::Weightfile::setTemporaryDirectory
void setTemporaryDirectory(const std::string &temporary_directory)
set temporary directory which is used to store temporary directories
Definition: Weightfile.h:286
Belle2::MVA::Weightfile::containsElement
bool containsElement(const std::string &identifier) const
Returns true if given element is stored in the property tree.
Definition: Weightfile.h:162
Belle2::MVA::Weightfile::addFile
void addFile(const std::string &identifier, const std::string &custom_weightfile)
Add a file (mostly a weightfile from a MVA library) to our Weightfile.
Definition: Weightfile.cc:124
Belle2::MVA::Weightfile
The Weightfile class serializes all information about a training into an xml tree.
Definition: Weightfile.h:40
Belle2::MVA::Weightfile::m_temporary_directory
std::string m_temporary_directory
temporary directory which is used to store temporary directories
Definition: Weightfile.h:297
Belle2::MVA::Weightfile::addFeatureImportance
void addFeatureImportance(const std::map< std::string, float > &importance)
Add variable importance.
Definition: Weightfile.cc:81
Belle2::MVA::Weightfile::addVector
void addVector(const std::string &identifier, const std::vector< T > &vector)
Add a vector to the xml tree.
Definition: Weightfile.h:127
Belle2::MVA::Weightfile::m_remove_temporary_directories
bool m_remove_temporary_directories
remove all temporary directories in the destructor of this class
Definition: Weightfile.h:296
Belle2::MVA::Weightfile::addStream
void addStream(const std::string &identifier, std::istream &in)
Add a stream to our weightfile.
Definition: Weightfile.cc:132
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::Weightfile::getXMLTree
const boost::property_tree::ptree & getXMLTree() const
Get xml tree.
Definition: Weightfile.h:291
Belle2::MVA::Weightfile::getStream
std::string getStream(const std::string &identifier) const
Returns the content of a stored stream as string.
Definition: Weightfile.cc:153
Belle2::MVA::Weightfile::loadFromROOTFile
static Weightfile loadFromROOTFile(const std::string &filename)
Static function which loads a Weightfile from a ROOT file.
Definition: Weightfile.cc:226
Belle2::MVA::Weightfile::getElement
T getElement(const std::string &identifier, const T &default_value) const
Returns a stored element from the xml tree.
Definition: Weightfile.h:172
Belle2::MVA::Options
Abstract base class of all Options given to the MVA interface.
Definition: Options.h:36
Belle2::MVA::Weightfile::getSignalFraction
float getSignalFraction() const
Loads the signal fraction frm the xml tree.
Definition: Weightfile.cc:109
Belle2::MVA::Weightfile::load
static Weightfile load(const std::string &filename, const Belle2::EventMetaData &emd=Belle2::EventMetaData(0, 0, 0))
Static function which loads a Weightfile from a file or fomr the database.
Definition: Weightfile.cc:204
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MVA::Weightfile::addOptions
void addOptions(const Options &options)
Add an Option object to the xml tree.
Definition: Weightfile.cc:71
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::MVA::Weightfile::loadFromXMLFile
static Weightfile loadFromXMLFile(const std::string &filename)
Static function which loads a Weightfile from a XML file.
Definition: Weightfile.cc:249
Belle2::MVA::Weightfile::m_filenames
std::vector< std::string > m_filenames
generated temporary filenames, which will be removed in the destructor of this class
Definition: Weightfile.h:295
Belle2::MVA::Weightfile::saveToStream
static void saveToStream(Weightfile &weightfile, std::ostream &stream)
Static function which serializes a Weightfile to a stream.
Definition: Weightfile.cc:194
Belle2::MVA::Weightfile::Weightfile
Weightfile()
Construct an empty weightfile.
Definition: Weightfile.h:46
Belle2::MVA::Weightfile::getFile
void getFile(const std::string &identifier, const std::string &custom_weightfile)
Creates a file from our weightfile (mostly this will be a weightfile of an MVA library)
Definition: Weightfile.cc:147
Belle2::MVA::Weightfile::saveToXMLFile
static void saveToXMLFile(Weightfile &weightfile, const std::string &filename)
Static function which saves a Weightfile to a XML file.
Definition: Weightfile.cc:184
Belle2::MVA::Weightfile::save
static void save(Weightfile &weightfile, const std::string &filename, const Belle2::IntervalOfValidity &iov=Belle2::IntervalOfValidity(0, 0, -1, -1))
Static function which saves a Weightfile to a file.
Definition: Weightfile.cc:163
Belle2::MVA::Weightfile::addElement
void addElement(const std::string &identifier, const T &element)
Add an element to the xml tree.
Definition: Weightfile.h:116
Belle2::MVA::Weightfile::loadFromDatabase
static Weightfile loadFromDatabase(const std::string &identifier, const Belle2::EventMetaData &emd=Belle2::EventMetaData(0, 0, 0))
Static function which loads a Weightfile from the basf2 condition database.
Definition: Weightfile.cc:290
Belle2::MVA::Weightfile::getFeatureImportance
std::map< std::string, float > getFeatureImportance() const
Get feature importance.
Definition: Weightfile.cc:92
Belle2::MVA::Weightfile::loadFromStream
static Weightfile loadFromStream(std::istream &stream)
Static function which deserializes a Weightfile from a stream.
Definition: Weightfile.cc:260
Belle2::EventMetaData
Store event, run, and experiment numbers.
Definition: EventMetaData.h:43
Belle2::MVA::Weightfile::generateFileName
std::string generateFileName(const std::string &suffix="")
Returns a temporary filename with the given suffix.
Definition: Weightfile.cc:114
Belle2::MVA::Weightfile::setRemoveTemporaryDirectories
void setRemoveTemporaryDirectories(bool remove_temporary_directories)
Set the deletion behaviour of the weightfile object for temporary directories For debugging it can be...
Definition: Weightfile.h:281
Belle2::MVA::Weightfile::saveToROOTFile
static void saveToROOTFile(Weightfile &weightfile, const std::string &filename)
Static function which saves a Weightfile to a ROOT file.
Definition: Weightfile.cc:174
Belle2::MVA::Weightfile::~Weightfile
~Weightfile()
Destructor (removes temporary files associated with this weightfiles)
Definition: Weightfile.cc:61
Belle2::MVA::Weightfile::saveArrayToDatabase
static void saveArrayToDatabase(std::vector< Weightfile > &weightfiles, const std::string &identifier, const Belle2::IntervalOfValidity &iov=Belle2::IntervalOfValidity(0, 0, -1, -1))
Static function which saves an array of Weightfile objects in the basf2 condition database.
Definition: Weightfile.cc:276