30 SigScale, SigSigma, SigMean,
33const std::map<ParIndex_t,std::string> parNames{
34 {Bkg0,
"Bkg0"}, {Bkg1,
"Bkg1"}, {Bkg2,
"Bkg2"},
35 {SigScale,
"Gauss scale"}, {SigSigma,
"Gauss #sigma"}, {SigMean,
"Gauss #mu"}
40double background(
double *
x,
double *par) {
41 return par[Bkg0] + par[Bkg1]*
x[0] + par[Bkg2]*
x[0]*
x[0];
45double signal(
double *
x,
double *par) {
46 return par[SigScale]*
TMath::Gaus(
x[0], par[SigMean], par[SigSigma],
true);
51double fitFunction(
double *
x,
double *par) {
52 return background(
x, par) + signal(
x, par);
59 TF1 fitFcn(
"fitFcn",fitFunction,fitxmin,fitxmax,N_PAR);
61 fitFcn.SetLineWidth(2);
62 fitFcn.SetLineColor(
kBlue);
65 for (
auto& idx_name : parNames) {
66 fitFcn.SetParName(idx_name.first, idx_name.second.c_str());
70 fitFcn.SetParameters(30,0,0,50.,0.1,1.);
72 histo->
Fit(
"fitFcn",
"VR+",
"ep");
76 TF1 backFcn(
"backFcn",background,fitxmin,fitxmax,N_PAR);
77 backFcn.SetLineColor(
kRed);
78 TF1 signalFcn(
"signalFcn",signal,fitxmin,fitxmax,N_PAR);
79 signalFcn.SetLineColor(
kBlue);
80 signalFcn.SetNpx(500);
84 fitFcn.GetParameters(par);
86 backFcn.SetParameters(par);
87 backFcn.DrawCopy(
"same");
89 signalFcn.SetParameters(par);
90 signalFcn.SetLineColor(
kGreen);
91 signalFcn.DrawCopy(
"same");
94 const double integral = signalFcn.Integral(0.,3.);
95 std::cout <<
"number of signal events = " << integral/binwidth <<
" " << binwidth<< std::endl;
98 TLegend legend(0.15,0.7,0.28,0.85);
99 legend.SetTextFont(72);
100 legend.SetTextSize(0.03);
101 legend.AddEntry(histo,
"Data",
"lpe");
102 legend.AddEntry(&backFcn,
"Bgd",
"l");
103 legend.AddEntry(&signalFcn,
"Sig",
"l");
104 legend.AddEntry(&fitFcn,
"Sig+Bgd",
"l");
106 histo->
Draw(
"esame");
111void CreateRootFile(){
113 const int nBins = 60;
114 double data[nBins] = { 6, 1,10,12, 6,13,23,22,15,21,
115 23,26,36,25,27,35,40,44,66,81,
116 75,57,43,37,36,31,35,36,43,32,
117 40,37,38,33,36,44,42,37,32,32,
118 43,44,35,33,33,39,29,41,32,44,
119 26,39,29,35,32,21,21,15,25,15};
121 TFile*
f =
new TFile(
"exampleRootFile.root",
"RECREATE");
122 TH1D *histo =
new TH1D(
"histo",
"Gauss Peak on Quadratic Background;x;Events/0.05",60,0,3);
129void FitHistoInFile() {
137 TCanvas *
c1 =
new TCanvas(
"c1",
"Fitting Demo of Histogram in File",10,10,700,500);
140 TH1D* histo=
nullptr;
141 f->GetObject(
"histo",histo);
143 std::cout <<
"histo not found" << std::endl;
149 FitRoutine(
c1,histo, fitxmin, fitxmax,
"FitHistoInFile.pdf");
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
R__EXTERN TStyle * gStyle
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
virtual void SetRange(Int_t first=0, Int_t last=0)
Set the viewing range for the axis using bin numbers.
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width.
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
1-D histogram with a double per channel (see TH1 documentation)
TH1 is the base class of all histogram classes in ROOT.
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.
void Draw(Option_t *option="") override
Draw this histogram with options.
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
This class displays a legend box (TPaveText) containing several legend entries.
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
void SetOptFit(Int_t fit=1)
The type of information about fit parameters printed in the histogram statistics box can be selected ...
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.