Re: [ROOT] Getting mean of Ntuple variables

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Wed Mar 20 2002 - 16:02:07 MET


Hi Rene, 

On Wed, 20 Mar 2002 12:18:50 +0000
Rene Brun <Rene.Brun@cern.ch> wrote
concerning "Re: [ROOT] Getting mean of Ntuple variables":
> Hi Christian,
> 
> A TTree::GetMean,RMS could be implemented. I am however reluctant to
> implement these functions. Very likely you would like a
>    
>   GetMean(variable, selection) 
> 
> and also the full machinery with
> 
>   GetMean(const char *varexp, const char *selection,
>           Int_t nentries=1000000000,  Int_t firstentry=0);
>
> etc for GetRMS. 
>
> This functionality is already provided by TTree::Draw as indicated
> by Pasha. I believe it is more general to do
>
>    tree.Draw("var>>hist,"some selection",etc);
>    hist->getMean();

It does require that you make a histogram, which is not necessarily
what you want, especially not in batch processing, and the like.  And
sometimes I guess you really would like an unbinned mean and RMS.  I
really see no reason why not to give the users the possiblity of
getting the mean and RMS directly for the horses mouth (sample), also
in that it makes it easier to make various kinds of statistical
analysis on the trees.  


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

> 
> Rene Brun
> 
> 
> Christian Holm Christensen wrote:
> > 
> > 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:46 MET