Joerg Marks
2 years ago
5 changed files with 5688 additions and 0 deletions
-
253notebooks/02_fit_ex_3_sol.ipynb
-
218notebooks/02_fit_ex_4_sol.ipynb
-
217notebooks/02_fit_ex_5_sol.ipynb
-
5000notebooks/FitTestData.txt
-
BINslides/CIPpoolAccess.PDF
@ -0,0 +1,253 @@ |
|||||
|
{ |
||||
|
"cells": [ |
||||
|
{ |
||||
|
"cell_type": "markdown", |
||||
|
"metadata": {}, |
||||
|
"source": [ |
||||
|
"Exercise 3: Least square fit with a 3rd order polynomial with iminuit" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"import numpy as np\n", |
||||
|
"import matplotlib.pyplot as plt\n", |
||||
|
"import math" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# Data x,y and dy\n", |
||||
|
"x = np.array([0.2 , 0.4 , 0.6 , 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2., 2.2, 2.4, 2.6, 2.8 , 3., 3.2 ,3.4, 3.6, 3.8,4.],dtype='d')\n", |
||||
|
"dy = np.array([0.04,0.021,0.035,0.03,0.029,0.019,0.024,0.018,0.019,0.022,0.02,0.025,0.018,0.024,0.019,0.021,0.03,0.019,0.03,0.024 ], dtype='d')\n", |
||||
|
"y = np.array([1.792,1.695,1.541,1.514,1.427,1.399,1.388,1.270,1.262,1.228,1.189,1.182,1.121,1.129,1.124,1.089,1.092,1.084,1.058,1.057 ], dtype='d')" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# Define fit functions - a 3rd order polynomial\n", |
||||
|
"def pol3(a0, a1, a2, a3):\n", |
||||
|
" return a0 + x*a1 + a2*x**2 + a3*x**3" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# least-squares function = sum of data residuals squared\n", |
||||
|
"def LSQ(a0, a1, a2, a3):\n", |
||||
|
" return np.sum((y - pol3(a0, a1, a2, a3)) ** 2 / dy ** 2)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# import minuit2 fitting library\n", |
||||
|
"from iminuit import Minuit" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# create instance of Minuit and use LSQ function to minimize\n", |
||||
|
"LSQ.errordef = Minuit.LEAST_SQUARES\n", |
||||
|
"m = Minuit(LSQ,a0=0.01, a1=0.05 ,a2=0.01 ,a3=0.001)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"m.params" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# run migrad for minimization\n", |
||||
|
"m.migrad()" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# run covariance \n", |
||||
|
"m.hesse()" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"#get correlation matrix\n", |
||||
|
"cov = m.covariance\n", |
||||
|
"print (cov)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# access elements of the numpy arrays\n", |
||||
|
"print(cov[0, 1])\n", |
||||
|
"print(cov[0, 2])" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# run minos error analysis\n", |
||||
|
"# The Minos algorithm uses the profile likelihood method to compute\n", |
||||
|
"# (generally asymmetric) confidence intervals.\n", |
||||
|
"m.minos()" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# Get a 2D contour of the function around the minimum for 2 parameters\n", |
||||
|
"# and draw a 2 D contours up to 4 sigma of a1 and a2 \n", |
||||
|
"m.draw_mncontour(\"a1\", \"a2\", cl=[1, 2, 3, 4])\n" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"m.draw_profile(\"a2\",subtract_min=True)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# access fit results by parameter name and get minos asymetric errors\n", |
||||
|
"print (m.merrors['a2'].lower)\n", |
||||
|
"print (m.merrors['a2'].upper)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# more print out\n", |
||||
|
"print (m.values,m.errors)\n", |
||||
|
"print (m.errors)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# Access fit results\n", |
||||
|
"a0_fit = m.values[\"a0\"]\n", |
||||
|
"a1_fit = m.values[\"a1\"]\n", |
||||
|
"a2_fit = m.values[\"a2\"]\n", |
||||
|
"a3_fit = m.values[\"a3\"]" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# display fitted function \n", |
||||
|
"x_plot = np.linspace( 0.1, 4.1 , 100 )\n", |
||||
|
"y_fit = a0_fit + a1_fit * x_plot + a2_fit * x_plot**2 + a3_fit * x_plot**3\n" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"plt.figure()\n", |
||||
|
"\n", |
||||
|
"plt.errorbar(x, y, dy , fmt=\"o\")\n", |
||||
|
"plt.plot(x_plot,y_fit ) \n", |
||||
|
"plt.xlabel('x')\n", |
||||
|
"plt.ylabel('f(x)')\n", |
||||
|
"plt.title('iminuit exponential Fit')\n", |
||||
|
"#plt.axis([0,30,-1.2,1.2])\n", |
||||
|
"\n", |
||||
|
"# show the plot\n", |
||||
|
"plt.show()" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [] |
||||
|
} |
||||
|
], |
||||
|
"metadata": { |
||||
|
"kernelspec": { |
||||
|
"display_name": "Python 3 (ipykernel)", |
||||
|
"language": "python", |
||||
|
"name": "python3" |
||||
|
}, |
||||
|
"language_info": { |
||||
|
"codemirror_mode": { |
||||
|
"name": "ipython", |
||||
|
"version": 3 |
||||
|
}, |
||||
|
"file_extension": ".py", |
||||
|
"mimetype": "text/x-python", |
||||
|
"name": "python", |
||||
|
"nbconvert_exporter": "python", |
||||
|
"pygments_lexer": "ipython3", |
||||
|
"version": "3.8.16" |
||||
|
} |
||||
|
}, |
||||
|
"nbformat": 4, |
||||
|
"nbformat_minor": 4 |
||||
|
} |
@ -0,0 +1,218 @@ |
|||||
|
{ |
||||
|
"cells": [ |
||||
|
{ |
||||
|
"cell_type": "markdown", |
||||
|
"metadata": {}, |
||||
|
"source": [ |
||||
|
"Exercise 4: Least square fit to data" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"from matplotlib import pyplot as plt\n", |
||||
|
"plt.rcParams[\"font.size\"] = 20\n", |
||||
|
"import numpy as np" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# data\n", |
||||
|
"x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype='d')\n", |
||||
|
"dx = np.array([0.1,0.1,0.5,0.1,0.5,0.1,0.5,0.1,0.5,0.1], dtype='d')\n", |
||||
|
"y = np.array([1.1 ,2.3 ,2.7 ,3.2 ,3.1 ,2.4 ,1.7 ,1.5 ,1.5 ,1.7 ], dtype='d')\n", |
||||
|
"dy = np.array([0.15,0.22,0.29,0.39,0.31,0.21,0.13,0.15,0.19,0.13], dtype='d')" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# define fit function \n", |
||||
|
"def pol3(a0, a1, a2, a3):\n", |
||||
|
" return a0 + x*a1 + a2*x**2 + a3*x**3" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# least-squares function = sum of data residuals squared\n", |
||||
|
"def LSQ(a0, a1, a2, a3):\n", |
||||
|
" return np.sum((y - pol3(a0, a1, a2, a3)) ** 2 / dy ** 2)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# import Minuit object\n", |
||||
|
"from iminuit import Minuit" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# create instance of Minuit and use LSQ function to minimize\n", |
||||
|
"LSQ.errordef = Minuit.LEAST_SQUARES\n", |
||||
|
"m = Minuit(LSQ,a0=-1.3, a1=2.6 ,a2=-0.24 ,a3=0.005)\n", |
||||
|
"# run migrad \n", |
||||
|
"m.migrad()" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# get function value at the minimum, which is per definition a chi2\n", |
||||
|
"# obtain chi2 / degree of freedom (dof)\n", |
||||
|
"chi2 = m.fval / (len(y) - len(m.values))\n", |
||||
|
"print (\"Chi2/ndof =\" , chi2)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# run covariance \n", |
||||
|
"m.hesse()" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"#get covariance matrix\n", |
||||
|
"m.covariance" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"#get correlation matrix in numpy array\n", |
||||
|
"cov = m.covariance\n", |
||||
|
"print (cov)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# run minos error analysis\n", |
||||
|
"# The Minos algorithm uses the profile likelihood method to compute\n", |
||||
|
"# (generally asymmetric) confidence intervals.\n", |
||||
|
"m.minos()" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# Get a 2D contour of the function around the minimum for 2 parameters\n", |
||||
|
"# and draw a 2 D contours up to 4 sigma of a1 and a2 \n", |
||||
|
"#m.draw_profile(\"a1\")\n", |
||||
|
"m.draw_mncontour(\"a2\", \"a3\", cl=[1, 2, 3, 4])" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"print(m.values,m.errors)\n", |
||||
|
"a0_fit = m.values[\"a0\"]\n", |
||||
|
"a1_fit = m.values[\"a1\"]\n", |
||||
|
"a2_fit = m.values[\"a2\"]\n", |
||||
|
"a3_fit = m.values[\"a3\"]" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# display fitted function \n", |
||||
|
"x_plot = np.linspace( 0.1, 10.1 , 200 )\n", |
||||
|
"y_fit = a0_fit + a1_fit * x_plot + a2_fit * x_plot**2 + a3_fit * x_plot**3" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# plot data \n", |
||||
|
"plt.figure()\n", |
||||
|
"plt.errorbar(x, y, dy , dx, fmt=\"o\")\n", |
||||
|
"plt.plot(x_plot,y_fit )\n", |
||||
|
"plt.title(\"iminuit Fit Test\")\n", |
||||
|
"plt.xlabel('x')\n", |
||||
|
"plt.ylabel('f(x)')\n", |
||||
|
"plt.xlim(-0.1, 10.1)\n", |
||||
|
"\n", |
||||
|
"# show the plot\n", |
||||
|
"plt.show()" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [] |
||||
|
} |
||||
|
], |
||||
|
"metadata": { |
||||
|
"kernelspec": { |
||||
|
"display_name": "Python 3 (ipykernel)", |
||||
|
"language": "python", |
||||
|
"name": "python3" |
||||
|
}, |
||||
|
"language_info": { |
||||
|
"codemirror_mode": { |
||||
|
"name": "ipython", |
||||
|
"version": 3 |
||||
|
}, |
||||
|
"file_extension": ".py", |
||||
|
"mimetype": "text/x-python", |
||||
|
"name": "python", |
||||
|
"nbconvert_exporter": "python", |
||||
|
"pygments_lexer": "ipython3", |
||||
|
"version": "3.8.16" |
||||
|
} |
||||
|
}, |
||||
|
"nbformat": 4, |
||||
|
"nbformat_minor": 4 |
||||
|
} |
@ -0,0 +1,217 @@ |
|||||
|
{ |
||||
|
"cells": [ |
||||
|
{ |
||||
|
"cell_type": "markdown", |
||||
|
"metadata": {}, |
||||
|
"source": [ |
||||
|
" Exercise 5: Fit Signal and background distribution of a histogramm " |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": 1, |
||||
|
"metadata": {}, |
||||
|
"outputs": [ |
||||
|
{ |
||||
|
"ename": "ImportError", |
||||
|
"evalue": "Failed to import libcppyy3_8. Please check that ROOT has been built for Python 3.8", |
||||
|
"output_type": "error", |
||||
|
"traceback": [ |
||||
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
||||
|
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", |
||||
|
"File \u001b[0;32m/cern/root/lib/cppyy/__init__.py:60\u001b[0m\n\u001b[1;32m 59\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 60\u001b[0m \u001b[43mimportlib\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mimport_module\u001b[49m\u001b[43m(\u001b[49m\u001b[43mlibcppyy_mod_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 61\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n", |
||||
|
"File \u001b[0;32m~/anaconda3/envs/myML/lib/python3.8/importlib/__init__.py:127\u001b[0m, in \u001b[0;36mimport_module\u001b[0;34m(name, package)\u001b[0m\n\u001b[1;32m 126\u001b[0m level \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m--> 127\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_bootstrap\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_gcd_import\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m[\u001b[49m\u001b[43mlevel\u001b[49m\u001b[43m:\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpackage\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlevel\u001b[49m\u001b[43m)\u001b[49m\n", |
||||
|
"File \u001b[0;32m<frozen importlib._bootstrap>:1014\u001b[0m, in \u001b[0;36m_gcd_import\u001b[0;34m(name, package, level)\u001b[0m\n", |
||||
|
"File \u001b[0;32m<frozen importlib._bootstrap>:991\u001b[0m, in \u001b[0;36m_find_and_load\u001b[0;34m(name, import_)\u001b[0m\n", |
||||
|
"File \u001b[0;32m<frozen importlib._bootstrap>:973\u001b[0m, in \u001b[0;36m_find_and_load_unlocked\u001b[0;34m(name, import_)\u001b[0m\n", |
||||
|
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'libcppyy3_8'", |
||||
|
"\nDuring handling of the above exception, another exception occurred:\n", |
||||
|
"\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", |
||||
|
"Cell \u001b[0;32mIn[1], line 5\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m path\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m#import ROOT\u001b[39;00m\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mROOT\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m TCanvas, TFile, TFormula, TH1D, TF1, TMinuit, TFitResult, TVirtualFitter\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mROOT\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m gROOT, gBenchmark, gRandom, gSystem\n", |
||||
|
"File \u001b[0;32m/cern/root/lib/ROOT/__init__.py:22\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[38;5;66;03m# Prevent cppyy from filtering ROOT libraries\u001b[39;00m\n\u001b[1;32m 20\u001b[0m environ[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mCPPYY_NO_ROOT_FILTER\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m1\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m---> 22\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mcppyy\u001b[39;00m\n\u001b[1;32m 23\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mROOTSYS\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m environ:\n\u001b[1;32m 24\u001b[0m \u001b[38;5;66;03m# Revert setting made by cppyy\u001b[39;00m\n\u001b[1;32m 25\u001b[0m cppyy\u001b[38;5;241m.\u001b[39mgbl\u001b[38;5;241m.\u001b[39mgROOT\u001b[38;5;241m.\u001b[39mSetBatch(\u001b[38;5;28;01mFalse\u001b[39;00m)\n", |
||||
|
"File \u001b[0;32m/cern/root/lib/cppyy/__init__.py:62\u001b[0m\n\u001b[1;32m 60\u001b[0m importlib\u001b[38;5;241m.\u001b[39mimport_module(libcppyy_mod_name)\n\u001b[1;32m 61\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n\u001b[0;32m---> 62\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m(\n\u001b[1;32m 63\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mFailed to import \u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m. Please check that ROOT has been built for Python \u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mformat(\n\u001b[1;32m 64\u001b[0m libcppyy_mod_name, major, minor))\n\u001b[1;32m 66\u001b[0m \u001b[38;5;66;03m# ensure 'import libcppyy' will find the versioned module\u001b[39;00m\n\u001b[1;32m 67\u001b[0m sys\u001b[38;5;241m.\u001b[39mmodules[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlibcppyy\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m sys\u001b[38;5;241m.\u001b[39mmodules[libcppyy_mod_name]\n", |
||||
|
"\u001b[0;31mImportError\u001b[0m: Failed to import libcppyy3_8. Please check that ROOT has been built for Python 3.8" |
||||
|
] |
||||
|
} |
||||
|
], |
||||
|
"source": [ |
||||
|
"import math\n", |
||||
|
"import numpy as np\n", |
||||
|
"from os import path\n", |
||||
|
"#import ROOT\n", |
||||
|
"from ROOT import TCanvas, TFile, TFormula, TH1D, TF1, TMinuit, TFitResult, TVirtualFitter\n", |
||||
|
"from ROOT import gROOT, gBenchmark, gRandom, gSystem" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"#read data from text file\n", |
||||
|
"data = np.genfromtxt('FitTestData.txt', dtype='d')\n", |
||||
|
"print(data)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# instanciate canvas and histogramm\n", |
||||
|
"c = TCanvas( 'c','Fit Test',200,10,700,500)\n", |
||||
|
"sig = TH1D( 'sig', 'Signal Mass', 100, 0. , 5. )" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# fill histogramm\n", |
||||
|
"for x in data:\n", |
||||
|
" sig.Fill(x)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# fit function: gaus + exponential\n", |
||||
|
"def myN(x, p):\n", |
||||
|
" return p[0] * np.exp(-0.5 * ((x[0]-p[1])/p[2])**2) + p[3] * np.exp( p[4]*x[0])" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# fit function: gaussian\n", |
||||
|
"def myGauss(x, p):\n", |
||||
|
" return p[0] * np.exp(-0.5 * ((x[0]-p[1])/p[2])**2) " |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# fit function: gaussian\n", |
||||
|
"def myExp(x, p):\n", |
||||
|
" return p[0] * np.exp( p[1]*x[0])" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# root formular mechanism\n", |
||||
|
"form = TFormula( 'form', '[0] * exp(-0.5 * ((x-[1])/[2])**2) + [3] * exp( [4]*x)')" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# define fit functions\n", |
||||
|
"#f = TF1('f','form', 0 , 5 , 5)\n", |
||||
|
"f = TF1('f',myN, 0 , 5 , 5)\n", |
||||
|
"f_exp = TF1('f_exp',myExp, 0.1 , 1.2 , 2)\n", |
||||
|
"f_gauss = TF1('f_exp',myGauss, 1.6 , 2.4 , 3)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# set start values of the fit\n", |
||||
|
"f.SetParameters(250.,2.,.2,5.5,-0.7)\n", |
||||
|
"f_gauss.SetParameters(250.,2.,.1)\n", |
||||
|
"f_gauss.SetLineColor(3)\n", |
||||
|
"f_exp.SetParameters(130.,-0.5)\n", |
||||
|
"f_exp.SetLineColor(4)" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# perform fit\n", |
||||
|
"# Options: Q/V Quiet/Verbose mode (default is between Q and V)\n", |
||||
|
"# E Perform better errors estimation using the Minos technique\n", |
||||
|
"# M Improve fit results\n", |
||||
|
"# R Use the range specified in the function range\n", |
||||
|
"# + Add this new fitted function to the list of fitted functions\n", |
||||
|
"fit = sig.Fit(f, \"V M E S\",\"\",0.,5.)\n", |
||||
|
"fit_exp = sig.Fit(f_exp, \"R+ E S\",\"\",0.1,1.2)\n", |
||||
|
"fit_gauss = sig.Fit(f_gauss, \"R+ E S\",\"\",1.6,2.4)\n", |
||||
|
"print (\"Fit results: mean=\",fit.Parameter(1),\" +/- \",fit.ParError(1), \n", |
||||
|
" \" sigma=\",fit.Parameter(2),\" +/- \",fit.ParError(2) )" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# print fit summary\n", |
||||
|
"fit.Print()" |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [ |
||||
|
"# drawing \n", |
||||
|
"c.Draw() " |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"cell_type": "code", |
||||
|
"execution_count": null, |
||||
|
"metadata": {}, |
||||
|
"outputs": [], |
||||
|
"source": [] |
||||
|
} |
||||
|
], |
||||
|
"metadata": { |
||||
|
"kernelspec": { |
||||
|
"display_name": "Python 3 (ipykernel)", |
||||
|
"language": "python", |
||||
|
"name": "python3" |
||||
|
}, |
||||
|
"language_info": { |
||||
|
"codemirror_mode": { |
||||
|
"name": "ipython", |
||||
|
"version": 3 |
||||
|
}, |
||||
|
"file_extension": ".py", |
||||
|
"mimetype": "text/x-python", |
||||
|
"name": "python", |
||||
|
"nbconvert_exporter": "python", |
||||
|
"pygments_lexer": "ipython3", |
||||
|
"version": "3.8.16" |
||||
|
} |
||||
|
}, |
||||
|
"nbformat": 4, |
||||
|
"nbformat_minor": 4 |
||||
|
} |
5000
notebooks/FitTestData.txt
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
Binary file not shown.
Write
Preview
Loading…
Cancel
Save
Reference in new issue