Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: geant4 python example #1334

Merged
merged 24 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Examples/Scripts/Python/fatras.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ def addFatras(
s.addAlgorithm(alg)

# Output
addFatrasWriters(s, outputDirCsv, outputDirRoot)

return s


def addFatrasWriters(
s: acts.examples.Sequencer,
outputDirCsv: Optional[Union[Path, str]] = None,
outputDirRoot: Optional[Union[Path, str]] = None,
) -> acts.examples.Sequencer:
if outputDirCsv is not None:
outputDirCsv = Path(outputDirCsv)
if not outputDirCsv.exists():
Expand Down
121 changes: 121 additions & 0 deletions Examples/Scripts/Python/geant4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/usr/bin/env python3
from typing import Optional, Union
from pathlib import Path

import acts
import acts.examples
import acts.examples.dd4hep
from acts.examples.geant4 import Geant4Simulation, geant4SimulationConfig
from acts.examples.geant4.dd4hep import DDG4DetectorConstruction
from common import getOpenDataDetector
from fatras import addFatrasWriters

u = acts.UnitConstants


def addGeant4(
s: acts.examples.Sequencer,
geometryService: acts.examples.dd4hep.DD4hepGeometryService,
trackingGeometry: acts.TrackingGeometry,
field: acts.MagneticFieldProvider,
outputDirCsv: Optional[Union[Path, str]] = None,
outputDirRoot: Optional[Union[Path, str]] = None,
seed: Optional[int] = None,
preselectParticles: bool = True,
) -> acts.examples.Sequencer:
"""This function steers the detector simulation using Geant4

Parameters
----------
s: Sequencer
the sequencer module to which we add the Geant4 steps (returned from addGeant4)
trackingGeometry : tracking geometry
field : magnetic field
outputDirCsv : Path|str, path, None
the output folder for the Csv output, None triggers no output
outputDirRoot : Path|str, path, None
the output folder for the Root output, None triggers no output
seed : int, None
random number generator seed
"""

if int(s.config.logLevel) <= int(acts.logging.DEBUG):
acts.examples.dump_args_calls(locals())

# Selector
if preselectParticles:
particles_selected = "particles_selected"
s.addAlgorithm(
acts.examples.ParticleSelector(
level=s.config.logLevel,
inputParticles="particles_input",
outputParticles=particles_selected,
)
)
else:
particles_selected = "particles_input"

g4detector = DDG4DetectorConstruction(geometryService)
g4conf = geant4SimulationConfig(
level=s.config.logLevel,
detector=g4detector,
inputParticles="particles_input",
trackingGeometry=trackingGeometry,
magneticField=field,
)
g4conf.outputSimHits = "simhits"
g4conf.outputParticlesInitial = "particles_initial"
g4conf.outputParticlesFinal = "particles_final"
g4conf.seed = seed

# Simulation
alg = Geant4Simulation(
level=s.config.logLevel,
config=g4conf,
)

# Sequencer
s.addAlgorithm(alg)

# Output
addFatrasWriters(s, None, None)
andiwand marked this conversation as resolved.
Show resolved Hide resolved

return s


def runGeant4(
geometryService,
trackingGeometry,
field,
outputDir,
s: acts.examples.Sequencer = None,
):
from particle_gun import addParticleGun, EtaConfig

s = s or acts.examples.Sequencer(events=100, numThreads=1)
s.config.logLevel = acts.logging.INFO
rnd = acts.examples.RandomNumbers()
seed = 42
s = addParticleGun(
s,
EtaConfig(-2.0, 2.0),
rnd=rnd,
)
outputDir = Path(outputDir)
return addGeant4(
s,
geometryService,
trackingGeometry,
field,
outputDirCsv=outputDir / "csv",
outputDirRoot=outputDir,
seed=seed,
)


if "__main__" == __name__:
detector, trackingGeometry, decorators = getOpenDataDetector()

field = acts.ConstantBField(acts.Vector3(0, 0, 2 * u.T))

runGeant4(detector.geometryService, trackingGeometry, field, Path.cwd()).run()