diff --git a/src/simweights/_corsika_weighter.py b/src/simweights/_corsika_weighter.py index 430619b..bae7999 100644 --- a/src/simweights/_corsika_weighter.py +++ b/src/simweights/_corsika_weighter.py @@ -55,6 +55,10 @@ def sframe_corsika_surface(table: Any) -> GenerationSurface: def weight_map_corsika_surface(table: Any) -> GenerationSurface: """Inspect the `CorsikaWeightMap` table object of a corsika file to generate a surface object.""" pdgids = sorted(np.unique(get_column(table, "ParticleType").astype(int))) + + if len(pdgids) == 0: + msg = "`CorsikaWeightMap` is empty. SimWeights cannot process this file" + raise RuntimeError(msg) surface: int | GenerationSurface = 0 for pdgid in pdgids: mask = pdgid == get_column(table, "ParticleType") diff --git a/src/simweights/_nugen_weighter.py b/src/simweights/_nugen_weighter.py index b82b04d..299b94e 100644 --- a/src/simweights/_nugen_weighter.py +++ b/src/simweights/_nugen_weighter.py @@ -53,10 +53,14 @@ def nugen_spectrum(table: Any) -> PowerLaw: def nugen_surface(table: Any) -> GenerationSurface: """Inspect the `I3MCWeightDict` table object of a nugen file to generate a surface object.""" - spatial = nugen_spatial(table) - spectrum = nugen_spectrum(table) pdgid = get_column(table, "PrimaryNeutrinoType") unique_pdgids = np.unique(pdgid) + if len(unique_pdgids) == 0: + msg = "`I3MCWeightDict` is empty. SimWeights cannot process this file" + raise RuntimeError(msg) + + spatial = nugen_spatial(table) + spectrum = nugen_spectrum(table) surfaces = [] for pid in unique_pdgids: diff --git a/tests/test_corsika_weighter.py b/tests/test_corsika_weighter.py index 39e92c2..f18ff8c 100755 --- a/tests/test_corsika_weighter.py +++ b/tests/test_corsika_weighter.py @@ -102,6 +102,10 @@ def test_old_corsika(self): with self.assertRaises(TypeError): CorsikaWeighter(d, nfiles=object()) + with self.assertRaises(RuntimeError): + x = {"CorsikaWeightMap": {"ParticleType": []}, "PolyplopiaPrimary": {}} + CorsikaWeighter(x, nfiles=1) + def test_sframe_corsika(self): c1 = simweights.NaturalRateCylinder(1200, 600, 0, 1) p1 = simweights.PowerLaw(0, 1e3, 1e4) diff --git a/tests/test_nugen_weighter.py b/tests/test_nugen_weighter.py index 6124e60..524f516 100755 --- a/tests/test_nugen_weighter.py +++ b/tests/test_nugen_weighter.py @@ -109,6 +109,11 @@ def test_nugen_energy_pre_V04_00(self): Ewidth = np.ediff1d(x) np.testing.assert_allclose(y, 2 * weight * flux * Ewidth * c1.etendue / nfiles, 6e-3) + def test_empty(self): + with self.assertRaises(RuntimeError): + x = {"I3MCWeightDict": {"PrimaryNeutrinoType": []}} + NuGenWeighter(x, nfiles=1) + if __name__ == "__main__": unittest.main()