FairShip
Loading...
Searching...
No Matches
shipVertex.Task Class Reference

Public Member Functions

None __init__ (self, hp, sTree, mcTree=None)
None execute (self)
float chi2 (self, res, Vy)
 residuals (self, y_data, a, float z0)
None fcn (self, npar, gin, f, par, iflag)
None TwoTrackVertex (self)
 VertexError (self, t1, t2, PosDir, CovMat=None, scalFac=None)

Public Attributes

 sTree = sTree
 mcTree = mcTree if mcTree is not None else sTree
 fPartArray = ROOT.std.vector("ShipParticle")()
 Particles = self.sTree.Branch("Particles", self.fPartArray)
 reps
 states
dict newPosDir = {}, {}, {}
dict LV = {1: ROOT.TLorentzVector(), 2: ROOT.TLorentzVector()}
 h = hp
 PDG = ROOT.TDatabasePDG.Instance()
str fitTrackLoc = "FitTracks"
str goodTracksLoc = "goodTracks"

Static Public Attributes

 y_data = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
int z0 = 0
 Vy = np.zeros(100)

Protected Member Functions

 _stepwise_extrapolate_to_z (self, extrapolate_fn, current_z, target_z, max_step=500.0)

Detailed Description

Definition at line 15 of file shipVertex.py.

Constructor & Destructor Documentation

◆ __init__()

None shipVertex.Task.__init__ ( self,
hp,
sTree,
mcTree = None )

Definition at line 18 of file shipVertex.py.

18 def __init__(self, hp, sTree, mcTree=None) -> None:
19 self.sTree = sTree
20 self.mcTree = mcTree if mcTree is not None else sTree
21 self.fPartArray = ROOT.std.vector("ShipParticle")()
22 if not self.sTree.GetBranch("Particles"):
23 self.Particles = self.sTree.Branch("Particles", self.fPartArray)
24 else:
25 self.Particles = self.sTree.Particles
26 self.reps, self.states, self.newPosDir = {}, {}, {}
27 self.LV = {1: ROOT.TLorentzVector(), 2: ROOT.TLorentzVector()}
28 self.h = hp
29 self.PDG = ROOT.TDatabasePDG.Instance()
30 self.fitTrackLoc = "FitTracks"
31 self.goodTracksLoc = "goodTracks"
32 # ut.bookHist(self.h,'Vzpull','Vz pull',100,-3.,3.)
33 # ut.bookHist(self.h,'Vxpull','Vx pull',100,-3.,3.)
34 # ut.bookHist(self.h,'Vypull','Vy pull',100,-3.,3.)
35
36 # ut.bookHist(self.h,'dVx','vertex X residual;X^{RECO}-X^{MC}, cm',400,-10.,10.)
37 # ut.bookHist(self.h,'dVy','vertex Y residual;Y^{RECO}-Y^{MC}, cm',400,-10.,10.)
38 # ut.bookHist(self.h,'dVz','vertex Z residual;Z^{RECO}-Z^{MC}, cm',500,-50.,50.)
39 # ut.bookHist(self.h,'VzpullFit','Vz pull, chi2 fit',100,-3.,3.)
40 # ut.bookHist(self.h,'VxpullFit','Vx pull, chi2 fit',100,-3.,3.)
41 # ut.bookHist(self.h,'VypullFit','Vy pull, chi2 fit',100,-3.,3.)
42 # ut.bookHist(self.h,'dVxFit','vertex X residual, chi2 fit;X^{RECO}-X^{MC}, cm',400,-10.,10.)
43 # ut.bookHist(self.h,'dVyFit','vertex Y residual, chi2 fit;Y^{RECO}-Y^{MC}, cm',400,-10.,10.)
44 # ut.bookHist(self.h,'dVzFit','vertex Z residual, chi2 fit;Z^{RECO}-Z^{MC}, cm',500,-50.,50.)
45 # ut.bookHist(self.h,'dMFit','HNL mass resolution, chi2 fit;M^{RECO}-M^{MC}, GeV',2000,-1.,1.)
46 # ut.bookHist(self.h,'MpullFit','M pull, chi2 fit',100,-3.,3.)
47
48 # ut.bookHist(self.h,'N_raveVtx','Number of RAVE vtx',5,-0.5,4.5)
49 # ut.bookHist(self.h,'N_Vtx','Number of vtx',5,-0.5,4.5)
50

Member Function Documentation

◆ _stepwise_extrapolate_to_z()

shipVertex.Task._stepwise_extrapolate_to_z ( self,
extrapolate_fn,
current_z,
target_z,
max_step = 500.0 )
protected
Extrapolate in steps to avoid RK failures over large distances.

Definition at line 92 of file shipVertex.py.

92 def _stepwise_extrapolate_to_z(self, extrapolate_fn, current_z, target_z, max_step=500.0):
93 """Extrapolate in steps to avoid RK failures over large distances."""
94 dz_total = target_z - current_z
95 n_steps = max(1, math.ceil(abs(dz_total) / max_step))
96 for istep in range(n_steps):
97 step_z = current_z + dz_total * (istep + 1) / n_steps
98 plane = ROOT.genfit.DetPlane(
99 ROOT.TVector3(0, 0, step_z),
100 ROOT.TVector3(1, 0, 0),
101 ROOT.TVector3(0, 1, 0),
102 )
103 extrapolate_fn(ROOT.genfit.SharedPlanePtr(plane))
104

◆ chi2()

float shipVertex.Task.chi2 ( self,
res,
Vy )

Definition at line 62 of file shipVertex.py.

62 def chi2(self, res, Vy) -> float:
63 # res^T V^-1 res, with the flat index i -> (row i//10, column i%10)
64 s = 0
65 for i in range(100):
66 s += Vy[i] * res[i // 10] * res[i % 10]
67 return s
68

◆ execute()

None shipVertex.Task.execute ( self)

Definition at line 51 of file shipVertex.py.

51 def execute(self) -> None:
52 # make particles persistent
53 self.TwoTrackVertex()
54

◆ fcn()

None shipVertex.Task.fcn ( self,
npar,
gin,
f,
par,
iflag )

Definition at line 87 of file shipVertex.py.

87 def fcn(self, npar, gin, f, par, iflag) -> None:
88 res = self.residuals(self.y_data, par, self.z0)
89 f.value = self.chi2(res, self.Vy)
90 return
91

◆ residuals()

shipVertex.Task.residuals ( self,
y_data,
a,
float z0 )

Definition at line 69 of file shipVertex.py.

69 def residuals(self, y_data, a, z0: float):
70 # fit parameters a: 0-2 vertex (x, y, z), 3-5 track 1 (dx/dz, dy/dz,
71 # 1/|p|), 6-8 track 2; positions are propagated from the vertex to z0.
72 # The q/p state component is compared via abs(), which equals 1/|p|
73 # for unit-charge tracks.
74 res = np.zeros(10)
75 res[0] = abs(y_data[0]) - a[5]
76 res[1] = y_data[1] - a[3]
77 res[2] = y_data[2] - a[4]
78 res[3] = y_data[3] - a[0] - a[3] * (z0 - a[2])
79 res[4] = y_data[4] - a[1] - a[4] * (z0 - a[2])
80 res[5] = abs(y_data[5]) - a[8]
81 res[6] = y_data[6] - a[6]
82 res[7] = y_data[7] - a[7]
83 res[8] = y_data[8] - a[0] - a[6] * (z0 - a[2])
84 res[9] = y_data[9] - a[1] - a[7] * (z0 - a[2])
85 return res
86

◆ TwoTrackVertex()

None shipVertex.Task.TwoTrackVertex ( self)

Definition at line 105 of file shipVertex.py.

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 # shipDigiReco fits every track under the muon hypothesis (+-13)
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 # abort too busy events
136 for t1 in PosDirCharge:
137 c1 = PosDirCharge[t1]["charge"]
138 for t2 in PosDirCharge:
139 if not t2 > t1:
140 continue
141 # ignore this for background studies
142 if PosDirCharge[t2]["charge"] == c1:
143 continue
144 newPos, _covX, doca = self.VertexError(t1, t2, PosDirCharge)
145 # Extrapolate both tracks toward the initial vertex
146 # estimate in steps to avoid long backward extrapolation
147 # through the magnetic field which causes RK failures.
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 # Recompute DOCA from the extrapolated positions
173 newPos, _covX, doca = self.VertexError(t1, t2, extrapolatedPosDir)
174 # as we have learned, need iterative procedure
175 dz = 99999.0
176 step = 0
177 while dz > 0.01:
178 zBefore = newPos[2]
179 # make a new rep for track 1,2
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 # extrapolation failed, makes no sense to 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 # monitor Vx resolution and pulls
228 # print "DEBUG",HNLPos[0],HNLPos[1],HNLPos[2],dist,covX[0][0],covX[1][1],covX[2][2]
229 # print " ",mctrack.GetStartX(),mctrack.GetStartY(),mctrack.GetStartZ()
230
231 # Copy the fitted states: getFittedState() returns a reference
232 # cached in the track, and extrapolating it in place would make
233 # results depend on the pair-processing order.
234 st1 = ROOT.genfit.MeasuredStateOnPlane(fittedTracks[t1].getFittedState())
235 st2 = ROOT.genfit.MeasuredStateOnPlane(fittedTracks[t2].getFittedState())
236 # Extrapolate to the vertex Z-plane stepwise to avoid
237 # RK failures for long backward extrapolation.
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 # pack both 5D states (and their inverse covariance below) as
263 # expected by fcn/residuals; the covariance is block-diagonal,
264 # i.e. the fit treats the two track states as independent
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) # minute quiet mode
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 # Check Migrad convergence via mnstat
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 # get results from TMinuit:
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 # Recompute DOCA at the chi^2-optimal vertex z-plane.
348 # The geometric `doca` from the iterative VertexError above is
349 # evaluated using tangent-line linearisations at the geometric
350 # iteration's converged vertex. For tracks that bend weakly in
351 # the helium decay volume (residual spectrometer-field leakage
352 # plus the Kalman fit's material handling), the line-to-line
353 # distance evaluated at HNLPosFit is closer to truth than the
354 # geometric value, often by a factor of several. Re-extrapolate
355 # the two FitTrack states (already at z = HNLPos[2]) the small
356 # residual dz to HNLPosFit.Z() and read off the perpendicular
357 # distance between the two lines through (pos, dir).
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 # Nearly parallel tracks — perpendicular component of (p2-p1).
373 doca = (delta - d1u * delta.Dot(d1u)).Mag()
374
375 # fixme: mass from track reconstraction needed
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 # self.h['VxpullFit'].Fill( (mctrack.GetStartX()-xFit)/xFitErr )
382 # self.h['VypullFit'].Fill( (mctrack.GetStartY()-yFit)/yFitErr )
383 # self.h['VzpullFit'].Fill( (mctrack.GetStartZ()-zFit)/zFitErr )
384 # self.h['dVxFit'].Fill( (mctrack.GetStartX()-xFit) )
385 # self.h['dVyFit'].Fill( (mctrack.GetStartY()-yFit) )
386 # self.h['dVzFit'].Fill( (mctrack.GetStartZ()-zFit) )
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 # extract the 6x6 block of parameters 3-8 (slopes and
454 # 1/|p|) from the flat, row-major 9x9 fit covariance
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 # print "******************************************************************************* "
468
469 # create ship particle
470 # notes:
471 # P-TLorentzVector of fitted HNL prticle
472 # covP - covariance matrix of HNL four-vector (Px,Py,Pz,M)
473 # HNLPosFit - fited position of HNL
474 # covV - comariance matrix of the vtx position
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 # try to make it persistent
493 vx = ROOT.TLorentzVector(
494 HNLPosFit, 0
495 ) # time at vertex still needs to be evaluated from time of tracks and time of flight
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 # self.h['dMFit'].Fill( (1.-P.M()) )
503 # self.h['MpullFit'].Fill( (1.-P.M())/ROOT.TMath.Sqrt(covP[3][3]) )
504
505 # self.h['N_Vtx'].Fill(hasVertex)
506

◆ VertexError()

shipVertex.Task.VertexError ( self,
t1,
t2,
PosDir,
CovMat = None,
scalFac = None )

Definition at line 507 of file shipVertex.py.

507 def VertexError(self, t1, t2, PosDir, CovMat=None, scalFac=None):
508 # with improved Vx x,y resolution. Returns (X, covX, dist) where covX is
509 # None when no track covariance was supplied.
510 a, u = PosDir[t1]["position"], PosDir[t1]["direction"]
511 c, v = PosDir[t2]["position"], PosDir[t2]["direction"]
512 Vsq = v.Dot(v)
513 Usq = u.Dot(u)
514 UV = u.Dot(v)
515 ca = c - a
516 denom = Usq * Vsq - UV**2
517 tmp2 = Vsq * u - UV * v
518 Va = ca.Dot(tmp2) / denom
519 tmp2 = UV * u - Usq * v
520 Vb = ca.Dot(tmp2) / denom
521 X = (a + c + Va * u + Vb * v) * 0.5
522 l1 = a - X + u * Va # l2 = c - X + v*Vb
523 dist = 2.0 * ROOT.TMath.Sqrt(l1.Dot(l1))
524 if not CovMat:
525 return X, None, dist
526 assert scalFac is not None, "VertexError requires scalFac when CovMat is set"
527 T = ROOT.TMatrixD(3, 12)
528 for i in range(3):
529 for k in range(4):
530 for j in range(3):
531 KD = 0
532 if i == j:
533 KD = 1
534 if k == 0 or k == 2:
535 # cova and covc
536 temp = (u[j] * Vsq - v[j] * UV) * u[i] + (u[j] * UV - v[j] * Usq) * v[i]
537 sign = -1
538 if k == 2:
539 sign = +1
540 T[i][3 * k + j] = 0.5 * (KD + sign * temp / denom)
541 elif k == 1:
542 # covu
543 aNAZ = denom * (ca[j] * Vsq - v.Dot(ca) * v[j])
544 aZAN = (ca.Dot(u) * Vsq - ca.Dot(v) * UV) * 2 * (u[j] * Vsq - v[j] * UV)
545 bNAZ = denom * (ca[j] * UV + (u.Dot(ca) * v[j]) - 2 * ca.Dot(v) * u[j])
546 bZAN = (ca.Dot(u) * UV - ca.Dot(v) * Usq) * 2 * (u[j] * Vsq - v[j] * UV)
547 T[i][3 * k + j] = 0.5 * (
548 Va * KD + u[i] / denom**2 * (aNAZ - aZAN) + v[i] / denom**2 * (bNAZ - bZAN)
549 )
550 elif k == 3:
551 # covv
552 aNAZ = denom * (2 * ca.Dot(u) * v[j] - ca.Dot(v) * u[j] - ca[j] * UV)
553 aZAN = (ca.Dot(u) * Vsq - ca.Dot(v) * UV) * 2 * (v[j] * Usq - u[j] * UV)
554 bNAZ = denom * (ca.Dot(u) * u[j] - ca[j] * Usq)
555 bZAN = (ca.Dot(u) * UV - ca.Dot(v) * Usq) * 2 * (v[j] * Usq - u[j] * UV)
556 T[i][3 * k + j] = 0.5 * (
557 Vb * KD + u[i] / denom**2 * (aNAZ - aZAN) + v[i] / denom**2 * (bNAZ - bZAN)
558 )
559 transT = ROOT.TMatrixD(12, 3)
560 transT.Transpose(T)
561 CovTracks = ROOT.TMatrixD(12, 12)
562 tlist = [t1, t2]
563 for k in range(2):
564 for i in range(6):
565 for j in range(6):
566 xfac = 1.0
567 if i > 2:
568 xfac = scalFac[tlist[k]]
569 if j > 2:
570 xfac = xfac * scalFac[tlist[k]]
571 CovTracks[i + k * 6][j + k * 6] = CovMat[tlist[k]][i][j] * xfac
572 # if i==5 or j==5 : CovMat[tlist[k]][i][j] = 0 # ignore error on z-direction
573 tmp = ROOT.TMatrixD(3, 12)
574 tmp.Mult(T, CovTracks)
575 covX = ROOT.TMatrixD(3, 3)
576 covX.Mult(tmp, transT)
577 return X, covX, dist
578
579
580# usage
581# import shipVertex
582# VertexError = shipVertex.Task()
583# newPos,doca = myVertexError(t1,t2,PosDirCharge)

Member Data Documentation

◆ fitTrackLoc

str shipVertex.Task.fitTrackLoc = "FitTracks"

Definition at line 30 of file shipVertex.py.

◆ fPartArray

shipVertex.Task.fPartArray = ROOT.std.vector("ShipParticle")()

Definition at line 21 of file shipVertex.py.

◆ goodTracksLoc

str shipVertex.Task.goodTracksLoc = "goodTracks"

Definition at line 31 of file shipVertex.py.

◆ h

shipVertex.Task.h = hp

Definition at line 28 of file shipVertex.py.

◆ LV

dict shipVertex.Task.LV = {1: ROOT.TLorentzVector(), 2: ROOT.TLorentzVector()}

Definition at line 27 of file shipVertex.py.

◆ mcTree

shipVertex.Task.mcTree = mcTree if mcTree is not None else sTree

Definition at line 20 of file shipVertex.py.

◆ newPosDir

dict shipVertex.Task.newPosDir = {}, {}, {}

Definition at line 26 of file shipVertex.py.

◆ Particles

shipVertex.Task.Particles = self.sTree.Branch("Particles", self.fPartArray)

Definition at line 23 of file shipVertex.py.

◆ PDG

shipVertex.Task.PDG = ROOT.TDatabasePDG.Instance()

Definition at line 29 of file shipVertex.py.

◆ reps

shipVertex.Task.reps

Definition at line 26 of file shipVertex.py.

◆ states

shipVertex.Task.states

Definition at line 26 of file shipVertex.py.

◆ sTree

shipVertex.Task.sTree = sTree

Definition at line 19 of file shipVertex.py.

◆ Vy

shipVertex.Task.Vy = np.zeros(100)
static

Definition at line 60 of file shipVertex.py.

◆ y_data

shipVertex.Task.y_data = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
static

Definition at line 58 of file shipVertex.py.

◆ z0

int shipVertex.Task.z0 = 0
static

Definition at line 59 of file shipVertex.py.


The documentation for this class was generated from the following file: