# Relative error of the ratio

**URL:** <https://root-forum.cern.ch/t/relative-error-of-the-ratio/51317>\
**Category:** Newbie\
**Created:** [August 24, 2022, 1:29pm UTC](https://root-forum.cern.ch/t/relative-error-of-the-ratio/51317 "2022-08-24T13:29:21Z")\
**Posts on this page:** 8\
**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:** [August 24, 2022, 1:29pm UTC](https://root-forum.cern.ch/t/relative-error-of-the-ratio/51317/1 "2022-08-24T13:29:21Z")

</div>

Hello Rooters,

I have two histograms called h\_simu and h\_gen(2D hist). Applied SumW2 option before filling the histograms.Then I took the ratio of these two histograms.  
h\_ratio= h\_simu/h\_gen

I need to calculate the “relative error of this ratio histogram”.  
Could you please check if my way is correct?

```auto
h_ratio_error=(TH2F *)h_ratio->Clone("h_ratio_error");
for(int i=0; i < h_ratio->GetSize();i++){
                h_ratio_error>GetArray()[i]= h_ratio_error->GetBinError(i)/(h_ratio_error->GetBinContent(i)+0.1);
                h_ratio_error->Sumw2(kFALSE);
                h_ratio_error->ResetStats();
                h_ratio_error->SetTitle("Relative Error of the ratio histogram CsHS vs mpipi");
                h_ratio_error->Draw("COLZ ");
                
   }

```

---

<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:** [August 24, 2022, 1:42pm UTC](https://root-forum.cern.ch/t/relative-error-of-the-ratio/51317/2 "2022-08-24T13:42:43Z")

</div>

For 1-D histograms:  
[TRatioPlot](https://root.cern/doc/master/classTRatioPlot.html)  
[`${ROOTSYS}/tutorials/hist/ratioplot*`](https://root.cern/doc/master/group __tutorial__ hist.html)

[I’m afraid there is no “ready-to-use” tool for 2-D cases.](https://root-forum.cern.ch/t/2d-fitting-using-tgraph2derrors/46012/4)

---

<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:** [August 24, 2022, 1:50pm UTC](https://root-forum.cern.ch/t/relative-error-of-the-ratio/51317/3 "2022-08-24T13:50:09Z")

</div>

Thank you.  
What about my calculation?  
The reason I was worring my calculation is, my errors are too big.  
When I commented out `h_ratio_error->Sumw2(kFALSE);`  
I got reasonable errors.  
Need to know my calculation is okay without `h_ratio_error->Sumw2(kFALSE);`  
I already applied sumw2 options for the h\_simu and h\_gen histograms.

Thanks  
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:** [August 24, 2022, 2:30pm UTC](https://root-forum.cern.ch/t/relative-error-of-the-ratio/51317/4 "2022-08-24T14:30:53Z")

</div>

If histograms have errors defined (see [`TH1::Sumw2`](https://root.cern/doc/master/classTH1.html#aefa4ee94f053ec3d217f3223b01fa014)) and you used:  
[`TH1::Divide(const TH1 *h1)`](https://root.cern/doc/master/classTH1.html#ac782a09c31b4f7de40f8fd4f77efa090)  
or:  
[`TH1::Divide(const TH1 *h1, const TH1 *h2, Double_t c1, Double_t c2, Option_t *option)`](https://root.cern/doc/master/classTH1.html#a4ef2329285beb6091515b836e160bd9f)  
then the errors should be “properly” taken into account.

---

<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:** [August 24, 2022, 2:36pm UTC](https://root-forum.cern.ch/t/relative-error-of-the-ratio/51317/5 "2022-08-24T14:36:51Z")

</div>

Yes. I used that way.

```auto
//Applied sumw2 while filling the histogram
h_simu
h_gen

h_ratio=(TH2F *)h_simu->Clone();
 h_ratio->Divide(h_gen);
 h_ratio->Draw("COLZ TEXT45");

//relative error

h_ratio_error=(TH2F *)h_ratio->Clone("h_ratio_error");
for(int i=0; i < h_ratio->GetSize();i++){
                h_ratio_error>GetArray()[i]= h_ratio_error->GetBinError(i)/(h_ratio_error->GetBinContent(i)+0.1);
             // h_ratio_error->Sumw2(kFALSE);
                h_ratio_error->ResetStats();
                h_ratio_error->SetTitle("Relative Error of the ratio histogram CsHS vs mpipi");
                h_ratio_error->Draw("COLZ ");
                
   }

```

My question is `h_ratio_error->Sumw2(kFALSE);`  
Is it need or not?

With `h_ratio_error->Sumw2(kFALSE);`-Big errors  
Without `h_ratio_error->Sumw2(kFALSE);`-small errors

which is correct?

Thanks  
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:** [August 24, 2022, 2:43pm UTC](https://root-forum.cern.ch/t/relative-error-of-the-ratio/51317/6 "2022-08-24T14:43:53Z")

</div>

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

```

BTW. See also the older thread: “[Error calculation of 2D histogram](https://root-forum.cern.ch/t/error-calculation-of-2d-histogram/50712/3)”

---

<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:** [August 24, 2022, 3:08pm UTC](https://root-forum.cern.ch/t/relative-error-of-the-ratio/51317/7 "2022-08-24T15:08:41Z")

</div>

Thank You,@Wile_E_Coyote.  
This gave me nice errors but when I commented out `h_ratio_error->Sumw2(kFALSE);`.

---

<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:** [August 24, 2022, 3:11pm UTC](https://root-forum.cern.ch/t/relative-error-of-the-ratio/51317/8 "2022-08-24T15:11:24Z")

</div>

Bin errors of the “h\_ratio\_error” histogram are meaningless (after the content of its bins has been modified).
