PyROOT, numpy matrices and TSpectrum2::SearchHighRes

Hello,

I’ve been trying to locate the peaks of a 2-d distribution using the TSpectrum2 class, but I just keep getting segfault errors (see attachment).

I guess the problem is the signature of the TSpectrum2::SearchHighRes function I am trying to use:

I am trying to use two numpy matrices as source and dest, as shown in the example attached.
May anybody suggest me a workaround, or a different array/vector/list/matrix data type compatible with this signature? I have also tried to use ROOT’s vector<vector> but I didn’t manage to get SearchHighRes working this way either.

The attached files provide:
[ul][li]a minimal (not) working example spectrum.py (380 Bytes)[/li]
[li]a rootfile containing the source histogram hh.root (65.3 KB)[/li]
[li]and the complete error stack trace spectrum-errors.txt (6.03 KB)[/li][/ul]

Cheers,

Elena

Elena,

you don’t want to use this …

Here’s what you’re up against: numpy does not support C-style 2D arrays particularly well, float in python is double in C, and in C there are float** and float[][] and they are not the same.

Now, if you insist, you can beat this into shape, but to make it even semi-usable, it would probably have to go something like this:[code]import array

binsx = 1800
binsy = 1000
source, c_source = {}, array.array( ‘L’, [0]*binsx )
dest, c_dest = {}, array.array( ‘L’, [0]*binsx )
def new_numpy1d_with_pointer(size):
np_a = np.zeros( size, dtype=np.float32 )
pointer, read_only_flag = np_a.array_interface[‘data’]
return np_a, pointer

for i in xrange(binsx):
source[i], c_source[i] = new_numpy1d_with_pointer( binsy )
dest[i], c_dest[i] = new_numpy1d_with_pointer( binsy )[/code]
This builds an array of pointers to arrays of C floats in c_source and c_dest, and that will work, as although PyROOT does not know about float**, it passes the array pointer as void*, which is fine. It also builds source and dest to a) keep the arrays alive from the reference counting, and b) still be usable in the expected way.

Cheers,
Wim

Dear Wim,

Ok, I understand the problem with pyroot and C arrays.
However, for my current purpose, your code efficiently solved my problem. Thank you very much!

Elena