From $ROOTSYS/tutorials/fit/myfit.C

// 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.
//Author: Rene Brun

Double_t fitf(Double_t *x, Double_t *par)
{
   Double_t arg = 0;
   if (par[2] != 0) arg = (x[0] - par[1])/par[2];

   Double_t fitval = par[0]*TMath::Exp(-0.5*arg*arg);
   return fitval;
}
void myfit()
{
   TString dir = gSystem->UnixPathName(__FILE__);
   dir.ReplaceAll("myfit.C","../hsimple.C");
   dir.ReplaceAll("/./","/");
   if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
   TFile *hsimple = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
   if (!hsimple) return;

   TCanvas *c1 = new TCanvas("c1","the fit canvas",500,400);

   TH1F *hpx = (TH1F*)hsimple->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));
}
 myfit.C:1
 myfit.C:2
 myfit.C:3
 myfit.C:4
 myfit.C:5
 myfit.C:6
 myfit.C:7
 myfit.C:8
 myfit.C:9
 myfit.C:10
 myfit.C:11
 myfit.C:12
 myfit.C:13
 myfit.C:14
 myfit.C:15
 myfit.C:16
 myfit.C:17
 myfit.C:18
 myfit.C:19
 myfit.C:20
 myfit.C:21
 myfit.C:22
 myfit.C:23
 myfit.C:24
 myfit.C:25
 myfit.C:26
 myfit.C:27
 myfit.C:28
 myfit.C:29
 myfit.C:30
 myfit.C:31
 myfit.C:32
 myfit.C:33
 myfit.C:34
 myfit.C:35
 myfit.C:36
 myfit.C:37
 myfit.C:38
 myfit.C:39
 myfit.C:40
 myfit.C:41
 myfit.C:42
 myfit.C:43
 myfit.C:44