Belle II Software  release-05-02-19
FBDTClassifierHelper.h
1 /*************************************************************************
2 * BASF2 (Belle Analysis Framework 2) *
3 * Copyright(C) 2016 - Belle II Collaboration *
4 * *
5 * Author: The Belle II Collaboration *
6 * Contributors: Thomas Madlener *
7 * *
8 * This software is provided "as is" without any warranty. *
9 **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/logging/Logger.h>
14 
15 #include <vector>
16 #include <iostream>
17 #include <string>
18 #include <array>
19 
20 namespace Belle2 {
27  template<size_t Ndims = 9>
28  struct FBDTTrainSample {
30  explicit FBDTTrainSample(const std::array<double, Ndims>& values, bool sig) : hits(values), signal(sig) {}
31 
33  FBDTTrainSample() { };
34 
36  std::array<double, Ndims> hits{};
37 
39  bool signal{};
40  };
41 
43  template<size_t Ndims>
44  static void readSamplesFromStream(std::istream& is, std::vector<FBDTTrainSample<Ndims> >& samples)
45  {
46  size_t nSamplesBefore = samples.size();
47  std::string line;
48  while (!is.eof()) {
49  getline(is, line);
50  if (line.empty()) continue; // ignore empty lines
51  std::stringstream ss(line);
52  std::array<double, 9> coords;
53  for (double& c : coords) ss >> c;
54  bool sig; ss >> sig;
55 
56  samples.push_back(FBDTTrainSample<9>(coords, sig));
57  }
58 
59  B2INFO("Read in " << (samples.size() - nSamplesBefore) << " samples.");
60  }
61 
63  template<size_t Ndims>
64  static void writeSamplesToStream(std::ostream& os, const std::vector<FBDTTrainSample<Ndims> >& samples)
65  {
66  for (const auto& event : samples) {
67  for (const auto& val : event.hits) {
68  os << val << " ";
69  }
70  os << event.signal << std::endl;
71  }
72  B2INFO("Wrote out " << samples.size() << " samples.");
73  }
75 }
Belle2::FBDTTrainSample
bundle together the classifier input and the target value into one struct for easier passing around.
Definition: FBDTClassifierHelper.h:36
Belle2::readSamplesFromStream
static void readSamplesFromStream(std::istream &is, std::vector< FBDTTrainSample< Ndims > > &samples)
read samples from stream and append them to samples
Definition: FBDTClassifierHelper.h:52
Belle2::FBDTTrainSample::FBDTTrainSample
FBDTTrainSample()
default constructor
Definition: FBDTClassifierHelper.h:41
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::writeSamplesToStream
static void writeSamplesToStream(std::ostream &os, const std::vector< FBDTTrainSample< Ndims > > &samples)
write all samples to stream
Definition: FBDTClassifierHelper.h:72
Belle2::FBDTTrainSample::signal
bool signal
target
Definition: FBDTClassifierHelper.h:47
Belle2::FBDTTrainSample::hits
std::array< double, Ndims > hits
inputs
Definition: FBDTClassifierHelper.h:44