Belle II Software  release-08-01-10
Weightfile Class Reference

The Weightfile class serializes all information about a training into an xml tree. More...

#include <Weightfile.h>

Collaboration diagram for Weightfile:

Public Member Functions

 Weightfile ()
 Construct an empty weightfile.
 
 ~Weightfile ()
 Destructor (removes temporary files associated with this weightfiles)
 
void addFeatureImportance (const std::map< std::string, float > &importance)
 Add variable importance. More...
 
std::map< std::string, float > getFeatureImportance () const
 Get feature importance.
 
void addOptions (const Options &options)
 Add an Option object to the xml tree. More...
 
void getOptions (Options &options) const
 Fills an Option object from the xml tree. More...
 
void addSignalFraction (float signal_fraction)
 Saves the signal fraction in the xml tree. More...
 
float getSignalFraction () const
 Loads the signal fraction frm the xml tree. More...
 
std::string generateFileName (const std::string &suffix="")
 Returns a temporary filename with the given suffix. More...
 
void addFile (const std::string &identifier, const std::string &custom_weightfile)
 Add a file (mostly a weightfile from a MVA library) to our Weightfile. More...
 
void addStream (const std::string &identifier, std::istream &in)
 Add a stream to our weightfile. More...
 
template<class T >
void addElement (const std::string &identifier, const T &element)
 Add an element to the xml tree. More...
 
template<class T >
void addVector (const std::string &identifier, const std::vector< T > &vector)
 Add a vector to the xml tree. More...
 
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) More...
 
std::string getStream (const std::string &identifier) const
 Returns the content of a stored stream as string. More...
 
template<class T >
getElement (const std::string &identifier) const
 Returns a stored element from the xml tree. More...
 
bool containsElement (const std::string &identifier) const
 Returns true if given element is stored in the property tree. More...
 
template<class T >
getElement (const std::string &identifier, const T &default_value) const
 Returns a stored element from the xml tree. More...
 
template<class T >
std::vector< T > getVector (const std::string &identifier) const
 Returns a stored vector from the xml tree. More...
 
void setRemoveTemporaryDirectories (bool remove_temporary_directories)
 Set the deletion behaviour of the weightfile object for temporary directories For debugging it can be useful to not delete temporary directories.
 
const boost::property_tree::ptree & getXMLTree () const
 Get xml tree.
 

Static Public Member Functions

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. More...
 
static void saveToROOTFile (Weightfile &weightfile, const std::string &filename)
 Static function which saves a Weightfile to a ROOT file. More...
 
static void saveToXMLFile (Weightfile &weightfile, const std::string &filename)
 Static function which saves a Weightfile to a XML file. More...
 
static void saveToStream (Weightfile &weightfile, std::ostream &stream)
 Static function which serializes a Weightfile to a stream. More...
 
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 from the database. More...
 
static Weightfile loadFromFile (const std::string &filename)
 Static function which loads a Weightfile from a file. More...
 
static Weightfile loadFromROOTFile (const std::string &filename)
 Static function which loads a Weightfile from a ROOT file. More...
 
static Weightfile loadFromXMLFile (const std::string &filename)
 Static function which loads a Weightfile from a XML file. More...
 
static Weightfile loadFromStream (std::istream &stream)
 Static function which deserializes a Weightfile from a stream. More...
 
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. More...
 
static void saveArrayToDatabase (const 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. More...
 
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. More...
 

Private Attributes

boost::property_tree::ptree m_pt
 xml tree containing all the saved information of this weightfile
 
std::vector< std::string > m_filenames
 generated temporary filenames, which will be removed in the destructor of this class
 
bool m_remove_temporary_directories = true
 remove all temporary directories in the destructor of this class
 

Detailed Description

The Weightfile class serializes all information about a training into an xml tree.

It can save and load this information from: a stream, a file, the database

Definition at line 38 of file Weightfile.h.

Member Function Documentation

◆ addElement()

void addElement ( const std::string &  identifier,
const T &  element 
)
inline

Add an element to the xml tree.

Parameters
identifieridentifier of the element
elementthe value

Definition at line 114 of file Weightfile.h.

115  {
116  m_pt.put(identifier, element);
117  }
boost::property_tree::ptree m_pt
xml tree containing all the saved information of this weightfile
Definition: Weightfile.h:287

◆ addFeatureImportance()

void addFeatureImportance ( const std::map< std::string, float > &  importance)

Add variable importance.

Parameters
importance

Definition at line 72 of file Weightfile.cc.

73  {
74  m_pt.put("number_of_importance_vars", importance.size());
75  unsigned int i = 0;
76  for (auto& pair : importance) {
77  m_pt.put(std::string("importance_key") + std::to_string(i), pair.first);
78  m_pt.put(std::string("importance_value") + std::to_string(i), pair.second);
79  ++i;
80  }
81  }

◆ 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.

Parameters
identifierof this weightfile
custom_weightfilethe filename

Definition at line 115 of file Weightfile.cc.

◆ addOptions()

void addOptions ( const Options options)

Add an Option object to the xml tree.

Parameters
optionsto add

Definition at line 62 of file Weightfile.cc.

◆ addSignalFraction()

void addSignalFraction ( float  signal_fraction)

Saves the signal fraction in the xml tree.

Parameters
signal_fraction

Definition at line 95 of file Weightfile.cc.

◆ addStream()

void addStream ( const std::string &  identifier,
std::istream &  in 
)

Add a stream to our weightfile.

Parameters
identifierof the stream
inthe stream

Definition at line 123 of file Weightfile.cc.

◆ addVector()

void addVector ( const std::string &  identifier,
const std::vector< T > &  vector 
)
inline

Add a vector to the xml tree.

Parameters
identifieridentifier of the vector
vectorthe std::vector

Definition at line 125 of file Weightfile.h.

◆ containsElement()

bool containsElement ( const std::string &  identifier) const
inline

Returns true if given element is stored in the property tree.

Parameters
identifierof the stored element

Definition at line 160 of file Weightfile.h.

◆ generateFileName()

std::string generateFileName ( const std::string &  suffix = "")

Returns a temporary filename with the given suffix.

All temporary files are removed when the Weightfile class is destroyed

Parameters
suffixof the temporary filename
Returns

Definition at line 105 of file Weightfile.cc.

◆ getElement() [1/2]

T getElement ( const std::string &  identifier) const
inline

Returns a stored element from the xml tree.

Parameters
identifierof the stored element

Definition at line 151 of file Weightfile.h.

◆ getElement() [2/2]

T getElement ( const std::string &  identifier,
const T &  default_value 
) const
inline

Returns a stored element from the xml tree.

Parameters
identifierof the stored element
default_valuedefault value of the stored element

Definition at line 171 of file Weightfile.h.

◆ 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)

Parameters
identifierof the file
custom_weightfilethe filename which is created

Definition at line 138 of file Weightfile.cc.

◆ getOptions()

void getOptions ( Options options) const

Fills an Option object from the xml tree.

Parameters
options

Definition at line 67 of file Weightfile.cc.

◆ getSignalFraction()

float getSignalFraction ( ) const

Loads the signal fraction frm the xml tree.

Returns

Definition at line 100 of file Weightfile.cc.

◆ getStream()

std::string getStream ( const std::string &  identifier) const

Returns the content of a stored stream as string.

Parameters
identifierof the stored stream

Definition at line 144 of file Weightfile.cc.

◆ getVector()

std::vector<T> getVector ( const std::string &  identifier) const
inline

Returns a stored vector from the xml tree.

Parameters
identifierof the stored std::vector

Definition at line 181 of file Weightfile.h.

◆ load()

Weightfile load ( const std::string &  filename,
const Belle2::EventMetaData emd = Belle2::EventMetaData(0, 0, 0) 
)
static

Static function which loads a Weightfile from a file or from the database.

Parameters
filenameof the file containing our serialized Weightfile
emdEventMetaData

Definition at line 195 of file Weightfile.cc.

◆ loadFromDatabase()

Weightfile loadFromDatabase ( const std::string &  identifier,
const Belle2::EventMetaData emd = Belle2::EventMetaData(0, 0,                                         0) 
)
static

Static function which loads a Weightfile from the basf2 condition database.

Parameters
identifierin the database
emdevent meta data containing the current valid event which is checked against the interval of validity of the stored weightfiles.

Definition at line 281 of file Weightfile.cc.

◆ loadFromFile()

Weightfile loadFromFile ( const std::string &  filename)
static

Static function which loads a Weightfile from a file.

Parameters
filenameof the file containing our serialized Weightfile

Definition at line 206 of file Weightfile.cc.

◆ loadFromROOTFile()

Weightfile loadFromROOTFile ( const std::string &  filename)
static

Static function which loads a Weightfile from a ROOT file.

Parameters
filenameof the file containing our serialized Weightfile

Definition at line 217 of file Weightfile.cc.

◆ loadFromStream()

Weightfile loadFromStream ( std::istream &  stream)
static

Static function which deserializes a Weightfile from a stream.

Parameters
streamcontaining our serialized Weightfile

Definition at line 251 of file Weightfile.cc.

◆ loadFromXMLFile()

Weightfile loadFromXMLFile ( const std::string &  filename)
static

Static function which loads a Weightfile from a XML file.

Parameters
filenameof the file containing our serialized Weightfile

Definition at line 240 of file Weightfile.cc.

◆ save()

void save ( Weightfile weightfile,
const std::string &  filename,
const Belle2::IntervalOfValidity iov = Belle2::IntervalOfValidity(0, 0, -1, -1) 
)
static

Static function which saves a Weightfile to a file.

Parameters
weightfileto store
filenameof the file or identifier of the database
iovIntervalOfValidity of the Weightfile

Definition at line 154 of file Weightfile.cc.

◆ saveArrayToDatabase()

void saveArrayToDatabase ( const std::vector< Weightfile > &  weightfiles,
const std::string &  identifier,
const Belle2::IntervalOfValidity iov = Belle2::IntervalOfValidity(0, 0, -1, -1) 
)
static

Static function which saves an array of Weightfile objects in the basf2 condition database.

Parameters
weightfilesvector of weightfiles to save
identifierin the database
iovinterval of validity of this weightfile

Definition at line 267 of file Weightfile.cc.

◆ saveToDatabase()

void saveToDatabase ( Weightfile weightfile,
const std::string &  identifier,
const Belle2::IntervalOfValidity iov = Belle2::IntervalOfValidity(0, 0, -1, -1) 
)
static

Static function which saves a Weightfile in the basf2 condition database.

Parameters
weightfileto save
identifierin the database
iovinterval of validity of this weightfile

Definition at line 258 of file Weightfile.cc.

◆ saveToROOTFile()

void saveToROOTFile ( Weightfile weightfile,
const std::string &  filename 
)
static

Static function which saves a Weightfile to a ROOT file.

Parameters
weightfileto store
filenameof the file

Definition at line 165 of file Weightfile.cc.

◆ saveToStream()

void saveToStream ( Weightfile weightfile,
std::ostream &  stream 
)
static

Static function which serializes a Weightfile to a stream.

Parameters
weightfileto serialize
stream

Definition at line 185 of file Weightfile.cc.

◆ saveToXMLFile()

void saveToXMLFile ( Weightfile weightfile,
const std::string &  filename 
)
static

Static function which saves a Weightfile to a XML file.

Parameters
weightfileto store
filenameof the file

Definition at line 175 of file Weightfile.cc.


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