File indexing completed on 2025-07-11 07:52:52
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 from __future__ import absolute_import, unicode_literals
0014 import os
0015 import time
0016 import DDG4
0017 from DDG4 import OutputLevel as Output
0018 from g4units import GeV, MeV, m
0019
0020
0021 """
0022
0023 dd4hep simulation example setup using the python configuration
0024
0025 @author M.Frank
0026 @version 1.0
0027
0028 """
0029
0030
0031 def run():
0032 args = DDG4.CommandLine()
0033 kernel = DDG4.Kernel()
0034 logger = DDG4.Logger('BoxOfStraws')
0035 install_dir = os.environ['DD4hepExamplesINSTALL']
0036
0037 if args.sensitive:
0038 kernel.loadGeometry(str('file:' + install_dir + '/examples/ClientTests/compact/BoxOfStraws_sensitive.xml'))
0039 else:
0040 kernel.loadGeometry(str('file:' + install_dir + '/examples/ClientTests/compact/BoxOfStraws.xml'))
0041
0042 DDG4.importConstants(kernel.detectorDescription(), debug=False)
0043 geant4 = DDG4.Geant4(kernel)
0044 geant4.printDetectors()
0045
0046 if args.macro:
0047 ui = geant4.setupCshUI(macro=args.macro)
0048 else:
0049 ui = geant4.setupCshUI()
0050
0051 if args.verbose:
0052 ui.Commands.append('/run/verbose ' + str(args.verbose))
0053 if args.batch:
0054 ui.Commands.append('/run/beamOn ' + str(args.events))
0055
0056
0057 if args.batch:
0058 ui.Commands.append('/ddg4/UI/terminate')
0059
0060
0061 geant4.setupTrackingField(prt=True)
0062
0063 prt = DDG4.EventAction(kernel, 'Geant4ParticlePrint/ParticlePrint')
0064 prt.OutputLevel = Output.DEBUG
0065 prt.OutputType = 3
0066 kernel.eventAction().adopt(prt)
0067
0068 seq, act = geant4.addDetectorConstruction('Geant4DetectorConstructionResources/ResourcesBeforeConstruction')
0069 act.When = "geometry"
0070
0071
0072 seq, act = geant4.addDetectorConstruction('Geant4DetectorGeometryConstruction/ConstructGeo')
0073 act.DebugVolumes = True
0074
0075 if args.sensitive:
0076
0077 seq, act = geant4.addDetectorConstruction('Geant4DetectorSensitivesConstruction/ConstructSD')
0078 else:
0079
0080 seq, act = geant4.addDetectorConstruction('Geant4RegexSensitivesConstruction/ConstructSDRegEx')
0081 act.Detector = 'BoxOfStrawsDet'
0082 act.OutputLevel = Output.ALWAYS
0083 act.Match = ['gas_']
0084
0085 seq, act = geant4.addDetectorConstruction('Geant4DetectorConstructionResources/ResourcesAfterConstruction')
0086 act.When = "sensitives"
0087
0088
0089 geant4.setupROOTOutput('RootOutput', 'BoxOfStraws_' + time.strftime('%Y-%m-%d_%H-%M'))
0090
0091
0092 gun = geant4.setupGun('Gun', particle='pi+', energy=10 * GeV, multiplicity=1)
0093 gun.OutputLevel = Output.INFO
0094 gun.enableUI()
0095
0096
0097 part = DDG4.GeneratorAction(kernel, 'Geant4ParticleHandler/ParticleHandler')
0098 kernel.generatorAction().adopt(part)
0099 part.SaveProcesses = ['Decay']
0100 part.MinimalKineticEnergy = 50 * MeV
0101 user = DDG4.Action(kernel, 'Geant4TCUserParticleHandler/UserParticleHandler')
0102 user.TrackingVolume_Zmax = 2.5 * m
0103 user.TrackingVolume_Rmax = 2.5 * m
0104 part.adopt(user)
0105
0106
0107 sd = geant4.description.sensitiveDetector(str('BoxOfStrawsDet'))
0108 logger.info(f'+++ BoxOfStraws: SD type: {str(sd.type())}')
0109 energy_filter = DDG4.Filter(kernel, 'EnergyDepositMinimumCut')
0110 energy_filter.Cut = 1.0 * MeV
0111 energy_filter.enableUI()
0112 kernel.registerGlobalFilter(energy_filter)
0113 seq, act = geant4.setupDetector(name='BoxOfStrawsDet', action='MyTrackerSDAction')
0114 seq.adopt(energy_filter)
0115 act.HaveCellID = False
0116
0117
0118 phys = geant4.setupPhysics(str('QGSP_BERT'))
0119 phys.dump()
0120 if args.simultate:
0121 geant4.execute()
0122 else:
0123 geant4.kernel().configure()
0124 geant4.kernel().initialize()
0125 geant4.kernel().terminate()
0126
0127
0128 if __name__ == "__main__":
0129 run()