# What is the error propagation formula of TH1 when using SetDefaultSumw2()?

**URL:** <https://root-forum.cern.ch/t/what-is-the-error-propagation-formula-of-th1-when-using-setdefaultsumw2/55467>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [June 28, 2023, 1:41pm UTC](https://root-forum.cern.ch/t/what-is-the-error-propagation-formula-of-th1-when-using-setdefaultsumw2/55467 "2023-06-28T13:41:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrli](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@mrli](https://root-forum.cern.ch/u/mrli)\
**Post date:** [June 28, 2023, 1:41pm UTC](https://root-forum.cern.ch/t/what-is-the-error-propagation-formula-of-th1-when-using-setdefaultsumw2/55467/1 "2023-06-28T13:41:57Z")

</div>

Dear ROOT users,  
I was using 2 TH1F histograms h\_m and h\_n to compute efficiency: h\_\varepsilon = h\_{m}/h\_{n}:

```python
TH1.SetDefaultSumw2()
# ... fill h_m and h_n
h_eff = TH1F(h_m)
h_eff.Divide(h_n)

```

According to the references it seems ROOT can handle errors properly by calling `TH1::SetDefaultSumw2()`. But when I checked the errors I found it is not equal to the value calculated by error propagation formula:

\begin{align} \varepsilon\_i &= \frac{m\_i}{n\_i}\space (i=1,2,3,...,N\_{bin}) \\ \sigma\_{i} &= \sqrt{ \left(\frac{\partial\varepsilon\_i}{\partial m\_i}\right)^2 \sigma\_{m\_i}^2 + \left(\frac{\partial\varepsilon\_i}{\partial n\_i}\right)^2 \sigma\_{n\_i}^2 } \\ &=\sqrt{\varepsilon\_i^2 \left( \frac{\sigma\_{m\_i}^2}{m\_i^2} + \frac{\sigma\_{n\_i}^2}{n\_i^2} \right)} \\ &=\sqrt{\varepsilon\_i^2 \left( \frac{1}{m\_i} + \frac{1}{n\_i} \right)} \ \ \ \ \ \ \text{since} \ \sigma\_{m(n)}^2=m(n) \\ &=\sqrt{\frac{m\_i(m\_i+n\_i)}{n\_i^3}} \end{align}

where m\_i and n\_i are obtained by `GetBinContent(i)`, \sigma\_{m\_i} and \sigma\_{n\_i} are obtained by `GetBinError(i)` of `h_m` or `h_n`.

For example, for one bin, if m=10,n=34 (omit subscript i), `GetBinError()` will return their bin error: \sigma\_m=\sqrt{10}=3.16,\sigma\_n=\sqrt{34}=5.83

then `h_eff.GetBinError()` will return efficiency error: \sigma\_{\mathrm{sumw2}}=0.25  
while error propagation formula gives \sigma\_\mathrm{propa}=0.11

I checked source codes of `TH1::Divide`, that formula seems the same as the error propagation formula I posted above.

Could anyone tell me how `TH1` calculates errors under `SetDefaultSumw2(kTrue)` or what mistake I made?

Best wishes!

_ROOT Version:_ 6.26/10  
_Platform:_ Linux

* * *

---

<div class="post-metadata">

**Author:** ![mczurylo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mczurylo/32/33269_2.png) [@mczurylo](https://root-forum.cern.ch/u/mczurylo)\
**Post date:** [June 29, 2023, 12:18pm UTC](https://root-forum.cern.ch/t/what-is-the-error-propagation-formula-of-th1-when-using-setdefaultsumw2/55467/2 "2023-06-29T12:18:02Z")

</div>

Hi @mrli,

welcome back to the forum. Maybe @moneta could take a look?

Cheers,  
Marta

---

<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:** [June 29, 2023, 4:40pm UTC](https://root-forum.cern.ch/t/what-is-the-error-propagation-formula-of-th1-when-using-setdefaultsumw2/55467/3 "2023-06-29T16:40:53Z")

</div>

Hi,  
`TH1::Divide` use this error propagation you have above. If you use the option 'B` assumes a binomial distribution.

in your case, I cannot reproduce your problem. This gives me the correct result (sigma = 0.105805)

```auto
   TH1::SetDefaultSumw2(true);
   TH1D h1("h1","h1",1,0,1);
   TH1D h2("h2","h2",1,0,1);
   h1.SetBinContent(1,10);
   h2.SetBinContent(1,34);
   // need to set the bin error if you have default sumw2
   h1.SetBinError(1,sqrt(10.));
   h2.SetBinError(1,sqrt(34.));
   TH1D h3 = h1;
   h3.Divide(&h2);
   std::cout << h3.GetBinError(1) << std::endl;

```

please share the full code, so I can see the error

Lorenzo

---

<div class="post-metadata">

**Author:** ![mrli](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@mrli](https://root-forum.cern.ch/u/mrli)\
**Post date:** [June 29, 2023, 5:31pm UTC](https://root-forum.cern.ch/t/what-is-the-error-propagation-formula-of-th1-when-using-setdefaultsumw2/55467/4 "2023-06-29T17:31:39Z")

</div>

I know how the problem occures!

I am using JupyROOT, the division cell is like:

```python
h3 = ROOT.TH1F(h1)
h3.Divide(h2)

```

When the first time I ran that notebook, I forgot to call `ROOT.RH1.SetDefaultSumw2(True)`, so I created and ran a new cell with `ROOT.RH1.SetDefaultSumw2(True)`. After that I ran the division cell again, thinking the `ROOT.TH1F` may create a new instance of `h1` with correct error which actually did not.

Sorry for my carelessness and thanks for help!

Best wishes
