{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Interactive Muon PDF Exploration\n",
    "\n",
    "_Author: Tamás Gál - tgal@km3net.de_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following example shows how to create an interactive muon PDF plot using `Jpp` and `jppy`.\n",
    "\n",
    "To install the dependencies run the following commands:\n",
    "\n",
    "    export JPP_DIR=/path/to/jpp\n",
    "    pip install jppy jupyterlab ipywidgets numpy matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jppy version: 3.1.1\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import ipywidgets as iw\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import jppy\n",
    "\n",
    "print(f\"jppy version: {jppy.version}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "muon_pdf = jppy.pdf.JMuonPDF(os.path.expandvars(\"$JPP_DIR/data/J%p.dat\"), TTS=0)\n",
    "xs = np.linspace(-20, 500, 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "fc0a3c0b976046dba5309849cf3edc2a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatLogSlider(value=10.0, continuous_update=False, description='E', max=5.0, min=1.0), …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "opts = dict(continuous_update=False)\n",
    "@iw.interact\n",
    "def show_pdf(E=iw.FloatLogSlider(min=1, max=5, step=0.1, value=3, base=10, **opts),\n",
    "             R=iw.IntSlider(min=1, max=200, step=1, value=50, **opts),\n",
    "             theta=iw.FloatSlider(min=0, max=np.pi, step=0.1, value=0, **opts),\n",
    "             phi=iw.FloatSlider(min=0, max=np.pi, step=0.1, value=0, **opts)):\n",
    "    pdf = lambda t_: muon_pdf.calculate(E, R, theta, phi, t_).f\n",
    "    ys = list(map(pdf, xs))\n",
    "    fig, ax = plt.subplots(figsize=(8,4))\n",
    "    ax.plot(xs, ys)\n",
    "    ax.set_xlabel('t / ns', fontsize=15)\n",
    "    ax.set_ylabel(r'$\\frac{dP}{dt}~/~($npe$~$ns$^{-1})$', fontsize=15)\n",
    "    ax.set_ylim((1e-8, 1e-2))\n",
    "    ax.set_yscale(\"log\")\n",
    "    plt.grid()\n",
    "    ax.set_title(r'$E = {:.0f}~GeV, R = {}~m, \\theta = {}, \\phi = {}$'.format(\n",
    "            E, R, theta, phi))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "km3net",
   "language": "python",
   "name": "km3net"
  },
  "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.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}