diff --git a/DDSim/DD4hepSimulation.py b/DDSim/DD4hepSimulation.py index 359dc0486895a8088b8cc7c605a0eae3cf5f9a14..4bc46f6f16ed24dd28b4b1d461c59fc621fdc863 100644 --- a/DDSim/DD4hepSimulation.py +++ b/DDSim/DD4hepSimulation.py @@ -372,41 +372,20 @@ class DD4hepSimulation(object): trk,cal = self.getDetectorLists( lcdd ) - # ---- add the trackers: - # FIXME: this assumes the same filters for all trackers ... - - for tracker in trk: - print 'simple.setupTracker( %s )' % tracker - action = None - for pattern in self.action.mapActions: - if pattern.lower() in tracker.lower(): - action = self.action.mapActions[pattern] - break - if action: - seq,act = simple.setupTracker( tracker, type=action ) - else: - seq,act = simple.setupTracker( tracker ) - seq.add(f1) - if self.detailedShowerMode: - act.HitCreationMode = 2 + # ---- add the trackers: + # FIXME: this assumes the same filters for all trackers ... + try: + self.__setupSensitiveDetectors( trk, simple.setupTracker, filt=f1) + except Exception as e: + print "ERROR setting up sensitive detector", str(e) + raise # ---- add the calorimeters: - for calo in cal: - print 'simple.setupCalorimeter( %s )' % calo - action = None - for pattern in self.action.mapActions: - if pattern.lower() in calo.lower(): - action = self.action.mapActions[pattern] - break - if action: - seq,act = simple.setupCalorimeter( calo, type=action ) - else: - seq,act = simple.setupCalorimeter( calo ) - - ##set detailed hit creation mode for this - if self.detailedShowerMode: - act.HitCreationMode = 2 - + try: + self.__setupSensitiveDetectors( cal, simple.setupCalorimeter ) + except Exception as e: + print "ERROR setting up sensitive detector", str(e) + raise #================================================================================= # Now build the physics list: @@ -581,6 +560,37 @@ class DD4hepSimulation(object): return runHeader + def __setupSensitiveDetectors(self, detectors, setupFuction, filt=None): + """ attach sensitive detector actions for all subdetectors + can be steered with the `Action` ConfigHelpers + + :param detectors: list of detectors + :param setupFunction: function used to register the sensitive detector + :param filt: optional, give a filter to attach to all sensitive detectors + """ + for det in detectors: + print 'Setting up SD for %s' % det + action = None + for pattern in self.action.mapActions: + if pattern.lower() in det.lower(): + action = self.action.mapActions[pattern] + break + if action: + if isinstance( action, tuple ): + sdAction = action[0] + parameterDict = action[1] + seq,act = setupFuction( det, type=sdAction ) + for parameter, value in parameterDict.iteritems(): + setattr( act, parameter, value) + else: + seq,act = setupFuction( det, type=action ) + else: + seq,act = setupFuction( det ) + if filt: + seq.add(filt) + ##set detailed hit creation mode for this + if self.detailedShowerMode: + act.HitCreationMode = 2 ################################################################################ ### MODULE FUNCTIONS GO HERE diff --git a/DDSim/Helper/Action.py b/DDSim/Helper/Action.py index 74c39ce41eac182cbedd1d11c7400b3a5945466f..686c794e7ac265cb2b55f5e302ca73213061bcb7 100644 --- a/DDSim/Helper/Action.py +++ b/DDSim/Helper/Action.py @@ -1,10 +1,26 @@ -"""Helper object for SD Actions""" +"""Helper object for SD Actions -from DDSim.Helper.ConfigHelper import ConfigHelper +The default tracker and calorimeter actions can be set with + +>>> SIM = DD4hepSimulation() +>>> SIM.action.tracker = "Geant4TrackerAction" +>>> SIM.action.tracker = "Geant4CalorimeterAction" + +for specific subdetectors specific sensitive detectors can be set based on pattern matching + +>>> SIM = DD4hepSimulation() +>>> SIM.action.mapActions['tpc'] = "TPCSDAction" +and additional parameters for the sensitive detectors can be set when the map is given a tuple + +>>> SIM = DD4hepSimulation() +>>> SIM.action.mapActions['ecal'] =( "CaloPreShowerSDAction", {"FirstLayerNumber": 1} ) +""" + +from DDSim.Helper.ConfigHelper import ConfigHelper class Action( ConfigHelper ): - """Action holding all gun properties""" + """Action holding sensitive detector actions""" def __init__( self ): super(Action, self).__init__() self._tracker = 'Geant4TrackerAction' @@ -37,13 +53,13 @@ class Action( ConfigHelper ): @mapActions.setter def mapActions( self, val ): """check if the argument is a dict, then we just update mapActions - if it is a string or list, we use pairs as patters --> Action + if it is a string or list, we use pairs as patterns --> Action """ if isinstance(val, dict): self._mapActions.update(val) return - if isinstance( val, basestring): + if isinstance(val, basestring): vals = val.split(" ") elif isinstance( val, list ): vals = val @@ -53,5 +69,5 @@ class Action( ConfigHelper ): self._mapActions[vals[index]] = vals[index+1] def clearMapActions( self ): - """empty the mapActions""" + """empty the mapActions dictionary""" self._mapActions = dict()