root icon indicating copy to clipboard operation
root copied to clipboard

THnSparse::Add() does not preserve weights

Open kpedro88 opened this issue 2 years ago • 2 comments

Describe the bug

When adding two histograms, the weights associated with the bin contents of those histograms should also be added appropriately. THnSparse does not do this. It still reports the weights of the first histogram, ignoring the second.

Expected behavior

The sum of weights should include the weights of both histograms that were added together.

To Reproduce

The script is here: https://www.dropbox.com/s/j5wvixrmdtdx3b5/testTHNadd.C?dl=0 (GitHub apparently does not allow attaching .C files)

root -b -l -q testTHNadd.C

This script shows the bug by comparing two cases:

  1. Filling one histogram twice
  2. Filling two histograms once and adding them

The output is:

THnSparseT<TArrayD> (*0x6c127c0): "hs" "CASE 1"
  8 dimensions, 2 entries in 2 filled bins
    axis 0 "": 10 bins (-5..10), variable bin sizes
    axis 1 "": 10 bins (-10..70), variable bin sizes
    axis 2 "": 5 bins (-1000..3000), variable bin sizes
    axis 3 "": 30 bins (-3..3), variable bin sizes
    axis 4 "": 10 bins (0..5), variable bin sizes
    axis 5 "": 4 bins (0..2), variable bin sizes
    axis 6 "": 18 bins (0..2), variable bin sizes
    axis 7 "": 12 bins (0..5), variable bin sizes
  with error calculation
    Sum(w)=2, Sum(w^2)=2.5
    axis 0: Sum(w*x)=3.5, Sum(w*x^2)=6.5
    axis 1: Sum(w*x)=3.5, Sum(w*x^2)=6.5
    axis 2: Sum(w*x)=3.5, Sum(w*x^2)=6.5
    axis 3: Sum(w*x)=3.5, Sum(w*x^2)=6.5
    axis 4: Sum(w*x)=3.5, Sum(w*x^2)=6.5
    axis 5: Sum(w*x)=3.5, Sum(w*x^2)=6.5
    axis 6: Sum(w*x)=3.5, Sum(w*x^2)=6.5
    axis 7: Sum(w*x)=3.5, Sum(w*x^2)=6.5
  coordinates stored in 1 chunks of 16384 entries
    3.07559e-09 of bins filled using 6.61576e-05 of memory compared to an array
Bin at (5,2,2,21,3,3,10,3) = 0.5 (+/- 0.5)
Bin at (5,2,2,26,5,5,19,5) = 1.5 (+/- 1.5)
2
THnSparseT<TArrayD> (*0x57c3b10): "hs" "CASE 2"
  8 dimensions, 2 entries in 2 filled bins
    axis 0 "": 10 bins (-5..10), variable bin sizes
    axis 1 "": 10 bins (-10..70), variable bin sizes
    axis 2 "": 5 bins (-1000..3000), variable bin sizes
    axis 3 "": 30 bins (-3..3), variable bin sizes
    axis 4 "": 10 bins (0..5), variable bin sizes
    axis 5 "": 4 bins (0..2), variable bin sizes
    axis 6 "": 18 bins (0..2), variable bin sizes
    axis 7 "": 12 bins (0..5), variable bin sizes
  with error calculation
    Sum(w)=0.5, Sum(w^2)=0.25
    axis 0: Sum(w*x)=0.5, Sum(w*x^2)=0.5
    axis 1: Sum(w*x)=0.5, Sum(w*x^2)=0.5
    axis 2: Sum(w*x)=0.5, Sum(w*x^2)=0.5
    axis 3: Sum(w*x)=0.5, Sum(w*x^2)=0.5
    axis 4: Sum(w*x)=0.5, Sum(w*x^2)=0.5
    axis 5: Sum(w*x)=0.5, Sum(w*x^2)=0.5
    axis 6: Sum(w*x)=0.5, Sum(w*x^2)=0.5
    axis 7: Sum(w*x)=0.5, Sum(w*x^2)=0.5
  coordinates stored in 1 chunks of 16384 entries
    3.07559e-09 of bins filled using 6.61576e-05 of memory compared to an array
Bin at (5,2,2,21,3,3,10,3) = 0.5 (+/- 0.5)
Bin at (5,2,2,26,5,5,19,5) = 1.5 (+/- 1.5)
0.5

Setup

  1. ROOT versions: tested 6.06.00, 6.12.07, 6.24.07 (all have the bug)
  2. Scientific Linux 7.9, AlmaLinux 8.6
  3. From CMSSW on cvmfs

kpedro88 avatar Sep 08 '22 18:09 kpedro88

@lmoneta in CMS we are using THnSparse for a fairly large-scale analysis, so any suggestions of viable fixes or workarounds that we can easily implement would be very appreciated!

kpedro88 avatar Sep 13 '22 19:09 kpedro88

Thank you to report this problem and the test to reproduce. The problem is to be not in the bin content and error of the resulting histograms but in the sum of all bin contents and errors, e.g. h->GetWeightSum(). I will provide soon a PR fixing this

lmoneta avatar Sep 21 '22 12:09 lmoneta

Hi @lmoneta,

It appears this issue is closed, but wasn't yet added to a project. Please add upcoming versions that will include the fix, or 'not applicable' otherwise.

Sincerely, :robot:

github-actions[bot] avatar Sep 23 '22 06:09 github-actions[bot]

Hi @lmoneta,

It appears this issue is closed, but wasn't yet added to a project. Please add upcoming versions that will include the fix, or 'not applicable' otherwise.

Sincerely, :robot:

github-actions[bot] avatar Sep 26 '22 06:09 github-actions[bot]

Hi @lmoneta,

It appears this issue is closed, but wasn't yet added to a project. Please add upcoming versions that will include the fix, or 'not applicable' otherwise.

Sincerely, :robot:

github-actions[bot] avatar Sep 27 '22 06:09 github-actions[bot]

Hi @lmoneta,

It appears this issue is closed, but wasn't yet added to a project. Please add upcoming versions that will include the fix, or 'not applicable' otherwise.

Sincerely, :robot:

github-actions[bot] avatar Sep 28 '22 06:09 github-actions[bot]