Logo ROOT   6.14/05
Reference Guide
fit1.C File Reference

Detailed Description

View in nbviewer Open in SWAN Simple fitting example (1-d histogram with an interpreted function)

pict1_fit1.C.png
Processing /mnt/build/workspace/root-makedoc-v614/rootspi/rdoc/src/v6-14-00-patches/tutorials/fit/fit1.C...
TFile** fillrandom.root
TFile* fillrandom.root
KEY: TFormula form1;1 abs(sin(x)/x)
KEY: TF1 sqroot;1 x*gaus(0) + [3]*form1
KEY: TH1F h1f;1 Test random numbers
Formula based function: sqroot
sqroot : x*gaus(0) + [3]*form1 Ndim= 1, Npar= 4, Number= 0
Formula expression:
x*[p0]*exp(-0.5*((x-[p1])/[p2])*((x-[p1])/[p2]))+[p3]*(abs(sin(x)/x))
FCN=228.306 FROM MIGRAD STATUS=CONVERGED 124 CALLS 125 TOTAL
EDM=2.9409e-07 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 4.1 per cent
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 3.28695e+01 5.29852e-01 3.80864e-03 -1.64534e-03
2 p1 4.00185e+00 1.74174e-02 -1.80013e-04 2.19473e-02
3 p2 9.87994e-01 1.24649e-02 3.96957e-05 -5.17266e-02
4 p3 6.38285e+01 1.32701e+00 4.51402e-03 -3.51647e-04
fit1 : Real Time = 0.15 seconds Cpu Time = 0.15 seconds
#include "TCanvas.h"
#include "TFrame.h"
#include "TBenchmark.h"
#include "TString.h"
#include "TF1.h"
#include "TH1.h"
#include "TFile.h"
#include "TROOT.h"
#include "TError.h"
#include "TInterpreter.h"
#include "TSystem.h"
#include "TPaveText.h"
void fit1() {
TCanvas *c1 = new TCanvas("c1_fit1","The Fit Canvas",200,10,700,500);
c1->SetGridx();
c1->SetGridy();
c1->GetFrame()->SetFillColor(21);
c1->GetFrame()->SetBorderMode(-1);
gBenchmark->Start("fit1");
//
// We connect the ROOT file generated in a previous tutorial
// (see <a href="fillrandom.C.nbconvert.ipynb">Filling histograms with random numbers from a function</a>)
//
TString dir = gROOT->GetTutorialDir();
dir.Append("/fit/");
TFile *file = TFile::Open("fillrandom.root");
if (!file) {
gROOT->ProcessLine(Form(".x %s../hist/fillrandom.C",dir.Data()));
file = TFile::Open("fillrandom.root");
if (!file) return;
}
//
// The function "ls()" lists the directory contents of this file
//
file->ls();
//
// Get object "sqroot" from the file. Undefined objects are searched
// for using gROOT->FindObject("xxx"), e.g.:
// TF1 *sqroot = (TF1*) gROOT.FindObject("sqroot")
//
TF1 * sqroot = 0;
file->GetObject("sqroot",sqroot);
if (!sqroot){
Error("fit1.C","Cannot find object sqroot of type TF1\n");
return;
}
sqroot->Print();
//
// Now get and fit histogram h1f with the function sqroot
//
TH1F* h1f = 0;
file->GetObject("h1f",h1f);
if (!h1f){
Error("fit1.C","Cannot find object h1f of type TH1F\n");
return;
}
h1f->SetFillColor(45);
h1f->Fit("sqroot");
// We now annotate the picture by creating a PaveText object
// and displaying the list of commands in this macro
//
TPaveText * fitlabel = new TPaveText(0.6,0.4,0.9,0.75,"NDC");
fitlabel->SetTextAlign(12);
fitlabel->SetFillColor(42);
fitlabel->ReadFile(Form("%sfit1_C.txt",dir.Data()));
fitlabel->Draw();
c1->Update();
gBenchmark->Show("fit1");
}
Author
Rene Brun

Definition in file fit1.C.