Belle II Software development
misalignment.py
1#!/usr/bin/env python3
2
3
10
11import basf2 as b2
12
13import ROOT
14from ROOT import Belle2
15
16import math
17
18
20 """
21 Base class to calculate global deformation of the detector
22 """
23
24 def __init__(self, scale):
25 """ Constructor, sets the scale of the transformation """
26
27
28 self.scale = scale
29
30 def __call__(self, xyz):
31 """ Return TVector3 with difference in global position induced by the deformation,
32 given the original x,y,z position as TVector3 or a list with length of 3.
33 """
34
35 rphiz = self._xyz_to_rphiz(xyz)
36 d_rphiz = self._transform(rphiz[0], rphiz[1], rphiz[2])
37
38 rphiz[0] += d_rphiz[0]
39 rphiz[1] += d_rphiz[1]
40 rphiz[2] += d_rphiz[2]
41
42 new_xyz = self._rphiz_to_xyz(rphiz)
43
44 d_xyz = ROOT.TVector3(new_xyz[0] - xyz[0], new_xyz[1] - xyz[1], new_xyz[2] - xyz[2])
45
46 return d_xyz
47
48 def _transform(self, r, phi, z):
49 """ Fcn to be overridden by child classes, return vector (list of 3 numbers) of displacement """
50
51 def _xyz_to_rphiz(self, xyz):
52 """ Convert (x,y,z) to (r,phi,z) """
53 import math
54 x = xyz[0]
55 y = xyz[1]
56 z = xyz[2]
57 r = math.sqrt(x * x + y * y)
58 phi = math.atan2(y, x)
59 rphiz = [r, phi, z]
60 return rphiz
61
62 def _rphiz_to_xyz(self, rphiz):
63 """ Convert (r,phi,z) to (x,y,z) """
64 import math
65 z = rphiz[2]
66 y = rphiz[0] * math.sin(rphiz[1])
67 x = rphiz[0] * math.cos(rphiz[1])
68 return [x, y, z]
69
70
72 """ radial expansion """
73
74 def __init__(self, scale):
75 """ init """
76 super().__init__(scale)
77
78 def _transform(self, r, phi, z):
79 """ the transformation """
80 return [self.scale * r, 0., 0.]
81
82
84 """ Curl """
85
86 def __init__(self, scale, scale2=0.):
87 """ init with scale, optionally scale2 for 1/r dependency """
88
89
90 self.scale2 = scale2
91 super().__init__(scale)
92
93 def _transform(self, r, phi, z):
94 """ the transformation """
95 return [0., self.scale * r + self.scale2 / r, 0.]
96
97
99 """ Telescope """
100
101 def __init__(self, scale):
102 """ init """
103 super().__init__(scale)
104
105 def _transform(self, r, phi, z):
106 """ the transformation """
107 return [0., 0., self.scale * r]
108
109
111 """ Elliptical distortion """
112
113 def __init__(self, scale):
114 """ init """
115 super().__init__(scale)
116
117 def _transform(self, r, phi, z):
118 """ the transformation """
119 import math
120 return [self.scale * 1. / 2. * math.cos(2 * phi) * r, 0., 0.]
121
122
124 """ Clamshell deformation """
125
126 def __init__(self, scale):
127 """ init """
128 super().__init__(scale)
129
130 def _transform(self, r, phi, z):
131 """ the transformation """
132 import math
133
134 return [0., self.scale * math.cos(phi), 0.]
135
136
138 """ Skew distortion """
139
140 def __init__(self, scale):
141 """ init """
142 super().__init__(scale)
143
144 def _transform(self, r, phi, z):
145 """ the transformation """
146 return [0., 0., self.scale * math.cos(phi) * r]
147
148
150 """ Bowing """
151
152 def __init__(self, scale):
153 """ init """
154 super().__init__(scale)
155
156 def _transform(self, r, phi, z):
157 """ the transformation """
158 return [self.scale * abs(z), 0., 0.]
159
160
162 """ Twist deformation """
163
164 def __init__(self, scale):
165 """ init """
166 super().__init__(scale)
167
168 def _transform(self, r, phi, z):
169 """ the transformation """
170 return [0., self.scale * z, 0.]
171
172
174 """ Z-expansion """
175
176 def __init__(self, scale):
177 """ init """
178 super().__init__(scale)
179
180 def _transform(self, r, phi, z):
181 """ the transformation """
182 return [0., 0., self.scale * z]
183
184
186 """ Module to create misalignment (first reads real sensor positions, then applies misalignment and returns DB payload)
187 TODO: random misalignment not finished...
188 """
189
190 def __init__(self, global_deformations=None, random_misalignments=None, iov=Belle2.IntervalOfValidity(0, 0, -1, -1)):
191 """ Constructor with with input misalignment """
192
193
194 self.global_deformations = global_deformations
195
196 self.random_misalignments = random_misalignments
197
198 self.iov = iov
199
200 super().__init__()
201
202 def initialize(self):
203 """ module initialize - read geometry, apply misalignment, create and store payloads """
204
205 alignment = Belle2.VXDAlignment()
206 txt = open('generated_misalignment.txt', 'w')
207 txt.write('layer ladder sensor x_orig y_orig z_orig x y z u v w\n')
208
209 for sensor in Belle2.VXD.GeoCache.getInstance().getListOfSensors():
210 info = Belle2.VXD.GeoCache.getInstance().get(sensor)
211 global_pos = info.pointToGlobal(ROOT.TVector3(0., 0., 0.))
212
213 delta = ROOT.TVector3(0., 0., 0.)
214
215 global_deformations = self.global_deformations
216 if global_deformations is not None:
217 if not isinstance(global_deformations, list):
218 global_deformations = [global_deformations]
219 self.global_deformations = global_deformations
220
221 for deformation in global_deformations:
222 delta += deformation(global_pos)
223
224 if sensor == Belle2.VxdID(1, 1, 1):
225 delta = ROOT.TVector3(0., 0., 0.)
226
227 new_global_pos = global_pos + delta
228 new_local_pos = info.pointToLocal(new_global_pos)
229
230 if self.random_misalignments is not None:
231 if not isinstance(self.random_misalignments, list):
233
234 for random in self.random_misalignments:
235 pass
236
237 for i in range(0, 3):
238 if abs(new_local_pos[i]) < 1.e-14:
239 new_local_pos[i] = 0.
240
241 alignment.set(sensor.getID(), i + 1, new_local_pos[i])
242
243 # helper = GlobalDeformation(0.)
244
245 # global_pos = helper._xyz_to_rphiz(global_pos)
246 # new_global_pos = helper._xyz_to_rphiz(new_global_pos)
247
248 txt.write(
249 '{} {} {} {} {} {} {} {} {} {} {} {}\n'.format(
250 str(sensor.getLayerNumber()),
251 str(sensor.getLadderNumber()),
252 str(sensor.getSensorNumber()),
253 str(global_pos[0]),
254 str(global_pos[1]),
255 str(global_pos[2]),
256 str(new_global_pos[0]),
257 str(new_global_pos[1]),
258 str(new_global_pos[2]),
259 str(new_local_pos[0]),
260 str(new_local_pos[1]),
261 str(new_local_pos[2])
262 ))
263
264 txt.close()
265
266 Belle2.Database.Instance().storeData('VXDAlignment', alignment, self.iov)
267 txt.close()
VXD alignment (and maybe some calibration) parameters.
static GeoCache & getInstance()
Return a reference to the singleton instance.
Definition GeoCache.cc:214
Class to uniquely identify a any structure of the PXD and SVD.
Definition VxdID.h:32
__init__(self, global_deformations=None, random_misalignments=None, iov=Belle2.IntervalOfValidity(0, 0, -1, -1))
iov
interval of validity for the generated payload
scale2
second scale for 1/r member
__init__(self, scale, scale2=0.)
_transform(self, r, phi, z)
scale
scaling parameter of the deformation
_transform(self, r, phi, z)
static Database & Instance()
Instance of a singleton Database.
Definition Database.cc:41