SLAC PEP-II BABAR SLAC<->RAL
Workbook HEPIC Databases PDG HEP preprints
Organization Detector Computing Physics Documentation
Personnel Glossary Sitemap Search Hypernews
 Wkbk. Search Wkbk. Sitemap Introduction Non SLAC HOWTO's Introduction Logging In QuickTour Detector Info Resources Software Infrastructure CM2 Introduction Unix OO SRT Objectivity Event Store Framework Beta Modifying Code Writing and Editing Compiling Debugging Analysis Framework II Analysis Find Data Batch Processing PAW PAW II ROOT I ROOT II ROOT III Advanced Infrastructure New Releases Workdir Main Packages Event Displays Gen/Sim/Reco Contributing Software SRT and CVS Coding Advanced Topics Make CM2 Ntuples New Packages New Packages 2 Persistent Classes Java Site Installation
 Check this page for HTML 4.01 Transitional compliance with the W3C Validator (More checks...)

# 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 user-defined functions of several parameters, fit over the full range of the data, as well as display the fit over discrete sub-ranges. 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 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");
```

## Back to Workbook Section: ROOT II

Author: Jenny Williams

Last modification: 13 June 2005
Last significant update: 30 August 2001 (page created)