# Adding contents of 2 or more TH2F/D

**URL:** <https://root-forum.cern.ch/t/adding-contents-of-2-or-more-th2f-d/35574>\
**Category:** Newbie\
**Created:** [August 8, 2019, 5:17pm UTC](https://root-forum.cern.ch/t/adding-contents-of-2-or-more-th2f-d/35574 "2019-08-08T17:17:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eadams](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/d2c977/32.png) [@eadams](https://root-forum.cern.ch/u/eadams)\
**Post date:** [August 8, 2019, 5:17pm UTC](https://root-forum.cern.ch/t/adding-contents-of-2-or-more-th2f-d/35574/1 "2019-08-08T17:17:11Z")

</div>

Hello,

I’m trying to add 2 TH2F/D histograms together so their bin contents are added. I have been trying in general and have not achieved any success when copying other forum posts:

> [@Adding two TH2F histograms](https://root-forum.cern.ch/t/adding-two-th2f-histograms/8592):
>
> Hi I’m currently trying to add (subtract) two TH2F histograms. I’ve plotted positive charge against momentum in os, and negative charge against momentum in ss. I wish to plot the different of positive to negative charges against momentum. I tried the following: h1-\>Add(os,ss,1,-1); h1-\>Draw(“e”); But what I get out is nonsense (covers the whole plot with entries). I have been working with TH1F only, so I wasn’t sure if the Add command was the right one. Can someone lend me a hand please? Tha…

> [@How to add two 2D histogram](https://root-forum.cern.ch/t/how-to-add-two-2d-histogram/23690):
>
> Hi, I am trying to do add two 2D histogram but getting error. Below I am describing briefly, what I am doing: [code]h1=ROOT.TH2F(“h1”,"",10,0,10,10,0,10) h2=ROOT.TH2F(“h2”,"",10,0,10,10,0,10) hnew = h1.Clone(“hnew”); hnew.Add(h2);[/code] But, I am getting following error: [color=#FF0000] Traceback (most recent call last): File “TwoDplots\_Wjets.py”, line 81, in hnew.Add(h1); AttributeError: ‘PyROOT\_NoneType’ object has no attribute ‘Add’[/color] What may be I am doing wrong? Please …

The error I keep getting through all of my attempts using different examples is: error: no member named ‘Add’ in ‘TObject’

I copied the example from the latter link posted (minus the namespace) :  
root [0] gROOT-\>GetVersion()  
(const char \*) “6.18/00”  
root [1] h1=TH2F(“h1”,“”,10,0,10,10,0,10)  
(TH2F &) Name: h1 Title: NbinsX: 10  
root [2] h1.Fill(5.,5.)  
(int) 78  
root [3] h2=TH2F(“h2”,“”,10,0,10,10,0,10)  
(TH2F &) Name: h2 Title: NbinsX: 10  
root [4] h2.Fill(1.,1.)  
(int) 26  
root [5] hnew = h1.Clone(“hnew”)  
(TObject \*) @0x7fffdf1cca58  
root [6] hnew.Add(h2)  
ROOT\_prompt\_6:1:5: error: member reference type ‘TObject \*’ is a pointer; did you mean to use ‘-\>’?  
hnew.Add(h2)

```auto
    ->
ROOT_prompt_6:1:6: error: no member named 'Add' in 'TObject'
hnew.Add(h2)
~~~~ ^ 
root [7] hnew->Add(h2)
ROOT_prompt_7:1:7: error: no member named 'Add' in 'TObject'
hnew->Add(h2)
~~~~ ^
root [8] .pwd
Current directory: Rint:/
Current style: Modern
root [9] .ls
 OBJ: TH2F h1 : 0 at: 0x7f19c4210010
 OBJ: TH2F h2 : 0 at: 0x7f19c4210418
 OBJ: TH2F hnew : 0 at: 0x7fffd8f70e60

Thank you for your help.
```

---

<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:** [August 8, 2019, 5:27pm UTC](https://root-forum.cern.ch/t/adding-contents-of-2-or-more-th2f-d/35574/2 "2019-08-08T17:27:42Z")

</div>

```cpp
((TH2F*)(h1.Clone("hnew")))

```

---

<div class="post-metadata">

**Author:** ![eadams](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/d2c977/32.png) [@eadams](https://root-forum.cern.ch/u/eadams)\
**Post date:** [August 8, 2019, 5:45pm UTC](https://root-forum.cern.ch/t/adding-contents-of-2-or-more-th2f-d/35574/3 "2019-08-08T17:45:06Z")

</div>

> [@eadams](#):
>
> hnew.Add(h2)

Thank you for your response. The same error as before is persisting, did I implement your suggesting correctly?

root [0] h1=TH2F(“h1”,“”,10,0,10,10,0,10)  
(TH2F &) Name: h1 Title: NbinsX: 10  
root [1] h1.Fill(5.,5.)  
(int) 78  
root [2] h2=TH2F(“h2”,“”,10,0,10,10,0,10)  
(TH2F &) Name: h2 Title: NbinsX: 10  
root [3] h2.Fill(1.,1.)  
(int) 26  
root [4] ((TH2F\*)(h1.Clone(“hnew”)))  
(TH2F \*) 0x7fffc38adeb0  
root [5] hnew.Add(h2)  
ROOT\_prompt\_5:1:5: error: member reference type ‘TH2F \*’ is a pointer; did you mean to use ‘-\>’?  
hnew.Add(h2)

```auto
    ->
ROOT_prompt_5:1:6: error: no matching member function for call to 'Add'
hnew.Add(h2)
~~~~~^~~
/home/eadams/Downloads/root_build/include/TH1.h:186:21: note: candidate function not viable: no known conversion from 'TH2F' to 'const TH1 *' for 1st argument; take the address of the argument with &
   virtual Bool_t Add(const TH1 *h1, Double_t c1=1);
                    ^
/home/eadams/Downloads/root_build/include/TH1.h:185:21: note: candidate function not viable: no known conversion from 'TH2F' to 'TF1 *' for 1st argument
   virtual Bool_t Add(TF1 *h1, Double_t c1=1, Option_t *option="");
                    ^
/home/eadams/Downloads/root_build/include/TH1.h:187:21: note: candidate function not viable: requires at least 2 arguments, but 1 was provided
   virtual Bool_t Add(const TH1 *h, const TH1 *h2, Double_t c1=1, Double_t c2=1); // *MENU*
                    ^
root [6] hnew->Add(h2)
ROOT_prompt_6:1:7: error: no matching member function for call to 'Add'
hnew->Add(h2)
~~~~~~^~~
/home/eadams/Downloads/root_build/include/TH1.h:186:21: note: candidate function not viable: no known conversion from 'TH2F' to 'const TH1 *' for 1st argument; take the address of the argument with &
   virtual Bool_t Add(const TH1 *h1, Double_t c1=1);
                    ^
/home/eadams/Downloads/root_build/include/TH1.h:185:21: note: candidate function not viable: no known conversion from 'TH2F' to 'TF1 *' for 1st argument
   virtual Bool_t Add(TF1 *h1, Double_t c1=1, Option_t *option="");
                    ^
/home/eadams/Downloads/root_build/include/TH1.h:187:21: note: candidate function not viable: requires at least 2 arguments, but 1 was provided
   virtual Bool_t Add(const TH1 *h, const TH1 *h2, Double_t c1=1, Double_t c2=1); // *MENU*
                    ^
root [7]
```

---

<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:** [August 8, 2019, 5:51pm UTC](https://root-forum.cern.ch/t/adding-contents-of-2-or-more-th2f-d/35574/4 "2019-08-08T17:51:49Z")

</div>

[A ROOT Guide For Beginners](https://root.cern.ch/root/htmldoc/guides/primer/ROOTPrimer.html)  
[ROOT User’s Guide](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html)

---

<div class="post-metadata">

**Author:** ![eadams](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/d2c977/32.png) [@eadams](https://root-forum.cern.ch/u/eadams)\
**Post date:** [August 8, 2019, 6:03pm UTC](https://root-forum.cern.ch/t/adding-contents-of-2-or-more-th2f-d/35574/5 "2019-08-08T18:03:26Z")

</div>

I’m confident my implementation of your suggestion was correct, after referencing the manual, and I still got the same error.

In the root manual:  
The section on adding histograms is only applicable to TH1 (as h1+h2 does not work for TH2) and the add function is only briefly mentioned and I have used it (as described in the root manual) and gotten the same error: no matching member function for call to ‘Add’ for TH2F

Please help me to understand why I can’t successfully add TH2s on my root. Thank you.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [August 10, 2019, 8:39am UTC](https://root-forum.cern.ch/t/adding-contents-of-2-or-more-th2f-d/35574/6 "2019-08-10T08:39:25Z")

</div>

> [@eadams](#):
>
> virtual Bool\_t Add(const TH1 \*h1, Double\_t c1=1);

The error message is telling you that you have to pass to Add a pointer to histogram, but your h2 (as well as h1) is not a pointer:

> [@eadams](#):
>
> root [2] h2=TH2F(“h2”,“”,10,0,10,10,0,10)  
> (TH2F &) Name: h2 Title: NbinsX: 10

see the difference with

> [@eadams](#):
>
> root [4] ((TH2F\*)(h1.Clone(“hnew”)))  
> (TH2F \*) 0x7fffc38adeb0

So better try with:

```auto
TH2F *h1 = new TH2F("h1","",10,0,10,10,0,10);
TH2F *h2 = new TH2F("h2","",10,0,10,10,0,10);

```

And use ‘-\>’ instead of ‘.’ for Fill, Clone, etc.
