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 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 = nullptr;
41 if (!gSystem->AccessPathName("fillrandom.root")) {
42 // file exists
43 file = TFile::Open("fillrandom.root");
44 } else {
45 gROOT->ProcessLine(Form(".x %s../hist/fillrandom.C(0)",dir.Data()));
46 file = TFile::Open("fillrandom.root");
47 if (!file) return;
48 }
49
50 //
51 // The function "ls()" lists the directory contents of this file
52 //
53 file->ls();
54
55 //
56 // Get object "sqroot" from the file. Undefined objects are searched
57 // for using gROOT->FindObject("xxx"), e.g.:
58 // TF1 *sqroot = (TF1*) gROOT.FindObject("sqroot")
59 //
60 TF1 * sqroot = nullptr;
61 file->GetObject("sqroot",sqroot);
62 if (!sqroot){
63 Error("fit1.C","Cannot find object sqroot of type TF1\n");
64 return;
65 }
66 sqroot->Print();
67
68 //
69 // Now get and fit histogram h1f with the function sqroot
70 //
71 TH1F* h1f = nullptr;
72 file->GetObject("h1f",h1f);
73 if (!h1f){
74 Error("fit1.C","Cannot find object h1f of type TH1F\n");
75 return;
76 }
77 h1f->SetFillColor(45);
78 h1f->Fit("sqroot");
79
80 // We now annotate the picture by creating a PaveText object
81 // and displaying the list of commands in this macro
82 //
83 TPaveText * fitlabel = new TPaveText(0.6,0.4,0.9,0.75,"NDC");
84 fitlabel->SetTextAlign(12);
85 fitlabel->SetFillColor(42);
86 fitlabel->ReadFile(Form("%sfit1_C.txt",dir.Data()));
87 fitlabel->Draw();
88 c1->Update();
89 gBenchmark->Show("fit1");
90}
R__EXTERN TBenchmark * gBenchmark
Definition TBenchmark.h:59
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
#define gROOT
Definition TROOT.h:406
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void Start(const char *name)
Starts Benchmark with the specified name.
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
The Canvas class.
Definition TCanvas.h:23
void GetObject(const char *namecycle, T *&ptr)
Get an object with proper type checking.
Definition TDirectory.h:212
1-Dim function class
Definition TF1.h:233
void Print(Option_t *option="") const override
This method must be overridden when a class wants to print itself.
Definition TF1.cxx:2897
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
void ls(Option_t *option="") const override
List file contents.
Definition TFile.cxx:1450
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4082
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
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:3898
A Pave (see TPave) with text, lines or/and boxes inside.
Definition TPaveText.h:21
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & Append(const char *cs)
Definition TString.h:572
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1296
return c1
Definition legend1.C:41
Definition fit1.py:1