forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmaterial_mapping_itk.py
More file actions
173 lines (146 loc) · 4.48 KB
/
material_mapping_itk.py
File metadata and controls
173 lines (146 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
import os
import argparse
from pathlib import Path
import acts
from acts import (
SurfaceMaterialMapper,
Navigator,
Propagator,
StraightLineStepper,
MaterialMapJsonConverter,
)
from acts.examples import (
Sequencer,
WhiteBoard,
AlgorithmContext,
ProcessCode,
MaterialMapping,
)
from acts.examples.root import (
RootMaterialTrackReader,
RootMaterialTrackWriter,
)
from acts.examples.json import (
JsonMaterialWriter,
JsonFormat,
)
def runMaterialMapping(
trackingGeometry,
decorators,
outputDir,
inputDir,
materialStepsFile,
mapName="material-map",
mapSurface=True,
readCachedSurfaceInformation=False,
dumpMaterialTracks=False,
s=None,
):
s = s or Sequencer(numThreads=1)
for decorator in decorators:
s.addContextDecorator(decorator)
wb = WhiteBoard(acts.logging.INFO)
context = AlgorithmContext(0, 0, wb)
for decorator in decorators:
assert decorator.decorate(context) == ProcessCode.SUCCESS
# Read material step information from a ROOT TTRee
s.addReader(
RootMaterialTrackReader(
level=acts.logging.INFO,
outputMaterialTracks="material_tracks",
fileList=[
os.path.join(
inputDir,
materialStepsFile,
)
],
readCachedSurfaceInformation=readCachedSurfaceInformation,
)
)
stepper = StraightLineStepper()
mmAlgCfg = MaterialMapping.Config(context.geoContext, context.magFieldContext)
mmAlgCfg.trackingGeometry = trackingGeometry
mmAlgCfg.inputMaterialTracks = "material_tracks"
if mapSurface:
navigator = Navigator(
trackingGeometry=trackingGeometry,
resolveSensitive=False,
resolveMaterial=True,
resolvePassive=True,
)
propagator = Propagator(stepper, navigator)
mapper = SurfaceMaterialMapper(level=acts.logging.INFO, propagator=propagator)
mmAlgCfg.materialSurfaceMapper = mapper
jmConverterCfg = MaterialMapJsonConverter.Config(
processSensitives=False,
processNonMaterial=False,
processApproaches=True,
processRepresenting=True,
processBoundaries=True,
processVolumes=True,
context=context.geoContext,
)
jmw = JsonMaterialWriter(
level=acts.logging.VERBOSE,
converterCfg=jmConverterCfg,
fileName=os.path.join(outputDir, mapName),
writeFormat=JsonFormat.Json,
)
if dumpMaterialTracks:
s.addWriter(
RootMaterialTrackWriter(
level=acts.logging.INFO,
inputMaterialTracks=mmAlgCfg.collection,
filePath=os.path.join(
outputDir,
mapName + "_tracks.root",
),
storeSurface=True,
storeVolume=True,
)
)
mmAlgCfg.materialWriters = [jmw]
s.addAlgorithm(MaterialMapping(level=acts.logging.INFO, config=mmAlgCfg))
return s
if "__main__" == __name__:
p = argparse.ArgumentParser(
description="Script to run material mapping on ITk geometry"
)
p.add_argument(
"geo_dir",
help="Input directory containing the ITk standalone geometry. Get in touch if you don't have this.",
)
p.add_argument(
"--inputFile",
type=str,
default="",
help="Input file containing material steps.",
)
p.add_argument(
"--material",
type=str,
default="",
help="Geometry file to define layers used in material mapping",
)
args = p.parse_args()
geo_example_dir = Path(args.geo_dir)
assert geo_example_dir.exists(), "Detector example input directory missing"
assert os.path.exists(
args.inputFile
), "Invalid file in --inputFile. Please check your input!"
assert os.path.exists(
args.material
), "Invalid file path/name in --material. Please check your input!"
from acts.examples.itk import buildITkGeometry
detector = buildITkGeometry(geo_example_dir, customMaterialFile=args.material)
trackingGeometry = detector.trackingGeometry()
decorators = detector.contextDecorators()
runMaterialMapping(
trackingGeometry,
decorators,
materialStepsFile=args.inputFile,
outputDir=os.getcwd(),
inputDir=os.getcwd(),
readCachedSurfaceInformation=False,
).run()