Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
macro8.C
Go to the documentation of this file.
1void format_line(TAttLine* line,int col,int sty){
2 line->SetLineWidth(5); line->SetLineColor(col);
3 line->SetLineStyle(sty);}
4
5double the_gausppar(double* vars, double* pars){
6 return pars[0]*TMath::Gaus(vars[0],pars[1],pars[2])+
7 pars[3]+pars[4]*vars[0]+pars[5]*vars[0]*vars[0];}
8
9int macro8(){
10 gStyle->SetOptTitle(0); gStyle->SetOptStat(0);
11 gStyle->SetOptFit(1111); gStyle->SetStatBorderSize(0);
12 gStyle->SetStatX(.89); gStyle->SetStatY(.89);
13
14 TF1 parabola("parabola","[0]+[1]*x+[2]*x**2",0,20);
15 format_line(&parabola,kBlue,2);
16
17 TF1 gaussian("gaussian","[0]*TMath::Gaus(x,[1],[2])",0,20);
18 format_line(&gaussian,kRed,2);
19
20 TF1 gausppar("gausppar",the_gausppar,-0,20,6);
21 double a=15; double b=-1.2; double c=.03;
22 double norm=4; double mean=7; double sigma=1;
23 gausppar.SetParameters(norm,mean,sigma,a,b,c);
24 gausppar.SetParNames("Norm","Mean","Sigma","a","b","c");
25 format_line(&gausppar,kBlue,1);
26
27 TH1F histo("histo","Signal plus background;X vals;Y Vals",50,0,20);
28 histo.SetMarkerStyle(8);
29
30 // Fake the data
31 for (int i=1;i<=5000;++i) histo.Fill(gausppar.GetRandom());
32
33 // Reset the parameters before the fit and set
34 // by eye a peak at 6 with an area of more or less 50
35 gausppar.SetParameter(0,50);
36 gausppar.SetParameter(1,6);
37 int npar=gausppar.GetNpar();
38 for (int ipar=2;ipar<npar;++ipar) gausppar.SetParameter(ipar,1);
39
40 // perform fit ...
41 auto fitResPtr = histo.Fit(&gausppar, "S");
42 // ... and retrieve fit results
43 fitResPtr->Print(); // print fit results
44 // get covariance Matrix an print it
45 TMatrixDSym covMatrix (fitResPtr->GetCovarianceMatrix());
46 covMatrix.Print();
47
48 // Set the values of the gaussian and parabola
49 for (int ipar=0;ipar<3;ipar++){
50 gaussian.SetParameter(ipar,gausppar.GetParameter(ipar));
51 parabola.SetParameter(ipar,gausppar.GetParameter(ipar+3));
52 }
53
54 histo.GetYaxis()->SetRangeUser(0,250);
55 histo.DrawClone("PE");
56 parabola.DrawClone("Same"); gaussian.DrawClone("Same");
57 TLatex latex(2,220,"#splitline{Signal Peak over}{background}");
58 latex.DrawClone("Same");
59 return 0;
60}
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
@ kRed
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:67
TMatrixTSym< Double_t > TMatrixDSym
externTStyle * gStyle
Definition TStyle.h:442
Line Attributes class.
Definition TAttLine.h:21
1-Dim function class
Definition TF1.h:182
virtual Double_t GetRandom(TRandom *rng=nullptr, Option_t *opt=nullptr)
Return a random number following this function shape.
Definition TF1.cxx:2240
virtual Int_t GetNpar() const
Definition TF1.h:446
virtual void SetParNames(const char *name0="", const char *name1="", const char *name2="", const char *name3="", const char *name4="", const char *name5="", const char *name6="", const char *name7="", const char *name8="", const char *name9="", const char *name10="")
Set up to 10 parameter names.
Definition TF1.cxx:3518
virtual void SetParameters(const Double_t *params)
Definition TF1.h:618
virtual void SetParameter(Int_t param, Double_t value)
Definition TF1.h:608
virtual Double_t GetParameter(Int_t ipar) const
Definition TF1.h:477
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
To draw Mathematical Formula.
Definition TLatex.h:20
void Print(Option_t *name="") const override
Print the matrix as a table of elements.
virtual TObject * DrawClone(Option_t *option="") const
Draw a clone of this object in the current selected pad with: gROOT->SetSelectedPad(c1).
Definition TObject.cxx:319
TLine * line
const Double_t sigma
Definition h1analysis.C:161
void latex()
Definition latex.C:11
int macro8()
Definition macro8.C:9
double the_gausppar(double *vars, double *pars)
Definition macro8.C:5
void format_line(TAttLine *line, int col, int sty)
Definition macro8.C:1
TH1 * histo
Double_t Gaus(Double_t x, Double_t mean=0, Double_t sigma=1, Bool_t norm=kFALSE)
Calculates a gaussian function with mean and sigma.
Definition TMath.cxx:471