Histograms - Fitting



How to fit histograms with predefined functions?

How to fit with a user-defined function

double myfunc(double *x, double *par) {
double arg = 0;
if (par[2]) arg = (x[0] - par[1])/par[2];
return par[0]*TMath::Exp(-0.5*arg*arg);
}

.L myfunc.C
TF1 *f1 = new TF1("f1", myfunc, -1., 1., 3); // where 3 is the number of parameters for the function
f1->SetParameters(10., h1->GetMean(), h1->GetRMS());
h1->Fit("f1");



How to use the Fit Panel

Move the pointer over the histogram line (the cursor should change from cross to arrow) and click on the right mouse button. A context menu on TH1 should appear, from which you can choose FitPanel.






To restrict the fit range, carefully move the pointer onto the slider and adjust the range (watch the lines appearing on the canvas)