Calculation of Mean when Projecting TH2

From: Chris Schilling <cschill_at_slac.stanford.edu>
Date: Wed, 16 Apr 2008 12:40:39 -0700


Hi,

I notice that if you fill a TH1, ROOT will use the left bin edge to calculate the mean. However if you fill a TH2, then project ROOT will use the bin center to calculate the mean.

Here is a simple macro to illustrate.

{
  TCanvas canvas ;
  canvas.Divide(2) ;
  TH2D h2("h2","h2",5,0,5,5,0,5) ;
  TH1D h1("h1","h1",5,0,5) ;
  for (Int_t i=0; i<9; i++) { h2.Fill(0,0) ; h1.Fill(0) ;}   for (Int_t i=0; i<1; i++) { h2.Fill(1,1) ; h1.Fill(1) ;}   TH1 * hp = (TH1D*)h2.ProjectionX() ;
  canvas.cd(1) ; h1.DrawCopy() ;
  canvas.cd(2) ; hp->DrawCopy() ;
}

We fill a TH1 with 0 9 times and with 1 1 time, so the mean should be 1/10 = 0.1 (left plot). We fill a TH2 with (0,0) 9 times and (1,1) 1 time. So, a projection of either axis will give the same histogram. However, the mean in the plot on the left is (9*0.5 + 1*1.5)/10 = 0.6. WHY????

You can easily get around this by subtracting half the bin width from the mean (0.6 - 0.5 = 0.1). However, this effect is inherent in any profile plots drawn from the TH2.

Is there in option when drawing profile plots to tell ROOT to use the left bin edge rather than the center?

Chris Received on Wed Apr 16 2008 - 21:40:47 CEST

This archive was generated by hypermail 2.2.0 : Fri Apr 18 2008 - 11:50:01 CEST