Logo ROOT   6.18/05
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
12TH1F *background;
13void 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
32Double_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}
40void 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}
#define f(i)
Definition: RSha256.hxx:104
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:41
double Double_t
Definition: RtypesCore.h:55
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:279
1-Dim function class
Definition: TF1.h:211
virtual void SetParLimits(Int_t ipar, Double_t parmin, Double_t parmax)
Set limits for parameter ipar.
Definition: TF1.cxx:3497
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:638
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:316
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:3791
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:7964
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4882
Double_t x[n]
Definition: legend1.C:17
TF1 * f1
Definition: legend1.C:11
static constexpr double sr
Double_t Exp(Double_t x)
Definition: TMath.h:715