Skip to content

THnSparse::CreateSparse creates histograms that are not sparse #19366

Open
@Zehvogel

Description

@Zehvogel

Check duplicate issues.

  • Checked for duplicates

Description

According to the documentation "THnSparse only allocates memory for the bins that have non-zero bin content". Therefore, I expected to obtain a THnSparse that is sparse when calling THnSparse::CreateSparse on a sparsely filled THn, i.e. that the new "sparse" histogram does not contain any entries for the empty bins in the original histogram. However, when creating a "sparse" histogram this way, also the empty bins are filled, losing every benefit of the sparseness.

Reproducer

from array import array
# not the most python friendly constructor
hn = ROOT.THnD("", "", 1, array("i", [2]), [[0., 1., 2.]])
# fill a bin
hn.Fill(0.5)
print(hn.GetBinContent(1))
# 1
print(hn.GetBinContent(2))
# 0
# convert to sparse
hn_sparse = ROOT.THnSparseD.CreateSparse("", "", hn)
print(hn_sparse.GetNbins())
# 4 (2 normal + 2 flow)
print(hn_sparse.GetSparseFractionBins())
# 1.0 -> the "sparse" histogram is 100% dense :(

ROOT version

   ------------------------------------------------------------------
  | Welcome to ROOT 6.36.000                       https://root.cern |
  | (c) 1995-2025, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Jun 07 2025, 14:20:17                 |
  | From tags/6-36-000@6-36-000                                      |
  | With g++ (Spack GCC) 14.2.0                                      |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

Installation method

source /cvmfs/sw-nightlies.hsf.org/key4hep/setup.sh

Operating system

Alma9

Additional context

I suspect the issue to be in the kTRUE here in the Add called eventually by THnBase::CreateHnAny, but just removing it will probably break something else ;)

mybinidx = GetBin(coord, kTRUE /*allocate*/);

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions