-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
289 lines (241 loc) · 8.64 KB
/
utils.py
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
import sys, re, os
import numpy as np
from math import cos,atan,tan,pi,sin,asin,copysign
from ROOT import TMath
pyutilspath = os.getenv('PYTHONUTILS','pythonutils')
sys.path.append(pyutilspath)
import hps_utils
debug = False
class HpsGblException(Exception):
def __init__(self, msg):
Exception.__init__(self, msg)
def chi2Prob(chi2,ndf):
return TMath.Prob(chi2,ndf)
def getBrackets(str):
if not '<' in str:
print 'Error no < in %s' % str
sys.exit(1)
str.split('<')[1]
def gblSimpleJacobianLambdaPhi(ds, cosl, bfac):
'''
Simple jacobian: quadratic in arc length difference.
using lambda phi as directions
curvilinear track parameter (q/p,lambda,phi,x_t,y_t)
@param ds: arc length difference
@type ds: float
@param cosl: cos(lambda)
@type cosl: float
@param bfac: Bz*c
@type bfac: float
@return: jacobian to move by 'ds' on trajectory
@rtype: matrix(float)
ajac(1,1)= 1.0D0
ajac(2,2)= 1.0D0
ajac(3,1)=-DBLE(bfac*ds)
ajac(3,3)= 1.0D0
ajac(4,1)=-DBLE(0.5*bfac*ds*ds*cosl)
ajac(4,3)= DBLE(ds*cosl)
ajac(4,4)= 1.0D0
ajac(5,2)= DBLE(ds)
ajac(5,5)= 1.0D0
'''
jac = np.eye(5)
jac[2, 0] = -bfac * ds
jac[3, 0] = -0.5 * bfac * ds * ds * cosl
jac[3, 2] = ds * cosl
jac[4, 1] = ds
return jac
def gblSimpleJacobian(ds, cosl, bfac):
'''
Simple jacobian: quadratic in arc length difference.
curvilinear local system (q/p,v',w',v,w), (v,w)=(x_t,y_t)
@param ds: arc length difference
@type ds: float
@param cosl: cos(lambda)
@type cosl: float
@param bfac: Bz*c
@type bfac: float
@return: jacobian to move by 'ds' on trajectory
@rtype: matrix(float)
'''
jac = np.eye(5)
jac[1, 0] = -bfac * ds * cosl
jac[3, 0] = -0.5 * bfac * ds * ds * cosl
jac[3, 1] = ds
jac[4, 2] = ds
return jac
class globalDers:
def __init__(self,id,umeas,vmeas,wmeas, tDir, tPred, normal):
self.millepedeId = id
self.umeas = umeas # measurement direction
self.vmeas = vmeas # unmeasured direction
self.wmeas = wmeas # normal to plane
self.t = tDir # track direction
self.p = tPred # track prediction
self.n = normal # normal to plane
# Global derivaties of the local measurements
self.dm_dg = self.getMeasDers()
# Derivatives of residuals w.r.t. measurement
self.dr_dm = self.getResDers()
# Derivatives of residuals w.r.t. global parameters
self.dr_dg = np.dot(self.dr_dm, self.dm_dg)
#print 'dr_dm'
#print self.dr_dm
#print 'dm_dg'
#print self.dm_dg
#print 'dr_dg'
#print self.dr_dg
def dump(self):
print 'globalDers:'
print 'layer ', self.millepedeId
print 'umeas ', self.umeas, ' vmeas ', self.vmeas, ' wmeas ', self.wmeas
print 't ', self.t, ' p ', self.p, ' n ', self.n
print 'dm_dg\n',self.dm_dg, '\ndr_dm\n',self.dr_dm,'\ndr_dg\n',self.dr_dg
def getDers(self,isTop):
half_offset = 10000
translation_offset = 1000
direction_offset = 100
topBot = 1
transRot = 1
direction = 1
if not isTop:
topBot = 2
res = {}
labels = []
ders = []
global_params = {1:'u',2:'v',3:'w',4:'alpha',5:'beta',6:'gamma'}
for ip, name in global_params.iteritems():
if ip > 3:
transRot = 2
direction = ((ip-1) % 3) + 1
else:
direction = ip
label = (int)(topBot * half_offset + transRot * translation_offset + direction * direction_offset + self.millepedeId)
labels.append(label)
ders.append(self.dr_dg[0,ip-1])
return {'labels':np.array([labels]) , 'ders':np.array([ders])}
def getResDers(self):
# Derivatives of the local perturbed residual w.r.t. the measurements m (u,v,w)'
tdotn = np.dot(self.t.T,self.n)
drdg = np.eye(3)
#print 't ', self.t, ' n ', self.n, ' dot(t,n) ', tdotn
for i in range(3):
for j in range(3):
delta = 0.
if i==j:
delta = 1.
drdg[i][j] = delta - self.t[i]*self.n[j]/tdotn[0]
return drdg
def getMeasDers(self):
# Derivative of mt, the perturbed measured coordinate vector m
# w.r.t. to global parameters: u,v,w,alpha,beta,gamma
# Derivative of the local measurement for a translation in u
dmu_du = 1.
dmv_du = 0.
dmw_du = 0.
# Derivative of the local measurement for a translation in v
dmu_dv = 0.
dmv_dv = 1.
dmw_dv = 0.
# Derivative of the local measurement for a translation in w
dmu_dw = 0.
dmv_dw = 0.
dmw_dw = 1.
# Derivative of the local measurement for a rotation around u-axis (alpha)
dmu_dalpha = 0.
dmv_dalpha = self.p[2] # self.wmeas
dmw_dalpha = -1.0 * self.p[1] # -1.0 * self.vmeas
# Derivative of the local measurement for a rotation around v-axis (beta)
dmu_dbeta = -1.0 * self.p[2] #-1.0 * self.wmeas
dmv_dbeta = 0.
dmw_dbeta = self.p[0] #self.umeas
# Derivative of the local measurement for a rotation around w-axis (gamma)
dmu_dgamma = self.p[1] # self.vmeas
dmv_dgamma = -1.0 * self.p[0] # -1.0 * self.umeas
dmw_dgamma = 0.
# put into matrix
dmdg = np.array([[dmu_du, dmu_dv, dmu_dw, dmu_dalpha, dmu_dbeta, dmu_dgamma],[dmv_du, dmv_dv, dmv_dw, dmv_dalpha, dmv_dbeta, dmv_dgamma],[dmw_du, dmw_dv, dmw_dw, dmw_dalpha, dmw_dbeta, dmw_dgamma]])
#print dmw_dbeta
#dmdg = np.array([[dmu_du, dmu_dv],[dmu_dw, dmu_dalpha], [dmw_dbeta, dmw_dgamma]])
return dmdg
def getHelixPathToX(parameters,x):
dca = parameters[3]
z0 = parameters[4]
phi0 = parameters[2]
theta = parameters[1]
C = parameters[0]
R = 1.0/C;
xc = (R - dca) * sin(phi0)
sinPhi = (xc - x)/R
phi_at_x = asin(sinPhi)
dphi_at_x = phi_at_x - phi0
if dphi_at_x > pi: dphi_at_x -= 2.0*pi
if dphi_at_x < -pi: dphi_at_x += 2.0*pi
s_at_x = -1.0 * dphi_at_x * R
return s_at_x
def getHelixPosAtX(parameters,x):
dca = parameters[3]
z0 = parameters[4]
phi0 = parameters[2]
theta = parameters[1]
C = parameters[0]
R = 1.0/C;
lam = pi/2.0 - theta
slope = tan(lam)
#print '%.10f' % x
#print '%.10f %.10f %.10f %.10f %.10f %.10f' % (dca, z0, phi0, slope, R, C)
xc = (R - dca) * sin(phi0)
sinPhi = (xc - x)/R
phi_at_x = asin(sinPhi)
dphi_at_x = phi_at_x - phi0
if dphi_at_x > pi:
dphi_at_x -= 2.0*pi
if dphi_at_x < -pi:
dphi_at_x += 2.0*pi
s_at_x = -1.0 * dphi_at_x * R
y = dca * cos(phi0) - R * cos(phi0) + R * cos(phi_at_x)
z = z0 + s_at_x * slope
#print x,y,z,s_at_x,dphi_at_x,phi_at_x,sinPhi,xc
return np.array([x,y,z])
def getXPlanePositionIterative(parameters,origin,normal,eps=0.0001):
d0 = parameters[3]
z0 = parameters[4]
phi0 = parameters[2]
theta = parameters[1]
C = parameters[0]
R = 1.0/C
lam = pi/2.0 - theta
slope = tan(lam)
#print 'Target origin ', origin, ' normal ', normal
#print '%.10f %.10f %.10f %.10f %.10f %.10f' % (d0, z0, phi0, slope, R, C)
#eps = 0.0001
#print 'eps ', eps
d = 9999.9
x = origin[0]
dx = 0.0
nIter = 0
pos = []
while abs(d) > eps and nIter < 50:
# Calculate position on helix at x
pos = getHelixPosAtX(parameters, x + dx)
# Check if we are on the plane
d = np.dot(pos-origin,np.array([normal]).T)
# the direction to move depends on the direction of normal w.r.t. track
# assume that the track moves in +x direction
if np.sign(normal[0])==-1:
dx += 1.0*d[0]/2.0
elif np.sign(normal[0])==1:
dx += -1.0*d[0]/2.0
else:
print 'ERROR: normal is invalid, should move along x-axis'
sys.exit(1)
#print nIter,' d ', d, ' pos ', pos, ' dx ', dx
nIter += 1
return pos
def getMeasurementResidualIterative(perPar, origin, u ,w, meas, eps):
'''Calculate the residual at plane defined by origin, u and w in the measurement direction for a set of track parameters.'''
predIter = getXPlanePositionIterative(perPar,origin, w, eps)
diffTrk = predIter - origin
uPredIter = np.dot(u , diffTrk.T)
uResIter = meas - uPredIter
return uResIter