Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-29 08:26:04

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-ecal.xml"))
0030   kernel.NumEvents = 5
0031   geant4 = DDG4.Geant4(kernel, tracker='Geant4TrackerCombineAction')
0032   geant4.printDetectors()
0033 
0034   batch = False
0035   test = False
0036   vis = False
0037   ui = None
0038   for i in range(len(sys.argv)):
0039     arg = sys.argv[i].lower()
0040     if arg == 'batch':
0041       batch = True
0042     elif arg[:4] == '-vis':
0043       vis = True
0044     elif arg == 'test':
0045       test = True
0046     elif arg == 'numevents':
0047       kernel.NumEvents = int(sys.argv[i + 1])
0048   if batch or test:
0049     geant4.setupCshUI(ui=None, vis=None)
0050     kernel.UI = 'UI'
0051   else:
0052     ui = geant4.setupCshUI(vis=vis)
0053 
0054   # Configure field
0055   geant4.setupTrackingField(prt=True)
0056   # Configure I/O
0057   geant4.setupROOTOutput('RootOutput', 'CMSEcal_' + time.strftime('%Y-%m-%d_%H-%M'), mc_truth=True)
0058   # Setup particle gun
0059   generators = []
0060   generators.append(geant4.setupGun("GunPi-", particle='pi-', energy=300 * GeV,
0061                                     multiplicity=1, Standalone=False, register=False, Mask=1))
0062   if not test:
0063     generators.append(geant4.setupGun("GunE+", particle='e+', energy=100 * GeV,
0064                                       multiplicity=1, Standalone=False, register=False, Mask=8))
0065   geant4.buildInputStage(generators)
0066   # Now setup all tracking detectors
0067   for i in geant4.description.detectors():
0068     o = DDG4.DetElement(i.second.ptr())
0069     sd = geant4.description.sensitiveDetector(o.name())
0070     if sd.isValid():
0071       typ = geant4.sensitive_types[sd.type()]
0072       logger.info('CMSTracker: Configure subdetector %-24s of type %s' % (o.name(), typ,))
0073       geant4.setupDetector(o.name(), typ)
0074 
0075   # And handle the simulation particles.
0076   part = DDG4.GeneratorAction(kernel, "Geant4ParticleHandler/ParticleHandler")
0077   kernel.generatorAction().adopt(part)
0078   part.SaveProcesses = ['conv', 'Decay']
0079   part.MinimalKineticEnergy = 1 * MeV
0080   part.OutputLevel = 5  # generator_output_level
0081   part.enableUI()
0082 
0083   # Now build the physics list:
0084   phys = kernel.physicsList()
0085   phys.extends = 'QGSP_BERT'
0086   phys.enableUI()
0087   #
0088   #
0089   if ui and vis:
0090     cmds = []
0091     cmds.append('/control/verbose 2')
0092     cmds.append('/run/initialize')
0093     cmds.append('/vis/open OGL')
0094     cmds.append('/vis/verbose errors')
0095     cmds.append('/vis/drawVolume')
0096     cmds.append('/vis/viewer/set/viewpointThetaPhi 55. 45.')
0097     cmds.append('/vis/scene/add/axes 0 0 0 3 m')
0098     ui.Commands = cmds
0099   #
0100   # and run
0101   geant4.execute()
0102 
0103 
0104 if __name__ == "__main__":
0105   run()