Belle II Software  release-05-02-19
LookupTable.h
1 /**************************************************************************
2 * BASF2 (Belle Analysis Framework 2) *
3 * Copyright(C) 2016 - Belle II Collaboration *
4 * *
5 * Author: The Belle II Collaboration *
6 * Contributors: Oliver Frost *
7 * *
8 * This software is provided "as is" without any warranty. *
9 **************************************************************************/
10 #pragma once
11 
12 #include <functional>
13 #include <algorithm>
14 #include <vector>
15 #include <cmath>
16 #include <cassert>
17 
18 namespace Belle2 {
23  namespace TrackFindingCDC {
25  template <class AResultType = double>
26  std::vector<AResultType> linspace(double start,
27  double final,
28  std::size_t n,
29  const std::function<AResultType(double)>& map)
30  {
31  std::vector<AResultType> result;
32  result.reserve(n);
33  result.push_back(map(start));
34  for (std::size_t i = 1; i < n - 1; ++i) {
35  result.push_back(map((start * (n - 1 - i) + final * i) / (n - 1)));
36  }
37  result.push_back(map(final));
38  assert(result.size() == n);
39  return result;
40  }
41 
43  template <class AResultType = double>
44  std::vector<AResultType> linspace(double start, double final, std::size_t n)
45  {
46  auto map = [](const double in) -> AResultType {return AResultType(in);};
47  return linspace<AResultType>(start, final, n, map);
48  }
49 
51  template<class T = double>
52  class LookupTable {
53 
54  public:
61  LookupTable(const std::function<T(double)>& map,
62  std::size_t nBins,
63  double lowerBound,
64  double upperBound)
65  : m_lowerBound(lowerBound)
66  , m_upperBound(upperBound)
67  , m_binWidth((m_upperBound - m_lowerBound) / nBins)
68  , m_values(linspace(lowerBound, upperBound, nBins + 1, map))
69  {
70  // Add a sentinel at the back.
71  m_values.push_back(map(NAN));
72  }
73 
75  T operator()(double x) const
76  {
77  if (not std::isfinite(x)) return m_values.back(); // Return sentinel
78  const int iMax = m_values.size() - 2; // Subtracting sentinel index
79  double iBin = 0;
80  double delta = std::modf((x - m_lowerBound) / m_binWidth, &iBin);
81  int i = std::min(std::max(0, static_cast<int>(iBin)), iMax);
82  return m_values[i] * (1 - delta) + m_values[i + 1] * delta;
83  }
84 
86  const T& nearest(double x) const
87  {
88  if (not std::isfinite(x)) return m_values.back(); // Return sentinel value
89  const int iMax = m_values.size() - 2; // Subtracting sentinel index
90  int iBin = std::round((x - m_lowerBound) / m_binWidth);
91  int i = std::min(std::max(0, iBin), iMax);
92  return m_values[i];
93  }
94 
96  const T& at(int i) const
97  {
98  const int iMax = m_values.size() - 2; // Subtracting sentinel index
99  i = std::min(std::max(0, i), iMax);
100  return m_values[i];
101  }
102 
104  int getNBins() const
105  {
106  return m_values.size() - 2;
107  }
108 
110  int getNPoints() const
111  {
112  return m_values.size() - 1;
113  }
114 
115  private:
117  double m_lowerBound;
118 
120  double m_upperBound;
121 
123  double m_binWidth;
124 
126  std::vector<T> m_values;
127  };
128  }
130 }
Belle2::TrackFindingCDC::LookupTable::getNBins
int getNBins() const
Return the number of bins in this lookup table.
Definition: LookupTable.h:112
Belle2::TrackFindingCDC::LookupTable::operator()
T operator()(double x) const
Evaluate as piecewise linear interpolation.
Definition: LookupTable.h:83
Belle2::TrackFindingCDC::LookupTable::nearest
const T & nearest(double x) const
Return the precomputed value at the position closest to the given value.
Definition: LookupTable.h:94
Belle2::TrackFindingCDC::LookupTable::m_values
std::vector< T > m_values
Precomputed value.
Definition: LookupTable.h:134
Belle2::TrackFindingCDC::LookupTable::LookupTable
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:69
Belle2::TrackFindingCDC::LookupTable::m_upperBound
double m_upperBound
Upper bound of the precomputed range.
Definition: LookupTable.h:128
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::LookupTable::at
const T & at(int i) const
Return the value at the given index.
Definition: LookupTable.h:104
Belle2::TrackFindingCDC::LookupTable::getNPoints
int getNPoints() const
Return the number of finite sampling points in this lookup table.
Definition: LookupTable.h:118
Belle2::TrackFindingCDC::LookupTable::m_lowerBound
double m_lowerBound
Lower bound of the precomputed range.
Definition: LookupTable.h:125
Belle2::TrackFindingCDC::LookupTable::m_binWidth
double m_binWidth
Distance between two precomputed positions.
Definition: LookupTable.h:131