TAxis::SetRange and TH2::Projection


When using the function TAxis::SetRange() on a TH2, a projection of this TH2 will include or not over/underflows, depending on the specified ranges, even though they stay within [1,N]
I find that quite confusing.

In the attached example, I have 3 bins on both axes.
If I do SetRange(1,2) on axis X, the projection on Y will not include axis X overflows.
If I do SetRange(1,3) on axis X, then the projection on Y will include overflows.

I think as long as the bin ranges dont include bins 0 or N+1, I should see no under/overflow in the projection.

Can you comment ?
Thanks !!
test_TH2D.C (608 Bytes)

Hello Renaud,

I agree with you that when you are setting the range using TAxis::SetRange(1,nbin) you expect the underflow/overflow to be excluded. Unfortunately this is due to a limitation and a confusing behavior of TAxis::SetRange. When you do TAxis::SetRange(1,nbin) the range is reset, it is like not having a range at all.
The workaround is to reset the range bit in the axis. In your case do:


Best Regards


Dear Lorenzo,

thanks for your answer, but the use of h21->GetXaxis()->SetBit(TAxis::kAxisRange);
screws up completely the histo (at least the display) : the X axis range becomes [-1,0] !!
however the resulting projection is fine.

can you try what you suggest and check if you get the same behaviour?
if yes, how should i deal with that ?
I am using v5.26.00b, will this be solved if I use today’s trunk ??
thanks a lot


Hi Renaud,

Do you still have the problem with v5.28?


Dear Philippe,

yes the problem still exists, same results:
1/ without setting the bit TAxis::kAxisRange , no axis range is used in the projection.
2/ setting TAxis::kAxisRange, the 1D projection is fine, but the X-axis on the 2D plot becomes [-1,0]

In addition, I now get this message (and only this one) :
Error in TGaxis::PaintAxis: wmin (0.000000) == wmax (0.000000)

I can live with 1/, but 2/ must be fixed imho.
Thanks !


Yes, setting the axis range bit for 1,Nbin, gives problem in drawing the axis.
The best solution for you is to do pass the first and last bin in the TH2::ProjectionY function. Do:

TH1D* h11 = h21->ProjectionY("h21_py",1,3);


Hi Lorenzo
Should we expect this feature to be fixed, or should we live with it ?


I fully agree that this might be fixed at some point, but it requires changing the behavior of the TAxis::SetRange method, which could have side effects and I am a bit hesitant in doing this