{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import uproot\n",
    "import numpy as np\n",
    "import sys\n",
    "import os\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits import mplot3d\n",
    "import itertools\n",
    "import awkward as ak\n",
    "import seaborn as sns\n",
    "from scipy.optimize import curve_fit\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "file = uproot.open(\"tracking_losses_ntuple_Bd2KstEE.root:PrDebugTrackingLosses.PrDebugTrackingTool/Tuple;1\")\n",
    "#file = uproot.open(\"tracking_losses_ntuple_Dst0ToD0EE.root:PrDebugTrackingLosses.PrDebugTrackingTool/Tuple;1\")\n",
    "\n",
    "#look at particles only from Signal\n",
    "allcolumns = file.arrays()\n",
    "tracked = allcolumns[(allcolumns.isElectron) & (~allcolumns.lost) & (allcolumns.fromSignal)]\n",
    "lost = allcolumns[(allcolumns.isElectron) & (allcolumns.lost) & (allcolumns.fromSignal)] \n",
    "\n",
    "#ak.num(tracked, axis=0)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#lost\n",
    "l_eph = lost[\"brem_photons_pe\"]\n",
    "ak.nan_to_num(l_eph)\n",
    "l_pT = lost[\"pt\"]\n",
    "l_sci_x = lost[\"scifi_hit_pos_x\"]\n",
    "ak.nan_to_num(l_sci_x)\n",
    "\n",
    "#found\n",
    "f_eph = tracked[\"brem_photons_pe\"]\n",
    "ak.nan_to_num(f_eph)\n",
    "f_pT = tracked[\"pt\"]\n",
    "f_sci_x = tracked[\"scifi_hit_pos_x\"]\n",
    "ak.nan_to_num(f_sci_x)\n",
    "\n",
    "l_sci_x, l_pT = ak.broadcast_arrays(l_sci_x, l_pT)\n",
    "f_sci_x, f_pT = ak.broadcast_arrays(f_sci_x, f_pT)\n",
    "\n",
    "l_sci_x = ak.to_numpy(ak.flatten(l_sci_x))\n",
    "l_pT = ak.to_numpy(ak.flatten(l_pT))\n",
    "f_sci_x = ak.to_numpy(ak.flatten(f_sci_x))\n",
    "f_pT = ak.to_numpy(ak.flatten(f_pT))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 115176 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ((ax0, ax1)) = plt.subplots(nrows=1, ncols=2, figsize=(20,6))\n",
    "\n",
    "a0=ax0.hist2d(l_sci_x, l_pT, bins=200,  cmap=plt.cm.jet, cmin=0)\n",
    "ax0.set_xlabel(\"scifi x\")\n",
    "ax0.set_ylabel(r\"$p_T$\")\n",
    "ax0.set_title(\"lost electron positions in the scifi in regard to their transverse momentum\")\n",
    "plt.colorbar(a0[3],ax=ax0)\n",
    "\n",
    "a1=ax1.hist2d(f_sci_x, f_pT, bins=200,  cmap=plt.cm.jet, cmin=0)\n",
    "ax1.set_xlabel(\"scifi x\")\n",
    "ax1.set_ylabel(r\"$p_T$\")\n",
    "ax1.set_title(\"found electron positions in the scifi in regard to their transverse momentum\")\n",
    "plt.colorbar(a1[3],ax=ax1)\n",
    "\n",
    "\"\"\"\n",
    "B:\n",
    "we can see that the lost electrons cover a wider spread in the x direction of the scifi tracker,\n",
    "while most of those have low pT \n",
    "\n",
    "\"\"\"\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4.09e+04], [8.66e+03], [8.07e+04], ..., [5.63e+03], [6.29e+03], [2.26e+03]]\n",
      "[4.62e+04, 9.36e+03, 1.34e+05, 5.63e+04, ..., 2.01e+04, 6.94e+03, 7.83e+03]\n",
      "8657.132\n",
      "9355.866625028413\n"
     ]
    }
   ],
   "source": [
    "energy_found = tracked[\"energy\"]\n",
    "energy_found = energy_found[tracked[\"brem_photons_pe_length\"]!=0]\n",
    "#ak.nan_to_num(energy_found)\n",
    "\n",
    "e_ph_found = tracked[\"brem_photons_pe\"]\n",
    "e_ph_found = e_ph_found[tracked[\"brem_photons_pe_length\"]!=0]\n",
    "#ak.nan_to_num(e_ph_found, nan=[0])\n",
    "e_ph_found = ak.sum(e_ph_found, axis=-1, keepdims=True)\n",
    "print(e_ph_found)\n",
    "print(energy_found)\n",
    "\n",
    "energy_lost = lost[\"energy\"]\n",
    "energy_lost = energy_lost[lost[\"brem_photons_pe_length\"]!=0]\n",
    "#ak.nan_to_num(energy_lost)\n",
    "\n",
    "e_ph_lost = lost[\"brem_photons_pe\"]\n",
    "e_ph_lost = e_ph_lost[lost[\"brem_photons_pe_length\"]!=0]\n",
    "#ak.nan_to_num(e_ph_lost)\n",
    "e_ph_lost = ak.sum(e_ph_lost, axis=-1,keepdims=True)\n",
    "\n",
    "#e_ph_found, energy_found = ak.broadcast_arrays(e_ph_found, energy_found)\n",
    "#e_ph_lost, energy_lost = ak.broadcast_arrays(e_ph_lost, energy_lost)\n",
    "\n",
    "e_ph_found = ak.to_numpy(ak.flatten(e_ph_found))\n",
    "energy_found = ak.to_numpy(energy_found)\n",
    "\n",
    "e_ph_lost = ak.to_numpy(ak.flatten(e_ph_lost))\n",
    "energy_lost = ak.to_numpy(energy_lost)\n",
    "\n",
    "print(e_ph_found[1])\n",
    "print(energy_found[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "q_e_found = e_ph_found/energy_found\n",
    "q_e_lost = e_ph_lost/energy_lost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18580 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(q_e_lost, bins=100, density=True, alpha=0.5, histtype='bar', color=\"darkorange\", label=\"lost\")\n",
    "plt.hist(q_e_found, bins=100, density=True, alpha=0.5, histtype='bar', color=\"blue\", label=\"found\")\n",
    "plt.xlabel(r\"$E_\\gamma/E_0$\")\n",
    "plt.ylabel(\"counts (normed)\")\n",
    "plt.title(r'$E_{ph}/E_0$')\n",
    "plt.legend()\n",
    "\n",
    "\"\"\"\n",
    "B:\n",
    "we can clearly see that lost electrons are responsible for higher energy photons\n",
    "\"\"\"\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 62524 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ((ax0, ax1)) = plt.subplots(nrows=1, ncols=2, figsize=(20,6))\n",
    "\n",
    "a0 = ax0.hist2d(e_ph_found/(1e3), energy_found/(1e3), density=False, bins=200,  cmap=plt.cm.jet, range=[[0,200],[0,200]], cmin=1)\n",
    "ax0.set_xlabel(r\"$E_\\gamma$ [GeV]\")\n",
    "ax0.set_ylabel(r\"$E_e$ [GeV]\")\n",
    "ax0.set_title(\"found electron energy against photon energy\")\n",
    "plt.colorbar(a0[3],ax=ax0)\n",
    "\n",
    "a1 = ax1.hist2d(e_ph_lost/(1e3), energy_lost/(1e3), density=False, bins=200, cmap=plt.cm.jet, range=[[0,200],[0,200]], cmin=1)\n",
    "ax1.set_xlabel(r\"$E_\\gamma$ [GeV]\")\n",
    "ax1.set_ylabel(r\"$E_e$ [GeV]\")\n",
    "ax1.set_title(\"lost electron energy against photon energy\")\n",
    "plt.colorbar(a1[3],ax=ax1)\n",
    "\n",
    "\"\"\"\n",
    "B:\n",
    "concentrated at the E_ph/E_0~1 line especially at lower energies.\n",
    "lost E_ph to E_0: fewer entries at lower q_e\n",
    "\"\"\"\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "brem_vtx_x_found = tracked[\"brem_vtx_x\"]\n",
    "brem_vtx_x_found = brem_vtx_x_found[tracked[\"brem_vtx_x_length\"]!=0]\n",
    "brem_vtx_x_found = ak.to_numpy(ak.flatten(brem_vtx_x_found))\n",
    "\n",
    "brem_vtx_z_found = tracked[\"brem_vtx_z\"]\n",
    "brem_vtx_z_found = brem_vtx_z_found[tracked[\"brem_vtx_z_length\"]!=0]\n",
    "#print(ak.to_numpy(brem_vtx_z_found))\n",
    "brem_vtx_z_found = ak.to_numpy(ak.flatten(brem_vtx_z_found))\n",
    "\n",
    "brem_vtx_x_lost = lost[\"brem_vtx_x\"]\n",
    "brem_vtx_x_lost = brem_vtx_x_lost[lost[\"brem_vtx_x_length\"]!=0]\n",
    "brem_vtx_x_lost = ak.to_numpy(ak.flatten(brem_vtx_x_lost))\n",
    "\n",
    "brem_vtx_z_lost = lost[\"brem_vtx_z\"]\n",
    "brem_vtx_z_lost = brem_vtx_z_lost[lost[\"brem_vtx_z_length\"]!=0]\n",
    "brem_vtx_z_lost = ak.to_numpy(ak.flatten(brem_vtx_z_lost))\n",
    "\n",
    "#vtx_x_fit= ak.to_numpy(vtx_x_found)\n",
    "#vtx_z_fit = ak.to_numpy(vtx_z_found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 94236 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ((ax0, ax1)) = plt.subplots(nrows=1, ncols=2, figsize=(20,6))\n",
    "\n",
    "a0 = ax0.hist2d(brem_vtx_z_found, brem_vtx_x_found, density=False, bins=300,  cmap=plt.cm.jet, cmin=1)\n",
    "ax0.set_xlabel(\"z [mm]\")\n",
    "ax0.set_ylabel(\"x [mm]\")\n",
    "ax0.set_title(r\"$e^\\pm$ found brem vertices\")\n",
    "\n",
    "plt.colorbar(a0[3],ax=ax0)\n",
    "\n",
    "a1 = ax1.hist2d(brem_vtx_z_lost, brem_vtx_x_lost, density=False, bins=300, cmap=plt.cm.jet, cmin=1)\n",
    "ax1.set_xlabel(\"z [mm]\")\n",
    "ax1.set_ylabel(\"x [mm]\")\n",
    "ax1.set_title(r\"$e^\\pm$ lost brem vertices\")\n",
    "#ax1.set(xlim=(0,4000), ylim=(-1000,1000))\n",
    "\n",
    "plt.colorbar(a1[3], ax=ax1)\n",
    "\n",
    "\"\"\"\n",
    "z: VeLo - RICH1 - TT - Magnet - T1,T2,T3 - RICH2 - M1\n",
    "B:\n",
    "vertices of lost e photons are more densely concentrated around the beampipe, especially in the z range of the magnet\n",
    "found: vertices are densely located @ or around the detectors, while there are no clusters in the z range of the magnet\n",
    "\"\"\"\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "#plot singular tracks by fitting brem vertices\n",
    "vtx_z_found = tracked[\"brem_vtx_z\"]\n",
    "vtx_z_found = vtx_z_found[tracked[\"brem_vtx_z_length\"]>3]\n",
    "\n",
    "vtx_x_found = tracked[\"brem_vtx_x\"]\n",
    "vtx_x_found = vtx_x_found[tracked[\"brem_vtx_x_length\"]>3]\n",
    "\n",
    "vtx_z_lost = lost[\"brem_vtx_z\"]\n",
    "vtx_z_lost = vtx_z_lost[lost[\"brem_vtx_z_length\"]>3]\n",
    "\n",
    "vtx_x_lost = lost[\"brem_vtx_x\"]\n",
    "vtx_x_lost = vtx_x_lost[lost[\"brem_vtx_x_length\"]>3]\n",
    "\n",
    "def cubic_fit(x, a, b, c, d):\n",
    "    return (a + b*x + c*x**2 + d*x**3)\n",
    "\n",
    "def quint_fit(x, a, b, c, d, e, f):\n",
    "    return (a + b*x + c*x**2 + d*x**3 + e*x**4 + f*x**5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/work/cetin/software/miniconda3/envs/env1/lib/python3.11/site-packages/scipy/optimize/_minpack_py.py:1010: OptimizeWarning: Covariance of the parameters could not be estimated\n",
      "  warnings.warn('Covariance of the parameters could not be estimated',\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 501648 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ((ax0, ax1)) = plt.subplots(nrows=1, ncols=2, figsize=(20,6))\n",
    "n_end=100\n",
    "\n",
    "for i in range(0,n_end):\n",
    "    popt, pcov = curve_fit(cubic_fit,ak.to_numpy(vtx_z_found[i,:]),ak.to_numpy(vtx_x_found[i,:]))\n",
    "    z_coord = np.linspace(vtx_z_found[i,0],12000,1000)\n",
    "    fit = cubic_fit(z_coord, popt[0], popt[1], popt[2], popt[3])\n",
    "    ax0.plot(z_coord, fit, \"-\", label=\"fit\"+str(i), lw=0.5)\n",
    "    ax0.errorbar(ak.to_numpy(vtx_z_found[i,:]),ak.to_numpy(vtx_x_found[i,:]),fmt=\".\",ms=2)\n",
    "\n",
    "#ax0.legend()\n",
    "ax0.set_xlabel(\"z [mm]\")\n",
    "ax0.set_ylabel(\"x [mm]\")\n",
    "ax0.set_title(\"found tracks of brem vertices from few signals\")\n",
    "ax0.set(xlim=(0,12000), ylim=(-4000,4000))\n",
    "ax0.grid()\n",
    "\n",
    "for i in range(0,n_end):\n",
    "    popt, pcov = curve_fit(cubic_fit,ak.to_numpy(vtx_z_lost[i,:]),ak.to_numpy(vtx_x_lost[i,:]))\n",
    "    z_coord = np.linspace(vtx_z_lost[i,0],12000,1000)\n",
    "    fit = cubic_fit(z_coord, popt[0], popt[1], popt[2], popt[3])\n",
    "    ax1.plot(z_coord, fit, \"-\", label=\"fit\"+str(i), lw=0.5)\n",
    "    ax1.errorbar(ak.to_numpy(vtx_z_lost[i,:]),ak.to_numpy(vtx_x_lost[i,:]),fmt=\".\",ms=2)\n",
    "\n",
    "#ax1.legend()\n",
    "ax1.set_xlabel(\"z [mm]\")\n",
    "ax1.set_ylabel(\"x [mm]\")\n",
    "ax1.set_title(\"lost tracks of brem vertices from few signals\")\n",
    "ax1.set(xlim=(0,12000), ylim=(-4000,4000))\n",
    "ax1.grid()\n",
    "\n",
    "\"\"\"\n",
    "we can see that of the lost brem vertices, many trajectory fits seem illogical and not plausible\n",
    "\"\"\"\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "endvtx_x_found = tracked[\"all_endvtx_x\"]\n",
    "endvtx_x_found = endvtx_x_found[tracked[\"all_endvtx_x_length\"]!=0]\n",
    "endvtx_x_found = ak.to_numpy(ak.flatten(endvtx_x_found))\n",
    "\n",
    "endvtx_z_found = tracked[\"all_endvtx_z\"]\n",
    "endvtx_z_found = endvtx_z_found[tracked[\"all_endvtx_z_length\"]!=0]\n",
    "#print(ak.to_numpy(brem_vtx_z_found))\n",
    "endvtx_z_found = ak.to_numpy(ak.flatten(endvtx_z_found))\n",
    "\n",
    "endvtx_x_lost = lost[\"all_endvtx_x\"]\n",
    "endvtx_x_lost = endvtx_x_lost[lost[\"all_endvtx_x_length\"]!=0]\n",
    "endvtx_x_lost = ak.to_numpy(ak.flatten(endvtx_x_lost))\n",
    "\n",
    "endvtx_z_lost = lost[\"all_endvtx_z\"]\n",
    "endvtx_z_lost = endvtx_z_lost[lost[\"all_endvtx_z_length\"]!=0]\n",
    "endvtx_z_lost = ak.to_numpy(ak.flatten(endvtx_z_lost))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 72720 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ((ax0, ax1)) = plt.subplots(nrows=1, ncols=2, figsize=(20,6))\n",
    "\n",
    "a0 = ax0.hist2d(endvtx_z_found, endvtx_x_found, density=False, bins=500,  cmap=plt.cm.jet, cmin=1)\n",
    "ax0.set_xlabel(\"z [mm]\")\n",
    "ax0.set_ylabel(\"x [mm]\")\n",
    "ax0.set_title(r\"$e^\\pm$ found end vertices\")\n",
    "ax0.set(xlim=(0,12000), ylim=(-4000,4000))\n",
    "\n",
    "plt.colorbar(a0[3],ax=ax0)\n",
    "\n",
    "a1 = ax1.hist2d(endvtx_z_lost, endvtx_x_lost, density=False, bins=500, cmap=plt.cm.jet, cmin=1)\n",
    "ax1.set_xlabel(\"z [mm]\")\n",
    "ax1.set_ylabel(\"x [mm]\")\n",
    "ax1.set_title(r\"$e^\\pm$ lost end vertices\")\n",
    "ax1.set(xlim=(0,12000), ylim=(-4000,4000))\n",
    "\n",
    "plt.colorbar(a1[3], ax=ax1)\n",
    "\n",
    "\"\"\"\n",
    "z: VeLo - RICH1 - TT - Magnet - T1,T2,T3 - RICH2 - M1\n",
    "B:\n",
    "vertices of lost e photons are more densely concentrated around the beampipe, especially in the z range of the magnet\n",
    "found: vertices are densely located @ or around the detectors, while there are no clusters in the z range of the magnet\n",
    "\"\"\"\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# try to plot trajectories using all tracker hits (Velo, UT, SciFi)\n",
    "\n",
    "velo_x_found = tracked[\"velo_hit_pos_x\"]\n",
    "velo_z_found = tracked[\"velo_hit_pos_z\"]\n",
    "ut_x_found = tracked[\"ut_hit_pos_x\"]\n",
    "ut_z_found = tracked[\"ut_hit_pos_z\"]\n",
    "scifi_x_found = tracked[\"scifi_hit_pos_x\"]\n",
    "scifi_z_found = tracked[\"scifi_hit_pos_z\"]\n",
    "\n",
    "tracker_x_found = ak.concatenate([velo_x_found,ut_x_found,scifi_x_found], axis=1)\n",
    "tracker_z_found = ak.concatenate([velo_z_found,ut_z_found,scifi_z_found], axis=1)\n",
    "\n",
    "velo_x_lost = lost[\"velo_hit_pos_x\"]\n",
    "velo_z_lost = lost[\"velo_hit_pos_z\"]\n",
    "ut_x_lost = lost[\"ut_hit_pos_x\"]\n",
    "ut_z_lost = lost[\"ut_hit_pos_z\"]\n",
    "scifi_x_lost = lost[\"scifi_hit_pos_x\"]\n",
    "scifi_z_lost = lost[\"scifi_hit_pos_z\"]\n",
    "\n",
    "tracker_x_lost = ak.concatenate([velo_x_lost,ut_x_lost,scifi_x_lost], axis=1)\n",
    "tracker_z_lost = ak.concatenate([velo_z_lost,ut_z_lost,scifi_z_lost], axis=1)\n",
    "\n",
    "\n",
    "tracker_x_found = tracker_x_found[tracked[\"energy\"]<10*1e3]\n",
    "tracker_z_found = tracker_z_found[tracked[\"energy\"]<10*1e3]\n",
    "\n",
    "\n",
    "tracker_x_lost = tracker_x_lost[lost[\"energy\"]<10*1e3]\n",
    "tracker_z_lost = tracker_z_lost[lost[\"energy\"]<10*1e3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 971560 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ((ax0, ax1)) = plt.subplots(nrows=1, ncols=2, figsize=(20,6))\n",
    "\n",
    "nstart=0\n",
    "nend=130\n",
    "\n",
    "for i in range(nstart,nend):\n",
    "    popt, pcov = curve_fit(cubic_fit,ak.to_numpy(tracker_z_found[i,:]),ak.to_numpy(tracker_x_found[i,:]))\n",
    "    z_coord = np.linspace(tracker_z_found[i,0],14000,1000)\n",
    "    fit = cubic_fit(z_coord, popt[0], popt[1], popt[2], popt[3])\n",
    "    ax0.plot(z_coord, fit, \"-\", lw=0.5)\n",
    "    ax0.errorbar(ak.to_numpy(tracker_z_found[i,:]),ak.to_numpy(tracker_x_found[i,:]),fmt=\".\",ms=3)\n",
    "\n",
    "ax0.legend([r\"$E<10$GeV\"])\n",
    "ax0.vlines(3000, -4000,4000, lw=1, ls=\":\", color=\"red\")\n",
    "ax0.vlines(7500, -4000,4000, lw=1, ls=\":\", color=\"red\")\n",
    "ax0.set_xticks(np.arange(0,14000,1000) , minor=True)\n",
    "ax0.set_yticks(np.arange(-4000,4000,500), minor=True)\n",
    "ax0.set_xlabel(\"z [mm]\")\n",
    "ax0.set_ylabel(\"x [mm]\")\n",
    "ax0.set_title(\"found tracks from detector hits from few signals\")\n",
    "ax0.set(xlim=(0,14000), ylim=(-4000,4000))\n",
    "ax0.grid()\n",
    "\n",
    "for i in range(nstart,nend):\n",
    "    popt, pcov = curve_fit(cubic_fit,ak.to_numpy(tracker_z_lost[i,:]),ak.to_numpy(tracker_x_lost[i,:]))\n",
    "    z_coord = np.linspace(tracker_z_lost[i,0],14000,1000)\n",
    "    fit = cubic_fit(z_coord, popt[0], popt[1], popt[2], popt[3])\n",
    "    ax1.plot(z_coord, fit, \"-\", lw=0.5)\n",
    "    ax1.errorbar(ak.to_numpy(tracker_z_lost[i,:]),ak.to_numpy(tracker_x_lost[i,:]),fmt=\".\",ms=3)\n",
    "\n",
    "ax1.vlines(3000, -4000,4000, lw=1, ls=\":\", color=\"red\")\n",
    "ax1.vlines(7500, -4000,4000, lw=1, ls=\":\", color=\"red\")\n",
    "ax1.set_xticks(np.arange(0,14000,1000) , minor=True)\n",
    "ax1.set_yticks(np.arange(-4000,4000,500), minor=True)\n",
    "ax1.set_xlabel(\"z [mm]\")\n",
    "ax1.set_ylabel(\"x [mm]\")\n",
    "ax1.set_title(\"lost tracks from detector hits from few signals\")\n",
    "ax1.set(xlim=(0,14000), ylim=(-4000,4000))\n",
    "ax1.grid()\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "electrons and photons will be stopped by the ECAL which serves to measure the particles energy\n",
    "\n",
    "the trajectories between the velo and tt should be linear, which cannot be plotted accurately using a single fit.\n",
    "lost tracks diverge more severely.\n",
    "\n",
    "most higher energy particles maintain a trajectory closer to the beamdirection ie a larger pseudorapidity,\n",
    "and show less bending in their trajectory, especially upstream.\n",
    "found: higher energy: very compact trajectory, less bending wrt lower energy particles \n",
    "\"\"\"\n",
    "\n",
    "\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'all_endvtx_types_length': 11,\n",
       " 'all_endvtx_types': [101.0,\n",
       "  101.0,\n",
       "  101.0,\n",
       "  101.0,\n",
       "  101.0,\n",
       "  101.0,\n",
       "  101.0,\n",
       "  101.0,\n",
       "  101.0,\n",
       "  101.0,\n",
       "  0.0],\n",
       " 'all_endvtx_x_length': 11,\n",
       " 'all_endvtx_x': [19.496400833129883,\n",
       "  24.957000732421875,\n",
       "  32.490699768066406,\n",
       "  34.14419937133789,\n",
       "  34.6599006652832,\n",
       "  36.427101135253906,\n",
       "  -1914.992431640625,\n",
       "  -2413.033203125,\n",
       "  -3782.947998046875,\n",
       "  -3786.80810546875,\n",
       "  -3819.826904296875],\n",
       " 'all_endvtx_y_length': 11,\n",
       " 'all_endvtx_y': [-20.370500564575195,\n",
       "  -26.043100357055664,\n",
       "  -33.85060119628906,\n",
       "  -35.53160095214844,\n",
       "  -36.069400787353516,\n",
       "  -37.92850112915039,\n",
       "  -504.0671081542969,\n",
       "  -533.6621704101562,\n",
       "  -598.8317260742188,\n",
       "  -599.1124877929688,\n",
       "  -593.3890991210938],\n",
       " 'all_endvtx_z_length': 11,\n",
       " 'all_endvtx_z': [399.7018127441406,\n",
       "  500.7049865722656,\n",
       "  637.5723266601562,\n",
       "  667.4852905273438,\n",
       "  677.0195922851562,\n",
       "  709.4874877929688,\n",
       "  8576.556640625,\n",
       "  9281.2119140625,\n",
       "  11183.1640625,\n",
       "  11188.4541015625,\n",
       "  11237.0966796875],\n",
       " 'brem_photons_pe_length': 10,\n",
       " 'brem_photons_pe': [2469.182861328125,\n",
       "  169.8916473388672,\n",
       "  223.71347045898438,\n",
       "  388.2983703613281,\n",
       "  3228.4228515625,\n",
       "  809.4715576171875,\n",
       "  171.57342529296875,\n",
       "  223.80494689941406,\n",
       "  178.85166931152344,\n",
       "  793.921142578125],\n",
       " 'brem_photons_px_length': 10,\n",
       " 'brem_photons_px': [133.6999969482422,\n",
       "  9.329999923706055,\n",
       "  12.489999771118164,\n",
       "  20.93000030517578,\n",
       "  175.22000122070312,\n",
       "  43.81999969482422,\n",
       "  -98.19999694824219,\n",
       "  -129.77000427246094,\n",
       "  -106.12000274658203,\n",
       "  -465.2200012207031],\n",
       " 'brem_photons_py_length': 10,\n",
       " 'brem_photons_py': [-137.9199981689453,\n",
       "  -9.65999984741211,\n",
       "  -12.579999923706055,\n",
       "  -21.809999465942383,\n",
       "  -183.27999877929688,\n",
       "  -47.79999923706055,\n",
       "  -6.090000152587891,\n",
       "  -7.429999828338623,\n",
       "  -5.800000190734863,\n",
       "  -36.66999816894531],\n",
       " 'brem_photons_pz_length': 10,\n",
       " 'brem_photons_pz': [2461.699951171875,\n",
       "  169.36000061035156,\n",
       "  223.00999450683594,\n",
       "  387.1199951171875,\n",
       "  3218.449951171875,\n",
       "  806.8699951171875,\n",
       "  140.55999755859375,\n",
       "  182.19000244140625,\n",
       "  143.85000610351562,\n",
       "  642.2899780273438],\n",
       " 'brem_vtx_x_length': 10,\n",
       " 'brem_vtx_x': [19.496400833129883,\n",
       "  24.957000732421875,\n",
       "  32.490699768066406,\n",
       "  34.14419937133789,\n",
       "  34.6599006652832,\n",
       "  36.427101135253906,\n",
       "  -1914.992431640625,\n",
       "  -2413.033203125,\n",
       "  -3782.947998046875,\n",
       "  -3786.80810546875],\n",
       " 'brem_vtx_y_length': 10,\n",
       " 'brem_vtx_y': [-20.370500564575195,\n",
       "  -26.043100357055664,\n",
       "  -33.85060119628906,\n",
       "  -35.53160095214844,\n",
       "  -36.069400787353516,\n",
       "  -37.92850112915039,\n",
       "  -504.0671081542969,\n",
       "  -533.6621704101562,\n",
       "  -598.8317260742188,\n",
       "  -599.1124877929688],\n",
       " 'brem_vtx_z_length': 10,\n",
       " 'brem_vtx_z': [399.7018127441406,\n",
       "  500.7049865722656,\n",
       "  637.5723266601562,\n",
       "  667.4852905273438,\n",
       "  677.0195922851562,\n",
       "  709.4874877929688,\n",
       "  8576.556640625,\n",
       "  9281.2119140625,\n",
       "  11183.1640625,\n",
       "  11188.4541015625],\n",
       " 'endvtx_type': 0,\n",
       " 'endvtx_x': nan,\n",
       " 'endvtx_y': nan,\n",
       " 'endvtx_z': nan,\n",
       " 'energy': 9355.866625028413,\n",
       " 'eta': 3.237728027535365,\n",
       " 'event_count': 2,\n",
       " 'fromB': True,\n",
       " 'fromD': False,\n",
       " 'fromDecay': True,\n",
       " 'fromHadInt': False,\n",
       " 'fromPV': False,\n",
       " 'fromPairProd': False,\n",
       " 'fromSignal': True,\n",
       " 'fromStrange': False,\n",
       " 'isElectron': True,\n",
       " 'isKaon': False,\n",
       " 'isMuon': False,\n",
       " 'isPion': False,\n",
       " 'isProton': False,\n",
       " 'lost': False,\n",
       " 'lost_in_track_fit': False,\n",
       " 'match_fraction': 1.0,\n",
       " 'mcp_idx': 5488,\n",
       " 'mother_id': 511,\n",
       " 'mother_key': 5479,\n",
       " 'originvtx_type': 2,\n",
       " 'originvtx_x': -0.0663,\n",
       " 'originvtx_y': -0.0023,\n",
       " 'originvtx_z': 40.3966,\n",
       " 'p': 9355.866611073503,\n",
       " 'phi': -0.8090232566094933,\n",
       " 'pid': -11,\n",
       " 'pt': 733.3612464536151,\n",
       " 'px': 506.17,\n",
       " 'py': -530.67,\n",
       " 'pz': 9327.08,\n",
       " 'scifi_hit_pos_x_length': 13,\n",
       " 'scifi_hit_pos_x': [-1402.2215576171875,\n",
       "  -1448.5460205078125,\n",
       "  -1495.479736328125,\n",
       "  -1542.3804931640625,\n",
       "  -1865.9482421875,\n",
       "  -1914.599853515625,\n",
       "  -1915.0545654296875,\n",
       "  -1963.8216552734375,\n",
       "  -2012.78173828125,\n",
       "  -2349.04052734375,\n",
       "  -2398.658935546875,\n",
       "  -2448.573486328125,\n",
       "  -2498.376220703125],\n",
       " 'scifi_hit_pos_y_length': 13,\n",
       " 'scifi_hit_pos_y': [-470.2232666015625,\n",
       "  -473.55914306640625,\n",
       "  -476.8390197753906,\n",
       "  -480.12762451171875,\n",
       "  -501.0274963378906,\n",
       "  -504.04315185546875,\n",
       "  -504.0709228515625,\n",
       "  -507.0843200683594,\n",
       "  -510.0830383300781,\n",
       "  -530.0570068359375,\n",
       "  -532.8589477539062,\n",
       "  -535.61572265625,\n",
       "  -538.2548217773438],\n",
       " 'scifi_hit_pos_z_length': 13,\n",
       " 'scifi_hit_pos_z': [7824.40576171875,\n",
       "  7894.1943359375,\n",
       "  7964.38330078125,\n",
       "  8034.171875,\n",
       "  8506.294921875,\n",
       "  8575.99609375,\n",
       "  8576.6455078125,\n",
       "  8646.2744140625,\n",
       "  8716.0634765625,\n",
       "  9191.1904296875,\n",
       "  9260.98046875,\n",
       "  9331.171875,\n",
       "  9400.962890625],\n",
       " 'track_p': 1931.9397828451663,\n",
       " 'track_pt': 151.36962154532284,\n",
       " 'tx': 0.05426886013629132,\n",
       " 'ty': -0.056895620065443846,\n",
       " 'ut_hit_pos_x_length': 4,\n",
       " 'ut_hit_pos_x': [112.31356048583984,\n",
       "  114.4996337890625,\n",
       "  122.83889770507812,\n",
       "  124.72588348388672],\n",
       " 'ut_hit_pos_y_length': 4,\n",
       " 'ut_hit_pos_y': [-135.26077270507812,\n",
       "  -138.64544677734375,\n",
       "  -152.51470947265625,\n",
       "  -155.91305541992188],\n",
       " 'ut_hit_pos_z_length': 4,\n",
       " 'ut_hit_pos_z': [2313.153564453125,\n",
       "  2368.153564453125,\n",
       "  2593.153564453125,\n",
       "  2648.153564453125],\n",
       " 'velo_hit_pos_x_length': 10,\n",
       " 'velo_hit_pos_x': [3.2025206089019775,\n",
       "  4.559732437133789,\n",
       "  5.917426109313965,\n",
       "  7.274953365325928,\n",
       "  8.638668060302734,\n",
       "  10.008487701416016,\n",
       "  11.378168106079102,\n",
       "  12.745656967163086,\n",
       "  15.399646759033203,\n",
       "  19.47773551940918],\n",
       " 'velo_hit_pos_y_length': 10,\n",
       " 'velo_hit_pos_y': [-3.429784059524536,\n",
       "  -4.8510894775390625,\n",
       "  -6.275496482849121,\n",
       "  -7.6981940269470215,\n",
       "  -9.124002456665039,\n",
       "  -10.54817008972168,\n",
       "  -11.963949203491211,\n",
       "  -13.374946594238281,\n",
       "  -16.1297550201416,\n",
       "  -20.351228713989258],\n",
       " 'velo_hit_pos_z_length': 10,\n",
       " 'velo_hit_pos_z': [100.64099884033203,\n",
       "  125.64099884033203,\n",
       "  150.64100646972656,\n",
       "  175.64100646972656,\n",
       "  200.64100646972656,\n",
       "  225.64100646972656,\n",
       "  250.64100646972656,\n",
       "  275.6409912109375,\n",
       "  324.3590087890625,\n",
       "  399.3590087890625],\n",
       " 'velo_track_idx': 143,\n",
       " 'velo_track_tx': 0.054571494460105896,\n",
       " 'velo_track_ty': -0.056447889655828476,\n",
       " 'velo_track_x': 39.710758209228516,\n",
       " 'velo_track_y': -41.2618293762207,\n",
       " 'velo_track_z': 770.0}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tracked[1].tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env1",
   "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.11.5"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
