def main(urls, outfile_path, baseline_path, do_plots=False):
    baseline = np.load(baseline_path)

    digicam = Camera()
    data_stream = event_stream(urls,
                               expert_mode=True,
                               camera=digicam,
                               camera_geometry=digicam.geometry)
    data_stream = random_triggers.fill_baseline_r0(data_stream, n_bins=1050)
    data_stream = filter.filter_missing_baseline(data_stream)
    data_stream = filter.filter_event_types(data_stream, flags=[8])
    data_stream = r1.calibrate_to_r1(data_stream, baseline)
    data_stream = filter.filter_period(data_stream, period=10 * u.second)

    save_external_triggers(data_stream,
                           output_filename=outfile_path,
                           pixel_list=np.arange(1296))

    if do_plots:
        make_plots(outfile_path)
Esempio n. 2
0
def main(args):
    # Input/Output files
    dark_baseline = np.load(args['--baseline_path'])

    digicam = Camera(
        # Source coordinates (in camera frame)
        source_x=0. * u.mm,
        source_y=0. * u.mm,
    )

    # Config for NSB + baseline evaluation
    n_bins = 1000

    # Config for Hillas parameters analysis
    reclean = True

    # Noisy pixels not taken into account in Hillas
    pixel_not_wanted = [
        1038, 1039, 1002, 1003, 1004, 966, 967, 968, 930, 931, 932, 896
    ]
    additional_mask = np.ones(1296)
    additional_mask[pixel_not_wanted] = 0
    additional_mask = additional_mask > 0

    # Integration configuration (signal reco.)
    time_integration_options = {
        'mask': None,
        'mask_edges': None,
        'peak': None,
        'window_start': 3,
        'window_width': 7,
        'threshold_saturation': np.inf,
        'n_samples': 50,
        'timing_width': 6,
        'central_sample': 11
    }

    peak_position = utils.fake_timing_hist(
        time_integration_options['n_samples'],
        time_integration_options['timing_width'],
        time_integration_options['central_sample'])

    (time_integration_options['peak'], time_integration_options['mask'],
     time_integration_options['mask_edges']) = utils.generate_timing_mask(
         time_integration_options['window_start'],
         time_integration_options['window_width'], peak_position)

    # Image cleaning configuration
    picture_threshold = 15
    boundary_threshold = 10
    shower_distance = 200 * u.mm

    # Define the event stream
    data_stream = event_stream(args['<files>'], camera=digicam)
    # Clean pixels
    data_stream = filter.set_pixels_to_zero(data_stream,
                                            unwanted_pixels=pixel_not_wanted)
    # Compute baseline with clocked triggered events
    # (sliding average over n_bins)
    data_stream = random_triggers.fill_baseline_r0(data_stream, n_bins=n_bins)
    # Stop events that are not triggered by DigiCam algorithm
    # (end of clocked triggered events)
    data_stream = filter.filter_event_types(data_stream, flags=[1, 2])
    # Do not return events that have not the baseline computed
    # (only first events)
    data_stream = filter.filter_missing_baseline(data_stream)

    # Run the r1 calibration (i.e baseline substraction)
    data_stream = r1.calibrate_to_r1(data_stream, dark_baseline)
    # Run the dl0 calibration (data reduction, does nothing)
    data_stream = dl0.calibrate_to_dl0(data_stream)
    # Run the dl1 calibration (compute charge in photons + cleaning)
    data_stream = dl1.calibrate_to_dl1(data_stream,
                                       time_integration_options,
                                       additional_mask=additional_mask,
                                       picture_threshold=picture_threshold,
                                       boundary_threshold=boundary_threshold)
    # Return only showers with total number of p.e. above min_photon
    data_stream = filter.filter_shower(data_stream,
                                       min_photon=args['--min_photon'])
    # Run the dl2 calibration (Hillas)
    data_stream = dl2.calibrate_to_dl2(data_stream,
                                       reclean=reclean,
                                       shower_distance=shower_distance)

    if args['--display']:

        with plt.style.context('ggplot'):
            display = EventViewer(data_stream)
            display.draw()
    else:
        save_hillas_parameters_in_text(data_stream=data_stream,
                                       output_filename=args['--outfile_path'])
def test_calibrate_to_dl1():
    from digicampipe.calib.camera.dl1 import calibrate_to_dl1

    # The next 50 lines are just setp.
    dark_baseline = make_dark_base_line()

    n_bins = 50
    pixel_not_wanted = [
        1038, 1039, 1002, 1003, 1004, 966, 967, 968, 930, 931, 932, 896
    ]
    additional_mask = np.ones(1296)
    additional_mask[pixel_not_wanted] = 0
    additional_mask = additional_mask > 0

    time_integration_options = {
        'mask': None,
        'mask_edges': None,
        'peak': None,
        'window_start': 3,
        'window_width': 7,
        'threshold_saturation': np.inf,
        'n_samples': 50,
        'timing_width': 6,
        'central_sample': 11
    }

    peak_position = utils.fake_timing_hist(
        time_integration_options['n_samples'],
        time_integration_options['timing_width'],
        time_integration_options['central_sample'])

    (time_integration_options['peak'], time_integration_options['mask'],
     time_integration_options['mask_edges']) = utils.generate_timing_mask(
         time_integration_options['window_start'],
         time_integration_options['window_width'], peak_position)

    data_stream = event_stream(example_file_path)
    data_stream = filter.set_pixels_to_zero(
        data_stream,
        unwanted_pixels=pixel_not_wanted,
    )
    data_stream = random_triggers.fill_baseline_r0(
        data_stream,
        n_bins=n_bins,
    )
    data_stream = filter.filter_event_types(data_stream, flags=[1, 2])
    data_stream = filter.filter_missing_baseline(data_stream)

    data_stream = r1.calibrate_to_r1(data_stream, dark_baseline)
    data_stream = dl0.calibrate_to_dl0(data_stream)

    # This is the function under test
    data_stream = calibrate_to_dl1(
        data_stream,
        time_integration_options,
        additional_mask=additional_mask,
        picture_threshold=15,
        boundary_threshold=10,
    )

    # This is the actual test:
    for event_counter, event in enumerate(data_stream):
        for dl1 in event.dl1.tel.values():

            assert dl1.cleaning_mask.shape == (1296, )
            assert dl1.cleaning_mask.dtype == np.bool

            assert not np.isnan(dl1.time_spread)

            assert dl1.pe_samples.shape == (1296, )
            assert dl1.pe_samples.dtype == np.float64

            assert dl1.on_border.shape == ()
            assert dl1.on_border.dtype == np.bool

    assert event_counter >= 86
def main(
    files,
    outfile_path,
    outfile_suffix,
    picture_threshold=15,
    boundary_threshold=7,
    baseline0=9,
    baseline1=15,
):

    # Input/Output files
    digicam_config_file = args['--camera_config']

    dark_baseline = None

    # Source coordinates (in camera frame)
    source_x = 0. * u.mm
    source_y = 0. * u.mm

    # Camera and Geometry objects
    # (mapping, pixel, patch + x,y coordinates pixels)
    digicam = Camera(_config_file=digicam_config_file)
    digicam_geometry = geometry.generate_geometry_from_camera(
        camera=digicam, source_x=source_x, source_y=source_y)

    # Noisy pixels not taken into account in Hillas
    pixel_not_wanted = [
        1038, 1039, 1002, 1003, 1004, 966, 967, 968, 930, 931, 932, 896
    ]
    additional_mask = np.ones(1296)
    additional_mask[pixel_not_wanted] = 0
    additional_mask = additional_mask > 0

    # Integration configuration (signal reco.)
    time_integration_options = {
        'mask': None,
        'mask_edges': None,
        'peak': None,
        'window_start': 3,
        'window_width': 7,  # length of integration window
        'threshold_saturation': np.inf,
        'n_samples': 50,
        'timing_width': 6,
        'central_sample': 11
    }

    peak_position = utils.fake_timing_hist(
        time_integration_options['n_samples'],
        time_integration_options['timing_width'],
        time_integration_options['central_sample'])

    (time_integration_options['peak'], time_integration_options['mask'],
     time_integration_options['mask_edges']) = utils.generate_timing_mask(
         time_integration_options['window_start'],
         time_integration_options['window_width'], peak_position)

    # Image cleaning configuration
    shower_distance = 200 * u.mm

    # Filtering on big showers
    min_photon = 50

    # Config for Hillas parameters analysis
    n_showers = 100000000
    reclean = True

    # Define the event stream
    data_stream = event_stream(files,
                               camera_geometry=digicam_geometry,
                               camera=digicam)

    # Clean pixels
    data_stream = filter.set_pixels_to_zero(data_stream,
                                            unwanted_pixels=pixel_not_wanted)

    # Computation of baseline
    #
    # Methods:
    #
    # simtel_baseline.baseline_data()
    # Baseline is computed as a mean of 'n_bins0' first time samples,
    # 'n_bins1' last time samples.
    # A key assumption in this method is that the shower in simulated
    # data is somewhere in the middle of 50 samples.
    # Each pixel in each event has its own baseline
    #
    # simtel_baseline.baseline_simtel()
    # Baseline is taken as a value reported by sim_telarray
    # event.mc.tel[tel_id].pedestal/event.r0.tel[tel_id].num_samples.
    # That should be OK for all sim_telarray version from April
    # 2018, where an error for DC coupled simulations was corrected.

    data_stream = simtel_baseline.baseline_data(data_stream,
                                                n_bins0=baseline0,
                                                n_bins1=baseline1)

    # data_stream = simtel_baseline.baseline_simtel(data_stream)

    # Run the r1 calibration (i.e baseline substraction)
    data_stream = r1.calibrate_to_r1(data_stream, dark_baseline)

    # Run the dl1 calibration (compute charge in photons + cleaning)
    data_stream = dl1.calibrate_to_dl1(data_stream,
                                       time_integration_options,
                                       additional_mask=additional_mask,
                                       picture_threshold=picture_threshold,
                                       boundary_threshold=boundary_threshold)

    # Return only showers with total number of p.e. above min_photon
    data_stream = filter.filter_shower(data_stream, min_photon=min_photon)

    # Save cleaned events - pixels and corresponding values
    filename_pix = 'pixels.txt'
    filename_eventsimage = 'events_image_' + outfile_suffix + '.txt'
    data_stream = events_image.save_events(data_stream,
                                           outfile_path + filename_pix,
                                           outfile_path + filename_eventsimage)

    # Save simulated shower paramters
    filename_showerparam = 'pipedmc_param_' + outfile_suffix + '.txt'
    data_stream = mc_shower.save_shower(data_stream,
                                        outfile_path + filename_showerparam)

    # Run the dl2 calibration (Hillas)
    data_stream = dl2.calibrate_to_dl2(data_stream,
                                       reclean=reclean,
                                       shower_distance=shower_distance)

    # Save arrival times of photons in pixels passed cleaning
    filename_timing = 'timing_' + outfile_suffix + '.txt'
    data_stream = events_image.save_timing(data_stream,
                                           outfile_path + filename_timing)

    # Save mean baseline in event pixels
    filename_baseline = ('baseline_' + outfile_suffix + '_bas' +
                         str(baseline0).zfill(2) + str(baseline1).zfill(2) +
                         '.txt')
    # data_stream = simtel_baseline.save_mean_event_baseline(
    #    data_stream, directory + filename_baseline)

    if args['--display']:

        with plt.style.context('ggplot'):
            display = EventViewer(data_stream)
        display.draw()

    else:
        # Save Hillas
        hillas_filename = 'hillas_' + outfile_suffix
        save_hillas_parameters(data_stream=data_stream,
                               n_showers=n_showers,
                               output_filename=outfile_path + hillas_filename)
        # save_hillas_parameters_in_text(
        #    data_stream=data_stream,
        #    output_filename=outfile_path + hillas_filename)
    """
Esempio n. 5
0
                                'mask_edges': None,
                                'peak': None,
                                'window_start': 3,
                                'window_width': 7,
                                'threshold_saturation': 3500,
                                'n_samples': 50,
                                'timing_width': 6,
                                'central_sample': 11}

    peak_position = utils.fake_timing_hist(time_integration_options['n_samples'],
                                           time_integration_options['timing_width'],
                                           time_integration_options['central_sample'])
    time_integration_options['peak'], time_integration_options['mask'], time_integration_options['mask_edges'] = \
        utils.generate_timing_mask(time_integration_options['window_start'],
                                   time_integration_options['window_width'],
                                   peak_position)

    # Define the event stream
    data_stream = containers.load_from_pickle_gz('test.pickle')
    data_stream = r1.calibrate_to_r1(data_stream, None)
    data_stream = dl0.calibrate_to_dl0(data_stream)
    # Run the dl1 calibration (compute charge in photons)
    data_stream = dl1.calibrate_to_dl1(data_stream, time_integration_options)
#    data_stream = filter.filter_shower(data_stream, min_photon=1000)
    # Run the dl2 calibration (Hillas + classification + energy + direction)
    data_stream = dl2.calibrate_to_dl2(data_stream)

    with plt.style.context('ggplot'):
        display = EventViewer(data_stream)
        display.draw()