How to add two 2D histogram

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

I cannot reproduce.

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()
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

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.

    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