Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-19 08:32:03

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 logging
0016 import DDG4
0017 from DDG4 import OutputLevel as Output
0018 from g4units import keV, GeV, mm, ns, MeV
0019 #
0020 global geant4
0021 geant4 = None
0022 
0023 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
0024 logger = logging.getLogger(__name__)
0025 #
0026 """
0027 
0028    dd4hep simulation example setup using the python configuration
0029 
0030    @author  M.Frank
0031    @version 1.0
0032 
0033 """
0034 
0035 
0036 def setupWorker():
0037   k = DDG4.Kernel()
0038   kernel = k.worker()
0039   logger.info('PYTHON: +++ Creating Geant4 worker thread ....')
0040 
0041   # Configure Run actions
0042   run1 = DDG4.RunAction(kernel, 'Geant4TestRunAction/RunInit')
0043   run1.Property_int = 12345
0044   run1.Property_double = -5e15 * keV
0045   run1.Property_string = 'Startrun: Hello_2'
0046   logger.info("%s %f %d", run1.Property_string, run1.Property_double, run1.Property_int)
0047   run1.enableUI()
0048   kernel.registerGlobalAction(run1)
0049   kernel.runAction().adopt(run1)
0050 
0051   # Configure Event actions
0052   prt = DDG4.EventAction(kernel, 'Geant4ParticlePrint/ParticlePrint')
0053   prt.OutputLevel = Output.DEBUG
0054   prt.OutputType = 3  # Print both: table and tree
0055   kernel.eventAction().adopt(prt)
0056 
0057   # Configure Event actions
0058   prt = DDG4.EventAction(kernel, 'Geant4SurfaceTest/SurfaceTest')
0059   prt.OutputLevel = Output.INFO
0060   kernel.eventAction().adopt(prt)
0061 
0062   # Configure I/O
0063   evt_lcio = geant4.setupLCIOOutput('LcioOutput', 'CLICSiD_' + time.strftime('%Y-%m-%d_%H-%M'))
0064   evt_lcio.OutputLevel = Output.DEBUG
0065 
0066   # evt_root = geant4.setupROOTOutput('RootOutput','CLICSiD_'+time.strftime('%Y-%m-%d_%H-%M'))
0067   # generator_output_level = Output.INFO
0068 
0069   gen = DDG4.GeneratorAction(kernel, "Geant4GeneratorActionInit/GenerationInit")
0070   kernel.generatorAction().adopt(gen)
0071 
0072   # VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
0073   """
0074   Generation of isotrope tracks of a given multiplicity with overlay:
0075   """
0076   # First particle generator: pi+
0077   gen = DDG4.GeneratorAction(kernel, "Geant4IsotropeGenerator/IsotropPi+")
0078   gen.Particle = 'pi+'
0079   gen.Energy = 100 * GeV
0080   gen.Multiplicity = 2
0081   gen.Mask = 1
0082   gen.OutputLevel = Output.DEBUG
0083   gen.PhiMin = 0
0084   gen.PhiMax = 0
0085   gen.ThetaMin = 1.61
0086   gen.ThetaMax = 1.61
0087 
0088   kernel.generatorAction().adopt(gen)
0089   # Install vertex smearing for this interaction
0090   gen = DDG4.GeneratorAction(kernel, "Geant4InteractionVertexSmear/SmearPi+")
0091   gen.Mask = 1
0092   gen.Offset = (20 * mm, 10 * mm, 10 * mm, 0 * ns)
0093   gen.Sigma = (4 * mm, 1 * mm, 1 * mm, 0 * ns)
0094   kernel.generatorAction().adopt(gen)
0095   """
0096   # Second particle generator: e-
0097   gen = DDG4.GeneratorAction(kernel,"Geant4IsotropeGenerator/IsotropE-");
0098   gen.Particle = 'e-'
0099   gen.Energy = 25 * GeV
0100   gen.Multiplicity = 3
0101   gen.Mask = 2
0102   gen.OutputLevel = Output.DEBUG
0103   kernel.generatorAction().adopt(gen)
0104   # Install vertex smearing for this interaction
0105   gen = DDG4.GeneratorAction(kernel,"Geant4InteractionVertexSmear/SmearE-");
0106   gen.Mask = 2
0107   gen.Offset = (-20*mm, -10*mm, -10*mm, 0*ns)
0108   gen.Sigma = (12*mm, 8*mm, 8*mm, 0*ns)
0109   kernel.generatorAction().adopt(gen)
0110   #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0111   """
0112   # Merge all existing interaction records
0113   gen = DDG4.GeneratorAction(kernel, "Geant4InteractionMerger/InteractionMerger")
0114   gen.OutputLevel = 4  # generator_output_level
0115   gen.enableUI()
0116   kernel.generatorAction().adopt(gen)
0117 
0118   # Finally generate Geant4 primaries
0119   gen = DDG4.GeneratorAction(kernel, "Geant4PrimaryHandler/PrimaryHandler")
0120   gen.OutputLevel = Output.DEBUG  # generator_output_level
0121   gen.enableUI()
0122   kernel.generatorAction().adopt(gen)
0123 
0124   # And handle the simulation particles.
0125   part = DDG4.GeneratorAction(kernel, "Geant4ParticleHandler/ParticleHandler")
0126   kernel.generatorAction().adopt(part)
0127   # part.SaveProcesses = ['conv','Decay']
0128   part.SaveProcesses = ['Decay']
0129   part.MinimalKineticEnergy = 100 * MeV
0130   part.OutputLevel = Output.DEBUG  # generator_output_level
0131   part.enableUI()
0132   user = DDG4.Action(kernel, "Geant4TCUserParticleHandler/UserParticleHandler")
0133   user.TrackingVolume_Zmax = DDG4.EcalEndcap_zmin
0134   user.TrackingVolume_Rmax = DDG4.EcalBarrel_rmin
0135   user.enableUI()
0136   part.adopt(user)
0137   logger.info('PYTHON: +++ Geant4 worker thread configured successfully....')
0138   return 1
0139 
0140 
0141 def setupMaster():
0142   logger.info('PYTHON: +++ Setting up master thread.....')
0143   return 1
0144 
0145 
0146 def setupSensitives():
0147   # First the tracking detectors
0148   seq, act = geant4.setupTracker('SiVertexBarrel')
0149   act.OutputLevel = Output.ERROR
0150   act.CollectSingleDeposits = False
0151   seq, act = geant4.setupTracker('SiVertexEndcap')
0152   act.OutputLevel = Output.ERROR
0153   act.CollectSingleDeposits = False
0154   logger.info('PYTHON: +++ Setting up Geant4 sensitive detectors for worker thread.....')
0155   return 1
0156 
0157 
0158 def dummy_sd():
0159   logger.info('PYTHON: +++ Setting up DUMMY Geant4 sensitive detectors for worker thread.....')
0160   return 1
0161 
0162 
0163 def dummy_geom():
0164   logger.info('PYTHON: +++ Setting up DUMMY Geant4 geometry for worker thread.....')
0165   return 1
0166 
0167 
0168 def run():
0169   global geant4
0170   kernel = DDG4.Kernel()
0171   description = kernel.detectorDescription()
0172   install_dir = os.environ['DD4hepINSTALL']
0173   kernel.loadGeometry(str("file:" + install_dir + "/DDDetectors/compact/SiD_Markus.xml"))
0174   DDG4.importConstants(description)
0175   DDG4.Core.setPrintLevel(Output.DEBUG)
0176   DDG4.Core.setPrintFormat(str("%-32s %6s %s"))
0177 
0178   kernel.NumberOfThreads = 1
0179   geant4 = DDG4.Geant4(kernel, tracker='Geant4TrackerWeightedAction')
0180   geant4.printDetectors()
0181   # Configure UI
0182   geant4.setupCshUI()
0183 
0184   # Geant4 user initialization action
0185   geant4.addUserInitialization(worker=setupWorker, master=setupMaster)
0186 
0187   # Configure G4 geometry setup
0188   seq, act = geant4.addDetectorConstruction("Geant4DetectorGeometryConstruction/ConstructGeo")
0189 
0190   # Configure G4 magnetic field tracking
0191   self.setupTrackingFieldMT()  # noqa: F821
0192 
0193   seq, act = geant4.addDetectorConstruction("Geant4PythonDetectorConstruction/DummyDet",
0194                                             geometry=dummy_geom,
0195                                             sensitives=dummy_sd)
0196   # Configure G4 sensitive detectors
0197   seq, act = geant4.addDetectorConstruction("Geant4PythonDetectorConstruction/SetupSD",
0198                                             sensitives=setupSensitives)
0199 
0200   # Configure G4 sensitive detectors
0201   seq, act = geant4.addDetectorConstruction("Geant4DetectorSensitivesConstruction/ConstructSD",
0202                                             allow_threads=True)
0203 
0204   # Setup random generator
0205   rndm = DDG4.Action(kernel, 'Geant4Random/Random')
0206   rndm.Seed = 987654321
0207   rndm.initialize()
0208 
0209   # Setup global filters fur use in sensntive detectors
0210   f1 = DDG4.Filter(kernel, 'GeantinoRejectFilter/GeantinoRejector')
0211   kernel.registerGlobalFilter(f1)
0212 
0213   # seq,act = geant4.setupTracker('SiTrackerBarrel')
0214   # seq,act = geant4.setupTracker('SiTrackerEndcap')
0215   # seq,act = geant4.setupTracker('SiTrackerForward')
0216   # Now the calorimeters
0217   # seq,act = geant4.setupCalorimeter('EcalBarrel')
0218   # seq,act = geant4.setupCalorimeter('EcalEndcap')
0219   # seq,act = geant4.setupCalorimeter('HcalBarrel')
0220   # seq,act = geant4.setupCalorimeter('HcalEndcap')
0221   # seq,act = geant4.setupCalorimeter('HcalPlug')
0222   # seq,act = geant4.setupCalorimeter('MuonBarrel')
0223   # seq,act = geant4.setupCalorimeter('MuonEndcap')
0224   # seq,act = geant4.setupCalorimeter('LumiCal')
0225   # seq,act = geant4.setupCalorimeter('BeamCal')
0226 
0227   # Now build the physics list:
0228   seq = geant4.setupPhysics('QGSP_BERT')
0229   phys = DDG4.PhysicsList(geant4.master(), 'Geant4PhysicsList/MyPhysics')
0230   part = DDG4.Action(geant4.master(), 'Geant4ExtraParticles/extraparts')
0231   part.pdgfile = 'checkout/DDG4/examples/particle.tbl'
0232   phys.adoptPhysicsConstructor(part.get())
0233   seq.add(phys)
0234 
0235   geant4.run()
0236 
0237   # kernel.configure()
0238   # kernel.initialize()
0239 
0240   # DDG4.setPrintLevel(Output.DEBUG)
0241   # kernel.run()
0242   # kernel.terminate()
0243   return 1
0244 
0245 
0246 if __name__ == "__main__":
0247   import sys
0248   logger.info('Arguments: %s', str(sys.argv))
0249   run()