Writing TH1's while opening multiple TFiles

Dear, I have an issue here when trying to Write TH1’s in a rootfile:

for i in range(0,len(rootfile_list):
file1 = ROOT.TFile(rootfile_list[i], “read”)
ihist = file1.Get(“hist”)
Hists_list.append(ihist)

outfile = ROOT.TFile(‘MCplotsFudgeFactors.root’,‘create’)
for i in range(0,len(Hists_list)):
Hists_list[i].Write()
outfile.Close()

However, an error appears saying:

Traceback (most recent call last):
File “PythonScript.py”, line 31, in
Hists_list[i].Write()
AttributeError: ‘PyROOT_NoneType’ object has no attribute ‘Write’

When I print the Hists_list before the creation of the outfile, it looks fine, but after that, all the elements of the list appears as “None” type. Is there a reason why this is happening? How can I avoid it?

Hi,

You need to call ihist.SetDirectory(0) after it read from the file.

Regards,
Sergey

Thank you very much!!!