From d09435bfd111480659e64859c96809a8a36f7067 Mon Sep 17 00:00:00 2001 From: Francois Drielsma Date: Sun, 17 Nov 2024 23:12:56 -0800 Subject: [PATCH] Make flash parser LArCV version agnostic --- spine/io/parse/cluster.py | 2 +- spine/io/parse/misc.py | 29 ++++++++++++++-------------- spine/io/parse/sparse.py | 16 +++++++-------- spine/post/optical/flash_matching.py | 2 +- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/spine/io/parse/cluster.py b/spine/io/parse/cluster.py index 7ede5413..a5546143 100644 --- a/spine/io/parse/cluster.py +++ b/spine/io/parse/cluster.py @@ -191,7 +191,7 @@ def __init__(self, dtype, particle_event=None, add_particle_info=False, if self.add_particle_info: assert particle_event is not None, ( "If `add_particle_info` is `True`, must provide the " - "`particle_event` argument") + "`particle_event` argument.") def __call__(self, trees): """Parse one entry. diff --git a/spine/io/parse/misc.py b/spine/io/parse/misc.py index e92e50ce..39a5e642 100644 --- a/spine/io/parse/misc.py +++ b/spine/io/parse/misc.py @@ -87,7 +87,7 @@ def process(self, sparse_event=None, cluster_event=None): """ # Check on the input, pick a source for the metadata assert (sparse_event is not None) ^ (cluster_event is not None), ( - "Must specify either `sparse_event` or `cluster_event`") + "Must specify either `sparse_event` or `cluster_event`.") ref_event = sparse_event if sparse_event is not None else cluster_event # Fetch a specific projection, if needed @@ -141,7 +141,7 @@ def process(self, sparse_event=None, cluster_event=None): """ # Check on the input, pick a source for the run information assert (sparse_event is not None) ^ (cluster_event is not None), ( - "Must specify either `sparse_event` or `cluster_event`") + "Must specify either `sparse_event` or `cluster_event`.") ref_event = sparse_event if sparse_event is not None else cluster_event return RunInfo.from_larcv(ref_event) @@ -195,34 +195,33 @@ def process(self, flash_event=None, flash_event_list=None): List[Flash] List of optical flash objects """ - # Check on the input, aggregate the sources for the optical flashes + # Check on the input assert ((flash_event is not None) ^ (flash_event_list is not None)), ( - "Must specify either `flash_event` or `flash_event_list`") + "Must specify either `flash_event` or `flash_event_list`.") + + # Parse flash objects if flash_event is not None: # If there is a single flash event, parse it as is flash_list = flash_event.as_vector() + flashes = [Flash.from_larcv(larcv.Flash(f)) for f in flash_list] else: # Otherwise, set the volume ID of the flash to the source index # and count the flash index from 0 to the largest number - flash_list = [] + flashes = [] idx = 0 for volume_id, flash_event in enumerate(flash_event_list): - for flash in flash_event.as_vector(): - # Update attributes (TODO: simplify volume_id with update) - flash.id(idx) - for attr in ['tpc', 'volume_id']: - if hasattr(flash, attr): - getattr(flash, attr)(volume_id) + for f in flash_event.as_vector(): + # Cast and update attributes + flash = Flash.from_larcv(f) + flash.id = idx + flash.volume_id = volume_id # Append, increment counter - flash_list.append(flash) + flashes.append(flash) idx += 1 - # Output as a list of LArCV optical flash objects - flashes = [Flash.from_larcv(larcv.Flash(f)) for f in flash_list] - return ObjectList(flashes, Flash()) diff --git a/spine/io/parse/sparse.py b/spine/io/parse/sparse.py index 309d5a6a..e428e203 100644 --- a/spine/io/parse/sparse.py +++ b/spine/io/parse/sparse.py @@ -62,9 +62,9 @@ def __init__(self, dtype, projection_id, sparse_event=None, # Get the number of features in the output tensor assert (sparse_event is not None) ^ (sparse_event_list is not None), ( - "Must provide either `sparse_event` or `sparse_event_list`") + "Must provide either `sparse_event` or `sparse_event_list`.") assert sparse_event_list is None or len(sparse_event_list), ( - "Must provide as least 1 sparse_event in the list") + "Must provide as least 1 sparse_event in the list.") self.num_features = 1 if sparse_event_list is not None: @@ -118,9 +118,9 @@ def process(self, sparse_event=None, sparse_event_list=None): larcv.fill_2d_voxels(tensor, np_voxels) else: assert meta == tensor.meta(), ( - "The metadata must match between tensors") + "The metadata must match between tensors.") assert num_points == tensor.as_vector().size(), ( - "The number of pixels must match between tensors") + "The number of pixels must match between tensors.") # Get the feature vector for this tensor np_data = np.empty((num_points, 1), dtype=self.ftype) @@ -194,9 +194,9 @@ def __init__(self, dtype, sparse_event=None, sparse_event_list=None, # Get the number of features in the output tensor assert (sparse_event is not None) ^ (sparse_event_list is not None), ( - "Must provide either `sparse_event` or `sparse_event_list`") + "Must provide either `sparse_event` or `sparse_event_list`.") assert sparse_event_list is None or len(sparse_event_list), ( - "Must provide as least 1 sparse_event in the list") + "Must provide as least 1 sparse_event in the list.") num_tensors = 1 if sparse_event is not None else len(sparse_event_list) if self.num_features is not None: @@ -260,7 +260,7 @@ def process(self, sparse_event=None, sparse_event_list=None): meta = sparse_event.meta() else: assert meta == sparse_event.meta(), ( - "The metadata must match between tensors") + "The metadata must match between tensors.") if num_points is None: num_points = sparse_event.as_vector().size() @@ -269,7 +269,7 @@ def process(self, sparse_event=None, sparse_event_list=None): larcv.fill_3d_voxels(sparse_event, np_voxels) else: assert num_points == sparse_event.as_vector().size(), ( - "The number of pixels must match between tensors") + "The number of pixels must match between tensors.") # Get the feature vector for this tensor np_data = np.empty((num_points, 1), dtype=self.ftype) diff --git a/spine/post/optical/flash_matching.py b/spine/post/optical/flash_matching.py index 27e789d5..9cc8161f 100644 --- a/spine/post/optical/flash_matching.py +++ b/spine/post/optical/flash_matching.py @@ -188,7 +188,7 @@ def process(self, data): matches = self.matcher.get_matches(interactions_v, flashes_v) # Store flash information - for i, (inter_v, flash, match) in enumerate(matches): + for inter_v, flash, match in matches: # Get the interaction that matches the cropped version inter = interactions[inter_v.id]