A puzzling problem about TSpectrum

Dear all:
There is a very strange problem when i use TSpectrum class. TSpectrum could not find the peaks at the front part of axis-X. I changed the parameter used in Search(), but it didn’t work.Next is my code:

 {
       TFile *file = new TFile("Spectrum.root");
       TH1F *h = (TH1F*)file->Get("spectrum");
       TSpectrum *s = new TSpectrum(220);
       s->Search(h,1,"nobackground",0.1);
       h->Draw();
   }

Next is the result,it really puzzled me.



Spectrum.root (14.5 KB)
SearchSpectrum.C (187 Bytes)

Yes … it seems it has some problems finding them all …
May be you can try other techniques shown here:
root.cern/doc/master/group__tut … ctrum.html

in particular the high resolution one:

root.cern/doc/master/SearchHR3_8C.html

[quote=“couet”]Yes … it seems it has some problems finding them all …
May be you can try other techniques shown here:
root.cern/doc/master/group__tut … ctrum.html

in particular the high resolution one:

root.cern/doc/master/SearchHR3_8C.html[/quote]

Thanks for your reply,I am trying SearchHighRes method.
I looked up the source code of Search() method, i found Seach() calls SearchHighRes() method in its definition.
anyway,Iet’s talk it after trying SearchHighRes method.

I tried SearchHighRes() method,and something new was found.
When I reset the searching range ,for example 5000,it works well ,while when nbins was setted greater than 6000,the fault rise again.So I think it may result from the buffer full.But I have no idea how to solve it.

int SearchSpectrum(){
       TFile *file = new TFile("Spectrum.root");
       TH1F *h = (TH1F*)file->Get("spectrum");
       int fNpeaks=0;
       int i,nfound,bin;
       //const int nbins = h->GetNbinsX();
       const int nbins = 5000; //here ,if nbins was greater than 7000,it could not work 
       double xmin = 0.;
      double xmax = nbins;
      double source[nbins],dest[nbins];
      h->GetXaxis()->SetRange(1,nbins);
      TSpectrum *s = new TSpectrum(220);
      for (i =0;i<nbins;++i) source[i]=h->GetBinContent(i+1);
      nfound = s->SearchHighRes(source,dest,nbins,3,10,kTRUE,3,kTRUE,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");
      return 0;
  }   

The only information I have is the documentation here:
root.cern/doc/master/classTSpec … f5f78d0b2b
and the source code going with it.
Not being the author of this code, I am afraid I cannot help more than that.

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 :slight_smile: