Change X axis range for THStack

Hi All

I have a few histograms, lets say h1, h2 ,h3, initially defined as

TH1F *h1 = new TH1F(“h1”,“test hstack”,100,-4,4);
TH1F *h2 = new TH1F(“h2”,“test hstack”,100,-4,4);
TH1F *h3 = new TH1F(“h3”,“test hstack”,100,-4,4);

and I later stack them together into a THStack object hs
THStack *hs = new THStack(“hs”,“test stacked histograms”);

I would like to re-set the x-axis range to (-3,3), but I have tried using

h1->GetXaxis()->SetRangeUser(-3.,3.); h2->GetXaxis()->SetRangeUser(-3.,3.); h3->GetXaxis()->SetRangeUser(-3.,3.);

or

h1->SetAxisRange(-3.,3.); h2->SetAxisRange(-3.,3.); h3->SetAxisRange(-3.,3.);

before stacking, or the same tricks to hs but still nothing worked, the code I have included only plotted the histogram in the range (-3,3), but the displayed x-axis remains at (-4,4). Any ideas?

Cheers for the help
Jason

Hi Jason,

Try this way:

Cheers, Bertrand.

I have tried that, but It tells me something like “Error: illegal pointer to GetXaxis()”, sorry I am replying from my phone at the moment and can’t remember that exact error line

Cheers
Jason

hs->Draw();
hs->GetXaxis()->SetLimits(-3, 3);

1 Like

Jason,

Indeed, you have to call hs->Draw() before calling hs->GetAxis().

And BTW, you can also use hs->GetXaxis()->SetLimits(-3, 3); but then you cannot (un)zoom outside these limits…

Cheers, Bertrand.