Can combine multiple grpahs into a canvas drew from a Tree?

Hi, rooters,

Right now, I’m thinking the possibility of combing multiple graphs into one graph(in one canvas).

In my case, each graph is produced by drawing from a Tree(not the common shape of an array), which has been indicated in this post. [url]Ntuple->Draw() can't get correct TH2F histogrsm

It sounds possible to use TMultiGraph, but I can’t figure out a solution even though I have tried a lot.
The problem is
(1) Only the second graph has been displayed but not both ones(this script takes two grapshs as a first try. In reality, more graphs need to be combined.)
(2) There is an error message like this [1].

The script is,

   // Open root file and open tree.
   TCanvas *c8 = new TCanvas("c8","temporary",10,10,500,400);
   TMultiGraph *mg = new TMultiGraph();
   ///
   T->Draw("Sdd_Energy:Bar8_Time_1stHit ","(Sdd_N_of_Hits > 0) && (Bar8_N_of_Hits > 0)");
   TGraph *g_SDDE_vs_Bar8Time    = (TGraph*)gPad->GetPrimitive("Graph");
   ///  
   T->Draw("Sdd_Energy:Bar1_Time_1stHit ","(Sdd_N_of_Hits > 0) && (Bar1_N_of_Hits > 0)");
   TGraph *g_SDDE_vs_Bar1Time    = (TGraph*)gPad->GetPrimitive("Graph");etPrimitive("Graph");
   ///   
   mg->Add(g_SDDE_vs_Bar1Time);
   mg->Add(g_SDDE_vs_Bar8Time);
   mg->Draw("P");
   ///
   f->Close();
   c8->Update();
   return c8;

Also, I refered this link, but it failed neither : can’t display two graphs at the same time, http://root.cern.ch/root/roottalk/roottalk01/0341.html

Can some peolpe comment on this please ?

Best,
Junhui

[1]
*** Break *** segmentation violation

===========================================================
There was a crash.
This is the entire stack trace of all threads:

#0 0x00000037fdcac5de in waitpid () from /lib64/libc.so.6
#1 0x00000037fdc3e619 in do_system () from /lib64/libc.so.6
#2 0x00007fabfbba0be8 in TUnixSystem::StackTrace() () from /home/junhui/root/lib/libCore.so
#3 0x00007fabfbb9fa63 in TUnixSystem::DispatchSignals(ESignals) () from /home/junhui/root/lib/libCore.so
#4
#5 0x00007fabf8c15d66 in TMultiGraph::Paint(char const*) () from /home/junhui/root/lib/libHist.so
#6 0x00007fabf7b0dd52 in TPad::PaintModified() () from /home/junhui/root/lib/libGpad.so
#7 0x00007fabf7af348c in TCanvas::Update() () from /home/junhui/root/lib/libGpad.so
#8 0x00007fabfbc27905 in G__G__Base1_200_0_162(G__value*, char const*, G__param*, int) () from /home/junhui/root/lib/libCore.so
#9 0x00007fabfadb7b79 in Cint::G__ExceptionWrapper(int ()(G__value, char const*, G__param*, int), G__value*, char*, G__param*, int) () from /home/junhui/root/lib/libCint.so
#10 0x00007fabfae5bf01 in G__execute_call () from /home/junhui/root/lib/libCint.so
#11 0x00007fabfae5cd42 in G__call_cppfunc () from /home/junhui/root/lib/libCint.so
#12 0x00007fabfae38f18 in G__interpret_func () from /home/junhui/root/lib/libCint.so
#13 0x00007fabfae26c6c in G__getfunction () from /home/junhui/root/lib/libCint.so
#14 0x00007fabfaf13a8a in G__getstructmem(int, G__FastAllocString&, char*, int, char*, int*, G__var_array*, int) () from /home/junhui/root/lib/libCint.so
#15 0x00007fabfaf0c25e in G__getvariable () from /home/junhui/root/lib/libCint.so
#16 0x00007fabfae03fed in G__getitem () from /home/junhui/root/lib/libCint.so
#17 0x00007fabfae092e8 in G__getexpr () from /home/junhui/root/lib/libCint.so
#18 0x00007fabfae88987 in G__exec_statement () from /home/junhui/root/lib/libCint.so
#19 0x00007fabfae3a2e5 in G__interpret_func () from /home/junhui/root/lib/libCint.so
#20 0x00007fabfae26ccb in G__getfunction () from /home/junhui/root/lib/libCint.so
#21 0x00007fabfae0476e in G__getitem () from /home/junhui/root/lib/libCint.so
#22 0x00007fabfae092e8 in G__getexpr () from /home/junhui/root/lib/libCint.so
#23 0x00007fabfae127f8 in G__calc_internal () from /home/junhui/root/lib/libCint.so
#24 0x00007fabfae97e33 in G__process_cmd () from /home/junhui/root/lib/libCint.so
#25 0x00007fabfbb63426 in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /home/junhui/root/lib/libCore.so
#26 0x00007fabfbb60b23 in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) () from /home/junhui/root/lib/libCore.so
#27 0x00007fabfbacc278 in TApplication::ExecuteFile(char const*, int*, bool) () from /home/junhui/root/lib/libCore.so
#28 0x00007fabfbacb706 in TApplication::ProcessLine(char const*, bool, int*) () from /home/junhui/root/lib/libCore.so
#29 0x00007fabfb71a548 in TRint::Run(bool) () from /home/junhui/root/lib/libRint.so
#30 0x000000000040103c in main ()

The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.

#5 0x00007fabf8c15d66 in TMultiGraph::Paint(char const*) () from /home/junhui/root/lib/libHist.so
#6 0x00007fabf7b0dd52 in TPad::PaintModified() () from /home/junhui/root/lib/libGpad.so
#7 0x00007fabf7af348c in TCanvas::Update() () from /home/junhui/root/lib/libGpad.so

Try: // ... TGraph *g_SDDE_vs_Bar8Time = (TGraph*)((gPad->GetPrimitive("Graph"))->Clone("g_SDDE_vs_Bar8Time")); // ... TGraph *g_SDDE_vs_Bar1Time = (TGraph*)((gPad->GetPrimitive("Graph"))->Clone("g_SDDE_vs_Bar1Time")); // ... mg->Draw("AP"); // ...

Hi, Wile,

Thanks ! It works.
The whole script looks like,

void combineTwoGraph() {
   TFile *f = new TFile("fileName.root ");
   TTree *T = (TTree*)f->Get("treeName");
   TCanvas *c8 = new TCanvas("c8","temporary",10,10,500,400);

   TMultiGraph *mg = new TMultiGraph();
   
   T->Draw("Sdd_Energy:Bar8_Time_1stHit ","(Sdd_N_of_Hits > 0) && (Bar8_N_of_Hits > 0)");
   
   TGraph *g_SDDE_vs_Bar8Time    = (TGraph*)((gPad->GetPrimitive("Graph"))->Clone("g_SDDE_vs_Bar8Time"));
   g_SDDE_vs_Bar8Time->SetMarkerColor(2);

   T->Draw("Sdd_Energy:Bar1_Time_1stHit ","(Sdd_N_of_Hits > 0) && (Bar1_N_of_Hits > 0)");
   TGraph *g_SDDE_vs_Bar1Time    = (TGraph*)((gPad->GetPrimitive("Graph"))->Clone("g_SDDE_vs_Bar1Time"));
   g_SDDE_vs_Bar1Time->SetMarkerColor(3);


   mg->Add(g_SDDE_vs_Bar1Time); 
   mg->Add(g_SDDE_vs_Bar8Time);

   mg->Draw("AP");

   mg->GetXaxis()->SetTitle("Bar time(ns)");  
   mg->GetXaxis()->CenterTitle(true);
   mg->GetXaxis()->SetTitleOffset(1.2);
   mg->GetYaxis()->SetTitle("SDD energy(keV)");
   mg->GetYaxis()->CenterTitle(true);
   mg->GetYaxis()->SetTitleOffset(1.2);

   c8->SaveAs("combineTwoGraphsTest.pdf");
   c8->SaveAs("combineTwoGraphsTest.root");

   f->Close();  
   c8->Update();   
}