Number of bins of TH1 histogram

hi,
I have a 1D histogram

TH1F *h=new TH1F(“h”,“histogram”,100,0.,100.);

I would to get the number of bins from range 0.0 to 50.0 of the histogram. What function can I use to get the number of bins on the X-axis of this histogram. I understand it would be 50 here, but I just wanted to find out if I can do this “programatically”

Thanks,
Christine

I guess something close to:


nbin_range = ((x2_range - x1_range)/(xmax-xmin))*nbin

or simpler

nbin_range = h->GetXaxis()->FindBin(50.0) - h->GetXaxis()->FindBin(0.)
Rene

Thanks this helped.

Christine

Does this method only get the number of bins at x = 50 and x = 0, then subtract them? Or does it compute all the bins between x = 0 up to x = 50?

I’m having a similar issue with this code, I have :

            xrange1 = frameHisto->GetXaxis()->FindBin(100.0) - frameHisto->GetXaxis()->FindBin(0.);
            xrange2 = frameHisto->GetXaxis()->FindBin(300.0) - frameHisto->GetXaxis()->FindBin(200.0);
            xrange3 = frameHisto->GetXaxis()->FindBin(500.0) - frameHisto->GetXaxis()->FindBin(400.0);
            xrange4 = frameHisto->GetXaxis()->FindBin(600.0) - frameHisto->GetXaxis()->FindBin(500.0);

xrange1,2,3,4 would always return the same number of entries even though I’m looping through different histograms. Any ideas?

also what is the difference between the method above and Th2I->Integral(binx,binx2) ?

Thank you in advance

Hi,

The function frameHisto->GetXaxis()->FindBin(100.0) returns the bin number corresponding to the x value = 100.

The function Th2I->Integral(binx,binx2) returns the sum of all the contents (integral) of the bins (the y values) between the bin number binx and binx2

Best Regards

Lorenzo

root.cern.ch/root/htmldoc/TH1.h … Integral@1
root.cern.ch/root/htmldoc/TAxis … is:FindBin

Ok thank you, also what if I wanted to reverse and find the X and Y value of a bin/or data point on a histogram, How would I do that?

i.e I have some data point on a histogram, I want to extract the X and Y location of that point. How?

Thanks

use TH1::GetXaxis() and TH1::GetYAxis() to get the corresponding axis and then
TAxis::GetBinCenter(ixbin) and TAxis::GetBinCenter(iybin)

Lorenzo

also, are there anyways to do a linear fit on a 2D histogram?

Hi,

Yes, you need to define a TF2 and use the “++” sign. Something like

TF2 * f2 = new TF2("f2","1 ++ x ++ y ++ x*x ++ y*y ++ x*x*y*y");

Lorenzo