After a quick glimpse at your code and some fast tests:
Removing “nobackground” flag ended up with finding much more peaks - since your spectrum doesn’t have any standing out, much higher peaks, the algorithm may be seeing them as a background. I suggest playing a bit with parameters of Search(), especially sigma, maybe it’ll give you a better result. At this moment I have no idea how to solve your problem completely - maybe it’s the fault of your input? When I had similiar problems with my input, it appeared that number of bins of histo didn’t match with my input data.
To make tests easier, you can print amount of found peaks and change your parameters to get the most of them.
int peaksfound = s->Search(h,1,"",0.1);
....
cout<<peaksfound<<endl;
p.s Your spectrum is quite strange - could you share what it represent’s? What’s on Xaxis and what’s on Yaxis?
edit:
I used your SearchHighRes() code, that’s what I’ve found:
Your buffer is getting full, because you set maxpeaksfound to 220 in this line:
Also, you had a mistake in this line:
It should be:
int nbins2 = h->GetSize();
const int nbins = nbins2;
Now you don’t have to worry about nbins.
Also, I changed type of source and dest to float - my version of ROOT had some problems with them being double…
Check the output of your code with my edits:
int SearchSpectrum(){
TFile *file = new TFile("Spectrum.root");
TH1F *h = (TH1F*)file->Get("spectrum");
int fNpeaks=0;
int i,nfound,bin;
int nbins2 = h->GetSize();
const int nbins = nbins2;
//const int nbins = 5000; //here ,if nbins was greater than 7000,it could not work
double xmin = 0;
double xmax = nbins;
float source[nbins],dest[nbins];
h->GetXaxis()->SetRange(1,nbins);
TSpectrum *s = new TSpectrum(1000000);
for (i =0;i<nbins;++i) source[i]=h->GetBinContent(i+1);
nfound = s->SearchHighRes(source,dest,nbins,2,0.05,kFALSE,1,kFALSE,3);
std::cout<<"found peaks : "<<nfound<<std::endl;
h->Draw();
TH1F *d1 = new TH1F("d1","",nbins,xmin,xmax);
for (i = 0; i < nbins; i++) d1->SetBinContent(i+1,dest[i]);
d1->SetLineColor(kRed);
d1->Draw("SAME");
//s->Search(d1,1,"",0.05);
//h->Draw("SAME");
return 0;
}
I don’t know if it solved your problem, but maybe it’ll get you closer to the solution