Belle II Software  release-08-01-10
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 <cstdlib>
24 #include <iostream>
25 #include <chrono>
26 #include <string>
27 #include <regex>
28 #include <fstream>
29 
30 using namespace Belle2::MVA;
31 
32 void Utility::download(const std::string& identifier, const std::string& filename, int experiment, int run, int event)
33 {
34  Belle2::EventMetaData emd(event, run, experiment);
36  if (boost::ends_with(filename, ".root")) {
37  Belle2::MVA::Weightfile::saveToROOTFile(weightfile, filename);
38  } else if (boost::ends_with(filename, ".xml")) {
39  Belle2::MVA::Weightfile::saveToXMLFile(weightfile, filename);
40  } else {
41  std::cerr << "Unknown file extension, fallback to xml" << std::endl;
42  Belle2::MVA::Weightfile::saveToXMLFile(weightfile, filename);
43  }
44 }
45 
46 void Utility::upload(const std::string& filename, const std::string& identifier, int exp1, int run1, int exp2, int run2)
47 {
48  Belle2::IntervalOfValidity iov(exp1, run1, exp2, run2);
49  Belle2::MVA::Weightfile weightfile;
50  if (boost::ends_with(filename, ".root")) {
51  weightfile = Belle2::MVA::Weightfile::loadFromROOTFile(filename);
52  } else if (boost::ends_with(filename, ".xml")) {
53  weightfile = Belle2::MVA::Weightfile::loadFromXMLFile(filename);
54  } else {
55  std::cerr << "Unknown file extension, fallback to xml" << std::endl;
56  weightfile = Belle2::MVA::Weightfile::loadFromXMLFile(filename);
57  }
58  Belle2::MVA::Weightfile::saveToDatabase(weightfile, identifier, iov);
59 }
60 
61 void Utility::upload_array(const std::vector<std::string>& filenames, const std::string& identifier, int exp1, int run1, int exp2,
62  int run2)
63 {
64  Belle2::IntervalOfValidity iov(exp1, run1, exp2, run2);
65 
66  std::vector<Belle2::MVA::Weightfile> weightfiles;
67  for (const auto& filename : filenames) {
68 
69  Belle2::MVA::Weightfile weightfile;
70  if (boost::ends_with(filename, ".root")) {
71  weightfile = Belle2::MVA::Weightfile::loadFromROOTFile(filename);
72  } else if (boost::ends_with(filename, ".xml")) {
73  weightfile = Belle2::MVA::Weightfile::loadFromXMLFile(filename);
74  } else {
75  std::cerr << "Unknown file extension, fallback to xml" << std::endl;
76  weightfile = Belle2::MVA::Weightfile::loadFromXMLFile(filename);
77  }
78  weightfiles.push_back(weightfile);
79  }
80  Belle2::MVA::Weightfile::saveArrayToDatabase(weightfiles, identifier, iov);
81 }
82 
83 void Utility::extract(const std::string& filename, const std::string& directory)
84 {
85 
87  auto supported_interfaces = AbstractInterface::getSupportedInterfaces();
88  auto weightfile = Weightfile::load(filename);
89  weightfile.setRemoveTemporaryDirectories(false);
90  setenv("TMPDIR", directory.c_str(), 1);
91  GeneralOptions general_options;
92  weightfile.getOptions(general_options);
93  auto expertLocal = supported_interfaces[general_options.m_method]->getExpert();
94  expertLocal->load(weightfile);
95 
96 }
97 
98 std::string Utility::info(const std::string& filename)
99 {
100 
102  auto supported_interfaces = AbstractInterface::getSupportedInterfaces();
103  auto weightfile = Weightfile::load(filename);
104  GeneralOptions general_options;
105  weightfile.getOptions(general_options);
106 
107  auto specific_options = supported_interfaces[general_options.m_method]->getOptions();
108  specific_options->load(weightfile.getXMLTree());
109 
110  boost::property_tree::ptree temp_tree;
111  general_options.save(temp_tree);
112  specific_options->save(temp_tree);
113  std::ostringstream oss;
114 
115 #if BOOST_VERSION < 105600
116  boost::property_tree::xml_writer_settings<char> settings('\t', 1);
117 #else
118  boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
119 #endif
120  boost::property_tree::xml_parser::write_xml(oss, temp_tree, settings);;
121 
122  return oss.str();
123 
124 }
125 
126 bool Utility::available(const std::string& filename, int experiment, int run, int event)
127 {
128 
129  try {
130  auto weightfile = Weightfile::load(filename, Belle2::EventMetaData(event, run, experiment));
131  return true;
132  } catch (...) {
133  return false;
134  }
135 
136 }
137 
138 void Utility::expert(const std::vector<std::string>& filenames, const std::vector<std::string>& datafiles,
139  const std::string& treename,
140  const std::string& outputfile, int experiment, int run, int event, bool copy_target)
141 {
142 
143  TFile file(outputfile.c_str(), "RECREATE");
144  file.cd();
145  TTree tree("variables", "variables");
146 
148  auto supported_interfaces = AbstractInterface::getSupportedInterfaces();
149 
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 
227  tree.SetEntries();
228  file.Write("variables");
229 
230 }
231 
232 void Utility::save_custom_weightfile(const GeneralOptions& general_options, const SpecificOptions& specific_options,
233  const std::string& custom_weightfile, const std::string& output_identifier)
234 {
235  std::ifstream ifile(custom_weightfile);
236  if (!(bool)ifile) {
237  B2FATAL("Input weight file: " << custom_weightfile << " does not exist!");
238  }
239 
240  Weightfile weightfile;
241  weightfile.addOptions(general_options);
242  weightfile.addOptions(specific_options);
243  weightfile.addFile(general_options.m_identifier + "_Weightfile", custom_weightfile);
244  std::string output_weightfile(custom_weightfile);
245  if (!output_identifier.empty()) {
246  std::regex to_replace("(\\.\\S+$)");
247  std::string replacement = "_" + output_identifier + "$0";
248  output_weightfile = std::regex_replace(output_weightfile, to_replace, replacement);
249  }
250  Weightfile::save(weightfile, output_weightfile);
251 }
252 
253 void Utility::teacher(const GeneralOptions& general_options, const SpecificOptions& specific_options,
254  const MetaOptions& meta_options)
255 {
256  unsigned int number_of_enabled_meta_trainings = 0;
257  if (meta_options.m_use_splot)
258  number_of_enabled_meta_trainings++;
259  if (meta_options.m_use_sideband_subtraction)
260  number_of_enabled_meta_trainings++;
261  if (meta_options.m_use_reweighting)
262  number_of_enabled_meta_trainings++;
263 
264  if (number_of_enabled_meta_trainings > 1) {
265  B2ERROR("You enabled more than one meta training option. You can only use one (sPlot, SidebandSubstraction or Reweighting)");
266  return;
267  }
268 
269  if (meta_options.m_use_splot) {
270  teacher_splot(general_options, specific_options, meta_options);
271  } else if (meta_options.m_use_sideband_subtraction) {
272  teacher_sideband_subtraction(general_options, specific_options, meta_options);
273  } else if (meta_options.m_use_reweighting) {
274  teacher_reweighting(general_options, specific_options, meta_options);
275  } else {
276  ROOTDataset data(general_options);
277  teacher_dataset(general_options, specific_options, data);
278  }
279 }
280 
281 
282 std::unique_ptr<Belle2::MVA::Expert> Utility::teacher_dataset(GeneralOptions general_options,
283  const SpecificOptions& specific_options,
284  Dataset& data)
285 {
286  if (general_options.m_method.empty()) {
287  general_options.m_method = specific_options.getMethod();
288  } else {
289  if (general_options.m_method != specific_options.getMethod()) {
290  B2ERROR("The method specified in the general options is in conflict with the provided specific option:" << general_options.m_method
291  << " " << specific_options.getMethod());
292  }
293  }
295  auto supported_interfaces = AbstractInterface::getSupportedInterfaces();
296  if (supported_interfaces.find(general_options.m_method) != supported_interfaces.end()) {
297  auto teacherLocal = supported_interfaces[general_options.m_method]->getTeacher(general_options, specific_options);
298  std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
299  auto weightfile = teacherLocal->train(data);
300  std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now();
301  std::chrono::duration<double, std::milli> training_time = stop - start;
302  B2INFO("Elapsed training time in ms " << training_time.count() << " for " << general_options.m_identifier);
303  Weightfile::save(weightfile, general_options.m_identifier);
304  auto expertLocal = supported_interfaces[general_options.m_method]->getExpert();
305  expertLocal->load(weightfile);
306  return expertLocal;
307  } else {
308  B2ERROR("Interface doesn't support chosen method" << general_options.m_method);
309  throw std::runtime_error("Interface doesn't support chosen method" + general_options.m_method);
310  }
311 }
312 
313 std::unique_ptr<Belle2::MVA::Expert> Utility::teacher_splot(const GeneralOptions& general_options,
314  const SpecificOptions& specific_options,
315  const MetaOptions& meta_options)
316 {
317 
318  GeneralOptions data_general_options = general_options;
319  data_general_options.m_target_variable = "";
320  if (meta_options.m_splot_combined)
321  data_general_options.m_identifier = general_options.m_identifier + "_splot.xml";
322  ROOTDataset data_dataset(data_general_options);
323  // Reset target variable so that it shows up in the weightfile at the end
324  data_general_options.m_target_variable = general_options.m_target_variable;
325 
326  GeneralOptions discriminant_general_options = general_options;
327  discriminant_general_options.m_target_variable = "";
328  discriminant_general_options.m_variables = {meta_options.m_splot_variable};
329  ROOTDataset discriminant_dataset(discriminant_general_options);
330  // Reset target variable so that it shows up in the weightfile at the end
331  discriminant_general_options.m_target_variable = general_options.m_target_variable;
332 
333  GeneralOptions mc_general_options = general_options;
334  mc_general_options.m_datafiles = meta_options.m_splot_mc_files;
335  mc_general_options.m_variables = {meta_options.m_splot_variable};
336  ROOTDataset mc_dataset(mc_general_options);
337 
338  auto mc_signals = mc_dataset.getSignals();
339  auto mc_weights = mc_dataset.getWeights();
340  auto mc_feature = mc_dataset.getFeature(0);
341  auto data_feature = discriminant_dataset.getFeature(0);
342  auto data_weights = discriminant_dataset.getWeights();
343 
344  Binning binning = Binning::CreateEqualFrequency(mc_feature, mc_weights, mc_signals, 100);
345 
346  float signalFraction = binning.m_signal_yield / (binning.m_signal_yield + binning.m_bckgrd_yield);
347 
348  std::vector<double> data(100, 0);
349  double total_data = 0.0;
350  for (unsigned int iEvent = 0; iEvent < data_dataset.getNumberOfEvents(); ++iEvent) {
351  data[binning.getBin(data_feature[iEvent])] += data_weights[iEvent];
352  total_data += data_weights[iEvent];
353  }
354 
355  // We do a simple fit here to estimate the signal and background yields
356  // We could use RooFit here to avoid using custom code,
357  // but I found RooFit to be difficult and unstable ...
358 
359  float best_yield = 0.0;
360  double best_chi2 = 1000000000.0;
361  bool empty_bin = false;
362  for (double yield = 0; yield < total_data; yield += 1) {
363  double chi2 = 0.0;
364  for (unsigned int iBin = 0; iBin < 100; ++iBin) {
365  double deviation = (data[iBin] - (yield * binning.m_signal_pdf[iBin] + (total_data - yield) * binning.m_bckgrd_pdf[iBin]) *
366  (binning.m_boundaries[iBin + 1] - binning.m_boundaries[iBin]) / (binning.m_boundaries[100] - binning.m_boundaries[0]));
367  if (data[iBin] > 0)
368  chi2 += deviation * deviation / data[iBin];
369  else
370  empty_bin = true;
371  }
372  if (chi2 < best_chi2) {
373  best_chi2 = chi2;
374  best_yield = yield;
375  }
376  }
377 
378  if (empty_bin) {
379  B2WARNING("Encountered empty bin in data histogram during fit of the components for sPlot");
380  }
381 
382  B2INFO("sPlot best yield " << best_yield);
383  B2INFO("sPlot Yields On MC " << binning.m_signal_yield << " " << binning.m_bckgrd_yield);
384 
385  binning.m_signal_yield = best_yield;
386  binning.m_bckgrd_yield = (total_data - best_yield);
387 
388  B2INFO("sPlot Yields Fitted On Data " << binning.m_signal_yield << " " << binning.m_bckgrd_yield);
389 
390  if (meta_options.m_splot_boosted) {
391  GeneralOptions boost_general_options = data_general_options;
392  boost_general_options.m_identifier = general_options.m_identifier + "_boost.xml";
393  SPlotDataset splot_dataset(boost_general_options, data_dataset, getBoostWeights(discriminant_dataset, binning), signalFraction);
394  auto boost_expert = teacher_dataset(boost_general_options, specific_options, splot_dataset);
395 
396  SPlotDataset aplot_dataset(data_general_options, data_dataset, getAPlotWeights(discriminant_dataset, binning,
397  boost_expert->apply(data_dataset)), signalFraction);
398  auto splot_expert = teacher_dataset(data_general_options, specific_options, aplot_dataset);
399  if (not meta_options.m_splot_combined)
400  return splot_expert;
401  } else {
402  SPlotDataset splot_dataset(data_general_options, data_dataset, getSPlotWeights(discriminant_dataset, binning), signalFraction);
403  auto splot_expert = teacher_dataset(data_general_options, specific_options, splot_dataset);
404  if (not meta_options.m_splot_combined)
405  return splot_expert;
406  }
407 
408  mc_general_options.m_identifier = general_options.m_identifier + "_pdf.xml";
409  mc_general_options.m_method = "PDF";
410  PDFOptions pdf_options;
411  // cppcheck-suppress unreadVariable
412  auto pdf_expert = teacher_dataset(mc_general_options, pdf_options, mc_dataset);
413 
414  GeneralOptions combination_general_options = general_options;
415  combination_general_options.m_method = "Combination";
416  combination_general_options.m_variables.push_back(meta_options.m_splot_variable);
417  CombinationOptions combination_options;
418  combination_options.m_weightfiles = {data_general_options.m_identifier, mc_general_options.m_identifier};
419  auto combination_expert = teacher_dataset(combination_general_options, combination_options, data_dataset);
420 
421  return combination_expert;
422 }
423 
424 std::unique_ptr<Belle2::MVA::Expert> Utility::teacher_reweighting(const GeneralOptions& general_options,
425  const SpecificOptions& specific_options,
426  const MetaOptions& meta_options)
427 {
428  if (std::find(general_options.m_variables.begin(), general_options.m_variables.end(),
429  meta_options.m_reweighting_variable) != general_options.m_variables.end()) {
430  B2ERROR("You cannot use the reweighting variable as a feature in your training");
431  return nullptr;
432  }
433 
434  GeneralOptions data_general_options = general_options;
435  data_general_options.m_target_variable = "";
436  data_general_options.m_datafiles = meta_options.m_reweighting_data_files;
437  ROOTDataset data_dataset(data_general_options);
438 
439  GeneralOptions mc_general_options = general_options;
440  mc_general_options.m_datafiles = meta_options.m_reweighting_mc_files;
441  ROOTDataset mc_dataset(mc_general_options);
442 
443  CombinedDataset boost_dataset(general_options, data_dataset, mc_dataset);
444 
445  GeneralOptions boost_general_options = general_options;
446  boost_general_options.m_identifier = general_options.m_identifier + "_boost.xml";
447  // cppcheck-suppress unreadVariable
448  auto boost_expert = teacher_dataset(boost_general_options, specific_options, boost_dataset);
449 
450  GeneralOptions reweighter_general_options = general_options;
451  reweighter_general_options.m_identifier = meta_options.m_reweighting_identifier;
452  reweighter_general_options.m_method = "Reweighter";
453  ReweighterOptions reweighter_specific_options;
454  reweighter_specific_options.m_weightfile = boost_general_options.m_identifier;
455  reweighter_specific_options.m_variable = meta_options.m_reweighting_variable;
456 
457  if (meta_options.m_reweighting_variable != "") {
458  if (std::find(reweighter_general_options.m_spectators.begin(), reweighter_general_options.m_spectators.end(),
459  meta_options.m_reweighting_variable) == reweighter_general_options.m_spectators.end() and
460  std::find(reweighter_general_options.m_variables.begin(), reweighter_general_options.m_variables.end(),
461  meta_options.m_reweighting_variable) == reweighter_general_options.m_variables.end() and
462  reweighter_general_options.m_target_variable != meta_options.m_reweighting_variable and
463  reweighter_general_options.m_weight_variable != meta_options.m_reweighting_variable) {
464  reweighter_general_options.m_spectators.push_back(meta_options.m_reweighting_variable);
465  }
466  }
467 
468  ROOTDataset dataset(reweighter_general_options);
469  auto reweight_expert = teacher_dataset(reweighter_general_options, reweighter_specific_options, dataset);
470  auto weights = reweight_expert->apply(dataset);
471  ReweightingDataset reweighted_dataset(general_options, dataset, weights);
472  auto expertLocal = teacher_dataset(general_options, specific_options, reweighted_dataset);
473 
474  return expertLocal;
475 }
476 
477 std::unique_ptr<Belle2::MVA::Expert> Utility::teacher_sideband_subtraction(const GeneralOptions& general_options,
478  const SpecificOptions& specific_options,
479  const MetaOptions& meta_options)
480 {
481 
482  if (std::find(general_options.m_variables.begin(), general_options.m_variables.end(),
483  meta_options.m_sideband_variable) != general_options.m_variables.end()) {
484  B2ERROR("You cannot use the sideband variable as a feature in your training");
485  return nullptr;
486  }
487 
488  GeneralOptions data_general_options = general_options;
489  if (std::find(data_general_options.m_spectators.begin(), data_general_options.m_spectators.end(),
490  meta_options.m_sideband_variable) == data_general_options.m_spectators.end()) {
491  data_general_options.m_spectators.push_back(meta_options.m_sideband_variable);
492  }
493  ROOTDataset data_dataset(data_general_options);
494 
495  GeneralOptions mc_general_options = general_options;
496  mc_general_options.m_datafiles = meta_options.m_sideband_mc_files;
497  if (std::find(mc_general_options.m_spectators.begin(), mc_general_options.m_spectators.end(),
498  meta_options.m_sideband_variable) == mc_general_options.m_spectators.end()) {
499  mc_general_options.m_spectators.push_back(meta_options.m_sideband_variable);
500  }
501  ROOTDataset mc_dataset(mc_general_options);
502 
503  GeneralOptions sideband_general_options = general_options;
504  SidebandDataset sideband_dataset(sideband_general_options, data_dataset, mc_dataset, meta_options.m_sideband_variable);
505  auto expertLocal = teacher_dataset(general_options, specific_options, sideband_dataset);
506 
507  return expertLocal;
508 }
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:429
virtual std::vector< float > getWeights() override
Returns all values of of the weights in a std::vector<float>
Definition: Dataset.cc:408
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:138
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:61
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:46
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:32
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:477
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:424
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:253
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:83
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:282
static std::string info(const std::string &filename)
Print information about the classifier stored in the given weightfile.
Definition: Utility.cc:98
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:232
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:313
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:126
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:115
static Weightfile loadFromXMLFile(const std::string &filename)
Static function which loads a Weightfile from a XML file.
Definition: Weightfile.cc:240
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:154
static void saveToXMLFile(Weightfile &weightfile, const std::string &filename)
Static function which saves a Weightfile to a XML file.
Definition: Weightfile.cc:175
void addOptions(const Options &options)
Add an Option object to the xml tree.
Definition: Weightfile.cc:62
static Weightfile loadFromROOTFile(const std::string &filename)
Static function which loads a Weightfile from a ROOT file.
Definition: Weightfile.cc:217
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:195
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
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 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:267
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
static std::string makeROOTCompatible(std::string str)
Remove special characters that ROOT dislikes in branch names, e.g.