Rebinning a Stack via the GUI


I’ve gotten in the habit of making histograms with many more bins than I might need, and then rebinning to the optimal number of bins interactively once I’ve drawn the canvas, using the Editor tool. This method does not work for THStack objects, as far as I can tell. Is there an alternative method I could use?


Jamie Antonelli

I use the THStack’s GetHists() method, then loop over the histograms to do FindFirstBinAbove(0) and FindLastBinAbove(0), then take the min/max of those sets to set the xmin and xmax of the THStack. It’s not as bad as it sounds since I do it in PyROOT:

def getTHStackRange(hs):
        hists = hs.GetHists()
    except AttributeError:
        hists = hs.values()
    minbin = set()
    maxbin = set()
    for h in hists:
    return min(minbin),max(maxbin)

If you’re handy with iterators and such in C++, it may not be much harder than what I did.


Hi Jean-François,
It seems like what you’re doing is trimming empty space from the left and right edges of a stack, is that correct?
What I’m interested in doing is combining bins to increase the bin width, as is done with the TH1::Rebin command. There’s a way to do this graphically for a TH1 object after drawing a canvas in the ROOT GUI, but not for a THStack. I was trying to find out if there is a way within the GUI to do this, but it’s quite possible that functionality isn’t implemented.
But I wasn’t aware of this FindFirstBinAbove(0) functionality, that seems very useful indeed, thanks!


Ah yes sorry, I read your sentence about making histograms with more bins than you need, and interpreted it as doing the same thing that I was doing. I make histograms with a set bin width, but with an overly large range, then I use that function I pasted to find the range over the non-empty bins for drawing and fitting.

I don’t have any advice about the re-binning as you describe.