Logo ROOT   6.12/07
Reference Guide
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 
25 void 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  gBenchmark->Start("fit1");
34  //
35  // We connect the ROOT file generated in a previous tutorial
36  // (see <a href="fillrandom.C.nbconvert.ipynb">Filling histograms with random numbers from a function</a>)
37  //
38  TString dir = gROOT->GetTutorialDir();
39  dir.Append("/fit/");
40  TFile *file = TFile::Open("fillrandom.root");
41  if (!file) {
42  gROOT->ProcessLine(Form(".x %s../hist/fillrandom.C",dir.Data()));
43  file = TFile::Open("fillrandom.root");
44  if (!file) return;
45  }
46 
47  //
48  // The function "ls()" lists the directory contents of this file
49  //
50  file->ls();
51 
52  //
53  // Get object "sqroot" from the file. Undefined objects are searched
54  // for using gROOT->FindObject("xxx"), e.g.:
55  // TF1 *sqroot = (TF1*) gROOT.FindObject("sqroot")
56  //
57  TF1 * sqroot = 0;
58  file->GetObject("sqroot",sqroot);
59  if (!sqroot){
60  Error("fit1.C","Cannot find object sqroot of type TF1\n");
61  return;
62  }
63  sqroot->Print();
64 
65  //
66  // Now get and fit histogram h1f with the function sqroot
67  //
68  TH1F* h1f = 0;
69  file->GetObject("h1f",h1f);
70  if (!h1f){
71  Error("fit1.C","Cannot find object h1f of type TH1F\n");
72  return;
73  }
74  h1f->SetFillColor(45);
75  h1f->Fit("sqroot");
76 
77  // We now annotate the picture by creating a PaveText object
78  // and displaying the list of commands in this macro
79  //
80  TPaveText * fitlabel = new TPaveText(0.6,0.4,0.9,0.75,"NDC");
81  fitlabel->SetTextAlign(12);
82  fitlabel->SetFillColor(42);
83  fitlabel->ReadFile(Form("%sfit1_C.txt",dir.Data()));
84  fitlabel->Draw();
85  c1->Update();
86  gBenchmark->Show("fit1");
87 }
virtual void SetGridx(Int_t value=1)
Definition: TPad.h:328
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:234
virtual void SetBorderMode(Short_t bordermode)
Definition: TWbox.h:49
return c1
Definition: legend1.C:41
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
#define gROOT
Definition: TROOT.h:402
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:157
Basic string class.
Definition: TString.h:125
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3950
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:174
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2812
Definition: fit1.py:1
fitlabel
Definition: fit1_py.py:12
TString & Append(const char *cs)
Definition: TString.h:495
virtual void ReadFile(const char *filename, Option_t *option="", Int_t nlines=50, Int_t fromline=0)
Read lines of filename in this pavetext.
Definition: TPaveText.cxx:598
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:50
void Error(const char *location, const char *msgfmt,...)
void GetObject(const char *namecycle, T *&ptr)
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition: TAttText.h:41
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
char * Form(const char *fmt,...)
The Canvas class.
Definition: TCanvas.h:31
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:21
virtual void SetGridy(Int_t value=1)
Definition: TPad.h:329
Definition: file.py:1
1-Dim function class
Definition: TF1.h:211
virtual void Print(Option_t *option="") const
Print TNamed name and title.
Definition: TF1.cxx:2763
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2248
virtual void ls(Option_t *option="") const
List file contents.
Definition: TFile.cxx:1383
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:3688
const char * Data() const
Definition: TString.h:345