Logo ROOT   6.12/07
Reference Guide
fithist.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_fit
3 /// \notebook -js
4 /// Example of fit where the model is histogram + function
5 ///
6 /// \macro_image
7 /// \macro_output
8 /// \macro_code
9 ///
10 /// \author Rene Brun
11 
13 void histgen() {
14  //generate the histogram background and save it to a file
15  //background taken as linearly decreasing
16 
17  TF1 f1("f1","pol1",0,10);
18  f1.SetParameters(5,-0.5);
19  TH1F h("background","linear background",100,0,10);
20  h.FillRandom("f1",10000);
21  TFile f("background.root","recreate");
22  //save the background histogram
23  h.Write();
24  //superimpose a gaussian signal to the background histogram
25  TF1 f2("f2","gaus",0,10);
26  f2.SetParameters(1,6,0.5);
27  h.FillRandom("f2",2000);
28  h.SetName("result");
29  h.Write();
30 }
31 
32 Double_t ftotal(Double_t *x, Double_t *par) {
33  Double_t xx = x[0];
34  Int_t bin = background->GetXaxis()->FindBin(xx);
35  Double_t br = par[3]*background->GetBinContent(bin);
36  Double_t arg = (xx-par[1])/par[2];
37  Double_t sr = par[0]*TMath::Exp(-0.5*arg*arg);
38  return sr + br;
39 }
40 void fithist() {
41  //fit function ftotal to signal + background
42 
43  histgen();
44 
45  TFile *f = new TFile("background.root");
46  background = (TH1F*)f->Get("background"); //pointer used in ftotal
47  TH1F *result = (TH1F*)f->Get("result");
48 
49  TF1 *ftot = new TF1("ftot",ftotal,0,10,4);
50  Double_t norm = result->GetMaximum();
51  ftot->SetParameters(0.5*norm,5,.2,norm);
52  ftot->SetParLimits(0,.3*norm,norm);
53 
54  result->Fit("ftot","b");
55 }
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:785
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:7807
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:628
TH1 * h
Definition: legend2.C:5
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4763
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
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:567
int Int_t
Definition: RtypesCore.h:41
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:3385
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3414
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:279
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8217
Double_t Exp(Double_t x)
Definition: TMath.h:621
double Double_t
Definition: RtypesCore.h:55
1-Dim function class
Definition: TF1.h:211
TF1 * f1
Definition: legend1.C:11
static constexpr double sr
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:3688
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:315