File indexing completed on 2026-11-26 09:25:03
0001 import multiprocessing
0002
0003 import pytest
0004
0005 from helpers import geant4Enabled
0006
0007
0008 def _region_creator(name, cut, volumes):
0009 from acts.examples.geant4 import RegionCreator
0010
0011 cfg = RegionCreator.Config()
0012 cfg.name = name
0013 cfg.gammaCut = cut
0014 cfg.electronCut = cut
0015 cfg.positronCut = cut
0016 cfg.protonCut = 0.7
0017 cfg.volumes = volumes
0018 return RegionCreator(cfg)
0019
0020
0021 @pytest.mark.skipif(not geant4Enabled, reason="Geant4 not set up")
0022 def test_region_creator_construction_options():
0023 from acts.examples.geant4 import Geant4ConstructionOptions
0024
0025 options = Geant4ConstructionOptions()
0026 options.regionCreators = [
0027 _region_creator("envelope", 1.0, ["Envelope Logic"]),
0028 _region_creator("layers", 0.01, ["Layer Logic"]),
0029 ]
0030
0031 assert [r.config.name for r in options.regionCreators] == [
0032 "envelope",
0033 "layers",
0034 ]
0035 assert options.regionCreators[1].config.electronCut == pytest.approx(0.01)
0036 assert options.regionCreators[1].config.volumes == ["Layer Logic"]
0037
0038
0039 def _run_telescope_simulation(outputDir, layerCut):
0040 import acts
0041 import acts.examples
0042 from acts.examples.geant4 import Geant4ConstructionOptions
0043 from acts.examples.simulation import (
0044 EtaConfig,
0045 ParticleConfig,
0046 PhiConfig,
0047 addGeant4,
0048 addParticleGun,
0049 )
0050
0051 u = acts.UnitConstants
0052
0053 detector = acts.examples.TelescopeDetector(
0054 acts.examples.TelescopeDetector.Config(
0055 bounds=[200, 200], positions=[30, 60, 90, 120, 150], stereos=[0] * 5
0056 )
0057 )
0058 trackingGeometry = detector.trackingGeometry()
0059 field = acts.ConstantBField(acts.Vector3(0, 0, 2 * u.T))
0060 rnd = acts.examples.RandomNumbers(seed=42)
0061
0062 options = Geant4ConstructionOptions()
0063 options.regionCreators = [
0064 _region_creator("telescope_envelope", 0.7, ["Envelope Logic"]),
0065 _region_creator("telescope_layers", layerCut, ["Layer Logic"]),
0066 ]
0067
0068 s = acts.examples.Sequencer(events=10, numThreads=1)
0069 addParticleGun(
0070 s,
0071 EtaConfig(4.0, 5.0),
0072 PhiConfig(0.0, 360.0 * u.degree),
0073 ParticleConfig(10, acts.PdgParticle.eMuon, True),
0074 multiplicity=5,
0075 rnd=rnd,
0076 )
0077 addGeant4(
0078 s,
0079 detector,
0080 trackingGeometry,
0081 field,
0082 rnd=rnd,
0083 outputDirCsv=outputDir,
0084 keepParticlesWithoutHits=True,
0085 killVolume=trackingGeometry.highestTrackingVolume,
0086 detectorConstructionOptions=options,
0087 )
0088 s.run()
0089
0090
0091 def _count_simulated_particles(outputDir, layerCut):
0092 outputDir.mkdir()
0093
0094
0095
0096 p = multiprocessing.get_context("spawn").Process(
0097 target=_run_telescope_simulation, args=(outputDir, layerCut)
0098 )
0099 p.start()
0100 p.join()
0101 assert p.exitcode == 0, "Geant4 telescope simulation failed"
0102
0103 files = sorted(outputDir.glob("*particles_simulated.csv"))
0104 assert len(files) == 10
0105 return sum(len(f.read_text().splitlines()) - 1 for f in files)
0106
0107
0108 @pytest.mark.skipif(not geant4Enabled, reason="Geant4 not set up")
0109 def test_region_creator_production_cuts(tmp_path):
0110
0111 nPrimaries = 500
0112
0113
0114
0115 nSmallCut = _count_simulated_particles(tmp_path / "small_cut", 1e-3)
0116 nLargeCut = _count_simulated_particles(tmp_path / "large_cut", 1e5)
0117
0118 assert nLargeCut >= nPrimaries
0119 assert nSmallCut > 2 * nLargeCut