Esempio n. 1
0
def get_pixel_nvs(digicam_config_file=camera_config_file):
    """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)
    pixels_pos_mm = np.array(
        [digicam_geometry.pix_x.to(u.mm), digicam_geometry.pix_y.to(u.mm)]
    ).transpose()
    pixels_pos_mm = pixels_pos_mm.dot(np.array([[0, -1], [1, 0]]))
    pixels_v1 = pixels_pos_mm[
        digicam_geometry.neighbors[0][1], :] - pixels_pos_mm[0, :]
    pixels_v2 = pixels_pos_mm[
        digicam_geometry.neighbors[0][0], :] - pixels_pos_mm[0, :]
    index_to_pos = np.array([pixels_v1, pixels_v2]).transpose()
    relative_pos = (pixels_pos_mm - pixels_pos_mm[0, :]).transpose()
    pixels_nvs = np.linalg.pinv(index_to_pos).dot(relative_pos)
    pixels_nvs -= np.mean(pixels_nvs, axis=1).reshape(2, 1)
    return pixels_nvs
Esempio n. 2
0
    def __init__(self, *args, **kwargs):
        if not args and kwargs.get('_config_file', None) is None:
            kwargs['_config_file'] = pkg_resources.resource_filename(
                'digicampipe',
                path.join('tests', 'resources', 'camera_config.cfg'))
            self.config_file = kwargs['_config_file']
        elif args:
            self.config_file = args[0]
        else:
            self.config_file = kwargs['_config_file']
        super().__init__(*args, **kwargs)

        geometry_kwargs = {}
        if 'source_x' in kwargs:
            geometry_kwargs['source_x'] = kwargs['source_x']
        if 'source_y' in kwargs:
            geometry_kwargs['source_y'] = kwargs['source_y']
        self.geometry = geometry.generate_geometry_from_camera(
            camera=self, **geometry_kwargs)
        self.patch_matrix = geometry.compute_patch_matrix(camera=self)
        self.cluster_7_matrix = geometry.compute_cluster_matrix_7(camera=self)
        self.cluster_19_matrix = geometry.compute_cluster_matrix_19(
            camera=self)
from digicampipe.calib.camera import filter, random_triggers, r1, dl0, dl2, dl1
from digicampipe.io.event_stream import event_stream
from digicampipe.utils import utils, geometry
from cts_core.camera import Camera
import astropy.units as u
from digicampipe.io.containers import save_to_pickle_gz

if __name__ == '__main__':
    # Data configuration
    directory = '/mnt/calib_data/first_light/20170831/'
    filename = directory + 'CameraDigicam@sst1mserver_0_000.%d.fits.fz'
    #    file_list = [filename % number for number in range(110,120)]
    urls = [filename % 110]
    camera_config_file = '/usr/src/cts/config/camera_config.cfg'
    digicam = Camera(_config_file=camera_config_file)
    digicam_geometry = geometry.generate_geometry_from_camera(
        camera=digicam, source_x=0 * u.mm, source_y=0. * u.mm)

    # Trigger configuration
    unwanted_patch = [391, 392, 403, 404, 405, 416, 417]

    # Integration configuration
    time_integration_options = {
        'mask': None,
        'mask_edges': None,
        'peak': None,
        'window_start': 3,
        'window_width': 7,
        'threshold_saturation': 3500,
        'n_samples': 50,
        'timing_width': 6,
        'central_sample': 11
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)
    """
        'resources',
        'example_100_evts.000.fits.fz'
    )
)

digicam_config_file = pkg_resources.resource_filename(
    'digicampipe',
    os.path.join(
        'tests',
        'resources',
        'camera_config.cfg'
    )
)

digicam = Camera(_config_file=digicam_config_file)
digicam_geometry = geometry.generate_geometry_from_camera(camera=digicam)


def test_and_benchmark_simple_event_source(benchmark):

    @benchmark
    def simple_loop():
        for _ in zfits_event_source(
                example_file_path,
                camera=digicam,
                camera_geometry=digicam_geometry,
                expert_mode=False,):
            pass


def test_and_benchmark_expert_event_source(benchmark):