How to find area under histogram

Hello. I’m trying to find the area under a TH1D histogram. How can I do it as simply as possible? Here is some detail:

TH1D *h = new TH1D(“h”,“Angle”,100,0,5);
TTreeReaderValue<Int_t> ntrack(dataReader,“ntrack”); //Gets data (y-axis)
TTreeReaderArray<Double_t> data(dataReader, “data”); //Gets data (x-axis)
for ( UChar_t i = 0; i < ntrack; i++ ) {
h->Fill(data[i]);} //Fills histogram with the data
hCanvas = new TCanvas(“hCanvas”, “h”,640,480);
h->Draw(); //Draws histogram

That was the important bits, and I left out optional stuff that wasn’t necessary. So again, I want a way to find the area under the histogram (like area under a curve) from a minimum value to a maximum value, say, 0 to 5. How can I do this? Is there an integration command I can use?


_ROOT Version: 6.15/01
_Platform: Ubuntu
Compiler: Not Provided

Yes, and very very surprisingly it is called … Integral

Note however that histo->Integral(a,b) is NOT -histo->Integral(b,a). I don’t exactly understand the results when low/high bins are switched:

TH1F h("h","h",10,0,10);
h.Integral(2,2); // 1.0
h.Integral(1,2); // 1.0
h.Integral(2,1); // also 1.0!
h.Integral(3,1); // zero!

Works well. Thanks for the quick response!

Also, another similar question relating to this.

What I want to do is basically solve for the upper bound value if given an area. In other words:

If I have h->Integral(0,x) = area; where 0 is the lower bound and “x” is the upper bound (and area is just the output of the integrate function), how can I solve for x given area?
The reason why I want to do this is to find the exact upper bound in order to get a very specific output. Like if I was given x + 5 = y and I know y = 2, solving for x is simple, expect here it’s a little more complicated.

What I’ve tried is:
float x;
float a = h->Integral(0,x);
cout << "x-value = " << a << endl;
//doesn’t work


Please note that the TH1::Integral method returns the integral of bin contents in a given bin range so, for a 1-D histogram, you should use something like this:

double a = h->Integral(h->FindFixBin(0), h->FindFixBin(x), ""); // "" or "width"

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