#include #include #include #include #include #include #include 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)); }