forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmillepede_alignment.py
More file actions
325 lines (288 loc) · 8.66 KB
/
millepede_alignment.py
File metadata and controls
325 lines (288 loc) · 8.66 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
#!/usr/bin/env python3
import os
import argparse
import pathlib
from pathlib import Path
import acts
import acts.examples
from acts.examples import (
TelescopeDetector,
Sequencer,
StructureSelector,
RandomNumbers,
GaussianVertexGenerator,
)
from acts.examples.alignment import (
AlignmentDecorator,
GeoIdAlignmentStore,
AlignmentGeneratorGlobalShift,
)
from acts.examples.alignmentmillepede import (
MillePedeAlignmentSandbox,
ActsSolverFromMille,
)
from acts.examples.simulation import (
MomentumConfig,
EtaConfig,
PhiConfig,
ParticleConfig,
ParticleSelectorConfig,
addParticleGun,
addFatras,
addDigitization,
addDigiParticleSelection,
)
from acts.examples.reconstruction import (
addSeeding,
CkfConfig,
addCKFTracks,
TrackSelectorConfig,
SeedingAlgorithm,
TrackSelectorConfig,
addSeeding,
SeedingAlgorithm,
SeedFinderConfigArg,
SeedFinderOptionsArg,
SeedingAlgorithm,
CkfConfig,
addCKFTracks,
TrackSelectorConfig,
)
# Helper to instantiate a telescope detector.
# The square sensors are oriented in the global
# y-direction and cover the x-z plane.
# You can change the number of layers by resizing
# the "bounds", "stereos" and "positions" arrays accordingly.
#
# By default, will be digitised as 25 x 100 pixel grid.
#
# The "layer" field of the geo ID of this detector
# will move in steps of 2 for each module (2,4,6,..,18 for 9 layers).
# Everything is located in volume "1".
#
# In the alignment, at least 4 layers are expected (layer ID 8 will
# be fixed as the alignment reference).
def getTelescopeDetector():
bounds = [200, 200]
positions = [30, 60, 90, 120, 150, 180, 210, 240, 270]
stereos = [0] * len(positions)
detector = TelescopeDetector(
bounds=bounds, positions=positions, stereos=stereos, binValue=1
)
return detector
# Add the alignment sandbox algorithm
def addAlignmentSandbox(
s: Sequencer,
trackingGeometry: acts.TrackingGeometry,
magField: acts.MagneticFieldProvider,
fixModules: set,
inputMeasurements: str = "measurements",
inputTracks: str = "ckf_tracks",
logLevel: acts.logging.Level = acts.logging.INFO,
milleOutput: str = "MilleBinary.root",
discardUnconstrainedTrackPar: bool = True,
outFileInternalSolving: str = "ActsInternalAlignment_Result.txt",
outFileDecomposition: str = "ActsInternalAlignment_Eigenvals.txt",
):
sandbox = MillePedeAlignmentSandbox(
level=logLevel,
milleOutput=milleOutput,
inputMeasurements=inputMeasurements,
inputTracks=inputTracks,
trackingGeometry=trackingGeometry,
magneticField=magField,
fixModules=fixModules,
discardUnconstrainedTrackPar=discardUnconstrainedTrackPar,
outFileInternalSolving=outFileInternalSolving,
outFileDecomposition=outFileDecomposition,
)
s.addAlgorithm(sandbox)
return s
def addSolverFromMille(
s: Sequencer,
trackingGeometry: acts.TrackingGeometry,
magField: acts.MagneticFieldProvider,
fixModules: set,
logLevel: acts.logging.Level = acts.logging.INFO,
milleInput: str = "MilleBinary.root",
outFile: str = "ActsAlignmentViaMille.txt",
):
solver = ActsSolverFromMille(
level=logLevel,
milleInput=milleInput,
trackingGeometry=trackingGeometry,
magneticField=magField,
fixModules=fixModules,
outFile=outFile,
)
s.addAlgorithm(solver)
return s
u = acts.UnitConstants
# Can also use zero-field - but not healthy for track covariance.
# field = acts.NullBField()
field = acts.ConstantBField(acts.Vector3(0, 0, 2 * acts.UnitConstants.T))
parser = argparse.ArgumentParser(
description="MillePede alignment demo with the Telescope Detector"
)
parser.add_argument(
"--output",
"-o",
help="Output directory",
type=pathlib.Path,
default=pathlib.Path.cwd() / "mpali_output",
)
parser.add_argument("--events", "-n", help="Number of events", type=int, default=2000)
parser.add_argument("--skip", "-s", help="Number of events", type=int, default=0)
args = parser.parse_args()
outputDir = args.output
# ensure out output dir exists
os.makedirs(outputDir, exist_ok=True)
# Instantiate the telescope detector - with alignment enabled
detector = getTelescopeDetector()
trackingGeometry = detector.trackingGeometry()
decorators = detector.contextDecorators()
# inject a known misalignment.
# Misalign the second tracking layer (ID = 4).
layerToBump = acts.GeometryIdentifier(layer=4, volume=1, sensitive=1)
# shift this layer by 200 microns in the global Z direction
leShift = AlignmentGeneratorGlobalShift()
leShift.shift = acts.Vector3(0, 0, 200.0e-3)
# now add some boilerplate code to make this happen
alignDecoConfig = AlignmentDecorator.Config()
alignDecoConfig.nominalStore = GeoIdAlignmentStore(
StructureSelector(trackingGeometry).selectedTransforms(
acts.GeometryContext.dangerouslyDefaultConstruct(), layerToBump
)
)
alignDecoConfig.iovGenerators = [((0, 10000000), leShift)]
alignDeco = AlignmentDecorator(alignDecoConfig, acts.logging.WARNING)
contextDecorators = [alignDeco]
# decide on at least on detector module to fix in place
# as a reference for the alignment.
# By default, fix the innermost layer.
fixModules = {
acts.GeometryIdentifier(layer=2, volume=1, sensitive=1),
acts.GeometryIdentifier(layer=10, volume=1, sensitive=1),
acts.GeometryIdentifier(layer=18, volume=1, sensitive=1),
}
# More Boilerplate code - for setting up the sequence
rnd = RandomNumbers(seed=42)
s = Sequencer(
events=args.events,
skip=args.skip,
numThreads=1,
outputDir=str(outputDir),
)
# Add a context with the alignment shift - sim, digi and
# initial reco will "see" the distorted detector
s.addContextDecorator(alignDeco)
# Run particle gun and fire some muons at our telescope
addParticleGun(
s,
MomentumConfig(
10 * u.GeV,
100 * u.GeV,
transverse=True,
),
EtaConfig(-0.3, 0.3),
# aim roughly along +Y...
PhiConfig(60 * u.degree, 120 * u.degree),
ParticleConfig(1, acts.PdgParticle.eMuon, randomizeCharge=True),
vtxGen=GaussianVertexGenerator(
mean=acts.Vector4(0, 0, 0, 0),
stddev=acts.Vector4(5.0 * u.mm, 0.0 * u.mm, 5.0 * u.mm, 0.0 * u.ns),
),
multiplicity=1,
rnd=rnd,
)
# fast sim
addFatras(
s,
trackingGeometry,
field,
enableInteractions=True,
outputDirRoot=None,
outputDirCsv=None,
outputDirObj=None,
rnd=rnd,
)
# digitise with the default 25x100 pixel config
srcdir = Path(__file__).resolve().parent.parent.parent.parent
addDigitization(
s,
trackingGeometry,
field,
digiConfigFile=srcdir / "Examples/Configs/telescope-digi-smearing-config.json",
outputDirRoot=None,
outputDirCsv=None,
rnd=rnd,
)
addDigiParticleSelection(
s,
ParticleSelectorConfig(
measurements=(3, None),
removeNeutral=True,
),
)
# Run grid seeder
addSeeding(
s,
trackingGeometry,
field,
# Settings copied from existing snippet, slightly adapted
seedFinderConfigArg=SeedFinderConfigArg(
r=(20 * u.mm, 200 * u.mm),
deltaR=(1 * u.mm, 300 * u.mm),
collisionRegion=(-250 * u.mm, 250 * u.mm),
z=(-100 * u.mm, 100 * u.mm),
maxSeedsPerSpM=1,
sigmaScattering=5,
radLengthPerSeed=0.1,
minPt=0.5 * u.GeV,
impactMax=3 * u.mm,
),
# why do we need to specify this here again? Not taken from event context?
seedFinderOptionsArg=SeedFinderOptionsArg(bFieldInZ=2 * u.T),
seedingAlgorithm=SeedingAlgorithm.GridTriplet,
initialSigmas=[
3 * u.mm,
3 * u.mm,
1 * u.degree,
1 * u.degree,
0 * u.e / u.GeV,
1 * u.ns,
],
initialSigmaQoverPt=0.1 * u.e / u.GeV,
initialSigmaPtRel=0.1,
initialVarInflation=[1.0] * 6,
# This file should be adapted if you add layers and want to include
# them in the seeding.
geoSelectionConfigFile=srcdir / "Examples/Configs/telescope-seeding-config.json",
outputDirRoot=None,
)
# Add CKF track finding
addCKFTracks(
s,
trackingGeometry,
field,
TrackSelectorConfig(),
CkfConfig(
chi2CutOffMeasurement=150.0,
chi2CutOffOutlier=250.0,
numMeasurementsCutOff=50,
seedDeduplication=(True),
stayOnSeed=True,
),
outputDirRoot=None,
writePerformance=False,
writeTrackSummary=False,
)
# And add our alignment sandbox
addAlignmentSandbox(
s, trackingGeometry, field, fixModules, milleOutput=outputDir / "MyBinary.root"
)
# And finally read back and solve in ACTS
addSolverFromMille(
s, trackingGeometry, field, fixModules, milleInput=outputDir / "MyBinary.root"
)
s.run()