Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:22

0001 """Configuration Helper for ParticleHandler"""
0002 from DDSim.Helper.ConfigHelper import ConfigHelper
0003 from g4units import MeV, mm
0004 import logging
0005 
0006 logger = logging.getLogger(__name__)
0007 
0008 
0009 class ParticleHandler(ConfigHelper):
0010   """Configuration for the Particle Handler/ MCTruth treatment"""
0011 
0012   def __init__(self):
0013     super(ParticleHandler, self).__init__()
0014     self._saveProcesses = ['Decay']
0015     self._minimalKineticEnergy = 1 * MeV
0016     self._keepAllParticles = False
0017     self._printEndTracking = False
0018     self._printStartTracking = False
0019     self._minDistToParentVertex = 2.2e-14 * mm
0020     self._enableDetailedHitsAndParticleInfo = False
0021     self._userParticleHandler = "Geant4TCUserParticleHandler"
0022     self._closeProperties()
0023 
0024   @property
0025   def enableDetailedHitsAndParticleInfo(self):
0026     """Enable lots of printout on simulated hits and MC-truth information"""
0027     return self._enableDetailedHitsAndParticleInfo
0028 
0029   @enableDetailedHitsAndParticleInfo.setter
0030   def enableDetailedHitsAndParticleInfo(self, val):
0031     self._enableDetailedHitsAndParticleInfo = val
0032 
0033   @property
0034   def userParticleHandler(self):
0035     """Optionally enable an extended Particle Handler"""
0036     return self._userParticleHandler
0037 
0038   @userParticleHandler.setter
0039   def userParticleHandler(self, val):
0040     self._userParticleHandler = val
0041 
0042   @property
0043   def minDistToParentVertex(self):
0044     """Minimal distance between particle vertex and endpoint of parent after
0045     which the vertexIsNotEndpointOfParent flag is set
0046     """
0047     return self._minDistToParentVertex
0048 
0049   @minDistToParentVertex.setter
0050   def minDistToParentVertex(self, val):
0051     self._minDistToParentVertex = val
0052 
0053   @property
0054   def saveProcesses(self):
0055     """List of processes to save, on command line give as whitespace separated string in quotation marks"""
0056     return self._saveProcesses
0057 
0058   @saveProcesses.setter
0059   def saveProcesses(self, stringVal):
0060     self._saveProcesses = ConfigHelper.makeList(stringVal)
0061 
0062   @property
0063   def minimalKineticEnergy(self):
0064     """MinimalKineticEnergy to store particles created in the tracking region"""
0065     return self._minimalKineticEnergy
0066 
0067   @minimalKineticEnergy.setter
0068   def minimalKineticEnergy(self, val):
0069     self._minimalKineticEnergy = val
0070 
0071   @property
0072   def keepAllParticles(self):
0073     """ Keep all created particles """
0074     return self._keepAllParticles
0075 
0076   @keepAllParticles.setter
0077   def keepAllParticles(self, val):
0078     self._keepAllParticles = val
0079 
0080   @property
0081   def printStartTracking(self):
0082     """ Printout at Start of Tracking """
0083     return self._printStartTracking
0084 
0085   @printStartTracking.setter
0086   def printStartTracking(self, val):
0087     self._printEndTracking = val
0088 
0089   @property
0090   def printEndTracking(self):
0091     """ Printout at End of Tracking """
0092     return self._printEndTracking
0093 
0094   @printEndTracking.setter
0095   def printEndTracking(self, val):
0096     self._printEndTracking = val
0097 
0098   def setDumpDetailedParticleInfo(self, kernel, DDG4):
0099     # ---- debug code from Markus for detailed dumps of hits and MC-truth assignement ------
0100     # Add the particle dumper to associate the MC truth
0101     evt = DDG4.EventAction(kernel, "Geant4ParticleDumpAction/ParticleDump")
0102     kernel.eventAction().adopt(evt)
0103     evt.enableUI()
0104     # Add the hit dumper BEFORE any hit truth is fixed
0105     evt = DDG4.EventAction(kernel, "Geant4HitDumpAction/RawDump")
0106     kernel.eventAction().adopt(evt)
0107     evt.enableUI()
0108     # Add the hit dumper to the event action sequence
0109     evt = DDG4.EventAction(kernel, "Geant4HitTruthHandler/HitTruth")
0110     kernel.eventAction().adopt(evt)
0111     evt.enableUI()
0112     # Add the hit dumper AFTER any hit truth is fixed. We should see the reduced track references
0113     evt = DDG4.EventAction(kernel, "Geant4HitDumpAction/HitDump")
0114     kernel.eventAction().adopt(evt)
0115     evt.enableUI()
0116 
0117   def setupUserParticleHandler(self, part, kernel, DDG4):
0118     """Create the UserParticleHandler and configure it.
0119 
0120     FIXME: this is not extensible at the moment
0121     """
0122     if not self.userParticleHandler:
0123       return
0124 
0125     if self.userParticleHandler not in ["Geant4TCUserParticleHandler", "Geant4TVUserParticleHandler"]:
0126       logger.error("unknown UserParticleHandler: %r" % self.userParticleHandler)
0127       exit(1)
0128 
0129     if self.userParticleHandler == "Geant4TCUserParticleHandler":
0130       user = DDG4.Action(kernel, "%s/UserParticleHandler" % self.userParticleHandler)
0131       try:
0132         user.TrackingVolume_Zmax = DDG4.tracker_region_zmax
0133         user.TrackingVolume_Rmax = DDG4.tracker_region_rmax
0134       except AttributeError as e:
0135         logger.error("Attribute of tracker region missing in detector model %s", e)
0136         logger.error("   make sure to specify the global constants tracker_region_zmax and tracker_region_rmax ")
0137         logger.error("   this is needed for the MC-truth link of created sim-hits  !  ")
0138         logger.error(" Or Disable the User Particle Handler with --part.userParticleHandler=''")
0139         exit(1)
0140 
0141       try:
0142         user.TrackingVolume_Zmin = DDG4.tracker_region_zmin
0143       except AttributeError as e:
0144         logger.debug("Attribute tracker_region_zmin for asymmetric tracker region missing %s", e)
0145         logger.debug("  will use symmetric region defined by tracker_region_zmax")
0146         user.TrackingVolume_Zmin = str(-float(DDG4.tracker_region_zmax))
0147 
0148       logger.info(" *** definition of tracker region *** ")
0149       logger.info("    tracker_region_zmin = %s", user.TrackingVolume_Zmin)
0150       logger.info("    tracker_region_zmax = %s", user.TrackingVolume_Zmax)
0151       logger.info("    tracker_region_rmax = %s", user.TrackingVolume_Rmax)
0152       logger.info(" ************************************ ")
0153 
0154       part.adopt(user)
0155 
0156     elif self.userParticleHandler == "Geant4TVUserParticleHandler":
0157       if not kernel.detectorDescription().trackingVolume().isValid():
0158         logger.error("Geant4TVUserParticleHandler requested but no tracking_volume defined in the XML")
0159         exit(1)
0160 
0161       user = DDG4.Action(kernel, "%s/UserParticleHandler" % self.userParticleHandler)
0162       part.adopt(user)
0163 
0164     return