I made some further tests and apparently drawing the THStack somehow “activate” it:
>>> import ROOT
>>> h1 = ROOT.TH1F("h1", "ciao bello", 100, 0., 100.)
>>> h2 = ROOT.TH1F("h2", "ciao bello!!!", 100, 0., 100.)
>>> ths = ROOT.THStack("ths", "pippo")
>>> ths.Add(h1)
>>> ths.GetHistogram()
<ROOT.TH1 object at 0x0> -----> NULL POINTER!!!
>>> ths.Draw() -----> DRAWING THE THSTACK!!!
<TCanvas::MakeDefCanvas>: created default TCanvas with name c1
>>> ths.GetHistogram()
<ROOT.TH1F object ("ths") at 0xa4f200> ------> NOW IT'S NOT NULL POINTER ANYMORE
>>> ths.Add(h2)
>>> ths.GetHistogram()
<ROOT.TH1 object at 0x0>
>>> ths.Draw()
<TCanvas::MakeDefCanvas>: created default TCanvas with name c1
>>> ths.GetHistogram()
<ROOT.TH1F object ("ths") at 0x4818000>
>>>
So if I draw the thstack before trying to use GetHistogram() on it, everything works.
But drawing a THStack is not always possible, especially when creating hundreds of THStacks in a script: in this scenario it would be better to create them, access the histo and its axis to set the title without having to draw it, and save directly the newly created thstack in an output .root file.
Is there any way to “activate” the THStack without drawing it?
I do not know what you mean by “activate”… a THStack is only a list of histograms. The histogram returned by the GetHistogram function is a temporary one used only for drawing purpose (axis drawing mainly). So it exist only when the THStack is drawn.
No … the histograms are in memory … They are group in a list: a THStack object.
The temporary histogram returned by THStack::GetHistogram() is created only at drawing time.