Logo ROOT   6.07/09
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/vdb/lsf/workspace/root-makedoc-v608/rootspi/rdoc/src/v6-08-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=227.072 FROM MIGRAD STATUS=CONVERGED 151 CALLS 152 TOTAL
EDM=1.70589e-08 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 3.38761e+01 5.48516e-01 3.27964e-03 -3.84524e-04
2 p1 3.99290e+00 1.65535e-02 9.90380e-05 -6.69119e-03
3 p2 9.63145e-01 1.21657e-02 6.10278e-05 -1.68758e-02
4 p3 6.34107e+01 1.32066e+00 9.33049e-03 -6.31545e-05
fit1 : Real Time = 0.42 seconds Cpu Time = 0.42 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->GetTutorialsDir();
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.