Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 08:18:18

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 import os
0014 import time
0015 import DDG4
0016 from DDG4 import OutputLevel as Output
0017 from g4units import GeV, MeV, m
0018 #
0019 #
0020 """
0021 
0022    dd4hep simulation example setup using the python configuration
0023 
0024    @author  M.Frank
0025    @version 1.0
0026 
0027 """
0028 
0029 
0030 def run():
0031   args = DDG4.CommandLine()
0032   kernel = DDG4.Kernel()
0033   logger = DDG4.Logger('BoxOfStraws')
0034   install_dir = os.environ['DD4hepExamplesINSTALL']
0035 
0036   if args.sensitive:
0037     kernel.loadGeometry(str('file:' + install_dir + '/examples/ClientTests/compact/BoxOfStraws_sensitive.xml'))
0038   else:
0039     kernel.loadGeometry(str('file:' + install_dir + '/examples/ClientTests/compact/BoxOfStraws.xml'))
0040 
0041   DDG4.importConstants(kernel.detectorDescription(), debug=False)
0042   geant4 = DDG4.Geant4(kernel)
0043   geant4.printDetectors()
0044   # Configure UI
0045   if args.macro:
0046     ui = geant4.setupCshUI(macro=args.macro)
0047   else:
0048     ui = geant4.setupCshUI()
0049 
0050   if args.verbose:
0051     ui.Commands.append('/run/verbose ' + str(args.verbose))
0052   if args.batch:
0053     ui.Commands.append('/run/beamOn ' + str(args.events))
0054 
0055   # Terminate sequence
0056   if args.batch:
0057     ui.Commands.append('/ddg4/UI/terminate')
0058   #
0059   # Configure field
0060   geant4.setupTrackingField(prt=True)
0061   #
0062   prt = DDG4.EventAction(kernel, 'Geant4ParticlePrint/ParticlePrint')
0063   prt.OutputLevel = Output.DEBUG
0064   prt.OutputType = 3  # Print both: table and tree
0065   kernel.eventAction().adopt(prt)
0066   #
0067   seq, act = geant4.addDetectorConstruction('Geant4DetectorConstructionResources/ResourcesBeforeConstruction')
0068   act.When = "geometry"
0069   #
0070   # Configure G4 geometry setup
0071   seq, act = geant4.addDetectorConstruction('Geant4DetectorGeometryConstruction/ConstructGeo')
0072   act.DebugVolumes = True
0073   #
0074   if args.sensitive:
0075     # Assign sensitive detectors according to the declarations 'tracker' or 'calorimeter', etc
0076     seq, act = geant4.addDetectorConstruction('Geant4DetectorSensitivesConstruction/ConstructSD')
0077   else:
0078     # Assign sensitive detectors in Geant4 by matching a regular expression in the detector sub-tree
0079     seq, act = geant4.addDetectorConstruction('Geant4RegexSensitivesConstruction/ConstructSDRegEx')
0080     act.Detector = 'BoxOfStrawsDet'
0081     act.OutputLevel = Output.ALWAYS
0082     act.Match = ['gas_']
0083   #
0084   seq, act = geant4.addDetectorConstruction('Geant4DetectorConstructionResources/ResourcesAfterConstruction')
0085   act.When = "sensitives"
0086   #
0087   # Configure I/O
0088   geant4.setupROOTOutput('RootOutput', 'BoxOfStraws_' + time.strftime('%Y-%m-%d_%H-%M'))
0089   #
0090   # Setup particle gun
0091   gun = geant4.setupGun('Gun', particle='pi+', energy=10 * GeV, multiplicity=1)
0092   gun.OutputLevel = Output.INFO
0093   gun.enableUI()
0094   #
0095   # And handle the simulation particles.
0096   part = DDG4.GeneratorAction(kernel, 'Geant4ParticleHandler/ParticleHandler')
0097   kernel.generatorAction().adopt(part)
0098   part.SaveProcesses = ['Decay']
0099   part.MinimalKineticEnergy = 50 * MeV
0100   user = DDG4.Action(kernel, 'Geant4TCUserParticleHandler/UserParticleHandler')
0101   user.TrackingVolume_Zmax = 2.5 * m
0102   user.TrackingVolume_Rmax = 2.5 * m
0103   part.adopt(user)
0104   #
0105   # Map sensitive detectors of type 'BoxOfStraws' to Geant4CalorimeterAction
0106   sd = geant4.description.sensitiveDetector(str('BoxOfStrawsDet'))
0107   logger.info(f'+++ BoxOfStraws: SD type: {str(sd.type())}')
0108   energy_filter = DDG4.Filter(kernel, 'EnergyDepositMinimumCut')
0109   energy_filter.Cut = 1.0 * MeV
0110   energy_filter.enableUI()
0111   kernel.registerGlobalFilter(energy_filter)
0112   seq, act = geant4.setupDetector(name='BoxOfStrawsDet', action='MyTrackerSDAction')
0113   seq.adopt(energy_filter)
0114   act.HaveCellID = False
0115   #
0116   # Now build the physics list:
0117   phys = geant4.setupPhysics(str('QGSP_BERT'))
0118   phys.dump()
0119   if args.simultate:
0120     geant4.execute()
0121   else:
0122     geant4.kernel().configure()
0123     geant4.kernel().initialize()
0124     geant4.kernel().terminate()
0125 
0126 
0127 if __name__ == "__main__":
0128   run()