Logo ROOT   6.13/01
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 
13 /**
14 
15 \class TStatistic
16 
17 Statistical variable, defined by its mean and variance (RMS).
18 Named, streamable, storable and mergeable.
19 
20 @ingroup MathCore
21 
22 */
23 
24 
25 
26 #include "TStatistic.h"
27 
28 
29 templateClassImp(TStatistic);
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 /// Constructor from a vector of values
33 
34 TStatistic::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.)
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.
50 
52 {
53  // Required since we overload TObject::Hash.
54  ROOT::CallRecursiveRemoveIfNeeded(*this);
55 }
56 
57 void TStatistic::Fill(Double_t val, Double_t w) {
58  // Incremental quantities
59  // use formula 1.4 in Chan-Golub, LeVeque
60  // Algorithms for computing the Sample Variance (1983)
61  // genralized by LM for the case of weights
62 
63  if (w == 0) return;
64 
65  fN++;
66 
67  Double_t tW = fW + w;
68  fM += w * val;
69 
70 // Double_t dt = val - fM ;
71  if (tW == 0) {
72  Warning("Fill","Sum of weights is zero - ignore current data point");
73  fN--;
74  return;
75  }
76  if (fW != 0) { // from the second time
77  Double_t rr = ( tW * val - fM);
78  fM2 += w * rr * rr / (tW * fW);
79  }
80  fW = tW;
81  fW2 += w*w;
82 }
83 
84 
85 void TStatistic::Print(Option_t *) const {
86  // Print this parameter content
87  TROOT::IndentLevel();
88  Printf(" OBJ: TStatistic\t %s = %.5g +- %.4g \t RMS = %.5g \t N = %lld",
89  fName.Data(), GetMean(), GetMeanErr(), GetRMS(), fN);
90 }
91 
92 
93 // Implementation of Merge
94 Int_t TStatistic::Merge(TCollection *in) {
95  // Merge objects in the list.
96  // Returns the number of objects that were in the list.
97  TIter nxo(in);
98  Int_t n = 0;
99  while (TObject *o = nxo()) {
100  TStatistic *c = dynamic_cast<TStatistic *>(o);
101  if (c) {
102  if (fW == 0 || c->fW == 0 || ((fW + c->fW) == 0) ) {
103  Error("Merge","Zero sum of weights - cannot merge data from %s",c->GetName() );
104  continue;
105  }
106  double temp = (c->fW)/(fW) * fM - c->fM;
107  fM2 += c->fM2 + fW/(c->fW*(c->fW + fW) ) * temp * temp;
108  fM += c->fM;
109  fW += c->fW;
110  fW2 += c->fW2;
111  fN += c->fN;
112  n++;
113  }
114  }
115  return n;
116 }
const char * GetName() const
Definition: TStatistic.h:52
Double_t GetRMS() const
Definition: TStatistic.h:60
Double_t GetMean() const
Definition: TStatistic.h:58
Double_t GetMeanErr() const
Definition: TStatistic.h:59
Double_t fM2
Definition: TStatistic.h:43
Double_t fM
Definition: TStatistic.h:42
void Print(Option_t *="") const
Definition: TStatistic.cxx:85
Double_t fW
Definition: TStatistic.h:40
TStatistic(const char *name="")
Definition: TStatistic.h:47
TString fName
Definition: TStatistic.h:38
Statistical variable, defined by its mean and variance (RMS).
Definition: TStatistic.h:35
Long64_t fN
Definition: TStatistic.h:39
~TStatistic()
TStatistic destructor.
Definition: TStatistic.cxx:51
Double_t fW2
Definition: TStatistic.h:41
void Fill(Double_t val, Double_t w=1.)
Definition: TStatistic.cxx:57
Int_t Merge(TCollection *in)
Definition: TStatistic.cxx:94