# How to add two 2D histogram

**URL:** <https://root-forum.cern.ch/t/how-to-add-two-2d-histogram/23690>\
**Category:** ROOT\
**Created:** [February 7, 2017, 3:52am UTC](https://root-forum.cern.ch/t/how-to-add-two-2d-histogram/23690 "2017-02-07T03:52:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramkrishna](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ramkrishna](https://root-forum.cern.ch/u/ramkrishna)\
**Post date:** [February 7, 2017, 3:52am UTC](https://root-forum.cern.ch/t/how-to-add-two-2d-histogram/23690/1 "2017-02-07T03:52:57Z")

</div>

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 let me know.

Thanks & regards,  
Ram

---

<div class="post-metadata">

**Author:** ![mato](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mato/32/12317_2.png) [@mato](https://root-forum.cern.ch/u/mato)\
**Post date:** [February 7, 2017, 3:13pm UTC](https://root-forum.cern.ch/t/how-to-add-two-2d-histogram/23690/2 "2017-02-07T15:13:53Z")

</div>

I cannot reproduce.

```auto
import ROOT
h1=ROOT.TH2F("h1","",10,0,10,10,0,10)
h1.Fill(5.,5.)
h2=ROOT.TH2F("h2","",10,0,10,10,0,10)
h2.Fill(1.,1.)
hnew = h1.Clone("hnew")
hnew.Add(h2)
hnew.Print()
```

```auto
TH1.Print Name = hnew, Entries= 2, Total sum= 2
```

Can it be that you define h1 and h2 variables and they go out of scope?  
Can you indicate the version of ROOT and the platform?  
Cheers, Pere

---

<div class="post-metadata">

**Author:** ![ramkrishna](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ramkrishna](https://root-forum.cern.ch/u/ramkrishna)\
**Post date:** [February 7, 2017, 7:34pm UTC](https://root-forum.cern.ch/t/how-to-add-two-2d-histogram/23690/3 "2017-02-07T19:34:15Z")

</div>

Dear Pere,

Thanks for helping.

Yes, this also worked for me. Now, I checked and got that I made an if condition which is causing the problem. If I remove the if condition then it worked. If you have idea how I can use “Add” in the if condition then please let me know.

```auto
    for files in RootFiles:
        filename=re.sub('.root','',RootFiles[countf])
        filename=re.sub('WWTree_','',filename)
        filename=filename.strip()       
        h1.SetTitle(filename)
        f1=ROOT.TFile(path+files,"READ")
        T=f1.Get("otree")
        print "Total Entries = ",T.GetEntries()
        T.Draw(columns[0]+">>h1",cut,"colz")
        h1.Scale(factor[countf])
        if countf==0:
                hnew = h1.Clone("hnew");
                print "made new histo......."
        else:
                print "Adding new histo......."
                hnew.Add(h1);
        countf=countf+1
    c.Print("plots/"+filename+"/Scatter_"+filename+"_"+titles[0]+"_vs_"+titles[1]+".png")
    c.Print("plots/"+filename+"/Scatter_"+filename+"_"+titles[0]+"_vs_"+titles[1]+".pdf")
    countv=countv+1
```
