Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 08:26:21

0001 #!/usr/bin/env python3
0002 # ==========================================================================
0003 #  AIDA Detector description implementation
0004 # --------------------------------------------------------------------------
0005 # Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0006 # All rights reserved.
0007 #
0008 # For the licensing terms see $DD4hepINSTALL/LICENSE.
0009 # For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0010 #
0011 # ==========================================================================
0012 
0013 import os
0014 import sys
0015 import errno
0016 import optparse
0017 import logging
0018 
0019 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
0020 logger = logging.getLogger(__name__)
0021 
0022 
0023 def printOpts(opts):
0024   o = eval(str(opts))
0025   prefix = sys.argv[0].split(os.sep)[-1]
0026   for name, value in o.items():
0027     logger.info('%s > %-18s %s  [%s]', prefix, name + ':', str(value), str(value.__class__))
0028 
0029 
0030 def materialScan(opts):
0031   kernel = DDG4.Kernel()
0032   kernel.loadGeometry(str(opts.compact))
0033   DDG4.Core.setPrintFormat(str("%-32s %6s %s"))
0034   geant4 = DDG4.Geant4(kernel)
0035   # Configure UI
0036   geant4.setupCshUI(ui=None)
0037   for i in geant4.description.detectors():
0038     o = DDG4.DetElement(i.second.ptr())
0039     sd = geant4.description.sensitiveDetector(o.name())
0040     if sd.isValid():
0041       typ = sd.type()
0042       if typ in geant4.sensitive_types:
0043         geant4.setupDetector(o.name(), geant4.sensitive_types[typ])
0044       else:
0045         logger.error('+++  %-32s type:%-12s  --> Unknown Sensitive type: %s', o.name(), typ, typ)
0046         sys.exit(errno.EINVAL)
0047 
0048   geant4.setupGun("Gun",
0049                   Standalone=True,
0050                   particle='geantino',
0051                   energy=20 * g4units.GeV,
0052                   position=opts.position,
0053                   direction=opts.direction,
0054                   multiplicity=1,
0055                   isotrop=False)
0056   scan = DDG4.SteppingAction(kernel, 'Geant4GeometryScanner/GeometryScan')
0057   kernel.steppingAction().adopt(scan)
0058 
0059   # Now build the physics list:
0060   geant4.setupPhysics('QGSP_BERT')
0061 
0062   kernel.configure()
0063   kernel.initialize()
0064   kernel.NumEvents = 1
0065   kernel.run()
0066   kernel.terminate()
0067   return 0
0068 
0069 
0070 parser = optparse.OptionParser()
0071 parser.formatter.width = 132
0072 parser.description = 'Scan Volumes along a trajectory using Geant4.'
0073 parser.add_option('-c', '--compact', dest='compact', default=None,
0074                   help='Define LCCDD style compact xml input',
0075                   metavar='<FILE>')
0076 parser.add_option('-P', '--print',
0077                   dest='print_level', default=2,
0078                   help='Set dd4hep print level.',
0079                   metavar='<int>')
0080 parser.add_option('-p', '--position',
0081                   dest='position', default='0.0,0.0,0.0',
0082                   help='Start position of the material scan. [give tuple "x,y,z" as string]',
0083                   metavar='<tuple>')
0084 parser.add_option('-d', '--direction',
0085                   dest='direction', default='0.0,1.0,0.0',
0086                   help='Direction of the material scan. [give tuple "x,y,z" as string]',
0087                   metavar='<tuple>')
0088 
0089 (opts, args) = parser.parse_args()
0090 
0091 if opts.compact is None:
0092   logger.info("%s", parser.format_help())
0093   sys.exit(1)
0094 
0095 opts.position = eval('(' + opts.position + ')')
0096 opts.direction = eval('(' + opts.direction + ')')
0097 printOpts(opts)
0098 
0099 try:
0100   from ROOT import gROOT
0101   gROOT.SetBatch(1)
0102 except ImportError as X:
0103   logger.error('PyROOT interface not accessible: %s', X)
0104   logger.info(parser.format_help())
0105   sys.exit(errno.ENOENT)
0106 
0107 try:
0108   import DDG4
0109   import g4units
0110 except ImportError as X:
0111   logger.error('DDG4 python interface not accessible: %s', X)
0112   logger.info(parser.format_help())
0113   sys.exit(errno.ENOENT)
0114 #
0115 ret = materialScan(opts)
0116 sys.exit(ret)