Belle II Software development
Weightfile Class Reference

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

#include <Weightfile.h>

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.
 
std::map< std::string, float > getFeatureImportance () const
 Get feature importance.
 
void addOptions (const Options &options)
 Add an Option object to the xml tree.
 
void getOptions (Options &options) const
 Fills an Option object from the xml tree.
 
void addSignalFraction (float signal_fraction)
 Saves the signal fraction in the xml tree.
 
float getSignalFraction () const
 Loads the signal fraction frm the xml tree.
 
std::string generateFileName (const std::string &suffix="")
 Returns a temporary filename with the given suffix.
 
void addFile (const std::string &identifier, const std::string &custom_weightfile)
 Add a file (mostly a weightfile from a MVA library) to our Weightfile.
 
void addStream (const std::string &identifier, std::istream &in)
 Add a stream to our weightfile.
 
template<class T>
void addElement (const std::string &identifier, const T &element)
 Add an element to the xml tree.
 
template<class T>
void addVector (const std::string &identifier, const std::vector< T > &vector)
 Add a vector to the xml tree.
 
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)
 
std::string getStream (const std::string &identifier) const
 Returns the content of a stored stream as string.
 
template<class T>
getElement (const std::string &identifier) const
 Returns a stored element from the xml tree.
 
bool containsElement (const std::string &identifier) const
 Returns true if given element is stored in the property tree.
 
template<class T>
getElement (const std::string &identifier, const T &default_value) const
 Returns a stored element from the xml tree.
 
template<class T>
std::vector< T > getVector (const std::string &identifier) const
 Returns a stored vector from the xml tree.
 
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.
 
static void saveToROOTFile (Weightfile &weightfile, const std::string &filename)
 Static function which saves a Weightfile to a ROOT file.
 
static void saveToXMLFile (Weightfile &weightfile, const std::string &filename)
 Static function which saves a Weightfile to a XML file.
 
static void saveToStream (Weightfile &weightfile, std::ostream &stream)
 Static function which serializes a Weightfile to a stream.
 
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.
 
static Weightfile loadFromFile (const std::string &filename)
 Static function which loads a Weightfile from a file.
 
static Weightfile loadFromROOTFile (const std::string &filename)
 Static function which loads a Weightfile from a ROOT file.
 
static Weightfile loadFromXMLFile (const std::string &filename)
 Static function which loads a Weightfile from a XML file.
 
static Weightfile loadFromStream (std::istream &stream)
 Static function which deserializes a Weightfile from a stream.
 
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.
 
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.
 
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.
 

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.

Constructor & Destructor Documentation

◆ Weightfile()

Weightfile ( )
inline

Construct an empty weightfile.

Definition at line 44 of file Weightfile.h.

44{};

◆ ~Weightfile()

~Weightfile ( )

Destructor (removes temporary files associated with this weightfiles)

Definition at line 51 of file Weightfile.cc.

52 {
53 for (auto& filename : m_filenames) {
54 if (fs::exists(filename)) {
55 if (m_remove_temporary_directories)
56 fs::remove_all(filename);
57 }
58 }
59 }

Member Function Documentation

◆ addElement()

template<class T>
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 }

◆ addFeatureImportance()

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

Add variable importance.

Parameters
importance

Definition at line 71 of file Weightfile.cc.

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

◆ 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 114 of file Weightfile.cc.

115 {
116 // TODO Test if file is valid
117 std::ifstream in(custom_weightfile, std::ios::in | std::ios::binary);
118 addStream(identifier, in);
119 in.close();
120 }

◆ addOptions()

void addOptions ( const Options & options)

Add an Option object to the xml tree.

Parameters
optionsto add

Definition at line 61 of file Weightfile.cc.

62 {
63 options.save(m_pt);
64 }

◆ addSignalFraction()

void addSignalFraction ( float signal_fraction)

Saves the signal fraction in the xml tree.

Parameters
signal_fraction

Definition at line 94 of file Weightfile.cc.

95 {
96 m_pt.put("signal_fraction", signal_fraction);
97 }

◆ 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 122 of file Weightfile.cc.

123 {
124 using base64_t = boost::archive::iterators::base64_from_binary <
125 boost::archive::iterators::transform_width<std::string::const_iterator, 6, 8 >>;
126
127 std::string contents;
128 in.seekg(0, std::ios::end);
129 contents.resize(in.tellg());
130 in.seekg(0, std::ios::beg);
131 in.read(&contents[0], contents.size());
132 std::string enc(base64_t(contents.begin()), base64_t(contents.end()));
133
134 m_pt.put(identifier, enc);
135 }

◆ addVector()

template<class T>
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.

126 {
127 m_pt.put(identifier + "_size", vector.size());
128 for (unsigned int i = 0; i < vector.size(); ++i) {
129 m_pt.put(identifier + std::to_string(i), vector[i]);
130 }
131 }

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

161 {
162 return m_pt.count(identifier) > 0;
163 }

◆ 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 104 of file Weightfile.cc.

105 {
106 char* directory_template = strdup((fs::temp_directory_path() / "Basf2MVA.XXXXXX").c_str());
107 auto directory = mkdtemp(directory_template);
108 std::string tmpfile = std::string(directory) + std::string("/weightfile") + suffix;
109 m_filenames.emplace_back(directory);
110 free(directory_template);
111 return tmpfile;
112 }

◆ getElement() [1/2]

template<class T>
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.

152 {
153 return m_pt.get<T>(identifier);
154 }

◆ getElement() [2/2]

template<class T>
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.

172 {
173 return m_pt.get<T>(identifier, default_value);
174 }

◆ getFeatureImportance()

std::map< std::string, float > getFeatureImportance ( ) const

Get feature importance.

Definition at line 82 of file Weightfile.cc.

83 {
84 std::map<std::string, float> importance;
85 unsigned int numberOfImportanceVars = m_pt.get<unsigned int>("number_of_importance_vars", 0);
86 for (unsigned int i = 0; i < numberOfImportanceVars; ++i) {
87 auto key = m_pt.get<std::string>(std::string("importance_key") + std::to_string(i));
88 auto value = m_pt.get<float>(std::string("importance_value") + std::to_string(i));
89 importance[key] = value;
90 }
91 return importance;
92 }

◆ 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 137 of file Weightfile.cc.

138 {
139 std::ofstream out(custom_weightfile, std::ios::out | std::ios::binary);
140 out << getStream(identifier);
141 }

◆ getOptions()

void getOptions ( Options & options) const

Fills an Option object from the xml tree.

Parameters
options

Definition at line 66 of file Weightfile.cc.

67 {
68 options.load(m_pt);
69 }

◆ getSignalFraction()

float getSignalFraction ( ) const

Loads the signal fraction frm the xml tree.

Returns

Definition at line 99 of file Weightfile.cc.

100 {
101 return m_pt.get<float>("signal_fraction");
102 }

◆ 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 143 of file Weightfile.cc.

144 {
145 using binary_t = boost::archive::iterators::transform_width <
146 boost::archive::iterators::binary_from_base64<std::string::const_iterator>, 8, 6 >;
147
148 auto contents = m_pt.get<std::string>(identifier);
149 std::string dec(binary_t(contents.begin()), binary_t(contents.end()));
150 return dec;
151 }

◆ getVector()

template<class T>
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.

182 {
183 std::vector<T> vector;
184 vector.resize(m_pt.get<size_t>(identifier + "_size"));
185 for (unsigned int i = 0; i < vector.size(); ++i) {
186 vector[i] = m_pt.get<T>(identifier + std::to_string(i));
187 }
188 return vector;
189 }

◆ getXMLTree()

const boost::property_tree::ptree & getXMLTree ( ) const
inline

Get xml tree.

Definition at line 287 of file Weightfile.h.

287{ return m_pt; };

◆ 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 194 of file Weightfile.cc.

195 {
196 if (filename.ends_with(".root")) {
197 return loadFromROOTFile(filename);
198 } else if (filename.ends_with(".xml")) {
199 return loadFromXMLFile(filename);
200 } else {
201 return loadFromDatabase(filename, emd);
202 }
203 }

◆ 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 280 of file Weightfile.cc.

281 {
282 auto pair = Belle2::Database::Instance().getData(emd, makeSaveForDatabase(identifier));
283
284 if (pair.first == 0) {
285 throw std::runtime_error("Given identifier cannot be loaded from the database: " + identifier);
286 }
287
288 DatabaseRepresentationOfWeightfile database_representation_of_weightfile = *static_cast<DatabaseRepresentationOfWeightfile*>
289 (pair.first);
290 std::stringstream ss(database_representation_of_weightfile.m_data);
291 return Weightfile::loadFromStream(ss);
292 }
std::pair< TObject *, IntervalOfValidity > getData(const EventMetaData &event, const std::string &name)
Request an object from the database.
Definition Database.cc:71
static Database & Instance()
Instance of a singleton Database.
Definition Database.cc:41

◆ 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 205 of file Weightfile.cc.

206 {
207 if (filename.ends_with(".root")) {
208 return loadFromROOTFile(filename);
209 } else if (filename.ends_with(".xml")) {
210 return loadFromXMLFile(filename);
211 } else {
212 throw std::runtime_error("Cannot load file " + filename + " because file extension is not supported");
213 }
214 }

◆ 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 216 of file Weightfile.cc.

217 {
218
219 if (not fs::exists(filename)) {
220 throw std::runtime_error("Given filename does not exist: " + filename);
221 }
222
223 TFile file(filename.c_str(), "READ");
224 if (file.IsZombie() or not file.IsOpen()) {
225 throw std::runtime_error("Error during open of ROOT file named " + filename);
226 }
227
228 DatabaseRepresentationOfWeightfile* database_representation_of_weightfile = nullptr;
229 file.GetObject("Weightfile", database_representation_of_weightfile);
230
231 if (database_representation_of_weightfile == nullptr) {
232 throw std::runtime_error("The provided ROOT file " + filename + " does not contain a valid MVA weightfile.");
233 }
234 std::stringstream ss(database_representation_of_weightfile->m_data);
235 delete database_representation_of_weightfile;
236 return Weightfile::loadFromStream(ss);
237 }

◆ loadFromStream()

Weightfile loadFromStream ( std::istream & stream)
static

Static function which deserializes a Weightfile from a stream.

Parameters
streamcontaining our serialized Weightfile

Definition at line 250 of file Weightfile.cc.

251 {
252 Weightfile weightfile;
253 boost::property_tree::xml_parser::read_xml(stream, weightfile.m_pt);
254 return weightfile;
255 }

◆ 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 239 of file Weightfile.cc.

240 {
241 if (not fs::exists(filename)) {
242 throw std::runtime_error("Given filename does not exist: " + filename);
243 }
244
245 Weightfile weightfile;
246 boost::property_tree::xml_parser::read_xml(filename, weightfile.m_pt);
247 return weightfile;
248 }

◆ 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 153 of file Weightfile.cc.

154 {
155 if (filename.ends_with(".root")) {
156 return saveToROOTFile(weightfile, filename);
157 } else if (filename.ends_with(".xml")) {
158 return saveToXMLFile(weightfile, filename);
159 } else {
160 return saveToDatabase(weightfile, filename, iov);
161 }
162 }

◆ 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 266 of file Weightfile.cc.

268 {
269 DBImportArray<DatabaseRepresentationOfWeightfile> dbArray(makeSaveForDatabase(identifier));
270
271 for (auto weightfile : weightfiles) {
272 std::stringstream ss;
273 Weightfile::saveToStream(weightfile, ss);
274 dbArray.appendNew(ss.str());
275 }
276
277 dbArray.import(iov);
278 }

◆ 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 257 of file Weightfile.cc.

258 {
259 std::stringstream ss;
260 Weightfile::saveToStream(weightfile, ss);
261 DatabaseRepresentationOfWeightfile database_representation_of_weightfile;
262 database_representation_of_weightfile.m_data = ss.str();
263 Belle2::Database::Instance().storeData(makeSaveForDatabase(identifier), &database_representation_of_weightfile, iov);
264 }
bool storeData(const std::string &name, TObject *object, const IntervalOfValidity &iov)
Store an object in the database.
Definition Database.cc:140

◆ 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 164 of file Weightfile.cc.

165 {
166 std::stringstream ss;
167 Weightfile::saveToStream(weightfile, ss);
168 DatabaseRepresentationOfWeightfile database_representation_of_weightfile;
169 database_representation_of_weightfile.m_data = ss.str();
170 TFile file(filename.c_str(), "RECREATE");
171 file.WriteObject(&database_representation_of_weightfile, "Weightfile");
172 }

◆ 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 184 of file Weightfile.cc.

185 {
186#if BOOST_VERSION < 105600
187 boost::property_tree::xml_writer_settings<char> settings('\t', 1);
188#else
189 boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
190#endif
191 boost::property_tree::xml_parser::write_xml(stream, weightfile.m_pt, settings);
192 }

◆ 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 174 of file Weightfile.cc.

175 {
176#if BOOST_VERSION < 105600
177 boost::property_tree::xml_writer_settings<char> settings('\t', 1);
178#else
179 boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
180#endif
181 boost::property_tree::xml_parser::write_xml(filename, weightfile.m_pt, std::locale(), settings);
182 }

◆ setRemoveTemporaryDirectories()

void setRemoveTemporaryDirectories ( bool remove_temporary_directories)
inline

Set the deletion behaviour of the weightfile object for temporary directories For debugging it can be useful to not delete temporary directories.

Definition at line 282 of file Weightfile.h.

282{ m_remove_temporary_directories = remove_temporary_directories; }

Member Data Documentation

◆ m_filenames

std::vector<std::string> m_filenames
private

generated temporary filenames, which will be removed in the destructor of this class

Definition at line 291 of file Weightfile.h.

◆ m_pt

boost::property_tree::ptree m_pt
private

xml tree containing all the saved information of this weightfile

Definition at line 290 of file Weightfile.h.

◆ m_remove_temporary_directories

bool m_remove_temporary_directories = true
private

remove all temporary directories in the destructor of this class

Definition at line 292 of file Weightfile.h.


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