-
Notifications
You must be signed in to change notification settings - Fork 243
feat: Python bindings for SimHit/SimHitContainer + UprootReader #5330
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
Merged
kodiakhq
merged 13 commits into
acts-project:main
from
benjaminhuth:feature/bind-particles-hits-python-uproot-reader
Apr 14, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
19d4110
feat: Python bindings for SimHit/SimHitContainer + pure-Python uproot…
benjaminhuth 8003c77
refactor: SimParticle simple setters as properties; optional file pat…
benjaminhuth 66d383c
Merge remote-tracking branch 'upstream/main' into feature/bind-partic…
benjaminhuth b472b2d
update
benjaminhuth 231c70a
update
benjaminhuth 18c2d4b
lint
benjaminhuth a802643
update
benjaminhuth 71dd626
update
benjaminhuth 47cb604
update
benjaminhuth 491d3cb
rename stuff
benjaminhuth f748dee
lint
benjaminhuth a58778f
Apply suggestions from code review
benjaminhuth b6f0fa3
Merge branch 'main' into feature/bind-particles-hits-python-uproot-re…
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| # This file is part of the ACTS project. | ||
| # | ||
| # Copyright (C) 2016 CERN for the benefit of the ACTS project | ||
| # | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| """Uproot-based readers for simulated particles and hits. | ||
|
|
||
| Reads the ROOT files written by RootParticleWriter and RootSimHitWriter | ||
| using uproot, without requiring the ROOT plugin. Suitable for use in the | ||
| PyPI distribution. | ||
| """ | ||
|
|
||
| from pathlib import Path | ||
| from typing import Union | ||
|
|
||
| import numpy as np | ||
| import uproot | ||
|
|
||
| import acts | ||
| import acts.examples | ||
|
|
||
|
|
||
| class UprootParticleReader(acts.examples.IReader): | ||
| """Reads simulated particles from a ROOT file using uproot. | ||
|
|
||
| Reads the file format produced by RootParticleWriter (one entry per event, | ||
| vector-valued branches). All data is loaded upfront for thread-safe | ||
| concurrent access from the sequencer. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| filePath: Union[Path, str], | ||
| outputParticles: str = "particles", | ||
| level: acts.logging.Level = acts.logging.INFO, | ||
| ): | ||
| acts.examples.IReader.__init__(self, "UprootParticleReader", level) | ||
|
|
||
| self._outputParticles = outputParticles | ||
|
|
||
| self._particleHandle = acts.examples.WriteDataHandle( | ||
| self, acts.examples.SimParticleContainer, "OutputParticles" | ||
| ) | ||
|
|
||
| with uproot.open(str(filePath)) as f: | ||
| tree = f["particles"] | ||
| self._data = tree.arrays(library="np") | ||
| event_ids = self._data["event_id"] | ||
| self._entry_map = {int(eid): i for i, eid in enumerate(event_ids)} | ||
|
|
||
| self._min_event = min(self._entry_map.keys()) | ||
| self._max_event = max(self._entry_map.keys()) | ||
|
|
||
| def availableEvents(self): | ||
| return (self._min_event, self._max_event + 1) | ||
|
|
||
| def initialize(self): | ||
| self._particleHandle.initialize(self._outputParticles) | ||
| return acts.examples.ProcessCode.SUCCESS | ||
|
|
||
| def read(self, context): | ||
| event_number = context.eventNumber | ||
| particles = acts.examples.SimParticleContainer() | ||
|
|
||
| if event_number in self._entry_map: | ||
|
benjaminhuth marked this conversation as resolved.
|
||
| idx = self._entry_map[event_number] | ||
| d = self._data | ||
| n = len(d["particle_type"][idx]) | ||
| u = acts.UnitConstants | ||
| for i in range(n): | ||
| barcode = acts.examples.SimBarcode() | ||
| barcode.vertexPrimary = int(d["vertex_primary"][idx][i]) | ||
| barcode.vertexSecondary = int(d["vertex_secondary"][idx][i]) | ||
| barcode.particle = int(d["particle"][idx][i]) | ||
| barcode.generation = int(d["generation"][idx][i]) | ||
| barcode.subParticle = int(d["sub_particle"][idx][i]) | ||
|
|
||
| p = acts.examples.SimParticle( | ||
| barcode, | ||
| acts.PdgParticle(int(d["particle_type"][idx][i])), | ||
| float(d["q"][idx][i]) * u.e, | ||
| float(d["m"][idx][i]) * u.GeV, | ||
| ) | ||
| p.process = acts.examples.GenerationProcess(int(d["process"][idx][i])) | ||
|
|
||
| p.fourPosition = acts.Vector4( | ||
| *[float(d[k][idx][i]) * u.mm for k in ("vx", "vy", "vz", "vt")] | ||
| ) | ||
| p.direction = acts.Vector3( | ||
| *[float(d[k][idx][i]) for k in ("px", "py", "pz")] | ||
| ) | ||
| p.absoluteMomentum = float(d["p"][idx][i]) * u.GeV | ||
|
|
||
| p.setFinalMaterialPassed( | ||
| float(d["total_x0"][idx][i]) * u.mm, | ||
| float(d["total_l0"][idx][i]) * u.mm, | ||
| ) | ||
| p.numberOfHits = int(d["number_of_hits"][idx][i]) | ||
| p.outcome = acts.examples.SimulationOutcome(int(d["outcome"][idx][i])) | ||
|
|
||
| particles.insert(p) | ||
|
|
||
| self._particleHandle(context, particles) | ||
| return acts.examples.ProcessCode.SUCCESS | ||
|
|
||
|
|
||
| class UprootSimHitReader(acts.examples.IReader): | ||
| """Reads simulated hits from a ROOT file using uproot. | ||
|
|
||
| Reads the file format produced by RootSimHitWriter (one row per hit, | ||
| scalar branches grouped by event_id). All data is loaded upfront for | ||
| thread-safe concurrent access from the sequencer. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| filePath: Union[Path, str], | ||
| outputSimHits: str = "simhits", | ||
| level: acts.logging.Level = acts.logging.INFO, | ||
| ): | ||
| acts.examples.IReader.__init__(self, "UprootSimHitReader", level) | ||
|
|
||
| self._outputSimHits = outputSimHits | ||
|
|
||
| self._hitHandle = acts.examples.WriteDataHandle( | ||
| self, acts.examples.SimHitContainer, "OutputSimHits" | ||
| ) | ||
|
|
||
| with uproot.open(str(filePath)) as f: | ||
| tree = f["hits"] | ||
| self._data = tree.arrays(library="np") | ||
| self._event_range_map = self._build_event_range_map(self._data["event_id"]) | ||
|
|
||
| all_ids = set(self._event_range_map.keys()) | ||
| self._min_event = int(min(all_ids)) | ||
| self._max_event = int(max(all_ids)) | ||
|
|
||
| @staticmethod | ||
| def _build_event_range_map(event_ids: np.ndarray) -> dict: | ||
| """Build a {event_id: (start, end)} map from a sorted event_id array.""" | ||
| if len(event_ids) == 0: | ||
| return {} | ||
| unique_ids, starts = np.unique(event_ids, return_index=True) | ||
| ends = np.append(starts[1:], len(event_ids)) | ||
| return { | ||
| int(uid): (int(s), int(e)) for uid, s, e in zip(unique_ids, starts, ends) | ||
| } | ||
|
|
||
| def availableEvents(self): | ||
| return (self._min_event, self._max_event + 1) | ||
|
|
||
| def initialize(self): | ||
| self._hitHandle.initialize(self._outputSimHits) | ||
| return acts.examples.ProcessCode.SUCCESS | ||
|
|
||
| def read(self, context): | ||
| event_number = context.eventNumber | ||
| hits = acts.examples.SimHitContainer() | ||
|
|
||
| if event_number in self._event_range_map: | ||
| start, end = self._event_range_map[event_number] | ||
| d = self._data | ||
| u = acts.UnitConstants | ||
| for i in range(start, end): | ||
| geoid = acts.GeometryIdentifier(int(d["geometry_id"][i])) | ||
|
|
||
| barcode = acts.examples.SimBarcode() | ||
| barcode.vertexPrimary = int(d["barcode_vertex_primary"][i]) | ||
| barcode.vertexSecondary = int(d["barcode_vertex_secondary"][i]) | ||
| barcode.particle = int(d["barcode_particle"][i]) | ||
| barcode.generation = int(d["barcode_generation"][i]) | ||
| barcode.subParticle = int(d["barcode_sub_particle"][i]) | ||
|
|
||
| pos4 = acts.Vector4( | ||
| *[float(d[k][i]) * u.mm for k in ("tx", "ty", "tz", "tt")] | ||
| ) | ||
| before4 = acts.Vector4( | ||
| *[float(d[k][i]) * u.GeV for k in ("tpx", "tpy", "tpz", "te")] | ||
| ) | ||
| after4 = acts.Vector4( | ||
| (float(d["tpx"][i]) + float(d["deltapx"][i])) * u.GeV, | ||
| (float(d["tpy"][i]) + float(d["deltapy"][i])) * u.GeV, | ||
| (float(d["tpz"][i]) + float(d["deltapz"][i])) * u.GeV, | ||
| (float(d["te"][i]) + float(d["deltae"][i])) * u.GeV, | ||
| ) | ||
|
|
||
| hit = acts.examples.SimHit( | ||
| geoid, barcode, pos4, before4, after4, int(d["index"][i]) | ||
| ) | ||
| hits.insert(hit) | ||
|
|
||
| self._hitHandle(context, hits) | ||
| return acts.examples.ProcessCode.SUCCESS | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.