THnSparse::SetBinContent - segmentation violation


There are 2 overloaded methods to set bin content in THnSparse.

  1. By the bin coordinates:
  1. And by the global bin index:

When I am trying to call the second method in a newly created THnSparseF object, I get segmentation violation.

Here is a simple example to reproduce the problem:

int bins[3] = {1, 1, 1};
THnSparseF h(“name”, “title”, 3, bins);

// This method works:
// int v[3] = {0, 0, 0};
// h.SetBinContent(v, 1.23);

// This method causes segmentation violation.
h.SetBinContent(0, 2.23);
My ROOT version is 5.30/01.


Because the bin has not been allocated yet. The bin-index overload only makes sense if THnSparse told you that this is the bin index to use given your coordinates, i.e. as a return value from GetBin(). If you call GetBin() with allocate == true it will create that bin (and set its coordinates).

Remember that THnSparse uses sparse storage, i.e. even the first bin has no meaning until its coordinates are initialized.

Cheers, Axel.