Belle II Software  release-08-01-10
LookupTable.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 #pragma once
9 
10 #include <functional>
11 #include <algorithm>
12 #include <vector>
13 #include <cmath>
14 #include <cassert>
15 
16 namespace Belle2 {
21  namespace TrackFindingCDC {
23  template <class AResultType = double>
24  std::vector<AResultType> linspace(double start,
25  double final,
26  std::size_t n,
27  const std::function<AResultType(double)>& map)
28  {
29  std::vector<AResultType> result;
30  result.reserve(n);
31  result.push_back(map(start));
32  for (std::size_t i = 1; i < n - 1; ++i) {
33  result.push_back(map((start * (n - 1 - i) + final * i) / (n - 1)));
34  }
35  result.push_back(map(final));
36  assert(result.size() == n);
37  return result;
38  }
39 
41  template <class AResultType = double>
42  std::vector<AResultType> linspace(double start, double final, std::size_t n)
43  {
44  auto map = [](const double in) -> AResultType {return AResultType(in);};
45  return linspace<AResultType>(start, final, n, map);
46  }
47 
49  template<class T = double>
50  class LookupTable {
51 
52  public:
59  LookupTable(const std::function<T(double)>& map,
60  std::size_t nBins,
61  double lowerBound,
62  double upperBound)
63  : m_lowerBound(lowerBound)
64  , m_upperBound(upperBound)
65  , m_binWidth((m_upperBound - m_lowerBound) / nBins)
66  , m_values(linspace(lowerBound, upperBound, nBins + 1, map))
67  {
68  // Add a sentinel at the back.
69  m_values.push_back(map(NAN));
70  }
71 
73  T operator()(double x) const
74  {
75  if (not std::isfinite(x)) return m_values.back(); // Return sentinel
76  const int iMax = m_values.size() - 2; // Subtracting sentinel index
77  double iBin = 0;
78  double delta = std::modf((x - m_lowerBound) / m_binWidth, &iBin);
79  int i = std::min(std::max(0, static_cast<int>(iBin)), iMax);
80  return m_values[i] * (1 - delta) + m_values[i + 1] * delta;
81  }
82 
84  const T& nearest(double x) const
85  {
86  if (not std::isfinite(x)) return m_values.back(); // Return sentinel value
87  const int iMax = m_values.size() - 2; // Subtracting sentinel index
88  int iBin = std::round((x - m_lowerBound) / m_binWidth);
89  int i = std::min(std::max(0, iBin), iMax);
90  return m_values[i];
91  }
92 
94  const T& at(int i) const
95  {
96  const int iMax = m_values.size() - 2; // Subtracting sentinel index
97  i = std::min(std::max(0, i), iMax);
98  return m_values[i];
99  }
100 
102  int getNBins() const
103  {
104  return m_values.size() - 2;
105  }
106 
108  int getNPoints() const
109  {
110  return m_values.size() - 1;
111  }
112 
113  private:
115  double m_lowerBound;
116 
118  double m_upperBound;
119 
121  double m_binWidth;
122 
124  std::vector<T> m_values;
125  };
126  }
128 }
Class which holds precomputed values of a function.
Definition: LookupTable.h:50
int getNBins() const
Return the number of bins in this lookup table.
Definition: LookupTable.h:102
double m_lowerBound
Lower bound of the precomputed range.
Definition: LookupTable.h:115
int getNPoints() const
Return the number of finite sampling points in this lookup table.
Definition: LookupTable.h:108
double m_binWidth
Distance between two precomputed positions.
Definition: LookupTable.h:121
T operator()(double x) const
Evaluate as piecewise linear interpolation.
Definition: LookupTable.h:73
std::vector< T > m_values
Precomputed value.
Definition: LookupTable.h:124
const T & at(int i) const
Return the value at the given index.
Definition: LookupTable.h:94
LookupTable(const std::function< T(double)> &map, std::size_t nBins, double lowerBound, double upperBound)
Constructs a look up table for the given function The function is sampled at nBins + 1 equally spaced...
Definition: LookupTable.h:59
const T & nearest(double x) const
Return the precomputed value at the position closest to the given value.
Definition: LookupTable.h:84
double m_upperBound
Upper bound of the precomputed range.
Definition: LookupTable.h:118
Abstract base class for different kinds of events.