Removing TMultiGraph removes the added to it graphs?

Hi all,

I find this behavior funny… And I don’t understand it


import ROOT

canvas = ROOT.TCanvas()
graphs = {}

for i in range(5):
    graphs[i] = ROOT.TGraph()
    for x in range(10):
        graphs[i].SetPoint(x, x, x*x + i*5)

    mg = ROOT.TMultiGraph()
    # mg.Add(graphs[i])
    graphs[i].Draw("AL" if i ==0 else "Lsame")

    print("Before update")
    print("After update")


Why removing the comment at mg.Add(graphs[i]) gives seg. fault at canvas update?

Creating mg = ROOT.TMultiGraph() out of the loop, doesn’t create a problem.
Thus, I am assuming, when TMultiGraph is deleted, it deletes all added to it Graphs?


ROOT Version: 6.24/00
Platform: Centos7
Compiler: Not Provided
Python: 3.8.6

Hi @FoxWise,
Indeed you are right, the graphs added to the TMultiGraph are owned by it, so when destroyed it destroys all the graphs in its belly. Here’s the destructor of TMultiGraph where this is done . As you have already pointed out, creating the multigraph outside of the loop prevents its destruction at each iteration so that’s the correct way to do it.

1 Like

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