Re: [ROOT] Getting mean of Ntuple variables

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Thu Mar 14 2002 - 13:39:10 MET


Hi, 

On Wed, 13 Mar 2002 13:19:55 -0800
"Aron, Navneet" <navneet@SLAC.stanford.edu> wrote
concerning "[ROOT] Getting mean of Ntuple variables":
> 
> Hi,
> Can any one tell me how do we get the mean , rms values of  the various ntuple variables in the root file.
> My try is as follows: 
> 
> {
> gROOT->Reset();
> TFile *r = new TFile("g1onaxis.root");
> TTree *tree = (TTree*)r->Get("TKR_Hits_In_Lyr_0");
> Float_t mean = tree->GetMean();
> }

While the suggestion given by Pasha is indeed the right way to go, it
would be nice to have methods like 

  Double_t TTree::GetMean(const Char_t* leaf)
  Double_t TTree::GetRMS(const Char_t* leaf)

(should not be const, since the tree should cache the calculation),
and maybe higher moments too.  

I once made a small class TStatistics, which can help you do low-level
statistics (average, variance, covariance) on a N-dimensional sample.
The advantage of this class is, that it minimizes the rounding errors
due to finite machine precision, _and_ you only need to loop over the
data once to make the (co)variance.  The algorithm is the same as the
one used in TPrincipal.  It also has a service (static) method for a
1D sample, so that one can easily use it to caculate the average and
variance of a leaf variable: 

  TTree* tree = (TTree*)gDirectory->Get("Tree"); 
  Double_t x; 
  Double_t mean = 0;
  Double_t rms  = 0;
  tree->SetBranchAddress("X", &x); 
  Ssiz_t  n = tree->GetEntries();
  for (Int_t i = 0; i < Int_t(n); i++) {
    tree->GetEntry(i); 
    TStatistics::AddPoint(x, i, mean, rms); 
  }

Notice, that the average and variance is calculated unbinned, unlike
in a histogram. 

You can get the TStatistics class from my web site [1].  

The tar-ball is standalone, but also demonstrates how to use Autotools
with ROOT. 

Yours, 

Christian Holm Christensen -------------------------------------------
Address: Sankt Hansgade 23, 1. th.           Phone:  (+45) 35 35 96 91 
         DK-2200 Copenhagen N                Cell:   (+45) 28 82 16 23
         Denmark                             Office: (+45) 353  25 305 
Email:   cholm@nbi.dk                        Web:    www.nbi.dk/~cholm

    
[1]  http://cholm.home.cern.ch/cholm/root/#tstatistics



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:45 MET