CMS 3D CMS Logo

Accumulator.cc
Go to the documentation of this file.
2 #include <ostream>
3 
5 : sum_(0.),
6  sumOfSquares_(0.),
7  weightedSum_(0.),
8  sumOfWeights_(0.),
9  n_(0)
10 {
11 }
12 
13 
14 void Accumulator::addEntry(double value, double weight) {
15  sum_ += value;
16  sumOfSquares_ += (value*value);
17  weightedSum_ += value*weight;
19  ++n_;
20 }
21 
22 
23 double Accumulator::mean() const {
24  return sum_/n_;
25 }
26 
27 
28 double Accumulator::variance() const {
29  double numerator = sumOfSquares_ - sum_*mean();
30  unsigned long denominator = n_-1;
31  return numerator/denominator;
32 }
33 
34 
35 double Accumulator::weightedMean() const {
36  return weightedSum_ / sumOfWeights_;
37 }
38 
39 
40 std::ostream& operator<<(std::ostream & os,const Accumulator & stat) {
41  os << "entries: " << stat.nEntries();
42  if(stat.nEntries() > 0) {
43  os << " Mean: " << stat.mean();
44  }
45  if(stat.nEntries() > 1) {
46  os << " Sigma: " << stat.sigma();
47  }
48  return os;
49 }
50 
51 
void addEntry(double value, double weight=1.)
Definition: Accumulator.cc:14
double mean() const
Definition: Accumulator.cc:23
double weightedSum_
Definition: Accumulator.h:32
Definition: weight.py:1
unsigned long n_
Definition: Accumulator.h:34
unsigned long nEntries() const
Definition: Accumulator.h:27
double sigma() const
Definition: Accumulator.h:23
double sum_
Definition: Accumulator.h:30
double weightedMean() const
Definition: Accumulator.cc:35
Definition: value.py:1
std::ostream & operator<<(std::ostream &os, const Accumulator &stat)
Definition: Accumulator.cc:40
double variance() const
Definition: Accumulator.cc:28
double sumOfSquares_
Definition: Accumulator.h:31
double sumOfWeights_
Definition: Accumulator.h:33