More fitting examples
I have lost the original source of these notes, but they are
reproduced here for convenience, they give clear examples of how to
make userdefined functions of several parameters, fit over the full
range of the data, as well as display the fit over discrete subranges.
My apologies to the original author of these notes for not giving full
recognition.
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 userdefined 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 builtin 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 userdesigned 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");
Author:
Jenny Williams
Last modification: 13 June 2005
Last significant update: 30 August 2001 (page created)
