# Draw two histograms in same canvas from two different root files

**URL:** <https://root-forum.cern.ch/t/draw-two-histograms-in-same-canvas-from-two-different-root-files/34900>\
**Category:** Newbie\
**Created:** [June 28, 2019, 10:30am UTC](https://root-forum.cern.ch/t/draw-two-histograms-in-same-canvas-from-two-different-root-files/34900 "2019-06-28T10:30:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [June 28, 2019, 10:30am UTC](https://root-forum.cern.ch/t/draw-two-histograms-in-same-canvas-from-two-different-root-files/34900/1 "2019-06-28T10:30:57Z")

</div>

I’m new to ROOT and trying to draw two histogram on the same canvas from root file. I tried to use “same” method for vs.6.14/6 but it didn’t work. My senior who used the same method for vs.5.38.xx got the result with the following command lines[first.root](https://root-forum.cern.ch/uploads/default/original/3X/f/d/fd1958ba24d8e25904a8e17dc6b9b42552e2492d.root) (52.9 KB)  
[second.root](https://root-forum.cern.ch/uploads/default/original/3X/c/3/c35c1e5c31aed44b707975c56a5b9456af7cb017.root) (41.4 KB)

```auto
TFile *f1 = TFile::Open("first.root")
TCanvas *c1 = (TCanvas*)f1->Get("h1");
h1->Draw("hist l");
h1->SetLineWidth(3);
h1->SetLineColor(2);

TFile *f2 = TFile::Open("second.root")
TCanvas *c2 = (TCanvas*)f2->Get("h1");
h1->SetLineWidth(2);
h1->SetLineColor(4);
h1->Draw("hist l same");

```

I’m attaching the root files.  
I’ll be very grateful if someone can do me favor.  
Thanks.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 28, 2019, 10:42am UTC](https://root-forum.cern.ch/t/draw-two-histograms-in-same-canvas-from-two-different-root-files/34900/2 "2019-06-28T10:42:51Z")

</div>

```auto
{
  TFile *f1 = TFile::Open("first.root");
  f1->ls();
  TH1D *h1f; f1->GetObject("h1", h1f);
  h1f->SetLineColor(2);
  TFile *f2 = TFile::Open("second.root");
  f2->ls();
  TH1D *h1s; f2->GetObject("h1", h1s);
  h1s->SetLineColor(4);
  h1f->Draw("hist l");
  h1s->Draw("hist l same");
}

```

---

<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:** [June 28, 2019, 10:42am UTC](https://root-forum.cern.ch/t/draw-two-histograms-in-same-canvas-from-two-different-root-files/34900/3 "2019-06-28T10:42:58Z")

</div>

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.

```auto
void macro()
{
    
    TFile *f1 = TFile::Open("first.root");
    TH1D *h1= (TH1D*)f1->Get("h1");
    h1->Draw("hist l");
    h1->SetLineWidth(3);
    h1->SetLineColor(2);
    
    TFile *f2 = TFile::Open("second.root");
    TH1D *h2 = (TH1D*)f2->Get("h1");
    h2->SetLineWidth(2);
    h2->SetLineColor(4);
    h2->Draw("hist l same");
    
}

```

---

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [June 28, 2019, 11:52am UTC](https://root-forum.cern.ch/t/draw-two-histograms-in-same-canvas-from-two-different-root-files/34900/4 "2019-06-28T11:52:27Z")

</div>

Thanks for helping. It worked.

---

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [June 28, 2019, 11:53am UTC](https://root-forum.cern.ch/t/draw-two-histograms-in-same-canvas-from-two-different-root-files/34900/5 "2019-06-28T11:53:14Z")

</div>

Thanks. It helped me.
