Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
material_recording.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file material_recording.py
1 #!/usr/bin/env python3
2 import os
3 import warnings
4 from pathlib import Path
5 import argparse
6 
7 import acts
8 from acts.examples import (
9  GaussianVertexGenerator,
10  ParametricParticleGenerator,
11  FixedMultiplicityGenerator,
12  EventGenerator,
13  RandomNumbers,
14 )
15 
19 from common import getOpenDataDetectorDirectory
20 from acts.examples.odd import getOpenDataDetector
21 from acts.examples.geant4 import GdmlDetectorConstructionFactory
22 
23 u = acts.UnitConstants
24 
25 _material_recording_executed = False
26 
27 
29  detectorConstructionFactory,
30  outputDir,
31  tracksPerEvent=10000,
32  s=None,
33  etaRange=(-4, 4),
34 ):
35  global _material_recording_executed
36  if _material_recording_executed:
37  warnings.warn("Material recording already ran in this process. Expect crashes")
38  _material_recording_executed = True
39 
40  rnd = RandomNumbers(seed=228)
41 
42  evGen = EventGenerator(
43  level=acts.logging.INFO,
44  generators=[
45  EventGenerator.Generator(
46  multiplicity=FixedMultiplicityGenerator(n=1),
47  vertex=GaussianVertexGenerator(
48  stddev=acts.Vector4(0, 0, 0, 0),
49  mean=acts.Vector4(0, 0, 0, 0),
50  ),
51  particles=ParametricParticleGenerator(
52  pdg=acts.PdgParticle.eInvalid,
53  charge=0,
54  randomizeCharge=False,
55  mass=0,
56  p=(1 * u.GeV, 10 * u.GeV),
57  eta=etaRange,
58  numParticles=tracksPerEvent,
59  etaUniform=True,
60  ),
61  )
62  ],
63  outputParticles="particles_initial",
64  randomNumbers=rnd,
65  )
66 
67  s.addReader(evGen)
68 
69  g4Alg = acts.examples.geant4.Geant4MaterialRecording(
70  level=acts.logging.INFO,
71  detectorConstructionFactory=detectorConstructionFactory,
72  randomNumbers=rnd,
73  inputParticles=evGen.config.outputParticles,
74  outputMaterialTracks="material_tracks",
75  )
76 
77  s.addAlgorithm(g4Alg)
78 
79  s.addWriter(
80  acts.examples.RootMaterialTrackWriter(
81  prePostStep=True,
82  recalculateTotals=True,
83  collection="material_tracks",
84  filePath=os.path.join(outputDir, "geant4_material_tracks.root"),
85  level=acts.logging.INFO,
86  )
87  )
88 
89  return s
90 
91 
92 def main():
93 
94  p = argparse.ArgumentParser()
95  p.add_argument(
96  "-n", "--events", type=int, default=1000, help="Number of events to generate"
97  )
98  p.add_argument(
99  "-t", "--tracks", type=int, default=100, help="Particle tracks per event"
100  )
101  p.add_argument(
102  "-i", "--input", type=str, default="", help="GDML input file (optional)"
103  )
104 
105  args = p.parse_args()
106 
107  detectorConstructionFactory = None
108  if args.input != "":
109  detectorConstructionFactory = (
110  acts.examples.geant4.GdmlDetectorConstructionFactory(args.input)
111  )
112  else:
113  detector, trackingGeometry, decorators = getOpenDataDetector(
115  )
116 
117  detectorConstructionFactory = (
118  acts.examples.geant4.dd4hep.DDG4DetectorConstructionFactory(detector)
119  )
120 
122  detectorConstructionFactory=detectorConstructionFactory,
123  tracksPerEvent=args.tracks,
124  outputDir=os.getcwd(),
125  s=acts.examples.Sequencer(events=args.events, numThreads=1),
126  ).run()
127 
128 
129 if "__main__" == __name__:
130  main()