TMath::Sort() on the array obtained from TSpectrum::GetPositionX()

Dear Rooters,

I’m trying to search the peak positions in a spectrum with TSpectrum::Search() and would like to sort the array obtained from TSpectrum::GetPositionX() with TMath::Sort, but when I look at the index obtained with Sort, they make no sense. I’ve found an example here of sorting an array that works fine for me :

float a[8]={2, 1, 5, 4,7,15, 12, 9}; long long size = 8; long long ind[8]; TMath::Sort(size,a,ind,kFALSE); for (int k = 0; k < size; ++k) std::cout <<a[k]<< " INDEX "<< ind[k] << std::endl;

but my code doesn’t at all.

Here is the code:

[code]{
TH1F*h; //I get the histo from a root file

int npeaks = 8;
float sigma = 3;
float threshold = 0.01;

TSpectrum *s = new TSpectrum(npeaks);
Int_t nfound = s->Search(h, sigma, “”,threshold);
const Float_t *mean = s->GetPositionX();

long long sizes = nfound;
long long inds[nfound];
for(int k=0; k<nfound; k++) inds[k] = 0;
TMath::Sort(sizes,mean,inds,kFALSE);

for(int j=0; j<nfound; j++)
cout<<mean[j]<<" index "<<inds[j]<<endl;

}
[/code]
and here is the output:

385 index 2
947 index 0
177 index 5
3631 index 1
2043 index 4
695 index 6
2511 index 7
2883 index 3

I naively (??) expect the index to increase from the smallest to the biggest value of the mean[i] array.
Where am I wrong? Can anyone help please?

Thank you, cheers

Paola

Hi,

This is strage. Can you provide please a full running code example ?

Lorenzo

Hello,

please find the code and the histograms root file attached.

This is the output that I get after compiling and running the code:

root [42] FindPeaks(5)
Warning in TSpectrum::SearchHighRes: Peak buffer full
Found 5 5 candidate peaks for Ge 6
357 index 2
893 index 0
159 index 3
653 index 1
1941 index 4

but if I run it with FondPeaks(4) I get the right result.

Thank you very much

Cheers

Paola

PS The root version is 5.34 (and I cannot update it unfortunately)
RunGeCalibration2.C (960 Bytes)
GeSpectra_R74.root (35.4 KB)

Hi

Sorry, the result is fine. You should carefully check the sorted values. In the attached macro is the way to do it

LorenzoRunGeCalibration2.C (1.06 KB)

Thank you very much.
I’m actually very confused on the reason for which the code

works fine although I’m not doing what you explained in your attached file, but it solves my problem.
Thank you again
Cheers
Paola

Hi Paola,

TMath::Sort returns a index vector which contains the sorted index values. So in your previous example:

357 index 2
893 index 0
159 index 3
653 index 1
1941 index 4

is correct because index[0] = 2 which corresponds to the third element in the original vector, 159.
index[1] = 0 which corresponds to the first element, 357,
index[2] = 3 which corresponds to 653
and so on…

Otherwise you can also use std::sort(vector.begin, vector.end) which will return directly the sorted vector.
The implementation is the same

Lorenzo

Now I got it!!! (sorry, I’m a bit slow :p)
Thank you very much Lorenzo

Paola