__init__.py 53.8 KB
Newer Older
1
2
# Event source for MAGIC calibrated data files.
# Requires uproot package (https://github.com/scikit-hep/uproot).
Moritz Huetten's avatar
Moritz Huetten committed
3
import logging
4
5
6
7

import glob
import re

8
import scipy
9
10
11
12
13
14
import numpy as np
import scipy.interpolate

from astropy import units as u
from astropy.time import Time
from ctapipe.io.eventsource import EventSource
Moritz Huetten's avatar
Moritz Huetten committed
15
from ctapipe.io.containers import DataContainer, TelescopePointingContainer
16
17
18
19
from ctapipe.instrument import TelescopeDescription, SubarrayDescription, OpticsDescription, CameraGeometry

__all__ = ['MAGICEventSource']

Moritz Huetten's avatar
Moritz Huetten committed
20
logger = logging.getLogger(__name__)
21

Ievgen Vovk's avatar
Ievgen Vovk committed
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
class MAGICEventSource(EventSource):
    """
    EventSource for MAGIC calibrated data.

    This class operates with the MAGIC data run-wise. This means that the files
    corresponding to the same data run are loaded and processed together.
    """
    _count = 0

    def __init__(self, config=None, tool=None, **kwargs):
        """
        Constructor

        Parameters
        ----------
        config: traitlets.loader.Config
            Configuration specified by config file or cmdline arguments.
            Used to set traitlet values.
            Set to None if no configuration to pass.
        tool: ctapipe.core.Tool
            Tool executable that is calling this component.
            Passes the correct logger to the component.
            Set to None if no Tool to pass.
        kwargs: dict
            Additional parameters to be passed.
            NOTE: The file mask of the data to read can be passed with
            the 'input_url' parameter.
        """

        try:
            import uproot
        except ImportError:
            msg = "The `uproot` python module is required to access the MAGIC data"
            self.log.error(msg)
            raise

59
        self.file_list = glob.glob(kwargs['input_url'])
60
61
        if len(self.file_list) == 0:
            raise ValueError("Unreadable or wrong wildcard file path given.")
62
        self.file_list.sort()
63
64
65
66
67

        # EventSource can not handle file wild cards as input_url
        # To overcome this we substitute the input_url with first file matching
        # the specified file mask.
        del kwargs['input_url']
68
        super().__init__(input_url=self.file_list[0], **kwargs)
69
70

        # Retrieving the list of run numbers corresponding to the data files
Moritz Huetten's avatar
Moritz Huetten committed
71
72
        run_info = list(map(self._get_run_info_from_name, self.file_list))
        run_numbers = [i[0] for i in run_info]
73
        is_mc_runs = [i[1] for i in run_info]
Moritz Huetten's avatar
Moritz Huetten committed
74
75

        self.run_numbers, indices = np.unique(run_numbers, return_index=True)
76
77
78
79
80
81
        is_mc_runs = [is_mc_runs[i] for i in indices]
        is_mc_runs = np.unique(is_mc_runs)
        # Checking if runt type (data/MC) is consistent:
        if len(is_mc_runs) > 1:
            raise ValueError("Loaded files contain data and MC runs. Please load only data OR Monte Carlos.")
        self.is_mc = is_mc_runs[0]
82
83
84
85
86
87
88
89
90
91
92
93
94

        # # Setting up the current run with the first run present in the data
        # self.current_run = self._set_active_run(run_number=0)
        self.current_run = None

        # MAGIC telescope positions in m wrt. to the center of CTA simulations
        self.magic_tel_positions = {
            1: [-27.24, -146.66, 50.00] * u.m,
            2: [-96.44, -96.77, 51.00] * u.m
        }
        # MAGIC telescope description
        optics = OpticsDescription.from_name('MAGIC')
        geom = CameraGeometry.from_name('MAGICCam')
95
        self.magic_tel_description = TelescopeDescription(name='MAGIC', tel_type='MAGIC', optics=optics, camera=geom)
96
        self.magic_tel_descriptions = {1: self.magic_tel_description, 2: self.magic_tel_description}
97
        self._subarray_info = SubarrayDescription('MAGIC', self.magic_tel_positions, self.magic_tel_descriptions)
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

    @staticmethod
    def is_compatible(file_mask):
        """
        This method checks if the specified file mask corresponds
        to MAGIC data files. The result will be True only if all
        the files are of ROOT format and contain an 'Events' tree.

        Parameters
        ----------
        file_mask: str
            A file mask to check

        Returns
        -------
        bool:
            True if the masked files are MAGIC data runs, False otherwise.

        """

        is_magic_root_file = True

        file_list = glob.glob(file_mask)

        for file_path in file_list:
            try:
                import uproot

                try:
                    with uproot.open(file_path) as input_data:
                        if 'Events' not in input_data:
                            is_magic_root_file = False
                except ValueError:
                    # uproot raises ValueError if the file is not a ROOT file
                    is_magic_root_file = False
                    pass

            except ImportError:
Ievgen Vovk's avatar
Ievgen Vovk committed
136
                if re.match(r'.+_m\d_.+root', file_path.lower()) is None:
137
138
139
140
141
                    is_magic_root_file = False

        return is_magic_root_file

    @staticmethod
Moritz Huetten's avatar
Moritz Huetten committed
142
    def _get_run_info_from_name(file_name):
143
        """
Moritz Huetten's avatar
Moritz Huetten committed
144
145
        This internal method extracts the run number and 
        type (data/MC) from the specified file name.
146
147
148
149
150
151
152
153
154
155
156
157

        Parameters
        ----------
        file_name: str
            A file name to process.

        Returns
        -------
        int:
            A run number of the file.
        """

Moritz Huetten's avatar
Moritz Huetten committed
158
        mask_data = r".*\d+_M\d+_(\d+)\.\d+_Y_.*"
Moritz Huetten's avatar
Moritz Huetten committed
159
160
161
162
163
164
165
166
167
168
169
        mask_mc = r".*_M\d_za\d+to\d+_\d_(\d+)_Y_.*"
        mask_mc_alt = r".*_M\d_\d_(\d+)_.*"
        if len(re.findall(mask_data, file_name)) > 0:
            parsed_info = re.findall(mask_data, file_name)
            is_mc = False
        elif len(re.findall(mask_mc, file_name)) > 0:
            parsed_info = re.findall(mask_mc, file_name)
            is_mc = True
        else:
            parsed_info = re.findall(mask_mc_alt, file_name)
            is_mc = True
170
171
172
173

        try:
            run_number = int(parsed_info[0])
        except IndexError:
Moritz Huetten's avatar
Moritz Huetten committed
174
            raise IndexError('Can not identify the run number and type (data/MC) of the file {:s}'.format(file_name))
175

Moritz Huetten's avatar
Moritz Huetten committed
176
        return run_number, is_mc
177

Moritz Huetten's avatar
Moritz Huetten committed
178
    def _set_active_run(self, run_number):
179
180
181
182
183
184
185
186
187
188
        """
        This internal method sets the run that will be used for data loading.

        Parameters
        ----------
        run_number: int
            The run number to use.

        Returns
        -------
Moritz Huetten's avatar
Moritz Huetten committed
189
        MarsRun:
Moritz Huetten's avatar
Moritz Huetten committed
190
            The run to use
191
192
193
194
195
196
197
198
        """

        input_path = '/'.join(self.input_url.split('/')[:-1])
        this_run_mask = input_path + '/*{:d}*root'.format(run_number)

        run = dict()
        run['number'] = run_number
        run['read_events'] = 0
Moritz Huetten's avatar
Moritz Huetten committed
199
        run['data'] = MarsRun(run_file_mask=this_run_mask, filter_list=self.file_list)
200
201
202

        return run

203
204
205
206
    @property
    def subarray(self):
        return self._subarray_info

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
    def _generator(self):
        """
        The default event generator. Return the stereo event
        generator instance.

        Returns
        -------

        """

        return self._stereo_event_generator()

    def _stereo_event_generator(self):
        """
        Stereo event generator. Yields DataContainer instances, filled
        with the read event data.

        Returns
        -------

        """

        counter = 0

        # Data container - is initialized once, and data is replaced within it after each yield
        data = DataContainer()

        # Telescopes with data:
        tels_in_file = ["m1", "m2"]
        tels_with_data = {1, 2}

        # Loop over the available data runs
239
        for run_number in self.run_numbers:
240
241
242
243
244
245

            # Removing the previously read data run from memory
            if self.current_run is not None:
                if 'data' in self.current_run:
                    del self.current_run['data']

Moritz Huetten's avatar
Moritz Huetten committed
246
            # Setting the new active run (class MarsRun object)
Moritz Huetten's avatar
Moritz Huetten committed
247
            self.current_run = self._set_active_run(run_number)
248
249
250
251
252
253
254
255
256
257

            # Loop over the events
            for event_i in range(self.current_run['data'].n_stereo_events):
                # Event and run ids
                event_order_number = self.current_run['data'].stereo_ids[event_i][0]
                event_id = self.current_run['data'].event_data['M1']['stereo_event_number'][event_order_number]
                obs_id = self.current_run['number']

                # Reading event data
                event_data = self.current_run['data'].get_stereo_event_data(event_i)
Moritz Huetten's avatar
Moritz Huetten committed
258
                
259
                data.meta = event_data['mars_meta']
260
261
262
263
264
265
266
267

                # Event counter
                data.count = counter

                # Setting up the R0 container
                data.r0.obs_id = obs_id
                data.r0.event_id = event_id
                data.r0.tel.clear()
268
                data.r0.tel[tel_i + 1].trigger_type = self.current_run['data'].event_data['M1']['trigger_pattern'][event_order_number]
269
270
271
272
273

                # Setting up the R1 container
                data.r1.obs_id = obs_id
                data.r1.event_id = event_id
                data.r1.tel.clear()
274
                data.r1.tel[tel_i + 1].trigger_type = self.current_run['data'].event_data['M1']['trigger_pattern'][event_order_number]
275
276
277
278
279

                # Setting up the DL0 container
                data.dl0.obs_id = obs_id
                data.dl0.event_id = event_id
                data.dl0.tel.clear()
280
                data.dl0.tel[tel_i + 1].trigger_type = self.current_run['data'].event_data['M1']['trigger_pattern'][event_order_number]
281
282
283
284
285

                # Filling the DL1 container with the event data
                for tel_i, tel_id in enumerate(tels_in_file):
                    # Creating the telescope pointing container
                    pointing = TelescopePointingContainer()
286
287
288
289
                    pointing.azimuth = np.deg2rad(event_data['{:s}_pointing_az'.format(tel_id)]) * u.rad
                    pointing.altitude = np.deg2rad(90 - event_data['{:s}_pointing_zd'.format(tel_id)]) * u.rad
                    pointing.ra = np.deg2rad(event_data['{:s}_pointing_ra'.format(tel_id)]) * u.rad
                    pointing.dec = np.deg2rad(event_data['{:s}_pointing_dec'.format(tel_id)]) * u.rad
290
291
292
293
294
295

                    # Adding the pointing container to the event data
                    data.pointing[tel_i + 1] = pointing

                    # Adding event charge and peak positions per pixel
                    data.dl1.tel[tel_i + 1].image = event_data['{:s}_image'.format(tel_id)]
Ievgen Vovk's avatar
Ievgen Vovk committed
296
                    data.dl1.tel[tel_i + 1].pulse_time = event_data['{:s}_pulse_time'.format(tel_id)]
297
                    data.dl1.tel[tel_i + 1].badpixels = event_data['{:s}_bad_pixels'.format(tel_id)]
298
299
300
                    # data.dl1.tel[i_tel + 1].badpixels = np.array(
                    #     file['dl1/tel' + str(i_tel + 1) + '/badpixels'], dtype=np.bool)

Moritz Huetten's avatar
Moritz Huetten committed
301

302
                if self.is_mc == False:
Moritz Huetten's avatar
Moritz Huetten committed
303
304
305
306
307
                    # Adding the event arrival time
                    time_tmp = Time(event_data['mjd'], scale='utc', format='mjd')
                    data.trig.gps_time = Time(time_tmp, format='unix', scale='utc', precision=9)
                else:
                    data.mc.energy = event_data['true_energy'] * u.GeV
308
                    data.mc.alt = (np.pi/2 - event_data['true_zd']) * u.rad
309
                    data.mc.az = -1 * (event_data['true_az'] - np.deg2rad(180 - 7)) * u.rad # check meaning of 7deg transformation (I.Vovk)
Moritz Huetten's avatar
Moritz Huetten committed
310
311
312
313
                    data.mc.shower_primary_id = 1 - event_data['true_shower_primary_id']
                    data.mc.h_first_int = event_data['true_h_first_int'] * u.cm
                    data.mc.core_x = event_data['true_core_x'] * u.cm
                    data.mc.core_y = event_data['true_core_y'] * u.cm
314
315
316
317
318
319

                # Setting the telescopes with data
                data.r0.tels_with_data = tels_with_data
                data.r1.tels_with_data = tels_with_data
                data.dl0.tels_with_data = tels_with_data
                data.trig.tels_with_trigger = tels_with_data
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

                yield data
                counter += 1

        return

    def _mono_event_generator(self, telescope):
        """
        Mono event generator. Yields DataContainer instances, filled
        with the read event data.

        Parameters
        ----------
        telescope: str
            The telescope for which to return events. Can be either "M1" or "M2".

        Returns
        -------

        """

        counter = 0
        telescope = telescope.upper()

        # Data container - is initialized once, and data is replaced within it after each yield
        data = DataContainer()

        # Telescopes with data:
        tels_in_file = ["M1", "M2"]

        if telescope not in tels_in_file:
            raise ValueError("Specified telescope {:s} is not in the allowed list {}".format(telescope, tels_in_file))

        tel_i = tels_in_file.index(telescope)
        tels_with_data = {tel_i + 1, }

        # Loop over the available data runs
358
        for run_number in self.run_numbers:
359
360
361
362
363
364
365

            # Removing the previously read data run from memory
            if self.current_run is not None:
                if 'data' in self.current_run:
                    del self.current_run['data']

            # Setting the new active run
Moritz Huetten's avatar
Moritz Huetten committed
366
            self.current_run = self._set_active_run(run_number)
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381

            if telescope == 'M1':
                n_events = self.current_run['data'].n_mono_events_m1
            else:
                n_events = self.current_run['data'].n_mono_events_m2

            # Loop over the events
            for event_i in range(n_events):
                # Event and run ids
                event_order_number = self.current_run['data'].mono_ids[telescope][event_i]
                event_id = self.current_run['data'].event_data[telescope]['stereo_event_number'][event_order_number]
                obs_id = self.current_run['number']

                # Reading event data
                event_data = self.current_run['data'].get_mono_event_data(event_i, telescope=telescope)
Moritz Huetten's avatar
Moritz Huetten committed
382
                
383
                data.meta = event_data['mars_meta']
384
385
386
387
388
389
390
391

                # Event counter
                data.count = counter

                # Setting up the R0 container
                data.r0.obs_id = obs_id
                data.r0.event_id = event_id
                data.r0.tel.clear()
392
                data.r0.tel[tel_i + 1].trigger_type = self.current_run['data'].event_data[telescope]['trigger_pattern'][event_order_number]
393
394
395
396
397

                # Setting up the R1 container
                data.r1.obs_id = obs_id
                data.r1.event_id = event_id
                data.r1.tel.clear()
398
                data.r1.tel[tel_i + 1].trigger_type = self.current_run['data'].event_data[telescope]['trigger_pattern'][event_order_number]
399
400
401
402
403

                # Setting up the DL0 container
                data.dl0.obs_id = obs_id
                data.dl0.event_id = event_id
                data.dl0.tel.clear()
404
                data.dl0.tel[tel_i + 1].trigger_type = self.current_run['data'].event_data[telescope]['trigger_pattern'][event_order_number]
405
406
407
408
409
410
411
412
413
414
415
416
417

                # Creating the telescope pointing container
                pointing = TelescopePointingContainer()
                pointing.azimuth = np.deg2rad(event_data['pointing_az']) * u.rad
                pointing.altitude = np.deg2rad(90 - event_data['pointing_zd']) * u.rad
                pointing.ra = np.deg2rad(event_data['pointing_ra']) * u.rad
                pointing.dec = np.deg2rad(event_data['pointing_dec']) * u.rad

                # Adding the pointing container to the event data
                data.pointing[tel_i + 1] = pointing

                # Adding event charge and peak positions per pixel
                data.dl1.tel[tel_i + 1].image = event_data['image']
Ievgen Vovk's avatar
Ievgen Vovk committed
418
                data.dl1.tel[tel_i + 1].pulse_time = event_data['pulse_time']
419
                data.dl1.tel[tel_i + 1].badpixels = event_data['bad_pixels']
420
421
422
                # data.dl1.tel[tel_i + 1].badpixels = np.array(
                #     file['dl1/tel' + str(i_tel + 1) + '/badpixels'], dtype=np.bool)

423
                if self.is_mc == False:
Moritz Huetten's avatar
Moritz Huetten committed
424
425
426
427
428
                    # Adding the event arrival time
                    time_tmp = Time(event_data['mjd'], scale='utc', format='mjd')
                    data.trig.gps_time = Time(time_tmp, format='unix', scale='utc', precision=9)
                else:
                    data.mc.energy = event_data['true_energy'] * u.GeV
429
                    data.mc.alt = (np.pi/2 - event_data['true_zd']) * u.rad
430
                    data.mc.az = -1 * (event_data['true_az'] - np.deg2rad(180 - 7)) * u.rad # check meaning of 7deg transformation (I.Vovk)
Moritz Huetten's avatar
Moritz Huetten committed
431
432
433
434
                    data.mc.shower_primary_id = 1 - event_data['true_shower_primary_id']
                    data.mc.h_first_int = event_data['true_h_first_int'] * u.cm
                    data.mc.core_x = event_data['true_core_x'] * u.cm
                    data.mc.core_y = event_data['true_core_y'] * u.cm
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

                # Setting the telescopes with data
                data.r0.tels_with_data = tels_with_data
                data.r1.tels_with_data = tels_with_data
                data.dl0.tels_with_data = tels_with_data
                data.trig.tels_with_trigger = tels_with_data

                yield data
                counter += 1

        return

    def _pedestal_event_generator(self, telescope):
        """
        Pedestal event generator. Yields DataContainer instances, filled
        with the read event data.

        Parameters
        ----------
        telescope: str
            The telescope for which to return events. Can be either "M1" or "M2".

        Returns
        -------

        """

        counter = 0
        telescope = telescope.upper()

        # Data container - is initialized once, and data is replaced within it after each yield
        data = DataContainer()

        # Telescopes with data:
        tels_in_file = ["M1", "M2"]

        if telescope not in tels_in_file:
            raise ValueError("Specified telescope {:s} is not in the allowed list {}".format(telescope, tels_in_file))

        tel_i = tels_in_file.index(telescope)
        tels_with_data = {tel_i + 1, }

        # Loop over the available data runs
478
        for run_number in self.run_numbers:
479
480
481
482
483
484
485

            # Removing the previously read data run from memory
            if self.current_run is not None:
                if 'data' in self.current_run:
                    del self.current_run['data']

            # Setting the new active run
Moritz Huetten's avatar
Moritz Huetten committed
486
            self.current_run = self._set_active_run(run_number)
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501

            if telescope == 'M1':
                n_events = self.current_run['data'].n_pedestal_events_m1
            else:
                n_events = self.current_run['data'].n_pedestal_events_m2

            # Loop over the events
            for event_i in range(n_events):
                # Event and run ids
                event_order_number = self.current_run['data'].pedestal_ids[telescope][event_i]
                event_id = self.current_run['data'].event_data[telescope]['stereo_event_number'][event_order_number]
                obs_id = self.current_run['number']

                # Reading event data
                event_data = self.current_run['data'].get_pedestal_event_data(event_i, telescope=telescope)
Moritz Huetten's avatar
Moritz Huetten committed
502
                
503
                data.meta = event_data['mars_meta']
504
505
506
507
508
509
510
511

                # Event counter
                data.count = counter

                # Setting up the R0 container
                data.r0.obs_id = obs_id
                data.r0.event_id = event_id
                data.r0.tel.clear()
512
                data.r0.tel[tel_i + 1].trigger_type = self.current_run['data'].event_data[telescope]['trigger_pattern'][event_order_number]
513
514
515
516
517

                # Setting up the R1 container
                data.r1.obs_id = obs_id
                data.r1.event_id = event_id
                data.r1.tel.clear()
518
                data.r1.tel[tel_i + 1].trigger_type = self.current_run['data'].event_data[telescope]['trigger_pattern'][event_order_number]
519
520
521
522
523

                # Setting up the DL0 container
                data.dl0.obs_id = obs_id
                data.dl0.event_id = event_id
                data.dl0.tel.clear()
524
                data.dl0.tel[tel_i + 1].trigger_type = self.current_run['data'].event_data[telescope]['trigger_pattern'][event_order_number]
525
526
527
528
529
530
531
532
533
534
535
536
537

                # Creating the telescope pointing container
                pointing = TelescopePointingContainer()
                pointing.azimuth = np.deg2rad(event_data['pointing_az']) * u.rad
                pointing.altitude = np.deg2rad(90 - event_data['pointing_zd']) * u.rad
                pointing.ra = np.deg2rad(event_data['pointing_ra']) * u.rad
                pointing.dec = np.deg2rad(event_data['pointing_dec']) * u.rad

                # Adding the pointing container to the event data
                data.pointing[tel_i + 1] = pointing

                # Adding event charge and peak positions per pixel
                data.dl1.tel[tel_i + 1].image = event_data['image']
Ievgen Vovk's avatar
Ievgen Vovk committed
538
                data.dl1.tel[tel_i + 1].pulse_time = event_data['pulse_time']
539
                data.dl1.tel[tel_i + 1].badpixels = event_data['bad_pixels']
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
                # data.dl1.tel[tel_i + 1].badpixels = np.array(
                #     file['dl1/tel' + str(i_tel + 1) + '/badpixels'], dtype=np.bool)

                # Adding the event arrival time
                time_tmp = Time(event_data['mjd'], scale='utc', format='mjd')
                data.trig.gps_time = Time(time_tmp, format='unix', scale='utc', precision=9)

                # Setting the telescopes with data
                data.r0.tels_with_data = tels_with_data
                data.r1.tels_with_data = tels_with_data
                data.dl0.tels_with_data = tels_with_data
                data.trig.tels_with_trigger = tels_with_data

                yield data
                counter += 1

        return


Moritz Huetten's avatar
Moritz Huetten committed
559
class MarsRun:
560
561
562
563
    """
    This class implements reading of the event data from a single MAGIC data run.
    """

Moritz Huetten's avatar
Moritz Huetten committed
564
    def __init__(self, run_file_mask, filter_list=None):
565
566
567
568
569
570
571
572
573
        """
        Constructor of the class. Defines the run to use and the camera pixel arrangement.

        Parameters
        ----------
        run_file_mask: str
            A path mask for files belonging to the run. Must correspond to a single run
            or an exception will be raised. Must correspond to calibrated ("sorcerer"-level)
            data.
574
575
576
        filter_list: list, optional
            A list of files, to which the run_file_mask should be applied. If None, all the
            files satisfying run_file_mask will be used. Defaults to None.
577
578
579
580
581
582
        """

        self.run_file_mask = run_file_mask

        # Preparing the lists of M1/2 data files
        file_list = glob.glob(run_file_mask)
583
584
585
586
587

        # Filtering out extra files if necessary
        if filter_list is not None:
            file_list = list(set(file_list) & set(filter_list))

588
589
        self.m1_file_list = list(filter(lambda name: '_M1_' in name, file_list))
        self.m2_file_list = list(filter(lambda name: '_M2_' in name, file_list))
590
        self.m1_file_list.sort()
591
592
593
        self.m2_file_list.sort()

        # Retrieving the list of run numbers corresponding to the data files
Moritz Huetten's avatar
Moritz Huetten committed
594
595
        run_info = list(map(MAGICEventSource._get_run_info_from_name, file_list))
        run_numbers = [i[0] for i in run_info]
596
597
598
599
600
601
602
603
604
        is_mc_runs   = [i[1] for i in run_info]

        run_numbers = np.unique(run_numbers)
        is_mc_runs = np.unique(is_mc_runs)
        # Checking if runt type (data/MC) is consistent:
        if len(is_mc_runs) > 1:
            raise ValueError("Run type is not consistently data or MC: {}".format(is_mc))
        
        self.is_mc = is_mc_runs[0]
605
606
607
608
609
610
611

        # Checking if a single run is going to be read
        if len(run_numbers) > 1:
            raise ValueError("Run mask corresponds to more than one run: {}".format(run_numbers))

        # Reading the event data
        self.event_data = dict()
Moritz Huetten's avatar
Moritz Huetten committed
612
613
        self.event_data['M1'] = self.load_events(self.m1_file_list, self.is_mc)
        self.event_data['M2'] = self.load_events(self.m2_file_list, self.is_mc)
614
615
616
617
618
619
620
621

        # Detecting pedestal events
        self.pedestal_ids = self._find_pedestal_events()
        # Detecting stereo events
        self.stereo_ids = self._find_stereo_events()
        # Detecting mono events
        self.mono_ids = self._find_mono_events()

622
623
        self.n_camera_pixels = 1039

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
    @property
    def n_events_m1(self):
        return len(self.event_data['M1']['MJD'])

    @property
    def n_events_m2(self):
        return len(self.event_data['M2']['MJD'])

    @property
    def n_stereo_events(self):
        return len(self.stereo_ids)

    @property
    def n_mono_events_m1(self):
        return len(self.mono_ids['M1'])

    @property
    def n_mono_events_m2(self):
        return len(self.mono_ids['M2'])

    @property
    def n_pedestal_events_m1(self):
        return len(self.pedestal_ids['M1'])

    @property
    def n_pedestal_events_m2(self):
        return len(self.pedestal_ids['M2'])

    @staticmethod
Moritz Huetten's avatar
Moritz Huetten committed
653
    def load_events(file_list, is_mc):
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
        """
        This method loads events from the pre-defiled file and returns them as a dictionary.

        Parameters
        ----------
        file_name: str
            Name of the MAGIC calibrated file to use.

        Returns
        -------
        dict:
            A dictionary with the even properties: charge / arrival time data, trigger, direction etc.
        """

        try:
            import uproot
        except ImportError:
            msg = "The `uproot` python module is required to access the MAGIC data"
            raise ImportError(msg)

        event_data = dict()

        event_data['charge'] = []
        event_data['arrival_time'] = []
678
679
        event_data['trigger_pattern'] = scipy.array([], dtype=np.int32)
        event_data['stereo_event_number'] = scipy.array([], dtype=np.int32)
680
681
682
683
684
        event_data['pointing_zd'] = scipy.array([])
        event_data['pointing_az'] = scipy.array([])
        event_data['pointing_ra'] = scipy.array([])
        event_data['pointing_dec'] = scipy.array([])
        event_data['MJD'] = scipy.array([])
685
        event_data['badpixelinfo'] = []
Moritz Huetten's avatar
Moritz Huetten committed
686
        event_data['mars_meta'] = []
687
688

        # run-wise meta information (same for all events)
Moritz Huetten's avatar
Moritz Huetten committed
689
        mars_meta = dict()
690
        
691
692
        event_data['file_edges'] = [0]

693
694
695
696
        degrees_per_hour = 15.0
        seconds_per_day = 86400.0
        seconds_per_hour = 3600.

Moritz Huetten's avatar
Moritz Huetten committed
697
698
699
700
701
702
703
        evt_common_list = [
            'MCerPhotEvt.fPixels.fPhot', 
            'MArrivalTime.fData',
            'MTriggerPattern.fPrescaled',
            'MRawEvtHeader.fStereoEvtNumber', 
            'MRawEvtHeader.fDAQEvtNumber',
            ]
704
        
Moritz Huetten's avatar
Moritz Huetten committed
705
706
707
708
709
710
711
712
713
714
715
716
        # Separately, because only used with pre-processed MARS data 
        # to create MPointingPos container
        pointing_array_list = [
            'MPointingPos.fZd', 
            'MPointingPos.fAz', 
            'MPointingPos.fRa', 
            'MPointingPos.fDec', 
            'MPointingPos.fDevZd',
            'MPointingPos.fDevAz', 
            'MPointingPos.fDevHa', 
            'MPointingPos.fDevDec',
            ]
717
        
Moritz Huetten's avatar
Moritz Huetten committed
718
719
720
721
722
723
        # Info only applicable for data:
        time_array_list = [
            'MTime.fMjd',
            'MTime.fTime.fMilliSec',
            'MTime.fNanoSec', 
            ]
Moritz Huetten's avatar
Moritz Huetten committed
724
        
Moritz Huetten's avatar
Moritz Huetten committed
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
        drive_array_list = [
            'MReportDrive.fMjd',
            'MReportDrive.fCurrentZd',
            'MReportDrive.fCurrentAz',
            'MReportDrive.fRa',
            'MReportDrive.fDec'
            ]

        # Info only applicable for MC:
        mc_list = [
            'MMcEvt.fEnergy',
            'MMcEvt.fTheta',
            'MMcEvt.fPhi',
            'MMcEvt.fPartId',
            'MMcEvt.fZFirstInteraction',
            'MMcEvt.fCoreX',
            'MMcEvt.fCoreY', 
            ]

        # Metadata, currently not strictly required
        metainfo_array_list = [
            'MRawRunHeader.fRunNumber',
            'MRawRunHeader.fRunType',
            'MRawRunHeader.fSubRunIndex',
            'MRawRunHeader.fSourceRA',
            'MRawRunHeader.fSourceDEC',
            'MRawRunHeader.fTelescopeNumber']
752

753
754
755
756
        for file_name in file_list:

            input_file = uproot.open(file_name)

Moritz Huetten's avatar
Moritz Huetten committed
757
            events = input_file['Events'].arrays(evt_common_list)
758

759
            # Reading the info common to MC and real data
760
761
762
763
            charge = events[b'MCerPhotEvt.fPixels.fPhot']
            arrival_time = events[b'MArrivalTime.fData']
            trigger_pattern = events[b'MTriggerPattern.fPrescaled']
            stereo_event_number = events[b'MRawEvtHeader.fStereoEvtNumber']
764

Moritz Huetten's avatar
Moritz Huetten committed
765
            # Reading meta information:
Moritz Huetten's avatar
Moritz Huetten committed
766
            mars_meta['is_simulation'] = is_mc
Moritz Huetten's avatar
Moritz Huetten committed
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
            try:
                meta_info = input_file['RunHeaders'].arrays(metainfo_array_list)
                
                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['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 = False
                elif is_simulation == 256:
                    is_simulation = True
                else:
                    msg = "Run type (Data or MC) of MAGIC data file not recognised."
Moritz Huetten's avatar
Moritz Huetten committed
785
786
                    logger.error(msg)
                    raise ValueError(msg)
Moritz Huetten's avatar
Moritz Huetten committed
787
788
                if is_simulation != is_mc:
                    msg = "Inconsistent run type (data or MC) between file name and runheader content."
Moritz Huetten's avatar
Moritz Huetten committed
789
790
                    logger.error(msg)
                    raise ValueError(msg)
Moritz Huetten's avatar
Moritz Huetten committed
791
                
Moritz Huetten's avatar
Moritz Huetten committed
792
793
794
795
796
797
798
799
800
801
802
                # Reading the info only contained in real data
                if is_simulation == False:
                    badpixelinfo = input_file['RunHeaders']['MBadPixelsCam.fArray.fInfo'].array(uproot.asjagged(uproot.asdtype(np.int32))).flatten().reshape((4, 1183), order='F')
                    # now we have 3 axes:
                    # 1st axis: Unsuitable pixels
                    # 2nd axis: Uncalibrated pixels (says why pixel is unsuitable)
                    # 3rd axis: Bad hardware pixels (says why pixel is unsuitable)
                    # Each axis cointains a 32bit integer encoding more information about the specific problem, see MARS software, MBADPixelsPix.h
                    # Here, we however discard this additional information and only grep the "unsuitable" axis.
                    badpixelinfo = badpixelinfo[1].astype(bool)
                else:
Moritz Huetten's avatar
Moritz Huetten committed
803
                    badpixelinfo = np.zeros(1183).astype(bool)
Moritz Huetten's avatar
Moritz Huetten committed
804
805
            except KeyError:
                logger.warning("RunHeaders tree not present in file. Cannot read meta information and assume it is a real data run.")
806
                badpixelinfo = np.zeros(1183)
Moritz Huetten's avatar
Moritz Huetten committed
807
                is_simulation = False
808

Moritz Huetten's avatar
Moritz Huetten committed
809
810
811
            if is_simulation == False:
                event_times = input_file['Events'].arrays(time_array_list)
                # Computing the event arrival time
812
                
Moritz Huetten's avatar
Moritz Huetten committed
813
814
815
816
817
                mjd = event_times[b'MTime.fMjd']
                millisec = event_times[b'MTime.fTime.fMilliSec']
                nanosec = event_times[b'MTime.fNanoSec']
    
                mjd = mjd + (millisec / 1e3 + nanosec / 1e9) / seconds_per_day
818
819

            # Reading pointing information (in units of degrees):
820
821
            if 'MPointingPos.' in input_file['Events']:
                # Retrieving the telescope pointing direction
822
823
                pointing = input_file['Events'].arrays(pointing_array_list)

824
825
826
827
                pointing_zd = pointing[b'MPointingPos.fZd'] - pointing[b'MPointingPos.fDevZd']
                pointing_az = pointing[b'MPointingPos.fAz'] - pointing[b'MPointingPos.fDevAz']
                pointing_ra = (pointing[b'MPointingPos.fRa'] + pointing[b'MPointingPos.fDevHa']) * degrees_per_hour # N.B. the positive sign here, as HA = local sidereal time - ra
                pointing_dec = pointing[b'MPointingPos.fDec'] - pointing[b'MPointingPos.fDevDec']
828
829
            else:
                # Getting the telescope drive info
830
831
832
833
834
                drive = input_file['Drive'].arrays(drive_array_list)

                drive_mjd = drive[b'MReportDrive.fMjd']
                drive_zd = drive[b'MReportDrive.fCurrentZd']
                drive_az = drive[b'MReportDrive.fCurrentAz']
835
                drive_ra = drive[b'MReportDrive.fRa'] * degrees_per_hour
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
                drive_dec = drive[b'MReportDrive.fDec']

                # Finding only non-repeating drive entries
                # Repeating entries lead to failure in pointing interpolation
                non_repeating = scipy.diff(drive_mjd) > 0
                non_repeating = scipy.concatenate((non_repeating, [True]))

                # Filtering out the repeating ones
                drive_mjd = drive_mjd[non_repeating]
                drive_zd = drive_zd[non_repeating]
                drive_az = drive_az[non_repeating]
                drive_ra = drive_ra[non_repeating]
                drive_dec = drive_dec[non_repeating]

                if len(drive_zd) > 2:
                    # If there are enough drive data, creating azimuth and zenith angles interpolators
                    drive_zd_pointing_interpolator = scipy.interpolate.interp1d(drive_mjd, drive_zd, fill_value="extrapolate")
                    drive_az_pointing_interpolator = scipy.interpolate.interp1d(drive_mjd, drive_az, fill_value="extrapolate")

                    # Creating azimuth and zenith angles interpolators
                    drive_ra_pointing_interpolator = scipy.interpolate.interp1d(drive_mjd, drive_ra, fill_value="extrapolate")
                    drive_dec_pointing_interpolator = scipy.interpolate.interp1d(drive_mjd, drive_dec, fill_value="extrapolate")

                    # Interpolating the drive pointing to the event time stamps
                    pointing_zd = drive_zd_pointing_interpolator(mjd)
                    pointing_az = drive_az_pointing_interpolator(mjd)
                    pointing_ra = drive_ra_pointing_interpolator(mjd)
                    pointing_dec = drive_dec_pointing_interpolator(mjd)

                else:
                    # Not enough data to interpolate the pointing direction.
                    pointing_zd = scipy.repeat(-1, len(mjd))
                    pointing_az = scipy.repeat(-1, len(mjd))
                    pointing_ra = scipy.repeat(-1, len(mjd))
                    pointing_dec = scipy.repeat(-1, len(mjd))
871
872
873

            event_data['charge'].append(charge)
            event_data['arrival_time'].append(arrival_time)
874
            event_data['badpixelinfo'].append(badpixelinfo)
Moritz Huetten's avatar
Moritz Huetten committed
875
            event_data['mars_meta'].append(mars_meta)
876
            event_data['trigger_pattern'] = scipy.concatenate((event_data['trigger_pattern'], trigger_pattern))
877
            event_data['stereo_event_number'] = scipy.concatenate((event_data['stereo_event_number'], stereo_event_number))
878
879
880
881
            event_data['pointing_zd'] = scipy.concatenate((event_data['pointing_zd'], pointing_zd))
            event_data['pointing_az'] = scipy.concatenate((event_data['pointing_az'], pointing_az))
            event_data['pointing_ra'] = scipy.concatenate((event_data['pointing_ra'], pointing_ra))
            event_data['pointing_dec'] = scipy.concatenate((event_data['pointing_dec'], pointing_dec))
Moritz Huetten's avatar
Moritz Huetten committed
882
883
884
885
886
887
888
889
890
891
892
893
            if is_simulation == False:
                event_data['MJD'] = scipy.concatenate((event_data['MJD'], mjd))
            else:
                mc_info = input_file['Events'].arrays(mc_list)
                # N.B.: For MC, there is only one subrun
                event_data['true_energy'] = mc_info[b'MMcEvt.fEnergy']
                event_data['true_zd'] = mc_info[b'MMcEvt.fTheta']
                event_data['true_az'] = mc_info[b'MMcEvt.fPhi']
                event_data['true_shower_primary_id'] = mc_info[b'MMcEvt.fPartId']
                event_data['true_h_first_int'] = mc_info[b'MMcEvt.fZFirstInteraction']
                event_data['true_core_x'] = mc_info[b'MMcEvt.fCoreX']
                event_data['true_core_y'] = mc_info[b'MMcEvt.fCoreY']
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928

            event_data['file_edges'].append(len(event_data['trigger_pattern']))

        return event_data

    def _find_pedestal_events(self):
        """
        This internal method identifies the IDs (order numbers) of the
        pedestal events in the run.

        Returns
        -------
        dict:
            A dictionary of pedestal event IDs in M1/2 separately.
        """

        pedestal_ids = dict()

        pedestal_trigger_pattern = 8

        for telescope in self.event_data:
            ped_triggers = np.where(self.event_data[telescope]['trigger_pattern'] == pedestal_trigger_pattern)
            pedestal_ids[telescope] = ped_triggers[0]

        return pedestal_ids

    def _find_stereo_events(self):
        """
        This internal methods identifies stereo events in the run.

        Returns
        -------
        list:
            A list of pairs (M1_id, M2_id) corresponding to stereo events in the run.
        """
929
930
931
932
933

        stereo_ids = []

        n_m1_events = len(self.event_data['M1']['stereo_event_number'])
        n_m2_events = len(self.event_data['M2']['stereo_event_number'])
Ievgen Vovk's avatar
Ievgen Vovk committed
934
        if (n_m1_events == 0) or (n_m2_events == 0):
935
936
            return stereo_ids

Ievgen Vovk's avatar
Ievgen Vovk committed
937
        if not self.is_mc:
Moritz Huetten's avatar
Moritz Huetten committed
938
            data_trigger_pattern = 128
Ievgen Vovk's avatar
Ievgen Vovk committed
939

Moritz Huetten's avatar
Moritz Huetten committed
940
            m2_data_condition = (self.event_data['M2']['trigger_pattern'] == data_trigger_pattern)
Ievgen Vovk's avatar
Ievgen Vovk committed
941

Moritz Huetten's avatar
Moritz Huetten committed
942
943
944
945
            for m1_id in range(0, n_m1_events):
                if self.event_data['M1']['trigger_pattern'][m1_id] == data_trigger_pattern:
                    m2_stereo_condition = (self.event_data['M2']['stereo_event_number'] ==
                                           self.event_data['M1']['stereo_event_number'][m1_id])
Ievgen Vovk's avatar
Ievgen Vovk committed
946

Moritz Huetten's avatar
Moritz Huetten committed
947
                    m12_match = np.where(m2_data_condition & m2_stereo_condition)
Ievgen Vovk's avatar
Ievgen Vovk committed
948

Moritz Huetten's avatar
Moritz Huetten committed
949
950
951
952
953
                    if len(m12_match[0]) > 0:
                        stereo_pair = (m1_id, m12_match[0][0])
                        stereo_ids.append(stereo_pair)
        else:
            data_trigger_pattern = 1
Ievgen Vovk's avatar
Ievgen Vovk committed
954

Moritz Huetten's avatar
Moritz Huetten committed
955
            m2_data_condition = (self.event_data['M2']['trigger_pattern'] == data_trigger_pattern)
Ievgen Vovk's avatar
Ievgen Vovk committed
956

Moritz Huetten's avatar
Moritz Huetten committed
957
958
959
960
            for m1_id in range(0, n_m1_events):
                if self.event_data['M1']['trigger_pattern'][m1_id] == data_trigger_pattern and self.event_data['M1']['stereo_event_number'][m1_id] != 0:
                    m2_stereo_condition = (self.event_data['M2']['stereo_event_number'] ==
                                           self.event_data['M1']['stereo_event_number'][m1_id])
Ievgen Vovk's avatar
Ievgen Vovk committed
961

Moritz Huetten's avatar
Moritz Huetten committed
962
                    m12_match = np.where(m2_data_condition & m2_stereo_condition)
Ievgen Vovk's avatar
Ievgen Vovk committed
963

Moritz Huetten's avatar
Moritz Huetten committed
964
965
966
                    if len(m12_match[0]) > 0:
                        stereo_pair = (m1_id, m12_match[0][0])
                        stereo_ids.append(stereo_pair)
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983

        return stereo_ids

    def _find_mono_events(self):
        """
        This internal method identifies the IDs (order numbers) of the
        pedestal events in the run.

        Returns
        -------
        dict:
            A dictionary of pedestal event IDs in M1/2 separately.
        """

        mono_ids = dict()
        mono_ids['M1'] = []
        mono_ids['M2'] = []
Moritz Huetten's avatar
Moritz Huetten committed
984
        
985
986
        n_m1_events = len(self.event_data['M1']['stereo_event_number'])
        n_m2_events = len(self.event_data['M2']['stereo_event_number'])
987

Ievgen Vovk's avatar
Ievgen Vovk committed
988
        if not self.is_mc:
Moritz Huetten's avatar
Moritz Huetten committed
989
990
991
992
993
994
995
996
997
998
999
1000
            data_trigger_pattern = 128
    
            m1_data_condition = self.event_data['M1']['trigger_pattern'] == data_trigger_pattern
            m2_data_condition = self.event_data['M2']['trigger_pattern'] == data_trigger_pattern
    
            for m1_id in range(0, n_m1_events):
                if m1_data_condition[m1_id]:
                    m2_stereo_condition = (self.event_data['M2']['stereo_event_number'] ==
                                           self.event_data['M1']['stereo_event_number'][m1_id])
    
                    m12_match = np.where(m2_data_condition & m2_stereo_condition)