Example #1
0
 def __getitem__(self, idx):
     index, = np.where(self.events["event_num"] == idx)
     try:
         event_data = self.events[index[0]]
     except IndexError:
         raise IndexError(f"event_num {idx} not in data {self.input_url}")
     event = DataContainer(count=idx)
     return self._fill_container(container=event, data=event_data)
Example #2
0
def test_rescale_dl1_charge():
    event = DataContainer()
    tel_ids = [1, 3]
    images = {}
    for tel_id in tel_ids:
        images[tel_id] = np.random.rand(1855)
        event.dl1.tel[tel_id].image = copy(images[tel_id])

    rescaling_factor = np.random.rand() * 10
    rescale_dl1_charge(event, rescaling_factor)

    for tel_id in tel_ids:
        np.testing.assert_allclose(event.dl1.tel[tel_id].image,
                                   images[tel_id] * rescaling_factor)
Example #3
0
def test_dl1_charge_calib(example_subarray):
    camera = CameraGeometry.from_name("CHEC")
    n_pixels = camera.n_pixels
    n_samples = 96
    mid = n_samples // 2
    pulse_sigma = 6
    random = np.random.RandomState(1)
    x = np.arange(n_samples)

    # Randomize times and create pulses
    time_offset = random.uniform(mid - 10, mid + 10, n_pixels)[:, np.newaxis]
    y = norm.pdf(x, time_offset, pulse_sigma).astype("float32")

    # Define absolute calibration coefficients
    absolute = random.uniform(100, 1000, n_pixels).astype("float32")
    y *= absolute[:, np.newaxis]

    # Define relative coefficients
    relative = random.normal(1, 0.01, n_pixels)
    y /= relative[:, np.newaxis]

    # Define pedestal
    pedestal = random.uniform(-4, 4, n_pixels)
    y += pedestal[:, np.newaxis]

    event = DataContainer()
    telid = list(example_subarray.tel.keys())[0]
    event.dl0.tel[telid].waveform = y

    # Test default
    calibrator = CameraCalibrator(
        subarray=example_subarray,
        image_extractor=FullWaveformSum(subarray=example_subarray),
    )
    calibrator(event)
    np.testing.assert_allclose(event.dl1.tel[telid].image, y.sum(1), rtol=1e-4)

    event.calibration.tel[telid].dl1.time_shift = time_offset
    event.calibration.tel[telid].dl1.pedestal_offset = pedestal * n_samples
    event.calibration.tel[telid].dl1.absolute_factor = absolute
    event.calibration.tel[telid].dl1.relative_factor = relative

    # Test without need for timing corrections
    calibrator = CameraCalibrator(
        subarray=example_subarray,
        image_extractor=FullWaveformSum(subarray=example_subarray),
    )
    calibrator(event)
    np.testing.assert_allclose(event.dl1.tel[telid].image, 1, rtol=1e-5)
Example #4
0
def test_check_dl0_empty(example_event, example_subarray):
    calibrator = CameraCalibrator(subarray=example_subarray)
    telid = list(example_event.r0.tel)[0]
    calibrator._calibrate_dl0(example_event, telid)
    waveform = example_event.dl0.tel[telid].waveform.copy()
    with pytest.warns(UserWarning):
        example_event.dl0.tel[telid].waveform = None
        calibrator._calibrate_dl1(example_event, telid)
        assert example_event.dl1.tel[telid].image is None

    assert calibrator._check_dl0_empty(None) is True
    assert calibrator._check_dl0_empty(waveform) is False

    calibrator = CameraCalibrator(subarray=example_subarray)
    event = DataContainer()
    event.dl1.tel[telid].image = np.full(2048, 2)
    with pytest.warns(UserWarning):
        calibrator(event)
    assert (event.dl1.tel[telid].image == 2).all()
Example #5
0
def test_check_r1_empty(example_event, example_subarray):
    calibrator = CameraCalibrator(subarray=example_subarray)
    telid = list(example_event.r0.tel)[0]
    waveform = example_event.r1.tel[telid].waveform.copy()
    with pytest.warns(UserWarning):
        example_event.r1.tel[telid].waveform = None
        calibrator._calibrate_dl0(example_event, telid)
        assert example_event.dl0.tel[telid].waveform is None

    assert calibrator._check_r1_empty(None) is True
    assert calibrator._check_r1_empty(waveform) is False

    calibrator = CameraCalibrator(
        subarray=example_subarray,
        image_extractor=FullWaveformSum(subarray=example_subarray))
    event = DataContainer()
    event.dl0.tel[telid].waveform = np.full((2048, 128), 2)
    with pytest.warns(UserWarning):
        calibrator(event)
    assert (event.dl0.tel[telid].waveform == 2).all()
    assert (event.dl1.tel[telid].image == 2 * 128).all()
Example #6
0
    def _generator(self):
        """
        Stereo event generator. Yields DataContainer instances, filled
        with the read event data.

        Returns
        -------

        """
        counter = 0
        data = DataContainer()
        data.meta['origin'] = "MAGIC"
        data.meta['input_url'] = self.input_url
        data.meta['is_simulation'] = True
        data.mcheader = self._mc_header

        if self.calib_M1 is not None and self.calib_M2 is not None:
            #Reading data from root file for Events table
            eventid_M1 = np.asarray(
                self.calib_M1["MRawEvtHeader.fStereoEvtNumber"].array())
            eventid_M2 = np.asarray(
                self.calib_M2["MRawEvtHeader.fStereoEvtNumber"].array())
            zenith = np.asarray(self.calib_M1["MMcEvt.fTheta"].array())
            pointing_altitude = np.asarray(
                self.calib_M1["MPointingPos.fZd"].array())
            azimuth = np.asarray(self.calib_M1["MMcEvt.fPhi"].array())
            pointing_azimuth = np.asarray(
                self.calib_M1["MPointingPos.fAz"].array())
            core_x = np.asarray(self.calib_M1["MMcEvt.fCoreX"].array())
            core_y = np.asarray(self.calib_M1["MMcEvt.fCoreY"].array())
            mc_energy = np.asarray(
                self.calib_M1["MMcEvt.fEnergy"].array()) / 1000.0
            h_first_int = np.asarray(
                self.calib_M1["MMcEvt.fZFirstInteraction"].array())

            #Reading data from root file for Image table
            charge_M1 = np.asarray(
                self.calib_M1["MCerPhotEvt.fPixels.fPhot"].array())
            peak_time_M1 = np.asarray(
                self.calib_M1["MArrivalTime.fData"].array())
            charge_M2 = np.asarray(
                self.calib_M2["MCerPhotEvt.fPixels.fPhot"].array())
            peak_time_M2 = np.asarray(
                self.calib_M2["MArrivalTime.fData"].array())

        if self.superstar is not None:
            #Reading data from root file for Events table
            eventid_M1 = np.asarray(
                self.superstar["MRawEvtHeader_1.fStereoEvtNumber"].array())
            eventid_M2 = np.asarray(
                self.superstar["MRawEvtHeader_2.fStereoEvtNumber"].array())
            zenith = np.asarray(self.superstar["MMcEvt_1.fTheta"].array())
            pointing_altitude = np.asarray(
                self.superstar["MPointingPos_1.fZd"].array())
            azimuth = np.asarray(self.superstar["MMcEvt_1.fPhi"].array())
            pointing_azimuth = np.asarray(
                self.superstar["MPointingPos_1.fAz"].array())
            core_x = np.asarray(self.superstar["MMcEvt_1.fCoreX"].array())
            core_y = np.asarray(self.superstar["MMcEvt_1.fCoreY"].array())
            mc_energy = np.asarray(
                self.superstar["MMcEvt_1.fEnergy"].array()) / 1000.0
            h_first_int = np.asarray(
                self.superstar["MMcEvt_1.fZFirstInteraction"].array())

            #Reading data from root file for Parameter table
            hillas_intensity_M1 = np.asarray(
                self.superstar["MHillas_1.fSize"].array())
            hillas_intensity_M2 = np.asarray(
                self.superstar["MHillas_2.fSize"].array())
            hillas_x_M1 = np.asarray(
                self.superstar["MHillas_1.fMeanX"].array())
            hillas_x_M2 = np.asarray(
                self.superstar["MHillas_2.fMeanX"].array())
            hillas_y_M1 = np.asarray(
                self.superstar["MHillas_1.fMeanY"].array())
            hillas_y_M2 = np.asarray(
                self.superstar["MHillas_2.fMeanY"].array())
            hillas_r_M1 = np.sqrt(
                np.power(hillas_x_M1, 2) + np.power(hillas_y_M1, 2))
            hillas_r_M2 = np.sqrt(
                np.power(hillas_x_M2, 2) + np.power(hillas_y_M2, 2))
            hillas_phi_M1 = np.arctan2(hillas_y_M1, hillas_x_M1)
            hillas_phi_M2 = np.arctan2(hillas_y_M2, hillas_x_M2)
            hillas_length_M1 = np.asarray(
                self.superstar["MHillas_1.fLength"].array())
            hillas_length_M2 = np.asarray(
                self.superstar["MHillas_2.fLength"].array())
            hillas_width_M1 = np.asarray(
                self.superstar["MHillas_1.fWidth"].array())
            hillas_width_M2 = np.asarray(
                self.superstar["MHillas_2.fWidth"].array())
            hillas_psi_M1 = np.asarray(
                self.superstar["MHillas_1.fDelta"].array())
            hillas_psi_M2 = np.asarray(
                self.superstar["MHillas_2.fDelta"].array())
            hillas_skewness_M1 = np.asarray(
                self.superstar["MHillasExt_1.fM3Long"].array())
            hillas_skewness_M2 = np.asarray(
                self.superstar["MHillasExt_2.fM3Long"].array())

            leakage_intensity_1_M1 = np.asarray(
                self.superstar["MNewImagePar_1.fLeakage1"].array())
            leakage_intensity_1_M2 = np.asarray(
                self.superstar["MNewImagePar_2.fLeakage1"].array())
            leakage_intensity_2_M1 = np.asarray(
                self.superstar["MNewImagePar_1.fLeakage2"].array())
            leakage_intensity_2_M2 = np.asarray(
                self.superstar["MNewImagePar_2.fLeakage2"].array())

            num_islands_M1 = np.asarray(
                self.superstar["MCerPhotEvt_1.fNumIslands"].array())
            num_islands_M2 = np.asarray(
                self.superstar["MCerPhotEvt_2.fNumIslands"].array())

            #Reading data from root file for Image table (peak time and image mask not )
            charge_M1 = np.asarray(
                self.superstar["MCerPhotEvt_1.fPixels.fPhot"].array())
            peak_time_M1 = np.zeros((charge_M1.shape[0], 1039))
            image_mask_M1 = np.zeros((charge_M1.shape[0], 1039), dtype=bool)
            charge_M2 = np.asarray(
                self.superstar["MCerPhotEvt_2.fPixels.fPhot"].array())
            peak_time_M2 = np.zeros((charge_M2.shape[0], 1039))
            image_mask_M2 = np.zeros((charge_M2.shape[0], 1039), dtype=bool)

        # Get the shower primary id
        shower_primary_id = 1
        if self.file_list[0].split("/")[-1].startswith("GA"):
            shower_primary_id = 1

        #Iterating over all events, and saving only stereo ones
        total_events = min(len(charge_M1), len(charge_M2))
        tels_with_data = {1, 2}
        for i in range(0, total_events):
            if eventid_M1[i] != 0:
                obs_id = self.run_number
                event_id = eventid_M1[i]
                i2 = np.where(eventid_M2 == eventid_M1[i])
                i2 = i2[0].astype(int)
                data.count = counter

                # Setting up the Data container
                data.index.obs_id = obs_id
                data.index.event_id = event_id
                data.r0.tel.clear()
                data.r1.tel.clear()
                data.dl0.tel.clear()

                # Adding the array pointing in the pointing container
                data.pointing.array_altitude = u.Quantity(
                    np.deg2rad(90.0 - pointing_altitude[i]), u.rad)
                data.pointing.array_azimuth = u.Quantity(
                    np.deg2rad(pointing_azimuth[i]), u.rad)

                # Filling the DL1 container with the event data
                for tel_id in tels_with_data:
                    #Adding telescope pointing container
                    data.pointing.tel[tel_id].azimuth = u.Quantity(
                        np.deg2rad(pointing_azimuth[i]), u.rad)
                    data.pointing.tel[tel_id].altitude = u.Quantity(
                        np.deg2rad(90.0 - pointing_altitude[i]), u.rad)

                    #Adding MC data
                    data.mc.alt = Angle(np.pi / 2.0 - zenith[i], u.rad)
                    data.mc.az = Angle(
                        np.deg2rad(180.0 - 7.0) - azimuth[i], u.rad)
                    data.mc.x_max = u.Quantity(0, X_MAX_UNIT)
                    data.mc.h_first_int = u.Quantity(h_first_int[i], u.m)
                    data.mc.core_x = u.Quantity(core_x[i], u.m)
                    data.mc.core_y = u.Quantity(core_y[i], u.m)
                    data.mc.energy = u.Quantity(mc_energy[i], u.TeV)
                    data.mc.shower_primary_id = shower_primary_id

                    if self.superstar is not None:
                        leakage_values = LeakageContainer()
                        hillas_parameters_values = HillasParametersContainer()
                        concentration_values = ConcentrationContainer()
                        timing_values = TimingParametersContainer()
                        morphology_values = MorphologyContainer()

                    # Adding charge, peak time and parameters
                    if tel_id == 1:
                        data.dl1.tel[tel_id].image = charge_M1[i][:1039]
                        data.dl1.tel[tel_id].peak_time = peak_time_M1[i][:1039]
                        if self.superstar is not None:
                            data.dl1.tel[tel_id].image_mask = image_mask_M1[
                                i][:1039]

                            hillas_parameters_values[
                                "intensity"] = hillas_intensity_M1[i]
                            hillas_parameters_values["x"] = u.Quantity(
                                hillas_x_M1[i], unit=u.mm)
                            hillas_parameters_values["y"] = u.Quantity(
                                hillas_y_M1[i], unit=u.mm)
                            hillas_parameters_values["r"] = u.Quantity(
                                hillas_r_M1[i], unit=u.mm)
                            hillas_parameters_values["phi"] = u.Quantity(
                                hillas_phi_M1[i], unit=u.rad)
                            hillas_parameters_values["length"] = u.Quantity(
                                hillas_length_M1[i], unit=u.mm)
                            hillas_parameters_values["width"] = u.Quantity(
                                hillas_width_M1[i], unit=u.mm)
                            hillas_parameters_values["psi"] = u.Quantity(
                                hillas_psi_M1[i], unit=u.rad)
                            hillas_parameters_values[
                                "skewness"] = hillas_skewness_M1[i]

                            leakage_values[
                                "intensity_width_1"] = leakage_intensity_1_M1[
                                    i]
                            leakage_values[
                                "intensity_width_2"] = leakage_intensity_2_M1[
                                    i]

                            morphology_values["num_pixels"] = 1039
                            morphology_values["num_islands"] = num_islands_M1[
                                i]

                    else:
                        data.dl1.tel[tel_id].image = charge_M2[i][:1039]
                        data.dl1.tel[tel_id].peak_time = peak_time_M2[i][:1039]
                        if self.superstar is not None:
                            data.dl1.tel[tel_id].image_mask = image_mask_M2[
                                i][:1039]

                            hillas_parameters_values[
                                "intensity"] = hillas_intensity_M2[i]
                            hillas_parameters_values["x"] = u.Quantity(
                                hillas_x_M2[i], unit=u.mm)
                            hillas_parameters_values["y"] = u.Quantity(
                                hillas_y_M2[i], unit=u.mm)
                            hillas_parameters_values["r"] = u.Quantity(
                                hillas_r_M2[i], unit=u.mm)
                            hillas_parameters_values["phi"] = u.Quantity(
                                hillas_phi_M2[i], unit=u.rad)
                            hillas_parameters_values["length"] = u.Quantity(
                                hillas_length_M2[i], unit=u.mm)
                            hillas_parameters_values["width"] = u.Quantity(
                                hillas_width_M2[i], unit=u.mm)
                            hillas_parameters_values["psi"] = u.Quantity(
                                hillas_psi_M2[i], unit=u.rad)
                            hillas_parameters_values[
                                "skewness"] = hillas_skewness_M2[i]

                            leakage_values[
                                "intensity_width_1"] = leakage_intensity_1_M2[
                                    i]
                            leakage_values[
                                "intensity_width_2"] = leakage_intensity_2_M2[
                                    i]

                            morphology_values["num_pixels"] = 1039
                            morphology_values["num_islands"] = num_islands_M2[
                                i]

                    if self.superstar is not None:
                        data.dl1.tel[
                            tel_id].parameters.leakage = leakage_values
                        data.dl1.tel[
                            tel_id].parameters.hillas = hillas_parameters_values
                        data.dl1.tel[
                            tel_id].parameters.concentration = concentration_values
                        data.dl1.tel[tel_id].parameters.timing = timing_values
                        data.dl1.tel[
                            tel_id].parameters.morphology = morphology_values

                # 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

                yield data
                counter += 1
        return
    def _generator(self):
        """
        Stereo event generator. Yields DataContainer instances, filled
        with the read event data.

        Returns
        -------

        """
        counter = 0
        data = DataContainer()
        data.meta["origin"] = "MAGIC"
        data.meta["input_url"] = self.input_url
        data.meta["is_simulation"] = self.mc
        data.mcheader = self._header

        if self.calib_M1 is not None and self.calib_M2 is not None:
            # Reading data from root file for Events table
            shower_primary_id = self.magic_to_cta_shower_primary_id[int(
                self.superstar["MMcEvt_1.fPartId"].array()[0])]
            eventid_M1 = np.asarray(
                self.calib_M1["MRawEvtHeader.fStereoEvtNumber"].array())
            eventid_M2 = np.asarray(
                self.calib_M2["MRawEvtHeader.fStereoEvtNumber"].array())
            zenith = np.asarray(self.calib_M1["MMcEvt.fTheta"].array())
            pointing_altitude = np.asarray(
                self.calib_M1["MPointingPos.fZd"].array())
            azimuth = np.asarray(self.calib_M1["MMcEvt.fPhi"].array())
            pointing_azimuth = np.asarray(
                self.calib_M1["MPointingPos.fAz"].array())
            core_x = np.asarray(self.calib_M1["MMcEvt.fCoreX"].array())
            core_y = np.asarray(self.calib_M1["MMcEvt.fCoreY"].array())
            mc_energy = np.asarray(
                self.calib_M1["MMcEvt.fEnergy"].array()) / 1000.0
            h_first_int = np.asarray(
                self.calib_M1["MMcEvt.fZFirstInteraction"].array())

            # Reading data from root file for Image table
            charge_M1 = np.asarray(
                self.calib_M1["MCerPhotEvt.fPixels.fPhot"].array())
            peak_time_M1 = np.asarray(
                self.calib_M1["MArrivalTime.fData"].array())
            image_mask_M1 = np.asarray(self.calib_M1["CleanCharge"].array())
            for i, mask in enumerate(image_mask_M1):
                image_mask_M1[i] = np.array(mask) != 0

            charge_M2 = np.asarray(
                self.calib_M2["MCerPhotEvt.fPixels.fPhot"].array())
            peak_time_M2 = np.asarray(
                self.calib_M2["MArrivalTime.fData"].array())
            image_mask_M2 = np.asarray(self.calib_M2["CleanCharge"].array())
            for i, mask in enumerate(image_mask_M2):
                image_mask_M2[i] = np.array(mask) != 0

        if self.superstar is not None:
            # Reading data from root file for Events table
            # only read MC information if it exists
            if self.is_simulation:
                shower_primary_id = self.magic_to_cta_shower_primary_id[int(
                    self.superstar["MMcEvt_1.fPartId"].array()[0])]
                src_pos_cam_Y = np.asarray(
                    self.superstar["MSrcPosCam_1.fY"].array())
                src_pos_cam_X = np.asarray(
                    self.superstar["MSrcPosCam_1.fX"].array())
                core_x = np.asarray(self.superstar["MMcEvt_1.fCoreX"].array())
                core_y = np.asarray(self.superstar["MMcEvt_1.fCoreY"].array())
                mc_energy = (
                    np.asarray(self.superstar["MMcEvt_1.fEnergy"].array()) /
                    1000.0)
                h_first_int = np.asarray(
                    self.superstar["MMcEvt_1.fZFirstInteraction"].array())

            eventid_M1 = np.asarray(
                self.superstar["MRawEvtHeader_1.fStereoEvtNumber"].array())
            eventid_M2 = np.asarray(
                self.superstar["MRawEvtHeader_2.fStereoEvtNumber"].array())
            pointing_altitude = np.asarray(
                self.superstar["MPointingPos_1.fZd"].array())
            pointing_azimuth = np.asarray(
                self.superstar["MPointingPos_1.fAz"].array())

            # Reading data from root file for Parameter table
            hillas_intensity_M1 = np.asarray(
                self.superstar["MHillas_1.fSize"].array())
            hillas_intensity_M2 = np.asarray(
                self.superstar["MHillas_2.fSize"].array())
            hillas_x_M1 = np.asarray(
                self.superstar["MHillas_1.fMeanX"].array())
            hillas_x_M2 = np.asarray(
                self.superstar["MHillas_2.fMeanX"].array())
            hillas_y_M1 = np.asarray(
                self.superstar["MHillas_1.fMeanY"].array())
            hillas_y_M2 = np.asarray(
                self.superstar["MHillas_2.fMeanY"].array())
            hillas_r_M1 = np.sqrt(
                np.power(hillas_x_M1, 2) + np.power(hillas_y_M1, 2))
            hillas_r_M2 = np.sqrt(
                np.power(hillas_x_M2, 2) + np.power(hillas_y_M2, 2))
            hillas_phi_M1 = np.arctan2(hillas_y_M1, hillas_x_M1)
            hillas_phi_M2 = np.arctan2(hillas_y_M2, hillas_x_M2)
            hillas_length_M1 = np.asarray(
                self.superstar["MHillas_1.fLength"].array())
            hillas_length_M2 = np.asarray(
                self.superstar["MHillas_2.fLength"].array())
            hillas_width_M1 = np.asarray(
                self.superstar["MHillas_1.fWidth"].array())
            hillas_width_M2 = np.asarray(
                self.superstar["MHillas_2.fWidth"].array())
            hillas_psi_M1 = np.asarray(
                self.superstar["MHillas_1.fDelta"].array())
            hillas_psi_M2 = np.asarray(
                self.superstar["MHillas_2.fDelta"].array())
            hillas_skewness_M1 = np.asarray(
                self.superstar["MHillasExt_1.fM3Long"].array())
            hillas_skewness_M2 = np.asarray(
                self.superstar["MHillasExt_2.fM3Long"].array())

            leakage_intensity_1_M1 = np.asarray(
                self.superstar["MNewImagePar_1.fLeakage1"].array())
            leakage_intensity_1_M2 = np.asarray(
                self.superstar["MNewImagePar_2.fLeakage1"].array())
            leakage_intensity_2_M1 = np.asarray(
                self.superstar["MNewImagePar_1.fLeakage2"].array())
            leakage_intensity_2_M2 = np.asarray(
                self.superstar["MNewImagePar_2.fLeakage2"].array())

            num_islands_M1 = np.asarray(
                self.superstar["MImagePar_1.fNumIslands"].array())
            num_islands_M2 = np.asarray(
                self.superstar["MImagePar_2.fNumIslands"].array())
            x_max = np.asarray(self.superstar["MStereoPar.fXMax"].array())

            # Reading data from root file for Image table (charge, peak time and image mask)
            charge_M1 = np.asarray(self.superstar["UprootImageOrig_1"].array())
            for i, charge in enumerate(charge_M1):
                charge_M1[i] = np.array(charge)
            peak_time_M1 = np.asarray(
                self.superstar["MArrivalTime_1.fData"].array())
            image_mask_M1 = np.asarray(
                self.superstar["UprootImageOrigClean_1"].array())
            for i, mask in enumerate(image_mask_M1):
                image_mask_M1[i] = np.array(mask) != 0

            charge_M2 = np.asarray(self.superstar["UprootImageOrig_2"].array())
            for i, charge in enumerate(charge_M2):
                charge_M2[i] = np.array(charge)

            charge_M2 = np.asarray(
                self.superstar["MCerPhotEvt_2.fPixels.fPhot"].array())
            peak_time_M2 = np.asarray(
                self.superstar["MArrivalTime_2.fData"].array())
            image_mask_M2 = np.asarray(
                self.superstar["UprootImageOrigClean_2"].array())
            for i, mask in enumerate(image_mask_M2):
                image_mask_M2[i] = np.array(mask) != 0

        # Iterating over all events, and saving only stereo ones
        total_events = min(len(charge_M1), len(charge_M2))
        tels_with_data = {1, 2}
        for i in range(0, total_events):
            if eventid_M1[i] != 0:
                obs_id = self.run_number
                event_id = eventid_M1[i]
                i2 = np.where(eventid_M2 == eventid_M1[i])
                i2 = i2[0].astype(int)
                data.count = counter

                # Setting up the Data container
                data.index.obs_id = obs_id
                data.index.event_id = event_id
                data.r0.tel.clear()
                data.r1.tel.clear()
                data.dl0.tel.clear()

                # Adding the array pointing in the pointing container
                data.pointing.array_altitude = u.Quantity(
                    np.deg2rad(90.0 - pointing_altitude[i]), u.rad)
                data.pointing.array_azimuth = u.Quantity(
                    np.deg2rad(pointing_azimuth[i]), u.rad)

                # Filling the DL1 container with the event data
                for tel_id in tels_with_data:
                    # Adding telescope pointing container
                    data.pointing.tel[tel_id].azimuth = u.Quantity(
                        np.deg2rad(pointing_azimuth[i]), u.rad)
                    data.pointing.tel[tel_id].altitude = u.Quantity(
                        np.deg2rad(90.0 - pointing_altitude[i]), u.rad)

                    # Adding MC data
                    if self.is_simulation:
                        data.mc.alt = Angle(
                            np.deg2rad(src_pos_cam_Y[i] * 0.00337), u.rad)
                        data.mc.az = Angle(
                            np.deg2rad(src_pos_cam_X[i] * 0.00337), u.rad)
                        data.mc.x_max = u.Quantity(x_max[i], X_MAX_UNIT)
                        data.mc.h_first_int = u.Quantity(h_first_int[i], u.m)
                        data.mc.core_x = u.Quantity(core_x[i], u.m)
                        data.mc.core_y = u.Quantity(core_y[i], u.m)
                        data.mc.energy = u.Quantity(mc_energy[i], u.TeV)
                        data.mc.shower_primary_id = shower_primary_id

                    if self.superstar is not None:
                        leakage_values = LeakageContainer()
                        hillas_parameters_values = HillasParametersContainer()
                        concentration_values = ConcentrationContainer()
                        timing_values = TimingParametersContainer()
                        morphology_values = MorphologyContainer()

                    # Adding charge, peak time and parameters
                    if tel_id == 1:
                        data.dl1.tel[tel_id].image = charge_M1[i][:1039]
                        data.dl1.tel[tel_id].peak_time = peak_time_M1[i][:1039]
                        data.dl1.tel[tel_id].image_mask = image_mask_M1[
                            i][:1039]

                        if self.superstar is not None:
                            hillas_parameters_values[
                                "intensity"] = hillas_intensity_M1[i]
                            hillas_parameters_values["x"] = u.Quantity(
                                hillas_x_M1[i], unit=u.mm)
                            hillas_parameters_values["y"] = u.Quantity(
                                hillas_y_M1[i], unit=u.mm)
                            hillas_parameters_values["r"] = u.Quantity(
                                hillas_r_M1[i], unit=u.mm)
                            hillas_parameters_values["phi"] = u.Quantity(
                                hillas_phi_M1[i], unit=u.rad)
                            hillas_parameters_values["length"] = u.Quantity(
                                hillas_length_M1[i], unit=u.mm)
                            hillas_parameters_values["width"] = u.Quantity(
                                hillas_width_M1[i], unit=u.mm)
                            hillas_parameters_values["psi"] = u.Quantity(
                                hillas_psi_M1[i], unit=u.rad)
                            hillas_parameters_values[
                                "skewness"] = hillas_skewness_M1[i]

                            leakage_values[
                                "intensity_width_1"] = leakage_intensity_1_M1[
                                    i]
                            leakage_values[
                                "intensity_width_2"] = leakage_intensity_2_M1[
                                    i]

                            morphology_values["num_islands"] = num_islands_M1[
                                i]

                    else:
                        data.dl1.tel[tel_id].image = charge_M2[i][:1039]
                        data.dl1.tel[tel_id].peak_time = peak_time_M2[i][:1039]
                        data.dl1.tel[tel_id].image_mask = image_mask_M2[
                            i][:1039]

                        if self.superstar is not None:
                            hillas_parameters_values[
                                "intensity"] = hillas_intensity_M2[i]
                            hillas_parameters_values["x"] = u.Quantity(
                                hillas_x_M2[i], unit=u.mm)
                            hillas_parameters_values["y"] = u.Quantity(
                                hillas_y_M2[i], unit=u.mm)
                            hillas_parameters_values["r"] = u.Quantity(
                                hillas_r_M2[i], unit=u.mm)
                            hillas_parameters_values["phi"] = u.Quantity(
                                hillas_phi_M2[i], unit=u.rad)
                            hillas_parameters_values["length"] = u.Quantity(
                                hillas_length_M2[i], unit=u.mm)
                            hillas_parameters_values["width"] = u.Quantity(
                                hillas_width_M2[i], unit=u.mm)
                            hillas_parameters_values["psi"] = u.Quantity(
                                hillas_psi_M2[i], unit=u.rad)
                            hillas_parameters_values[
                                "skewness"] = hillas_skewness_M2[i]

                            leakage_values[
                                "intensity_width_1"] = leakage_intensity_1_M2[
                                    i]
                            leakage_values[
                                "intensity_width_2"] = leakage_intensity_2_M2[
                                    i]

                            morphology_values["num_islands"] = num_islands_M2[
                                i]

                    if self.superstar is not None:
                        data.dl1.tel[
                            tel_id].parameters.leakage = leakage_values
                        data.dl1.tel[
                            tel_id].parameters.hillas = hillas_parameters_values
                        data.dl1.tel[
                            tel_id].parameters.concentration = concentration_values
                        data.dl1.tel[tel_id].parameters.timing = timing_values
                        data.dl1.tel[
                            tel_id].parameters.morphology = morphology_values

                # 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

                yield data
                counter += 1
        return
def fact_event_generator(inputfile,
                         drsfile,
                         auxpath='/fact/aux',
                         allowed_triggers=None):
    fact_fits_calib = FactFitsCalib(inputfile, drsfile)
    auxservice = AUXService(auxpath)

    header = fact_fits_calib.data_file.header()

    for event in fact_fits_calib:
        trigger_type = event['TriggerType']

        if allowed_triggers is not None:
            if trigger_type not in allowed_triggers:
                continue

        event_id = event['EventNum']

        night = header['NIGHT']
        run_id = header['RUNID']

        data = DataContainer()
        data.meta['origin'] = 'FACT'

        if 'UnixTimeUTC' in event:
            mc = False
            data.trig.gps_time = Time(
                event['UnixTimeUTC'][0] + event['UnixTimeUTC'][1] * 1e-6,
                scale='utc',
                format='unix',
            )
        else:
            mc = True
            data.trig.gps_time = Time.now()

        if mc:
            event['CalibData'] = reorder_softid2chid(event['CalibData'])
            event['McCherPhotWeight'] = reorder_softid2chid(
                event['McCherPhotWeight'])
            add_mc_data(event, data)
        else:
            add_aux_data(auxservice, data)

        data.trig.tels_with_trigger = [0]
        data.inst = FACT

        for c in (data.r0, data.r1, data.dl0):
            c.event_id = event_id
            c.run_id = '{}_{:03d}'.format(night, run_id)
            c.tels_with_data = [0]

        event['CalibData'][:, 150:300] = 0
        event['CalibData'][:, :10] = 0

        data.r0.tel[0].adc_samples = event['Data'].reshape(1, 1440, -1)
        data.r0.tel[0].num_samples = event['Data'].shape[1]
        data.r1.tel[0].pe_samples = event['CalibData'].reshape(1, 1440,
                                                               -1) / 242
        data.dl0.tel[0].pe_samples = event['CalibData'].reshape(1, 1440,
                                                                -1) / 242

        yield data
Example #9
0
    def _generator(self):
        """
        Stereo event generator. Yields DataContainer instances, filled
        with the read event data.
        
        Returns
        -------
        
        """
        counter = 0
        data = DataContainer()
        data.meta['origin'] = "MAGIC"
        data.meta['input_url'] = self.input_url
        data.meta['is_simulation'] = True
        data.mcheader = self._mc_header
        #Reading data from root file for Events table
        eventid_M1 = np.asarray(
            self.event_M1["MRawEvtHeader.fStereoEvtNumber"].array())
        eventid_M2 = np.asarray(
            self.event_M2["MRawEvtHeader.fStereoEvtNumber"].array())

        src_pos_cam_Y = np.asarray(self.event_M1["MSrcPosCam.fY"].array())

        pointing_altitude = np.asarray(
            self.event_M1["MPointingPos.fZd"].array())

        src_pos_cam_X = np.asarray(self.event_M1["MSrcPosCam.fX"].array())

        pointing_azimuth = np.asarray(
            self.event_M1["MPointingPos.fAz"].array())

        core_x = np.asarray(self.event_M1["MMcEvt.fCoreX"].array())
        core_y = np.asarray(self.event_M1["MMcEvt.fCoreY"].array())

        mc_energy = np.asarray(
            self.event_M1["MMcEvt.fEnergy"].array()) / 1000.0
        h_first_int = np.asarray(
            self.event_M1["MMcEvt.fZFirstInteraction"].array())

        mask = r".([A-Z]+)_M\d_za\d+to\d+_\d_\d+_Y_.*"
        primary_id = re.findall(mask, self.file_list[0])[0]
        if primary_id == 'GA':
            shower_primary_id = 1

        stereo_total = np.max(eventid_M1)
        #Reading data from root file for Image table

        charge_M1 = self.event_M1["MCerPhotEvt.fPixels.fPhot"].array()
        peak_time_M1 = self.event_M1["MArrivalTime.fData"].array()
        charge_M1 = np.asarray(charge_M1)
        peak_time_M1 = np.asarray(peak_time_M1)

        charge_M2 = self.event_M2["MCerPhotEvt.fPixels.fPhot"].array()
        peak_time_M2 = self.event_M2["MArrivalTime.fData"].array()
        charge_M2 = np.asarray(charge_M2)
        peak_time_M2 = np.asarray(peak_time_M2)

        total_events = min(
            len(self.event_M1["MCerPhotEvt.fPixels.fPhot"].array()),
            len(self.event_M2["MCerPhotEvt.fPixels.fPhot"].array()))
        #Iterating over all events, and saving only stereo ones
        tels_in_file = ["m1", "m2"]
        tels_with_data = {1, 2}
        for i in range(0, total_events):
            if eventid_M1[i] != 0:
                obs_id = self.run_number
                event_id = eventid_M1[i]
                i2 = np.where(eventid_M2 == eventid_M1[i])
                i2 = i2[0].astype(int)
                data.count = counter

                # Setting up the Data container
                data.index.obs_id = obs_id
                data.index.event_id = event_id
                data.r0.tel.clear()
                data.r1.tel.clear()
                data.dl0.tel.clear()

                # Filling the DL1 container with the event data
                for tel_i, tel_id in enumerate(tels_in_file):

                    #Adding telescope pointing container

                    data.pointing.tel[tel_i + 1].azimuth = u.Quantity(
                        np.deg2rad(pointing_azimuth[i]), u.rad)
                    data.pointing.tel[tel_i + 1].altitude = u.Quantity(
                        np.deg2rad(90.0 - pointing_altitude[i]), u.rad)

                    #Adding MC data
                    #The src_pos_cam_X/src_pos_cam_Y values are stored as alt/az to follow the generic data format.
                    data.mc.alt = Angle(np.deg2rad(src_pos_cam_Y[i] * 0.00337),
                                        u.rad)
                    data.mc.az = Angle(np.deg2rad(src_pos_cam_X[i] * 0.00337),
                                       u.rad)
                    data.mc.x_max = u.Quantity(0, X_MAX_UNIT)
                    data.mc.h_first_int = u.Quantity(h_first_int[i], u.m)
                    data.mc.core_x = u.Quantity(core_x[i], u.m)
                    data.mc.core_y = u.Quantity(core_y[i], u.m)
                    data.mc.energy = u.Quantity(mc_energy[i], u.TeV)
                    data.mc.shower_primary_id = shower_primary_id
                    # Adding event charge and peak positions per pixel
                    if tel_i == 0:
                        data.dl1.tel[tel_i + 1].image = charge_M1[i][:1039]
                        data.dl1.tel[tel_i +
                                     1].peak_time = peak_time_M1[i][:1039]
                    else:
                        data.dl1.tel[tel_i + 1].image = charge_M2[i][:1039]
                        data.dl1.tel[tel_i +
                                     1].peak_time = peak_time_M2[i][:1039]

                # 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

                yield data
                counter += 1
        return
Example #10
0
 def _generator(self):
     for i in range(self.header["NAXIS2"]):
         event_data = self.events[i]
         event = DataContainer(count=i)
         yield self._fill_container(container=event, data=event_data)
Example #11
0
    def _generator(self):
        with self.pyhessio.open_hessio(self.input_url) as file:
            # the container is initialized once, and data is replaced within
            # it after each yield
            counter = 0
            eventstream = file.move_to_next_event()
            data = DataContainer()
            data.meta['origin'] = "hessio"

            # some hessio_event_source specific parameters
            data.meta['input_url'] = self.input_url
            data.meta['max_events'] = self.max_events

            for event_id in eventstream:

                if counter == 0:
                    # subarray info is only available when an event is loaded,
                    # so load it on the first event.
                    data.inst.subarray = self._build_subarray_info(file)

                obs_id = file.get_run_number()
                tels_with_data = set(file.get_teldata_list())
                data.count = counter
                data.r0.obs_id = obs_id
                data.r0.event_id = event_id
                data.r0.tels_with_data = tels_with_data
                data.r1.obs_id = obs_id
                data.r1.event_id = event_id
                data.r1.tels_with_data = tels_with_data
                data.dl0.obs_id = obs_id
                data.dl0.event_id = event_id
                data.dl0.tels_with_data = tels_with_data

                # handle telescope filtering by taking the intersection of
                # tels_with_data and allowed_tels
                if len(self.allowed_tels) > 0:
                    selected = tels_with_data & self.allowed_tels
                    if len(selected) == 0:
                        continue  # skip event
                    data.r0.tels_with_data = selected
                    data.r1.tels_with_data = selected
                    data.dl0.tels_with_data = selected

                data.trig.tels_with_trigger = (
                    file.get_central_event_teltrg_list())
                time_s, time_ns = file.get_central_event_gps_time()
                data.trig.gps_time = Time(time_s * u.s,
                                          time_ns * u.ns,
                                          format='unix',
                                          scale='utc')
                data.mc.energy = file.get_mc_shower_energy() * u.TeV
                data.mc.alt = Angle(file.get_mc_shower_altitude(), u.rad)
                data.mc.az = Angle(file.get_mc_shower_azimuth(), u.rad)
                data.mc.core_x = file.get_mc_event_xcore() * u.m
                data.mc.core_y = file.get_mc_event_ycore() * u.m
                first_int = file.get_mc_shower_h_first_int() * u.m
                data.mc.h_first_int = first_int
                data.mc.x_max = file.get_mc_shower_xmax() * u.g / (u.cm**2)
                data.mc.shower_primary_id = file.get_mc_shower_primary_id()

                # mc run header data
                data.mcheader.run_array_direction = Angle(
                    file.get_mc_run_array_direction() * u.rad)

                # this should be done in a nicer way to not re-allocate the
                # data each time (right now it's just deleted and garbage
                # collected)

                data.r0.tel.clear()
                data.r1.tel.clear()
                data.dl0.tel.clear()
                data.dl1.tel.clear()
                data.mc.tel.clear()  # clear the previous telescopes

                for tel_id in tels_with_data:

                    mc = data.mc.tel[tel_id]
                    r0 = data.r0.tel[tel_id]
                    r1 = data.r1.tel[tel_id]
                    pointing = data.pointing[tel_id]

                    adc_samples = file.get_adc_sample(tel_id)
                    if adc_samples.size == 0:
                        adc_samples = file.get_adc_sum(tel_id)[..., None]
                    dc_to_pe = file.get_calibration(tel_id)
                    pedestal = file.get_pedestal(tel_id)
                    r0.waveform = adc_samples
                    r1.waveform, r1.selected_gain_channel = apply_simtel_r1_calibration(
                        adc_samples, pedestal, dc_to_pe, self.gain_selector)

                    mc.dc_to_pe = dc_to_pe
                    mc.pedestal = pedestal
                    r0.num_trig_pix = file.get_num_trig_pixels(tel_id)
                    r0.trig_pix_id = file.get_trig_pixels(tel_id)

                    # load the data per telescope/pixel
                    hessio_mc_npe = file.get_mc_number_photon_electron(tel_id)
                    mc.photo_electron_image = hessio_mc_npe
                    mc.azimuth_raw = file.get_azimuth_raw(tel_id)
                    mc.altitude_raw = file.get_altitude_raw(tel_id)
                    azimuth_cor = file.get_azimuth_cor(tel_id)
                    altitude_cor = file.get_altitude_cor(tel_id)

                    # hessioeventsource pass 0 if there is no altitude/azimuth correction
                    if azimuth_cor == 0 and mc.azimuth_raw != 0:
                        mc.azimuth_cor = np.nan
                        pointing.azimuth = u.Quantity(mc.azimuth_raw, u.rad)
                    else:
                        mc.azimuth_cor = azimuth_cor
                        pointing.azimuth = u.Quantity(azimuth_cor, u.rad)

                    if altitude_cor == 0 and mc.altitude_raw != 0:
                        mc.altitude_cor = np.nan
                        pointing.altitude = u.Quantity(mc.altitude_raw, u.rad)
                    else:
                        mc.altitude_cor = altitude_cor
                        pointing.altitude = u.Quantity(mc.altitude_cor, u.rad)

                yield data
                counter += 1

        return