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