Esempio n. 1
0
def test_alpha_computation_for_aligned_showers():
    x = np.linspace(-100, 100, num=100)
    y = np.linspace(-100, 100, num=100)
    np.random.shuffle(y)
    data = {'x': x, 'y': y}
    data['r'] = np.sqrt(data['x']**2 + data['y']**2)
    data['phi'] = np.arctan2(data['y'], data['x'])
    data['psi'] = data['phi']
    data = pd.DataFrame(data)
    alpha_1 = np.array(compute_alpha(data['phi'], data['psi']))
    assert (alpha_1 == 0).all()
Esempio n. 2
0
def test_alpha_computation_for_missaligned_showers():
    thetas = np.linspace(0, np.pi / 2, num=100)
    for theta in thetas:
        miss_alignement = theta
        x = np.linspace(-100, 100, num=100)
        y = np.linspace(-100, 100, num=100)
        np.random.shuffle(y)
        data = dict({'x': x, 'y': y})
        data['r'] = np.sqrt(data['x']**2 + data['y']**2)
        data['phi'] = np.arctan2(data['y'], data['x'])
        data['psi'] = data['phi'] + miss_alignement
        alpha_1 = compute_alpha(data['phi'], data['psi'])
        assert (np.isfinite(alpha_1).all())
        np.testing.assert_almost_equal(alpha_1, miss_alignement)
Esempio n. 3
0
def main_pipeline(files,
                  aux_basepath,
                  max_events,
                  dark_filename,
                  integral_width,
                  debug,
                  hillas_filename,
                  parameters_filename,
                  picture_threshold,
                  boundary_threshold,
                  template_filename,
                  saturation_threshold,
                  threshold_pulse,
                  bad_pixels=None,
                  disable_bar=False):
    # get configuration
    with open(parameters_filename) as file:
        calibration_parameters = yaml.load(file)
    if bad_pixels is None:
        bad_pixels = get_bad_pixels(calib_file=parameters_filename,
                                    dark_histo=dark_filename,
                                    plot=None)
    pulse_template = NormalizedPulseTemplate.load(template_filename)
    pulse_area = pulse_template.integral() * u.ns
    ratio = pulse_template.compute_charge_amplitude_ratio(
        integral_width=integral_width, dt_sampling=4)  # ~ 0.24
    gain = np.array(calibration_parameters['gain'])  # ~ 20 LSB / p.e.
    gain_amplitude = gain * ratio
    crosstalk = np.array(calibration_parameters['mu_xt'])
    bias_resistance = 10 * 1E3 * u.Ohm  # 10 kOhm
    cell_capacitance = 50 * 1E-15 * u.Farad  # 50 fF
    geom = DigiCam.geometry
    dark_histo = Histogram1D.load(dark_filename)
    dark_baseline = dark_histo.mean()

    # define pipeline
    events = calibration_event_stream(files,
                                      max_events=max_events,
                                      disable_bar=disable_bar)
    events = add_slow_data_calibration(
        events,
        basepath=aux_basepath,
        aux_services=('DriveSystem', 'DigicamSlowControl', 'MasterSST1M',
                      'SafetyPLC', 'PDPSlowControl'))
    events = baseline.fill_dark_baseline(events, dark_baseline)
    events = baseline.fill_digicam_baseline(events)
    events = tagging.tag_burst_from_moving_average_baseline(events)
    events = baseline.compute_baseline_shift(events)
    events = baseline.subtract_baseline(events)
    events = filters.filter_clocked_trigger(events)
    events = baseline.compute_nsb_rate(events, gain_amplitude, pulse_area,
                                       crosstalk, bias_resistance,
                                       cell_capacitance)
    events = baseline.compute_gain_drop(events, bias_resistance,
                                        cell_capacitance)
    events = peak.find_pulse_with_max(events)
    events = charge.compute_dynamic_charge(
        events,
        integral_width=integral_width,
        saturation_threshold=saturation_threshold,
        threshold_pulse=threshold_pulse,
        debug=debug,
        pulse_tail=False,
    )
    events = charge.compute_photo_electron(events, gains=gain)
    events = charge.interpolate_bad_pixels(events, geom, bad_pixels)
    events = cleaning.compute_tailcuts_clean(
        events,
        geom=geom,
        overwrite=True,
        picture_thresh=picture_threshold,
        boundary_thresh=boundary_threshold,
        keep_isolated_pixels=False)
    events = cleaning.compute_boarder_cleaning(events, geom,
                                               boundary_threshold)
    events = cleaning.compute_dilate(events, geom)
    events = image.compute_hillas_parameters(events, geom)
    events = charge.compute_sample_photo_electron(events, gain_amplitude)
    events = cleaning.compute_3d_cleaning(events,
                                          geom,
                                          n_sample=50,
                                          threshold_sample_pe=20,
                                          threshold_time=2.1 * u.ns,
                                          threshold_size=0.005 * u.mm)
    # create pipeline output file
    output_file = Serializer(hillas_filename, mode='w', format='fits')
    data_to_store = PipelineOutputContainer()
    for event in events:
        if debug:
            print(event.hillas)
            print(event.data.nsb_rate)
            print(event.data.gain_drop)
            print(event.data.baseline_shift)
            print(event.data.border)
            plot_array_camera(np.max(event.data.adc_samples, axis=-1))
            plot_array_camera(
                np.nanmax(event.data.reconstructed_charge, axis=-1))
            plot_array_camera(event.data.cleaning_mask.astype(float))
            plot_array_camera(event.data.reconstructed_number_of_pe)
            plt.show()
        # fill container
        data_to_store.local_time = event.data.local_time
        data_to_store.event_type = event.event_type
        data_to_store.event_id = event.event_id
        data_to_store.az = event.slow_data.DriveSystem.current_position_az
        data_to_store.el = event.slow_data.DriveSystem.current_position_el

        r = event.hillas.r
        phi = event.hillas.phi
        psi = event.hillas.psi
        alpha = compute_alpha(phi.value, psi.value) * psi.unit
        data_to_store.alpha = alpha
        data_to_store.miss = compute_miss(r=r.value, alpha=alpha.value)
        data_to_store.miss = data_to_store.miss * r.unit
        data_to_store.baseline = np.mean(event.data.digicam_baseline)
        data_to_store.nsb_rate = np.mean(event.data.nsb_rate)
        temp_crate1 = event.slow_data.DigicamSlowControl.Crate1_T
        temp_crate2 = event.slow_data.DigicamSlowControl.Crate2_T
        temp_crate3 = event.slow_data.DigicamSlowControl.Crate3_T
        temp_digicam = np.array(
            np.hstack([temp_crate1, temp_crate2, temp_crate3]))
        temp_digicam_mean = np.mean(temp_digicam[np.logical_and(
            temp_digicam > 0, temp_digicam < 60)])
        data_to_store.digicam_temperature = temp_digicam_mean
        temp_sector1 = event.slow_data.PDPSlowControl.Sector1_T
        temp_sector2 = event.slow_data.PDPSlowControl.Sector2_T
        temp_sector3 = event.slow_data.PDPSlowControl.Sector3_T
        temp_pdp = np.array(
            np.hstack([temp_sector1, temp_sector2, temp_sector3]))
        temp_pdp_mean = np.mean(temp_pdp[np.logical_and(
            temp_pdp > 0, temp_pdp < 60)])
        data_to_store.pdp_temperature = temp_pdp_mean
        target_radec = event.slow_data.MasterSST1M.target_radec
        data_to_store.target_ra = target_radec[0]
        data_to_store.target_dec = target_radec[1]
        status_leds = event.slow_data.SafetyPLC.SPLC_CAM_Status
        # bit 8 of status_LEDs is about on/off, bit 9 about blinking
        data_to_store.pointing_leds_on = bool((status_leds & 1 << 8) >> 8)
        data_to_store.pointing_leds_blink = bool((status_leds & 1 << 9) >> 9)
        hv_sector1 = event.slow_data.PDPSlowControl.Sector1_HV
        hv_sector2 = event.slow_data.PDPSlowControl.Sector2_HV
        hv_sector3 = event.slow_data.PDPSlowControl.Sector3_HV
        hv_pdp = np.array(np.hstack([hv_sector1, hv_sector2, hv_sector3]),
                          dtype=bool)
        data_to_store.all_hv_on = np.all(hv_pdp)
        ghv_sector1 = event.slow_data.PDPSlowControl.Sector1_GHV
        ghv_sector2 = event.slow_data.PDPSlowControl.Sector2_GHV
        ghv_sector3 = event.slow_data.PDPSlowControl.Sector3_GHV
        ghv_pdp = np.array(np.hstack([ghv_sector1, ghv_sector2, ghv_sector3]),
                           dtype=bool)
        data_to_store.all_ghv_on = np.all(ghv_pdp)
        is_on_source = bool(event.slow_data.DriveSystem.is_on_source)
        data_to_store.is_on_source = is_on_source
        is_tracking = bool(event.slow_data.DriveSystem.is_tracking)
        data_to_store.is_tracking = is_tracking
        data_to_store.shower = bool(event.data.shower)
        data_to_store.border = bool(event.data.border)
        data_to_store.burst = bool(event.data.burst)
        data_to_store.saturated = bool(event.data.saturated)
        for key, val in event.hillas.items():
            data_to_store[key] = val
        output_file.add_container(data_to_store)
    try:
        output_file.close()
        print(hillas_filename, 'created.')
    except ValueError:
        print('WARNING: no data to save,', hillas_filename, 'not created.')
Esempio n. 4
0
def scan_2d_plot(
        pipeline_data,
        alphas_min=(1, 2, 5, 10, 20),
        plot="show",
        num_steps=200,
        fov=((-500, 500), (-500, 500)),
        disable_bar=True,
):
    """
    2D scan of spike in alpha
    :param pipeline_data: hillas parameters data file, output of pipeline.py
    :param alphas_min: list of float, each element being a alpha_min.
    events are taken into account as possibly coming from
    the scanned point if the alpha parameter calculated at that point is below
    alpha_min.
    :param plot: path to the plot for a 2d scan of the source position.
    If set to "none", the plot is not produced. If set to "show" the plot
    is displayed instead.
    :param num_steps: number of binning in the FoV
    :param fov: x and y range of the field of view. Format:
    ((x_min, x_max), (y_min, y_max))
    :param disable_bar: If true shows the progress bar of the alpha computation
    :return: None
    """

    alphas_min = np.array(alphas_min)
    x_fov_start = fov[0][0]  # limits of the FoV in mm
    y_fov_start = fov[1][0]  # limits of the FoV in mm
    x_fov_end = fov[0][1]  # limits of the FoV in mm
    y_fov_end = fov[1][1]  # limits of the FoV in mm
    x_fov = np.linspace(x_fov_start, x_fov_end, num_steps)
    y_fov = np.linspace(y_fov_start, y_fov_end, num_steps)
    dx = x_fov[1] - x_fov[0]
    dy = y_fov[1] - y_fov[0]
    x_fov_bins = np.linspace(x_fov_start - dx / 2, x_fov_end + dx / 2,
                             num_steps + 1)
    y_fov_bins = np.linspace(y_fov_start - dy / 2, y_fov_end + dy / 2,
                             num_steps + 1)
    num_alpha = len(alphas_min)
    n = np.zeros([num_steps, num_steps, num_alpha], dtype=int)
    print('2D scan calculation:')

    X, Y = np.meshgrid(x_fov, y_fov)

    for index, hillas in tqdm(pipeline_data.iterrows(),
                              total=len(pipeline_data), disable=disable_bar):

        x, y, r, phi = correct_hillas(hillas['x'], hillas['y'],
                                      source_x=X,
                                      source_y=Y)

        alpha = compute_alpha(phi, hillas['psi'])
        alpha = np.rad2deg(alpha)
        alpha = alpha[..., None] < alphas_min
        n += alpha

    for ai, alpha_min in enumerate(alphas_min):
        if len(alphas_min) > 1:
            plot_name = plot.replace('.png', '_{}deg.png'.format(alpha_min))
        else:
            plot_name = plot
        fig = plt.figure(figsize=(16, 12))
        ax1 = fig.add_subplot(111)
        pcm = ax1.pcolormesh(x_fov_bins, y_fov_bins, n[:, :, ai],
                             rasterized=True, cmap='nipy_spectral')
        plt.ylabel('FOV Y [mm]')
        plt.xlabel('FOV X [mm]')
        cbar = fig.colorbar(pcm)
        cbar.set_label('# of events')
        plt.grid()
        plt.tight_layout()
        if plot == "show":
            plt.show()
        else:
            plt.savefig(plot_name)
            print(plot_name, 'created')
        plt.close(fig)