FittingDemo.C

Go to the documentation of this file.
00001 //Example for fitting signal/background. 
00002 // This example can be executed with:
00003 // root > .x FittingDemo.C  (using the CINT interpreter)
00004 // root > .x FittingDemo.C+ (using the native complier via ACLIC)
00005 //Author: Rene Brun
00006    
00007 #include "TH1.h"
00008 #include "TMath.h"
00009 #include "TF1.h"
00010 #include "TLegend.h"
00011 #include "TCanvas.h"
00012 
00013 // Quadratic background function
00014 Double_t background(Double_t *x, Double_t *par) {
00015    return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
00016 }
00017 
00018 
00019 // Lorenzian Peak function
00020 Double_t lorentzianPeak(Double_t *x, Double_t *par) {
00021   return (0.5*par[0]*par[1]/TMath::Pi()) / 
00022     TMath::Max( 1.e-10,(x[0]-par[2])*(x[0]-par[2]) 
00023    + .25*par[1]*par[1]);
00024 }
00025 
00026 // Sum of background and peak function
00027 Double_t fitFunction(Double_t *x, Double_t *par) {
00028   return background(x,par) + lorentzianPeak(x,&par[3]);
00029 }
00030 
00031 void FittingDemo() {
00032  //Bevington Exercise by Peter Malzacher, modified by Rene Brun
00033  
00034    const int nBins = 60;
00035    
00036    Double_t data[nBins] = { 6, 1,10,12, 6,13,23,22,15,21,
00037                            23,26,36,25,27,35,40,44,66,81,
00038                            75,57,48,45,46,41,35,36,53,32,
00039                            40,37,38,31,36,44,42,37,32,32,
00040                            43,44,35,33,33,39,29,41,32,44,
00041                            26,39,29,35,32,21,21,15,25,15};
00042    TCanvas *c1 = new TCanvas("c1","Fitting Demo",10,10,700,500);
00043    c1->SetFillColor(33);
00044    c1->SetFrameFillColor(41);
00045    c1->SetGrid();
00046    
00047    TH1F *histo = new TH1F("histo",
00048       "Lorentzian Peak on Quadratic Background",60,0,3);
00049    histo->SetMarkerStyle(21);
00050    histo->SetMarkerSize(0.8);
00051    histo->SetStats(0);
00052        
00053    for(int i=0; i < nBins;  i++) histo->SetBinContent(i+1,data[i]);
00054    
00055    // create a TF1 with the range from 0 to 3 and 6 parameters
00056    TF1 *fitFcn = new TF1("fitFcn",fitFunction,0,3,6);
00057    fitFcn->SetNpx(500);
00058    fitFcn->SetLineWidth(4);
00059    fitFcn->SetLineColor(kMagenta);
00060     
00061    // first try without starting values for the parameters
00062    // This defaults to 1 for each param. 
00063    // this results in an ok fit for the polynomial function
00064    // however the non-linear part (lorenzian) does not 
00065    // respond well.
00066    fitFcn->SetParameters(1,1,1,1,1,1);
00067    histo->Fit("fitFcn","0");
00068    
00069    // second try: set start values for some parameters
00070    fitFcn->SetParameter(4,0.2); // width
00071    fitFcn->SetParameter(5,1);   // peak
00072  
00073    histo->Fit("fitFcn","V+","ep");
00074    
00075    // improve the picture:
00076    TF1 *backFcn = new TF1("backFcn",background,0,3,3);
00077    backFcn->SetLineColor(kRed);
00078    TF1 *signalFcn = new TF1("signalFcn",lorentzianPeak,0,3,3);
00079    signalFcn->SetLineColor(kBlue);
00080    signalFcn->SetNpx(500);
00081    Double_t par[6];
00082   
00083    // writes the fit results into the par array
00084    fitFcn->GetParameters(par);
00085     
00086    backFcn->SetParameters(par);
00087    backFcn->Draw("same");
00088  
00089    signalFcn->SetParameters(&par[3]);
00090    signalFcn->Draw("same"); 
00091    
00092    // draw the legend
00093    TLegend *legend=new TLegend(0.6,0.65,0.88,0.85);
00094    legend->SetTextFont(72);
00095    legend->SetTextSize(0.04);
00096    legend->AddEntry(histo,"Data","lpe");
00097    legend->AddEntry(backFcn,"Background fit","l");
00098    legend->AddEntry(signalFcn,"Signal fit","l");
00099    legend->AddEntry(fitFcn,"Global Fit","l");
00100    legend->Draw();
00101    
00102 }

Generated on Tue Jul 5 15:44:07 2011 for ROOT_528-00b_version by  doxygen 1.5.1