fithist.C

Go to the documentation of this file.
00001 //example of fit where the model is histogram + function
00002 //Author: Rene Brun
00003    
00004 TH1F *background;
00005 void histgen() {
00006    //generate the histogram background and save it to a file
00007    //background taken as linearly decreasing
00008    
00009    TF1 f1("f1","pol1",0,10);
00010    f1.SetParameters(5,-0.5);
00011    TH1F h("background","linear background",100,0,10);
00012    h.FillRandom("f1",10000);
00013    TFile f("background.root","recreate");
00014    //save the background histogram
00015    h.Write();
00016    //superimpose a gaussian signal to the background histogram
00017    TF1 f2("f2","gaus",0,10);
00018    f2.SetParameters(1,6,0.5);
00019    h.FillRandom("f2",2000);
00020    h.SetName("result");
00021    h.Write();
00022 }
00023 
00024 Double_t ftotal(Double_t *x, Double_t *par) {
00025    Double_t xx = x[0];
00026    Int_t bin = background->GetXaxis()->FindBin(xx);
00027    Double_t br = par[3]*background->GetBinContent(bin);
00028    Double_t arg = (xx-par[1])/par[2];
00029    Double_t sr = par[0]*TMath::Exp(-0.5*arg*arg);
00030    return sr + br;
00031 }
00032 void fithist() {
00033    //fit function ftotal to signal + background
00034    
00035    histgen();
00036    
00037    TFile *f = new TFile("background.root");
00038    background = (TH1F*)f->Get("background"); //pointer used in ftotal
00039    TH1F *result = (TH1F*)f->Get("result");
00040    
00041    TF1 *ftot = new TF1("ftot",ftotal,0,10,4);
00042    Double_t norm = result->GetMaximum();
00043    ftot->SetParameters(0.5*norm,5,.2,norm);
00044    ftot->SetParLimits(0,.3*norm,norm);
00045    
00046    result->Fit("ftot","b");   
00047 }   

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