Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added longitudinal fit parameters to 7.xx event end fields #37

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion corsikaio/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def __next__(self):
raise IOError("File seems to be truncated")

if self.parse_blocks:
event_end = parse_event_end(block)[0]
event_end = parse_event_end(block,self.version)[0]
data = self.parse_data_blocks(data_bytes)
longitudinal = parse_longitudinal(long_bytes)
else:
Expand Down
6 changes: 3 additions & 3 deletions corsikaio/subblocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .run_end import run_end_dtype

from .event_header import event_header_types
from .event_end import event_end_dtype
from .event_end import event_end_types

from .data import cherenkov_photons_dtype, particle_data_dtype
from .longitudinal import longitudinal_data_dtype
Expand Down Expand Up @@ -38,8 +38,8 @@ def parse_event_header(event_header_bytes):
return np.frombuffer(event_header_bytes, dtype=event_header_types[version])


def parse_event_end(event_end_bytes):
return np.frombuffer(event_end_bytes, dtype=event_end_dtype)
def parse_event_end(event_end_bytes,version):
return np.frombuffer(event_end_bytes, dtype=event_end_types[float(str(version)[:3])])


def get_version(header_bytes, version_pos):
Expand Down
48 changes: 45 additions & 3 deletions corsikaio/subblocks/event_end.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from .dtypes import build_dtype, Field
import warnings
from collections import defaultdict

from .dtypes import build_dtype, Field

event_end_fields = [
event_end_fields_65 = [
Field(1, "event_end", dtype="S4"),
Field(2, "event_number"),
Field(3, "n_photons_weighted"),
Expand All @@ -17,4 +19,44 @@
Field(267, "n_em_particles_preshower"),
]

event_end_dtype = build_dtype(event_end_fields)
event_end_fields_7x = [
Field(1, "event_end", dtype="S4"),
Field(2, "event_number"),
Field(3, "n_photons_weighted"),
Field(4, "n_electrons_weighted"),
Field(5, "n_hadrons_weighted"),
Field(6, "n_muons_weighted"),
Field(7, "n_particles_written"),
Field(256, "longitudinal_fit_parameters", shape=6),
Field(262, "chi_square_longitudinal"),
Field(263, "n_photons_written"),
Field(264, "n_electrons_written"),
Field(265, "n_hadrons_written"),
Field(266, "n_muons_written"),
Field(267, "n_em_particles_preshower"),
]

event_end_dtype_65 = build_dtype(event_end_fields_65)
event_end_dtype_7x = build_dtype(event_end_fields_7x)

def warn_dtype():
warnings.warn("Version unknown, using default event end definition dtype of version 7.x")
return event_end_dtype_7x

Check warning on line 44 in corsikaio/subblocks/event_end.py

View check run for this annotation

Codecov / codecov/patch

corsikaio/subblocks/event_end.py#L43-L44

Added lines #L43 - L44 were not covered by tests

def warn_fields():
warnings.warn("Version unknown, using default event end fields definition of version 7.x")
return event_end_fields_7x

Check warning on line 48 in corsikaio/subblocks/event_end.py

View check run for this annotation

Codecov / codecov/patch

corsikaio/subblocks/event_end.py#L47-L48

Added lines #L47 - L48 were not covered by tests

event_end_fields = defaultdict(warn_fields)
maxnoe marked this conversation as resolved.
Show resolved Hide resolved
event_end_fields[6.5] = event_end_fields_65
event_end_fields[7.4] = event_end_fields_7x
event_end_fields[7.5] = event_end_fields_7x
event_end_fields[7.6] = event_end_fields_7x
event_end_fields[7.7] = event_end_fields_7x

event_end_types = defaultdict(warn_dtype)
event_end_types[6.5] = event_end_dtype_65
event_end_types[7.4] = event_end_dtype_7x
event_end_types[7.5] = event_end_dtype_7x
event_end_types[7.6] = event_end_dtype_7x
event_end_types[7.7] = event_end_dtype_7x
Binary file added tests/resources/corsika_77500_particle
Binary file not shown.
16 changes: 16 additions & 0 deletions tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,19 @@ def test_truncated(tmp_path, size):
with CorsikaParticleFile(path) as f:
for _ in f:
pass


def test_longitudinal_parameters():
'''Test event end blocks contain longitudinal parameters'''
from corsikaio import CorsikaParticleFile

path = "tests/resources/corsika_77500_particle"

with CorsikaParticleFile(path) as f:
n_events = 0
for event in f:
n_events += 1
assert "longitudinal_fit_parameters" in event.end.dtype.names
parameters = event.end["longitudinal_fit_parameters"]
np.testing.assert_array_equal(parameters != 0, True)
assert n_events == 5
6 changes: 4 additions & 2 deletions tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ def test_new_field():


def test_event_end_units():

assert all([f.unit is None for f in event_end_fields])

for version in event_end_fields:

assert all([f.unit is None for f in event_end_fields[version]])


def test_run_end_units():
Expand Down
Loading