Logo ROOT  
Reference Guide
myfit.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_fit
3/// \notebook -js
4/// Get in memory an histogram from a root file and fit a user defined function.
5/// Note that a user defined function must always be defined
6/// as in this example:
7/// - first parameter: array of variables (in this example only 1-dimension)
8/// - second parameter: array of parameters
9/// Note also that in case of user defined functions, one must set
10/// an initial value for each parameter.
11///
12/// \macro_image
13/// \macro_output
14/// \macro_code
15///
16/// \author Rene Brun
17
18Double_t fitf(Double_t *x, Double_t *par)
19{
20 Double_t arg = 0;
21 if (par[2] != 0) arg = (x[0] - par[1])/par[2];
22
23 Double_t fitval = par[0]*TMath::Exp(-0.5*arg*arg);
24 return fitval;
25}
26void myfit()
27{
28 TString dir = gROOT->GetTutorialDir();
29 dir.Append("/hsimple.C");
30 dir.ReplaceAll("/./","/");
31 if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
32 TFile *hsimpleFile = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
33 if (!hsimpleFile) return;
34
35 TCanvas *c1 = new TCanvas("c1","the fit canvas",500,400);
36
37 TH1F *hpx = (TH1F*)hsimpleFile->Get("hpx");
38
39// Creates a Root function based on function fitf above
40 TF1 *func = new TF1("fitf",fitf,-2,2,3);
41
42// Sets initial values and parameter names
43 func->SetParameters(100,0,1);
44 func->SetParNames("Constant","Mean_value","Sigma");
45
46// Fit histogram in range defined by function
47 hpx->Fit(func,"r");
48
49// Gets integral of function between fit limits
50 printf("Integral of function = %g\n",func->Integral(-2,2));
51}
double Double_t
Definition: RtypesCore.h:55
#define gInterpreter
Definition: TInterpreter.h:555
#define gROOT
Definition: TROOT.h:415
The Canvas class.
Definition: TCanvas.h:31
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
1-Dim function class
Definition: TF1.h:211
virtual Double_t Integral(Double_t a, Double_t b, Double_t epsrel=1.e-12)
IntegralOneDim or analytical integral.
Definition: TF1.cxx:2502
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:638
virtual void SetParNames(const char *name0="p0", const char *name1="p1", const char *name2="p2", const char *name3="p3", const char *name4="p4", const char *name5="p5", const char *name6="p6", const char *name7="p7", const char *name8="p8", const char *name9="p9", const char *name10="p10")
Set up to 10 parameter names.
Definition: TF1.cxx:3461
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
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:3808
Basic string class.
Definition: TString.h:131
const char * Data() const
Definition: TString.h:364
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
TString & Append(const char *cs)
Definition: TString.h:559
return c1
Definition: legend1.C:41
Double_t x[n]
Definition: legend1.C:17
Double_t Exp(Double_t x)
Definition: TMath.h:717