Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TStatistic.cxx
Go to the documentation of this file.
1// @(#)root/base:$Id$
2// Author: G. Ganis 2012
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TStatistic.h"
13
14#include "TROOT.h"
15#include "TList.h"
16
17// clang-format off
18/**
19* \class TStatistic
20* \ingroup MathCore
21* \brief Statistical variable, defined by its mean and variance (RMS). Named, streamable, storable and mergeable.
22*/
23// clang-format on
24
26
27////////////////////////////////////////////////////////////////////////////
28/// \brief Constructor from a vector of values
29/// \param[in] name The name given to the object
30/// \param[in] n The total number of entries
31/// \param[in] val The vector of values
32/// \param[in] w The vector of weights for the values
33///
34/// Recursively calls the TStatistic::Fill() function to fill the object.
35TStatistic::TStatistic(const char *name, Int_t n, const Double_t *val, const Double_t *w)
36 : fName(name), fN(0), fW(0.), fW2(0.), fM(0.), fM2(0.), fMin(TMath::Limits<Double_t>::Max()), fMax(TMath::Limits<Double_t>::Min())
37{
38 if (n > 0) {
39 for (Int_t i = 0; i < n; i++) {
40 if (w) {
41 Fill(val[i], w[i]);
42 } else {
43 Fill(val[i]);
44 }
45 }
46 }
47}
48
49////////////////////////////////////////////////////////////////////////////////
50/// TStatistic destructor.
52{
53 // Required since we overload TObject::Hash.
55}
56
57////////////////////////////////////////////////////////////////////////////////
58/// \brief Increment the entries in the object by one value-weight pair.
59/// \param[in] val Value to fill the Tstatistic with
60/// \param[in] w The weight of the value
61///
62/// Also updates statistics in the object. For number of entries, sum of weights,
63/// sum of squared weights and sum of (value*weight), one extra value is added to the
64/// statistic. For the sum of squared (value*weight) pairs, the function uses formula 1.4
65/// in Chan-Golub, LeVeque : Algorithms for computing the Sample Variance (1983),
66/// genralized by LM for the case of weights:
67/// \f[
68/// \frac{w_j}{\sum_{i=0}^{j} w_i \cdot \sum_{i=0}^{j-1} w_i} \cdot
69/// \left(
70/// \sum_{i=0}^{j} w_i \cdot val_i -
71/// \sum_{i=0}^{j} \left(w_i \cdot val_i\right)
72/// \right)
73/// \f]
74///
75/// The minimum(maximum) is computed by checking that the fill value
76/// is either less(greater) than the current minimum(maximum)
78
79
80 if (w == 0) return;
81 // increase data count
82 fN++;
83
84 // update sum of weights
85 Double_t tW = fW + w;
86
87 // update sum of (value * weight) pairs
88 fM += w * val;
89
90 // update minimum and maximum values
91 fMin = (val < fMin) ? val : fMin;
92 fMax = (val > fMax) ? val : fMax;
93
94// Double_t dt = val - fM ;
95 if (tW == 0) {
96 Warning("Fill","Sum of weights is zero - ignore current data point");
97 fN--;
98 return;
99 }
100
101 if (fW != 0) { // from the second time
102 Double_t rr = ( tW * val - fM);
103 fM2 += w * rr * rr / (tW * fW);
104 }
105 fW = tW;
106 fW2 += w*w;
107}
108
109////////////////////////////////////////////////////////////////////////////////
110/// \brief Print the content of the object
111///
112/// Prints the statistics held by the object in one line. These include the mean,
113/// mean error, RMS, the total number of values, the minimum and the maximum.
116 Printf(" OBJ: TStatistic\t %s \t Mean = %.5g +- %.4g \t RMS = %.5g \t Count = %lld \t Min = %.5g \t Max = %.5g",
117 fName.Data(), GetMean(), GetMeanErr(), GetRMS(), GetN(), GetMin(), GetMax());
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// \brief Merge implementation of TStatistic
122/// \param[in] in Other TStatistic objects to be added to the current one
123///
124/// The function merges the statistics of all objects together to form a new one.
125/// Merging quantities is done via simple addition for the following class data members:
126/// - number of entries fN
127/// - the sum of weights fW
128/// - the sum of squared weights fW2
129/// - the sum of (value*weight) fM
130///
131/// The sum of squared (value*weight) pairs fM2 is updated using the same formula as in
132/// TStatistic::Fill() function.
133///
134/// The minimum(maximum) is updated by checking that the minimum(maximum) of
135/// the next TStatistic object in the queue is either less(greater) than the current minimum(maximum).
137
138 // Let's organise the list of objects to merge excluding the empty ones
139 std::vector<TStatistic*> statPtrs;
140 if (this->fN != 0LL) statPtrs.push_back(this);
141 TStatistic *statPtr;
142 for (auto o : *in) {
143 if ((statPtr = dynamic_cast<TStatistic *>(o)) && statPtr->fN != 0LL) {
144 statPtrs.push_back(statPtr);
145 }
146 }
147
148 // No object included this has entries
149 const auto nStatsPtrs = statPtrs.size();
150
151 // Early return possible in case nothing has been filled
152 if (nStatsPtrs == 0) return 0;
153
154 // Merge the statistic quantities into local variables to then
155 // update the data members of this object
156 auto firstStatPtr = statPtrs[0];
157 auto N = firstStatPtr->fN;
158 auto M = firstStatPtr->fM;
159 auto M2 = firstStatPtr->fM2;
160 auto W = firstStatPtr->fW;
161 auto W2 = firstStatPtr->fW2;
162 auto Min = firstStatPtr->fMin;
163 auto Max = firstStatPtr->fMax;
164 for (auto i = 1U; i < nStatsPtrs; ++i) {
165 auto c = statPtrs[i];
166 double temp = (c->fW) / (W)*M - c->fM;
167 M2 += c->fM2 + W / (c->fW * (c->fW + W)) * temp * temp;
168 M += c->fM;
169 W += c->fW;
170 W2 += c->fW2;
171 N += c->fN;
172 Min = (c->fMin < Min) ? c->fMin : Min;
173 Max = (c->fMax > Max) ? c->fMax : Max;
174 }
175
176 // Now update the data members of this object
177 fN = N;
178 fW = W;
179 fW2 = W2;
180 fM = M;
181 fM2 = M2;
182 fMin = Min;
183 fMax = Max;
184
185 return nStatsPtrs;
186
187}
#define c(i)
Definition RSha256.hxx:101
double Double_t
Definition RtypesCore.h:59
const char Option_t
Definition RtypesCore.h:66
#define templateClassImp(name)
Definition Rtypes.h:408
#define N
char name[80]
Definition TGX11.cxx:110
void Printf(const char *fmt,...)
Collection abstract base class.
Definition TCollection.h:63
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:879
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:2811
Statistical variable, defined by its mean and variance (RMS).
Definition TStatistic.h:33
TString fName
Name given to the TStatistic object.
Definition TStatistic.h:36
~TStatistic()
TStatistic destructor.
Double_t GetMeanErr() const
Definition TStatistic.h:59
Double_t fW2
Sum of squared weights.
Definition TStatistic.h:39
Double_t GetMin() const
Definition TStatistic.h:64
Double_t fW
Sum of weights.
Definition TStatistic.h:38
TStatistic(const char *name="")
Definition TStatistic.h:47
void Fill(Double_t val, Double_t w=1.)
Increment the entries in the object by one value-weight pair.
Long64_t GetN() const
Definition TStatistic.h:55
Double_t GetMax() const
Definition TStatistic.h:65
Double_t GetMean() const
Definition TStatistic.h:58
Double_t fMin
Minimum value in the TStatistic object.
Definition TStatistic.h:42
void Print(Option_t *="") const
Print the content of the object.
Double_t GetRMS() const
Definition TStatistic.h:60
Double_t fMax
Maximum value in the TStatistic object.
Definition TStatistic.h:43
Double_t fM
Sum of elements (i.e. sum of (val * weight) pairs.
Definition TStatistic.h:40
Double_t fM2
Second order momentum.
Definition TStatistic.h:41
Int_t Merge(TCollection *in)
Merge implementation of TStatistic.
Long64_t fN
Number of fills.
Definition TStatistic.h:37
const char * Data() const
Definition TString.h:369
const Int_t n
Definition legend1.C:16
void CallRecursiveRemoveIfNeeded(TObject &obj)
call RecursiveRemove for obj if gROOT is valid and obj.TestBit(kMustCleanup) is true.
Definition TROOT.h:395
TMath.
Definition TMathBase.h:35