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