Functions
Check in the histogram-fitting page how to fit histograms with functions
How
to define a function with predefined components
TFormula
contains the list of predefined functions.
TF1
*f1 = new TF1("f1", "sin(x)", -5., 5.); //
built-in
TF1 *f1 = new TF1("f1",
"x*sin(x)", -2., 2.); //combined
TF1
*f1 = new TF1("f1", "[0]*x + [1] + gaus(2)", 0.,
15.); // gaus(2) means that parameter numbering starts at
2
f1->SetParameters(0.1, 10., 50., 0.5,
0.01); // need to set initial parameters for non-trivial
functions before fitting
TF2 *f2 = new
TF2("f2", "sin(x)*sin(y)", -10., 10., -10., 10.);
How
to define a user-defined function
In
a file myfunc.C define
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);
}
Load
this macro and define a function with it:
.L
myfunc.C
TF1 *f1 = new TF1("f1", myfunc, -1., 1., 3); //
where 3 is the number of parameters for the function
How
to draw a function
There are several specific
and general
options:
f1->Draw();
// default
f1->Draw("p");
// markers
f1->Draw("h");
// histogram
f2->Draw("surf1");
// For better colors do
gStyle->SetPalette(1) and plot again
Note
that you may need to set the markers to something sensible before
getting
output:
f1->SetMarkerStyle(20);
f1->SetMarkerSize(1.0);
f1->SetMarkerColor(kBlue);
It
may also be that you need to set the parameters to your function
before drawing them, e.g.
TF1
*f1 = new TF1("f1", "gaus");
f1->SetParameters(1000.,
0., 1.);
f1->Draw();