Adding contents of 2 or more TH2F/D

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:


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)

    ->
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.
((TH2F*)(h1.Clone("hnew")))

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)

    ->
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]

A ROOT Guide For Beginners
ROOT User’s Guide

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.

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:

see the difference with

So better try with:

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.