npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
MultivariateWeightedSumAccumulator.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_MULTIVARIATEWEIGHTEDSUMACCUMULATOR_HH_
2 #define NPSTAT_MULTIVARIATEWEIGHTEDSUMACCUMULATOR_HH_
3 
4 /*!
5 // \file MultivariateWeightedSumAccumulator.hh
6 //
7 // \brief Accumulator of weighted array sums for use with AbsNtuple method
8 // weightedCycleOverRows and similar
9 //
10 // Author: I. Volobouev
11 //
12 // April 2011
13 */
14 
15 #include <vector>
16 #include <cassert>
17 #include <stdexcept>
18 
19 namespace npstat {
20  /**
21  // Class for accumulating weighted multivariate sums and calculating
22  // corresponding averages. The first call to the "accumulate"
23  // method after construction (or upon reset) will be used to
24  // determine the input array length. All subsequent "accumulate"
25  // calls until the next "reset" must use the same array length.
26  */
27  template<typename Precise = long double>
29  {
30  public:
31  typedef Precise precise_type;
32 
33  /** Only default constructor is necessary */
35  : weightSum_(), maxWeight_(0.0), dim_(0), ncalls_(0) {}
36 
37  /** The sum of weights so far accumulated */
38  inline Precise weightSum() const {return weightSum_;}
39 
40  /** Dimensionality of the data */
41  inline unsigned long dim() const {return dim_;}
42 
43  /** Number of times "accumulate" was called since last reset */
44  inline unsigned long ncalls() const {return ncalls_;}
45 
46  /** Maximum weight among those so far accumulated */
47  inline double maxWeight() const {return maxWeight_;}
48 
49  /** Get the sums accumulated so far */
50  inline const Precise* data() const
51  {return dim_ ? &data_[0] : (Precise*)0;}
52 
53  /** Calculate average weight */
54  inline Precise averageWeight() const
55  {
56  if (!ncalls_) throw std::runtime_error(
57  "In npstat::MultivariateWeightedSumAccumulator::averageWeight:"
58  " no data accumulated");
59  return weightSum_/ncalls_;
60  }
61 
62  /** Accumulate sums for a weighted array of numbers */
63  template<typename T>
64  inline void accumulate(const T* data, const unsigned long len,
65  const double w)
66  {
67  if (dim_)
68  {
69  if (len != dim_) throw std::invalid_argument(
70  "In npstat::MultivariateWeightedSumAccumulator::accumulate:"
71  " unexpected data length");
72  }
73  else
74  {
75  // First call of this method for this particular object
76  // after construction or reset
77  if (!len) throw std::invalid_argument(
78  "In npstat::MultivariateWeightedSumAccumulator::accumulate:"
79  " data length must be positive");
80  data_.resize(len);
81  dim_ = len;
82  }
83  if (w < 0.0) throw std::invalid_argument(
84  "In npstat::MultivariateWeightedSumAccumulator::accumulate:"
85  " weight must be non-negative");
86  if (w > 0.0)
87  {
88  assert(data);
89  Precise* buf = &data_[0];
90  for (unsigned long i=0; i<len; ++i)
91  buf[i] += data[i]*w;
92  weightSum_ += w;
93  if (w > maxWeight_)
94  maxWeight_ = w;
95  }
96  ++ncalls_;
97  }
98 
99  /** Reset all accumulators */
100  inline void reset()
101  {
102  data_.clear();
103  weightSum_ = Precise();
104  maxWeight_ = 0.0;
105  dim_ = 0UL;
106  ncalls_ = 0UL;
107  }
108 
109  /** Retrieve the sum for a particular index */
110  inline const Precise& sum(const unsigned long i) const
111  {return data_.at(i);}
112 
113  /** Retrieve the mean for a particular index */
114  inline Precise mean(const unsigned long i) const
115  {
116  if (weightSum_ == Precise()) throw std::runtime_error(
117  "In npstat::MultivariateWeightedSumAccumulator::mean:"
118  " no data accumulated");
119  return data_.at(i)/weightSum_;
120  }
121 
122  /** Retrieve the vector of means */
123  inline std::vector<Precise> meanVector() const
124  {
125  if (weightSum_ == Precise()) throw std::runtime_error(
126  "In npstat::MultivariateWeightedSumAccumulator::meanVector:"
127  " no data accumulated");
128  std::vector<Precise> v(data_);
129  Precise* buf = &v[0];
130  for (unsigned long i=0; i<dim_; ++i)
131  buf[i] /= weightSum_;
132  return v;
133  }
134 
135  private:
136  std::vector<Precise> data_;
137  Precise weightSum_;
138  double maxWeight_;
139  unsigned long dim_;
140  unsigned long ncalls_;
141  };
142 }
143 
144 #endif // NPSTAT_MULTIVARIATEWEIGHTEDSUMACCUMULATOR_HH_
Definition: MultivariateWeightedSumAccumulator.hh:29
Precise averageWeight() const
Definition: MultivariateWeightedSumAccumulator.hh:54
Precise weightSum() const
Definition: MultivariateWeightedSumAccumulator.hh:38
void accumulate(const T *data, const unsigned long len, const double w)
Definition: MultivariateWeightedSumAccumulator.hh:64
unsigned long ncalls() const
Definition: MultivariateWeightedSumAccumulator.hh:44
double maxWeight() const
Definition: MultivariateWeightedSumAccumulator.hh:47
void reset()
Definition: MultivariateWeightedSumAccumulator.hh:100
const Precise & sum(const unsigned long i) const
Definition: MultivariateWeightedSumAccumulator.hh:110
Precise mean(const unsigned long i) const
Definition: MultivariateWeightedSumAccumulator.hh:114
const Precise * data() const
Definition: MultivariateWeightedSumAccumulator.hh:50
std::vector< Precise > meanVector() const
Definition: MultivariateWeightedSumAccumulator.hh:123
MultivariateWeightedSumAccumulator()
Definition: MultivariateWeightedSumAccumulator.hh:34
unsigned long dim() const
Definition: MultivariateWeightedSumAccumulator.hh:41
Definition: AbsArrayProjector.hh:14