PyROOT: differences in object ownership betweeh TH1 and TGraphErrors


ROOT Version: 6.41.01
Platform: Linux (Fedora 44)
Compiler: g++ (GCC) 16.1.1 20260515 (Red Hat 16.1.1-2)


Hi…

Could someone kindly teach me why the python script below

  • works with withHist = True, and
  • works with withHist = False and ngraphs = 1, but
  • SEGs with withHist = False and ngraphs larger than 1 (w/o doing ROOT.SetOwnership()?

The last case works when uncommenting the ROOT.SetOwnership(g, False) line.

Thanks in advance.

Kazuyoshi

import ROOT
from ROOT import TFile, TObjArray, TGraphErrors, TH1F

withHist = True
print("withHist = ", withHist)

fout = TFile(“nge.root”, “recreate”)
histos = TObjArray()

npoints = 10
ngraphs = 5
xtitle = “the x title”
ytitle = “the y title”

for idx in range(ngraphs):
    gname = “g%d” % (idx)
    gtitle = “the title of %s” % (gname)
    print("gtitle = ",gtitle)
    if not withHist:
        g = TGraphErrors(npoints)
        # ROOT.SetOwnership(g, False)
        g.SetNameTitle(gname, “%s;%s;%s” % (gtitle,xtitle,ytitle))
    else:
        g = TH1F(gname,gname,npoints,0.,npoints)
    histos.Add(g)

print("#items: ", histos.GetEntries())
histos.Write()
fout.Close()

If you search the forum : Search results for 'Ownership order:latest' - ROOT Forum
You can find, for example:
ROOT Manual - Basics: Object Ownership Question
Ownership in Python
And in the ROOT documentation chapter about ownership:
Chapter: Object Ownership