MINT2
StatisticsFinder.cpp
Go to the documentation of this file.
2 #include <math.h>
3 
4 #include <algorithm>
5 
6 #define NOT_STORED_VAL -999999.9
7 
14 StatisticsFinder::StatisticsFinder(bool mean, bool width, bool widthError, bool keepOrderedEvents) :
15  _min (0.0),
16  _max (0.0),
17  _nEvents(0 ),
18  _wSum (0.0),
19  _wSum2 (0.0),
20  _wSum3 (0.0),
21  _wSum4 (0.0),
22  _sumW (0.0),
23  _keepOrderedEvents(keepOrderedEvents)
24 {
26  if( width == 0 ) { _wSum2 = NOT_STORED_VAL; }
27  if( mean == 0 ) { _wSum = NOT_STORED_VAL; _sumW = NOT_STORED_VAL;}
28 }
29 
34  if (_keepOrderedEvents == 0){
35  ERROR_LOG << "You need to keep ordered events to calculate this value";
36  return 0;
37  }
38  return 1;
39 }
40 
46  if ((int)_sumW != _nEvents){
47  INFO_LOG << "You have used weighted events. This may make this function meaningless";
48  }
49 }
50 
54 double StatisticsFinder::median() const{
55  if ( needOrderedEvents() == 0) return 0.0;
57 
58  std::sort(_orderedEvents.begin(),_orderedEvents.end());
59 
60  int nEntries = _orderedEvents.size();
61 
62  if (nEntries % 2 == 0){
63  int lowi = (nEntries/2) - 1;
64  int highi = (nEntries/2);
65  return 0.5*(_orderedEvents.at(lowi) + _orderedEvents.at(highi));
66  }
67 
68  return _orderedEvents.at( (nEntries - 1)/2 );
69 
70 }
71 
76 bool StatisticsFinder::notEnoughInformation(const double& val) const{
77  if (val == NOT_STORED_VAL) {
78  ERROR_LOG << "You are not storing the correct information to calculate this value. Consider changing the constuctor";
79  return 0;
80  }
81  return 1;
82 }
83 
87 void StatisticsFinder::add(const double& x, const double& weight){
88  if (_nEvents == 0){
89  _min = x;
90  _max = x;
91  }
92  else{
93  if (x < _min) _min = x;
94  if (x > _max) _max = x;
95  }
96  if (_sumW != NOT_STORED_VAL) _sumW += weight;
97  if (_wSum != NOT_STORED_VAL) _wSum += x*weight;
98  if (_wSum2 != NOT_STORED_VAL) _wSum2 += x*x*weight;
99  if (_wSum3 != NOT_STORED_VAL) _wSum3 += x*x*x*weight;
100  if (_wSum4 != NOT_STORED_VAL) _wSum4 += x*x*x*x*weight;
101 
102  if ( _keepOrderedEvents == 1) _orderedEvents.push_back(x);
103 
104  _nEvents += 1.0;
105 }
106 
110 double StatisticsFinder::mean() const{
111  return expX();
112 }
113 
118  return width() /sqrt(_nEvents);
119 }
120 
125  return secondCentralMom();
126 }
127 
131 double StatisticsFinder::width() const{
132  return sqrt(secondCentralMom());
133 }
134 
139  return sqrt( sqrt( ( fourthCentralMom() - varience()*varience() )*(1.0/(8.0*_nEvents*_nEvents)) ) );
140 }
141 
145 double StatisticsFinder::expX() const{
146  if ( notEnoughInformation(_wSum) == 0 ) return 0.0;
147  return _wSum/_sumW;
148 }
149 
153 double StatisticsFinder::expX2() const{
154  if ( notEnoughInformation(_wSum2) == 0 ) return 0.0;
155  return _wSum2/_sumW;
156 }
157 
161 double StatisticsFinder::expX3() const{
162  if ( notEnoughInformation(_wSum3) == 0 ) return 0.0;
163  return _wSum3/_sumW;
164 }
165 
169 double StatisticsFinder::expX4() const{
170  if ( notEnoughInformation(_wSum4) == 0 ) return 0.0;
171  return _wSum4/_sumW;
172 }
173 
180  return expX2() - expX()*expX();
181 }
182 
189  return expX4() - 4.0*expX()*expX3() + 6.0*expX()*expX()*expX2() - 3.0*expX()*expX()*expX()*expX();
190 }
191 
196 
197 }
198 
199 #undef NOT_STORED_VAL
void warnIfWeightedEvents() const
double fourthCentralMom() const
#define INFO_LOG
std::vector< double > _orderedEvents
double secondCentralMom() const
double median() const
double expX4() const
#define ERROR_LOG
void add(const double &x, const double &weight=1.0)
double varience() const
double widthError() const
#define NOT_STORED_VAL
double expX3() const
double expX() const
double width() const
StatisticsFinder(bool mean=1, bool width=1, bool widthError=1, bool keepOrderedEvents=0)
bool needOrderedEvents() const
double meanError() const
bool notEnoughInformation(const double &val) const
double expX2() const
double mean() const