{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "\n",
    "import argparse\n",
    "import os\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from utils import h5_utils as h5u\n",
    "\n",
    "\n",
    "def roi_count(f0, th0 = 0):\n",
    "    '''\n",
    "    f0 is the denominator\n",
    "    '''\n",
    "    f0m = f0.copy()\n",
    "    f0m[f0m<=th0] = 0\n",
    "    f0m[f0m>th0] = 1\n",
    "    roi_lens = []\n",
    "    print('f0m.shape[1]', f0m.shape[1])\n",
    "    for ich in range(0, f0m.shape[1]):\n",
    "        start = 0\n",
    "        end = 0\n",
    "        for it in range(0, f0m.shape[0]):\n",
    "            if f0m[it,ich] <= 0:\n",
    "                if start < end:\n",
    "                    roi_lens.append(end-start+1)\n",
    "                start = it\n",
    "            else:\n",
    "                end = it\n",
    "    return roi_lens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(6000, 800)\n",
      "f0m.shape[1] 800\n",
      "3886\n",
      "ROIs/event: 3886.0\n"
     ]
    }
   ],
   "source": [
    "ma_tags = ['frame_ductor0']\n",
    "\n",
    "rebin = [1, 1]\n",
    "x_range = [800, 1600]\n",
    "y_range = [0, 6000]\n",
    "z_scale = 4000\n",
    "truth_th = 100\n",
    "\n",
    "# x_range = [800, 900]\n",
    "# y_range = [800, 1500]\n",
    "truth_th = 100\n",
    "\n",
    "nevents = 1\n",
    "ids = list(np.arange(nevents))\n",
    "masks = h5u.get_masks('data/cosmic-tru-0.h5', ids, ma_tags, rebin, x_range, y_range, truth_th)\n",
    "\n",
    "roi_lens = []\n",
    "for mask in masks :\n",
    "    mask = np.transpose(mask, [1, 0])\n",
    "    print(mask.shape)\n",
    "    roi_lens += roi_count(mask)\n",
    "    print(len(roi_lens))\n",
    "    plt.imshow(mask, aspect='auto', origin='low')\n",
    "    plt.show()\n",
    "\n",
    "print('ROIs/event: {}'.format(len(roi_lens)/nevents))\n",
    "\n",
    "plt.hist(np.array(roi_lens), 1000, range=(0, 100), density=True, histtype='step', linewidth=2, cumulative=True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.07334019557385486"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.count_nonzero(np.array(roi_lens)<10)/len(roi_lens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib tk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.hist(np.array(roi_lens), 1000, range=(0, 100), density=True, histtype='step', linewidth=2, cumulative=True)\n",
    "plt.xlabel('ROI length [tick]')\n",
    "plt.grid()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3886"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(roi_lens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
