How to mimic drawclone in tmultigraph


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:


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};
TGraph gr1(px1.size(),,;
//what I shoud write here?
graphs.Add(/what I should write here?/);

int main()
TCanvas c1;
TMultiGraph mg;
mg.GetXaxis()->SetTitle(“X values”);
mg.GetYaxis()->SetTitle(“Y values”);

I also check the source code of drawgraph in … tml#l00798
and source code of drawclone in … 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(),,;