def make_dark_base_line():
    from digicampipe.calib.camera import filter
    from digicampipe.io.event_stream import event_stream
    from digicampipe.io.save_adc import save_dark

    with TemporaryDirectory() as temp_dir:
        outfile_path = os.path.join(temp_dir, "dark_baseline.npz")

        data_stream = event_stream(example_file_path)
        data_stream = filter.set_pixels_to_zero(
            data_stream,
            unwanted_pixels=[],
        )
        data_stream = filter.filter_event_types(data_stream, flags=[8])
        data_stream = save_dark(data_stream, outfile_path)
        for event_counter, _ in enumerate(data_stream):
            pass
        assert event_counter >= 5

        npz_file = np.load(outfile_path)
        assert not np.any(np.isnan(npz_file['baseline']))
        assert not np.any(np.isnan(npz_file['standard_deviation']))
        assert len(npz_file['baseline']) == 1296

    return npz_file
Exemplo n.º 2
0
def main(baseline_file_path, urls, unwanted_pixels=[]):

    data_stream = event_stream(urls)
    data_stream = filter.set_pixels_to_zero(data_stream,
                                            unwanted_pixels=unwanted_pixels)
    data_stream = filter.filter_event_types(data_stream, flags=[8])
    data_stream = save_dark(data_stream, baseline_file_path)

    for _ in tqdm(data_stream):
        pass
Exemplo n.º 3
0
def main(output_directory, urls, unwanted_patches, unwanted_clusters,
         blinding):
    dark_file_path = path.join(output_directory, 'dark.npz')
    dark_trigger_file_path = path.join(output_directory, 'bias_curve_dark.npz')

    thresholds = np.arange(0, 400, 10)

    data_stream = event_stream(urls, camera=DigiCam)
    data_stream = filter.set_patches_to_zero(data_stream,
                                             unwanted_patch=unwanted_patches)
    data_stream = r0.fill_trigger_input_7(data_stream)
    # Fill the flags (to be replaced by Digicam)
    data_stream = filter.filter_event_types(data_stream, flags=[8])

    data_stream = save_bias_curve(data_stream,
                                  thresholds=thresholds,
                                  blinding=blinding,
                                  output_filename=dark_trigger_file_path,
                                  unwanted_cluster=unwanted_clusters)

    data_stream = save_dark(data_stream, dark_file_path)

    for _ in tqdm(data_stream):
        pass

    # Filter the events for display
    data_dark = np.load(dark_file_path)
    data_rate = np.load(dark_trigger_file_path)

    plt.figure()
    plt.hist(data_dark['baseline'], bins='auto')
    plt.xlabel('dark baseline [LSB]')
    plt.ylabel('count')

    plt.figure()
    plt.hist(data_dark['standard_deviation'], bins='auto')
    plt.xlabel('dark std [LSB]')
    plt.ylabel('count')

    fig = plt.figure()
    axis = fig.add_subplot(111)
    axis.errorbar(x=data_rate['threshold'],
                  y=data_rate['rate'] * 1E9,
                  yerr=data_rate['rate_error'] * 1E9,
                  label='Blinding : {}'.format(blinding))
    axis.set_ylabel('rate [Hz]')
    axis.set_xlabel('threshold [LSB]')
    axis.set_yscale('log')
    axis.legend(loc='best')
    plt.show()
Exemplo n.º 4
0
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)
Exemplo n.º 5
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
         9.98248525e+07, 4.58257597e+07, 7.87400834e+06],
        [1.44337576e+08, 1.44337576e+08, 1.44337576e+08, 1.44337576e+08,
         1.39066839e+08, 7.07843010e+07, 6.14071151e+07, 6.12372472e+07,
         6.12372472e+07, 6.12372472e+07, 6.12372472e+07, 6.12372472e+07]
    ]
    threshold_care = [
        [10., 25., 40., 55., 70., 85., 100.],
        [10., 20., 30., 40., 50., 60., 70.],
        [10., 20., 30., 40., 50., 60., 70., 80., 90., 100., 110., 120.]
    ]
    nsb_care = [1.5, 1.0, 0.9]

    # Define the event stream

    data_stream = event_stream(args['<fitsfiles>'])
    data_stream = filter.filter_event_types(data_stream, flags=[8])

    data = np.load(args['<outfile>'])

    fig = plt.figure()
    axis = fig.add_subplot(111)
    axis.errorbar(
        data['threshold'],
        data['rate'] * 1E9,
        yerr=data['rate_error'] * 1E9,
        label='Blinding : {}'.format(blinding)
    )
    axis.errorbar(
        threshold_care[0],
        rate_care[0],
        yerr=rate_error_care[0],