TTree::Draw Automatically re-adjusdting the binning scheme


I’m trying to fill two histograms from a tree, then divide one by the other. Using the TTree::Draw method (var is a string that matches the branch I want to plot)

maxInput = tree.GetMaximum(var) minInput = tree.GetMinimum(var) tree.Draw(var+'>>fineBins(100,minInput,maxInput)','bTag') tree.Draw(var+'>>hist1b(100,minInput,maxInput)','isB') fineBins = r.gDirectory.Get('fineBins') hist1b = r.gDirectory.Get('hist1b') fineBins.Divide(hist1b) fineBins.Fit('pol1','Q') fineBins.Draw()

I get the error: Error in <TH1F::Divide>: Attempt to divide histograms with different number of bins . But this shouldn’t be happening, right? Each histogram has the same number of bins, and the same upper and lower limits.

I also get the error: TH1F::Divide:0: RuntimeWarning: Attempt to divide histograms with different axis limits . Again, this shouldn’t be happening, for the same reason.

Does this method of creating histograms automatically readjust the binning scheme that I have told it to use?


I just tried with the ntuple produce by hsimple.C (

root [0] ntuple->Draw("px>>hpx(100,-10,10)","","goff");
root [1] ntuple->Draw("py>>hpy(100,-10,10)","","goff");
root [2] hpx->Draw()
root [3] hpy->Draw()
root [4] hpx->Divide(hpy);

and it works…

I think the problem is that the highest non-zero bin of one histogram is significantly lower than the highest non-zero bin of the other.
After some google-fu, I found this snippet:

Talking about h->SetBit(TH1:: kCanRebin)

But I’ve specified the range of the histogram already, haven’t I? Is there a way to specify the range on a Branch to force TTree::Draw to keep the binning I need?

Book the histogram before the Draw() command and do:


That worked, thanks!

So the specifications for the binning scheme in the TTree::Draw method are just ‘suggestions’ rather than absolutes? Why even have the option to input them if it will just be over ridden?

Also, do I flag this topic as Solved, or does an admin/mod have to do that? Nevermind, I found it.

Yes, if you want to have the full control over the histogram characteristics you better book it before the Draw than using the short cut inside the Draw. For instance booking it before also to specify Title and Name… which might be useful too.