Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 07:52:52

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