Skip to content

Filtering by PostPosition_X in PhaseSpaceActor #1049

@f-blan

Description

@f-blan

Hello,

Apologies if this issue is known or has a trivial solution that I am not noticing.
I am using opengate version 10.1.0 on python 3.12.5 and Windows 11, but I am having trouble to filter particles in the PhaseSpaceActor by their position. I have a fairly standard case similar to what is present in the 10.1.0 documentation, where I set a simple PhaseSpaceActor with the following filter:

F = GateFilterBuilder()

f = F.PostPosition_X < 0*mm
phsp.filter = f

However, I am encountering the following error once I call opengate.Simulation.run():

Traceback (most recent call last):
  File "<my_script>.py", line 68, in <module>
    main()
  File "<my_script>.py", line 64, in main
    sim.run()
  File "<my_venv>\Lib\site-packages\opengate\managers.py", line 1933, in run
    output = self._run_simulation_engine(False)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<my_venv>\Lib\site-packages\opengate\managers.py", line 1885, in _run_simulation_engine
    output = se.run_engine()
             ^^^^^^^^^^^^^^^
  File "<my_venv>\Lib\site-packages\opengate\engines.py", line 1220, in run_engine
    self.initialize()
  File "<my_venv>\Lib\site-packages\opengate\engines.py", line 1458, in initialize
    self.filter_engine.initialize()
  File "<my_venv>\Lib\site-packages\opengate\engines.py", line 708, in initialize
    f.initialize()
  File "<my_venv>\Lib\site-packages\opengate\actors\filters.py", line 362, in initialize
    super().initialize()
  File "<my_venv>\Lib\site-packages\opengate\actors\filters.py", line 351, in initialize
    super().initialize()
  File "<my_venv>\Lib\site-packages\opengate\actors\filters.py", line 50, in initialize
    self.InitializeUserInfo(self.user_info)
RuntimeError: OPENGATE-CORE Error the attribute named 'PostPosition_X' does not exists. Abort.
List of available attributes : CurrentStepNumber Direction EventDirection EventID EventKineticEnergy EventPosition GlobalTime KineticEnergy LocalTime PDGCode ParentID ParentParticleName ParticleName ParticleType Polarization Position PostDirection PostKineticEnergy PostPosition PostPositionLocal PostStepUniqueVolumeID PostStepUniqueVolumeIDAsInt PostStepVolumeCopyNo PostVelocity PreDirection PreDirectionLocal PreGlobalTime PreKineticEnergy PrePosition PrePositionLocal PreStepUniqueVolumeID PreStepUniqueVolumeIDAsInt PreStepVolumeCopyNo PreVelocity ProcessDefinedStep RunID StepLength ThreadID TimeFromBeginOfEvent TotalEnergyDeposit TrackCreatorModelIndex TrackCreatorModelName TrackCreatorProcess TrackID TrackLength TrackProperTime TrackVertexKineticEnergy TrackVertexMomentumDirection TrackVertexPosition TrackVolumeCopyNo TrackVolumeInstanceID TrackVolumeName UnscatteredPrimaryFlag Weight 

Setting the filter to F.PostPosition < 0*mm (whatever that means) instead crashes the simulation entirely and no trace is printed.
For reference, I am reporting the script reproducing this error below:

from pathlib import Path
import importlib.metadata as md

import opengate as gate
from opengate.actors.filters import GateFilterBuilder


def main():
    print("opengate:", md.version("opengate"))
    print("opengate-core:", md.version("opengate-core"))

    mm = gate.g4_units.mm
    cm = gate.g4_units.cm
    MeV = gate.g4_units.MeV

    sim = gate.Simulation()
    sim.output_dir = Path("output_reproduce_filter_error")
    sim.output_dir.mkdir(exist_ok=True)

    sim.g4_verbose = False
    sim.visu = False
    sim.number_of_threads = 1
    sim.random_seed = 123456

    # World
    sim.world.size = [20 * cm, 20 * cm, 20 * cm]
    sim.world.material = "G4_AIR"

    # Basic scoring volume: a thin air slab centered at z = 0.
    detector = sim.add_volume("Box", "detector")
    detector.size = [50 * mm, 50 * mm, 1 * mm]
    detector.translation = [0 * mm, 0 * mm, 0 * mm]
    detector.material = "G4_AIR"

    # Basic proton source upstream of the detector.
    source = sim.add_source("GenericSource", "proton_source")
    source.particle = "proton"
    source.n = 10
    source.energy.type = "mono"
    source.energy.mono = 100 * MeV
    source.position.type = "point"
    source.position.translation = [0 * mm, 0 * mm, -50 * mm]
    source.direction.type = "momentum"
    source.direction.momentum = [0, 0, 1]

    # PhaseSpaceActor attached to the detector.
    phsp = sim.add_actor("PhaseSpaceActor", "phsp")
    phsp.attached_to = detector.name
    phsp.attributes = [
        "ParticleName",
        "PostPosition",
        "TrackID",
        "EventID",
    ]

    phsp.steps_to_store = "all"

    F = GateFilterBuilder()

    f = F.PostPosition_X < 0*mm
    phsp.filter = f

    print("Starting sim.run();")
    sim.run()


if __name__ == "__main__":
    main()

Is there a fix to this, or an alternate way to filter scored particles based on their position (and by extention, other vector properties such as their direction)?

Thank you very much in advance

f-blan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions