Histograms lost

Hi!

I have a problem running a simple script in python:
I try to make a

uncerts = ["AllUncert","BaselineJES","CloseBy","FlavourComp","FlavourResponse","FwdJES","Mu","NPV"]

uncertFullUp = [[],[],[],[],[],[],[],[]] 
uncertFullDown = [[],[],[],[],[],[],[],[]] 

for un in uncerts:
    s_uncert = "JIXSMC12Kt6"+un
    f = TFile("../JIXSMC"+suffix+"/JIXSMC12Kt6NoUncert.root")
    fUncertUp = TFile("../JIXSMC"+suffix+"/"+s_uncert+suffix+".root")
    fUncertDown = TFile("../JIXSMC"+suffix+"/"+s_uncert+suffix+".root")
    
    h_data = f.Get("mjjyATLAS")
    h_datauncertUp = fUncertUp.Get("mjjyATLAS")
    h_datauncertDown = fUncertDown.Get("mjjyATLAS")  
    
    for i in range(h_data.GetNbinsY()):
        hdata = h_data.ProjectionX("mjjtruth"+str(i)+un, i, i).Rebin(nMassBins, "mjjtruth", massBins)
        SetOwnership(hdata,False)
      
        huncertUp = hdata.Clone("huncertUp"+un+str(i))
        SetOwnership(huncertUp,False)
        
        huncertDown = hdata.Clone("huncertDown"+un+str(i))
        SetOwnership(huncertDown,False)

        uncertFullUp[uncerts.index(un, )].append(huncertUp)
        uncertFullDown[uncerts.index(un, )].append(huncertDown)
        print uncertFullDown[uncerts.index(un, )][i].GetName()
        print "NEntries in %i - %i: %d"%(uncerts.index(un, ),i,uncertFullDown[uncerts.index(un, )][i].GetEntries())
        print "NEntries in %i - %i: %d"%(0,0,uncertFullDown[0][0].GetEntries())

and the output is

huncertDownAllUncert0
NEntries in 0 - 0: 0
NEntries in 0 - 0: 0
huncertDownAllUncert1
NEntries in 0 - 1: 337
NEntries in 0 - 0: 0
huncertDownAllUncert2
NEntries in 0 - 2: 87
NEntries in 0 - 0: 0
huncertDownAllUncert3
NEntries in 0 - 3: 22
NEntries in 0 - 0: 0
huncertDownAllUncert4
NEntries in 0 - 4: 30
NEntries in 0 - 0: 0
huncertDownAllUncert5
NEntries in 0 - 5: 139
NEntries in 0 - 0: 0
huncertDownAllUncert6
NEntries in 0 - 6: 0
NEntries in 0 - 0: 0
huncertDownAllUncert7
NEntries in 0 - 7: 0
NEntries in 0 - 0: 0
huncertDownAllUncert8
NEntries in 0 - 8: 0
NEntries in 0 - 0: 0
huncertDownBaselineJES0
NEntries in 1 - 0: 0
Traceback (most recent call last):
  File "PlotMakros/CalcJESUncert.py", line 165, in <module>
    print "NEntries in %i - %i: %d"%(0,0,uncertFullDown[0][0].GetEntries())
AttributeError: 'PyROOT_NoneType' object has no attribute 'GetEntries'

where the last line should be “NEntries in 0 - 0: 0” again.
I guessed that the garbage collection somehow deletes the histogram, but SetOwnership didn’t solve the problem…
I would be happy about any idea why this code fails.

Thanks,

Tobias

Hi,

the histograms are part of the file, so when that closes (when the file goes out of scope), the get taken with it by the C++ side (the SetOwnership only changes things on the python side). I think calling SetDirectory(0) on the histogram should do the trick.

Cheers,
Wim

Thanks a lot!
Using SetDirectory(0) solved the problem.

Cheers,

Tobias