forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhashing_seeding.py
More file actions
executable file
·413 lines (344 loc) · 11.4 KB
/
hashing_seeding.py
File metadata and controls
executable file
·413 lines (344 loc) · 11.4 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env python3
import argparse
import pathlib
import acts
import acts.examples
from acts.examples.simulation import (
addPythia8,
ParticleSelectorConfig,
addGenParticleSelection,
addFatras,
addDigitization,
addDigiParticleSelection,
)
from acts.examples.reconstruction import (
addSeeding,
addCKFTracks,
TrackSelectorConfig,
SeedingAlgorithm,
)
from acts.examples.root import (
RootSpacePointWriter,
)
from pathlib import Path
from typing import Optional
from enum import Enum
DetectorName = Enum("DetectorName", "ODD generic")
HashingMetric = Enum("HashingMetric", "dphi dR")
class Config:
def __init__(
self,
mu: int = None,
bucketSize: int = 100,
maxSeedsPerSpM: int = 10,
detector: DetectorName = DetectorName.generic,
seedingAlgorithm: SeedingAlgorithm = SeedingAlgorithm.HashingPrototype,
metric: str = HashingMetric.dphi,
annoySeed: int = 123456789,
zBins: int = 0,
phiBins: int = 100,
):
self.mu = mu
self.bucketSize = bucketSize
self.maxSeedsPerSpM = maxSeedsPerSpM
self.detector = detector
self.seedingAlgorithm = (
seedingAlgorithm
if type(seedingAlgorithm) == SeedingAlgorithm
else SeedingAlgorithm[seedingAlgorithm]
)
self.metric = metric if type(metric) == HashingMetric else HashingMetric[metric]
self.annoySeed = annoySeed
self.zBins = zBins
self.phiBins = phiBins
if seedingAlgorithm == SeedingAlgorithm.GridTriplet:
self.bucketSize = 0
self.metric = HashingMetric.dphi
self.annoySeed = 123456789
self.zBins = 0
self.phiBins = 0
def __str__(self):
return (
f"mu={self.mu}, bucketSize={self.bucketSize}, maxSeedsPerSpM={self.maxSeedsPerSpM}, "
f"detector={self.detector}, seedingAlgorithm={self.seedingAlgorithm}, metric={self.metric}, "
f"annoySeed={self.annoySeed}, zBins={self.zBins}, phiBins={self.phiBins}"
)
def __repr__(self):
return self.__str__()
def save(self, path: pathlib.Path):
with open(path, "w") as f:
f.write(str(self))
@property
def doHashing(self):
return self.bucketSize > 0
@property
def directory(self):
outDir = f"detector_{extractEnumName(self.detector)}"
outDir += "_output"
doHashing = self.bucketSize > 0
if doHashing:
outDir += "_hashing"
outDir += f"_mu_{self.mu}"
if doHashing:
outDir += f"_bucket_{self.bucketSize}"
outDir += f"_maxSeedsPerSpM_{self.maxSeedsPerSpM}"
outDir += f"_seedFinderConfig_{'TrackML' if self.detector == DetectorName.generic else 'ODD'}"
outDir += f"_seedingAlgorithm_{extractEnumName(self.seedingAlgorithm)}Seeding"
if doHashing:
if self.metric != "angular":
outDir += f"_metric_{extractEnumName(self.metric)}"
outDir += f"_annoySeed_{self.annoySeed}"
if self.zBins != 0:
outDir += f"_zBins_{self.zBins}"
if self.phiBins != 0:
outDir += f"_phiBins_{self.phiBins}"
return outDir
def getDetectorInfo(self):
actsExamplesDir = Path(__file__).parent.parent.parent
if self.detector == DetectorName.ODD:
from acts.examples.odd import (
getOpenDataDetector,
getOpenDataDetectorDirectory,
)
geoDir = getOpenDataDetectorDirectory()
oddMaterialMap = geoDir / "data/odd-material-maps.root"
oddDigiConfig = actsExamplesDir / "Configs/odd-digi-smearing-config.json"
oddSeedingSel = actsExamplesDir / "Configs/odd-seeding-config.json"
oddMaterialDeco = acts.IMaterialDecorator.fromFile(oddMaterialMap)
detector = getOpenDataDetector(
odd_dir=geoDir, materialDecorator=oddMaterialDeco
)
trackingGeometry = detector.trackingGeometry()
digiConfig = oddDigiConfig
geoSelectionConfigFile = oddSeedingSel
elif self.detector == DetectorName.generic:
print("Create detector and tracking geometry")
detector = acts.examples.GenericDetector()
trackingGeometry = detector.trackingGeometry()
digiConfig = actsExamplesDir / "Configs/generic-digi-smearing-config.json"
geoSelectionConfigFile = (
actsExamplesDir / "Configs/generic-seeding-config.json"
)
else:
exit("Detector not supported")
return detector, trackingGeometry, digiConfig, geoSelectionConfigFile
def extractEnumName(enumvar):
return str(enumvar).split(".")[-1]
def runHashingSeeding(
nevents: int,
trackingGeometry: acts.TrackingGeometry,
field: acts.MagneticFieldProvider,
outputDir: pathlib.Path,
saveFiles: bool,
npileup: int,
seedingAlgorithm: SeedingAlgorithm,
maxSeedsPerSpM: int,
digiConfig: pathlib.Path,
geoSelectionConfigFile: pathlib.Path,
eta=4,
rnd: acts.examples.RandomNumbers = acts.examples.RandomNumbers(seed=42),
config: Config = None,
s=None,
):
from acts.examples.reconstruction import (
SeedFinderConfigArg,
SeedFinderOptionsArg,
HashingTrainingConfigArg,
HashingAlgorithmConfigArg,
)
u = acts.UnitConstants
outputDir = Path(outputDir)
s = s or acts.examples.Sequencer(
events=nevents,
numThreads=1,
outputDir=str(outputDir),
trackFpes=False,
)
addPythia8(
s,
hardProcess=["Top:qqbar2ttbar=on"],
npileup=npileup,
vtxGen=acts.examples.GaussianVertexGenerator(
stddev=acts.Vector4(0, 0, 50 * u.mm, 0),
mean=acts.Vector4(0, 0, 0, 0),
),
rnd=rnd,
)
addGenParticleSelection(
s,
ParticleSelectorConfig(
rho=(0.0, 24 * u.mm),
absZ=(0.0, 1.0 * u.m),
),
)
addFatras(
s,
trackingGeometry,
field,
enableInteractions=True,
# outputDirRoot=outputDir, # RootParticle ERROR when setting the outputDirRoot
outputDirCsv=outputDir if saveFiles else None,
rnd=rnd,
)
addDigitization(
s,
trackingGeometry,
field,
digiConfigFile=digiConfig,
outputDirRoot=outputDir,
outputDirCsv=outputDir if saveFiles else None,
rnd=rnd,
)
addDigiParticleSelection(
s,
ParticleSelectorConfig(
pt=(1.0 * u.GeV, None),
eta=(-eta, eta),
measurements=(9, None),
removeNeutral=True,
),
)
import numpy as np
cotThetaMax = 1 / (np.tan(2 * np.arctan(np.exp(-eta)))) # =1/tan(2×atan(e^(-eta)))
seedFinderConfigArg = SeedFinderConfigArg(
r=(None, 200 * u.mm), # rMin=default, 33mm
deltaR=(1 * u.mm, 300 * u.mm),
collisionRegion=(-250 * u.mm, 250 * u.mm),
z=(-2000 * u.mm, 2000 * u.mm),
maxSeedsPerSpM=maxSeedsPerSpM,
sigmaScattering=5,
radLengthPerSeed=0.1,
minPt=500 * u.MeV,
impactMax=3 * u.mm,
cotThetaMax=cotThetaMax,
)
seedFinderOptionsArg: SeedFinderOptionsArg = SeedFinderOptionsArg(bFieldInZ=2 * u.T)
hashingTrainingConfigArg: HashingTrainingConfigArg = HashingTrainingConfigArg(
annoySeed=config.annoySeed, f=2 if config.metric == HashingMetric.dR else 1
)
hashingAlgorithmConfigArg: HashingAlgorithmConfigArg = HashingAlgorithmConfigArg(
bucketSize=config.bucketSize,
zBins=config.zBins,
phiBins=config.phiBins,
)
initialSigmas: Optional[list] = [
1 * u.mm,
1 * u.mm,
1 * u.degree,
1 * u.degree,
0.1 / u.GeV,
1 * u.ns,
]
initialVarInflation: Optional[list] = [1.0] * 6
addSeeding(
s,
trackingGeometry,
field,
seedFinderConfigArg,
seedFinderOptionsArg,
hashingTrainingConfigArg,
hashingAlgorithmConfigArg,
seedingAlgorithm=seedingAlgorithm,
geoSelectionConfigFile=geoSelectionConfigFile,
initialSigmas=initialSigmas,
initialVarInflation=initialVarInflation,
outputDirRoot=outputDir,
outputDirCsv=outputDir if saveFiles else None,
)
return s
if __name__ == "__main__":
eta = 3
parser = argparse.ArgumentParser(
description="Example script to run seed finding with hashing"
)
parser.add_argument("--mu", type=int, default=50)
parser.add_argument("--bucketSize", type=int, default=100)
parser.add_argument(
"--nevents",
type=int,
default=20,
)
parser.add_argument("--maxSeedsPerSpM", type=int, default=10)
parser.add_argument("--seedingAlgorithm", type=str, default="HashingPrototype")
parser.add_argument("--saveFiles", type=bool, default=True)
parser.add_argument("--annoySeed", type=int, default=123456789)
parser.add_argument("--zBins", type=int, default=0)
parser.add_argument("--phiBins", type=int, default=100)
parser.add_argument("--metric", type=str, default="dphi")
args = parser.parse_args()
print(args)
mu = args.mu
bucketSize = args.bucketSize
nevents = args.nevents
saveFiles = args.saveFiles
annoySeed = args.annoySeed
zBins = args.zBins
phiBins = args.phiBins
maxSeedsPerSpM = args.maxSeedsPerSpM
metric = HashingMetric[args.metric]
seedingAlgorithm = SeedingAlgorithm[args.seedingAlgorithm]
detector = DetectorName.generic
u = acts.UnitConstants
config = Config(
mu=mu,
bucketSize=bucketSize,
maxSeedsPerSpM=maxSeedsPerSpM,
detector=detector,
seedingAlgorithm=seedingAlgorithm,
metric=metric,
annoySeed=annoySeed,
zBins=zBins,
phiBins=phiBins,
)
doHashing = config.doHashing
bucketSize = config.bucketSize
npileup = config.mu
maxSeedsPerSpM = config.maxSeedsPerSpM
outputDir = pathlib.Path.cwd() / config.directory
if not outputDir.exists():
outputDir.mkdir(parents=True)
config.save(outputDir / "config_file.txt")
field = acts.ConstantBField(acts.Vector3(0.0, 0.0, 2.0 * u.T))
rnd = acts.examples.RandomNumbers(seed=42)
_, trackingGeometry, digiConfig, geoSelectionConfigFile = config.getDetectorInfo()
s = runHashingSeeding(
nevents,
trackingGeometry,
field,
outputDir,
saveFiles,
npileup,
seedingAlgorithm,
maxSeedsPerSpM,
digiConfig,
geoSelectionConfigFile,
rnd=rnd,
eta=eta,
config=config,
)
logLevel = acts.logging.VERBOSE
rootSpacePointsWriter = RootSpacePointWriter(
level=logLevel,
inputSpacePoints="spacepoints",
filePath=str(outputDir / "spacepoints.root"),
)
s.addWriter(rootSpacePointsWriter)
rootSeedsWriter = acts.examples.root.RootSeedWriter(
level=logLevel,
inputSeeds="seeds",
filePath=str(outputDir / "seeds.root"),
)
s.addWriter(rootSeedsWriter)
addCKFTracks(
s,
trackingGeometry,
field,
TrackSelectorConfig(
pt=(1.0 * u.GeV, None),
absEta=(None, eta),
nMeasurementsMin=6,
),
twoWay=False,
outputDirRoot=outputDir,
)
s.run()