THStack - The X and Y axis objects, segfault (THStack->GetXaxis())

Hi again, another THStack related question…

When using TH1 objects, and drawing to canvas, one can do…


etc to change the axis labels/fonts/titles etc… (before calling hist->Draw())

When working with THStack objects, the objects returned by GetXaxis() and GetYaxis do not appear to exist until the THStack is drawn to a pad.

That is to say,


causes a segmentation fault.

Using the other ordering,


does work, but this seems like a ROOT inconsistency, regarding the syntax.

Am I doing something illogical? Should I be doing this a different way?

Hi @Birdsall

It is behaves like this. THStack does not have TAxis objects inside, but create histogram when first drawing is performed. Therefore you cannot access X axis before.


The axis system of the THStack is determined from the elements of the THStack histogram collection, as part of the drawing. So indeed, if you want to resolve the inconsistency you’d have to draw the histogram, then fix up its axes, then update the canvas also for histograms to make it symmetrical with THStack. Note that THStack isn’t the only object behaving this way; TGraph (and its GetHistogram()) is a popular other example.

And we’ve documented this:

Cheers, Axel.

Ok thanks, so to draw first is the correct method to do this.

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