Commit 0e97a041 authored by Moritz Huetten's avatar Moritz Huetten
Browse files

tests again pass

parent 71a37510
Pipeline #65683 canceled with stage
# Event source for MAGIC calibrated data files. # Event source for MAGIC calibrated data files.
# Requires uproot package (https://github.com/scikit-hep/uproot). # Requires uproot package (https://github.com/scikit-hep/uproot).
import logging
import glob import glob
import re import re
...@@ -16,6 +17,7 @@ from ctapipe.instrument import TelescopeDescription, SubarrayDescription, Optics ...@@ -16,6 +17,7 @@ from ctapipe.instrument import TelescopeDescription, SubarrayDescription, Optics
__all__ = ['MAGICEventSource'] __all__ = ['MAGICEventSource']
logger = logging.getLogger(__name__)
class MAGICEventSource(EventSource): class MAGICEventSource(EventSource):
""" """
...@@ -162,7 +164,8 @@ class MAGICEventSource(EventSource): ...@@ -162,7 +164,8 @@ class MAGICEventSource(EventSource):
Returns Returns
------- -------
MarsDataRun:
The run to use
""" """
input_path = '/'.join(self.input_url.split('/')[:-1]) input_path = '/'.join(self.input_url.split('/')[:-1])
...@@ -218,7 +221,7 @@ class MAGICEventSource(EventSource): ...@@ -218,7 +221,7 @@ class MAGICEventSource(EventSource):
if 'data' in self.current_run: if 'data' in self.current_run:
del self.current_run['data'] del self.current_run['data']
# Setting the new active run # Setting the new active run (class MarsDataRun object)
self.current_run = self._set_active_run(run_number) self.current_run = self._set_active_run(run_number)
# Loop over the events # Loop over the events
...@@ -1016,39 +1019,44 @@ class MarsDataRun: ...@@ -1016,39 +1019,44 @@ class MarsDataRun:
stereo_event_number = events[b'MRawEvtHeader.fStereoEvtNumber'] stereo_event_number = events[b'MRawEvtHeader.fStereoEvtNumber']
# Reading meta information: # Reading meta information:
meta_info = input_file['RunHeaders'].arrays(metainfo_array_list) try:
meta_info = input_file['RunHeaders'].arrays(metainfo_array_list)
mars_meta['origin'] = "MAGIC"
mars_meta['input_url'] = file_name mars_meta['origin'] = "MAGIC"
mars_meta['input_url'] = file_name
mars_meta['number'] = int(meta_info[b'MRawRunHeader.fRunNumber'][0])
#mars_meta['number_subrun'] = int(meta_info[b'MRawRunHeader.fSubRunIndex'][0]) mars_meta['number'] = int(meta_info[b'MRawRunHeader.fRunNumber'][0])
mars_meta['source_ra'] = meta_info[b'MRawRunHeader.fSourceRA'][0] / seconds_per_hour * degrees_per_hour * u.deg #mars_meta['number_subrun'] = int(meta_info[b'MRawRunHeader.fSubRunIndex'][0])
mars_meta['source_dec'] = meta_info[b'MRawRunHeader.fSourceDEC'][0] / seconds_per_hour * u.deg mars_meta['source_ra'] = meta_info[b'MRawRunHeader.fSourceRA'][0] / seconds_per_hour * degrees_per_hour * u.deg
mars_meta['source_dec'] = meta_info[b'MRawRunHeader.fSourceDEC'][0] / seconds_per_hour * u.deg
is_simulation = int(meta_info[b'MRawRunHeader.fRunType'][0])
if is_simulation == 0: is_simulation = int(meta_info[b'MRawRunHeader.fRunType'][0])
is_simulation = False if is_simulation == 0:
elif is_simulation == 256: is_simulation = False
is_simulation = True elif is_simulation == 256:
else: is_simulation = True
msg = "Run type (Data or MC) of MAGIC data file not recognised." else:
self.log.error(msg) msg = "Run type (Data or MC) of MAGIC data file not recognised."
raise self.log.error(msg)
mars_meta['is_simulation'] = is_simulation raise
mars_meta['is_simulation'] = is_simulation
# Reading the info only contained in real data
if is_simulation == False: # Reading the info only contained in real data
badpixelinfo = input_file['RunHeaders']['MBadPixelsCam.fArray.fInfo'].array(uproot.asjagged(uproot.asdtype(np.int32))).flatten().reshape((4, 1183), order='F') if is_simulation == False:
# now we have 3 axes: badpixelinfo = input_file['RunHeaders']['MBadPixelsCam.fArray.fInfo'].array(uproot.asjagged(uproot.asdtype(np.int32))).flatten().reshape((4, 1183), order='F')
# 1st axis: Unsuitable pixels # now we have 3 axes:
# 2nd axis: Uncalibrated pixels (says why pixel is unsuitable) # 1st axis: Unsuitable pixels
# 3rd axis: Bad hardware pixels (says why pixel is unsuitable) # 2nd axis: Uncalibrated pixels (says why pixel is unsuitable)
# Each axis cointains a 32bit integer encoding more information about the specific problem, see MARS software, MBADPixelsPix.h # 3rd axis: Bad hardware pixels (says why pixel is unsuitable)
# Here, we however discard this additional information and only grep the "unsuitable" axis. # Each axis cointains a 32bit integer encoding more information about the specific problem, see MARS software, MBADPixelsPix.h
badpixelinfo = badpixelinfo[1].astype(bool) # Here, we however discard this additional information and only grep the "unsuitable" axis.
else: badpixelinfo = badpixelinfo[1].astype(bool)
else:
badpixelinfo = np.zeros(1183)
except KeyError:
logger.warning("RunHeaders tree not present in file. Cannot read meta information and assume it is a real data run.")
badpixelinfo = np.zeros(1183) badpixelinfo = np.zeros(1183)
is_simulation = False
# Computing the event arrival time # Computing the event arrival time
mjd = events[b'MTime.fMjd'] mjd = events[b'MTime.fMjd']
......
...@@ -75,8 +75,8 @@ def test_geom(): ...@@ -75,8 +75,8 @@ def test_geom():
with MAGICEventSource(input_url=dataset) as source: with MAGICEventSource(input_url=dataset) as source:
event = next(source._generator()) event = next(source._generator())
assert event.inst.subarray.tels[1].camera.pix_x.size == 1039 assert source.subarray.tels[1].camera.pix_x.size == 1039
assert event.inst.subarray.tels[2].camera.pix_x.size == 1039 assert source.subarray.tels[2].camera.pix_x.size == 1039
def test_eventseeker(): def test_eventseeker():
...@@ -87,8 +87,20 @@ def test_eventseeker(): ...@@ -87,8 +87,20 @@ def test_eventseeker():
seeker = EventSeeker(source) seeker = EventSeeker(source)
event = seeker[0] event = seeker[0]
assert event.count == 0 assert event.count == 0
assert event.dl0.event_id == 29795.0 assert event.dl0.event_id == 29795
event = seeker[2] event = seeker[2]
assert event.count == 2 assert event.count == 2
assert event.r1.event_id == 29798.0 assert event.r1.event_id == 29798
def test_eventcontent():
dataset = get_dataset_path("20131004_M1_05029747.003_Y_MagicCrab-W0.40+035.root")
dataset = dataset.replace('_M1_', '_M*_')
with MAGICEventSource(input_url=dataset) as source:
seeker = EventSeeker(source)
event = seeker[0]
assert event.dl1.tel[1].image[0] == -0.53125
assert event.dl1.tel[2].image[0] == 2.2265625
assert event.dl1.tel[1].pulse_time[0] == 49.125
assert event.dl1.tel[2].pulse_time[0] == 23.5625
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment