Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
myfit.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Get in memory an histogram from a root file and fit a user defined function.

Note that a user defined function must always be defined as in this example:

  • first parameter: array of variables (in this example only 1-dimension)
  • second parameter: array of parameters Note also that in case of user defined functions, one must set an initial value for each parameter.
****************************************
Minimizer is Minuit2 / Migrad
Chi2 = 36.7428
NDf = 47
Edm = 2.03167e-06
NCalls = 101
Constant = 797.969 +/- 6.79742
Mean_value = -7.42918e-05 +/- 0.00734861
Sigma = 0.998754 +/- 0.0071337
Integral of function = 1907.36
#include <TCanvas.h>
#include <TF1.h>
#include <TFile.h>
#include <TH1F.h>
#include <TInterpreter.h>
#include <TROOT.h>
#include <cmath>
double fitf(double *x, double *par)
{
double arg = 0;
if (par[2] != 0) arg = (x[0] - par[1])/par[2];
double fitval = par[0]*std::exp(-0.5*arg*arg);
return fitval;
}
void myfit()
{
TString dir = gROOT->GetTutorialDir();
dir.Append("/hsimple.C");
dir.ReplaceAll("/./","/");
if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
TFile *hsimpleFile = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
if (!hsimpleFile) return;
TCanvas *c1 = new TCanvas("c1","the fit canvas",500,400);
TH1F *hpx = (TH1F*)hsimpleFile->Get("hpx");
// Creates a Root function based on function fitf above
TF1 *func = new TF1("fitf",fitf,-2,2,3);
// Sets initial values and parameter names
func->SetParameters(100,0,1);
func->SetParNames("Constant","Mean_value","Sigma");
// Fit histogram in range defined by function
hpx->Fit(func,"r");
// Gets integral of function between fit limits
printf("Integral of function = %g\n",func->Integral(-2,2));
}
#define gInterpreter
#define gROOT
Definition TROOT.h:406
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:233
virtual Double_t Integral(Double_t a, Double_t b, Double_t epsrel=1.e-12)
IntegralOneDim or analytical integral.
Definition TF1.cxx:2531
virtual void SetParNames(const char *name0="", const char *name1="", const char *name2="", const char *name3="", const char *name4="", const char *name5="", const char *name6="", const char *name7="", const char *name8="", const char *name9="", const char *name10="")
Set up to 10 parameter names.
Definition TF1.cxx:3463
virtual void SetParameters(const Double_t *params)
Definition TF1.h:677
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:622
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
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
TString & Append(const char *cs)
Definition TString.h:572
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
Author
Rene Brun

Definition in file myfit.C.