# Adding histograms changes number of entries in ways I don't understand, please help

**URL:** <https://root-forum.cern.ch/t/adding-histograms-changes-number-of-entries-in-ways-i-dont-understand-please-help/35487>\
**Category:** ROOT\
**Created:** [August 3, 2019, 9:14pm UTC](https://root-forum.cern.ch/t/adding-histograms-changes-number-of-entries-in-ways-i-dont-understand-please-help/35487 "2019-08-03T21:14:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frigorifico](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/frigorifico/32/19356_2.png) [@Frigorifico](https://root-forum.cern.ch/u/Frigorifico)\
**Post date:** [August 3, 2019, 9:14pm UTC](https://root-forum.cern.ch/t/adding-histograms-changes-number-of-entries-in-ways-i-dont-understand-please-help/35487/1 "2019-08-03T21:14:32Z")

</div>

* * *

\_ROOT Version: 6.19  
\_Platform: OS 10.14.3

* * *

I include all the files and code at the end.

I have 2 files of “raw histograms” of measurements taken under different conditions, but the amount of data points is exactly the same.

I have also some “noise histograms” which is the same for both.

I add the raw histograms and subtract the noise like this:

```auto
    hHist.Add(hNoise,-1.0*n)

```

where n is just a scale factor. If I took 20 rounds of measurements for the noise but 15 for the actual measurements then the noise gets normalized and multiplied by 15 to match the scale of the measurements.

Everything was working fine when I noticed that one of them had way better results, I thought it was just the measurement conditions until I noticed the number of entries, one has easily 10 times more than the other.

It was my understanding that if I have 2 histograms with 10 entries and you add them up you get one with 20, so I would expect both of them to have the same number of entries. However this is just the case sometimes.

To investigate this I decided to print the number of entries after each step in the process, this is what I got (“sumando” means adding up in spanish):

> First data set  
> sumando  
> 3647.0  
> 7294.0  
> 10941.0  
> 14588.0  
> 18235.0  
> 21882.0  
> 25529.0  
> 29176.0  
> 32823.0  
> 32823.0  
> noise entries  
> 32823.0  
> data entries  
> 32823.0  
> after noise subtraction  
> 290765.26725  
> Second data set  
> sumando  
> 3647.0  
> 7294.0  
> 10941.0  
> 14588.0  
> 18235.0  
> 21882.0  
> 25529.0  
> 29176.0  
> 32823.0  
> 32823.0  
> noise entries  
> 32823.0  
> data entries  
> 32823.0  
> after noise subtraction  
> 1301369.43743

As you can see after the noise is subtracted the amount of entries changes drastically, it even becomes fractionary. I do not understand this at all. I have been reading the documentation, but I don’t see anything about this.

What am i doing wrong?

[testtest 2.zip](https://root-forum.cern.ch/uploads/short-url/um2zLLPAJApSdJR8J0REgt6e8MK.zip) (1.5 MB)

---

<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 4, 2019, 6:40am UTC](https://root-forum.cern.ch/t/adding-histograms-changes-number-of-entries-in-ways-i-dont-understand-please-help/35487/2 "2019-08-04T06:40:51Z")

</div>

> [@Subtracting multiple histogram with same TH2F](https://root-forum.cern.ch/t/subtracting-multiple-histogram-with-same-th2f/31114/2):
>
> 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. { 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 \< …

> [@Subtracting histograms does not yield difference in entries](https://root-forum.cern.ch/t/subtracting-histograms-does-not-yield-difference-in-entries/31133):
>
> When I subtract two histograms, the number of entries in the subtracted histogram is not equal to the difference in the number of entries between the two histograms. It seems that the resulting histogram does not contain the contents of the underflow and overflow bins. This is not, however, the case for addition, which seems to preserve the sums just fine. Is this behavior expected? It is very unintuitive. Step-by-step demonstration: Get TFile and TTree and draw a branch into histograms (wit…

---

<div class="post-metadata">

**Author:** ![Frigorifico](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/frigorifico/32/19356_2.png) [@Frigorifico](https://root-forum.cern.ch/u/Frigorifico)\
**Post date:** [August 4, 2019, 1:05pm UTC](https://root-forum.cern.ch/t/adding-histograms-changes-number-of-entries-in-ways-i-dont-understand-please-help/35487/3 "2019-08-04T13:05:02Z")

</div>

thanks a lot, but I have a follow up question, in fact, Newbie made the same question but nobody answered it: am I supposed then to set the entries to what I know should be the correct value?, or should I leave it as it is?

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [August 18, 2019, 1:08pm UTC](https://root-forum.cern.ch/t/adding-histograms-changes-number-of-entries-in-ways-i-dont-understand-please-help/35487/4 "2019-08-18T13:08:40Z")

</div>

Closed on August 18, 2019, 1:08pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
