# Subtract two histograms

**URL:** <https://root-forum.cern.ch/t/subtract-two-histograms/42046>\
**Category:** Newbie\
**Created:** [October 29, 2020, 4:09am UTC](https://root-forum.cern.ch/t/subtract-two-histograms/42046 "2020-10-29T04:09:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [October 29, 2020, 4:09am UTC](https://root-forum.cern.ch/t/subtract-two-histograms/42046/1 "2020-10-29T04:09:40Z")

</div>

Hello,  
I wish to plot a histogram that is the result of the data in `true_110deg_28.txt` minus the data in `true_background_110deg_28.txt`. Both files have the exact same number of bins (2048), but have different number of data points. Does someone know how to do it?

[true\_110deg\_28.txt](https://root-forum.cern.ch/uploads/short-url/2zHs8sccV5LGtBtRNpRlvx4zpEA.txt) (315.0 KB)  
[true\_background\_110deg\_28.txt](https://root-forum.cern.ch/uploads/short-url/hxGZthAZ90ATioQgLHlLoandwUE.txt) (190.8 KB)

\_ROOT Version:6.23/01

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [October 29, 2020, 9:47am UTC](https://root-forum.cern.ch/t/subtract-two-histograms/42046/2 "2020-10-29T09:47:57Z")

</div>

Hi ,  
to subtract two histograms you can call, if h1` and ‘h2’ are the pointers for the two histograms and assuming the histograms are of type TH1D:

```auto
TH1D * h3 = new TH1D(*h1); 
h3->Add(h2,-1);

```

`h3` will then be the result of `h1-h2`

Lorenzo

---

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [October 29, 2020, 12:47pm UTC](https://root-forum.cern.ch/t/subtract-two-histograms/42046/3 "2020-10-29T12:47:54Z")

</div>

I don’t know what I do wrong. When I do the following, I just get the inverse of h2 plotted, not h1-h2.

```auto
    TH1D* h1=new TH1D("Statistics" , "Compton Scattering for CS-137" ,2048,0.,2048.);
    h1->SetMarkerStyle(8);
    h1->SetTitle("Compton Scattering for CS-137 (Actual 110 Degrees);Channel Number (Energy);Number of Photons");
    ifstream inp; double y;
    gStyle->SetOptFit(1111);
    inp.open("110deg_28.pdf");
    while(!(inp >> y)==0){h1->Fill(y);}
    
TH1D* h2=new TH1D("Statistics" , "Compton Scattering for CS-137" ,2048,0.,2048.);
h2->SetMarkerStyle(8);
h2->SetTitle("Compton Scattering for CS-137 (Background 110 Degrees);Channel Number (Energy);Number of Photons");
ifstream inps; double x;
gStyle->SetOptFit(1111);
inps.open("true_background_110deg_28.txt");
while(!(inps >> x)==0){h2->Fill(x);}
    
    TH1D * h3 = new TH1D(*h1);
    h3->Add(h2,-1);
    h3->Draw();

```

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [October 29, 2020, 1:57pm UTC](https://root-forum.cern.ch/t/subtract-two-histograms/42046/4 "2020-10-29T13:57:15Z")

</div>

Hi Edward,

this

> [@Edward\_Finkelstein](#):
>
> `inp.open("110deg_28.pdf");`

can’t be right, can it?  
But the main problem is I think with

> [@Edward\_Finkelstein](#):
>
> `TH1D * h3 = new TH1D(*h1);`

– try changing this into

```auto
TH1D * h3 = (TH1*)h1->Clone("h3");

```

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [October 29, 2020, 2:51pm UTC](https://root-forum.cern.ch/t/subtract-two-histograms/42046/5 "2020-10-29T14:51:13Z")

</div>

@yus. This line

```auto
TH1D * h3 = new TH1D(*h1);

```

is correct.

After fixing the input file the code works for me

Lorenzo

---

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [October 29, 2020, 6:38pm UTC](https://root-forum.cern.ch/t/subtract-two-histograms/42046/6 "2020-10-29T18:38:56Z")

</div>

Ah ok, I just wrote the input file incorrectly, thank you all!
