# Adding TH2D in a loop

**URL:** <https://root-forum.cern.ch/t/adding-th2d-in-a-loop/33966>\
**Category:** Newbie\
**Tags:** python\
**Created:** [May 8, 2019, 9:51pm UTC](https://root-forum.cern.ch/t/adding-th2d-in-a-loop/33966 "2019-05-08T21:51:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_rthur](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/_rthur/32/18547_2.png) [@\_rthur](https://root-forum.cern.ch/u/_rthur)\
**Post date:** [May 8, 2019, 9:51pm UTC](https://root-forum.cern.ch/t/adding-th2d-in-a-loop/33966/1 "2019-05-08T21:51:28Z")

</div>

Dear All,

I hope this question fits in the Newbie section. I have several root files which contain a TH2D histogram with exactly the same number of bins (basically it is the same histogram but filled with different data in each root file). I want to produce a single plot out of all of them. I am initializing an empty histogram and then adding the others to it. For obvious reasons (different binning) my code does not run but I want to avoid to change specific histogram and then loop over the others. Is there any workaround this? I am using PyROOT for this. My code is this one:

```auto
    for folder in folders:
        h_ph_2D_sum = TH2D()
        # Looping over all the root files in subdirectory
        for root_file in rootfile_dict[folder]:
            tmp_file = TFile(folder + root_file) # Getting root file
            h_ph_2D_sum.Add(tmp_file.Get("h_2D"))

```

the error code is then:

```auto
Info in <TH2D::Add>: Attempt to add histograms with different number of bins - trying to use TH1::Merge
Error in <Merge>: Cannot merge histograms - limits are inconsistent:
 first: (1, 0.000000, 1.000000), second: h_2D (128, 0.500000, 1280.500000)

```

I understand the error but I do not know any other solution apart from initializing the histogram to the correct number of bins.

Thanks in advance,  
Arturo

---

<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:** [May 9, 2019, 7:48am UTC](https://root-forum.cern.ch/t/adding-th2d-in-a-loop/33966/2 "2019-05-09T07:48:29Z")

</div>

> [@\_rthur](#):
>
> apart from initializing the histogram to the correct number of bins.

Yes that what you should do.  
You can simply get this information from the stored histograms.

---

<div class="post-metadata">

**Author:** ![\_rthur](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/_rthur/32/18547_2.png) [@\_rthur](https://root-forum.cern.ch/u/_rthur)\
**Post date:** [May 9, 2019, 8:09am UTC](https://root-forum.cern.ch/t/adding-th2d-in-a-loop/33966/3 "2019-05-09T08:09:46Z")

</div>

Hi,  
Thanks for the reply.  
So, there is not other workaround this?

---

<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:** [May 9, 2019, 8:20am UTC](https://root-forum.cern.ch/t/adding-th2d-in-a-loop/33966/4 "2019-05-09T08:20:08Z")

</div>

I do not see what is the penalty doing that ?

---

<div class="post-metadata">

**Author:** ![\_rthur](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/_rthur/32/18547_2.png) [@\_rthur](https://root-forum.cern.ch/u/_rthur)\
**Post date:** [May 9, 2019, 11:23am UTC](https://root-forum.cern.ch/t/adding-th2d-in-a-loop/33966/5 "2019-05-09T11:23:58Z")

</div>

You’re right I just have to initialize with one histogram and the run my loop over the others.  
Thank you
