Skip to content
Snippets Groups Projects
g4MaterialScan.py 3.9 KiB
Newer Older
#!/bin/python
#==========================================================================
Markus Frank's avatar
Markus Frank committed
#  AIDA Detector description implementation 
#--------------------------------------------------------------------------
# Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
# All rights reserved.
#
# For the licensing terms see $DD4hepINSTALL/LICENSE.
# For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
#
#==========================================================================

logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
def printOpts(opts):
  o = eval(str(opts))
  prefix = sys.argv[0].split(os.sep)[-1]
  for name,value in o.items():
    logging.info('%s > %-18s %s  [%s]',prefix,name+':',str(value),str(value.__class__))

def materialScan(opts):
  kernel = DDG4.Kernel()
  install_dir = os.environ['DD4hepINSTALL']
  kernel.loadGeometry(opts.compact)
  DDG4.Core.setPrintFormat("%-32s %6s %s")
  geant4 = DDG4.Geant4(kernel)
  # Configure UI
  geant4.setupCshUI(ui=None)
Markus Frank's avatar
Markus Frank committed
  for i in geant4.description.detectors():
    o = DDG4.DetElement(i.second.ptr())
Markus Frank's avatar
Markus Frank committed
    sd = geant4.description.sensitiveDetector(o.name())
    if sd.isValid():
      typ = sd.type()
      if geant4.sensitive_types.has_key(typ):
        geant4.setupDetector(o.name(),geant4.sensitive_types[typ])
        sdtyp = geant4.sensitive_types[typ]
      else:
        logging.info('+++  %-32s type:%-12s  --> Unknown Sensitive type: %s',o.name(), typ, sdtyp)
        sys.exit(errno.EINVAL)

  gun = geant4.setupGun("Gun",
                        Standalone=True,
                        particle='geantino',
                        energy=20*SystemOfUnits.GeV,
                        position=opts.position,
                        direction=opts.direction,
                        multiplicity=1,
                        isotrop=False )
  scan = DDG4.SteppingAction(kernel,'Geant4MaterialScanner/MaterialScan')
  kernel.steppingAction().adopt(scan)

  # Now build the physics list:
  phys = geant4.setupPhysics('QGSP_BERT')
  """
  phys = geant4.setupPhysics('')
  ph = DDG4.PhysicsList(kernel,'Geant4PhysicsList/Myphysics')
  ph.addParticleConstructor('G4Geantino')
  ph.addParticleConstructor('G4BosonConstructor')
  ph.addParticleConstructor('G4LeptonConstructor')
  phys.transportation = True
  phys.decays = True
  phys.adopt(ph) 
  """

  kernel.configure()
  kernel.initialize()
  kernel.NumEvents = 1
  kernel.run()
  kernel.terminate()
  return 0
  
parser = optparse.OptionParser()
parser.formatter.width = 132
parser.description = 'Material scan using Geant4.'
parser.add_option('-c', '--compact', dest='compact', default=None,
                  help='Define LCCDD style compact xml input',
		  metavar='<FILE>')
parser.add_option('-P', '--print',
		  dest='print_level', default=2,
Markus Frank's avatar
Markus Frank committed
                  help='Set dd4hep print level.',
		  metavar='<int>')
parser.add_option('-p', '--position',
		  dest='position', default='0.0,0.0,0.0',
                  help='Start position of the material scan. [give tuple "x,y,z" as string]',
		  metavar='<tuple>')
parser.add_option('-d', '--direction',
		  dest='direction', default='0.0,1.0,0.0',
                  help='Direction of the material scan. [give tuple "x,y,z" as string]',
		  metavar='<tuple>')

(opts, args) = parser.parse_args()

if opts.compact is None:
  sys.exit(1)

opts.position=eval('('+opts.position+')')
opts.direction=eval('('+opts.direction+')')
printOpts(opts)

try:
  import ROOT
  from ROOT import gROOT
  gROOT.SetBatch(1)
except ImportError,X:
  logging.info('PyROOT interface not accessible: %s',str(X))
  logging.info(parser.format_help())
  sys.exit(errno.ENOENT)

try:
  import DDG4, SystemOfUnits
except ImportError,X:
  logging.info('DDG4 python interface not accessible: %s',str(X))
  logging.info(parser.format_help())
  sys.exit(errno.ENOENT)
#
ret = materialScan(opts)
sys.exit(ret);