Skip to content

Instantly share code, notes, and snippets.

@jbernhard
Last active August 29, 2015 14:05
Show Gist options
  • Save jbernhard/58d213e67bd73d21631d to your computer and use it in GitHub Desktop.
Save jbernhard/58d213e67bd73d21631d to your computer and use it in GitHub Desktop.
def event_generator(phi_column_number, pT_column_number):
"""
Lazily generate arrays of phi angles and pT for each event.
Fastest way I can find to read a numerical text file in chunks,
where the chunks are of non-uniform size and separated by markers in the text file.
"""
event = []
for l in sys.stdin: # or other file object
s = l.split()
if not s or s[0].startswith('#'): # blank lines and comments separate events
if event:
yield np.array(event, dtype=np.float64)
event.clear()
else:
event.append(s[phi_column_number], s[pT_column_number])
if event:
yield np.array(event, dtype=np.float64)
# example usage
for event in event_generator():
phi, pT = event.T
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment