78 def TwoTrackVertex(self):
79 self.fPartArray.Delete()
80 fittedTracks = getattr(self.sTree,self.fitTrackLoc)
81 goodTracks = getattr(self.sTree,self.goodTracksLoc)
82 if goodTracks.size() < 2: return
83 particles = self.fPartArray
84 PosDirCharge,CovMat,scalFac = {},{},{}
85 for tr in goodTracks:
86 fitStatus = fittedTracks[tr].getFitStatus()
87 xx = fittedTracks[tr].getFittedState()
88 pid = xx.getPDG()
89 if not global_variables.pidProton and abs(pid) == 2212:
90 pid = int(math.copysign(211,pid))
91 rep = ROOT.genfit.RKTrackRep(xx.getPDG())
92 state = ROOT.genfit.StateOnPlane(rep)
93 rep.setPosMom(state,xx.getPos(),xx.getMom())
94 PosDirCharge[tr] = {'position':xx.getPos(),'direction':xx.getDir(),\
95 'momentum':xx.getMom(),'charge':xx.getCharge(),'pdgCode':pid,'state':xx,'rep':rep,'newstate':state}
96 CovMat[tr] = xx.get6DCov()
97
98 if len(PosDirCharge) < 2: return
99 if len(PosDirCharge) > 4: return
100 for t1 in PosDirCharge:
101 c1 = PosDirCharge[t1]['charge']
102 for t2 in PosDirCharge:
103 if not t2>t1: continue
104
105 if PosDirCharge[t2]['charge'] == c1 : continue
106 newPos,doca = self.VertexError(t1,t2,PosDirCharge)
107
108 dz = 99999.
109 rc = True
110 step = 0
111 while dz > 0.01:
112 zBefore = newPos[2]
113
114 for tr in [t1,t2]:
115 try:
116 PosDirCharge[tr]['rep'].extrapolateToPoint(PosDirCharge[tr]['newstate'], newPos, False)
117 except:
118 ut.reportError('shipVertex: extrapolation did not worked')
119 rc = False
120 break
121 self.newPosDir[tr] = {'position':PosDirCharge[tr]['rep'].getPos(PosDirCharge[tr]['newstate']),\
122 'direction':PosDirCharge[tr]['rep'].getDir(PosDirCharge[tr]['newstate']),\
123 'momentum':PosDirCharge[tr]['rep'].getMom(PosDirCharge[tr]['newstate'])}
124 if not rc: break
125 newPos,doca = self.VertexError(t1,t2,self.newPosDir)
126 dz = abs(zBefore-newPos[2])
127 step+=1
128 if step > 10:
129 ut.reportError("shipVertex::abort iteration, too many steps")
130 if global_variables.debug:
131 print('abort iteration, too many steps, pos=',newPos[0],newPos[1],newPos[2],' doca=',doca,'z before and dz',zBefore,dz)
132 rc = False
133 break
134
135 if not rc: continue
136
137 scalFac[t1] = (PosDirCharge[t1]['position'][2]-newPos[2])/PosDirCharge[t1]['direction'][2]/PosDirCharge[t1]['momentum'].Mag()
138 scalFac[t2] = (PosDirCharge[t2]['position'][2]-newPos[2])/PosDirCharge[t2]['direction'][2]/PosDirCharge[t2]['momentum'].Mag()
139 HNLPos,covX,dist = self.VertexError(t1,t2,self.newPosDir,CovMat,scalFac)
140
141
142
143
144 if self.sTree.GetBranch("fitTrack2MC"):
145 mctrack = self.sTree.MCTrack[self.sTree.fitTrack2MC[t1]]
146 mctrack2 = self.sTree.MCTrack[self.sTree.fitTrack2MC[t2]]
147 mcHNL = self.sTree.MCTrack[mctrack.GetMotherId()]
148
149
150
151
152
153
154
155
156
157
158
159
160 detPlane = ROOT.genfit.DetPlane(ROOT.TVector3(0,0,HNLPos[2]),ROOT.TVector3(1,0,0),ROOT.TVector3(0,1,0))
161 plane = ROOT.genfit.RKTrackRep().makePlane(ROOT.TVector3(0,0,HNLPos[2]),ROOT.TVector3(1,0,0),ROOT.TVector3(0,1,0))
162 st1 = fittedTracks[t1].getFittedState()
163 st2 = fittedTracks[t2].getFittedState()
164 try:
165 st1.extrapolateToPlane(plane)
166 except:
167 ut.reportError("shipVertex::TwoTrackVertex: extrapolation does not worked")
168 continue
169 try:
170 st2.extrapolateToPlane(plane)
171 except:
172 ut.reportError("shipVertex::TwoTrackVertex: extrapolation does not worked")
173 continue
174 mom1 = st1.getMom()
175 mom2 = st2.getMom()
176 cov1 = st1.getCov()
177 cov2 = st2.getCov()
178 cov = ROOT.TMatrixDSym(10)
179 for i in range(10):
180 for j in range(10):
181 if i<5 and j<5: cov[i][j]=cov1[i][j]
182 if i>4 and j>4: cov[i][j]=cov2[i-5][j-5]
183 covInv = ROOT.TMatrixDSym()
184 ROOT.genfit.tools.invertMatrix(cov,covInv)
185
186 self.y_data = np.array([0.,0.,0.,0.,0.,0.,0.,0.,0.,0.])
187 stVal1 = st1.getState()
188 stVal2 = st2.getState()
189 for i in range(5):
190 self.y_data[i]=stVal1[i]
191 self.y_data[i+5]=stVal2[i]
192 self.z0 = HNLPos[2]
193 self.Vy = np.zeros(100)
194 for i in range(100):
195 self.Vy[i] = covInv[i//10][i%10]
196
197 f=np.array([0.])
198 gMinuit = ROOT.TMinuit(9)
199 tempFcn = self.fcn
200 gMinuit.SetFCN(tempFcn)
201 gMinuit.SetPrintLevel(-1)
202 rc = gMinuit.DefineParameter(0, 'X pos',HNLPos[0], 0.1,0,0)
203 rc = gMinuit.DefineParameter(1, 'Y pos',HNLPos[1], 0.1,0,0)
204 rc = gMinuit.DefineParameter(2, 'Z pos',HNLPos[2], 0.1,0,0)
205 rc = gMinuit.DefineParameter(3, 'tan1X',mom1[0]/mom1[2], 0.1,0,0)
206 rc = gMinuit.DefineParameter(4, 'tan1Y',mom1[1]/mom1[2], 0.1,0,0)
207 rc = gMinuit.DefineParameter(5, '1/mom1',1./mom1.Mag(), 0.1,0,0)
208 rc = gMinuit.DefineParameter(6, 'tan2X',mom2[0]/mom2[2], 0.1,0,0)
209 rc = gMinuit.DefineParameter(7, 'tan2Y',mom2[1]/mom2[2], 0.1,0,0)
210 rc = gMinuit.DefineParameter(8, '1/mom2',1./mom2.Mag(), 0.1,0,0)
211 gMinuit.Clear()
212 gMinuit.Migrad()
213 try:
214 tmp = array('d',[0])
215 err = array('i',[0])
216 gMinuit.mnexcm( "HESSE", tmp, -1, err )
217
218 except:
219 ut.reportError("shipVertex::minos does not work")
220 continue
221
222 emat = array('d',[0,]*81)
223 gMinuit.mnemat(emat,9)
224 values = array('d',[0,]*9)
225 errors = array('d',[0,]*9)
226 dValue = ctypes.c_double()
227 dError = ctypes.c_double()
228 rc = gMinuit.GetParameter(0, dValue, dError)
229 values[0]=dValue.value
230 errors[0]=dError.value
231 rc = gMinuit.GetParameter(1, dValue, dError)
232 values[1]=dValue.value
233 errors[1]=dError.value
234 rc = gMinuit.GetParameter(2, dValue, dError)
235 values[2]=dValue.value
236 errors[2]=dError.value
237 rc = gMinuit.GetParameter(3, dValue, dError)
238 values[3]=dValue.value
239 errors[3]=dError.value
240 rc = gMinuit.GetParameter(4, dValue, dError)
241 values[4]=dValue.value
242 errors[4]=dError.value
243 rc = gMinuit.GetParameter(5, dValue, dError)
244 values[5]=dValue.value
245 errors[5]=dError.value
246 rc = gMinuit.GetParameter(6, dValue, dError)
247 values[6]=dValue.value
248 errors[6]=dError.value
249 rc = gMinuit.GetParameter(7, dValue, dError)
250 values[7]=dValue.value
251 errors[7]=dError.value
252 rc = gMinuit.GetParameter(8, dValue, dError)
253 values[8]=dValue.value
254 errors[8]=dError.value
255
256 xFit = values[0]
257 yFit = values[1]
258 zFit = values[2]
259 HNLPosFit = ROOT.TVector3(xFit,yFit,zFit)
260 xFitErr = errors[0]
261 yFitErr = errors[1]
262 zFitErr = errors[2]
263
264
265 m1 = self.PDG.GetParticle(PosDirCharge[t1]['pdgCode']).Mass()
266 m2 = self.PDG.GetParticle(PosDirCharge[t2]['pdgCode']).Mass()
267
268
269
270
271
272
273
274
275 def getP(fitValues,cov,m1,m2):
276 a3=fitValues[3]
277 a4=fitValues[4]
278 a5=fitValues[5]
279 a6=fitValues[6]
280 a7=fitValues[7]
281 a8=fitValues[8]
282 px1 = a3/(a5*ROOT.TMath.Sqrt(1 + a3**2 + a4**2))
283 py1 = a4/(a5*ROOT.TMath.Sqrt(1 + a3**2 + a4**2))
284 pz1 = 1/(a5*ROOT.TMath.Sqrt(1 + a3**2 + a4**2))
285 px2 = a6/(a8*ROOT.TMath.Sqrt(1 + a6**2 + a7**2))
286 py2 = a7/(a8*ROOT.TMath.Sqrt(1 + a6**2 + a7**2))
287 pz2 = 1/(a8*ROOT.TMath.Sqrt(1 + a6**2 + a7**2))
288 Px = px1 + px2
289 Py = py1 + py2
290 Pz = pz1 + pz2
291 E1 = ROOT.TMath.Sqrt(px1**2 + py1**2 + pz1**2 + m1**2)
292 E2 = ROOT.TMath.Sqrt(px2**2 + py2**2 + pz2**2 + m2**2)
293 M = ROOT.TMath.Sqrt(2*E1*E2+m1**2+m2**2-2*pz1*pz2*(1+a3*a6+a4*a7))
294 MM = 2*M
295 A5 = 1 + a3**2 + a4**2
296 A8 = 1 + a6**2 + a7**2
297 M_AtoP = ROOT.TMatrixD(4,6)
298 MT_AtoP = ROOT.TMatrixD(6,4)
299 covA = ROOT.TMatrixD(6,6)
300 M_AtoP[0][0] = (1.-a3*a3/A5)/(a5*ROOT.TMath.Sqrt(A5))
301 M_AtoP[0][1] = (-a3*a4/A5)/(a5*ROOT.TMath.Sqrt(A5))
302 M_AtoP[0][2] = (-a3)/(a5*a5*ROOT.TMath.Sqrt(A5))
303 M_AtoP[0][3] = (1.-a6*a6/A8)/(a8*ROOT.TMath.Sqrt(A8))
304 M_AtoP[0][4] = (-a6*a7/A8)/(a8*ROOT.TMath.Sqrt(A8))
305 M_AtoP[0][5] = (-a6)/(a8*a8*ROOT.TMath.Sqrt(A8))
306
307 M_AtoP[1][0] = (-a3*a4/A5)/(a5*ROOT.TMath.Sqrt(A5))
308 M_AtoP[1][1] = (1.-a4*a4/A5)/(a5*ROOT.TMath.Sqrt(A5))
309 M_AtoP[1][2] = (-a4)/(a5*a5*ROOT.TMath.Sqrt(A5))
310 M_AtoP[1][3] = (-a6*a7/A8)/(a8*ROOT.TMath.Sqrt(A8))
311 M_AtoP[1][4] = (1.-a7*a7/A8)/(a8*ROOT.TMath.Sqrt(A8))
312 M_AtoP[1][5] = (-a7)/(a8*a8*ROOT.TMath.Sqrt(A8))
313
314 M_AtoP[2][0] = (-a3/A5)/(a5*ROOT.TMath.Sqrt(A5))
315 M_AtoP[2][1] = (-a4/A5)/(a5*ROOT.TMath.Sqrt(A5))
316 M_AtoP[2][2] = (-1.)/(a5*a5*ROOT.TMath.Sqrt(A5))
317 M_AtoP[2][3] = (-a6/A8)/(a8*ROOT.TMath.Sqrt(A8))
318 M_AtoP[2][4] = (-a7/A8)/(a8*ROOT.TMath.Sqrt(A8))
319 M_AtoP[2][5] = (-1.)/(a8*a8*ROOT.TMath.Sqrt(A8))
320
321 a5a8 = a5*a8*ROOT.TMath.Sqrt(A5)*ROOT.TMath.Sqrt(A8)
322
323 M_AtoP[3][0] = (-2*a6/a5a8 + 2*a3*E2/(a5*a5*A5*E1))/MM
324 M_AtoP[3][1] = (-2*a7/a5a8 + 2*a4*E2/(a5*a5*A5*E1))/MM
325 M_AtoP[3][2] = (2*(1+a3*a6+a4*a7)/(a5*a5a8) - 2*(1.+a3*a3+a4*a4)*E2/(a5*a5*a5*A5*E1))/MM
326 M_AtoP[3][3] = (-2*a3/a5a8 + 2*a6*E1/(a8*a8*A8*E2))/MM
327 M_AtoP[3][4] = (-2*a4/a5a8 + 2*a7*E1/(a8*a8*A8*E2))/MM
328 M_AtoP[3][5] = (2*(1+a3*a6+a4*a7)/(a8*a5a8) - 2*(1.+a6*a6+a7*a7)*E1/(a8*a8*a8*A8*E2))/MM
329
330 for i in range(4):
331 for j in range(6):
332 MT_AtoP[j][i] = M_AtoP[i][j]
333
334 for i in range(36):
335 covA[i//6][i%6] = cov[i//6+3+(i%6+3)*9]
336
337 tmp = ROOT.TMatrixD(4,6)
338 tmp.Mult(M_AtoP,covA)
339 covP = ROOT.TMatrixD(4,4)
340 covP.Mult(tmp,MT_AtoP)
341 P = ROOT.TLorentzVector()
342 P.SetXYZM(Px,Py,Pz,M)
343 return P,covP
344
345 P,covP = getP(values,emat,m1,m2)
346
347
348
349
350
351
352
353
354 covV = array('d',[emat[0],emat[1],emat[2],emat[1+9],emat[2+9],emat[2+2*9]])
355 covP = array('d',[covP[0][0],covP[0][1],covP[0][2],covP[0][3],covP[1][1],covP[1][2],covP[1][3],covP[2][2],covP[2][3],covP[3][3]])
356
357
358 vx = ROOT.TLorentzVector(HNLPosFit,0)
359 particle = ROOT.ShipParticle(9900015,0,-1,-1,t1,t2,P,vx)
360 particle.SetCovV(covV)
361 particle.SetCovP(covP)
362 particle.SetDoca(doca)
363 nParts = particles.GetEntries()
364 particles[nParts] = particle
365
366
367
368
369
370