npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
ntupleStats.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_NTUPLESTATS_HH_
2 #define NPSTAT_NTUPLESTATS_HH_
3 
4 /*!
5 // \file ntupleStats.hh
6 //
7 // \brief Convenience functions for calculating ntuple means and covariances
8 //
9 // Author: I. Volobouev
10 //
11 // October 2022
12 */
13 
14 #include "npstat/stat/AbsNtuple.hh"
17 
18 namespace npstat {
19  template<typename Numeric>
20  inline void ntupleMean(const AbsNtuple<Numeric>& nt,
21  double* mean, const unsigned long lengthMean)
22  {
23  const unsigned long nCols = nt.nColumns();
24  assert(lengthMean >= nCols);
25  assert(mean);
26 
27  MultivariateSumAccumulator<> sums;
28  nt.cycleOverRows(sums);
29  for (unsigned long i=0; i<nCols; ++i)
30  mean[i] = sums.mean(i);
31  }
32 
33  template<typename Numeric, class WeightCalc>
34  inline void ntupleWeightedMean(const AbsNtuple<Numeric>& nt,
35  const WeightCalc& wcalc,
36  double* mean, const unsigned long lengthMean)
37  {
38  const unsigned long nCols = nt.nColumns();
39  assert(lengthMean >= nCols);
40  assert(mean);
41 
42  MultivariateWeightedSumAccumulator<> sums;
43  nt.weightedCycleOverRows(sums, wcalc);
44  for (unsigned long i=0; i<nCols; ++i)
45  mean[i] = sums.mean(i);
46  }
47 
48  template<typename Numeric>
49  inline Matrix<double> ntupleCovariance(const AbsNtuple<Numeric>& nt)
50  {
51  MultivariateSumAccumulator<> sums;
52  nt.cycleOverRows(sums);
53  MultivariateSumsqAccumulator<> sumsqs(sums);
54  nt.cycleOverRows(sumsqs);
55  return sumsqs.covMat();
56  }
57 
58  template<typename Numeric, class WeightCalc>
59  inline Matrix<double> ntupleWeightedCovariance(
60  const AbsNtuple<Numeric>& nt, const WeightCalc& wcalc)
61  {
62  MultivariateWeightedSumAccumulator<> sums;
63  nt.weightedCycleOverRows(sums, wcalc);
64  MultivariateWeightedSumsqAccumulator<> sumsqs(sums);
65  nt.weightedCycleOverRows(sumsqs, wcalc);
66  return sumsqs.covMat();
67  }
68 
69 #ifdef SWIG
70  template<typename Numeric>
71  inline std::vector<double> ntupleMean2(const AbsNtuple<Numeric>& nt)
72  {
73  const unsigned long nCols = nt.nColumns();
74  std::vector<double> tmp(nCols);
75  ntupleMean(nt, &tmp[0], nCols);
76  return tmp;
77  }
78 
79  template<typename Numeric, class WeightCalc>
80  inline std::vector<double> ntupleWeightedMean2(const AbsNtuple<Numeric>& nt,
81  const WeightCalc& wcalc)
82  {
83  const unsigned long nCols = nt.nColumns();
84  std::vector<double> tmp(nCols);
85  ntupleWeightedMean(nt, wcalc, &tmp[0], nCols);
86  return tmp;
87  }
88 #endif // SWIG
89 }
90 
91 #endif // NPSTAT_NTUPLESTATS_HH_
Interface definition for homogeneous ntuples (point clouds)
Accumulator of array sums of squares for covariance calculations.
Accumulator of weighted sums of squares for covariance calculations.
Definition: AbsArrayProjector.hh:14