# Subtracting multiple histogram with same TH2F

**URL:** <https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114>\
**Category:** Newbie\
**Created:** [October 15, 2018, 1:08pm UTC](https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114 "2018-10-15T13:08:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclee0620](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sclee0620](https://root-forum.cern.ch/u/sclee0620)\
**Post date:** [October 15, 2018, 1:08pm UTC](https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114/1 "2018-10-15T13:08:59Z")

</div>

Hello,  
I have a series of histograms that need to be added then subtracted in the final plot so I did following things

TCanvas\* c6 = new TCanvas(“c6”, “”, 50, 50, 1000, 1000);  
c6-\>Divide(2,2);  
if(c6){

c6-\>cd(1);  
TH2F \*posXYdiff12 = new TH2F(“posXYdiff12”, “The distribution of neutrons escaped from the target geometry (XY), r1+2”, 401, -200.5, 200.5, 401, -200.5, 200.5);  
EverythingN-\>Draw(“NPosY:NPosX\>\>posXYdiff12”, “reasonID==1”,“COLZ1”);  
posXYdiff12-\>Add(posXYr2,1);

TH2F \*posXYdiff23 = new TH2F(“posXYdiff23”, “The distribution of neutrons escaped from the target geometry (XY), r12+3”, 401, -200.5, 200.5, 401, -200.5, 200.5);  
EverythingN-\>Draw(“NPosY:NPosX\>\>posXYdiff23”, “reasonID==3”,“COLZ1”);  
posXYdiff23-\>Add(posXYdiff12,1);

c6-\>cd(2);  
TH2F \*posXYdiffr0 = new TH2F(“posXYdiffr0”, “The distribution of neutrons r-1 - (r1+r2+r3) (XY)”, 401, -200.5, 200.5, 401, -200.5, 200.5);  
EverythingN-\>Draw(“NPosY:NPosX\>\>posXYdiffr0”, “reasonID==-1”,“COLZ1”);  
posXYdiffr0-\>Add(posXYdiff23,-1);

Up to the point where cd(1), the plot is correct. However, when I did the subtraction, the number of entries are not matched up. If it is subtracting from posXYdiffr0, the number of entries showing in stat, should be 6050816.  
I believe that the subtraction is not taking from the addition of reason id 1, and 3.

I really appreciate if you can help.

Thank you

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 15, 2018, 2:55pm UTC](https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114/2 "2018-10-15T14:55:03Z")

</div>

I guess you could be interested in the [TH1:Integral](https://root.cern.ch/root/html534/TH1.html#TH1:Integral@1) / [TH2::Integral](https://root.cern.ch/root/html534/TH2.html#TH2:Integral@1) / [TH3::Integral](https://root.cern.ch/root/html534/TH3.html#TH3:Integral@1) and / or the [TH1::IntegralAndError](https://root.cern.ch/root/html534/TH1.html#TH1:IntegralAndError) / [TH2::IntegralAndError](https://root.cern.ch/root/html534/TH2.html#TH2:IntegralAndError) / [TH3::IntegralAndError](https://root.cern.ch/root/html534/TH3.html#TH3:IntegralAndError) methods.

When you “subtract” histograms, the “number of entries” is recalculated in a different way than when you “add” them so, you may get “unexpected” / “weird” values.

```auto
{
  TH1F *h1 = new TH1F("h1", "h1", 100, -5., 5.);
  h1->Sumw2(kTRUE);
#if 0 /* 0 or 1 */
  h1->FillRandom("gaus");
#else /* 0 or 1 */
  for (Int_t i = 0; i < 5000; i++) h1->Fill(gRandom->Gaus(), gRandom->Rndm());
#endif /* 0 or 1 */
  std::cout << h1->GetEntries() << std::endl;
  std::cout << h1->GetEffectiveEntries() << std::endl;
  TH1F *h2 = ((TH1F*)(h1->Clone("h2")));
  std::cout << h2->GetEntries() << std::endl;
  std::cout << h2->GetEffectiveEntries() << std::endl;
  h2->Add(h1, 1.0);
  std::cout << h2->GetEntries() << std::endl;
  std::cout << h2->GetEffectiveEntries() << std::endl;
  h2->Add(h1, -1.0);
  std::cout << h2->GetEntries() << std::endl;
  std::cout << h2->GetEffectiveEntries() << std::endl;
  std::cout << (h2->Integral() - h1->Integral()) << std::endl;
  gStyle->SetOptStat("nemruoi");
  TCanvas *c = new TCanvas("c", "c");
  h2->SetTitle("#color[2]{h2} = #color[3]{h1} + #color[3]{h1} - #color[3]{h1}");
  h2->SetLineColor(2); h1->SetLineColor(3);
  h2->Draw("E"); h1->Draw("SAME");
}

```

---

<div class="post-metadata">

**Author:** ![sclee0620](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sclee0620](https://root-forum.cern.ch/u/sclee0620)\
**Post date:** [October 15, 2018, 3:12pm UTC](https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114/3 "2018-10-15T15:12:06Z")

</div>

Thank you I ran the code you put it as an example.

I understand what you meant.

Hence, if I want to plot the subtracted histogram, am I suppose to set the entries?  
like  
posXYdiffr0-\>SetEntries(entries from h2 - entries from h1); ?

---

<div class="post-metadata">

**Author:** ![sclee0620](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sclee0620](https://root-forum.cern.ch/u/sclee0620)\
**Post date:** [October 16, 2018, 7:57am UTC](https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114/4 "2018-10-16T07:57:20Z")

</div>

Hello I have changed the way to plot the subtraction.

```auto
  // Open file filled by Geant4 simulation
  TFile* f = new TFile("totalN.root","READ");
  TTree *EverythingN;
  f->GetObject("EverythingN", EverythingN);

c6->cd(1);
TH2F *posXYdiff12 = new TH2F("posXYdiff12", "The distribution of neutrons escaped from the target geometry (XY), r1+2", 401, -200.5, 200.5, 401, -200.5, 200.5);
EverythingN->Draw("NPosY:NPosX>>posXYdiff12", "reasonID==1","COLZ1");
posXYdiff12->Add(posXYr2,1);

TH2F *posXYdiff23 = new TH2F("posXYdiff23", "The distribution of neutrons escaped from the target geometry (XY), r12+3", 401, -200.5, 200.5, 401, -200.5, 200.5);
EverythingN->Draw("NPosY:NPosX>>posXYdiff23", "reasonID==3","COLZ1");
posXYdiff23->Add(posXYdiff12,1);
posXYdiff23->Draw("COLZ1");

c6->cd(2);
TH2F *posXYdiffr0 = new TH2F("posXYdiffr0", "The distribution of neutrons r-1 - (r1+r2+r3) (XY)", 401, -200.5, 200.5, 401, -200.5, 200.5);
EverythingN->Draw("NPosY:NPosX>>posXYdiffr0", "reasonID==-1","COLZ1");

TH2F *h_out = (TH2F*)posXYdiffr0->Clone("");
int nx = h_out->GetXaxis()->GetNbins();
int ny = h_out->GetYaxis()->GetNbins();
for (int i=1;i<=nx;i++) {
for (int j=1;j<=ny;j++) {
double c1 =posXYdiffr0->GetBinContent(i,j);
double c2 =posXYdiff23->GetBinContent(i,j);
h_out->SetBinContent(i,j,c1-c2);
h_out->Draw("COLZ1");
}
}
}

```

when I do this, I have this plot.

 ![addedall](https://root-forum.cern.ch/uploads/default/original/3X/5/1/517ffbf8dbf9b7bd252e616862943fb66278dfb1.jpeg)  
Basically I do not understand why the statistic is off. The number of entries on the graph right side should be 6050816 but it is no where close to that.

Please help me

Thank you

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 16, 2018, 9:13am UTC](https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114/5 "2018-10-16T09:13:59Z")

</div>

Can you provide an example we can run ? the file `totalN.root` is not available.

---

<div class="post-metadata">

**Author:** ![sclee0620](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sclee0620](https://root-forum.cern.ch/u/sclee0620)\
**Post date:** [October 16, 2018, 9:48am UTC](https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114/6 "2018-10-16T09:48:22Z")

</div>

Yes sure the file is too large to post in here so I will share you the cernbox link:

[https://cernbox.cern.ch/index.php/s/8eBgsIbkJ8Dfe1E](https://cernbox.cern.ch/index.php/s/8eBgsIbkJ8Dfe1E)

and here is the root script I was working on

[plotNtuple\_diffN.C](https://root-forum.cern.ch/uploads/default/original/3X/f/e/fe55d3a58db18a29cf60728c0741fc0fda55de75.C) (14.0 KB)

Thank you so much

---

<div class="post-metadata">

**Author:** ![sclee0620](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sclee0620](https://root-forum.cern.ch/u/sclee0620)\
**Post date:** [October 16, 2018, 10:43am UTC](https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114/7 "2018-10-16T10:43:01Z")

</div>

I have also removed the Draw option “COLZ1”.

When I did that , I have everything blacked out like this

 ![problem](https://root-forum.cern.ch/uploads/default/original/3X/4/0/40a3b99b235864002b91dded06a51f74e10a7bea.jpeg)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 16, 2018, 11:51am UTC](https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114/8 "2018-10-16T11:51:11Z")

</div>

I ran your macro and do not get any black pad. What should I do to reproduce the problem ?

 ![25](https://root-forum.cern.ch/uploads/default/original/3X/0/a/0a94261972a75b678f3339a6293e410fd14e8217.jpeg)

---

<div class="post-metadata">

**Author:** ![sclee0620](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sclee0620](https://root-forum.cern.ch/u/sclee0620)\
**Post date:** [October 16, 2018, 11:53am UTC](https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114/9 "2018-10-16T11:53:10Z")

</div>

The problem is when you look at the c6 canvas.  
If you look the plot like posXYdiffr0 , the problem is that the number of entries are not matching with what it suppose to be.

Secondarly, the blackout only creates when you remove COLZ1 draw option.
