pipeline_stacked_mem.py

Ziegler Alexander, 11/25/2016 07:30 AM

Download (5.74 KB)

 
1
#! /usr/bin/env python
2
# ==========================================================================
3
# Perform stacked in-memory analysis of simulated CTA data
4
#
5
# Copyright (C) 2014-2016 Juergen Knoedlseder
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
#
20
# ==========================================================================
21
import gammalib
22
import ctools
23
import cscripts
24

    
25

    
26
# ================================ #
27
# Simulation and analysis pipeline #
28
# ================================ #
29
def run_pipeline(obs, ra=83.63, dec=22.01, emin=0.1, emax=100.0,
30
                 enumbins=20, nxpix=200, nypix=200, binsz=0.02,
31
                 coordsys='CEL', proj='CAR', debug=False):
32
    """
33
    Simulation and stacked analysis pipeline
34

35
    Parameters
36
    ----------
37
    obs : `~gammalib.GObservations`
38
        Observation container
39
    ra : float, optional
40
        Right Ascension of counts cube centre (deg)
41
    dec : float, optional
42
        Declination of Region of counts cube centre (deg)
43
    emin : float, optional
44
        Minimum energy (TeV)
45
    emax : float, optional
46
        Maximum energy (TeV)
47
    enumbins : int, optional
48
        Number of energy bins
49
    nxpix : int, optional
50
        Number of pixels in X axis
51
    nypix : int, optional
52
        Number of pixels in Y axis
53
    binsz : float, optional
54
        Pixel size (deg)
55
    coordsys : str, optional
56
        Coordinate system
57
    proj : str, optional
58
        Coordinate projection
59
    debug : bool, optional
60
        Debug function
61
    """
62
    # Simulate events
63
    sim = ctools.ctobssim(obs)
64
    sim['debug']     = debug
65
    sim.run()
66

    
67
    # Bin events into counts map
68
    bin = ctools.ctbin(sim.obs())
69
    bin['ebinalg']  = 'LOG'
70
    bin['emin']     = emin
71
    bin['emax']     = emax
72
    bin['enumbins'] = enumbins
73
    bin['nxpix']    = nxpix
74
    bin['nypix']    = nypix
75
    bin['binsz']    = binsz
76
    bin['coordsys'] = coordsys
77
    bin['proj']     = proj
78
    bin['xref']     = ra
79
    bin['yref']     = dec
80
    bin['debug']    = debug
81
    bin.run()
82

    
83
    # Create exposure cube
84
    expcube = ctools.ctexpcube(sim.obs())
85
    expcube['incube']   = 'NONE'
86
    expcube['ebinalg']  = 'LOG'
87
    expcube['emin']     = emin
88
    expcube['emax']     = emax
89
    expcube['enumbins'] = enumbins
90
    expcube['nxpix']    = nxpix
91
    expcube['nypix']    = nypix
92
    expcube['binsz']    = binsz
93
    expcube['coordsys'] = coordsys
94
    expcube['proj']     = proj
95
    expcube['xref']     = ra
96
    expcube['yref']     = dec
97
    expcube['debug']    = debug
98
    expcube.run()
99

    
100
    # Create PSF cube
101
    psfcube = ctools.ctpsfcube(sim.obs())
102
    psfcube['incube']   = 'NONE'
103
    psfcube['ebinalg']  = 'LOG'
104
    psfcube['emin']     = emin
105
    psfcube['emax']     = emax
106
    psfcube['enumbins'] = enumbins
107
    psfcube['nxpix']    = 10
108
    psfcube['nypix']    = 10
109
    psfcube['binsz']    = 1.0
110
    psfcube['coordsys'] = coordsys
111
    psfcube['proj']     = proj
112
    psfcube['xref']     = ra
113
    psfcube['yref']     = dec
114
    psfcube['debug']    = debug
115
    psfcube.run()
116

    
117
    # Create background cube
118
    bkgcube = ctools.ctbkgcube(sim.obs())
119
    bkgcube['incube']   = 'NONE'
120
    bkgcube['ebinalg']  = 'LOG'
121
    bkgcube['emin']     = emin
122
    bkgcube['emax']     = emax
123
    bkgcube['enumbins'] = enumbins
124
    bkgcube['nxpix']    = 10
125
    bkgcube['nypix']    = 10
126
    bkgcube['binsz']    = 1.0
127
    bkgcube['coordsys'] = coordsys
128
    bkgcube['proj']     = proj
129
    bkgcube['xref']     = ra
130
    bkgcube['yref']     = dec
131
    bkgcube['debug']    = debug
132
    bkgcube.run()
133

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

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

    
141
    # Perform maximum likelihood fitting
142
    like = ctools.ctlike(bin.obs())
143
    like['debug'] = True # Switch this always on for results in console
144
    like.run()
145

    
146
    # Return
147
    return
148

    
149

    
150
# ============================== #
151
# Run stacked in-memory pipeline #
152
# ============================== #
153
def pipeline_stacked_mem():
154
    """
155
    Run stacked in-memory pipeline
156
    """
157
    # Set usage string
158
    usage = 'pipeline_stacked_mem.py [-d datadir]'
159

    
160
    # Set default options
161
    options = [{'option': '-d', 'value': 'data'}]
162

    
163
    # Get arguments and options from command line arguments
164
    args, options = cscripts.ioutils.get_args_options(options, usage)
165

    
166
    # Extract script parameters from options
167
    datadir = options[0]['value']
168

    
169
    # Setup observations
170
    obs = cscripts.obsutils.set_observations(83.63, 22.01, 5.0, 0.0, 180.0,
171
                                             0.1, 100.0, 'South_0.5h', 'prod2',
172
                                             pattern='four', offset=1.5)
173

    
174
    # Setup model
175
    #obs.models(gammalib.GModels(datadir+'/crab.xml'))
176

    
177
    # Setup with composite model
178
    obs.models(gammalib.GModels(datadir+'/model_spatial_composite.xml'))
179

    
180
    # Run analysis pipeline
181
    run_pipeline(obs, enumbins=10, nxpix=40, nypix=40, binsz=0.1)
182

    
183
    # Return
184
    return
185

    
186

    
187
# ======================== #
188
# Main routine entry point #
189
# ======================== #
190
if __name__ == '__main__':
191

    
192
    # Run stacked in-memory pipeline
193
    pipeline_stacked_mem()