# Adding two histograms from two different root directories

**URL:** <https://root-forum.cern.ch/t/adding-two-histograms-from-two-different-root-directories/41807>\
**Category:** Newbie\
**Created:** [October 10, 2020, 10:39pm UTC](https://root-forum.cern.ch/t/adding-two-histograms-from-two-different-root-directories/41807 "2020-10-10T22:39:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sbecka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@Sbecka](https://root-forum.cern.ch/u/Sbecka)\
**Post date:** [October 10, 2020, 10:39pm UTC](https://root-forum.cern.ch/t/adding-two-histograms-from-two-different-root-directories/41807/1 "2020-10-10T22:39:22Z")

</div>

Hi,  
I have a root file with two directories, say, dir1 and dir2. The two directories contain two different histograms, hist1 and hist2. I need to add these two histograms. Can anybody please tell me how can I do it from the root prompt with a command line?

Thank you.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [October 12, 2020, 8:41am UTC](https://root-forum.cern.ch/t/adding-two-histograms-from-two-different-root-directories/41807/2 "2020-10-12T08:41:33Z")

</div>

Hi @Sbecka,  
you can retrieve a histogram from a directory with

```c++
root [0] auto f = TFile::Open("yourfile.root");
root [1] auto h = f->Get<TH1>("dir1/histogramname")

```

and when you get the two histograms you can add one to the other with `h1->Add(h2)`.

Hope this helps!  
Enrico

---

<div class="post-metadata">

**Author:** ![Sbecka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@Sbecka](https://root-forum.cern.ch/u/Sbecka)\
**Post date:** [October 12, 2020, 6:58pm UTC](https://root-forum.cern.ch/t/adding-two-histograms-from-two-different-root-directories/41807/3 "2020-10-12T18:58:22Z")

</div>

Thanks Enrico.

When I add two histograms from the same directory using Add method, it works fine. But when I follow the procedure you described and try to add two histograms afterwards, it shows me error message ,  
ROOT\_prompt\_3:1:5: error: no member named ‘Add’ in ‘TObject’.  
Do you have any insight why this can happen?

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [October 12, 2020, 7:54pm UTC](https://root-forum.cern.ch/t/adding-two-histograms-from-two-different-root-directories/41807/4 "2020-10-12T19:54:05Z")

</div>

Ah sorry, the plain `f->Get` returns a `TObject*` (that you can cast to a `TH1*`). With a recent enough ROOT version you can instead call `f->Get<TH1>("...")` to directly retrieve a pointer of the desired type, otherwise you can do something like `static_cast<TH1*>(f->Get("..."))` – edited the original code snippet now.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Sbecka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@Sbecka](https://root-forum.cern.ch/u/Sbecka)\
**Post date:** [October 12, 2020, 8:04pm UTC](https://root-forum.cern.ch/t/adding-two-histograms-from-two-different-root-directories/41807/5 "2020-10-12T20:04:30Z")

</div>

Now it works, thank you so much!
