How to mimic drawclone in tmultigraph

Hi:

TMultiGraph can automatic compute all the x and y ranges such that all the graphs will be visible. However, its add method does not transfer the ownership of TGraph to TMultiGraph. I can not find a way like drawclone or drawgraph which make a copy of TGraph and transfer the ownership of copy to the underlying pad or canvas.

So how to mimic the behavior of drawclone or drawgraph to make a copy of TGraph and transfer the ownership of copy to the TMultiGraph?

To be more specific:

#include<root/TCanvas.h>
#include<root/TMultiGraph.h>
#include<root/TGraph.h>
#include<root/TAxis.h>

void graph1(TMultiGraph&graphs)
{
std::vectorpx1{-0.1, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};
std::vectorpy1{-1,-2.9,-5.6,-7.4,-9,-9.6,-8.7,-6.3,-4.5,-1};
TGraph gr1(px1.size(),px1.data(),py1.data());
//what I shoud write here?
graphs.Add(/what I should write here?/);
}

int main()
{
TCanvas c1;
TMultiGraph mg;
graph1(mg);
mg.DrawClone(“ac”);
mg.GetXaxis()->SetTitle(“X values”);
mg.GetYaxis()->SetTitle(“Y values”);
c1.Print(“haha.pdf”);
}

I also check the source code of drawgraph in root.cern.ch/doc/master/TGraph_ … tml#l00798
and source code of drawclone in root.cern.ch/doc/master/TObject … tml#l00278
they are implemented in a very different way. So I do not know which one I should mimic. Moreover, I really care about the memory leaking problem, I do not want to just create a new TGraph in a function and not delete it before leaving the function just like most of the case in the reference example.

TGraph *gr1 = new TGraph(px1.size(), px1.data(), py1.data());
graphs.Add(gr1);