Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
material_mapping.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file material_mapping.py
1 #!/usr/bin/env python3
2 import os
3 
4 from acts.examples import (
5  Sequencer,
6  WhiteBoard,
7  AlgorithmContext,
8  ProcessCode,
9  RootMaterialTrackReader,
10  RootMaterialTrackWriter,
11  MaterialMapping,
12  JsonMaterialWriter,
13  JsonFormat,
14 )
15 
16 import acts
17 from acts import (
18  Vector4,
19  UnitConstants as u,
20  SurfaceMaterialMapper,
21  VolumeMaterialMapper,
22  Navigator,
23  Propagator,
24  StraightLineStepper,
25  MaterialMapJsonConverter,
26 )
27 from common import getOpenDataDetectorDirectory
28 from acts.examples.odd import getOpenDataDetector
29 
30 
32  trackingGeometry,
33  decorators,
34  outputDir,
35  inputDir,
36  mapName="material-map",
37  mapSurface=True,
38  mapVolume=True,
39  readCachedSurfaceInformation=False,
40  mappingStep=1,
41  s=None,
42 ):
43  s = s or Sequencer(numThreads=1)
44 
45  for decorator in decorators:
46  s.addContextDecorator(decorator)
47 
48  wb = WhiteBoard(acts.logging.INFO)
49 
50  context = AlgorithmContext(0, 0, wb)
51 
52  for decorator in decorators:
53  assert decorator.decorate(context) == ProcessCode.SUCCESS
54 
55  # Read material step information from a ROOT TTRee
56  s.addReader(
57  RootMaterialTrackReader(
58  level=acts.logging.INFO,
59  collection="material-tracks",
60  fileList=[
61  os.path.join(
62  inputDir,
63  mapName + "_tracks.root"
64  if readCachedSurfaceInformation
65  else "geant4_material_tracks.root",
66  )
67  ],
68  readCachedSurfaceInformation=readCachedSurfaceInformation,
69  )
70  )
71 
72  stepper = StraightLineStepper()
73 
74  mmAlgCfg = MaterialMapping.Config(context.geoContext, context.magFieldContext)
75  mmAlgCfg.trackingGeometry = trackingGeometry
76  mmAlgCfg.collection = "material-tracks"
77 
78  if mapSurface:
79  navigator = Navigator(
80  trackingGeometry=trackingGeometry,
81  resolveSensitive=True,
82  resolveMaterial=True,
83  resolvePassive=True,
84  )
85  propagator = Propagator(stepper, navigator)
86  mapper = SurfaceMaterialMapper(level=acts.logging.INFO, propagator=propagator)
87  mmAlgCfg.materialSurfaceMapper = mapper
88 
89  if mapVolume:
90  navigator = Navigator(
91  trackingGeometry=trackingGeometry,
92  )
93  propagator = Propagator(stepper, navigator)
94  mapper = VolumeMaterialMapper(
95  level=acts.logging.INFO, propagator=propagator, mappingStep=mappingStep
96  )
97  mmAlgCfg.materialVolumeMapper = mapper
98 
99  jmConverterCfg = MaterialMapJsonConverter.Config(
100  processSensitives=True,
101  processApproaches=True,
102  processRepresenting=True,
103  processBoundaries=True,
104  processVolumes=True,
105  context=context.geoContext,
106  )
107 
108  jmw = JsonMaterialWriter(
109  level=acts.logging.VERBOSE,
110  converterCfg=jmConverterCfg,
111  fileName=os.path.join(outputDir, mapName),
112  writeFormat=JsonFormat.Json,
113  )
114 
115  mmAlgCfg.materialWriters = [jmw]
116 
117  s.addAlgorithm(MaterialMapping(level=acts.logging.INFO, config=mmAlgCfg))
118 
119  s.addWriter(
120  RootMaterialTrackWriter(
121  level=acts.logging.INFO,
122  collection=mmAlgCfg.mappingMaterialCollection,
123  filePath=os.path.join(
124  outputDir,
125  mapName + "_tracks.root",
126  ),
127  storeSurface=True,
128  storeVolume=True,
129  )
130  )
131 
132  return s
133 
134 
135 if "__main__" == __name__:
136  matDeco = acts.IMaterialDecorator.fromFile("geometry-map.json")
137  detector, trackingGeometry, decorators = getOpenDataDetector(
139  )
140 
142  trackingGeometry,
143  decorators,
144  outputDir=os.getcwd(),
145  inputDir=os.getcwd(),
146  readCachedSurfaceInformation=False,
147  ).run()