# Opening two .root file in the same canvas

**URL:** <https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562>\
**Category:** ROOT\
**Created:** [June 9, 2011, 8:52am UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562 "2011-06-09T08:52:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 9, 2011, 8:52am UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/1 "2011-06-09T08:52:56Z")

</div>

Hi Every Body,

I have two .root files including histograms. I got them from AIDA in geant4. For opening these files I need root.  
I need to open these two files in one canvas in order to be able to compare them.  
I am wondering should I write a c++ code for that or can I do it easily from the browser?  
Really thank for your help

---

<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:** [June 9, 2011, 10:07am UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/2 "2011-06-09T10:07:30Z")

</div>

What do you have in these root files ? Histograms ? trees ? Canvases ?..  
I would say you should create a canvas, divide it using Divide(), do one plot in the first pad and the other in the pad next to it. For instance like in :  
[root.cern.ch/root/html/tutorials … opt.C.html](http://root.cern.ch/root/html/tutorials/hist/draw2dopt.C.html)  
[root.cern.ch/root/html/tutorials … dom.C.html](http://root.cern.ch/root/html/tutorials/hist/fillrandom.C.html)  
etc …

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 9, 2011, 12:25pm UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/3 "2011-06-09T12:25:10Z")

</div>

Hej,

I have histograms in those root files that I have to compare them.

No, I think using Divide() just put them close to each other .

I need something like: [root.cern.ch/drupal/category/ima … ualization](http://root.cern.ch/drupal/category/image-galleries/data-analysis-visualization)

The first screenshot from the left shows an Exclusion graph in ATLAS. You see there are more than one graphs in one canvas.

Thank you

---

<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:** [June 9, 2011, 1:35pm UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/4 "2011-06-09T13:35:42Z")

</div>

You plot the first histogram then the 2nd one using the [option SAME](https://root.cern.ch/doc/master/classTHistPainter.html#HP060).

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 9, 2011, 2:31pm UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/5 "2011-06-09T14:31:56Z")

</div>

Hi again,

Can I ask you a very beginner question.  
I need to create a script to type those codes, Could you please kindly tell me how I can create a script in root ?  
Thank you again

---

<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:** [June 9, 2011, 2:37pm UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/6 "2011-06-09T14:37:45Z")

</div>

In a file called simple.C you put:

```auto
{
   TFile f1("f1.root");
   h1->Draw(); // h1 is the first histogram to draw sitting in f1.root
   TFile f2("f2.root");
   h2->Draw("same"); // h2 is the 2nd histogram to draw sitting in f2.root
}
```

Then you enter root and you type “.x simple.C”

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 9, 2011, 3:21pm UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/7 "2011-06-09T15:21:19Z")

</div>

Aha Thanks 😊

I have enclosed my two files,  
By looking at the Superimposing Histograms with Different Scales script, I see that I only need to do

```
                                                             [code]2__2->Draw("same");[/code]

```

So I just combined the C code for two histograms and added the line above.

1. I don’t think that I can see the file essai2\_2.C in TBrowser ☹

2.I am just wondering that when I change a script, I don’t need to compile it in root! just execute it?

Thanks again  
[essai1\_2.C](https://root-forum.cern.ch/uploads/default/original/2X/2/20e10fb8abcc55ccd92dd1b1de6ef213980274ac.C) (15.2 KB)  
[essai2\_2.C](https://root-forum.cern.ch/uploads/default/original/2X/e/e3e32a3b54f52c37aea589bdbe553b053fdc7754.C) (7.6 KB)

---

<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:** [June 9, 2011, 3:30pm UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/8 "2011-06-09T15:30:24Z")

</div>

I think you should spend a bit of time reading the doc…  
What you did cannot work … you cannot combine macros like that … ![[-X](https://root-forum.cern.ch/uploads/default/original/2X/9/95086c44a6d6e15518f9051f135a3de48bdad392.gif "Shame on you")  
do you have the root files with the histograms what to plot ? i will try to make a macro …

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 9, 2011, 6:20pm UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/9 "2011-06-09T18:20:07Z")

</div>

Thank you for your help

These three root files contain three histograms (1,2,3)  
Let’s suppose that I want to compare all histograms number number 2 in these three files.  
Best Regards  
[essai3.root](https://root-forum.cern.ch/uploads/default/original/2X/e/e6e8f681f9fa55a071e5ff27675dcd3001882651.root) (9.4 KB)  
[essai2.root](https://root-forum.cern.ch/uploads/default/original/2X/a/ad79d0bdfcb2453dd629381057a3f1bf8064880d.root) (9.74 KB)  
[essai1.root](https://root-forum.cern.ch/uploads/default/original/2X/2/27ccb701607cbb50dfae7d345ab3acbe89f04cfb.root) (10 KB)

---

<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:** [June 10, 2011, 7:46am UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/10 "2011-06-10T07:46:03Z")

</div>

Here is the script:  
[essai.C](https://root-forum.cern.ch/uploads/default/original/2X/3/3282ed9c25926e2d4d142dc68d82e4b44908222b.C) (286 Bytes)

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 10, 2011, 8:48am UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/11 "2011-06-10T08:48:15Z")

</div>

Hello,

Thanks a lot for your script. Unfortunately here comes my weakness in pointers and c++.  
I get illegal pointer error message by running your script.

> [@](#):
>
> Error: Incorect assignment to h1,wrong type ‘TObject\*’ essai.C:3:

Could you pls look at these two specific root files: I tried to replace 2\_\_1 and 2\_\_2 instead of h1 and h2 , but I got:

> [@](#):
>
> illeagl numerical expression 2\_\_1-\>Draw(“l”)

Again many thanks in advance  
[essai2\_2.root](https://root-forum.cern.ch/uploads/default/original/2X/c/c220383cb24d8a06f8825d381a0f195b896d7427.root) (16.1 KB)  
[essai1\_2.root](https://root-forum.cern.ch/uploads/default/original/2X/5/5e1e60e26773ff03233e22f85fde4b100bc2d7d3.root) (16.1 KB)

---

<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:** [June 10, 2011, 8:52am UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/12 "2011-06-10T08:52:55Z")

</div>

Have you run the macro I sent you ? it should work if you do not modify it… does it ?

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 10, 2011, 9:05am UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/13 "2011-06-10T09:05:45Z")

</div>

Yes,

and the result is:

> [@](#):
>
> Incorrect assignment to h1, wrong type ‘TObject\*’ essai.C:3:

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 10, 2011, 9:08am UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/14 "2011-06-10T09:08:21Z")

</div>

No really sorry…

I opened a new root window. it works… 🙂

Could u please also help me with the two recent files , I sent u.  
Really thanks

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 10, 2011, 9:15am UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/15 "2011-06-10T09:15:30Z")

</div>

Sorry,

I type the same as your script in my terminal…

e.g: `TH1D *h1=f1.Get("2__1");` instead

But when I do `h1->Draw("l"); ` or `h1->SetLineColor(kRed);` I receive:

> [@](#):
>
> illegal pointer to class object h1 0\*0 1239 (tmpfile):1:

---

<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:** [June 10, 2011, 9:16am UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/16 "2011-06-10T09:16:43Z")

</div>

These file contains Canvases not histrograms … You are mixing up many diferents things…

```auto
root [0] TFile f("essai1_2.root")
root [1] f.ls()
TFile** essai1_2.root	
 TFile* essai1_2.root	
  KEY: TCanvas	Canvas 1;1	Canvas 1
root [2] TCanvas *c = f.Get("Canvas 1");
root [3] c.Draw()
```

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 10, 2011, 11:01am UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/17 "2011-06-10T11:01:00Z")

</div>

Yes you are right. The problem is that I have sort of limited time and I am sort of stressed unfortunately.

I have enclosed my two files again asking you another question. I will really appreciate if you help me with them.

1. in essai3.root, the histogram number 2 is sort of noisy, but it is not the case in essai2.root, Nevertheless while superimposing them, the green dots represents a line like y=0 , not the histogram 2 in essai3.root!

2. I need to have a legend,naming both histograms. I found this link:  
[slac.stanford.edu/BFROOT/www … ml#compare](http://www.slac.stanford.edu/BFROOT/www/doc/workbook/root1/root1.html#compare)  
in section comparing histograms:

` leg_hist = new TLegend(0.5,0.6,0.79,0.79);
  leg_hist->SetHeader("Some histograms");
  leg_hist->AddEntry(hist_1,"First histo","l");
  leg_hist->AddEntry(hist_2,"Second histo","l");
  leg_hist->Draw(); `

in my case i only changed `hist_1` with `h2` and `hist_2` with `h3` for my case, and you know that it didn’t work.

I am really thankful if you help me with this as well.  
[essai2.root](https://root-forum.cern.ch/uploads/default/original/2X/a/ad79d0bdfcb2453dd629381057a3f1bf8064880d.root) (9.74 KB)  
[essai3.root](https://root-forum.cern.ch/uploads/default/original/2X/e/e6e8f681f9fa55a071e5ff27675dcd3001882651.root) (9.4 KB)

---

<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:** [June 10, 2011, 12:09pm UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/18 "2011-06-10T12:09:02Z")

</div>

Try this:

```auto
{
   TFile f1("essai1.root");
   TH1D *h1=f1.Get("2");
   TFile f2("essai2.root");
   TH1D *h2=f2.Get("2");
   TFile f3("essai3.root");
   TH1D *h3=f3.Get("2");
   
   h2->SetLineColor(kRed);
   h3->SetLineColor(kGreen);
   
   h1->Draw("l");
   h2->Draw("same");
   h3->Draw("same");
   
   gPad->BuildLegend();
}
```

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 10, 2011, 1:15pm UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/19 "2011-06-10T13:15:24Z")

</div>

Could you please kindly run the script as well and see if you get the same result as 1and2and3.root?  
[1and2and3.root](https://root-forum.cern.ch/uploads/default/original/2X/d/d392cf2bd9c787709515b89b3758ada9503b828c.root) (18.2 KB)  
[essai3.root](https://root-forum.cern.ch/uploads/default/original/2X/b/ba107227281d5125f789354b649bbb0f96986ee7.root) (9.66 KB)  
[essai2.root](https://root-forum.cern.ch/uploads/default/original/2X/7/7be02dbe8eaf8c66aac5929d373d0b8d79658803.root) (9.97 KB)  
[essai1.root](https://root-forum.cern.ch/uploads/default/original/2X/3/36fcf9de978455585e7269f550f102be1793faf2.root) (9.61 KB)

---

<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:** [June 10, 2011, 1:31pm UTC](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/20 "2011-06-10T13:31:07Z")

</div>

Try the attached macro. It gives the attached picture.

 ![](https://root-forum.cern.ch/uploads/default/original/2X/4/40e97ac57561d06bd1877efa2de293d93e17f1fa.gif)  
  
[essai2.C](https://root-forum.cern.ch/uploads/default/original/2X/1/12fab766be1555ba83d912bbbec2408c98986368.C) (495 Bytes)

[Next page](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562.md?page=2)
