openmp.py
1 |
#! /usr/bin/env python
|
---|---|
2 |
# ==========================================================================
|
3 |
# This script simulates a number of CTA observations and fits a model to the
|
4 |
# simulated events. To make this script work, make sure that the $GAMMALIB
|
5 |
# environment variable points to the directory whene gammalib and ctools are
|
6 |
# installed. Here an example of how to configure gammalib and ctools:
|
7 |
#
|
8 |
# Suppose you have two directories, one holding a copy of gammalib and one
|
9 |
# holding a copy of ctools. Then install both components in a specific
|
10 |
# install directory by executing the following commands:
|
11 |
#
|
12 |
# cd gammalib
|
13 |
# ./configure --prefix=/home/knodlseder/testinstall
|
14 |
# make install
|
15 |
# cd ..
|
16 |
# cd ctools
|
17 |
# ./configure --prefix=/home/knodlseder/testinstall
|
18 |
# make install
|
19 |
#
|
20 |
# Now you need to configure gammalib and ctools:
|
21 |
#
|
22 |
# export GAMMALIB=/home/knodlseder/testinstall
|
23 |
# source $GAMMALIB/bin/gammalib-init.sh
|
24 |
#
|
25 |
# Copyright (C) 2012 Juergen Knoedlseder
|
26 |
#
|
27 |
# This program is free software: you can redistribute it and/or modify
|
28 |
# it under the terms of the GNU General Public License as published by
|
29 |
# the Free Software Foundation, either version 3 of the License, or
|
30 |
# (at your option) any later version.
|
31 |
#
|
32 |
# This program is distributed in the hope that it will be useful,
|
33 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
34 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
35 |
# GNU General Public License for more details.
|
36 |
#
|
37 |
# You should have received a copy of the GNU General Public License
|
38 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
39 |
#
|
40 |
# ==========================================================================
|
41 |
from ctools import * |
42 |
from gammalib import * |
43 |
import obsutils |
44 |
import sys |
45 |
import time |
46 |
|
47 |
|
48 |
# ========= #
|
49 |
# cta class #
|
50 |
# ========= #
|
51 |
class cta: |
52 |
"""
|
53 |
Implements methods for observation definition, simulations and model
|
54 |
fitting.
|
55 |
"""
|
56 |
def __init__(self, *argv): |
57 |
"""
|
58 |
Constructor.
|
59 |
"""
|
60 |
# Initialise members
|
61 |
self.m_obs = GObservations()
|
62 |
self.m_srcmdl = "${GAMMALIB}/share/models/crab.xml" |
63 |
self.m_caldb = "${GAMMALIB}/share/caldb/cta" |
64 |
self.m_irf = "kb_E_50h_v3" |
65 |
self.m_ra = 83.6331 |
66 |
self.m_dec = 22.0145 |
67 |
self.m_emin = 0.1 |
68 |
self.m_emax = 100.0 |
69 |
self.m_duration = 1800.0 |
70 |
self.m_deadc = 0.95 |
71 |
self.m_rad = 5.0 |
72 |
|
73 |
# Return
|
74 |
return
|
75 |
|
76 |
def __del__(self): |
77 |
"""
|
78 |
Destructor.
|
79 |
"""
|
80 |
# Return
|
81 |
return
|
82 |
|
83 |
def simulate(self, number): |
84 |
"""
|
85 |
Simulation CTA observations.
|
86 |
|
87 |
Parameters:
|
88 |
number - Number of observations in container.
|
89 |
"""
|
90 |
# Set seed value
|
91 |
seed = 0
|
92 |
|
93 |
# Clear observation container
|
94 |
self.m_obs.clear()
|
95 |
|
96 |
# Set pointing direction
|
97 |
pntdir = GSkyDir() |
98 |
pntdir.radec_deg(self.m_ra, self.m_dec) |
99 |
|
100 |
# Create CTA observation
|
101 |
run = obsutils.set(pntdir, caldb=self.m_caldb, irf=self.m_irf, \ |
102 |
duration=self.m_duration, deadc=self.m_deadc, \ |
103 |
emin=self.m_emin, emax=self.m_emax, rad=self.m_rad) |
104 |
|
105 |
# Append observation to container
|
106 |
for i in range(number): |
107 |
self.m_obs.append(run)
|
108 |
|
109 |
# Append source model
|
110 |
self.m_obs.models(GModels(self.m_srcmdl)) |
111 |
|
112 |
# Simulate observations
|
113 |
self.m_obs = obsutils.sim(self.m_obs, seed=seed, debug=False) |
114 |
|
115 |
# Return
|
116 |
return
|
117 |
|
118 |
def fit(self): |
119 |
"""
|
120 |
Fit model.
|
121 |
"""
|
122 |
# Save start time
|
123 |
start = time.clock() |
124 |
|
125 |
# Fit model
|
126 |
like = obsutils.fit(self.m_obs)
|
127 |
|
128 |
# Compute elapsed time
|
129 |
elapsed = (time.clock() - start) |
130 |
print "Elapsed time: ", elapsed |
131 |
|
132 |
# Return
|
133 |
return
|
134 |
|
135 |
|
136 |
# ======================== #
|
137 |
# Main routine entry point #
|
138 |
# ======================== #
|
139 |
if __name__ == '__main__': |
140 |
"""
|
141 |
This script simulates a number of CTA observations and fits a model
|
142 |
to the simulated events. The command line parameter specifies the
|
143 |
number of CTA observations that should be simulated. By default, 100
|
144 |
observations are simulated.
|
145 |
"""
|
146 |
# Get input arguments
|
147 |
usage = "openmp [number of observations]"
|
148 |
if len(sys.argv) < 1 or len(sys.argv) > 2: |
149 |
print usage
|
150 |
sys.exit() |
151 |
|
152 |
# Set number of observations
|
153 |
if len(sys.argv) > 1: |
154 |
number = int(sys.argv[1]) |
155 |
else:
|
156 |
number = 100
|
157 |
|
158 |
# Create instance of test class
|
159 |
obs = cta() |
160 |
|
161 |
# Simulate observations
|
162 |
obs.simulate(number) |
163 |
|
164 |
# Do model fitting
|
165 |
obs.fit() |
166 |
|