sim_and_analisys.py

Rodriguez Fernandez Gonzalo, 06/16/2017 10:55 AM

Download (2.22 KB)

 
1
#! /usr/bin/env python
2

    
3
import gammalib
4
import ctools
5
import cscripts
6
import numpy as np
7

    
8
# ======================== #
9
# Main routine entry point #
10
# ======================== #
11
if __name__ == '__main__':
12

    
13
    ra         = 0.0
14
    dec        = 0.0
15
    obs_offset = 1.0
16

    
17
    noff       = 4
18
    size       = 0.3 #degrees
19
    off_offset = 1.0
20

    
21
    nbins = 31
22
    duration = 1800
23
    emin     = 0.03
24
    emax     = 100
25
    rad      = 5.0
26
    caldb    = 'prod3b'
27
    irf      = 'South_z20_30m'
28
    
29
    pattern = cscripts.obsutils.set_obs_patterns("single", ra=ra, dec=dec, offset=obs_offset)
30
    obs = cscripts.obsutils.set_obs_list(pattern, duration=duration, emin=emin, emax=emax, rad=rad, caldb=caldb, irf=irf)
31

    
32
    xmlname  = './crab.xml'
33
    obs.models(gammalib.GModels(xmlname))
34

    
35
    obs = cscripts.obsutils.sim(obs)
36
    
37
    # Define ON region
38
    ondir = gammalib.GSkyDir()
39
    ondir.radec_deg(ra,dec)
40
    
41
    on    = gammalib.GSkyRegions()
42
    onreg = gammalib.GSkyRegionCircle(ondir,size)
43
    on.append(onreg)
44

    
45
    # Define OFF regions
46
    off = gammalib.GSkyRegions()
47
    ra_off  = [0,1,0,-1]
48
    dec_off = [1,0,-1,-1]
49
    for i in range(noff):
50
        
51
        offdir = gammalib.GSkyDir()
52
        offdir.radec_deg(ra_off[i], dec_off[i])
53
                
54
        offreg = gammalib.GSkyRegionCircle(offdir, size)
55
        off.append(offreg)
56

    
57

    
58
    # Define energy binning
59
    etrue = gammalib.GEbounds(nbins, gammalib.GEnergy(emin, "TeV"), gammalib.GEnergy(emax, "TeV"))
60
    ereco = gammalib.GEbounds(nbins, gammalib.GEnergy(emin, "TeV"), gammalib.GEnergy(emax, "TeV"))
61

    
62
    onoff_container = gammalib.GObservations()
63
    obs_onoff = gammalib.GCTAOnOffObservation(obs[0], etrue, ereco, on, off)
64
    obs_onoff.name("TestCrab")
65
    obs_onoff.id("0001")
66
    
67
    onoff_container.append(obs_onoff)
68
    onoff_container.models(gammalib.GModels('crab_fitmodel.xml'))
69
       
70
    # Set optimizer and logger
71
    opt = gammalib.GOptimizerLM()
72
    onoff_container.optimize(opt)
73
    onoff_container.errors(opt)
74
    
75
    print(onoff_container.models())
76

    
77
    fit_models = onoff_container.models()
78
    gradient  = gammalib.GVector()
79
    curvature = gammalib.GMatrixSparse()
80
    npred = []
81
    value = onoff_container[0].likelihood(fit_models, gradient, curvature, npred)
82
    
83
 
84