File indexing completed on 2026-09-11 08:25:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 import os
0014 import sys
0015 import time
0016 import logging
0017 import DDG4
0018 from DDG4 import OutputLevel as Output
0019 from g4units import MeV, GeV, m, mm
0020
0021
0022 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
0023 logger = logging.getLogger(__name__)
0024 """
0025
0026 dd4hep simulation example setup using the python configuration
0027
0028 @author M.Frank
0029 @version 1.0
0030
0031 """
0032
0033
0034 def show_help():
0035 logging.info("Check_shape.py -option [-option] ")
0036 logging.info(" -vis Enable visualization ")
0037 logging.info(" -batch Batch execution ")
0038
0039
0040 def run():
0041 hlp = False
0042 vis = False
0043 dump = False
0044 batch = False
0045 install_dir = os.environ['DD4hepINSTALL']
0046
0047 for i in list(range(len(sys.argv))):
0048 c = sys.argv[i].upper()
0049 if c.find('BATCH') < 2 and c.find('BATCH') >= 0:
0050 batch = True
0051 elif c[:4] == '-VIS':
0052 vis = True
0053 elif c[:4] == '-DUM':
0054 dump = True
0055 elif c[:2] == '-H':
0056 hlp = True
0057
0058 if hlp:
0059 show_help()
0060 sys.exit(1)
0061
0062 kernel = DDG4.Kernel()
0063 description = kernel.detectorDescription()
0064 install_dir = os.environ['DD4hepExamplesINSTALL']
0065 geant4 = DDG4.Geant4(kernel, tracker='Geant4TrackerCombineAction')
0066
0067 logger.info("# Configure UI")
0068 ui = None
0069 if batch:
0070 geant4.setupCshUI(ui=None, vis=None)
0071 kernel.UI = 'UI'
0072 else:
0073 ui = geant4.setupCshUI(vis=vis)
0074
0075 kernel.loadGeometry(str("file:" + install_dir + "/examples/ClientTests/compact/NestedBoxReflection.xml"))
0076 DDG4.importConstants(description)
0077
0078 geant4.printDetectors()
0079 if dump:
0080 seq, act = geant4.addDetectorConstruction("Geant4DetectorGeometryConstruction/ConstructGeo")
0081 act.DebugReflections = True
0082 act.DebugMaterials = False
0083 act.DebugElements = False
0084 act.DebugVolumes = False
0085 act.DebugShapes = False
0086 act.DumpHierarchy = ~0x0
0087
0088 logger.info("# Configure G4 magnetic field tracking")
0089 geant4.setupTrackingField()
0090
0091 logger.info("# Setup random generator")
0092 rndm = DDG4.Action(kernel, 'Geant4Random/Random')
0093 rndm.Seed = 987654321
0094 rndm.initialize()
0095
0096 logger.info("# Configure Event actions")
0097 prt = DDG4.EventAction(kernel, 'Geant4ParticlePrint/ParticlePrint')
0098 prt.OutputType = 3
0099 prt.OutputLevel = Output.INFO
0100 kernel.eventAction().adopt(prt)
0101
0102 logger.info("# Configure I/O")
0103 geant4.setupROOTOutput('RootOutput', 'BoxReflect_' + time.strftime('%Y-%m-%d_%H-%M'))
0104
0105 gen = DDG4.GeneratorAction(kernel, "Geant4GeneratorActionInit/GenerationInit")
0106 gen.enableUI()
0107 kernel.generatorAction().adopt(gen)
0108
0109 logger.info("# Generation of isotrope tracks of a given multiplicity with overlay:")
0110 gen = DDG4.GeneratorAction(kernel, "Geant4ParticleGun/IsotropE+")
0111 gen.mask = 4
0112 gen.isotrop = True
0113 gen.particle = 'e+'
0114 gen.Energy = 100 * GeV
0115 gen.multiplicity = 200
0116 gen.position = (0 * m, 0 * m, 0 * m)
0117 gen.direction = (0, 0, 1.)
0118 gen.distribution = 'uniform'
0119 gen.standalone = False
0120
0121
0122
0123
0124 gen.enableUI()
0125 kernel.generatorAction().adopt(gen)
0126
0127 logger.info("# Merge all existing interaction records")
0128 gen = DDG4.GeneratorAction(kernel, "Geant4InteractionMerger/InteractionMerger")
0129 gen.OutputLevel = 4
0130 gen.enableUI()
0131 kernel.generatorAction().adopt(gen)
0132
0133 logger.info("# Finally generate Geant4 primaries")
0134 gen = DDG4.GeneratorAction(kernel, "Geant4PrimaryHandler/PrimaryHandler")
0135 gen.OutputLevel = 4
0136 gen.enableUI()
0137 kernel.generatorAction().adopt(gen)
0138
0139 logger.info("# ....and handle the simulation particles.")
0140 part = DDG4.GeneratorAction(kernel, "Geant4ParticleHandler/ParticleHandler")
0141 kernel.generatorAction().adopt(part)
0142
0143 part.SaveProcesses = ['Decay']
0144 part.MinimalKineticEnergy = 100 * MeV
0145 part.OutputLevel = 5
0146 part.enableUI()
0147 user = DDG4.Action(kernel, "Geant4TCUserParticleHandler/UserParticleHandler")
0148 user.TrackingVolume_Zmax = 3.0 * m
0149 user.TrackingVolume_Rmax = 3.0 * m
0150 user.enableUI()
0151 part.adopt(user)
0152
0153 logger.info("# Now setup the calorimeters")
0154 seq, actions = geant4.setupDetectors()
0155
0156 logger.info("# Now build the physics list:")
0157 geant4.setupPhysics('QGSP_BERT')
0158 ph = geant4.addPhysics(str('Geant4PhysicsList/Myphysics'))
0159 ph.addPhysicsConstructor(str('G4StepLimiterPhysics'))
0160
0161
0162 part = geant4.addPhysics('Geant4ExtraParticles/ExtraParticles')
0163 part.pdgfile = os.path.join(install_dir, 'examples/DDG4/examples/particle.tbl')
0164
0165
0166 rg = geant4.addPhysics('Geant4DefaultRangeCut/GlobalRangeCut')
0167 rg.RangeCut = 0.7 * mm
0168
0169
0170 if ui and vis:
0171 cmds = []
0172 cmds.append('/control/verbose 2')
0173 cmds.append('/run/initialize')
0174 cmds.append('/vis/open OGL')
0175 cmds.append('/vis/verbose errors')
0176 cmds.append('/vis/drawVolume')
0177 cmds.append('/vis/viewer/set/viewpointThetaPhi 55. 45.')
0178 cmds.append('/vis/scene/add/axes 0 0 0 3 m')
0179 ui.Commands = cmds
0180
0181 kernel.configure()
0182 kernel.initialize()
0183
0184
0185 kernel.run()
0186 kernel.terminate()
0187
0188
0189 if __name__ == "__main__":
0190 run()