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