# TProfile differs from mean

Dear ROOT friends,

I have tested the TProfile output with the example below. The closest values that I get between the TProfile and the mean are:

`````` The TH2D mean value is: 3.267409
The TProfile  value is: 3.267731
``````

Is it possible / how can I get the TProfile to always return the exact mean?

Thanks,
Jonatan

``````{
TH2D* htot = new TH2D("htot", "", 10, 0, 10, 1000, 0, 10);

TF2 f2("f2", "xygaus", 0, 100, 0, 100);

f2.SetParameters(1, 0, 5, 0, 4);

double x;
double y;

double bin_sum     = 0;
int    bin_entries = 0;

for(int i=0; i<2000; i++)
{
f2.GetRandom2(x,y);

if (y < 0.1) continue;  // Just in case, avoiding edges
if (y > 9.9) continue;  // Just in case, avoiding edges

if (x > 1.1 && x < 1.9)  // Just in case, avoiding edges
{
htot->Fill(x,y);

bin_entries++;
bin_sum += y;
}
}

TProfile* hprof = (TProfile*)htot->ProfileX();

printf("\n");
printf(" The TH2D mean value is: %f\n", bin_sum / bin_entries);
printf(" The TProfile  value is: %f\n", hprof->GetBinContent(2));
printf("\n");
}

``````

ROOT Version: 6.10/09

It seems to be a binning issue if you multiply by 10 the number of Y bins for `htot` you get:

`````` The TH2D mean value is: 3.267409
The TProfile  value is: 3.267421
``````

by 100:

`````` The TH2D mean value is: 3.267409
The TProfile  value is: 3.267409
``````

Thank you Olivier.

Then I guess I need a very large number of bins for TProfile to reproduce the exact mean. Therefore, TProfile is good only for visualizations / approximations, but not for the real thing, if any.

Best,
Jonatan

May be @moneta can comment about it but I am not sure the only goal of TProfile is to compute the mean (?). Also, seems to me, like for any histogram the “definition”/ “granularity” along an axis is defined by the number of bins. If on the opposite you create `htot` with only two bins along `y` axis I guess you would not be surprised to get a very rough value. Which is indeed the case. 2 bins gives:

`````` The TH2D mean value is: 3.267409
The TProfile  value is: 3.750000
``````

Hi,

The difference is that when calling TH2::ProfileX the resulting mean is computed using the bin center, while when using directly TH2 the mean is computed at filling time using the real x,y vaLues of each entry

Lorenzo

Thank you Lorenzo, but I’m sorry if I don’t fully understand. Can you maybe give an example?

It sounds quite clear. And that what I explained you. The smallest the bins are the closest the bins center value is to the real value. Therefore, and that’s what we saw, the more bins you have the closest the Mean computed by the profile is close to the mean computed from the real value.

Now I understand, thanks! I didn’t get before what Lorenzo meant by “using the bin center” This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.