{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#Generate Additional Tables#"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "##Preamble\n",
      "from __future__ import division,print_function\n",
      "import pandas as pds\n",
      "\n",
      "rc('figure',figsize=(12,4))\n",
      "rc('legend',loc='best')\n",
      "rc('font',size = 14.0)\n",
      "import os\n",
      "from os.path import expanduser\n",
      "\n",
      "%load_ext autoreload\n",
      "%autoreload 2\n",
      "\n",
      "import scipy.optimize as opt\n",
      "from scipy import stats\n",
      "import uncertainties as unc\n",
      "from uncertainties import unumpy\n",
      "\n",
      "import ProcessingCTRData as pc\n",
      "\n",
      "print(\"Complete\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Complete\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##Extended CTR for 30mm##"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "df = pds.read_csv('alldata.csv',index_col=[0,1])\n",
      "\n",
      "df = df[df.length == 30]\n",
      "df = df[df.CTR < 300]\n",
      "df = df[df.specialkey != 'normal']\n",
      "\n",
      "chifit = [pc.getchi(grp,plot=False) for key, grp in df.groupby(['SampleB','configuration'])]\n",
      "\n",
      "for col in df.columns:\n",
      "    if col.endswith('err'):\n",
      "        df[col[:-3]] = df.apply(lambda srs : unc.ufloat(srs[col[:-3]],srs[col]),axis=1)\n",
      "        df = df.drop(col,1)\n",
      "    \n",
      "df['ggevents'] = unumpy.uarray(list(df.numofsamples),list(sqrt(df.numofsamples)))\n",
      "\n",
      "def ProperMean(agrp):\n",
      "    if isinstance(agrp.dtype,object):\n",
      "        return mean(list(agrp))\n",
      "    else:\n",
      "        return mean(agrp)\n",
      "    \n",
      "\n",
      "df = df.groupby(['SampleB','configuration']).aggregate(ProperMean)\n",
      "print(df.CTR)\n",
      "\n",
      "df['chifit'] = chifit\n",
      "\n",
      "fdf = list(df.CTR)\n",
      "#df = df.sort('CTR',ascending=False)\n",
      "\n",
      "\n",
      "for col in df.columns:\n",
      "    if col in ['length', 'ggevents']:\n",
      "        ff = \"{0:.0f}\"\n",
      "    elif col in ['ERleft','ERright']:\n",
      "        ff = \"{0:.2f}\"\n",
      "    else:\n",
      "        ff = \"{0:.1f}\"\n",
      "    try:\n",
      "        df[col] = [ff.format(val).replace(\"+/-\",\"$\\pm$\") for val in df[col]]\n",
      "    except ValueError:\n",
      "        continue\n",
      "\n",
      "print(df.to_latex(float_format=lambda x: '%10.1f' % x, index=True,columns=[\"ERleft\",\"ERright\",\"ggevents\",\"location\",\"CTR\",\"chisquared\",\"chifit\"]))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "SampleB  configuration\n",
        "30A      wrapped             273+/-15\n",
        "30B      wrapped          246.4+/-2.3\n",
        "Name: CTR, dtype: object\n",
        "\\begin{tabular}{llllllll}\n",
        "\\toprule\n",
        "{} &          ERleft &         ERright &     ggevents &       location &             CTR & chisquared & chifit \\\\\n",
        "SampleB configuration &                 &                 &              &                &                 &            &        \\\\\n",
        "\\midrule\n",
        "30A     wrapped       &  10.24$\\pm$0.11 &  15.91$\\pm$0.16 &   253$\\pm$11 &  261.6$\\pm$4.5 &  273.1$\\pm$14.6 &        0.9 &    0.3 \\\\\n",
        "30B     wrapped       &   9.47$\\pm$0.02 &  13.29$\\pm$0.02 &  2713$\\pm$20 &   16.3$\\pm$0.7 &   246.4$\\pm$2.3 &        1.6 &    0.6 \\\\\n",
        "\\bottomrule\n",
        "\\end{tabular}\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##20mm LYSO:Ce Measurements##"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "df = pds.read_csv('alldata.csv',index_col=[0,1])\n",
      "\n",
      "df = df[df.length == 20]\n",
      "df = df[df.CTR < 300]\n",
      "df = df[df.SampleB != '2396']\n",
      "\n",
      "chifit = [pc.getchi(grp,plot=False) for key, grp in df.groupby(['SampleB','configuration'])]\n",
      "\n",
      "for col in df.columns:\n",
      "    if col.endswith('err'):\n",
      "        df[col[:-3]] = df.apply(lambda srs : unc.ufloat(srs[col[:-3]],srs[col]),axis=1)\n",
      "        df = df.drop(col,1)\n",
      "    \n",
      "df['ggevents'] = unumpy.uarray(list(df.numofsamples),list(sqrt(df.numofsamples)))\n",
      "\n",
      "def ProperMean(agrp):\n",
      "    if isinstance(agrp.dtype,object):\n",
      "        return mean(list(agrp))\n",
      "    else:\n",
      "        return mean(agrp)\n",
      "    \n",
      "\n",
      "df = df.groupby(['SampleB','configuration']).aggregate(ProperMean)\n",
      "print(df.CTR)\n",
      "\n",
      "df['chifit'] = chifit\n",
      "\n",
      "fdf = list(df.CTR)\n",
      "#df = df.sort('CTR',ascending=False)\n",
      "\n",
      "\n",
      "for col in df.columns:\n",
      "    if col in ['length', 'ggevents']:\n",
      "        ff = \"{0:.0f}\"\n",
      "    elif col in ['ERleft','ERright']:\n",
      "        ff = \"{0:.2f}\"\n",
      "    else:\n",
      "        ff = \"{0:.1f}\"\n",
      "    try:\n",
      "        df[col] = [ff.format(val).replace(\"+/-\",\"$\\pm$\") for val in df[col]]\n",
      "    except ValueError:\n",
      "        continue\n",
      "\n",
      "print(df.to_latex(float_format=lambda x: '%10.1f' % x, index=True,columns=[\"ERleft\",\"ERright\",\"ggevents\",\"location\",\"CTR\",\"chisquared\",\"chifit\"]))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "SampleB  configuration\n",
        "20A      wrapped          228+/-7\n",
        "20B      wrapped          230+/-4\n",
        "Name: CTR, dtype: object\n",
        "\\begin{tabular}{llllllll}\n",
        "\\toprule\n",
        "{} &          ERleft &         ERright &     ggevents &       location &            CTR & chisquared & chifit \\\\\n",
        "SampleB configuration &                 &                 &              &                &                &            &        \\\\\n",
        "\\midrule\n",
        "20A     wrapped       &  10.86$\\pm$0.03 &  15.78$\\pm$0.03 &  1394$\\pm$17 &  206.0$\\pm$1.6 &  227.7$\\pm$6.7 &        1.2 &    0.4 \\\\\n",
        "20B     wrapped       &  11.00$\\pm$0.02 &  14.42$\\pm$0.02 &  1601$\\pm$13 &  224.5$\\pm$1.0 &  230.4$\\pm$4.2 &        1.2 &    0.9 \\\\\n",
        "\\bottomrule\n",
        "\\end{tabular}\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##Threshold Measurements#"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "df = pds.read_csv('alldata.csv',index_col=[0,1])\n",
      "\n",
      "data = \"1.640\t1.560\t1.640\t1.560\t72.4\t72.7\t5 1.640\t1.560\t1.700\t1.500\t72.4\t72.7\t20 1.640\t1.560\t1.900\t1.300\t72.4\t72.7\t20 1.640\t1.560\t2.100\t1.100\t72.4\t72.7\t20\"\n",
      "data = array([row.split('\\t') for row in data.split(' ')],dtype=float)\n",
      "Voltages = (data[:,2]-data[:,3])*1e3\n",
      "df = df[df.specialkey == 'threshold']\n",
      "\n",
      "df['threshold'] = df.uniquename.apply(lambda astr : Voltages[int(astr.split('_')[-1])])\n",
      "\n",
      "df = df[df.length == 30]\n",
      "#df = df[df.CTR < 300]\n",
      "    \n",
      "for col in df.columns:\n",
      "    if col.endswith('err'):\n",
      "        df[col[:-3]] = df.apply(lambda srs : unc.ufloat(srs[col[:-3]],srs[col]),axis=1)\n",
      "        df = df.drop(col,1)\n",
      "    \n",
      "df['ggevents'] = unumpy.uarray(list(df.numofsamples),list(sqrt(df.numofsamples)))\n",
      "\n",
      "def ProperMean(agrp):\n",
      "    if isinstance(agrp.dtype,object):\n",
      "        return mean(list(agrp))\n",
      "    else:\n",
      "        return mean(agrp)\n",
      "    \n",
      "\n",
      "ptpchange = ptp(df.groupby(['SampleB','threshold']).CTR)\n",
      "\n",
      "df = df.groupby(['SampleB','threshold']).aggregate(ProperMean)\n",
      "df['ptpchange'] = ptpchange\n",
      "for col in df.columns:\n",
      "    if col in ['length', 'ggevents']:\n",
      "        ff = \"{0:.0f}\"\n",
      "    elif col in ['ERleft','ERright']:\n",
      "        ff = \"{0:.2f}\"\n",
      "    else:\n",
      "        ff = \"{0:.1f}\"\n",
      "    try:\n",
      "        df[col] = [ff.format(val).replace(\"+/-\",\"$\\pm$\") for val in df[col]]\n",
      "    except ValueError:\n",
      "        continue\n",
      "\n",
      "print(df.to_latex(float_format=lambda x: '%10.1f' % x, index=True,columns=['ptpchange',\"ERleft\",\"ERright\",\"ggevents\",\"location\",\"CTR\",\"chisquared\"]))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\\begin{tabular}{llllllll}\n",
        "\\toprule\n",
        "{} &       ptpchange &          ERleft &         ERright &     ggevents &       location &             CTR & chisquared \\\\\n",
        "SampleB threshold &                 &                 &                 &              &                &                 &            \\\\\n",
        "\\midrule\n",
        "30A     80        &  102.8$\\pm$35.3 &  10.24$\\pm$0.07 &  15.61$\\pm$0.10 &    245$\\pm$7 &  346.7$\\pm$3.1 &  308.3$\\pm$10.6 &        0.6 \\\\\n",
        "        200       &   35.7$\\pm$16.8 &  10.01$\\pm$0.04 &  15.78$\\pm$0.05 &  1098$\\pm$15 &  550.2$\\pm$1.6 &   337.4$\\pm$5.5 &        1.0 \\\\\n",
        "        600       &   34.5$\\pm$14.9 &   9.98$\\pm$0.04 &  16.10$\\pm$0.05 &  1221$\\pm$16 &  789.8$\\pm$1.6 &   358.7$\\pm$5.4 &        1.0 \\\\\n",
        "        1000      &   38.0$\\pm$15.7 &  10.12$\\pm$0.04 &  16.01$\\pm$0.06 &  1197$\\pm$17 &  898.6$\\pm$1.9 &   359.5$\\pm$5.9 &        1.1 \\\\\n",
        "\\bottomrule\n",
        "\\end{tabular}\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 2
    }
   ],
   "metadata": {}
  }
 ]
}