Belle II Software development
LookupTable< T > Class Template Reference

Class which holds precomputed values of a function. More...

#include <LookupTable.h>

Public Member Functions

 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 points between the given bounds, such that the distance between consecutive sampling points is (upperBound - lowerBound) / nBins.
 
operator() (double x) const
 Evaluate as piecewise linear interpolation.
 
const T & nearest (double x) const
 Return the precomputed value at the position closest to the given value.
 
const T & at (int i) const
 Return the value at the given index.
 
int getNBins () const
 Return the number of bins in this lookup table.
 
int getNPoints () const
 Return the number of finite sampling points in this lookup table.
 

Private Attributes

double m_lowerBound
 Lower bound of the precomputed range.
 
double m_upperBound
 Upper bound of the precomputed range.
 
double m_binWidth
 Distance between two precomputed positions.
 
std::vector< T > m_values
 Precomputed value.
 

Detailed Description

template<class T = double>
class Belle2::TrackFindingCDC::LookupTable< T >

Class which holds precomputed values of a function.

Definition at line 50 of file LookupTable.h.

Constructor & Destructor Documentation

◆ LookupTable()

LookupTable ( const std::function< T(double)> &  map,
std::size_t  nBins,
double  lowerBound,
double  upperBound 
)
inline

Constructs a look up table for the given function The function is sampled at nBins + 1 equally spaced points between the given bounds, such that the distance between consecutive sampling points is (upperBound - lowerBound) / nBins.

Definition at line 59 of file LookupTable.h.

63 : m_lowerBound(lowerBound)
64 , m_upperBound(upperBound)
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 }
double m_lowerBound
Lower bound of the precomputed range.
Definition: LookupTable.h:115
double m_binWidth
Distance between two precomputed positions.
Definition: LookupTable.h:121
std::vector< T > m_values
Precomputed value.
Definition: LookupTable.h:124
double m_upperBound
Upper bound of the precomputed range.
Definition: LookupTable.h:118

Member Function Documentation

◆ at()

const T & at ( int  i) const
inline

Return the value at the given index.

Definition at line 94 of file LookupTable.h.

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 }

◆ getNBins()

int getNBins ( ) const
inline

Return the number of bins in this lookup table.

Definition at line 102 of file LookupTable.h.

103 {
104 return m_values.size() - 2;
105 }

◆ getNPoints()

int getNPoints ( ) const
inline

Return the number of finite sampling points in this lookup table.

Definition at line 108 of file LookupTable.h.

109 {
110 return m_values.size() - 1;
111 }

◆ nearest()

const T & nearest ( double  x) const
inline

Return the precomputed value at the position closest to the given value.

Definition at line 84 of file LookupTable.h.

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 }

◆ operator()()

T operator() ( double  x) const
inline

Evaluate as piecewise linear interpolation.

Definition at line 73 of file LookupTable.h.

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 }

Member Data Documentation

◆ m_binWidth

double m_binWidth
private

Distance between two precomputed positions.

Definition at line 121 of file LookupTable.h.

◆ m_lowerBound

double m_lowerBound
private

Lower bound of the precomputed range.

Definition at line 115 of file LookupTable.h.

◆ m_upperBound

double m_upperBound
private

Upper bound of the precomputed range.

Definition at line 118 of file LookupTable.h.

◆ m_values

std::vector<T> m_values
private

Precomputed value.

Definition at line 124 of file LookupTable.h.


The documentation for this class was generated from the following file: