pyROOT memory management issue

I have an issue where my list object gets randomly deleted. I fill a np.array of objects (RooCurves) that are taken from canvas in a root file using GetPrimitives() in a loop. I when I check to see if the object is there soon after the loop as ended it’s there and everything is fine. But, after going through a second loop that does something similar, when I check to see if my first list of objects is there it seems to be deleted (returns [None]).

def readSamples(INFILE):
        infile = open(INFILE,"r")

        for line in infile:
                if len(line)<1 : continue
                if "%PROCESS" in line :
                if "%END" in line:
                if LAST_PROCESS=="preInj":
                if LAST_PROCESS=="postInj":

        return SAMPLES

INFILE   = sys.argv[1]
samples  = readSamples(INFILE)

canvas_sig      = np.array([])
rc_sig          = np.array([])
erc_sig         = np.array([])
dataHist_sig    = np.array([])
## no Sig                                                                                                                                                                                                                              
rc_nosig        = np.array([])

for x in samples[0]:
        file     = ROOT.TFile(x,"OPEN")
        canvas   = file.Get("extrapolation").Clone()
        rc       = canvas.GetPrimitive("curve_diphoton5_fit_diphoton5_dataHist_extrapFitRange")
        rc_nosig  = np.append(rc_nosig,rc)
print "RC list: ",rc_nosig  #Returns the pointer to the rooCurve

for y in samples[1]:
        file     = ROOT.TFile(y,"OPEN")
        canvas   = file.Get("extrapolation").Clone()
        rc       = canvas.GetPrimitive("curve_diphoton5_fit_diphoton5_dataHist_extrapFitRange")
        erc      = canvas.GetPrimitive("errorBand")
        dataHist = canvas.GetPrimitive("dataHist")
        canvas_sig      = np.append(canvas_sig,canvas)
        rc_sig    = np.append(rc_sig,rc)
        erc_sig   = np.append(rc_nosig,erc)
        dataHist_sig    = np.append(dataHist_sig,dataHist)

print "RC list2: ",rc_nosig #Returns [None]

_ROOT Version:6.12/04
Platform: Not Provided
Compiler: Not Provided

Problem fixed.
rc_nosig = np.append(rc_nosig,rc.Clone())
seems to have stopped the object from being deleted!

Dear @dabhayas,
Indeed, in the first loop you were creating a canvas variable inside the loop, and that canvas was destroyed at the end of the iteration (and consequently, all the elements in it were also destroyed).

By appending a clone, the clone is not cleaned by the destructor of the canvas and therefore it works.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.