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