{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#b: 46193\n"
     ]
    }
   ],
   "source": [
    "import uproot\n",
    "import numpy as np\n",
    "root_file = uproot.open(\"result_5384_130_6501.root\")\n",
    "root_file.allkeys()\n",
    "TC = root_file['TC']\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 _bsignature(tree, entry=0):\n",
    "    cluster_id = tree[\"cluster_id\"].array()[entry]\n",
    "    cluster_id = np.expand_dims(cluster_id, axis=1)\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((cluster_id,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",
    "\n",
    "def _csignature(tree, entry=0):\n",
    "    bsigs = _bsignature(tree, entry)\n",
    "    print(f'#b: {bsigs.shape[0]}')\n",
    "    clusters = [bsigs[bsigs[:,0] == cluster_id, :] for cluster_id in np.unique(bsigs[:,0])]\n",
    "    def sig(cluster):\n",
    "        cluster_id = cluster[0,0]\n",
    "        min_start = min(cluster[:,1])\n",
    "        max_start = max(cluster[:,1])\n",
    "        nblob = cluster.shape[0]\n",
    "        min_u = min(cluster[:,3])\n",
    "        max_u = min(cluster[:,4])\n",
    "        min_v = min(cluster[:,5])\n",
    "        max_v = min(cluster[:,6])\n",
    "        min_w = min(cluster[:,7])\n",
    "        max_w = min(cluster[:,8])\n",
    "        return np.array([min_start, max_start, nblob, min_u, max_u, min_v, max_v, min_w, max_w])\n",
    "    csigs = [sig(cluster) for cluster in clusters]\n",
    "    csigs = np.array(csigs)\n",
    "    return csigs\n",
    "\n",
    "csigs = _csignature(TC, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1873, 9)\n",
      "[[   0 1012  425 1294 1295 3558 3558 6678 6679]\n",
      " [   0  460  261 1947 1949 4329 4330 8091 8092]\n",
      " [  32   48    5 2032 2033 4376 4377 8137 8139]\n",
      " [  60   60    1 1507 1507 4028 4028 7263 7263]\n",
      " [  72  120   22 1493 1493 4028 4028 7249 7250]\n",
      " [  80  104   12 1497 1497 4005 4005 7230 7231]\n",
      " [ 192  200    9 1860 1860 4068 4068 7657 7657]\n",
      " [ 220  236    5 1877 1877 4115 4115 7720 7721]\n",
      " [ 220  236    5 1877 1877 4503 4508 8109 8113]\n",
      " [ 228  236    3 1877 1877 4047 4048 7653 7653]]\n"
     ]
    }
   ],
   "source": [
    "print(csigs.shape)\n",
    "print(csigs[0:10,:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "len(csigs.shape)"
   ]
  },
  {
   "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"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b9483a4dd587476b98dd17ef78944d87a21a22a78f2af06dcfca96da16a6273c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
