Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-26 08:47:15

0001 """ddsim steering file for a material recording with geantinos.
0002 
0003 Shoots geantinos through the Geant4 geometry that DDG4 builds from a DD4hep
0004 detector and records the material they cross with the `ActsMaterialRecorder`
0005 DDG4 stepping action. The recorded material tracks are written to
0006 `--outputFile` by the `ActsMaterialTrackWriter` event action, in the format the
0007 ACTS material mapping reads, instead of the usual simulation output.
0008 
0009 The `ActsPluginDD4hepGeant4` library has to be on `LD_LIBRARY_PATH`, e.g.
0010 by sourcing `this_acts.sh`. Usage:
0011 
0012     ddsim --steeringFile material_recording_ddsim.py \\
0013           --compactFile detector.xml \\
0014           --numberOfEvents 1000 \\
0015           --outputFile geant4_material_tracks.root
0016 
0017 Gun settings can be overridden on the command line, e.g. `--gun.etaMin`,
0018 `--gun.etaMax`, `--gun.multiplicity` or `--random.seed`. The recording can be
0019 limited to a cylinder, e.g. at the calorimeter face, with
0020 `--outputConfig.recordingMaxR`, `--outputConfig.recordingMinZ` and
0021 `--outputConfig.recordingMaxZ` in mm. Multi-threaded
0022 running with `--numberOfThreads`, available from DD4hep 1.38, writes a single
0023 output file.
0024 """
0025 
0026 from DDSim.DD4hepSimulation import DD4hepSimulation
0027 from g4units import GeV
0028 
0029 SIM = DD4hepSimulation()
0030 SIM.runType = "batch"
0031 SIM.outputFile = "geant4_material_tracks.root"
0032 
0033 SIM.enableGun = True
0034 SIM.gun.particle = "geantino"
0035 SIM.gun.multiplicity = 1000
0036 SIM.gun.distribution = "eta"
0037 SIM.gun.etaMin = -4.0
0038 SIM.gun.etaMax = 4.0
0039 SIM.gun.momentumMin = 1 * GeV
0040 SIM.gun.momentumMax = 1 * GeV
0041 SIM.gun.position = (0.0, 0.0, 0.0)
0042 
0043 # Limit the recording to a cylinder around the beam axis, e.g. to stop at the
0044 # calorimeter face, as radius and z range in mm. The z range does not need to
0045 # be symmetric. Infinite means no limit.
0046 SIM.outputConfig.recordingMaxR = float("inf")
0047 SIM.outputConfig.recordingMinZ = float("-inf")
0048 SIM.outputConfig.recordingMaxZ = float("inf")
0049 
0050 
0051 def materialRecordingOutput(dd4hepSimulation):
0052     """Record material tracks to the output file, instead of the simulation output"""
0053     # ddsim runs the steering file such that functions only see their own
0054     # imports
0055     import math
0056 
0057     from DDG4 import EventAction, Kernel, SteppingAction
0058     from g4units import mm
0059 
0060     kernel = Kernel().worker()
0061 
0062     # One recorder per worker thread, collecting the material tracks per event
0063     recorder = SteppingAction(kernel, "ActsMaterialRecorder/MaterialRecorder")
0064     recorder.ExcludeMaterials = ["Air", "Vacuum"]
0065     recorder.RecordElementFractions = False
0066     # Only finite bounds are passed on, the recorder defaults to no limit
0067     for name in ("MaxR", "MinZ", "MaxZ"):
0068         value = float(getattr(dd4hepSimulation.outputConfig, "recording" + name))
0069         if math.isfinite(value):
0070             setattr(recorder, name, value * mm)
0071     kernel.steppingAction().add(recorder)
0072 
0073     # One writer, shared by all worker threads in multi-threaded running. The
0074     # kernel knows the number of threads independently of the ddsim version.
0075     shared = Kernel().NumberOfThreads > 1
0076     writer = EventAction(kernel, "ActsMaterialTrackWriter/MaterialTrackWriter", shared)
0077     writer.OutputFile = dd4hepSimulation.outputFile
0078     writer.TreeName = "material_tracks"
0079     kernel.eventAction().add(writer)
0080 
0081 
0082 SIM.outputConfig.userOutputPlugin = materialRecordingOutput