Ejemplo n.º 1
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_path("gamma_test.simtel.gz")

    fit = FitGammaHillas()

    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] = 0.*u.deg
                tel_theta[tel_id] = 20.*u.deg

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

            mask = tailcuts_clean(cam_geom[tel_id], pmt_signal, 1,
                                  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
Ejemplo n.º 2
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
Ejemplo n.º 3
0
def test_FitGammaHillas():
    '''
    a test on one event of the complete fit procedure including:
    • tailcut cleaning
    • hillas parametrisation
    • GreatCircle creation
    • direction fit
    • position fit

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

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

    fit = FitGammaHillas()

    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] = 180.*u.deg
                tel_theta[tel_id] = 20.*u.deg

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

            mask = tailcuts_clean(cam_geom[tel_id], pmt_signal, 1,
                                  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
Ejemplo n.º 4
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(get_path(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
Ejemplo n.º 5
0
def test_camera_dl1_calibrator():
    event = get_test_event()
    telid = 11
    calibrator = CameraDL1Calibrator(None, None)

    calibrator.get_neighbours(event, telid)
    assert (calibrator.neighbour_dict[telid][0][0] == 5)

    calibrator.get_correction(event, telid)
    assert_almost_equal(calibrator.correction_dict[telid][0], 2.154, 3)

    dl0 = calibrator.obtain_dl0(event, telid)
    assert_almost_equal(dl0[0, 0, 0], -0.091, 3)

    calibrator.calibrate(event)
    image = event.dl1.tel[telid].image
    assert_almost_equal(image[0, 0], -4.431, 3)

    filename = get_path('gamma_test.simtel.gz')
    source = hessio_event_source(filename,
                                 requested_event=409,
                                 use_event_id=True)
    calibrator.calibrate_source(source)
    event = next(source)
    assert_almost_equal(event.dl1.tel[21].image[0, 0], -3.410, 3)
Ejemplo n.º 6
0
def test_showermaxestimator(en=5 * u.TeV,
                            h_first_int=10 * u.km,
                            az=70 * u.deg):
    estim = ShowerMaxEstimator(get_path("atmprof_paranal.dat"))
    h_max = estim.find_shower_max_height(en, h_first_int, az)
    assert h_max.unit.is_equivalent(u.m), "return value has not proper unit"
    return h_max
Ejemplo n.º 7
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
Ejemplo n.º 8
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
        log.debug("[file] Reading file...")
        if max_events:
            log.info("[file] Max events being read = {}".format(max_events))
        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
        log.debug("[file] Reading complete")

        return source
Ejemplo n.º 9
0
def get_array_layout(instrument_name):
    """
    Returns the array layout for the given instrument as an
    `astropy.table.Table` object. 
    """
    name = instrument_name.lower()
    layoutfile = glob(get_path('{}_arraylayout.fits*'.format(name)))[0]
    return load_array_layout_from_file(layoutfile)
Ejemplo n.º 10
0
def test_jsonToFits():
    backup = sys.argv
    full_config_name = get_path('config.json')
    sys.argv = ['test_json_2_fits.py', '--config_file='+full_config_name]
    app = MyApp()
    app.initialize()
    app.start()
    app.jsonToFits()
    sys.argv = backup
Ejemplo n.º 11
0
def test_jsonToFits():
    backup = sys.argv
    full_config_name = get_path('config.json')
    sys.argv = ['test_json_2_fits.py', '--config_file=' + full_config_name]
    app = MyApp()
    app.initialize()
    app.start()
    assert (app.jsonToFits() == True)
    sys.argv = backup
Ejemplo n.º 12
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
Ejemplo n.º 13
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
Ejemplo n.º 14
0
def test_jsonToFits():
    backup = sys.argv
    full_config_name = get_path('config.json')
    sys.argv = ['test_json_2_fits.py', '--config_file=' + full_config_name]
    app = MyApp()
    app.initialize()
    app.start()
    tmp = tempfile.NamedTemporaryFile()
    app.jsonToFits(tmp)
    sys.argv = backup
Ejemplo n.º 15
0
 def event_id_list(self):
     self.log.info("Retrieving list of event ids...")
     if self._event_id_list:
         pass
     else:
         self.log.info("Building new list of event ids...")
         ids = hessio_get_list_event_ids(get_path(self.input_path),
                                         max_events=self.max_events)
         self._event_id_list = ids
     self.log.info("List of event ids retrieved.")
     return self._event_id_list
Ejemplo n.º 16
0
def test_traitlets_config_to_fits():
    backup = sys.argv
    full_config_name = get_path('config.json')
    sys.argv = ['test_json_2_fits.py', '--config_file=' + full_config_name]
    app = MyApp()
    app.initialize()
    app.start()

    tmp = tempfile.NamedTemporaryFile()
    app.traitlets_config_to_fits(tmp.name)
    sys.argv = backup
Ejemplo n.º 17
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
Ejemplo n.º 18
0
def get_camera_geometry(instrument_name, cam_id, recalc_neighbors=True):
    """Helper function to provide the camera geometry definition for a
    camera by name.

    Parameters
    ----------
    instrument_name : {'hess'}
        name of instrument
    cam_id : int
        identifier of camera, in case of multiple versions
    recalc_neighbors : bool
        if True, recalculate the neighbor pixel list, otherwise
        use what is in the file

    Returns
    -------
    a `CameraGeometry` object

    Examples
    --------

    >>> geom_ct1 = get_camera_geometry( "hess", 1 )
    >>> neighbors_pix_1 = geom_ct1.pix_id[geom_ct1.neighbors[1]]
    """

    # let's assume the instrument name is encoded in the
    # filename
    name = instrument_name.lower()
    geomfile = get_path('{}_camgeom.fits.gz'.format(name))

    geom = _load_camera_table_from_file(cam_id, geomfile=geomfile)
    neigh_list = geom['PIX_NEIG'].data
    neigh = np.ma.masked_array(neigh_list, neigh_list < 0),

    # put them all in units of M (conversions are automatic)
    xx = u.Quantity(geom['PIX_POSX'], u.m)
    yy = u.Quantity(geom['PIX_POSY'], u.m)
    dd = u.Quantity(geom['PIX_DIAM'], u.m)
    aa = u.Quantity(geom['PIX_AREA'], u.m ** 2)

    if recalc_neighbors is True:
        neigh = find_neighbor_pixels(xx.value, yy.value,
                                     (dd.mean() + 0.01 * u.m).value)

    return CameraGeometry(
        cam_id=cam_id,
        pix_id=np.array(geom['PIX_ID']),
        pix_x=xx,
        pix_y=yy,
        pix_area=aa,
        neighbors=neigh,
        pix_type='hexagonal'
    )
Ejemplo n.º 19
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
Ejemplo n.º 20
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
Ejemplo n.º 21
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
Ejemplo n.º 22
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.,
                                  boundary_thresh=5.)
            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
Ejemplo n.º 23
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
Ejemplo n.º 24
0
class EventFileReaderFactory(Factory):
    name = "EventFileReaderFactory"
    description = "Obtain EventFileReader based on file type"

    subclasses = Factory.child_subclasses(EventFileReader)
    subclass_names = [c.__name__ for c in subclasses]

    reader = CaselessStrEnum(subclass_names,
                             None,
                             allow_none=True,
                             help='Event file reader to use. If None then '
                             'a reader will be chosen based on file '
                             'extension').tag(config=True)

    # Product classes traits
    # Would be nice to have these automatically set...!
    input_path = Unicode(get_path('gamma_test.simtel.gz'),
                         allow_none=True,
                         help='Path to the input file containing '
                         'events.').tag(config=True)
    max_events = Int(None,
                     allow_none=True,
                     help='Maximum number of events that will be read from'
                     'the file').tag(config=True)

    def get_factory_name(self):
        return self.name

    def get_product_name(self):
        if self.reader is not None:
            return self.reader
        else:
            if self.input_path is None:
                raise ValueError("Please specify an input_path for event file")
            try:
                for subclass in self.subclasses:
                    if subclass.check_file_compatibility(self.input_path):
                        return subclass.__name__
                raise ValueError
            except ValueError:
                self.log.exception("Cannot find compatible EventFileReader "
                                   "for: {}".format(self.input_path))
                raise
Ejemplo n.º 25
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
Ejemplo n.º 26
0
def test_camera_dl1_calibrator():
    event = get_test_event()
    telid = 11
    calibrator = CameraDL1Calibrator(None, None)

    calibrator.get_neighbours(event, telid)
    assert(calibrator.neighbour_dict[telid][0][0] == 5)

    calibrator.get_correction(event, telid)
    assert_almost_equal(calibrator.correction_dict[telid][0], 2.154, 3)

    dl0 = calibrator.obtain_dl0(event, telid)
    assert_almost_equal(dl0[0, 0, 0], -0.091, 3)

    calibrator.calibrate(event)
    image = event.dl1.tel[telid].image
    assert_almost_equal(image[0, 0], -4.431, 3)

    filename = get_path('gamma_test.simtel.gz')
    source = hessio_event_source(filename, requested_event=409,
                                 use_event_id=True)
    calibrator.calibrate_source(source)
    event = next(source)
    assert_almost_equal(event.dl1.tel[21].image[0, 0], -3.410, 3)
Ejemplo n.º 27
0
def argparsing():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-f',
                        '--file',
                        dest='input_paths',
                        default=[get_path('gamma_test.simtel.gz')],
                        nargs='*',
                        help='path to the input files to be combined for a '
                        'single charge resolution.')
    parser.add_argument('-O',
                        '--origin',
                        dest='origin',
                        action='store',
                        choices=InputFile.origin_list(),
                        default='hessio',
                        help='origin of the file')
    parser.add_argument('-t',
                        '--telescope',
                        dest='tel',
                        action='store',
                        type=int,
                        default=None,
                        nargs='*',
                        help='list of telecopes to be included. '
                        'Default = All')
    parser.add_argument('-o',
                        '--output',
                        dest='output_path',
                        action='store',
                        default=None,
                        help='path to store a pdf output of the plots. '
                        'default = display on screen instead')
    parser.add_argument('--comparison',
                        dest='comparison',
                        action='store',
                        default=None,
                        help='output path for a True Charge versus Measured'
                        'Charge graph. Default = do not plot graph')
    parser.add_argument('-M',
                        '--maxpe',
                        dest='maxpe',
                        action='store',
                        default=None,
                        type=float,
                        help='maximum pe to calculate the charge resolution'
                        ' up to. Default = maximum pe in file')
    parser.add_argument('--maxpeplot',
                        dest='maxpeplot',
                        action='store',
                        default=None,
                        type=float,
                        help='maximum pe to plot up to. Default = maxpe')
    parser.add_argument('-B',
                        '--binning',
                        dest='binning',
                        action='store',
                        default="log",
                        choices=['none', 'normal', 'log'],
                        help='binning of the charge resoltion graph: '
                        '"none" = no binning, "normal" = bin, '
                        '"log" = bin in log space.')
    parser.add_argument('--normalx',
                        dest='normalx',
                        action='store_true',
                        default=False,
                        help='Use a normal x axis instead of the defualt log'
                        'axis')
    parser.add_argument('--normaly',
                        dest='normaly',
                        action='store_true',
                        default=False,
                        help='Use a normal y axis instead of the defualt log'
                        'axis')
    parser.add_argument('-E',
                        '--example',
                        dest='example',
                        action='store_true',
                        default=False,
                        help='print an example runcard')
    parser.add_argument('-R',
                        '--runcard',
                        dest='runcard',
                        action='store',
                        default=None,
                        help='path to a runcard text file with arguments that '
                        'override command line arguments. This run card '
                        'can allow complex combinations of files and '
                        'calibrations to compare the charge resolution '
                        'against each other.')
    parser.add_argument('--chargeres-names',
                        dest='chargeres_names',
                        default=['default'],
                        nargs='*',
                        help='chargres calculation to include in plot. '
                        'Only used for runcards.')
    parser.add_argument('--calib-help',
                        dest='calib_help',
                        action='store_true',
                        default=False,
                        help='display the arguments used for the camera '
                        'calibration')

    logger_detail = parser.add_mutually_exclusive_group()
    logger_detail.add_argument('-q',
                               '--quiet',
                               dest='quiet',
                               action='store_true',
                               default=False,
                               help='Quiet mode')
    logger_detail.add_argument('-v',
                               '--verbose',
                               dest='verbose',
                               action='store_true',
                               default=False,
                               help='Verbose mode')
    logger_detail.add_argument('-d',
                               '--debug',
                               dest='debug',
                               action='store_true',
                               default=False,
                               help='Debug mode')

    args, excess_args = parser.parse_known_args()

    if args.quiet:
        log.setLevel(40)
    if args.verbose:
        log.setLevel(20)
    if args.debug:
        log.setLevel(10)

    if args.calib_help:
        params, unknown_args = calibration_parameters(excess_args, args.origin,
                                                      args.calib_help)

    if args.example:
        print("""
# Each charge resolution block starts with [chargeres] and the names for
# this charge resolution.
# The options in each block are equivalent to the scripts help message.
# Options that seem to apply to plotting will only have effect in a
# plotting block.

[chargeres] test_file_local
#-f gamma_test.simtel.gz
-f ~/Software/outputs/sim_telarray/simtel_run4_gcts_hnsb.gz
-O hessio
--integrator 4
--integration-window 7 3
--integration-sigamp 2 4
--integration-lwt 0
--integration-calib_scale 1.05
--comparison ~/Downloads/test_file_local.pdf


# A second charge resolution block to also calculate the resolution with
# a different integrator so the two resolutions can be plotted against
# each other.

[chargeres] test_file_nei
#-f gamma_test.simtel.gz
-f ~/Software/outputs/sim_telarray/simtel_run4_gcts_hnsb.gz
-O hessio
--integrator 5
--integration-window 7 3
--integration-sigamp 2 4
--integration-lwt 0
--integration-calib_scale 1.05
--comparison ~/Downloads/test_file_nei.pdf

# A plotting block configures an output plot

[plot] normal_plot
--chargeres-names test_file_local test_file_nei
-o ~/Downloads/normal_plot.pdf
--binning normal
--normalx
--normaly

[plot] log_plot
--chargeres-names test_file_local test_file_nei
-o ~/Downloads/log_plot.pdf""")
        exit()

    chargeres_cmdlines = {}
    plot_cmdlines = {}

    if args.runcard is None:
        name = args.chargeres_names[0]
        chargeres_cmdlines[name] = sys.argv[1:]
        plot_cmdlines[name] = sys.argv[1:]
        chargeres_args = {name: args}
        plot_args = {name: args}
    else:
        chargeres_args = {}
        plot_args = {}
        current = None
        runcard = open(args.runcard)
        for line in runcard:
            if line.strip() and not line.startswith('#'):
                argument = line.split()[0]
                if argument == '[chargeres]':
                    name = line.split()[1:][0]
                    chargeres_cmdlines[name] = []
                    current = chargeres_cmdlines[name]
                    continue
                elif argument == '[plot]':
                    name = line.split()[1:][0]
                    plot_cmdlines[name] = []
                    current = plot_cmdlines[name]
                    continue
                current.extend(line.split())

        # Temp fill for checks
        for name, cmdline in chargeres_cmdlines.items():
            chargeres_args[name], unknown = parser.parse_known_args(cmdline)
        for name, cmdline in plot_cmdlines.items():
            plot_args[name], unknown = parser.parse_known_args(cmdline)

    # Check all chargeres_names exist
    for plot_name, args in plot_args.items():
        for name in args.chargeres_names:
            try:
                if name not in chargeres_args:
                    raise IndexError
            except IndexError:
                log.exception("[chargeres_names] For plot: {}, no chargeres "
                              "has the name: {}".format(plot_name, name))
                raise

    return parser, chargeres_cmdlines, plot_cmdlines
@author: zornju

Example of using the instrument module and reading data from a hessio, a
fits, and a sim_telarray-config file.
"""

from ctapipe.instrument import InstrumentDescription as ID,util_functions as uf
from ctapipe.instrument.telescope.camera import CameraDescription as CD
from ctapipe.utils.datasets import get_path
from astropy import units as u
import matplotlib.pyplot as plt

if __name__ == '__main__':
    
    filename1 = get_path('PROD2_telconfig.fits.gz')
    filename2 = get_path('gamma_test.simtel.gz')
    filename3 ='/afs/ifh.de/group/cta/MC_Production/d20150828_GM/mrg/sim_telarray/cfg/CTA/CTA-ULTRA6-SCT.cfg'
    
    #open both files:
    item1 = uf.load(filename1)
    item2 = uf.load(filename2)
    item3 = uf.load(filename3)
    
    #initialize the whole telescope, i.e. read all data from the files which
    #concerns the whole telescope (including camera and optics)
    instr1 = ID.Telescope.initialize(filename1,item1)
    instr2 = ID.Telescope.initialize(filename2,item2)
    instr3 = ID.Telescope.initialize(filename3,item3)

    #The ID.telescope.initializ-function returns 3 objects as a list. The
Ejemplo n.º 29
0
def get_test_event():
    filename = get_path(
        'gamma_20deg_0deg_run31964___cta-prod2_desert-1640m-Aar.simtel.gz')
    for event in hessio_event_source(filename):
        if event.dl0.event_id == 409:
            return event
Ejemplo n.º 30
0
def get_test_event():
    filename = get_path(
        'gamma_20deg_0deg_run31964___cta-prod2_desert-1640m-Aar.simtel.gz')
    for event in hessio_event_source(filename):
        if event.dl0.event_id == 409:
            return event
Ejemplo n.º 31
0
Created on Thu Nov 19 11:43:50 2015

@author: zornju

Example of using the instrument module and reading data from a hessio, a
fits, and a sim_telarray-config file.
"""

from ctapipe.instrument import InstrumentDescription as ID
from ctapipe.utils.datasets import get_path
import matplotlib.pyplot as plt
import os

if __name__ == '__main__':
    
    filename1 = get_path('PROD2_telconfig.fits.gz')
    filename2 = get_path('gamma_test.simtel.gz')
    filename3 = get_path('CTA-ULTRA6-SCT.cfg')
    
    tel1,cam1,opt1 = ID.load(filename1)
    tel2,cam2,opt2 = ID.load(filename2)
    tel3,cam3,opt3 = ID.load(filename3)
    tel4,cam4,opt4 = ID.load()
    
    #To check which tables are in the dictionaries, do:
    print('Print the name of the tables present in the dictionaries taken',\
    'from the fits file:')
    print(tel1.keys(),cam1.keys(),opt1.keys())
    print('As you can see, in the fits file, only the first dictionary is',\
    'filled with tables.')
    print('------------------------------------------------------------------')
            disp[-1].add_colorbar(label=" [{} ADC cts]".format(gain_label[i]))

            
        plt.pause(0.1)
        pp.savefig(fig)

    pp.close()

if __name__ == '__main__':

    # Declare and parse command line option
    parser = argparse.ArgumentParser(
        description='Tel_id, pixel id and number of event to compute.')
    parser.add_argument('--f', dest='filename',
                        required=False,
                        default=get_path('gamma_test.simtel.gz'),
                        help='filename <MC file name>')
    parser.add_argument('--d', dest='display', action='store_true',
                        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)
Created on Thu Nov 19 11:43:50 2015

@author: zornju

Example of using the instrument module and reading data from a hessio, a
fits, and a sim_telarray-config file.
"""

from ctapipe.instrument import InstrumentDescription as ID, CameraDescription as CD
from ctapipe.utils.datasets import get_path
from astropy import units as u
import matplotlib.pyplot as plt

if __name__ == '__main__':

    filename1 = get_path('PROD2_telconfig.fits.gz')
    filename2 = get_path('gamma_test.simtel.gz')
    #filename3 = get_path('CTA-ULTRA6-SCT.cfg')
    filename3 = '/afs/ifh.de/group/cta/MC_Production/d20150828_GM/mrg/sim_telarray/cfg/CTA/CTA-ULTRA6-SCT.cfg'

    instr1 = ID.Telescope.from_file(filename1)
    instr2 = ID.Telescope.from_file(filename2)
    instr3 = ID.Telescope.from_file(filename3)

    #The ID.telescope.initializ-function returns 3 objects as a list. The
    #first entry of the list is the object containing the telescope
    #configuration whithout the camera and optics, thus:
    tel1 = instr1[0]
    tel2 = instr2[0]
    tel3 = instr3[0]
Ejemplo n.º 34
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):
Ejemplo n.º 35
0
def test_showermaxestimator(en=5*u.TeV, h_first_int=10*u.km, az=70*u.deg):
    estim = ShowerMaxEstimator(get_path("atmprof_paranal.dat"))
    h_max = estim.find_shower_max_height(en, h_first_int, az)
    assert h_max.unit.is_equivalent(u.m), "return value has not proper unit"
    return h_max
Ejemplo n.º 36
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
Ejemplo n.º 37
0
def main():
    script = os.path.splitext(os.path.basename(__file__))[0]
    log.info("[SCRIPT] {}".format(script))

    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-f', '--file', dest='input_path', action='store',
                        default=get_path('gamma_test.simtel.gz'),
                        help='path to the input file')
    parser.add_argument('-O', '--origin', dest='origin', action='store',
                        choices=InputFile.origin_list(),
                        default='hessio', help='origin of the file')
    parser.add_argument('-o', '--output', dest='output_dir', action='store',
                        default=None,
                        help='path of the output directory to store the '
                             'images (default = input file directory)')
    parser.add_argument('-e', '--event', dest='event_req', action='store',
                        default=0, type=int,
                        help='event index to plot (not id!)')
    parser.add_argument('--id', dest='event_id_f', action='store_true',
                        default=False, help='-e will specify event_id instead '
                                            'of index')
    parser.add_argument('-t', '--telescope', dest='tel', action='store',
                        type=int, default=None, help='telecope to view')
    parser.add_argument('-c', '--channel', dest='chan', action='store',
                        type=int, default=0,
                        help='channel to view (default = 0 (HG))')
    parser.add_argument('--calib-help', dest='calib_help', action='store_true',
                        default=False,
                        help='display the arguments used for the camera '
                             'calibration')

    logger_detail = parser.add_mutually_exclusive_group()
    logger_detail.add_argument('-q', '--quiet', dest='quiet',
                               action='store_true', default=False,
                               help='Quiet mode')
    logger_detail.add_argument('-v', '--verbose', dest='verbose',
                               action='store_true', default=False,
                               help='Verbose mode')
    logger_detail.add_argument('-d', '--debug', dest='debug',
                               action='store_true', default=False,
                               help='Debug mode')

    args, excess_args = parser.parse_known_args()

    if args.quiet:
        log.setLevel(40)
    if args.verbose:
        log.setLevel(20)
    if args.debug:
        log.setLevel(10)

    telid = args.tel
    chan = args.chan

    log.debug("[file] Reading file")
    input_file = InputFile(args.input_path, args.origin)
    event = input_file.get_event(args.event_req, args.event_id_f)

    # Print event/args values
    log.info("[event_index] {}".format(event.count))
    log.info("[event_id] {}".format(event.dl0.event_id))
    log.info("[telescope] {}".format(telid))
    log.info("[channel] {}".format(chan))

    params, unknown_args = calibration_parameters(excess_args,
                                                  args.origin,
                                                  args.calib_help)

    if unknown_args:
        parser.print_help()
        calibration_parameters(unknown_args, args.origin, True)
        msg = 'unrecognized arguments: %s'
        parser.error(msg % ' '.join(unknown_args))

    # Create a dictionary to store any geoms in
    geom = CameraGeometry.guess(*event.meta.pixel_pos[telid],
                                event.meta.optical_foclen[telid])
    geom_dict = {telid: geom}
    calibrated_event = calibrate_event(event, params, geom_dict)

    # Select telescope
    tels = list(calibrated_event.dl0.tels_with_data)
    if telid is None or telid not in tels:
        log.error("[event] please specify one of the following telescopes "
                  "for this event: {}".format(tels))
        exit()

    # Extract required images
    data_ped = calibrated_event.dl1.tel[telid].pedestal_subtracted_adc[chan]
    true_pe = calibrated_event.mc.tel[telid].photo_electrons
    measured_pe = calibrated_event.dl1.tel[telid].pe_charge
    max_time = np.unravel_index(np.argmax(data_ped), data_ped.shape)[1]
    max_charges = np.max(data_ped, axis=1)
    max_pixel = int(np.argmax(max_charges))
    min_pixel = int(np.argmin(max_charges))

    # Get Neighbours
    max_pixel_nei = geom.neighbors[max_pixel]
    min_pixel_nei = geom.neighbors[min_pixel]

    # Get Windows
    windows = calibrated_event.dl1.tel[telid].integration_window[chan]
    length = np.sum(windows, axis=1)
    start = np.argmax(windows, axis=1)
    end = start + length - 1

    # Draw figures
    ax_max_nei = {}
    ax_min_nei = {}
    fig_waveforms = plt.figure(figsize=(18, 9))
    fig_waveforms.subplots_adjust(hspace=.5)
    fig_camera = plt.figure(figsize=(15, 12))

    ax_max_pix = fig_waveforms.add_subplot(4, 2, 1)
    ax_min_pix = fig_waveforms.add_subplot(4, 2, 2)
    ax_max_nei[0] = fig_waveforms.add_subplot(4, 2, 3)
    ax_min_nei[0] = fig_waveforms.add_subplot(4, 2, 4)
    ax_max_nei[1] = fig_waveforms.add_subplot(4, 2, 5)
    ax_min_nei[1] = fig_waveforms.add_subplot(4, 2, 6)
    ax_max_nei[2] = fig_waveforms.add_subplot(4, 2, 7)
    ax_min_nei[2] = fig_waveforms.add_subplot(4, 2, 8)

    ax_img_nei = fig_camera.add_subplot(2, 2, 1)
    ax_img_max = fig_camera.add_subplot(2, 2, 2)
    ax_img_true = fig_camera.add_subplot(2, 2, 3)
    ax_img_cal = fig_camera.add_subplot(2, 2, 4)

    plotter = CameraPlotter(event, geom_dict)

    # Draw max pixel traces
    plotter.draw_waveform(data_ped[max_pixel], ax_max_pix)
    ax_max_pix.set_title("(Max) Pixel: {}, "
                         "True: {}, "
                         "Measured = {:.3f}".format(max_pixel,
                                                    true_pe[max_pixel],
                                                    measured_pe[max_pixel]))
    ax_max_pix.set_ylabel("Amplitude-Ped (ADC)")
    max_ylim = ax_max_pix.get_ylim()
    plotter.draw_waveform_positionline(start[max_pixel], ax_max_pix)
    plotter.draw_waveform_positionline(end[max_pixel], ax_max_pix)
    for i, ax in ax_max_nei.items():
        if len(max_pixel_nei) > i:
            pix = max_pixel_nei[i]
            plotter.draw_waveform(data_ped[pix], ax)
            ax.set_title("(Max Nei) Pixel: {}, "
                         "True: {}, "
                         "Measured = {:.3f}".format(pix, true_pe[pix],
                                                    measured_pe[pix]))
            ax.set_ylabel("Amplitude-Ped (ADC)")
            ax.set_ylim(max_ylim)
            plotter.draw_waveform_positionline(start[pix], ax)
            plotter.draw_waveform_positionline(end[pix], ax)

    # Draw min pixel traces
    plotter.draw_waveform(data_ped[min_pixel], ax_min_pix)
    ax_min_pix.set_title("(Min) Pixel: {}, "
                         "True: {}, "
                         "Measured = {:.3f}".format(min_pixel,
                                                    true_pe[min_pixel],
                                                    measured_pe[min_pixel]))
    ax_min_pix.set_ylabel("Amplitude-Ped (ADC)")
    ax_min_pix.set_ylim(max_ylim)
    plotter.draw_waveform_positionline(start[min_pixel], ax_min_pix)
    plotter.draw_waveform_positionline(end[min_pixel], ax_min_pix)
    for i, ax in ax_min_nei.items():
        if len(min_pixel_nei) > i:
            pix = min_pixel_nei[i]
            plotter.draw_waveform(data_ped[pix], ax)
            ax.set_title("(Min Nei) Pixel: {}, "
                         "True: {}, "
                         "Measured = {:.3f}".format(pix, true_pe[pix],
                                                    measured_pe[pix]))
            ax.set_ylabel("Amplitude-Ped (ADC)")
            ax.set_ylim(max_ylim)
            plotter.draw_waveform_positionline(start[pix], ax)
            plotter.draw_waveform_positionline(end[pix], ax)

    # Draw cameras
    nei_camera = np.zeros_like(max_charges, dtype=np.int)
    nei_camera[min_pixel_nei] = 2
    nei_camera[min_pixel] = 1
    nei_camera[max_pixel_nei] = 3
    nei_camera[max_pixel] = 4
    camera = plotter.draw_camera(telid, nei_camera, ax_img_nei)
    camera.cmap = plt.cm.viridis
    ax_img_nei.set_title("Neighbour Map")
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_nei)

    camera = plotter.draw_camera(telid, data_ped[:, max_time], ax_img_max)
    camera.add_colorbar(ax=ax_img_max, label="Amplitude-Ped (ADC)")
    ax_img_max.set_title("Max Timeslice (T = {})".format(max_time))
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_max)

    camera = plotter.draw_camera(telid, true_pe, ax_img_true)
    camera.add_colorbar(ax=ax_img_true, label="True Charge (Photo-electrons)")
    ax_img_true.set_title("True Charge")
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_true)

    int_dict, inverted = integrator_dict()
    integrator_name = '' if 'integrator' not in params else \
        params['integrator']

    camera = plotter.draw_camera(telid, measured_pe, ax_img_cal)
    camera.add_colorbar(ax=ax_img_cal, label="Calib Charge (Photo-electrons)")
    ax_img_cal.set_title("Charge (integrator={})".format(integrator_name))
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_cal)

    fig_waveforms.suptitle("Integrator = {}".format(integrator_name))
    fig_camera.suptitle("Camera = {}".format(geom.cam_id))

    waveform_output_name = "{}_e{}_t{}_c{}_integrator{}_waveform.pdf"\
        .format(input_file.filename, event.count, telid, chan,
                inverted[params['integrator']])
    camera_output_name = "{}_e{}_t{}_c{}_integrator{}_camera.pdf"\
        .format(input_file.filename, event.count, telid, chan,
                inverted[params['integrator']])
    output_dir = args.output_dir if args.output_dir is not None else \
        input_file.output_directory
    output_dir = os.path.join(output_dir, script)
    if not os.path.exists(output_dir):
        log.info("[output] Creating directory: {}".format(output_dir))
        os.makedirs(output_dir)

    waveform_output_path = os.path.join(output_dir, waveform_output_name)
    log.info("[output] {}".format(waveform_output_path))
    fig_waveforms.savefig(waveform_output_path, format='pdf',
                          bbox_inches='tight')

    camera_output_path = os.path.join(output_dir, camera_output_name)
    log.info("[output] {}".format(camera_output_path))
    fig_camera.savefig(camera_output_path, format='pdf', bbox_inches='tight')

    log.info("[COMPLETE]")
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 18 17:37:19 2015

@author: zornju
"""

import ctapipe.instrument.InstrumentDescription as ID
from ctapipe.utils.datasets import get_path

filename1 = get_path('PROD2_telconfig.fits.gz')
filename2 = get_path('gamma_test.simtel.gz')
filename3 = get_path('CTA-ULTRA6-SCT.cfg')

def test_read_telescope_data():
    instr1 = ID.Telescope.from_file(filename1)
    instr2 = ID.Telescope.from_file(filename2)
    instr3 = ID.Telescope.from_file(filename3)
    instr4 = ID.Telescope.from_file()
    tel1 = instr1[0]
    tel2 = instr2[0]
    tel3 = instr3[0]
    tel4 = instr4[0]
    assert(len(tel1.tel_id)>0)
    assert(len(tel2.tel_id)>0)
    assert(len(tel3.tel_id)>0)
    assert(len(tel4.tel_id)>0)
    opt1 = instr1[1]
    opt2 = instr2[1]
    opt3 = instr3[1]
    opt4 = instr4[1]
Ejemplo n.º 39
0
from ctapipe.io.hessio import hessio_event_source
from ctapipe.utils.datasets import get_path

# input_filepath = "/Users/Jason/Software/outputs/sim_telarray/simtel_run4_gcts_hnsb.gz"
# input_filepath = "/Users/Jason/Software/outputs/sim_telarray/gamma_20deg_0deg_run10251___cta-prod3-merged_desert-2150m-Paranal-subarray-3_cone10.simtel.gz"
# input_filepath = "/Users/Jason/Software/outputs/sim_telarray/simtel_runmeudon_gamma_30tel_30deg_1.gz"
input_filepath = "/Users/Jason/Software/outputs/sim_telarray/simtel_runmeudon_proton_30tel_30deg_1.gz"

source = hessio_event_source(get_path(input_filepath), max_events=100)

event = next(source)
tels = list(event.dl0.tels_with_data)

Qt = event.mc.tel[tels[0]].photo_electrons
if Qt.any() > 0:
    print("File has true pe")
else:
    print("No true pe data")
Ejemplo n.º 40
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
Ejemplo n.º 41
0
from ctapipe.io.hessio import hessio_event_source
from ctapipe.utils.datasets import get_path

# input_filepath = "/Users/Jason/Software/outputs/sim_telarray/simtel_run4_gcts_hnsb.gz"
# input_filepath = "/Users/Jason/Software/outputs/sim_telarray/gamma_20deg_0deg_run10251___cta-prod3-merged_desert-2150m-Paranal-subarray-3_cone10.simtel.gz"
# input_filepath = "/Users/Jason/Software/outputs/sim_telarray/simtel_runmeudon_gamma_30tel_30deg_1.gz"
input_filepath = "/Users/Jason/Software/outputs/sim_telarray/simtel_runmeudon_proton_30tel_30deg_1.gz"


source = hessio_event_source(get_path(input_filepath), max_events=100)

event = next(source)
tels = list(event.dl0.tels_with_data)

Qt = event.mc.tel[tels[0]].photo_electrons
if Qt.any() > 0:
    print("File has true pe")
else:
    print("No true pe data")
Ejemplo n.º 42
0
class EventFileReader(Component):
    """
    Parent class for specific FileReaders

    Attributes
    ----------
    input_path : str
    directory : str
        Automatically set from `input_path`.
    filename : str
        Name of the file without the extension.
        Automatically set from `input_path`.
    extension : str
        Automatically set from `input_path`.
    output_directory : str
        Directory to save outputs for this file

    """
    name = 'EventFileReader'
    origin = None

    input_path = Unicode(get_path('gamma_test.simtel.gz'),
                         allow_none=True,
                         help='Path to the input file containing '
                         'events.').tag(config=True)
    max_events = Int(None,
                     allow_none=True,
                     help='Maximum number of events that will be read from'
                     'the file').tag(config=True)

    def __init__(self, config, tool, **kwargs):
        """
        Class to handle generic input files. Enables obtaining the "source"
        generator, regardless of the type of file (either hessio or camera
        file).

        Parameters
        ----------
        config : traitlets.loader.Config
            Configuration specified by config file or cmdline arguments.
            Used to set traitlet values.
            Set to None if no configuration to pass.
        tool : ctapipe.core.Tool
            Tool executable that is calling this component.
            Passes the correct logger to the component.
            Set to None if no Tool to pass.
        kwargs
        """
        super().__init__(config=config, parent=tool, **kwargs)

        if self.origin is None:
            raise ValueError("Subclass of EventFileReader should specify "
                             "an origin")

        self._num_events = None
        self._event_id_list = []

        if self.input_path is None:
            raise ValueError("Please specify an input_path for event file")
        self._init_path(self.input_path)

    def _init_path(self, input_path):
        if not exists(input_path):
            raise FileNotFoundError(
                "file path does not exist: '{}'".format(input_path))

        self.input_path = input_path
        self.directory = dirname(input_path)
        self.filename = splitext(basename(input_path))[0]
        self.extension = splitext(input_path)[1]
        self.output_directory = join(self.directory, self.filename)

        if self.log:
            self.log.info("INPUT PATH = {}".format(self.input_path))

    @observe('input_path')
    def on_input_path_changed(self, change):
        new = change['new']
        try:
            self.log.warning("Change: input_path={}".format(new))
            self._num_events = None
            self._event_id_list = []
            self._init_path(new)
        except AttributeError:
            pass

    @observe('origin')
    def on_origin_changed(self, change):
        new = change['new']
        try:
            self.log.warning("Change: origin={}".format(new))
        except AttributeError:
            pass

    @observe('max_events')
    def on_max_events_changed(self, change):
        new = change['new']
        try:
            self.log.warning("Change: max_events={}".format(new))
            self._num_events = None
            self._event_id_list = []
        except AttributeError:
            pass

    @property
    @abstractmethod
    def origin(self):
        """
        Abstract property to be defined in child class.

        Get the name for the origin of the file. E.g. 'hessio'.

        Returns
        -------
        origin : str
        """

    @staticmethod
    @abstractmethod
    def check_file_compatibility(file_path):
        """
        Abstract method to be defined in child class.

        Perform a set of checks to see if the input file is compatible
        with this file reader.

        Parameters
        ----------
        file_path : str
            File path to the event file.

        Returns
        -------
        compatible : bool
            True if file is compatible, False if it is incompatible
        """

    @property
    @abstractmethod
    def num_events(self):
        """
        Abstract property to be defined in child class.

        Obtain the number of events from the file, store it inside
        self._num_events, and return the value.

        Returns
        -------
        self._num_events : int
        """

    @property
    @abstractmethod
    def event_id_list(self):
        """
        Abstract property to be defined in child class.

        Obtain the number of events from the file, store it as
        self._event_id_list, and return the value.

        Returns
        -------
        self._event_id_list : list[self._num_events]
        """

    @abstractmethod
    def read(self,
             allowed_tels=None,
             requested_event=None,
             use_event_id=False):
        """
        Abstract method to be defined in child class.

        Read the file using the processes required by the readers file-type.

        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
        """

    def get_event(self, requested_event, use_event_id=False):
        """
        Loop through events until the requested event is found

        Parameters
        ----------
        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
        -------
        event : `ctapipe` event-container

        """
        source = self.read(requested_event=requested_event,
                           use_event_id=use_event_id)
        event = next(source)
        return deepcopy(event)

    def find_max_true_npe(self, telescopes=None):
        """
        Loop through events to find the maximum true npe

        Parameters
        ----------
        telescopes : list
            List of telecopes to include. If None, then all telescopes
            are included.

        Returns
        -------
        max_pe : int

        """
        # TODO: Find an alternate method so this can be removed
        self.log.info("Finding maximum true npe inside file...")
        source = self.read()
        max_pe = 0
        for event in source:
            tels = list(event.dl0.tels_with_data)
            if telescopes is not None:
                tels = []
                for tel in telescopes:
                    if tel in event.dl0.tels_with_data:
                        tels.append(tel)
            if event.count == 0:
                # Check events have true charge included
                try:
                    if np.all(event.mc.tel[tels[0]].photo_electron_image == 0):
                        raise KeyError
                except KeyError:
                    self.log.exception('[chargeres] Source does not contain '
                                       'true charge')
                    raise
            for telid in tels:
                pe = event.mc.tel[telid].photo_electron_image
                this_max = np.max(pe)
                if this_max > max_pe:
                    max_pe = this_max
        self.log.info("Maximum true npe inside file = {}".format(max_pe))

        return max_pe
Ejemplo n.º 43
0
def get_input():
    print("============================================")
    print("n or [enter]    - go to Next event")
    print("d               - Display the event")
    print("p               - Print all event data")
    print("i               - event Info")
    print("q               - Quit")
    return input("Choice: ")

if __name__ == '__main__':

    if len(sys.argv) > 1:
        filename = sys.argv.pop(1)
    else:
        filename = get_path("gamma_test.simtel.gz")

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

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

    for event in source:

        print("EVENT_ID: ", event.dl0.event_id, "TELS: ",
              event.dl0.tels_with_data)

        while True:
            response = get_input()
            if response.startswith("d"):
Ejemplo n.º 44
0
from ctapipe.visualization import ArrayDisplay
from ctapipe.utils import datasets

from astropy.table import Table
from numpy import ones_like
import matplotlib.pylab as plt

if __name__ == '__main__':

    plt.style.use("ggplot")
    plt.figure(figsize=(9.5, 8.5))

    # load up an example table that has the telescope positions and
    # mirror areas in it:
    arrayfile = datasets.get_path("PROD2_telconfig.fits.gz")
    tels = Table.read(arrayfile, hdu="TELESCOPE_LEVEL0")

    X = tels['TelX']
    Y = tels['TelY']
    A = tels['MirrorArea'] * 2  # exaggerate scale a bit

    # display the array, and set the color value to 50
    ad = ArrayDisplay(X, Y, A, title="Prod 2 Full Array")
    ad.values = ones_like(X) * 50

    # label them
    for tel in tels:
        name = "CT{tid}".format(tid=tel['TelID'])
        plt.text(tel['TelX'], tel['TelY'], name, fontsize=8)

    ad.axes.set_xlim(-1000, 1000)
Ejemplo n.º 45
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
Ejemplo n.º 46
0
import numpy as np
from astropy.io import fits
from tqdm import tqdm
from ctapipe.utils.datasets import get_path
from ctapipe.io.hessio import hessio_event_source
import os.path
import math
import sys

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

events_per_byte = 16673/2150294565

est_events = os.path.getsize(filepath) * events_per_byte
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]
for pix in range(n_pix):
    pos_x = pos_arr[0,pix]
    pos_y = pos_arr[1,pix]
    print("[{0:.5g}, {1:.5g}],  # {2}".format(pos_x,-pos_y,pix))
Ejemplo n.º 47
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
Ejemplo n.º 48
0
def argparsing():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-f', '--file', dest='input_paths',
                        default=[get_path('gamma_test.simtel.gz')], nargs='*',
                        help='path to the input files to be combined for a '
                             'single charge resolution.')
    parser.add_argument('-O', '--origin', dest='origin', action='store',
                        choices=InputFile.origin_list(),
                        default='hessio', help='origin of the file')
    parser.add_argument('-t', '--telescope', dest='tel', action='store',
                        type=int, default=None, nargs='*',
                        help='list of telecopes to be included. '
                             'Default = All')
    parser.add_argument('-o', '--output', dest='output_path', action='store',
                        default=None,
                        help='path to store a pdf output of the plots. '
                             'default = display on screen instead')
    parser.add_argument('--comparison', dest='comparison', action='store',
                        default=None,
                        help='output path for a True Charge versus Measured'
                             'Charge graph. Default = do not plot graph')
    parser.add_argument('-M', '--maxpe', dest='maxpe', action='store',
                        default=None, type=float,
                        help='maximum pe to calculate the charge resolution'
                             ' up to. Default = maximum pe in file')
    parser.add_argument('--maxpeplot', dest='maxpeplot', action='store',
                        default=None, type=float,
                        help='maximum pe to plot up to. Default = maxpe')
    parser.add_argument('-B', '--binning', dest='binning', action='store',
                        default="log", choices=['none', 'normal', 'log'],
                        help='binning of the charge resoltion graph: '
                             '"none" = no binning, "normal" = bin, '
                             '"log" = bin in log space.')
    parser.add_argument('--normalx', dest='normalx', action='store_true',
                        default=False,
                        help='Use a normal x axis instead of the defualt log'
                             'axis')
    parser.add_argument('--normaly', dest='normaly', action='store_true',
                        default=False,
                        help='Use a normal y axis instead of the defualt log'
                             'axis')
    parser.add_argument('-E', '--example', dest='example', action='store_true',
                        default=False,
                        help='print an example runcard')
    parser.add_argument('-R', '--runcard', dest='runcard', action='store',
                        default=None,
                        help='path to a runcard text file with arguments that '
                             'override command line arguments. This run card '
                             'can allow complex combinations of files and '
                             'calibrations to compare the charge resolution '
                             'against each other.')
    parser.add_argument('--chargeres-names', dest='chargeres_names',
                        default=['default'], nargs='*',
                        help='chargres calculation to include in plot. '
                             'Only used for runcards.')
    parser.add_argument('--calib-help', dest='calib_help', action='store_true',
                        default=False,
                        help='display the arguments used for the camera '
                             'calibration')

    logger_detail = parser.add_mutually_exclusive_group()
    logger_detail.add_argument('-q', '--quiet', dest='quiet',
                               action='store_true', default=False,
                               help='Quiet mode')
    logger_detail.add_argument('-v', '--verbose', dest='verbose',
                               action='store_true', default=False,
                               help='Verbose mode')
    logger_detail.add_argument('-d', '--debug', dest='debug',
                               action='store_true', default=False,
                               help='Debug mode')

    args, excess_args = parser.parse_known_args()

    if args.quiet:
        log.setLevel(40)
    if args.verbose:
        log.setLevel(20)
    if args.debug:
        log.setLevel(10)

    if args.calib_help:
        params, unknown_args = calibration_parameters(excess_args,
                                                      args.origin,
                                                      args.calib_help)

    if args.example:
        print("""
# Each charge resolution block starts with [chargeres] and the names for
# this charge resolution.
# The options in each block are equivalent to the scripts help message.
# Options that seem to apply to plotting will only have effect in a
# plotting block.

[chargeres] test_file_local
#-f gamma_test.simtel.gz
-f ~/Software/outputs/sim_telarray/simtel_run4_gcts_hnsb.gz
-O hessio
--integrator 4
--integration-window 7 3
--integration-sigamp 2 4
--integration-lwt 0
--integration-calib_scale 1.05
--comparison ~/Downloads/test_file_local.pdf


# A second charge resolution block to also calculate the resolution with
# a different integrator so the two resolutions can be plotted against
# each other.

[chargeres] test_file_nei
#-f gamma_test.simtel.gz
-f ~/Software/outputs/sim_telarray/simtel_run4_gcts_hnsb.gz
-O hessio
--integrator 5
--integration-window 7 3
--integration-sigamp 2 4
--integration-lwt 0
--integration-calib_scale 1.05
--comparison ~/Downloads/test_file_nei.pdf

# A plotting block configures an output plot

[plot] normal_plot
--chargeres-names test_file_local test_file_nei
-o ~/Downloads/normal_plot.pdf
--binning normal
--normalx
--normaly

[plot] log_plot
--chargeres-names test_file_local test_file_nei
-o ~/Downloads/log_plot.pdf""")
        exit()

    chargeres_cmdlines = {}
    plot_cmdlines = {}

    if args.runcard is None:
        name = args.chargeres_names[0]
        chargeres_cmdlines[name] = sys.argv[1:]
        plot_cmdlines[name] = sys.argv[1:]
        chargeres_args = {name: args}
        plot_args = {name: args}
    else:
        chargeres_args = {}
        plot_args = {}
        current = None
        runcard = open(args.runcard)
        for line in runcard:
            if line.strip() and not line.startswith('#'):
                argument = line.split()[0]
                if argument == '[chargeres]':
                    name = line.split()[1:][0]
                    chargeres_cmdlines[name] = []
                    current = chargeres_cmdlines[name]
                    continue
                elif argument == '[plot]':
                    name = line.split()[1:][0]
                    plot_cmdlines[name] = []
                    current = plot_cmdlines[name]
                    continue
                current.extend(line.split())

        # Temp fill for checks
        for name, cmdline in chargeres_cmdlines.items():
            chargeres_args[name], unknown = parser.parse_known_args(cmdline)
        for name, cmdline in plot_cmdlines.items():
            plot_args[name], unknown = parser.parse_known_args(cmdline)

    # Check all chargeres_names exist
    for plot_name, args in plot_args.items():
        for name in args.chargeres_names:
            try:
                if name not in chargeres_args:
                    raise IndexError
            except IndexError:
                log.exception("[chargeres_names] For plot: {}, no chargeres "
                              "has the name: {}".format(plot_name, name))
                raise 

    return parser, chargeres_cmdlines, plot_cmdlines
Ejemplo n.º 49
0
import numpy as np
from astropy.table import Table
from ctapipe.utils import datasets
from ctapipe.visualization import ArrayDisplay
from matplotlib import pyplot as plt


if __name__ == '__main__':

    plt.style.use("ggplot")
    plt.figure(figsize=(10, 8))

    arrayfile = datasets.get_path("PROD2_telconfig.fits.gz")
    tels = Table.read(arrayfile, hdu="TELESCOPE_LEVEL0")

    adisp = ArrayDisplay(tels['TelX'], tels['TelY'], tels['MirrorArea'] * 2,
                         title='PROD2 telescopes', autoupdate=True)
    plt.tight_layout()

    values = np.zeros(len(tels))

    # do a small animation to show various trigger patterns:

    for ii in range(20):

        # generate a random trigger pattern and integrated intensity:
        ntrig = np.random.poisson(10)
        trigmask = np.random.random_integers(len(tels) - 1, size=ntrig)
        values[:] = 0
        values[trigmask] = np.random.uniform(0, 100, size=ntrig)
Ejemplo n.º 50
0
def main():
    script = os.path.splitext(os.path.basename(__file__))[0]
    log.info("[SCRIPT] {}".format(script))

    parser = argparse.ArgumentParser(
        description='Display each event in the file')
    parser.add_argument('-f', '--file', dest='input_path', action='store',
                        default=get_path('gamma_test.simtel.gz'),
                        help='path to the input file. '
                             'Default = gamma_test.simtel.gz')
    parser.add_argument('-O', '--origin', dest='origin', action='store',
                        default='hessio',
                        help='origin of the file: {}. Default = hessio'
                        .format(origin_list()))
    parser.add_argument('-D', dest='display', action='store_true',
                        default=False, help='display the camera events')
    parser.add_argument('--pdf', dest='output_path', action='store',
                        default=None,
                        help='path to store a pdf output of the plots')
    parser.add_argument('-t', '--telescope', dest='tel', action='store',
                        type=int, default=None, help='telecope to view. '
                                                     'Default = All')

    calibration_arguments(parser)

    logger_detail = parser.add_mutually_exclusive_group()
    logger_detail.add_argument('-q', '--quiet', dest='quiet',
                               action='store_true', default=False,
                               help='Quiet mode')
    logger_detail.add_argument('-v', '--verbose', dest='verbose',
                               action='store_true', default=False,
                               help='Verbose mode')
    logger_detail.add_argument('-d', '--debug', dest='debug',
                               action='store_true', default=False,
                               help='Debug mode')

    args = parser.parse_args()
    params = calibration_parameters(args)

    if args.quiet:
        log.setLevel(40)
    if args.verbose:
        log.setLevel(20)
    if args.debug:
        log.setLevel(10)

    log.debug("[file] Reading file")
    input_file = InputFile(args.input_path, args.origin)
    source = input_file.read()

    # geom_dict is a dictionary of CameraGeometry, with keys of
    # (num_pixels, focal_length), the parameters that are used to guess the
    # geometry of the telescope. By using these keys, the geometry is
    # calculated only once per telescope type as needed, reducing computation
    # time.
    # Creating a geom_dict at this point is optional, but is recommended, as
    # the same geom_dict can then be shared between the calibration and
    # CameraPlotter, again reducing computation time.
    # The dictionary becomes filled as a result of a dictionary's mutable
    # nature.
    geom_dict = {}

    # Calibrate events and fill geom_dict
    calibrated_source = calibrate_source(source, params, geom_dict)

    fig = plt.figure(figsize=(16, 7))
    if args.display:
        plt.show(block=False)
    pp = PdfPages(args.output_path) if args.output_path is not None else None
    for event in calibrated_source:
        tels = list(event.dl0.tels_with_data)
        if args.tel is None:
            tel_loop = tels
        else:
            if args.tel not in tels:
                continue
            tel_loop = [args.tel]
        log.debug(tels)
        for tel_id in tel_loop:
            display_telescope(event, tel_id, args.display, geom_dict, pp, fig)
    if pp is not None:
        pp.close()

    log.info("[COMPLETE]")
Ejemplo n.º 51
0
def main():
    script = os.path.splitext(os.path.basename(__file__))[0]
    log.info("[SCRIPT] {}".format(script))

    parser = argparse.ArgumentParser(
        description='Display each event in the file')
    parser.add_argument('-f',
                        '--file',
                        dest='input_path',
                        action='store',
                        default=get_path('gamma_test.simtel.gz'),
                        help='path to the input file. '
                        'Default = gamma_test.simtel.gz')
    parser.add_argument('-O',
                        '--origin',
                        dest='origin',
                        action='store',
                        default='hessio',
                        help='origin of the file: {}. Default = hessio'.format(
                            InputFile.origin_list()))
    parser.add_argument('-D',
                        dest='display',
                        action='store_true',
                        default=False,
                        help='display the camera events')
    parser.add_argument('--pdf',
                        dest='output_path',
                        action='store',
                        default=None,
                        help='path to store a pdf output of the plots')
    parser.add_argument('-t',
                        '--telescope',
                        dest='tel',
                        action='store',
                        type=int,
                        default=None,
                        help='telecope to view. '
                        'Default = All')

    calibration_arguments(parser)

    logger_detail = parser.add_mutually_exclusive_group()
    logger_detail.add_argument('-q',
                               '--quiet',
                               dest='quiet',
                               action='store_true',
                               default=False,
                               help='Quiet mode')
    logger_detail.add_argument('-v',
                               '--verbose',
                               dest='verbose',
                               action='store_true',
                               default=False,
                               help='Verbose mode')
    logger_detail.add_argument('-d',
                               '--debug',
                               dest='debug',
                               action='store_true',
                               default=False,
                               help='Debug mode')

    args = parser.parse_args()
    print('DEBUG type(args) {}'.format(type(args)))
    print('DEBUG args {}'.format(args))
    params = calibration_parameters(args)

    if args.quiet:
        log.setLevel(40)
    if args.verbose:
        log.setLevel(20)
    if args.debug:
        log.setLevel(10)

    log.debug("[file] Reading file")
    input_file = InputFile(args.input_path, args.origin)
    source = input_file.read()

    # geom_dict is a dictionary of CameraGeometry, with keys of
    # (num_pixels, focal_length), the parameters that are used to guess the
    # geometry of the telescope. By using these keys, the geometry is
    # calculated only once per telescope type as needed, reducing computation
    # time.
    # Creating a geom_dict at this point is optional, but is recommended, as
    # the same geom_dict can then be shared between the calibration and
    # CameraPlotter, again reducing computation time.
    # The dictionary becomes filled as a result of a dictionary's mutable
    # nature.
    geom_dict = {}

    # Calibrate events and fill geom_dict

    calibrated_source = calibrate_source(source, params, geom_dict)

    fig = plt.figure(figsize=(16, 7))
    if args.display:
        plt.show(block=False)
    pp = PdfPages(args.output_path) if args.output_path is not None else None
    for event in calibrated_source:
        tels = list(event.dl0.tels_with_data)
        if args.tel is None:
            tel_loop = tels
        else:
            if args.tel not in tels:
                continue
            tel_loop = [args.tel]
        log.debug(tels)
        for tel_id in tel_loop:
            display_telescope(event, tel_id, args.display, geom_dict, pp, fig)
    if pp is not None:
        pp.close()

    log.info("[COMPLETE]")
Ejemplo n.º 52
0
                        else:
                            continue
                else:
                    plt.pause(0.1)
            elif ello == 'q':
                return None

    print("Total NEVT", nt)

if __name__ == '__main__':

    # Declare and parse command line option
    parser = argparse.ArgumentParser(
        description='Tel_id, pixel id and number of event to compute.')
    parser.add_argument('--f', dest='filename',
                        default=get_path('gamma_test.simtel.gz'),
                        required=False, help='filename <MC file name>')
    parser.add_argument('--d', dest='display', action='store_true',
                        required=False, help='display the camera events')
    args = parser.parse_args()

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

    # Function description of camera_calibration options, given here
    # Integrator: samples integration algorithm (equivalent to hessioxxx
    # option --integration-sheme)
    #   -options: full_integration,
    #             simple_integration,
    #             global_peak_integration,
    #             local_peak_integration,
Ejemplo n.º 53
0
def main():
    script = os.path.splitext(os.path.basename(__file__))[0]
    log.info("[SCRIPT] {}".format(script))

    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-f',
                        '--file',
                        dest='input_path',
                        action='store',
                        default=get_path('gamma_test.simtel.gz'),
                        help='path to the input file')
    parser.add_argument('-O',
                        '--origin',
                        dest='origin',
                        action='store',
                        choices=InputFile.origin_list(),
                        default='hessio',
                        help='origin of the file')
    parser.add_argument('-o',
                        '--output',
                        dest='output_dir',
                        action='store',
                        default=None,
                        help='path of the output directory to store the '
                        'images (default = input file directory)')
    parser.add_argument('-e',
                        '--event',
                        dest='event_req',
                        action='store',
                        default=0,
                        type=int,
                        help='event index to plot (not id!)')
    parser.add_argument('--id',
                        dest='event_id_f',
                        action='store_true',
                        default=False,
                        help='-e will specify event_id instead '
                        'of index')
    parser.add_argument('-t',
                        '--telescope',
                        dest='tel',
                        action='store',
                        type=int,
                        default=None,
                        help='telecope to view')
    parser.add_argument('-c',
                        '--channel',
                        dest='chan',
                        action='store',
                        type=int,
                        default=0,
                        help='channel to view (default = 0 (HG))')
    parser.add_argument('--calib-help',
                        dest='calib_help',
                        action='store_true',
                        default=False,
                        help='display the arguments used for the camera '
                        'calibration')

    logger_detail = parser.add_mutually_exclusive_group()
    logger_detail.add_argument('-q',
                               '--quiet',
                               dest='quiet',
                               action='store_true',
                               default=False,
                               help='Quiet mode')
    logger_detail.add_argument('-v',
                               '--verbose',
                               dest='verbose',
                               action='store_true',
                               default=False,
                               help='Verbose mode')
    logger_detail.add_argument('-d',
                               '--debug',
                               dest='debug',
                               action='store_true',
                               default=False,
                               help='Debug mode')

    args, excess_args = parser.parse_known_args()

    if args.quiet:
        log.setLevel(40)
    if args.verbose:
        log.setLevel(20)
    if args.debug:
        log.setLevel(10)

    telid = args.tel
    chan = args.chan

    log.debug("[file] Reading file")
    input_file = InputFile(args.input_path, args.origin)
    event = input_file.get_event(args.event_req, args.event_id_f)

    # Print event/args values
    log.info("[event_index] {}".format(event.count))
    log.info("[event_id] {}".format(event.dl0.event_id))
    log.info("[telescope] {}".format(telid))
    log.info("[channel] {}".format(chan))

    params, unknown_args = calibration_parameters(excess_args, args.origin,
                                                  args.calib_help)

    if unknown_args:
        parser.print_help()
        calibration_parameters(unknown_args, args.origin, True)
        msg = 'unrecognized arguments: %s'
        parser.error(msg % ' '.join(unknown_args))

    # Create a dictionary to store any geoms in
    geom = CameraGeometry.guess(*event.meta.pixel_pos[telid],
                                event.meta.optical_foclen[telid])
    geom_dict = {telid: geom}
    calibrated_event = calibrate_event(event, params, geom_dict)

    # Select telescope
    tels = list(calibrated_event.dl0.tels_with_data)
    if telid is None or telid not in tels:
        log.error("[event] please specify one of the following telescopes "
                  "for this event: {}".format(tels))
        exit()

    # Extract required images
    data_ped = calibrated_event.dl1.tel[telid].pedestal_subtracted_adc[chan]
    true_pe = calibrated_event.mc.tel[telid].photo_electrons
    measured_pe = calibrated_event.dl1.tel[telid].pe_charge
    max_time = np.unravel_index(np.argmax(data_ped), data_ped.shape)[1]
    max_charges = np.max(data_ped, axis=1)
    max_pixel = int(np.argmax(max_charges))
    min_pixel = int(np.argmin(max_charges))

    # Get Neighbours
    max_pixel_nei = geom.neighbors[max_pixel]
    min_pixel_nei = geom.neighbors[min_pixel]

    # Get Windows
    windows = calibrated_event.dl1.tel[telid].integration_window[chan]
    length = np.sum(windows, axis=1)
    start = np.argmax(windows, axis=1)
    end = start + length - 1

    # Draw figures
    ax_max_nei = {}
    ax_min_nei = {}
    fig_waveforms = plt.figure(figsize=(18, 9))
    fig_waveforms.subplots_adjust(hspace=.5)
    fig_camera = plt.figure(figsize=(15, 12))

    ax_max_pix = fig_waveforms.add_subplot(4, 2, 1)
    ax_min_pix = fig_waveforms.add_subplot(4, 2, 2)
    ax_max_nei[0] = fig_waveforms.add_subplot(4, 2, 3)
    ax_min_nei[0] = fig_waveforms.add_subplot(4, 2, 4)
    ax_max_nei[1] = fig_waveforms.add_subplot(4, 2, 5)
    ax_min_nei[1] = fig_waveforms.add_subplot(4, 2, 6)
    ax_max_nei[2] = fig_waveforms.add_subplot(4, 2, 7)
    ax_min_nei[2] = fig_waveforms.add_subplot(4, 2, 8)

    ax_img_nei = fig_camera.add_subplot(2, 2, 1)
    ax_img_max = fig_camera.add_subplot(2, 2, 2)
    ax_img_true = fig_camera.add_subplot(2, 2, 3)
    ax_img_cal = fig_camera.add_subplot(2, 2, 4)

    plotter = CameraPlotter(event, geom_dict)

    # Draw max pixel traces
    plotter.draw_waveform(data_ped[max_pixel], ax_max_pix)
    ax_max_pix.set_title("(Max) Pixel: {}, "
                         "True: {}, "
                         "Measured = {:.3f}".format(max_pixel,
                                                    true_pe[max_pixel],
                                                    measured_pe[max_pixel]))
    ax_max_pix.set_ylabel("Amplitude-Ped (ADC)")
    max_ylim = ax_max_pix.get_ylim()
    plotter.draw_waveform_positionline(start[max_pixel], ax_max_pix)
    plotter.draw_waveform_positionline(end[max_pixel], ax_max_pix)
    for i, ax in ax_max_nei.items():
        if len(max_pixel_nei) > i:
            pix = max_pixel_nei[i]
            plotter.draw_waveform(data_ped[pix], ax)
            ax.set_title("(Max Nei) Pixel: {}, "
                         "True: {}, "
                         "Measured = {:.3f}".format(pix, true_pe[pix],
                                                    measured_pe[pix]))
            ax.set_ylabel("Amplitude-Ped (ADC)")
            ax.set_ylim(max_ylim)
            plotter.draw_waveform_positionline(start[pix], ax)
            plotter.draw_waveform_positionline(end[pix], ax)

    # Draw min pixel traces
    plotter.draw_waveform(data_ped[min_pixel], ax_min_pix)
    ax_min_pix.set_title("(Min) Pixel: {}, "
                         "True: {}, "
                         "Measured = {:.3f}".format(min_pixel,
                                                    true_pe[min_pixel],
                                                    measured_pe[min_pixel]))
    ax_min_pix.set_ylabel("Amplitude-Ped (ADC)")
    ax_min_pix.set_ylim(max_ylim)
    plotter.draw_waveform_positionline(start[min_pixel], ax_min_pix)
    plotter.draw_waveform_positionline(end[min_pixel], ax_min_pix)
    for i, ax in ax_min_nei.items():
        if len(min_pixel_nei) > i:
            pix = min_pixel_nei[i]
            plotter.draw_waveform(data_ped[pix], ax)
            ax.set_title("(Min Nei) Pixel: {}, "
                         "True: {}, "
                         "Measured = {:.3f}".format(pix, true_pe[pix],
                                                    measured_pe[pix]))
            ax.set_ylabel("Amplitude-Ped (ADC)")
            ax.set_ylim(max_ylim)
            plotter.draw_waveform_positionline(start[pix], ax)
            plotter.draw_waveform_positionline(end[pix], ax)

    # Draw cameras
    nei_camera = np.zeros_like(max_charges, dtype=np.int)
    nei_camera[min_pixel_nei] = 2
    nei_camera[min_pixel] = 1
    nei_camera[max_pixel_nei] = 3
    nei_camera[max_pixel] = 4
    camera = plotter.draw_camera(telid, nei_camera, ax_img_nei)
    camera.cmap = plt.cm.viridis
    ax_img_nei.set_title("Neighbour Map")
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_nei)

    camera = plotter.draw_camera(telid, data_ped[:, max_time], ax_img_max)
    camera.add_colorbar(ax=ax_img_max, label="Amplitude-Ped (ADC)")
    ax_img_max.set_title("Max Timeslice (T = {})".format(max_time))
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_max)

    camera = plotter.draw_camera(telid, true_pe, ax_img_true)
    camera.add_colorbar(ax=ax_img_true, label="True Charge (Photo-electrons)")
    ax_img_true.set_title("True Charge")
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_true)

    int_dict, inverted = integrator_dict()
    integrator_name = '' if 'integrator' not in params else \
        params['integrator']

    camera = plotter.draw_camera(telid, measured_pe, ax_img_cal)
    camera.add_colorbar(ax=ax_img_cal, label="Calib Charge (Photo-electrons)")
    ax_img_cal.set_title("Charge (integrator={})".format(integrator_name))
    plotter.draw_camera_pixel_annotation(telid, max_pixel, min_pixel,
                                         ax_img_cal)

    fig_waveforms.suptitle("Integrator = {}".format(integrator_name))
    fig_camera.suptitle("Camera = {}".format(geom.cam_id))

    waveform_output_name = "{}_e{}_t{}_c{}_integrator{}_waveform.pdf"\
        .format(input_file.filename, event.count, telid, chan,
                inverted[params['integrator']])
    camera_output_name = "{}_e{}_t{}_c{}_integrator{}_camera.pdf"\
        .format(input_file.filename, event.count, telid, chan,
                inverted[params['integrator']])
    output_dir = args.output_dir if args.output_dir is not None else \
        input_file.output_directory
    output_dir = os.path.join(output_dir, script)
    if not os.path.exists(output_dir):
        log.info("[output] Creating directory: {}".format(output_dir))
        os.makedirs(output_dir)

    waveform_output_path = os.path.join(output_dir, waveform_output_name)
    log.info("[output] {}".format(waveform_output_path))
    fig_waveforms.savefig(waveform_output_path,
                          format='pdf',
                          bbox_inches='tight')

    camera_output_path = os.path.join(output_dir, camera_output_name)
    log.info("[output] {}".format(camera_output_path))
    fig_camera.savefig(camera_output_path, format='pdf', bbox_inches='tight')

    log.info("[COMPLETE]")
Ejemplo n.º 54
0
from astropy.io import fits
from scipy.interpolate import RegularGridInterpolator

from ctapipe.utils.datasets import get_path
from ctapipe.utils import Histogram


if __name__ == '__main__':

    # Load an example datacube (an energy table generated for a
    # small subset of HESS simulations) to use as a lookup table. Here
    # we will use the Histogram class, which automatically loads both
    # the data cube and creates arrays for the coordinates of each
    # axis.
    
    testfile = get_path("hess_ct001_energylut.fits.gz")
    energy_hdu = fits.open(testfile)['MEAN']
    energy_table = Histogram(initFromFITS=energy_hdu)
    print(energy_table)
    
    # Now, construct an interpolator that we can use to get values at
    # any point:
    centers = [energy_table.binCenters(ii) for ii in range(energy_table.ndims)]
    energy_lookup = RegularGridInterpolator(centers, energy_table.hist,
                                            bounds_error=False,fill_value=-100)

    # energy_lookup is now just a continuous function of log(SIZE),
    # DISTANCE in m.  We can now plot some curves from the
    # interpolator.  
    #
    # Note that the LUT we used is does not have very high statistics,
Ejemplo n.º 55
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
    def initialize(self, argv=None):
        self.log.info("Initializing the calibration pipeline...")

        # Declare and parse command line option
        parser = argparse.ArgumentParser(
            description='Display each event in the file')
        parser.add_argument('-f', '--file', dest='input_path',
                            action='store',
                            default=get_path('gamma_test.simtel.gz'),
                            help='path to the input file. '
                            ' Default = gamma_test.simtel.gz')
        parser.add_argument('-O', '--origin', dest='origin',
                            action='store',
                            default='hessio',
                            help='origin of the file: {}. '
                            'Default = hessio'
                            .format(origin_list()))
        parser.add_argument('-D', dest='display', action='store_true',
                            default=False,
                            help='display the camera events')
        parser.add_argument('--pdf', dest='output_path', action='store',
                            default=None,
                            help='path to store a pdf output of the plots')
        parser.add_argument('-t', '--telescope', dest='tel',
                            action='store',
                            type=int, default=None,
                            help='telecope to view. Default = All')

        calibration_arguments(parser)

        logger_detail = parser.add_mutually_exclusive_group()
        logger_detail.add_argument('-q', '--quiet', dest='quiet',
                                   action='store_true', default=False,
                                   help='Quiet mode')
        logger_detail.add_argument('-v', '--verbose', dest='verbose',
                                   action='store_true', default=False,
                                   help='Verbose mode')
        logger_detail.add_argument('-d', '--debug', dest='debug',
                                   action='store_true', default=False,
                                   help='Debug mode')

        self.args = parser.parse_args()
        self.params = calibration_parameters(self.args)

        if self.args.quiet:
            self.log.setLevel(40)
        if self.args.verbose:
            self.log.setLevel(20)
        if self.args.debug:
            self.log.setLevel(10)

        self.log.debug("[file] Reading file")
        input_file = InputFile(self.args.input_path, self.args.origin)
        self.source = input_file.read()

        # geom_dict is a dictionary of CameraGeometry, with keys of
        # (num_pixels, focal_length), the parameters that are used to guess the
        # geometry of the telescope. By using these keys, the geometry is
        # calculated only once per telescope type as needed, reducing
        # computation time.
        # Creating a geom_dict at this point is optional, but is recommended,
        # as the same geom_dict can then be shared between the calibration and
        # CameraPlotter, again reducing computation time.
        # The dictionary becomes filled as a result of a dictionary's mutable
        # nature.def display_telescope(event, tel_id, display,
        # geom_dict, pp, fig):
        self.geom_dict = {}