THStack and TH1F

Hi, i’m trying to change a THStack to TH1F to use TH1’s Get functions.
the THStack is made with TH1F histos.
i tried

TH1F *primo =(TH1F*)myfile->Get("pmts");	
int nbins1 = primo->GetNbinsX();

where “pmts” is the THStack.

I get a segfault.
Is there a way to do it?

thank you

[quote]i’m trying to change a THStack to TH1F to use TH1’s Get functions.[/quote]This is not possible and can only lead to a core dumps.
To get the information you might want to use THStack::GetXaxis or retrieve one of the histo.


You have a cast error (if “pmts” is a THStack). Instead of

TH1F *primo =(TH1F*)myfile->Get("pmts"); do

then you can do


Thank you all for the help.

I’ve made two files:
in i tried to get the histogram from the stack as Rene said but i still have segfault.
in stackRange i tried to retrieve the X axis of the stack and then resize it. segfault too.

here are the files i use .

probably there is some c++ error i can’t find :frowning:

thank you
AmirFiles.tar.gz (24.6 KB)

If you want to pick the numbers of bins of teh first histogram in the list of histograms stored in your THStack, replace:

TH1F *primo =(TH1F*)stack->GetHistogram();by


but if i’ll take only one histo and then set his axis range, i’ll have to do a cycle over the histos composing the stack to finally have it drawn with a smaller range isn’t it?
---------another solution--------
with the GUI is easy to do, i have only to zoom over the x axis after drawing the stack. then i have to add a legend.
there is a way to add the legend using the GUI?


Please indicate clearly and precisely what you are trying to do.


i have the THStack. i want to consider it like a single histogram to adjust the axis range to draw it nicely.
now, when i draw the stack “pmts” its X axis extends far over the region of interest. so i want to resize it
the THStack is a list of histo. so if i want to have a smaller axis range when i draw it probably i have first to resize the x axis of every histo.

I’m asking if there is a way of resizing the x axis after the THStack has been build.

I would like to draw the THStack, take a look and then say “oh, a resize of the axis is necessary” . Resize the axis, and save the THStack with the resized axis.

By definition THStack computes the total range based on the individual range of its TH1 components.
If you simply want to draw a THStack in a different range that you choose yourself, use for example TPad::DrawFrame to draw a pad with an empty frame and your range and superimpose your THStack with the option “same”


ok now the things are much clearer.
Thank you very much!