From 044cdcc3d64537e601b057334521da28a5eec24f Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Tue, 12 Sep 2023 15:50:39 +0200 Subject: [PATCH] MAINT: autofix syntax with `flake8` and `pyupgrade` (#9) * MAINT: sync Lecture 6 with Lecture 12 (solutions) --- .flake8 | 8 + .pre-commit-config.yaml | 3 + lecture02.ipynb | 1 + lecture06-gammap.ipynb | 359 ++++++++++++++++++++++--------- lecture06-nbarp.ipynb | 40 ++-- lecture09.ipynb | 20 +- lecture12a-gammap-solution.ipynb | 54 +++-- lecture12b-kinematics.ipynb | 35 ++- lecture12c-angles.ipynb | 36 ++-- lecture12d-phasespace.ipynb | 49 ++--- lecture12e-longitudinal.ipynb | 2 - requirements.txt | 2 +- 12 files changed, 374 insertions(+), 235 deletions(-) create mode 100644 .flake8 diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..ff41337 --- /dev/null +++ b/.flake8 @@ -0,0 +1,8 @@ +[flake8] +extend-ignore = + E402, + E501, + E722, + E741, + F404, + F841, diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e44e98e..491e468 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,7 +18,10 @@ repos: rev: 1.7.0 hooks: - id: nbqa-black + - id: nbqa-flake8 - id: nbqa-isort + - id: nbqa-pyupgrade + args: [--py37-plus] - repo: https://github.com/kynan/nbstripout rev: 0.6.1 diff --git a/lecture02.ipynb b/lecture02.ipynb index 9036d5f..cfd82d5 100644 --- a/lecture02.ipynb +++ b/lecture02.ipynb @@ -66,6 +66,7 @@ "\n", "import gdown\n", "import numpy as np\n", + "from IPython.display import display\n", "\n", "warnings.filterwarnings(\"ignore\")" ] diff --git a/lecture06-gammap.ipynb b/lecture06-gammap.ipynb index 8ade9dc..20cc314 100644 --- a/lecture06-gammap.ipynb +++ b/lecture06-gammap.ipynb @@ -37,39 +37,56 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "jupyter": { + "source_hidden": true + }, + "mystnb": { + "code_prompt_show": "Import Python libraries" + }, + "tags": [ + "hide-cell" + ] + }, "outputs": [], "source": [ + "import gdown\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", - "import pandas as pd" + "import pandas as pd\n", + "from pylorentz import Momentum4" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "jupyter": { - "source_hidden": true - }, "mystnb": { "code_prompt_show": "Download data" }, "tags": [ - "hide-input" + "hide-input", + "full-width" ] }, "outputs": [], "source": [ - "import gdown\n", - "\n", "output_path = gdown.cached_download(\n", " url=\"https://drive.google.com/uc?id=1qiYjPbR5nx3_Sw7MXuUKhNAUpkXPoxYh\",\n", " path=\"data/lecture6-gammap-data.csv\",\n", " md5=\"38cf5bf915318df756a21a82ad9e4afa\",\n", " quiet=True,\n", ")\n", - "data = pd.read_csv(output_path)" + "data = pd.read_csv(output_path)\n", + "data.columns = data.columns.str.strip()\n", + "data" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> The columns headers present some trailing blanks, that must be dropped to be able to use correctly the DataFormat structure (if not, they deliver an error message). To do so, the `str.strip()` function must be used beforehand to reformat the column shape." ] }, { @@ -80,24 +97,28 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { - "tags": [ - "full-width" - ] + "tags": [] + }, + "source": [ + "## Invariant mass distributions" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "tags": [] }, - "outputs": [], "source": [ - "data.head()" + "### Measured data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The columns headers present some trailing blanks, that must be dropped to be able to use correctly the DataFormat structure (if not, they deliver an error message). To do so, the *str.strip()* function must be used beforehand to reformat the column shape.\n", - "In the following commands in the cell, columns are shown, overall with the data.columns command, and per single variable (like *data.p2x*). If the format is correct, no error should appear." + "Evaluate the invariant masses (squared and linear) for particle pairs, in a similar way as done in the first example." ] }, { @@ -106,29 +127,108 @@ "metadata": {}, "outputs": [], "source": [ - "data.columns = data.columns.str.strip()\n", - "data.p2x" + "# Please write your solution here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Evaluate the invariant masses (squared and linear) for particle pairs, in a similar way as done in the first example." + "Let's plot the evaluated invariant masses. First, though, let's plot the antineutron momentum to see how the distribution looks like." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "source_hidden": true + }, + "tags": [ + "hide-input" + ] + }, + "outputs": [], + "source": [ + "# Please write your solution here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Let's plot the evaluated invariant masses. First, though, let's plot the antineutron momentum to see how the distribution looks like." + "Let's plot now the invariant masses distributions for the three pion pairs:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "source_hidden": true + }, + "tags": [ + "hide-input" + ] + }, + "outputs": [], + "source": [ + "# Please write your solution here" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "source_hidden": true + }, + "tags": [ + "hide-input" + ] + }, + "outputs": [], + "source": [ + "# Please write your solution here" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "source_hidden": true + }, + "tags": [ + "hide-input" + ] + }, + "outputs": [], + "source": [ + "# Please write your solution here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Let's plot now the invariant masses distributions for the three pion pairs:" + "You can superimpose the two last plots to check the differences in the lineshapes of the two distributions." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "source_hidden": true + }, + "tags": [ + "hide-input" + ] + }, + "outputs": [], + "source": [ + "# Please write your solution here" ] }, { @@ -138,6 +238,31 @@ "2D distributions: Dalitz plots" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "source_hidden": true + }, + "tags": [ + "hide-input" + ] + }, + "outputs": [], + "source": [ + "# Please write your solution here" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "### Monte Carlo data" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -172,6 +297,15 @@ "mc.columns = mc.columns.str.strip()" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Please write your solution here" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -179,6 +313,22 @@ "The phase space simulation was done extracting the photon momentum from the real data distribution." ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "source_hidden": true + }, + "tags": [ + "hide-input" + ] + }, + "outputs": [], + "source": [ + "# Please write your solution here" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -186,6 +336,22 @@ "Let's see the invariant masses histogram shapes using phase space Monte Carlo events." ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "source_hidden": true + }, + "tags": [ + "hide-input" + ] + }, + "outputs": [], + "source": [ + "# Please write your solution here" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -193,15 +359,27 @@ "And now the Dalitz plots:" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "source_hidden": true + }, + "tags": [ + "hide-input" + ] + }, + "outputs": [], + "source": [ + "# Please write your solution here" + ] + }, { "cell_type": "markdown", "metadata": {}, "source": [ - "\n", - "\n", - "---\n", - "\n", - "\n", + "## Missing mass distributions\n", "\n", "Now, let's take a look to the missing mass distribution (final - initial\n", "state). Let's start from the real data using the pylorentz package to build 4-vectors (first, work out *relativisticKinematics.ipynb* notebook)." @@ -213,47 +391,18 @@ "metadata": {}, "outputs": [], "source": [ - "import math\n", - "\n", - "from pylorentz import Momentum4\n", - "\n", "# final state\n", - "p1T = np.sqrt(data.p1x**2 + data.p1y**2)\n", - "p1mod = np.sqrt(p1T**2 + data.p1z**2)\n", - "eta1 = np.arcsinh(data.p1z / p1T)\n", - "phi1 = np.arctan2(data.p1y, data.p1x)\n", - "pionPlus = Momentum4.e_eta_phi_p(data.E1, eta1, phi1, p1mod)\n", - "\n", - "p2T = np.sqrt(data.p2x**2 + data.p2y**2)\n", - "p2mod = np.sqrt(p2T**2 + data.p2z**2)\n", - "eta2 = np.arcsinh(data.p2z / p2T)\n", - "phi2 = np.arctan2(data.p2y, data.p2x)\n", - "pionMinus = Momentum4.e_eta_phi_p(data.E2, eta2, phi2, p2mod)\n", - "\n", - "p3T = np.sqrt(data.p3x**2 + data.p3y**2)\n", - "p3mod = np.sqrt(p3T**2 + data.p3z**2)\n", - "eta3 = np.arcsinh(data.p3z / p3T)\n", - "phi3 = np.arctan2(data.p3y, data.p3x)\n", - "proton = Momentum4.e_eta_phi_p(data.E3, eta3, phi3, p3mod)\n", + "pip = Momentum4(data.E1, data.p1x, data.p1y, data.p1z)\n", + "pim = Momentum4(data.E2, data.p2x, data.p2y, data.p2z)\n", + "proton = Momentum4(data.E3, data.p3x, data.p3y, data.p3z)\n", "\n", "# initial state\n", - "# len is the number of events read by the csv file\n", - "len = len(data.pgamma)\n", - "pgamT = np.zeros(len)\n", - "pgammod = data.pgamma\n", - "etagam = 1.0e11 * np.ones(len)\n", - "phigam = np.zeros(len)\n", - "massGamma = 0.0\n", - "Egamma = np.sqrt(massGamma**2 + pgammod**2)\n", - "photon = Momentum4.e_eta_phi_p(Egamma, etagam, phigam, pgammod)\n", - "\n", - "# the target (proton) is at rest\n", - "massProton = 0.93827\n", - "ETgt = massProton * np.ones(len)\n", - "etaTgt = np.zeros(len)\n", - "phiTgt = np.zeros(len)\n", - "pTgt = np.zeros(len)\n", - "protonTarget = Momentum4.e_eta_phi_p(ETgt, etaTgt, phiTgt, pTgt)" + "m_proton = 0.93827\n", + "n_events = len(data)\n", + "zeros = np.zeros(n_events)\n", + "ones = np.ones(n_events)\n", + "gamma = Momentum4(data.pgamma, zeros, zeros, data.pgamma)\n", + "target = Momentum4(m_proton * ones, zeros, zeros, zeros)" ] }, { @@ -269,13 +418,13 @@ }, "outputs": [], "source": [ - "init = photon + protonTarget\n", - "final = pionPlus + pionMinus + proton\n", + "init = gamma + target\n", + "final = pip + pim + proton\n", "missingMomentum = final - init\n", "\n", "plt.hist(missingMomentum.m2, bins=200, color=\"palegreen\", range=(-0.02, 0.01))\n", - "plt.xlabel(\"Missing mass squared [$(\\mathrm{GeV}/c^2)^2$]\")\n", - "plt.ylabel(\"Entries/($5x10^{-5}\\;(\\mathrm{GeV}/c^2)^2$)\")\n", + "plt.xlabel(R\"Missing mass squared [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "plt.ylabel(R\"Entries/($5x10^{-5}\\;(\\mathrm{GeV}/c^2)^2$)\")\n", "plt.title(\"Missing momentum \\n\")\n", "plt.show()" ] @@ -284,7 +433,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The missing mass square is always negative: this means that the total energy of the initial state exceeds the measured energy of the final state, so there is (likely) a missing particle which carries away some energy. We know that the reaction occurred on a HD molecule as a target: this means that a recoiling neutron is present in all cases when the reaction occurred on a deuteron nucleus: $\\gamma p(n)\\rightarrow \\pi^+\\pi^- p (n)$. In this case, moreover, the hit proton is not at rest but it may have a momentum (called Fermi momentum) which, in the deuteron center-of-mass, is roughly distributed as a gaussian 50 MeV/c/ wide, with maximum at abouth 270 MeV/c. The missing mass momentum distribution shows the effect of the presence of a non-null Fermi momentum, and the possible contribution to the reaction kinematics of the spectator neutron.\n", + "The missing mass square is always negative: this means that the total energy of the initial state exceeds the measured energy of the final state, so there is (likely) a missing particle which carries away some energy. We know that the reaction occurred on a HD molecule as a target: this means that a recoiling neutron is present in all cases when the reaction occurred on a deuteron nucleus: $\\gamma p(n)\\rightarrow \\pi^+\\pi^- p (n)$. In this case, moreover, the hit proton is not at rest but it may have a momentum (called Fermi momentum) which, in the deuteron center-of-mass, is roughly distributed as a gaussian 50 MeV/c wide, with maximum at abouth 270 MeV/c. The missing mass momentum distribution shows the effect of the presence of a non-null Fermi momentum, and the possible contribution to the reaction kinematics of the spectator neutron.\n", "\n", "In the data selection procedure, the Fermi momentum was required not to exceed 100 MeV/c to preserve the condition that the neutron is a spectator in the reaction occurring on deuteron. Let's see the shape of the missing momentum distribution: " ] @@ -329,12 +478,12 @@ }, "outputs": [], "source": [ - "dipion = pionPlus + pionMinus\n", + "dipion = pip + pim\n", "missingMomentumDipion = init - dipion\n", "\n", "plt.hist(missingMomentumDipion.m2, bins=200, color=\"violet\", range=(0.0, 2))\n", - "plt.xlabel(\"Missing mass squared [$(\\mathrm{GeV}/c^2)^2$]\")\n", - "plt.ylabel(\"Entries/($0.01\\;(\\mathrm{GeV}/c^2)^2$)\")\n", + "plt.xlabel(R\"Missing mass squared [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "plt.ylabel(R\"Entries/($0.01\\;(\\mathrm{GeV}/c^2)^2$)\")\n", "plt.title(\"Missing momentum recoiling against the dipion\\n\")\n", "plt.show()" ] @@ -367,8 +516,8 @@ " range=[[0.80, 1.0], [-0.003, 0.001]],\n", " cmap=\"rainbow\",\n", ")\n", - "plt.xlabel(\"Dipion M.M. squared [$(\\mathrm{GeV}/c^2)^2$]\")\n", - "plt.ylabel(\"Reaction M.M. squared [$(\\mathrm{GeV}/c^2)^2$)\")\n", + "plt.xlabel(R\"Dipion M.M. squared [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "plt.ylabel(R\"Reaction M.M. squared [$(\\mathrm{GeV}/c^2)^2$)\")\n", "plt.show()" ] }, @@ -376,12 +525,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "---\n", - "\n", "## Let's do some fits\n", + "\n", "Which is the maximum of the missing momentum, and where is the peak of the\n", "missing mass distribution?\n", - "Let's attempt two fits with a single gaussian function.\n" + "Let's attempt two fits with a single gaussian function." ] }, { @@ -392,21 +540,20 @@ "source_hidden": true }, "tags": [ + "scroll-input", "hide-input" ] }, "outputs": [], "source": [ - "from numpy import exp\n", - "from scipy import stats\n", "from scipy.optimize import curve_fit\n", "\n", "# normalize the histogram to 1\n", - "lowEdge = 0.015\n", - "upEdge = 0.035\n", - "hist, bin_edges = np.histogram(missingMomentum.p, 100, range=(lowEdge, upEdge))\n", - "integralHist = sum(hist)\n", - "hist = hist / integralHist\n", + "low_edge = 0.015\n", + "up_edge = 0.035\n", + "hist, bin_edges = np.histogram(missingMomentum.p, 100, range=(low_edge, up_edge))\n", + "integral_hist = sum(hist)\n", + "hist = hist / integral_hist\n", "\n", "n = hist.size\n", "x_hist = np.zeros((n), dtype=float)\n", @@ -419,7 +566,7 @@ "\n", "\n", "def gaus(X, C, X_mean, sigma):\n", - " return C * exp(-((X - X_mean) ** 2) / (2 * sigma**2))\n", + " return C * np.exp(-((X - X_mean) ** 2) / (2 * sigma**2))\n", "\n", "\n", "mean = sum(x_hist * y_hist) / sum(y_hist)\n", @@ -486,9 +633,9 @@ "for ii in range(n):\n", " xf_hist[ii] = (bin_edgesf[ii + 1] + bin_edgesf[ii]) / 2\n", " # find the closest bin to edges\n", - " if bin_edgesf[ii + 1] <= lowEdge:\n", + " if bin_edgesf[ii + 1] <= low_edge:\n", " ilow = ii\n", - " if bin_edgesf[ii + 1] <= upEdge:\n", + " if bin_edgesf[ii + 1] <= up_edge:\n", " iup = ii\n", "\n", "\n", @@ -520,11 +667,11 @@ "outputs": [], "source": [ "# normalize the histogram to 1\n", - "lowEdge = -0.001\n", - "upEdge = 0.0\n", - "hist, bin_edges = np.histogram(missingMomentum.m2, 100, range=(lowEdge, upEdge))\n", - "integralHist = sum(hist)\n", - "hist = hist / integralHist\n", + "low_edge = -0.001\n", + "up_edge = 0.0\n", + "hist, bin_edges = np.histogram(missingMomentum.m2, 100, range=(low_edge, up_edge))\n", + "integral_hist = sum(hist)\n", + "hist = hist / integral_hist\n", "\n", "n = hist.size\n", "x_hist = np.zeros((n), dtype=float)\n", @@ -624,11 +771,11 @@ "outputs": [], "source": [ "# normalize the histogram to 1\n", - "lowEdge = -0.0007\n", - "upEdge = 0.0001\n", - "hist, bin_edges = np.histogram(missingMomentum.m2, 100, range=(lowEdge, upEdge))\n", - "integralHist = sum(hist)\n", - "hist = hist / integralHist\n", + "low_edge = -0.0007\n", + "up_edge = 0.0001\n", + "hist, bin_edges = np.histogram(missingMomentum.m2, 100, range=(low_edge, up_edge))\n", + "integral_hist = sum(hist)\n", + "hist = hist / integral_hist\n", "\n", "n = hist.size\n", "x_hist = np.zeros((n), dtype=float)\n", @@ -649,7 +796,7 @@ "\n", "\n", "def gausAndBCK(X, C, X_mean, sigma, P1, P2, P3, P4):\n", - " return C * exp(-((X - X_mean) ** 2) / (2 * sigma**2)) + (\n", + " return C * np.exp(-((X - X_mean) ** 2) / (2 * sigma**2)) + (\n", " P1 + P2 * X + P3 * X * X + P4 * X * X * X\n", " )\n", "\n", @@ -753,11 +900,11 @@ "outputs": [], "source": [ "# normalize the histogram to 1\n", - "lowEdge = 0.8\n", - "upEdge = 1.0\n", - "hist, bin_edges = np.histogram(missingMomentumDipion.m2, 100, range=(lowEdge, upEdge))\n", - "integralHist = sum(hist)\n", - "hist = hist / integralHist\n", + "low_edge = 0.8\n", + "up_edge = 1.0\n", + "hist, bin_edges = np.histogram(missingMomentumDipion.m2, 100, range=(low_edge, up_edge))\n", + "integral_hist = np.sum(hist)\n", + "hist = hist / integral_hist\n", "\n", "n = hist.size\n", "x_hist = np.zeros((n), dtype=float)\n", @@ -778,7 +925,7 @@ "\n", "\n", "def gausAndBCK(X, C, X_mean, sigma, P1, P2, P3, P4):\n", - " return C * exp(-((X - X_mean) ** 2) / (2 * sigma**2)) + (\n", + " return C * np.exp(-((X - X_mean) ** 2) / (2 * sigma**2)) + (\n", " P1 + P2 * X + P3 * X * X + P4 * X * X * X\n", " )\n", "\n", diff --git a/lecture06-nbarp.ipynb b/lecture06-nbarp.ipynb index 785038b..ad91663 100644 --- a/lecture06-nbarp.ipynb +++ b/lecture06-nbarp.ipynb @@ -223,7 +223,7 @@ "plt.hist(invariant_mass12, bins=100, range=(0.2, 1.8))\n", "plt.xlabel(\"Invariant mass [GeV]\")\n", "plt.ylabel(\"Entries/(16 MeV)\")\n", - "plt.title(\"The histogram of the invariant mass of $\\pi_1^+$ and $\\pi_2^+$ \\n\")\n", + "plt.title(R\"The histogram of the invariant mass of $\\pi_1^+$ and $\\pi_2^+$ \\n\")\n", "plt.show()" ] }, @@ -244,7 +244,7 @@ "plt.hist(invariant_mass13, bins=100, color=\"red\", range=(0.2, 1.8))\n", "plt.xlabel(\"Invariant mass [GeV]\")\n", "plt.ylabel(\"Entries/(16 MeV)\")\n", - "plt.title(\"The histogram of the invariant mass of $\\pi_1^+$ and $\\pi_3^-$ \\n\")\n", + "plt.title(\"The histogram of the invariant mass of $\\\\pi_1^+$ and $\\\\pi_3^-$\\n\")\n", "plt.show()" ] }, @@ -265,7 +265,7 @@ "plt.hist(invariant_mass23, bins=100, color=\"magenta\", range=(0.2, 1.8))\n", "plt.xlabel(\"Invariant mass [GeV]\")\n", "plt.ylabel(\"Entries/(16 MeV)\")\n", - "plt.title(\"The histogram of the invariant mass of $\\pi_2^+$ and $\\pi_3^-$ \\n\")\n", + "plt.title(\"The histogram of the invariant mass of $\\\\pi_2^+$ and $\\\\pi_3^-$\\n\")\n", "plt.show()" ] }, @@ -307,7 +307,7 @@ ")\n", "ax[0].set_xlabel(\"Invariant mass [GeV]\")\n", "ax[0].set_ylabel(\"Entries/(16 MeV)\")\n", - "ax[0].set_title(\"$(\\pi_{1,2}^+\\pi_3^-)$ i.m.\")\n", + "ax[0].set_title(R\"$(\\pi_{1,2}^+\\pi_3^-)$ i.m.\")\n", "ax[1].hist(\n", " im_merged,\n", " bins=100,\n", @@ -322,7 +322,7 @@ ")\n", "ax[1].set_xlabel(\"Invariant mass [GeV]\")\n", "ax[1].set_ylabel(\"Entries/(16 MeV)\")\n", - "ax[1].set_title(\"$(\\pi_{1,2}^+\\pi_3^-)$ i.m., density=True\")\n", + "ax[1].set_title(R\"$(\\pi_{1,2}^+\\pi_3^-)$ i.m., density=True\")\n", "ax[2].hist(\n", " im_merged,\n", " bins=100,\n", @@ -344,7 +344,7 @@ ")\n", "ax[2].set_xlabel(\"Invariant mass [GeV]\")\n", "ax[2].set_ylabel(\"Entries/(16 MeV)\")\n", - "ax[2].set_title(\"$(\\pi_{1,2}^+\\pi_3^-)$ i.m., density=False\")\n", + "ax[2].set_title(R\"$(\\pi_{1,2}^+\\pi_3^-)$ i.m., density=False\")\n", "plt.show()" ] }, @@ -372,12 +372,12 @@ "ax[0].hist2d(invariant_massSquared13, invariant_massSquared12, bins=100, cmap=\"jet\")\n", "ax[0].hist2d(invariant_massSquared23, invariant_massSquared12, bins=100, cmap=\"jet\")\n", "# ax[0].colorbar()\n", - "ax[0].set_xlabel(\"i.m.$^2(\\pi^+_{(1,2)}\\pi^-_{3}$) [GeV$^2$]\")\n", - "ax[0].set_ylabel(\"i.m.$^2(\\pi^+\\pi^+$) [GeV$^2$]\")\n", + "ax[0].set_xlabel(R\"i.m.$^2(\\pi^+_{(1,2)}\\pi^-_{3}$) [GeV$^2$]\")\n", + "ax[0].set_ylabel(R\"i.m.$^2(\\pi^+\\pi^+$) [GeV$^2$]\")\n", "ax[1].hist2d(invariant_massSquared13, invariant_massSquared23, bins=100, cmap=\"jet\")\n", "ax[1].hist2d(invariant_massSquared23, invariant_massSquared13, bins=100, cmap=\"jet\")\n", - "ax[1].set_xlabel(\"i.m.$^2(\\pi^+_1\\pi^-$) [GeV$^2$]\")\n", - "ax[1].set_ylabel(\"i.m.$^2(\\pi^+_2\\pi^-$) [GeV$^2$]\")\n", + "ax[1].set_xlabel(R\"i.m.$^2(\\pi^+_1\\pi^-$) [GeV$^2$]\")\n", + "ax[1].set_ylabel(R\"i.m.$^2(\\pi^+_2\\pi^-$) [GeV$^2$]\")\n", "plt.show()" ] }, @@ -458,12 +458,12 @@ "ax[0].hist2d(invariant_massSquared13mc, invariant_massSquared12mc, bins=100, cmap=\"jet\")\n", "ax[0].hist2d(invariant_massSquared23mc, invariant_massSquared12mc, bins=100, cmap=\"jet\")\n", "# ax[0].colorbar()\n", - "ax[0].set_xlabel(\"i.m.$^2(\\pi^+_{(1,2)}\\pi^-_{3}$) [GeV$^2$]\")\n", - "ax[0].set_ylabel(\"i.m.$^2(\\pi^+\\pi^+$) [GeV$^2$]\")\n", + "ax[0].set_xlabel(R\"i.m.$^2(\\pi^+_{(1,2)}\\pi^-_{3}$) [GeV$^2$]\")\n", + "ax[0].set_ylabel(R\"i.m.$^2(\\pi^+\\pi^+$) [GeV$^2$]\")\n", "ax[1].hist2d(invariant_massSquared13mc, invariant_massSquared23mc, bins=100, cmap=\"jet\")\n", "ax[1].hist2d(invariant_massSquared23mc, invariant_massSquared13mc, bins=100, cmap=\"jet\")\n", - "ax[1].set_xlabel(\"i.m.$^2(\\pi^+_1\\pi^-$) [GeV$^2$]\")\n", - "ax[1].set_ylabel(\"i.m.$^2(\\pi^+_2\\pi^-$) [GeV$^2$]\")\n", + "ax[1].set_xlabel(R\"i.m.$^2(\\pi^+_1\\pi^-$) [GeV$^2$]\")\n", + "ax[1].set_ylabel(R\"i.m.$^2(\\pi^+_2\\pi^-$) [GeV$^2$]\")\n", "plt.show()" ] }, @@ -518,7 +518,7 @@ "plt.colorbar()\n", "plt.xlabel(\"x\")\n", "plt.ylabel(\"y\")\n", - "plt.title(\"$\\pi^-_{3}$ vs $\\pi^+_{1}\\pi^+_2$ kin. energies DP\")\n", + "plt.title(R\"$\\pi^-_{3}$ vs $\\pi^+_{1}\\pi^+_2$ kin. energies DP\")\n", "plt.show()" ] }, @@ -559,18 +559,18 @@ "fig.colorbar(h0[3], ax=ax[0])\n", "ax[0].set_xlabel(\"$x_1$\")\n", "ax[0].set_ylabel(\"$y_1$\")\n", - "ax[0].set_title(\"$\\pi^+_{2}$ vs $\\pi^+_{1}\\pi^-_3$ kin. energies DP\")\n", + "ax[0].set_title(R\"$\\pi^+_{2}$ vs $\\pi^+_{1}\\pi^-_3$ kin. energies DP\")\n", "h1 = ax[1].hist2d(x2, y2, bins=100, cmap=\"rainbow\")\n", "fig.colorbar(h1[3], ax=ax[1])\n", "ax[1].set_xlabel(\"$x_2$\")\n", "ax[1].set_ylabel(\"$y_2$\")\n", - "ax[1].set_title(\"$\\pi^+_{1}$ vs $\\pi^+_{2}\\pi^-_3$ kin. energies DP\")\n", + "ax[1].set_title(R\"$\\pi^+_{1}$ vs $\\pi^+_{2}\\pi^-_3$ kin. energies DP\")\n", "# both combinations plotted on the same pad\n", "h2 = ax[2].hist2d(x2entries, y2entries, bins=100, cmap=\"rainbow\")\n", "fig.colorbar(h2[3], ax=ax[2])\n", "ax[2].set_xlabel(\"$x$\")\n", "ax[2].set_ylabel(\"$y$\")\n", - "ax[2].set_title(\"$\\pi^+_{1,2}$ vs $\\pi^+_{2,1}\\pi^-_3$ kin. energies DP\")\n", + "ax[2].set_title(R\"$\\pi^+_{1,2}$ vs $\\pi^+_{2,1}\\pi^-_3$ kin. energies DP\")\n", "plt.show()" ] }, @@ -616,12 +616,12 @@ "fig.colorbar(h0[3], ax=ax[0])\n", "ax[0].set_xlabel(\"$x_{mc}$\")\n", "ax[0].set_ylabel(\"$y_{mc}$\")\n", - "ax[0].set_title(\"$\\pi^-_3$ vs $\\pi^+_1\\pi^+_2$ kin. energies DP\")\n", + "ax[0].set_title(R\"$\\pi^-_3$ vs $\\pi^+_1\\pi^+_2$ kin. energies DP\")\n", "h1 = ax[1].hist2d(x2entriesmc, y2entriesmc, bins=100, cmap=\"rainbow\")\n", "fig.colorbar(h1[3], ax=ax[1])\n", "ax[1].set_xlabel(\"$x_{mc}$\")\n", "ax[1].set_ylabel(\"$y_{mc}$\")\n", - "ax[1].set_title(\"$\\pi^+_{1,2}$ vs $\\pi^+_{2,1}\\pi^-_3$ kin. energies DP\")\n", + "ax[1].set_title(R\"$\\pi^+_{1,2}$ vs $\\pi^+_{2,1}\\pi^-_3$ kin. energies DP\")\n", "plt.show()" ] } diff --git a/lecture09.ipynb b/lecture09.ipynb index 5ea5abf..df7cb6e 100644 --- a/lecture09.ipynb +++ b/lecture09.ipynb @@ -37,18 +37,12 @@ "outputs": [], "source": [ "import gdown\n", - "import matplotlib\n", - "import matplotlib.cm as cm\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", - "import pylab as py\n", "import scipy.optimize as opt\n", "import scipy.stats as stats\n", "from lmfit import Model\n", - "from lmfit.models import ExpressionModel\n", - "from matplotlib.colors import BoundaryNorm\n", "from matplotlib.ticker import MaxNLocator\n", - "from numpy import exp, loadtxt, pi, sqrt\n", "from scipy.stats import chi2, norm" ] }, @@ -1076,8 +1070,8 @@ "cbar = fig.colorbar(mesh)\n", "cbar.ax.set_ylabel(\"Log likelihood\")\n", "\n", - "plt.ylabel(\"$\\sigma$\")\n", - "plt.xlabel(\"$\\mu$\")\n", + "plt.ylabel(R\"$\\sigma$\")\n", + "plt.xlabel(R\"$\\mu$\")\n", "ax.grid()\n", "plt.show()" ] @@ -1483,11 +1477,11 @@ "fig.subplots_adjust(bottom=0.12)\n", "for k, ls in zip(k_values, linestyles):\n", " dist = chi2(k)\n", - " res = plt.plot(x, dist.pdf(x), ls=ls, c=\"black\", label=r\"$k=%i$\" % k)\n", + " res = plt.plot(x, dist.pdf(x), ls=ls, c=\"black\", label=f\"$k={k}$\")\n", "plt.xlim(0, 10)\n", "plt.ylim(0, 0.5)\n", "plt.xlabel(\"$x$\")\n", - "plt.title(r\"$\\chi^2\\ \\mathrm{Distribution}$\")\n", + "plt.title(R\"$\\chi^2\\ \\mathrm{Distribution}$\")\n", "plt.legend()\n", "plt.show()" ] @@ -2216,7 +2210,7 @@ " errcounts.append(np.sqrt(counts[i]))\n", " else:\n", " errcounts.append(10)\n", - "## Why do we do the above, i.e. setting zero values with large uncertainties?\n", + "# Why do we do the above, i.e. setting zero values with large uncertainties?\n", "xvals = []\n", "for i in range(len(xbins) - 1):\n", " xvals.append((xbins[i] + xbins[i + 1]) / 2)\n", @@ -2841,8 +2835,8 @@ }, "outputs": [], "source": [ - "## We can plot the Gaussian function on top of our histogram. We need to scale the Gaussian function\n", - "## to our bin width to properly vizualise it.\n", + "# We can plot the Gaussian function on top of our histogram. We need to scale the Gaussian function\n", + "# to our bin width to properly vizualise it.\n", "x = np.linspace(1.05, 1.20, num=1_000)\n", "\n", "\n", diff --git a/lecture12a-gammap-solution.ipynb b/lecture12a-gammap-solution.ipynb index 8c7cf6a..125cf16 100644 --- a/lecture12a-gammap-solution.ipynb +++ b/lecture12a-gammap-solution.ipynb @@ -201,7 +201,7 @@ "plt.hist(invariant_mass_12, bins=200, range=(0.2, 1.2))\n", "plt.xlabel(\"Invariant mass [GeV]\")\n", "plt.ylabel(\"Entries/(5 MeV)\")\n", - "plt.title(\"The histogram of the invariant mass of $\\pi^+$ and $\\pi^-$ \\n\")\n", + "plt.title(\"The histogram of the invariant mass of $\\\\pi^+$ and $\\\\pi^-$ \\n\")\n", "plt.show()" ] }, @@ -222,7 +222,7 @@ "plt.hist(invariant_mass_13, bins=200, color=\"tomato\", range=(1.0, 2.0))\n", "plt.xlabel(\"Invariant mass [GeV]\")\n", "plt.ylabel(\"Entries/(5 MeV)\")\n", - "plt.title(\"The histogram of the invariant mass of $\\pi^+$ and proton \\n\")\n", + "plt.title(\"The histogram of the invariant mass of $\\\\pi^+$ and proton \\n\")\n", "plt.show()" ] }, @@ -243,7 +243,7 @@ "plt.hist(invariant_mass_23, bins=200, color=\"darkmagenta\", range=(1.0, 2.0))\n", "plt.xlabel(\"Invariant mass [GeV]\")\n", "plt.ylabel(\"Entries/(5 MeV)\")\n", - "plt.title(\"The histogram of the invariant mass of $\\pi^-$ and proton \\n\")\n", + "plt.title(\"The histogram of the invariant mass of $\\\\pi^-$ and proton \\n\")\n", "plt.show()" ] }, @@ -302,8 +302,8 @@ " range=[[1.0, 3], [0.0, 0.8]],\n", " cmap=\"jet\",\n", ")\n", - "ax[0].set_xlabel(\"i.m.$^2(\\pi^+p$) [GeV$^2$]\")\n", - "ax[0].set_ylabel(\"i.m.$^2(\\pi^+\\pi^-$) [GeV$^2$]\")\n", + "ax[0].set_xlabel(R\"i.m.$^2(\\pi^+p$) [GeV$^2$]\")\n", + "ax[0].set_ylabel(R\"i.m.$^2(\\pi^+\\pi^-$) [GeV$^2$]\")\n", "ax[1].hist2d(\n", " inv_mass_squared_13,\n", " inv_mass_squared_23,\n", @@ -311,8 +311,8 @@ " range=[[1.0, 3], [1.0, 3.0]],\n", " cmap=\"jet\",\n", ")\n", - "ax[1].set_xlabel(\"i.m.$^2(\\pi^+p$) [GeV$^2$]\")\n", - "ax[1].set_ylabel(\"i.m.$^2(\\pi^-p$) [GeV$^2$]\")\n", + "ax[1].set_xlabel(R\"i.m.$^2(\\pi^+p$) [GeV$^2$]\")\n", + "ax[1].set_ylabel(R\"i.m.$^2(\\pi^-p$) [GeV$^2$]\")\n", "plt.show()" ] }, @@ -436,21 +436,21 @@ "\n", "# plot the histogram pi+pi-\n", "ax[0].hist(np.sqrt(inv_mass_squared_12_mc), bins=100, range=(0.2, 1.4))\n", - "ax[0].set_xlabel(\"($\\pi^+\\pi^-$) Invariant mass [GeV]\")\n", + "ax[0].set_xlabel(R\"($\\pi^+\\pi^-$) Invariant mass [GeV]\")\n", "ax[0].set_ylabel(\"Entries/(12 MeV)\")\n", - "ax[0].set_title(\"($\\pi^+\\pi^-$) i.m. \\n\")\n", + "ax[0].set_title(\"($\\\\pi^+\\\\pi^-$) i.m. \\n\")\n", "# plot the histogram pi+p\n", "ax[1].hist(np.sqrt(inv_mass_squared_13_mc), color=\"tomato\", bins=100, range=(1.0, 2.0))\n", - "ax[1].set_xlabel(\"($\\pi^+p$) Invariant mass [GeV]\")\n", + "ax[1].set_xlabel(R\"($\\pi^+p$) Invariant mass [GeV]\")\n", "ax[1].set_ylabel(\"Entries/(10 MeV)\")\n", - "ax[1].set_title(\"($\\pi^+p$) i.m. \\n\")\n", + "ax[1].set_title(\"($\\\\pi^+p$) i.m. \\n\")\n", "# plot the histogram pi-pi-\n", "ax[2].hist(\n", " np.sqrt(inv_mass_squared_23_mc), bins=100, color=\"darkmagenta\", range=(1.0, 2.0)\n", ")\n", - "ax[2].set_xlabel(\"($\\pi^-p$) Invariant mass [GeV]\")\n", + "ax[2].set_xlabel(R\"($\\pi^-p$) Invariant mass [GeV]\")\n", "ax[2].set_ylabel(\"Entries/(10 MeV)\")\n", - "ax[2].set_title(\"($\\pi^-p$) i.m. \\n\")\n", + "ax[2].set_title(\"($\\\\pi^-p$) i.m. \\n\")\n", "plt.show()" ] }, @@ -482,8 +482,8 @@ " range=[[1.0, 3], [0.0, 0.8]],\n", " cmap=\"jet\",\n", ")\n", - "ax[0].set_xlabel(\"i.m.$^2(\\pi^+p$) [GeV$^2$]\")\n", - "ax[0].set_ylabel(\"i.m.$^2(\\pi^+\\pi^-$) [GeV$^2$]\")\n", + "ax[0].set_xlabel(R\"i.m.$^2(\\pi^+p$) [GeV$^2$]\")\n", + "ax[0].set_ylabel(R\"i.m.$^2(\\pi^+\\pi^-$) [GeV$^2$]\")\n", "ax[1].hist2d(\n", " inv_mass_squared_13_mc,\n", " inv_mass_squared_23_mc,\n", @@ -491,8 +491,8 @@ " range=[[1.0, 3], [1.0, 3.0]],\n", " cmap=\"jet\",\n", ")\n", - "ax[1].set_xlabel(\"i.m.$^2(\\pi^+p$) [GeV$^2$]\")\n", - "ax[1].set_ylabel(\"i.m.$^2(\\pi^-p$) [GeV$^2$]\")\n", + "ax[1].set_xlabel(R\"i.m.$^2(\\pi^+p$) [GeV$^2$]\")\n", + "ax[1].set_ylabel(R\"i.m.$^2(\\pi^-p$) [GeV$^2$]\")\n", "plt.show()" ] }, @@ -544,8 +544,8 @@ "missingMomentum = final - init\n", "\n", "plt.hist(missingMomentum.m2, bins=200, color=\"palegreen\", range=(-0.02, 0.01))\n", - "plt.xlabel(\"Missing mass squared [$(\\mathrm{GeV}/c^2)^2$]\")\n", - "plt.ylabel(\"Entries/($5x10^{-5}\\;(\\mathrm{GeV}/c^2)^2$)\")\n", + "plt.xlabel(R\"Missing mass squared [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "plt.ylabel(R\"Entries/($5x10^{-5}\\;(\\mathrm{GeV}/c^2)^2$)\")\n", "plt.title(\"Missing momentum \\n\")\n", "plt.show()" ] @@ -603,8 +603,8 @@ "missingMomentumDipion = init - dipion\n", "\n", "plt.hist(missingMomentumDipion.m2, bins=200, color=\"violet\", range=(0.0, 2))\n", - "plt.xlabel(\"Missing mass squared [$(\\mathrm{GeV}/c^2)^2$]\")\n", - "plt.ylabel(\"Entries/($0.01\\;(\\mathrm{GeV}/c^2)^2$)\")\n", + "plt.xlabel(R\"Missing mass squared [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "plt.ylabel(R\"Entries/($0.01\\;(\\mathrm{GeV}/c^2)^2$)\")\n", "plt.title(\"Missing momentum recoiling against the dipion\\n\")\n", "plt.show()" ] @@ -637,8 +637,8 @@ " range=[[0.80, 1.0], [-0.003, 0.001]],\n", " cmap=\"rainbow\",\n", ")\n", - "plt.xlabel(\"Dipion M.M. squared [$(\\mathrm{GeV}/c^2)^2$]\")\n", - "plt.ylabel(\"Reaction M.M. squared [$(\\mathrm{GeV}/c^2)^2$)\")\n", + "plt.xlabel(R\"Dipion M.M. squared [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "plt.ylabel(R\"Reaction M.M. squared [$(\\mathrm{GeV}/c^2)^2$)\")\n", "plt.show()" ] }, @@ -667,8 +667,6 @@ }, "outputs": [], "source": [ - "from numpy import exp\n", - "from scipy import stats\n", "from scipy.optimize import curve_fit\n", "\n", "# normalize the histogram to 1\n", @@ -689,7 +687,7 @@ "\n", "\n", "def gaus(X, C, X_mean, sigma):\n", - " return C * exp(-((X - X_mean) ** 2) / (2 * sigma**2))\n", + " return C * np.exp(-((X - X_mean) ** 2) / (2 * sigma**2))\n", "\n", "\n", "mean = sum(x_hist * y_hist) / sum(y_hist)\n", @@ -919,7 +917,7 @@ "\n", "\n", "def gausAndBCK(X, C, X_mean, sigma, P1, P2, P3, P4):\n", - " return C * exp(-((X - X_mean) ** 2) / (2 * sigma**2)) + (\n", + " return C * np.exp(-((X - X_mean) ** 2) / (2 * sigma**2)) + (\n", " P1 + P2 * X + P3 * X * X + P4 * X * X * X\n", " )\n", "\n", @@ -1048,7 +1046,7 @@ "\n", "\n", "def gausAndBCK(X, C, X_mean, sigma, P1, P2, P3, P4):\n", - " return C * exp(-((X - X_mean) ** 2) / (2 * sigma**2)) + (\n", + " return C * np.exp(-((X - X_mean) ** 2) / (2 * sigma**2)) + (\n", " P1 + P2 * X + P3 * X * X + P4 * X * X * X\n", " )\n", "\n", diff --git a/lecture12b-kinematics.ipynb b/lecture12b-kinematics.ipynb index f34ff98..9558d78 100644 --- a/lecture12b-kinematics.ipynb +++ b/lecture12b-kinematics.ipynb @@ -195,13 +195,13 @@ "h0 = ax[0].hist2d(invariantMassSquared13, invariantMassSquared12, bins=100, cmap=\"jet\")\n", "h0 = ax[0].hist2d(invariantMassSquared23, invariantMassSquared12, bins=100, cmap=\"jet\")\n", "fig.colorbar(h0[3], ax=ax[0])\n", - "ax[0].set_xlabel(\"i.m.$^2(\\pi^+_{(1,2)}\\pi^-_{3}$) [GeV$^2$]\")\n", - "ax[0].set_ylabel(\"i.m.$^2(\\pi^+\\pi^+$) [GeV$^2$]\")\n", + "ax[0].set_xlabel(R\"i.m.$^2(\\pi^+_{(1,2)}\\pi^-_{3}$) [GeV$^2$]\")\n", + "ax[0].set_ylabel(R\"i.m.$^2(\\pi^+\\pi^+$) [GeV$^2$]\")\n", "h1 = ax[1].hist2d(invariantMassSquared13, invariantMassSquared23, bins=100, cmap=\"jet\")\n", "h1 = ax[1].hist2d(invariantMassSquared23, invariantMassSquared13, bins=100, cmap=\"jet\")\n", "fig.colorbar(h1[3], ax=ax[1])\n", - "ax[1].set_xlabel(\"i.m.$^2(\\pi^+_1\\pi^-$) [GeV$^2$]\")\n", - "ax[1].set_ylabel(\"i.m.$^2(\\pi^+_2\\pi^-$) [GeV$^2$]\")\n", + "ax[1].set_xlabel(R\"i.m.$^2(\\pi^+_1\\pi^-$) [GeV$^2$]\")\n", + "ax[1].set_ylabel(R\"i.m.$^2(\\pi^+_2\\pi^-$) [GeV$^2$]\")\n", "plt.show()" ] }, @@ -326,13 +326,13 @@ "source": [ "fig, ax = plt.subplots(2, 2, tight_layout=True, figsize=(9, 4))\n", "ax[0, 0].hist(t_lab_1, bins=100, color=\"Lime\")\n", - "ax[0, 0].set_xlabel(\"t = $(p_{nbar} - p_D)^2$ lab RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "ax[0, 0].set_xlabel(R\"t = $(p_{nbar} - p_D)^2$ lab RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", "ax[0, 1].hist(t_lab_2, bins=100, color=\"LimeGreen\")\n", - "ax[0, 1].set_xlabel(\"t = $(p_p - p_{\\pi^+_2})^2$ lab RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "ax[0, 1].set_xlabel(R\"t = $(p_p - p_{\\pi^+_2})^2$ lab RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", "ax[1, 0].hist(t_cm_1, bins=100, color=\"GreenYellow\")\n", - "ax[1, 0].set_xlabel(\"t = $(p_{nbar} - p_D)^2$ CM RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "ax[1, 0].set_xlabel(R\"t = $(p_{nbar} - p_D)^2$ CM RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", "ax[1, 1].hist(t_cm_2, bins=100, color=\"LawnGreen\")\n", - "ax[1, 1].set_xlabel(\"t = $(p_p - p_{\\pi^+_2})^2$ CM RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "ax[1, 1].set_xlabel(R\"t = $(p_p - p_{\\pi^+_2})^2$ CM RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", "plt.show()" ] }, @@ -371,13 +371,13 @@ "source": [ "fig, ax = plt.subplots(2, 2, tight_layout=True, figsize=(9, 4))\n", "ax[0, 0].hist(u_lab_1, bins=100, color=\"HotPink\")\n", - "ax[0, 0].set_xlabel(\"u = $(p_{nbar} - p_{\\pi^+_2})^2$ lab RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "ax[0, 0].set_xlabel(R\"u = $(p_{nbar} - p_{\\pi^+_2})^2$ lab RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", "ax[0, 1].hist(u_lab_2, bins=100, color=\"DeepPink\")\n", - "ax[0, 1].set_xlabel(\"u = $(p_p - p_D)^2$ lab RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "ax[0, 1].set_xlabel(R\"u = $(p_p - p_D)^2$ lab RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", "ax[1, 0].hist(u_cm_1, bins=100, color=\"Fuchsia\")\n", - "ax[1, 0].set_xlabel(\"u = $(p_{nbar} - p_{\\pi^+_2})^2$ CM RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "ax[1, 0].set_xlabel(R\"u = $(p_{nbar} - p_{\\pi^+_2})^2$ CM RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", "ax[1, 1].hist(u_cm_2, bins=100, color=\"DarkMagenta\")\n", - "ax[1, 1].set_xlabel(\"u = $(p_p - p_D)^2$ CM RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "ax[1, 1].set_xlabel(R\"u = $(p_p - p_D)^2$ CM RF [$(\\mathrm{GeV}/c^2)^2$]\")\n", "plt.show()" ] }, @@ -462,18 +462,18 @@ "source": [ "fig, ax = plt.subplots(1, 3, tight_layout=True, figsize=(11, 3.5))\n", "ax[0].hist(p4_missing.m2, bins=100, color=\"crimson\")\n", - "ax[0].set_xlabel(\"m.m. [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "ax[0].set_xlabel(R\"m.m. [$(\\mathrm{GeV}/c^2)^2$]\")\n", "# ptot vs Etot plot\n", "h1 = ax[1].hist2d(p4_final_state.e, p4_final_state.p, bins=100, cmap=\"rainbow\")\n", "fig.colorbar(h1[3], ax=ax[1])\n", - "ax[1].set_xlabel(\"$\\mathrm{E}_{tot}(2\\pi^+\\pi^-)$ [GeV]\")\n", - "ax[1].set_ylabel(\"$\\mathrm{p}_{tot}(2\\pi^+\\pi^-)$ [GeV/c]\")\n", + "ax[1].set_xlabel(R\"$\\mathrm{E}_{tot}(2\\pi^+\\pi^-)$ [GeV]\")\n", + "ax[1].set_ylabel(R\"$\\mathrm{p}_{tot}(2\\pi^+\\pi^-)$ [GeV/c]\")\n", "# missing mass recoiling against the neutral dipion (2 entries/event)\n", "all_missing_dipion = np.concatenate(\n", " [recoiling_missing_mass1.m2, recoiling_missing_mass2.m2]\n", ")\n", "missingHisto = ax[2].hist(all_missing_dipion, bins=100, color=\"darkorange\")\n", - "ax[2].set_xlabel(\"m.m. recoiling against $(\\pi^+\\pi^-)$ [$(\\mathrm{GeV}/c^2)^2$]\")\n", + "ax[2].set_xlabel(R\"m.m. recoiling against $(\\pi^+\\pi^-)$ [$(\\mathrm{GeV}/c^2)^2$]\")\n", "ax[2].xaxis.get_label().set_fontsize(8)\n", "ax[2].tick_params(axis=\"both\", which=\"major\", labelsize=8)\n", "plt.show()" @@ -497,7 +497,6 @@ }, "outputs": [], "source": [ - "import matplotlib.mlab as mlab\n", "from scipy.stats import norm\n", "\n", "# best fit of data\n", @@ -533,7 +532,7 @@ "source": [ "plt.xlabel(\"missing mass recoiling against the neutral dipion\")\n", "plt.ylabel(\"counts\")\n", - "plt.title(r\"$\\mu=%.3f,\\ \\sigma=%.3f$\" % (mu, sigma))\n", + "plt.title(Rf\"$\\mu={mu:.3f},\\ \\sigma={sigma:.3f}$\")\n", "plt.grid(True)\n", "plt.hist(\n", " all_missing_dipion, 100, density=True, facecolor=\"orange\", histtype=\"barstacked\"\n", diff --git a/lecture12c-angles.ipynb b/lecture12c-angles.ipynb index b3103cc..de29cc4 100644 --- a/lecture12c-angles.ipynb +++ b/lecture12c-angles.ipynb @@ -36,8 +36,6 @@ }, "outputs": [], "source": [ - "import math\n", - "\n", "import gdown\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", @@ -129,10 +127,9 @@ "Let us suppose that the two pions form an intermediate state (that can be resonant or not), which we identify as \"dipion\" D.\n", "Let's choose the axes directions in the center-of mass system as follows, being $\\mathbf{k}$ the photon momentum and $\\mathbf{q}$ the momentum of the dipion:\n", "\n", - "\n", - "* $\\mathbf{Z} = \\frac{\\mathbf{k}}{|\\mathbf{k}|}$\n", - "* $\\mathbf{Y} = \\frac{\\mathbf{k}\\times \\mathbf{q}}{|\\mathbf{k}\\times \\mathbf{q}|}$\n", - "* $\\mathbf{X} = \\frac{(\\mathbf{k}\\times \\mathbf{q})\\times \\mathbf{k}}{(|\\mathbf{k}\\times \\mathbf{q})\\times \\mathbf{k}|}$\n", + "* $\\mathbf{Z} = \\frac{\\mathbf{k}}{|\\mathbf{k}|}$\n", + "* $\\mathbf{Y} = \\frac{\\mathbf{k}\\times \\mathbf{q}}{|\\mathbf{k}\\times \\mathbf{q}|}$\n", + "* $\\mathbf{X} = \\frac{(\\mathbf{k}\\times \\mathbf{q})\\times \\mathbf{k}}{(|\\mathbf{k}\\times \\mathbf{q})\\times \\mathbf{k}|}$\n", "\n" ] }, @@ -159,12 +156,9 @@ "The decay angles $\\phi,\\; \\theta$ are defined as the polar and azimuthal angles of the unit vector $\\mathbf n$. In the present case (of a two-particle decay), it indicates the direction of flight of one of the two decay particles; for a three-particle decay, $\\mathbf n$ is the normal to the decay plane in the decaying particle rest frame.\n", "The angles are defines as follows:\n", "\n", - "\n", - "\n", - "\n", - "* $\\cos\\theta = \\mathbf{n\\cdot z}$\n", - "* $\\cos\\phi = \\frac{\\mathbf{y\\cdot(z\\times n)}}{|\\mathbf{z\\times n}|}$\n", - "* $\\sin\\phi = -\\frac{\\mathbf{x\\cdot(z\\times n)}} {|\\mathbf{z\\times n}|}$" + "* $\\cos\\theta = \\mathbf{n\\cdot z}$\n", + "* $\\cos\\phi = \\frac{\\mathbf{y\\cdot(z\\times n)}}{|\\mathbf{z\\times n}|}$\n", + "* $\\sin\\phi = -\\frac{\\mathbf{x\\cdot(z\\times n)}} {|\\mathbf{z\\times n}|}$" ] }, { @@ -224,13 +218,13 @@ "ax[0].set_xlabel(\"cos(theta)\")\n", "ax[0].set_ylabel(\"Entries/0.01\")\n", "ax[1].hist(cos_phi, color=\"lightskyblue\", bins=50)\n", - "ax[1].set_xlabel(\"$\\cos\\phi$\")\n", + "ax[1].set_xlabel(R\"$\\cos\\phi$\")\n", "ax[1].set_ylabel(\"Entries/0.01\")\n", "ax[2].hist(sin_phi, bins=50, color=\"slateblue\")\n", - "ax[2].set_xlabel(\"$\\sin\\phi$\")\n", + "ax[2].set_xlabel(R\"$\\sin\\phi$\")\n", "ax[2].set_ylabel(\"Entries/0.01\")\n", "ax[3].hist(phi_helicity, bins=50, color=\"darkorchid\")\n", - "ax[3].set_xlabel(\"$\\phi_{hel}$ [rad]\")\n", + "ax[3].set_xlabel(R\"$\\phi_\\mathrm{hel}$ [rad]\")\n", "ax[3].set_ylabel(\"Entries/(0.13 rad)\")\n", "plt.show()" ] @@ -285,13 +279,13 @@ "ax[0].set_xlabel(\"cos(theta)\")\n", "ax[0].set_ylabel(\"Entries/0.01\")\n", "ax[1].hist(cos_phi, color=\"lightskyblue\", bins=50, alpha=0.66)\n", - "ax[1].set_xlabel(\"$\\cos\\phi$\")\n", + "ax[1].set_xlabel(R\"$\\cos\\phi$\")\n", "ax[1].set_ylabel(\"Entries/0.01\")\n", "ax[2].hist(sin_phi, bins=50, color=\"slateblue\", alpha=0.66)\n", - "ax[2].set_xlabel(\"$\\sin\\phi$\")\n", + "ax[2].set_xlabel(R\"$\\sin\\phi$\")\n", "ax[2].set_ylabel(\"Entries/0.01\")\n", "ax[3].hist(phi_GJ, bins=50, color=\"darkorchid\", alpha=0.66)\n", - "ax[3].set_xlabel(\"$\\phi_{GJ}$ [rad]\")\n", + "ax[3].set_xlabel(R\"$\\phi_{GJ}$ [rad]\")\n", "ax[3].set_ylabel(\"Entries/(0.13 rad)\")\n", "plt.show()" ] @@ -346,13 +340,13 @@ "ax[0].set_xlabel(\"cos(theta)\")\n", "ax[0].set_ylabel(\"Entries/0.01\")\n", "ax[1].hist(cos_phi, color=\"lightskyblue\", bins=50, alpha=0.33)\n", - "ax[1].set_xlabel(\"$\\cos\\phi$\")\n", + "ax[1].set_xlabel(R\"$\\cos\\phi$\")\n", "ax[1].set_ylabel(\"Entries/0.01\")\n", "ax[2].hist(sin_phi, bins=50, color=\"slateblue\", alpha=0.33)\n", - "ax[2].set_xlabel(\"$\\sin\\phi$\")\n", + "ax[2].set_xlabel(R\"$\\sin\\phi$\")\n", "ax[2].set_ylabel(\"Entries/0.01\")\n", "ax[3].hist(phi_adair, bins=50, color=\"darkorchid\", alpha=0.33)\n", - "ax[3].set_xlabel(\"$\\phi_{Adair}$ [rad]\")\n", + "ax[3].set_xlabel(R\"$\\phi_{Adair}$ [rad]\")\n", "ax[3].set_ylabel(\"Entries/(0.13 rad)\")\n", "plt.show()" ] diff --git a/lecture12d-phasespace.ipynb b/lecture12d-phasespace.ipynb index 78c9ac9..37c03fe 100644 --- a/lecture12d-phasespace.ipynb +++ b/lecture12d-phasespace.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Prepare the notebook by importing [`pandas`](https://pandas.pydata.org), [`numpy`](https://numpy.org), [`matplotlib.pyplot`](https://matplotlib.org/3.5.3/api/_as_gen/matplotlib.pyplot.html), and [`pylorentz`](https://github.com/PyLorentz/PyLorentz) and download the data file (CSV format) from Google Drive." + "Prepare the notebook by importing [`numpy`](https://numpy.org), [`matplotlib.pyplot`](https://matplotlib.org/3.5.3/api/_as_gen/matplotlib.pyplot.html), and [`pylorentz`](https://github.com/PyLorentz/PyLorentz) and download the data file (CSV format) from Google Drive." ] }, { @@ -31,7 +31,7 @@ }, "outputs": [], "source": [ - "%pip install -q gdown matplotlib numpy pandas phasespace pylorentz" + "%pip install -q gdown matplotlib numpy phasespace pylorentz" ] }, { @@ -68,11 +68,8 @@ }, "outputs": [], "source": [ - "import math\n", - "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", - "import pandas as pd\n", "import phasespace\n", "import tensorflow as tf\n", "from pylorentz import Momentum4" @@ -192,7 +189,7 @@ "source": [ "fig, ax = plt.subplots(1, 4, tight_layout=True, figsize=(11, 3.5))\n", "ax[0].hist(B0.m, bins=100, color=\"CornFlowerBlue\", range=(5279, 5280))\n", - "ax[0].set_xlabel(\"i.m.($\\pi$K) [MeV/$c^2$]\")\n", + "ax[0].set_xlabel(R\"i.m.($\\pi$K) [MeV/$c^2$]\")\n", "ax[0].set_title(\"(pion-kaon) i.m. \\n\")\n", "\n", "ax[1].hist(kaon.p_x, bins=70, color=\"lightcoral\", hatch=\"//\")\n", @@ -298,18 +295,18 @@ "fig, ax = plt.subplots(1, 3, tight_layout=True, figsize=(12, 3.5))\n", "f0 = ax[0].hist2d(s1, s3, bins=70, cmap=\"turbo\", cmin=1)\n", "fig.colorbar(f0[3], ax=ax[0])\n", - "ax[0].set_xlabel(\"i.m.$^2(\\pi^-K^+)$ [(MeV/$c^2)^2]$\")\n", - "ax[0].set_ylabel(\"i.m.$^2(\\pi^-\\pi^0)$ [(MeV/$c^2)^2]$\")\n", + "ax[0].set_xlabel(R\"i.m.$^2(\\pi^-K^+)$ [(MeV/$c^2)^2]$\")\n", + "ax[0].set_ylabel(R\"i.m.$^2(\\pi^-\\pi^0)$ [(MeV/$c^2)^2]$\")\n", "\n", "f1 = ax[1].hist2d(s2, s3, bins=70, cmap=\"turbo\", cmin=1)\n", "fig.colorbar(f1[3], ax=ax[1])\n", - "ax[1].set_xlabel(\"i.m.$^2(\\pi^0K^+)$ [(MeV/$c^2)^2]$\")\n", - "ax[1].set_ylabel(\"i.m.$^2(\\pi^-\\pi^0)$ [(MeV/$c^2)^2]$\")\n", + "ax[1].set_xlabel(R\"i.m.$^2(\\pi^0K^+)$ [(MeV/$c^2)^2]$\")\n", + "ax[1].set_ylabel(R\"i.m.$^2(\\pi^-\\pi^0)$ [(MeV/$c^2)^2]$\")\n", "\n", "f2 = ax[2].hist2d(s1, s2, bins=70, cmap=\"turbo\", cmin=1)\n", "fig.colorbar(f2[3], ax=ax[2])\n", - "ax[2].set_xlabel(\"i.m.$^2(\\pi^-K^+)$ [(MeV/$c^2)^2]$\")\n", - "ax[2].set_ylabel(\"i.m.$^2(\\pi^0K^+)$ [(MeV/$c^2)^2]$\")\n", + "ax[2].set_xlabel(R\"i.m.$^2(\\pi^-K^+)$ [(MeV/$c^2)^2]$\")\n", + "ax[2].set_ylabel(R\"i.m.$^2(\\pi^0K^+)$ [(MeV/$c^2)^2]$\")\n", "plt.show()" ] }, @@ -407,18 +404,18 @@ "\n", "f0 = ax[0].hist2d(s1, s2, bins=70, cmap=\"turbo\")\n", "fig.colorbar(f0[3], ax=ax[0])\n", - "ax[0].set_xlabel(\"i.m.$^2(\\pi^-K^+)$ [(MeV/$c^2)^2]$\")\n", - "ax[0].set_ylabel(\"i.m.$^2(K^+\\gamma)$ [(MeV/$c^2)^2]$\")\n", + "ax[0].set_xlabel(R\"i.m.$^2(\\pi^-K^+)$ [(MeV/$c^2)^2]$\")\n", + "ax[0].set_ylabel(R\"i.m.$^2(K^+\\gamma)$ [(MeV/$c^2)^2]$\")\n", "\n", "f1 = ax[1].hist2d(s2, s3, bins=70, cmap=\"turbo\")\n", "fig.colorbar(f1[3], ax=ax[1])\n", - "ax[1].set_xlabel(\"i.m.$^2(K^+\\gamma)$ [(MeV/$c^2)^2]$\")\n", - "ax[1].set_ylabel(\"i.m.$^2(\\pi^-\\gamma)$ [(MeV/$c^2)^2]$\")\n", + "ax[1].set_xlabel(R\"i.m.$^2(K^+\\gamma)$ [(MeV/$c^2)^2]$\")\n", + "ax[1].set_ylabel(R\"i.m.$^2(\\pi^-\\gamma)$ [(MeV/$c^2)^2]$\")\n", "\n", "f2 = ax[2].hist2d(s1, s3, bins=70, cmap=\"turbo\")\n", "fig.colorbar(f2[3], ax=ax[2])\n", - "ax[2].set_xlabel(\"i.m.$^2(\\pi^-K^+)$ [(MeV/$c^2)^2]$\")\n", - "ax[2].set_ylabel(\"i.m.$^2(\\pi^-\\gamma)$ [(MeV/$c^2)^2]$\")\n", + "ax[2].set_xlabel(R\"i.m.$^2(\\pi^-K^+)$ [(MeV/$c^2)^2]$\")\n", + "ax[2].set_ylabel(R\"i.m.$^2(\\pi^-\\gamma)$ [(MeV/$c^2)^2]$\")\n", "plt.show()" ] }, @@ -459,8 +456,8 @@ " min_mass = tf.cast(min_mass, tf.float64)\n", " max_mass = tf.cast(max_mass, tf.float64)\n", " kstar_mass_cast = tf.cast(K0STAR_MASS, dtype=tf.float64)\n", - " kstar_width_cast = tf.cast(K0STAR_WIDTH, tf.float64)\n", - " kstar_mass = tf.broadcast_to(kstar_mass_cast, shape=(n_events,))\n", + " tf.cast(K0STAR_WIDTH, tf.float64)\n", + " tf.broadcast_to(kstar_mass_cast, shape=(n_events,))\n", " return tfp.distributions.TruncatedNormal(\n", " loc=K0STAR_MASS,\n", " scale=K0STAR_WIDTH,\n", @@ -527,18 +524,18 @@ "fig, ax = plt.subplots(1, 3, tight_layout=True, figsize=(12, 3.5))\n", "f0 = ax[0].hist2d(s1, s2, bins=70, cmap=\"rainbow\")\n", "fig.colorbar(f0[3], ax=ax[0])\n", - "ax[0].set_xlabel(\"i.m.$^2(\\pi^-K^+)$ [(MeV/$c^2)^2]$\")\n", - "ax[0].set_ylabel(\"i.m.$^2(K^+\\gamma)$ [(MeV/$c^2)^2]$\")\n", + "ax[0].set_xlabel(R\"i.m.$^2(\\pi^-K^+)$ [(MeV/$c^2)^2]$\")\n", + "ax[0].set_ylabel(R\"i.m.$^2(K^+\\gamma)$ [(MeV/$c^2)^2]$\")\n", "\n", "f1 = ax[1].hist2d(s2, s3, bins=70, cmap=\"rainbow\")\n", "fig.colorbar(f1[3], ax=ax[1])\n", - "ax[1].set_xlabel(\"i.m.$^2(K^+\\gamma)$ [(MeV/$c^2)^2]$\")\n", - "ax[1].set_ylabel(\"i.m.$^2(\\pi^-\\gamma)$ [(MeV/$c^2)^2]$\")\n", + "ax[1].set_xlabel(R\"i.m.$^2(K^+\\gamma)$ [(MeV/$c^2)^2]$\")\n", + "ax[1].set_ylabel(R\"i.m.$^2(\\pi^-\\gamma)$ [(MeV/$c^2)^2]$\")\n", "\n", "f2 = ax[2].hist2d(s1, s3, bins=70, cmap=\"rainbow\")\n", "fig.colorbar(f2[3], ax=ax[2])\n", - "ax[2].set_xlabel(\"i.m.$^2(\\pi^-K^+)$ [(MeV/$c^2)^2]$\")\n", - "ax[2].set_ylabel(\"i.m.$^2(\\pi^-\\gamma)$ [(MeV/$c^2)^2]$\")\n", + "ax[2].set_xlabel(R\"i.m.$^2(\\pi^-K^+)$ [(MeV/$c^2)^2]$\")\n", + "ax[2].set_ylabel(R\"i.m.$^2(\\pi^-\\gamma)$ [(MeV/$c^2)^2]$\")\n", "plt.show()" ] } diff --git a/lecture12e-longitudinal.ipynb b/lecture12e-longitudinal.ipynb index f777e88..90cf4c2 100644 --- a/lecture12e-longitudinal.ipynb +++ b/lecture12e-longitudinal.ipynb @@ -44,8 +44,6 @@ }, "outputs": [], "source": [ - "import math\n", - "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", diff --git a/requirements.txt b/requirements.txt index ac207ac..b826a73 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,4 @@ jupyterlab-code-formatter jupyterlab-lsp nbmake pre-commit -python-lsp-server[rope] +python-lsp-server[flake8,rope]