LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 
14 // C/C++ standard libraries
15 #include <utility> // std::move(), std::forward()
16 
17 
18 namespace lar {
19  namespace util {
20 
54  template <typename F, typename T = double>
56  public:
57  using Function_t = F;
58  using Data_t = T;
59 
61 
69  : fExpected(expected) {}
70  // @}
71 
72 
73  // --- BEGIN -- Access to results ----------------------------------------
76 
77  // @{
79  Data_t chiSquare() const { return fChiSq; }
80  Data_t operator() () const { return chiSquare(); }
81  operator Data_t() const { return chiSquare(); }
83 
85  unsigned int N() const { return fN; }
86 
88  Data_t expected(Data_t x) const { return fExpected(x); }
89 
91  // --- END -- Access to results ------------------------------------------
92 
93 
94 
95  // --- BEGIN -- Data manipulation ----------------------------------------
98 
108  void add(Data_t x, Data_t y)
109  { fChiSq += sqr(y - expected(x)); ++fN; }
110 
121  { fChiSq += sqr(z(y, expected(x), s)); ++fN; }
122 
124  void clear() { fChiSq = Data_t{0}; fN = 0U; }
125 
127  // --- END -- Data manipulation ------------------------------------------
128 
129  private:
130  unsigned int fN = 0U;
131  Data_t fChiSq = 0.0;
132 
134 
136  static Data_t z(Data_t x, Data_t mu, Data_t sigma)
137  { return (x - mu) / sigma; }
138 
140  static Data_t sqr(Data_t v) { return v*v; }
141 
142  }; // ChiSquareAccumulator<>
143 
144 
145  //--------------------------------------------------------------------------
160  template <typename F>
162  { return lar::util::ChiSquareAccumulator<F>(std::forward<F>(e)); }
163 
179  template <typename T, typename F>
181  { return lar::util::ChiSquareAccumulator<F, T>(std::forward<F>(e)); }
182 
183 
184  //--------------------------------------------------------------------------
185 
186  } // namespace util
187 } // namespace lar
188 
189 
190 
191 #endif // LARDATA_UTILITIES_CHISQUAREACCUMULATOR_H
192 
Float_t x
Definition: compare.C:6
Float_t s
Definition: plot.C:23
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:17
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:34
Data_t operator()() const
Returns the value of χ² currently accumulated.