File indexing completed on 2025-08-05 08:10:09
0001
0002
0003 import os
0004 import warnings
0005 import argparse
0006
0007 import acts
0008 from acts.examples import (
0009 GaussianVertexGenerator,
0010 ParametricParticleGenerator,
0011 FixedMultiplicityGenerator,
0012 EventGenerator,
0013 RandomNumbers,
0014 )
0015 import acts.examples.dd4hep
0016 import acts.examples.geant4
0017 import acts.examples.geant4.dd4hep
0018 from acts.examples.odd import getOpenDataDetector
0019
0020 u = acts.UnitConstants
0021
0022 _material_recording_executed = False
0023
0024
0025 def runMaterialRecording(
0026 detectorConstructionFactory,
0027 outputDir,
0028 tracksPerEvent=10000,
0029 s=None,
0030 etaRange=(-4, 4),
0031 ):
0032 global _material_recording_executed
0033 if _material_recording_executed:
0034 warnings.warn("Material recording already ran in this process. Expect crashes")
0035 _material_recording_executed = True
0036
0037 rnd = RandomNumbers(seed=228)
0038
0039 evGen = EventGenerator(
0040 level=acts.logging.INFO,
0041 generators=[
0042 EventGenerator.Generator(
0043 multiplicity=FixedMultiplicityGenerator(n=1),
0044 vertex=GaussianVertexGenerator(
0045 stddev=acts.Vector4(0, 0, 0, 0),
0046 mean=acts.Vector4(0, 0, 0, 0),
0047 ),
0048 particles=ParametricParticleGenerator(
0049 pdg=acts.PdgParticle.eInvalid,
0050 charge=0,
0051 randomizeCharge=False,
0052 mass=0,
0053 p=(1 * u.GeV, 10 * u.GeV),
0054 eta=etaRange,
0055 numParticles=tracksPerEvent,
0056 etaUniform=True,
0057 ),
0058 )
0059 ],
0060 outputParticles="particles_initial",
0061 outputVertices="vertices_initial",
0062 randomNumbers=rnd,
0063 )
0064
0065 s.addReader(evGen)
0066
0067 g4Alg = acts.examples.geant4.Geant4MaterialRecording(
0068 level=acts.logging.INFO,
0069 detectorConstructionFactory=detectorConstructionFactory,
0070 randomNumbers=rnd,
0071 inputParticles=evGen.config.outputParticles,
0072 outputMaterialTracks="material_tracks",
0073 )
0074
0075 s.addAlgorithm(g4Alg)
0076
0077 s.addWriter(
0078 acts.examples.RootMaterialTrackWriter(
0079 prePostStep=True,
0080 recalculateTotals=True,
0081 inputMaterialTracks="material_tracks",
0082 filePath=os.path.join(outputDir, "geant4_material_tracks.root"),
0083 level=acts.logging.INFO,
0084 )
0085 )
0086
0087 return s
0088
0089
0090 def main():
0091 p = argparse.ArgumentParser()
0092 p.add_argument(
0093 "-n", "--events", type=int, default=1000, help="Number of events to generate"
0094 )
0095 p.add_argument(
0096 "-t", "--tracks", type=int, default=100, help="Particle tracks per event"
0097 )
0098 p.add_argument(
0099 "-i", "--input", type=str, default="", help="GDML input file (optional)"
0100 )
0101
0102 args = p.parse_args()
0103
0104 detectorConstructionFactory = None
0105 if args.input != "":
0106 detectorConstructionFactory = (
0107 acts.examples.geant4.GdmlDetectorConstructionFactory(args.input)
0108 )
0109 else:
0110 detector, trackingGeometry, decorators = getOpenDataDetector()
0111
0112 detectorConstructionFactory = (
0113 acts.examples.geant4.dd4hep.DDG4DetectorConstructionFactory(detector)
0114 )
0115
0116 runMaterialRecording(
0117 detectorConstructionFactory=detectorConstructionFactory,
0118 tracksPerEvent=args.tracks,
0119 outputDir=os.getcwd(),
0120 s=acts.examples.Sequencer(events=args.events, numThreads=1),
0121 ).run()
0122
0123
0124 if "__main__" == __name__:
0125 main()