Skip to content

Commit

Permalink
fix: expect list of tuple or dict for actions; parse JSON on CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
wdconinc committed Feb 4, 2024
1 parent a6ceb1f commit d3d447a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
28 changes: 12 additions & 16 deletions DDG4/python/DDSim/DD4hepSimulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,22 +348,18 @@ def run(self):
self.geometry.constructGeometry(kernel, geant4, self.output.geometry)

# ----------------------------------------------------------------------------------
# Configure run, event, track, step actions, if present
for action_name in self.action.run:
action = DDG4.RunAction(kernel, action_name)
kernel.runAction().add(action)
for action_name in self.action.event:
action = DDG4.EventAction(kernel, action_name)
kernel.eventAction().add(action)
for action_name in self.action.track:
action = DDG4.TrackingAction(kernel, action_name)
kernel.trackingAction().add(action)
for action_name in self.action.step:
action = DDG4.SteppingAction(kernel, action_name)
kernel.steppingAction().add(action)
for action_name in self.action.stack:
action = DDG4.StackingAction(kernel, action_name)
kernel.stackingAction().add(action)
# Configure run, event, track, step, and stack actions, if present
for action_list, DDG4_Action, kernel_Action in \
[(self.action.run, DDG4.RunAction, kernel.runAction),
(self.action.event, DDG4.EventAction, kernel.eventAction),
(self.action.track, DDG4.TrackingAction, kernel.trackingAction),
(self.action.step, DDG4.SteppingAction, kernel.steppingAction),
(self.action.stack, DDG4.StackingAction, kernel.stackingAction)]:
for action_dict in action_list:
action = DDG4_Action(kernel, action_dict["name"])
for parameter, value in action_dict['parameter'].items():
setattr(action, parameter, value)
kernel_Action().add(action)

# ----------------------------------------------------------------------------------
# Configure Run actions
Expand Down
44 changes: 34 additions & 10 deletions DDG4/python/DDSim/Helper/Action.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class Action(ConfigHelper):
Additional actions can be set as well with
>>> SIM = DD4hepSimulation()
>>> SIM.action.run = "Geant4TestRunAction"
>>> SIM.action.event = "Geant4TestEventAction"
>>> SIM.action.track = "Geant4TestTrackAction"
>>> SIM.action.step = "Geant4TestStepAction"
>>> SIM.action.stack = "Geant4TestStackAction"
>>> SIM.action.run = [ ("Geant4TestRunAction", {"Property_int": 10} ) ]
>>> SIM.action.event = [ ("Geant4TestEventAction", {"Property_int": 10} ) ]
>>> SIM.action.track = [ ("Geant4TestTrackAction", {"Property_int": 10} ) ]
>>> SIM.action.step = [ ("Geant4TestStepAction", {"Property_int": 10} ) ]
>>> SIM.action.stack = [ ("Geant4TestStackAction", {"Property_int": 10} ) ]
"""

Expand Down Expand Up @@ -123,14 +123,38 @@ def calorimeterSDTypes(self):
def calorimeterSDTypes(self, val):
self._calorimeterSDTypes = ConfigHelper.makeList(val)


@staticmethod
def makeListOfDictFromJSON(val):
if isinstance(val, str):
# assumes: valid JSON string or comma-separated list of names
import json
try:
val = json.loads(val)
except:
val = tuple(val.split(","))
if isinstance(val, tuple):
# assumes: ( "Geant4TestEventAction", {"Property_int": 10} )
# creates: { "name": "Geant4TestEventAction", "parameter": {"Property_int": 10} }
# note: not able to specified as json which only allows a list
val = dict(name=val[0], parameter=val[1])
if isinstance(val, dict):
# assumes: { "name": "Geant4TestEventAction", "parameter": {"Property_int": 10} }
# creates: [ { "name": "Geant4TestEventAction", "parameter": {"Property_int": 10} } ]
val = [ val ]
if isinstance(val, list):
# assumes: [ { "name": "Geant4TestEventAction", "parameter": {"Property_int": 10} } ]
return val
raise RuntimeError("Commandline setting of action is not successful for: %s " % val)

@property
def run(self):
""" set the default run action """
return self._run

@run.setter
def run(self, val):
self._run = ConfigHelper.makeList(val)
self._run.extend(Action.makeListOfDictFromJSON(val))

@property
def event(self):
Expand All @@ -139,7 +163,7 @@ def event(self):

@event.setter
def event(self, val):
self._event = ConfigHelper.makeList(val)
self._event.extend(Action.makeListOfDictFromJSON(val))

@property
def track(self):
Expand All @@ -148,7 +172,7 @@ def track(self):

@track.setter
def track(self, val):
self._track = ConfigHelper.makeList(val)
self._track.extend(Action.makeListOfDictFromJSON(val))

@property
def step(self):
Expand All @@ -157,7 +181,7 @@ def step(self):

@step.setter
def step(self, val):
self._step = ConfigHelper.makeList(val)
self._step.extend(Action.makeListOfDictFromJSON(val))

@property
def stack(self):
Expand All @@ -166,4 +190,4 @@ def stack(self):

@stack.setter
def stack(self, val):
self._stack = ConfigHelper.makeList(val)
self._stack.extend(Action.makeListOfDictFromJSON(val))

0 comments on commit d3d447a

Please sign in to comment.