ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fit1.C
Go to the documentation of this file.
1 #include "TCanvas.h"
2 #include "TFrame.h"
3 #include "TBenchmark.h"
4 #include "TString.h"
5 #include "TF1.h"
6 #include "TH1.h"
7 #include "TFile.h"
8 #include "TROOT.h"
9 #include "TError.h"
10 #include "TInterpreter.h"
11 #include "TSystem.h"
12 #include "TPaveText.h"
13 
14 void fit1() {
15  //Simple fitting example (1-d histogram with an interpreted function)
16  //To see the output of this macro, click begin_html <a href="gif/fit1.gif">here</a>. end_html
17  //Author: Rene Brun
18 
19  TCanvas *c1 = new TCanvas("c1_fit1","The Fit Canvas",200,10,700,500);
20  c1->SetGridx();
21  c1->SetGridy();
22  c1->GetFrame()->SetFillColor(21);
23  c1->GetFrame()->SetBorderMode(-1);
24  c1->GetFrame()->SetBorderSize(5);
25 
26  gBenchmark->Start("fit1");
27  //
28  // We connect the ROOT file generated in a previous tutorial
29  // (see begin_html <a href="fillrandom.C.html">Filling histograms with random numbers from a function</a>) end_html
30  //
31  TString dir = gSystem->UnixPathName(__FILE__);
32  dir.ReplaceAll("fit1.C","");
33  dir.ReplaceAll("/./","/");
34  TFile *file = TFile::Open("fillrandom.root");
35  if (!file) {
36  gROOT->ProcessLine(Form(".x %s../hist/fillrandom.C",dir.Data()));
37  file = TFile::Open("fillrandom.root");
38  if (!file) return;
39  }
40 
41  //
42  // The function "ls()" lists the directory contents of this file
43  //
44  file->ls();
45 
46  //
47  // Get object "sqroot" from the file. Undefined objects are searched
48  // for using gROOT->FindObject("xxx"), e.g.:
49  // TF1 *sqroot = (TF1*) gROOT.FindObject("sqroot")
50  //
51  TF1 * sqroot = 0;
52  file->GetObject("sqroot",sqroot);
53  if (!sqroot){
54  Error("fit1.C","Cannot find object sqroot of type TF1\n");
55  return;
56  }
57  sqroot->Print();
58 
59  //
60  // Now get and fit histogram h1f with the function sqroot
61  //
62  TH1F* h1f = 0;
63  file->GetObject("h1f",h1f);
64  if (!h1f){
65  Error("fit1.C","Cannot find object h1f of type TH1F\n");
66  return;
67  }
68  h1f->SetFillColor(45);
69  h1f->Fit("sqroot");
70 
71  // We now annotate the picture by creating a PaveText object
72  // and displaying the list of commands in this macro
73  //
74  TPaveText * fitlabel = new TPaveText(0.6,0.3,0.9,0.80,"NDC");
75  fitlabel->SetTextAlign(12);
76  fitlabel->SetFillColor(42);
77  fitlabel->ReadFile(Form("%sfit1_C.txt",dir.Data()));
78  fitlabel->Draw();
79  c1->Update();
80  gBenchmark->Show("fit1");
81 }
virtual void SetGridx(Int_t value=1)
Definition: TPad.h:327
virtual void ls(Option_t *option="") const
List file contents.
Definition: TFile.cxx:1361
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:211
virtual void SetBorderMode(Short_t bordermode)
Definition: TWbox.h:62
TCanvas * c1
Definition: legend1.C:2
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
void fit1()
Definition: fit1.C:14
#define gROOT
Definition: TROOT.h:344
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
Basic string class.
Definition: TString.h:137
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
tuple fitlabel
Definition: fit1.py:44
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1020
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:3851
tuple h1f
Definition: fillrandom.py:48
const char * Data() const
Definition: TString.h:349
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2729
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:579
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:63
void Error(const char *location, const char *msgfmt,...)
void GetObject(const char *namecycle, T *&ptr)
virtual void SetTextAlign(Short_t align=11)
Definition: TAttText.h:55
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
char * Form(const char *fmt,...)
The Canvas class.
Definition: TCanvas.h:48
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:35
tuple file
Definition: fildir.py:20
virtual void SetGridy(Int_t value=1)
Definition: TPad.h:328
virtual void Print(Option_t *option="") const
Print TNamed name and title.
Definition: TF1.cxx:2600
void dir(char *path=0)
Definition: rootalias.C:30
1-Dim function class
Definition: TF1.h:149
tuple sqroot
Definition: fillrandom.py:25
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
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:3607