pipeline_unbinned_mem.py

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

Download (3.63 KB)

 
1
#! /usr/bin/env python
2
# ==========================================================================
3
# Perform unbinned in-memory analysis of simulated CTA data
4
#
5
# Copyright (C) 2015-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, rad=3.0,
30
                 emin=0.1, emax=100.0,
31
                 tmin=0.0, tmax=0.0,
32
                 debug=False):
33
    """
34
    Simulation and binned analysis pipeline
35

36
    Parameters
37
    ----------
38
    obs : `~gammalib.GObservations`
39
        Observation container
40
    ra : float, optional
41
        Right Ascension of Region of Interest centre (deg)
42
    dec : float, optional
43
        Declination of Region of Interest centre (deg)
44
    rad : float, optional
45
        Radius of Region of Interest (deg)
46
    emin : float, optional
47
        Minimum energy (TeV)
48
    emax : float, optional
49
        Maximum energy (TeV)
50
    tmin : float, optional
51
        Start time (s)
52
    tmax : float, optional
53
        Stop time (s)
54
    debug : bool, optional
55
        Debug function
56
    """
57
    # Simulate events
58
    sim = ctools.ctobssim(obs)
59
    sim['debug'] = debug
60
    sim.run()
61

    
62
    # Select events
63
    select = ctools.ctselect(sim.obs())
64
    select['ra']    = ra
65
    select['dec']   = dec
66
    select['rad']   = rad
67
    select['emin']  = emin
68
    select['emax']  = emax
69
    select['tmin']  = tmin
70
    select['tmax']  = tmax
71
    select['debug'] = debug
72
    select.run()
73

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

    
79
    # Return
80
    return
81

    
82

    
83
# =============================== #
84
# Run unbinned in-memory pipeline #
85
# =============================== #
86
def pipeline_unbinned_mem():
87
    """
88
    Run unbinned in-memory pipeline
89
    """
90
    # Set usage string
91
    usage = 'pipeline_unbinned_mem.py [-d datadir]'
92

    
93
    # Set default options
94
    options = [{'option': '-d', 'value': 'data'}]
95

    
96
    # Get arguments and options from command line arguments
97
    args, options = cscripts.ioutils.get_args_options(options, usage)
98

    
99
    # Extract script parameters from options
100
    datadir = options[0]['value']
101

    
102
    # Setup observations
103
    obs = cscripts.obsutils.set_observations(83.63, 22.01, 5.0, 0.0, 180.0,
104
                                             0.1, 100.0, 'South_0.5h', 'prod2',
105
                                             pattern='four', offset=1.5)
106

    
107
    # Setup model
108
    #obs.models(gammalib.GModels(datadir+'/crab.xml'))
109

    
110
    # Setup with composite model
111
    obs.models(gammalib.GModels(datadir+'/model_spatial_composite.xml'))
112

    
113
    # Run analysis pipeline
114
    run_pipeline(obs)
115

    
116
    # Return
117
    return
118

    
119

    
120
# ======================== #
121
# Main routine entry point #
122
# ======================== #
123
if __name__ == '__main__':
124

    
125
    # Run unbinned in-memory pipeline
126
    pipeline_unbinned_mem()