Home | News | Documentation | Download

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…

hist->GetXaxis()->SetLabelSize(0);

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,

stack->GetXaxis()->SetTickSize(0);
stack->Draw();

causes a segmentation fault.

Using the other ordering,

stack->Draw();
stack->GetXaxis()->SetTickSize(0);

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.

Regards,
Sergey

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: https://root.cern.ch/doc/master/classTHStack.html#af9ef4e065b6f4727c2a9242afac24d28

Cheers, Axel.

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