Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-24 09:24:58

0001 # ==========================================================================
0002 #  AIDA Detector description implementation
0003 # --------------------------------------------------------------------------
0004 # Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 # All rights reserved.
0006 #
0007 # For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 # For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 #
0010 # ==========================================================================
0011 #
0012 #
0013 from __future__ import absolute_import, unicode_literals
0014 import os
0015 import sys
0016 import time
0017 import logging
0018 import DDG4
0019 from DDG4 import OutputLevel as Output
0020 from g4units import MeV, GeV, m, mm
0021 #
0022 #
0023 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
0024 logger = logging.getLogger(__name__)
0025 """
0026 
0027    dd4hep simulation example setup using the python configuration
0028 
0029    @author  M.Frank
0030    @version 1.0
0031 
0032 """
0033 
0034 
0035 def show_help():
0036   logging.info("Check_shape.py -option [-option]                           ")
0037   logging.info("       -vis                          Enable visualization  ")
0038   logging.info("       -batch                        Batch execution       ")
0039 
0040 
0041 def run():
0042   hlp = False
0043   vis = False
0044   dump = False
0045   batch = False
0046   install_dir = os.environ['DD4hepINSTALL']
0047   #
0048   for i in list(range(len(sys.argv))):
0049     c = sys.argv[i].upper()
0050     if c.find('BATCH') < 2 and c.find('BATCH') >= 0:
0051       batch = True
0052     elif c[:4] == '-VIS':
0053       vis = True
0054     elif c[:4] == '-DUM':
0055       dump = True
0056     elif c[:2] == '-H':
0057       hlp = True
0058 
0059   if hlp:
0060     show_help()
0061     sys.exit(1)
0062 
0063   kernel = DDG4.Kernel()
0064   description = kernel.detectorDescription()
0065   install_dir = os.environ['DD4hepExamplesINSTALL']
0066   geant4 = DDG4.Geant4(kernel, tracker='Geant4TrackerCombineAction')
0067   #
0068   logger.info("#  Configure UI")
0069   ui = None
0070   if batch:
0071     geant4.setupCshUI(ui=None, vis=None)
0072     kernel.UI = 'UI'
0073   else:
0074     ui = geant4.setupCshUI(vis=vis)
0075 
0076   kernel.loadGeometry(str("file:" + install_dir + "/examples/ClientTests/compact/NestedBoxReflection.xml"))
0077   DDG4.importConstants(description)
0078 
0079   geant4.printDetectors()
0080   if dump:
0081     seq, act = geant4.addDetectorConstruction("Geant4DetectorGeometryConstruction/ConstructGeo")
0082     act.DebugReflections = True
0083     act.DebugMaterials = False
0084     act.DebugElements = False
0085     act.DebugVolumes = False
0086     act.DebugShapes = False
0087     act.DumpHierarchy = ~0x0
0088 
0089   logger.info("#  Configure G4 magnetic field tracking")
0090   geant4.setupTrackingField()
0091 
0092   logger.info("#  Setup random generator")
0093   rndm = DDG4.Action(kernel, 'Geant4Random/Random')
0094   rndm.Seed = 987654321
0095   rndm.initialize()
0096   #
0097   logger.info("#  Configure Event actions")
0098   prt = DDG4.EventAction(kernel, 'Geant4ParticlePrint/ParticlePrint')
0099   prt.OutputType = 3  # Print both: table and tree
0100   prt.OutputLevel = Output.INFO
0101   kernel.eventAction().adopt(prt)
0102   #
0103   logger.info("#  Configure I/O")
0104   geant4.setupROOTOutput('RootOutput', 'BoxReflect_' + time.strftime('%Y-%m-%d_%H-%M'))
0105   #
0106   gen = DDG4.GeneratorAction(kernel, "Geant4GeneratorActionInit/GenerationInit")
0107   gen.enableUI()
0108   kernel.generatorAction().adopt(gen)
0109   #
0110   logger.info("#  Generation of isotrope tracks of a given multiplicity with overlay:")
0111   gen = DDG4.GeneratorAction(kernel, "Geant4ParticleGun/IsotropE+")
0112   gen.mask = 4
0113   gen.isotrop = True
0114   gen.particle = 'e+'
0115   gen.Energy = 100 * GeV
0116   gen.multiplicity = 200
0117   gen.position = (0 * m, 0 * m, 0 * m)
0118   gen.direction = (0, 0, 1.)
0119   gen.distribution = 'uniform'
0120   gen.standalone = False
0121   # gen.PhiMin = 0.0*rad
0122   # gen.PhiMax = 2.0*math.pi*rad
0123   # gen.ThetaMin = 0.0*math.pi*rad
0124   # gen.ThetaMax = 1.0*math.pi*rad
0125   gen.enableUI()
0126   kernel.generatorAction().adopt(gen)
0127   #
0128   logger.info("#  Merge all existing interaction records")
0129   gen = DDG4.GeneratorAction(kernel, "Geant4InteractionMerger/InteractionMerger")
0130   gen.OutputLevel = 4  # generator_output_level
0131   gen.enableUI()
0132   kernel.generatorAction().adopt(gen)
0133   #
0134   logger.info("#  Finally generate Geant4 primaries")
0135   gen = DDG4.GeneratorAction(kernel, "Geant4PrimaryHandler/PrimaryHandler")
0136   gen.OutputLevel = 4  # generator_output_level
0137   gen.enableUI()
0138   kernel.generatorAction().adopt(gen)
0139   #
0140   logger.info("#  ....and handle the simulation particles.")
0141   part = DDG4.GeneratorAction(kernel, "Geant4ParticleHandler/ParticleHandler")
0142   kernel.generatorAction().adopt(part)
0143   # part.SaveProcesses = ['conv','Decay']
0144   part.SaveProcesses = ['Decay']
0145   part.MinimalKineticEnergy = 100 * MeV
0146   part.OutputLevel = 5  # generator_output_level
0147   part.enableUI()
0148   user = DDG4.Action(kernel, "Geant4TCUserParticleHandler/UserParticleHandler")
0149   user.TrackingVolume_Zmax = 3.0 * m
0150   user.TrackingVolume_Rmax = 3.0 * m
0151   user.enableUI()
0152   part.adopt(user)
0153   #
0154   logger.info("#  Now setup the calorimeters")
0155   seq, actions = geant4.setupDetectors()
0156   #
0157   logger.info("#  Now build the physics list:")
0158   geant4.setupPhysics('QGSP_BERT')
0159   ph = geant4.addPhysics(str('Geant4PhysicsList/Myphysics'))
0160   ph.addPhysicsConstructor(str('G4StepLimiterPhysics'))
0161   #
0162   # Add special particle types from specialized physics constructor
0163   part = geant4.addPhysics('Geant4ExtraParticles/ExtraParticles')
0164   part.pdgfile = os.path.join(install_dir, 'examples/DDG4/examples/particle.tbl')
0165   #
0166   # Add global range cut
0167   rg = geant4.addPhysics('Geant4DefaultRangeCut/GlobalRangeCut')
0168   rg.RangeCut = 0.7 * mm
0169   #
0170   #
0171   if ui and vis:
0172     cmds = []
0173     cmds.append('/control/verbose 2')
0174     cmds.append('/run/initialize')
0175     cmds.append('/vis/open OGL')
0176     cmds.append('/vis/verbose errors')
0177     cmds.append('/vis/drawVolume')
0178     cmds.append('/vis/viewer/set/viewpointThetaPhi 55. 45.')
0179     cmds.append('/vis/scene/add/axes 0 0 0 3 m')
0180     ui.Commands = cmds
0181 
0182   kernel.configure()
0183   kernel.initialize()
0184 
0185   # DDG4.setPrintLevel(Output.DEBUG)
0186   kernel.run()
0187   kernel.terminate()
0188 
0189 
0190 if __name__ == "__main__":
0191   run()