Ejemplo n.º 1
0
def get_coincidences_from_esd_in_range(start, end, stations, n):
    """Get coincidences from ESD in time range.

    :param start: start of datetime range
    :param end: end of datetime range
    :param stations: station numbers
    :param n: minimum number of events in coincidence
    :yield: id, station number and event

    """
    id = -1
    for t0, t1 in single_day_ranges(start, end):
        try:
            NetworkSummary.objects.get(date=t0)
        except NetworkSummary.DoesNotExist:
            continue
        with tables.open_file(esd.get_esd_data_path(t0)) as f:
            try:
                cq = CoincidenceQuery(f)
                ts0 = datetime_to_gps(t0)
                ts1 = datetime_to_gps(t1)
                if stations:
                    coincidences = cq.at_least(stations, n, start=ts0, stop=ts1)
                    events = cq.events_from_stations(coincidences, stations, n)
                else:
                    coincidences = cq.timerange(start=ts0, stop=ts1)
                    events = cq.all_events(coincidences, n)
                for id, coin in enumerate(events, id + 1):
                    for number, event in coin:
                        yield id, number, event
            except (IOError, tables.NoSuchNodeError):
                continue
Ejemplo n.º 2
0
def determine_dt_for_pair(stations):
    """Determine and store dt for a pair of stations

    :param ref_station: reference station number to use as refernece
    :param station: station number to determine the dt for

    """
    path = DATA_PATH + 'dt_ref%d_%d.h5' % stations
    if os.path.exists(path):
        print 'dt data already exists for %d-%d' % stations
        return

    ref_station, station = stations
    try:
        with tables.open_file(PAIR_DATAPATH % tuple(sorted(stations)),
                              'r') as data:
            cq = CoincidenceQuery(data)
            ref_detector_offsets = Station(ref_station).detector_timing_offset
            detector_offsets = Station(station).detector_timing_offset
            for dt0, dt1 in monthrange((2004, 1), (2015, 9)):
                coins = cq.all(stations, start=dt0, stop=dt1, iterator=True)
                coin_events = cq.events_from_stations(coins, stations)
                ets, dt = determine_time_differences(coin_events, ref_station,
                                                     station,
                                                     ref_detector_offsets,
                                                     detector_offsets)
                store_dt(ref_station, station, ets, dt)
    except Exception as e:
        print 'Failed for %d, %d' % stations
        print e
        return
Ejemplo n.º 3
0
    def find_coincidence(self, date, session):
        """Find coincidences for the given cluster on the given date

        Store the found coincidences and events in the database.
        Then return the number of found coincidences.

        """
        stations = Station.objects.filter(cluster=self.cluster,
                                          pcs__is_test=False).distinct().values_list('number', flat=True)
        path = get_esd_data_path(date)

        if not os.path.isfile(path):
            # No data file, so no coincidences
            return 0

        number_of_coincidences = 0

        # Get all coincidences containing stations in the requested cluster
        with tables.open_file(path, 'r') as data:
            cq = CoincidenceQuery(data)
            all_coincidences = cq.any(stations)
            coincidences = cq.events_from_stations(all_coincidences, stations, n=3)
            for coincidence in coincidences:
                # Todo: Double check for multiple events from same station,
                self.save_coincidence(coincidence, session)
                number_of_coincidences += 1

        return number_of_coincidences
Ejemplo n.º 4
0
def analyse(data, id):
    event_node = data.get_node('/station_99/events')
    print 'Total number of events: %d' % event_node.nrows
    cq = CoincidenceQuery(data)
    coincidences = cq.all(stations=[99])
    coincident_events = cq.events_from_stations(coincidences, [99], n=1)
    coincident_event_ids = [e[0][1]['event_id'] for e in coincident_events]
    event_ids = [i for i in range(event_node.nrows)
                 if i not in coincident_event_ids]
    events = event_node.read_coordinates(event_ids)
    coincident_events = event_node.read_coordinates(coincident_event_ids)
    print 'Total number of events not in coincidence: %d' % len(events)
    print 'Total number of events in coincidence: %d' % len(coincident_events)

    cph1 = coincident_events['pulseheights'][:, 0]
    cph2 = coincident_events['pulseheights'][:, 1]
    ph1 = events['pulseheights'][:, 0]
    ph2 = events['pulseheights'][:, 1]
    plot = Plot()
    bins = np.arange(0, 4000, 50)
    for ph, ls in [(cph1, 'black,dotted'), (cph2, 'red,dotted'),
                   (ph1, 'black'), (ph2, 'red')]:
        counts, bins = np.histogram(ph, bins=bins)
        plot.histogram(counts, bins, linestyle=ls)
    plot.set_xlimits(min=0, max=4000)
    plot.set_ylimits(min=.5)
    plot.set_ylabel('Counts')
    plot.set_xlabel('Pulseheight [ADC]')
    plot.save_as_pdf('muonlab_pulseheights_%d' % id)

    cdt = coincident_events['t2'] - coincident_events['t1']
    dt = events['t2'] - events['t1']
    plot = Plot()
    bins = np.arange(-100, 100, 2.5)
    for t, ls in [(dt, ''), (cdt, 'dotted')]:
        counts, bins = np.histogram(t, bins=bins)
        plot.histogram(counts, bins, linestyle=ls)
    plot.set_ylimits(min=0)
    plot.set_ylabel('Counts')
    plot.set_xlabel('Time difference [ns]')
    plot.save_as_pdf('muonlab_dt_%d' % id)
Ejemplo n.º 5
0
Archivo: main.py Proyecto: 153957/topaz
    501: 'black',
    502: 'red!80!black',
    503: 'blue!80!black',
    504: 'green!80!black',
    505: 'orange!80!black',
    506: 'pink!80!black',
    508: 'blue!40!black',
    509: 'red!40!black'
}

if __name__ == "__main__":
    with tables.open_file(COIN_DATA, 'r') as data:
        cq = CoincidenceQuery(data)
        coincidence = cq.coincidences[4323]
        coincidence_events = next(
            cq.events_from_stations([coincidence], STATIONS))
        reconstruction = cq._get_reconstruction(coincidence)
        core_x = reconstruction['x']
        core_y = reconstruction['y']

        plot = Plot()

        ref_extts = coincidence_events[0][1]['ext_timestamp']

        distances = arange(1, 370, 1)
        times = (2.43 * (1 + distances / 30.)**1.55) + 20
        plot.plot(distances, times, mark=None)

        for station_number, event in coincidence_events:
            station = CLUSTER.get_station(station_number)
            offsets = OFFSETS[station_number]
Ejemplo n.º 6
0
def determine_station_timing_offsets(d, data):
    # First determine detector offsets for each station
    offsets = {}
    for s in [501, 510]:
        station_group = data.get_node('/hisparc/cluster_amsterdam/station_%d' % s)
        offsets[s] = determine_detector_timing_offsets2(station_group.events)

    ref_station = 501
    ref_d_off = offsets[ref_station]

    station = 510

    cq = CoincidenceQuery(data, '/coincidences')
    dt = []
    d_off = offsets[station]
    stations = [ref_station, station]
    coincidences = cq.all(stations)
    c_events = cq.events_from_stations(coincidences, stations)
    for events in c_events:
        # Filter for possibility of same station twice in coincidence
        if len(events) is not 2:
            continue
        if events[0][0] == ref_station:
            ref_event = events[0][1]
            event = events[1][1]
        else:
            ref_event = events[1][1]
            event = events[0][1]

        try:
            ref_t = min([ref_event['t%d' % (i + 1)] - ref_d_off[i]
                         for i in range(4)
                         if ref_event['t%d' % (i + 1)] not in ERR])
            t = min([event['t%d' % (i + 1)] - d_off[i]
                     for i in range(4)
                     if event['t%d' % (i + 1)] not in ERR])
        except ValueError:
            continue
        if (ref_event['t_trigger'] in ERR or event['t_trigger'] in ERR):
            continue
        dt.append((int(event['ext_timestamp']) -
                   int(ref_event['ext_timestamp'])) -
                  (event['t_trigger'] - ref_event['t_trigger']) +
                  (t - ref_t))

    bins = linspace(-150, 150, 200)
    y, bins = histogram(dt, bins=bins)
    x = (bins[:-1] + bins[1:]) / 2
    try:
        popt, pcov = curve_fit(gauss, x, y, p0=(len(dt), 0., 50))
        station_offset = popt[1]
    except RuntimeError:
        station_offset = 0.
    offsets[station] = [detector_offset + station_offset
                        for detector_offset in offsets[station]]
    print 'Station 501 - 510: %f (%f)' % (popt[1], popt[2])
    graph = Plot()
    graph.histogram(y, bins)
    graph.set_title('Time difference, between station 501-510')
    graph.set_label('%s' % d.replace('_', ' '))
    graph.set_xlimits(-150, 150)
    graph.set_ylimits(min=0)
    graph.set_xlabel('$\Delta t$')
    graph.set_ylabel('Counts')
    graph.save_as_pdf('%s' % d)