# Error calculation of 2D histogram

**URL:** <https://root-forum.cern.ch/t/error-calculation-of-2d-histogram/50712>\
**Category:** Newbie\
**Created:** [July 7, 2022, 6:34pm UTC](https://root-forum.cern.ch/t/error-calculation-of-2d-histogram/50712 "2022-07-07T18:34:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [July 7, 2022, 6:34pm UTC](https://root-forum.cern.ch/t/error-calculation-of-2d-histogram/50712/1 "2022-07-07T18:34:04Z")

</div>

Hello Rooters,

I have two histograms called h\_QSq\_xBj and h\_QSq\_xBj\_MC.  
I applied Sumw2 option for these histograms.  
Then I took the division of these two histograms  
h\_ratio= h\_QSq\_xBj\_MC/h\_QSq\_xBj.

(not applied sumw2 option for this ratio histogram)

I need to copy the error of h\_ratio histogram to a another histogram.  
Could you please help me to draw it?  
I have no idea how to calculate error of h\_ratio and copy that errors to the another histogram.

```auto
{
//open 2D raw and simu histograms
    TFile *hFile=TFile::Open("xBQ2binning.root");
    TH2F *h_QSq_xBj; //added Sumw2() option
    TH2F *h_QSq_xBj_MC; //added Sumw2() option
    TH2F *h_ratio;
    hFile->GetObject("h_QSq_xBj", h_QSq_xBj);
    hFile->GetObject("h_QSq_xBj_MC", h_QSq_xBj_MC);
    
    h_ratio = (TH2F*)h_QSq_xBj_MC->Clone();
    h_ratio->GetXaxis()->SetTitle(" ");
    h_ratio->GetYaxis()->SetTitle(" ");
    h_ratio->SetTitle("h_QSq_xBj_MC(raw)/h_QSq_xBj(simu)");
    h_ratio->Divide(h_QSq_xBj);

TCanvas* C_QSqxBj= new TCanvas();
    C_QSqxBj->Divide(3,1);
    C_QSqxBj->cd(1);
    h_QSq_xBj->Draw("colz");
    C_QSqxBj->cd(2);
    h_QSq_xBj_MC->Draw("colz");
    C_QSqxBj->cd(3);
    h_ratio->Draw("colz");

}

```

Thank You  
Dil

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 7, 2022, 7:49pm UTC](https://root-forum.cern.ch/t/error-calculation-of-2d-histogram/50712/2 "2022-07-07T19:49:46Z")

</div>

I think @moneta can help

---

<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:** [July 8, 2022, 7:17am UTC](https://root-forum.cern.ch/t/error-calculation-of-2d-histogram/50712/3 "2022-07-08T07:17:33Z")

</div>

```cpp
  TH2F *h_ratio_errors = (TH2F*)h_ratio->Clone("h_ratio_errors");
  for (int i = 0; i < h_ratio_errors->GetSize(); i++)
    h_ratio_errors->GetArray()[i] = h_ratio_errors->GetBinError(i);
  h_ratio_errors->Sumw2(kFALSE);
  // h_ratio_errors->ResetStats();

```

---

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [July 8, 2022, 3:13pm UTC](https://root-forum.cern.ch/t/error-calculation-of-2d-histogram/50712/4 "2022-07-08T15:13:29Z")

</div>

Thank You, Coyote.  
This works.

---

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [July 8, 2022, 5:01pm UTC](https://root-forum.cern.ch/t/error-calculation-of-2d-histogram/50712/5 "2022-07-08T17:01:08Z")

</div>

Hello,  
This is a different question, but related to the same code,  
When I am doing histogram division what if I have empty bins?  
Is there any convenient way to get rid of those bins or return it to zero?

```auto
h_ratio = (TH2F*)h_QSq_xBj_MC->Clone();
    h_ratio->GetXaxis()->SetTitle(" ");
    h_ratio->GetYaxis()->SetTitle(" ");
    h_ratio->SetTitle("h_QSq_xBj_MC(raw)/h_QSq_xBj(simu)");
    h_ratio->Divide(h_QSq_xBj);

```

Thank You  
Dil

---

<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:** [July 8, 2022, 5:38pm UTC](https://root-forum.cern.ch/t/error-calculation-of-2d-histogram/50712/6 "2022-07-08T17:38:33Z")

</div>

[TH1::Divide](https://root.cern/doc/master/classTH1.html#ac782a09c31b4f7de40f8fd4f77efa090) automatically takes care of all cases.
