Belle II Software  release-08-01-10
FBDTClassifierHelper.h
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 #pragma once
10 
11 #include <framework/logging/Logger.h>
12 
13 #include <vector>
14 #include <iostream>
15 #include <string>
16 #include <array>
17 
18 namespace Belle2 {
25  template<size_t Ndims = 9>
26  struct FBDTTrainSample {
28  explicit FBDTTrainSample(const std::array<double, Ndims>& values, bool sig) : hits(values), signal(sig) {}
29 
32 
34  std::array<double, Ndims> hits{};
35 
37  bool signal{};
38  };
39 
41  template<size_t Ndims>
42  static void readSamplesFromStream(std::istream& is, std::vector<FBDTTrainSample<Ndims> >& samples)
43  {
44  size_t nSamplesBefore = samples.size();
45  std::string line;
46  while (!is.eof()) {
47  getline(is, line);
48  if (line.empty()) continue; // ignore empty lines
49  std::stringstream ss(line);
50  std::array<double, 9> coords;
51  for (double& c : coords) ss >> c;
52  bool sig; ss >> sig;
53 
54  samples.push_back(FBDTTrainSample<9>(coords, sig));
55  }
56 
57  B2INFO("Read in " << (samples.size() - nSamplesBefore) << " samples.");
58  }
59 
61  template<size_t Ndims>
62  static void writeSamplesToStream(std::ostream& os, const std::vector<FBDTTrainSample<Ndims> >& samples)
63  {
64  for (const auto& event : samples) {
65  for (const auto& val : event.hits) {
66  os << val << " ";
67  }
68  os << event.signal << std::endl;
69  }
70  B2INFO("Wrote out " << samples.size() << " samples.");
71  }
73 }
static void readSamplesFromStream(std::istream &is, std::vector< FBDTTrainSample< Ndims > > &samples)
read samples from stream and append them to samples
static void writeSamplesToStream(std::ostream &os, const std::vector< FBDTTrainSample< Ndims > > &samples)
write all samples to stream
Abstract base class for different kinds of events.
bundle together the classifier input and the target value into one struct for easier passing around.
FBDTTrainSample(const std::array< double, Ndims > &values, bool sig)
constructor from array of inputs and a target value
std::array< double, Ndims > hits
inputs
FBDTTrainSample()
default constructor