2Simple Test Program for General Broken Lines.
33from gblfit
import GblPoint, GblTrajectory
62 useThickScatterer =
False
63 print " Gbltst $Id$ ", nTry, nLayer, useThickScatterer
67 cosLambda = math.sqrt(1.0 - sinLambda ** 2)
69 cosPhi = math.sqrt(1.0 - sinPhi ** 2)
71 tuvDir = np.array([[cosLambda * cosPhi, cosLambda * sinPhi, sinLambda], \
72 [-sinPhi, cosPhi, 0.], \
73 [-sinLambda * cosPhi, -sinLambda * sinPhi, cosLambda]])
75 measErr = np.array([ 0.001, 0.001])
76 measPrec = 1.0 / measErr ** 2
80 scatCov = np.zeros((4, 4))
82 clErr = np.array([0.001, -0.1, 0.2, -0.15, 0.25])
87 print " external seed at label ", seedLabel
90 step = 1.5 / cosLambda
96 binaryFile = open(
"milleBinaryISN.dat",
"wb")
98 for iTry
in range(nTry):
100 clNorm = np.random.normal(0., 1., 5)
101 clPar = clErr * clNorm
105 clCov[i, i] = clErr[i] ** 2
109 jacPointToPoint = np.eye(5)
111 addDer = np.array([[1.0], [0.0]])
112 labGlobal = np.array([[4711], [4711]])
114 traj = GblTrajectory(bfac != 0.)
119 for iLayer
in range(nLayer):
122 sinStereo = (0.
if iLayer % 2 == 0
else 0.1)
123 cosStereo = math.sqrt(1.0 - sinStereo ** 2)
125 ijkDir = np.array([[1., 0., 0.], \
126 [0., cosStereo, sinStereo], \
127 [0., -sinStereo, cosStereo]])
132 proL2m = local.getTransLocalToMeas()
133 proL2c = local.getTransLocalToCurvi()
134 proC2l = np.linalg.inv(proL2c)
136 proC2m = local.getTransCurviToMeas()
138 measNorm = np.random.normal(0., 1., 2)
139 meas = np.dot(proC2m, clPar[3:5]) + measErr * measNorm
141 jac = np.dot(proC2l, np.dot(jacPointToPoint, oldL2c))
143 point = GblPoint(jac)
144 point.addMeasurement([proL2m, meas, measPrec])
148 point.addGlobals(labGlobal, addDer)
150 if useThickScatterer:
151 if scatCov[0, 0] > 0:
153 scatPrec = np.linalg.inv(scatCov)
156 point.addScatterer([scat, scatPrec])
157 scatCov = np.zeros((4, 4))
159 measNorm = np.random.uniform(-5., 5., 2)
160 if np.random.uniform() < 0.:
161 meas = np.dot(proC2m, clPar[3:5]) + measErr * measNorm
162 print " noise ", iLayer
163 point.addMeasurement([proL2m, meas, measPrec])
164 point.addGlobals(labGlobal, addDer)
168 iLabel = traj.addPoint(point)
170 if iLabel == abs(seedLabel):
171 clSeed = np.linalg.inv(clCov)
172 locSeed = np.dot(proL2c, np.dot(clSeed, proL2c.T))
175 clPar = np.dot(jacPointToPoint, clPar)
176 clCov = np.dot(jacPointToPoint, np.dot(clCov, jacPointToPoint.T))
179 scatCov = np.dot (jacPointToPoint[1:, 1:], np.dot(scatCov, jacPointToPoint[1:, 1:].T))
180 if (iLayer < nLayer - 1):
181 scat = np.array([0., 0.])
183 jac = np.dot(proC2l, np.dot(jacPointToPoint, proL2c))
184 point = GblPoint(jac)
186 if not useThickScatterer:
188 scatP = local.getScatPrecision(scatErr)
189 point.addScatterer([scat, scatP])
190 iLabel = traj.addPoint(point)
192 if iLabel == abs(seedLabel):
193 clSeed = np.linalg.inv(clCov)
194 locSeed = np.dot(proL2c, np.dot(clSeed, proL2c.T))
197 scatNorm = np.random.normal(0., 1., 2)
198 clPar[1:3] = clPar[1:3] + scatErr * scatNorm
199 scatCov[0, 0] += scatErr ** 2
200 scatCov[1, 1] += scatErr ** 2
203 clPar = np.dot(jacPointToPoint, clPar)
204 clCov = np.dot(jacPointToPoint, np.dot(clCov, jacPointToPoint.T))
207 scatCov = np.dot (jacPointToPoint[1:, 1:], np.dot(scatCov, jacPointToPoint[1:, 1:].T))
208 if (iLayer < nLayer - 1):
209 scat = np.array([0., 0.])
211 jac = np.dot(proC2l, np.dot(jacPointToPoint, proL2c))
212 point = GblPoint(jac)
214 if not useThickScatterer:
216 scatP = local.getScatPrecision(scatErr)
217 point.addScatterer([scat, scatP])
218 iLabel = traj.addPoint(point)
220 if iLabel == abs(seedLabel):
221 clSeed = np.linalg.inv(clCov)
222 locSeed = np.dot(proL2c, np.dot(clSeed, proL2c.T))
225 scatNorm = np.random.normal(0., 1., 2)
226 clPar[1:3] = clPar[1:3] + scatErr * scatNorm
227 scatCov[0, 0] += scatErr ** 2
228 scatCov[1, 1] += scatErr ** 2
230 clPar = np.dot(jacPointToPoint, clPar)
231 clCov = np.dot(jacPointToPoint, np.dot(clCov, jacPointToPoint.T))
234 scatCov = np.dot (jacPointToPoint[1:, 1:], np.dot(scatCov, jacPointToPoint[1:, 1:].T))
238 if locSeed
is not None:
239 traj.addExternalSeed(seedLabel, locSeed)
242 Chi2, Ndf, Lost, bandCond = traj.fit()
243 print " Record, Chi2, Ndf, Lost", iTry, Chi2, Ndf, Lost, bandCond
if bandCond > 0.
else 0.
248 traj.milleOut(binaryFile)
255 for i
in range(1, nLayer + 1):
256 locPar, locCov = traj.getResults(-i)
258 print " locPar ", locPar
260 locPar, locCov = traj.getResults(i)
262 print " locPar ", locPar
265 for i
in range(traj.getNumPoints()):
266 numData, aResiduals, aMeasErr, aResErr, aDownWeight = traj.getMeasResults(i + 1)
267 for j
in range(numData):
268 print " measRes " , i, j, aResiduals[j], aMeasErr[j], aResErr[j], aDownWeight[j]
274 print " Time [s] ", end - start
275 print " Chi2Sum/NdfSum ", Chi2Sum / NdfSum
276 print " LostSum/nTry ", LostSum / nTry
285 binaryFile = open(
"/Users/kleinwrt/Documents/workspace/GBL-thickScat/milleBinaryISN.dat")
294 while(nRec < maxRec):
296 traj = GblTrajectory(0)
298 traj.milleIn(binaryFile)
301 Chi2, Ndf, Lost, bandCond = traj.fit()
302 print " Record, Chi2, Ndf, Lost", nRec, Chi2, Ndf, Lost, bandCond
311 print " records read ", nRec
313 print " Time [s] ", end - start
314 print " Chi2Sum/NdfSum ", Chi2Sum / NdfSum
315 print " LostSum/nTry ", LostSum / nRec
330 jac[1, 0] = -bfac * ds * cosl
331 jac[3, 0] = -0.5 * bfac * ds * ds * cosl
350 self.
__prod = np.dot(curviDir, measDir.T)
364 meas2crv = np.zeros((5, 5))
366 meas2crv[1:3, 1:3] = self.
__prod[1:3, 1:3] * self.
__prod[0, 0]
367 meas2crv[3:5, 3:5] = self.
__prod[1:3, 1:3]
375 return np.linalg.inv(self.
__prod[1:3, 1:3])
384 fac = (1 - c1 * c1 - c2 * c2) / (scatErr * scatErr)
385 scatP = np.empty((2, 2))
386 scatP[0, 0] = fac * (1 - c1 * c1)
387 scatP[0, 1] = fac * (-c1 * c2)
388 scatP[1, 0] = fac * (-c1 * c2)
389 scatP[1, 1] = fac * (1 - c2 * c2)
407 self.
__c2m = np.linalg.inv(np.dot(curviDir[1:3, 1:3], measDir[1:3, 1:3].T))
435 return np.array([1., 1.]) / (scatErr * scatErr)
Curvilinear system as local system.
__c2m
projection curvilinear to measurement system: ((U,V)*(J,K))^-1
def getTransLocalToCurvi(self)
Transformation of (q/p, slopes, offsets) from local to curvilinear system.
def __init__(self, measDir, curviDir)
Construct local system.
def getTransLocalToMeas(self)
Transformation from local to measurement system.
def getTransCurviToMeas(self)
Transformation from curvilinear to measurement system.
def getScatPrecision(self, scatErr)
Scattering precision matrix in local system
Measurement system as local system.
def __init__(self, measDir, curviDir)
Construct local system.
def getTransLocalToCurvi(self)
Transformation of (q/p, slopes, offsets) from local to curvilinear system.
def getTransCurviToMeas(self)
Transformation from curvilinear to measurement system.
__prod
products (T,U,V) * (I,J,K)
def getScatPrecision(self, scatErr)
Scattering precision matrix in local system
def getTransLocalToMeas(self)
Transformation from local to measurement system.
def example1()
Create points on initial trajectory, create trajectory from points, fit and write trajectory to MP-II...
def gblSimpleJacobian(ds, cosl, bfac)
Simple jacobian.
def example2()
Read trajectory from MP-II binary file and refit.