Reset THStack X-Axis Range

I’m trying to generate a set of stacked histograms, then clear and re-use the THStack.

I declare the stack, add a couple of histos, draw and save the result. This works fine for the first set. I’d then like to clear/reset the stack, and re-use it, this time with another set of plots. Rinse and repeat.

So far I’m using thestack.GetHists()->Clear(); thestack.Clear(); before adding the new set of plots, but while this clears the old plots, the X-axis range does not seem to be reset. If the first set of plots has range 0-200, then clearing and adding a new set of plots between 0-2000 does not extend the X-axis.

Is there a way to reset the stack? I’d rather not have to set the axis range manually (no need for the first set), and new/deleting a fresh THStack for each set seems inefficient.

Thanks in advance

What is the point of keeping the THStack if you clean it completely ? why not re-creating a new one ? Is there something you want to keep from the old stack ?

1 Like

It just seemed like it would be more efficient to reset whatever flag is preventing it from adjusting the axes than new / deleting a fresh object each time.

To have a clean stack, re-creating it is surely the most efficient.

OK, i generally thought new/deleting was an expensive operation, but fair enough.

Unfortunately I seem to have hit a hitch in doing it this way, too, however.
When I create the first stack, I can get the X- and Y- axes and set the titles appropriately.
But when I create a second stack, if I try to do the same thing, the program segfaults.
I’ve attached a minimal macro to reproduce it. I’m using ROOT 6.06.04.

stacktest.C (1.8 KB)

YourStack->Draw("nostack"); // first things first
gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn
YourStack->GetXaxis()->... // ... modify newly created axes ...
gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn

BTW. You can also set axes’ names right when you create “YourStack”:

THStack *YourStack = new THStack("YourStack", "My Stack;X Axis Name;Y Axis Name;Z Axis Name");
1 Like

Thanks @Wile_E_Coyote, that works now. And for the tip. :slight_smile:
Thanks also to @couet

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