Help with PYROOT

I am a newbie with Pyroot. And I need some help. I have a histogram and I need to find the error per bin. I don’t really understand how can I do this?
The histogram is already available and I cannot change the histogram data.
Any help would be appreciated.

See the documentation of TH1:
Double_t error = h->GetBinError(bin);

Thank you for your help! I am trying this out. But I want to know if it is the same thing like getting error bars?
I know error bars give me sqrt(contents) which is basically the error. Is it the same for GetBinError?

Thank you :slight_smile:

The documentation says:

Associated errors

By default, for each bin, the sum of weights is computed at fill time. One can also call TH1::Sumw2 to force the storage and computation of the sum of the square of weights per bin. If Sumw2 has been called, the error per bin is computed as the sqrt(sum of squares of weights), otherwise the error is set equal to the sqrt(bin content). To return the error for a given bin number, do:

    Double_t error = h->GetBinError(bin);

Thank you for your response.

Could you tell me how can I add this to the Pyroot.
Is this correct?

for i in range(bin):
histo.GetBinError(bin)
return histo

Just try… (why return histo?)

return histo because I want to see the histo as well. I tried to run. It works. But is it possible to get the error in value and also the mean error in terms of value?

Also I tried to use:
gStyle.SetOptStat(“emr”)
gStyle.SetOptFit(1111)

Does this give me a mean of the errors of the bin?

See TH1::GetMean and TH1::GetMeanError

See the TStyle::SetOptStat and TStyle::SetOptFit documentation

1 Like

Thank you for your help Bellenot!

There is one thing which I don’t understand; I am sorry I am new to this platform.

The GetMeanError gives me the mean of the errors of each bin?

for i in range(bin)
MeanError = histo.GetMeanError(bin)
print (MeanError)

Is it like this?

No, the overall mean error (what would be the mean of one single bin?) You already have the individual bin error.

Yeah that’s true!

I have the histogram which displays the error bars for each bin. But what I now want is the value which I am unable to obtain.

Could you clarify which value you’re talking about? Now you have the bin errors, the mean, and the mean error. What is missing?

I mean I cannot obtain the numerical value. I have the histogram but not the true value even when I have print (MeanError), the mean error is not displayed. The same is for each bin error.

Try something like this:

for i in range(bin) :
   error = histo.GetBinError(i)
   print (error)
Mean = histo.GetMean()
print (Mean)
MeanError = histo.GetMeanError()
print (MeanError)
return histo
1 Like

You can also try with the hsimple.py tutorial:

C:\Users\bellenot\rootdev\tutorials\pyroot>python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> execfile("hsimple.py")
hsimple   : Real Time =   0.61 seconds Cpu Time =   0.31 seconds
>>> hpx.GetMean()
-0.00496930074914031
>>> hpx.GetMeanError()
0.00632272251056969
>>> hpx.GetBinError(1)
0.0
>>> hpx.GetBinError(50)
28.460498941515414
>>>
1 Like

Thank you Bellenot.

What I think is by GetMean and GetMeanError I am getting the mean of the contents and the error on this mean.
What I need :

I have 100 bins and the histogram is filled with values of energy. For every bin, I have errors. I want the mean of these errors and not the mean of the values.

I hope it would be clear now.

PS: I have the bin error by GetBinError and I want the mean of these values.

Oh OK, I see now. So you can compute it yourself, or maybe @moneta or @couet has an idea how to get this value directly…

It is difficult to compute by myself. Now I have like 100 bins but there are times it is beyond that too. So, it is a little complicated to compute by oneself.

Thank you @bellenot You are really nice and helpful! :slight_smile:

@couet and @moneta It would be great if I could receive some help from you. Thank you in advance.

How about a for loop?

avgError = 0
for i in range(bin) :
   avgError += histo.GetBinError(i)
avgError /= len(bin)