In [1]:
import comptools as comp
import comptools.analysis.plotting as plotting
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import datetime
import multiprocessing as mp
import os

%matplotlib inline


/home/jbourbeau/.virtualenvs/composition/lib/python2.7/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)

Detector livetime calculation

For a reference see the IT-73 spectrum livetime reference.


In [2]:
config = 'IC86.2015'

In [3]:
time_bins = np.linspace(0, 2, 101)
time_midpoints = (time_bins[1:] + time_bins[:-1]) / 2
time_mask = time_midpoints < 1.4

In [4]:
def livetime_fit_func(t, I0, T):
    return I0 * np.exp(-t/T)

In [5]:
def get_livetime_counts_and_fit(month=None):
    # Get time difference histogram counts from level3 pickle files
    counts = comp.datafunctions.get_level3_livetime_hist(config, month)
    # Fit decaying exponential to histogram
    popt, pcov = curve_fit(livetime_fit_func, time_midpoints[time_mask], counts[time_mask],
                           sigma=np.sqrt(counts[time_mask]), p0=[1e5, 1e-2])
    I0_fit, T_fit = popt
    I0_fit_err, T_fit_err = np.sqrt(np.diag(pcov))
    # Get livetime from fit parameters
    livetime = T_fit * np.sum(counts)
    livetime_err = T_fit_err * np.sum(counts)

    data_dict = {'month': month, 'counts': counts,
                 'livetime': livetime, 'livetime_err': livetime_err,
                 'I0_fit': I0_fit, 'I0_fit_err': I0_fit_err,
                 'T_fit': T_fit, 'T_fit_err': T_fit_err}
    
    month_str = datetime.date(2000, month, 1).strftime('%B')
    print('Completed month {}'.format(month_str))
    
    return data_dict

In [6]:
pool = mp.Pool(processes=12)
results = [pool.apply_async(get_livetime_counts_and_fit, args=(month,)) for month in range(1, 13)]
output = [p.get() for p in results]


Completed month September
Completed month June
Completed month July
Completed month August
Completed month October
Completed month March
Completed month May
Completed month April
Completed month February
Completed month November
Completed month January
Completed month December

In [7]:
df = pd.DataFrame(output)
df.set_index('month', inplace=True)
df


Out[7]:
I0_fit I0_fit_err T_fit T_fit_err counts livetime livetime_err
month
1 1.579515e+06 608.076515 0.180825 0.000050 [1493456, 1339580, 1198216, 1071562, 961494, 8... 2.581019e+06 708.479729
2 1.420298e+06 668.251549 0.185879 0.000062 [1344370, 1209205, 1087552, 975272, 876367, 78... 2.452463e+06 824.127099
3 1.656536e+06 662.358312 0.175920 0.000050 [1564252, 1396838, 1247304, 1113447, 993627, 8... 2.561921e+06 729.451304
4 1.687668e+06 1222.198962 0.177452 0.000092 [1597671, 1430569, 1278396, 1138783, 1016323, ... 2.655905e+06 1371.892570
5 1.975748e+06 2245.839538 0.168949 0.000137 [1872983, 1660765, 1475746, 1308413, 1160716, ... 2.818350e+06 2283.262450
6 2.006173e+06 843.549167 0.159763 0.000048 [1883835, 1664406, 1467061, 1296341, 1144333, ... 2.558643e+06 763.634957
7 2.347619e+06 946.806924 0.150970 0.000043 [2196047, 1928974, 1687626, 1477890, 1293906, ... 2.673408e+06 764.560761
8 2.240288e+06 1314.564451 0.153283 0.000064 [2101174, 1847769, 1618626, 1420731, 1246069, ... 2.630042e+06 1095.265210
9 2.168127e+06 1727.793375 0.153465 0.000087 [2035471, 1789258, 1569718, 1376520, 1206056, ... 2.551430e+06 1444.007633
10 1.897318e+06 810.515714 0.167027 0.000051 [1787058, 1589917, 1406810, 1247360, 1108837, ... 2.645021e+06 803.259335
11 1.778556e+06 866.832517 0.170043 0.000059 [1676382, 1496194, 1325879, 1180232, 1047109, ... 2.569845e+06 891.149577
12 1.564557e+06 554.915573 0.182470 0.000046 [1480215, 1329793, 1190768, 1065388, 955536, 8... 2.603305e+06 658.597149

In [8]:
fig, axarr = plt.subplots(3, 4, figsize=(10,8), sharex=True, sharey=True)
for month, ax in zip(df.index, axarr.flatten()):
    row = df.loc[month]
    counts = row['counts']
    I0_fit = row['I0_fit']
    T_fit = row['T_fit']
    livetime = row['livetime']
    livetime_err = row['livetime_err']
    livetime_str = 'Livetime [s]:\n{:0.2e} +/- {:0.2f}'.format(livetime, livetime_err)
    
    # Plot time difference histogram and corresponding fit
    plotting.plot_steps(time_bins, counts, ax=ax)
    ax.plot(time_midpoints, livetime_fit_func(time_midpoints, I0_fit, T_fit),
            marker='None', ls='-', c='C1')
    month_str = datetime.date(2000, month, 1).strftime('%B')
    ax.set_title(month_str)
    ax.set_xlim((0, 2))
    ax.set_yscale('log', nonposy='clip')
    ax.text(0.6, 2e5, livetime_str)
    ax.grid()
    
fig.text(0.5, -0.025, 'Time between events [s]', ha='center', fontsize=16)
fig.text(-0.025, 0.5, 'Counts', va='center', rotation='vertical', fontsize=16)
fig.text(0.5, 1.01, config, ha='center', fontsize=20)
plt.tight_layout()
outfile = os.path.join(comp.paths.figures_dir, 'livetime', 
                       'livetime-array-{}.png'.format(config))
comp.check_output_dir(outfile)
plt.savefig(outfile)
plt.show()


/home/jbourbeau/.virtualenvs/composition/lib/python2.7/site-packages/matplotlib/figure.py:1743: UserWarning: This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect.
  warnings.warn("This figure includes Axes that are not "

In [20]:
full_livetime = df['livetime'].sum()
full_livetime_err = np.sqrt(np.sum([err**2 for err in df['livetime_err']]))
print('Full {} livetime [s]:\n{:0.2e} +/- {:0.2f}'.format(config, full_livetime, full_livetime_err))


Full IC86.2015 livetime [s]:
3.13e+07 +/- 3890.27

In [25]:
data_dict = {'livetime(s)': full_livetime, 'livetime_err(s)': full_livetime_err}
for month in df.index:
    month_str = datetime.date(2000, month, 1).strftime('%B')
    data_dict[month_str + '_livetime(s)'] = df.loc[month]['livetime']
    data_dict[month_str + '_livetime_err(s)'] = df.loc[month]['livetime_err']

livetime_file = comp.get_livetime_file()
try:
    livetime_df = pd.read_csv(livetime_file, index_col=0)
    livetime_df.loc[config] = data_dict
except IOError:
    livetime_df = pd.DataFrame(data_dict, index=[config])
livetime_df.to_csv(livetime_file)

In [26]:
livetime_df


Out[26]:
April_livetime(s) April_livetime_err(s) August_livetime(s) August_livetime_err(s) December_livetime(s) December_livetime_err(s) February_livetime(s) February_livetime_err(s) January_livetime(s) January_livetime_err(s) ... May_livetime(s) May_livetime_err(s) November_livetime(s) November_livetime_err(s) October_livetime(s) October_livetime_err(s) September_livetime(s) September_livetime_err(s) livetime(s) livetime_err(s)
IC86.2012 2.474154e+06 836.110595 2.636265e+06 719.180507 2.352788e+06 629.580407 2.238360e+06 529.761190 2.388066e+06 570.728817 ... 1.500543e+06 888.218841 2.242179e+06 576.724000 2.570781e+06 692.250320 2.500055e+06 696.293602 2.843990e+07 2698.989669
IC86.2013 2.452304e+06 688.627658 2.594000e+06 2463.491184 2.506116e+06 659.215037 2.370605e+06 625.592480 2.388579e+06 658.201505 ... 2.962786e+06 3008.302126 2.551393e+06 1216.193382 2.636249e+06 750.623435 2.554584e+06 1498.810049 3.068700e+07 4945.279495
IC86.2014 2.564014e+06 891.962099 2.661381e+06 1315.671707 2.615383e+06 703.815820 2.373259e+06 710.629478 1.837001e+06 540.662910 ... 3.634068e+06 2540.014766 2.497581e+06 838.311704 2.649791e+06 1661.174582 2.547906e+06 1193.722847 3.111701e+07 4224.617190
IC86.2015 2.655905e+06 1371.892570 2.630042e+06 1095.265210 2.603305e+06 658.597149 2.452463e+06 824.127099 2.581019e+06 708.479729 ... 2.818350e+06 2283.262450 2.569845e+06 891.149577 2.645021e+06 803.259335 2.551430e+06 1444.007633 3.130135e+07 3890.267806

4 rows × 26 columns


In [ ]: