Get total energy from an histogram


I have a question about: I have an histogram that gives the energy distribution in a detector. So, on the X axis I have the energy range and on the Y axis the total number of times each bin is filled.

Now, I want to get the total energy… so I thought that the Integral() method should help me. But, if I understood correctly, the method simply returns the sum of the bin contents…

So is there a quicker way or I have to do something like:

for(i=1;iGetNbinsX(); i++)
energy += h->GetBinContent(i)*h->GetBinCenter(i)


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

The loop you suggest seems fine assuming in each bin all the counts are comming from the same value equal to the bin center. May be @moneta has a better sugestion.


It was an assumption I implicitly made since I don’t know how to consider all the possibile energies in a given bin.

if you have access to the data before the histogram filling you can compute the exact total energy.

Mh I don’t think so, because data come from a Geant4 simulation and the histos are automatically filled.

ok. maybe @moneta has more hints to provide.

I was thinking that things could be easier than expected… since my bins have all the same width, let’s say 0.1 MeV, I could simply take for each bin the total number of entries and multiply by 0.1MeV to get the total energy deposit in that bin. Then, I sum all contributes.

if they are the same width you can do what you suggest assuming the x-axis starts at 0 MeV. And as the integral is the sum of the contents of the bins may be it is enough to do 0.1*integral ?

Ok, so the first two options give the same output, which is in both cases different from the one I get using my code…

So, following your suggestion, the associated error could be computed as hist->GetMeanError()*hist->GetSumofWeights()… right?

I added some small notes about the error in the old post.

Sorry, I didn’t see… but this will be true if I use your last option with GetStats

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.