Пример #1
0
 def test_gauss(self):
     x, N, mu, sigma = (1., 1., 0., 1.)
     self.assertEqual(utils.gauss(x, N, mu, sigma), self.gaussian(x, N, mu, sigma))
     N = 2.
     self.assertEqual(utils.gauss(x, N, mu, sigma), self.gaussian(x, N, mu, sigma))
     sigma = 2.
     self.assertEqual(utils.gauss(x, N, mu, sigma), self.gaussian(x, N, mu, sigma))
     x = 1e5
     self.assertEqual(utils.gauss(x, N, mu, sigma), 0.)
Пример #2
0
def analyse(name):
    data = genfromtxt('data/%s.tsv' % name, delimiter='\t', dtype=None,
                      names=['ext_timestamp', 'time_delta'])
    time_delta = data['time_delta']

    # Plot distribution
    counts, bins = histogram(time_delta, bins=arange(-10.5, 11.5, 1))
    plot = Plot()
    plot.histogram(counts, bins)
    x = (bins[1:] + bins[:-1]) / 2.
    popt, pcov = curve_fit(gauss, x, counts, p0=(sum(counts), 0., 2.5))
    plot.plot(x, gauss(x, *popt), mark=None)
    print popt
    plot.set_ylimits(min=0)
    plot.set_ylabel('Counts')
    plot.set_xlabel(r'Time delta [\si{\nano\second}]')
    plot.save_as_pdf(name)

    # Plot moving average
    n = 5000
    skip = 100
    moving_average = convolve(time_delta, ones((n,)) / n, mode='valid')
    plot = Plot()
    timestamps = (data['ext_timestamp'][:-n + 1:skip] -
                  data['ext_timestamp'][0]) / 1e9 / 3600.
    plot.plot(timestamps, moving_average[::skip], mark=None)
    plot.set_xlimits(min=0)
    plot.set_ylabel(r'time delta [\si{\nano\second}]')
    plot.set_xlabel('timestamp [\si{\hour}]')
    plot.save_as_pdf('moving_average_%s' % name)
Пример #3
0
    def test_gauss_array(self):
        """Test for arrays of random values"""

        n = 10000
        x, N, mu = random.uniform(-100, 100, size=(3, n))
        # sigma can not be 0
        sigma = random.uniform(1e-15, 100, size=n)
        value1 = utils.gauss(x, N, mu, sigma)
        value2 = self.gaussian(x, N, mu, sigma)
        self.assertTrue(all(abs(value1 - value2) < 1e-10))
Пример #4
0
def mod_ciampa_conv_full(a, b, d):
    """ Based on Ciampa 1998: zenith angle distribution

    Positive C parameter

    """
    x = arange(0.1, 85, 0.1)
    rx = radians(x)
    result = sum([
        gauss(rx, mod_ciampa(xi, a, b), rxi, radians(d / cos(rxi)))
        for xi, rxi in zip(x, rx)
    ],
                 axis=0)
    return rx, result
Пример #5
0
def plot_delta_histogram(ids, **kwargs):
    """ Plot a histogram of the deltas

    """
    if type(ids) is int:
        ids = [ids]

    # Bin width
    bin_size = 1  # 2.5*n

    # Begin Figure
    plot = Plot()
    for id in ids:
        ext_timestamps, deltas = get(id)
        low = floor(min(deltas))
        high = ceil(max(deltas))
        bins = np.arange(low - .5 * bin_size, high + bin_size, bin_size)
        n, bins = np.histogram(deltas, bins)
        bin_centers = (bins[:-1] + bins[1:]) / 2
        popt, pcov = curve_fit(gauss,
                               bin_centers,
                               n,
                               p0=[.15, np.mean(deltas),
                                   np.std(deltas)])
        plot.histogram(n, bins)
        plot.plot(bin_centers,
                  gauss(bin_centers, *popt),
                  mark=None,
                  linestyle='gray')
    if kwargs.keys():
        plot.set_title('Tijdtest ' + kwargs[kwargs.keys()[0]])
    plot.set_label(r'$\mu={1:.1f}$, $\sigma={2:.1f}$'.format(*popt))
    plot.set_xlabel(r'Time difference [ns]')
    plot.set_ylabel(r'Counts')
    plot.set_xlimits(low, high)
    plot.set_ylimits(min=0.)

    # Save Figure
    if len(ids) == 1:
        name = 'delta_histogram/tt_delta_hist_%03d' % ids[0]
    elif kwargs.keys():
        name = 'delta_histogram/tt_delta_hist_' + kwargs[kwargs.keys()[0]]
    plot.save_as_pdf(PLOT_PATH + name)

    print 'tt_analyse: Plotted histogram'
Пример #6
0
def plot_offset_distribution(ids, **kwargs):
    """Offset distribution"""

    # Begin Figure
    plot = Plot()
    offsets = [
        np.average([x for x in get(id)[1] if abs(x) < 100]) for id in ids
    ]
    bins = np.arange(-70, 70, 2)
    n, bins = np.histogram(offsets, bins)
    plot.histogram(n, bins)

    bin_centers = (bins[:-1] + bins[1:]) / 2
    popt, pcov = curve_fit(gauss,
                           bin_centers,
                           n,
                           p0=[1., np.mean(offsets),
                               np.std(offsets)])
    plot.plot(bin_centers,
              gauss(bin_centers, *popt),
              mark=None,
              linestyle='gray')

    if kwargs.keys():
        plot.set_title('Tijdtest offset distribution ' +
                       kwargs[kwargs.keys()[0]])
    plot.set_label(r'$\mu={1:.1f}$, $\sigma={2:.1f}$'.format(*popt))
    plot.set_xlabel(r'Offset [\si{\nano\second}]')
    plot.set_ylabel(r'Counts')
    plot.set_ylimits(min=0)

    # Save Figure
    name = 'box/tt_offset_distribution'
    if kwargs.keys():
        name += kwargs[kwargs.keys()[0]]
    plot.save_as_pdf(PLOT_PATH + name)

    print 'tt_analyse: Plotted offsets'
Пример #7
0
def determine_station_timing_offsets(data):
    """Determine the offsets between the stations."""

    c = .3

    ref_station_number = 501
    ref_d_off = DETECTOR_OFFSETS[ref_station_number]
    ref_events = data.root.hisparc.cluster_amsterdam.station_501.events
    ref_t = array([
        where(
            ref_events.col('t1') == -999, 9000,
            ref_events.col('t1') - ref_d_off[0]),
        where(
            ref_events.col('t2') == -999, 9000,
            ref_events.col('t2') - ref_d_off[1]),
        where(
            ref_events.col('t3') == -999, 9000,
            ref_events.col('t3') - ref_d_off[2]),
        where(
            ref_events.col('t4') == -999, 9000,
            ref_events.col('t4') - ref_d_off[3])
    ])
    ref_min_t = ref_t.min(axis=0)

    station_number = 510
    d_off = DETECTOR_OFFSETS[station_number]
    events = data.root.hisparc.cluster_amsterdam.station_510.events
    t = array([
        where(events.col('t1') == -999, 90000,
              events.col('t1') - d_off[0]),
        where(events.col('t2') == -999, 90000,
              events.col('t2') - d_off[1]),
        where(events.col('t3') == -999, 90000,
              events.col('t3') - d_off[2]),
        where(events.col('t4') == -999, 90000,
              events.col('t4') - d_off[3])
    ])
    min_t = t.min(axis=0)

    dt = []
    for event, ref_event in itertools.izip(events, ref_events):
        if (ref_event['t_trigger'] in ERR or event['t_trigger'] in ERR):
            dt.append(nan)
            continue
        dt.append((int(event['ext_timestamp']) -
                   int(ref_event['ext_timestamp'])) -
                  (event['t_trigger'] - ref_event['t_trigger']))

    dt = array(dt)
    dt = dt + (min_t - ref_min_t)

    plot = Plot()
    bins = linspace(-50, 50, 100)
    y, bins = histogram(dt, bins=bins)
    plot.histogram(y, bins)

    x = (bins[:-1] + bins[1:]) / 2
    try:
        popt, pcov = curve_fit(gauss, x, y, p0=(len(dt), 0., 10))
        station_offset = popt[1]
        plot.draw_vertical_line(station_offset)
        bins = linspace(-50, 50, 1000)
        plot.plot(bins, gauss(bins, *popt), mark=None, linestyle='gray')
    except RuntimeError:
        station_offset = 0.
    print station_offset

    plot.set_title('Time difference, station 510-501')
    plot.set_xlimits(-50, 50)
    plot.set_ylimits(min=0)
    plot.set_xlabel('$\Delta t$ [ns]')
    plot.set_ylabel('Counts')
    plot.save_as_pdf('station_offsets')
Пример #8
0
def plot_fit(x, y, popt, graph):
    graph.plot(x - BIN_WIDTH / 2, y, mark=None, use_steps=True)
    fit_x = arange(min(x), max(x), 0.1)
    graph.plot(fit_x, gauss(fit_x, *popt), mark=None, linestyle='gray')
Пример #9
0
def combine_delta_data():
    delta_data = genfromtxt('data/time_delta.tsv',
                            delimiter='\t',
                            dtype=None,
                            names=['ext_timestamp', 'time_delta'])
    delta_data = {ets: td for ets, td in delta_data}

    tm = []
    ts = []
    td = []
    te = []
    with tables.open_file('data/data.h5', 'r') as data:
        for row in data.root.s99.events:
            ets = row['ext_timestamp']
            try:
                dt = delta_data[ets]
            except KeyError:
                continue
            tm.append(row['t1'])
            ts.append(row['t3'])
            td.append(dt)
            te.append(ets)
    tm = array(tm)
    ts = array(ts)
    td = array(td)
    te = array(te)
    filter = (tm >= 0) & (ts >= 0)
    tm = tm.compress(filter)
    ts = ts.compress(filter)
    td = td.compress(filter)
    te = te.compress(filter)

    bins = arange(-10.25, 10.26, .5)
    mplot = MultiPlot(3, 2, width=r'.4\linewidth', height=r'.3\linewidth')
    mplot.show_xticklabels_for_all([(2, 0), (2, 1)])
    mplot.show_yticklabels_for_all([(0, 0), (1, 1), (2, 0)])
    mplot.set_xlimits_for_all(min=-11, max=11)
    mplot.set_ylimits_for_all(min=0, max=1500)
    mplot.set_ylabel('Counts')
    mplot.set_xlabel(r'Time difference [\si{\nano\second}]')

    plot = mplot.get_subplot_at(0, 0)
    counts, bins = histogram(tm - ts, bins=bins)
    popt = fit_timing_offset(counts / 3., bins)
    plot.set_label(r'$t_{M} - t_{S}$, $%.1f\pm%.1f$\si{\nano\second}, scaled' %
                   (popt[1], popt[2]))
    plot.histogram(counts / 3., bins)
    plot.plot(bins, gauss(bins, *popt), mark=None, linestyle='gray')

    plot = mplot.get_subplot_at(1, 0)
    plot.set_label(r'$t_{\delta}$')
    plot.histogram(*histogram(td, bins=bins))

    plot = mplot.get_subplot_at(2, 0)
    counts, bins = histogram(tm - (ts + td), bins=bins)
    popt = fit_timing_offset(counts, bins)
    plot.set_label(
        r'$t_{M} - (t_{S} + t_{\delta})$, $%.1f\pm%.1f$\si{\nano\second}' %
        (popt[1], popt[2]))
    plot.histogram(counts, bins)
    plot.plot(bins, gauss(bins, *popt), mark=None, linestyle='gray')

    plot = mplot.get_subplot_at(0, 1)
    plot.set_label(r'$t_{\delta}$ where $t_{M} - t_{S} == -5$')
    plot.draw_vertical_line(-5, linestyle='gray')
    plot.histogram(*histogram(td.compress(tm - ts == -5), bins=bins))
    plot = mplot.get_subplot_at(1, 1)
    plot.set_label(r'$t_{\delta}$ where $t_{M} - t_{S} == -2.5$')
    plot.draw_vertical_line(-2.5, linestyle='gray')
    plot.histogram(*histogram(td.compress(tm - ts == -2.5), bins=bins))
    plot = mplot.get_subplot_at(2, 1)
    plot.set_label(r'$t_{\delta}$ where $t_{M} - t_{S} == 0$')
    plot.draw_vertical_line(0, linestyle='gray')
    plot.histogram(*histogram(td.compress(tm - ts == 0), bins=bins))
    mplot.save_as_pdf('dt_time_delta')

    plot = Plot()

    counts, xbins, ybins = histogram2d(tm - ts, td, bins=bins)
    plot.histogram2d(counts, xbins, ybins, bitmap=True, type='reverse_bw')
    plot.set_xlabel(r'$t_{M} - t_{S}$ [\si{\nano\second}]')
    plot.set_ylabel(r'$t_{\delta}$ [\si{\nano\second}]')

    plot.save_as_pdf('dt_vs_time_delta')
Пример #10
0
def plot_data_and_fit(x, y, popt, plot):
    plot.plot(x - BIN_WIDTH / 2, y, mark=None, use_steps=True)
    fit_x = arange(min(x), max(x), 0.1)
    plot.plot(fit_x, gauss(fit_x, *popt), mark=None, linestyle='gray')