Reading from THnSparse

Dear experts,

I’m trying to use a THnSparse as an N dimensional map for storing scale-factors.
I would like, given a set of values per axis, to obtain the bin content at the relevant bin. Something similar to h.GetBinContent(bx,by,bz) for a TH3 hist.

The example shown in the class reference is doing the opposite of what I’m looking for: when iterating on bins it returns the axis coordinates.

( std::cout << hs.GetBinContent(0, coord); std::cout <<" is the content of bin [x = " << coord[0] " << " | y = " << coord[1] << “]” << std::endl; )

I’m able to get the bins per axis using eg h.GetAxis(0).FindBin(15), how can I translate those to the linearized bin idx to get the content value at said bin, can anyone advise?

Below is a summary of my failed attempts.
I also tried using THnF without success. If there are better methods to do this I’d also be happy to hear about them.

Best,
Mattias

>>>h
<ROOT.THnSparseT object (“Z_ee_eff_ee”) at 0x7fc6cc1058b0>
>>> h.GetNbins()
12606L
>>> varrs=array(‘d’, [15782.5, 18558.0, 2.03285, 1.6331, 1.0])
>>> h.GetBin(varrs,0)
12606L
>>> h.GetAxis(0).FindBin(15782.5)
4
>>> h.GetAxis(1).FindBin(18558.0)
4
>>> h.GetAxis(2).FindBin(2.03285)
19
>>> h.GetAxis(3).FindBin(1.6331)
17
>>> h.GetAxis(4).FindBin(1.0)
2
>>> bins=array(‘i’,[4,4,19,17,2])
>>> h.GetBinContent(bins)
0.0
>>> h.GetBin(bins)
12606L
>>> bins=array(‘d’,[4,4,19,17,2])
>>> h.GetBin(bins)
12607L

@couet can you help please here?

Thank you in advance,
Oksana.

@couet
Any news on this?
Thanks in advance,
Mattias

I think this question is more for @moneta

how about this: (Note: GetBin(xx)
sparse_exa.C (616 Bytes)
Otto

Note for maintainer of THnSparse:
there is a typo here:
THnSparse hs(“hs”, “hs”, 2, bins, min, max);
should be:
THnSparse hs(“hs”, “hs”, 2, bins, xmin, xmax);

Hi Otto,

Fixed,
Thanks

Hi,
You’re right, the GetBin(varrs) works. I was probably looking for an empty bin by mistake, so it was returning the last (empty) bin.
Thanks for the help!
Mattias