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 overriden 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 if self.global_deformations is not None:
216 if not isinstance(self.global_deformations, list):
218
219 for deformation in self.global_deformations:
220 delta += deformation(global_pos)
221
222 if sensor == Belle2.VxdID(1, 1, 1):
223 delta = ROOT.TVector3(0., 0., 0.)
224
225 new_global_pos = global_pos + delta
226 new_local_pos = info.pointToLocal(new_global_pos)
227
228 if self.random_misalignments is not None:
229 if not isinstance(self.random_misalignments, list):
231
232 for random in self.random_misalignments:
233 pass
234
235 for i in range(0, 3):
236 if abs(new_local_pos[i]) < 1.e-14:
237 new_local_pos[i] = 0.
238
239 alignment.set(sensor.getID(), i + 1, new_local_pos[i])
240
241 # helper = GlobalDeformation(0.)
242
243 # global_pos = helper._xyz_to_rphiz(global_pos)
244 # new_global_pos = helper._xyz_to_rphiz(new_global_pos)
245
246 txt.write(
247 '{} {} {} {} {} {} {} {} {} {} {} {}\n'.format(
248 str(sensor.getLayerNumber()),
249 str(sensor.getLadderNumber()),
250 str(sensor.getSensorNumber()),
251 str(global_pos[0]),
252 str(global_pos[1]),
253 str(global_pos[2]),
254 str(new_global_pos[0]),
255 str(new_global_pos[1]),
256 str(new_global_pos[2]),
257 str(new_local_pos[0]),
258 str(new_local_pos[1]),
259 str(new_local_pos[2])
260 ))
261
262 txt.close()
263
264 Belle2.Database.Instance().storeData('VXDAlignment', alignment, self.iov)
265 txt.close()
VXD alignment (and maybe some calibration) parameters.
Definition: VXDAlignment.h:19
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:33
def _transform(self, r, phi, z)
def _transform(self, r, phi, z)
def __init__(self, global_deformations=None, random_misalignments=None, iov=Belle2.IntervalOfValidity(0, 0, -1, -1))
iov
interval of validity for the generated payload
def __init__(self, scale, scale2=0.)
Definition: misalignment.py:86
scale2
second scale for 1/r member
Definition: misalignment.py:90
def _transform(self, r, phi, z)
Definition: misalignment.py:93
def _transform(self, r, phi, z)
scale
scaling parameter of the deformation
Definition: misalignment.py:28
def __init__(self, scale)
def _transform(self, r, phi, z)
def _transform(self, r, phi, z)
def _transform(self, r, phi, z)
def _transform(self, r, phi, z)
static Database & Instance()
Instance of a singleton Database.
Definition: Database.cc:42