Belle II Software development
Utility Class Reference

Wrapper class for some utility functions. More...

#include <Utility.h>

Static Public Member Functions

static void download (const std::string &identifier, const std::string &filename, int experiment=0, int run=0, int event=0)
 Convenience function which downloads a given weightfile from the database.
 
static void upload (const std::string &filename, const std::string &identifier, int exp1=0, int run1=0, int exp2=-1, int run2=-1)
 Convenience function which uploads a given weightfile to the database.
 
static void upload_array (const std::vector< std::string > &filenames, const std::string &identifier, int exp1=0, int run1=0, int exp2=-1, int run2=-1)
 Convenience function which uploads an array of weightfiles to the database.
 
static bool available (const std::string &filename, int experiment=0, int run=0, int event=0)
 Convenience function which checks if an experise is available.
 
static void extract (const std::string &filename, const std::string &directory)
 Convenience function which extracts the expertise in a given weightfile into a temporary directory.
 
static std::string info (const std::string &filename)
 Print information about the classifier stored in the given weightfile.
 
static void save_custom_weightfile (const GeneralOptions &general_options, const SpecificOptions &specific_options, const std::string &custom_weightfile, const std::string &output_identifier="")
 Convenience function which saves a pre-existing weightfile in a mva package-compliant format.
 
static void teacher (const GeneralOptions &general_options, const SpecificOptions &specific_options, const MetaOptions &meta_options=MetaOptions())
 Convenience function which performs a training with the given options.
 
static void expert (const std::vector< std::string > &filenames, const std::vector< std::string > &datafiles, const std::string &treename, const std::string &outputfile, int experiment=0, int run=0, int event=0, bool copy_target=true)
 Convenience function applies experts on given data.
 
static std::unique_ptr< Belle2::MVA::Expertteacher_dataset (GeneralOptions general_options, const SpecificOptions &specific_options, Dataset &data)
 Convenience function which performs a training on a dataset.
 
static std::unique_ptr< Belle2::MVA::Expertteacher_splot (const GeneralOptions &general_options, const SpecificOptions &specific_options, const MetaOptions &meta_options)
 Performs an splot training, convenience function.
 
static std::unique_ptr< Belle2::MVA::Expertteacher_sideband_subtraction (const GeneralOptions &general_options, const SpecificOptions &specific_options, const MetaOptions &meta_options)
 Performs a sideband subtraction training, convenience function.
 
static std::unique_ptr< Belle2::MVA::Expertteacher_reweighting (const GeneralOptions &general_options, const SpecificOptions &specific_options, const MetaOptions &meta_options)
 Performs a MC vs data pre-training and afterwards reweighted training, convenience function.
 

Detailed Description

Wrapper class for some utility functions.

Definition at line 20 of file Utility.h.

Member Function Documentation

◆ available()

bool available ( const std::string & filename,
int experiment = 0,
int run = 0,
int event = 0 )
static

Convenience function which checks if an experise is available.

Parameters
filenamefilename or identifier of the expertise
experimentcurrent experiment
runcurrent run
eventcurrent event

Definition at line 125 of file Utility.cc.

126{
127
128 try {
129 auto weightfile = Weightfile::load(filename, Belle2::EventMetaData(event, run, experiment));
130 return true;
131 } catch (...) {
132 return false;
133 }
134
135}
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.

◆ download()

void download ( const std::string & identifier,
const std::string & filename,
int experiment = 0,
int run = 0,
int event = 0 )
static

Convenience function which downloads a given weightfile from the database.

Parameters
identifieridentifier in the database
filenameof the weightfile
experimentcurrent experiment
runcurrent run
eventcurrent event

Definition at line 31 of file Utility.cc.

32{
33 Belle2::EventMetaData emd(event, run, experiment);
34 Belle2::MVA::Weightfile weightfile = Belle2::MVA::Weightfile::loadFromDatabase(identifier, emd);
35 if (filename.ends_with(".root")) {
36 Belle2::MVA::Weightfile::saveToROOTFile(weightfile, filename);
37 } else if (filename.ends_with(".xml")) {
38 Belle2::MVA::Weightfile::saveToXMLFile(weightfile, filename);
39 } else {
40 std::cerr << "Unknown file extension, fallback to xml" << std::endl;
41 Belle2::MVA::Weightfile::saveToXMLFile(weightfile, filename);
42 }
43}
static void saveToXMLFile(Weightfile &weightfile, const std::string &filename)
Static function which saves a Weightfile to a XML file.
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.
static void saveToROOTFile(Weightfile &weightfile, const std::string &filename)
Static function which saves a Weightfile to a ROOT file.

◆ expert()

void expert ( const std::vector< std::string > & filenames,
const std::vector< std::string > & datafiles,
const std::string & treename,
const std::string & outputfile,
int experiment = 0,
int run = 0,
int event = 0,
bool copy_target = true )
static

Convenience function applies experts on given data.

Parameters
filenamesvector of filenames or database identifiers
datafilesROOT files containing the data
treenametreename of ROOT file
outputfilename of the output ROOT file
experimentnumber of the experiment
runnumber of the run
eventnumber of the event
copy_targetdefine if the target variable should be copied, if no target is found, an exception will be raised

Definition at line 137 of file Utility.cc.

140{
141
142 TFile file(outputfile.c_str(), "RECREATE");
143 file.cd();
144 TTree tree("variables", "variables");
145
147 auto supported_interfaces = AbstractInterface::getSupportedInterfaces();
148
149 for (auto& filename : filenames) {
150
151 Belle2::EventMetaData emd(event, run, experiment);
152 auto weightfile = Weightfile::load(filename, emd);
153
154 GeneralOptions general_options;
155 weightfile.getOptions(general_options);
156
157 general_options.m_treename = treename;
158 // Override possible restriction of number of events in training
159 // otherwise this would apply to the expert as well.
160 general_options.m_max_events = 0;
161
162 auto expertLocal = supported_interfaces[general_options.m_method]->getExpert();
163 expertLocal->load(weightfile);
164
165 bool isMulticlass = general_options.m_nClasses > 2;
166
167 // define if target variables should be copied
168 if (not copy_target) {
169 general_options.m_target_variable = std::string();
170 }
171
172 general_options.m_datafiles = datafiles;
173 ROOTDataset data(general_options);
174
175 std::vector<TBranch*> branches;
176 //create the output branches
177 if (not isMulticlass) {
178 float result = 0;
179 std::string branchname = Belle2::MakeROOTCompatible::makeROOTCompatible(filename);
180 branches.push_back(tree.Branch(branchname.c_str(), &result, (branchname + "/F").c_str()));
181 std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
182
183 auto results = expertLocal->apply(data);
184 std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now();
185 std::chrono::duration<double, std::milli> application_time = stop - start;
186 B2INFO("Elapsed application time in ms " << application_time.count() << " for " << general_options.m_identifier);
187 for (auto& r : results) {
188 result = r;
189 branches[0]->Fill();
190 }
191
192 } else {
193 float result = 0;
194 for (unsigned int iClass = 0; iClass < general_options.m_nClasses; ++iClass) {
195 std::string branchname = Belle2::MakeROOTCompatible::makeROOTCompatible(filename + "_" + std::to_string(iClass));
196 branches.push_back(tree.Branch(branchname.c_str(), &result, (branchname + "/F").c_str()));
197 }
198 std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
199 auto results = expertLocal->applyMulticlass(data);
200 std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now();
201 std::chrono::duration<double, std::milli> application_time = stop - start;
202 B2INFO("Elapsed application time in ms " << application_time.count() << " for " << general_options.m_identifier);
203 for (auto& r : results) {
204 for (unsigned int iClass = 0; iClass < general_options.m_nClasses; ++iClass) {
205 result = r[iClass];
206 branches[iClass]->Fill();
207 }
208 }
209
210 }
211
212
213 if (not general_options.m_target_variable.empty()) {
214 std::string branchname = Belle2::MakeROOTCompatible::makeROOTCompatible(filename + "_" +
215 general_options.m_target_variable);
216 float target = 0;
217 auto target_branch = tree.Branch(branchname.c_str(), &target, (branchname + "/F").c_str());
218 auto targets = data.getTargets();
219 for (auto& t : targets) {
220 target = t;
221 target_branch->Fill();
222 }
223 }
224 }
225
226 tree.SetEntries();
227 file.Write("variables");
228
229}
static void initSupportedInterfaces()
Static function which initializes all supported interfaces, has to be called once before getSupported...
Definition Interface.cc:46
static std::map< std::string, AbstractInterface * > getSupportedInterfaces()
Returns interfaces supported by the MVA Interface.
Definition Interface.h:53
static std::string makeROOTCompatible(std::string str)
Remove special characters that ROOT dislikes in branch names, e.g.

◆ extract()

void extract ( const std::string & filename,
const std::string & directory )
static

Convenience function which extracts the expertise in a given weightfile into a temporary directory.

Parameters
filenameof the weightfile
directorytemporary directory to use

Definition at line 82 of file Utility.cc.

83{
84
86 auto supported_interfaces = AbstractInterface::getSupportedInterfaces();
87 auto weightfile = Weightfile::load(filename);
88 weightfile.setRemoveTemporaryDirectories(false);
89 setenv("TMPDIR", directory.c_str(), 1);
90 GeneralOptions general_options;
91 weightfile.getOptions(general_options);
92 auto expertLocal = supported_interfaces[general_options.m_method]->getExpert();
93 expertLocal->load(weightfile);
94
95}

◆ info()

std::string info ( const std::string & filename)
static

Print information about the classifier stored in the given weightfile.

Parameters
filenamefilename of the weightfile

Definition at line 97 of file Utility.cc.

98{
99
101 auto supported_interfaces = AbstractInterface::getSupportedInterfaces();
102 auto weightfile = Weightfile::load(filename);
103 GeneralOptions general_options;
104 weightfile.getOptions(general_options);
105
106 auto specific_options = supported_interfaces[general_options.m_method]->getOptions();
107 specific_options->load(weightfile.getXMLTree());
108
109 boost::property_tree::ptree temp_tree;
110 general_options.save(temp_tree);
111 specific_options->save(temp_tree);
112 std::ostringstream oss;
113
114#if BOOST_VERSION < 105600
115 boost::property_tree::xml_writer_settings<char> settings('\t', 1);
116#else
117 boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
118#endif
119 boost::property_tree::xml_parser::write_xml(oss, temp_tree, settings);;
120
121 return oss.str();
122
123}

◆ save_custom_weightfile()

void save_custom_weightfile ( const GeneralOptions & general_options,
const SpecificOptions & specific_options,
const std::string & custom_weightfile,
const std::string & output_identifier = "" )
static

Convenience function which saves a pre-existing weightfile in a mva package-compliant format.

Parameters
general_optionsshared options
specific_optionsmethod specific options
custom_weightfilepath to the pre-existing weightfile
output_identifieran optional string to append to the output file name. By default the function overwrites the input file.

Definition at line 231 of file Utility.cc.

233{
234 std::ifstream ifile(custom_weightfile);
235 if (!(bool)ifile) {
236 B2FATAL("Input weight file: " << custom_weightfile << " does not exist!");
237 }
238
239 Weightfile weightfile;
240 weightfile.addOptions(general_options);
241 weightfile.addOptions(specific_options);
242 weightfile.addFile(general_options.m_identifier + "_Weightfile", custom_weightfile);
243 std::string output_weightfile(custom_weightfile);
244 if (!output_identifier.empty()) {
245 std::regex to_replace("(\\.\\S+$)");
246 std::string replacement = "_" + output_identifier + "$0";
247 output_weightfile = std::regex_replace(output_weightfile, to_replace, replacement);
248 }
249 Weightfile::save(weightfile, output_weightfile);
250}
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.

◆ teacher()

void teacher ( const GeneralOptions & general_options,
const SpecificOptions & specific_options,
const MetaOptions & meta_options = MetaOptions() )
static

Convenience function which performs a training with the given options.

Parameters
general_optionsshared options
specific_optionsmethod specific options
meta_optionsoptional options

Definition at line 252 of file Utility.cc.

254{
255 unsigned int number_of_enabled_meta_trainings = 0;
256 if (meta_options.m_use_splot)
257 number_of_enabled_meta_trainings++;
258 if (meta_options.m_use_sideband_subtraction)
259 number_of_enabled_meta_trainings++;
260 if (meta_options.m_use_reweighting)
261 number_of_enabled_meta_trainings++;
262
263 if (number_of_enabled_meta_trainings > 1) {
264 B2ERROR("You enabled more than one meta training option. You can only use one (sPlot, SidebandSubstraction or Reweighting)");
265 return;
266 }
267
268 if (meta_options.m_use_splot) {
269 teacher_splot(general_options, specific_options, meta_options);
270 } else if (meta_options.m_use_sideband_subtraction) {
271 teacher_sideband_subtraction(general_options, specific_options, meta_options);
272 } else if (meta_options.m_use_reweighting) {
273 teacher_reweighting(general_options, specific_options, meta_options);
274 } else {
275 ROOTDataset data(general_options);
276 teacher_dataset(general_options, specific_options, data);
277 }
278}
static std::unique_ptr< Belle2::MVA::Expert > teacher_sideband_subtraction(const GeneralOptions &general_options, const SpecificOptions &specific_options, const MetaOptions &meta_options)
Performs a sideband subtraction training, convenience function.
Definition Utility.cc:476
static std::unique_ptr< Belle2::MVA::Expert > teacher_reweighting(const GeneralOptions &general_options, const SpecificOptions &specific_options, const MetaOptions &meta_options)
Performs a MC vs data pre-training and afterwards reweighted training, convenience function.
Definition Utility.cc:423
static std::unique_ptr< Belle2::MVA::Expert > teacher_dataset(GeneralOptions general_options, const SpecificOptions &specific_options, Dataset &data)
Convenience function which performs a training on a dataset.
Definition Utility.cc:281
static std::unique_ptr< Belle2::MVA::Expert > teacher_splot(const GeneralOptions &general_options, const SpecificOptions &specific_options, const MetaOptions &meta_options)
Performs an splot training, convenience function.
Definition Utility.cc:312

◆ teacher_dataset()

std::unique_ptr< Belle2::MVA::Expert > teacher_dataset ( GeneralOptions general_options,
const SpecificOptions & specific_options,
Dataset & data )
static

Convenience function which performs a training on a dataset.

Parameters
general_optionsshared options
specific_optionsmethod specific options
datadata to use

Definition at line 281 of file Utility.cc.

284{
285 if (general_options.m_method.empty()) {
286 general_options.m_method = specific_options.getMethod();
287 } else {
288 if (general_options.m_method != specific_options.getMethod()) {
289 B2ERROR("The method specified in the general options is in conflict with the provided specific option:" << general_options.m_method
290 << " " << specific_options.getMethod());
291 }
292 }
294 auto supported_interfaces = AbstractInterface::getSupportedInterfaces();
295 if (supported_interfaces.find(general_options.m_method) != supported_interfaces.end()) {
296 auto teacherLocal = supported_interfaces[general_options.m_method]->getTeacher(general_options, specific_options);
297 std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
298 auto weightfile = teacherLocal->train(data);
299 std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now();
300 std::chrono::duration<double, std::milli> training_time = stop - start;
301 B2INFO("Elapsed training time in ms " << training_time.count() << " for " << general_options.m_identifier);
302 Weightfile::save(weightfile, general_options.m_identifier);
303 auto expertLocal = supported_interfaces[general_options.m_method]->getExpert();
304 expertLocal->load(weightfile);
305 return expertLocal;
306 } else {
307 B2ERROR("Interface doesn't support chosen method" << general_options.m_method);
308 throw std::runtime_error("Interface doesn't support chosen method" + general_options.m_method);
309 }
310}

◆ teacher_reweighting()

std::unique_ptr< Belle2::MVA::Expert > teacher_reweighting ( const GeneralOptions & general_options,
const SpecificOptions & specific_options,
const MetaOptions & meta_options )
static

Performs a MC vs data pre-training and afterwards reweighted training, convenience function.

Parameters
general_optionsshared options of all methods
specific_optionsof the used mva method
meta_optionsoptions defining the splot training

Definition at line 423 of file Utility.cc.

426{
427 if (std::find(general_options.m_variables.begin(), general_options.m_variables.end(),
428 meta_options.m_reweighting_variable) != general_options.m_variables.end()) {
429 B2ERROR("You cannot use the reweighting variable as a feature in your training");
430 return nullptr;
431 }
432
433 GeneralOptions data_general_options = general_options;
434 data_general_options.m_target_variable = "";
435 data_general_options.m_datafiles = meta_options.m_reweighting_data_files;
436 ROOTDataset data_dataset(data_general_options);
437
438 GeneralOptions mc_general_options = general_options;
439 mc_general_options.m_datafiles = meta_options.m_reweighting_mc_files;
440 ROOTDataset mc_dataset(mc_general_options);
441
442 CombinedDataset boost_dataset(general_options, data_dataset, mc_dataset);
443
444 GeneralOptions boost_general_options = general_options;
445 boost_general_options.m_identifier = general_options.m_identifier + "_boost.xml";
446 // cppcheck-suppress unreadVariable
447 auto boost_expert = teacher_dataset(boost_general_options, specific_options, boost_dataset);
448
449 GeneralOptions reweighter_general_options = general_options;
450 reweighter_general_options.m_identifier = meta_options.m_reweighting_identifier;
451 reweighter_general_options.m_method = "Reweighter";
452 ReweighterOptions reweighter_specific_options;
453 reweighter_specific_options.m_weightfile = boost_general_options.m_identifier;
454 reweighter_specific_options.m_variable = meta_options.m_reweighting_variable;
455
456 if (meta_options.m_reweighting_variable != "") {
457 if (std::find(reweighter_general_options.m_spectators.begin(), reweighter_general_options.m_spectators.end(),
458 meta_options.m_reweighting_variable) == reweighter_general_options.m_spectators.end() and
459 std::find(reweighter_general_options.m_variables.begin(), reweighter_general_options.m_variables.end(),
460 meta_options.m_reweighting_variable) == reweighter_general_options.m_variables.end() and
461 reweighter_general_options.m_target_variable != meta_options.m_reweighting_variable and
462 reweighter_general_options.m_weight_variable != meta_options.m_reweighting_variable) {
463 reweighter_general_options.m_spectators.push_back(meta_options.m_reweighting_variable);
464 }
465 }
466
467 ROOTDataset dataset(reweighter_general_options);
468 auto reweight_expert = teacher_dataset(reweighter_general_options, reweighter_specific_options, dataset);
469 auto weights = reweight_expert->apply(dataset);
470 ReweightingDataset reweighted_dataset(general_options, dataset, weights);
471 auto expertLocal = teacher_dataset(general_options, specific_options, reweighted_dataset);
472
473 return expertLocal;
474}
std::vector< std::string > m_datafiles
Name of the datafiles containing the training data.
Definition Options.h:84
std::vector< std::string > m_variables
Vector of all variables (branch names) used in the training.
Definition Options.h:86
std::string m_weight_variable
Weight variable (branch name) defining the weights.
Definition Options.h:91
std::vector< std::string > m_spectators
Vector of all spectators (branch names) used in the training.
Definition Options.h:87
std::string m_method
Name of the MVA method to use.
Definition Options.h:82
std::string m_target_variable
Target variable (branch name) defining the target.
Definition Options.h:90
std::string m_identifier
Identifier containing the finished training.
Definition Options.h:83
std::string m_weightfile
Weightfile of the reweighting expert.
Definition Reweighter.h:53
std::string m_variable
Variable which decides if the reweighter is applied or not.
Definition Reweighter.h:54

◆ teacher_sideband_subtraction()

std::unique_ptr< Belle2::MVA::Expert > teacher_sideband_subtraction ( const GeneralOptions & general_options,
const SpecificOptions & specific_options,
const MetaOptions & meta_options )
static

Performs a sideband subtraction training, convenience function.

Parameters
general_optionsshared options of all methods
specific_optionsof the used mva method
meta_optionsoptional options

Definition at line 476 of file Utility.cc.

479{
480
481 if (std::find(general_options.m_variables.begin(), general_options.m_variables.end(),
482 meta_options.m_sideband_variable) != general_options.m_variables.end()) {
483 B2ERROR("You cannot use the sideband variable as a feature in your training");
484 return nullptr;
485 }
486
487 GeneralOptions data_general_options = general_options;
488 if (std::find(data_general_options.m_spectators.begin(), data_general_options.m_spectators.end(),
489 meta_options.m_sideband_variable) == data_general_options.m_spectators.end()) {
490 data_general_options.m_spectators.push_back(meta_options.m_sideband_variable);
491 }
492 ROOTDataset data_dataset(data_general_options);
493
494 GeneralOptions mc_general_options = general_options;
495 mc_general_options.m_datafiles = meta_options.m_sideband_mc_files;
496 if (std::find(mc_general_options.m_spectators.begin(), mc_general_options.m_spectators.end(),
497 meta_options.m_sideband_variable) == mc_general_options.m_spectators.end()) {
498 mc_general_options.m_spectators.push_back(meta_options.m_sideband_variable);
499 }
500 ROOTDataset mc_dataset(mc_general_options);
501
502 GeneralOptions sideband_general_options = general_options;
503 SidebandDataset sideband_dataset(sideband_general_options, data_dataset, mc_dataset, meta_options.m_sideband_variable);
504 auto expertLocal = teacher_dataset(general_options, specific_options, sideband_dataset);
505
506 return expertLocal;
507}

◆ teacher_splot()

std::unique_ptr< Belle2::MVA::Expert > teacher_splot ( const GeneralOptions & general_options,
const SpecificOptions & specific_options,
const MetaOptions & meta_options )
static

Performs an splot training, convenience function.

Parameters
general_optionsshared options of all methods
specific_optionsof the used mva method
meta_optionsoptional options

Definition at line 312 of file Utility.cc.

315{
316
317 GeneralOptions data_general_options = general_options;
318 data_general_options.m_target_variable = "";
319 if (meta_options.m_splot_combined)
320 data_general_options.m_identifier = general_options.m_identifier + "_splot.xml";
321 ROOTDataset data_dataset(data_general_options);
322 // Reset target variable so that it shows up in the weightfile at the end
323 data_general_options.m_target_variable = general_options.m_target_variable;
324
325 GeneralOptions discriminant_general_options = general_options;
326 discriminant_general_options.m_target_variable = "";
327 discriminant_general_options.m_variables = {meta_options.m_splot_variable};
328 ROOTDataset discriminant_dataset(discriminant_general_options);
329 // Reset target variable so that it shows up in the weightfile at the end
330 discriminant_general_options.m_target_variable = general_options.m_target_variable;
331
332 GeneralOptions mc_general_options = general_options;
333 mc_general_options.m_datafiles = meta_options.m_splot_mc_files;
334 mc_general_options.m_variables = {meta_options.m_splot_variable};
335 ROOTDataset mc_dataset(mc_general_options);
336
337 auto mc_signals = mc_dataset.getSignals();
338 auto mc_weights = mc_dataset.getWeights();
339 auto mc_feature = mc_dataset.getFeature(0);
340 auto data_feature = discriminant_dataset.getFeature(0);
341 auto data_weights = discriminant_dataset.getWeights();
342
343 Binning binning = Binning::CreateEqualFrequency(mc_feature, mc_weights, mc_signals, 100);
344
345 float signalFraction = binning.m_signal_yield / (binning.m_signal_yield + binning.m_bckgrd_yield);
346
347 std::vector<double> data(100, 0);
348 double total_data = 0.0;
349 for (unsigned int iEvent = 0; iEvent < data_dataset.getNumberOfEvents(); ++iEvent) {
350 data[binning.getBin(data_feature[iEvent])] += data_weights[iEvent];
351 total_data += data_weights[iEvent];
352 }
353
354 // We do a simple fit here to estimate the signal and background yields
355 // We could use RooFit here to avoid using custom code,
356 // but I found RooFit to be difficult and unstable ...
357
358 float best_yield = 0.0;
359 double best_chi2 = 1000000000.0;
360 bool empty_bin = false;
361 for (double yield = 0; yield < total_data; yield += 1) {
362 double chi2 = 0.0;
363 for (unsigned int iBin = 0; iBin < 100; ++iBin) {
364 double deviation = (data[iBin] - (yield * binning.m_signal_pdf[iBin] + (total_data - yield) * binning.m_bckgrd_pdf[iBin]) *
365 (binning.m_boundaries[iBin + 1] - binning.m_boundaries[iBin]) / (binning.m_boundaries[100] - binning.m_boundaries[0]));
366 if (data[iBin] > 0)
367 chi2 += deviation * deviation / data[iBin];
368 else
369 empty_bin = true;
370 }
371 if (chi2 < best_chi2) {
372 best_chi2 = chi2;
373 best_yield = yield;
374 }
375 }
376
377 if (empty_bin) {
378 B2WARNING("Encountered empty bin in data histogram during fit of the components for sPlot");
379 }
380
381 B2INFO("sPlot best yield " << best_yield);
382 B2INFO("sPlot Yields On MC " << binning.m_signal_yield << " " << binning.m_bckgrd_yield);
383
384 binning.m_signal_yield = best_yield;
385 binning.m_bckgrd_yield = (total_data - best_yield);
386
387 B2INFO("sPlot Yields Fitted On Data " << binning.m_signal_yield << " " << binning.m_bckgrd_yield);
388
389 if (meta_options.m_splot_boosted) {
390 GeneralOptions boost_general_options = data_general_options;
391 boost_general_options.m_identifier = general_options.m_identifier + "_boost.xml";
392 SPlotDataset splot_dataset(boost_general_options, data_dataset, getBoostWeights(discriminant_dataset, binning), signalFraction);
393 auto boost_expert = teacher_dataset(boost_general_options, specific_options, splot_dataset);
394
395 SPlotDataset aplot_dataset(data_general_options, data_dataset, getAPlotWeights(discriminant_dataset, binning,
396 boost_expert->apply(data_dataset)), signalFraction);
397 auto splot_expert = teacher_dataset(data_general_options, specific_options, aplot_dataset);
398 if (not meta_options.m_splot_combined)
399 return splot_expert;
400 } else {
401 SPlotDataset splot_dataset(data_general_options, data_dataset, getSPlotWeights(discriminant_dataset, binning), signalFraction);
402 auto splot_expert = teacher_dataset(data_general_options, specific_options, splot_dataset);
403 if (not meta_options.m_splot_combined)
404 return splot_expert;
405 }
406
407 mc_general_options.m_identifier = general_options.m_identifier + "_pdf.xml";
408 mc_general_options.m_method = "PDF";
409 PDFOptions pdf_options;
410 // cppcheck-suppress unreadVariable
411 auto pdf_expert = teacher_dataset(mc_general_options, pdf_options, mc_dataset);
412
413 GeneralOptions combination_general_options = general_options;
414 combination_general_options.m_method = "Combination";
415 combination_general_options.m_variables.push_back(meta_options.m_splot_variable);
416 CombinationOptions combination_options;
417 combination_options.m_weightfiles = {data_general_options.m_identifier, mc_general_options.m_identifier};
418 auto combination_expert = teacher_dataset(combination_general_options, combination_options, data_dataset);
419
420 return combination_expert;
421}
std::vector< float > m_bckgrd_pdf
Background pdf of data distribution per bin.
Definition Binning.h:58
std::vector< float > m_signal_pdf
Signal pdf of data distribution per bin.
Definition Binning.h:56
std::vector< float > m_boundaries
Boundaries of data distribution, including minimum and maximum value as first and last boundary.
Definition Binning.h:61
double m_bckgrd_yield
Background yield in data distribution.
Definition Binning.h:54
double m_signal_yield
Signal yield in data distribution.
Definition Binning.h:53
unsigned int getBin(float datapoint) const
Gets the bin corresponding to the given datapoint.
Definition Binning.cc:34
std::vector< std::string > m_weightfiles
Weightfiles of all methods we want to combine.
Definition Combination.h:53
std::vector< std::pair< double, double > > Binning
Bin holder as vector for bin limit pairs: [energy limits, theta limits, phi limits].

◆ upload()

void upload ( const std::string & filename,
const std::string & identifier,
int exp1 = 0,
int run1 = 0,
int exp2 = -1,
int run2 = -1 )
static

Convenience function which uploads a given weightfile to the database.

Parameters
filenameof the weightfile
identifieridentifier in the database
exp1first valid experiment
run1first valid run
exp2last valid experiment
run2last valid run

Definition at line 45 of file Utility.cc.

46{
47 Belle2::IntervalOfValidity iov(exp1, run1, exp2, run2);
48 Belle2::MVA::Weightfile weightfile;
49 if (filename.ends_with(".root")) {
50 weightfile = Belle2::MVA::Weightfile::loadFromROOTFile(filename);
51 } else if (filename.ends_with(".xml")) {
52 weightfile = Belle2::MVA::Weightfile::loadFromXMLFile(filename);
53 } else {
54 std::cerr << "Unknown file extension, fallback to xml" << std::endl;
55 weightfile = Belle2::MVA::Weightfile::loadFromXMLFile(filename);
56 }
57 Belle2::MVA::Weightfile::saveToDatabase(weightfile, identifier, iov);
58}
static Weightfile loadFromXMLFile(const std::string &filename)
Static function which loads a Weightfile from a XML file.
static Weightfile loadFromROOTFile(const std::string &filename)
Static function which loads a Weightfile from a ROOT file.
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.

◆ upload_array()

void upload_array ( const std::vector< std::string > & filenames,
const std::string & identifier,
int exp1 = 0,
int run1 = 0,
int exp2 = -1,
int run2 = -1 )
static

Convenience function which uploads an array of weightfiles to the database.

Parameters
filenamesarray of names of the weightfiles
identifieridentifier in the database
exp1first valid experiment
run1first valid run
exp2last valid experiment
run2last valid run

Definition at line 60 of file Utility.cc.

62{
63 Belle2::IntervalOfValidity iov(exp1, run1, exp2, run2);
64
65 std::vector<Belle2::MVA::Weightfile> weightfiles;
66 for (const auto& filename : filenames) {
67
68 Belle2::MVA::Weightfile weightfile;
69 if (filename.ends_with(".root")) {
70 weightfile = Belle2::MVA::Weightfile::loadFromROOTFile(filename);
71 } else if (filename.ends_with(".xml")) {
72 weightfile = Belle2::MVA::Weightfile::loadFromXMLFile(filename);
73 } else {
74 std::cerr << "Unknown file extension, fallback to xml" << std::endl;
75 weightfile = Belle2::MVA::Weightfile::loadFromXMLFile(filename);
76 }
77 weightfiles.push_back(weightfile);
78 }
79 Belle2::MVA::Weightfile::saveArrayToDatabase(weightfiles, identifier, iov);
80}
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.

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