How to prevent TH1F histos from becoming None

Hi all,

I have a question: given the following sample code and output:

[code]>>> from ROOT import *

h = {}
for i in range(2):
… f = TFile(“%s”%i, “RECREATE”)
… h[i] = TH1F(“%s”%i, “%s”%i, 100, 0, 1)
… h[i].FillRandom(‘gaus’)
… print h
… f.Close()

{0: <ROOT.TH1F object (“0”) at 0x95d600>}
{0: None, 1: <ROOT.TH1F object (“1”) at 0x95d600>}
[/code]

I don’t understand why 0 should be None, i.e., the TH1F object disappears from memory when tf is closed. What I want to do is create several histograms from different ROOT files and have them available for further processing, but I haven’t been able to do so…
How should I proceed?

Many thanks,
Albert

Hi,

The histogram, by default, belongs to (are owned by) the TFile they are read from. They are deleted when the file is closed (See the User’s Guide chapter on Object ownership). If you want to take ownership of the histogram you can do so by calling h[i].SetDirectory(0) (or equivalent in PyROOT).

Cheers,
Philippe.