Logo ROOT  
Reference Guide
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.
12/// \macro_image
13/// \macro_output
14/// \macro_code
16/// \author Rene Brun
18#include <TCanvas.h>
19#include <TF1.h>
20#include <TFile.h>
21#include <TH1F.h>
22#include <TInterpreter.h>
23#include <TROOT.h>
25#include <cmath>
27double fitf(double *x, double *par)
29 double arg = 0;
30 if (par[2] != 0) arg = (x[0] - par[1])/par[2];
32 double fitval = par[0]*std::exp(-0.5*arg*arg);
33 return fitval;
35void myfit()
37 TString dir = gROOT->GetTutorialDir();
38 dir.Append("/hsimple.C");
39 dir.ReplaceAll("/./","/");
40 if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
41 TFile *hsimpleFile = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
42 if (!hsimpleFile) return;
44 TCanvas *c1 = new TCanvas("c1","the fit canvas",500,400);
46 TH1F *hpx = (TH1F*)hsimpleFile->Get("hpx");
48// Creates a Root function based on function fitf above
49 TF1 *func = new TF1("fitf",fitf,-2,2,3);
51// Sets initial values and parameter names
52 func->SetParameters(100,0,1);
53 func->SetParNames("Constant","Mean_value","Sigma");
55// Fit histogram in range defined by function
56 hpx->Fit(func,"r");
58// Gets integral of function between fit limits
59 printf("Integral of function = %g\n",func->Integral(-2,2));
#define gInterpreter
Definition: TInterpreter.h:564
#define gROOT
Definition: TROOT.h:404
The Canvas class.
Definition: TCanvas.h:23
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
1-Dim function class
Definition: TF1.h:213
virtual Double_t Integral(Double_t a, Double_t b, Double_t epsrel=1.e-12)
IntegralOneDim or analytical integral.
Definition: TF1.cxx:2544
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:649
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:3506
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:574
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:3894
Basic string class.
Definition: TString.h:136
const char * Data() const
Definition: TString.h:369
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
TString & Append(const char *cs)
Definition: TString.h:564
RVec< PromoteType< T > > exp(const RVec< T > &v)
Definition: RVec.hxx:1761
return c1
Definition: legend1.C:41
Double_t x[n]
Definition: legend1.C:17