Belle II Software  light-2212-foldex
Utility.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <mva/utility/Utility.h>
10 #include <mva/utility/DataDriven.h>
11 #include <mva/methods/PDF.h>
12 #include <mva/methods/Reweighter.h>
13 #include <mva/methods/Trivial.h>
14 #include <mva/methods/Combination.h>
15 
16 #include <framework/logging/Logger.h>
17 
18 #include <framework/utilities/MakeROOTCompatible.h>
19 
20 #include <boost/algorithm/string/predicate.hpp>
21 #include <boost/property_tree/xml_parser.hpp>
22 
23 #include <iostream>
24 #include <chrono>
25 #include <string>
26 #include <regex>
27 #include <fstream>
28 
29 using namespace Belle2::MVA;
30 
31 void Utility::download(const std::string& identifier, const std::string& filename, int experiment, int run, int event)
32 {
33  Belle2::EventMetaData emd(event, run, experiment);
35  if (boost::ends_with(filename, ".root")) {
36  Belle2::MVA::Weightfile::saveToROOTFile(weightfile, filename);
37  } else if (boost::ends_with(filename, ".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 }
44 
45 void Utility::upload(const std::string& filename, const std::string& identifier, int exp1, int run1, int exp2, int run2)
46 {
47  Belle2::IntervalOfValidity iov(exp1, run1, exp2, run2);
48  Belle2::MVA::Weightfile weightfile;
49  if (boost::ends_with(filename, ".root")) {
50  weightfile = Belle2::MVA::Weightfile::loadFromROOTFile(filename);
51  } else if (boost::ends_with(filename, ".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 }
59 
60 void Utility::upload_array(const std::vector<std::string>& filenames, const std::string& identifier, int exp1, int run1, int exp2,
61  int run2)
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 (boost::ends_with(filename, ".root")) {
70  weightfile = Belle2::MVA::Weightfile::loadFromROOTFile(filename);
71  } else if (boost::ends_with(filename, ".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 }
81 
82 void Utility::extract(const std::string& filename, const std::string& directory)
83 {
84 
86  auto supported_interfaces = AbstractInterface::getSupportedInterfaces();
87  auto weightfile = Weightfile::load(filename);
88  weightfile.setRemoveTemporaryDirectories(false);
89  weightfile.setTemporaryDirectory(directory);
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 }
96 
97 std::string Utility::info(const std::string& filename)
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 }
124 
125 bool Utility::available(const std::string& filename, int experiment, int run, int event)
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 }
136 
137 void Utility::expert(const std::vector<std::string>& filenames, const std::vector<std::string>& datafiles,
138  const std::string& treename,
139  const std::string& outputfile, int experiment, int run, int event, bool copy_target)
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  unsigned int i = 0;
150  for (auto& filename : filenames) {
151 
152  Belle2::EventMetaData emd(event, run, experiment);
153  auto weightfile = Weightfile::load(filename, emd);
154 
155  GeneralOptions general_options;
156  weightfile.getOptions(general_options);
157 
158  general_options.m_treename = treename;
159  // Override possible restriction of number of events in training
160  // otherwise this would apply to the expert as well.
161  general_options.m_max_events = 0;
162 
163  auto expertLocal = supported_interfaces[general_options.m_method]->getExpert();
164  expertLocal->load(weightfile);
165 
166  bool isMulticlass = general_options.m_nClasses > 2;
167 
168  // define if target variables should be copied
169  if (not copy_target) {
170  general_options.m_target_variable = std::string();
171  }
172 
173  general_options.m_datafiles = datafiles;
174  ROOTDataset data(general_options);
175 
176  std::vector<TBranch*> branches;
177  //create the output branches
178  if (not isMulticlass) {
179  float result = 0;
180  std::string branchname = Belle2::MakeROOTCompatible::makeROOTCompatible(filename);
181  branches.push_back(tree.Branch(branchname.c_str(), &result, (branchname + "/F").c_str()));
182  std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
183 
184  auto results = expertLocal->apply(data);
185  std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now();
186  std::chrono::duration<double, std::milli> application_time = stop - start;
187  B2INFO("Elapsed application time in ms " << application_time.count() << " for " << general_options.m_identifier);
188  for (auto& r : results) {
189  result = r;
190  branches[0]->Fill();
191  }
192 
193  } else {
194  float result = 0;
195  for (unsigned int iClass = 0; iClass < general_options.m_nClasses; ++iClass) {
196  std::string branchname = Belle2::MakeROOTCompatible::makeROOTCompatible(filename + "_" + std::to_string(iClass));
197  branches.push_back(tree.Branch(branchname.c_str(), &result, (branchname + "/F").c_str()));
198  }
199  std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
200  auto results = expertLocal->applyMulticlass(data);
201  std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now();
202  std::chrono::duration<double, std::milli> application_time = stop - start;
203  B2INFO("Elapsed application time in ms " << application_time.count() << " for " << general_options.m_identifier);
204  for (auto& r : results) {
205  for (unsigned int iClass = 0; iClass < general_options.m_nClasses; ++iClass) {
206  result = r[iClass];
207  branches[iClass]->Fill();
208  }
209  }
210 
211  }
212 
213 
214  if (not general_options.m_target_variable.empty()) {
215  std::string branchname = Belle2::MakeROOTCompatible::makeROOTCompatible(filename + "_" +
216  general_options.m_target_variable);
217  float target = 0;
218  auto target_branch = tree.Branch(branchname.c_str(), &target, (branchname + "/F").c_str());
219  auto targets = data.getTargets();
220  for (auto& t : targets) {
221  target = t;
222  target_branch->Fill();
223  }
224  }
225 
226  ++i;
227  }
228 
229  tree.SetEntries();
230  file.Write("variables");
231 
232 }
233 
234 void Utility::save_custom_weightfile(const GeneralOptions& general_options, const SpecificOptions& specific_options,
235  const std::string& custom_weightfile, const std::string& output_identifier)
236 {
237  std::ifstream ifile(custom_weightfile);
238  if (!(bool)ifile) {
239  B2FATAL("Input weight file: " << custom_weightfile << " does not exist!");
240  }
241 
242  Weightfile weightfile;
243  weightfile.addOptions(general_options);
244  weightfile.addOptions(specific_options);
245  weightfile.addFile(general_options.m_identifier + "_Weightfile", custom_weightfile);
246  std::string output_weightfile(custom_weightfile);
247  if (!output_identifier.empty()) {
248  std::regex to_replace("(\\.\\S+$)");
249  std::string replacement = "_" + output_identifier + "$0";
250  output_weightfile = std::regex_replace(output_weightfile, to_replace, replacement);
251  }
252  Weightfile::save(weightfile, output_weightfile);
253 }
254 
255 void Utility::teacher(const GeneralOptions& general_options, const SpecificOptions& specific_options,
256  const MetaOptions& meta_options)
257 {
258  unsigned int number_of_enabled_meta_trainings = 0;
259  if (meta_options.m_use_splot)
260  number_of_enabled_meta_trainings++;
261  if (meta_options.m_use_sideband_subtraction)
262  number_of_enabled_meta_trainings++;
263  if (meta_options.m_use_reweighting)
264  number_of_enabled_meta_trainings++;
265 
266  if (number_of_enabled_meta_trainings > 1) {
267  B2ERROR("You enabled more than one meta training option. You can only use one (sPlot, SidebandSubstraction or Reweighting)");
268  return;
269  }
270 
271  if (meta_options.m_use_splot) {
272  teacher_splot(general_options, specific_options, meta_options);
273  } else if (meta_options.m_use_sideband_subtraction) {
274  teacher_sideband_subtraction(general_options, specific_options, meta_options);
275  } else if (meta_options.m_use_reweighting) {
276  teacher_reweighting(general_options, specific_options, meta_options);
277  } else {
278  ROOTDataset data(general_options);
279  teacher_dataset(general_options, specific_options, data);
280  }
281 }
282 
283 
284 std::unique_ptr<Belle2::MVA::Expert> Utility::teacher_dataset(GeneralOptions general_options,
285  const SpecificOptions& specific_options,
286  Dataset& data)
287 {
288  if (general_options.m_method.empty()) {
289  general_options.m_method = specific_options.getMethod();
290  } else {
291  if (general_options.m_method != specific_options.getMethod()) {
292  B2ERROR("The method specified in the general options is in conflict with the provided specific option:" << general_options.m_method
293  << " " << specific_options.getMethod());
294  }
295  }
297  auto supported_interfaces = AbstractInterface::getSupportedInterfaces();
298  if (supported_interfaces.find(general_options.m_method) != supported_interfaces.end()) {
299  auto teacherLocal = supported_interfaces[general_options.m_method]->getTeacher(general_options, specific_options);
300  std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
301  auto weightfile = teacherLocal->train(data);
302  std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now();
303  std::chrono::duration<double, std::milli> training_time = stop - start;
304  B2INFO("Elapsed training time in ms " << training_time.count() << " for " << general_options.m_identifier);
305  Weightfile::save(weightfile, general_options.m_identifier);
306  auto expertLocal = supported_interfaces[general_options.m_method]->getExpert();
307  expertLocal->load(weightfile);
308  return expertLocal;
309  } else {
310  B2ERROR("Interface doesn't support chosen method" << general_options.m_method);
311  throw std::runtime_error("Interface doesn't support chosen method" + general_options.m_method);
312  }
313 }
314 
315 std::unique_ptr<Belle2::MVA::Expert> Utility::teacher_splot(const GeneralOptions& general_options,
316  const SpecificOptions& specific_options,
317  const MetaOptions& meta_options)
318 {
319 
320  GeneralOptions data_general_options = general_options;
321  data_general_options.m_target_variable = "";
322  if (meta_options.m_splot_combined)
323  data_general_options.m_identifier = general_options.m_identifier + "_splot.xml";
324  ROOTDataset data_dataset(data_general_options);
325  // Reset target variable so that it shows up in the weightfile at the end
326  data_general_options.m_target_variable = general_options.m_target_variable;
327 
328  GeneralOptions discriminant_general_options = general_options;
329  discriminant_general_options.m_target_variable = "";
330  discriminant_general_options.m_variables = {meta_options.m_splot_variable};
331  ROOTDataset discriminant_dataset(discriminant_general_options);
332  // Reset target variable so that it shows up in the weightfile at the end
333  discriminant_general_options.m_target_variable = general_options.m_target_variable;
334 
335  GeneralOptions mc_general_options = general_options;
336  mc_general_options.m_datafiles = meta_options.m_splot_mc_files;
337  mc_general_options.m_variables = {meta_options.m_splot_variable};
338  ROOTDataset mc_dataset(mc_general_options);
339 
340  auto mc_signals = mc_dataset.getSignals();
341  auto mc_weights = mc_dataset.getWeights();
342  auto mc_feature = mc_dataset.getFeature(0);
343  auto data_feature = discriminant_dataset.getFeature(0);
344  auto data_weights = discriminant_dataset.getWeights();
345 
346  Binning binning = Binning::CreateEqualFrequency(mc_feature, mc_weights, mc_signals, 100);
347 
348  float signalFraction = binning.m_signal_yield / (binning.m_signal_yield + binning.m_bckgrd_yield);
349 
350  std::vector<double> data(100, 0);
351  double total_data = 0.0;
352  for (unsigned int iEvent = 0; iEvent < data_dataset.getNumberOfEvents(); ++iEvent) {
353  data[binning.getBin(data_feature[iEvent])] += data_weights[iEvent];
354  total_data += data_weights[iEvent];
355  }
356 
357  // We do a simple fit here to estimate the signal and background yields
358  // We could use RooFit here to avoid using custom code,
359  // but I found RooFit to be difficult and unstable ...
360 
361  float best_yield = 0.0;
362  double best_chi2 = 1000000000.0;
363  bool empty_bin = false;
364  for (double yield = 0; yield < total_data; yield += 1) {
365  double chi2 = 0.0;
366  for (unsigned int iBin = 0; iBin < 100; ++iBin) {
367  double deviation = (data[iBin] - (yield * binning.m_signal_pdf[iBin] + (total_data - yield) * binning.m_bckgrd_pdf[iBin]) *
368  (binning.m_boundaries[iBin + 1] - binning.m_boundaries[iBin]) / (binning.m_boundaries[100] - binning.m_boundaries[0]));
369  if (data[iBin] > 0)
370  chi2 += deviation * deviation / data[iBin];
371  else
372  empty_bin = true;
373  }
374  if (chi2 < best_chi2) {
375  best_chi2 = chi2;
376  best_yield = yield;
377  }
378  }
379 
380  if (empty_bin) {
381  B2WARNING("Encountered empty bin in data histogram during fit of the components for sPlot");
382  }
383 
384  B2INFO("sPlot best yield " << best_yield);
385  B2INFO("sPlot Yields On MC " << binning.m_signal_yield << " " << binning.m_bckgrd_yield);
386 
387  binning.m_signal_yield = best_yield;
388  binning.m_bckgrd_yield = (total_data - best_yield);
389 
390  B2INFO("sPlot Yields Fitted On Data " << binning.m_signal_yield << " " << binning.m_bckgrd_yield);
391 
392  if (meta_options.m_splot_boosted) {
393  GeneralOptions boost_general_options = data_general_options;
394  boost_general_options.m_identifier = general_options.m_identifier + "_boost.xml";
395  SPlotDataset splot_dataset(boost_general_options, data_dataset, getBoostWeights(discriminant_dataset, binning), signalFraction);
396  auto boost_expert = teacher_dataset(boost_general_options, specific_options, splot_dataset);
397 
398  SPlotDataset aplot_dataset(data_general_options, data_dataset, getAPlotWeights(discriminant_dataset, binning,
399  boost_expert->apply(data_dataset)), signalFraction);
400  auto splot_expert = teacher_dataset(data_general_options, specific_options, aplot_dataset);
401  if (not meta_options.m_splot_combined)
402  return splot_expert;
403  } else {
404  SPlotDataset splot_dataset(data_general_options, data_dataset, getSPlotWeights(discriminant_dataset, binning), signalFraction);
405  auto splot_expert = teacher_dataset(data_general_options, specific_options, splot_dataset);
406  if (not meta_options.m_splot_combined)
407  return splot_expert;
408  }
409 
410  mc_general_options.m_identifier = general_options.m_identifier + "_pdf.xml";
411  mc_general_options.m_method = "PDF";
412  PDFOptions pdf_options;
413  // cppcheck-suppress unreadVariable
414  auto pdf_expert = teacher_dataset(mc_general_options, pdf_options, mc_dataset);
415 
416  GeneralOptions combination_general_options = general_options;
417  combination_general_options.m_method = "Combination";
418  combination_general_options.m_variables.push_back(meta_options.m_splot_variable);
419  CombinationOptions combination_options;
420  combination_options.m_weightfiles = {data_general_options.m_identifier, mc_general_options.m_identifier};
421  auto combination_expert = teacher_dataset(combination_general_options, combination_options, data_dataset);
422 
423  return combination_expert;
424 }
425 
426 std::unique_ptr<Belle2::MVA::Expert> Utility::teacher_reweighting(const GeneralOptions& general_options,
427  const SpecificOptions& specific_options,
428  const MetaOptions& meta_options)
429 {
430  if (std::find(general_options.m_variables.begin(), general_options.m_variables.end(),
431  meta_options.m_reweighting_variable) != general_options.m_variables.end()) {
432  B2ERROR("You cannot use the reweighting variable as a feature in your training");
433  return nullptr;
434  }
435 
436  GeneralOptions data_general_options = general_options;
437  data_general_options.m_target_variable = "";
438  data_general_options.m_datafiles = meta_options.m_reweighting_data_files;
439  ROOTDataset data_dataset(data_general_options);
440 
441  GeneralOptions mc_general_options = general_options;
442  mc_general_options.m_datafiles = meta_options.m_reweighting_mc_files;
443  ROOTDataset mc_dataset(mc_general_options);
444 
445  CombinedDataset boost_dataset(general_options, data_dataset, mc_dataset);
446 
447  GeneralOptions boost_general_options = general_options;
448  boost_general_options.m_identifier = general_options.m_identifier + "_boost.xml";
449  // cppcheck-suppress unreadVariable
450  auto boost_expert = teacher_dataset(boost_general_options, specific_options, boost_dataset);
451 
452  GeneralOptions reweighter_general_options = general_options;
453  reweighter_general_options.m_identifier = meta_options.m_reweighting_identifier;
454  reweighter_general_options.m_method = "Reweighter";
455  ReweighterOptions reweighter_specific_options;
456  reweighter_specific_options.m_weightfile = boost_general_options.m_identifier;
457  reweighter_specific_options.m_variable = meta_options.m_reweighting_variable;
458 
459  if (meta_options.m_reweighting_variable != "") {
460  if (std::find(reweighter_general_options.m_spectators.begin(), reweighter_general_options.m_spectators.end(),
461  meta_options.m_reweighting_variable) == reweighter_general_options.m_spectators.end() and
462  std::find(reweighter_general_options.m_variables.begin(), reweighter_general_options.m_variables.end(),
463  meta_options.m_reweighting_variable) == reweighter_general_options.m_variables.end() and
464  reweighter_general_options.m_target_variable != meta_options.m_reweighting_variable and
465  reweighter_general_options.m_weight_variable != meta_options.m_reweighting_variable) {
466  reweighter_general_options.m_spectators.push_back(meta_options.m_reweighting_variable);
467  }
468  }
469 
470  ROOTDataset dataset(reweighter_general_options);
471  auto reweight_expert = teacher_dataset(reweighter_general_options, reweighter_specific_options, dataset);
472  auto weights = reweight_expert->apply(dataset);
473  ReweightingDataset reweighted_dataset(general_options, dataset, weights);
474  auto expertLocal = teacher_dataset(general_options, specific_options, reweighted_dataset);
475 
476  return expertLocal;
477 }
478 
479 std::unique_ptr<Belle2::MVA::Expert> Utility::teacher_sideband_subtraction(const GeneralOptions& general_options,
480  const SpecificOptions& specific_options,
481  const MetaOptions& meta_options)
482 {
483 
484  if (std::find(general_options.m_variables.begin(), general_options.m_variables.end(),
485  meta_options.m_sideband_variable) != general_options.m_variables.end()) {
486  B2ERROR("You cannot use the sideband variable as a feature in your training");
487  return nullptr;
488  }
489 
490  GeneralOptions data_general_options = general_options;
491  if (std::find(data_general_options.m_spectators.begin(), data_general_options.m_spectators.end(),
492  meta_options.m_sideband_variable) == data_general_options.m_spectators.end()) {
493  data_general_options.m_spectators.push_back(meta_options.m_sideband_variable);
494  }
495  ROOTDataset data_dataset(data_general_options);
496 
497  GeneralOptions mc_general_options = general_options;
498  mc_general_options.m_datafiles = meta_options.m_sideband_mc_files;
499  if (std::find(mc_general_options.m_spectators.begin(), mc_general_options.m_spectators.end(),
500  meta_options.m_sideband_variable) == mc_general_options.m_spectators.end()) {
501  mc_general_options.m_spectators.push_back(meta_options.m_sideband_variable);
502  }
503  ROOTDataset mc_dataset(mc_general_options);
504 
505  GeneralOptions sideband_general_options = general_options;
506  SidebandDataset sideband_dataset(sideband_general_options, data_dataset, mc_dataset, meta_options.m_sideband_variable);
507  auto expertLocal = teacher_dataset(general_options, specific_options, sideband_dataset);
508 
509  return expertLocal;
510 }
Store event, run, and experiment numbers.
Definition: EventMetaData.h:33
A class that describes the interval of experiments/runs for which an object in the database is valid.
static std::map< std::string, AbstractInterface * > getSupportedInterfaces()
Returns interfaces supported by the MVA Interface.
Definition: Interface.h:53
static void initSupportedInterfaces()
Static function which initliazes all supported interfaces, has to be called once before getSupportedI...
Definition: Interface.cc:45
Binning of a data distribution Provides PDF and CDF values of the distribution per bin.
Definition: Binning.h:27
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
static Binning CreateEqualFrequency(const std::vector< float > &data, const std::vector< float > &weights, const std::vector< bool > &isSignal, unsigned int nBins)
Create an equal frequency (aka equal-statistics) binning.
Definition: Binning.cc:93
unsigned int getBin(float datapoint) const
Gets the bin corresponding to the given datapoint.
Definition: Binning.cc:34
Options for the Combination MVA method.
Definition: Combination.h:28
std::vector< std::string > m_weightfiles
Weightfiles of all methods we want to combine.
Definition: Combination.h:53
Wraps two other Datasets, one containing signal, the other background events Used by the reweighting ...
Definition: Dataset.h:294
Abstract base class of all Datasets given to the MVA interface The current event can always be access...
Definition: Dataset.h:33
virtual std::vector< bool > getSignals()
Returns all is Signals.
Definition: Dataset.cc:122
General options which are shared by all MVA trainings.
Definition: Options.h:62
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
Meta Options which modify the underlying training by doing sPlot, Multiclass and HyperparameterSearch...
Definition: Options.h:111
Options for the PDF MVA method.
Definition: PDF.h:29
Proivdes a dataset from a ROOT file This is the usually used dataset providing training data to the m...
Definition: Dataset.h:349
virtual unsigned int getNumberOfEvents() const override
Returns the number of events in this dataset.
Definition: Dataset.h:371
virtual std::vector< float > getFeature(unsigned int iFeature) override
Returns all values of one feature in a std::vector<float>
Definition: Dataset.cc:443
virtual std::vector< float > getWeights() override
Returns all values of of the weights in a std::vector<float>
Definition: Dataset.cc:415
Options for the Reweighter MVA method.
Definition: Reweighter.h:28
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
Dataset for Reweighting Wraps a dataset and provides each data-point with a new weight.
Definition: DataDriven.h:29
Dataset for sPlot Wraps a dataset and provides each data-point twice, once as signal and once as back...
Definition: DataDriven.h:161
Dataset for Sideband Subtraction Wraps a dataset and provides each data-point with a new weight.
Definition: DataDriven.h:104
Specific Options, all method Options have to inherit from this class.
Definition: Options.h:98
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.
Definition: Utility.cc:137
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.
Definition: Utility.cc:60
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.
Definition: Utility.cc:45
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.
Definition: Utility.cc:31
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:479
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:426
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.
Definition: Utility.cc:255
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.
Definition: Utility.cc:82
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:284
static std::string info(const std::string &filename)
Print information about the classifier stored in the given weightfile.
Definition: Utility.cc:97
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.
Definition: Utility.cc:234
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:315
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.
Definition: Utility.cc:125
The Weightfile class serializes all information about a training into an xml tree.
Definition: Weightfile.h:38
void addFile(const std::string &identifier, const std::string &custom_weightfile)
Add a file (mostly a weightfile from a MVA library) to our Weightfile.
Definition: Weightfile.cc:114
static Weightfile loadFromXMLFile(const std::string &filename)
Static function which loads a Weightfile from a XML file.
Definition: Weightfile.cc:239
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.
Definition: Weightfile.cc:153
static void saveToXMLFile(Weightfile &weightfile, const std::string &filename)
Static function which saves a Weightfile to a XML file.
Definition: Weightfile.cc:174
void addOptions(const Options &options)
Add an Option object to the xml tree.
Definition: Weightfile.cc:61
static Weightfile loadFromROOTFile(const std::string &filename)
Static function which loads a Weightfile from a ROOT file.
Definition: Weightfile.cc:216
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.
Definition: Weightfile.cc:194
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:280
static void saveToROOTFile(Weightfile &weightfile, const std::string &filename)
Static function which saves a Weightfile to a ROOT file.
Definition: Weightfile.cc:164
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.
Definition: Weightfile.cc:266
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:257
static std::string makeROOTCompatible(std::string str)
Remove special characters that ROOT dislikes in branch names, e.g.