# Adding multiple histograms preserving poisson statistical error

**URL:** <https://root-forum.cern.ch/t/adding-multiple-histograms-preserving-poisson-statistical-error/57796>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [January 10, 2024, 5:46pm UTC](https://root-forum.cern.ch/t/adding-multiple-histograms-preserving-poisson-statistical-error/57796 "2024-01-10T17:46:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![elemental](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/elemental/32/34841_2.png) [@elemental](https://root-forum.cern.ch/u/elemental)\
**Post date:** [January 10, 2024, 5:46pm UTC](https://root-forum.cern.ch/t/adding-multiple-histograms-preserving-poisson-statistical-error/57796/1 "2024-01-10T17:46:23Z")

</div>

I would like to obtain by adding and subtracting histograms using the `Add()` function in the `TH1` class. The operation is as follows:  
hResult = h5 - h2 - h8 - h4 - h6 + h1 + h3 + h7 + h9

where h\*\* are all histogram objects with same bin size. Could you suggest a macro on how to do this operation while preserving the poisson error from all the histograms in the resultant histogram `hResult`.

* * *

\_ROOT Version:6.28/06  
\_Platform: macOS  
\_Compiler: cling 0.9

* * *

---

<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:** [January 11, 2024, 8:38am UTC](https://root-forum.cern.ch/t/adding-multiple-histograms-preserving-poisson-statistical-error/57796/2 "2024-01-11T08:38:14Z")

</div>

> [@elemental](#):
>
> Could you suggest a macro on how to do this operation

Have you tried to simply do the 9 `Add()` in sequence ?

---

<div class="post-metadata">

**Author:** ![elemental](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/elemental/32/34841_2.png) [@elemental](https://root-forum.cern.ch/u/elemental)\
**Post date:** [January 11, 2024, 11:20am UTC](https://root-forum.cern.ch/t/adding-multiple-histograms-preserving-poisson-statistical-error/57796/3 "2024-01-11T11:20:52Z")

</div>

I’ve tried that, Will it then preserve the poisson error from the individual components ?

---

<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:** [January 11, 2024, 1:22pm UTC](https://root-forum.cern.ch/t/adding-multiple-histograms-preserving-poisson-statistical-error/57796/4 "2024-01-11T13:22:15Z")

</div>

> [@elemental](#):
>
> Will it then preserve the poisson error from the individual components ?

@moneta will know

---

<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:** [January 11, 2024, 1:56pm UTC](https://root-forum.cern.ch/t/adding-multiple-histograms-preserving-poisson-statistical-error/57796/5 "2024-01-11T13:56:19Z")

</div>

Hi,

The Poisson error is computed from the existing histogram counts. If you add histograms and all contains counts, (i.e. are not weighted histograms), you will be able to compute the Poisson errors on the resulting summed histogram.

Lorenzo

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [January 12, 2024, 8:12am UTC](https://root-forum.cern.ch/t/adding-multiple-histograms-preserving-poisson-statistical-error/57796/6 "2024-01-12T08:12:28Z")

</div>

But he wants to substract one histogram from another, so even if it is integer counts, it might not work. I think then the only solution would be to keep track separately of the error using an auxiliary histogram where he sums all instead of subtract, and take that one as the error.

---

<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:** [January 12, 2024, 9:16am UTC](https://root-forum.cern.ch/t/adding-multiple-histograms-preserving-poisson-statistical-error/57796/7 "2024-01-12T09:16:07Z")

</div>

Sorry, I have missed the subtraction of histograms in the formula above. If you subtract histograms, then the resulting bin distribution is not anymore a Poisson distribution, it follows the [Skellam distribution](https://en.wikipedia.org/wiki/Skellam_distribution), so it does not make sense to compute afterwards Poisson statistical error. You can use in this case the normal approximation.  
If you call `TH1::Sumw2()` on the input histograms, the resulting bin error after `TH1::Add` will be computed correctly (from the square root of the sum of the content instead of the subtracted one).

Lorenzo

---

<div class="post-metadata">

**Author:** ![elemental](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/elemental/32/34841_2.png) [@elemental](https://root-forum.cern.ch/u/elemental)\
**Post date:** [January 23, 2024, 2:44pm UTC](https://root-forum.cern.ch/t/adding-multiple-histograms-preserving-poisson-statistical-error/57796/8 "2024-01-23T14:44:24Z")

</div>

Hi @moneta,

You are right, I have subtractions in the operation. Could you please elaborate on how to use `TH1::Sumw2()` and `TH1::Add()` in this case.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [January 24, 2024, 8:13am UTC](https://root-forum.cern.ch/t/adding-multiple-histograms-preserving-poisson-statistical-error/57796/9 "2024-01-24T08:13:00Z")

</div>

```auto
h1->Sumw2();
h2->Sumw2();
h1->Add(h2, -1);

```
