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

**URL:** <https://root-forum.cern.ch/t/draw-multiple-root-files-along-with-graph-file-on-the-same-canvas/34956>\
**Category:** Newbie\
**Created:** [July 2, 2019, 10:22am UTC](https://root-forum.cern.ch/t/draw-multiple-root-files-along-with-graph-file-on-the-same-canvas/34956 "2019-07-02T10:22:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_Kumar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@A\_Kumar](https://root-forum.cern.ch/u/A_Kumar)\
**Post date:** [July 2, 2019, 10:22am UTC](https://root-forum.cern.ch/t/draw-multiple-root-files-along-with-graph-file-on-the-same-canvas/34956/1 "2019-07-02T10:22:10Z")

</div>

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 this[first.root](https://root-forum.cern.ch/uploads/short-url/r39mKLoEkj2KfIsGNfECkTXVWZo.root) (45.0 KB) [Testgraph.root](https://root-forum.cern.ch/uploads/short-url/wsmDb8H0s3ezpzPlzK9toq9UaE1.root) (14.5 KB)

```auto
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.

---

<div class="post-metadata">

**Author:** ![A\_Kumar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@A\_Kumar](https://root-forum.cern.ch/u/A_Kumar)\
**Post date:** [July 3, 2019, 5:51am UTC](https://root-forum.cern.ch/t/draw-multiple-root-files-along-with-graph-file-on-the-same-canvas/34956/2 "2019-07-03T05:51:21Z")

</div>

Can anyone help me out ?

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [July 3, 2019, 6:11am UTC](https://root-forum.cern.ch/t/draw-multiple-root-files-along-with-graph-file-on-the-same-canvas/34956/3 "2019-07-03T06:11:14Z")

</div>

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

```auto
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

> [@Draw two histograms in same canvas from two different root files](https://root-forum.cern.ch/t/draw-two-histograms-in-same-canvas-from-two-different-root-files/34900/3):
>
> Hi, I attached below a modified version of your code. (Remember to put you code between two ``` in this way will appear highlited). there are 3 main point I added two ; after the opening of the files. When you use Get() to get the histogram recast as a TH1D and not a TCanvas, otherwise you cannot the TH1D methods. the Draw is made using the TH1D pointer and not the name of the histogram in the file. void macro() { TFile \*f1 = TFile::Open("first.root"); TH1D \*h1= (TH1D\*)f1…

Ciao,  
Stefano

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 3, 2019, 8:09am UTC](https://root-forum.cern.ch/t/draw-multiple-root-files-along-with-graph-file-on-the-same-canvas/34956/4 "2019-07-03T08:09:17Z")

</div>

Hi @A_Kumar. Welcome here. Notice, [in this page](https://root-forum.cern.ch/t/tips-for-efficient-and-successful-posting/28292/2), the way to post properly code snippets. I have edited your post to make it easier to read.
