Logo ROOT  
Reference Guide
fithist.C File Reference

Detailed Description

View in nbviewer Open in SWAN Example of fit where the model is histogram + function

FCN=7.84156 FROM MIGRAD STATUS=CONVERGED 183 CALLS 184 TOTAL
EDM=4.73985e-09 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 1.56629e+02 5.91047e+00 7.83834e-05 3.68508e-06
2 p1 5.98184e+00 1.89049e-02 2.72632e-05 -3.85176e-03
3 p2 -5.06680e-01 1.80489e-02 2.01697e-05 -3.36498e-03
4 p3 1.00029e+00 1.12955e-02 1.52093e-05 1.64149e-03
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");
}
#define f(i)
Definition: RSha256.hxx:104
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:43
double Double_t
Definition: RtypesCore.h:57
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:290
1-Dim function class
Definition: TF1.h:210
virtual void SetParLimits(Int_t ipar, Double_t parmin, Double_t parmax)
Set limits for parameter ipar.
Definition: TF1.cxx:3500
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:53
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:3808
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:8006
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4907
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:717
Author
Rene Brun

Definition in file fithist.C.