Logo ROOT   6.08/07
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 
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 
49  // Incremental quantities
50  // use formula 1.4 in Chan-Golub, LeVeque
51  // Algorithms for computing the Sample Variance (1983)
52  // genralized by LM for the case of weights
53 
54  if (w == 0) return;
55 
56  fN++;
57 
58  Double_t tW = fW + w;
59  fM += w * val;
60 
61 // Double_t dt = val - fM ;
62  if (tW == 0) {
63  Warning("Fill","Sum of weights is zero - ignore current data point");
64  fN--;
65  return;
66  }
67  if (fW != 0) { // from the second time
68  Double_t rr = ( tW * val - fM);
69  fM2 += w * rr * rr / (tW * fW);
70  }
71  fW = tW;
72  fW2 += w*w;
73 }
74 
75 
76 void TStatistic::Print(Option_t *) const {
77  // Print this parameter content
79  Printf(" OBJ: TStatistic\t %s = %.5g +- %.4g \t RMS = %.5g \t N = %lld",
80  fName.Data(), GetMean(), GetMeanErr(), GetRMS(), fN);
81 }
82 
83 
84 // Implementation of Merge
86  // Merge objects in the list.
87  // Returns the number of objects that were in the list.
88  TIter nxo(in);
89  Int_t n = 0;
90  while (TObject *o = nxo()) {
91  TStatistic *c = dynamic_cast<TStatistic *>(o);
92  if (c) {
93  if (fW == 0 || c->fW == 0 || ((fW + c->fW) == 0) ) {
94  Error("Merge","Zero sum of weights - cannot merge data from %s",c->GetName() );
95  continue;
96  }
97  double temp = (c->fW)/(fW) * fM - c->fM;
98  fM2 += c->fM2 + fW/(c->fW*(c->fW + fW) ) * temp * temp;
99  fM += c->fM;
100  fW += c->fW;
101  fW2 += c->fW2;
102  fN += c->fN;
103  n++;
104  }
105  }
106  return n;
107 }
const char * GetName() const
Returns name of object.
Definition: TStatistic.h:62
Double_t GetRMS() const
Definition: TStatistic.h:70
Double_t GetMean() const
Definition: TStatistic.h:68
Double_t GetMeanErr() const
Definition: TStatistic.h:69
Double_t fM2
Definition: TStatistic.h:53
return c
const char Option_t
Definition: RtypesCore.h:62
Double_t fM
Definition: TStatistic.h:52
int Int_t
Definition: RtypesCore.h:41
void Print(Option_t *="") const
This method must be overridden when a class wants to print itself.
Definition: TStatistic.cxx:76
Double_t fW
Definition: TStatistic.h:50
#define templateClassImp(name)
Definition: Rtypes.h:325
TString fName
Definition: TStatistic.h:48
Statistical variable, defined by its mean and variance (RMS).
Definition: TStatistic.h:45
Long64_t fN
Definition: TStatistic.h:49
Collection abstract base class.
Definition: TCollection.h:48
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:925
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition: TROOT.cxx:2618
#define Printf
Definition: TGeoToOCC.h:18
double Double_t
Definition: RtypesCore.h:55
Double_t fW2
Definition: TStatistic.h:51
void Fill(Double_t val, Double_t w=1.)
Definition: TStatistic.cxx:48
Mother of all ROOT objects.
Definition: TObject.h:37
Int_t Merge(TCollection *in)
Definition: TStatistic.cxx:85
const Int_t n
Definition: legend1.C:16
char name[80]
Definition: TGX11.cxx:109
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:911
const char * Data() const
Definition: TString.h:349