Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
fit1.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_fit
3/// \notebook
4/// Simple fitting example (1-d histogram with an interpreted function)
5///
6/// \macro_image
7/// \macro_output
8/// \macro_code
9///
10/// \author Rene Brun
11
12#include "TCanvas.h"
13#include "TFrame.h"
14#include "TBenchmark.h"
15#include "TString.h"
16#include "TF1.h"
17#include "TH1.h"
18#include "TFile.h"
19#include "TROOT.h"
20#include "TError.h"
21#include "TInterpreter.h"
22#include "TSystem.h"
23#include "TPaveText.h"
24
25void fit1() {
26 TCanvas *c1 = new TCanvas("c1_fit1","The Fit Canvas",200,10,700,500);
27 c1->SetGridx();
28 c1->SetGridy();
29 c1->GetFrame()->SetFillColor(21);
30 c1->GetFrame()->SetBorderMode(-1);
31 c1->GetFrame()->SetBorderSize(5);
32
33 // (for more details, see
34 // <a href="hist001_TH1_fillrandom_userfunc.C.nbconvert.ipynb">filling histograms with random numbers from a function</a>)
35 TFormula *form1 = new TFormula("form1", "abs(sin(x)/x)");
36 TF1 *sqroot = new TF1("sqroot", "x*gaus(0) + [3]*form1", 0.0, 10.0);
37 sqroot->SetLineColor(4);
38 sqroot->SetLineWidth(6);
39 // Set parameters to the functions "gaus" and "form1".
40 sqroot->SetParameters(10.0, 4.0, 1.0, 20.0);
41 sqroot->Print();
42
43 TH1D *h1d = new TH1D("h1d", "Test random numbers", 200, 0.0, 10.0);
44 h1d->FillRandom("sqroot", 10000);
45
46 //
47 // Now fit histogram h1d with the function sqroot
48 //
49 h1d->SetFillColor(45);
50 h1d->Fit("sqroot");
51
52 // We now annotate the picture by creating a PaveText object
53 // and displaying the list of commands in this macro
54 //
55 TString dir = gROOT->GetTutorialDir();
56 dir.Append("/math/fit/");
58 fitlabel->SetTextAlign(12);
59 fitlabel->SetFillColor(42);
60 fitlabel->ReadFile(Form("%sfit1_C.txt", dir.Data()));
61 fitlabel->Draw();
62 c1->Update();
63}
#define gROOT
Definition TROOT.h:417
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2496
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:40
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:47
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
The Canvas class.
Definition TCanvas.h:23
Definition TF1.h:182
void Print(Option_t *option="") const override
This method must be overridden when a class wants to print itself.
virtual void SetParameters(const Double_t *params)
Definition TF1.h:618
The Formula class.
Definition TFormula.h:89
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng=nullptr)
Definition TH1.cxx:3577
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:3954
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
TString & Append(const char *cs)
Definition TString.h:581
return c1
Definition legend1.C:41
Definition fit1.py:1