{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(36, 11)\n",
      "0 [    0     4  1517  1518  3560  3562  6805  6808   916  6852 37007]\n",
      "1 [   0    4 1518 1518 3558 3558 6805 6805  494 1623 1033]\n",
      "2 [    0     4  1521  1522  3558  3558  6807  6808  2983  1623 27511]\n",
      "3 [    0     4  2073  2074  4329  4330  8130  8131  1667  3270 14744]\n",
      "4 [    4     8  1517  1522  3558  3562  6805  6808 14728 21845 41226]\n",
      "5 [    4     8  2071  2074  4329  4331  8129  8131 10007 11158 17357]\n",
      "6 [    8    12  1518  1521  3558  3561  6805  6808 21384 23932 36489]\n",
      "7 [   8   12 2067 2067 4333 4333 8129 8129  252  616 1749]\n",
      "8 [   8   12 2070 2070 4333 4333 8131 8131 1576  616 7365]\n",
      "9 [    8    12  2070  2073  4330  4331  8129  8131 13775 16067 20396]\n",
      "10 [   12    16  1518  1520  3558  3561  6804  6808 29552 32984 37981]\n",
      "11 [   12    16  2067  2067  4333  4334  8129  8130   745  1642 17116]\n",
      "12 [   12    16  2069  2072  4330  4334  8129  8131 16890 20841 21570]\n",
      "13 [   16    20  1515  1520  3558  3560  6804  6808 29745 41462 42124]\n",
      "14 [   16    20  2068  2071  4331  4334  8128  8131 18224 19800 19987]\n",
      "15 [   20    24  1515  1519  3558  3560  6803  6808 22685 37720 37990]\n",
      "16 [   20    24  2065  2065  4334  4335  8128  8129   355  3490 10772]\n",
      "17 [   20    24  2067  2070  4331  4335  8128  8131 17600 19170 18459]\n",
      "18 [   24    28  1515  1519  3558  3560  6803  6808 13842 23613 23703]\n",
      "19 [   24    28  2064  2069  4332  4335  8128  8131 18223 17952 16706]\n"
     ]
    }
   ],
   "source": [
    "import uproot\n",
    "import numpy as np\n",
    "root_file = uproot.open(\"result_5384_130_6501.root\")\n",
    "root_file.allkeys()\n",
    "TDC = root_file['TDC']\n",
    "TC = root_file['TC']\n",
    "# print(TC.allkeys())\n",
    "\n",
    "def _minmax_from_branch(tree, bname, entry=0, offset=0):\n",
    "    '''\n",
    "    Awkward Array\n",
    "    TDC['wire_index_u'].array()[event#][blob#]\n",
    "    '''\n",
    "    akarray = tree[bname].array()[entry]\n",
    "    min = np.array([np.min(l)+offset for l in akarray])\n",
    "    min = np.expand_dims(min, axis=1)\n",
    "    max = np.array([np.max(l)+offset for l in akarray])\n",
    "    max = np.expand_dims(max, axis=1)\n",
    "    return np.concatenate((min,max), axis=1)\n",
    "\n",
    "def _wire_charge_sum(tree, bname, entry=0):\n",
    "    wire_charge = tree[bname].array()[entry]\n",
    "    wire_charge = np.array([int(sum(l)) for l in wire_charge])\n",
    "    wire_charge = np.expand_dims(wire_charge, axis=1)\n",
    "    return wire_charge\n",
    "\n",
    "def _signature(tree, entry=0):\n",
    "    time_slice_mm = _minmax_from_branch(tree, 'time_slice',entry)\n",
    "    time_slice_mm = time_slice_mm*4\n",
    "    time_slice_mm[:,1] = time_slice_mm[:,1]+4\n",
    "    wire_index_u_mm = _minmax_from_branch(tree, 'wire_index_u',entry,0)\n",
    "    wire_index_v_mm = _minmax_from_branch(tree, 'wire_index_v',entry,2400)\n",
    "    wire_index_w_mm = _minmax_from_branch(tree, 'wire_index_w',entry,4800)\n",
    "    sig = np.concatenate((time_slice_mm,wire_index_u_mm,wire_index_v_mm,wire_index_w_mm), axis=1)\n",
    "    wire_charge_u = _wire_charge_sum(tree,'wire_charge_u',entry)\n",
    "    wire_charge_v = _wire_charge_sum(tree,'wire_charge_v',entry)\n",
    "    wire_charge_w = _wire_charge_sum(tree,'wire_charge_w',entry)\n",
    "    sig = np.concatenate((sig,wire_charge_u,wire_charge_v,wire_charge_w), axis=1)\n",
    "    return sig\n",
    "#     flag_u = np.array(tree['flag_u'].array()[entry])\n",
    "#     flag_u = np.expand_dims(flag_u, axis=1)\n",
    "#     flag_v = np.array(tree['flag_v'].array()[entry])\n",
    "#     flag_v = np.expand_dims(flag_v, axis=1)\n",
    "#     flag_w = np.array(tree['flag_w'].array()[entry])\n",
    "#     flag_w = np.expand_dims(flag_w, axis=1)\n",
    "#     sig = np.concatenate((sig,wire_charge_u,wire_charge_v,wire_charge_w,flag_u,flag_v,flag_w), axis=1)\n",
    "#     return sig\n",
    "\n",
    "def _sort(arr):\n",
    "    ind = np.lexsort((arr[:,7],arr[:,6],arr[:,5],arr[:,4],arr[:,3],arr[:,2],arr[:,1],arr[:,0]))\n",
    "    arr = np.array([arr[i] for i in ind])\n",
    "    return arr\n",
    "\n",
    "sigs = _signature(TC, 0)\n",
    "\n",
    "sigs = _sort(sigs)\n",
    "# sigs = sigs[sigs[:,11]==0,:]\n",
    "sigs = sigs[sigs[:,0]<40,:]\n",
    "print(sigs.shape)\n",
    "for i in range(min([sigs.shape[0], 20])):\n",
    "# for i in range(sigs.shape[0]):\n",
    "    print(i, sigs[i,:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "# %matplotlib inline\n",
    "%matplotlib tk\n",
    "plt.plot(mm[:,2]*1e7+mm[:,3]*1e6+mm[:,4]*1e5,'o')\n",
    "plt.grid()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2.   0.3  0.01]\n",
      " [3.   0.1  0.02]\n",
      " [1.   0.2  0.03]\n",
      " [2.   0.1  0.03]]\n",
      "[[3.   0.1  0.02]\n",
      " [2.   0.1  0.03]\n",
      " [1.   0.2  0.03]\n",
      " [2.   0.3  0.01]]\n",
      "[[1.   0.2  0.03]\n",
      " [2.   0.1  0.03]\n",
      " [2.   0.3  0.01]\n",
      " [3.   0.1  0.02]]\n"
     ]
    }
   ],
   "source": [
    "l = np.array([[3,0.1,0.02],[1,0.2,0.03],[2,0.3,0.01],[2,0.1,0.03]])\n",
    "l = l[l[:,2].argsort()]\n",
    "print(l)\n",
    "l = l[l[:,1].argsort()]\n",
    "print(l)\n",
    "l = l[l[:,0].argsort()]\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1175.3699330244774, 36.99662624347637)\n",
      "(1173.6378826284347, 39.99663121405449)\n"
     ]
    }
   ],
   "source": [
    "def line_intersection(line1, line2):\n",
    "    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])\n",
    "    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])\n",
    "\n",
    "    def det(a, b):\n",
    "        return a[0] * b[1] - a[1] * b[0]\n",
    "\n",
    "    div = det(xdiff, ydiff)\n",
    "    if div == 0:\n",
    "       raise Exception('lines do not intersect')\n",
    "\n",
    "    d = (det(*line1), det(*line2))\n",
    "    x = det(d, xdiff) / div\n",
    "    y = det(d, ydiff) / div\n",
    "    return x, y\n",
    "\n",
    "# [Y, Z]\n",
    "line_5 = [[1174.5, 35.49],[1154.21,0.35]]\n",
    "line_6 = [[1174.5, 41.49],[1150.75,0.35]]\n",
    "line_3078 = [[1174.45, 38.59], [-1155.05, 4073.41]]\n",
    "# BB: Y < 1174.70\n",
    "print(line_intersection(line_5, line_3078))\n",
    "print(line_intersection(line_6, line_3078))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2983.0"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tree = TC\n",
    "wire_charge_u = tree['wire_charge_u'].array()[0]\n",
    "np.array([sum(l) for l in wire_charge_u])\n",
    "sum(wire_charge_u[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(0.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pt13",
   "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.6.10 | packaged by conda-forge | (default, Apr 24 2020, 16:44:11) \n[GCC 7.3.0]"
  },
  "vscode": {
   "interpreter": {
    "hash": "b9483a4dd587476b98dd17ef78944d87a21a22a78f2af06dcfca96da16a6273c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
