105 def TwoTrackVertex(self) -> None:
106 self.fPartArray.clear()
107 fittedTracks = getattr(self.sTree, self.fitTrackLoc)
108 goodTracks = getattr(self.sTree, self.goodTracksLoc)
109 if len(goodTracks) < 2:
110 return
111 PosDirCharge, CovMat, scalFac = {}, {}, {}
112 for tr in goodTracks:
113 fittedTracks[tr].getFitStatus()
114 xx = fittedTracks[tr].getFittedState()
115
116 pid = xx.getPDG()
117 rep = ROOT.genfit.RKTrackRep(pid)
118 state = ROOT.genfit.StateOnPlane(rep)
119 rep.setPosMom(state, xx.getPos(), xx.getMom())
120 PosDirCharge[tr] = {
121 "position": xx.getPos(),
122 "direction": xx.getDir(),
123 "momentum": xx.getMom(),
124 "charge": xx.getCharge(),
125 "pdgCode": pid,
126 "state": xx,
127 "rep": rep,
128 "newstate": state,
129 }
130 CovMat[tr] = xx.get6DCov()
131
132 if len(PosDirCharge) < 2:
133 return
134 if len(PosDirCharge) > 4:
135 return
136 for t1 in PosDirCharge:
137 c1 = PosDirCharge[t1]["charge"]
138 for t2 in PosDirCharge:
139 if not t2 > t1:
140 continue
141
142 if PosDirCharge[t2]["charge"] == c1:
143 continue
144 newPos, _covX, doca = self.VertexError(t1, t2, PosDirCharge)
145
146
147
148 rc = True
149 target_z = newPos[2]
150 extrapolatedPosDir = {}
151 for tr in [t1, t2]:
152 current_z = PosDirCharge[tr]["rep"].getPos(PosDirCharge[tr]["newstate"]).Z()
153 try:
154 self._stepwise_extrapolate_to_z(
155 lambda sp, _tr=tr: PosDirCharge[_tr]["rep"].extrapolateToPlane(
156 PosDirCharge[_tr]["newstate"], sp
157 ),
158 current_z,
159 target_z,
160 )
161 except Exception:
162 ut.reportError("shipVertex: stepwise plane extrapolation did not work")
163 rc = False
164 break
165 extrapolatedPosDir[tr] = {
166 "position": PosDirCharge[tr]["rep"].getPos(PosDirCharge[tr]["newstate"]),
167 "direction": PosDirCharge[tr]["rep"].getDir(PosDirCharge[tr]["newstate"]),
168 "momentum": PosDirCharge[tr]["rep"].getMom(PosDirCharge[tr]["newstate"]),
169 }
170 if not rc:
171 continue
172
173 newPos, _covX, doca = self.VertexError(t1, t2, extrapolatedPosDir)
174
175 dz = 99999.0
176 step = 0
177 while dz > 0.01:
178 zBefore = newPos[2]
179
180 for tr in [t1, t2]:
181 try:
182 PosDirCharge[tr]["rep"].extrapolateToPoint(PosDirCharge[tr]["newstate"], newPos, False)
183 except Exception:
184 ut.reportError("shipVertex: extrapolation did not work")
185 rc = False
186 break
187 self.newPosDir[tr] = {
188 "position": PosDirCharge[tr]["rep"].getPos(PosDirCharge[tr]["newstate"]),
189 "direction": PosDirCharge[tr]["rep"].getDir(PosDirCharge[tr]["newstate"]),
190 "momentum": PosDirCharge[tr]["rep"].getMom(PosDirCharge[tr]["newstate"]),
191 }
192 if not rc:
193 break
194 newPos, _covX, doca = self.VertexError(t1, t2, self.newPosDir)
195 dz = abs(zBefore - newPos[2])
196 step += 1
197 if step > 10:
198 ut.reportError("shipVertex: abort iteration, too many steps")
199 if global_variables.debug:
200 print(
201 "abort iteration, too many steps, pos=",
202 newPos[0],
203 newPos[1],
204 newPos[2],
205 " doca=",
206 doca,
207 "z before and dz",
208 zBefore,
209 dz,
210 )
211 rc = False
212 break
213
214 if not rc:
215 continue
216 scalFac[t1] = (
217 (PosDirCharge[t1]["position"][2] - newPos[2])
218 / PosDirCharge[t1]["direction"][2]
219 / PosDirCharge[t1]["momentum"].Mag()
220 )
221 scalFac[t2] = (
222 (PosDirCharge[t2]["position"][2] - newPos[2])
223 / PosDirCharge[t2]["direction"][2]
224 / PosDirCharge[t2]["momentum"].Mag()
225 )
226 HNLPos, _covX, _dist = self.VertexError(t1, t2, self.newPosDir, CovMat, scalFac)
227
228
229
230
231
232
233
234 st1 = ROOT.genfit.MeasuredStateOnPlane(fittedTracks[t1].getFittedState())
235 st2 = ROOT.genfit.MeasuredStateOnPlane(fittedTracks[t2].getFittedState())
236
237
238 rc = True
239 for st in [st1, st2]:
240 try:
241 self._stepwise_extrapolate_to_z(st.extrapolateToPlane, st.getPos().Z(), HNLPos[2])
242 except Exception:
243 ut.reportError("shipVertex.TwoTrackVertex: extrapolation did not work")
244 rc = False
245 break
246 if not rc:
247 continue
248 mom1 = st1.getMom()
249 mom2 = st2.getMom()
250 cov1 = st1.getCov()
251 cov2 = st2.getCov()
252 cov = ROOT.TMatrixDSym(10)
253 for i in range(10):
254 for j in range(10):
255 if i < 5 and j < 5:
256 cov[i][j] = cov1[i][j]
257 if i > 4 and j > 4:
258 cov[i][j] = cov2[i - 5][j - 5]
259 covInv = ROOT.TMatrixDSym()
260 ROOT.genfit.tools.invertMatrix(cov, covInv)
261
262
263
264
265 self.y_data = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
266 stVal1 = st1.getState()
267 stVal2 = st2.getState()
268 for i in range(5):
269 self.y_data[i] = stVal1[i]
270 self.y_data[i + 5] = stVal2[i]
271 self.z0 = HNLPos[2]
272 self.Vy = np.zeros(100)
273 for i in range(100):
274 self.Vy[i] = covInv[i // 10][i % 10]
275
276 gMinuit = ROOT.TMinuit(9)
277 tempFcn = self.fcn
278 gMinuit.SetFCN(tempFcn)
279 gMinuit.SetPrintLevel(-1)
280 rc = gMinuit.DefineParameter(0, "X pos", HNLPos[0], 0.1, 0, 0)
281 rc = gMinuit.DefineParameter(1, "Y pos", HNLPos[1], 0.1, 0, 0)
282 rc = gMinuit.DefineParameter(2, "Z pos", HNLPos[2], 0.1, 0, 0)
283 rc = gMinuit.DefineParameter(3, "tan1X", mom1[0] / mom1[2], 0.1, 0, 0)
284 rc = gMinuit.DefineParameter(4, "tan1Y", mom1[1] / mom1[2], 0.1, 0, 0)
285 rc = gMinuit.DefineParameter(5, "1/mom1", 1.0 / mom1.Mag(), 0.1, 0, 0)
286 rc = gMinuit.DefineParameter(6, "tan2X", mom2[0] / mom2[2], 0.1, 0, 0)
287 rc = gMinuit.DefineParameter(7, "tan2Y", mom2[1] / mom2[2], 0.1, 0, 0)
288 rc = gMinuit.DefineParameter(8, "1/mom2", 1.0 / mom2.Mag(), 0.1, 0, 0)
289 gMinuit.Clear()
290 gMinuit.Migrad()
291
292 fmin = ctypes.c_double()
293 fedm = ctypes.c_double()
294 errdef = ctypes.c_double()
295 npari = ctypes.c_int()
296 nparx = ctypes.c_int()
297 istat = ctypes.c_int()
298 gMinuit.mnstat(fmin, fedm, errdef, npari, nparx, istat)
299 if istat.value == 0:
300 ut.reportError(f"shipVertex::Migrad not converged, istat={istat.value}")
301 continue
302 try:
303 tmp = array("d", [0])
304 err = array("i", [0])
305 gMinuit.mnexcm("HESSE", tmp, -1, err)
306 if err[0] != 0:
307 ut.reportError(f"shipVertex::HESSE failed, ierflg={err[0]}")
308 continue
309 except Exception:
310 ut.reportError("shipVertex::HESSE raised exception")
311 continue
312
313 emat = array(
314 "d",
315 [
316 0,
317 ]
318 * 81,
319 )
320 gMinuit.mnemat(emat, 9)
321 values = array(
322 "d",
323 [
324 0,
325 ]
326 * 9,
327 )
328 errors = array(
329 "d",
330 [
331 0,
332 ]
333 * 9,
334 )
335 dValue = ctypes.c_double()
336 dError = ctypes.c_double()
337 for i in range(9):
338 gMinuit.GetParameter(i, dValue, dError)
339 values[i] = dValue.value
340 errors[i] = dError.value
341
342 xFit = values[0]
343 yFit = values[1]
344 zFit = values[2]
345 HNLPosFit = ROOT.TVector3(xFit, yFit, zFit)
346
347
348
349
350
351
352
353
354
355
356
357
358 try:
359 self._stepwise_extrapolate_to_z(st1.extrapolateToPlane, st1.getPos().Z(), HNLPosFit.Z())
360 self._stepwise_extrapolate_to_z(st2.extrapolateToPlane, st2.getPos().Z(), HNLPosFit.Z())
361 except Exception:
362 ut.reportError("shipVertex: extrapolation to HNLPosFit failed")
363 continue
364 d1u = st1.getMom().Unit()
365 d2u = st2.getMom().Unit()
366 delta = st2.getPos() - st1.getPos()
367 cross = d1u.Cross(d2u)
368 cross_mag = cross.Mag()
369 if cross_mag > 1e-9:
370 doca = abs(delta.Dot(cross)) / cross_mag
371 else:
372
373 doca = (delta - d1u * delta.Dot(d1u)).Mag()
374
375
376 p1 = ut.get_particle(PosDirCharge[t1]["pdgCode"])
377 p2 = ut.get_particle(PosDirCharge[t2]["pdgCode"])
378 m1 = p1.Mass()
379 m2 = p2.Mass()
380
381
382
383
384
385
386
387
388 def getP(fitValues: array[float], cov: array[float], m1, m2):
389 a3 = fitValues[3]
390 a4 = fitValues[4]
391 a5 = fitValues[5]
392 a6 = fitValues[6]
393 a7 = fitValues[7]
394 a8 = fitValues[8]
395 px1 = a3 / (a5 * ROOT.TMath.Sqrt(1 + a3**2 + a4**2))
396 py1 = a4 / (a5 * ROOT.TMath.Sqrt(1 + a3**2 + a4**2))
397 pz1 = 1 / (a5 * ROOT.TMath.Sqrt(1 + a3**2 + a4**2))
398 px2 = a6 / (a8 * ROOT.TMath.Sqrt(1 + a6**2 + a7**2))
399 py2 = a7 / (a8 * ROOT.TMath.Sqrt(1 + a6**2 + a7**2))
400 pz2 = 1 / (a8 * ROOT.TMath.Sqrt(1 + a6**2 + a7**2))
401 Px = px1 + px2
402 Py = py1 + py2
403 Pz = pz1 + pz2
404 E1 = ROOT.TMath.Sqrt(px1**2 + py1**2 + pz1**2 + m1**2)
405 E2 = ROOT.TMath.Sqrt(px2**2 + py2**2 + pz2**2 + m2**2)
406 M = ROOT.TMath.Sqrt(2 * E1 * E2 + m1**2 + m2**2 - 2 * pz1 * pz2 * (1 + a3 * a6 + a4 * a7))
407 MM = 2 * M
408 A5 = 1 + a3**2 + a4**2
409 A8 = 1 + a6**2 + a7**2
410 M_AtoP = ROOT.TMatrixD(4, 6)
411 MT_AtoP = ROOT.TMatrixD(6, 4)
412 covA = ROOT.TMatrixD(6, 6)
413 M_AtoP[0][0] = (1.0 - a3 * a3 / A5) / (a5 * ROOT.TMath.Sqrt(A5))
414 M_AtoP[0][1] = (-a3 * a4 / A5) / (a5 * ROOT.TMath.Sqrt(A5))
415 M_AtoP[0][2] = (-a3) / (a5 * a5 * ROOT.TMath.Sqrt(A5))
416 M_AtoP[0][3] = (1.0 - a6 * a6 / A8) / (a8 * ROOT.TMath.Sqrt(A8))
417 M_AtoP[0][4] = (-a6 * a7 / A8) / (a8 * ROOT.TMath.Sqrt(A8))
418 M_AtoP[0][5] = (-a6) / (a8 * a8 * ROOT.TMath.Sqrt(A8))
419
420 M_AtoP[1][0] = (-a3 * a4 / A5) / (a5 * ROOT.TMath.Sqrt(A5))
421 M_AtoP[1][1] = (1.0 - a4 * a4 / A5) / (a5 * ROOT.TMath.Sqrt(A5))
422 M_AtoP[1][2] = (-a4) / (a5 * a5 * ROOT.TMath.Sqrt(A5))
423 M_AtoP[1][3] = (-a6 * a7 / A8) / (a8 * ROOT.TMath.Sqrt(A8))
424 M_AtoP[1][4] = (1.0 - a7 * a7 / A8) / (a8 * ROOT.TMath.Sqrt(A8))
425 M_AtoP[1][5] = (-a7) / (a8 * a8 * ROOT.TMath.Sqrt(A8))
426
427 M_AtoP[2][0] = (-a3 / A5) / (a5 * ROOT.TMath.Sqrt(A5))
428 M_AtoP[2][1] = (-a4 / A5) / (a5 * ROOT.TMath.Sqrt(A5))
429 M_AtoP[2][2] = (-1.0) / (a5 * a5 * ROOT.TMath.Sqrt(A5))
430 M_AtoP[2][3] = (-a6 / A8) / (a8 * ROOT.TMath.Sqrt(A8))
431 M_AtoP[2][4] = (-a7 / A8) / (a8 * ROOT.TMath.Sqrt(A8))
432 M_AtoP[2][5] = (-1.0) / (a8 * a8 * ROOT.TMath.Sqrt(A8))
433
434 a5a8 = a5 * a8 * ROOT.TMath.Sqrt(A5) * ROOT.TMath.Sqrt(A8)
435
436 M_AtoP[3][0] = (-2 * a6 / a5a8 + 2 * a3 * E2 / (a5 * a5 * A5 * E1)) / MM
437 M_AtoP[3][1] = (-2 * a7 / a5a8 + 2 * a4 * E2 / (a5 * a5 * A5 * E1)) / MM
438 M_AtoP[3][2] = (
439 2 * (1 + a3 * a6 + a4 * a7) / (a5 * a5a8)
440 - 2 * (1.0 + a3 * a3 + a4 * a4) * E2 / (a5 * a5 * a5 * A5 * E1)
441 ) / MM
442 M_AtoP[3][3] = (-2 * a3 / a5a8 + 2 * a6 * E1 / (a8 * a8 * A8 * E2)) / MM
443 M_AtoP[3][4] = (-2 * a4 / a5a8 + 2 * a7 * E1 / (a8 * a8 * A8 * E2)) / MM
444 M_AtoP[3][5] = (
445 2 * (1 + a3 * a6 + a4 * a7) / (a8 * a5a8)
446 - 2 * (1.0 + a6 * a6 + a7 * a7) * E1 / (a8 * a8 * a8 * A8 * E2)
447 ) / MM
448
449 for i in range(4):
450 for j in range(6):
451 MT_AtoP[j][i] = M_AtoP[i][j]
452
453
454
455 for i in range(36):
456 covA[i // 6][i % 6] = cov[(i // 6 + 3) * 9 + (i % 6 + 3)]
457
458 tmp = ROOT.TMatrixD(4, 6)
459 tmp.Mult(M_AtoP, covA)
460 covP = ROOT.TMatrixD(4, 4)
461 covP.Mult(tmp, MT_AtoP)
462 P = ROOT.TLorentzVector()
463 P.SetXYZM(Px, Py, Pz, M)
464 return P, covP
465
466 P, covP = getP(values, emat, m1, m2)
467
468
469
470
471
472
473
474
475 covV = array("d", [emat[0], emat[1], emat[2], emat[1 + 9], emat[2 + 9], emat[2 + 2 * 9]])
476 covP = array(
477 "d",
478 [
479 covP[0][0],
480 covP[0][1],
481 covP[0][2],
482 covP[0][3],
483 covP[1][1],
484 covP[1][2],
485 covP[1][3],
486 covP[2][2],
487 covP[2][3],
488 covP[3][3],
489 ],
490 )
491
492
493 vx = ROOT.TLorentzVector(
494 HNLPosFit, 0
495 )
496 particle = ROOT.ShipParticle(9900015, 0, -1, -1, t1, t2, P, vx)
497 particle.SetCovV(covV)
498 particle.SetCovP(covP)
499 particle.SetDoca(doca)
500 self.fPartArray.push_back(particle)
501
502
503
504
505
506