Skip to content

Commit

Permalink
Add useful warning message for empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
kjmeagher committed Apr 17, 2024
1 parent 35cf7e7 commit e28f605
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/simweights/_corsika_weighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 6 additions & 2 deletions src/simweights/_nugen_weighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_corsika_weighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_nugen_weighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit e28f605

Please sign in to comment.