ROOT logo

From $ROOTSYS/tutorials/fit/fithist.C

//example of fit where the model is histogram + function
//Author: Rene Brun
   
TH1F *background;
void histgen() {
   //generate the histogram background and save it to a file
   //background taken as linearly decreasing
   
   TF1 f1("f1","pol1",0,10);
   f1.SetParameters(5,-0.5);
   TH1F h("background","linear background",100,0,10);
   h.FillRandom("f1",10000);
   TFile f("background.root","recreate");
   //save the background histogram
   h.Write();
   //superimpose a gaussian signal to the background histogram
   TF1 f2("f2","gaus",0,10);
   f2.SetParameters(1,6,0.5);
   h.FillRandom("f2",2000);
   h.SetName("result");
   h.Write();
}

Double_t ftotal(Double_t *x, Double_t *par) {
   Double_t xx = x[0];
   Int_t bin = background->GetXaxis()->FindBin(xx);
   Double_t br = par[3]*background->GetBinContent(bin);
   Double_t arg = (xx-par[1])/par[2];
   Double_t sr = par[0]*TMath::Exp(-0.5*arg*arg);
   return sr + br;
}
void fithist() {
   //fit function ftotal to signal + background
   
   histgen();
   
   TFile *f = new TFile("background.root");
   background = (TH1F*)f->Get("background"); //pointer used in ftotal
   TH1F *result = (TH1F*)f->Get("result");
   
   TF1 *ftot = new TF1("ftot",ftotal,0,10,4);
   Double_t norm = result->GetMaximum();
   ftot->SetParameters(0.5*norm,5,.2,norm);
   ftot->SetParLimits(0,.3*norm,norm);
   
   result->Fit("ftot","b");   
}   
 fithist.C:1
 fithist.C:2
 fithist.C:3
 fithist.C:4
 fithist.C:5
 fithist.C:6
 fithist.C:7
 fithist.C:8
 fithist.C:9
 fithist.C:10
 fithist.C:11
 fithist.C:12
 fithist.C:13
 fithist.C:14
 fithist.C:15
 fithist.C:16
 fithist.C:17
 fithist.C:18
 fithist.C:19
 fithist.C:20
 fithist.C:21
 fithist.C:22
 fithist.C:23
 fithist.C:24
 fithist.C:25
 fithist.C:26
 fithist.C:27
 fithist.C:28
 fithist.C:29
 fithist.C:30
 fithist.C:31
 fithist.C:32
 fithist.C:33
 fithist.C:34
 fithist.C:35
 fithist.C:36
 fithist.C:37
 fithist.C:38
 fithist.C:39
 fithist.C:40
 fithist.C:41
 fithist.C:42
 fithist.C:43
 fithist.C:44
 fithist.C:45
 fithist.C:46
 fithist.C:47
 fithist.C:48
thumb