13 #ifndef LARDATA_UTILITIES_STATCOLLECTOR_H 14 #define LARDATA_UTILITIES_STATCOLLECTOR_H 20 #include <initializer_list> 33 constexpr
auto operator()(T&& v) const noexcept -> decltype(
std::forward<T>(v))
35 return std::forward<T>(v);
63 int N()
const {
return n; }
77 static constexpr V
sqr(V
const& v)
97 template <
unsigned int PWR,
typename T,
typename W = T>
100 static constexpr
unsigned int Power = PWR;
101 static_assert(Power >= 1,
"DataTracker must have at least power 1");
113 for (
size_t i = 0; i < Power; ++i)
121 template <
unsigned int N>
124 static_assert((N > 0) && (N <= Power),
"Invalid sum of power requested");
135 std::array<Weight_t, Power>
sums;
144 template <
typename T,
typename W = T,
unsigned int PWR = 2>
149 static_assert(Power >= 2,
"DataTracker2 must have Power >= 2");
163 template <
typename T,
typename W = T,
unsigned int PWR = 3>
168 static_assert(Power >= 3,
"DataTracker3 must have Power >= 3");
242 template <
typename T,
typename W = T>
269 template <
typename Iter>
272 add_unweighted(begin, end,
identity());
288 template <
typename Iter,
typename Pred>
289 void add_unweighted(Iter
begin, Iter
end, Pred extractor);
305 template <
typename Cont,
typename Pred>
320 template <
typename Cont>
349 template <
typename VIter,
typename WIter,
typename VPred,
typename WPred =
identity>
350 void add_weighted(VIter begin_value,
353 VPred value_extractor,
354 WPred weight_extractor = WPred());
368 template <
typename Iter>
369 void add_weighted(Iter begin, Iter end);
380 template <
typename Cont>
395 int N()
const {
return Base_t::N(); }
476 template <
typename T,
typename W = T>
487 using Pair_t = std::tuple<Data_t, Data_t>;
501 add(std::get<0>(value), std::get<1>(value),
weight);
506 add(std::get<0>(value), std::get<1>(value), std::get<2>(value));
519 template <
typename Iter>
522 add_unweighted(begin, end,
identity());
538 template <
typename Iter,
typename Pred>
539 void add_unweighted(Iter
begin, Iter
end, Pred extractor);
555 template <
typename Cont,
typename Pred>
570 template <
typename Cont>
599 template <
typename VIter,
typename WIter,
typename VPred,
typename WPred =
identity>
600 void add_weighted(VIter begin_value,
603 VPred value_extractor,
604 WPred weight_extractor = WPred());
617 template <
typename Iter>
618 void add_weighted(Iter begin, Iter end);
629 template <
typename Cont>
644 int N()
const {
return Base_t::N(); }
750 template <
typename T>
769 template <
typename Iter>
801 template <
typename Iter>
806 bool has_data()
const {
return minimum <= maximum; }
819 Data_t minimum = std::numeric_limits<Data_t>::max();
822 Data_t maximum = std::numeric_limits<Data_t>::min();
837 template <
typename W>
841 if (N() == 0)
throw std::range_error(
"WeightTracker<>::AverageWeight(): divide by 0");
842 return Weights() / N();
849 template <
typename T,
typename W>
853 x.add(value, weight);
856 template <
typename T,
typename W>
857 template <
typename Iter,
typename Pred>
860 std::for_each(begin, end, [
this, extractor](
auto item) { this->add(extractor(item)); });
863 template <
typename T,
typename W>
864 template <
typename VIter,
typename WIter,
typename VPred,
typename WPred>
868 VPred value_extractor,
869 WPred weight_extractor
872 while (begin_value != end_value) {
873 add(value_extractor(*begin_value), weight_extractor(*begin_weight));
879 template <
typename T,
typename W>
880 template <
typename Iter>
884 std::for_each(begin, end, [
this](
auto p) { this->add(p.first, p.second); });
888 template <
typename T,
typename W>
895 template <
typename T,
typename W>
898 if (Weights() ==
Weight_t(0))
throw std::range_error(
"StatCollector<>::Average(): divide by 0");
899 return Sum() / Weights();
902 template <
typename T,
typename W>
905 if (Weights() ==
Weight_t(0))
throw std::range_error(
"StatCollector<>::Variance(): divide by 0");
906 return std::max(
Weight_t(0), (SumSq() -
sqr(Sum()) / Weights()) / Weights());
909 template <
typename T,
typename W>
915 return std::sqrt(rms2);
922 template <
typename T,
typename W>
928 x.add(x_value, weight);
929 y.add(y_value, weight);
930 sum_xy += weight * x_value * y_value;
933 template <
typename T,
typename W>
934 template <
typename Iter,
typename Pred>
937 std::for_each(begin, end, [
this, extractor](
auto item) { this->add(extractor(item)); });
940 template <
typename T,
typename W>
941 template <
typename VIter,
typename WIter,
typename VPred,
typename WPred>
945 VPred value_extractor,
946 WPred weight_extractor
949 while (begin_value != end_value) {
950 add(value_extractor(*begin_value), weight_extractor(*begin_weight));
956 template <
typename T,
typename W>
957 template <
typename Iter>
961 std::for_each(begin, end, [
this](
auto p) { this->add(p); });
965 template <
typename T,
typename W>
974 template <
typename T,
typename W>
979 throw std::range_error(
"StatCollector2D<>::AverageX(): divide by 0");
980 return SumX() / Weights();
983 template <
typename T,
typename W>
988 throw std::range_error(
"StatCollector2D<>::VarianceX(): divide by 0");
989 return (SumSqX() -
sqr(SumX()) / Weights()) / Weights();
992 template <
typename T,
typename W>
998 return std::sqrt(rms2);
1001 template <
typename T,
typename W>
1006 throw std::range_error(
"StatCollector2D<>::AverageY(): divide by 0");
1007 return SumY() / Weights();
1010 template <
typename T,
typename W>
1015 throw std::range_error(
"StatCollector2D<>::VarianceY(): divide by 0");
1016 return (SumSqY() -
sqr(SumY()) / Weights()) / Weights();
1019 template <
typename T,
typename W>
1024 throw std::range_error(
"StatCollector2D<>::Covariance(): divide by 0");
1025 return (SumXY() - SumX() * SumY() / Weights()) / Weights();
1028 template <
typename T,
typename W>
1034 return std::sqrt(rms2);
1037 template <
typename T,
typename W>
1041 if (Weights() ==
Data_t(0))
1042 throw std::range_error(
"StatCollector2D<>::LinearCorrelation(): divide by 0");
1044 const Weight_t var_prod = VarianceX() * VarianceY();
1046 throw std::range_error(
"StatCollector2D<>::LinearCorrelation(): variance is 0");
1048 return Covariance() / std::sqrt(var_prod);
1055 template <
typename T>
1058 if (value < minimum) minimum =
value;
1059 if (value > maximum) maximum =
value;
1063 template <
typename T>
1065 std::initializer_list<Data_t>
values)
1067 return add(values.begin(), values.end());
1070 template <
typename T>
1071 template <
typename Iter>
1074 std::for_each(begin, end, [
this](
Data_t value) { this->add(value); });
1078 template <
typename T>
1081 minimum = std::numeric_limits<Data_t>::max();
1082 maximum = std::numeric_limits<Data_t>::min();
1087 #endif // LARDATA_UTILITIES_STATCOLLECTOR_H
Weight_t Weights() const
Returns the sum of the weights.
void add(Data_t v, Weight_t w)
Adds the specified weight to the statistics.
Data_t max() const
Returns the accumulated maximum, or a very small number if no values.
void add_unweighted(Cont cont)
Adds all entries from a container, with weight 1.
bool has_data() const
Returns whether at least one datum has been added.
int N() const
Returns the number of entries added.
Namespace for general, non-LArSoft-specific utilities.
This_t & add(Data_t value)
Include a single value in the statistics.
void add_unweighted(Cont cont, Pred extractor)
Adds all entries from a container, with weight 1.
void add_weighted(VIter begin_value, VIter end_value, WIter begin_weight, VPred value_extractor, WPred weight_extractor=WPred())
Adds entries from a sequence with individually specified weights.
MinMaxCollector(std::initializer_list< Data_t > init)
Constructor: starts with parsing the specified data.
Weight_t SumSqY() const
Returns the weighted sum of the square of the y values.
void add(Data_t x, Data_t y, Weight_t weight=Weight_t(1.0))
Adds one entry with specified values and weight.
Weight_t AverageWeight() const
Returns the arithmetic average of the weights.
static constexpr V sqr(V const &v)
Returns the square of the specified value.
Weight_t SumSq() const
Returns the weighted sum of the square of the entries.
Weight_t AverageWeight() const
Returns the arithmetic average of the weights.
void add(Pair_t value, Weight_t weight=Weight_t(1.0))
Adds one entry with specified values and weight.
Weight_t SumXY() const
Returns the weighted sum of the product of x and y values.
Weight_t SumCube() const
Returns the weighted sum of the square of the entries.
MinMaxCollector(Iter begin, Iter end)
Include a sequence of values in the statistics.
Weight_t Covariance() const
Returns the covariance of the (x, y) pair.
typename Base_t::Weight_t Weight_t
type of the weight
Variable_t x
accumulator for variable x
void add(Weight_t weight)
Adds the specified weight to the statistics.
Weight_t RMS() const
Returns the root mean square.
Weight_t VarianceY() const
Returns the variance of the y values.
Variable_t y
accumulator for variable y
DataTracker()
Default constructor.
Weight_t Average() const
Returns the value average.
Weight_t Sum(unsigned int n) const
Returns the sum of the values to the power n (1 <= n <= 2, no check)
void add_unweighted(Iter begin, Iter end)
Adds entries from a sequence with weight 1.
void clear()
Resets the count.
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Keeps track of the minimum and maximum value we observed.
void add_unweighted(Cont cont, Pred extractor)
Adds all entries from a container, with weight 1.
Weight_t Variance() const
Returns the square of the RMS of the values.
Data_t min() const
Returns the accumulated minimum, or a very large number if no values.
Weight_t Sum() const
Returns the weighted sum of the values.
int N() const
Returns the number of entries added.
Weight_t AverageWeight() const
Returns the arithmetic average of the weights.
Data_t Data_t
type of data
Weight_t AverageY() const
Returns the y value average.
void add_weighted(Cont cont)
Adds entries from a sequence with individually specified weights.
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
Weight_t SumSqX() const
Returns the weighted sum of the square of the x values.
void add_unweighted(Iter begin, Iter end)
Adds entries from a sequence with weight 1.
A unary functor returning its own argument (any type)
Weight_t SumN() const
Returns the sum of the values to the power N (1 <= N <= 2)
Weight_t AverageX() const
Returns the x value average.
float Data_t
type of data we collect
Weight_t SumSq() const
Returns the weighted sum of the square of the values.
Class tracking sums of variables up to a specified power.
int N() const
Returns the number of entries added.
Weight_t SumX() const
Returns the weighted sum of the x values.
void clear()
Clears all the statistics.
constexpr auto operator()(T &&v) const noexcept-> decltype(std::forward< T >(v))
void add_weighted(Cont cont)
Adds entries from a sequence with individually specified weights.
std::tuple< Data_t, Data_t > Pair_t
void clear()
Resets the count.
void clear()
Removes all statistics and reinitializes the object.
void add(WeightedPair_t value)
Adds one entry with specified values and weight.
void add_weighted(VIter begin_value, VIter end_value, WIter begin_weight, VPred value_extractor, WPred weight_extractor=WPred())
Adds entries from a sequence with individually specified weights.
Variable_t x
accumulator for variable x
LArSoft-specific namespace.
Weight_t SumY() const
Returns the weighted sum of the y values.
Class tracking sums of variables up to power 2.
Collects statistics on two homogeneous quantities (weighted)
Data_t Weight_t
type of weight
Weight_t LinearCorrelation() const
Returns the linear correlation.
Data_t Weight_t
type of the weight
Weight_t RMSy() const
Returns the standard deviation of the y sample.
typename Base_t::Weight_t Weight_t
type of the weight
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Weight_t RMSx() const
Returns the standard deviation of the x sample.
Weight_t Weights() const
Returns the sum of the weights.
Weight_t VarianceX() const
Returns the variance of the x values.
Weight_t Sum() const
Returns the weighted sum of the entries.
void add_unweighted(Cont cont)
Adds all entries from a container, with weight 1.
void clear()
Clears all the statistics.
Collects statistics on a single quantity (weighted)
Class tracking sums of variables up to power 2.
Weight_t Weights() const
Returns the sum of the weights.
void add(Data_t value, Weight_t weight=Weight_t(1.0))
Adds one entry with specified value and weight.
std::tuple< Data_t, Data_t, Weight_t > WeightedPair_t
std::array< Weight_t, Power > sums