Ejemplo n.º 1
0
    def _test_sim_onoff(self):
        """
        Test sim() function in On/Off mode
        """
        # Set-up observation container
        pnt = gammalib.GSkyDir()
        pnt.radec_deg(83.63, 22.51)
        obs = gammalib.GObservations()
        run = obsutils.set_obs(pnt, duration=100.0, emin=1.0, emax=10.0, obsid='0')
        obs.append(run)
        pnt.radec_deg(83.63, 21.51)
        run = obsutils.set_obs(pnt, duration=100.0, emin=1.0, emax=10.0, obsid='1')
        obs.append(run)
        obs.models(gammalib.GModels(self._model))

        # Simulate stacked observations
        res = obsutils.sim(obs, onsrc='Crab', nbins=5)

        # Check simulation results
        self.test_value(res.size(), 2, 'Check number of observations')
        self.test_value(res.models().size(), 2, 'Check number of models')
        self.test_value(res.nobserved(), 46, 'Check number of observed events')
        self.test_value(res.npred(), 0.0, 'Check number of predicted events')
        self.test_value(res[0].on_spec().ebounds().emin().TeV(), 1.0,
                        'Check minimum energy of On spectrum')
        self.test_value(res[0].on_spec().ebounds().emax().TeV(), 10.0,
                        'Check minimum energy of On spectrum')
        self.test_value(res[0].on_spec().ebounds().size(), 5,
                        'Check number of energy bins of On spectrum')
        self.test_value(res[0].on_spec().counts(), 24,
                        'Check number of events in of On spectrum')
        self.test_value(res[0].off_spec().ebounds().emin().TeV(), 1.0,
                        'Check minimum energy of Off spectrum')
        self.test_value(res[0].off_spec().ebounds().emax().TeV(), 10.0,
                        'Check minimum energy of Off spectrum')
        self.test_value(res[0].off_spec().ebounds().size(), 5,
                        'Check number of energy bins of Off spectrum')
        self.test_value(res[0].off_spec().counts(), 2,
                        'Check number of events in of Off spectrum')
        self.test_value(res[0].arf().ebounds().emin().TeV(), 0.5,
                        'Check minimum energy of ARF')
        self.test_value(res[0].arf().ebounds().emax().TeV(), 12.0,
                        'Check minimum energy of ARF')
        self.test_value(res[0].arf().ebounds().size(), 42,
                        'Check number of energy bins of ARF')
        self.test_value(res[0].rmf().etrue().emin().TeV(), 0.5,
                        'Check minimum true energy of RMF')
        self.test_value(res[0].rmf().etrue().emax().TeV(), 12.0,
                        'Check minimum true energy of RMF')
        self.test_value(res[0].rmf().etrue().size(), 42,
                        'Check number of true energy bins of RMF')
        self.test_value(res[0].rmf().emeasured().emin().TeV(), 1.0,
                        'Check minimum reconstructed energy of RMF')
        self.test_value(res[0].rmf().emeasured().emax().TeV(), 10.0,
                        'Check minimum reconstructed energy of RMF')
        self.test_value(res[0].rmf().emeasured().size(), 5,
                        'Check number of reconstructed energy bins of RMF')

        # Return
        return
Ejemplo n.º 2
0
def survey_single():
    """
    Creates a single observation survey for test purposes.
    """
    # Allocate observation container
    obs = gammalib.GObservations()

    # Set single pointing at galactic centre
    pntdir = gammalib.GSkyDir()
    pntdir.lb_deg(0.0, 0.0)
    run = obsutils.set_obs(pntdir)
    obs.append(run)

    # Define single point source with Crab flux at galactic centre
    center = gammalib.GSkyDir()
    center.lb_deg(0.0, 0.0)
    point_spatial  = gammalib.GModelSpatialPointSource(center)
    point_spectrum = crab_spec()
    point          = gammalib.GModelSky(point_spatial, point_spectrum)
    point.name('GC source')

    # Create model container
    models = gammalib.GModels()
    models.append(point)
    obs.models(models)

    # Return observation container
    return obs
Ejemplo n.º 3
0
def survey_single():
    """
    Creates a single observation survey for test purposes.
    """
    # Allocate observation container
    obs = gammalib.GObservations()

    # Set single pointing at galactic centre
    pntdir = gammalib.GSkyDir()
    pntdir.lb_deg(0.0, 0.0)
    run = obsutils.set_obs(pntdir)
    obs.append(run)

    # Define single point source with Crab flux at galactic centre
    center = gammalib.GSkyDir()
    center.lb_deg(0.0, 0.0)
    point_spatial  = gammalib.GModelSpatialPointSource(center)
    point_spectrum = crab_spec()
    point          = gammalib.GModelSky(point_spatial, point_spectrum)
    point.name('GC source')

    # Create model container
    models = gammalib.GModels()
    models.append(point)
    obs.models(models)

    # Return observation container
    return obs
Ejemplo n.º 4
0
    def _set_obs(self, emin, emax, lpnt=0.0, bpnt=0.0):
        """
        Set an observation container

        Parameters
        ----------
        emin : float
            Minimum energy (TeV)
        emax : float
            Maximum energy (TeV)
        lpnt : float, optional
            Galactic longitude of pointing (deg)
        bpnt : float, optional
            Galactic latitude of pointing (deg)

        Returns
        -------
        obs : `~gammalib.GObservations`
            Observation container
        """
        # If an observation was provided on input then load it from XML file
        filename = self['inobs'].filename()
        if filename != 'NONE' and filename != '':
            obs = self._get_observations()

        # ... otherwise allocate a single observation
        else:

            # Read relevant user parameters
            caldb    = self['caldb'].string()
            irf      = self['irf'].string()
            deadc    = self['deadc'].real()
            duration = self['duration'].real()
            rad      = self['rad'].real()

            # Allocate observation container
            obs = gammalib.GObservations()

            # Set single pointing
            pntdir = gammalib.GSkyDir()
            pntdir.lb_deg(lpnt, bpnt)

            # Create CTA observation
            run = obsutils.set_obs(pntdir, caldb=caldb, irf=irf,
                                   duration=duration, deadc=deadc,
                                   emin=emin, emax=emax, rad=rad)

            # Append observation to container
            obs.append(run)

            # Set source position
            offset    = self['offset'].real()
            pntdir.lb_deg(lpnt, bpnt+offset)
            self._ra  = pntdir.ra_deg()
            self._dec = pntdir.dec_deg()

        # Return observation container
        return obs
Ejemplo n.º 5
0
    def _set_obs(self, lpnt=0.0, bpnt=0.0, emin=0.1, emax=100.0):
        """
        Set an observation container.

        Kwargs:
            lpnt: Galactic longitude of pointing [deg] (default: 0.0)
            bpnt: Galactic latitude of pointing [deg] (default: 0.0)
            emin: Minimum energy [TeV] (default: 0.1)
            emax: Maximum energy [TeV] (default: 100.0)

        Returns:
            Observation container.
        """
        # If an observation was provided on input then load it from XML
        # file
        filename = self["inobs"].filename()
        if filename != "NONE" and filename != "":
            obs = self._get_observations()

        # ... otherwise allocate a single observation
        else:

            # Read relevant user parameters
            caldb = self["caldb"].string()
            irf = self["irf"].string()
            deadc = self["deadc"].real()
            duration = self["duration"].real()
            rad = self["rad"].real()

            # Allocate observation container
            obs = gammalib.GObservations()

            # Set single pointing
            pntdir = gammalib.GSkyDir()
            pntdir.lb_deg(lpnt, bpnt)

            # Create CTA observation
            run = obsutils.set_obs(pntdir,
                                   caldb=caldb,
                                   irf=irf,
                                   duration=duration,
                                   deadc=deadc,
                                   emin=emin,
                                   emax=emax,
                                   rad=rad)

            # Append observation to container
            obs.append(run)

            # Set source position
            offset = self["offset"].real()
            pntdir.lb_deg(lpnt, bpnt + offset)
            self._ra = pntdir.ra_deg()
            self._dec = pntdir.dec_deg()

        # Return observation container
        return obs
Ejemplo n.º 6
0
    def _test_set_obs(self):
        """
        Test set_obs() function
        """
        # Setup pointing direction
        pnt = gammalib.GSkyDir()
        pnt.radec_deg(83.63, 22.51)

        # Setup one CTA observation
        res = obsutils.set_obs(pnt, emin=1.0, emax=10.0)

        # Check result
        self.test_value(res.eventtype(), 'EventList', 'Check event type')
        self.test_value(res.events().ebounds().emin().TeV(), 1.0,
                        'Check minimum energy')
        self.test_value(res.events().ebounds().emax().TeV(), 10.0,
                        'Check maximum energy')
        self.test_value(res.pointing().dir().ra_deg(), 83.63,
                        'Check pointing Right Ascension')
        self.test_value(res.pointing().dir().dec_deg(), 22.51,
                        'Check pointing declination')

        # Setup one CTA observation for local caldb directory
        res = obsutils.set_obs(pnt, caldb=self._datadir, irf='irf_file.fits',
                               emin=1.0, emax=10.0)

        # Check result
        self.test_value(res.eventtype(), 'EventList', 'Check event type')
        self.test_value(res.events().ebounds().emin().TeV(), 1.0,
                        'Check minimum energy')
        self.test_value(res.events().ebounds().emax().TeV(), 10.0,
                        'Check maximum energy')
        self.test_value(res.pointing().dir().ra_deg(), 83.63,
                        'Check pointing Right Ascension')
        self.test_value(res.pointing().dir().dec_deg(), 22.51,
                        'Check pointing declination')

        # Return
        return
Ejemplo n.º 7
0
    def set_obs(self, lpnt=0.0, bpnt=0.0, emin=0.1, emax=100.0):
        """
        Returns an observation container.

        Keywords:
         lpnt - Galactic longitude of pointing [deg] (default: 0.0)
         bpnt - Galactic latitude of pointing [deg] (default: 0.0)
         emin - Minimum energy [TeV] (default: 0.1)
         emax - Maximum energy [TeV] (default: 100.0)
        """
        # If an observation was provided on input then load it from XML file
        filename = self["inobs"].filename()
        if filename != "NONE" and filename != "":
            obs = self.get_observations()

        # ... otherwise allocate a single observation
        else:

            # Read relevant user parameters
            caldb    = self["caldb"].string()
            irf      = self["irf"].string()
            deadc    = self["deadc"].real()
            duration = self["duration"].real()
            rad      = self["rad"].real()

            # Allocate observation container
            obs = gammalib.GObservations()

            # Set single pointing
            pntdir = gammalib.GSkyDir()
            pntdir.lb_deg(lpnt, bpnt)

            # Create CTA observation
            run = obsutils.set_obs(pntdir, caldb=caldb, irf=irf,
                                   duration=duration, deadc=deadc,
                                   emin=emin, emax=emax, rad=rad)

            # Append observation to container
            obs.append(run)

            # Set source position
            offset     = self["offset"].real()
            pntdir.lb_deg(lpnt, bpnt+offset)
            self.m_ra  = pntdir.ra_deg()
            self.m_dec = pntdir.dec_deg()

        # Return observation container
        return obs
Ejemplo n.º 8
0
    def _setup_sim(self, two=False):
        """
        Setup method for sim() function test
        """
        # Set-up observation container
        pnt = gammalib.GSkyDir()
        pnt.radec_deg(83.6331, 22.0145)
        obs = gammalib.GObservations()
        run = obsutils.set_obs(pnt, duration=20.0, emin=1.0, emax=10.0)
        run.id('0')
        obs.append(run)
        if two:
            run.id('1')
            obs.append(run)

        # Append model
        obs.models(gammalib.GModels(self._model))

        # Return
        return obs
Ejemplo n.º 9
0
    def _setup_sim(self, two=False):
        """
        Setup method for sim() function test
        """
        # Set-up observation container
        pnt = gammalib.GSkyDir()
        pnt.radec_deg(83.6331, 22.0145)
        obs = gammalib.GObservations()
        run = obsutils.set_obs(pnt, duration=20.0, emin=1.0, emax=10.0)
        run.id('0')
        obs.append(run)
        if two:
            run.id('1')
            obs.append(run)

        # Append model
        obs.models(gammalib.GModels(self._model))

        # Return
        return obs
Ejemplo n.º 10
0
def survey_gplane(lrange=10, lstep=2):
    """
    Creates a single observation survey for test purposes.

    Keywords:
     lrange - Longitude range (integer deg)
     lstep  - Longitude step size (integer deg)
    """
    # Allocate observation container
    obs = gammalib.GObservations()

    # Loop over longitudes
    for l in range(-lrange,lrange+lstep,lstep):

        # Set pointing
        pntdir = gammalib.GSkyDir()
        pntdir.lb_deg(l, 0.0)
        run = obsutils.set_obs(pntdir)
        run.id(str(l))
        obs.append(run)

    # Define single point source with Crab flux at galactic centre
    center = gammalib.GSkyDir()
    center.lb_deg(0.0, 0.0)
    point_spatial  = gammalib.GModelSpatialPointSource(center)
    point_spectrum = crab_spec()
    point          = gammalib.GModelSky(point_spatial, point_spectrum)
    point.name('GC source')

    # Create model container
    models = gammalib.GModels()
    models.append(point)
    obs.models(models)

    # Return observation container
    return obs
Ejemplo n.º 11
0
def survey_gplane(lrange=10, lstep=2):
    """
    Creates a single observation survey for test purposes.

    Keywords:
     lrange - Longitude range (integer deg)
     lstep  - Longitude step size (integer deg)
    """
    # Allocate observation container
    obs = gammalib.GObservations()

    # Loop over longitudes
    for l in range(-lrange,lrange+lstep,lstep):

        # Set pointing
        pntdir = gammalib.GSkyDir()
        pntdir.lb_deg(l, 0.0)
        run = obsutils.set_obs(pntdir)
        run.id(str(l))
        obs.append(run)

    # Define single point source with Crab flux at galactic centre
    center = gammalib.GSkyDir()
    center.lb_deg(0.0, 0.0)
    point_spatial  = gammalib.GModelSpatialPointSource(center)
    point_spectrum = crab_spec()
    point          = gammalib.GModelSky(point_spatial, point_spectrum)
    point.name('GC source')

    # Create model container
    models = gammalib.GModels()
    models.append(point)
    obs.models(models)

    # Return observation container
    return obs
Ejemplo n.º 12
0
    def _test_sim_onoff(self):
        """
        Test sim() function in On/Off mode
        """
        # Set-up observation container
        pnt = gammalib.GSkyDir()
        pnt.radec_deg(83.63, 22.51)
        obs = gammalib.GObservations()
        run = obsutils.set_obs(pnt, duration=100.0, emin=1.0, emax=10.0, obsid='0')
        obs.append(run)
        pnt.radec_deg(83.63, 21.51)
        run = obsutils.set_obs(pnt, duration=100.0, emin=1.0, emax=10.0, obsid='1')
        obs.append(run)
        obs.models(gammalib.GModels(self._model))

        # Simulate On/Off observations
        res = obsutils.sim(obs, onsrc='Crab', nbins=5)

        # Check simulation results
        self.test_value(res.size(), 2, 'Check number of observations')
        self.test_value(res.models().size(), 2, 'Check number of models')
        self.test_value(res.nobserved(), 48, 'Check number of observed events')
        self.test_value(res.npred(), 0.0, 'Check number of predicted events')

        # Check results of first observation
        self.test_value(res[0].on_spec().ebounds().emin().TeV(), 1.0,
                        'Check minimum energy of On spectrum')
        self.test_value(res[0].on_spec().ebounds().emax().TeV(), 10.0,
                        'Check maximum energy of On spectrum')
        self.test_value(res[0].on_spec().ebounds().size(), 5,
                        'Check number of energy bins of On spectrum')
        self.test_value(res[0].on_spec().counts(), 26,
                        'Check number of events in of On spectrum')
        self.test_value(res[0].off_spec().ebounds().emin().TeV(), 1.0,
                        'Check minimum energy of Off spectrum')
        self.test_value(res[0].off_spec().ebounds().emax().TeV(), 10.0,
                        'Check maximum energy of Off spectrum')
        self.test_value(res[0].off_spec().ebounds().size(), 5,
                        'Check number of energy bins of Off spectrum')
        self.test_value(res[0].off_spec().counts(), 1,
                        'Check number of events in of Off spectrum')
        self.test_value(res[0].arf().ebounds().emin().TeV(), 0.5,
                        'Check minimum energy of ARF')
        self.test_value(res[0].arf().ebounds().emax().TeV(), 12.0,
                        'Check maximum energy of ARF')
        self.test_value(res[0].arf().ebounds().size(), 41,
                        'Check number of energy bins of ARF')
        self.test_value(res[0].rmf().etrue().emin().TeV(), 0.5,
                        'Check minimum true energy of RMF')
        self.test_value(res[0].rmf().etrue().emax().TeV(), 12.0,
                        'Check maximum true energy of RMF')
        self.test_value(res[0].rmf().etrue().size(), 41,
                        'Check number of true energy bins of RMF')
        self.test_value(res[0].rmf().emeasured().emin().TeV(), 1.0,
                        'Check minimum reconstructed energy of RMF')
        self.test_value(res[0].rmf().emeasured().emax().TeV(), 10.0,
                        'Check maximum reconstructed energy of RMF')
        self.test_value(res[0].rmf().emeasured().size(), 5,
                        'Check number of reconstructed energy bins of RMF')

        # Check results of second observation
        self.test_value(res[1].on_spec().ebounds().emin().TeV(), 1.0,
                        'Check minimum energy of On spectrum')
        self.test_value(res[1].on_spec().ebounds().emax().TeV(), 10.0,
                        'Check maximum energy of On spectrum')
        self.test_value(res[1].on_spec().ebounds().size(), 5,
                        'Check number of energy bins of On spectrum')
        self.test_value(res[1].on_spec().counts(), 22,
                        'Check number of events in of On spectrum')
        self.test_value(res[1].off_spec().ebounds().emin().TeV(), 1.0,
                        'Check minimum energy of Off spectrum')
        self.test_value(res[1].off_spec().ebounds().emax().TeV(), 10.0,
                        'Check maximum energy of Off spectrum')
        self.test_value(res[1].off_spec().ebounds().size(), 5,
                        'Check number of energy bins of Off spectrum')
        self.test_value(res[1].off_spec().counts(), 1,
                        'Check number of events in of Off spectrum')
        self.test_value(res[1].arf().ebounds().emin().TeV(), 0.5,
                        'Check minimum energy of ARF')
        self.test_value(res[1].arf().ebounds().emax().TeV(), 12.0,
                        'Check maximum energy of ARF')
        self.test_value(res[1].arf().ebounds().size(), 41,
                        'Check number of energy bins of ARF')
        self.test_value(res[1].rmf().etrue().emin().TeV(), 0.5,
                        'Check minimum true energy of RMF')
        self.test_value(res[1].rmf().etrue().emax().TeV(), 12.0,
                        'Check maximum true energy of RMF')
        self.test_value(res[1].rmf().etrue().size(), 41,
                        'Check number of true energy bins of RMF')
        self.test_value(res[1].rmf().emeasured().emin().TeV(), 1.0,
                        'Check minimum reconstructed energy of RMF')
        self.test_value(res[1].rmf().emeasured().emax().TeV(), 10.0,
                        'Check maximum reconstructed energy of RMF')
        self.test_value(res[1].rmf().emeasured().size(), 5,
                        'Check number of reconstructed energy bins of RMF')

        # Return
        return
Ejemplo n.º 13
0
    def _set_obs(self, emin, emax, lpnt=0.0, bpnt=0.0):
        """
        Set an observation container

        Parameters
        ----------
        emin : float
            Minimum energy (TeV)
        emax : float
            Maximum energy (TeV)
        lpnt : float, optional
            Galactic longitude of pointing (deg)
        bpnt : float, optional
            Galactic latitude of pointing (deg)

        Returns
        -------
        obs : `~gammalib.GObservations`
            Observation container
        """
        # If an observation was provided on input then load it from XML file
        if self['inobs'].is_valid():
            obs = self._get_observations()

        # ... otherwise allocate a single observation
        else:

            # Read relevant user parameters
            caldb = self['caldb'].string()
            irf = self['irf'].string()
            deadc = self['deadc'].real()
            duration = self['duration'].real()
            rad = self['rad'].real()

            # Allocate observation container
            obs = gammalib.GObservations()

            # Set single pointing
            pntdir = gammalib.GSkyDir()
            pntdir.lb_deg(lpnt, bpnt)

            # Create CTA observation
            run = obsutils.set_obs(pntdir,
                                   caldb=caldb,
                                   irf=irf,
                                   duration=duration,
                                   deadc=deadc,
                                   emin=emin,
                                   emax=emax,
                                   rad=rad)

            # Append observation to container
            obs.append(run)

            # Set source position
            offset = self['offset'].real()
            pntdir.lb_deg(lpnt, bpnt + offset)
            self._ra = pntdir.ra_deg()
            self._dec = pntdir.dec_deg()

        # Return observation container
        return obs