Draw multiple root files along with graph file on the same canvas

I wish to draw selected histogram(20th) from every root file (4 root files) which contains 21 histogram along with graph having particular values which is saved in form of root file. With vs.5.xx.xx it perhaps looks like thisfirst.root (45.0 KB) Testgraph.root (14.5 KB)

Test()
{

TFile *f3 = TFile::Open("Testgraph.root");
TCanvas *c1 = (TCanvas*)f3->Get("c1");
TGraphErrors *g = (TGraphErrors*)c1->GetPrimitive("Graph");
g->GetYaxis()->SetRangeUser(10,80000);
g->GetXaxis()->SetLimits(0,13.5);
c1->Draw();
c1->SetGridy(0);
c1->SetGridx(0);

TFile *f1 = TFile::Open("first.root");
TCanvas *c1 = (TCanvas*)f1->Get("h2");
h2->SetName("F");
h2->Draw("hist same");
F->SetLineColor(1);
F->SetLineWidth(2);


   
   TFile *f2 = TFile::Open("second.root");
   TCanvas *c2 = (TCanvas*)f2->Get("h2");
   h2->SetName("S");
   S->SetLineColor(2);
   S->SetLineWidth(2);
   h2->Draw("hist same");
   
   TFile *f2 = TFile::Open("third.root");
   TCanvas *c2 = (TCanvas*)f2->Get("h2");
   h2->SetName("T");
   T->SetLineColor(3);
   T->SetLineWidth(2);
   h2->Draw("hist same");
   
   TFile *f2 = TFile::Open("fourth.root");
   TCanvas *c2 = (TCanvas*)f2->Get("h2");
   h2->SetName("X");
   X->SetLineColor(4);
   X->SetLineWidth(2);
   h2->Draw("hist same"); 


leg = new TLegend(0.51,0.7,0.9,0.9);
   leg->SetHeader("Test");
   leg->AddEntry(F,"First","l");
   leg->AddEntry(S, "second","l");
   leg->AddEntry(T, "third","l");
   leg->AddEntry(X, "fourth","l");
   leg->AddEntry(Graph,"Testgraph","p");
   leg->SetFillColor(10);
   leg->Draw();
   


}

I need help for vs.6

I’m attaching root files for reference purpose. (*Being a new user i can only add two files. So, avoiding the last three files).
Thanks in advance.

Can anyone help me out ?

Hi,
since in your first.root file have no canvas you have to get the histogram.

TFile *f1 = TFile::Open(“first.root”);
TH1D *h2 = (TH1D)f1->Get(“h2”);
h2->SetName("F");
h2->Draw(“hist same”);
h2->SetLineColor(1);
h2->SetLineWidth(2);

almost the same problem is addressed here

Ciao,
Stefano

Hi @A_Kumar. Welcome here. Notice, in this page, the way to post properly code snippets. I have edited your post to make it easier to read.