Exemplo n.º 1
0
def test_get_specific_event():
    dataset = get_datasets_path("gamma_test.simtel.gz")
    source = hessio_event_source(dataset, requested_event=2)
    event = next(source)
    assert event.count == 2
    assert event.dl0.event_id == 803
    source = hessio_event_source(dataset, requested_event=803, use_event_id=True)
    event = next(source)
    assert event.count == 2
    assert event.dl0.event_id == 803
Exemplo n.º 2
0
def list_telescopes_geometry(simtel_file_path):
    """Print the list of triggered telescopes ID and geometry of the
    'simtel_file_path' file.

    Parameters
    ----------
    simtel_file_path : str
        The path of the simtel file to process.
    """

    source = hessio_event_source(simtel_file_path)

    tel_id_set = set()

    for event in source:
        for tel_id in event.dl0.tels_with_data:
            tel_id_set.add(int(tel_id))

    tel_geometry_dict = {}

    for tel_id in tel_id_set:
        x, y = event.meta.pixel_pos[tel_id]
        foclen = event.meta.optical_foclen[tel_id]
        geom = ctapipe.io.CameraGeometry.guess(x, y, foclen)
        tel_geometry_dict[tel_id] = [geom.cam_id, geom.pix_type]
        print("Telescope {:03d}: {} ({} pixels)".format(tel_id, geom.cam_id, geom.pix_type))

    return tel_geometry_dict
Exemplo n.º 3
0
def test_FitGammaHillas():
    '''
    a test of the complete fit procedure on one event including:
    • tailcut cleaning
    • hillas parametrisation
    • GreatCircle creation
    • direction fit
    • position fit

    in the end, proper units in the output are asserted '''

    filename = get_dataset("gamma_test.simtel.gz")

    fit = HillasReconstructor()

    cam_geom = {}
    tel_phi = {}
    tel_theta = {}

    source = hessio_event_source(filename)

    for event in source:

        hillas_dict = {}
        for tel_id in event.dl0.tels_with_data:

            if tel_id not in cam_geom:
                cam_geom[tel_id] = CameraGeometry.guess(
                                        event.inst.pixel_pos[tel_id][0],
                                        event.inst.pixel_pos[tel_id][1],
                                        event.inst.optical_foclen[tel_id])

                tel_phi[tel_id] = event.mc.tel[tel_id].azimuth_raw * u.rad
                tel_theta[tel_id] = (np.pi/2-event.mc.tel[tel_id].altitude_raw)*u.rad

            pmt_signal = event.r0.tel[tel_id].adc_sums[0]

            mask = tailcuts_clean(cam_geom[tel_id], pmt_signal,
                                  picture_thresh=10., boundary_thresh=5.)
            pmt_signal[mask == 0] = 0

            try:
                moments = hillas_parameters(event.inst.pixel_pos[tel_id][0],
                                            event.inst.pixel_pos[tel_id][1],
                                            pmt_signal)
                hillas_dict[tel_id] = moments
            except HillasParameterizationError as e:
                print(e)
                continue

        if len(hillas_dict) < 2: continue

        fit_result = fit.predict(hillas_dict, event.inst, tel_phi, tel_theta)

        print(fit_result)
        fit_result.alt.to(u.deg)
        fit_result.az.to(u.deg)
        fit_result.core_x.to(u.m)
        assert fit_result.is_valid
        return
Exemplo n.º 4
0
    def read(self, max_events=None):
        """
        Read the file using the appropriate method depending on the file origin

        Parameters
        ----------
        max_events : int
            Maximum number of events to read

        Returns
        -------
        source : generator
            A generator that can be iterated over to obtain events
        """

        # Obtain relevent source
        switch = {
            'hessio':
                lambda: hessio_event_source(get_path(self.input_path),
                                            max_events=max_events),
            'targetio':
                lambda: oxpytools_source(self.input_path,
                                         max_events=max_events),
        }
        try:
            source = switch[self.origin]()
        except KeyError:
            log.exception("unknown file origin '{}'".format(self.origin))
            raise

        return source
def list_simtel_events_per_telescope(simtel_file_path):
    """Make a dictionary of triggered events per telescope of the
    'simtel_file_path' file.

    Parameters
    ----------
    simtel_file_path : str
        The path of the simtel file to process.

    Returns
    -------
    Dictionary of triggered events per telescope of the 'simtel_file_path'
    file (for each item: key=telescope id, value=the list of triggered events).
    """

    source = hessio_event_source(simtel_file_path, allowed_tels=None, max_events=None)

    events_per_tel_dict = {}   # List of events per telescope

    for event in source:
        triggered_telescopes_list = [int(tel_id) for tel_id in event.trig.tels_with_trigger]
        for telescope_id in triggered_telescopes_list:
            if telescope_id not in events_per_tel_dict:
                events_per_tel_dict[telescope_id] = []
            events_per_tel_dict[telescope_id].append(int(event.dl0.event_id))

    return events_per_tel_dict
def list_simtel_content(simtel_file_path):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    # 
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path,
                                 allowed_tels=[1],  
                                 max_events=1)

    for event in source:
        print("Count:", event["count"])
        print("Trigger data:", event["trig"])
        print("Monte-Carlo shower data:", event["mc"])
        print("Raw data:", event["dl0"])

        #print("Simtel file path:", event.meta["hessio__input"])
        print("Pixel pos:", event.meta["pixel_pos"])
        #print("Max events:", event.meta["hessio__max_events"])

        print()
def show_image(simtel_file_path, output_file_path, tel_num, event_id, channel=0, quiet=False):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    # 
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path, allowed_tels=[tel_num])

    event = None

    for ev in source:
        if int(ev.dl0.event_id) == event_id:
            event = ev
            break

    if event is None:
        raise Exception("Error: event '{}' not found for telescope '{}'.".format(event_id, tel_num))

    # INIT PLOT #############################################################

    x, y = event.meta.pixel_pos[tel_num]
    foclen = event.meta.optical_foclen[tel_num]
    geom = ctapipe.io.CameraGeometry.guess(x, y, foclen)

    disp = ctapipe.visualization.CameraDisplay(geom, title='CT%d' % tel_num)
    disp.enable_pixel_picker()
    disp.add_colorbar()

    disp.axes.set_title('Telescope {:03d}, Event {:05d}'.format(tel_num, event_id))

    # DISPLAY TIME-VARYING EVENT ############################################

    #data = event.dl0.tel[tel_num].adc_samples[channel]
    #for ii in range(data.shape[1]):
    #    disp.image = data[:, ii]
    #    disp.set_limits_percent(70)   # TODO
    #    plt.savefig('CT{:03d}_EV{:05d}_S{:02d}.png'.format(tel_num, event_id, ii), bbox_inches='tight')

    # DISPLAY INTEGRATED EVENT ##############################################

    uncalibrated_image = event.dl0.tel[tel_num].adc_sums[channel]
    calibrated_image = apply_mc_calibration(uncalibrated_image, tel_num)

    # The image "event.dl0.tel[tel_num].adc_sums[channel]" is a 1D numpy array (dtype=int32)
    disp.image = calibrated_image

    #disp.set_limits_minmax(0, 9000)
    disp.set_limits_percent(70)        # TODO

    # PLOT ##################################################################

    plt.savefig(output_file_path, bbox_inches='tight')

    if not quiet:
        plt.show()
Exemplo n.º 8
0
    def read(self, allowed_tels=None, requested_event=None,
             use_event_id=False):
        """
        Read the file using the appropriate method depending on the file origin

        Parameters
        ----------
        allowed_tels : list[int]
            select only a subset of telescope, if None, all are read. This can
            be used for example emulate the final CTA data format, where there
            would be 1 telescope per file (whereas in current monte-carlo,
            they are all interleaved into one file)
        requested_event : int
            Seek to a paricular event index
        use_event_id : bool
            If True ,'requested_event' now seeks for a particular event id
            instead of index

        Returns
        -------
        source : generator
            A generator that can be iterated over to obtain events
        """

        # Obtain relevent source
        self.log.debug("Reading file...")
        if self.max_events:
            self.log.info("Max events being read = {}".format(self.max_events))
        source = hessio_event_source(self.input_path,
                                     max_events=self.max_events,
                                     allowed_tels=allowed_tels,
                                     requested_event=requested_event,
                                     use_event_id=use_event_id)
        self.log.debug("File reading complete")
        return source
Exemplo n.º 9
0
def list_simtel_content(simtel_file_path, tel_num, event_id):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    # 
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path, allowed_tels=[tel_num])

    event = None

    for ev in source:
        if int(ev.dl0.event_id) == event_id:
            event = ev
            break

    if event is None:
        print("Error: event '{}' not found for telescope '{}'.".format(event_id, tel_num))
        sys.exit(1)

    # DISPLAY EVENT INFORMATIONS ##############################################

    print("- event:")
    recursive_print(event, level=1)
    print("- event.meta:")
    recursive_print(event.meta, level=1)
    def test_geom_json_file_astri_ex1(self):

        tel_id = 1
        output_json_file = "/tmp/astri.geom.json"

        source = hessio_event_source(ASTRI_SIMTEL_FILE_PATH, allowed_tels=[tel_id])

        for ev in source:
            event = ev

        # Get the geometry object with the "guess()" method

        pix_x = event.inst.pixel_pos[tel_id][0]
        pix_y = event.inst.pixel_pos[tel_id][1]
        optical_foclen = event.inst.optical_foclen[tel_id]

        geom = camera.CameraGeometry.guess(pix_x, pix_y, optical_foclen)

        # Convert and write the geom object

        geometry_converter.geom_to_json_file(geom, output_json_file)
        geom2 = geometry_converter.json_file_to_geom(output_json_file)

        # Check whether the input image has changed

        self.assertEqual(geom2.cam_id, geom.cam_id)
        np.testing.assert_array_equal(geom2.pix_id,         geom.pix_id)
        np.testing.assert_array_equal(geom2.pix_x.value,    geom.pix_x.value)
        np.testing.assert_array_equal(geom2.pix_y.value,    geom.pix_y.value)
        np.testing.assert_array_equal(geom2.pix_area.value, geom.pix_area.value)
        self.assertEqual(geom2.neighbors, geom.neighbors)
        self.assertEqual(geom2.pix_type,  geom.pix_type)
def count_simtel_events(simtel_file_path):
    """
    Count the number of events per telescope in a simtel file.

    Returns the number of events per telescope and the total number of events.
    """

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    #
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path,
                                 allowed_tels=None,
                                 max_events=None)

    num_event_dict = {}   # Number of events per telescope
    total_num_events = 0  # Total number of events

    for event in source:
        total_num_events += 1

        for telescope_id in event["trig"]["tels_with_trigger"]:
            if telescope_id not in num_event_dict:
                num_event_dict[telescope_id] = 0
            num_event_dict[telescope_id] += 1

    return num_event_dict, total_num_events
Exemplo n.º 12
0
def test_get_run_id():
	filename = get_datasets_path("gamma_test.simtel.gz")
	print(filename)
	gen = hessio_event_source(filename)
	event = next(gen)
	tels = event.dl0.tels_with_data
	print(tels)
	assert tels == {38, 47}
Exemplo n.º 13
0
def generate_input_containers():
    # Get event from hessio file, append them into input_containers
    input_filename = get_datasets_path("gamma_test.simtel.gz")
    gen = hessio_event_source(input_filename, max_events=3)
    input_containers = []
    for event in gen:
        input_containers.append(deepcopy(event))
    return input_containers
def show_image(simtel_file_path, output_file_path, tel_num, event_id, channel=0, quiet=False):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    # 
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path, allowed_tels=[tel_num])

    event = None

    for ev in source:
        if int(ev.dl0.event_id) == event_id:
            event = ev
            break

    if event is None:
        raise Exception("Error: event '{}' not found for telescope '{}'.".format(event_id, tel_num))

    # INIT PLOT #############################################################

    x, y = event.meta.pixel_pos[tel_num]
    foclen = event.meta.optical_foclen[tel_num]
    geom = ctapipe.io.CameraGeometry.guess(x, y, foclen)

    disp = ctapipe.visualization.CameraDisplay(geom, title='CT%d' % tel_num)
    disp.enable_pixel_picker()
    disp.add_colorbar()

    disp.axes.set_title('Telescope {:03d}, Event {:05d}'.format(tel_num, event_id))

    # GET PHOTOELECTRON IMAGE (CLEAN IMAGE) #################################

    disp.image = event.mc.tel[tel_num].photo_electrons

    # COMPUTE hILLAS PARAMETERS #############################################

    image = disp.image.copy()
    #hillas = hillas_parameters(geom.pix_x.value, geom.pix_y.value, image)
    hillas = hillas_parameters(geom.pix_x, geom.pix_y, image)
    print("Hillas parameters:", hillas)

    # PLOT ##################################################################

    #disp.set_limits_minmax(0, 9000)
    disp.set_limits_percent(70)        # TODO

    # Plot Hillas parameters
    disp.overlay_moments(hillas, linewidth=3, color='yellow')

    plt.savefig(output_file_path, bbox_inches='tight')

    if not quiet:
        plt.show()
def show_pe_image(simtel_file_path, tel_num=1, channel=0, event_index=0):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    # 
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path,
                                 allowed_tels=[tel_num],
                                 max_events=event_index+1)

    event_list = list(source)          # TODO
    event = event_list[event_index]    # TODO

    # INIT PLOT #############################################################

    x, y = event.meta.pixel_pos[tel_num]
    geom = ctapipe.io.CameraGeometry.guess(x, y)
    disp = ctapipe.visualization.CameraDisplay(geom, title='CT%d' % tel_num)
    disp.enable_pixel_picker()
    disp.add_colorbar()

    disp.axes.set_title('CT{:03d}, event {:010d}'.format(tel_num, event.dl0.event_id))

    # DISPLAY TIME-VARYING EVENT ############################################

    #data = event.dl0.tel[tel_num].adc_samples[channel]
    #for ii in range(data.shape[1]):
    #    disp.image = data[:, ii]
    #    #disp.set_limits_percent(70)   # TODO help(disp.set_limits_percent)
    #    disp.set_limits_minmax(0, 400)
    #    plt.savefig('CT{:03d}_EV{:010d}_S{:02d}.png'.format(tel_num, event.dl0.event_id, ii))

    # DISPLAY INTEGRATED EVENT ##############################################

    #disp.image = event.dl0.tel[tel_num].adc_sums[channel]
    #disp.set_limits_percent(70)        # TODO help(disp.set_limits_percent)


    # TODO: check that (taken from https://github.com/tino-michael/tino_cta/blob/e6cc6db3e64135c9ac92bce2dae6e6f81a36096a/sandbox/show_ADC_and_PE_per_event.py)
    for jj in range(len(event.mc.tel[tel_num].photo_electrons)):
        #event.dl0.tel[tel_num].adc_sums[channel][jj] = event.mc.tel[tel_num].photo_electrons[jj]
        event.r0.tel[tel_num].adc_sums[channel][jj] = event.mc.tel[tel_num].photo_electrons[jj]
    signals2 = event.dl0.tel[tel_num].adc_sums[channel].astype(float)
    disp.image = signals2


    disp.set_limits_minmax(0, 9000)
    plt.savefig('CT{:03d}_EV{:010d}.png'.format(tel_num, event.dl0.event_id))

    # PLOT ##################################################################

    plt.show()
def extract_image(simtel_file_path, tel_num, event_id, channel=0):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    # 
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path, allowed_tels=[tel_num])

    event = None

    for ev in source:
        if int(ev.dl0.event_id) == event_id:
            event = ev
            break

    if event is None:
        raise Exception("Error: event '{}' not found for telescope '{}'.".format(event_id, tel_num))

    # CHECK THE IMAGE GEOMETRY (ASTRI AND SCTCAM ONLY) ######################

    x, y = event.meta.pixel_pos[tel_num]
    foclen = event.meta.optical_foclen[tel_num]
    geom = ctapipe.io.CameraGeometry.guess(x, y, foclen)

    if geom.pix_type != "rectangular":
        raise ValueError("The input image is not a valide ASTRI or SCTCam telescope image.")

    # GET AND CROP THE IMAGE ################################################

    uncalibrated_image = event.dl0.tel[tel_num].adc_sums[channel]         # 1D numpy array
    calibrated_image = apply_mc_calibration(uncalibrated_image, tel_num)

    if geom.cam_id == "ASTRI":
        cropped_img = crop_astri_image(calibrated_image)
    elif geom.cam_id == "SCTCam":
        cropped_img = crop_sctcam_image(calibrated_image)
    else:
        raise ValueError("The input image is not a valide ASTRI or SCTCam telescope image.")

    # GET AND CROP THE PHOTOELECTRON IMAGE ##################################

    pe_image = event.mc.tel[tel_num].photo_electrons       # 1D numpy array

    if geom.cam_id == "ASTRI":
        cropped_pe_img = crop_astri_image(pe_image)
    elif geom.cam_id == "SCTCam":
        cropped_pe_img = crop_sctcam_image(pe_image)
    else:
        raise ValueError("The input image is not a valide ASTRI or SCTCam telescope image.")

    return (cropped_img, cropped_pe_img)
Exemplo n.º 17
0
def test_array_draw():
    filename = get_dataset("gamma_test.simtel.gz")
    cam_geom = {}

    source = hessio_event_source(filename, max_events=2)
    r1 = HessioR1Calibrator(None, None)
    dl0 = CameraDL0Reducer(None, None)

    calibrator = CameraDL1Calibrator(None, None)

    for event in source:
        array_pointing = SkyCoord(event.mcheader.run_array_direction[1] * u.rad,
                                  event.mcheader.run_array_direction[0] * u.rad,
                                  frame=AltAz)
        # array_view = ArrayPlotter(instrument=event.inst,
        #                          system=TiltedGroundFrame(
        # pointing_direction=array_pointing))

        hillas_dict = {}
        r1.calibrate(event)
        dl0.reduce(event)
        calibrator.calibrate(event)  # calibrate the events

        # store MC pointing direction for the array

        for tel_id in event.dl0.tels_with_data:

            pmt_signal = event.dl1.tel[tel_id].image[0]
            geom = event.inst.subarray.tel[tel_id].camera
            fl = event.inst.subarray.tel[tel_id].optics.effective_focal_length

            # Transform the pixels positions into nominal coordinates
            camera_coord = CameraFrame(x=geom.pix_x, y=geom.pix_y,
                                       z=np.zeros(geom.pix_x.shape) * u.m,
                                       focal_length=fl,
                                       rotation=90 * u.deg - geom.cam_rotation)

            nom_coord = camera_coord.transform_to(
                NominalFrame(array_direction=array_pointing,
                             pointing_direction=array_pointing))

            mask = tailcuts_clean(geom, pmt_signal,
                                  picture_thresh=10., boundary_thresh=5.)

            try:
                moments = hillas_parameters(nom_coord.x,
                                            nom_coord.y,
                                            pmt_signal * mask)
                hillas_dict[tel_id] = moments
                nom_coord = NominalPlotter(hillas_parameters=hillas_dict,
                                           draw_axes=True)
                nom_coord.draw_array()

            except HillasParameterizationError as e:
                print(e)
                continue
Exemplo n.º 18
0
 def init(self):
     self.log.debug("--- SimTelArrayReader init {}---".format(self.filename))
     try:
         in_file = get_path(self.filename)
         self.source = hessio_event_source(in_file,max_events=100)
         self.log.debug('{} successfully opened {}'.format(self.filename,self.source))
     except:
         self.log.error('could not open ' + in_file)
         return False
     return True
Exemplo n.º 19
0
def test_calibrate_source():
    telid = 38
    filename = get_path('gamma_test.simtel.gz')
    source = hessio_event_source(filename)
    c_source = calibrate_source(source, get_test_parameters())
    for event in c_source:
        if event.dl0.event_id == 408:
            pe = event.dl1.tel[telid].calibrated_image
            assert round(pe[0], 5) == 1.86419
            break
def show_image_histogram(simtel_file_path, output_file_path, tel_num, event_id, channel=0, quiet=False):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    # 
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path, allowed_tels=[tel_num])

    event = None

    for ev in source:
        if int(ev.dl0.event_id) == event_id:
            event = ev
            break

    if event is None:
        print("Error: event '{}' not found for telescope '{}'.".format(event_id, tel_num))
        sys.exit(1)

    # GET TIME-VARYING EVENT ##################################################

    #data = event.dl0.tel[tel_num].adc_samples[channel]
    #for ii in range(data.shape[1]):
    #    image_array = data[:, ii]

    # GET INTEGRATED EVENT ####################################################

    # The image "event.dl0.tel[tel_num].adc_sums[channel]" is a 1D numpy array (dtype=int32)
    image_array = event.dl0.tel[tel_num].adc_sums[channel]

    # INIT PLOT ###############################################################

    fig = plt.figure(figsize=(8.0, 8.0))
    ax = fig.add_subplot(111)

    values, bins, patches = ax.hist(image_array.ravel(),
                                    histtype=HISTOGRAM_TYPE,
                                    #bins=image_array.max() - image_array.min(),
                                    bins=100,
                                    fc='gray',
                                    ec='k')

    ax.set_xlim([image_array.min(), image_array.max()])

    # PLOT ####################################################################

    plt.savefig(output_file_path, bbox_inches='tight')

    if not quiet:
        plt.show()
Exemplo n.º 21
0
 def init(self):
     
     print("--- HessioReader init ---")
     if self.configuration == None:
         self.configuration = Configuration()
         
     self.raw_data = self.configuration.get('source', section='HESSIO_READER')
     try:
         self.source = hessio_event_source(get_path(self.raw_data), max_events=9)
         return True
     except(RuntimeError):
         print("could not open",self.raw_data, file=stderr)
         return False
Exemplo n.º 22
0
def export_cameras_geometry(simtel_file_path, tel_id):

    source = hessio_event_source(simtel_file_path)

    for event in source:
        #######################################################################
        # CAUTION:                                                            #
        # event.meta.optical_foclen and event.meta.tel_pos are only filled    #
        # when source is traversed !                                          #
        # The current value of these two variables is not the actual value    #
        # for the current event but the cumulated value of past events !      #
        #######################################################################

        print("Event", event.dl0.event_id)
        #print("tels_with_trigger", event.trig.tels_with_trigger)
        #print("tels_with_data", event.dl0.tels_with_data)
        #print("optical_foclen", event.meta.optical_foclen)
        #print("tel_pos", event.meta.tel_pos)

    # GET CAMERAS GEOMETRY ################################
    
    x, y = event.meta.pixel_pos[tel_id]
    foclen = event.meta.optical_foclen[tel_id]

    geom = ctapipe.io.CameraGeometry.guess(x, y, foclen)

    foclen = float(foclen.value)
    x = [float(v.value) for v in x]
    y = [float(v.value) for v in y]
    cam_id = geom.cam_id
    pix_type = geom.pix_type

    camera_dict = {
            "camera_id": cam_id,
            "pixel_type": pix_type,
            "pixel_pos_x": x,
            "pixel_pos_y": y,
            "foclen": foclen,
            "tel_id": tel_id
            }

    # PRINT CAMERAS GEOMETRY ##############################

    for k, v in camera_dict.items():
        print("- ", k, v)

    # EXPORT CAMERAS GEOMETRY #############################

    with open("cameras_geometry_tel{:03d}.json".format(tel_id), "w") as fd:
        #json.dump(camera_dict, fd)                           # no pretty print
        json.dump(camera_dict, fd, sort_keys=True, indent=4)  # pretty print format
Exemplo n.º 23
0
def plot_data(simtel_file_path_list, output_file_path, tel_num, pmt_id, channel=0, quiet=False):

    adc_list = []
    pe_list = []

    for simtel_file_path in simtel_file_path_list:

        print(simtel_file_path)

        # GET EVENT #############################################################

        # hessio_event_source returns a Python generator that streams data from an
        # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
        # This generator contains ctapipe.core.Container instances ("event").
        # 
        # Parameters:
        # - max_events: maximum number of events to read
        # - allowed_tels: select only a subset of telescope, if None, all are read.
        source = hessio_event_source(simtel_file_path, allowed_tels=[tel_num])

        for event in source:
            # Get ADC image
            adc_list.append(event.dl0.tel[tel_num].adc_sums[channel][pmt_id])

            # Get photoelectron image
            pe_list.append(event.mc.tel[tel_num].photo_electrons[pmt_id])


    # INIT PLOT ###############################################################

    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 8))

    ax.plot(pe_list, adc_list, '.')

    #H = ax.hist2d(pe_list, adc_list, bins=1000, norm=LogNorm())
    #fig.colorbar(H[3], ax=ax)

    #ax.set_xlim([0, 60])

    ax.set_title("Telescope {:03d} - PMT {} - Channel {}".format(tel_num, pmt_id, channel))

    ax.set_xlabel("PE", fontsize=24)
    ax.set_ylabel("ADC", fontsize=24)

    # PLOT ####################################################################

    #plt.savefig(output_file_path, bbox_inches='tight')
    plt.savefig(output_file_path)

    if not quiet:
        plt.show()
Exemplo n.º 24
0
    def read(self, allowed_tels=None, requested_event=None,
             use_event_id=False):
        """
        Read the file using the appropriate method depending on the file origin

        Parameters
        ----------
        allowed_tels : list[int]
            select only a subset of telescope, if None, all are read. This can
            be used for example emulate the final CTA data format, where there
            would be 1 telescope per file (whereas in current monte-carlo,
            they are all interleaved into one file)
        requested_event : int
            Seek to a paricular event index
        use_event_id : bool
            If True ,'requested_event' now seeks for a particular event id
            instead of index

        Returns
        -------
        source : generator
            A generator that can be iterated over to obtain events
        """

        # Obtain relevent source
        log.debug("[file] Reading file...")
        if self._max_events:
            log.info("[file] Max events being read = {}"
                     .format(self._max_events))
        switch = {
            'hessio':
                lambda: hessio_event_source(get_path(self.input_path),
                                            max_events=self._max_events,
                                            allowed_tels=allowed_tels,
                                            requested_event=requested_event,
                                            use_event_id=use_event_id),
            'targetio':
                lambda: targetio_source(self.input_path,
                                        max_events=self._max_events,
                                        allowed_tels=allowed_tels,
                                        requested_event=requested_event,
                                        use_event_id=use_event_id),
        }
        try:
            source = switch[self.origin]()
        except KeyError:
            log.exception("unknown file origin '{}'".format(self.origin))
            raise
        log.debug("[file] Reading complete")

        return source
def show_photoelectron_image(simtel_file_path, output_file_path, tel_num, event_id, channel=0, quiet=False):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    # 
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path, allowed_tels=[tel_num])

    event = None

    for ev in source:
        if int(ev.dl0.event_id) == event_id:
            event = ev
            break

    if event is None:
        raise Exception("Error: event '{}' not found for telescope '{}'.".format(event_id, tel_num))

    # INIT PLOT #############################################################

    x, y = event.meta.pixel_pos[tel_num]
    foclen = event.meta.optical_foclen[tel_num]
    geom = ctapipe.io.CameraGeometry.guess(x, y, foclen)

    disp = ctapipe.visualization.CameraDisplay(geom, title='CT%d' % tel_num)
    disp.enable_pixel_picker()
    disp.add_colorbar()

    disp.axes.set_title('Telescope {:03d}, Event {:05d}'.format(tel_num, event_id))

    # GET PHOTOELECTRON IMAGE (CLEAN IMAGE) #################################

    # The photoelectron image "event.mc.tel[tel_num].photo_electrons" is a 1D numpy array with the same shape (dtype=int32)
    # Inspired by https://github.com/tino-michael/tino_cta/blob/e6cc6db3e64135c9ac92bce2dae6e6f81a36096a/sandbox/show_ADC_and_PE_per_event.py
    disp.image = event.mc.tel[tel_num].photo_electrons

    # PLOT ##################################################################

    #disp.set_limits_minmax(0, 9000)
    disp.set_limits_percent(70)        # TODO

    plt.savefig(output_file_path, bbox_inches='tight')

    if not quiet:
        plt.show()
Exemplo n.º 26
0
 def setup(self):
     # load up the telescope types table (need to first open a file, a bit of
     # a hack until a proper insturment module exists) and select only the
     # telescopes with the same camera type
     data = next(hessio_event_source(self.infile, max_events=1))
     camtypes = get_camera_types(data.inst)
     print_camera_types(data.inst, printer=self.log.info)
     group = camtypes.groups[self.telgroup]
     self._selected_tels = group['tel_id'].data
     self._base_tel = self._selected_tels[0]
     self.log.info("Telescope group %d: %s with %s camera", self.telgroup,
                   group[0]['tel_type'], group[0]['cam_type'])
     self.log.info("SELECTED TELESCOPES:{}".format(self._selected_tels))
     self.calibrator = CameraCalibrator(self.config, self)
Exemplo n.º 27
0
def show_pedestal_image(simtel_file_path, output_file_path, tel_num, quiet=False):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    #
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path, allowed_tels=[tel_num])

    # TODO
    for ev in source:
        event = ev

    # INIT PLOT #############################################################

    x, y = event.meta.pixel_pos[tel_num]
    foclen = event.meta.optical_foclen[tel_num]
    geom = ctapipe.io.CameraGeometry.guess(x, y, foclen)

    disp = ctapipe.visualization.CameraDisplay(geom, title="CT%d" % tel_num)
    disp.enable_pixel_picker()
    disp.add_colorbar()

    disp.axes.set_title("Telescope {:03d}".format(tel_num))

    # DISPLAY INTEGRATED EVENT ##############################################

    pedestal, gains = get_mc_calibration_coeffs(tel_num)
    disp.image = pedestal

    # print("pedestal: ndim={} shape={} dtype={}".format(pedestal.ndim, pedestal.shape, pedestal.dtype))
    # print("gains: ndim={} shape={} dtype={}".format(gains.ndim, gains.shape, gains.dtype))
    # print("pedestal:", pedestal)
    # print("gains:", gains)

    # disp.set_limits_minmax(0, 9000)
    disp.set_limits_percent(70)  # TODO

    # PLOT ##################################################################

    plt.savefig(output_file_path, bbox_inches="tight")

    if not quiet:
        plt.show()
def show_image(simtel_file_path, tel_num=1, channel=0, event_index=0):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    # 
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path,
                                 allowed_tels=[tel_num],
                                 max_events=event_index+1)

    event_list = list(source)          # TODO
    event = event_list[event_index]    # TODO

    # INIT PLOT #############################################################

    x, y = event.meta.pixel_pos[tel_num]
    geom = ctapipe.io.CameraGeometry.guess(x, y)
    disp = ctapipe.visualization.CameraDisplay(geom, title='CT%d' % tel_num)
    disp.enable_pixel_picker()
    disp.add_colorbar()

    disp.axes.set_title('CT{:03d}, event {:010d}'.format(tel_num, event.dl0.event_id))

    # DISPLAY TIME-VARYING EVENT ############################################

    #data = event.dl0.tel[tel_num].adc_samples[channel]
    #for ii in range(data.shape[1]):
    #    disp.image = data[:, ii]
    #    #disp.set_limits_percent(70)   # TODO help(disp.set_limits_percent)
    #    disp.set_limits_minmax(0, 400)
    #    plt.savefig('CT{:03d}_EV{:010d}_S{:02d}.png'.format(tel_num, event.dl0.event_id, ii))

    # DISPLAY INTEGRATED EVENT ##############################################

    #disp.image = event.dl0.tel[tel_num].adc_sums[channel]  # ctapipe 0.3.0
    disp.image = event.r0.tel[tel_num].adc_sums[channel]    # ctapipe 0.4.0
    #disp.set_limits_percent(70)        # TODO help(disp.set_limits_percent)
    disp.set_limits_minmax(0, 9000)
    plt.savefig('CT{:03d}_EV{:010d}.png'.format(tel_num, event.dl0.event_id))

    # PLOT ##################################################################

    plt.show()
Exemplo n.º 29
0
def plot_pixels_layout(simtel_file_path, tel_id):

    source = hessio_event_source(simtel_file_path, allowed_tels=[tel_id])

    for event in source:
        #######################################################################
        # CAUTION:                                                            #
        # event.meta.optical_foclen and event.meta.tel_pos are only filled    #
        # when source is traversed !                                          #
        # The current value of these two variables is not the actual value    #
        # for the current event but the cumulated value of past events !      #
        #######################################################################
        print("Event", event.dl0.event_id)

    (pos_x_list, pos_y_list) = event.meta.pixel_pos[tel_id]
    pos_x_list = [float(pos.value) for pos in pos_x_list]
    pos_y_list = [float(pos.value) for pos in pos_y_list]

    assert len(pos_x_list) == len(pos_y_list)

    # PLOT ####################################################################

    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(10, 10))
    
    # Scatter method ##################

    ax.scatter(pos_x_list,    # x
               pos_y_list,    # y
               s=0.2,  # radius
               c="black",  # color
               alpha=0.75)

    ax.plot(pos_x_list,    # x
            pos_y_list,    # y
            "k-",
            alpha=0.25)

    for pixel_index in range(len(pos_x_list)):
        ax.text(pos_x_list[pixel_index], pos_y_list[pixel_index], str(pixel_index), fontsize=9)

    ax.set_title("Pixels position (telescope {})".format(tel_id), fontsize=16)

    ax.set_xlabel("x position (m)", fontsize=16)
    ax.set_ylabel("y position (m)", fontsize=16)

    plt.show()
Exemplo n.º 30
0
def test_FitGammaHillas():

    filename = get_path("gamma_test.simtel.gz")

    fit = FitGammaHillas()
    fit.setup_geometry(*load_hessio(filename), phi=180 * u.deg, theta=20 * u.deg)

    tel_geom = {}

    source = hessio_event_source(filename)

    for event in source:

        hillas_dict = {}
        for tel_id in set(event.trig.tels_with_trigger) & set(event.dl0.tels_with_data):

            if tel_id not in tel_geom:
                tel_geom[tel_id] = CameraGeometry.guess(
                    fit.cameras(tel_id)["PixX"].to(u.m),
                    fit.cameras(tel_id)["PixY"].to(u.m),
                    fit.telescopes["FL"][tel_id - 1] * u.m,
                )

            pmt_signal = event.dl0.tel[tel_id].adc_sums[0]

            mask = tailcuts_clean(tel_geom[tel_id], pmt_signal, 1, picture_thresh=10.0, boundary_thresh=5.0)
            pmt_signal[mask is False] = 0

            try:
                moments, moms2 = hillas_parameters(fit.cameras(tel_id)["PixX"], fit.cameras(tel_id)["PixY"], pmt_signal)
                hillas_dict[tel_id] = moments
            except HillasParameterizationError as e:
                print(e)
                continue

        if len(hillas_dict) < 2:
            continue

        fit_result = fit.predict(hillas_dict)

        print(fit_result)
        assert fit_result
        return
def get_num_events(Filename, right_tel):
    try:
        source = hessio_event_source(Filename, allowed_tels=right_tel)
    except:
        os.exit(1)

    num_events = 0
    anzahl_gesamt = 0
    image1 = 0
    image2 = 0
    for event in source:
        num_events += 1
        for tel_id in event.r0.tels_with_data:
            anzahl_gesamt += 1
            camera_art = str(event.inst.subarray.tel[tel_id].camera)
            if camera_art == "FlashCam":
                image1 += 1
            else:
                image2 += 1
    print(num_events)
    print(anzahl_gesamt)
    print(image1)
    print(image2)
    return num_events, image1, image2
def list_telescopes_geometry(simtel_file_path):
    """Print the list of triggered telescopes ID and geometry of the
    'simtel_file_path' file.

    Parameters
    ----------
    simtel_file_path : str
        The path of the simtel file to process.
    """

    source = hessio_event_source(simtel_file_path)

    tel_id_set = set()

    for event in source:
        for tel_id in event.r0.tels_with_data:
            tel_id_set.add(tel_id)

    for tel_id in tel_id_set:
        x, y = event.inst.pixel_pos[tel_id]
        foclen = event.inst.optical_foclen[tel_id]
        geom = ctapipe.io.CameraGeometry.guess(x, y, foclen)
        print("Telescope {:03d}: {} ({} pixels)".format(
            tel_id, geom.cam_id, geom.pix_type))
Exemplo n.º 33
0
def event_set_operations(simtel_file_path_list):

    event_set_dict = {}

    # For each simtel file...
    for simtel_file_path in simtel_file_path_list:

        event_set = set(
        )  # Set of events (or more exactly pairs (event_id, telescope_id))

        source = hessio_event_source(simtel_file_path,
                                     allowed_tels=None,
                                     max_events=None)

        for event in source:
            event_id = event.dl0.event_id
            for telescope_id in event.trig.tels_with_trigger:
                event_set.add((int(event_id), int(telescope_id)))

        if len(event_set) > 0:
            # Ignore empty simtel files
            event_set_dict[simtel_file_path] = event_set

    return event_set_dict
Exemplo n.º 34
0
def test_eventplotter():
    dataset = get_dataset("gamma_test.simtel.gz")
    source = hessio_event_source(dataset, max_events=1)
    event = next(source)
    del source

    telid = list(event.r0.tels_with_data)[0]

    data = event.r0.tel[telid].adc_samples[0]
    plotter = CameraPlotter(event)

    camera = plotter.draw_camera(telid, data[:, 0])
    assert camera is not None
    np.testing.assert_array_equal(camera.image, data[:, 0])

    plotter.draw_camera_pixel_ids(telid, [0, 1, 2])

    waveform = plotter.draw_waveform(data[0, :])
    assert waveform is not None
    np.testing.assert_array_equal(waveform.get_ydata(), data[0, :])

    line = plotter.draw_waveform_positionline(0)
    assert line is not None
    np.testing.assert_array_equal(line.get_xdata(), [0, 0])
Exemplo n.º 35
0
def plot_telarray_layout(simtel_file_path, show_labels=False):

    # GET EVENT ###############################################################

    source = hessio_event_source(simtel_file_path)

    for event in source:
        #######################################################################
        # WARNING:                                                            #
        # event.meta.optical_foclen and event.meta.tel_pos are only filled    #
        # when source is traversed !                                          #
        # The current value of these two variables is not the actual value    #
        # for the current event but the cumulated value of past events !      #
        #######################################################################

        pass
        #print("Event", event.dl0.event_id)
        #print("tels_with_trigger", event.trig.tels_with_trigger)
        #print("tels_with_data", event.dl0.tels_with_data)
        #print("optical_foclen", event.meta.optical_foclen)
        #print("tel_pos", event.meta.tel_pos)

    # Print values

    print()
    print("optical_foclen:")
    for telid, foclen in event.meta.optical_foclen.items():
        print(" - TEL {:03d}: {}".format(telid, foclen))

    print()
    print("tel_pos:")
    for telid, position in event.meta.tel_pos.items():
        print(" - TEL {:03d}: {}".format(telid, position))

    # Make a numpy array of telescopes position

    tel_list = []
    for tel_id, tel_pos in event.meta.tel_pos.items():
        tel_id = int(tel_id)
        tel_pos = [float(v.value) for v in tel_pos]
        tel_foclen = float(event.meta.optical_foclen[tel_id].value)
        tel_list.append([tel_id, *tel_pos, tel_foclen])

    tel_array = np.array(tel_list)
    #print(len(tel_array))

    # PLOT ####################################################################

    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(10, 10))

    # Patches method ##################

    #for tel_id, pos_x, pos_y, pos_z, tel_foclen in tel_list:
    #    ax.add_patch(plt.Circle((pos_x, pos_y), radius=tel_foclen, color='red'))

    #max_foclen = float(np.max(tel_array[:,4]))
    #margin = 2. * max_foclen

    #ax.set_xlim([np.min(tel_array[:,1]) - margin, np.max(tel_array[:,1]) + margin])
    #ax.set_ylim([np.min(tel_array[:,2]) - margin, np.max(tel_array[:,2]) + margin])

    # Scatter method ##################

    ax.scatter(
        tel_array[:, 1],  # x
        tel_array[:, 2],  # y
        s=tel_array[:, 4],  # radius
        c=tel_array[:, 4],  # color
        alpha=0.75)

    ## Highlight some telescopes
    #ax.scatter(tel_array[:,1],    # x
    #           tel_array[:,2],    # y
    #           s=tel_array[:,4],  # radius
    #           c="gray",  # color
    #           alpha=0.25)

    #for n in range(1-1, 4):
    #    ax.scatter(tel_array[n,1],    # x
    #               tel_array[n,2],    # y
    #               s=32,  # radius
    #               c="red")  # color

    if show_labels:
        for tel_id, pos_x, pos_y, pos_z, tel_foclen in tel_list:
            ax.text(pos_x + tel_foclen / 2., pos_y, str(tel_id), fontsize=9)

    #ax.set_title("Telescopes position for " + simtel_file_path)
    ax.set_title("Telescopes position", fontsize=16)

    ax.set_xlabel("x position (m)", fontsize=16)
    ax.set_ylabel("y position (m)", fontsize=16)

    plt.savefig("telarray.pdf", bbox_inches='tight')

    plt.show()
Exemplo n.º 36
0
def extract_images(simtel_file_path,
                   tel_id_filter_list=None,
                   event_id_filter_list=None,
                   output_directory=None):

    # EXTRACT IMAGES ##########################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    #
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path,
                                 allowed_tels=tel_id_filter_list)

    # ITERATE OVER EVENTS #####################################################

    calib = CameraCalibrator(None, None)

    for event in source:

        calib.calibrate(event)  # calibrate the event

        event_id = int(event.dl0.event_id)

        if (event_id_filter_list is None) or (event_id
                                              in event_id_filter_list):

            #print("event", event_id)

            # ITERATE OVER IMAGES #############################################

            for tel_id in event.trig.tels_with_trigger:

                tel_id = int(tel_id)

                if tel_id in tel_id_filter_list:

                    #print("telescope", tel_id)

                    # CHECK THE IMAGE GEOMETRY ################################

                    #print("checking geometry")

                    x, y = event.inst.subarray.tel[
                        tel_id].camera.pix_x, event.inst.subarray.tel[
                            tel_id].camera.pix_y
                    foclen = event.inst.subarray.tel[
                        tel_id].optics.equivalent_focal_length
                    geom = CameraGeometry.guess(x, y, foclen)

                    if (geom.pix_type != "hexagonal") or (geom.cam_id !=
                                                          "DigiCam"):
                        raise ValueError(
                            "Telescope {}: error (the input image is not a valide DigiCam telescope image) -> {} ({})"
                            .format(tel_id, geom.pix_type, geom.cam_id))

                    # GET IMAGES ##############################################

                    pe_image = event.mc.tel[
                        tel_id].photo_electron_image  # 1D np array

                    #uncalibrated_image = event.dl0.tel[tel_id].adc_sums  # ctapipe 0.3.0
                    uncalibrated_image = event.r0.tel[
                        tel_id].adc_sums  # ctapipe 0.4.0
                    pedestal = event.mc.tel[tel_id].pedestal
                    gain = event.mc.tel[tel_id].dc_to_pe
                    pixel_pos = (event.inst.subarray.tel[tel_id].camera.pix_x,
                                 event.inst.subarray.tel[tel_id].camera.pix_y)

                    calibrated_image = event.dl1.tel[tel_id].image

                    #print(pe_image.shape)
                    #print(calibrated_image.shape)
                    #print(uncalibrated_image.shape)
                    #print(pedestal.shape)
                    #print(gain.shape)
                    #print(pixel_pos.shape)
                    #print(pixel_pos[0])
                    #print(pixel_pos[1])

                    # CONVERTING GEOMETRY (1D TO 2D) ##########################

                    buffer_id_str = geom.cam_id + "0"

                    geom2d, pe_image_2d = ctapipe_geom_converter.convert_geometry_1d_to_2d(
                        geom, pe_image, buffer_id_str, add_rot=0)
                    geom2d, calibrated_image_2d = ctapipe_geom_converter.convert_geometry_1d_to_2d(
                        geom, calibrated_image[0], buffer_id_str, add_rot=0)

                    geom2d, uncalibrated_image_2d = ctapipe_geom_converter.convert_geometry_1d_to_2d(
                        geom, uncalibrated_image[0], buffer_id_str, add_rot=0)
                    geom2d, pedestal_2d = ctapipe_geom_converter.convert_geometry_1d_to_2d(
                        geom, pedestal[0], buffer_id_str, add_rot=0)
                    geom2d, gains_2d = ctapipe_geom_converter.convert_geometry_1d_to_2d(
                        geom, gain[0], buffer_id_str, add_rot=0)

                    # Make a mock pixel position array...
                    pixel_pos_2d = np.array(
                        np.meshgrid(
                            np.linspace(pixel_pos[0].min(), pixel_pos[0].max(),
                                        pe_image_2d.shape[0]),
                            np.linspace(pixel_pos[1].min(), pixel_pos[1].max(),
                                        pe_image_2d.shape[1])))

                    # PUT NAN IN BLANK PIXELS #################################

                    calibrated_image_2d[np.logical_not(geom2d.mask)] = np.nan
                    pe_image_2d[np.logical_not(geom2d.mask)] = np.nan

                    uncalibrated_image_2d[np.logical_not(geom2d.mask)] = np.nan
                    pedestal_2d[np.logical_not(geom2d.mask)] = np.nan
                    gains_2d[np.logical_not(geom2d.mask)] = np.nan

                    pixel_pos_2d[0, np.logical_not(geom2d.mask)] = np.nan
                    pixel_pos_2d[1, np.logical_not(geom2d.mask)] = np.nan

                    ###########################################################

                    # The ctapipe geometry converter operate on one channel
                    # only and then takes and return a 2D array but pywicta
                    # fits files keep all channels and thus takes 3D arrays...

                    uncalibrated_image_2d = np.array([uncalibrated_image_2d])
                    pedestal_2d = np.array([pedestal_2d])
                    gains_2d = np.array([gains_2d])

                    ###########################################################

                    #print(pe_image_2d.shape)
                    #print(calibrated_image_2d.shape)
                    #print(uncalibrated_image_2d.shape)
                    #print(pedestal_2d.shape)
                    #print(gains_2d.shape)

                    #img = pixel_pos_2d
                    #print(img[1])

                    #import matplotlib.pyplot as plt
                    #im = plt.imshow(img[1])
                    #plt.colorbar(im)
                    #plt.show()
                    #sys.exit(0)

                    # GET PIXEL MASK ##########################################

                    pixel_mask = geom2d.mask.astype(
                        int
                    )  # 1 for pixels with actual data, 0 for virtual (blank) pixels

                    # MAKE METADATA ###########################################

                    metadata = {}

                    metadata[
                        'version'] = 1  # Version of the pywicta fits format

                    metadata['cam_id'] = "DigiCam"

                    metadata['tel_id'] = tel_id
                    metadata['event_id'] = event_id
                    metadata['simtel'] = simtel_file_path

                    metadata['tel_trig'] = len(event.trig.tels_with_trigger)

                    metadata['energy'] = quantity_to_tuple(
                        event.mc.energy, 'TeV')
                    metadata['mc_az'] = quantity_to_tuple(event.mc.az, 'rad')
                    metadata['mc_alt'] = quantity_to_tuple(event.mc.alt, 'rad')
                    metadata['mc_corex'] = quantity_to_tuple(
                        event.mc.core_x, 'm')
                    metadata['mc_corey'] = quantity_to_tuple(
                        event.mc.core_y, 'm')
                    metadata['mc_hfi'] = quantity_to_tuple(
                        event.mc.h_first_int, 'm')

                    metadata['count'] = int(event.count)

                    metadata['run_id'] = int(event.dl0.obs_id)
                    metadata['tel_data'] = len(event.dl0.tels_with_data)

                    metadata['foclen'] = quantity_to_tuple(
                        event.inst.subarray.tel[tel_id].optics.
                        equivalent_focal_length, 'm')
                    metadata['tel_posx'] = quantity_to_tuple(
                        event.inst.subarray.tel_coords[tel_id].x, 'm')
                    metadata['tel_posy'] = quantity_to_tuple(
                        event.inst.subarray.tel_coords[tel_id].y, 'm')
                    metadata['tel_posz'] = quantity_to_tuple(
                        event.inst.subarray.tel_coords[tel_id].z, 'm')

                    # TODO: Astropy fails to store the following data in FITS files
                    #metadata['uid'] = os.getuid()
                    #metadata['datetime'] = str(datetime.datetime.now())
                    #metadata['version'] = VERSION
                    #metadata['argv'] = " ".join(sys.argv).encode('ascii', errors='ignore').decode('ascii')
                    #metadata['python'] = " ".join(sys.version.splitlines()).encode('ascii', errors='ignore').decode('ascii')
                    #metadata['system'] = " ".join(os.uname())

                    # SAVE THE IMAGE ##########################################

                    output_file_path_template = "{}_TEL{:03d}_EV{:05d}.fits"

                    if output_directory is not None:
                        simtel_basename = os.path.basename(simtel_file_path)
                        prefix = os.path.join(output_directory,
                                              simtel_basename)
                    else:
                        prefix = simtel_file_path

                    output_file_path = output_file_path_template.format(
                        prefix, tel_id, event_id)

                    print("saving", output_file_path)

                    images.save_benchmark_images(
                        img=calibrated_image_2d,
                        pe_img=pe_image_2d,
                        adc_sums_img=uncalibrated_image_2d,
                        pedestal_img=pedestal_2d,
                        gains_img=gains_2d,
                        pixel_pos=pixel_pos_2d,
                        pixel_mask=pixel_mask,
                        metadata=metadata,
                        output_file_path=output_file_path)
Exemplo n.º 37
0
        image.save("test_images/test_" + str(GLOBAL_COUNT) + ".png")
        global GLOBAL_COUNT
        GLOBAL_COUNT += 1

    return im_array


if __name__ == '__main__':

    parser = argparse.ArgumentParser(
        description='Reads event parameters directly from simtel files.')
    parser.add_argument('data_file', help='path to input .simtel file')
    args = parser.parse_args()

    # load camera types from first event, take note of
    source = hessio_event_source(args.data_file, max_events=1)

    LST_list = []
    SCT_list = []
    SST_list = []

    for event in source:
        for i in event.inst.telescope_ids:
            if event.inst.num_pixels[i] == 11328:
                SCT_list.append(i)
            elif event.inst.num_pixels[i] == 1855:
                LST_list.append(i)
            # elif event.inst.num_pixels[i] ==

    # camera calibrator
    cal = CameraCalibrator(None, None)
Exemplo n.º 38
0
                        default=get_example_simtelarray_file())
    parser.add_argument('-m', '--max-events', type=int, default=10)
    parser.add_argument('-c', '--channel', type=int, default=0)
    parser.add_argument('-w',
                        '--write',
                        action='store_true',
                        help='write images to files')
    parser.add_argument('-s',
                        '--show-samples',
                        action='store_true',
                        help='show time-variablity, one frame at a time')
    args = parser.parse_args()

    source = hessio_event_source(args.filename,
                                 allowed_tels=[
                                     args.tel,
                                 ],
                                 max_events=args.max_events)
    disp = None

    print('SELECTING EVENTS FROM TELESCOPE {}'.format(args.tel))
    print('=' * 70)

    for event in source:

        print('Scanning input file... count = {}'.format(event.count))
        print(event.trig)
        print(event.mc)
        print(event.dl0)

        if disp is None:
                        required=False,help='display the camera events')
    parser.add_argument('--tel', dest='tel_id', action='store',
                        required=False, default=None,
                        help='Telescope ID to display')
    parser.add_argument('--pdf', dest='pdffilename', action='store',
                        required=False, default="./tmp.pdf",
                        help='PDF output filename')

    args = parser.parse_args()

    if args.display:
        plt.show(block=False)

    tel, cam, opt = ID.load(args.filename)
    # Load the DL0 events into the generator source
    source = hessio_event_source(args.filename)

    print(args.tel_id)
    # Display the calibrated results into camera displays
    if args.display:
        if args.tel_id is not None:
            display_telescope(source,cam,int(args.tel_id),args.pdffilename)
        else:
            logger.error("[event] please specify one telescope "
                      "(given in the file name) ")

            exit()
 

    logger.info("[COMPLETE]")
    sys.stdout.flush()
Exemplo n.º 40
0
    filenamelist = glob( "{}*run{}*gz".format(args.indir,args.runnr ))
    if len(filenamelist) == 0: 
        print("no files found; check indir: {}".format(args.indir))
        exit(-1)
    
    fit = FitGammaLikelihood()
    fit.set_atmosphere("atmprof_paranal.dat")
    fit.set_instrument_description( *load_hessio(filenamelist[0]) )
    
    signal.signal(signal.SIGINT, signal_handler)
    for filename in sorted(filenamelist):
        print("filename = {}".format(filename))
        
        source = hessio_event_source(filename,
                                    # for now use only identical telescopes...
                                    allowed_tels=[1,2,3,4],
                                    #allowed_tels=TelDict[args.teltype],
                                    max_events=args.max_events)

        for event in source:
            ## TODO replace with actual pixel direction when they become available
            #for tel_id in event.dl0.tels_with_data:
                #if tel_id not in fit.pix_dirs:
                    #geom = CameraGeometry.guess(fit.cameras(tel_id)['PixX'].to(u.m), fit.cameras(tel_id)['PixY'].to(u.m), fit.telescopes['FL'][tel_id-1] * u.m)
                    #fit.pix_dirs[tel_id] = guessPixDirectionFocLength(geom.pix_x, geom.pix_y, tel_phi, tel_theta, fit.telescopes['FL'][tel_id-1] * u.m)


            print('Scanning input file... count = {}'.format(event.count))
            print('available telscopes: {}'.format(event.dl0.tels_with_data))

            fit.fill_pdf(event=event)
Exemplo n.º 41
0
def main():

    # your favourite units here
    energy_unit = u.TeV
    angle_unit = u.deg
    dist_unit = u.m

    parser = make_argparser()
    parser.add_argument(
        '-o',
        '--outfile',
        type=str,
        help="if given, write output file with reconstruction results")
    parser.add_argument('--plot_c',
                        action='store_true',
                        help="plot camera-wise displays")
    group = parser.add_mutually_exclusive_group()
    group.add_argument('--proton',
                       action='store_true',
                       help="do protons instead of gammas")
    group.add_argument('--electron',
                       action='store_true',
                       help="do electrons instead of gammas")

    args = parser.parse_args()

    if args.infile_list:
        filenamelist = []
        for f in args.infile_list:
            filenamelist += glob("{}/{}".format(args.indir, f))
    elif args.proton:
        filenamelist = glob("{}/proton/*gz".format(args.indir))
        channel = "proton"
    elif args.electron:
        filenamelist = glob("{}/electron/*gz".format(args.indir))
        channel = "electron"
    elif args.gamma:
        filenamelist = glob("{}/gamma/*gz".format(args.indir))
        channel = "gamma"
    else:
        raise ValueError("don't know which input to use...")
    filenamelist.sort()

    if not filenamelist:
        print("no files found; check indir: {}".format(args.indir))
        exit(-1)
    else:
        print("found {} files".format(len(filenamelist)))

    tel_phi = {}
    tel_theta = {}

    # keeping track of events and where they were rejected
    Eventcutflow = CutFlow("EventCutFlow")
    Imagecutflow = CutFlow("ImageCutFlow")

    # takes care of image cleaning
    cleaner = ImageCleaner(mode=args.mode,
                           cutflow=Imagecutflow,
                           wavelet_options=args.raw,
                           skip_edge_events=args.skip_edge_events,
                           island_cleaning=True)

    # the class that does the shower reconstruction
    shower_reco = HillasReconstructor()

    shower_max_estimator = ShowerMaxEstimator("paranal")

    preper = EventPreparer(
        cleaner=cleaner,
        hillas_parameters=hillas_parameters,
        shower_reco=shower_reco,
        event_cutflow=Eventcutflow,
        image_cutflow=Imagecutflow,
        # event/image cuts:
        allowed_cam_ids=[],  # means: all
        min_ntel=3,
        min_charge=args.min_charge,
        min_pixel=3)

    # a signal handler to abort the event loop but still do the post-processing
    signal_handler = SignalHandler()
    signal.signal(signal.SIGINT, signal_handler)

    try:
        # this class defines the reconstruction parameters to keep track of
        class RecoEvent(tb.IsDescription):
            NTels_trigg = tb.Int16Col(dflt=1, pos=0)
            NTels_clean = tb.Int16Col(dflt=1, pos=1)
            EnMC = tb.Float32Col(dflt=1, pos=2)
            xi = tb.Float32Col(dflt=1, pos=3)
            DeltaR = tb.Float32Col(dflt=1, pos=4)
            ErrEstPos = tb.Float32Col(dflt=1, pos=5)
            ErrEstDir = tb.Float32Col(dflt=1, pos=6)
            h_max = tb.Float32Col(dflt=1, pos=7)

        reco_outfile = tb.open_file(
            args.outfile,
            mode="w",
            # if we don't want to write the event list to disk, need to add more arguments
            **({} if args.store else {
                "driver": "H5FD_CORE",
                "driver_core_backing_store": False
            }))
        reco_table = reco_outfile.create_table("/", "reco_event", RecoEvent)
        reco_event = reco_table.row
    except:
        reco_event = RecoEvent()
        print("no pytables installed?")

    # ##        #######   #######  ########
    # ##       ##     ## ##     ## ##     ##
    # ##       ##     ## ##     ## ##     ##
    # ##       ##     ## ##     ## ########
    # ##       ##     ## ##     ## ##
    # ##       ##     ## ##     ## ##
    # ########  #######   #######  ##

    cam_id_map = {}

    # define here which telescopes to loop over
    allowed_tels = None
    # allowed_tels = prod3b_tel_ids("L+F+D")
    for i, filename in enumerate(filenamelist[:args.last]):
        print("file: {i} filename = {filename}".format(i=i, filename=filename))

        source = hessio_event_source(filename,
                                     allowed_tels=allowed_tels,
                                     max_events=args.max_events)

        # loop that cleans and parametrises the images and performs the reconstruction
        for (event, hillas_dict, n_tels, tot_signal, max_signal, pos_fit,
             dir_fit, h_max, err_est_pos,
             err_est_dir) in preper.prepare_event(source):

            shower = event.mc

            org_alt = u.Quantity(shower.alt).to(u.deg)
            org_az = u.Quantity(shower.az).to(u.deg)
            if org_az > 180 * u.deg:
                org_az -= 360 * u.deg

            org_the = alt_to_theta(org_alt)
            org_phi = az_to_phi(org_az)
            if org_phi > 180 * u.deg:
                org_phi -= 360 * u.deg
            if org_phi < -180 * u.deg:
                org_phi += 360 * u.deg

            shower_org = linalg.set_phi_theta(org_phi, org_the)
            shower_core = convert_astropy_array([shower.core_x, shower.core_y])

            xi = linalg.angle(dir_fit, shower_org).to(angle_unit)
            diff = linalg.length(pos_fit[:2] - shower_core)

            # print some performance
            print()
            print("xi = {:4.3f}".format(xi))
            print("pos = {:4.3f}".format(diff))
            print("h_max reco: {:4.3f}".format(h_max.to(u.km)))
            print("err_est_dir: {:4.3f}".format(err_est_dir.to(angle_unit)))
            print("err_est_pos: {:4.3f}".format(err_est_pos))

            try:
                # store the reconstruction data in the PyTable
                reco_event["NTels_trigg"] = n_tels["tot"]
                reco_event["NTels_clean"] = len(shower_reco.circles)
                reco_event["EnMC"] = event.mc.energy / energy_unit
                reco_event["xi"] = xi / angle_unit
                reco_event["DeltaR"] = diff / dist_unit
                reco_event["ErrEstPos"] = err_est_pos / dist_unit
                reco_event["ErrEstDir"] = err_est_dir / angle_unit
                reco_event["h_max"] = h_max / dist_unit
                reco_event.append()
                reco_table.flush()

                print()
                print("xi res (68-percentile) = {:4.3f} {}".format(
                    np.percentile(reco_table.cols.xi, 68), angle_unit))
                print("core res (68-percentile) = {:4.3f} {}".format(
                    np.percentile(reco_table.cols.DeltaR, 68), dist_unit))
                print("h_max (median) = {:4.3f} {}".format(
                    np.percentile(reco_table.cols.h_max, 50), dist_unit))

            except NoPyTables:
                pass

            if args.plot_c:
                from mpl_toolkits.mplot3d import Axes3D
                fig = plt.figure()
                ax = fig.gca(projection='3d')
                for c in shower_reco.circles.values():
                    points = [
                        c.pos + t * c.a * u.km for t in np.linspace(0, 15, 3)
                    ]
                    ax.plot(*np.array(points).T,
                            linewidth=np.sqrt(c.weight) / 10)
                    ax.scatter(*c.pos[:, None].value, s=np.sqrt(c.weight))
                plt.xlabel("x")
                plt.ylabel("y")
                plt.pause(.1)

                # this plots
                # • the MC shower core
                # • the reconstructed shower core
                # • the used telescopes
                # • and the trace of the Hillas plane on the ground
                plt.figure()
                for tel_id, c in shower_reco.circles.items():
                    plt.scatter(c.pos[0], c.pos[1], s=np.sqrt(c.weight))
                    plt.gca().annotate(tel_id,
                                       (c.pos[0].value, c.pos[1].value))
                    plt.plot([
                        c.pos[0].value - 500 * c.norm[1],
                        c.pos[0].value + 500 * c.norm[1]
                    ], [
                        c.pos[1].value + 500 * c.norm[0],
                        c.pos[1].value - 500 * c.norm[0]
                    ],
                             linewidth=np.sqrt(c.weight) / 10)
                plt.scatter(*pos_fit[:2],
                            c="black",
                            marker="*",
                            label="fitted")
                plt.scatter(*shower_core[:2],
                            c="black",
                            marker="P",
                            label="MC")
                plt.legend()
                plt.xlabel("x")
                plt.ylabel("y")
                plt.xlim(-1400, 1400)
                plt.ylim(-1400, 1400)
                plt.show()

            if signal_handler.stop: break
        if signal_handler.stop: break

    print("\n" + "=" * 35 + "\n")
    print("xi res (68-percentile) = {:4.3f} {}".format(
        np.percentile(reco_table.cols.xi, 68), angle_unit))
    print("core res (68-percentile) = {:4.3f} {}".format(
        np.percentile(reco_table.cols.DeltaR, 68), dist_unit))
    print("h_max (median) = {:4.3f} {}".format(
        np.percentile(reco_table.cols.h_max, 50), dist_unit))

    # print the cutflows for telescopes and camera images
    print("\n\n")
    Eventcutflow("min2Tels trig")
    print()
    Imagecutflow(sort_column=1)

    # if we don't want to plot anything, we can exit now
    if not args.plot:
        return

    # ########  ##        #######  ########  ######
    # ##     ## ##       ##     ##    ##    ##    ##
    # ##     ## ##       ##     ##    ##    ##
    # ########  ##       ##     ##    ##     ######
    # ##        ##       ##     ##    ##          ##
    # ##        ##       ##     ##    ##    ##    ##
    # ##        ########  #######     ##     ######

    plt.figure()
    plt.hist(reco_table.cols.h_max, bins=np.linspace(000, 15000, 51, True))
    plt.title(channel)
    plt.xlabel("h_max reco")
    plt.pause(.1)

    figure = plt.figure()
    xi_edges = np.linspace(0, 5, 20)
    plt.hist(reco_table.cols.xi, bins=xi_edges, log=True)
    plt.xlabel(r"$\xi$ / deg")
    if args.write:
        save_fig('{}/reco_xi_{}'.format(args.plots_dir, args.mode))
    plt.pause(.1)

    plt.figure()
    plt.hist(reco_table.cols.ErrEstDir[:], bins=np.linspace(0, 20, 50))
    plt.title(channel)
    plt.xlabel("beta")
    plt.pause(.1)

    plt.figure()
    plt.hist(np.log10(reco_table.cols.xi[:] / reco_table.cols.ErrEstDir[:]),
             bins=50)
    plt.title(channel)
    plt.xlabel("log_10(xi / beta)")
    plt.pause(.1)

    # convert the xi-list into a dict with the number of used telescopes as keys
    xi_vs_tel = {}
    for xi, ntel in zip(reco_table.cols.xi, reco_table.cols.NTels_clean):
        if ntel not in xi_vs_tel:
            xi_vs_tel[ntel] = [xi]
        else:
            xi_vs_tel[ntel].append(xi)

    print(args.mode)
    for ntel, xis in sorted(xi_vs_tel.items()):
        print("NTel: {} -- median xi: {}".format(ntel, np.median(xis)))
        # print("histogram:", np.histogram(xis, bins=xi_edges))

    # create a list of energy bin-edges and -centres for violin plots
    Energy_edges = np.linspace(2, 8, 13)
    Energy_centres = (Energy_edges[1:] + Energy_edges[:-1]) / 2.

    # convert the xi-list in to an energy-binned dict with the bin centre as keys
    xi_vs_energy = {}
    for en, xi in zip(reco_table.cols.EnMC, reco_table.cols.xi):

        # get the bin number this event belongs into
        sbin = np.digitize(np.log10(en), Energy_edges) - 1

        # the central value of the bin is the key for the dictionary
        if Energy_centres[sbin] not in xi_vs_energy:
            xi_vs_energy[Energy_centres[sbin]] = [xi]
        else:
            xi_vs_energy[Energy_centres[sbin]] += [xi]

    # plotting the angular error as violin plots with binning in
    # number of telescopes and shower energy
    figure = plt.figure()
    plt.subplot(211)
    plt.violinplot([np.log10(a) for a in xi_vs_tel.values()],
                   [a for a in xi_vs_tel.keys()],
                   points=60,
                   widths=.75,
                   showextrema=False,
                   showmedians=True)
    plt.xlabel("Number of Telescopes")
    plt.ylabel(r"log($\xi$ / deg)")
    plt.ylim(-3, 2)
    plt.grid()

    plt.subplot(212)
    plt.violinplot([np.log10(a) for a in xi_vs_energy.values()],
                   [a for a in xi_vs_energy.keys()],
                   points=60,
                   widths=(Energy_edges[1] - Energy_edges[0]) / 1.5,
                   showextrema=False,
                   showmedians=True)
    plt.xlabel(r"log(Energy / GeV)")
    plt.ylabel(r"log($\xi$ / deg)")
    plt.ylim(-3, 2)
    plt.grid()

    plt.tight_layout()
    if args.write:
        save_fig('{}/reco_xi_vs_E_NTel_{}'.format(args.plots_dir, args.mode))

    plt.pause(.1)

    # convert the diffs-list into a dict with the number of used telescopes as keys
    diff_vs_tel = {}
    for diff, ntel in zip(reco_table.cols.DeltaR, reco_table.cols.NTels_clean):
        if ntel not in diff_vs_tel:
            diff_vs_tel[ntel] = [diff]
        else:
            diff_vs_tel[ntel].append(diff)

    # convert the diffs-list in to an energy-binned dict with the bin centre as keys
    diff_vs_energy = {}
    for en, diff in zip(reco_table.cols.EnMC, reco_table.cols.DeltaR):

        # get the bin number this event belongs into
        sbin = np.digitize(np.log10(en), Energy_edges) - 1

        # the central value of the bin is the key for the dictionary
        if Energy_centres[sbin] not in diff_vs_energy:
            diff_vs_energy[Energy_centres[sbin]] = [diff]
        else:
            diff_vs_energy[Energy_centres[sbin]] += [diff]

    # plotting the core position error as violin plots with binning in
    # number of telescopes an shower energy
    plt.figure()
    plt.subplot(211)
    plt.violinplot([np.log10(a) for a in diff_vs_tel.values()],
                   [a for a in diff_vs_tel.keys()],
                   points=60,
                   widths=.75,
                   showextrema=False,
                   showmedians=True)
    plt.xlabel("Number of Telescopes")
    plt.ylabel(r"log($\Delta R$ / m)")
    plt.grid()

    plt.subplot(212)
    plt.violinplot([np.log10(a) for a in diff_vs_energy.values()],
                   [a for a in diff_vs_energy.keys()],
                   points=60,
                   widths=(Energy_edges[1] - Energy_edges[0]) / 1.5,
                   showextrema=False,
                   showmedians=True)
    plt.xlabel(r"log(Energy / GeV)")
    plt.ylabel(r"log($\Delta R$ / m)")
    plt.grid()

    plt.tight_layout()
    if args.write:
        save_fig('{}/reco_dist_vs_E_NTel_{}'.format(args.plots_dir, args.mode))
    plt.show()
Exemplo n.º 42
0
"""
The most basic pipeline, using no special features of the framework other 
than a for-loop. This is useful for debugging and profiling of speed.
"""

import sys

import numpy as np

from ctapipe.calib import CameraCalibrator
from ctapipe.io.hessio import hessio_event_source

if __name__ == '__main__':

    filename = sys.argv[1]

    source = hessio_event_source(filename, max_events=None)

    cal = CameraCalibrator(None, None)

    for data in source:

        print("EVENT: {}, ENERGY: {:.2f}, TELS:{}".format(
            data.r0.event_id, data.mc.energy, len(data.dl0.tels_with_data)))

        cal.calibrate(data)

        # now the calibrated images are in data.dl1.tel[x].image
Exemplo n.º 43
0
    disp = None
    dirpath = '/Users/armstrongt/Workspace/CTA/CHEC-S-ASTRI/sicily_analysis/scripts/'
    filelist = np.loadtxt(sys.argv[1], dtype=str)

    # for ep in eps:

    if write is True:
        outfile = open('%shillas_parameters_gamma_mc.dat' % (dirpath), 'w')
        outfile.write(
            '#ID[0],size[1],cenX[2],cenY[3],l[4],w[5],r[6],phi[7],psi[8],miss[9],skewness[10],kurtosis[11], '
            'recox[12], recoy[13], maxpe [14] || MC: ||, energy[15], alt[16], az[17], core_x[18], core_y[19],'
            'hfirstint[20\n')

    for filename in filelist:

        source = hessio_event_source(filename, max_events=1000)
        for event in source:
            try:
                calib.calibrate(event)
                if display_each is True:
                    if disp is None:
                        geom = event.inst.subarray.tel[1].camera
                        disp = CameraDisplay(geom)
                        disp.add_colorbar()
                        plt.show(block=False)
                else:
                    geom = event.inst.subarray.tel[1].camera
                im = event.dl1.tel[1].image[0]
                mask = tailcuts_clean(geom,
                                      im,
                                      picture_thresh=10,
Exemplo n.º 44
0
    if len(sys.argv) > 1:
        filename = sys.argv.pop(1)
    else:
        filename = get_dataset("gamma_test.simtel.gz")

    # set a fixed window (for now from samples 20 to the end), which may not
    # be appropriate for all telescopes (this should eventually be
    # chosen based on the telescope type, since some have shorter
    # sample windows:

    start = 15
    end = None  # None means through sample

    # loop over all events, all telescopes and all channels and call
    # the calc_peds function defined above to do some work:
    for event in hessio_event_source(filename):
        for telid in event.r0.tels_with_data:
            for chan in range(event.r0.tel[telid].adc_samples.shape[0]):

                print("CT{} chan {}:".format(telid, chan))

                traces = event.r0.tel[telid].adc_samples[chan, ...]

                peds, pedvars = pedestals.calc_pedestals_from_traces(
                    traces, start, end)

                print("Number of samples: {}".format(traces.shape[1]))
                print("Calculate over window:({},{})".format(start, end))
                print("PEDS:", peds)
                print("VARS:", pedvars)
                print("-----")
Exemplo n.º 45
0
from pyspark import SparkContext
from pyspark.streaming import StreamingContext
from pyspark.streaming.kafka import KafkaUtils
from ctapipe.image.cleaning import tailcuts_clean
from ctapipe.io import CameraGeometry
from ctapipe.image.hillas import hillas_parameters, HillasParameterizationError
from astropy import units as u

import msgpack
import msgpack_numpy as m
from ctapipe.reco.FitGammaHillas import FitGammaHillas
from ctapipe.io.hessio import hessio_event_source

# we read the instrument configuration from an arbitrary simtel file
# instrument info does not change per event. (except for pointing maybe)
source = hessio_event_source('gamma_test.simtel.gz', max_events=7)
instrument = next(source).inst

# Create a local StreamingContext with three working threads and batch
# interval of 5 seconds
sc = SparkContext('local[3]', 'test app')
broadcastInst = sc.broadcast(instrument)
ssc = StreamingContext(sc, 5)


def reco(single_telescope_data):
    fit = FitGammaHillas()
    inst = broadcastInst.value
    hillas_dict, tel_phi, tel_theta = single_telescope_data

    fit_result = fit.predict(hillas_dict, inst, tel_phi, tel_theta)
def extract_images(simtel_file_path,
                   tel_id_filter_list=None,
                   event_id_filter_list=None,
                   output_directory=None):

    # EXTRACT IMAGES ##########################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    #
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path,
                                 allowed_tels=tel_id_filter_list)

    # ITERATE OVER EVENTS #####################################################

    for event in source:

        event_id = int(event.dl0.event_id)

        if (event_id_filter_list is None) or (event_id
                                              in event_id_filter_list):

            print("event", event_id)

            # ITERATE OVER IMAGES #############################################

            for tel_id in event.trig.tels_with_trigger:

                tel_id = int(tel_id)

                if tel_id in tel_id_filter_list:

                    print("telescope", tel_id)

                    # CHECK THE IMAGE GEOMETRY (ASTRI ONLY) ###################

                    # TODO

                    print("checking geometry")

                    x, y = event.meta.pixel_pos[tel_id]
                    foclen = event.meta.optical_foclen[tel_id]
                    geom = ctapipe.io.CameraGeometry.guess(x, y, foclen)

                    if (geom.pix_type != "rectangular") or (geom.cam_id !=
                                                            "ASTRI"):
                        raise ValueError(
                            "Telescope {}: error (the input image is not a valide ASTRI telescope image)"
                            .format(tel_id))

                    # GET AND CROP THE IMAGE ##################################

                    # uncalibrated_image = [1D numpy array of channel1, 1D numpy array of channel2]
                    # calibrated_image = 1D numpy array

                    print("calibrating")

                    adc_channel_0 = event.dl0.tel[tel_id].adc_sums[0]  # TODO
                    adc_channel_1 = event.dl0.tel[tel_id].adc_sums[1]  # TODO
                    uncalibrated_image = np.array(
                        [adc_channel_0, adc_channel_1])

                    calibrated_image = apply_mc_calibration(
                        uncalibrated_image, tel_id)

                    print("cropping ADC image")

                    cropped_img = crop_astri_image(calibrated_image)

                    # GET AND CROP THE PHOTOELECTRON IMAGE ####################

                    pe_image = event.mc.tel[
                        tel_id].photo_electrons  # 1D np array

                    print("cropping PE image")

                    cropped_pe_img = crop_astri_image(pe_image)

                    # SAVE THE IMAGE ##########################################

                    output_file_path_template = "{}_EV{:05d}_TEL{:03d}.fits"

                    if output_directory is not None:
                        simtel_basename = os.path.basename(simtel_file_path)
                        prefix = os.path.join(output_directory,
                                              simtel_basename)
                    else:
                        prefix = simtel_file_path

                    output_file_path = output_file_path_template.format(
                        prefix, event_id, tel_id)

                    print("saving", output_file_path)

                    metadata = {}
                    metadata['tel_id'] = tel_id
                    metadata['foclen'] = quantity_to_tuple(
                        event.meta.optical_foclen[tel_id], 'm')
                    metadata['event_id'] = event_id
                    metadata['energy'] = quantity_to_tuple(
                        event.mc.energy, 'TeV')
                    metadata['mc_az'] = quantity_to_tuple(event.mc.az, 'rad')
                    metadata['mc_alt'] = quantity_to_tuple(event.mc.alt, 'rad')
                    metadata['mc_corex'] = quantity_to_tuple(
                        event.mc.core_x, 'm')
                    metadata['mc_corey'] = quantity_to_tuple(
                        event.mc.core_y, 'm')

                    save_fits(cropped_img, cropped_pe_img, output_file_path,
                              metadata)
Exemplo n.º 47
0
def test_convert_geometry():
    filename = get_path("gamma_test.simtel.gz")

    cam_geom = {}

    source = hessio_event_source(filename)

    # testing a few images just for the sake of being thorough
    counter = 5

    for event in source:

        for tel_id in event.dl0.tels_with_data:
            if tel_id not in cam_geom:
                cam_geom[tel_id] = CameraGeometry.guess(
                    event.inst.pixel_pos[tel_id][0],
                    event.inst.pixel_pos[tel_id][1],
                    event.inst.optical_foclen[tel_id])

            # we want to test conversion of hex to rectangular pixel grid
            if cam_geom[tel_id].pix_type is not "hexagonal":
                continue

            print(tel_id, cam_geom[tel_id].pix_type)

            pmt_signal = apply_mc_calibration(
                #event.dl0.tel[tel_id].adc_samples[0],
                event.dl0.tel[tel_id].adc_sums[0],
                event.mc.tel[tel_id].dc_to_pe[0],
                event.mc.tel[tel_id].pedestal[0])

            new_geom, new_signal = convert_geometry_1d_to_2d(
                cam_geom[tel_id],
                pmt_signal,
                cam_geom[tel_id].cam_id,
                add_rot=-2)

            unrot_geom, unrot_signal = convert_geometry_back(
                new_geom,
                new_signal,
                cam_geom[tel_id].cam_id,
                event.inst.optical_foclen[tel_id],
                add_rot=4)

            # if run as main, do some plotting
            if __name__ == "__main__":
                fig = plt.figure()
                plt.style.use('seaborn-talk')

                ax1 = fig.add_subplot(131)
                disp1 = CameraDisplay(
                    cam_geom[tel_id],
                    image=np.sum(pmt_signal, axis=1)
                    if pmt_signal.shape[-1] == 25 else pmt_signal,
                    ax=ax1)
                disp1.cmap = plt.cm.hot
                disp1.add_colorbar()
                plt.title("original geometry")

                ax2 = fig.add_subplot(132)
                disp2 = CameraDisplay(
                    new_geom,
                    image=np.sum(new_signal, axis=2)
                    if new_signal.shape[-1] == 25 else new_signal,
                    ax=ax2)
                disp2.cmap = plt.cm.hot
                disp2.add_colorbar()
                plt.title("slanted geometry")

                ax3 = fig.add_subplot(133)
                disp3 = CameraDisplay(
                    unrot_geom,
                    image=np.sum(unrot_signal, axis=1)
                    if unrot_signal.shape[-1] == 25 else unrot_signal,
                    ax=ax3)
                disp3.cmap = plt.cm.hot
                disp3.add_colorbar()
                plt.title("geometry converted back to hex")

                plt.show()

            # do some tailcuts cleaning
            mask1 = tailcuts_clean(cam_geom[tel_id],
                                   pmt_signal,
                                   1,
                                   picture_thresh=10.,
                                   boundary_thresh=5.)

            mask2 = tailcuts_clean(unrot_geom,
                                   unrot_signal,
                                   1,
                                   picture_thresh=10.,
                                   boundary_thresh=5.)
            pmt_signal[mask1 == False] = 0
            unrot_signal[mask2 == False] = 0
            '''
            testing back and forth conversion on hillas parameters... '''
            try:
                moments1 = hillas_parameters(cam_geom[tel_id].pix_x,
                                             cam_geom[tel_id].pix_y,
                                             pmt_signal)

                moments2 = hillas_parameters(unrot_geom.pix_x,
                                             unrot_geom.pix_y, unrot_signal)
            except (HillasParameterizationError, AssertionError) as e:
                '''
                we don't want this test to fail because the hillas code
                threw an error '''
                print(e)
                counter -= 1
                if counter < 0:
                    return
                else:
                    continue
            '''
            test if the hillas parameters from the original geometry and the
            forth-and-back rotated geometry are close '''
            assert np.allclose([
                moments1.length.value, moments1.width.value, moments1.phi.value
            ], [
                moments2.length.value, moments2.width.value, moments2.phi.value
            ],
                               rtol=1e-2,
                               atol=1e-2)
            counter -= 1
            if counter < 0:
                return
Exemplo n.º 48
0
    def get_charge(self):
        fig = plt.figure(1)
        ax = fig.add_subplot(111)

        calculator = ChargeResolutionCalculator(config=None, tool=None)
        calculator2 = ChargeResolutionCalculator(config=None, tool=None)

        pevals = [
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20,
            21, 23, 24, 26, 28, 30, 32, 35, 37, 40, 43, 46, 49, 53, 57, 61, 65,
            70, 75, 81, 86, 93, 100, 107, 114, 123, 132, 141, 151, 162, 174,
            187, 200, 215, 231, 247, 265, 284, 305, 327, 351, 376, 403, 432,
            464, 497, 533, 572, 613, 657, 705, 756, 811, 869, 932, 1000
        ]

        for n, i in enumerate(pevals):
            self.filename = '/Users/armstrongt/Workspace/CTA/MCValidation/data/bypass2_enoise_pe_e%s.simtel.gz' % n
            ntrig = 0
            source = hessio_event_source(self.filename,
                                         allowed_tels=None,
                                         max_events=None)
            for event in source:
                self.calib.calibrate(event)
                true_charge = event.mc.tel[
                    1].photo_electron_image * self.pixel_mask
                measured_charge = event.dl1.tel[1].image[0] * self.pixel_mask
                true_charge2 = np.asarray(
                    [int(i)] * len(measured_charge)) * self.pixel_mask
                calculator.add_charges(true_charge, measured_charge)
                calculator2.add_charges(true_charge2, measured_charge)
                ntrig = ntrig + 1

        x, res, res_error, scaled_res, scaled_res_error = calculator.get_charge_resolution(
        )
        x2, res2, res_error2, scaled_res2, scaled_res_error2 = calculator2.get_charge_resolution(
        )
        ax.errorbar(x,
                    res,
                    yerr=res_error,
                    marker='x',
                    linestyle="None",
                    label='MC Charge Res')
        ax.errorbar(x2,
                    res2,
                    yerr=res_error2,
                    marker='x',
                    color='C1',
                    linestyle="None",
                    label='\'Lab\' Charge Res')
        x = np.logspace(np.log10(0.9), np.log10(1000 * 1.1), 100)
        requirement = ChargeResolutionCalculator.requirement(x)
        goal = ChargeResolutionCalculator.goal(x)
        poisson = ChargeResolutionCalculator.poisson(x)
        r_p = ax.plot(x, requirement, 'r', ls='--', label='Requirement')
        g_p = ax.plot(x, goal, 'g', ls='--', label='Goal')
        p_p = ax.plot(x, poisson, c='0.75', ls='--', label='Poisson')
        plt.legend()
        plt.yscale('log')
        plt.xscale('log')
        plt.xlabel('true charge')
        plt.ylabel('charge resolution')
        plt.show()
Exemplo n.º 49
0
def get_test_event():
    filename = get_path('gamma_test.simtel.gz')
    for event in hessio_event_source(filename):
        if event.dl0.event_id == 409:
            return event
Exemplo n.º 50
0
    Imagecutflow.add_cut("features nan", lambda x: np.isnan(x).any())

    energy_mc = []
    energy_rec = []

    filenamelist_gamma = sorted(glob("{}/gamma/*gz".format(args.indir)))
    filenamelist_gamma = sorted(
        glob(expandvars("$CTA_DATA/Prod3b/Paranal/*simtel.gz")))

    allowed_tels = prod3b_tel_ids("L+N+D")
    for filename in filenamelist_gamma[:5][:args.last]:

        print("filename = {}".format(filename))

        source = hessio_event_source(filename,
                                     allowed_tels=allowed_tels,
                                     max_events=args.max_events)

        # loop that cleans and parametrises the images and performs the reconstruction
        for (event, hillas_dict, n_tels, tot_signal, max_signals, pos_fit,
             dir_fit, h_max, err_est_pos,
             err_est_dir) in preper.prepare_event(source):

            # now prepare the features for the classifier
            cls_features_evt = {}
            reg_features_evt = {}
            for tel_id in hillas_dict.keys():
                Imagecutflow.count("pre-features")

                tel_pos = np.array(event.inst.tel_pos[tel_id][:2]) * u.m
Exemplo n.º 51
0
def test_array_draw():

    filename = get_dataset("gamma_test.simtel.gz")
    cam_geom = {}

    source = hessio_event_source(filename, max_events=2)
    r1 = HessioR1Calibrator(None, None)
    dl0 = CameraDL0Reducer(None, None)

    calibrator = CameraDL1Calibrator(None, None)

    for event in source:
        array_pointing = SkyCoord(
            event.mcheader.run_array_direction[1] * u.rad,
            event.mcheader.run_array_direction[0] * u.rad,
            frame=AltAz)
        #array_view = ArrayPlotter(instrument=event.inst,
        #                          system=TiltedGroundFrame(pointing_direction=array_pointing))

        hillas_dict = {}
        r1.calibrate(event)
        dl0.reduce(event)
        calibrator.calibrate(event)  # calibrate the events

        # store MC pointing direction for the array

        for tel_id in event.dl0.tels_with_data:

            pmt_signal = event.dl1.tel[tel_id].image[0]

            x, y = event.inst.pixel_pos[tel_id]
            fl = event.inst.optical_foclen[tel_id]

            if tel_id not in cam_geom:
                cam_geom[tel_id] = CameraGeometry.guess(
                    event.inst.pixel_pos[tel_id][0],
                    event.inst.pixel_pos[tel_id][1],
                    event.inst.optical_foclen[tel_id])

            # Transform the pixels positions into nominal coordinates
            camera_coord = CameraFrame(x=x,
                                       y=y,
                                       z=np.zeros(x.shape) * u.m,
                                       focal_length=fl,
                                       rotation=90 * u.deg -
                                       cam_geom[tel_id].cam_rotation)

            nom_coord = camera_coord.transform_to(
                NominalFrame(array_direction=array_pointing,
                             pointing_direction=array_pointing))

            mask = tailcuts_clean(cam_geom[tel_id],
                                  pmt_signal,
                                  picture_thresh=10.,
                                  boundary_thresh=5.)

            try:
                moments = hillas_parameters(nom_coord.x, nom_coord.y,
                                            pmt_signal * mask)
                hillas_dict[tel_id] = moments
                nom_coord = NominalPlotter(hillas_parameters=hillas_dict,
                                           draw_axes=True)
                nom_coord.draw_array()

            except HillasParameterizationError as e:
                print(e)
                continue
Exemplo n.º 52
0
def get_test_event():
    filename = get_path('gamma_test.simtel.gz')
    source = hessio_event_source(filename, requested_event=409,
                                 use_event_id=True)
    event = next(source)
    return event
Exemplo n.º 53
0
import numpy as np
from matplotlib import pyplot as plt
from os.path import realpath, join, dirname
from ctapipe.utils.datasets import get_path
from ctapipe.io.hessio import hessio_event_source
from ctapipe.instrument import CameraGeometry
from ctapipe.visualization import CameraDisplay
from targetpipe.io.pixels import checm_pixel_id


filepath = get_path("/Users/Jason/Software/outputs/sim_telarray/meudon_gamma/"
                    "simtel_runmeudon_gamma_30tel_30deg_19.gz")
# filepath = get_path(sys.argv[1])

source = hessio_event_source(filepath, max_events=1)
event = next(source)
tel = list(event.dl0.tels_with_data)[0]
pos_arr = np.array(event.inst.pixel_pos[tel])

n_pix = pos_arr.shape[1]

pos_arr = pos_arr[:, checm_pixel_id]
pos_arr[1] = -pos_arr[1]

fig = plt.figure(figsize=(13, 13))
ax = fig.add_subplot(111)
geom = CameraGeometry.guess(*event.inst.pixel_pos[tel],
                            event.inst.optical_foclen[tel])
camera = CameraDisplay(geom, ax=ax, image=np.zeros(2048))

for pix in range(n_pix):
def main():

    # your favourite units here
    energy_unit = u.TeV
    angle_unit = u.deg
    dist_unit = u.m

    agree_threshold = .5
    min_tel = 3

    parser = make_argparser()
    parser.add_argument('--classifier',
                        type=str,
                        default=expandvars(
                            "$CTA_SOFT/tino_cta/data/classifier_pickle/"
                            "classifier_{mode}_{cam_id}_{classifier}.pkl"))
    parser.add_argument('--regressor',
                        type=str,
                        default=expandvars(
                            "$CTA_SOFT/tino_cta/data/classifier_pickle/"
                            "regressor_{mode}_{cam_id}_{regressor}.pkl"))
    parser.add_argument('-o',
                        '--outfile',
                        type=str,
                        default="",
                        help="location to write the classified events to.")
    parser.add_argument('--wave_dir',
                        type=str,
                        default=None,
                        help="directory where to find mr_filter. "
                        "if not set look in $PATH")
    parser.add_argument(
        '--wave_temp_dir',
        type=str,
        default='/dev/shm/',
        help="directory where mr_filter to store the temporary fits "
        "files")

    group = parser.add_mutually_exclusive_group()
    group.add_argument('--proton',
                       action='store_true',
                       help="do protons instead of gammas")
    group.add_argument('--electron',
                       action='store_true',
                       help="do electrons instead of gammas")

    args = parser.parse_args()

    if args.infile_list:
        filenamelist = []
        for f in args.infile_list:
            filenamelist += glob("{}/{}".format(args.indir, f))
        filenamelist.sort()
    elif args.proton:
        filenamelist = sorted(glob("{}/proton/*gz".format(args.indir)))
    elif args.electron:
        filenamelist = glob("{}/electron/*gz".format(args.indir))
        channel = "electron"
    else:
        filenamelist = sorted(glob("{}/gamma/*gz".format(args.indir)))

    if not filenamelist:
        print("no files found; check indir: {}".format(args.indir))
        exit(-1)

    # keeping track of events and where they were rejected
    Eventcutflow = CutFlow("EventCutFlow")
    Imagecutflow = CutFlow("ImageCutFlow")

    # takes care of image cleaning
    cleaner = ImageCleaner(mode=args.mode,
                           cutflow=Imagecutflow,
                           wavelet_options=args.raw,
                           tmp_files_directory=args.wave_temp_dir,
                           skip_edge_events=False,
                           island_cleaning=True)

    # the class that does the shower reconstruction
    shower_reco = HillasReconstructor()

    preper = EventPreparer(
        cleaner=cleaner,
        hillas_parameters=hillas_parameters,
        shower_reco=shower_reco,
        event_cutflow=Eventcutflow,
        image_cutflow=Imagecutflow,
        # event/image cuts:
        allowed_cam_ids=[],
        min_ntel=2,
        min_charge=args.min_charge,
        min_pixel=3)

    # wrapper for the scikit-learn classifier
    classifier = EventClassifier.load(args.classifier.format(
        **{
            "mode": args.mode,
            "wave_args": "mixed",
            "classifier": 'RandomForestClassifier',
            "cam_id": "{cam_id}"
        }),
                                      cam_id_list=args.cam_ids)

    # wrapper for the scikit-learn regressor
    regressor = EnergyRegressor.load(args.regressor.format(
        **{
            "mode": args.mode,
            "wave_args": "mixed",
            "regressor": "RandomForestRegressor",
            "cam_id": "{cam_id}"
        }),
                                     cam_id_list=args.cam_ids)

    ClassifierFeatures = namedtuple(
        "ClassifierFeatures",
        ("impact_dist", "sum_signal_evt", "max_signal_cam", "sum_signal_cam",
         "N_LST", "N_MST", "N_SST", "width", "length", "skewness", "kurtosis",
         "h_max", "err_est_pos", "err_est_dir"))

    EnergyFeatures = namedtuple(
        "EnergyFeatures",
        ("impact_dist", "sum_signal_evt", "max_signal_cam", "sum_signal_cam",
         "N_LST", "N_MST", "N_SST", "width", "length", "skewness", "kurtosis",
         "h_max", "err_est_pos", "err_est_dir"))

    # catch ctr-c signal to exit current loop and still display results
    signal_handler = SignalHandler()
    signal.signal(signal.SIGINT, signal_handler)

    # this class defines the reconstruction parameters to keep track of
    class RecoEvent(tb.IsDescription):
        Run_ID = tb.Int16Col(dflt=-1, pos=0)
        Event_ID = tb.Int16Col(dflt=-1, pos=1)
        NTels_trig = tb.Int16Col(dflt=0, pos=0)
        NTels_reco = tb.Int16Col(dflt=0, pos=1)
        NTels_reco_lst = tb.Int16Col(dflt=0, pos=2)
        NTels_reco_mst = tb.Int16Col(dflt=0, pos=3)
        NTels_reco_sst = tb.Int16Col(dflt=0, pos=4)
        MC_Energy = tb.Float32Col(dflt=np.nan, pos=5)
        reco_Energy = tb.Float32Col(dflt=np.nan, pos=6)
        reco_phi = tb.Float32Col(dflt=np.nan, pos=7)
        reco_theta = tb.Float32Col(dflt=np.nan, pos=8)
        off_angle = tb.Float32Col(dflt=np.nan, pos=9)
        xi = tb.Float32Col(dflt=np.nan, pos=10)
        DeltaR = tb.Float32Col(dflt=np.nan, pos=11)
        ErrEstPos = tb.Float32Col(dflt=np.nan, pos=12)
        ErrEstDir = tb.Float32Col(dflt=np.nan, pos=13)
        gammaness = tb.Float32Col(dflt=np.nan, pos=14)
        success = tb.BoolCol(dflt=False, pos=15)

    channel = "gamma" if "gamma" in " ".join(filenamelist) else "proton"
    reco_outfile = tb.open_file(
        mode="w",
        # if no outfile name is given (i.e. don't to write the event list to disk),
        # need specify two "driver" arguments
        **({
            "filename": args.outfile
        } if args.outfile else {
            "filename": "no_outfile.h5",
            "driver": "H5FD_CORE",
            "driver_core_backing_store": False
        }))

    reco_table = reco_outfile.create_table("/", "reco_events", RecoEvent)
    reco_event = reco_table.row

    allowed_tels = None  # all telescopes
    allowed_tels = prod3b_tel_ids("L+N+D")
    for i, filename in enumerate(filenamelist[:args.last]):
        # print(f"file: {i} filename = {filename}")

        source = hessio_event_source(filename,
                                     allowed_tels=allowed_tels,
                                     max_events=args.max_events)

        # loop that cleans and parametrises the images and performs the reconstruction
        for (event, hillas_dict, n_tels, tot_signal, max_signals, pos_fit,
             dir_fit, h_max, err_est_pos,
             err_est_dir) in preper.prepare_event(source, True):

            # now prepare the features for the classifier
            cls_features_evt = {}
            reg_features_evt = {}
            if hillas_dict is not None:
                for tel_id in hillas_dict.keys():
                    Imagecutflow.count("pre-features")

                    tel_pos = np.array(event.inst.tel_pos[tel_id][:2]) * u.m

                    moments = hillas_dict[tel_id]

                    impact_dist = linalg.length(tel_pos - pos_fit)
                    cls_features_tel = ClassifierFeatures(
                        impact_dist=impact_dist / u.m,
                        sum_signal_evt=tot_signal,
                        max_signal_cam=max_signals[tel_id],
                        sum_signal_cam=moments.size,
                        N_LST=n_tels["LST"],
                        N_MST=n_tels["MST"],
                        N_SST=n_tels["SST"],
                        width=moments.width / u.m,
                        length=moments.length / u.m,
                        skewness=moments.skewness,
                        kurtosis=moments.kurtosis,
                        h_max=h_max / u.m,
                        err_est_pos=err_est_pos / u.m,
                        err_est_dir=err_est_dir / u.deg)

                    reg_features_tel = EnergyFeatures(
                        impact_dist=impact_dist / u.m,
                        sum_signal_evt=tot_signal,
                        max_signal_cam=max_signals[tel_id],
                        sum_signal_cam=moments.size,
                        N_LST=n_tels["LST"],
                        N_MST=n_tels["MST"],
                        N_SST=n_tels["SST"],
                        width=moments.width / u.m,
                        length=moments.length / u.m,
                        skewness=moments.skewness,
                        kurtosis=moments.kurtosis,
                        h_max=h_max / u.m,
                        err_est_pos=err_est_pos / u.m,
                        err_est_dir=err_est_dir / u.deg)

                    if np.isnan(cls_features_tel).any() or np.isnan(
                            reg_features_tel).any():
                        continue

                    Imagecutflow.count("features nan")

                    cam_id = event.inst.subarray.tel[tel_id].camera.cam_id

                    try:
                        reg_features_evt[cam_id] += [reg_features_tel]
                        cls_features_evt[cam_id] += [cls_features_tel]
                    except KeyError:
                        reg_features_evt[cam_id] = [reg_features_tel]
                        cls_features_evt[cam_id] = [cls_features_tel]

            if cls_features_evt and reg_features_evt:

                predict_energ = regressor.predict_by_event([reg_features_evt
                                                            ])["mean"][0]
                predict_proba = classifier.predict_proba_by_event(
                    [cls_features_evt])
                gammaness = predict_proba[0, 0]

                try:
                    # the MC direction of origin of the simulated particle
                    shower = event.mc
                    shower_core = np.array(
                        [shower.core_x / u.m, shower.core_y / u.m]) * u.m
                    shower_org = linalg.set_phi_theta(az_to_phi(shower.az),
                                                      alt_to_theta(shower.alt))

                    # and how the reconstructed direction compares to that
                    xi = linalg.angle(dir_fit, shower_org)
                    DeltaR = linalg.length(pos_fit[:2] - shower_core)
                except Exception:
                    # naked exception catch, because I'm not sure where
                    # it would break in non-MC files
                    xi = np.nan
                    DeltaR = np.nan

                phi, theta = linalg.get_phi_theta(dir_fit)
                phi = (phi if phi > 0 else phi + 360 * u.deg)

                # TODO: replace with actual array pointing direction
                array_pointing = linalg.set_phi_theta(0 * u.deg, 20. * u.deg)
                # angular offset between the reconstructed direction and the array
                # pointing
                off_angle = linalg.angle(dir_fit, array_pointing)

                reco_event["NTels_trig"] = len(event.dl0.tels_with_data)
                reco_event["NTels_reco"] = len(hillas_dict)
                reco_event["NTels_reco_lst"] = n_tels["LST"]
                reco_event["NTels_reco_mst"] = n_tels["MST"]
                reco_event["NTels_reco_sst"] = n_tels["SST"]
                reco_event["reco_Energy"] = predict_energ.to(energy_unit).value
                reco_event["reco_phi"] = phi / angle_unit
                reco_event["reco_theta"] = theta / angle_unit
                reco_event["off_angle"] = off_angle / angle_unit
                reco_event["xi"] = xi / angle_unit
                reco_event["DeltaR"] = DeltaR / dist_unit
                reco_event["ErrEstPos"] = err_est_pos / dist_unit
                reco_event["ErrEstDir"] = err_est_dir / angle_unit
                reco_event["gammaness"] = gammaness
                reco_event["success"] = True
            else:
                reco_event["success"] = False

            # save basic event infos
            reco_event["MC_Energy"] = event.mc.energy.to(energy_unit).value
            reco_event["Event_ID"] = event.r1.event_id
            reco_event["Run_ID"] = event.r1.run_id

            reco_table.flush()
            reco_event.append()

            if signal_handler.stop:
                break
        if signal_handler.stop:
            break

    # make sure everything gets written out nicely
    reco_table.flush()

    try:
        print()
        Eventcutflow()
        print()
        Imagecutflow()

        # do some simple event selection
        # and print the corresponding selection efficiency
        N_selected = len([
            x for x in reco_table.where(
                """(NTels_reco > min_tel) & (gammaness > agree_threshold)""")
        ])
        N_total = len(reco_table)
        print("\nfraction selected events:")
        print("{} / {} = {} %".format(N_selected, N_total,
                                      N_selected / N_total * 100))

    except ZeroDivisionError:
        pass

    print("\nlength filenamelist:", len(filenamelist[:args.last]))

    # do some plotting if so desired
    if args.plot:
        gammaness = [x['gammaness'] for x in reco_table]
        NTels_rec = [x['NTels_reco'] for x in reco_table]
        NTel_bins = np.arange(np.min(NTels_rec), np.max(NTels_rec) + 2) - .5

        NTels_rec_lst = [x['NTels_reco_lst'] for x in reco_table]
        NTels_rec_mst = [x['NTels_reco_mst'] for x in reco_table]
        NTels_rec_sst = [x['NTels_reco_sst'] for x in reco_table]

        reco_energy = np.array([x['reco_Energy'] for x in reco_table])
        mc_energy = np.array([x['MC_Energy'] for x in reco_table])

        fig = plt.figure(figsize=(15, 5))
        plt.suptitle(" ** ".join(
            [args.mode, "protons" if args.proton else "gamma"]))
        plt.subplots_adjust(left=0.05, right=0.97, hspace=0.39, wspace=0.2)

        ax = plt.subplot(131)
        histo = np.histogram2d(NTels_rec,
                               gammaness,
                               bins=(NTel_bins, np.linspace(0, 1, 11)))[0].T
        histo_normed = histo / histo.max(axis=0)
        im = ax.imshow(
            histo_normed,
            interpolation='none',
            origin='lower',
            aspect='auto',
            # extent=(*NTel_bins[[0, -1]], 0, 1),
            cmap=plt.cm.inferno)
        ax.set_xlabel("NTels")
        ax.set_ylabel("drifted gammaness")
        plt.title("Total Number of Telescopes")

        # next subplot

        ax = plt.subplot(132)
        histo = np.histogram2d(NTels_rec_sst,
                               gammaness,
                               bins=(NTel_bins, np.linspace(0, 1, 11)))[0].T
        histo_normed = histo / histo.max(axis=0)
        im = ax.imshow(
            histo_normed,
            interpolation='none',
            origin='lower',
            aspect='auto',
            # extent=(*NTel_bins[[0, -1]], 0, 1),
            cmap=plt.cm.inferno)
        ax.set_xlabel("NTels")
        plt.setp(ax.get_yticklabels(), visible=False)
        plt.title("Number of SSTs")

        # next subplot

        ax = plt.subplot(133)
        histo = np.histogram2d(NTels_rec_mst,
                               gammaness,
                               bins=(NTel_bins, np.linspace(0, 1, 11)))[0].T
        histo_normed = histo / histo.max(axis=0)
        im = ax.imshow(
            histo_normed,
            interpolation='none',
            origin='lower',
            aspect='auto',
            # extent=(*NTel_bins[[0, -1]], 0, 1),
            cmap=plt.cm.inferno)
        cb = fig.colorbar(im, ax=ax)
        ax.set_xlabel("NTels")
        plt.setp(ax.get_yticklabels(), visible=False)
        plt.title("Number of MSTs")

        plt.subplots_adjust(wspace=0.05)

        # plot the energy migration matrix
        plt.figure()
        plt.hist2d(np.log10(reco_energy),
                   np.log10(mc_energy),
                   bins=20,
                   cmap=plt.cm.inferno)
        plt.xlabel("E_MC / TeV")
        plt.ylabel("E_rec / TeV")
        plt.colorbar()

        plt.show()
Exemplo n.º 55
0
    print("q               - Quit")
    return input("Choice: ")


if __name__ == '__main__':

    if len(sys.argv) > 1:
        filename = sys.argv.pop(1)
    else:
        filename = get_example_simtelarray_file()

    plt.style.use("ggplot")
    plt.show(block=False)

    # loop over events and display menu at each event:
    source = hessio_event_source(filename)

    for event in source:

        print("EVENT_ID: ", event.dl0.event_id, "TELS: ",
              event.dl0.tels_with_data, "MC Energy:", event.mc.energy)

        while True:
            response = get_input()
            if response.startswith("d"):
                disps = display_event(event)
                plt.pause(0.1)
            elif response.startswith("p"):
                print("--event-------------------")
                print(event)
                print("--event.dl0---------------")
Exemplo n.º 56
0
def show_photoelectron_image_histogram(simtel_file_path,
                                       output_file_path,
                                       tel_num,
                                       event_id,
                                       channel=0,
                                       quiet=False):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    #
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = hessio_event_source(simtel_file_path, allowed_tels=[tel_num])

    event = None

    for ev in source:
        if int(ev.dl0.event_id) == event_id:
            event = ev
            break

    if event is None:
        print("Error: event '{}' not found for telescope '{}'.".format(
            event_id, tel_num))
        sys.exit(1)

    # GET TIME-VARYING EVENT ##################################################

    #data = event.dl0.tel[tel_num].adc_samples[channel]
    #for ii in range(data.shape[1]):
    #    image_array = data[:, ii]

    # GET INTEGRATED EVENT ####################################################

    # The photoelectron image "event.mc.tel[tel_num].photo_electrons" is a 1D numpy array with the same shape (dtype=int32)
    image_array = event.mc.tel[tel_num].photo_electrons

    # INIT PLOT ###############################################################

    fig = plt.figure(figsize=(8.0, 8.0))
    ax = fig.add_subplot(111)

    values, bins, patches = ax.hist(
        image_array.ravel(),
        histtype=HISTOGRAM_TYPE,
        #bins=image_array.max() - image_array.min(),
        bins=100,
        fc='gray',
        ec='k')

    ax.set_xlim([image_array.min(), image_array.max()])

    # PLOT ####################################################################

    plt.savefig(output_file_path, bbox_inches='tight')

    if not quiet:
        plt.show()
Exemplo n.º 57
0
from ctapipe.visualization import CameraDisplay
from ctapipe.instrument.camera import CameraGeometry
from ctapipe.calib import CameraCalibrator
from ctapipe.reco.HillasReconstructor import HillasReconstructor
from ctapipe.image.hillas import hillas_parameters
from ctapipe.image.cleaning import tailcuts_clean
from ctapipe.utils import linalg

from ctapipe.utils import datasets

#importing data from avaiable datasets in ctapipe
filename = datasets.get_dataset("gamma_test_large.simtel.gz")
#filename

# reading the Monte Carlo file for LST
source = hessio_event_source(filename, allowed_tels={1, 2, 3, 4})

#pointing direction of the telescopes
point_azimuth = {}
point_altitude = {}

reco = HillasReconstructor()
calib = CameraCalibrator(None, None)
off_angles = []

for event in source:

    # The direction the incident particle.
    # Converting Monte Carlo Shower parameter theta and phi to
    # corresponding to 3 components (x,y,z) of a vector
    shower_azimuth = event.mc.az  # same as in Monte Carlo file i.e. phi
Exemplo n.º 58
0
def test_FitGammaHillas():
    '''
    a test of the complete fit procedure on one event including:
    • tailcut cleaning
    • hillas parametrisation
    • GreatCircle creation
    • direction fit
    • position fit

    in the end, proper units in the output are asserted '''

    filename = get_dataset("gamma_test.simtel.gz")

    fit = HillasReconstructor()

    cam_geom = {}
    tel_phi = {}
    tel_theta = {}

    source = hessio_event_source(filename)

    for event in source:

        hillas_dict = {}
        for tel_id in event.dl0.tels_with_data:

            if tel_id not in cam_geom:
                cam_geom[tel_id] = CameraGeometry.guess(
                    event.inst.pixel_pos[tel_id][0],
                    event.inst.pixel_pos[tel_id][1],
                    event.inst.optical_foclen[tel_id])

                tel_phi[tel_id] = event.mc.tel[tel_id].azimuth_raw * u.rad
                tel_theta[tel_id] = (np.pi / 2 -
                                     event.mc.tel[tel_id].altitude_raw) * u.rad

            pmt_signal = event.r0.tel[tel_id].adc_sums[0]

            mask = tailcuts_clean(cam_geom[tel_id],
                                  pmt_signal,
                                  picture_thresh=10.,
                                  boundary_thresh=5.)
            pmt_signal[mask == 0] = 0

            try:
                moments = hillas_parameters(event.inst.pixel_pos[tel_id][0],
                                            event.inst.pixel_pos[tel_id][1],
                                            pmt_signal)
                hillas_dict[tel_id] = moments
            except HillasParameterizationError as e:
                print(e)
                continue

        if len(hillas_dict) < 2: continue

        fit_result = fit.predict(hillas_dict, event.inst, tel_phi, tel_theta)

        print(fit_result)
        fit_result.alt.to(u.deg)
        fit_result.az.to(u.deg)
        fit_result.core_x.to(u.m)
        assert fit_result.is_valid
        return
Exemplo n.º 59
0
"""
Example of showing some information about the instrumental description
"""

from ctapipe.io.hessio import hessio_event_source

if __name__ == '__main__':

    # load up one event so that we get the instrument info
    source = hessio_event_source(
        "/Users/kosack/Data/CTA/Prod3/gamma.simtel.gz")
    event = next(source)
    del source  # close the file

    subarray = event.inst.subarray
    subarray.info()
    print(subarray.to_table())

    print(subarray.tel[1].optics.mirror_area)
Exemplo n.º 60
0
 def setup(self):
     source = hessio_event_source(self.infile)
     data = next(source)  # get one event, so the instrument table is there
     del source
     self.inst = data.inst  # keep a pointer to the instrument stuff
     pass