SetBinLabel causes unexpected behavior when handling the histograms

Dear all,
I found some some unexpected (for me, at least) behaviour when operating the histograms after setting the bin labels:

  1. Setting by hand the overflow to 0 content and 0 error changes the number returned by h->GetNbinsX() (it becomes twice the original number)
  2. For 2D histograms, the Projection operation returns an histogram with twice the binning of the original TH2D.
  3. I have observed also some weird behavior of hadd, but I have not be able to reproduce it (the hadd-ed result dependend on the order of the input files, but in both cases it was wrong. The hadd.C example macro worked correctly, though)

I’m attaching a small macro to replicate issues 1. and 2.


binLabels.C (1.9 KB)

You’re right. The problem is only in ROOT 6 (with ROOT 5 it works as expected). Maybe @moneta has an idea…


An histogram with bin labels can extend its axis automatically when you set content outside the range.
There is no real meaning anymore for underflow/overflow in this case.
The histogram has therefore its axes automatically extendable. Now, if you don’t want this behaviour
in your first case, you need to call before calling SetBinContent in bib outside the range


In case of Projection, also the overflow are by default include in this case and since you are projecting in a labeled axis, the projection in th overflowed bins is displayed. This explains the doubling of the axis.
If you don’t want to consider the underflow/overflow in the projected Y axis, it is enough to set a range in the TH2 histogram before doing the projection:

h->GetYaxis()->SetRange(1, h2->GetYaxis()->GetNbins() ); 

If you have a problem with TH1::Add please let me know

Best Regards


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