13 #ifndef LARDATA_UTILITIES_STATCOLLECTOR_H 14 #define LARDATA_UTILITIES_STATCOLLECTOR_H 21 #include <initializer_list> 36 -> decltype(
std::forward<
T>(v))
38 return std::forward<T>(v);
59 int N()
const {
return n; }
74 static constexpr V
sqr(V
const& v) {
return v*v; }
92 template <
unsigned int PWR,
typename T,
typename W = T>
95 static constexpr
unsigned int Power = PWR;
96 static_assert(Power >= 1,
"DataTracker must have at least power 1");
108 for (
size_t i = 0; i < Power; ++i) sums[i] += (x *= v);
115 template <
unsigned int N>
119 ((
N > 0) && (
N <= Power),
"Invalid sum of power requested");
130 std::array<Weight_t, Power>
sums;
140 template <
typename T,
typename W = T,
unsigned int PWR = 2>
145 static_assert(Power >= 2,
"DataTracker2 must have Power >= 2");
150 {
return Base_t::sums[1]; }
160 template <
typename T,
typename W = T,
unsigned int PWR = 3>
165 static_assert(Power >= 3,
"DataTracker3 must have Power >= 3");
170 {
return Base_t::sums[2]; }
242 template <
typename T,
typename W = T>
268 template <
typename Iter>
270 { add_unweighted(begin, end,
identity()); }
285 template <
typename Iter,
typename Pred>
286 void add_unweighted(Iter
begin, Iter
end, Pred extractor);
302 template <
typename Cont,
typename Pred>
315 template <
typename Cont>
344 typename VIter,
typename WIter,
345 typename VPred,
typename WPred =
identity 348 VIter begin_value, VIter end_value,
350 VPred value_extractor,
351 WPred weight_extractor = WPred()
367 template <
typename Iter>
368 void add_weighted(Iter begin, Iter end);
379 template <
typename Cont>
476 template <
typename T,
typename W = T>
486 using Pair_t = std::tuple<Data_t, Data_t>;
499 {
add(std::get<0>(value), std::get<1>(value),
weight); }
502 {
add(std::get<0>(value), std::get<1>(value), std::get<2>(value)); }
514 template <
typename Iter>
516 { add_unweighted(begin, end,
identity()); }
531 template <
typename Iter,
typename Pred>
532 void add_unweighted(Iter
begin, Iter
end, Pred extractor);
548 template <
typename Cont,
typename Pred>
561 template <
typename Cont>
590 typename VIter,
typename WIter,
591 typename VPred,
typename WPred =
identity 594 VIter begin_value, VIter end_value,
596 VPred value_extractor,
597 WPred weight_extractor = WPred()
612 template <
typename Iter>
613 void add_weighted(Iter begin, Iter end);
624 template <
typename Cont>
747 template <
typename T>
767 template <
typename Iter>
798 template <
typename Iter>
839 template <
typename W>
844 throw std::range_error(
"WeightTracker<>::AverageWeight(): divide by 0");
845 return Weights() /
N();
853 template <
typename T,
typename W>
858 x.add(value, weight);
862 template <
typename T,
typename W>
863 template <
typename Iter,
typename Pred>
868 (begin, end, [
this, extractor](
auto item) { this->
add(extractor(item)); });
872 template <
typename T,
typename W>
873 template <
typename VIter,
typename WIter,
typename VPred,
typename WPred>
875 VIter begin_value, VIter end_value,
877 VPred value_extractor,
878 WPred weight_extractor
880 while (begin_value != end_value) {
881 add(value_extractor(*begin_value), weight_extractor(*begin_weight));
888 template <
typename T,
typename W>
889 template <
typename Iter>
892 std::for_each(begin, end, [
this](
auto p) { this->
add(
p.first,
p.second); });
898 template <
typename T,
typename W>
905 template <
typename T,
typename W>
910 throw std::range_error(
"StatCollector<>::Average(): divide by 0");
911 return Sum() / Weights();
915 template <
typename T,
typename W>
920 throw std::range_error(
"StatCollector<>::Variance(): divide by 0");
925 template <
typename T,
typename W>
932 return std::sqrt(rms2);
941 template <
typename T,
typename W>
946 x.add(x_value, weight);
947 y.add(y_value, weight);
948 sum_xy += weight * x_value * y_value;
952 template <
typename T,
typename W>
953 template <
typename Iter,
typename Pred>
955 (Iter begin, Iter end, Pred extractor)
958 (begin, end, [
this, extractor](
auto item) { this->
add(extractor(item)); });
962 template <
typename T,
typename W>
963 template <
typename VIter,
typename WIter,
typename VPred,
typename WPred>
965 VIter begin_value, VIter end_value,
967 VPred value_extractor,
968 WPred weight_extractor
970 while (begin_value != end_value) {
971 add(value_extractor(*begin_value), weight_extractor(*begin_weight));
978 template <
typename T,
typename W>
979 template <
typename Iter>
982 std::for_each(begin, end, [
this](
auto p) { this->
add(
p); });
988 template <
typename T,
typename W>
997 template <
typename T,
typename W>
1002 throw std::range_error(
"StatCollector2D<>::AverageX(): divide by 0");
1003 return SumX() / Weights();
1007 template <
typename T,
typename W>
1012 throw std::range_error(
"StatCollector2D<>::VarianceX(): divide by 0");
1013 return (SumSqX() -
sqr(SumX()) / Weights()) / Weights();
1017 template <
typename T,
typename W>
1024 return std::sqrt(rms2);
1028 template <
typename T,
typename W>
1033 throw std::range_error(
"StatCollector2D<>::AverageY(): divide by 0");
1034 return SumY() / Weights();
1038 template <
typename T,
typename W>
1043 throw std::range_error(
"StatCollector2D<>::VarianceY(): divide by 0");
1044 return (SumSqY() -
sqr(SumY()) / Weights()) / Weights();
1048 template <
typename T,
typename W>
1053 throw std::range_error(
"StatCollector2D<>::Covariance(): divide by 0");
1054 return (SumXY() - SumX() * SumY() / Weights()) / Weights();
1058 template <
typename T,
typename W>
1065 return std::sqrt(rms2);
1069 template <
typename T,
typename W>
1073 if (Weights() ==
Data_t(0))
1074 throw std::range_error(
"StatCollector2D<>::LinearCorrelation(): divide by 0");
1076 const Weight_t var_prod = VarianceX() * VarianceY();
1078 throw std::range_error(
"StatCollector2D<>::LinearCorrelation(): variance is 0");
1080 return Covariance() / std::sqrt(var_prod);
1089 template <
typename T>
1098 template <
typename T>
1101 {
return add(values.begin(), values.end()); }
1104 template <
typename T>
template <
typename Iter>
1106 (Iter begin, Iter end)
1108 std::for_each(begin, end, [
this](
Data_t value) { this->
add(value); });
1113 template <
typename T>
1122 #endif // LARDATA_UTILITIES_STATCOLLECTOR_H Weight_t Weights() const
Returns the sum of the weights.
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
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.
Coord add(Coord c1, Coord c2)
void add(Pair_t value, Weight_t weight=Weight_t(1.0))
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.
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.
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.
static int max(int a, int b)
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)
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
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
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.
vector< vector< double > > clear
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