More fitting examples
Here are some examples of how to
make user-defined functions of several parameters, fit over the full
range of the data, as well as display the fit over discrete sub-ranges.
Fitting a saved histogram "myHist" with a gaussian fit:
myHist->Fit("gaus");
Fitting the histogram with a polynomial fit of degree N,
where in this example N=9:
myHist->Fit("pol9");
Create a user-defined function over a specific range and draw it:
f1 = new TF1("f1","abs(sin(x)/x)",0,10);
f1->Draw();
Declare an exponential function called "e1" to fit over the
subrange 0 to 200, and perform that fit:
TF1 *e1= new TF1("e1","expo",0,200);
myHist->Fit("e1","R");
Note that "R" in the above Fit command stands for
"range". If you leave out the "R" as an argument
in the Fit command, the range of the fit is the current range of the
histogram (in this case, the full range). To reset the range to what
you want, you would have to use the SetRange command. If,
in the above example, "R" was left out, to reset the range of
the fit "e1" as 0 to 200:
e1->SetRange(0,200);
Then you need to enter the fit command again, using the
"R" option:
myHist->Fit("e1","R");
To display two subranges of fits on one display using, for example,
the built-in Gaussian function:
TF1 *g1=new TF1("g1","gaus",-200.,200);
TF1 *g2=new TF1("g2","gaus",-200.,0);
myHist->Fit("g1","R");
myHist->Fit("g2","R+");
To create a user-designed function to use in a fit:
f2 = new TF1("f2","[0]/(x*x+[1]*[1])",-100,100);
Now set the initial values of the parameters:
f2->SetParameters(1000000,30);
...and fit the function to histogram "myHist"
myHist->Fit("f2","R");