6 #include "TVirtualFitter.h"
9 Double_t
fpeaks(Double_t *
x, Double_t *par) {
10 Double_t result = par[0] + par[1]*x[0];
12 Double_t
norm = par[3*
p+2];
13 Double_t
mean = par[3*
p+3];
14 Double_t
sigma = par[3*
p+4];
15 result += norm*TMath::Gaus(x[0],mean,sigma);
23 TH1F *
h =
new TH1F(
"h",
"test",500,0,1000);
31 par[3*p+3] = 10+gRandom->Rndm()*980;
32 par[3*p+4] = 3+2*gRandom->Rndm();
34 TF1 *
f =
new TF1(
"f",
fpeaks,0,1000,2+3*npeaks);
36 f->SetParameters(par);
37 TCanvas *c1 =
new TCanvas(
"c1",
"c1",10,10,1000,900);
40 h->FillRandom(
"f",200000);
43 TH1F *
h2 = (TH1F*)h->Clone(
"h2");
45 TSpectrum *
s =
new TSpectrum(2*npeaks);
46 Int_t nfound = s->Search(h,1,
"new");
47 printf(
"Found %d candidate peaks to fitn",nfound);
52 TF1 *fline =
new TF1(
"fline",
"pol1",0,1000);
55 par[0] = fline->GetParameter(0);
56 par[1] = fline->GetParameter(1);
58 Float_t *xpeaks = s->GetPositionX();
59 for (p=0;p<nfound;p++) {
60 Float_t xp = xpeaks[
p];
61 Int_t bin = h->GetXaxis()->FindBin(xp);
62 Float_t yp = h->GetBinContent(bin);
63 if (yp-TMath::Sqrt(yp) < fline->Eval(xp))
continue;
70 printf(
"Found %d useful peaks to fitn",npeaks);
71 printf(
"Now fitting: Be patientn");
72 TF1 *
fit =
new TF1(
"fit",
fpeaks,0,1000,2+3*npeaks);
73 TVirtualFitter::Fitter(h2,10+3*npeaks);
74 fit->SetParameters(par);