Tick marks on a histogram with non-uniform bins

I have the following problem:

I book a histogram with non-uniform bins. When I draw the histogram, the tick marks on the X-axis are still drawn uniformly, which in this case simply makes no sense.

So my question is: How do I make ROOT draw the tick marks on bin boundaries, instead?
(And also - how to make this behaviour default, since it is the only meaningful one in the case of non-uniform bins?)

I don’t care too much about the tick labels (if neccessary I can set them explicitly).

I remember seeing an option that controls this beaviour some time ago, but I cannot find it now…

Here is my example script:

Double_t xbins[17]={0.0000,0.4173,0.7576,1.0978,1.4381,1.7784,2.1187,2.4590,2.7993, 3.1423,3.5197,3.8998,4.2800,4.6600,5.0403,5.4204,5.8762}; TH1F layers("layers","layers",16,xbins); layers->Fill(0.1,1); layers->Fill(0.9,2); layers->Fill(2.2,2); layers->Fill(2.9,1); layers->Fill(4.6,3); layers->Fill(5.2,1); layers->Draw();

There is no option doing this, because in the general case it does not make really sense and this is very specific for each case. I modified your example to do it by adding a simple loop.

{
   Double_t xbins[17]={0.0000,0.4173,0.7576,1.0978,1.4381,
   1.7784,2.1187,2.4590,2.7993, 3.1423,3.5197,3.8998,4.2800,
   4.6600,5.0403,5.4204,5.8762};
   TH1F layers("layers","layers",16,xbins);
   layers->Fill(0.1,1);
   layers->Fill(0.9,2);
   layers->Fill(2.2,2);
   layers->Fill(2.9,1);
   layers->Fill(4.6,3);
   layers->Fill(5.2,1);
   layers->GetXaxis()->SetLabelOffset(1);
   layers->GetXaxis()->SetTickLength(0);
   layers->Draw();
                                                                                
   // Draw ticks and labels on the bins boundary
   Int_t nb = layers->GetXaxis()->GetNbins();
   TLine *l = new TLine;
   l->SetLineWidth(3);
   TText *t = new TText;
   t->SetTextAlign(22);
   t->SetTextSize(0.02);
   Double_t x;
   for (Int_t i=1; i<=nb; i++) {
      x = layers->GetXaxis()->GetBinLowEdge(i);
      l->DrawLine(x,0,x,0.1);
      t->DrawText(x,-0.15,Form("%g",x));
   }
}

It gives the following result:


Yes, but this is faking it. I could also draw an elephant around my plot using TLine… :wink:

Are you saying that there is really no switch/flag/parameter in ROOT that does this job internally?

I still maintain that for a non-uniformly binned histogram the most natural ticks are on bin boundaries and this should be the default behaviour. Perhaps in a future release, at least…

As I said, that sort of axis drawing is not implemented in TGaxis because it depends a lot on the number of bins you have, the value of the labels etc. Most of the time you will get an ugly result. As it is VERY simple to draw (just 10 lines of code), it can be enclosed in a user macro as I propose here. Drawing an elephant is more tricky…

Well, TGaxis does not handle non-uniform bins at all.
My hope was more with TAxis…

In fact we’ve spent some time with the source code yesterday and saw a possibility to handle this case in TGaxis too, in a way similar to the case of using ‘fFunction’ to draw the tics.
It will just require ticks values to be taken from the bin boudary definitions (which are stored with the histogram) and not evaluated from the fFunction.
And a new switch to handle this case - which should be the default, of course :wink:

Do not mix TAxis and TGaxis. Of course TAxis handle non uniform bins as it is the container of the histograms’ bins. TGaxis does the graphics rendering. Your problem is with the axis rendering not with the internal storage of data.