File indexing completed on 2026-06-17 07:49:09
0001 import os
0002 import sys
0003 import time
0004 import DDG4
0005 from DDG4 import OutputLevel as Output
0006 from g4units import GeV, MeV
0007 import logging
0008
0009 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
0010 logger = logging.getLogger(__name__)
0011
0012
0013
0014 """
0015
0016 dd4hep example setup using the python configuration
0017
0018 \author M.Frank
0019 \version 1.0
0020
0021 """
0022
0023
0024 def run():
0025 kernel = DDG4.Kernel()
0026 install_dir = os.environ['DD4hepExamplesINSTALL']
0027 kernel.setOutputLevel(str('Geant4Converter'), Output.DEBUG)
0028 kernel.setOutputLevel(str('Gun'), Output.INFO)
0029 kernel.detectorDescription().fromXML(str("file:" + install_dir + "/examples/DDCMS/data/dd4hep-config.xml"))
0030 kernel.NumEvents = 5
0031 geant4 = DDG4.Geant4(kernel, tracker='Geant4TrackerCombineAction')
0032 geant4.printDetectors()
0033 geant4.setupCshUI()
0034 batch = False
0035 test = False
0036 for i in range(len(sys.argv)):
0037 arg = sys.argv[i].lower()
0038 if arg == 'batch':
0039 batch = True
0040 elif arg == 'test':
0041 test = True
0042 elif arg == 'numevents':
0043 kernel.NumEvents = int(sys.argv[i + 1])
0044 if batch or test:
0045 kernel.UI = ''
0046
0047
0048 geant4.setupTrackingField(prt=True)
0049
0050 geant4.setupROOTOutput('RootOutput', 'CMSTracker_' + time.strftime('%Y-%m-%d_%H-%M'), mc_truth=True)
0051
0052 generators = []
0053 generators.append(geant4.setupGun("GunPi-", particle='pi-', energy=300 * GeV,
0054 multiplicity=1, Standalone=False, register=False, Mask=1))
0055 if not test:
0056 generators.append(geant4.setupGun("GunPi+", particle='pi+', energy=300 * GeV,
0057 multiplicity=1, Standalone=False, register=False, Mask=2))
0058 generators.append(geant4.setupGun("GunE-", particle='e-', energy=100 * GeV,
0059 multiplicity=1, Standalone=False, register=False, Mask=4))
0060 generators.append(geant4.setupGun("GunE+", particle='e+', energy=100 * GeV,
0061 multiplicity=1, Standalone=False, register=False, Mask=8))
0062 geant4.buildInputStage(generators)
0063
0064 for i in geant4.description.detectors():
0065 o = DDG4.DetElement(i.second.ptr())
0066 sd = geant4.description.sensitiveDetector(o.name())
0067 if sd.isValid():
0068 typ = geant4.sensitive_types[sd.type()]
0069 logger.info('CMSTracker: Configure subdetector %-24s of type %s' % (o.name(), typ,))
0070 geant4.setupDetector(o.name(), typ)
0071
0072
0073 part = DDG4.GeneratorAction(kernel, "Geant4ParticleHandler/ParticleHandler")
0074 kernel.generatorAction().adopt(part)
0075 part.SaveProcesses = ['conv', 'Decay']
0076 part.MinimalKineticEnergy = 1 * MeV
0077 part.OutputLevel = 5
0078 part.enableUI()
0079
0080
0081 phys = kernel.physicsList()
0082 phys.extends = 'QGSP_BERT'
0083 phys.enableUI()
0084
0085 geant4.execute()
0086
0087
0088 if __name__ == "__main__":
0089 run()