Exemplo n.º 1
0
    def test_container(self):
        """
        Test ctobssim on observation container.
        """
        # Set-up observation container
        obs = self.set_obs(4)
        
        # Set-up ctobssim
        sim = ctools.ctobssim(obs)
        sim["outevents"].filename("sim_events.xml")
        sim["inmodel"].filename(self.model_name)
        # Run tool
        self.test_try("Run ctobssim")
        try:
            sim.run()
            self.test_try_success()
        except:
            self.test_try_failure("Exception occured in ctobssim.")

        # Retrieve observation and check content
        self.test_value(sim.obs().size(), 4, "There are not 4 observations")

        # Save events
        self.test_try("Save events")
        try:
            sim.save()
            self.test_try_success()
        except:
            self.test_try_failure("Exception occured in saving events.")
Exemplo n.º 2
0
def simulate_ctobssim(obs, xmlname, seed=0):
    """
    Simulate events using ctobssim.
    """
    # Create containers
    observations = gammalib.GObservations()
    observations.append(obs)

    # Append models
    models = gammalib.GModels(xmlname)
    observations.models(models)
    print(models[0])

    # Allocate ctobssim application and set parameters
    sim = ctools.ctobssim(observations)
    sim['seed'].integer(seed)

    # Run simulator
    sim.run()
    
    # Retrieve events
    events = sim.obs()[0].events().copy()

    # Print event statistics
    npred = obs.npred(models)
    print(str(len(events)) + " events simulated.")
    print(str(npred) + " events expected from Npred.")

    # Delete the simulation
    del sim

    # Return events
    return events
Exemplo n.º 3
0
 def test_unbinned_mem(self):
     """
     Test unbinned in-memory pipeline.
     """
     # Set script parameters
     model_name           = "data/crab.xml"
     caldb                = "irf"
     irf                  = "cta_dummy_irf"
     ra                   =   83.63
     dec                  =   22.01
     rad_sim              =   10.0
     tstart               =    0.0
     tstop                = 1800.0
     emin                 =    0.1
     emax                 =  100.0
     rad_select           =    3.0
 
     # Simulate events
     sim = ctools.ctobssim()
     sim["inmodel"].filename(model_name)
     sim["caldb"].string(caldb)
     sim["irf"].string(irf)
     sim["ra"].real(ra)
     sim["dec"].real(dec)
     sim["rad"].real(rad_sim)
     sim["tmin"].real(tstart)
     sim["tmax"].real(tstop)
     sim["emin"].real(emin)
     sim["emax"].real(emax)
     self.test_try("Run ctobssim")
     try:
         sim.run()
         self.test_try_success()
     except:
         self.test_try_failure("Exception occured in ctobssim.")
 
     # Select events
     select = ctools.ctselect(sim.obs())
     select["ra"].real(ra)
     select["dec"].real(dec)
     select["rad"].real(rad_select)
     select["tmin"].real(tstart)
     select["tmax"].real(tstop)
     select["emin"].real(emin)
     select["emax"].real(emax)
     self.test_try("Run ctselect")
     try:
         select.run()
         self.test_try_success()
     except:
         self.test_try_failure("Exception occured in ctselect.")
 
     # Perform maximum likelihood fitting
     like = ctools.ctlike(select.obs())
     self.test_try("Run ctlike")
     try:
         like.run()
         self.test_try_success()
     except:
         self.test_try_failure("Exception occured in ctlike.")
Exemplo n.º 4
0
def ctobssim(inmodel,
             outevents,
             ra=221.0,
             dec=46.0,
             rad=5.0,
             tmin='2020-01-01T00:00:00',
             tmax='2020-01-01T00:15:00',
             emin=0.1,
             emax=100.0,
             caldb='prod2',
             irf='South_0.5h',
             seed=1):
    sim = ctools.ctobssim()
    sim['ra'] = ra
    sim['dec'] = dec
    sim['rad'] = rad
    sim['tmin'] = tmin
    sim['tmax'] = tmax
    sim['emin'] = emin
    sim['emax'] = emax
    sim['caldb'] = caldb
    sim['irf'] = irf
    sim['inmodel'] = inmodel
    sim['outevents'] = outevents
    sim['seed'] = seed
    sim.execute()
    print("Generated " + outevents + " using seed: " + str(seed))
Exemplo n.º 5
0
def simulate_ctobssim(obs, xmlname, seed=0):
    """
    Simulate events using ctobssim.
    """
    # Create containers
    observations = gammalib.GObservations()
    observations.append(obs)

    # Append models
    models = gammalib.GModels(xmlname)
    observations.models(models)
    print(models[0])

    # Allocate ctobssim application and set parameters
    sim = ctools.ctobssim(observations)
    sim['seed'].integer(seed)

    # Run simulator
    sim.run()

    # Retrieve events
    events = sim.obs()[0].events().copy()

    # Print event statistics
    npred = obs.npred(models)
    print(str(len(events)) + " events simulated.")
    print(str(npred) + " events expected from Npred.")

    # Delete the simulation
    del sim

    # Return events
    return events
Exemplo n.º 6
0
def unbinned_pipeline(duration):
    """
    Unbinned analysis pipeline.
    """
    # Set script parameters
    model_name  = "${CTOOLS}/share/models/crab.xml"
    caldb       = "prod2"
    irf         = "South_50h"
    ra          =   83.63
    dec         =   22.01
    rad_sim     =   10.0
    tstart      =    0.0
    tstop       = duration
    emin        =    0.1
    emax        =  100.0
    rad_select  =    3.0

    # Get start CPU time
    tstart = time.clock()

    # Simulate events
    sim = ctools.ctobssim()
    sim["inmodel"].filename(model_name)
    sim["caldb"].string(caldb)
    sim["irf"].string(irf)
    sim["ra"].real(ra)
    sim["dec"].real(dec)
    sim["rad"].real(rad_sim)
    sim["tmin"].real(tstart)
    sim["tmax"].real(tstop)
    sim["emin"].real(emin)
    sim["emax"].real(emax)
    sim.run()

    # Select events
    select = ctools.ctselect(sim.obs())
    select["ra"].real(ra)
    select["dec"].real(dec)
    select["rad"].real(rad_select)
    select["tmin"].real(tstart)
    select["tmax"].real(tstop)
    select["emin"].real(emin)
    select["emax"].real(emax)
    select.run()

    # Get ctlike start CPU time
    tctlike = time.clock()

    # Perform maximum likelihood fitting
    like = ctools.ctlike(select.obs())
    like.run()

    # Get stop CPU time
    tstop    = time.clock()
    telapsed = tstop - tstart
    tctlike  = tstop - tctlike
	
    # Return
    return telapsed, tctlike
Exemplo n.º 7
0
def unbinned_pipeline(model_name, duration):
    """
    Unbinned analysis pipeline.
    """
    # Set script parameters
    caldb = "prod2"
    irf = "South_50h"
    ra = 83.63
    dec = 22.01
    rad_sim = 10.0
    tstart = 0.0
    tstop = duration
    emin = 0.1
    emax = 100.0
    rad_select = 3.0

    # Get start CPU time
    cpu_start = time.clock()

    # Simulate events
    sim = ctools.ctobssim()
    sim["inmodel"] = model_name
    sim["caldb"] = caldb
    sim["irf"] = irf
    sim["ra"] = ra
    sim["dec"] = dec
    sim["rad"] = rad_sim
    sim["tmin"] = tstart
    sim["tmax"] = tstop
    sim["emin"] = emin
    sim["emax"] = emax
    sim.run()

    # Select events
    select = ctools.ctselect(sim.obs())
    select["ra"] = ra
    select["dec"] = dec
    select["rad"] = rad_select
    select["tmin"] = tstart
    select["tmax"] = tstop
    select["emin"] = emin
    select["emax"] = emax
    select.run()

    # Get ctlike start CPU time
    cpu_ctlike = time.clock()

    # Perform maximum likelihood fitting
    like = ctools.ctlike(select.obs())
    like.run()

    # Get stop CPU time and compute elapsed times
    cpu_stop = time.clock()
    cpu_elapsed = cpu_stop - cpu_start
    cpu_ctlike = cpu_stop - cpu_ctlike

    # Return
    return cpu_elapsed, cpu_ctlike
Exemplo n.º 8
0
def unbinned_pipeline(model_name, duration):
    """
    Unbinned analysis pipeline.
    """
    # Set script parameters
    caldb       = "prod2"
    irf         = "South_50h"
    ra          =   83.63
    dec         =   22.01
    rad_sim     =   10.0
    tstart      =    0.0
    tstop       = duration
    emin        =    0.1
    emax        =  100.0
    rad_select  =    3.0

    # Get start CPU time
    cpu_start = time.clock()

    # Simulate events
    sim = ctools.ctobssim()
    sim["inmodel"] = model_name
    sim["caldb"]   = caldb
    sim["irf"]     = irf
    sim["ra"]      = ra
    sim["dec"]     = dec
    sim["rad"]     = rad_sim
    sim["tmin"]    = tstart
    sim["tmax"]    = tstop
    sim["emin"]    = emin
    sim["emax"]    = emax
    sim.run()

    # Select events
    select = ctools.ctselect(sim.obs())
    select["ra"]   = ra
    select["dec"]  = dec
    select["rad"]  = rad_select
    select["tmin"] = tstart
    select["tmax"] = tstop
    select["emin"] = emin
    select["emax"] = emax
    select.run()

    # Get ctlike start CPU time
    cpu_ctlike = time.clock()

    # Perform maximum likelihood fitting
    like = ctools.ctlike(select.obs())
    like.run()

    # Get stop CPU time and compute elapsed times
    cpu_stop    = time.clock()
    cpu_elapsed = cpu_stop - cpu_start
    cpu_ctlike  = cpu_stop - cpu_ctlike

    # Return
    return cpu_elapsed, cpu_ctlike
Exemplo n.º 9
0
def run_pipeline(obs,
                 ra=83.63,
                 dec=22.01,
                 rad=3.0,
                 emin=0.1,
                 emax=100.0,
                 tmin=0.0,
                 tmax=0.0,
                 debug=False):
    """
    Simulation and binned analysis pipeline

    Parameters
    ----------
    obs : `~gammalib.GObservations`
        Observation container
    ra : float, optional
        Right Ascension of Region of Interest centre (deg)
    dec : float, optional
        Declination of Region of Interest centre (deg)
    rad : float, optional
        Radius of Region of Interest (deg)
    emin : float, optional
        Minimum energy (TeV)
    emax : float, optional
        Maximum energy (TeV)
    tmin : float, optional
        Start time (s)
    tmax : float, optional
        Stop time (s)
    debug : bool, optional
        Debug function
    """
    # Simulate events
    sim = ctools.ctobssim(obs)
    sim['debug'] = debug
    sim.run()

    # Select events
    select = ctools.ctselect(sim.obs())
    select['ra'] = ra
    select['dec'] = dec
    select['rad'] = rad
    select['emin'] = emin
    select['emax'] = emax
    select['tmin'] = tmin
    select['tmax'] = tmax
    select['debug'] = debug
    select.run()

    # Perform maximum likelihood fitting
    like = ctools.ctlike(select.obs())
    like['debug'] = True  # Switch this always on for results in console
    like.run()

    # Return
    return
Exemplo n.º 10
0
def run_pipeline(obs, ra=83.63, dec=22.01, rad=3.0,
                 emin=0.1, emax=100.0,
                 tmin=0.0, tmax=0.0,
                 model="${CTOOLS}/share/models/crab.xml",
                 caldb="prod2", irf="South_50h",
                 debug=False):
    """
    Simulation and unbinned analysis pipeline.

    Keywords:
     ra    - RA of cube centre [deg] (default: 83.63)
     dec   - DEC of cube centre [deg] (default: 22.01)
     rad   - Selection radius [deg] (default: 3.0)
     emin  - Minimum energy of cube [TeV] (default: 0.1)
     emax  - Maximum energy of cube [TeV] (default: 100.0)
     tmin  - Start time [MET] (default: 0.0)
     tmax  - Stop time [MET] (default: 0.0)
     model - Model Xml file
     caldb - Calibration database path (default: "dummy")
     irf   - Instrument response function (default: cta_dummy_irf)
     debug - Enable debugging (default: False)
    """
    # Get model

    # Simulate events
    sim = ctools.ctobssim(obs)
    sim["debug"]     = debug
    sim["outevents"] = "obs.xml"
    sim.execute()

    # Select events
    select = ctools.ctselect()
    select["inobs"]  = "obs.xml"
    select["outobs"] = "obs_selected.xml"
    select["ra"]     = ra
    select["dec"]    = dec
    select["rad"]    = rad
    select["emin"]   = emin
    select["emax"]   = emax
    select["tmin"]   = tmin
    select["tmax"]   = tmax
    select["debug"]  = debug
    select.execute()

    # Perform maximum likelihood fitting
    like = ctools.ctlike()
    like["inobs"]    = "obs_selected.xml"
    like["inmodel"]  = model
    like["outmodel"] = "fit_results.xml"
    like["caldb"]    = caldb
    like["irf"]      = irf
    like["debug"]    = True # Switch this always on for results in console
    like.execute()

    # Return
    return
Exemplo n.º 11
0
def create_spectrum(datadir):
    """
    Simulate events and generate a source spectrum

    Returns
    -------
    datadir : str
        Data directory
    """
    # Set script parameters
    model_name = datadir + '/crab.xml'
    caldb = 'prod2'
    irf = 'South_0.5h'
    ra = 83.63
    dec = 22.01
    rad_sim = 3.0
    tstart = 0.0
    tstop = 1800.0
    emin = 0.1
    emax = 100.0
    enumbins = 10

    # Simulate events
    sim = ctools.ctobssim()
    sim['inmodel'] = model_name
    sim['caldb'] = caldb
    sim['irf'] = irf
    sim['ra'] = ra
    sim['dec'] = dec
    sim['rad'] = rad_sim
    sim['tmin'] = tstart
    sim['tmax'] = tstop
    sim['emin'] = emin
    sim['emax'] = emax
    sim.run()

    # Create spectrum
    spec = cscripts.csspec(sim.obs())
    spec['srcname'] = 'Crab'
    spec['outfile'] = 'example_spectrum.fits'
    spec['expcube'] = 'NONE'
    spec['psfcube'] = 'NONE'
    spec['bkgcube'] = 'NONE'
    spec['edisp'] = False
    spec['emin'] = emin
    spec['emax'] = emax
    spec['enumbins'] = enumbins
    spec['ebinalg'] = 'LOG'
    spec.run()
    spec.save()

    # Get copy of spectrum
    spectrum = spec.spectrum().copy()

    # Return
    return spectrum
Exemplo n.º 12
0
    def test_unbinned_fits(self):
        """
        Test unbinned pipeline with FITS file saving
        """
        # Set script parameters
        events_name          = 'events.fits'
        selected_events_name = 'selected_events.fits'
        result_name          = 'results.xml'
        ra                   =   83.63
        dec                  =   22.01
        rad_sim              =   10.0
        rad_select           =    3.0
        tstart               =    0.0
        tstop                =  300.0
        emin                 =    0.1
        emax                 =  100.0

        # Simulate events
        sim = ctools.ctobssim()
        sim['inmodel']   = self._model
        sim['outevents'] = events_name
        sim['caldb']     = self._caldb
        sim['irf']       = self._irf
        sim['ra']        = ra
        sim['dec']       = dec
        sim['rad']       = rad_sim
        sim['tmin']      = tstart
        sim['tmax']      = tstop
        sim['emin']      = emin
        sim['emax']      = emax
        sim.execute()

        # Select events
        select = ctools.ctselect()
        select['inobs']  = events_name
        select['outobs'] = selected_events_name
        select['ra']     = ra
        select['dec']    = dec
        select['rad']    = rad_select
        select['tmin']   = tstart
        select['tmax']   = tstop
        select['emin']   = emin
        select['emax']   = emax
        select.execute()

        # Perform maximum likelihood fitting
        like = ctools.ctlike()
        like['inobs']    = selected_events_name
        like['inmodel']  = self._model
        like['outmodel'] = result_name
        like['caldb']    = self._caldb
        like['irf']      = self._irf
        like.execute()

        # Return
        return
Exemplo n.º 13
0
    def test_unbinned_fits(self):
        """
        Test unbinned pipeline with FITS file saving
        """
        # Set script parameters
        events_name          = 'events.fits'
        selected_events_name = 'selected_events.fits'
        result_name          = 'results.xml'
        ra                   =   83.63
        dec                  =   22.01
        rad_sim              =    3.0
        rad_select           =    2.0
        tstart               =    0.0
        tstop                =  300.0
        emin                 =    1.0
        emax                 =  100.0

        # Simulate events
        sim = ctools.ctobssim()
        sim['inmodel']   = self._model
        sim['outevents'] = events_name
        sim['caldb']     = self._caldb
        sim['irf']       = self._irf
        sim['ra']        = ra
        sim['dec']       = dec
        sim['rad']       = rad_sim
        sim['tmin']      = tstart
        sim['tmax']      = tstop
        sim['emin']      = emin
        sim['emax']      = emax
        sim.execute()

        # Select events
        select = ctools.ctselect()
        select['inobs']  = events_name
        select['outobs'] = selected_events_name
        select['ra']     = ra
        select['dec']    = dec
        select['rad']    = rad_select
        select['tmin']   = tstart
        select['tmax']   = tstop
        select['emin']   = emin
        select['emax']   = emax
        select.execute()

        # Perform maximum likelihood fitting
        like = ctools.ctlike()
        like['inobs']    = selected_events_name
        like['inmodel']  = self._model
        like['outmodel'] = result_name
        like['caldb']    = self._caldb
        like['irf']      = self._irf
        like.execute()

        # Return
        return
Exemplo n.º 14
0
 def __init__(self, outdir='.'):
     ''' init function
     Parameters
     ---------
     outdir : place where the fits files and the log file will be kept
     '''
     super(CTA_ctools_sim, self).__init__()
     self.sim = ctools.ctobssim()
     self.outfiles = []
     Common.CTA_ctools_common.__init__(self, outdir=outdir)
Exemplo n.º 15
0
 def __init__(self,outdir='.'):
     ''' init function
     Parameters
     ---------
     outdir : place where the fits files and the log file will be kept
     '''
     super(CTA_ctools_sim,self).__init__()
     self.sim = ctools.ctobssim()
     self.outfiles = []
     Common.CTA_ctools_common.__init__(self,outdir=outdir)
Exemplo n.º 16
0
def make_spectrum():
    """
    Make a spectrum.
    """
    # Set script parameters
    model_name  = "${CTOOLS}/share/models/crab.xml"
    caldb       = "prod2"
    irf         = "South_50h"
    ra          =   83.63
    dec         =   22.01
    rad_sim     =    3.0
    tstart      =    0.0
    tstop       = 1800.0
    emin        =    0.1
    emax        =  100.0
    enumbins    =     10

    # Simulate events
    sim = ctools.ctobssim()
    sim["inmodel"] = model_name
    sim["caldb"]   = caldb
    sim["irf"]     = irf
    sim["ra"]      = ra
    sim["dec"]     = dec
    sim["rad"]     = rad_sim
    sim["tmin"]    = tstart
    sim["tmax"]    = tstop
    sim["emin"]    = emin
    sim["emax"]    = emax
    sim.run()

    # Generate an energy binning
    #e_min   = gammalib.GEnergy(emin, "TeV")
    #e_max   = gammalib.GEnergy(emax, "TeV")
    #ebounds = gammalib.GEbounds(10, e_min, e_max)

    # Setup csspec run
    spec = cscripts.csspec(sim.obs())
    spec["srcname"]  = "Crab"
    spec["outfile"]  = "spectrum.fits"
    spec["expcube"]  = "NONE"
    spec["psfcube"]  = "NONE"
    spec["bkgcube"]  = "NONE"
    spec["edisp"]    = False
    spec["emin"]     = emin
    spec["emax"]     = emax
    spec["enumbins"] = enumbins
    spec["binned"]   = False
    spec.run()

    # Get copy of spectrum
    spectrum = spec.spectrum().copy()

    # Return
    return spectrum
Exemplo n.º 17
0
def run_pipeline(obs, emin=0.1, emax=100.0,
                 enumbins=20, nxpix=200, nypix=200, binsz=0.02,
                 coordsys="CEL", proj="CAR", debug=False):
    """
    Simulation and binned analysis pipeline.

    Keywords:
     emin     - Minimum energy of cube [TeV] (default: 0.1)
     emax     - Maximum energy of cube [TeV] (default: 100.0)
     enumbins - Number of energy bins in cube (default: 20)
     nxpix    - Number of RA pixels in cube (default: 200)
     nypix    - Number of DEC pixels in cube (default: 200)
     binsz    - Spatial cube bin size [deg] (default: 0.02)
     coordsys - Cube coordinate system (CEL or GAL)
     proj     - Cube World Coordinate System (WCS) projection
     debug    - Enable debugging (default: False)
    """
    # Simulate events
    sim = ctools.ctobssim(obs)
    sim["debug"] = debug
    sim.run()

    # Bin events by looping over all observations in the container
    obs = gammalib.GObservations()
    obs.models(sim.obs().models())
    for run in sim.obs():

        # Create container with a single observation
        container = gammalib.GObservations()
        container.append(run)

        # Bin events for that observation
        bin = ctools.ctbin(container)
        bin["ebinalg"]  = "LOG"
        bin["emin"]     = emin
        bin["emax"]     = emax
        bin["enumbins"] = enumbins
        bin["nxpix"]    = nxpix
        bin["nypix"]    = nypix
        bin["binsz"]    = binsz
        bin["coordsys"] = coordsys
        bin["usepnt"]   = True
        bin["proj"]     = proj
        bin.run()

        # Append result to observations
        obs.extend(bin.obs())

    # Perform maximum likelihood fitting
    like = ctools.ctlike(obs)
    like["debug"] = True # Switch this always on for results in console
    like.run()

    # Return
    return
Exemplo n.º 18
0
def run_obssim():
    sim = ctools.ctobssim()
    sim['inmodel'] = 'model.xml'
    sim['inobs'] = 'inobs1.xml'
    sim['outevents'] = 'outevents1.xml'
    sim['prefix'] = 'events1_'
    sim['emin'] = 0.5
    sim['emax'] = 70
    sim['rad'] = 3
    sim['logfile'] = 'make1.log'
    sim.execute()
Exemplo n.º 19
0
def run_obssim():
    sim = ctools.ctobssim()
    sim['inmodel'] = 'model.xml'
    sim['inobs'] = 'inobs1.xml'
    sim['outevents'] = 'outevents1.xml'
    sim['prefix'] = 'events1_'
    sim['emin'] = 0.5
    sim['emax'] = 70
    sim['rad'] = 3
    sim['logfile'] = 'make1.log'
    sim.execute()
Exemplo n.º 20
0
def run_obssim():
    sim = ctools.ctobssim()
    sim['inmodel'] = 'model.xml'
    sim['inobs'] = 'observation_definition.xml'
    sim['outevents'] = 'outevents.xml'
    sim['prefix'] = 'hess_events_'
    sim['emin'] = 0.5
    sim['emax'] = 70
    sim['rad'] = 3
    sim['logfile'] = 'simulation_output.log'
    sim.execute()
Exemplo n.º 21
0
def run_obssim():
    import ctools
    sim = ctools.ctobssim()
    sim['inmodel'] = 'model.xml'
    sim['inobs'] = 'inobs2.xml'
    sim['outevents'] = 'outevents2.xml'
    sim['prefix'] = 'events2_'
    sim['emin'] = 0.463794
    sim['emax'] = 80
    sim['rad'] = 5
    sim['logfile'] = 'make2.log'
    sim.execute()
Exemplo n.º 22
0
 def __init__(self, workdir='.', outdir='.'):
     ''' init function
     Parameters
     ---------
     workdir : place where fits file will be temporarily stored and where the log file will be kept
     outdir : place where the fits file 
     '''
     super(CTA_ctools_sim, self).__init__()
     self.sim = ctools.ctobssim()
     self.outfiles = []
     self.workfiles = []
     Common.CTA_ctools_common.__init__(self, workdir=workdir, outdir=outdir)
Exemplo n.º 23
0
def run_obssim():
    import ctools

    sim = ctools.ctobssim()
    sim["inmodel"] = "model.xml"
    sim["inobs"] = "inobs2.xml"
    sim["outevents"] = "outevents2.xml"
    sim["prefix"] = "events2_"
    sim["emin"] = 0.463794
    sim["emax"] = 80
    sim["rad"] = 5
    sim["logfile"] = "make2.log"
    sim.execute()
Exemplo n.º 24
0
 def ctobssim(self, xml, RA, DEC):
     sim = ctools.ctobssim()
     sim['inmodel'] = xml
     sim['outevents'] = 'events.fits'
     sim['caldb'] = 'prod2'
     sim['irf'] = 'South_0.5h'
     sim['ra'] = RA
     sim['dec'] = DEC
     sim['rad'] = 5.0
     sim['tmin'] = '2020-01-01T00:00:00'
     sim['tmax'] = '2020-01-01T01:00:00'
     sim['emin'] = 0.03
     sim['emax'] = 150.0
     sim.execute()
Exemplo n.º 25
0
def sim(src="cCrab"):  #bkg, Crabbkg
    "Simulates acceptance ccube and model cube"
    print 30 * "-" + "\n", "Simulating", src + "\n" + 30 * "-"
    #Simulate observation
    sim = ctools.ctobssim()
    sim["inmodel"] = modeldir + src + ".xml"
    sim["outevents"] = outdir + "events_" + src + ".fits"
    sim["caldb"] = caldb
    sim["irf"] = irf
    sim["ra"] = ra
    sim["dec"] = dec
    sim["rad"] = 10.0
    sim["tmin"] = 0.0
    sim["tmax"] = tsim
    sim["emin"] = emin
    sim["emax"] = emax
    sim["edisp"] = False
    sim.execute()

    #Bin data into a cube
    ctbin = ctools.ctbin()
    ctbin["inobs"] = outdir + "events_" + src + ".fits"
    ctbin["outcube"] = outdir + "ccube_" + src + ".fits"
    ctbin["ebinalg"] = "LOG"
    ctbin["emin"] = emin
    ctbin["emax"] = emax
    ctbin["enumbins"] = enumbins
    ctbin["nxpix"] = npix
    ctbin["nypix"] = npix
    ctbin["binsz"] = binsz
    ctbin["coordsys"] = "CEL"
    ctbin["xref"] = ra
    ctbin["yref"] = dec
    ctbin["proj"] = "AIT"
    ctbin.execute()

    #Create model cube
    ctmodel = ctools.ctmodel()
    ctmodel["inobs"] = outdir + "events_" + src + ".fits"
    ctmodel["inmodel"] = modeldir + src + ".xml"
    ctmodel["incube"] = outdir + "ccube_" + src + ".fits"
    ctmodel["caldb"] = "prod2"
    ctmodel["caldb"] = caldb
    ctmodel["irf"] = irf
    ctmodel["outcube"] = outdir + "mcube_" + src + ".fits"
    ctmodel["edisp"] = False
    ctmodel.execute()
Exemplo n.º 26
0
def simulate_data():
    sim = ctools.ctobssim()
    sim["inmodel"].filename("${CTOOLS}/share/models/crab.xml")
    sim["outevents"].filename("events.fits")
    sim["caldb"].string("prod2")
    sim["irf"].string("South_50h")
    sim["ra"].real(83.63)
    sim["dec"].real(22.01)
    sim["rad"].real(5.0)
    sim["tmin"].real(0.0)
    sim["tmax"].real(1800.0)
    sim["emin"].real(0.1)
    sim["emax"].real(100.0)
    sim.execute()

    obs = sim.obs()
    return obs
Exemplo n.º 27
0
    def test_unbinned_mem(self):
        """
        Test unbinned in-memory pipeline
        """
        # Set script parameters
        ra         =   83.63
        dec        =   22.01
        rad_sim    =   10.0
        rad_select =    3.0
        tstart     =    0.0
        tstop      =  300.0
        emin       =    0.1
        emax       =  100.0

        # Simulate events
        sim = ctools.ctobssim()
        sim['inmodel'] = self._model
        sim['caldb']   = self._caldb
        sim['irf']     = self._irf
        sim['ra']      = ra
        sim['dec']     = dec
        sim['rad']     = rad_sim
        sim['tmin']    = tstart
        sim['tmax']    = tstop
        sim['emin']    = emin
        sim['emax']    = emax
        sim.run()

        # Select events
        select = ctools.ctselect(sim.obs())
        select['ra']   = ra
        select['dec']  = dec
        select['rad']  = rad_select
        select['tmin'] = tstart
        select['tmax'] = tstop
        select['emin'] = emin
        select['emax'] = emax
        select.run()

        # Perform maximum likelihood fitting
        like = ctools.ctlike(select.obs())
        like.run()

        # Return
        return
Exemplo n.º 28
0
    def test_unbinned_mem(self):
        """
        Test unbinned in-memory pipeline
        """
        # Set script parameters
        ra         =   83.63
        dec        =   22.01
        rad_sim    =    3.0
        rad_select =    2.0
        tstart     =    0.0
        tstop      =  300.0
        emin       =    1.0
        emax       =  100.0

        # Simulate events
        sim = ctools.ctobssim()
        sim['inmodel'] = self._model
        sim['caldb']   = self._caldb
        sim['irf']     = self._irf
        sim['ra']      = ra
        sim['dec']     = dec
        sim['rad']     = rad_sim
        sim['tmin']    = tstart
        sim['tmax']    = tstop
        sim['emin']    = emin
        sim['emax']    = emax
        sim.run()

        # Select events
        select = ctools.ctselect(sim.obs())
        select['ra']   = ra
        select['dec']  = dec
        select['rad']  = rad_select
        select['tmin'] = tstart
        select['tmax'] = tstop
        select['emin'] = emin
        select['emax'] = emax
        select.run()

        # Perform maximum likelihood fitting
        like = ctools.ctlike(select.obs())
        like.run()

        # Return
        return
Exemplo n.º 29
0
    def test_functional(self):
        """
        Test ctobssim functionnality.
        """
        # Set-up ctobssim
        sim = ctools.ctobssim()
        sim["inmodel"].filename(self.model_name)
        sim["outevents"].filename("events.fits")
        sim["caldb"].string(self.caldb)
        sim["irf"].string(self.irf)
        sim["ra"].real(83.63)
        sim["dec"].real(22.01)
        sim["rad"].real(10.0)
        sim["tmin"].real(0.0)
        sim["tmax"].real(1800.0)
        sim["emin"].real(0.1)
        sim["emax"].real(100.0)
        
        # Run tool
        self.test_try("Run ctobssim")
        try:
            sim.run()
            self.test_try_success()
        except:
            self.test_try_failure("Exception occured in ctobssim.")

        # Retrieve observation and check content
        obs = gammalib.GCTAObservation(sim.obs()[0])
        pnt = obs.pointing()
        evt = obs.events()
        self.test_assert(obs.instrument() == "CTA", "Observation not a CTA observation")
        self.test_value(sim.obs().size(), 1, "There is not a single observation")
        self.test_value(obs.ontime(), 1800.0, 1.0e-6, "Ontime is not 1800 sec")
        self.test_value(obs.livetime(), 1710.0, 1.0e-6, "Livetime is not 1710 sec")
        self.test_value(pnt.dir().ra_deg(), 83.63, 1.0e-6, "ROI Right Ascension is not 83.63 deg")
        self.test_value(pnt.dir().dec_deg(), 22.01, 1.0e-6, "ROI Declination is not 22.01 deg")
        self.test_value(evt.size(), 4134, "Number of events is not 4134")
        
        # Save events
        self.test_try("Save events")
        try:
            sim.save()
            self.test_try_success()
        except:
            self.test_try_failure("Exception occured in saving events.")
Exemplo n.º 30
0
def simulate_background(input_yaml, jobs_yaml, count):
    config_in = yaml.safe_load(open(input_yaml))
    jobs_config = yaml.safe_load(open(jobs_yaml))

    try:
        ctools_pipe_path = create_path(jobs_config['exe']['software_path'])
    except KeyError:
        ctools_pipe_path = "."

    # find proper IRF name
    irf = IRFPicker(config_in, ctools_pipe_path)
    name_irf = irf.irf_pick()

    if irf.prod_number == "3b" and irf.prod_version == 0:
        caldb = "prod3b"
    else:
        caldb = f'prod{irf.prod_number}-v{irf.prod_version}'

    out_path = create_path(
        f"{jobs_config['exe']['path']}/back_sim/{irf.prod_number}_{irf.prod_version}_{name_irf}"
    )

    # simulation details
    sim_details = config_in['sim']

    seed = int(count) * 10

    # do the simulation
    sim = ctools.ctobssim()
    sim['inmodel'] = f"{ctools_pipe_path}/models/bkg_only_model.xml"
    sim['caldb'] = caldb
    sim['irf'] = name_irf
    sim['ra'] = 0
    sim['dec'] = 0
    sim['rad'] = sim_details['radius']
    sim['tmin'] = u.Quantity(sim_details['time']['t_min']).to_value(u.s)
    sim['tmax'] = u.Quantity(sim_details['time']['t_max']).to_value(u.s)
    sim['emin'] = u.Quantity(sim_details['energy']['e_min']).to_value(u.TeV)
    sim['emax'] = u.Quantity(sim_details['energy']['e_max']).to_value(u.TeV)
    sim['outevents'] = f"{out_path}/background_z-{irf.zenith}_site-{irf.irf_site}_{str(count).zfill(2)}_seed{seed}.fits"
    sim['seed'] = seed
    sim.execute()
Exemplo n.º 31
0
def run_multi_ctobssim(RA, DEC, TSTART, DURATION, DEADC, EMIN, EMAX, RAD, IRF,
                       CALDB, outfile, nobs):
    """TODO: document what it does"""

    observations = gammalib.GObservations()

    # Automatically generate a number of nobs
    for i in xrange(nobs):
        obs = set(RA, DEC, TSTART, DURATION, DEADC, EMIN, EMAX, RAD, IRF,
                  CALDB)
        obs.id(str(i))
        observations.append(obs)

    observations.models('$CTOOLS/share/models/crab.xml')

    ctobssim = ctools.ctobssim(observations)
    ctobssim.logFileOpen()
    ctobssim['outfile'].filename(outfile)

    ctobssim.execute()
Exemplo n.º 32
0
def run_multi_ctobssim(RA, DEC, TSTART, DURATION,
                       DEADC, EMIN, EMAX, RAD, IRF, CALDB, outfile, nobs):
    """TODO: document what it does"""

    observations = gammalib.GObservations()

    # Automatically generate a number of nobs
    for i in xrange(nobs):
        obs = set(RA, DEC, TSTART, DURATION, DEADC, EMIN, EMAX,
                  RAD, IRF, CALDB)
        obs.id(str(i))
        observations.append(obs)

    observations.models('$CTOOLS/share/models/crab.xml')

    ctobssim = ctools.ctobssim(observations)
    ctobssim.logFileOpen()
    ctobssim['outfile'].filename(outfile)

    ctobssim.execute()
Exemplo n.º 33
0
 def simulation_run(self,
                    model_file,
                    events_file,
                    ra=None,
                    dec=None,
                    time=[0, 1800],
                    energy=[None, None],
                    log_file='ctobssim.log',
                    force=False,
                    save=False):
     sim = ctools.ctobssim()
     sim["inmodel"] = model_file
     sim["outevents"] = events_file
     sim["seed"] = self.seed
     sim["ra"] = ra if ra else self.ra
     sim["dec"] = dec if dec else self.dec
     sim["rad"] = self.rad
     sim["tmin"] = float(time[0])
     sim["tmax"] = float(time[1])
     sim["emin"] = energy[0] if energy[0] else self.energy_min
     sim["emax"] = energy[1] if energy[1] else self.energy_max
     sim["caldb"] = self.caldb
     sim["irf"] = self.irf
     sim["logfile"] = log_file
     sim["nthreads"] = self.nthreads
     if force or not os.path.isfile(events_file):
         sim.logFileOpen()
         sim.run()
     else:
         container = gammalib.GObservations()
         gcta_obs = gammalib.GCTAObservation(events_file)
         container.append(gcta_obs)
         sim.obs(container)
         sim.obs().models(gammalib.GModels(model_file))
     saved = False
     if (save and force) or (save and not os.path.isfile(events_file)):
         sim.save()
         saved = True
         logger.info("Events file '{}' saved. time [{}-{}]".format(
             sim["outevents"].value(), time[0], time[1]))
     return sim
Exemplo n.º 34
0
def run_pipeline(obs, ra=83.63, dec=22.01, rad=3.0, \
                 emin=0.1, emax=100.0, \
                 tmin=0.0, tmax=0.0, \
                 debug=False):
    """
    Simulation and unbinned analysis pipeline.

    Keywords:
     ra    - RA of cube centre [deg] (default: 83.63)
     dec   - DEC of cube centre [deg] (default: 22.01)
     rad   - Selection radius [deg] (default: 3.0)
     emin  - Minimum energy of cube [TeV] (default: 0.1)
     emax  - Maximum energy of cube [TeV] (default: 100.0)
     tmin  - Start time [MET] (default: 0.0)
     tmax  - Stop time [MET] (default: 0.0)
     debug - Enable debugging (default: False)
    """
    # Simulate events
    sim = ctools.ctobssim(obs)
    sim["debug"].boolean(debug)
    sim.run()

    # Select events
    select = ctools.ctselect(sim.obs())
    select["ra"].real(ra)
    select["dec"].real(dec)
    select["rad"].real(rad)
    select["emin"].real(emin)
    select["emax"].real(emax)
    select["tmin"].real(tmin)
    select["tmax"].real(tmax)
    select["debug"].boolean(debug)
    select.run()

    # Perform maximum likelihood fitting
    like = ctools.ctlike(select.obs())
    like["debug"].boolean(True) # Switch this always on for results in console
    like.run()
	
    # Return
    return
Exemplo n.º 35
0
def create_event_list():
    """
    Create event list
    """
    # Simulate events
    sim = ctools.ctobssim()
    sim['inmodel'] = 'test/data/crab.xml'
    sim['outevents'] = 'test/data/crab_events.fits'
    sim['caldb'] = 'prod2'
    sim['irf'] = 'South_0.5h'
    sim['edisp'] = False
    sim['ra'] = 83.63
    sim['dec'] = 22.51
    sim['rad'] = 2.0
    sim['tmin'] = '2020-01-01T00:00:00'
    sim['tmax'] = '2020-01-01T00:05:00'
    sim['emin'] = 1.0
    sim['emax'] = 100.0
    sim.execute()

    # Return
    return
Exemplo n.º 36
0
def simulation_run(output_events_file, ra, dec, tmax=1800, seed=1, force=0):
    log_file = os.path.join(os.path.dirname(output_events_file),
                            "ctobssim.log")
    if not os.path.isfile(output_events_file) or force == 1:
        sim = ctools.ctobssim()
        sim.clear()
        sim["seed"] = seed
        sim["ra"] = ra
        sim["dec"] = dec
        sim["rad"] = 5.0
        sim["tmin"] = 0
        sim["tmax"] = tmax
        sim["emin"] = ENERGY["min"]
        sim["emax"] = ENERGY["max"]
        sim["caldb"] = "prod2"
        sim["irf"] = "South_0.5h"
        sim["inmodel"] = OBJ["model"]
        sim["outevents"] = output_events_file
        sim["logfile"] = log_file
        sim.logFileOpen()
        sim.run()
        sim.save()
        sys.stderr.write("File '%s' created.\n" % output_events_file)
    return True
Exemplo n.º 37
0
    def run_sim_obs(self, obsID=None, seed=None):
        """
        Run all the observations at once
        
        Parameters
        ----------
        - obsID (str or str list): list of runs to be observed
        - seed (int): the seed used for simulations of observations
        """

        #----- Create the output directory if needed
        if not os.path.exists(self.output_dir): os.mkdir(self.output_dir)

        #----- Get the obs ID to run
        obsID = self._check_obsID(obsID)
        if not self.silent: print('----- ObsID to be observed: ' + str(obsID))
        self.obs_setup.match_bkg_id()  # make sure Bkg are unique

        #----- Make sure the cluster FoV matches all requested observations
        self._match_cluster_to_pointing()

        #----- Make cluster templates
        self._make_model(prefix='Sim_Model', includeIC=True)
        self.cluster.save_param()
        os.rename(self.cluster.output_dir + '/parameters.txt',
                  self.cluster.output_dir + '/Sim_Model_Cluster_param.txt')
        os.rename(self.cluster.output_dir + '/parameters.pkl',
                  self.cluster.output_dir + '/Sim_Model_Cluster_param.pkl')

        #----- Make observation files
        self.obs_setup.write_pnt(self.output_dir + '/Sim_Pnt.def', obsid=obsID)
        self.obs_setup.run_csobsdef(self.output_dir + '/Sim_Pnt.def',
                                    self.output_dir + '/Sim_ObsDef.xml')

        #----- Get the seed for reapeatable simu
        if seed is None: seed = randint(1, 1e6)

        #----- Run the observation
        obssim = ctools.ctobssim()
        obssim['inobs'] = self.output_dir + '/Sim_ObsDef.xml'
        obssim['inmodel'] = self.output_dir + '/Sim_Model_Unstack.xml'
        #obssim['caldb']     =
        #obssim['irf']       =
        obssim['edisp'] = self.spec_edisp
        obssim['outevents'] = self.output_dir + '/Events.xml'
        obssim['prefix'] = self.output_dir + '/TmpEvents'
        obssim['startindex'] = 1
        obssim['seed'] = seed
        #obssim['ra']        =
        #obssim['dec']       =
        #obssim['rad']       =
        #obssim['tmin']      =
        #obssim['tmax']      =
        #obssim['mjdref']    =
        #obssim['emin']      =
        #obssim['emax']      =
        #obssim['deadc']     =
        obssim['maxrate'] = 1e6
        obssim['logfile'] = self.output_dir + '/Events_log.txt'
        obssim['chatter'] = 2
        obssim.logFileOpen()
        obssim.execute()
        obssim.logFileClose()

        if not self.silent:
            print('------- Simulation log -------')
            print(obssim)
            print('')

        self._correct_eventfile_names(self.output_dir + '/Events.xml',
                                      prefix='Events')
Exemplo n.º 38
0
    def run_pipeline(self, debug=False, seed=0):
        """
		Test unbinned pipeline with FITS file saving
		"""
        # Set script parameters
        events_name = 'events.fits'
        cubefile_name = ''
        selected_events_name = 'selected_events.fits'
        result_name = 'results.xml'

        if self.simfilename and self.eventfilename:
            print('error')
            exit(10)

        ############ Simulate events

        if self.simfilename:

            # Setup simulation model
            if self.simfilename:
                self.obs.models(gammalib.GModels(self.simfilename))

            if self.runconf.WorkInMemory == 0:
                print('# Generate simulated event list on disk')
                # Simulate events on disk
                sim = ctools.ctobssim(self.obs)
                sim['outevents'] = events_name
                sim['debug'] = debug
                sim['seed'] = seed
                sim.execute()
                self.eventfilename = events_name

            if self.runconf.WorkInMemory == 1:
                print('# Generate simulated event list in memory')
                # Simulate events on memory
                sim = ctools.ctobssim(self.obs)
                sim['debug'] = debug
                sim['seed'] = seed
                sim.run()

        #if self.runfilename is not present
        #   import into DB if any
        #	exit(0)

        ############ Get events from DB

        if not self.simfilename and not self.eventfilename:
            #load from DB
            print('# Load event list from DB')

            #write selected_events_name
            if self.runconf.timeref_timesys == 'tt':
                tstart_tt = self.runconf.tmin
                tstop_tt = self.runconf.tmax
            if self.runconf.timeref_timesys == 'mjd':
                tstart_tt = Utility.convert_mjd_to_tt(self.runconf.tmin)
                tstop_tt = Utility.convert_mjd_to_tt(self.runconf.tmax)

            #tstart_tt = self.runconf.tmin
            #tstop_tt = self.runconf.tmax
            observationid = self.obsconf.id
            print("tstart" + str(tstart_tt))
            print("tstop" + str(tstart_tt))
            print(self.runconf.timeref_timesys)

            conf_dictionary = get_path_conf()

            path_base_fits = conf_dictionary['path_base_fits']
            tref_mjd = self.runconf.timeref

            if self.runconf.roi_frame == 'fk5':
                obs_ra = self.obsconf.roi_ra
                obs_dec = self.obsconf.roi_dec
            else:
                exit(10)

            emin = self.runconf.emin
            emax = self.runconf.emax
            fov = self.obsconf.roi_fov
            instrumentname = self.obsconf.instrument

            datarepository_dictionary = get_data_repository(instrumentname)
            datarepositoryid = datarepository_dictionary['datarepositoryid']

            print("before write fits")
            print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])
            events_name = write_fits(tstart_tt, tstop_tt, observationid,
                                     datarepositoryid, path_base_fits,
                                     tref_mjd, obs_ra, obs_dec, emin, emax,
                                     180, instrumentname)
            print("after write fits")
            print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])

            print("fitsname" + events_name)
            if self.runconf.WorkInMemory == 2:
                print('# Load event list from disk')
                #Load events from fits file on memory
                sim = ctools.ctobssim(self.obs)
                events = sim.obs()[0].events()
                for event in events:
                    print(event)

                #events.load(events_name)
                events.load(selected_events_name)

        ############ Select events

        if self.runconf.WorkInMemory == 1:
            print('# Select event list in memory')
            select = ctools.ctselect(sim.obs())

        #using file
        if self.runconf.WorkInMemory == 0:
            print('# Select event list from disk')
            select = ctools.ctselect()
            select['inobs'] = events_name
            select['outobs'] = selected_events_name

        select['ra'] = self.runconf.roi_ra
        select['dec'] = self.runconf.roi_dec
        select['rad'] = self.runconf.roi_ringrad
        select['tmin'] = 'MJD ' + str(self.runconf.tmin)
        #select['tmin']   = 'INDEF'
        #select['tmin'] = 0.0
        #select['tmin'] = 'MJD 55197.0007660185'
        #select['tmax']   = self.runconf.tmax
        #select['tmax']   = 'INDEF'
        select['tmax'] = 'MJD ' + str(self.runconf.tmax)
        #select['tmax']   = 55197.0007660185 + self.runconf.tmax / 86400.
        select['emin'] = self.runconf.emin
        select['emax'] = self.runconf.emax

        if self.runconf.WorkInMemory == 1:
            select.run()

        if self.runconf.WorkInMemory == 0:
            select.execute()

        print("after select events")
        print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])

        #print(self.runconf.roi_ra)
        #print(self.runconf.roi_dec)
        #print(self.obsconf.obs_fov)
        #print(self.runconf.tmin)
        #print(self.runconf.tmax)
        #print(self.runconf.emin)
        #print(self.runconf.emax)
        #print(select.obs()[0])
        print("select --")
        print(select)
        print("select --")

        if self.runconf.WorkInMemory == 2:
            print('# Load event list from disk')
            #Load events from fits file on memory
            sim = ctools.ctobssim(self.obs)
            events = sim.obs()[0].events()
            for event in events:
                print(event)

            #events.load(events_name)
            events.load(selected_events_name)

        print('Event list generated ----------------------')
        if self.runconf.WorkInMemory == 0:
            print(self.obs)
            print("obs --")
            print(self.obs[0])
            print("obs 0 --")
            localobs = self.obs

        if self.runconf.WorkInMemory == 1:
            print(select.obs())
            print(select.obs()[0])
            localobs = select.obs()

        for run in localobs:

            print('run ---' + selected_events_name)
            print("before ctbin")
            print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])

            print(run)

            # Create container with a single observation
            container = gammalib.GObservations()
            container.append(run)

            if self.runconf.MakeCtsMap == 1:
                cubefile_name = 'cube.fits'
                #event file in memory or read from fits file on memory
                if self.runconf.WorkInMemory == 1:
                    bin = ctools.ctbin(container)

                if self.runconf.WorkInMemory == 0:
                    #event file on disk
                    bin = ctools.ctbin()
                    bin['inobs'] = selected_events_name

                #make binned map on disk
                bin['outcube'] = cubefile_name

                #common configs
                bin['ebinalg'] = self.runconf.cts_ebinalg
                bin['emin'] = self.runconf.emin
                bin['emax'] = self.runconf.emax
                bin['enumbins'] = self.runconf.cts_enumbins
                bin['nxpix'] = ceil(self.runconf.roi_ringrad * 2 /
                                    self.runconf.cts_binsz)
                bin['nypix'] = ceil(self.runconf.roi_ringrad * 2 /
                                    self.runconf.cts_binsz)
                bin['binsz'] = self.runconf.cts_binsz
                bin['coordsys'] = self.runconf.cts_coordsys
                bin['usepnt'] = self.runconf.cts_usepnt  # Use pointing for map centre
                bin['proj'] = self.runconf.cts_proj
                bin['xref'] = self.runconf.roi_ra
                bin['yref'] = self.runconf.roi_dec
                print("-- bin")
                print(bin)
                #make binned map on disk
                if self.runconf.WorkInMemory == 0:
                    bin.execute()
                    # Set observation ID if make binned map on disk
                    bin.obs()[0].id(cubefile_name)
                    bin.obs()[0].eventfile(cubefile_name)
                    os.system('mkdir -p ' + self.runconf.resdir)
                    shutil.copy(
                        './cube.fits', self.runconf.resdir + '/' +
                        self.runconf.runprefix + '_cube.fits')

                    if self.runconf.CtsMapToPng == 1:
                        title = 'OBS ' + str(
                            self.obsconf.id) + ' / MJD ' + str(
                                self.runconf.tmin) + ' - ' + 'MJD ' + str(
                                    self.runconf.tmax)
                        SkyImage.display(cubefile_name, "sky1.png", 2, title)
                        #SkyImage.dispalywithds9_cts1(cubefile_name, "sky2", 10)
                        #copy results
                        os.system('mkdir -p ' + self.runconf.resdir)
                        shutil.copy(
                            './sky1.png', self.runconf.resdir + '/' +
                            self.runconf.runprefix + '_sky1.png')

                #make binned map on memory
                if self.runconf.WorkInMemory == 1:
                    bin.run()

                # Append result to observations
                #localobs.extend(bin.obs())
            print("after ctbin")
            print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])
            #print(obs)
            #print(obs[0])
            print(str(self.obsconf.caldb))

            #hypothesis builders
            #3GHextractor
            # 3GH Extractor code
            if self.runconf.HypothesisGenerator3GH and cubefile_name:
                self.analysisfilename = CTA3GHextractor_wrapper.extract_source(
                    cubefile_name)
                print(self.analysisfilename)
                #cv2.waitKey(0)
                print('HypothesisGeneratorEG3')

            #eseguire MLE
            print('analysisfilename: ' + self.analysisfilename)
            if self.analysisfilename:
                print('before MLE')
                print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])

                # Perform maximum likelihood fitting

                if self.runconf.WorkInMemory == 1:
                    like = ctools.ctlike(select.obs())

                if self.runconf.WorkInMemory == 0:
                    like = ctools.ctlike()

                    if (self.runconf.binned == "1"):
                        like['inobs'] = cubefile_name
                        like['expcube'] = "NONE"
                        like['psfcube'] = "NONE"
                        like['edispcube'] = "NONE"
                        like['bkgcube'] = "NONE"
                        like['statistic'] = "CHI2"

                    if (self.runconf.binned == "0"):
                        like['inobs'] = selected_events_name
                        like['statistic'] = "DEFAULT"

                like['inmodel'] = self.analysisfilename
                like['outmodel'] = result_name
                like['caldb'] = str(self.obsconf.caldb)
                like['irf'] = self.obsconf.irf

                like.execute()
                print(like)
                logL = like.opt().value()
                print(like.obs().models())

                print("after MLE")
                print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])

                # insert logL into results.xml
                tree = etree.parse(result_name)
                contentnav = tree.find(".//source[@type='PointSource']")
                #contentdiv = contentnav.getparent()
                #contentnav.set("ts",str(logL))
                contentnav.set("runid", str(self.runconf.runid))
                #print(etree.tostring(tree,encoding="unicode",pretty_print=True))
                f = open(result_name, 'w')
                f.write(
                    etree.tostring(tree, encoding="unicode",
                                   pretty_print=True))
                f.close()

                os.system('mkdir -p ' + self.runconf.resdir)
                shutil.copy(
                    './results.xml', self.runconf.resdir + '/' +
                    self.runconf.runprefix + '_results.xml')

                if self.runconf.deleterun == "1":
                    cmd = 'rm -r ' + self.runconf.rundir
                    os.system(cmd)

            if self.runconf.HypothesisGenerator3GH:
                CTA3GHextractor_wrapper.print_graphs(self.simfilename,
                                                     result_name,
                                                     self.analysisfilename)
                #cv2.destroyAllWindows()
                print('HypothesisGeneratorEG3')

        # Return
        return
Exemplo n.º 39
0
def run_pipeline(obs, ra=83.63, dec=22.01, emin=0.1, emax=100.0, \
                 enumbins=20, nxpix=200, nypix=200, binsz=0.02, \
                 coordsys="CEL", proj="CAR", \
                 model="${CTOOLS}/share/models/crab.xml", \
                 caldb="prod2", irf="South_50h", \
                 debug=False):
    """
    Simulation and stacked analysis pipeline.

    Keywords:
     ra       - RA of cube centre [deg] (default: 83.6331)
     dec      - DEC of cube centre [deg] (default: 22.0145)
     emin     - Minimum energy of cube [TeV] (default: 0.1)
     emax     - Maximum energy of cube [TeV] (default: 100.0)
     enumbins - Number of energy bins in cube (default: 20)
     nxpix    - Number of RA pixels in cube (default: 200)
     nypix    - Number of DEC pixels in cube (default: 200)
     binsz    - Spatial cube bin size [deg] (default: 0.02)
     coordsys - Cube coordinate system (CEL or GAL)
     proj     - Cube World Coordinate System (WCS) projection
     debug    - Enable debugging (default: False)
    """
    # Simulate events
    sim = ctools.ctobssim(obs)
    sim["debug"].boolean(debug)
    sim["outevents"].filename("obs.xml")
    sim.execute()

    # Bin events into counts map
    bin = ctools.ctbin()
    bin["inobs"].filename("obs.xml")
    bin["outcube"].filename("cntcube.fits")
    bin["ebinalg"].string("LOG")
    bin["emin"].real(emin)
    bin["emax"].real(emax)
    bin["enumbins"].integer(enumbins)
    bin["nxpix"].integer(nxpix)
    bin["nypix"].integer(nypix)
    bin["binsz"].real(binsz)
    bin["coordsys"].string(coordsys)
    bin["proj"].string(proj)
    bin["xref"].real(ra)
    bin["yref"].real(dec)
    bin["debug"].boolean(debug)
    bin.execute()

    # Create exposure cube
    expcube = ctools.ctexpcube()
    expcube["inobs"].filename("obs.xml")
    expcube["incube"].filename("cntcube.fits")
    expcube["outcube"].filename("expcube.fits")
    expcube["caldb"].string(caldb)
    expcube["irf"].string(irf)
    expcube["ebinalg"].string("LOG")
    expcube["emin"].real(emin)
    expcube["emax"].real(emax)
    expcube["enumbins"].integer(enumbins)
    expcube["nxpix"].integer(nxpix)
    expcube["nypix"].integer(nypix)
    expcube["binsz"].real(binsz)
    expcube["coordsys"].string(coordsys)
    expcube["proj"].string(proj)
    expcube["xref"].real(ra)
    expcube["yref"].real(dec)
    expcube["debug"].boolean(debug)
    expcube.execute()

    # Create PSF cube
    psfcube = ctools.ctpsfcube()
    psfcube["inobs"].filename("obs.xml")
    psfcube["incube"].filename("NONE")
    psfcube["outcube"].filename("psfcube.fits")
    psfcube["caldb"].string(caldb)
    psfcube["irf"].string(irf)
    psfcube["ebinalg"].string("LOG")
    psfcube["emin"].real(emin)
    psfcube["emax"].real(emax)
    psfcube["enumbins"].integer(enumbins)
    psfcube["nxpix"].integer(10)
    psfcube["nypix"].integer(10)
    psfcube["binsz"].real(1.0)
    psfcube["coordsys"].string(coordsys)
    psfcube["proj"].string(proj)
    psfcube["xref"].real(ra)
    psfcube["yref"].real(dec)
    psfcube["debug"].boolean(debug)
    psfcube.execute()

    # Create background cube
    bkgcube = ctools.ctbkgcube()
    bkgcube["inobs"].filename("obs.xml")
    bkgcube["inmodel"].filename(model)
    bkgcube["incube"].filename("cntcube.fits")
    bkgcube["outcube"].filename("bkgcube.fits")
    bkgcube["outmodel"].filename("model_bkg.xml")
    bkgcube["caldb"].string(caldb)
    bkgcube["irf"].string(irf)
    bkgcube["ebinalg"].string("LOG")
    bkgcube["emin"].real(emin)
    bkgcube["emax"].real(emax)
    bkgcube["enumbins"].integer(enumbins)
    bkgcube["nxpix"].integer(10)
    bkgcube["nypix"].integer(10)
    bkgcube["binsz"].real(1.0)
    bkgcube["coordsys"].string(coordsys)
    bkgcube["proj"].string(proj)
    bkgcube["xref"].real(ra)
    bkgcube["yref"].real(dec)
    bkgcube["debug"].boolean(debug)
    bkgcube.execute()

    # Perform maximum likelihood fitting
    like = ctools.ctlike()
    like["inobs"].filename("cntcube.fits")
    like["inmodel"].filename("model_bkg.xml")
    like["outmodel"].filename("fit_results.xml")
    like["expcube"].filename("expcube.fits")
    like["psfcube"].filename("psfcube.fits")
    like["bkgcube"].filename("bkgcube.fits")
    like["caldb"].string(caldb)
    like["irf"].string(irf)
    like["debug"].boolean(True) # Switch this always on for results in console
    like.execute()
	
    # Return
    return
Exemplo n.º 40
0
def grb_simulation(sim_in, config_in, model_xml, fits_header_0, counter):
    """
    Function to handle the GRB simulation.
    :param sim_in: the yaml file for the simulation (unpacked as a dict of dicts)
    :param config_in: the yaml file for the job handling (unpacked as a dict of dicts)
    :param model_xml: the XML model name for the source under analysis
    :param fits_header_0: header for the fits file of the GRB model to use. Used in the visibility calculation
    :param counter: integer number. counts the id of the source realization
    :return: significance obtained with the activated detection methods
    """

    src_name = model_xml.split('/')[-1].split('model_')[1][:-4]
    print(src_name, counter)

    ctools_pipe_path = create_path(config_in['exe']['software_path'])
    ctobss_params = sim_in['ctobssim']

    seed = int(counter)*10

    # PARAMETERS FROM THE CTOBSSIM
    sim_t_min = u.Quantity(ctobss_params['time']['t_min']).to_value(u.s)
    sim_t_max = u.Quantity(ctobss_params['time']['t_max']).to_value(u.s)
    sim_e_min = u.Quantity(ctobss_params['energy']['e_min']).to_value(u.TeV)
    sim_e_max = u.Quantity(ctobss_params['energy']['e_max']).to_value(u.TeV)
    sim_rad = ctobss_params['radius']

    models = sim_in['source']
    source_type = models['type']

    if source_type == "GRB":
        phase_path = "/" + models['phase']
    elif source_type == "GW":
        phase_path = ""

    output_path = create_path(sim_in['output']['path'] + phase_path + '/' + src_name)

    save_simulation = ctobss_params['save_simulation']

    with open(f"{output_path}/GRB-{src_name}_seed-{seed}.txt", "w") as f:
        f.write(f"GRB,seed,time_start,time_end,sigma_lima,sqrt_TS_onoff,sqrt_TS_std\n")
        # VISIBILITY PART
        # choose between AUTO mode (use visibility) and MANUAL mode (manually insert IRF)
        simulation_mode = sim_in['IRF']['mode']

        if simulation_mode == "auto":
            print("using visibility to get IRFs")

            # GRB information from the fits header
            ra = fits_header_0['RA']
            dec = fits_header_0['DEC']
            t0 = Time(fits_header_0['GRBJD'])

            irf_dict = sim_in['IRF']
            site = irf_dict['site']
            obs_condition = Observability(site=site)
            obs_condition.set_irf(irf_dict)

            t_zero_mode = ctobss_params['time']['t_zero'].lower()

            if t_zero_mode == "VIS":
                # check if the source is visible one day after the onset of the source
                print("time starts when source becomes visible")
                obs_condition.Proposal_obTime = 86400
                condition_check = obs_condition.check(RA=ra, DEC=dec, t_start=t0)

            elif t_zero_mode == "ONSET":
                print("time starts from the onset of the GRB")
                condition_check = obs_condition.check(RA=ra, DEC=dec, t_start=t0, t_min=sim_t_min, t_max=sim_t_max)

            else:
                print(f"Choose some proper mode between 'VIS' and 'ONSET'. {t_zero_mode} is not a valid one.")
                sys.exit()

            # NO IRF in AUTO mode ==> No simulation! == EXIT!
            if len(condition_check) == 0:
                f.write(f"{src_name},{seed}, -1, -1, -1, -1, -1\n")
                sys.exit()

        elif simulation_mode == "manual":
            print("manual picking IRF")

            # find proper IRF name
            irf = IRFPicker(sim_in, ctools_pipe_path)
            name_irf = irf.irf_pick()

            backgrounds_path = create_path(ctobss_params['bckgrnd_path'])
            fits_background_list = glob.glob(
                f"{backgrounds_path}/{irf.prod_number}_{irf.prod_version}_{name_irf}/background*.fits")

            if len(fits_background_list) == 0:
                print(f"No background for IRF {name_irf}")
                sys.exit()

            fits_background_list = sorted(fits_background_list, key=sort_background)
            background_fits = fits_background_list[int(counter) - 1]
            obs_back = gammalib.GCTAObservation(background_fits)

        else:
            print(f"wrong input for IRF - mode. Input is {simulation_mode}. Use 'auto' or 'manual' instead")
            sys.exit()

        if irf.prod_number == "3b" and irf.prod_version == 0:
            caldb = "prod3b"
        else:
            caldb = f'prod{irf.prod_number}-v{irf.prod_version}'

        # source simulation
        sim = ctools.ctobssim()
        sim['inmodel'] = model_xml
        sim['caldb'] = caldb
        sim['irf'] = name_irf
        sim['ra'] = 0.0
        sim['dec'] = 0.0
        sim['rad'] = sim_rad
        sim['tmin'] = sim_t_min
        sim['tmax'] = sim_t_max
        sim['emin'] = sim_e_min
        sim['emax'] = sim_e_max
        sim['seed'] = seed
        sim.run()

        obs = sim.obs()

        # # move the source photons from closer to (RA,DEC)=(0,0), where the background is located
        # for event in obs[0].events():
        #     # ra_evt = event.dir().dir().ra()
        #     dec_evt = event.dir().dir().dec()
        #     ra_evt_deg = event.dir().dir().ra_deg()
        #     dec_evt_deg = event.dir().dir().dec_deg()
        #
        #     ra_corrected = (ra_evt_deg - ra_pointing)*np.cos(dec_evt)
        #     dec_corrected = dec_evt_deg - dec_pointing
        #     event.dir().dir().radec_deg(ra_corrected, dec_corrected)

        # append all background events to GRB ones ==> there's just one observation and not two
        for event in obs_back.events():
            obs[0].events().append(event)

        # ctselect to save data on disk
        if save_simulation:
            event_list_path = create_path(f"{ctobss_params['output_path']}/{src_name}/")
            #obs.save(f"{event_list_path}/event_list_source-{src_name}_seed-{seed:03}.fits")

            select_time = ctools.ctselect(obs)
            select_time['rad'] = sim_rad
            select_time['tmin'] = sim_t_min
            select_time['tmax'] = sim_t_max
            select_time['emin'] = sim_e_min
            select_time['emax'] = sim_e_max
            select_time['outobs'] = f"{event_list_path}/event_list_source-{src_name}_{seed:03}.fits"
            select_time.run()
            sys.exit()

        # delete all 70+ models from the obs def file...not needed any more
        obs.models(gammalib.GModels())

        # CTSELECT
        select_time = sim_in['ctselect']['time_cut']
        slices = int(select_time['t_slices'])

        if slices == 0:
            times = [sim_t_min, sim_t_max]
            times_start = times[:-1]
            times_end = times[1:]
        elif slices > 0:
            time_mode = select_time['mode']
            if time_mode == "log":
                times = np.logspace(np.log10(sim_t_min), np.log10(sim_t_max), slices + 1, endpoint=True)
            elif time_mode == "lin":
                times = np.linspace(sim_t_min, sim_t_max, slices + 1, endpoint=True)
            else:
                print(f"{time_mode} not valid. Use 'log' or 'lin' ")
                sys.exit()

            if select_time['obs_mode'] == "iter":
                times_start = times[:-1]
                times_end = times[1:]
            elif select_time['obs_mode'] == "cumul":
                times_start = np.repeat(times[0], slices)         # this is to use the same array structure for the loop
                times_end = times[1:]
            elif select_time['obs_mode'] == "all":
                begins, ends = np.meshgrid(times[:-1], times[1:])
                mask_times = begins < ends
                times_start = begins[mask_times].ravel()
                times_end = ends[mask_times].ravel()
            else:
                print(f"obs_mode: {select_time['obs_mode']} not supported")
                sys.exit()

        else:
            print(f"value {slices} not supported...check yaml file")
            sys.exit()

        # ------------------------------------
        # ----- TIME LOOP STARTS HERE --------
        # ------------------------------------

        ctlike_mode = sim_in['detection']
        mode_1 = ctlike_mode['counts']
        mode_2 = ctlike_mode['ctlike-onoff']
        mode_3 = ctlike_mode['ctlike-std']

        for t_in, t_end in zip(times_start, times_end):
            sigma_onoff = 0
            sqrt_ts_like_onoff = 0
            sqrt_ts_like_std = 0
            print("-----------------------------")
            print(f"t_in: {t_in:.2f}, t_end: {t_end:.2f}")

            # different ctlikes (onoff or std) need different files.
            # will be appended here and used later on for the final likelihood
            dict_obs_select_time = {}

            # perform time selection for this specific time bin
            select_time = ctools.ctselect(obs)
            select_time['rad'] = sim_rad
            select_time['tmin'] = t_in
            select_time['tmax'] = t_end
            select_time['emin'] = sim_e_min
            select_time['emax'] = sim_e_max
            select_time.run()

            if mode_1:
                fits_temp_title = f"skymap_{seed}_{t_in:.2f}_{t_end:.2f}.fits"
                pars_counts = ctlike_mode['pars_counts']
                scale = float(pars_counts['scale'])
                npix = 2*int(sim_rad/scale)
                skymap = ctools.ctskymap(select_time.obs().copy())
                skymap['emin'] = sim_e_min
                skymap['emax'] = sim_e_max
                skymap['nxpix'] = npix
                skymap['nypix'] = npix
                skymap['binsz'] = scale
                skymap['proj'] = 'TAN'
                skymap['coordsys'] = 'CEL'
                skymap['xref'] = 0
                skymap['yref'] = 0
                skymap['bkgsubtract'] = 'RING'
                skymap['roiradius'] = pars_counts['roiradius']
                skymap['inradius'] = pars_counts['inradius']
                skymap['outradius'] = pars_counts['outradius']
                skymap['iterations'] = pars_counts['iterations']
                skymap['threshold'] = pars_counts['threshold']
                skymap['outmap'] = fits_temp_title
                skymap.execute()

                input_fits = fits.open(fits_temp_title)
                datain = input_fits[2].data
                datain[np.isnan(datain)] = 0.0
                datain[np.isinf(datain)] = 0.0

                sigma_onoff = np.max(datain)
                os.remove(fits_temp_title)

            if mode_3:
                dict_obs_select_time['std'] = select_time.obs().copy()

            if mode_2:
                onoff_time_sel = cscripts.csphagen(select_time.obs().copy())
                onoff_time_sel['inmodel'] = 'NONE'
                onoff_time_sel['ebinalg'] = 'LOG'
                onoff_time_sel['emin'] = sim_e_min
                onoff_time_sel['emax'] = sim_e_max
                onoff_time_sel['enumbins'] = 30
                onoff_time_sel['coordsys'] = 'CEL'
                onoff_time_sel['ra'] = 0.0
                onoff_time_sel['dec'] = 0.5
                onoff_time_sel['rad'] = 0.2
                onoff_time_sel['bkgmethod'] = 'REFLECTED'
                onoff_time_sel['use_model_bkg'] = False
                onoff_time_sel['stack'] = False
                onoff_time_sel.run()

                dict_obs_select_time['onoff'] = onoff_time_sel.obs().copy()

                del onoff_time_sel

                # print(f"sigma ON/OFF: {sigma_onoff:.2f}")

            if mode_2 or mode_3:

                # Low Energy PL fitting
                # to be saved in this dict
                dict_pl_ctlike_out = {}

                e_min_pl_ctlike = 0.030
                e_max_pl_ctlike = 0.080

                # simple ctobssim copy and select for ctlike-std
                select_pl_ctlike = ctools.ctselect(select_time.obs().copy())
                select_pl_ctlike['rad'] = 3
                select_pl_ctlike['tmin'] = t_in
                select_pl_ctlike['tmax'] = t_end
                select_pl_ctlike['emin'] = e_min_pl_ctlike
                select_pl_ctlike['emax'] = e_max_pl_ctlike
                select_pl_ctlike.run()

                # create test source
                src_dir = gammalib.GSkyDir()
                src_dir.radec_deg(0, 0.5)
                spatial = gammalib.GModelSpatialPointSource(src_dir)

                # create and append source spectral model
                spectral = gammalib.GModelSpectralPlaw()
                spectral['Prefactor'].value(5.5e-16)
                spectral['Prefactor'].scale(1e-16)
                spectral['Index'].value(-2.6)
                spectral['Index'].scale(-1.0)
                spectral['PivotEnergy'].value(50000)
                spectral['PivotEnergy'].scale(1e3)
                model_src = gammalib.GModelSky(spatial, spectral)
                model_src.name('PL_fit_temp')
                model_src.tscalc(True)

                spectral_back = gammalib.GModelSpectralPlaw()
                spectral_back['Prefactor'].value(1.0)
                spectral_back['Prefactor'].scale(1.0)
                spectral_back['Index'].value(0)
                spectral_back['PivotEnergy'].value(300000)
                spectral_back['PivotEnergy'].scale(1e6)

                if mode_2:
                    back_model = gammalib.GCTAModelIrfBackground()
                    back_model.instruments('CTAOnOff')
                    back_model.name('Background')
                    back_model.spectral(spectral_back.copy())

                    onoff_pl_ctlike_lima = cscripts.csphagen(select_pl_ctlike.obs().copy())
                    onoff_pl_ctlike_lima['inmodel'] = 'NONE'
                    onoff_pl_ctlike_lima['ebinalg'] = 'LOG'
                    onoff_pl_ctlike_lima['emin'] = e_min_pl_ctlike
                    onoff_pl_ctlike_lima['emax'] = e_max_pl_ctlike
                    onoff_pl_ctlike_lima['enumbins'] = 30
                    onoff_pl_ctlike_lima['coordsys'] = 'CEL'
                    onoff_pl_ctlike_lima['ra'] = 0.0
                    onoff_pl_ctlike_lima['dec'] = 0.5
                    onoff_pl_ctlike_lima['rad'] = 0.2
                    onoff_pl_ctlike_lima['bkgmethod'] = 'REFLECTED'
                    onoff_pl_ctlike_lima['use_model_bkg'] = False
                    onoff_pl_ctlike_lima['stack'] = False
                    onoff_pl_ctlike_lima.run()

                    onoff_pl_ctlike_lima.obs().models(gammalib.GModels())
                    onoff_pl_ctlike_lima.obs().models().append(model_src.copy())
                    onoff_pl_ctlike_lima.obs().models().append(back_model.copy())

                    like_pl = ctools.ctlike(onoff_pl_ctlike_lima.obs())
                    like_pl['refit'] = True
                    like_pl.run()
                    dict_pl_ctlike_out['onoff'] = like_pl.obs().copy()
                    del onoff_pl_ctlike_lima
                    del like_pl

                if mode_3:
                    models_ctlike_std = gammalib.GModels()
                    models_ctlike_std.append(model_src.copy())
                    back_model = gammalib.GCTAModelIrfBackground()
                    back_model.instruments('CTA')
                    back_model.name('Background')
                    back_model.spectral(spectral_back.copy())
                    models_ctlike_std.append(back_model)

                    # save models
                    xmlmodel_PL_ctlike_std = 'test_model_PL_ctlike_std.xml'
                    models_ctlike_std.save(xmlmodel_PL_ctlike_std)
                    del models_ctlike_std

                    like_pl = ctools.ctlike(select_pl_ctlike.obs().copy())
                    like_pl['inmodel'] = xmlmodel_PL_ctlike_std
                    like_pl['refit'] = True
                    like_pl.run()
                    dict_pl_ctlike_out['std'] = like_pl.obs().copy()
                    del like_pl

                del spatial
                del spectral
                del model_src
                del select_pl_ctlike

                # EXTENDED CTLIKE
                for key in dict_obs_select_time.keys():
                    likelihood_pl_out = dict_pl_ctlike_out[key]
                    selected_data = dict_obs_select_time[key]

                    pref_out_pl = likelihood_pl_out.models()[0]['Prefactor'].value()
                    index_out_pl = likelihood_pl_out.models()[0]['Index'].value()
                    pivot_out_pl = likelihood_pl_out.models()[0]['PivotEnergy'].value()

                    expplaw = gammalib.GModelSpectralExpPlaw()
                    expplaw['Prefactor'].value(pref_out_pl)
                    expplaw['Index'].value(index_out_pl)
                    expplaw['PivotEnergy'].value(pivot_out_pl)
                    expplaw['CutoffEnergy'].value(80e3)

                    if key == "onoff":
                        selected_data.models()[0].name(src_name)
                        selected_data.models()[0].tscalc(True)
                        selected_data.models()[0].spectral(expplaw.copy())

                        like = ctools.ctlike(selected_data)
                        like['refit'] = True
                        like.run()
                        ts = like.obs().models()[0].ts()
                        if ts > 0:
                            sqrt_ts_like_onoff = np.sqrt(like.obs().models()[0].ts())
                        else:
                            sqrt_ts_like_onoff = 0

                        del like

                    if key == "std":
                        models_fit_ctlike = gammalib.GModels()

                        # create test source
                        src_dir = gammalib.GSkyDir()
                        src_dir.radec_deg(0, 0.5)
                        spatial = gammalib.GModelSpatialPointSource(src_dir)

                        # append spatial and spectral models
                        model_src = gammalib.GModelSky(spatial, expplaw.copy())
                        model_src.name('Source_fit')
                        model_src.tscalc(True)
                        models_fit_ctlike.append(model_src)

                        # create and append background
                        back_model = gammalib.GCTAModelIrfBackground()
                        back_model.instruments('CTA')
                        back_model.name('Background')
                        spectral_back = gammalib.GModelSpectralPlaw()
                        spectral_back['Prefactor'].value(1.0)
                        spectral_back['Prefactor'].scale(1.0)
                        spectral_back['Index'].value(0)
                        spectral_back['PivotEnergy'].value(300000)
                        spectral_back['PivotEnergy'].scale(1e6)
                        back_model.spectral(spectral_back)
                        models_fit_ctlike.append(back_model)

                        # save models
                        input_ctlike_xml = "model_GRB_fit_ctlike_in.xml"
                        models_fit_ctlike.save(input_ctlike_xml)
                        del models_fit_ctlike

                        like = ctools.ctlike(selected_data)
                        like['inmodel'] = input_ctlike_xml
                        like['refit'] = True
                        like.run()
                        ts = like.obs().models()[0].ts()
                        if ts > 0:
                            sqrt_ts_like_std = np.sqrt(like.obs().models()[0].ts())
                        else:
                            sqrt_ts_like_std = 0

                        del like

                    # E_cut_off = like.obs().models()[0]['CutoffEnergy'].value()
                    # E_cut_off_error = like.obs().models()[0]['CutoffEnergy'].error()

                    # print(f"sqrt(TS) {key}: {np.sqrt(ts_like):.2f}")
                    # print(f"E_cut_off {key}: {E_cut_off:.2f} +- {E_cut_off_error:.2f}")
                del dict_pl_ctlike_out

            f.write(f"{src_name},{seed},{t_in:.2f},{t_end:.2f},{sigma_onoff:.2f},{sqrt_ts_like_onoff:.2f},{sqrt_ts_like_std:.2f}\n")
            del dict_obs_select_time
            del select_time
Exemplo n.º 41
0
def gw_simulation(sim_in, config_in, model_xml, fits_model, counter):
    """

    :param sim_in:
    :param config_in:
    :param model_xml:
    :param fits_model:
    :param counter:
    :return:
    """

    src_name = fits_model.split("/")[-1][:-5]
    run_id, merger_id = src_name.split('_')

    fits_header_0 = fits.open(fits_model)[0].header
    ra_src = fits_header_0['RA']
    dec_src = fits_header_0['DEC']

    coordinate_source = SkyCoord(ra=ra_src * u.deg, dec=dec_src * u.deg, frame="icrs")

    src_yaml = sim_in['source']

    point_path = create_path(src_yaml['pointings_path'])
    opt_point_path = f"{point_path}/optimized_pointings"

    ctools_pipe_path = create_path(config_in['exe']['software_path'])
    ctobss_params = sim_in['ctobssim']

    seed = int(counter)*10

    # # PARAMETERS FROM THE CTOBSSIM
    sim_e_min = u.Quantity(ctobss_params['energy']['e_min']).to_value(u.TeV)
    sim_e_max = u.Quantity(ctobss_params['energy']['e_max']).to_value(u.TeV)

    sim_rad = ctobss_params['radius']
    output_path = create_path(sim_in['output']['path'] + f"/{src_name}/seed-{seed:03}")

    irf_dict = sim_in['IRF']
    site = irf_dict['site']

    detection = sim_in['detection']
    significance_map = detection['skymap_significance']
    srcdetect_ctlike = detection['srcdetect_likelihood']

    save_simulation = ctobss_params['save_simulation']

    try:
        mergers_data = pd.read_csv(
            f"{point_path}/BNS-GW-Time_onAxis5deg.txt",
            sep=" ")
    except FileNotFoundError:
        print("merger data not present. check that the text file with the correct pointings is in the 'pointings' folder!")
        sys.exit()

    filter_mask = (mergers_data["run"] == run_id) & (mergers_data["MergerID"] == f"Merger{merger_id}")
    merger_onset_data = mergers_data[filter_mask]
    time_onset_merger = merger_onset_data['Time'].values[0]

    with open(f"{output_path}/GW-{src_name}_seed-{seed:03}_site-{site}.txt", "w") as f:
        f.write(f"GW_name\tRA_src\tDEC_src\tseed\tpointing_id\tsrc_to_point\tsrc_in_point\tra_point\tdec_point\tradius\ttime_start\ttime_end\tsignificanceskymap\tsigmasrcdetectctlike\n")
        try:
            file_name = f"{opt_point_path}/{run_id}_Merger{merger_id}_GWOptimisation_v3.txt"
            pointing_data = pd.read_csv(
                file_name,
                header=0,
                sep=",")
        except FileNotFoundError:
            print("File not found\n")
            sys.exit()

        RA_data = pointing_data['RA(deg)']
        DEC_data = pointing_data['DEC(deg)']
        times = pointing_data['Observation Time UTC']
        durations = pointing_data['Duration']

        # LOOP OVER POINTINGS
        for index in range(0, len(pointing_data)):
            RA_point = RA_data[index]
            DEC_point = DEC_data[index]
            coordinate_pointing = SkyCoord(
                ra=RA_point * u.degree,
                dec=DEC_point * u.degree,
                frame="icrs"
            )
            src_from_pointing = coordinate_pointing.separation(coordinate_source)

            t_in_point = Time(times[index])

            obs_condition = Observability(site=site)
            obs_condition.set_irf(irf_dict)
            obs_condition.Proposal_obTime = 10
            obs_condition.TimeOffset = 0
            obs_condition.Steps_observability = 10
            condition_check = obs_condition.check(RA=RA_point, DEC=DEC_point, t_start=t_in_point)

            # once the IRF has been chosen, the times are shifted
            # this is a quick and dirty solution to handle the times in ctools...not elegant for sure
            t_in_point = (Time(times[index]) - Time(time_onset_merger)).to(u.s)
            t_end_point = t_in_point + durations[index] * u.s

            if len(condition_check) == 0:
                print(f"Source Not Visible in pointing {index}")
                f.write(
                    f"{src_name}\t{ra_src}\t{dec_src}\t{seed}\t{index}\t{src_from_pointing.value:.2f}\t{src_from_pointing.value < sim_rad}\t{RA_point}\t{DEC_point}\t{sim_rad}\t{t_in_point.value:.2f}\t{t_end_point.value:.2f}\t -1 \t -1\n")
                continue

            name_irf = condition_check['IRF_name'][0]
            irf = condition_check['IRF'][0]
            # model loading

            if irf.prod_number == "3b" and irf.prod_version == 0:
                caldb = "prod3b"
            else:
                caldb = f'prod{irf.prod_number}-v{irf.prod_version}'

            # simulation
            sim = ctools.ctobssim()
            sim['inmodel'] = model_xml
            sim['caldb'] = caldb
            sim['irf'] = name_irf
            sim['ra'] = RA_point
            sim['dec'] = DEC_point
            sim['rad'] = sim_rad
            sim['tmin'] = t_in_point.value
            sim['tmax'] = t_end_point.value
            sim['emin'] = sim_e_min
            sim['emax'] = sim_e_max
            sim['seed'] = seed

            if save_simulation:
                event_list_path = create_path(f"{ctobss_params['output_path']}/{src_name}/seed-{seed:03}/")
                sim['outevents'] = f"{event_list_path}/event_list_source-{src_name}_seed-{seed:03}_pointingID-{index}.fits"
                sim.execute()
                f.write(
                    f"{src_name}\t{ra_src}\t{dec_src}\t{seed}\t{index}\t{src_from_pointing.value:.2f}\t{src_from_pointing.value < sim_rad}\t{RA_point}\t{DEC_point}\t{sim_rad}\t{t_in_point.value:.2f}\t{t_end_point.value:.2f}\t -1 \t -1\n"
                )
                continue
            else:
                sim.run()

            obs = sim.obs()

            obs.models(gammalib.GModels())

            # ctskymap

            sigma_onoff = -1
            sqrt_ts_like = -1

            if significance_map:
                pars_skymap = detection['parameters_skymap']
                scale = float(pars_skymap['scale'])
                npix = 2 * int(sim_rad / scale)

                fits_temp_title = f"{output_path}/GW-skymap_point-{index}_{seed}.fits"

                skymap = ctools.ctskymap(obs.copy())
                skymap['proj'] = 'CAR'
                skymap['coordsys'] = 'CEL'
                skymap['xref'] = RA_point
                skymap['yref'] = DEC_point
                skymap['binsz'] = scale
                skymap['nxpix'] = npix
                skymap['nypix'] = npix
                skymap['emin'] = sim_e_min
                skymap['emax'] = sim_e_max
                skymap['bkgsubtract'] = 'RING'
                skymap['roiradius'] = pars_skymap['roiradius']
                skymap['inradius'] = pars_skymap['inradius']
                skymap['outradius'] = pars_skymap['outradius']
                skymap['iterations'] = pars_skymap['iterations']
                skymap['threshold'] = pars_skymap['threshold']
                skymap['outmap'] = fits_temp_title
                skymap.execute()

                input_fits = fits.open(fits_temp_title)
                datain = input_fits['SIGNIFICANCE'].data
                datain[np.isnan(datain)] = 0.0
                datain[np.isinf(datain)] = 0.0

                sigma_onoff = np.max(datain)

                if pars_skymap['remove_fits']:
                    os.remove(fits_temp_title)

            if srcdetect_ctlike:
                pars_detect = detection['parameters_detect']
                scale = float(pars_detect['scale'])
                npix = 2 * int(sim_rad / scale)

                skymap = ctools.ctskymap(obs.copy())
                skymap['proj'] = 'TAN'
                skymap['coordsys'] = 'CEL'
                skymap['xref'] = RA_point
                skymap['yref'] = DEC_point
                skymap['binsz'] = scale
                skymap['nxpix'] = npix
                skymap['nypix'] = npix
                skymap['emin'] = sim_e_min
                skymap['emax'] = sim_e_max
                skymap['bkgsubtract'] = 'NONE'
                skymap.run()

                # cssrcdetect
                srcdetect = cscripts.cssrcdetect(skymap.skymap().copy())
                srcdetect['srcmodel'] = 'POINT'
                srcdetect['bkgmodel'] = 'NONE'
                srcdetect['corr_kern'] = 'GAUSSIAN'
                srcdetect['threshold'] = pars_detect['threshold']
                srcdetect['corr_rad'] = pars_detect['correlation']
                srcdetect.run()

                models = srcdetect.models()

                # if there's some detection we can do the likelihood.
                # Spectral model is a PL and the spatial model is the one from cssrcdetect
                if len(models) > 0:
                    hotspot = models['Src001']
                    ra_hotspot = hotspot['RA'].value()
                    dec_hotspot = hotspot['DEC'].value()

                    models_ctlike = gammalib.GModels()

                    src_dir = gammalib.GSkyDir()
                    src_dir.radec_deg(ra_hotspot, dec_hotspot)
                    spatial = gammalib.GModelSpatialPointSource(src_dir)

                    spectral = gammalib.GModelSpectralPlaw()
                    spectral['Prefactor'].value(5.5e-16)
                    spectral['Prefactor'].scale(1e-16)
                    spectral['Index'].value(-2.6)
                    spectral['Index'].scale(-1.0)
                    spectral['PivotEnergy'].value(50000)
                    spectral['PivotEnergy'].scale(1e3)

                    model_src = gammalib.GModelSky(spatial, spectral)
                    model_src.name('PL_fit_GW')
                    model_src.tscalc(True)

                    models_ctlike.append(model_src)

                    spectral_back = gammalib.GModelSpectralPlaw()
                    spectral_back['Prefactor'].value(1.0)
                    spectral_back['Prefactor'].scale(1.0)
                    spectral_back['Index'].value(0)
                    spectral_back['PivotEnergy'].value(300000)
                    spectral_back['PivotEnergy'].scale(1e6)

                    back_model = gammalib.GCTAModelIrfBackground()
                    back_model.instruments('CTA')
                    back_model.name('Background')
                    back_model.spectral(spectral_back.copy())
                    models_ctlike.append(back_model)

                    xmlmodel_PL_ctlike_std = f"{output_path}/model_PL_ctlike_std_seed-{seed}_pointing-{index}.xml"
                    models_ctlike.save(xmlmodel_PL_ctlike_std)

                    like_pl = ctools.ctlike(obs.copy())
                    like_pl['inmodel'] = xmlmodel_PL_ctlike_std
                    like_pl['caldb'] = caldb
                    like_pl['irf'] = name_irf
                    like_pl.run()

                    ts = -like_pl.obs().models()[0].ts()
                    if ts > 0:
                        sqrt_ts_like = np.sqrt(ts)
                    else:
                        sqrt_ts_like = 0

                    if pars_detect['remove_xml']:
                        os.remove(xmlmodel_PL_ctlike_std)

            f.write(
                f"{src_name}\t{ra_src}\t{dec_src}\t{seed}\t{index}\t{src_from_pointing.value:.2f}\t{src_from_pointing.value < sim_rad}\t{RA_point:.2f}\t{DEC_point:.2f}\t{sim_rad}\t{t_in_point:.2f}\t{t_end_point:.2f}\t{sigma_onoff:.2f}\t{sqrt_ts_like}\n")
Exemplo n.º 42
0
def sim_select_like(sim_yaml, jobs_in, model_xml, background_fits, counter):
    """

    :param sim_yaml:
    :param jobs_in:
    :param model_xml:
    :param background_fits:
    :param counter:
    :return:
    """
    #print("----------------------------")
    print(model_xml.split('/')[-1], counter)

    config_in = yaml.safe_load(open(jobs_in))
    ctools_pipe_path = create_path(config_in['exe']['software_path'])

    sim_in = yaml.safe_load(open(sim_yaml))
    ctobss_params = sim_in['ctobssim']

    # find proper IRF name
    irf = IRFPicker(sim_in, ctools_pipe_path)
    name_irf = irf.irf_pick()

    if irf.prod_version == "3b" and irf.prod_number == 0:
        caldb = "prod3b"
    else:
        caldb = f'prod{irf.prod_number}-v{irf.prod_version}'

    # loading background (this is a way of doing it without saving any file)
    # output.save("name.xml") to save the file
    obs_def = gammalib.GObservations()

    background_id = f"{str(int(counter) + 1).zfill(6)}"

    output = gammalib.GXml()
    level0 = gammalib.GXmlElement('observation_list title="observation library"')
    level1 = gammalib.GXmlElement(f'observation name="name_source" id="{background_id}" instrument="CTA"')
    level2 = gammalib.GXmlElement(f'parameter name="EventList"  file="{background_fits}"')
    level1.append(level2)
    level0.append(level1)
    output.append(level0)

    obs_def.read(output)

    seed = int(counter)*10

    # do the simulation
    sim = ctools.ctobssim()
    sim['inmodel'] = model_xml
    sim['caldb'] = caldb
    sim['irf'] = name_irf
    sim['ra'] = 0
    sim['dec'] = 0
    sim['rad'] = ctobss_params['radius']
    sim['tmin'] = u.Quantity(ctobss_params['time']['t_min']).to_value(u.s)
    sim['tmax'] = u.Quantity(ctobss_params['time']['t_max']).to_value(u.s)
    sim['emin'] = u.Quantity(ctobss_params['energy']['e_min']).to_value(u.TeV)
    sim['emax'] = u.Quantity(ctobss_params['energy']['e_max']).to_value(u.TeV)
    sim['seed'] = seed
    sim.run()

    obs_def.append(sim.obs()[0])

    # for obs in obs_def:
    #     print(obs.id(), obs.nobserved())

    select = ctools.ctselect(obs_def)
    select['rad'] = 3
    select['tmin'] = 0
    select['tmax'] = 50
    select['emin'] = 0.05
    select['emax'] = 1.0
    select.run()
Exemplo n.º 43
0
def binned_pipeline(model_name, duration):
    """
    Binned analysis pipeline.
    """
    # Set script parameters
    caldb = "prod2"
    irf = "South_50h"
    ra = 83.63
    dec = 22.01
    rad_sim = 10.0
    tstart = 0.0
    tstop = duration
    emin = 0.1
    emax = 100.0
    enumbins = 40
    nxpix = 200
    nypix = 200
    binsz = 0.02
    coordsys = "CEL"
    proj = "CAR"

    # Get start CPU time
    cpu_start = time.clock()

    # Simulate events
    sim = ctools.ctobssim()
    sim["inmodel"] = model_name
    sim["caldb"] = caldb
    sim["irf"] = irf
    sim["ra"] = ra
    sim["dec"] = dec
    sim["rad"] = rad_sim
    sim["tmin"] = tstart
    sim["tmax"] = tstop
    sim["emin"] = emin
    sim["emax"] = emax
    sim.run()

    # Bin events into counts map
    bin = ctools.ctbin(sim.obs())
    bin["ebinalg"] = "LOG"
    bin["emin"] = emin
    bin["emax"] = emax
    bin["enumbins"] = enumbins
    bin["nxpix"] = nxpix
    bin["nypix"] = nypix
    bin["binsz"] = binsz
    bin["coordsys"] = coordsys
    bin["xref"] = ra
    bin["yref"] = dec
    bin["proj"] = proj
    bin.run()

    # Get ctlike start CPU time
    cpu_ctlike = time.clock()

    # Perform maximum likelihood fitting
    like = ctools.ctlike(bin.obs())
    like.run()

    # Get stop CPU time and compute elapsed times
    cpu_stop = time.clock()
    cpu_elapsed = cpu_stop - cpu_start
    cpu_ctlike = cpu_stop - cpu_ctlike

    # Return
    return cpu_elapsed, cpu_ctlike
Exemplo n.º 44
0
def binned_pipeline(duration):
    """
    Binned analysis pipeline.
    """
    # Set script parameters
    model_name  = "${CTOOLS}/share/models/crab.xml"
    caldb       = "prod2"
    irf         = "South_50h"
    ra          =   83.63
    dec         =   22.01
    rad_sim     =   10.0
    tstart      =    0.0
    tstop       = duration
    emin        =    0.1
    emax        =  100.0
    enumbins    =   20
    nxpix       =  200
    nypix       =  200
    binsz       =    0.02
    coordsys    = "CEL"
    proj        = "CAR"

    # Get start CPU time
    tstart = time.clock()

    # Simulate events
    sim = ctools.ctobssim()
    sim["inmodel"].filename(model_name)
    sim["caldb"].string(caldb)
    sim["irf"].string(irf)
    sim["ra"].real(ra)
    sim["dec"].real(dec)
    sim["rad"].real(rad_sim)
    sim["tmin"].real(tstart)
    sim["tmax"].real(tstop)
    sim["emin"].real(emin)
    sim["emax"].real(emax)
    sim.run()

    # Bin events into counts map
    bin = ctools.ctbin(sim.obs())
    bin["ebinalg"].string("LOG")
    bin["emin"].real(emin)
    bin["emax"].real(emax)
    bin["enumbins"].integer(enumbins)
    bin["nxpix"].integer(nxpix)
    bin["nypix"].integer(nypix)
    bin["binsz"].real(binsz)
    bin["coordsys"].string(coordsys)
    bin["xref"].real(ra)
    bin["yref"].real(dec)
    bin["proj"].string(proj)
    bin.run()

    # Get ctlike start CPU time
    tctlike = time.clock()

    # Perform maximum likelihood fitting
    like = ctools.ctlike(bin.obs())
    like.run()

    # Get stop CPU time
    tstop    = time.clock()
    telapsed = tstop - tstart
    tctlike  = tstop - tctlike
	
    # Return
    return telapsed, tctlike
Exemplo n.º 45
0
def binned_pipeline(model_name, duration):
    """
    Binned analysis pipeline.
    """
    # Set script parameters
    caldb       = "prod2"
    irf         = "South_50h"
    ra          =   83.63
    dec         =   22.01
    rad_sim     =   10.0
    tstart      =    0.0
    tstop       = duration
    emin        =    0.1
    emax        =  100.0
    enumbins    =   40
    nxpix       =  200
    nypix       =  200
    binsz       =    0.02
    coordsys    = "CEL"
    proj        = "CAR"

    # Get start CPU time
    cpu_start = time.clock()

    # Simulate events
    sim = ctools.ctobssim()
    sim["inmodel"] = model_name
    sim["caldb"]   = caldb
    sim["irf"]     = irf
    sim["ra"]      = ra
    sim["dec"]     = dec
    sim["rad"]     = rad_sim
    sim["tmin"]    = tstart
    sim["tmax"]    = tstop
    sim["emin"]    = emin
    sim["emax"]    = emax
    sim.run()

    # Bin events into counts map
    bin = ctools.ctbin(sim.obs())
    bin["ebinalg"]  = "LOG"
    bin["emin"]     = emin
    bin["emax"]     = emax
    bin["enumbins"] = enumbins
    bin["nxpix"]    = nxpix
    bin["nypix"]    = nypix
    bin["binsz"]    = binsz
    bin["coordsys"] = coordsys
    bin["xref"]     = ra
    bin["yref"]     = dec
    bin["proj"]     = proj
    bin.run()

    # Get ctlike start CPU time
    cpu_ctlike = time.clock()

    # Perform maximum likelihood fitting
    like = ctools.ctlike(bin.obs())
    like.run()

    # Get stop CPU time and compute elapsed times
    cpu_stop    = time.clock()
    cpu_elapsed = cpu_stop - cpu_start
    cpu_ctlike  = cpu_stop - cpu_ctlike

    # Return
    return cpu_elapsed, cpu_ctlike
Exemplo n.º 46
0
def stacked_pipeline(duration):
    """
    Cube-style analysis pipeline.
    """
    # Set script parameters
    model_name  = "${CTOOLS}/share/models/crab.xml"
    caldb       = "prod2"
    irf         = "South_50h"
    ra          =   83.63
    dec         =   22.01
    rad_sim     =   10.0
    tstart      =    0.0
    tstop       = duration
    emin        =    0.1
    emax        =  100.0
    enumbins    =   20
    nxpix       =  200
    nypix       =  200
    binsz       =    0.02
    coordsys    = "CEL"
    proj        = "CAR"

    # Get start CPU time
    tstart = time.clock()

    # Simulate events
    sim = ctools.ctobssim()
    sim["inmodel"].filename(model_name)
    sim["caldb"].string(caldb)
    sim["irf"].string(irf)
    sim["ra"].real(ra)
    sim["dec"].real(dec)
    sim["rad"].real(rad_sim)
    sim["tmin"].real(tstart)
    sim["tmax"].real(tstop)
    sim["emin"].real(emin)
    sim["emax"].real(emax)
    sim.run()

    # Bin events into counts map
    bin = ctools.ctbin(sim.obs())
    bin["ebinalg"].string("LOG")
    bin["emin"].real(emin)
    bin["emax"].real(emax)
    bin["enumbins"].integer(enumbins)
    bin["nxpix"].integer(nxpix)
    bin["nypix"].integer(nypix)
    bin["binsz"].real(binsz)
    bin["coordsys"].string(coordsys)
    bin["proj"].string(proj)
    bin["xref"].real(ra)
    bin["yref"].real(dec)
    bin.run()

    # Create exposure cube
    expcube = ctools.ctexpcube(sim.obs())
    expcube["incube"].filename("NONE")
    expcube["caldb"].string(caldb)
    expcube["irf"].string(irf)
    expcube["ebinalg"].string("LOG")
    expcube["emin"].real(emin)
    expcube["emax"].real(emax)
    expcube["enumbins"].integer(enumbins)
    expcube["nxpix"].integer(nxpix)
    expcube["nypix"].integer(nypix)
    expcube["binsz"].real(binsz)
    expcube["coordsys"].string(coordsys)
    expcube["proj"].string(proj)
    expcube["xref"].real(ra)
    expcube["yref"].real(dec)
    expcube.run()

    # Create PSF cube
    psfcube = ctools.ctpsfcube(sim.obs())
    psfcube["incube"].filename("NONE")
    psfcube["caldb"].string(caldb)
    psfcube["irf"].string(irf)
    psfcube["ebinalg"].string("LOG")
    psfcube["emin"].real(emin)
    psfcube["emax"].real(emax)
    psfcube["enumbins"].integer(enumbins)
    psfcube["nxpix"].integer(10)
    psfcube["nypix"].integer(10)
    psfcube["binsz"].real(1.0)
    psfcube["coordsys"].string(coordsys)
    psfcube["proj"].string(proj)
    psfcube["xref"].real(ra)
    psfcube["yref"].real(dec)
    psfcube.run()

    # Create background cube
    bkgcube = ctools.ctbkgcube(sim.obs())
    bkgcube["incube"].filename("NONE")
    bkgcube["ebinalg"].string("LOG")
    bkgcube["emin"].real(emin)
    bkgcube["emax"].real(emax)
    bkgcube["enumbins"].integer(enumbins)
    bkgcube["nxpix"].integer(10)
    bkgcube["nypix"].integer(10)
    bkgcube["binsz"].real(1.0)
    bkgcube["coordsys"].string(coordsys)
    bkgcube["proj"].string(proj)
    bkgcube["xref"].real(ra)
    bkgcube["yref"].real(dec)
    bkgcube.run()

    # Attach background model to observation container
    bin.obs().models(bkgcube.models())

    # Set Exposure and Psf cube for first CTA observation
    # (ctbin will create an observation with a single container)
    bin.obs()[0].response(expcube.expcube(), psfcube.psfcube(), bkgcube.bkgcube())

    # Get ctlike start CPU time
    tctlike = time.clock()

    # Perform maximum likelihood fitting
    like = ctools.ctlike(bin.obs())
    like.run()

    # Get stop CPU time
    tstop    = time.clock()
    telapsed = tstop - tstart
    tctlike  = tstop - tctlike
	
    # Return
    return telapsed, tctlike
Exemplo n.º 47
0
def run_pipeline(obs, emin=0.1, emax=100.0, \
                 enumbins=20, nxpix=200, nypix=200, binsz=0.02, \
                 coordsys="CEL", proj="CAR", \
                 model="${CTOOLS}/share/models/crab.xml", \
                 caldb="prod2", irf="South_50h", \
                 debug=False):
    """
    Simulation and binned analysis pipeline.

    Keywords:
     emin     - Minimum energy of cube [TeV] (default: 0.1)
     emax     - Maximum energy of cube [TeV] (default: 100.0)
     enumbins - Number of energy bins in cube (default: 20)
     nxpix    - Number of RA pixels in cube (default: 200)
     nypix    - Number of DEC pixels in cube (default: 200)
     binsz    - Spatial cube bin size [deg] (default: 0.02)
     coordsys - Cube coordinate system (CEL or GAL)
     proj     - Cube World Coordinate System (WCS) projection
     model    - Model Xml file
     caldb    - Calibration database path (default: "dummy")
     irf      - Instrument response function (default: cta_dummy_irf)
     debug    - Enable debugging (default: False)
    """
    # Simulate events
    sim = ctools.ctobssim(obs)
    sim["debug"].boolean(debug)
    sim["outevents"].filename("obs.xml")
    sim.execute()

    # Bin events by looping over all observations in the container
    sim_obs = gammalib.GObservations("obs.xml")
    obs     = gammalib.GObservations()
    for run in sim_obs:

        # Get event filename and set counts cube filename
        eventfile = run.eventfile()
        cubefile  = "cube_"+eventfile

        # Bin events for that observation
        bin = ctools.ctbin()
        bin["inobs"].filename(eventfile)
        bin["outcube"].filename(cubefile)
        bin["ebinalg"].string("LOG")
        bin["emin"].real(emin)
        bin["emax"].real(emax)
        bin["enumbins"].integer(enumbins)
        bin["nxpix"].integer(nxpix)
        bin["nypix"].integer(nypix)
        bin["binsz"].real(binsz)
        bin["coordsys"].string(coordsys)
        bin["usepnt"].boolean(True)
        bin["proj"].string(proj)
        bin.execute()

        # Set observation ID
        bin.obs()[0].id(cubefile)
        bin.obs()[0].eventfile(cubefile)

        # Append result to observations
        obs.extend(bin.obs())

    # Save XML file
    xml = gammalib.GXml()
    obs.write(xml)
    xml.save("obs_cube.xml")

    # Perform maximum likelihood fitting
    like = ctools.ctlike()
    like["inobs"].filename("obs_cube.xml")
    like["inmodel"].filename(model)
    like["outmodel"].filename("fit_results.xml")
    like["expcube"].filename("NONE")
    like["psfcube"].filename("NONE")
    like["bkgcube"].filename("NONE")
    like["caldb"].string(caldb)
    like["irf"].string(irf)
    like["debug"].boolean(True) # Switch this always on for results in console
    like.execute()
	
    # Return
    return
Exemplo n.º 48
0
def sim(obs, log=False, debug=False, chatter=2, edisp=False, seed=0,
        emin=None, emax=None, nbins=0, onsrc=None, onrad=0.2, addbounds=False,
        binsz=0.05, npix=200, proj='TAN', coord='GAL'):
    """
    Simulate events for all observations in the container

    Simulate events for all observations using ctobssim. If the number of
    energy bins is positive, the events are binned into a counts cube using
    ctbin. If multiple observations are simulated, the counts cube is a
    stacked cube and the corresponding response cubes are computed using
    ctexpcube, ctpsfcube, ctbkgcube and optionally ctedispcube. The response
    cubes are attached to the first observation in the container, which
    normally is the observation with the counts cube.

    Parameters
    ----------
    obs : `~gammalib.GObservations`
        Observation container without events
    log : bool, optional
        Create log file(s)
    debug : bool, optional
        Create console dump?
    chatter : int, optional
        Chatter level
    edisp : bool, optional
        Apply energy dispersion?
    seed : int, optional
        Seed value for simulations
    emin : float, optional
        Minimum energy of counts cube for binned (TeV)
    emax : float, optional
        Maximum energy of counts cube for binned (TeV)
    nbins : int, optional
        Number of energy bins (0=unbinned)
    onsrc : str, optional
        Name of source for On region (None if no On/Off obs. is used)
    onrad : float, optional
        Radius for On region (deg)
    addbounds : bool, optional
        Add boundaries at observation energies
    binsz : float, optional
        Pixel size for binned simulation (deg/pixel)
    npix : int, optional
        Number of pixels in X and Y for binned simulation
    proj : str, optional
        Projection for binned simulation
    coord : str, optional
        Coordinate system for binned simulation

    Returns
    -------
    obs : `~gammalib.GObservations`
        Observation container filled with simulated events
    """
    # Allocate ctobssim application and set parameters
    obssim = ctools.ctobssim(obs)
    obssim['seed']    = seed
    obssim['edisp']   = edisp
    obssim['chatter'] = chatter
    obssim['debug']   = debug

    # Optionally open the log file
    if log:
        obssim.logFileOpen()

    # Run ctobssim application. This will loop over all observations in the
    # container and simulation the events for each observation. Note that
    # events are not added together, they still apply to each observation
    # separately.
    obssim.run()

    # Binned option?
    if nbins > 0:

        # If energy boundaries are not given then determine the minimum and
        # the maximum energies from all observations and use these values
        # as energy boundaries. The energy boundaries are given in TeV.
        if emin == None or emax == None:
            emin = 1.0e30
            emax = 0.0
            for run in obssim.obs():
                emin = min(run.events().ebounds().emin().TeV(), emin)
                emax = max(run.events().ebounds().emax().TeV(), emax)

        # If a On source is specified then create On/Off observations
        if onsrc != None:

            # Extract source position from model
            model = obssim.obs().models()[onsrc]
            ra    = model['RA'].value()
            dec   = model['DEC'].value()

            # Allocate csphagen application and set parameters
            phagen = cscripts.csphagen(obssim.obs())
            phagen['ebinalg']     = 'LOG'
            phagen['emin']        = emin
            phagen['emax']        = emax
            phagen['enumbins']    = nbins
            phagen['coordsys']    = 'CEL'
            phagen['ra']          = ra
            phagen['dec']         = dec
            phagen['rad']         = onrad
            phagen['stack']       = False
            phagen['inexclusion'] = 'NONE'
            phagen['bkgmethod']   = 'REFLECTED'

            # Optionally open the log file
            if log:
                phagen.logFileOpen()

            # Run csphagen application
            phagen.run()

            # Make a deep copy of the observation that will be returned
            # (the csphagen object will go out of scope one the function is
            # left)
            obs = phagen.obs().copy()

        # ... otherwise use binned observations
        else:

            # Allocate ctbin application and set parameters
            binning = ctools.ctbin(obssim.obs())
            binning['ebinalg']  = 'LOG'
            binning['emin']     = emin
            binning['emax']     = emax
            binning['enumbins'] = nbins
            binning['usepnt']   = True # Use pointing for map centre
            binning['nxpix']    = npix
            binning['nypix']    = npix
            binning['binsz']    = binsz
            binning['coordsys'] = coord
            binning['proj']     = proj
            binning['chatter']  = chatter
            binning['debug']    = debug

            # Optionally open the log file
            if log:
                binning.logFileOpen()

            # Run ctbin application. This will loop over all observations in
            # the container and bin the events in counts maps
            binning.run()

            # If we have multiple input observations then create stacked response
            # cubes and append them to the observation
            if len(obssim.obs()) > 1:

                # Get stacked response (use pointing for map centre)
                response = get_stacked_response(obssim.obs(), None, None,
                                                binsz=binsz, nxpix=npix, nypix=npix,
                                                emin=emin, emax=emax, enumbins=nbins,
                                                edisp=edisp,
                                                coordsys=coord, proj=proj,
                                                addbounds=addbounds,
                                                log=log, debug=debug,
                                                chatter=chatter)

                # Set stacked response
                if edisp:
                    binning.obs()[0].response(response['expcube'],
                                             response['psfcube'],
                                             response['edispcube'],
                                             response['bkgcube'])
                else:
                    binning.obs()[0].response(response['expcube'],
                                              response['psfcube'],
                                              response['bkgcube'])

                # Set new models
                binning.obs().models(response['models'])

            # Make a deep copy of the observation that will be returned
            # (the ctbin object will go out of scope one the function is
            # left)
            obs = binning.obs().copy()

    else:

        # Make a deep copy of the observation that will be returned
        # (the ctobssim object will go out of scope one the function is
        # left)
        obs = obssim.obs().copy()

    # Delete the simulation
    del obssim

    # Return observation container
    return obs
Exemplo n.º 49
0
def sim(obs,
        log=False,
        debug=False,
        chatter=2,
        edisp=False,
        seed=0,
        emin=None,
        emax=None,
        nbins=0,
        binsz=0.05,
        npix=200,
        proj='TAN',
        coord='GAL'):
    """
    Simulate events for all observations in the container

    Parameters
    ----------
    obs : `~gammalib.GObservations`
        Observation container without events
    log : bool, optional
        Create log file(s)
    debug : bool, optional
        Create console dump?
    chatter : int, optional
        Chatter level
    edisp : bool, optional
        Apply energy dispersion?
    seed : int, integer
        Seed value for simulations
    emin : float, optional
        Minimum energy of counts cube for binned (TeV)
    emax : float, optional
        Maximum energy of counts cube for binned (TeV)
    nbins : int, optional
        Number of energy bins (0=unbinned)
    binsz : float, optional
        Pixel size for binned simulation (deg/pixel)
    npix : int, optional
        Number of pixels in X and Y for binned simulation
    proj : str, optional
        Projection for binned simulation
    coord : str, optional
        Coordinate system for binned simulation

    Returns
    -------
    obs : `~gammalib.GObservations`
        Observation container filled with simulated events
    """

    # Allocate ctobssim application and set parameters
    sim = ctools.ctobssim(obs)
    sim['seed'] = seed
    sim['edisp'] = edisp
    sim['chatter'] = chatter
    sim['debug'] = debug

    # Optionally open the log file
    if log:
        sim.logFileOpen()

    # Run ctobssim application. This will loop over all observations in the
    # container and simulation the events for each observation. Note that
    # events are not added together, they still apply to each observation
    # separately.
    sim.run()

    # Binned option?
    if nbins > 0:

        # If energy boundaries are not given then determine common energy
        # boundaries for all observations
        if emin == None or emax == None:
            emin = None
            emax = None
            for run in sim.obs():
                run_emin = run.events().ebounds().emin().TeV()
                run_emax = run.events().ebounds().emax().TeV()
                if emin == None:
                    emin = run_emin
                elif run_emin > emin:
                    emin = run_emin
                if emax == None:
                    emax = run_emax
                elif run_emax > emax:
                    emax = run_emax

        # Allocate ctbin application and set parameters
        bin = ctools.ctbin(sim.obs())
        bin['ebinalg'] = 'LOG'
        bin['emin'] = emin
        bin['emax'] = emax
        bin['enumbins'] = nbins
        bin['usepnt'] = True  # Use pointing for map centre
        bin['nxpix'] = npix
        bin['nypix'] = npix
        bin['binsz'] = binsz
        bin['coordsys'] = coord
        bin['proj'] = proj
        bin['chatter'] = chatter
        bin['debug'] = debug

        # Optionally open the log file
        if log:
            bin.logFileOpen()

        # Run ctbin application. This will loop over all observations in
        # the container and bin the events in counts maps
        bin.run()

        # Make a deep copy of the observation that will be returned
        # (the ctbin object will go out of scope one the function is
        # left)
        obs = bin.obs().copy()

    else:

        # Make a deep copy of the observation that will be returned
        # (the ctobssim object will go out of scope one the function is
        # left)
        obs = sim.obs().copy()

    # Delete the simulation
    del sim

    # Return observation container
    return obs
Exemplo n.º 50
0
def sim(obs, log=False, debug=False, seed=0, nbins=0, binsz=0.05, npix=200):
    """
    Simulate events for all observations in the container.
    
    Parameters:
     obs   - Observation container
    Keywords:
     log   - Create log file(s)
     debug - Create screen dump
     seed  - Seed value for simulations (default: 0)
     nbins - Number of energy bins (default: 0=unbinned)
     binsz - Pixel size for binned simulation (deg/pixel)
     npix  - Number of pixels in X and Y for binned simulation
    """
    # Allocate ctobssim application and set parameters
    sim = ctools.ctobssim(obs)
    sim['seed'].integer(seed)
        
    # Optionally open the log file
    if log:
        sim.logFileOpen()

    # Optionally switch-on debugging model
    if debug:
        sim["debug"].boolean(True)
    
    # Run ctobssim application. This will loop over all observations in the
    # container and simulation the events for each observation. Note that
    # events are not added together, they still apply to each observation
    # separately.
    sim.run()
    
    # Binned option?
    if nbins > 0:
    
        # Determine common energy boundaries for observations
        emin = None
        emax = None
        for run in sim.obs():
            run_emin = run.events().ebounds().emin().TeV()
            run_emax = run.events().ebounds().emax().TeV()
            if emin == None:
                emin = run_emin
            elif run_emin > emin:
                emin = run_emin
            if emax == None:
                emax = run_emax
            elif run_emax > emax:
                emax = run_emax
    
        # Allocate ctbin application and set parameters
        bin = ctools.ctbin(sim.obs())
        bin["ebinalg"].string("LOG")
        bin["emin"].real(emin)
        bin["emax"].real(emax)
        bin["enumbins"].integer(nbins)
        bin["usepnt"].boolean(True) # Use pointing for map centre
        bin["nxpix"].integer(npix)
        bin["nypix"].integer(npix)
        bin["binsz"].real(binsz)
        bin["coordsys"].string("GAL")
        bin["proj"].string("TAN")
        
        # Optionally open the log file
        if log:
            bin.logFileOpen()

        # Optionally switch-on debugging model
        if debug:
            bin["debug"].boolean(True)

        # Run ctbin application. This will loop over all observations in
        # the container and bin the events in counts maps
        bin.run()
        
        # Make a deep copy of the observation that will be returned
        # (the ctbin object will go out of scope one the function is
        # left)
        obs = bin.obs().copy()
        
    else:
        
        # Make a deep copy of the observation that will be returned
        # (the ctobssim object will go out of scope one the function is
        # left)
        obs = sim.obs().copy()
 
    # Delete the simulation
    del sim
    
    # Return observation container
    return obs
Exemplo n.º 51
0
#    sor_seed = numpy.sort(seeds)
#    equal_el = 0
#    for j in xrange(len(sor_seed)-1):
#        if sor_seed[j] != sor_seed[j+1]:
#            equal_el = equal_el+1
#	if equal_el == len(sor_seed)-1:  
#            seed_index = 1

start_time_MET = 662774400
end_time_MET = 662774400 + 3600*n_hours

print 'starting simulation...'

#for i in range(1, n_simulations+1):
from ctools import ctobssim
sm = ctools.ctobssim()
sm["inmodel"]=config["file"]["inmodel_sim"]
sm["outevents"]= srcname+"_sim"+".fits"
sm["caldb"]= config["irfs"]["caldb"]
sm["irf"]= config["irfs"]["irf"]
#sm["edisp"]=yes
#sm["seed"]=seeds[i-1]
sm["ra"]= config["target"]["ra"]
sm["dec"]= config["target"]["dec"]
sm["rad"]= 3
sm["tmin"]= start_time_MET
sm["tmax"]= end_time_MET
#sm["emin"]= config["simulation"]["emin_sim"]
#sm["emax"]= config["simulation"]["emax_sim"]
sm["emin"]= 0.03
sm["emax"]= 12.5
Exemplo n.º 52
0
def stacked_pipeline(model_name, duration):
    """
    Stacked analysis pipeline.
    """
    # Set script parameters
    caldb = "prod2"
    irf = "South_50h"
    ra = 83.63
    dec = 22.01
    rad_sim = 10.0
    tstart = 0.0
    tstop = duration
    emin = 0.1
    emax = 100.0
    enumbins = 40
    nxpix = 200
    nypix = 200
    binsz = 0.02
    coordsys = "CEL"
    proj = "CAR"

    # Get start CPU time
    cpu_start = time.clock()

    # Simulate events
    sim = ctools.ctobssim()
    sim["inmodel"] = model_name
    sim["caldb"] = caldb
    sim["irf"] = irf
    sim["ra"] = ra
    sim["dec"] = dec
    sim["rad"] = rad_sim
    sim["tmin"] = tstart
    sim["tmax"] = tstop
    sim["emin"] = emin
    sim["emax"] = emax
    sim.run()

    # Bin events into counts map
    bin = ctools.ctbin(sim.obs())
    bin["ebinalg"] = "LOG"
    bin["emin"] = emin
    bin["emax"] = emax
    bin["enumbins"] = enumbins
    bin["nxpix"] = nxpix
    bin["nypix"] = nypix
    bin["binsz"] = binsz
    bin["coordsys"] = coordsys
    bin["proj"] = proj
    bin["xref"] = ra
    bin["yref"] = dec
    bin.run()

    # Create exposure cube
    expcube = ctools.ctexpcube(sim.obs())
    expcube["incube"] = "NONE"
    expcube["caldb"] = caldb
    expcube["irf"] = irf
    expcube["ebinalg"] = "LOG"
    expcube["emin"] = emin
    expcube["emax"] = emax
    expcube["enumbins"] = enumbins
    expcube["nxpix"] = nxpix
    expcube["nypix"] = nypix
    expcube["binsz"] = binsz
    expcube["coordsys"] = coordsys
    expcube["proj"] = proj
    expcube["xref"] = ra
    expcube["yref"] = dec
    expcube.run()

    # Create PSF cube
    psfcube = ctools.ctpsfcube(sim.obs())
    psfcube["incube"] = "NONE"
    psfcube["caldb"] = caldb
    psfcube["irf"] = irf
    psfcube["ebinalg"] = "LOG"
    psfcube["emin"] = emin
    psfcube["emax"] = emax
    psfcube["enumbins"] = enumbins
    psfcube["nxpix"] = 10
    psfcube["nypix"] = 10
    psfcube["binsz"] = 1.0
    psfcube["coordsys"] = coordsys
    psfcube["proj"] = proj
    psfcube["xref"] = ra
    psfcube["yref"] = dec
    psfcube.run()

    # Create background cube
    bkgcube = ctools.ctbkgcube(sim.obs())
    bkgcube["incube"] = "NONE"
    bkgcube["ebinalg"] = "LOG"
    bkgcube["emin"] = emin
    bkgcube["emax"] = emax
    bkgcube["enumbins"] = enumbins
    bkgcube["nxpix"] = 10
    bkgcube["nypix"] = 10
    bkgcube["binsz"] = 1.0
    bkgcube["coordsys"] = coordsys
    bkgcube["proj"] = proj
    bkgcube["xref"] = ra
    bkgcube["yref"] = dec
    bkgcube.run()

    # Attach background model to observation container
    bin.obs().models(bkgcube.models())

    # Set Exposure and Psf cube for first CTA observation
    # (ctbin will create an observation with a single container)
    bin.obs()[0].response(expcube.expcube(), psfcube.psfcube(),
                          bkgcube.bkgcube())

    # Get ctlike start CPU time
    cpu_ctlike = time.clock()

    # Perform maximum likelihood fitting
    like = ctools.ctlike(bin.obs())
    like.run()

    # Get stop CPU time and compute elapsed times
    cpu_stop = time.clock()
    cpu_elapsed = cpu_stop - cpu_start
    cpu_ctlike = cpu_stop - cpu_ctlike

    # Return
    return cpu_elapsed, cpu_ctlike
Exemplo n.º 53
0
def run_pipeline(obs, ra=83.63, dec=22.01, emin=0.1, emax=100.0,
                 enumbins=20, nxpix=200, nypix=200, binsz=0.02,
                 coordsys="CEL", proj="CAR",
                 model="${CTOOLS}/share/models/crab.xml",
                 caldb="prod2", irf="South_50h",
                 debug=False):
    """
    Simulation and stacked analysis pipeline.

    Keywords:
     ra       - RA of cube centre [deg] (default: 83.6331)
     dec      - DEC of cube centre [deg] (default: 22.0145)
     emin     - Minimum energy of cube [TeV] (default: 0.1)
     emax     - Maximum energy of cube [TeV] (default: 100.0)
     enumbins - Number of energy bins in cube (default: 20)
     nxpix    - Number of RA pixels in cube (default: 200)
     nypix    - Number of DEC pixels in cube (default: 200)
     binsz    - Spatial cube bin size [deg] (default: 0.02)
     coordsys - Cube coordinate system (CEL or GAL)
     proj     - Cube World Coordinate System (WCS) projection
     debug    - Enable debugging (default: False)
    """
    # Simulate events
    sim = ctools.ctobssim(obs)
    sim["debug"]     = debug
    sim["outevents"] = "obs.xml"
    sim.execute()

    # Bin events into counts map
    bin = ctools.ctbin()
    bin["inobs"]    = "obs.xml"
    bin["outcube"]  = "cntcube.fits"
    bin["ebinalg"]  = "LOG"
    bin["emin"]     = emin
    bin["emax"]     = emax
    bin["enumbins"] = enumbins
    bin["nxpix"]    = nxpix
    bin["nypix"]    = nypix
    bin["binsz"]    = binsz
    bin["coordsys"] = coordsys
    bin["proj"]     = proj
    bin["xref"]     = ra
    bin["yref"]     = dec
    bin["debug"]    = debug
    bin.execute()

    # Create exposure cube
    expcube = ctools.ctexpcube()
    expcube["inobs"]    = "obs.xml"
    expcube["incube"]   = "cntcube.fits"
    expcube["outcube"]  = "expcube.fits"
    expcube["caldb"]    = caldb
    expcube["irf"]      = irf
    expcube["ebinalg"]  = "LOG"
    expcube["emin"]     = emin
    expcube["emax"]     = emax
    expcube["enumbins"] = enumbins
    expcube["nxpix"]    = nxpix
    expcube["nypix"]    = nypix
    expcube["binsz"]    = binsz
    expcube["coordsys"] = coordsys
    expcube["proj"]     = proj
    expcube["xref"]     = ra
    expcube["yref"]     = dec
    expcube["debug"]    = debug
    expcube.execute()

    # Create PSF cube
    psfcube = ctools.ctpsfcube()
    psfcube["inobs"]    = "obs.xml"
    psfcube["incube"]   = "NONE"
    psfcube["outcube"]  = "psfcube.fits"
    psfcube["caldb"]    = caldb
    psfcube["irf"]      = irf
    psfcube["ebinalg"]  = "LOG"
    psfcube["emin"]     = emin
    psfcube["emax"]     = emax
    psfcube["enumbins"] = enumbins
    psfcube["nxpix"]    = 10
    psfcube["nypix"]    = 10
    psfcube["binsz"]    = 1.0
    psfcube["coordsys"] = coordsys
    psfcube["proj"]     = proj
    psfcube["xref"]     = ra
    psfcube["yref"]     = dec
    psfcube["debug"]    = debug
    psfcube.execute()

    # Create background cube
    bkgcube = ctools.ctbkgcube()
    bkgcube["inobs"]    = "obs.xml"
    bkgcube["inmodel"]  = model
    bkgcube["incube"]   = "cntcube.fits"
    bkgcube["outcube"]  = "bkgcube.fits"
    bkgcube["outmodel"] = "model_bkg.xml"
    bkgcube["caldb"]    = caldb
    bkgcube["irf"]      = irf
    bkgcube["ebinalg"]  = "LOG"
    bkgcube["emin"]     = emin
    bkgcube["emax"]     = emax
    bkgcube["enumbins"] = enumbins
    bkgcube["nxpix"]    = 10
    bkgcube["nypix"]    = 10
    bkgcube["binsz"]    = 1.0
    bkgcube["coordsys"] = coordsys
    bkgcube["proj"]     = proj
    bkgcube["xref"]     = ra
    bkgcube["yref"]     = dec
    bkgcube["debug"]    = debug
    bkgcube.execute()

    # Perform maximum likelihood fitting
    like = ctools.ctlike()
    like["inobs"]    = "cntcube.fits"
    like["inmodel"]  = "model_bkg.xml"
    like["outmodel"] = "fit_results.xml"
    like["expcube"]  = "expcube.fits"
    like["psfcube"]  = "psfcube.fits"
    like["bkgcube"]  = "bkgcube.fits"
    like["caldb"]    = caldb
    like["irf"]      = irf
    like["debug"]    = True # Switch this always on for results in console
    like.execute()

    # Return
    return
Exemplo n.º 54
0
    def _test_ctobssim_python(self):
        """
        Test ctobssim from Python
        """
        # Allocate ctobssim
        sim = ctools.ctobssim()

        # Check that empty ctobssim tool holds an empty observation
        self.test_value(sim.obs().size(), 0, 'Check number of observations')

        # Check that saving saves an empty model definition file
        sim['outevents'] = 'ctobssim_py0.fits'
        sim['logfile']   = 'ctobssim_py0.log'
        sim.logFileOpen()
        sim.save()
        self.test_assert(not os.path.isfile('ctobssim_py0.fits'),
             'Check that no event list has been created')

        # Check that clearing does not lead to an exception or segfault
        sim.clear()

        # Now set ctobssim parameters
        sim = ctools.ctobssim()
        sim['inmodel']   = self._model
        sim['caldb']     = self._caldb
        sim['irf']       = self._irf
        sim['ra']        = 83.63
        sim['dec']       = 22.01
        sim['rad']       = 3.0
        sim['tmin']      = '2020-01-01T00:00:00'
        sim['tmax']      = '2020-01-01T00:05:00'
        sim['emin']      = 0.1
        sim['emax']      = 100.0
        sim['outevents'] = 'ctobssim_py1.fits'
        sim['logfile']   = 'ctobssim_py1.log'
        sim['chatter']   = 2

        # Run tool
        sim.logFileOpen()
        sim.run()

        # Check content of observation
        self._test_observation(sim)
        self._test_list(sim.obs()[0].events(), 3775)

        # Save events
        sim.save()

        # Load counts cube and check content.
        evt = gammalib.GCTAEventList('ctobssim_py1.fits')
        self._test_list(evt, 3775)

        # Copy ctobssim tool
        cpy_sim = sim.copy()

        # Retrieve observation and check content of copy
        self._test_observation(cpy_sim)
        self._test_list(cpy_sim.obs()[0].events(), 3775)

        # Execute copy of ctobssim tool again, now with a higher chatter
        # level than before
        cpy_sim['outevents'] = 'ctobssim_py2.fits'
        cpy_sim['logfile']   = 'ctobssim_py2.log'
        cpy_sim['chatter']   = 3
        cpy_sim['publish']   = True
        cpy_sim.logFileOpen()  # Needed to get a new log file
        cpy_sim.execute()

        # Load counts cube and check content.
        evt = gammalib.GCTAEventList('ctobssim_py2.fits')
        self._test_list(evt, 3775)

        # Set-up observation container
        pnts = [{'ra': 83.63, 'dec': 21.01},
                {'ra': 84.63, 'dec': 22.01}]
        obs = obsutils.set_obs_list(pnts, caldb=self._caldb, irf=self._irf,
                                    duration=300.0, rad=3.0)

        # Set-up ctobssim tool from observation container
        sim = ctools.ctobssim(obs)
        sim['inmodel']   = self._model
        sim['outevents'] = 'ctobssim_py3.xml'
        sim['logfile']   = 'ctobssim_py3.log'
        sim['chatter']   = 3

        # Double maximum event range
        max_rate = sim.max_rate()
        sim.max_rate(2.0*max_rate)
        self.test_value(sim.max_rate(), 2.0*max_rate, 1.0e-3,
                        'Check setting of maximum event rate')

        # Run ctobssim tool
        sim.logFileOpen()
        sim.run()

        # Retrieve observation and check content
        self._test_observation(sim, nobs=2, pnts=pnts)
        self._test_list(sim.obs()[0].events(), 3569)
        self._test_list(sim.obs()[1].events(), 3521)

        # Save events
        sim.save()

        # Load events
        obs = gammalib.GObservations('ctobssim_py3.xml')

        # Retrieve observation and check content
        self._test_list(obs[0].events(), 3569)
        self._test_list(obs[1].events(), 3521)

        # Return
        return
Exemplo n.º 55
0
    def _test_ctobssim_python(self):
        """
        Test ctobssim from Python
        """
        # Allocate ctobssim
        sim = ctools.ctobssim()

        # Check that empty ctobssim tool holds an empty observation
        self.test_value(sim.obs().size(), 0, 'Check number of observations')

        # Check that saving saves an empty model definition file
        sim['outevents'] = 'ctobssim_py0.fits'
        sim['logfile']   = 'ctobssim_py0.log'
        sim.logFileOpen()
        sim.save()
        self.test_assert(not os.path.isfile('ctobssim_py0.fits'),
             'Check that no event list has been created')

        # Check that clearing does not lead to an exception or segfault
        sim.clear()

        # Now set ctobssim parameters
        sim = ctools.ctobssim()
        sim['inmodel']   = self._model
        sim['caldb']     = self._caldb
        sim['irf']       = self._irf
        sim['ra']        = 83.63
        sim['dec']       = 22.01
        sim['rad']       = 2.0
        sim['tmin']      = '2020-01-01T00:00:00'
        sim['tmax']      = '2020-01-01T00:05:00'
        sim['emin']      = 1.0
        sim['emax']      = 100.0
        sim['outevents'] = 'ctobssim_py1.fits'
        sim['logfile']   = 'ctobssim_py1.log'
        sim['chatter']   = 2

        # Run tool
        sim.logFileOpen()
        sim.run()

        # Check content of observation
        self._test_observation(sim)
        self._test_list(sim.obs()[0].events(), 261)

        # Save events
        sim.save()

        # Load counts cube and check content.
        evt = gammalib.GCTAEventList('ctobssim_py1.fits')
        self._test_list(evt, 261)

        # Copy ctobssim tool
        cpy_sim = sim.copy()

        # Retrieve observation and check content of copy
        self._test_observation(cpy_sim)
        self._test_list(cpy_sim.obs()[0].events(), 261)

        # Execute copy of ctobssim tool again, now with a higher chatter
        # level than before
        cpy_sim['outevents'] = 'ctobssim_py2.fits'
        cpy_sim['logfile']   = 'ctobssim_py2.log'
        cpy_sim['chatter']   = 3
        cpy_sim['publish']   = True
        cpy_sim.logFileOpen()  # Needed to get a new log file
        cpy_sim.execute()

        # Load counts cube and check content.
        evt = gammalib.GCTAEventList('ctobssim_py2.fits')
        self._test_list(evt, 261)

        # Set-up observation container
        pnts = [{'ra': 83.63, 'dec': 21.01},
                {'ra': 84.63, 'dec': 22.01}]
        obs = obsutils.set_obs_list(pnts, caldb=self._caldb, irf=self._irf,
                                    duration=300.0, rad=3.0)

        # Set-up ctobssim tool from observation container
        sim = ctools.ctobssim(obs)
        sim['inmodel']   = self._model
        sim['outevents'] = 'ctobssim_py3.xml'
        sim['logfile']   = 'ctobssim_py3.log'
        sim['chatter']   = 3

        # Double maximum event range
        max_rate = sim.max_rate()
        sim.max_rate(2.0*max_rate)
        self.test_value(sim.max_rate(), 2.0*max_rate, 1.0e-3,
                        'Check setting of maximum event rate')

        # Run ctobssim tool
        sim.logFileOpen()
        sim.run()

        # Retrieve observation and check content
        self._test_observation(sim, nobs=2, pnts=pnts)
        self._test_list(sim.obs()[0].events(), 3630)
        self._test_list(sim.obs()[1].events(), 3594)

        # Save events
        sim.save()

        # Load events
        obs = gammalib.GObservations('ctobssim_py3.xml')

        # Retrieve observation and check content
        self._test_list(obs[0].events(), 3630)
        self._test_list(obs[1].events(), 3594)

        # Return
        return
Exemplo n.º 56
0
def run_pipeline(obs, ra=83.63, dec=22.01, emin=0.1, emax=100.0,
                 enumbins=20, nxpix=200, nypix=200, binsz=0.02,
                 coordsys="CEL", proj="CAR", debug=False):
    """
    Simulation and stacked analysis pipeline.

    Keywords:
     ra       - RA of cube centre [deg] (default: 83.6331)
     dec      - DEC of cube centre [deg] (default: 22.0145)
     emin     - Minimum energy of cube [TeV] (default: 0.1)
     emax     - Maximum energy of cube [TeV] (default: 100.0)
     enumbins - Number of energy bins in cube (default: 20)
     nxpix    - Number of RA pixels in cube (default: 200)
     nypix    - Number of DEC pixels in cube (default: 200)
     binsz    - Spatial cube bin size [deg] (default: 0.02)
     coordsys - Cube coordinate system (CEL or GAL)
     proj     - Cube World Coordinate System (WCS) projection
     debug    - Enable debugging (default: False)
    """
    # Simulate events
    sim = ctools.ctobssim(obs)
    sim["debug"] = debug
    sim.run()

    # Bin events into counts map
    bin = ctools.ctbin(sim.obs())
    bin["ebinalg"]  = "LOG"
    bin["emin"]     = emin
    bin["emax"]     = emax
    bin["enumbins"] = enumbins
    bin["nxpix"]    = nxpix
    bin["nypix"]    = nypix
    bin["binsz"]    = binsz
    bin["coordsys"] = coordsys
    bin["proj"]     = proj
    bin["xref"]     = ra
    bin["yref"]     = dec
    bin["debug"]    = debug
    bin.run()

    # Create exposure cube
    expcube = ctools.ctexpcube(sim.obs())
    expcube["incube"]   = "NONE"
    expcube["ebinalg"]  = "LOG"
    expcube["emin"]     = emin
    expcube["emax"]     = emax
    expcube["enumbins"] = enumbins
    expcube["nxpix"]    = nxpix
    expcube["nypix"]    = nypix
    expcube["binsz"]    = binsz
    expcube["coordsys"] = coordsys
    expcube["proj"]     = proj
    expcube["xref"]     = ra
    expcube["yref"]     = dec
    expcube["debug"]    = debug
    expcube.run()

    # Create PSF cube
    psfcube = ctools.ctpsfcube(sim.obs())
    psfcube["incube"]   = "NONE"
    psfcube["ebinalg"]  = "LOG"
    psfcube["emin"]     = emin
    psfcube["emax"]     = emax
    psfcube["enumbins"] = enumbins
    psfcube["nxpix"]    = 10
    psfcube["nypix"]    = 10
    psfcube["binsz"]    = 1.0
    psfcube["coordsys"] = coordsys
    psfcube["proj"]     = proj
    psfcube["xref"]     = ra
    psfcube["yref"]     = dec
    psfcube["debug"]    = debug
    psfcube.run()

    # Create background cube
    bkgcube = ctools.ctbkgcube(sim.obs())
    bkgcube["incube"]   = "NONE"
    bkgcube["ebinalg"]  = "LOG"
    bkgcube["emin"]     = emin
    bkgcube["emax"]     = emax
    bkgcube["enumbins"] = enumbins
    bkgcube["nxpix"]    = 10
    bkgcube["nypix"]    = 10
    bkgcube["binsz"]    = 1.0
    bkgcube["coordsys"] = coordsys
    bkgcube["proj"]     = proj
    bkgcube["xref"]     = ra
    bkgcube["yref"]     = dec
    bkgcube["debug"]    = debug
    bkgcube.run()

    # Attach background model to observation container
    bin.obs().models(bkgcube.models())

    # Set Exposure and Psf cube for first CTA observation
    # (ctbin will create an observation with a single container)
    bin.obs()[0].response(expcube.expcube(), psfcube.psfcube(), bkgcube.bkgcube())

    # Perform maximum likelihood fitting
    like = ctools.ctlike(bin.obs())
    like["debug"] = True # Switch this always on for results in console
    like.run()

    # Return
    return
Exemplo n.º 57
0
def run_pipeline(obs, emin=0.1, emax=100.0,
                 enumbins=20, nxpix=200, nypix=200, binsz=0.02,
                 coordsys='CEL', proj='CAR',
                 model='data/crab.xml',
                 caldb='prod2', irf='South_0.5h',
                 debug=False):
    """
    Simulation and binned analysis pipeline

    Parameters
    ----------
    obs : `~gammalib.GObservations`
        Observation container
    emin : float, optional
        Minimum energy (TeV)
    emax : float, optional
        Maximum energy (TeV)
    enumbins : int, optional
        Number of energy bins
    nxpix : int, optional
        Number of pixels in X axis
    nypix : int, optional
        Number of pixels in Y axis
    binsz : float, optional
        Pixel size (deg)
    coordsys : str, optional
        Coordinate system
    proj : str, optional
        Coordinate projection
    model : str, optional
        Model definition XML file
    caldb : str, optional
        Calibration database path
    irf : str, optional
        Instrument response function
    debug : bool, optional
        Debug function
    """
    # Simulate events
    sim = ctools.ctobssim(obs)
    sim['debug']     = debug
    sim['outevents'] = 'obs.xml'
    sim.execute()

    # Bin events by looping over all observations in the container
    sim_obs = gammalib.GObservations('obs.xml')
    obs     = gammalib.GObservations()
    for run in sim_obs:

        # Get event filename and set counts cube filename
        eventfile = run.eventfile().url()
        cubefile  = 'cube_'+eventfile

        # Bin events for that observation
        bin = ctools.ctbin()
        bin['inobs']    = eventfile
        bin['outcube']  = cubefile
        bin['ebinalg']  = 'LOG'
        bin['emin']     = emin
        bin['emax']     = emax
        bin['enumbins'] = enumbins
        bin['nxpix']    = nxpix
        bin['nypix']    = nypix
        bin['binsz']    = binsz
        bin['coordsys'] = coordsys
        bin['usepnt']   = True
        bin['proj']     = proj
        bin.execute()

        # Set observation ID
        bin.obs()[0].id(cubefile)
        bin.obs()[0].eventfile(cubefile)

        # Append result to observations
        obs.extend(bin.obs())

    # Save XML file
    xml = gammalib.GXml()
    obs.write(xml)
    xml.save('obs_cube.xml')

    # Perform maximum likelihood fitting
    like = ctools.ctlike()
    like['inobs']    = 'obs_cube.xml'
    like['inmodel']  = model
    like['outmodel'] = 'fit_results.xml'
    like['expcube']  = 'NONE'
    like['psfcube']  = 'NONE'
    like['bkgcube']  = 'NONE'
    like['caldb']    = caldb
    like['irf']      = irf
    like['debug']    = True # Switch this always on for results in console
    like.execute()

    # Return
    return
Exemplo n.º 58
0
 def test_unbinned_fits(self):
     """
     Test unbinned pipeline with FITS file saving.
     """
     # Set script parameters
     model_name           = "data/crab.xml"
     events_name          = "events.fits"
     selected_events_name = "selected_events.fits"
     result_name          = "results.xml"
     caldb                = "irf"
     irf                  = "cta_dummy_irf"
     ra                   =   83.63
     dec                  =   22.01
     rad_sim              =   10.0
     tstart               =    0.0
     tstop                = 1800.0
     emin                 =    0.1
     emax                 =  100.0
     rad_select           =    3.0
 
     # Simulate events
     sim = ctools.ctobssim()
     sim["inmodel"].filename(model_name)
     sim["outevents"].filename(events_name)
     sim["caldb"].string(caldb)
     sim["irf"].string(irf)
     sim["ra"].real(ra)
     sim["dec"].real(dec)
     sim["rad"].real(rad_sim)
     sim["tmin"].real(tstart)
     sim["tmax"].real(tstop)
     sim["emin"].real(emin)
     sim["emax"].real(emax)
     self.test_try("Execute ctobssim")
     try:
         sim.execute()
         self.test_try_success()
     except:
         self.test_try_failure("Exception occured in ctobssim.")
 
     # Select events
     select = ctools.ctselect()
     select["inobs"].filename(events_name)
     select["outobs"].filename(selected_events_name)
     select["ra"].real(ra)
     select["dec"].real(dec)
     select["rad"].real(rad_select)
     select["tmin"].real(tstart)
     select["tmax"].real(tstop)
     select["emin"].real(emin)
     select["emax"].real(emax)
     self.test_try("Execute ctselect")
     try:
         select.execute()
         self.test_try_success()
     except:
         self.test_try_failure("Exception occured in ctselect.")
 
     # Perform maximum likelihood fitting
     like = ctools.ctlike()
     like["inobs"].filename(selected_events_name)
     like["inmodel"].filename(model_name)
     like["outmodel"].filename(result_name)
     like["caldb"].string(caldb)
     like["irf"].string(irf)
     self.test_try("Execute ctlike")
     try:
         like.execute()
         self.test_try_success()
     except:
         self.test_try_failure("Exception occured in ctlike.")