Converting from "linearized bin number"

Years ago on RootTalk Rene explained how to get the “linearized
bin number” or “global bin number” from the 2D or 3D coordinates:

You can use these functions for all type of histograms and
all dimensions. In case of 2-d and 3-d histograms, however, you
must compute a “linearized bin number” before invoking these
functions. For example for a 3-d histogram, you can do:
Int_t bin = h3->GetBin(binx,biny,binz);
Float_t y = h3->GetBinContent(bin);

Is there not a method to reverse this? It looks like the linearization
is straightforward. So, I can say:

int biny = h2->GetMaximumBin() / ( h2->GetNbinsX() + 2);
int binx = h2->GetMaximumBin() % ( h2->GetNbinsX() + 2);

and then, if I want,

double binyCenter = h2->GetYaxis()->GetBinCenter(biny);
double binxCenter = h2->GetXaxis()->GetBinCenter(binx);

But, there’s no method that does this (either of these) for me?


see examplees in TH2::Fill, TH3::Fill, eg in TH3::Fill

binx = fXaxis.FindBin(x); biny = fYaxis.FindBin(y); binz = fZaxis.FindBin(z); bin = binx + (fXaxis.GetNbins()+2)*(biny + (fYaxis.GetNbins()+2)*binz);


[quote=“brun”]see examplees in TH2::Fill, TH3::Fill, eg in TH3::Fill

binx = fXaxis.FindBin(x); biny = fYaxis.FindBin(y); binz = fZaxis.FindBin(z); bin = binx + (fXaxis.GetNbins()+2)*(biny + (fYaxis.GetNbins()+2)*binz);


Well, this is the linearization. But, what I was hoping for was a method
that would do the reverse.

I know the linearized bin number for a bin of interest, perhaps from


Now I want to convert that into the x, y, and z bin.

One can do it with a few lines of code, but there’s no method that
does it automatically?


well (::slight_smile: this is simply the inverse transformation, ie

binx = bin%(nx+2); biny = ((bin-binx)/(nx+2))%(ny+2); binz = ((bin-binx)/(nx+2) -biny)/(ny+2);

where nx,ny,nz are the number of bins in x,y,z.
In general this transformation should not be necessary. What is your use case?


The case I mentioned is the first that comes to mind. There’s an existing
method, h2->GetMaximumBin(), which returns the linearized bin number
of the maximum value bin. But, I probably prefer to know the x and y bins, or the centers of those bins.

Or, if I want to loop through every bin in a histogram it could be
convenient to do this with the linearized bin number. But, then if
I decide a bin is interesting I would like to know its coordinates, not
its linearized bin number

Imagine looking for noisy cells in a detector, for example. You have
some monitoring plot, and you want to print out the coordinates
of interesting cells.

It’s not so hard for the user to do the bookkeeping themselves. But, since ROOT has so many methods to make life easier, I was a bit surprised it didn’t have a method for this back conversion of linearized bin.



For the special case that you mention, we already privide the interface returning the global bin number or binx,biny,binz

virtual Int_t GetMaximumBin() const; virtual Int_t GetMaximumBin(Int_t &locmax, Int_t &locmay, Int_t &locmaz) const;

Anyhow, I will add the function described above in a comning version.


I have added a new function in the SVN trunk