diff --git a/DDG4/python/DDSim/DD4hepSimulation.py b/DDG4/python/DDSim/DD4hepSimulation.py index 5a2760ada8015a63e488b4941f26a5191e8b1a05..0917f7790de06cb11b27ba4991b7ae54a0b54f67 100644 --- a/DDG4/python/DDSim/DD4hepSimulation.py +++ b/DDG4/python/DDSim/DD4hepSimulation.py @@ -348,22 +348,18 @@ class DD4hepSimulation(object): 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 diff --git a/DDG4/python/DDSim/Helper/Action.py b/DDG4/python/DDSim/Helper/Action.py index f4e57235166ca4e1a134a6f9af17cabd7b241d7d..dc7dbf34feeb866047ff6c3d6e9c901ba494c6a0 100644 --- a/DDG4/python/DDSim/Helper/Action.py +++ b/DDG4/python/DDSim/Helper/Action.py @@ -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} ) ] """ @@ -123,6 +123,30 @@ class Action(ConfigHelper): 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 """ @@ -130,7 +154,7 @@ class Action(ConfigHelper): @run.setter def run(self, val): - self._run = ConfigHelper.makeList(val) + self._run.extend(Action.makeListOfDictFromJSON(val)) @property def event(self): @@ -139,7 +163,7 @@ class Action(ConfigHelper): @event.setter def event(self, val): - self._event = ConfigHelper.makeList(val) + self._event.extend(Action.makeListOfDictFromJSON(val)) @property def track(self): @@ -148,7 +172,7 @@ class Action(ConfigHelper): @track.setter def track(self, val): - self._track = ConfigHelper.makeList(val) + self._track.extend(Action.makeListOfDictFromJSON(val)) @property def step(self): @@ -157,7 +181,7 @@ class Action(ConfigHelper): @step.setter def step(self, val): - self._step = ConfigHelper.makeList(val) + self._step.extend(Action.makeListOfDictFromJSON(val)) @property def stack(self): @@ -166,4 +190,4 @@ class Action(ConfigHelper): @stack.setter def stack(self, val): - self._stack = ConfigHelper.makeList(val) + self._stack.extend(Action.makeListOfDictFromJSON(val))