# Adding two TH2F histograms

**URL:** <https://root-forum.cern.ch/t/adding-two-th2f-histograms/8592>\
**Category:** ROOT\
**Created:** [July 6, 2009, 2:23pm UTC](https://root-forum.cern.ch/t/adding-two-th2f-histograms/8592 "2009-07-06T14:23:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmasterkim](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@pmasterkim](https://root-forum.cern.ch/u/pmasterkim)\
**Post date:** [July 6, 2009, 2:23pm UTC](https://root-forum.cern.ch/t/adding-two-th2f-histograms/8592/1 "2009-07-06T14:23:40Z")

</div>

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? Thank you

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [July 6, 2009, 4:24pm UTC](https://root-forum.cern.ch/t/adding-two-th2f-histograms/8592/2 "2009-07-06T16:24:04Z")

</div>

Could you send the shortest possible RUNNING script reproducing/explaining your problem?  
Do you have the same binning in your histograms?  
Note that you can simply do

` os.Add(ss,-1);
`This will substract ss from os. You can save the original os before the operation if necessary.

Rene

---

<div class="post-metadata">

**Author:** ![pmasterkim](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@pmasterkim](https://root-forum.cern.ch/u/pmasterkim)\
**Post date:** [July 6, 2009, 5:07pm UTC](https://root-forum.cern.ch/t/adding-two-th2f-histograms/8592/3 "2009-07-06T17:07:28Z")

</div>

This is very vague,

```auto

os = new TH2F("os","TLpt:OS",120,0,120,120,0,120);    
ss = new TH2F("ss","TLpt:SS",120,0,120,120,0,120);
oss = new TH2F("oss","TLpt:OS-SS",120,0,120,120,0,120);
 

data->Draw("pt*dQ:TLpt>>os","njet==1&&Ht>=0&&Met>20&&SLT==1&&run<237795&&dQ<0");
 data->Draw("pt*dQ:TLpt>>ss","njet==1&&Ht>=0&&Met>20&&SLT==1&&run<237795&&dQ>0");
oss->Add(os,ss,1,-1);
oss->Draw();

```

They have the same number of bins, and the variables are filled from ntuples. Thank you for helping

Kim
