// @(#)root/mathcore:$Id$
// Author: G. Ganis 2012

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TStatistic
#define ROOT_TStatistic


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TStatistic                                                           //
//                                                                      //
// Statistical variable, defined by its mean, RMS and related errors.   //
// Named, streamable, storable and mergeable.                           //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TObject
#include "TObject.h"
#endif

#ifndef ROOT_TCollection
#include "TCollection.h"
#endif

#ifndef ROOT_TMath
#include "TMath.h"
#endif

#ifndef ROOT_TString
#include "TString.h"
#endif

#ifndef ROOT_TROOT
#include "TROOT.h"
#endif

class TStatistic : public TObject {

private:
   TString     fName;
   Long64_t    fN;       // Number of fills
   Double_t    fW;       // Sum of weights    
   Double_t    fW2;      // Sum of weights**2    
   Double_t    fMean;    // Mean
   Double_t    fM2;      // Second order momentum

public:
   TStatistic(const char *name = "") : fName(name), fN(0), fW(0.), fW2(0.), fMean(0.), fM2(0.) { }
   TStatistic(const char *name, Int_t n, const Double_t *val, const Double_t *w = 0);
   ~TStatistic() { }
 
   // Getters
   const char    *GetName() const { return fName; }
   ULong_t        Hash() const { return fName.Hash(); }

   inline const Long64_t &GetN() const { return fN; }
   inline const Double_t &GetM2() const { return fM2; }
   inline const Double_t &GetMean() const { return fMean; }
   inline       Double_t  GetMeanErr() const { if (fW > 0.) return TMath::Sqrt(fM2 / fW2 / fW);
                                               return 0.; }
   inline       Double_t  GetRMS() const { if (fW > 0.) { return TMath::Sqrt(fM2 / fW); } return -1; }
   inline       Double_t  GetVar() const { if (fW > 0.) {
                                            if (fN > 1) { return (fM2 / fW)*(fN / (fN-1.)); }
                                            return 0; } return -1.; }
   inline       Double_t  GetVarN() const { if (fW > 0.) { return fM2 / fW; } return -1.; }
   inline const Double_t &GetW() const { return fW; }
   inline const Double_t &GetW2() const { return fW2; }

   // Merging
   Int_t Merge(TCollection *in);

   // Fill
   inline void Fill(const Double_t &val, Double_t w = 1.) {
      fN++;
      // Incremental quantities
      Double_t tW = w + fW;
      Double_t dt = val - fMean ;
      Double_t rr = dt * w / tW ;
      fMean += rr;
      fM2 += fW * dt * rr;
      fW = tW;
      fW2 += w*w;
   }

   // Print
   void ls(Option_t *opt = "") const { Print(opt); }
   void Print(Option_t * = "") const {
      // Print this parameter content
      TROOT::IndentLevel();
      Printf(" OBJ: TStatistic\t %s = %.3g +- %.3g \t RMS = %.3g \t N = %lld",
             fName.Data(), fMean, GetMeanErr(), GetRMS(), fN);
   }
   
   ClassDef(TStatistic,1)  //Named statistical variable
};

// Implementation of Merge
inline Int_t TStatistic::Merge(TCollection *in) {
   // Merge objects in the list.
   // Returns the number of objects that were in the list.
   TIter nxo(in);
   Int_t n = 0;
   while (TObject *o = nxo()) {
      TStatistic *c = dynamic_cast<TStatistic *>(o);
      if (c) {
         fMean += c->GetMean();
         fW += c->GetW();
         fW2 += c->GetW2();
         fM2 += c->GetM2();
         fN += c->GetN();
         n++;
      }
   }
   return n;
}
#endif
 TStatistic.h:1
 TStatistic.h:2
 TStatistic.h:3
 TStatistic.h:4
 TStatistic.h:5
 TStatistic.h:6
 TStatistic.h:7
 TStatistic.h:8
 TStatistic.h:9
 TStatistic.h:10
 TStatistic.h:11
 TStatistic.h:12
 TStatistic.h:13
 TStatistic.h:14
 TStatistic.h:15
 TStatistic.h:16
 TStatistic.h:17
 TStatistic.h:18
 TStatistic.h:19
 TStatistic.h:20
 TStatistic.h:21
 TStatistic.h:22
 TStatistic.h:23
 TStatistic.h:24
 TStatistic.h:25
 TStatistic.h:26
 TStatistic.h:27
 TStatistic.h:28
 TStatistic.h:29
 TStatistic.h:30
 TStatistic.h:31
 TStatistic.h:32
 TStatistic.h:33
 TStatistic.h:34
 TStatistic.h:35
 TStatistic.h:36
 TStatistic.h:37
 TStatistic.h:38
 TStatistic.h:39
 TStatistic.h:40
 TStatistic.h:41
 TStatistic.h:42
 TStatistic.h:43
 TStatistic.h:44
 TStatistic.h:45
 TStatistic.h:46
 TStatistic.h:47
 TStatistic.h:48
 TStatistic.h:49
 TStatistic.h:50
 TStatistic.h:51
 TStatistic.h:52
 TStatistic.h:53
 TStatistic.h:54
 TStatistic.h:55
 TStatistic.h:56
 TStatistic.h:57
 TStatistic.h:58
 TStatistic.h:59
 TStatistic.h:60
 TStatistic.h:61
 TStatistic.h:62
 TStatistic.h:63
 TStatistic.h:64
 TStatistic.h:65
 TStatistic.h:66
 TStatistic.h:67
 TStatistic.h:68
 TStatistic.h:69
 TStatistic.h:70
 TStatistic.h:71
 TStatistic.h:72
 TStatistic.h:73
 TStatistic.h:74
 TStatistic.h:75
 TStatistic.h:76
 TStatistic.h:77
 TStatistic.h:78
 TStatistic.h:79
 TStatistic.h:80
 TStatistic.h:81
 TStatistic.h:82
 TStatistic.h:83
 TStatistic.h:84
 TStatistic.h:85
 TStatistic.h:86
 TStatistic.h:87
 TStatistic.h:88
 TStatistic.h:89
 TStatistic.h:90
 TStatistic.h:91
 TStatistic.h:92
 TStatistic.h:93
 TStatistic.h:94
 TStatistic.h:95
 TStatistic.h:96
 TStatistic.h:97
 TStatistic.h:98
 TStatistic.h:99
 TStatistic.h:100
 TStatistic.h:101
 TStatistic.h:102
 TStatistic.h:103
 TStatistic.h:104
 TStatistic.h:105
 TStatistic.h:106
 TStatistic.h:107
 TStatistic.h:108
 TStatistic.h:109
 TStatistic.h:110
 TStatistic.h:111
 TStatistic.h:112
 TStatistic.h:113
 TStatistic.h:114
 TStatistic.h:115
 TStatistic.h:116
 TStatistic.h:117
 TStatistic.h:118
 TStatistic.h:119
 TStatistic.h:120
 TStatistic.h:121
 TStatistic.h:122
 TStatistic.h:123
 TStatistic.h:124