Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:17:19

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 logging
0017 import DDG4
0018 from DDG4 import OutputLevel as Output
0019 from g4units import keV, GeV, mm, ns, MeV
0020 #
0021 global geant4
0022 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
0023 logger = logging.getLogger(__name__)
0024 #
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 setupWorker():
0036   k = DDG4.Kernel()
0037   kernel = k.worker()
0038   logger.info('PYTHON: +++ Creating Geant4 worker thread ....')
0039 
0040   # Configure Run actions
0041   run1 = DDG4.RunAction(kernel, 'Geant4TestRunAction/RunInit')
0042   run1.Property_int = 12345
0043   run1.Property_double = -5e15 * keV
0044   run1.Property_string = 'Startrun: Hello_2'
0045   logger.info("%s %f %d", run1.Property_string, run1.Property_double, run1.Property_int)
0046   run1.enableUI()
0047   kernel.registerGlobalAction(run1)
0048   kernel.runAction().adopt(run1)
0049 
0050   # Configure Event actions
0051   prt = DDG4.EventAction(kernel, 'Geant4ParticlePrint/ParticlePrint')
0052   prt.OutputLevel = Output.DEBUG
0053   prt.OutputType = 3  # Print both: table and tree
0054   kernel.eventAction().adopt(prt)
0055 
0056   # Configure Event actions
0057   prt = DDG4.EventAction(kernel, 'Geant4SurfaceTest/SurfaceTest')
0058   prt.OutputLevel = Output.INFO
0059   kernel.eventAction().adopt(prt)
0060 
0061   # Configure I/O
0062   evt_lcio = geant4.setupLCIOOutput('LcioOutput', 'CLICSiD_' + time.strftime('%Y-%m-%d_%H-%M'))
0063   evt_lcio.OutputLevel = Output.DEBUG
0064 
0065   # evt_root = geant4.setupROOTOutput('RootOutput','CLICSiD_'+time.strftime('%Y-%m-%d_%H-%M'))
0066   # generator_output_level = Output.INFO
0067 
0068   gen = DDG4.GeneratorAction(kernel, "Geant4GeneratorActionInit/GenerationInit")
0069   kernel.generatorAction().adopt(gen)
0070 
0071   # VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
0072   """
0073   Generation of isotrope tracks of a given multiplicity with overlay:
0074   """
0075   # First particle generator: pi+
0076   gen = DDG4.GeneratorAction(kernel, "Geant4IsotropeGenerator/IsotropPi+")
0077   gen.Particle = 'pi+'
0078   gen.Energy = 100 * GeV
0079   gen.Multiplicity = 2
0080   gen.Mask = 1
0081   gen.OutputLevel = Output.DEBUG
0082   gen.PhiMin = 0
0083   gen.PhiMax = 0
0084   gen.ThetaMin = 1.61
0085   gen.ThetaMax = 1.61
0086 
0087   kernel.generatorAction().adopt(gen)
0088   # Install vertex smearing for this interaction
0089   gen = DDG4.GeneratorAction(kernel, "Geant4InteractionVertexSmear/SmearPi+")
0090   gen.Mask = 1
0091   gen.Offset = (20 * mm, 10 * mm, 10 * mm, 0 * ns)
0092   gen.Sigma = (4 * mm, 1 * mm, 1 * mm, 0 * ns)
0093   kernel.generatorAction().adopt(gen)
0094   """
0095   # Second particle generator: e-
0096   gen = DDG4.GeneratorAction(kernel,"Geant4IsotropeGenerator/IsotropE-");
0097   gen.Particle = 'e-'
0098   gen.Energy = 25 * GeV
0099   gen.Multiplicity = 3
0100   gen.Mask = 2
0101   gen.OutputLevel = Output.DEBUG
0102   kernel.generatorAction().adopt(gen)
0103   # Install vertex smearing for this interaction
0104   gen = DDG4.GeneratorAction(kernel,"Geant4InteractionVertexSmear/SmearE-");
0105   gen.Mask = 2
0106   gen.Offset = (-20*mm, -10*mm, -10*mm, 0*ns)
0107   gen.Sigma = (12*mm, 8*mm, 8*mm, 0*ns)
0108   kernel.generatorAction().adopt(gen)
0109   #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0110   """
0111   # Merge all existing interaction records
0112   gen = DDG4.GeneratorAction(kernel, "Geant4InteractionMerger/InteractionMerger")
0113   gen.OutputLevel = 4  # generator_output_level
0114   gen.enableUI()
0115   kernel.generatorAction().adopt(gen)
0116 
0117   # Finally generate Geant4 primaries
0118   gen = DDG4.GeneratorAction(kernel, "Geant4PrimaryHandler/PrimaryHandler")
0119   gen.OutputLevel = Output.DEBUG  # generator_output_level
0120   gen.enableUI()
0121   kernel.generatorAction().adopt(gen)
0122 
0123   # And handle the simulation particles.
0124   part = DDG4.GeneratorAction(kernel, "Geant4ParticleHandler/ParticleHandler")
0125   kernel.generatorAction().adopt(part)
0126   # part.SaveProcesses = ['conv','Decay']
0127   part.SaveProcesses = ['Decay']
0128   part.MinimalKineticEnergy = 100 * MeV
0129   part.OutputLevel = Output.DEBUG  # generator_output_level
0130   part.enableUI()
0131   user = DDG4.Action(kernel, "Geant4TCUserParticleHandler/UserParticleHandler")
0132   user.TrackingVolume_Zmax = DDG4.EcalEndcap_zmin
0133   user.TrackingVolume_Rmax = DDG4.EcalBarrel_rmin
0134   user.enableUI()
0135   part.adopt(user)
0136   logger.info('PYTHON: +++ Geant4 worker thread configured successfully....')
0137   return 1
0138 
0139 
0140 def setupMaster():
0141   logger.info('PYTHON: +++ Setting up master thread.....')
0142   return 1
0143 
0144 
0145 def setupSensitives():
0146   global geant4
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()