Histogram sum

Hi guys,

I have an histo h giving the energy distribution in a given material. On the x axis I have the energy value and on the Y axis the corresponding number of occurrence. If I want to get the total energy, how should I do? Should I take the integral, namely h->Integral()?


I used

int first_bin = hist->FindFirstBinAbove(E_threshold,1); //1 for x axis
int last_bin = hist->FindLastBinAbove(E_threshold,1);

Is it correct?

Try (sensitive to user’s changes to the current x-axis range):

std::cout << h->GetMean() * h->Integral() << "\n"; // mean * integral

and/or (valid only for the “entire” x-axis range):

std::cout << h->GetMean() * h->GetSumOfWeights() << "\n";

Another good idea would be (sensitive to user’s changes to the current x-axis range):

// change the x-axis range, if desired, and then ...
Double_t stats[(TH1::kNstat)]; // sums of w, w^2, w*x, w*x^2, ...
std::cout << stats[2] << "\n"; // sum of w*x

Then the error could be (whatever you like more):

std::cout << " +- " << TMath::Sqrt(stats[3]) << "\n"; // sqrt(sum of w*x^2)

or (note: “stats[1] / stats[0]” is the “average” weight):

std::cout << " +- " << TMath::Sqrt((stats[1] / stats[0]) * stats[3]) << "\n";


std::cout << " +- " << TMath::Sqrt(TMath::Sq(mean_error * integral) + TMath::Sq(mean * integral_error)) << "\n";

Hi @Wile_E_Coyote,

Why should I do GetMean()? And what do you mean by *?

Do you think what I did is wrong?

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