# 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:** 1\
**Showing post:** 2

<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");
}

```

---

_[View the full topic](https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114)._
