LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ChiSquareAccumulator.h
Go to the documentation of this file.
1 
10 #ifndef LARDATA_UTILITIES_CHISQUAREACCUMULATOR_H
11 #define LARDATA_UTILITIES_CHISQUAREACCUMULATOR_H
12 
13 // C/C++ standard libraries
14 #include <utility> // std::move(), std::forward()
15 
16 namespace lar {
17  namespace util {
18 
52  template <typename F, typename T = double>
54  public:
55  using Function_t = F;
56  using Data_t = T;
57 
59 
67  // @}
68 
69  // --- BEGIN -- Access to results ----------------------------------------
72 
73  // @{
75  Data_t chiSquare() const { return fChiSq; }
76  Data_t operator()() const { return chiSquare(); }
77  operator Data_t() const { return chiSquare(); }
79 
81  unsigned int N() const { return fN; }
82 
84  Data_t expected(Data_t x) const { return fExpected(x); }
85 
87  // --- END -- Access to results ------------------------------------------
88 
89  // --- BEGIN -- Data manipulation ----------------------------------------
92 
102  void add(Data_t x, Data_t y)
103  {
104  fChiSq += sqr(y - expected(x));
105  ++fN;
106  }
107 
117  void add(Data_t x, Data_t y, Data_t s)
118  {
119  fChiSq += sqr(z(y, expected(x), s));
120  ++fN;
121  }
122 
124  void clear()
125  {
126  fChiSq = Data_t{0};
127  fN = 0U;
128  }
129 
131  // --- END -- Data manipulation ------------------------------------------
132 
133  private:
134  unsigned int fN = 0U;
135  Data_t fChiSq = 0.0;
136 
138 
140  static Data_t z(Data_t x, Data_t mu, Data_t sigma) { return (x - mu) / sigma; }
141 
143  static Data_t sqr(Data_t v) { return v * v; }
144 
145  }; // ChiSquareAccumulator<>
146 
147  //--------------------------------------------------------------------------
162  template <typename F>
164  {
165  return lar::util::ChiSquareAccumulator<F>(std::forward<F>(e));
166  }
167 
183  template <typename T, typename F>
185  {
186  return lar::util::ChiSquareAccumulator<F, T>(std::forward<F>(e));
187  }
188 
189  //--------------------------------------------------------------------------
190 
191  } // namespace util
192 } // namespace lar
193 
194 #endif // LARDATA_UTILITIES_CHISQUAREACCUMULATOR_H
Float_t x
Definition: compare.C:6
auto makeChiSquareAccumulator(F &&e)
Creates a ChiSquareAccumulator object with the specified function.
void add(Data_t x, Data_t y, Data_t s)
Adds a data point to the χ².
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:26
void clear()
Resets all the counts, starting from no data.
Computes a χ² from expectation function and data points.
Data_t chiSquare() const
Returns the value of χ² currently accumulated.
static Data_t sqr(Data_t v)
The usual square function.
Float_t y
Definition: compare.C:6
unsigned int fN
Number of data entries.
void add(Data_t x, Data_t y)
Adds a data point to the χ².
ChiSquareAccumulator(Function_t const &expected)
Constructor: uses the specified expectation function.
Function_t fExpected
Function for the expectation.
Data_t expected(Data_t x) const
Returns the expected value for the specified parameter.
F Function_t
Type of function for the expectation.
Data_t fChiSq
Accumulated χ² value.
static Data_t z(Data_t x, Data_t mu, Data_t sigma)
Normal variable.
T Data_t
Type of parameter and observed values.
LArSoft-specific namespace.
unsigned int N() const
Returns the number of added points (it&#39;s not degrees of freedom yet!).
Float_t e
Definition: plot.C:35
Data_t operator()() const
Returns the value of χ² currently accumulated.