{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 60,
   "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",
    "from scipy.optimize import curve_fit\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "51"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file = uproot.open(\"tracking_losses_ntuple_Dst0ToD0EE.root:PrDebugTrackingLosses.PrDebugTrackingTool/Tuple;1\")\n",
    "\n",
    "\n",
    "#selektiere nur elektronen von D*0->D0ee und nur solche mit einem momentum von unter 5 GeV \n",
    "allcolumns = file.arrays()\n",
    "found = allcolumns[(allcolumns.isElectron) & (~allcolumns.lost) & (allcolumns.fromSignal) & (allcolumns.p < 5e3)] #D: 2591\n",
    "lost = allcolumns[(allcolumns.isElectron) & (allcolumns.lost) & (allcolumns.fromSignal) & (allcolumns.p < 5e3)] #D: 1908\n",
    "\n",
    "#ak.num(lost, axis=0)\n",
    "ak.num(allcolumns[(allcolumns.fromPairProd) & (allcolumns.isElectron) & (~allcolumns.lost) & (allcolumns.fromSignal)],axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5759057568348522"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def t_eff(found, lost):\n",
    "    sel = found[\"energy\"]\n",
    "    des = lost[\"energy\"]\n",
    "    return ak.count(sel,axis=None)/(ak.count(sel,axis=None)+ak.count(des,axis=None))\n",
    "\n",
    "t_eff(found, lost)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7960893854748603"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#finden wir die elektronen die keine bremsstrahlung gemacht haben mit hoher effizienz?\n",
    "nobrem_found = found[found[\"brem_photons_pe_length\"]==0]\n",
    "nobrem_lost = lost[lost[\"brem_photons_pe_length\"]==0]\n",
    "\n",
    "\"\"\"\n",
    "die effizienz mit der wir elektronen finden, die keine bremsstrahlung gemacht haben, ist nicht besonders gut, aber trotzdem besser als\n",
    "für alle elektronen.\n",
    "Auch hier handelt es sich um eine recht geringe sample size (~350)\n",
    "\"\"\"\n",
    "\n",
    "t_eff(nobrem_found, nobrem_lost)\n",
    "#ak.num(nobrem_lost, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5568703211784594"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#wie viel energie relativ zur anfangsenergie verlieren die elektronen durch bremstrahlung und hat das einen einfluss darauf ob wir sie finden oder nicht?\n",
    "brem_found = found[found[\"brem_photons_pe_length\"]!=0]\n",
    "energy_found = ak.to_numpy(brem_found[\"energy\"])\n",
    "eph_found = ak.to_numpy(ak.sum(brem_found[\"brem_photons_pe\"], axis=-1, keepdims=False))\n",
    "energyloss_found = eph_found/energy_found\n",
    "\n",
    "\n",
    "brem_lost = lost[lost[\"brem_photons_pe_length\"]!=0]\n",
    "energy_lost = ak.to_numpy(brem_lost[\"energy\"])\n",
    "eph_lost = ak.to_numpy(ak.sum(brem_lost[\"brem_photons_pe\"], axis=-1, keepdims=False))\n",
    "energyloss_lost = eph_lost/energy_lost\n",
    "\n",
    "t_eff(brem_found,brem_lost)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean energyloss relative to initial energy (found):  0.4422334194682742\n",
      "mean energyloss relative to initial energy (lost):  0.5885317187224427\n"
     ]
    }
   ],
   "source": [
    "mean_energyloss_found = ak.mean(energyloss_found)\n",
    "mean_energyloss_lost = ak.mean(energyloss_lost)\n",
    "print(\"mean energyloss relative to initial energy (found): \", mean_energyloss_found)\n",
    "print(\"mean energyloss relative to initial energy (lost): \", mean_energyloss_lost)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26068 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(energyloss_lost, bins=150, density=True, alpha=0.5, histtype='bar', color=\"darkorange\", label=\"lost\")\n",
    "plt.hist(energyloss_found, bins=150, density=True, alpha=0.5, histtype='bar', color=\"blue\", label=\"found\")\n",
    "plt.xticks(np.arange(0,1.1,0.1), minor=True,)\n",
    "plt.yticks(np.arange(0,5,0.5), minor=True)\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",
    "plt.grid()\n",
    "\n",
    "\"\"\"\n",
    "found: elektronen verlieren durchschnittlich 0.44 ihrer anfangsenergie durch bremsstrahlung\n",
    "lost: elektronen verlieren durchschnittlich 0.59 ihrer anfangsenergie durch bremsstrahlung\n",
    "\n",
    "-> lost electrons lose slightly more energy than found electrons. This is however nowhere near as extreme as for the B decay\n",
    "\"\"\"\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "#ist die shape der teilspur im scifi anders? (koenntest du zum beispiel durch vergleich der verteilungen der fit parameter studieren,\n",
    "#in meiner thesis findest du das fitmodell -- ist einfach ein polynom dritten grades)\n",
    "z_ref=8520 #mm\n",
    "\n",
    "def scifi_track(z, a, b, c, d):\n",
    "    return a + b*(z-z_ref) + c*(z-z_ref)**2 + d*(z-z_ref)**3\n",
    "\n",
    "def z_mag(xv, zv, tx, a, b):\n",
    "    \"\"\" optical centre of the magnet is defined as the intersection between the trajectory tangents before and after the magnet\n",
    "\n",
    "    Args:\n",
    "        xv (double): velo x track\n",
    "        zv (double): velo z track\n",
    "        tx (double): velo x slope\n",
    "        a (double): ax parameter of track fit\n",
    "        b (double): bx parameter of track fit\n",
    "\n",
    "    Returns:\n",
    "        double: z_mag\n",
    "    \"\"\"\n",
    "    return (xv-tx*zv-a+b*z_ref)/(b-tx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "scifi_found = found[found[\"scifi_hit_pos_x_length\"]>3]\n",
    "scifi_lost = lost[lost[\"scifi_hit_pos_x_length\"]>3]\n",
    "\n",
    "scifi_x_found = scifi_found[\"scifi_hit_pos_x\"]\n",
    "scifi_z_found = scifi_found[\"scifi_hit_pos_z\"]\n",
    "\n",
    "tx_found = scifi_found[\"velo_track_tx\"]\n",
    "\n",
    "scifi_x_lost = scifi_lost[\"scifi_hit_pos_x\"]\n",
    "scifi_z_lost = scifi_lost[\"scifi_hit_pos_z\"]\n",
    "\n",
    "tx_lost = scifi_lost[\"velo_track_tx\"]\n",
    "\n",
    "xv_found = scifi_found[\"velo_track_x\"]\n",
    "zv_found = scifi_found[\"velo_track_z\"]\n",
    "\n",
    "xv_lost = scifi_lost[\"velo_track_x\"]\n",
    "zv_lost = scifi_lost[\"velo_track_z\"]\n",
    "\n",
    "\n",
    "\n",
    "#ak.num(scifi_found[\"energy\"], axis=0)\n",
    "#scifi_found.snapshot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "scifi_fitpars_found = ak.ArrayBuilder()\n",
    "\n",
    "for i in range(0,ak.num(scifi_found[\"energy\"], axis=0)):\n",
    "    popt, pcov = curve_fit(scifi_track,ak.to_numpy(scifi_z_found[i,:]),ak.to_numpy(scifi_x_found[i,:]))\n",
    "    scifi_fitpars_found.begin_list()\n",
    "    scifi_fitpars_found.real(popt[0])\n",
    "    scifi_fitpars_found.real(popt[1])\n",
    "    scifi_fitpars_found.real(popt[2])\n",
    "    scifi_fitpars_found.real(popt[3])\n",
    "    scifi_fitpars_found.end_list()\n",
    "\n",
    "scifi_fitpars_lost = ak.ArrayBuilder()\n",
    "\n",
    "for i in range(0,ak.num(scifi_lost[\"energy\"], axis=0)):\n",
    "    popt, pcov = curve_fit(scifi_track,ak.to_numpy(scifi_z_lost[i,:]),ak.to_numpy(scifi_x_lost[i,:]))\n",
    "    scifi_fitpars_lost.begin_list()\n",
    "    scifi_fitpars_lost.real(popt[0])\n",
    "    scifi_fitpars_lost.real(popt[1])\n",
    "    scifi_fitpars_lost.real(popt[2])\n",
    "    scifi_fitpars_lost.real(popt[3])\n",
    "    scifi_fitpars_lost.end_list()\n",
    "\n",
    "\n",
    "scifi_fitpars_lost = scifi_fitpars_lost.to_numpy()\n",
    "scifi_fitpars_found = scifi_fitpars_found.to_numpy()\n",
    "\n",
    "\n",
    "\n",
    "dtx_found = scifi_fitpars_found[:,1] - tx_found\n",
    "dtx_lost = scifi_fitpars_lost[:,1] - tx_lost\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 80084 characters]",
      "text/plain": [
       "<Figure size 1500x1000 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(nrows=2, ncols=2, figsize=(15,10))\n",
    "\n",
    "ax0.hist(scifi_fitpars_found[:,0], bins=100, density=True, alpha=0.5, histtype='bar', color=\"blue\", label=r\"$a_x$ found\")\n",
    "ax0.hist(scifi_fitpars_lost[:,0], bins=100, density=True, alpha=0.5, histtype='bar', color=\"darkorange\", label=r\"$a_x$ lost\")\n",
    "ax0.set_xlabel(\"a\")\n",
    "ax0.set_ylabel(\"normed\")\n",
    "ax0.set_title(\"fitparameter a der scifi track\")\n",
    "ax0.legend()\n",
    "\n",
    "ax1.hist(scifi_fitpars_found[:,1], bins=100, density=True, alpha=0.5, histtype='bar', color=\"blue\", label=r\"$b_x$ found\")\n",
    "ax1.hist(scifi_fitpars_lost[:,1], bins=100, density=True, alpha=0.5, histtype='bar', color=\"darkorange\", label=r\"$b_x$ lost\")\n",
    "ax1.set_xlabel(\"b\")\n",
    "ax1.set_ylabel(\"normed\")\n",
    "ax1.set_title(\"fitparameter b der scifi track\")\n",
    "ax1.legend()\n",
    "\n",
    "ax2.hist(scifi_fitpars_found[:,2], bins=200, density=True, alpha=0.5, histtype='bar', color=\"blue\", label=r\"$c_x$ found\")\n",
    "ax2.hist(scifi_fitpars_lost[:,2], bins=1000, density=True, alpha=0.5, histtype='bar', color=\"darkorange\", label=r\"$c_x$ lost\")\n",
    "ax2.set_xlim([-4e-5,4e-5])\n",
    "ax2.set_xticks(np.arange(-4e-5,4.5e-5,1e-5),minor=False)\n",
    "ax2.set_xlabel(\"c\")\n",
    "ax2.set_ylabel(\"normed\")\n",
    "ax2.set_title(\"fitparameter c der scifi track\")\n",
    "ax2.legend()\n",
    "\n",
    "ax3.hist(scifi_fitpars_found[:,3], bins=200, density=True, alpha=0.5, histtype='bar', color=\"blue\", label=r\"$d_x$ found\")\n",
    "ax3.hist(scifi_fitpars_lost[:,3], bins=1000, density=True, alpha=0.5, histtype='bar', color=\"darkorange\", label=r\"$d_x$ lost\")\n",
    "ax3.set(xlim=(-5e-8,5e-8))\n",
    "#ax3.text(-4e-8,3e8,\"d negligible <1e-7\")\n",
    "ax3.set_xlabel(\"d\")\n",
    "ax3.set_ylabel(\"normed\")\n",
    "ax3.set_title(\"fitparameter d der scifi track\")\n",
    "ax3.legend()\n",
    "\n",
    "\"\"\"\n",
    "a_x: virtual hit on the reference plane\n",
    "\"\"\"\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "found\n",
      "a =  18.04180550457298\n",
      "b =  0.0057651944487250376\n",
      "c =  9.480277789440454e-08\n",
      "d =  -4.452015498411874e-11\n",
      "lost\n",
      "a =  -35.48371609662243\n",
      "b =  -0.010382326385451382\n",
      "c =  -6.208301288913938e-07\n",
      "d =  9.580933791481267e-11\n"
     ]
    }
   ],
   "source": [
    "print(\"found\")\n",
    "print(\"a = \", str(np.mean(scifi_fitpars_found[:,0])))\n",
    "print(\"b = \", str(np.mean(scifi_fitpars_found[:,1])))\n",
    "print(\"c = \", str(np.mean(scifi_fitpars_found[:,2])))\n",
    "print(\"d = \", str(np.mean(scifi_fitpars_found[:,3])))\n",
    "\n",
    "print(\"lost\")\n",
    "print(\"a = \", str(np.mean(scifi_fitpars_lost[:,0])))\n",
    "print(\"b = \", str(np.mean(scifi_fitpars_lost[:,1])))\n",
    "print(\"c = \", str(np.mean(scifi_fitpars_lost[:,2])))\n",
    "print(\"d = \", str(np.mean(scifi_fitpars_lost[:,3])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.5438992626615335e-08"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min(scifi_fitpars_found[:,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1185932 characters]",
      "text/plain": [
       "<Figure size 1500x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ((ax0, ax1)) = plt.subplots(nrows=1, ncols=2, figsize=(15,6))\n",
    "\n",
    "for i in range(0,ak.num(scifi_found[\"energy\"], axis=0)):\n",
    "    z_coord = np.linspace(scifi_z_found[i,0],12000,300)\n",
    "    fit = scifi_track(z_coord, *scifi_fitpars_found[i])\n",
    "    ax0.plot(z_coord, fit, \"-\", lw=0.5)\n",
    "    ax0.errorbar(ak.to_numpy(scifi_z_found[i,:]),ak.to_numpy(scifi_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 scifi hits\")\n",
    "ax0.set(xlim=(7e3,12000), ylim=(-4000,4000))\n",
    "ax0.grid()\n",
    "\n",
    "for i in range(0,ak.num(scifi_lost[\"energy\"], axis=0)):\n",
    "    z_coord = np.linspace(scifi_z_lost[i,0],12000,300)\n",
    "    fit = scifi_track(z_coord, *scifi_fitpars_lost[i])\n",
    "    ax1.plot(z_coord, fit, \"-\", lw=0.5)\n",
    "    ax1.errorbar(ak.to_numpy(scifi_z_lost[i,:]),ak.to_numpy(scifi_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 scifi hits\")\n",
    "ax1.set(xlim=(7e3,12000), ylim=(-4000,4000))\n",
    "ax1.grid()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "found \n",
      "zmag =  5318.452650242005\n",
      "lost \n",
      "zmag =  5425.137423441005\n"
     ]
    }
   ],
   "source": [
    "#vergleich der zmag werte\n",
    "zmag_found = z_mag(xv_found, zv_found, tx_found, scifi_fitpars_found[:,0], scifi_fitpars_found[:,1])\n",
    "zmag_lost = z_mag(xv_lost, zv_lost, tx_lost, scifi_fitpars_lost[:,0], scifi_fitpars_lost[:,1])\n",
    "zmag_lost = zmag_lost[~np.isnan(zmag_lost)]\n",
    "zmag_found = zmag_found[~np.isnan(zmag_found)]\n",
    "\n",
    "print(\"found \\nzmag = \", str(np.mean(zmag_found)))\n",
    "print(\"lost \\nzmag = \", str(np.mean(zmag_lost)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26960 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(zmag_found, bins=100, density=True, alpha=0.5, histtype='bar', color=\"blue\", label=\"found\")\n",
    "plt.hist(zmag_lost, bins=3000, density=True, alpha=0.5, histtype=\"bar\",color=\"darkorange\", label=\"lost\")\n",
    "plt.xlabel(\"$z_{mag}$ [mm]\")\n",
    "plt.ylabel(\"normed\")\n",
    "plt.title(\"magnet kick position $z_{mag}$ calculated w fitparameters\")\n",
    "plt.legend()\n",
    "plt.xticks(np.arange(5100,5805,5), minor=True)\n",
    "plt.yticks(np.arange(0,0.011,0.001), minor=True)\n",
    "plt.xlim(5100,5800)\n",
    "\n",
    "\"\"\"\n",
    "wir können definitiv einen unterschied zwischen den z_mag werten, bzw deren verteilungen für lost and found, erkennen\n",
    "\"\"\"\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2591\n",
      "1908\n",
      "4499\n"
     ]
    }
   ],
   "source": [
    "both = ak.concatenate([found,lost],axis=0)\n",
    "print(ak.num(found,axis=0))\n",
    "print(ak.num(lost,axis=0))\n",
    "print(ak.num(both,axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#events w/ shared track electrons from found and lost:  1\n",
      "event_count:  [2822, 2822]\n",
      "velo idx:  [146, 146]\n",
      "mcp_index:  [5806, 5829]\n",
      "\n",
      "velo x:  [-75.1, -75.1]\n",
      "velo y:  [5.88, 5.88]\n",
      "\n",
      "velo tx:  [-0.0908, -0.0908]\n",
      "velo ty:  [0.00703, 0.00703]\n"
     ]
    }
   ],
   "source": [
    "#versuche teilchen von denselben events mit shared tracks zu finden. \n",
    "#idee: alle teilchen eines events sind durch event_count auffindbar. \n",
    "a_f_itr = found[\"event_count\"].to_numpy()\n",
    "f_itr = np.unique(a_f_itr)\n",
    "\n",
    "shared = ak.ArrayBuilder()\n",
    "count = 0\n",
    "\n",
    "for itr in f_itr:\n",
    "    temp = both[both[\"event_count\"]==itr]\n",
    "    if ak.num(temp,axis=0)>1:\n",
    "        #iterate over cols in temp and append all with duplicate velo_track_idx, such that possibles is array with possible shared tracks particles\n",
    "        #idea: look at one event at a time and at one velo track at a time. if there are at least two e with the same velo_track_idx in the same event,\n",
    "        #concatenate to the array of other shared track particles\n",
    "        _jitr = temp[\"velo_track_idx\"].to_numpy()\n",
    "        jitr = np.unique(_jitr)\n",
    "        for jentry in jitr:\n",
    "            jtem = temp[temp[\"velo_track_idx\"]==jentry]\n",
    "            if ak.num(jtem,axis=0)>1:\n",
    "                shared.append(jtem)\n",
    "            else:\n",
    "                continue\n",
    "    else:\n",
    "        continue\n",
    "shared = ak.Array(shared)\n",
    "\n",
    "idx=0\n",
    "print(\"#events w/ shared track electrons from found and lost: \",ak.num(shared, axis=0))\n",
    "\n",
    "print(\"event_count: \", shared[idx,:,\"event_count\"])\n",
    "print(\"velo idx: \" ,shared[idx,:,\"velo_track_idx\"])\n",
    "print(\"mcp_index: \", shared[idx,:,\"mcp_idx\"])\n",
    "\n",
    "print(\"\\nvelo x: \" ,shared[idx,:,\"velo_track_x\"])\n",
    "print(\"velo y: \" ,shared[idx,:,\"velo_track_y\"])\n",
    "\n",
    "print(\"\\nvelo tx: \" ,shared[idx,:,\"velo_track_tx\"])\n",
    "print(\"velo ty: \" ,shared[idx,:,\"velo_track_ty\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#velo_track_idx in all events:  217\n",
      "velo idx:  [0, 0, 0]\n",
      "mcp_index:  [1066, 1251, 666]\n",
      "event_count:  [5735, 7049, 7378]\n",
      "\n",
      "velo x:  [-2.21, 9.45, -18.5]\n",
      "velo y:  [-21.6, -33.4, 17.4]\n",
      "\n",
      "velo tx:  [-0.0022, 0.0153, -0.0224]\n",
      "velo ty:  [-0.0263, -0.0469, 0.0212]\n"
     ]
    }
   ],
   "source": [
    "#electrons with same velo_track_idx from all events\n",
    "temp_ = found[\"velo_track_idx\"].to_numpy()\n",
    "temp = np.unique(temp_)\n",
    "count=0\n",
    "psb=ak.ArrayBuilder()\n",
    "for jentry in temp:\n",
    "    jtem = found[found[\"velo_track_idx\"]==jentry]\n",
    "    if ak.num(jtem,axis=0)>1:\n",
    "        psb.append(jtem)\n",
    "    else:\n",
    "        continue\n",
    "\n",
    "psb = ak.Array(psb)\n",
    "\n",
    "print(\"#velo_track_idx in all events: \",ak.num(psb,axis=0))\n",
    "idx = 0\n",
    "print(\"velo idx: \" ,psb[idx,:,\"velo_track_idx\"])\n",
    "print(\"mcp_index: \", psb[idx,:,\"mcp_idx\"])\n",
    "print(\"event_count: \", psb[idx,:,\"event_count\"])\n",
    "print(\"\\nvelo x: \" ,psb[idx,:,\"velo_track_x\"])\n",
    "print(\"velo y: \" ,psb[idx,:,\"velo_track_y\"])\n",
    "\n",
    "print(\"\\nvelo tx: \" ,psb[idx,:,\"velo_track_tx\"])\n",
    "print(\"velo ty: \" ,psb[idx,:,\"velo_track_ty\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#events w/ shared track electrons from Photon Conversions:  951\n",
      "shared_idx:  0\n",
      "event_count:  [11, 11]\n",
      "velo idx:  [27, 27]\n",
      "mcp_index:  [1211, 1215]\n",
      "\n",
      "velo x:  [-20.4, -20.4]\n",
      "velo y:  [-24.4, -24.4]\n",
      "\n",
      "velo tx:  [-0.0234, -0.0234]\n",
      "velo ty:  [-0.028, -0.028]\n"
     ]
    }
   ],
   "source": [
    "#generell: wie viele elektronen von photon conversions teilen sich einen track?\n",
    "conv = allcolumns[(allcolumns.fromPairProd) & (allcolumns.isElectron) & (~allcolumns.lost)]\n",
    "\n",
    "conv_it = conv[\"event_count\"].to_numpy()\n",
    "conv_itr = np.unique(conv_it)\n",
    "\n",
    "cshared = ak.ArrayBuilder()\n",
    "count = 0\n",
    "\n",
    "for itr in conv_itr:\n",
    "    temp = conv[conv[\"event_count\"]==itr]\n",
    "    if ak.num(temp,axis=0)>1:\n",
    "        #iterate over cols in temp and append all with duplicate velo_track_idx, such that possibles is array with possible shared tracks particles\n",
    "        #idea: look at one event at a time and at one velo track at a time. if there are at least two e with the same velo_track_idx in the same event,\n",
    "        #concatenate to the array of other shared track particles\n",
    "        _jitr = temp[\"velo_track_idx\"].to_numpy()\n",
    "        jitr = np.unique(_jitr)\n",
    "        for jentry in jitr:\n",
    "            jtem = temp[temp[\"velo_track_idx\"]==jentry]\n",
    "            if ak.num(jtem,axis=0)>1:\n",
    "                cshared.append(jtem)\n",
    "            else:\n",
    "                continue\n",
    "    else:\n",
    "        continue\n",
    "cshared = ak.Array(cshared)\n",
    "\n",
    "\n",
    "print(\"#events w/ shared track electrons from Photon Conversions: \",ak.num(cshared, axis=0))\n",
    "idx = 0\n",
    "print(\"shared_idx: \", idx)\n",
    "print(\"event_count: \", cshared[idx,:,\"event_count\"])\n",
    "print(\"velo idx: \" ,cshared[idx,:,\"velo_track_idx\"])\n",
    "print(\"mcp_index: \", cshared[idx,:,\"mcp_idx\"])\n",
    "\n",
    "print(\"\\nvelo x: \" ,cshared[idx,:,\"velo_track_x\"])\n",
    "print(\"velo y: \" ,cshared[idx,:,\"velo_track_y\"])\n",
    "\n",
    "print(\"\\nvelo tx: \" ,cshared[idx,:,\"velo_track_tx\"])\n",
    "print(\"velo ty: \" ,cshared[idx,:,\"velo_track_ty\"])"
   ]
  },
  {
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "was meinst du? velo track sharen? wie überprüfe ich das? versuche ich einfach e teilchen mit identischen velo tracks und slopes zu finden?\n",
    "benutze ich mother key um elektronen von gleichen events zu finden? meinst du wie viele elektronen vom selben event teilen sich einen track,\n",
    "oder wie viele teilchen von unterschiedlichen events haben dieselben tracks?\n",
    "\"\"\"\n"
   ]
  },
  {
   "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": []
  }
 ],
 "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
}
