This repository was archived by the owner on May 27, 2022. It is now read-only.
forked from cta-rta/ctoolsint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGammaPipe.py
More file actions
222 lines (179 loc) · 6.2 KB
/
GammaPipe.py
File metadata and controls
222 lines (179 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import os
import gammalib
import ctools
import obsutils
from Configuration import ObservationConfiguration
from Configuration import RunConfiguration
import wrapper
import cv2
class GammaPipe:
def __init__(self):
return
def init(self, obsfilename, simfilename, analysisfilename, runconffilename, eventfilename):
self.obsfilename = obsfilename
self.simfilename = simfilename
self.analysisfilename = analysisfilename
self.runconffilename = runconffilename
self.eventfilename = eventfilename
# Setup observations
if self.obsfilename:
self.obs = self.open_observation(self.obsfilename)
# Setup simulation model
if self.simfilename:
self.obs.models(gammalib.GModels(self.simfilename))
# Setup run
if self.runconffilename:
self.runconf = RunConfiguration(self.runconffilename)
return
def open_observation(self, obsfilename):
print('Open observation')
obs = gammalib.GObservations()
self.obsconf = ObservationConfiguration(obsfilename)
print(self.obsconf.obs_caldb)
pntdir = gammalib.GSkyDir()
# in_pnttype = info_dict['in_pnttype']
# if in_pnttype == 'celestial' :
# pntdir.radec_deg(self.obs_ra, self.obs_dec)
# if in_pnttype == 'equatorial' :
# pntdir.radec_deg(self.obs_ra, self.obs_dec)
# if in_pnttype == 'galactic' :
# pntdir.radec_deg(self.in_l, self.in_b)
pntdir.radec_deg(self.obsconf.obs_ra, self.obsconf.obs_dec)
obs1 = obsutils.set_obs(pntdir, self.obsconf.obs_tstart, self.obsconf.obs_duration, 1.0, \
self.obsconf.obs_emin, self.obsconf.obs_emax, self.obsconf.obs_fov, \
self.obsconf.obs_irf, self.obsconf.obs_caldb, self.obsconf.in_obsid)
obs.append(obs1)
# print(obs1)
return obs
# rules
# 1) if simfilename perform simulation based on observation configuration
# 2) if eventfilename read the file. Select events based on run configuration
# 3) if simfilename and eventfilename are not present, query the DB based on run and observation configuration
# 4) now that you have the event list in memory
# 4.A) make cts map
# 4.B) hypothesis generation (with spotfinders OR with the analysisfilename)
# 4.C) if you have an hypothesis, perform MLE
def run_pipeline(self, debug=False, seed=0):
"""
Test unbinned pipeline with FITS file saving
"""
# Set script parameters
events_name = 'events.fits'
cubefile_name = 'cube.fits'
selected_events_name = 'selected_events.fits'
result_name = 'results.xml'
if self.simfilename and self.eventfilename:
print('error')
exit()
if 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()
if self.runconf.WorkInMemory == 1:
print('Generate simulated event list on memory')
# Simulate events on memory
sim = ctools.ctobssim(self.obs)
sim['debug'] = debug
sim['seed'] = seed
sim.run()
if self.eventfilename:
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)
# Select events
# TODO: non seleziona gli eventi (provare prima su file, poi in memory)
# e' probabile che i tempi non siano corretti
print('select event list')
select = ctools.ctselect(sim.obs())
# using file
# 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.obsconf.obs_fov
select['tmin'] = self.runconf.tmin
# select['tmin'] = 55197.0007660185
select['tmax'] = self.runconf.tmax
# select['tmax'] = 55197.0007660185 + self.runconf.tmax / 86400.
select['emin'] = self.runconf.emin
select['emax'] = self.runconf.emax
select.run()
# select.execute()
# 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])
### Alla fine bisogna passare la lista selezionata a sim.obs()
if not self.simfilename and not self.eventfilename:
# load from DB
print('load event list from DB')
print('event list generated ----------------------')
# print(sim.obs())
# print(sim.obs()[0])
for run in sim.obs():
print('run ---')
# Create container with a single observation
container = gammalib.GObservations()
container.append(run)
# event file in memory or read from fits file on memory
bin = ctools.ctbin(container)
# event file on disk
# bin = ctools.ctbin()
# bin['inobs'] = 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'] = self.runconf.cts_nxpix
bin['nypix'] = self.runconf.cts_nypix
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
# make binned map on disk
bin.execute()
# make binned map on memory
# bin.run()
# Set observation ID if make binned map on disk
bin.obs()[0].id(cubefile_name)
bin.obs()[0].eventfile(cubefile_name)
# Append result to observations
self.obs.extend(bin.obs())
# print(obs)
# print(obs[0])
print(str(self.obsconf.obs_caldb))
# 3GH Extractor code
self.analysisfilename = wrapper.extract_source(cubefile_name)
cv2.waitKey(0)
# TODO: eseguire MLE
if self.analysisfilename:
print('MLE')
# Perform maximum likelihood fitting
like = ctools.ctlike()
like['inobs'] = events_name
like['inmodel'] = self.analysisfilename
like['outmodel'] = result_name
like['caldb'] = str(self.obsconf.obs_caldb)
like['irf'] = self.obsconf.obs_irf
like.execute()
wrapper.print_graphs(self.simfilename, result_name, self.analysisfilename)
cv2.destroyAllWindows()
# Return
return