SearchHighRes working for 1-d but not for 2-d

Hello,

I tried to use TSpectrum2>>SearchHighRes from PyRoot in python, but it doesn’t work for me on Root 5.34/36 with python 2.7 nor on Root 6.22/02 with python 3.7.
On both systems, I get the following error:

RuntimeWarning: creating converter for unknown type “float**”

*** Break *** segmentation violation

I tried it in 1-d with TSpectrum >>SearchHighRes, and it works fine. First of all, I have created a Gauss peak with some noise in the background. Then, I created a TH1D/TH2D and filled it with the generated data. Using the GetBinContent gives me the histogram data which is used for the SearchHighRed function. A short Draw() method provides me a good plot.
Due to a grid search, I tried to find better parameters, but it did not work either, so I just copied those from 1-d.
I would like to prefer a solution for python 2.7 with Root 5.34/36.
You can find both scripts for python 2.7 for a 1-d and 2-d Gauss peak in the attachment.
It would be nice if someone could give me a hint or a solution to this problem and please tell me if you need more information.

Thank you

Peaks_1d.py (1.8 KB)
Peaks_2d.py (2.5 KB)

Welcome to the forum! I think @moneta can help you with this.

May be it is a problem in my side but when I run your scripts I get a different error message. Not unknown type float It seems there is a type error with argument 1 of SearchHighRes.

% python Peaks_1d.py 
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
Traceback (most recent call last):
  File "Peaks_1d.py", line 55, in <module>
    threshold, background, deconIterations, markov, averWindow)
TypeError: int TSpectrum::SearchHighRes(double* source, double* destVector, int ssize, double sigma, double threshold, bool backgroundRemove, int deconIterations, bool markov, int averWindow) =>
    TypeError: could not convert argument 1 (could not convert argument to buffer or nullptr)

% python Peaks_2d.py
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
Traceback (most recent call last):
  File "Peaks_2d.py", line 66, in <module>
    threshold, background, deconIterations, markov, averWindow)
TypeError: int TSpectrum2::SearchHighRes(double** source, double** dest, int ssizex, int ssizey, double sigma, double threshold, bool backgroundRemove, int deconIterations, bool markov, int averWindow) =>
    TypeError: could not convert argument 1 (could not convert argument to buffer or nullptr)

Hello couet,
that sounds strange. I know this problem because the documentation says that the input should be a pointer to “source” and “dest” but in my case, it works like this (in 1-d). See my output.

(‘npeaks’, 1)
('Peaks: ', [42.0])

May be that connected to the python interface ? have you tried in C++ ?

I worked out a solution such that I could find peaks, and everything works perfectly in Root. Now I need a solution with PyRoot. I am still not able to use TSpectrum2::SearchHighRes in PyRoot. Maybe the problem is that the function needs “float **source” and “float **dest” and I am not sure how to initialize them in python.
I would like to have the following code in python with PyRoot:

TCanvas *c1 = new TCanvas();
const Int_t bins = 500;
TH2D *hist = new TH2D("2d_Gauss", "Histogram", bins, 0, 100, bins, 0, 100);
hist -> SetStats(0);
TRandom *rand = new TRandom(10);
for(int i=0; i<500000; i++){float x = rand->Gaus(15, 2); float y = rand->Gaus(15, 2); hist -> Fill(x, y);}
for(int i=0; i<500000; i++){float x = rand->Gaus(80, 2); float y = rand->Gaus(80, 2); hist -> Fill(x, y);}
for(int i=0; i<500000; i++){float x = rand->Gaus(50, 2); float y = rand->Gaus(50, 2); hist -> Fill(x, y);}
Float_t** source = new Float_t*[bins];
Float_t** dest = new Float_t*[bins];
for(int i=0; i<bins; i++){source[i]=new Float_t[bins]; dest[i]=new Float_t[bins];}
for(int i=0; i<bins; i++){for(int j=0; j<bins; j++){source[i][j] = hist->GetBinContent(i+1,j+1);}}
TSpectrum2 *s = new TSpectrum2(5);
Int_t nfound = s->SearchHighRes(source, dest, bins, bins, 5, 50, false, 30, false, 3);
Float_t *PositionX = s->GetPositionX();
Float_t *PositionY = s->GetPositionY();
printf("Found %d peaks\n", nfound)
printf("BINS %i \n", bins);
for (int i=0; i<nfound; i++) {printf("posx%i= %f\n", i, PositionX[i]/500*100);}
for (int i=0; i<nfound; i++) {printf("posy%i= %f\n", i, PositionY[i]/500*100);}
hist->Draw("COL")

posx0= 49.800797
posx1= 79.800775
posx2= 14.800760
posy0= 49.999307
posy1= 79.999615
posy2= 14.999489

2d_Output

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.