Exemplo n.º 1
0
def test_dl3_energy_dependent_cuts():
    temp_cuts = DL3Cuts()

    temp_cuts.gh_max_efficiency = 0.8
    temp_cuts.theta_containment = 0.68

    temp_data = QTable({
        "gh_score":
        u.Quantity(np.tile(np.arange(0.35, 0.85, 0.05), 3)),
        "reco_energy":
        np.geomspace(50 * u.GeV, 50 * u.TeV, 30),
        "theta":
        u.Quantity(np.tile(np.arange(0.05, 0.35, 0.03), 3), unit=u.deg),
        "tel_id":
        u.Quantity(np.repeat([1, 2, 3], 10)),
        "mc_type":
        u.Quantity(np.repeat([0], 30)),
    })
    en_range = u.Quantity([0.01, 0.1, 1, 10, 100, np.inf], unit=u.TeV)

    theta_cut = temp_cuts.energy_dependent_theta_cuts(temp_data,
                                                      en_range,
                                                      min_events=2)

    gh_cut = temp_cuts.energy_dependent_gh_cuts(temp_data,
                                                en_range,
                                                min_events=2)

    data_th = temp_cuts.apply_energy_dependent_theta_cuts(temp_data, theta_cut)
    data_gh = temp_cuts.apply_energy_dependent_gh_cuts(temp_data, gh_cut)

    assert theta_cut["cut"][0] == 0.0908 * u.deg
    assert gh_cut["cut"][1] == 0.3725
    assert len(data_th) == 21
    assert len(data_gh) == 26
Exemplo n.º 2
0
def test_dl3_global_cuts():
    temp_cuts = DL3Cuts()

    temp_cuts.global_gh_cut = 0.7
    temp_cuts.global_theta_cut = 0.2
    temp_cuts.global_alpha_cut = 20
    temp_cuts.allowed_tels = [1, 2]

    temp_data = QTable({
        "gh_score":
        u.Quantity(np.tile(np.arange(0.35, 0.85, 0.05), 3)),
        "reco_energy":
        np.geomspace(50 * u.GeV, 50 * u.TeV, 30),
        "theta":
        u.Quantity(np.tile(np.arange(0.05, 0.35, 0.03), 3), unit=u.deg),
        "alpha":
        u.Quantity(np.tile(np.arange(5, 85, 8), 3), unit=u.deg),
        "tel_id":
        u.Quantity(np.repeat([1, 2, 3], 10)),
        "mc_type":
        u.Quantity(np.repeat([0], 30)),
    })

    assert len(temp_cuts.apply_global_gh_cut(temp_data)) == 6
    assert len(temp_cuts.apply_global_theta_cut(temp_data)) == 15
    assert len(temp_cuts.apply_global_alpha_cut(temp_data)) == 6
    assert len(temp_cuts.allowed_tels_filter(temp_data)) == 20
Exemplo n.º 3
0
    def setup(self):

        if self.output_irf_file.absolute().exists():
            if self.overwrite:
                self.log.warning(f"Overwriting {self.output_irf_file}")
                self.output_irf_file.unlink()
            else:
                raise ToolConfigurationError(
                    f"Output file {self.output_irf_file} already exists,"
                    " use --overwrite to overwrite"
                )

        filename = self.output_irf_file.name
        if not (filename.endswith('.fits') or filename.endswith('.fits.gz')):
            raise ValueError(
                f"{filename} is not a correct compressed FITS file name"
                "(use .fits or .fits.gz)."
                )

        if self.input_proton_dl2 and self.input_electron_dl2 is not Undefined:
            self.only_gamma_irf = False
        else:
            self.only_gamma_irf = True

        self.event_sel = EventSelector(parent=self)
        self.cuts = DL3Cuts(parent=self)
        self.data_bin = DataBinning(parent=self)

        self.mc_particle = {
            "gamma": {
                "file": self.input_gamma_dl2,
                "target_spectrum": CRAB_MAGIC_JHEAP2015,
            },
        }
        Provenance().add_input_file(self.input_gamma_dl2)

        self.t_obs = self.irf_obs_time * u.hour

        # Read and update MC information
        if not self.only_gamma_irf:
            self.mc_particle["proton"] = {
                "file":  self.input_proton_dl2,
                "target_spectrum": IRFDOC_PROTON_SPECTRUM,
            }

            self.mc_particle["electron"] = {
                "file": self.input_electron_dl2,
                "target_spectrum": IRFDOC_ELECTRON_SPECTRUM,
            }

            Provenance().add_input_file(self.input_proton_dl2)
            Provenance().add_input_file(self.input_electron_dl2)

        self.provenance_log = self.output_irf_file.parent / (
                self.name + ".provenance.log"
        )
    def setup(self):

        self.filename_dl3 = dl2_to_dl3_filename(self.input_dl2,
                                                compress=self.gzip)
        self.provenance_log = self.output_dl3_path / (self.name +
                                                      ".provenance.log")

        Provenance().add_input_file(self.input_dl2)

        self.event_sel = EventSelector(parent=self)
        self.cuts = DL3Cuts(parent=self)

        self.output_file = self.output_dl3_path.absolute() / self.filename_dl3
        if self.output_file.exists():
            if self.overwrite:
                self.log.warning(f"Overwriting {self.output_file}")
                self.output_file.unlink()
            else:
                raise ToolConfigurationError(
                    f"Output file {self.output_file} already exists,"
                    " use --overwrite to overwrite")
        if not (self.source_ra or self.source_dec):
            self.source_pos = SkyCoord.from_name(self.source_name)
        elif bool(self.source_ra) != bool(self.source_dec):
            raise ToolConfigurationError(
                "Either provide both RA and DEC values for the source or none")
        else:
            self.source_pos = SkyCoord(ra=self.source_ra, dec=self.source_dec)

        self.log.debug(f"Output DL3 file: {self.output_file}")

        try:
            with fits.open(self.input_irf) as hdul:
                self.use_energy_dependent_gh_cuts = (
                    "GH_CUT" not in hdul["EFFECTIVE AREA"].header)
        except:
            raise ToolConfigurationError(
                f"{self.input_irf} does not have EFFECTIVE AREA HDU, "
                " to check for global cut information in the Header value")

        if self.source_dep:
            with fits.open(self.input_irf) as hdul:
                self.use_energy_dependent_alpha_cuts = (
                    "AL_CUT" not in hdul["EFFECTIVE AREA"].header)