Retrieving axis values from bin center or bin number

Hi All,

I have a TH2F histogram, and I would like to obtain the x and y values from either the bin center or bin number value. So far I have tried:

``````bin_num = h1->FindBin(y_adj,z_adj,0);

bin_centerx = ((TAxis*)h1->GetXaxis())->GetBinCenter(bin_num);
bin_centery = ((TAxis*)h1->GetYaxis())->GetBinCenter(bin_num);

``````

to no avail. What I do return is the center of the given bin, and not the axis values (x, y). Thank you all so much for your help with this.

Frank

ROOT Version: 6.22.06
Platform: MacOSX
Compiler: GCC

I’m sure @couet knows how to retrieve those values. But did you try to search the forum? For example:

Hi @bellenot,

Thanks for the links and suggestions. I did search the forum before my post, and I assume it is my ignorance, but I was unable to find a method that was clear to me to retrieve these values.

Hi @couet do you have any suggestions?

Thank you all for your time and help with this.

``````bin_centerx = h1->GetXaxis()->GetBinCenter(h1->GetXaxis()->FindFixBin(somex));
bin_centery = h1->GetYaxis()->GetBinCenter(h1->GetYaxis()->FindFixBin(somey));
``````

The method you suggested works very well. Thank you. I am still curious to learn if there is a method to get the (x,y) bin center directly from knowing the bin number.

Thanks.

Hi All,

Also what is the difference between FindBin() and FindFixBin(). From the reference materials they seem to look the same:

FindBin():

" Return Global bin number corresponding to x,y,z.

2-D and 3-D histograms are represented with a one dimensional structure. This has the advantage that all existing functions, such as GetBinContent, GetBinError, GetBinFunction work for all dimensions. This function tries to extend the axis if the given point belongs to an under-/overflow bin AND if CanExtendAllAxes() is true.

Reimplemented in TH2Poly.

Definition at line 3680 of file TH1.cxx."

FindFixBin():

" Return Global bin number corresponding to x,y,z.

2-D and 3-D histograms are represented with a one dimensional structure. This has the advantage that all existing functions, such as GetBinContent, GetBinError, GetBinFunction work for all dimensions. This function DOES NOT try to extend the axis if the given point belongs to an under-/overflow bin.

Definition at line 3713 of file TH1.cxx."

Well the difference shows in th help…

FindBin;

``````This function tries to extend the axis
``````

FindFixBin

``````This function DOES NOT try ...
``````

Hi @couet,

I am still learning how to read, lol. Thank you so much.

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