\n",
"This ROOTbook shows how to create a [histogram](https://root.cern.ch/doc/master/classTH1F.html), [fill it](https://root.cern.ch/doc/master/classTH1.html#a77e71290a82517d317ea8d05e96b6c4a) and [fit it](https://root.cern.ch/doc/master/classTH1.html#a7e7d34c91d5ebab4fc9bba3ca47dabdd).\n",
"\n",
"Let's start importing the [ROOT](https://root.cern.ch) module, which gives us access to all [ROOT](https://root.cern.ch) classes, and activate the [JSROOT](https://root.cern.ch/js/) visualisation."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"application/javascript": [
"IPython.CodeCell.config_defaults.highlight_modes['magic_text/x-c++src'] = {'reg':[/^%%cpp/]};"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to JupyROOT 6.07/07\n"
]
}
],
"source": [
"import ROOT"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%jsroot on"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we will create a [histogram](https://root.cern.ch/doc/master/classTH1F.html) specifying its title and axes titles. We'll also subsequently fill it with gaussian random numbers."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"h = ROOT.TH1F(\"myHisto\",\"My Histo;X axis;Y axis\",64, -4, 4)\n",
"h.FillRandom(\"gaus\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now setup our fit. ROOT provides a set of predefined functions, among which the Gaussian. You can have a look to the [TF1 class documentation](https://root.cern.ch/doc/master/classTF1.html) to learn more about how functions are implemented in ROOT. For example you will see that you can define functional forms with strings, lambda functions or other kind of functors.\n",
"\n",
"We specify the option \"S\" to extract the information relative to the fit in a separate object: we will inspect it later."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" FCN=47.4997 FROM MIGRAD STATUS=CONVERGED 53 CALLS 54 TOTAL\n",
" EDM=8.44224e-09 STRATEGY= 1 ERROR MATRIX ACCURATE \n",
" EXT PARAMETER STEP FIRST \n",
" NO. NAME VALUE ERROR SIZE DERIVATIVE \n",
" 1 Constant 2.46469e+02 4.31494e+00 1.19094e-02 -2.44811e-05\n",
" 2 Mean 1.04782e-02 1.43576e-02 4.87656e-05 -6.34020e-03\n",
" 3 Sigma 1.00315e+00 1.03818e-02 9.45504e-06 -2.70309e-02\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : created default TCanvas with name c1\n"
]
}
],
"source": [
"fitResultPtr = h.Fit(\"gaus\",\"S\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can see above the output of the fit, which converged. The value, uncertainty and other useful quantities are reported for each parameter.\n",
"\n",
"If more detail is needed, other information can be extracted, for example the final chi-square value or the number of free parameters:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The final Chi2 value was 47.50 and the number of free parameters was 3\n"
]
}
],
"source": [
"chi2_nparams = (fitResultPtr.Chi2(), fitResultPtr.NFreeParameters()) \n",
"print \"The final Chi2 value was %.2f and the number of free parameters was %d\" %chi2_nparams"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So far so good. Now we can draw our fitted histogram. We have two options. \n",
" 1. Create a new, fresh canvas and draw the histogram in it.\n",
" 2. Find back the canvas which was created (see the message above!) and draw it.\n",
"The fitted function is now associated of the histogram: no special operation is needed.\n",
"\n",
"Let's tackle this task following option 2. ROOT keeps track of all canvases and it is trivial to retrieve any of them by name."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"