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