11 #include <mva/interface/Weightfile.h>
13 #include <mva/dataobjects/DatabaseRepresentationOfWeightfile.h>
14 #include <framework/database/Database.h>
15 #include <framework/database/DBImportArray.h>
17 #include <boost/archive/iterators/base64_from_binary.hpp>
18 #include <boost/archive/iterators/binary_from_base64.hpp>
19 #include <boost/archive/iterators/transform_width.hpp>
21 #include <boost/property_tree/xml_parser.hpp>
22 #include <boost/filesystem/operations.hpp>
23 #include <boost/algorithm/string/predicate.hpp>
24 #include <boost/algorithm/string.hpp>
25 #include <boost/algorithm/string/replace.hpp>
26 #include <boost/regex.hpp>
40 std::string makeSaveForDatabase(std::string str)
42 std::map<std::string, std::string> replace {
45 for (
auto& pair : replace) {
46 boost::replace_all(str, pair.first, pair.second);
56 if (boost::filesystem::exists(filename)) {
58 boost::filesystem::remove_all(filename);
75 m_pt.put(
"number_of_importance_vars", importance.size());
77 for (
auto& pair : importance) {
78 m_pt.put(std::string(
"importance_key") + std::to_string(i), pair.first);
79 m_pt.put(std::string(
"importance_value") + std::to_string(i), pair.second);
86 std::map<std::string, float> importance;
87 unsigned int numberOfImportanceVars =
m_pt.get<
unsigned int>(
"number_of_importance_vars", 0);
88 for (
unsigned int i = 0; i < numberOfImportanceVars; ++i) {
89 auto key =
m_pt.get<std::string>(std::string(
"importance_key") + std::to_string(i));
90 auto value =
m_pt.get<
float>(std::string(
"importance_value") + std::to_string(i));
91 importance[key] = value;
98 m_pt.put(
"signal_fraction", signal_fraction);
103 return m_pt.get<
float>(
"signal_fraction");
109 auto directory = mkdtemp(directory_template);
110 std::string tmpfile = std::string(directory) + std::string(
"/weightfile") + suffix;
112 free(directory_template);
116 void Weightfile::addFile(
const std::string& identifier,
const std::string& custom_weightfile)
119 std::ifstream in(custom_weightfile, std::ios::in | std::ios::binary);
126 using base64_t = boost::archive::iterators::base64_from_binary <
127 boost::archive::iterators::transform_width<std::string::const_iterator, 6, 8 >>;
129 std::string contents;
130 in.seekg(0, std::ios::end);
131 contents.resize(in.tellg());
132 in.seekg(0, std::ios::beg);
133 in.read(&contents[0], contents.size());
134 std::string enc(base64_t(contents.begin()), base64_t(contents.end()));
136 m_pt.put(identifier, enc);
139 void Weightfile::getFile(
const std::string& identifier,
const std::string& custom_weightfile)
141 std::ofstream out(custom_weightfile, std::ios::out | std::ios::binary);
147 using binary_t = boost::archive::iterators::transform_width <
148 boost::archive::iterators::binary_from_base64<std::string::const_iterator>, 8, 6 >;
150 auto contents =
m_pt.get<std::string>(identifier);
151 std::string dec(binary_t(contents.begin()), binary_t(contents.end()));
157 if (boost::ends_with(filename,
".root")) {
159 }
else if (boost::ends_with(filename,
".xml")) {
168 std::stringstream ss;
171 database_representation_of_weightfile.
m_data = ss.str();
172 TFile file(filename.c_str(),
"RECREATE");
173 file.WriteObject(&database_representation_of_weightfile,
"Weightfile");
178 #if BOOST_VERSION < 105600
179 boost::property_tree::xml_writer_settings<char> settings(
'\t', 1);
181 boost::property_tree::xml_writer_settings<std::string> settings(
'\t', 1);
183 boost::property_tree::xml_parser::write_xml(filename, weightfile.
m_pt, std::locale(), settings);
188 #if BOOST_VERSION < 105600
189 boost::property_tree::xml_writer_settings<char> settings(
'\t', 1);
191 boost::property_tree::xml_writer_settings<std::string> settings(
'\t', 1);
193 boost::property_tree::xml_parser::write_xml(stream, weightfile.
m_pt, settings);
198 if (boost::ends_with(filename,
".root")) {
200 }
else if (boost::ends_with(filename,
".xml")) {
209 if (boost::ends_with(filename,
".root")) {
211 }
else if (boost::ends_with(filename,
".xml")) {
214 throw std::runtime_error(
"Cannot load file " + filename +
" because file extension is not supported");
221 if (not boost::filesystem::exists(filename)) {
222 throw std::runtime_error(
"Given filename does not exist: " + filename);
225 TFile file(filename.c_str(),
"READ");
226 if (file.IsZombie() or not file.IsOpen()) {
227 throw std::runtime_error(
"Error during open of ROOT file named " + filename);
231 file.GetObject(
"Weightfile", database_representation_of_weightfile);
233 if (database_representation_of_weightfile ==
nullptr) {
234 throw std::runtime_error(
"The provided ROOT file " + filename +
" does not contain a valid MVA weightfile.");
236 std::stringstream ss(database_representation_of_weightfile->
m_data);
237 delete database_representation_of_weightfile;
243 if (not boost::filesystem::exists(filename)) {
244 throw std::runtime_error(
"Given filename does not exist: " + filename);
248 boost::property_tree::xml_parser::read_xml(filename, weightfile.m_pt);
255 boost::property_tree::xml_parser::read_xml(stream, weightfile.
m_pt);
261 std::stringstream ss;
264 database_representation_of_weightfile.
m_data = ss.str();
273 for (
auto weightfile : weightfiles) {
274 std::stringstream ss;
286 if (pair.first == 0) {
287 throw std::runtime_error(
"Given identifier cannot be loaded from the database: " + identifier);
292 std::stringstream ss(database_representation_of_weightfile.
m_data);