From 58b2554f50fcc352e38d7a73e7940ca49e8ba629 Mon Sep 17 00:00:00 2001
From: Andre Sailer <andre.philippe.sailer@cern.ch>
Date: Fri, 20 Jul 2018 13:23:38 +0200
Subject: [PATCH] DDSim: make the use of UserParticleHandler configurable

---
 DDG4/python/DDSim/DD4hepSimulation.py       | 24 +-------------
 DDG4/python/DDSim/Helper/ParticleHandler.py | 36 +++++++++++++++++++++
 2 files changed, 37 insertions(+), 23 deletions(-)

diff --git a/DDG4/python/DDSim/DD4hepSimulation.py b/DDG4/python/DDSim/DD4hepSimulation.py
index dac758cde..0548a71f4 100644
--- a/DDG4/python/DDSim/DD4hepSimulation.py
+++ b/DDG4/python/DDSim/DD4hepSimulation.py
@@ -418,29 +418,7 @@ class DD4hepSimulation(object):
     if self.part.enableDetailedHitsAndParticleInfo:
       self.part.setDumpDetailedParticleInfo( kernel, DDG4 )
 
-    #----------------------------------
-
-
-
-
-    user = DDG4.Action(kernel,"Geant4TCUserParticleHandler/UserParticleHandler")
-    try:
-      user.TrackingVolume_Zmax = DDG4.tracker_region_zmax
-      user.TrackingVolume_Rmax = DDG4.tracker_region_rmax
-
-      print " *** definition of tracker region *** "
-      print "    tracker_region_zmax = " ,  user.TrackingVolume_Zmax
-      print "    tracker_region_rmax = " ,  user.TrackingVolume_Rmax
-      print " ************************************ "
-
-    except AttributeError as e:
-      print "ERROR - attribute of tracker region missing in detector model   ", str(e)
-      print "   make sure to specify the global constants tracker_region_zmax and tracker_region_rmax "
-      print "   this is needed for the MC-truth link of created sim-hits  !  "
-      exit(1)
-      
-    #  user.enableUI()
-    part.adopt(user)
+    self.part.setupUserParticleHandler(part)
 
     #=================================================================================
 
diff --git a/DDG4/python/DDSim/Helper/ParticleHandler.py b/DDG4/python/DDSim/Helper/ParticleHandler.py
index f66c4b02e..aaabea68f 100644
--- a/DDG4/python/DDSim/Helper/ParticleHandler.py
+++ b/DDG4/python/DDSim/Helper/ParticleHandler.py
@@ -14,6 +14,7 @@ class ParticleHandler( ConfigHelper ):
     self._printStartTracking = False
     self._minDistToParentVertex = 2.2e-14*mm
     self._enableDetailedHitsAndParticleInfo = False
+    self._userParticleHandler = "Geant4TCUserParticleHandler"
 
   @property
   def enableDetailedHitsAndParticleInfo(self):
@@ -24,6 +25,13 @@ class ParticleHandler( ConfigHelper ):
   def enableDetailedHitsAndParticleInfo( self, val ):
     self._enableDetailedHitsAndParticleInfo = val
 
+  @property
+  def userParticleHandler( self ):
+    """Optionally enable an extended Particle Handler"""
+    return self._userParticleHandler
+  @userParticleHandler.setter
+  def userParticleHandler( self, val ):
+    self._userParticleHandler = val
 
   @property
   def minDistToParentVertex( self ):
@@ -93,3 +101,31 @@ class ParticleHandler( ConfigHelper ):
     evt = DDG4.EventAction(kernel,"Geant4HitDumpAction/HitDump")
     kernel.eventAction().adopt(evt)
     evt.enableUI()
+
+  def setupUserParticleHandler(self, part):
+    """Create the UserParticleHandler and configure it.
+
+    FIXME: this is not extensible at the moment
+    """
+    if not self.userParticleHandler:
+      return
+
+    if self.userParticleHandler == "Geant4TCUserParticleHandler":
+      user = DDG4.Action(kernel,"%s/UserParticleHandler" % self.userParticleHandler)
+      try:
+        user.TrackingVolume_Zmax = DDG4.tracker_region_zmax
+        user.TrackingVolume_Rmax = DDG4.tracker_region_rmax
+        print " *** definition of tracker region *** "
+        print "    tracker_region_zmax = " , user.TrackingVolume_Zmax
+        print "    tracker_region_rmax = " , user.TrackingVolume_Rmax
+        print " ************************************ "
+      except AttributeError as e:
+        print "ERROR - attribute of tracker region missing in detector model   ", str(e)
+        print "   make sure to specify the global constants tracker_region_zmax and tracker_region_rmax "
+        print "   this is needed for the MC-truth link of created sim-hits  !  "
+        print " Or Disable the User Particle Handler with --part.userParticleHandler=''"
+        exit(1)
+      part.adopt(user)
+
+    print "ERROR: unknown UserParticleHandler: %r" % self.userParticleHandler
+    exit(1)
-- 
GitLab