LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
PhotonPropagationUtils.h
Go to the documentation of this file.
1 // Description:
3 // Utility functions
5 #ifndef PhotonPropagationUtils_H
6 #define PhotonPropagationUtils_H
7 
8 #include <array>
9 #include <cmath>
10 #include <limits>
11 #include <vector>
12 
13 namespace phot {
14  double fast_acos(double x);
15  double interpolate(const std::vector<double>& xData,
16  const std::vector<double>& yData,
17  const double x,
18  const bool extrapolate,
19  size_t i = 0);
20  double interpolate2(const std::vector<double>& xDistances,
21  const std::vector<double>& rDistances,
22  const std::vector<std::vector<std::vector<double>>>& parameters,
23  const double x,
24  const double r,
25  const size_t k);
26  void interpolate3(std::array<double, 3>& inter,
27  const std::vector<double>& xData,
28  const std::vector<double>& yData1,
29  const std::vector<double>& yData2,
30  const std::vector<double>& yData3,
31  const double x,
32  const bool extrapolate);
33 
34  // implements relative method - do not use for comparing with zero
35  // use this most of the time, tolerance needs to be meaningful in your context
36  template <typename TReal>
37  inline constexpr static bool
38  isApproximatelyEqual(TReal a, TReal b, TReal tolerance = std::numeric_limits<TReal>::epsilon())
39  {
40  TReal diff = std::fabs(a - b);
41  if (diff <= tolerance) return true;
42  if (diff < std::fmax(std::fabs(a), std::fabs(b)) * tolerance) return true;
43  return false;
44  }
45 
46  // supply tolerance that is meaningful in your context
47  // for example, default tolerance may not work if you are comparing double with
48  // float
49  template <typename TReal>
50  inline constexpr static bool isApproximatelyZero(
51  TReal a,
52  TReal tolerance = std::numeric_limits<TReal>::epsilon())
53  {
54  if (std::fabs(a) <= tolerance) return true;
55  return false;
56  }
57 
58  // use this when you want to be on safe side
59  // for example, don't start rover unless signal is above 1
60  template <typename TReal>
61  inline constexpr static bool
62  isDefinitelyLessThan(TReal a, TReal b, TReal tolerance = std::numeric_limits<TReal>::epsilon())
63  {
64  TReal diff = a - b;
65  if (diff < tolerance) return true;
66  if (diff < std::fmax(std::fabs(a), std::fabs(b)) * tolerance) return true;
67  return false;
68  }
69 
70  template <typename TReal>
71  inline constexpr static bool
72  isDefinitelyGreaterThan(TReal a, TReal b, TReal tolerance = std::numeric_limits<TReal>::epsilon())
73  {
74  TReal diff = a - b;
75  if (diff > tolerance) return true;
76  if (diff > std::fmax(std::fabs(a), std::fabs(b)) * tolerance) return true;
77  return false;
78  }
79 
80 } // namespace phot
81 #endif
Float_t x
Definition: compare.C:6
TRandom r
Definition: spectrum.C:23
void interpolate3(std::array< double, 3 > &inter, const std::vector< double > &xData, const std::vector< double > &yData1, const std::vector< double > &yData2, const std::vector< double > &yData3, const double x, const bool extrapolate)
static constexpr bool isDefinitelyGreaterThan(TReal a, TReal b, TReal tolerance=std::numeric_limits< TReal >::epsilon())
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
double fast_acos(double xin)
double interpolate2(const std::vector< double > &xDistances, const std::vector< double > &rDistances, const std::vector< std::vector< std::vector< double >>> &parameters, const double x, const double r, const size_t k)
General LArSoft Utilities.
static constexpr bool isApproximatelyEqual(TReal a, TReal b, TReal tolerance=std::numeric_limits< TReal >::epsilon())
static constexpr bool isDefinitelyLessThan(TReal a, TReal b, TReal tolerance=std::numeric_limits< TReal >::epsilon())
double interpolate(const std::vector< double > &xData, const std::vector< double > &yData, const double x, const bool extrapolate, size_t i)
static constexpr bool isApproximatelyZero(TReal a, TReal tolerance=std::numeric_limits< TReal >::epsilon())