Belle II Software light-2406-ragdoll
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.
 
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 52 of file Weightfile.cc.

53 {
54 for (auto& filename : m_filenames) {
55 if (fs::exists(filename)) {
57 fs::remove_all(filename);
58 }
59 }
60 }
bool m_remove_temporary_directories
remove all temporary directories in the destructor of this class
Definition: Weightfile.h:292
std::vector< std::string > m_filenames
generated temporary filenames, which will be removed in the destructor of this class
Definition: Weightfile.h:291

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:290

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

116 {
117 // TODO Test if file is valid
118 std::ifstream in(custom_weightfile, std::ios::in | std::ios::binary);
119 addStream(identifier, in);
120 in.close();
121 }
void addStream(const std::string &identifier, std::istream &in)
Add a stream to our weightfile.
Definition: Weightfile.cc:123

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

139 {
140 std::ofstream out(custom_weightfile, std::ios::out | std::ios::binary);
141 out << getStream(identifier);
142 }
std::string getStream(const std::string &identifier) const
Returns the content of a stored stream as string.
Definition: Weightfile.cc:144

◆ getOptions()

void getOptions ( Options options) const

Fills an Option object from the xml tree.

Parameters
options

Definition at line 67 of file Weightfile.cc.

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

◆ getSignalFraction()

float getSignalFraction ( ) const

Loads the signal fraction frm the xml tree.

Returns

Definition at line 100 of file Weightfile.cc.

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

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

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

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

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

196 {
197 if (boost::ends_with(filename, ".root")) {
198 return loadFromROOTFile(filename);
199 } else if (boost::ends_with(filename, ".xml")) {
200 return loadFromXMLFile(filename);
201 } else {
202 return loadFromDatabase(filename, emd);
203 }
204 }
static Weightfile loadFromXMLFile(const std::string &filename)
Static function which loads a Weightfile from a XML file.
Definition: Weightfile.cc:240
static Weightfile loadFromROOTFile(const std::string &filename)
Static function which loads a Weightfile from a ROOT file.
Definition: Weightfile.cc:217
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:281

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

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

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

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

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

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

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

252 {
253 Weightfile weightfile;
254 boost::property_tree::xml_parser::read_xml(stream, weightfile.m_pt);
255 return weightfile;
256 }
Weightfile()
Construct an empty weightfile.
Definition: Weightfile.h:44

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

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

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

155 {
156 if (boost::ends_with(filename, ".root")) {
157 return saveToROOTFile(weightfile, filename);
158 } else if (boost::ends_with(filename, ".xml")) {
159 return saveToXMLFile(weightfile, filename);
160 } else {
161 return saveToDatabase(weightfile, filename, iov);
162 }
163 }
static void saveToXMLFile(Weightfile &weightfile, const std::string &filename)
Static function which saves a Weightfile to a XML file.
Definition: Weightfile.cc:175
static void saveToROOTFile(Weightfile &weightfile, const std::string &filename)
Static function which saves a Weightfile to a ROOT file.
Definition: Weightfile.cc:165
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:258

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

269 {
270 DBImportArray<DatabaseRepresentationOfWeightfile> dbArray(makeSaveForDatabase(identifier));
271
272 for (auto weightfile : weightfiles) {
273 std::stringstream ss;
274 Weightfile::saveToStream(weightfile, ss);
275 dbArray.appendNew(ss.str());
276 }
277
278 dbArray.import(iov);
279 }
static void saveToStream(Weightfile &weightfile, std::ostream &stream)
Static function which serializes a Weightfile to a stream.
Definition: Weightfile.cc:185

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

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

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

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

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

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

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

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

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