forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgnn_module_map_odd.py
More file actions
277 lines (241 loc) · 7.97 KB
/
gnn_module_map_odd.py
File metadata and controls
277 lines (241 loc) · 7.97 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python3
"""
Example: GNN track finding with module maps on ODD.
Full simulation pipeline using module map (geometry-based) graph construction on the
OpenDataDetector. Demonstrates the complete workflow from event generation to track finding.
All parameters are hardcoded except file paths. Users should copy and modify this script
for their specific needs.
"""
from pathlib import Path
import os
import acts
from acts import UnitConstants as u
from acts.examples import Sequencer
from acts.examples.odd import getOpenDataDetector
from acts.examples.simulation import (
addParticleGun,
EtaConfig,
PhiConfig,
MomentumConfig,
ParticleConfig,
addFatras,
addDigitization,
addDigiParticleSelection,
ParticleSelectorConfig,
)
from acts.examples.reconstruction import addGnn, addSpacePointsMaking
from acts.gnn import (
ModuleMapCuda,
CudaTrackBuilding,
Device,
)
from acts.examples.gnn import NodeFeature
def runGnnModuleMap(
trackingGeometry,
field,
geometrySelection,
stripGeometrySelection,
digiConfigFile,
moduleMapPath,
gnnModel,
outputDir,
events=100,
s=None,
):
"""
Run GNN tracking with module maps on ODD.
This example shows the full simulation chain with module map-based GNN.
All GNN parameters are hardcoded for reproducibility.
Args:
trackingGeometry: Tracking geometry
field: Magnetic field
geometrySelection: Geometry selection JSON file path
digiConfigFile: Digitization config file path
moduleMapPath: Path prefix for module map files
(will load .doublets.root and .triplets.root)
gnnModel: Path to trained model (.pt, .onnx, or .engine)
outputDir: Output directory for performance files
events: Number of events to process
s: Optional sequencer (creates new one if None)
"""
# Validate inputs
assert Path(
moduleMapPath + ".doublets.root"
).exists(), f"Module map not found: {moduleMapPath}.doublets.root"
assert Path(
moduleMapPath + ".triplets.root"
).exists(), f"Module map not found: {moduleMapPath}.triplets.root"
assert Path(gnnModel).exists(), f"Model file not found: {gnnModel}"
s = s or Sequencer(events=events, numThreads=1)
# Random number generator
rnd = acts.examples.RandomNumbers(seed=42)
addParticleGun(
s,
MomentumConfig(1.0 * u.GeV, 10.0 * u.GeV, transverse=True),
EtaConfig(-3.0, 3.0, uniform=True),
PhiConfig(0.0, 360.0 * u.degree),
ParticleConfig(10, acts.PdgParticle.eMuon, randomizeCharge=True),
vtxGen=acts.examples.GaussianVertexGenerator(
mean=acts.Vector4(0, 0, 0, 0),
stddev=acts.Vector4(0.0125 * u.mm, 0.0125 * u.mm, 55.5 * u.mm, 1.0 * u.ns),
),
multiplicity=50,
rnd=rnd,
)
# FATRAS simulation
addFatras(
s,
trackingGeometry,
field,
rnd=rnd,
enableInteractions=True,
outputDirRoot=None,
outputDirCsv=None,
outputDirObj=None,
logLevel=acts.logging.INFO,
)
# Digitization
addDigitization(
s,
trackingGeometry,
field,
digiConfigFile=digiConfigFile,
rnd=rnd,
logLevel=acts.logging.INFO,
)
addDigiParticleSelection(
s,
ParticleSelectorConfig(
pt=(1.0 * u.GeV, None),
eta=(-3.0, 3.0),
measurements=(7, None),
removeNeutral=True,
),
)
addSpacePointsMaking(
s,
trackingGeometry,
geoSelectionConfigFile=geometrySelection,
stripGeoSelectionConfigFile=stripGeometrySelection,
logLevel=acts.logging.INFO,
)
moduleMapConfig = {
"level": acts.logging.INFO,
"moduleMapPath": moduleMapPath,
"rScale": 1000.0,
"phiScale": 3.141592654,
"zScale": 1000.0,
"etaScale": 1.0,
"gpuDevice": 0,
"gpuBlocks": 512,
}
graphConstructor = ModuleMapCuda(**moduleMapConfig)
gnnModel = Path(gnnModel)
edgeClassifierConfig = {
"level": acts.logging.INFO,
"modelPath": str(gnnModel),
"cut": 0.5,
}
if gnnModel.suffix == ".pt":
edgeClassifierConfig["useEdgeFeatures"] = True
from acts.gnn import TorchEdgeClassifier
edgeClassifiers = [TorchEdgeClassifier(**edgeClassifierConfig)]
elif gnnModel.suffix == ".onnx":
from acts.gnn import OnnxEdgeClassifier
edgeClassifierConfig["device"] = Device.Cuda()
edgeClassifiers = [OnnxEdgeClassifier(**edgeClassifierConfig)]
elif gnnModel.suffix == ".engine":
from acts.gnn import TensorRTEdgeClassifier
edgeClassifiers = [TensorRTEdgeClassifier(**edgeClassifierConfig)]
else:
raise ValueError(f"Unsupported model format: {gnnModel.suffix}")
trackBuilderConfig = {
"level": acts.logging.INFO,
"useOneBlockImplementation": False,
"doJunctionRemoval": True,
}
trackBuilder = CudaTrackBuilding(**trackBuilderConfig)
e = NodeFeature
nodeFeatures = [
e.R,
e.Phi,
e.Z,
e.Eta,
e.Cluster1R,
e.Cluster1Phi,
e.Cluster1Z,
e.Cluster1Eta,
e.Cluster2R,
e.Cluster2Phi,
e.Cluster2Z,
e.Cluster2Eta,
]
featureScales = [1000.0, 3.141592654, 1000.0, 1.0] * 3
addGnn(
s,
graphConstructor=graphConstructor,
edgeClassifiers=edgeClassifiers,
trackBuilder=trackBuilder,
nodeFeatures=nodeFeatures,
featureScales=featureScales,
inputClusters="clusters",
outputDirRoot=str(outputDir),
logLevel=acts.logging.INFO,
)
protoTracksToSeeds = acts.examples.ProtoTracksToSeeds(
level=acts.logging.INFO,
inputProtoTracks="gnn-protoTracks",
inputSpacePoints="spacepoints",
outputSeeds="gnn-seeds",
outputProtoTracks="gnn-protoTracks-seeds-filtered",
)
s.addAlgorithm(protoTracksToSeeds)
parEstAlg = acts.examples.TrackParamsEstimationAlgorithm(
level=acts.logging.INFO,
inputSeeds=protoTracksToSeeds.config.outputSeeds,
outputTrackParameters="gnn-initial-parameters",
trackingGeometry=trackingGeometry,
magneticField=field,
)
s.addAlgorithm(parEstAlg)
s.run()
return s
if __name__ == "__main__":
# Setup detector (ODD)
detector = getOpenDataDetector()
trackingGeometry = detector.trackingGeometry()
decorators = detector.contextDecorators()
# Magnetic field
field = acts.ConstantBField(acts.Vector3(0, 0, 2 * u.T))
# Configuration files
srcdir = Path(__file__).resolve().parent.parent.parent.parent
geometrySelection = srcdir / "Examples/Configs/odd-seeding-config.json"
assert geometrySelection.exists(), f"File not found: {geometrySelection}"
stripGeometrySelection = (
srcdir / "Examples/Configs/odd-strip-spacepoint-selection.json"
)
assert srcdir.exists(), f"File not found: {stripGeometrySelection}"
digiConfigFile = srcdir / "Examples/Configs/odd-digi-smearing-config.json"
assert digiConfigFile.exists(), f"File not found: {digiConfigFile}"
# Model paths from MODEL_STORAGE environment variable
model_storage = os.environ.get("MODEL_STORAGE")
assert model_storage is not None, "MODEL_STORAGE environment variable is not set"
ci_models_odd = Path(model_storage)
moduleMapPath = str(
ci_models_odd / "module_map_odd_2k_events.1e-03.float.v1_3_PATCH"
)
gnnModel = str(ci_models_odd / "gnn_odd_module_map.pt")
outputDir = Path.cwd()
events = 100
# Run the workflow
runGnnModuleMap(
trackingGeometry=trackingGeometry,
field=field,
geometrySelection=geometrySelection,
stripGeometrySelection=stripGeometrySelection,
digiConfigFile=digiConfigFile,
moduleMapPath=moduleMapPath,
gnnModel=gnnModel,
outputDir=outputDir,
events=events,
)