ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fithist.C
Go to the documentation of this file.
1 //example of fit where the model is histogram + function
2 //Author: Rene Brun
3 
5 void histgen() {
6  //generate the histogram background and save it to a file
7  //background taken as linearly decreasing
8 
9  TF1 f1("f1","pol1",0,10);
10  f1.SetParameters(5,-0.5);
11  TH1F h("background","linear background",100,0,10);
12  h.FillRandom("f1",10000);
13  TFile f("background.root","recreate");
14  //save the background histogram
15  h.Write();
16  //superimpose a gaussian signal to the background histogram
17  TF1 f2("f2","gaus",0,10);
18  f2.SetParameters(1,6,0.5);
19  h.FillRandom("f2",2000);
20  h.SetName("result");
21  h.Write();
22 }
23 
25  Double_t xx = x[0];
26  Int_t bin = background->GetXaxis()->FindBin(xx);
27  Double_t br = par[3]*background->GetBinContent(bin);
28  Double_t arg = (xx-par[1])/par[2];
29  Double_t sr = par[0]*TMath::Exp(-0.5*arg*arg);
30  return sr + br;
31 }
32 void fithist() {
33  //fit function ftotal to signal + background
34 
35  histgen();
36 
37  TFile *f = new TFile("background.root");
38  background = (TH1F*)f->Get("background"); //pointer used in ftotal
39  TH1F *result = (TH1F*)f->Get("result");
40 
41  TF1 *ftot = new TF1("ftot",ftotal,0,10,4);
42  Double_t norm = result->GetMaximum();
43  ftot->SetParameters(0.5*norm,5,.2,norm);
44  ftot->SetParLimits(0,.3*norm,norm);
45 
46  result->Fit("ftot","b");
47 }
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:823
double par[1]
Definition: unuranDistr.cxx:38
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:432
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4629
tuple f2
Definition: surfaces.py:24
TH1 * h
Definition: legend2.C:5
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
TFile * f
Double_t x[n]
Definition: legend1.C:17
virtual void SetParLimits(Int_t ipar, Double_t parmin, Double_t parmax)
Set limits for parameter ipar.
Definition: TF1.cxx:3183
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3330
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:264
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8288
Double_t Exp(Double_t x)
Definition: TMath.h:495
Double_t ftotal(Double_t *x, Double_t *par)
Definition: fithist.C:24
double Double_t
Definition: RtypesCore.h:55
1-Dim function class
Definition: TF1.h:149
TF1 * f1
Definition: legend1.C:11
void histgen()
Definition: fithist.C:5
void fithist()
Definition: fithist.C:32
double result[121]
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition: TH1.cxx:7921
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition: TH1.cxx:3607
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
TH1F * background
Definition: fithist.C:4
TAxis * GetXaxis()
Definition: TH1.h:319