Belle II Software  release-06-01-15
Dataset.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/interface/Dataset.h>
10 
11 #include <framework/utilities/MakeROOTCompatible.h>
12 #include <framework/logging/Logger.h>
13 #include <framework/io/RootIOUtilities.h>
14 
15 #include <TLeaf.h>
16 
17 #include <boost/filesystem/operations.hpp>
18 
19 namespace Belle2 {
24  namespace MVA {
25 
26  Dataset::Dataset(const GeneralOptions& general_options) : m_general_options(general_options)
27  {
28  m_input.resize(m_general_options.m_variables.size(), 0);
30  m_target = 0.0;
31  m_weight = 1.0;
32  m_isSignal = false;
33  }
34 
36  {
37 
38  double signal_weight_sum = 0;
39  double weight_sum = 0;
40  for (unsigned int i = 0; i < getNumberOfEvents(); ++i) {
41  loadEvent(i);
42  weight_sum += m_weight;
43  if (m_isSignal)
44  signal_weight_sum += m_weight;
45  }
46  return signal_weight_sum / weight_sum;
47 
48  }
49 
50  unsigned int Dataset::getFeatureIndex(const std::string& feature)
51  {
52 
53  auto it = std::find(m_general_options.m_variables.begin(), m_general_options.m_variables.end(), feature);
54  if (it == m_general_options.m_variables.end()) {
55  B2ERROR("Unknown feature named " << feature);
56  return 0;
57  }
58  return std::distance(m_general_options.m_variables.begin(), it);
59 
60  }
61 
62  unsigned int Dataset::getSpectatorIndex(const std::string& spectator)
63  {
64 
65  auto it = std::find(m_general_options.m_spectators.begin(), m_general_options.m_spectators.end(), spectator);
66  if (it == m_general_options.m_spectators.end()) {
67  B2ERROR("Unknown spectator named " << spectator);
68  return 0;
69  }
70  return std::distance(m_general_options.m_spectators.begin(), it);
71 
72  }
73 
74  std::vector<float> Dataset::getFeature(unsigned int iFeature)
75  {
76 
77  std::vector<float> result(getNumberOfEvents());
78  for (unsigned int iEvent = 0; iEvent < getNumberOfEvents(); ++iEvent) {
79  loadEvent(iEvent);
80  result[iEvent] = m_input[iFeature];
81  }
82  return result;
83 
84  }
85 
86  std::vector<float> Dataset::getSpectator(unsigned int iSpectator)
87  {
88 
89  std::vector<float> result(getNumberOfEvents());
90  for (unsigned int iEvent = 0; iEvent < getNumberOfEvents(); ++iEvent) {
91  loadEvent(iEvent);
92  result[iEvent] = m_spectators[iSpectator];
93  }
94  return result;
95 
96  }
97 
98  std::vector<float> Dataset::getWeights()
99  {
100 
101  std::vector<float> result(getNumberOfEvents());
102  for (unsigned int iEvent = 0; iEvent < getNumberOfEvents(); ++iEvent) {
103  loadEvent(iEvent);
104  result[iEvent] = m_weight;
105  }
106  return result;
107 
108  }
109 
110  std::vector<float> Dataset::getTargets()
111  {
112 
113  std::vector<float> result(getNumberOfEvents());
114  for (unsigned int iEvent = 0; iEvent < getNumberOfEvents(); ++iEvent) {
115  loadEvent(iEvent);
116  result[iEvent] = m_target;
117  }
118  return result;
119 
120  }
121 
122  std::vector<bool> Dataset::getSignals()
123  {
124 
125  std::vector<bool> result(getNumberOfEvents());
126  for (unsigned int iEvent = 0; iEvent < getNumberOfEvents(); ++iEvent) {
127  loadEvent(iEvent);
128  result[iEvent] = m_isSignal;
129  }
130  return result;
131 
132  }
133 
134 
135  SingleDataset::SingleDataset(const GeneralOptions& general_options, const std::vector<float>& input, float target,
136  const std::vector<float>& spectators) : Dataset(general_options)
137  {
138  m_input = input;
139  m_spectators = spectators;
140  m_target = target;
141  m_weight = 1.0;
142  m_isSignal = std::lround(target) == m_general_options.m_signal_class;
143  }
144 
145  MultiDataset::MultiDataset(const GeneralOptions& general_options, const std::vector<std::vector<float>>& input,
146  const std::vector<std::vector<float>>& spectators,
147  const std::vector<float>& targets, const std::vector<float>& weights) : Dataset(general_options), m_matrix(input),
148  m_spectator_matrix(spectators),
149  m_targets(targets), m_weights(weights)
150  {
151 
152  if (m_targets.size() > 0 and m_matrix.size() != m_targets.size()) {
153  B2ERROR("Feature matrix and target vector need same number of elements in MultiDataset, got " << m_targets.size() << " and " <<
154  m_matrix.size());
155  }
156  if (m_weights.size() > 0 and m_matrix.size() != m_weights.size()) {
157  B2ERROR("Feature matrix and weight vector need same number of elements in MultiDataset, got " << m_weights.size() << " and " <<
158  m_matrix.size());
159  }
160  if (m_spectator_matrix.size() > 0 and m_matrix.size() != m_spectator_matrix.size()) {
161  B2ERROR("Feature matrix and spectator matrix need same number of elements in MultiDataset, got " << m_spectator_matrix.size() <<
162  " and " <<
163  m_matrix.size());
164  }
165  }
166 
167 
168  void MultiDataset::loadEvent(unsigned int iEvent)
169  {
170  m_input = m_matrix[iEvent];
171 
172  if (m_spectator_matrix.size() > 0) {
174  }
175 
176  if (m_targets.size() > 0) {
177  m_target = m_targets[iEvent];
179  }
180 
181  if (m_weights.size() > 0)
182  m_weight = m_weights[iEvent];
183 
184  }
185 
186  SubDataset::SubDataset(const GeneralOptions& general_options, const std::vector<bool>& events,
187  Dataset& dataset) : Dataset(general_options), m_dataset(dataset)
188  {
189 
190  for (auto& v : m_general_options.m_variables) {
191  auto it = std::find(m_dataset.m_general_options.m_variables.begin(), m_dataset.m_general_options.m_variables.end(), v);
192  if (it == m_dataset.m_general_options.m_variables.end()) {
193  B2ERROR("Couldn't find variable " << v << " in GeneralOptions");
194  throw std::runtime_error("Couldn't find variable " + v + " in GeneralOptions");
195  }
197  }
198 
199  for (auto& v : m_general_options.m_spectators) {
200  auto it = std::find(m_dataset.m_general_options.m_spectators.begin(), m_dataset.m_general_options.m_spectators.end(), v);
201  if (it == m_dataset.m_general_options.m_spectators.end()) {
202  B2ERROR("Couldn't find spectator " << v << " in GeneralOptions");
203  throw std::runtime_error("Couldn't find spectator " + v + " in GeneralOptions");
204  }
206  }
207 
208  if (events.size() > 0)
209  m_use_event_indices = true;
210 
211  if (m_use_event_indices) {
212  m_event_indices.resize(dataset.getNumberOfEvents());
213  unsigned int n_events = 0;
214  for (unsigned int iEvent = 0; iEvent < dataset.getNumberOfEvents(); ++iEvent) {
215  if (events.size() == 0 or events[iEvent]) {
216  m_event_indices[n_events] = iEvent;
217  n_events++;
218  }
219  }
220  m_event_indices.resize(n_events);
221  }
222 
223  }
224 
225  void SubDataset::loadEvent(unsigned int iEvent)
226  {
227  unsigned int index = iEvent;
229  index = m_event_indices[iEvent];
230  m_dataset.loadEvent(index);
234 
235  for (unsigned int iFeature = 0; iFeature < m_input.size(); ++iFeature) {
236  m_input[iFeature] = m_dataset.m_input[m_feature_indices[iFeature]];
237  }
238 
239  for (unsigned int iSpectator = 0; iSpectator < m_spectators.size(); ++iSpectator) {
240  m_spectators[iSpectator] = m_dataset.m_spectators[m_spectator_indices[iSpectator]];
241  }
242 
243  }
244 
245  std::vector<float> SubDataset::getFeature(unsigned int iFeature)
246  {
247 
248  auto v = m_dataset.getFeature(m_feature_indices[iFeature]);
249  if (not m_use_event_indices)
250  return v;
251  std::vector<float> result(m_event_indices.size());
252  for (unsigned int iEvent = 0; iEvent < getNumberOfEvents(); ++iEvent) {
253  result[iEvent] = v[m_event_indices[iEvent]];
254  }
255  return result;
256 
257  }
258 
259  std::vector<float> SubDataset::getSpectator(unsigned int iSpectator)
260  {
261 
262  auto v = m_dataset.getSpectator(m_spectator_indices[iSpectator]);
263  if (not m_use_event_indices)
264  return v;
265  std::vector<float> result(m_event_indices.size());
266  for (unsigned int iEvent = 0; iEvent < getNumberOfEvents(); ++iEvent) {
267  result[iEvent] = v[m_event_indices[iEvent]];
268  }
269  return result;
270 
271  }
272 
273  CombinedDataset::CombinedDataset(const GeneralOptions& general_options, Dataset& signal_dataset,
274  Dataset& background_dataset) : Dataset(general_options), m_signal_dataset(signal_dataset),
275  m_background_dataset(background_dataset) { }
276 
277  void CombinedDataset::loadEvent(unsigned int iEvent)
278  {
279  if (iEvent < m_signal_dataset.getNumberOfEvents()) {
280  m_signal_dataset.loadEvent(iEvent);
281  m_target = 1.0;
282  m_isSignal = true;
286  } else {
288  m_target = 0.0;
289  m_isSignal = false;
293  }
294  }
295 
296  std::vector<float> CombinedDataset::getFeature(unsigned int iFeature)
297  {
298 
299  auto s = m_signal_dataset.getFeature(iFeature);
300  auto b = m_background_dataset.getFeature(iFeature);
301  s.insert(s.end(), b.begin(), b.end());
302  return s;
303 
304  }
305 
306  std::vector<float> CombinedDataset::getSpectator(unsigned int iSpectator)
307  {
308 
309  auto s = m_signal_dataset.getSpectator(iSpectator);
310  auto b = m_background_dataset.getSpectator(iSpectator);
311  s.insert(s.end(), b.begin(), b.end());
312  return s;
313 
314  }
315 
316  ROOTDataset::ROOTDataset(const GeneralOptions& general_options) : Dataset(general_options)
317  {
318  m_input_double.resize(m_general_options.m_variables.size(), 0);
320  m_target_double = 0.0;
321  m_weight_double = 1.0;
322 
323  for (const auto& variable : general_options.m_variables)
324  for (const auto& spectator : general_options.m_spectators)
325  if (variable == spectator or variable == general_options.m_target_variable or spectator == general_options.m_target_variable) {
326  B2ERROR("Interface doesn't support variable more then one time in either spectators, variables or target variable");
327  throw std::runtime_error("Interface doesn't support variable more then one time in either spectators, variables or target variable");
328  }
329 
330  std::vector<std::string> filenames;
331  for (const auto& filename : m_general_options.m_datafiles) {
332  if (boost::filesystem::exists(filename)) {
333  filenames.push_back(filename);
334  } else {
336  filenames.insert(filenames.end(), temp.begin(), temp.end());
337  }
338  }
339  if (filenames.empty()) {
340  B2ERROR("Found no valid filenames in GeneralOptions");
341  throw std::runtime_error("Found no valid filenames in GeneralOptions");
342  }
343 
344  //Open TFile
345  TDirectory* dir = gDirectory;
346  for (const auto& filename : filenames) {
347  if (not boost::filesystem::exists(filename)) {
348  B2ERROR("Error given ROOT file does not exist " << filename);
349  throw std::runtime_error("Error during open of ROOT file named " + filename);
350  }
351 
352  TFile* f = TFile::Open(filename.c_str(), "READ");
353  if (!f or f->IsZombie() or not f->IsOpen()) {
354  B2ERROR("Error during open of ROOT file named " << filename);
355  throw std::runtime_error("Error during open of ROOT file named " + filename);
356  }
357  delete f;
358  }
359  dir->cd();
360 
361  m_tree = new TChain(m_general_options.m_treename.c_str());
362  for (const auto& filename : filenames) {
363  //nentries = -1 forces AddFile() to read headers
364  if (!m_tree->AddFile(filename.c_str(), -1)) {
365  B2ERROR("Error during open of ROOT file named " << filename << " cannot retrieve tree named " <<
367  throw std::runtime_error("Error during open of ROOT file named " + filename + " cannot retrieve tree named " +
369  }
370  }
373  }
374 
375  void ROOTDataset::loadEvent(unsigned int event)
376  {
377  if (m_tree->GetEntry(event, 0) == 0) {
378  B2ERROR("Error during loading entry from chain");
379  }
380  if (m_isDoubleInputType) {
381  m_weight = (float) m_weight_double;
382  m_target = (float) m_target_double;
383  for (unsigned int i = 0; i < m_input_double.size(); i++)
384  m_input[i] = (float) m_input_double[i];
385  for (unsigned int i = 0; i < m_spectators_double.size(); i++)
386  m_spectators[i] = (float) m_spectators_double[i];
387  }
388 
390  }
391 
392  std::vector<float> ROOTDataset::getWeights()
393  {
395  if (branchName.empty()) {
396  B2INFO("No TBranch name given for weights. Using 1s as default weights.");
397  int nentries = getNumberOfEvents();
398  std::vector<float> values(nentries, 1.);
399  return values;
400  }
401  if (branchName == "__weight__") {
402  if (!checkForBranch(m_tree, "__weight__")) {
403  B2INFO("No default weight branch with name __weight__ found. Using 1s as default weights.");
404  int nentries = getNumberOfEvents();
405  std::vector<float> values(nentries, 1.);
406  return values;
407  }
408  }
409 
410  std::string typeName = "weights";
411 
413  return ROOTDataset::getVectorFromTTree(typeName, branchName, m_weight_double);
414 
415  return ROOTDataset::getVectorFromTTree(typeName, branchName, m_weight);
416  }
417 
418  std::vector<float> ROOTDataset::getFeature(unsigned int iFeature)
419  {
420  if (iFeature >= getNumberOfFeatures()) {
421  B2ERROR("Feature index " << iFeature << " is out of bounds of given number of features: "
422  << getNumberOfFeatures());
423  }
424 
425  std::string branchName = Belle2::makeROOTCompatible(m_general_options.m_variables[iFeature]);
426  std::string typeName = "features";
427 
429  return ROOTDataset::getVectorFromTTree(typeName, branchName, m_input_double[iFeature]);
430 
431  return ROOTDataset::getVectorFromTTree(typeName, branchName, m_input[iFeature]);
432  }
433 
434  std::vector<float> ROOTDataset::getSpectator(unsigned int iSpectator)
435  {
436  if (iSpectator >= getNumberOfSpectators()) {
437  B2ERROR("Spectator index " << iSpectator << " is out of bounds of given number of spectators: "
438  << getNumberOfSpectators());
439  }
440 
441  std::string branchName = Belle2::makeROOTCompatible(m_general_options.m_spectators[iSpectator]);
442  std::string typeName = "spectators";
443 
445  return ROOTDataset::getVectorFromTTree(typeName, branchName, m_spectators_double[iSpectator]);
446 
447  return ROOTDataset::getVectorFromTTree(typeName, branchName, m_spectators[iSpectator]);
448  }
449 
451  {
452  delete m_tree;
453  m_tree = nullptr;
454  }
455 
456  template<class T>
457  std::vector<float> ROOTDataset::getVectorFromTTree(std::string& variableType, std::string& branchName,
458  T& memberVariableTarget)
459  {
460  int nentries = getNumberOfEvents();
461  std::vector<float> values(nentries);
462 
463  // Float or Double to be filled
464  T object;
465  auto currentTreeNumber = m_tree->GetTreeNumber();
466  TBranch* branch = m_tree->GetBranch(branchName.c_str());
467  if (not branch) {
468  B2ERROR("TBranch for " + variableType + " named '" << branchName.c_str() << "' does not exist!");
469  }
470  branch->SetAddress(&object);
471  for (int i = 0; i < nentries; ++i) {
472  auto entry = m_tree->LoadTree(i);
473  if (entry < 0) {
474  B2ERROR("Error during loading root tree from chain, error code: " << entry);
475  }
476  // if current tree changed we have to update the branch
477  if (currentTreeNumber != m_tree->GetTreeNumber()) {
478  currentTreeNumber = m_tree->GetTreeNumber();
479  branch = m_tree->GetBranch(branchName.c_str());
480  branch->SetAddress(&object);
481  }
482  branch->GetEntry(entry);
483  values[i] = object;
484  }
485  // Reset branch to correct input address, just to be sure
486  m_tree->SetBranchAddress(branchName.c_str(), &memberVariableTarget);
487  return values;
488  }
489 
490  bool ROOTDataset::checkForBranch(TTree* tree, const std::string& branchname) const
491  {
492  auto branch = tree->GetListOfBranches()->FindObject(branchname.c_str());
493  return branch != nullptr;
494 
495  }
496 
497  template<class T>
498  void ROOTDataset::setScalarVariableAddress(std::string& variableType, std::string& variableName,
499  T& variableTarget)
500  {
501  if (not variableName.empty()) {
502  if (checkForBranch(m_tree, variableName)) {
503  m_tree->SetBranchStatus(variableName.c_str(), true);
504  m_tree->SetBranchAddress(variableName.c_str(), &variableTarget);
505  } else {
506  if (checkForBranch(m_tree, Belle2::makeROOTCompatible(variableName))) {
507  m_tree->SetBranchStatus(Belle2::makeROOTCompatible(variableName).c_str(), true);
508  m_tree->SetBranchAddress(Belle2::makeROOTCompatible(variableName).c_str(), &variableTarget);
509  } else {
510  B2ERROR("Couldn't find given " << variableType << " variable named " << variableName <<
511  " (I tried also using makeROOTCompatible)");
512  throw std::runtime_error("Couldn't find given " + variableType + " variable named " + variableName +
513  " (I tried also using makeROOTCompatible)");
514  }
515  }
516  }
517  }
518 
519  template<class T>
520  void ROOTDataset::setVectorVariableAddress(std::string& variableType, std::vector<std::string>& variableNames,
521  T& variableTargets)
522  {
523  for (unsigned int i = 0; i < variableNames.size(); ++i)
524  ROOTDataset::setScalarVariableAddress(variableType, variableNames[i], variableTargets[i]);
525  }
526 
528  {
529  // Deactivate all branches by default
530  m_tree->SetBranchStatus("*", false);
531  std::string typeName;
532 
533  if (m_general_options.m_weight_variable.empty()) {
534  m_weight = 1;
535  m_weight_double = 1;
536  B2INFO("No weight variable provided. The weight will be set to 1.");
537  }
538 
539  if (m_general_options.m_weight_variable == "__weight__") {
540  if (checkForBranch(m_tree, "__weight__")) {
541  m_tree->SetBranchStatus("__weight__", true);
543  m_tree->SetBranchAddress("__weight__", &m_weight_double);
544  else
545  m_tree->SetBranchAddress("__weight__", &m_weight);
546  } else {
547  B2INFO("Couldn't find default weight feature named __weight__, all weights will be 1. Consider setting the "
548  "weight variable to an empty string if you don't need it.");
549  m_weight = 1;
550  m_weight_double = 1;
551  }
552  } else if (m_isDoubleInputType) {
553  typeName = "weight";
555  } else {
556  typeName = "weight";
558  }
559 
560  if (m_isDoubleInputType) {
561  typeName = "target";
563  typeName = "feature";
565  typeName = "spectator";
567  } else {
568  typeName = "target";
570  typeName = "feature";
572  typeName = "spectator";
574  }
575  }
576 
577 
579  {
580  std::string control_variable;
581  for (auto& variable : m_general_options.m_variables) {
582  if (checkForBranch(m_tree, variable))
583  control_variable = variable;
584  else if (checkForBranch(m_tree, Belle2::makeROOTCompatible(variable)))
585  control_variable = Belle2::makeROOTCompatible(variable);
586  if (not control_variable.empty()) {
587  TBranch* branch = m_tree->GetBranch(control_variable.c_str());
588  TLeaf* leaf = branch->GetLeaf(control_variable.c_str());
589  std::string type_name = leaf->GetTypeName();
590  if (type_name == "Double_t")
591  m_isDoubleInputType = true;
592  else if (type_name == "Float_t")
593  m_isDoubleInputType = false;
594  else {
595  B2FATAL("Unknown root input type: " << type_name);
596  throw std::runtime_error("Unknown root input type: " + type_name);
597  }
598  return;
599  }
600  }
601  B2FATAL("No valid feature was found. Check your input features.");
602  throw std::runtime_error("No valid feature was found. Check your input features.");
603  }
604 
605 
606  }
608 }
CombinedDataset(const GeneralOptions &general_options, Dataset &signal_dataset, Dataset &background_dataset)
Constructs a new CombinedDataset holding a reference to the wrapped Datasets.
Definition: Dataset.cc:273
Dataset & m_background_dataset
Reference to the wrapped dataset containing background events.
Definition: Dataset.h:338
virtual std::vector< float > getSpectator(unsigned int iSpectator) override
Returns all values of one spectator in a std::vector<float> of the wrapped dataset.
Definition: Dataset.cc:306
virtual std::vector< float > getFeature(unsigned int iFeature) override
Returns all values of one feature in a std::vector<float> of the wrapped dataset.
Definition: Dataset.cc:296
virtual void loadEvent(unsigned int iEvent) override
Load the event number iEvent from the wrapped dataset.
Definition: Dataset.cc:277
Dataset & m_signal_dataset
Reference to the wrapped dataset containing signal events.
Definition: Dataset.h:337
Abstract base class of all Datasets given to the MVA interface The current event can always be access...
Definition: Dataset.h:31
virtual unsigned int getNumberOfEvents() const =0
Returns the number of events in this dataset.
virtual std::vector< bool > getSignals()
Returns all is Signals.
Definition: Dataset.cc:122
virtual unsigned int getFeatureIndex(const std::string &feature)
Return index of feature with the given name.
Definition: Dataset.cc:50
virtual std::vector< float > getSpectator(unsigned int iSpectator)
Returns all values of one spectator in a std::vector<float>
Definition: Dataset.cc:86
std::vector< float > m_spectators
Contains all spectators values of the currently loaded event.
Definition: Dataset.h:122
virtual std::vector< float > getTargets()
Returns all targets.
Definition: Dataset.cc:110
virtual void loadEvent(unsigned int iEvent)=0
Load the event number iEvent.
GeneralOptions m_general_options
GeneralOptions passed to this dataset.
Definition: Dataset.h:120
std::vector< float > m_input
Contains all feature values of the currently loaded event.
Definition: Dataset.h:121
Dataset(const GeneralOptions &general_options)
Constructs a new dataset given the general options.
Definition: Dataset.cc:26
virtual std::vector< float > getFeature(unsigned int iFeature)
Returns all values of one feature in a std::vector<float>
Definition: Dataset.cc:74
virtual std::vector< float > getWeights()
Returns all weights.
Definition: Dataset.cc:98
virtual float getSignalFraction()
Returns the signal fraction of the whole sample.
Definition: Dataset.cc:35
bool m_isSignal
Defines if the currently loaded event is signal or background.
Definition: Dataset.h:125
float m_weight
Contains the weight of the currently loaded event.
Definition: Dataset.h:123
virtual unsigned int getSpectatorIndex(const std::string &spectator)
Return index of spectator with the given name.
Definition: Dataset.cc:62
float m_target
Contains the target value of the currently loaded event.
Definition: Dataset.h:124
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
int m_signal_class
Signal class which is used as signal in a classification problem.
Definition: Options.h:88
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:90
std::vector< std::string > m_spectators
Vector of all spectators (branch names) used in the training.
Definition: Options.h:87
std::string m_treename
Name of the TTree inside the datafile containing the training data.
Definition: Options.h:85
std::string m_target_variable
Target variable (branch name) defining the target.
Definition: Options.h:89
std::vector< float > m_weights
weight vector
Definition: Dataset.h:224
std::vector< std::vector< float > > m_matrix
Feature matrix.
Definition: Dataset.h:221
std::vector< std::vector< float > > m_spectator_matrix
Spectator matrix.
Definition: Dataset.h:222
MultiDataset(const GeneralOptions &general_options, const std::vector< std::vector< float >> &input, const std::vector< std::vector< float >> &spectators, const std::vector< float > &targets={}, const std::vector< float > &weights={})
Constructs a new MultiDataset.
Definition: Dataset.cc:145
std::vector< float > m_targets
target vector
Definition: Dataset.h:223
virtual void loadEvent(unsigned int iEvent) override
Does nothing in the case of a single dataset, because the only event is already loaded.
Definition: Dataset.cc:168
void setBranchAddresses()
Sets the branch addresses of all features, weight and target again.
Definition: Dataset.cc:527
void setVectorVariableAddress(std::string &variableType, std::vector< std::string > &variableName, T &variableTargets)
sets the branch address for a vector variable to a given target
Definition: Dataset.cc:520
virtual unsigned int getNumberOfEvents() const override
Returns the number of events in this dataset.
Definition: Dataset.h:369
TChain * m_tree
Pointer to the TChain containing the data.
Definition: Dataset.h:455
double m_target_double
Contains the target value of the currently loaded event.
Definition: Dataset.h:460
virtual void loadEvent(unsigned int event) override
Load the event number iEvent from the TTree.
Definition: Dataset.cc:375
virtual std::vector< float > getSpectator(unsigned int iSpectator) override
Returns all values of one spectator in a std::vector<float>
Definition: Dataset.cc:434
std::vector< double > m_spectators_double
Contains all spectators values of the currently loaded event.
Definition: Dataset.h:458
double m_weight_double
Contains the weight of the currently loaded event.
Definition: Dataset.h:459
virtual std::vector< float > getFeature(unsigned int iFeature) override
Returns all values of one feature in a std::vector<float>
Definition: Dataset.cc:418
void setScalarVariableAddress(std::string &variableType, std::string &variableName, T &variableTarget)
sets the branch address for a scalar variable to a given target
Definition: Dataset.cc:498
virtual std::vector< float > getWeights() override
Returns all values of of the weights in a std::vector<float>
Definition: Dataset.cc:392
ROOTDataset(const GeneralOptions &_general_options)
Creates a new ROOTDataset.
Definition: Dataset.cc:316
void setRootInputType()
Tries to infer the data-type of a root file and sets m_isDoubleInputType.
Definition: Dataset.cc:578
virtual unsigned int getNumberOfSpectators() const override
Returns the number of features in this dataset.
Definition: Dataset.h:364
std::vector< double > m_input_double
Contains all feature values of the currently loaded event.
Definition: Dataset.h:457
bool checkForBranch(TTree *, const std::string &) const
Checks if the given branchname exists in the TTree.
Definition: Dataset.cc:490
std::vector< float > getVectorFromTTree(std::string &variableType, std::string &branchName, T &memberVariableTarget)
Returns all values for a specified variableType and branchName.
Definition: Dataset.cc:457
virtual ~ROOTDataset()
Virtual destructor.
Definition: Dataset.cc:450
virtual unsigned int getNumberOfFeatures() const override
Returns the number of features in this dataset.
Definition: Dataset.h:359
bool m_isDoubleInputType
Defines the expected datatype in the ROOT file.
Definition: Dataset.h:456
SingleDataset(const GeneralOptions &general_options, const std::vector< float > &input, float target=1.0, const std::vector< float > &spectators=std::vector< float >())
Constructs a new SingleDataset.
Definition: Dataset.cc:135
Dataset & m_dataset
Reference to the wrapped dataset.
Definition: Dataset.h:284
SubDataset(const GeneralOptions &general_options, const std::vector< bool > &events, Dataset &dataset)
Constructs a new SubDataset holding a reference to the wrapped Dataset.
Definition: Dataset.cc:186
virtual unsigned int getNumberOfEvents() const override
Returns the number of events in the wrapped dataset.
Definition: Dataset.h:256
virtual std::vector< float > getSpectator(unsigned int iSpectator) override
Returns all values of one spectator in a std::vector<float> of the wrapped dataset.
Definition: Dataset.cc:259
std::vector< unsigned int > m_feature_indices
Mapping from the position of a feature in the given subset to its position in the wrapped dataset.
Definition: Dataset.h:279
virtual std::vector< float > getFeature(unsigned int iFeature) override
Returns all values of one feature in a std::vector<float> of the wrapped dataset.
Definition: Dataset.cc:245
std::vector< unsigned int > m_spectator_indices
Mapping from the position of a spectator in the given subset to its position in the wrapped dataset.
Definition: Dataset.h:281
virtual void loadEvent(unsigned int iEvent) override
Load the event number iEvent from the wrapped dataset.
Definition: Dataset.cc:225
std::vector< unsigned int > m_event_indices
Mapping from the position of a event in the given subset to its position in the wrapped dataset.
Definition: Dataset.h:283
bool m_use_event_indices
Use only a subset of the wrapped dataset events.
Definition: Dataset.h:277
std::string makeROOTCompatible(std::string str)
Remove special characters that ROOT dislikes in branch names, e.g.
std::vector< std::string > expandWordExpansions(const std::vector< std::string > &filenames)
Performs wildcard expansion using wordexp(), returns matches.
Abstract base class for different kinds of events.