Machine Learning Kurs im Rahmen der Studierendentage im SS 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
9.7 KiB

2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. " Exercise 5: Fit Signal and background distribution of a histogramm "
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 1,
  13. "metadata": {},
  14. "outputs": [
  15. {
  16. "ename": "ImportError",
  17. "evalue": "Failed to import libcppyy3_8. Please check that ROOT has been built for Python 3.8",
  18. "output_type": "error",
  19. "traceback": [
  20. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  21. "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
  22. "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",
  23. "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",
  24. "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",
  25. "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",
  26. "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",
  27. "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'libcppyy3_8'",
  28. "\nDuring handling of the above exception, another exception occurred:\n",
  29. "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
  30. "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",
  31. "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",
  32. "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",
  33. "\u001b[0;31mImportError\u001b[0m: Failed to import libcppyy3_8. Please check that ROOT has been built for Python 3.8"
  34. ]
  35. }
  36. ],
  37. "source": [
  38. "import math\n",
  39. "import numpy as np\n",
  40. "from os import path\n",
  41. "#import ROOT\n",
  42. "from ROOT import TCanvas, TFile, TFormula, TH1D, TF1, TMinuit, TFitResult, TVirtualFitter\n",
  43. "from ROOT import gROOT, gBenchmark, gRandom, gSystem"
  44. ]
  45. },
  46. {
  47. "cell_type": "code",
  48. "execution_count": null,
  49. "metadata": {},
  50. "outputs": [],
  51. "source": [
  52. "#read data from text file\n",
  53. "data = np.genfromtxt('FitTestData.txt', dtype='d')\n",
  54. "print(data)"
  55. ]
  56. },
  57. {
  58. "cell_type": "code",
  59. "execution_count": null,
  60. "metadata": {},
  61. "outputs": [],
  62. "source": [
  63. "# instanciate canvas and histogramm\n",
  64. "c = TCanvas( 'c','Fit Test',200,10,700,500)\n",
  65. "sig = TH1D( 'sig', 'Signal Mass', 100, 0. , 5. )"
  66. ]
  67. },
  68. {
  69. "cell_type": "code",
  70. "execution_count": null,
  71. "metadata": {},
  72. "outputs": [],
  73. "source": [
  74. "# fill histogramm\n",
  75. "for x in data:\n",
  76. " sig.Fill(x)"
  77. ]
  78. },
  79. {
  80. "cell_type": "code",
  81. "execution_count": null,
  82. "metadata": {},
  83. "outputs": [],
  84. "source": [
  85. "# fit function: gaus + exponential\n",
  86. "def myN(x, p):\n",
  87. " return p[0] * np.exp(-0.5 * ((x[0]-p[1])/p[2])**2) + p[3] * np.exp( p[4]*x[0])"
  88. ]
  89. },
  90. {
  91. "cell_type": "code",
  92. "execution_count": null,
  93. "metadata": {},
  94. "outputs": [],
  95. "source": [
  96. "# fit function: gaussian\n",
  97. "def myGauss(x, p):\n",
  98. " return p[0] * np.exp(-0.5 * ((x[0]-p[1])/p[2])**2) "
  99. ]
  100. },
  101. {
  102. "cell_type": "code",
  103. "execution_count": null,
  104. "metadata": {},
  105. "outputs": [],
  106. "source": [
  107. "# fit function: gaussian\n",
  108. "def myExp(x, p):\n",
  109. " return p[0] * np.exp( p[1]*x[0])"
  110. ]
  111. },
  112. {
  113. "cell_type": "code",
  114. "execution_count": null,
  115. "metadata": {},
  116. "outputs": [],
  117. "source": [
  118. "# root formular mechanism\n",
  119. "form = TFormula( 'form', '[0] * exp(-0.5 * ((x-[1])/[2])**2) + [3] * exp( [4]*x)')"
  120. ]
  121. },
  122. {
  123. "cell_type": "code",
  124. "execution_count": null,
  125. "metadata": {},
  126. "outputs": [],
  127. "source": [
  128. "# define fit functions\n",
  129. "#f = TF1('f','form', 0 , 5 , 5)\n",
  130. "f = TF1('f',myN, 0 , 5 , 5)\n",
  131. "f_exp = TF1('f_exp',myExp, 0.1 , 1.2 , 2)\n",
  132. "f_gauss = TF1('f_exp',myGauss, 1.6 , 2.4 , 3)"
  133. ]
  134. },
  135. {
  136. "cell_type": "code",
  137. "execution_count": null,
  138. "metadata": {},
  139. "outputs": [],
  140. "source": [
  141. "# set start values of the fit\n",
  142. "f.SetParameters(250.,2.,.2,5.5,-0.7)\n",
  143. "f_gauss.SetParameters(250.,2.,.1)\n",
  144. "f_gauss.SetLineColor(3)\n",
  145. "f_exp.SetParameters(130.,-0.5)\n",
  146. "f_exp.SetLineColor(4)"
  147. ]
  148. },
  149. {
  150. "cell_type": "code",
  151. "execution_count": null,
  152. "metadata": {},
  153. "outputs": [],
  154. "source": [
  155. "# perform fit\n",
  156. "# Options: Q/V Quiet/Verbose mode (default is between Q and V)\n",
  157. "# E Perform better errors estimation using the Minos technique\n",
  158. "# M Improve fit results\n",
  159. "# R Use the range specified in the function range\n",
  160. "# + Add this new fitted function to the list of fitted functions\n",
  161. "fit = sig.Fit(f, \"V M E S\",\"\",0.,5.)\n",
  162. "fit_exp = sig.Fit(f_exp, \"R+ E S\",\"\",0.1,1.2)\n",
  163. "fit_gauss = sig.Fit(f_gauss, \"R+ E S\",\"\",1.6,2.4)\n",
  164. "print (\"Fit results: mean=\",fit.Parameter(1),\" +/- \",fit.ParError(1), \n",
  165. " \" sigma=\",fit.Parameter(2),\" +/- \",fit.ParError(2) )"
  166. ]
  167. },
  168. {
  169. "cell_type": "code",
  170. "execution_count": null,
  171. "metadata": {},
  172. "outputs": [],
  173. "source": [
  174. "# print fit summary\n",
  175. "fit.Print()"
  176. ]
  177. },
  178. {
  179. "cell_type": "code",
  180. "execution_count": null,
  181. "metadata": {},
  182. "outputs": [],
  183. "source": [
  184. "# drawing \n",
  185. "c.Draw() "
  186. ]
  187. },
  188. {
  189. "cell_type": "code",
  190. "execution_count": null,
  191. "metadata": {},
  192. "outputs": [],
  193. "source": []
  194. }
  195. ],
  196. "metadata": {
  197. "kernelspec": {
  198. "display_name": "Python 3 (ipykernel)",
  199. "language": "python",
  200. "name": "python3"
  201. },
  202. "language_info": {
  203. "codemirror_mode": {
  204. "name": "ipython",
  205. "version": 3
  206. },
  207. "file_extension": ".py",
  208. "mimetype": "text/x-python",
  209. "name": "python",
  210. "nbconvert_exporter": "python",
  211. "pygments_lexer": "ipython3",
  212. "version": "3.8.16"
  213. }
  214. },
  215. "nbformat": 4,
  216. "nbformat_minor": 4
  217. }