-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsimtelfile.py
498 lines (404 loc) · 16.3 KB
/
simtelfile.py
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
'''
Implementation of an EventIOFile that
loops through SimTel Array events.
'''
import re
from copy import copy
from collections import defaultdict
import warnings
import logging
from ..base import EventIOFile
from ..exceptions import check_type
from .. import iact
from ..histograms import Histograms
from .objects import (
ADCSamples,
ADCSums,
ArrayEvent,
CalibrationEvent,
CameraMonitoring,
CameraOrganization,
CameraSettings,
CameraSoftwareSettings,
DisabledPixels,
DriveSettings,
History,
HistoryMeta,
ImageParameters,
LaserCalibration,
MCEvent,
MCPhotoelectronSum,
MCRunHeader,
MCShower,
PixelList,
PixelSettings,
PixelTiming,
PixelTriggerTimes,
PixelMonitoring,
PointingCorrection,
RunHeader,
TelescopeEvent,
TelescopeEventHeader,
TrackingPosition,
TriggerInformation,
CalibrationPhotoelectrons,
)
telescope_descriptions_types = (
CameraSettings,
CameraOrganization,
PixelSettings,
DisabledPixels,
CameraSoftwareSettings,
DriveSettings,
PointingCorrection,
)
class UnknownObjectWarning(UserWarning):
pass
log = logging.getLogger(__name__)
camel_re1 = re.compile('(.)([A-Z][a-z]+)')
camel_re2 = re.compile('([a-z0-9])([A-Z])')
def camel_to_snake(name):
s1 = camel_re1.sub(r'\1_\2', name)
return camel_re2.sub(r'\1_\2', s1).lower()
class NoTrackingPositions(Exception):
pass
class SimTelFile(EventIOFile):
def __init__(self, path, allowed_telescopes=None, skip_calibration=False, zcat=True):
super().__init__(path, zcat=zcat)
self.path = path
self.allowed_telescopes = None
if allowed_telescopes:
self.allowed_telescopes = set(allowed_telescopes)
self.histograms = None
self.history = []
self.mc_run_headers = []
self.corsika_inputcards = []
self.atmospheric_profiles = []
self.header = None
self.n_telescopes = None
self.telescope_meta = {}
self.global_meta = {}
self.telescope_descriptions = defaultdict(dict)
self.pixel_monitorings = defaultdict(dict)
self.camera_monitorings = defaultdict(dict)
self.laser_calibrations = defaultdict(dict)
self.current_mc_shower = None
self.current_mc_shower_id = None
self.current_mc_event = None
self.current_mc_event_id = None
self.current_telescope_data_event_id = None
self.current_photoelectron_sum = None
self.current_photoelectron_sum_event_id = None
self.current_photoelectrons = {}
self.current_photons = {}
self.current_emitter = {}
self.current_array_event = None
self.current_calibration_event = None
self.current_calibration_event_id = None
self.current_calibration_pe = {}
self.skip_calibration = skip_calibration
# read the header:
# assumption: the header is done when
# any of the objects in check is not None anymore
# and we found the telescope_descriptions of all telescopes
check = []
found_all_telescopes = False
while not (any(o for o in check) and found_all_telescopes):
self.next_low_level()
check = [
self.current_mc_shower,
self.current_array_event,
self.current_calibration_event,
self.laser_calibrations,
self.camera_monitorings,
]
# check if we found all the descriptions of all telescopes
if self.n_telescopes is not None:
found = sum(
len(t) == len(telescope_descriptions_types)
for t in self.telescope_descriptions.values()
)
found_all_telescopes = found == self.n_telescopes
def __iter__(self):
return self.iter_array_events()
def next_low_level(self):
o = next(self)
# order of if statements is roughly sorted
# by the number of occurences in a simtel file
# this should minimize the number of if statements evaluated
if isinstance(o, MCEvent):
self.current_mc_event = o.parse()
self.current_mc_event_id = o.header.id
elif isinstance(o, MCShower):
self.current_mc_shower = o.parse()
self.current_mc_shower_id = o.header.id
elif isinstance(o, ArrayEvent):
self.current_array_event = parse_array_event(
o,
self.allowed_telescopes
)
elif isinstance(o, iact.TelescopeData):
event_id, photons, emitter, photoelectrons = parse_telescope_data(o)
self.current_telescope_data_event_id = event_id
self.current_photons = photons
self.current_emitter = emitter
self.current_photoelectrons = photoelectrons
elif isinstance(o, MCPhotoelectronSum):
self.current_photoelectron_sum_event_id = o.header.id
self.current_photoelectron_sum = o.parse()
elif isinstance(o, CameraMonitoring):
self.camera_monitorings[o.telescope_id].update(o.parse())
elif isinstance(o, LaserCalibration):
self.laser_calibrations[o.telescope_id].update(o.parse())
elif isinstance(o, PixelMonitoring):
self.pixel_monitorings[o.telescope_id].update(o.parse())
elif isinstance(o, telescope_descriptions_types):
key = camel_to_snake(o.__class__.__name__)
self.telescope_descriptions[o.telescope_id][key] = o.parse()
elif isinstance(o, RunHeader):
self.header = o.parse()
self.n_telescopes = self.header['n_telescopes']
elif isinstance(o, MCRunHeader):
self.mc_run_headers.append(o.parse())
elif isinstance(o, iact.InputCard):
self.corsika_inputcards.append(o.parse())
elif isinstance(o, CalibrationEvent):
if not self.skip_calibration:
array_event = next(o)
self.current_calibration_event = parse_array_event(
array_event,
self.allowed_telescopes,
)
# assign negative event_ids to calibration events to avoid
# duplicated event_ids
self.current_calibration_event_id = -array_event.header.id
self.current_calibration_event['calibration_type'] = o.type
elif isinstance(o, CalibrationPhotoelectrons):
telescope_data = next(o)
if not isinstance(telescope_data, iact.TelescopeData):
warnings.warn(
f"Unexpected sub-object: {telescope_data} in {o}, ignoring",
UnknownObjectWarning
)
return
self.current_calibration_pe = {}
for photoelectrons in telescope_data:
if not isinstance(photoelectrons, iact.PhotoElectrons):
warnings.warn(
f"Unexpected sub-object: {photoelectrons} in {telescope_data}, ignoring",
UnknownObjectWarning
)
tel_id = photoelectrons.telescope_id
self.current_calibration_pe[tel_id] = photoelectrons.parse()
elif isinstance(o, History):
for sub in o:
self.history.append(sub.parse())
elif isinstance(o, HistoryMeta):
if o.header.id == -1:
self.global_meta = o.parse()
else:
self.telescope_meta[o.header.id] = o.parse()
elif isinstance(o, Histograms):
self.histograms = o.parse()
elif isinstance(o, iact.AtmosphericProfile):
self.atmospheric_profiles.append(o.parse())
else:
warnings.warn(
'object type encountered, which is no handled'
' at the moment: {}'.format(o),
UnknownObjectWarning,
)
def iter_mc_events(self):
while True:
try:
next_event = self.try_build_mc_event()
except StopIteration:
break
if next_event is not None:
yield next_event
def try_build_mc_event(self):
if self.current_mc_event:
event_data = {
'event_id': self.current_mc_event_id,
'mc_shower': self.current_mc_shower,
'mc_event': self.current_mc_event,
}
# if next object is TelescopeData, it belongs to this event
if isinstance(self.peek(), iact.TelescopeData):
self.next_low_level()
event_data['photons'] = self.current_photons
event_data['emitter'] = self.current_emitter
event_data['photoelectrons'] = self.current_photoelectrons
self.current_mc_event = None
return event_data
self.next_low_level()
def iter_array_events(self):
while True:
next_event = self.try_build_event()
if next_event is not None:
yield next_event
try:
self.next_low_level()
except StopIteration:
break
def try_build_event(self):
'''check if all necessary info for an event was found,
then make an event and invalidate old data
'''
if self.current_array_event:
if (
self.allowed_telescopes
and not self.current_array_event['telescope_events']
):
self.current_array_event = None
return None
event_id = self.current_array_event['event_id']
event_data = {
'type': 'data',
'event_id': event_id,
'mc_shower': None,
'mc_event': None,
'telescope_events': self.current_array_event['telescope_events'],
'tracking_positions': self.current_array_event['tracking_positions'],
'trigger_information': self.current_array_event['trigger_information'],
'photons': {},
'emitter': {},
'photoelectrons': {},
'photoelectron_sums': None,
}
if self.current_mc_event_id == event_id:
event_data['mc_shower'] = self.current_mc_shower
event_data['mc_event'] = self.current_mc_event
if self.current_telescope_data_event_id == event_id:
event_data['photons'] = self.current_photons
event_data['emitter'] = self.current_emitter
event_data['photoelectrons'] = self.current_photoelectrons
if self.current_photoelectron_sum_event_id == event_id:
event_data['photoelectron_sums'] = self.current_photoelectron_sum
event_data['camera_monitorings'] = {
telescope_id: copy(self.camera_monitorings[telescope_id])
for telescope_id in self.current_array_event['telescope_events'].keys()
}
event_data['laser_calibrations'] = {
telescope_id: copy(self.laser_calibrations[telescope_id])
for telescope_id in self.current_array_event['telescope_events'].keys()
}
event_data['pixel_monitorings'] = {
telescope_id: copy(self.pixel_monitorings[telescope_id])
for telescope_id in self.current_array_event['telescope_events'].keys()
}
self.current_array_event = None
return event_data
elif self.current_calibration_event:
event = self.current_calibration_event
if (
self.allowed_telescopes
and not self.current_array_event['telescope_events']
):
self.current_calibration_event = None
return None
event_data = {
'type': 'calibration',
'event_id': self.current_calibration_event_id,
'telescope_events': event['telescope_events'],
'tracking_positions': event['tracking_positions'],
'trigger_information': event['trigger_information'],
'calibration_type': event['calibration_type'],
'photoelectrons': self.current_calibration_pe,
}
event_data['camera_monitorings'] = {
telescope_id: copy(self.camera_monitorings[telescope_id])
for telescope_id in event['telescope_events'].keys()
}
event_data['laser_calibrations'] = {
telescope_id: copy(self.laser_calibrations[telescope_id])
for telescope_id in event['telescope_events'].keys()
}
self.current_calibration_event = None
return event_data
def parse_array_event(array_event, allowed_telescopes=None):
'''structure of event:
TriggerInformation[2009] <-- this knows how many TelescopeEvents
TelescopeEvent[2202]
...
TelescopeEvent[2208]
TrackingPosition[2101]
...
TrackingPosition[2164]
StereoReconstruction[2015]
In words:
1 cent event
n tel events
m track events (n does not need to be == m)
1 shower
'''
check_type(array_event, ArrayEvent)
telescope_events = {}
tracking_positions = {}
# for older files, the array_event.header.id does not match the mc event id
# so we overwrite it later with the event id in the trigger information
event_id = array_event.header.id
for i, o in enumerate(array_event):
# require first element to be a TriggerInformation
if i == 0:
check_type(o, TriggerInformation)
event_id = o.header.id
trigger_information = o.parse()
telescopes = set(trigger_information['telescopes_with_data'])
if allowed_telescopes and len(telescopes & allowed_telescopes) == 0:
break
elif isinstance(o, TelescopeEvent):
if allowed_telescopes is None or o.telescope_id in allowed_telescopes:
telescope_events[o.telescope_id] = parse_telescope_event(o)
elif isinstance(o, TrackingPosition):
if allowed_telescopes is None or o.telescope_id in allowed_telescopes:
tracking_positions[o.telescope_id] = o.parse()
missing_tracking = set(telescope_events.keys()) - set(tracking_positions.keys())
if missing_tracking:
raise NoTrackingPositions(
'Missing tracking positions for telescopes {}'.format(
missing_tracking
)
)
return {
'event_id': event_id,
'trigger_information': trigger_information,
'telescope_events': telescope_events,
'tracking_positions': tracking_positions,
}
def parse_telescope_data(telescope_data):
''' Parse the TelescopeData block with Cherenkov Photon information'''
check_type(telescope_data, iact.TelescopeData)
photons = {}
emitter = {}
photo_electrons = {}
for o in telescope_data:
if isinstance(o, iact.PhotoElectrons):
photo_electrons[o.telescope_id] = o.parse()
elif isinstance(o, iact.Photons):
p, e = o.parse()
photons[o.telescope_id] = p
if e is not None:
emitter[o.telescope_id] = e
return telescope_data.header.id, photons, emitter, photo_electrons
def parse_telescope_event(telescope_event):
'''Parse a telescope event'''
check_type(telescope_event, TelescopeEvent)
event = {'pixel_lists': {}}
for i, o in enumerate(telescope_event):
if i == 0:
check_type(o, TelescopeEventHeader)
event['header'] = o.parse()
elif isinstance(o, ADCSamples):
event['adc_samples'] = o.parse()
elif isinstance(o, ADCSums):
event['adc_sums'] = o.parse()
elif isinstance(o, PixelTiming):
event['pixel_timing'] = o.parse()
elif isinstance(o, ImageParameters):
event['image_parameters'] = o.parse()
elif isinstance(o, PixelList):
event['pixel_lists'][o.code] = o.parse()
elif isinstance(o, PixelTriggerTimes):
event['pixel_trigger_times'] = o.parse()
return event