Skip to content
Open
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
29 changes: 18 additions & 11 deletions examples/iter_mc_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
energies = []
corex = []
corey = []
with SimTelFile(args.inputfile) as f:
for i, event in enumerate(f.iter_mc_events()):

# to get all simulated mc events, we set skip_non_triggered to False
with SimTelFile(args.inputfile, skip_non_triggered=False) as f:
for i, event in enumerate(f):
energies.append(event['mc_shower']['energy'])
corex.append(event['mc_event']['xcore'])
corey.append(event['mc_event']['ycore'])
Expand All @@ -31,19 +33,24 @@
51
)

plt.figure()
plt.hist(energies, bins=bins)
plt.xscale('log')
plt.xlabel(r'$E \,/\, \mathrm{TeV}$')
plt.tight_layout()
fig, (ax1, ax2) = plt.subplots(1, 2, layout="constrained")

ax1.hist(energies, bins=bins)
ax1.set(
xscale='log',
yscale='log',
xlabel=r'$E \,/\, \mathrm{TeV}$',
)


fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.hist2d(
ax2.hist2d(
corex, corey,
bins=100
)
ax2.set(
aspect=1,
xlabel=r'$x \,/\, \mathrm{m}$',
ylabel=r'$y \,/\, \mathrm{m}$',
)

fig.tight_layout()
plt.show()
31 changes: 22 additions & 9 deletions src/eventio/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from io import BufferedIOBase
import warnings
import weakref
import struct
import gzip
Expand Down Expand Up @@ -64,7 +66,8 @@ def __init__(self, path, zcat=True):
self.read_process = None
self.zstd = False
self.next = None
self._filehandle = None
self.peek_error = None
self._filehandle: BufferedIOBase | None = None

if not is_eventio(path):
raise ValueError('File {} is not an eventio file'.format(path))
Expand Down Expand Up @@ -94,7 +97,7 @@ def __init__(self, path, zcat=True):

elif is_zstd(path):
log.info('Found zstd compressed file')
self._filehandle = zstd.ZstdDecompressor().stream_reader(open(path, 'rb'))
self._filehandle = zstd.ZstdDecompressor().stream_reader(open(path, 'rb'), read_size=1024**2)
self.zstd = True

else:
Expand All @@ -115,14 +118,16 @@ def __iter__(self):

def __next__(self):
if self.next is not None:
o = self.next
self.next = None
o, self.next = self.next, None
return o

self.seek(self._next_header_pos)
read_sync_marker(self)
return self._read_next_object()

def _read_next_object(self):
self._filehandle.seek(self._next_header_pos)
read_sync_marker(self._filehandle)
header = read_header(
self,
self._filehandle,
toplevel=True,
offset=self._next_header_pos,
)
Expand All @@ -134,8 +139,16 @@ def __next__(self):
)

def peek(self):
if self.next is None:
self.next = next(self)
if self.next is None and self.peek_error is None:
try:
self.next = self._read_next_object()
except StopIteration:
self.next = None
except (EOFError, IOError) as e:
warnings.warn(f"Error in _read_next_object during peek: {e}")
self.next = None
self.peek_error = e

return self.next

def seek(self, position, whence=0):
Expand Down
4 changes: 2 additions & 2 deletions src/eventio/simtel/objects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
''' Implementations of the simtel_array EventIO object types '''
'''Implementations of the simtel_array EventIO object types.'''
import os
import sys
import numpy as np
Expand Down Expand Up @@ -1891,7 +1891,7 @@ class CalibrationPhotoelectrons(EventIOObject):
eventio_type = 2034

def __str__(self):
return f'{self.__class__.__name__}(type={self.header.id})'
return f'{self.__class__.__name__}[{self.header.type}](type={self.header.id})'



Expand Down
Loading
Loading