寻峰 find peaks

#include "TCanvas.h"
#include "TH1.h"
#include "TF1.h"
#include "TRandom.h"
#include "TSpectrum.h"
#include "TVirtualFitter.h"
 
Int_t npeaks = 30;
Double_t fpeaks(Double_t *x, Double_t *par) {
   Double_t result = par[0] + par[1]*x[0];
   for (Int_t p=0;pRndm()*980;
      par[3*p+4] = 3+2*gRandom->Rndm();
   }
   TF1 *f = new TF1("f",fpeaks,0,1000,2+3*npeaks);
   f->SetNpx(1000);
   f->SetParameters(par);
   TCanvas *c1 = new TCanvas("c1","c1",10,10,1000,900);
   c1->Divide(1,2);
   c1->cd(1);
   h->FillRandom("f",200000);
   h->Draw();
   TH1F *h2 = (TH1F*)h->Clone("h2");
   //Use TSpectrum to find the peak candidates
   TSpectrum *s = new TSpectrum(2*npeaks);
   Int_t nfound = s->Search(h,1,"new");
   printf("Found %d candidate peaks to fitn",nfound);
   c1->Update();
   c1->cd(2);
 
   //estimate linear background
   TF1 *fline = new TF1("fline","pol1",0,1000);
   h->Fit("fline","qn");
   //Loop on all found peaks. Eliminate peaks at the background level
   par[0] = fline->GetParameter(0);
   par[1] = fline->GetParameter(1);
   npeaks = 0;
   Float_t *xpeaks = s->GetPositionX();
   for (p=0;pGetXaxis()->FindBin(xp);
      Float_t yp = h->GetBinContent(bin);
      if (yp-TMath::Sqrt(yp) < fline->Eval(xp)) continue;
      par[3*npeaks+2] = yp;
      par[3*npeaks+3] = xp;
      par[3*npeaks+4] = 3;
      npeaks++;
   }
   printf("Found %d useful peaks to fitn",npeaks);
   printf("Now fitting: Be patientn");
   TF1 *fit = new TF1("fit",fpeaks,0,1000,2+3*npeaks);
   TVirtualFitter::Fitter(h2,10+3*npeaks); //we may have more than the default 25 parameters
   fit->SetParameters(par);
   fit->SetNpx(1000);
   h2->Fit("fit");             
}

第一步:准备线性本底+多个高斯峰的函数fpeaks.

第二步:设置参数初值,直方图填充随机产生多个峰的图

第三步:TSpectrum寻峰,得到寻峰个数,先拟合线性本底,得到本底部分的两个参数值,利用寻峰位置(x,y)作为高斯峰拟合参数初值。

第四步:再用fpeaks这个函数进行多峰拟合

你可能感兴趣的:(C++)