# Converting from extended axis bin number to non-extended bin number, FindBin() vs FindFixBin()

Hi All,

I am curious to see if there is a quick way to convert from the extended axis bin number, such as the one that would be obtained when using the FindBin() method to the non-extended bin number that would be obtained from FindFixBin().

Thanks!

-Frank

ROOT Version: 6.22/06
Platform: MacOS Big Sur
Compiler: gcc

Hi,

There is no different bin number, FindBin(x), if the axis is extendable and if x is larger than the axis maximum, will extend the axis and then return the corresponding bin number. FindFixBin(x) will instead, for the same x case, return as bin number, the overflow number (nbins+1).

Best,

Lorenzo

@moneta I think I previously misunderstood the difference between the two methods, FindBin() and FindFixBin().

As a test I have done the following:

I generated a histogram:

``````TH2F *h_p = new TH2F("h_p", "Element array", 20, -100, 100, 20, -100, 100);
``````

Which represents a grid over an area which I would like to fit sets of (x, y) points on and obtain which box/bin a given point is in. For instance, I will have an event at some point within the grid, within the range of (-100, 100) along both axes, and I would like to use:

``````element = h_p->FindBin(x, y, 0);
``````

to determine the bin that contains the point. For my check I have done the following to make sure I return the same points:

``````x_test = h_p->GetXaxis()->GetBinCenter(element);
y_test = h_p->GetYaxis()->GetBinCenter(element);

cout << "test position = (" << x_test << " ," << y_test << ")" << endl;
cout << "seeded position = (" << x << " ," << y << ")" << endl;
``````

upon compilation I obtain the following:

``````test position = (1905 ,1905)
seeded position = (-72.269 ,-10.6774)

test position = (1025 ,1025)
seeded position = (-77.7099 ,-53.9449)

test position = (835 ,835)
seeded position = (-44.8618 ,-66.9224)

test position = (2575 ,2575)
seeded position = (-61.6721 ,11.6775)

test position = (1375 ,1375)
seeded position = (53.6068 ,-41.5784)

test position = (3565 ,3565)
seeded position = (49.3616 ,56.9929)

test position = (2275 ,2275)
seeded position = (74.2571 ,-3.31977)

test position = (1505 ,1505)
seeded position = (-39.6788 ,-32.3741)

test position = (2765 ,2765)
seeded position = (-90.0729 ,24.7655)

test position = (2045 ,2045)
seeded position = (62.5263 ,-13.7962)
``````

While I understand that the bin center and the seeded point used to determine the bin, will not exactly match, I expected them to be within a range of at maximum 5 units of each other.

I assume my understanding is still flawed. Any and all help is greatly appreciated.

Hello,
What you are doing is not correct, because `TH1::FindBin` returns a global bin number for the 2-dim histogram and you are using it as an axis bin number.
You have two possibilities:

1. get separately the axis bin number for the x axis and y axis:
``````xbin = h_p->GetXaxis()->FindBin(x);
x_test = h_p->GetXaxis()->GetBinCenter(xbin);
ybin = h_p->GetYaxis()->FindBin(y);
y_test = h_p->GetYaxis()->GetBinCenter(ybin);
``````
1. Convert from the global bin number to the axis bin number:
``````element = h_p->FindBin(x, y, 0);
int xbin, ybin, bin = 0;
h_p->GetBinXYZ(element, xbin, ybin,zbin);
``````

and then use xbin and ybin as above.

Best

Lorenzo

@moneta Thank you so much, that fixed that issue! I have another question I am hoping you can answer. Do you happen to know how the global bin number is assigned to each bin? For example is it like axis bin: (xbin, ybin) = (0, 0) :: global bin = 0, and (xbin, ybin) = (20, 20) :: global bin = 400 ?

See ROOT: hist/hist/src/TH3.cxx Source File
where the global bin is computed from the bin axis number

Lorenzo

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