# Re: [ROOT] Mean calculation in TH1

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Sep 15 2004 - 21:19:31 MEST

```Andrei,

The results are exactly what I expected. As soon as you set a range,
the mean is computed using the bin centers. In the particular case
where you set the range to the first and last channel, one could however
use the more precise algorithm, but this would be yet another case to
explain!

Rene Brun

On
Wed, 15 Sep 2004, Andrei Kuznetsov wrote:

>
> {
>   gROOT->Reset();
>   TCanvas *cr = new TCanvas("cr","Amplitude",200,10,800,600);
>   TH1D *h=new TH1D("Pixel","Pixel",13,0,13);
>   for(Int_t i=0;i<6;i++) h->Fill(10*gRandom->Rndm());
>   cout<<"Mean befor SetRange ="<<h->GetMean()<<endl;
>   h->GetXaxis()->SetRange(0,12);
>   cout<<"Mean after SetRange ="<<h->GetMean()<<endl;
>   h->Draw();
>   cr->Update();
>   cr->Pop();
> }
>
> The changing of the mean confuses people a little bit.
>
> Best regards,
> Andrei.
>
>
> On Wed, 15 Sep 2004, Rene Brun wrote:
>
> > Andrei,
> >
> > The member fTsumwx is incremented in the TH1::Fill methods using the true
> > value of the x coordinate. TH1::GetMean returns the mean using this value.
> >
> > However, when you zoom via TAxis::SetRange, ROOT recomputes the mean value
> > using the bin contents and the center of each bin.
> >
> > I do not see any problems with this method. If you have evidence of the
> > contrary, please send the shortest possible running script showing the
> > problem.
> >
> > Rene Brun
> >
> > Andrei Kuznetsov wrote:
> > >
> > > Hello,
> > >
> > > it looks like ROOT calculates fTsumwx (and hence Mean of a histogram) in
> > > TH1 incorrectly.
> > >
> > > But if you perform
> > > your_histogram->GetXaxis()->SetRange(a,b);
> > > after the filling (or "zoom" by hands) it works correctly.
> > >
> > > It looks likes in the first case it is not used the center of bin when
> > > fTsumwx is calculated.
> > >
> > > Best regards,
> > > Andrei.
> >
>
```

This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:09 MET