# Combining root histograms not working

**URL:** <https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813>\
**Category:** Newbie\
**Created:** [July 24, 2023, 4:08pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813 "2023-07-24T16:08:48Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_L](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ian_l/32/33898_2.png) [@Ian\_L](https://root-forum.cern.ch/u/Ian_L)\
**Post date:** [July 24, 2023, 4:08pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/1 "2023-07-24T16:08:49Z")

</div>

Hello, I am trying to get histograms from three different .root files to be plotted on the same plot. This is the code I had used to try to get them to combine:

```auto
void simple()
{
   TFile *f1 = new TFile("TestCAL_Co60_d5z3.root", "READ");
   TH1D *h1 = f1->Get("Edep");

   TFile f2("TestCAL_C060_d5z6.root");
   TH1D *h2 = f2.Get("Edep");

   TFile f3("TestCAL_C060_d5z9.root");
   TH1D *h3 = f3.Get("Edep");

   h1->Draw();
   h2->Draw("same");
   h3->Draw("same");
}

```

When I run this code, these are the errors I get:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/7/9/793a7c394834c9481c0e36c17d5e3c0c2b1a711c.png)

How do I fix this problem?

Thank you.

_ROOT Version:_ 6.26  
_Platform:_ Ubuntu  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 24, 2023, 4:52pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/2 "2023-07-24T16:52:01Z")

</div>

Hi,

try replacing

```auto
   TH1D *h1 = f1->Get("Edep");
...
   TH1D *h2 = f2.Get("Edep");
...
   TH1D *h3 = f3.Get("Edep");

```

with

```auto
   TH1D *h1 = static_cast<TH1D*>(f1->Get("Edep"));
...
   TH1D *h2 = static_cast<TH1D*>(f2.Get("Edep"));
...
   TH1D *h3 = static_cast<TH1D*>(f3.Get("Edep"));

```

---

<div class="post-metadata">

**Author:** ![Ian\_L](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ian_l/32/33898_2.png) [@Ian\_L](https://root-forum.cern.ch/u/Ian_L)\
**Post date:** [July 24, 2023, 5:22pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/3 "2023-07-24T17:22:27Z")

</div>

I made those changes and the root files are being read. Now I get an error about using “same” when trying to plot the graphs on the same plot. Here is the error:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/a/4/a4172716567430a7022ead751115d4caac876b93.png)

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 24, 2023, 6:12pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/4 "2023-07-24T18:12:08Z")

</div>

> [@Ian\_L](#):
>
> Now I get an error about using “same”

This should not happen. Please upload your `.root` files and the latest version of your `simple.C` so that we could reproduce the issue.

---

<div class="post-metadata">

**Author:** ![Ian\_L](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ian_l/32/33898_2.png) [@Ian\_L](https://root-forum.cern.ch/u/Ian_L)\
**Post date:** [July 24, 2023, 6:47pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/5 "2023-07-24T18:47:41Z")

</div>

Here is the C file:  
[simple.C](https://root-forum.cern.ch/uploads/short-url/ildDPg7AnUk9PrScWdfLu5fDzBe.C) (384 Bytes)

I cannot upload the root files since they are each ~50 MB, and github wont let me upload them either.  
Is there another way to upload or send you the files?

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 24, 2023, 6:52pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/6 "2023-07-24T18:52:48Z")

</div>

You can use [https://www.transfernow.net/en](https://www.transfernow.net/en) to share large files without registration.

---

<div class="post-metadata">

**Author:** ![Ian\_L](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ian_l/32/33898_2.png) [@Ian\_L](https://root-forum.cern.ch/u/Ian_L)\
**Post date:** [July 24, 2023, 7:01pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/7 "2023-07-24T19:01:36Z")

</div>

Here is the link to the other files:

> **[Transfer "Root Forum Files" is available for download](https://www.transfernow.net/en/dltransfer?utm_source=20230724vRjfKnkl)**
>
> Click to access the download of 3 files (161 MB) from the "Root Forum Files" transfer via TransferNow

Thank you again!

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 24, 2023, 7:08pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/8 "2023-07-24T19:08:31Z")

</div>

The problem is that `Edep` in all these files is not a `TH1D`, but rather a `TTree`.

---

<div class="post-metadata">

**Author:** ![Ian\_L](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ian_l/32/33898_2.png) [@Ian\_L](https://root-forum.cern.ch/u/Ian_L)\
**Post date:** [July 24, 2023, 7:12pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/9 "2023-07-24T19:12:52Z")

</div>

Ah I see. Would TChain work in this case? I have never used it but I have heard it could help with TTrees.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 24, 2023, 7:19pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/10 "2023-07-24T19:19:49Z")

</div>

No, you just need to fetch info from your `TTree`, and `TChain` has nothing to do with this.

---

<div class="post-metadata">

**Author:** ![Ian\_L](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ian_l/32/33898_2.png) [@Ian\_L](https://root-forum.cern.ch/u/Ian_L)\
**Post date:** [July 24, 2023, 7:25pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/11 "2023-07-24T19:25:23Z")

</div>

Okay. Sorry to ask this, but how would I do that? I am not familiar enough with TTree yet. Thanks

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 24, 2023, 7:29pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/12 "2023-07-24T19:29:55Z")

</div>

That depends on what it is you want to do exactly. If it is just plotting the content of `fEdep_HPGe` from all three files on the same canvas, it is easy enough.

---

<div class="post-metadata">

**Author:** ![Ian\_L](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ian_l/32/33898_2.png) [@Ian\_L](https://root-forum.cern.ch/u/Ian_L)\
**Post date:** [July 24, 2023, 7:33pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/13 "2023-07-24T19:33:03Z")

</div>

Yes, I just want to plot the contents of each `fEdep_HPGe` on one plot. Where can I find the documentation on how to do this?

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [July 24, 2023, 7:35pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/14 "2023-07-24T19:35:44Z")

</div>

[https://root.cern.ch/root/htmldoc/guides/users-guide/Trees.html#simple-analysis-using-ttreedraw](https://root.cern.ch/root/htmldoc/guides/users-guide/Trees.html#simple-analysis-using-ttreedraw)

---

<div class="post-metadata">

**Author:** ![Ian\_L](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ian_l/32/33898_2.png) [@Ian\_L](https://root-forum.cern.ch/u/Ian_L)\
**Post date:** [July 24, 2023, 7:41pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/15 "2023-07-24T19:41:26Z")

</div>

Thank you! I will look into this and get back if I have any more questions or if I figure it out.

---

<div class="post-metadata">

**Author:** ![Ian\_L](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ian_l/32/33898_2.png) [@Ian\_L](https://root-forum.cern.ch/u/Ian_L)\
**Post date:** [July 27, 2023, 5:26pm UTC](https://root-forum.cern.ch/t/combining-root-histograms-not-working/55813/16 "2023-07-27T17:26:42Z")

</div>

I was able to figure it out! Thank you!
