Exemplo n.º 1
0
def plot_results(results_filename, figure_path=None):

    fit_results = Table.read(results_filename, format='fits')
    fit_results = fit_results.to_pandas()

    gain = fit_results['gain']
    sigma_e = fit_results['sigma_e']
    sigma_s = fit_results['sigma_s']

    _, fig_1 = plot_array_camera(gain, label='Gain [LSB $\cdot$ ns]')
    _, fig_2 = plot_array_camera(sigma_e, label='$\sigma_e$')
    _, fig_3 = plot_array_camera(sigma_s, label='$\sigma_s$')

    fig_4 = plot_histo(gain, x_label='Gain [LSB $\cdot$ ns]', bins='auto')
    fig_5 = plot_histo(sigma_e, x_label='$\sigma_e$', bins='auto')
    fig_6 = plot_histo(sigma_s, x_label='$\sigma_s$', bins='auto')

    if figure_path is not None:

        fig_1.savefig(os.path.join(figure_path, 'gain_camera'))
        fig_2.savefig(os.path.join(figure_path, 'sigma_e_camera'))
        fig_3.savefig(os.path.join(figure_path, 'sigma_s_camera'))

        fig_4.savefig(os.path.join(figure_path, 'gain_histo'))
        fig_5.savefig(os.path.join(figure_path, 'sigma_e_histo'))
        fig_6.savefig(os.path.join(figure_path, 'sigma_s_histo'))
Exemplo n.º 2
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.')
Exemplo n.º 3
0
def test_array_to_camera_view():
    image = np.random.normal(size=1296)
    image[:100] = np.nan
    plot_array_camera(image, 'some random numbers', limits=(-1, 1))
Exemplo n.º 4
0
#fit_params = 'A = {} '.format(np.around(arg.x[0], decimals=3)) + '\n' + 'B = {}'.format(np.around(arg.x[1], decimals=3)) + '\n' + r'$x_{offset}=$'+' {} mm'.format(np.around(arg.x[2], decimals=3)) + '\n' + r'$y_{offset}=$'+' {} mm'.format(np.around(arg.x[3], decimals=3))
#ax_fit.text(50.50, 50.50, fit_params, {'color': 'r', 'fontsize': 20, 'bbox': dict(boxstyle="round", fc="white", ec="black", pad=0.4)})
ax_fit.legend(loc=3, fontsize=8)

ax_res = plt.subplot(gs[1], sharex=ax_fit)
# Making residuals
residuals = charge_array[mask] - radial_irradiance(
    xpix[mask], ypix[mask], arg.x[0], arg.x[1], arg.x[2], arg.x[3], distance)
residuals /= charge_array[mask]
ax_res.plot(np.degrees(theta), residuals, linestyle='None', marker='o', ms=2)
ax_res.set_ylabel('residuals')
ax_res.set_xlabel(r'$\theta$ [°]')

pdf.savefig(fig_fit)

# fig_res, ax_res = plt.subplots()
# ax_res.plot(np.degrees(theta), residuals, linestyle='None', marker='o', ms=2)
# pdf.savefig(fig_res)

cam_display_chg, fig_cam_chg = plot_array_camera(
    data=np.ma.masked_array(charge_array, mask=(~mask + (charge_array < 40))),
    label='Intensity [from charge]')
pdf.savefig(fig_cam_chg)
cam_display_amp, fig_cam_amp = plot_array_camera(
    data=np.ma.masked_array(maximum_array,
                            mask=(~mask + (maximum_array < 40))),
    label='Intensity [from amplitude]')
pdf.savefig(fig_cam_amp)

pdf.close()
Exemplo n.º 5
0
def entry():
    args = docopt(__doc__)
    files = args['<INPUT>']
    max_events = convert_int(args['--max_events'])
    pixel_id = convert_pixel_args(args['--pixel'])
    base_sub = args['--baseline_subtracted']
    raw_histo_filename = args['--output']
    event_types = convert_list_int(args['--event_types'])
    baseline_filename = args['--baseline_filename']
    disable_bar = args['--disable_bar']
    if baseline_filename.lower() == 'none':
        baseline_filename = None
    output_path = os.path.dirname(raw_histo_filename)
    if not os.path.exists(output_path) and output_path != "":
        raise IOError('Path {} for output '
                      'does not exists \n'.format(output_path))

    if args['--compute']:
        compute(files=files,
                filename=raw_histo_filename,
                max_events=max_events,
                pixel_id=pixel_id,
                event_types=event_types,
                disable_bar=disable_bar,
                baseline_subtracted=base_sub)
        if baseline_filename:
            compute_baseline_histogram(files=files,
                                       filename=baseline_filename,
                                       max_events=max_events,
                                       pixel_id=pixel_id,
                                       disable_bar=disable_bar)

    if args['--save_figures']:
        raw_histo = Histogram1D.load(raw_histo_filename)
        path = os.path.join(output_path, 'figures/', 'raw_histo/')
        if not os.path.exists(path):
            os.makedirs(path)
        figure = plt.figure()
        for i, pixel in tqdm(enumerate(pixel_id), total=len(pixel_id)):
            axis = figure.add_subplot(111)
            figure_path = os.path.join(path, 'pixel_{}.pdf')
            try:
                raw_histo.draw(index=(i, ), axis=axis, log=True, legend=False)
                figure.savefig(figure_path.format(pixel))
            except Exception as e:
                print('Could not save pixel {} to : {} \n'.format(
                    pixel, figure_path))
                print(e)
            axis.remove()

    if args['--display']:
        raw_histo = Histogram1D.load(raw_histo_filename)
        pixel = 0
        raw_histo.draw(index=(pixel, ),
                       log=True,
                       legend=False,
                       label='Histogram {}'.format(pixel),
                       x_label='[LSB]')
        mean_value = raw_histo.mean()
        plot_histo(mean_value, bins='auto', x_label='Mean value [LSB]')
        plot_array_camera(mean_value, label='Mean value [LSB]')
        if baseline_filename:
            baseline_histo = Histogram1D.load(baseline_filename)
            baseline_histo.draw(index=(pixel, ),
                                log=True,
                                legend=False,
                                label='Histogram {}'.format(pixel),
                                x_label='DigiCam baseline [LSB]')
            mean_baseline = baseline_histo.mean()
            plot_histo(mean_baseline,
                       bins='auto',
                       x_label='Mean DigiCam baseline [LSB]')
            plot_array_camera(mean_baseline,
                              label='Mean DigiCam baseline [LSB]')
            plot_array_camera(mean_baseline - mean_value, label='Diff [LSB]')
            plot_histo(mean_baseline - mean_value,
                       bins='auto',
                       x_label='Diff [LSB]')
        plt.show()
Exemplo n.º 6
0
def entry():
    args = docopt(__doc__)
    files = args['<INPUT>']

    max_events = convert_int(args['--max_events'])
    pixel_id = convert_pixel_args(args['--pixel'])
    n_samples = int(args['--n_samples'])
    timing_histo_filename = args['--timing_histo_filename']
    ac_levels = convert_list_int(args['--ac_levels'])

    output_path = os.path.dirname(timing_histo_filename)
    results_filename = os.path.join(output_path, 'timing.npz')

    if not os.path.exists(output_path):
        raise IOError('Path for output does not exists \n')

    if args['--compute']:
        compute(files,
                max_events,
                pixel_id,
                n_samples,
                ac_levels,
                timing_histo_filename,
                save=True,
                time_method=compute_time_from_max)
        # or try to use compute_time_from_leading_edge)

    if args['--fit']:
        timing_histo = Histogram1D.load(timing_histo_filename)

        timing = timing_histo.mode()
        timing = mode(timing, axis=0)[0][0]

        np.savez(results_filename, time=timing)

    if args['--save_figures']:

        raw_histo = Histogram1D.load(timing_histo_filename)

        path = os.path.join(output_path, 'figures/', 'timing_histo/')

        if not os.path.exists(path):
            os.makedirs(path)

        figure = plt.figure()

        for i, pixel in tqdm(enumerate(pixel_id), total=len(pixel_id)):
            axis = figure.add_subplot(111)
            figure_path = path + 'pixel_{}.pdf'

            try:

                raw_histo.draw(index=(i, ), axis=axis, log=True, legend=False)
                figure.savefig(figure_path.format(pixel))

            except Exception as e:

                print('Could not save pixel {} to : {} \n'.format(
                    pixel, figure_path))
                print(e)

            axis.remove()

    if args['--display']:

        timing_histo = Histogram1D.load(timing_histo_filename)
        timing_histo.draw(index=(
            len(ac_levels) - 1,
            0,
        ),
                          log=True,
                          legend=False)

        pulse_time = timing_histo.mode()

        plt.figure()
        plt.plot(ac_levels, pulse_time)
        plt.xlabel('DAC level')
        plt.ylabel('Reconstructed pulse time [ns]')

        pulse_time = np.load(results_filename)['time']

        plot_array_camera(pulse_time,
                          label='time of pulse [ns]',
                          allow_pick=True)

        plot_parameter(pulse_time, 'time of pulse', '[ns]', bins=20)

        plt.show()

    return