#include <TFile.h>

#include <TH1.h>

#include <TF1.h>

#include <TCanvas.h>

#include <TString.h>

#include <iostream.h>

#include <TGraph.h>

#include <TStyle.h>

#include <TMath.h>

Double_t kTH = -0.5;

Double_t Background(Double_t *x, Double_t *par)

{

return par[0]+par[1]*x[0]+par[2]*x[0]*x[0];

}

Double_t Signal(Double_t *x, Double_t *par)

{Double_t arg = 0;

if(par[2]) arg = (x[0]-par[1])/par[2];

Double_t sig = par[0]*TMath::Exp(-0.5*arg*arg);

return sig;

}

Double_t fitFunction(Double_t *x, Double_t *par)

{

Double_t tot = Background(x,par) + Signal(x,&par[3]);

return tot;

}

void fittingDemo()

{

const int nBins = 50;

Stat_t data[nBins] = {332,301,347,381,372,400,399,433,420,403,476,468,464,493,5$ TH1F *histo = new TH1F("Statistics","Gaussian Peak on a smooth

background",50,0.5,2.95);

for (int i = 0; i<=nBins;i++){

histo->SetBinContent(i,data[i]);

histo->SetBinError(i,TMath::Sqrt(data[i]));

}

TF1 *fitFcn = new TF1("fitFcn",fitFunction,0.5,2.95,6);

fitFcn->SetParameter(6,0.6);

fitFcn->SetParameter(5,1.78);

fitFcn->SetParameters(327,140,-22,800,1.74,0.25);

histo->Fit("fitFcn");

TF1 *backFcn = new TF1("backFcn",Background,0.5,2.95,3);

backFcn->SetLineColor(3);

TF1 *sigFcn = new TF1("sigFcn",Signal,0.5,2.95,3);

sigFcn->SetLineColor(4);

Double_t par[6];

fitFcn->GetParameters(par);

backFcn->SetParameters(par);

backFcn->Draw("same");

sigFcn->SetParameters(&par[3]);

sigFcn->Draw("same");

}

2006-09-23T14:28:48