]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliExternalTrackParam.cxx
Additional protection
[u/mrichter/AliRoot.git] / STEER / AliExternalTrackParam.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // Implementation of the external track parameterisation class.              //
21 //                                                                           //
22 // This parameterisation is used to exchange tracks between the detectors.   //
23 // A set of functions returning the position and the momentum of tracks      //
24 // in the global coordinate system as well as the track impact parameters    //
25 // are implemented.
26 // Origin: I.Belikov, CERN, Jouri.Belikov@cern.ch                            //
27 ///////////////////////////////////////////////////////////////////////////////
28 #include "AliExternalTrackParam.h"
29 #include "AliKalmanTrack.h"
30 #include "AliTracker.h"
31 #include "AliStrLine.h"
32 #include "AliESDVertex.h"
33
34
35 ClassImp(AliExternalTrackParam)
36
37 //_____________________________________________________________________________
38 AliExternalTrackParam::AliExternalTrackParam() :
39   fX(0),
40   fAlpha(0)
41 {
42   //
43   // default constructor
44   //
45   for (Int_t i = 0; i < 5; i++) fP[i] = 0;
46   for (Int_t i = 0; i < 15; i++) fC[i] = 0;
47 }
48
49 //_____________________________________________________________________________
50 AliExternalTrackParam::AliExternalTrackParam(Double_t x, Double_t alpha, 
51                                              const Double_t param[5], 
52                                              const Double_t covar[15]) :
53   fX(x),
54   fAlpha(alpha)
55 {
56   //
57   // create external track parameters from given arguments
58   //
59   for (Int_t i = 0; i < 5; i++)  fP[i] = param[i];
60   for (Int_t i = 0; i < 15; i++) fC[i] = covar[i];
61 }
62
63 //_____________________________________________________________________________
64 AliExternalTrackParam::AliExternalTrackParam(const AliKalmanTrack& track) :
65   fAlpha(track.GetAlpha())
66 {
67   //
68   //
69   track.GetExternalParameters(fX,fP);
70   track.GetExternalCovariance(fC);
71 }
72
73 //_____________________________________________________________________________
74 void AliExternalTrackParam::Set(const AliKalmanTrack& track) {
75   //
76   //
77   fAlpha=track.GetAlpha();
78   track.GetExternalParameters(fX,fP);
79   track.GetExternalCovariance(fC);
80 }
81
82 //_____________________________________________________________________________
83 void AliExternalTrackParam::Reset() {
84   fX=fAlpha=0.;
85   for (Int_t i = 0; i < 5; i++) fP[i] = 0;
86   for (Int_t i = 0; i < 15; i++) fC[i] = 0;
87 }
88
89 Double_t AliExternalTrackParam::GetP() const {
90   //---------------------------------------------------------------------
91   // This function returns the track momentum
92   // Results for (nearly) straight tracks are meaningless !
93   //---------------------------------------------------------------------
94   if (TMath::Abs(fP[4])<=kAlmost0) return kVeryBig;
95   return TMath::Sqrt(1.+ fP[3]*fP[3])/TMath::Abs(fP[4]);
96 }
97
98 Double_t AliExternalTrackParam::Get1P() const {
99   //---------------------------------------------------------------------
100   // This function returns the 1/(track momentum)
101   //---------------------------------------------------------------------
102   return TMath::Abs(fP[4])/TMath::Sqrt(1.+ fP[3]*fP[3]);
103 }
104
105 //_______________________________________________________________________
106 Double_t AliExternalTrackParam::GetD(Double_t x,Double_t y,Double_t b) const {
107   //------------------------------------------------------------------
108   // This function calculates the transverse impact parameter
109   // with respect to a point with global coordinates (x,y)
110   // in the magnetic field "b" (kG)
111   //------------------------------------------------------------------
112   if (TMath::Abs(b) < kAlmost0Field) return GetLinearD(x,y);
113   Double_t rp4=kB2C*b*fP[4];
114
115   Double_t xt=fX, yt=fP[0];
116
117   Double_t sn=TMath::Sin(fAlpha), cs=TMath::Cos(fAlpha);
118   Double_t a = x*cs + y*sn;
119   y = -x*sn + y*cs; x=a;
120   xt-=x; yt-=y;
121
122   sn=rp4*xt - fP[2]; cs=rp4*yt + TMath::Sqrt(1.- fP[2]*fP[2]);
123   a=2*(xt*fP[2] - yt*TMath::Sqrt(1.- fP[2]*fP[2]))-rp4*(xt*xt + yt*yt);
124   if (rp4<0) a=-a;
125   return a/(1 + TMath::Sqrt(sn*sn + cs*cs));
126 }
127
128 //_______________________________________________________________________
129 Double_t AliExternalTrackParam::GetLinearD(Double_t xv,Double_t yv) const {
130   //------------------------------------------------------------------
131   // This function calculates the transverse impact parameter
132   // with respect to a point with global coordinates (xv,yv)
133   // neglecting the track curvature.
134   //------------------------------------------------------------------
135   Double_t sn=TMath::Sin(fAlpha), cs=TMath::Cos(fAlpha);
136   Double_t x= xv*cs + yv*sn;
137   Double_t y=-xv*sn + yv*cs;
138
139   Double_t d = (fX-x)*fP[2] - (fP[0]-y)*TMath::Sqrt(1.- fP[2]*fP[2]);
140
141   return d;
142 }
143
144 Bool_t AliExternalTrackParam::
145 CorrectForMaterial(Double_t d,  Double_t x0, Double_t mass) {
146   //------------------------------------------------------------------
147   // This function corrects the track parameters for the crossed material
148   // "d"    - the thickness (fraction of the radiation length)
149   // "x0"   - the radiation length (g/cm^2) 
150   // "mass" - the mass of this particle (GeV/c^2)
151   //------------------------------------------------------------------
152   Double_t &fP2=fP[2];
153   Double_t &fP3=fP[3];
154   Double_t &fP4=fP[4];
155
156   Double_t &fC22=fC[5];
157   Double_t &fC33=fC[9];
158   Double_t &fC43=fC[13];
159   Double_t &fC44=fC[14];
160
161   Double_t p2=(1.+ fP3*fP3)/(fP4*fP4);
162   Double_t beta2=p2/(p2 + mass*mass);
163   d*=TMath::Sqrt((1.+ fP3*fP3)/(1.- fP2*fP2));
164
165   //Multiple scattering******************
166   if (d!=0) {
167      Double_t theta2=14.1*14.1/(beta2*p2*1e6)*TMath::Abs(d);
168      //Double_t theta2=1.0259e-6*14*14/28/(beta2*p2)*TMath::Abs(d)*9.36*2.33;
169      fC22 += theta2*(1.- fP2*fP2)*(1. + fP3*fP3);
170      fC33 += theta2*(1. + fP3*fP3)*(1. + fP3*fP3);
171      fC43 += theta2*fP3*fP4*(1. + fP3*fP3);
172      fC44 += theta2*fP3*fP4*fP3*fP4;
173   }
174
175   //Energy losses************************
176   if (x0!=0.) {
177      d*=x0;
178      Double_t dE=0.153e-3/beta2*(log(5940*beta2/(1-beta2)) - beta2)*d;
179      if (beta2/(1-beta2)>3.5*3.5)
180        dE=0.153e-3/beta2*(log(3.5*5940)+0.5*log(beta2/(1-beta2)) - beta2)*d;
181
182      fP4*=(1.- TMath::Sqrt(p2 + mass*mass)/p2*dE);
183   }
184
185   return kTRUE;
186 }
187
188 Bool_t AliExternalTrackParam::Rotate(Double_t alpha) {
189   //------------------------------------------------------------------
190   // Transform this track to the local coord. system rotated
191   // by angle "alpha" (rad) with respect to the global coord. system. 
192   //------------------------------------------------------------------
193   if      (alpha < -TMath::Pi()) alpha += 2*TMath::Pi();
194   else if (alpha >= TMath::Pi()) alpha -= 2*TMath::Pi();
195
196   Double_t &fP0=fP[0];
197   Double_t &fP2=fP[2];
198   Double_t &fC00=fC[0];
199   Double_t &fC10=fC[1];
200   Double_t &fC20=fC[3];
201   Double_t &fC21=fC[4];
202   Double_t &fC22=fC[5];
203   Double_t &fC30=fC[6];
204   Double_t &fC32=fC[8];
205   Double_t &fC40=fC[10];
206   Double_t &fC42=fC[12];
207
208   Double_t x=fX;
209   Double_t ca=TMath::Cos(alpha-fAlpha), sa=TMath::Sin(alpha-fAlpha);
210   Double_t sf=fP2, cf=TMath::Sqrt(1.- fP2*fP2);
211
212   fAlpha = alpha;
213   fX =  x*ca + fP0*sa;
214   fP0= -x*sa + fP0*ca;
215   fP2=  sf*ca - cf*sa;
216
217   if (TMath::Abs(cf)<kAlmost0) {
218     AliError(Form("Too small cosine value %f",cf)); 
219     cf = kAlmost0;
220   } 
221
222   Double_t rr=(ca+sf/cf*sa);  
223
224   fC00 *= (ca*ca);
225   fC10 *= ca;
226   fC20 *= ca*rr;
227   fC21 *= rr;
228   fC22 *= rr*rr;
229   fC30 *= ca;
230   fC32 *= rr;
231   fC40 *= ca;
232   fC42 *= rr;
233
234   return kTRUE;
235 }
236
237 Bool_t AliExternalTrackParam::PropagateTo(Double_t xk, Double_t b) {
238   //----------------------------------------------------------------
239   // Propagate this track to the plane X=xk (cm) in the field "b" (kG)
240   //----------------------------------------------------------------
241   Double_t dx=xk-fX;
242   if (TMath::Abs(dx)<=0)  return kTRUE;
243
244   Double_t crv=kB2C*b*fP[4];
245   if (TMath::Abs(b) < kAlmost0Field) crv=0.;
246
247   Double_t f1=fP[2], f2=f1 + crv*dx;
248   if (TMath::Abs(f1) >= kAlmost1) return kFALSE;
249   if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
250
251   Double_t &fP0=fP[0], &fP1=fP[1], &fP2=fP[2], &fP3=fP[3], &fP4=fP[4];
252   Double_t 
253   &fC00=fC[0],
254   &fC10=fC[1],   &fC11=fC[2],  
255   &fC20=fC[3],   &fC21=fC[4],   &fC22=fC[5],
256   &fC30=fC[6],   &fC31=fC[7],   &fC32=fC[8],   &fC33=fC[9],  
257   &fC40=fC[10],  &fC41=fC[11],  &fC42=fC[12],  &fC43=fC[13], &fC44=fC[14];
258
259   Double_t r1=TMath::Sqrt(1.- f1*f1), r2=TMath::Sqrt(1.- f2*f2);
260
261   fX=xk;
262   fP0 += dx*(f1+f2)/(r1+r2);
263   fP1 += dx*(r2 + f2*(f1+f2)/(r1+r2))*fP3;  // Many thanks to P.Hristov !
264   fP2 += dx*crv;
265
266   //f = F - 1
267    
268   Double_t f02=    dx/(r1*r1*r1);            Double_t cc=crv/fP4;
269   Double_t f04=0.5*dx*dx/(r1*r1*r1);         f04*=cc;
270   Double_t f12=    dx*fP3*f1/(r1*r1*r1);
271   Double_t f14=0.5*dx*dx*fP3*f1/(r1*r1*r1);  f14*=cc;
272   Double_t f13=    dx/r1;
273   Double_t f24=    dx;                       f24*=cc;
274   
275   //b = C*ft
276   Double_t b00=f02*fC20 + f04*fC40, b01=f12*fC20 + f14*fC40 + f13*fC30;
277   Double_t b02=f24*fC40;
278   Double_t b10=f02*fC21 + f04*fC41, b11=f12*fC21 + f14*fC41 + f13*fC31;
279   Double_t b12=f24*fC41;
280   Double_t b20=f02*fC22 + f04*fC42, b21=f12*fC22 + f14*fC42 + f13*fC32;
281   Double_t b22=f24*fC42;
282   Double_t b40=f02*fC42 + f04*fC44, b41=f12*fC42 + f14*fC44 + f13*fC43;
283   Double_t b42=f24*fC44;
284   Double_t b30=f02*fC32 + f04*fC43, b31=f12*fC32 + f14*fC43 + f13*fC33;
285   Double_t b32=f24*fC43;
286   
287   //a = f*b = f*C*ft
288   Double_t a00=f02*b20+f04*b40,a01=f02*b21+f04*b41,a02=f02*b22+f04*b42;
289   Double_t a11=f12*b21+f14*b41+f13*b31,a12=f12*b22+f14*b42+f13*b32;
290   Double_t a22=f24*b42;
291
292   //F*C*Ft = C + (b + bt + a)
293   fC00 += b00 + b00 + a00;
294   fC10 += b10 + b01 + a01; 
295   fC20 += b20 + b02 + a02;
296   fC30 += b30;
297   fC40 += b40;
298   fC11 += b11 + b11 + a11;
299   fC21 += b21 + b12 + a12;
300   fC31 += b31; 
301   fC41 += b41;
302   fC22 += b22 + b22 + a22;
303   fC32 += b32;
304   fC42 += b42;
305
306   return kTRUE;
307 }
308
309 Double_t 
310 AliExternalTrackParam::GetPredictedChi2(Double_t p[2],Double_t cov[3]) const {
311   //----------------------------------------------------------------
312   // Estimate the chi2 of the space point "p" with the cov. matrix "cov"
313   //----------------------------------------------------------------
314   Double_t sdd = fC[0] + cov[0]; 
315   Double_t sdz = fC[1] + cov[1];
316   Double_t szz = fC[2] + cov[2];
317   Double_t det = sdd*szz - sdz*sdz;
318
319   if (TMath::Abs(det) < kAlmost0) return kVeryBig;
320
321   Double_t d = fP[0] - p[0];
322   Double_t z = fP[1] - p[1];
323
324   return (d*szz*d - 2*d*sdz*z + z*sdd*z)/det;
325 }
326
327 Bool_t AliExternalTrackParam::Update(Double_t p[2], Double_t cov[3]) {
328   //------------------------------------------------------------------
329   // Update the track parameters with the space point "p" having
330   // the covariance matrix "cov"
331   //------------------------------------------------------------------
332   Double_t &fP0=fP[0], &fP1=fP[1], &fP2=fP[2], &fP3=fP[3], &fP4=fP[4];
333   Double_t 
334   &fC00=fC[0],
335   &fC10=fC[1],   &fC11=fC[2],  
336   &fC20=fC[3],   &fC21=fC[4],   &fC22=fC[5],
337   &fC30=fC[6],   &fC31=fC[7],   &fC32=fC[8],   &fC33=fC[9],  
338   &fC40=fC[10],  &fC41=fC[11],  &fC42=fC[12],  &fC43=fC[13], &fC44=fC[14];
339
340   Double_t r00=cov[0], r01=cov[1], r11=cov[2];
341   r00+=fC00; r01+=fC10; r11+=fC11;
342   Double_t det=r00*r11 - r01*r01;
343
344   if (TMath::Abs(det) < kAlmost0) return kFALSE;
345
346
347   Double_t tmp=r00; r00=r11/det; r11=tmp/det; r01=-r01/det;
348  
349   Double_t k00=fC00*r00+fC10*r01, k01=fC00*r01+fC10*r11;
350   Double_t k10=fC10*r00+fC11*r01, k11=fC10*r01+fC11*r11;
351   Double_t k20=fC20*r00+fC21*r01, k21=fC20*r01+fC21*r11;
352   Double_t k30=fC30*r00+fC31*r01, k31=fC30*r01+fC31*r11;
353   Double_t k40=fC40*r00+fC41*r01, k41=fC40*r01+fC41*r11;
354
355   Double_t dy=p[0] - fP0, dz=p[1] - fP1;
356   Double_t sf=fP2 + k20*dy + k21*dz;
357   if (TMath::Abs(sf) > kAlmost1) return kFALSE;  
358   
359   fP0 += k00*dy + k01*dz;
360   fP1 += k10*dy + k11*dz;
361   fP2  = sf;
362   fP3 += k30*dy + k31*dz;
363   fP4 += k40*dy + k41*dz;
364   
365   Double_t c01=fC10, c02=fC20, c03=fC30, c04=fC40;
366   Double_t c12=fC21, c13=fC31, c14=fC41;
367
368   fC00-=k00*fC00+k01*fC10; fC10-=k00*c01+k01*fC11;
369   fC20-=k00*c02+k01*c12;   fC30-=k00*c03+k01*c13;
370   fC40-=k00*c04+k01*c14; 
371
372   fC11-=k10*c01+k11*fC11;
373   fC21-=k10*c02+k11*c12;   fC31-=k10*c03+k11*c13;
374   fC41-=k10*c04+k11*c14; 
375
376   fC22-=k20*c02+k21*c12;   fC32-=k20*c03+k21*c13;
377   fC42-=k20*c04+k21*c14; 
378
379   fC33-=k30*c03+k31*c13;
380   fC43-=k30*c04+k31*c14; 
381
382   fC44-=k40*c04+k41*c14; 
383
384   return kTRUE;
385 }
386
387 void 
388 AliExternalTrackParam::GetHelixParameters(Double_t hlx[6], Double_t b) const {
389   //--------------------------------------------------------------------
390   // External track parameters -> helix parameters 
391   // "b" - magnetic field (kG)
392   //--------------------------------------------------------------------
393   Double_t cs=TMath::Cos(fAlpha), sn=TMath::Sin(fAlpha);
394   
395   hlx[0]=fP[0]; hlx[1]=fP[1]; hlx[2]=fP[2]; hlx[3]=fP[3]; hlx[4]=fP[4];
396
397   hlx[5]=fX*cs - hlx[0]*sn;               // x0
398   hlx[0]=fX*sn + hlx[0]*cs;               // y0
399 //hlx[1]=                                 // z0
400   hlx[2]=TMath::ASin(hlx[2]) + fAlpha;    // phi0
401 //hlx[3]=                                 // tgl
402   hlx[4]=hlx[4]*kB2C*b;                   // C
403 }
404
405
406 static void Evaluate(const Double_t *h, Double_t t,
407                      Double_t r[3],  //radius vector
408                      Double_t g[3],  //first defivatives
409                      Double_t gg[3]) //second derivatives
410 {
411   //--------------------------------------------------------------------
412   // Calculate position of a point on a track and some derivatives
413   //--------------------------------------------------------------------
414   Double_t phase=h[4]*t+h[2];
415   Double_t sn=TMath::Sin(phase), cs=TMath::Cos(phase);
416
417   r[0] = h[5] + (sn - h[6])/h[4];
418   r[1] = h[0] - (cs - h[7])/h[4];  
419   r[2] = h[1] + h[3]*t;
420
421   g[0] = cs; g[1]=sn; g[2]=h[3];
422   
423   gg[0]=-h[4]*sn; gg[1]=h[4]*cs; gg[2]=0.;
424 }
425
426 Double_t AliExternalTrackParam::GetDCA(const AliExternalTrackParam *p, 
427 Double_t b, Double_t &xthis, Double_t &xp) const {
428   //------------------------------------------------------------
429   // Returns the (weighed !) distance of closest approach between 
430   // this track and the track "p".
431   // Other returned values:
432   //   xthis, xt - coordinates of tracks' reference planes at the DCA 
433   //-----------------------------------------------------------
434   Double_t dy2=GetSigmaY2() + p->GetSigmaY2();
435   Double_t dz2=GetSigmaZ2() + p->GetSigmaZ2();
436   Double_t dx2=dy2; 
437
438   //dx2=dy2=dz2=1.;
439
440   Double_t p1[8]; GetHelixParameters(p1,b);
441   p1[6]=TMath::Sin(p1[2]); p1[7]=TMath::Cos(p1[2]);
442   Double_t p2[8]; p->GetHelixParameters(p2,b);
443   p2[6]=TMath::Sin(p2[2]); p2[7]=TMath::Cos(p2[2]);
444
445
446   Double_t r1[3],g1[3],gg1[3]; Double_t t1=0.;
447   Evaluate(p1,t1,r1,g1,gg1);
448   Double_t r2[3],g2[3],gg2[3]; Double_t t2=0.;
449   Evaluate(p2,t2,r2,g2,gg2);
450
451   Double_t dx=r2[0]-r1[0], dy=r2[1]-r1[1], dz=r2[2]-r1[2];
452   Double_t dm=dx*dx/dx2 + dy*dy/dy2 + dz*dz/dz2;
453
454   Int_t max=27;
455   while (max--) {
456      Double_t gt1=-(dx*g1[0]/dx2 + dy*g1[1]/dy2 + dz*g1[2]/dz2);
457      Double_t gt2=+(dx*g2[0]/dx2 + dy*g2[1]/dy2 + dz*g2[2]/dz2);
458      Double_t h11=(g1[0]*g1[0] - dx*gg1[0])/dx2 + 
459                   (g1[1]*g1[1] - dy*gg1[1])/dy2 +
460                   (g1[2]*g1[2] - dz*gg1[2])/dz2;
461      Double_t h22=(g2[0]*g2[0] + dx*gg2[0])/dx2 + 
462                   (g2[1]*g2[1] + dy*gg2[1])/dy2 +
463                   (g2[2]*g2[2] + dz*gg2[2])/dz2;
464      Double_t h12=-(g1[0]*g2[0]/dx2 + g1[1]*g2[1]/dy2 + g1[2]*g2[2]/dz2);
465
466      Double_t det=h11*h22-h12*h12;
467
468      Double_t dt1,dt2;
469      if (TMath::Abs(det)<1.e-33) {
470         //(quasi)singular Hessian
471         dt1=-gt1; dt2=-gt2;
472      } else {
473         dt1=-(gt1*h22 - gt2*h12)/det; 
474         dt2=-(h11*gt2 - h12*gt1)/det;
475      }
476
477      if ((dt1*gt1+dt2*gt2)>0) {dt1=-dt1; dt2=-dt2;}
478
479      //check delta(phase1) ?
480      //check delta(phase2) ?
481
482      if (TMath::Abs(dt1)/(TMath::Abs(t1)+1.e-3) < 1.e-4)
483      if (TMath::Abs(dt2)/(TMath::Abs(t2)+1.e-3) < 1.e-4) {
484         if ((gt1*gt1+gt2*gt2) > 1.e-4/dy2/dy2) 
485           AliWarning(" stopped at not a stationary point !");
486         Double_t lmb=h11+h22; lmb=lmb-TMath::Sqrt(lmb*lmb-4*det);
487         if (lmb < 0.) 
488           AliWarning(" stopped at not a minimum !");
489         break;
490      }
491
492      Double_t dd=dm;
493      for (Int_t div=1 ; ; div*=2) {
494         Evaluate(p1,t1+dt1,r1,g1,gg1);
495         Evaluate(p2,t2+dt2,r2,g2,gg2);
496         dx=r2[0]-r1[0]; dy=r2[1]-r1[1]; dz=r2[2]-r1[2];
497         dd=dx*dx/dx2 + dy*dy/dy2 + dz*dz/dz2;
498         if (dd<dm) break;
499         dt1*=0.5; dt2*=0.5;
500         if (div>512) {
501            AliWarning(" overshoot !"); break;
502         }   
503      }
504      dm=dd;
505
506      t1+=dt1;
507      t2+=dt2;
508
509   }
510
511   if (max<=0) AliWarning(" too many iterations !");
512
513   Double_t cs=TMath::Cos(GetAlpha());
514   Double_t sn=TMath::Sin(GetAlpha());
515   xthis=r1[0]*cs + r1[1]*sn;
516
517   cs=TMath::Cos(p->GetAlpha());
518   sn=TMath::Sin(p->GetAlpha());
519   xp=r2[0]*cs + r2[1]*sn;
520
521   return TMath::Sqrt(dm*TMath::Sqrt(dy2*dz2));
522 }
523  
524 Double_t AliExternalTrackParam::
525 PropagateToDCA(AliExternalTrackParam *p, Double_t b) {
526   //--------------------------------------------------------------
527   // Propagates this track and the argument track to the position of the
528   // distance of closest approach.
529   // Returns the (weighed !) distance of closest approach.
530   //--------------------------------------------------------------
531   Double_t xthis,xp;
532   Double_t dca=GetDCA(p,b,xthis,xp);
533
534   if (!PropagateTo(xthis,b)) {
535     //AliWarning(" propagation failed !");
536     return 1e+33;
537   }
538
539   if (!p->PropagateTo(xp,b)) {
540     //AliWarning(" propagation failed !";
541     return 1e+33;
542   }
543
544   return dca;
545 }
546
547
548
549
550 Bool_t AliExternalTrackParam::PropagateToDCA(const AliESDVertex *vtx, Double_t b, Double_t maxd){
551   //
552   // Try to relate this track to the vertex "vtx", 
553   // if the (rough) transverse impact parameter is not bigger then "maxd". 
554   //            Magnetic field is "b" (kG).
555   //
556   // a) The track gets extapolated to the DCA to the vertex.
557   // b) The impact parameters and their covariance matrix are calculated.
558   //
559   //    In the case of success, the returned value is kTRUE
560   //    (otherwise, it's kFALSE)
561   //  
562   Double_t alpha=GetAlpha();
563   Double_t sn=TMath::Sin(alpha), cs=TMath::Cos(alpha);
564   Double_t x=GetX(), y=GetParameter()[0], snp=GetParameter()[2];
565   Double_t xv= vtx->GetXv()*cs + vtx->GetYv()*sn;
566   Double_t yv=-vtx->GetXv()*sn + vtx->GetYv()*cs;
567   x-=xv; y-=yv;
568
569   //Estimate the impact parameter neglecting the track curvature
570   Double_t d=TMath::Abs(x*snp - y*TMath::Sqrt(1.- snp*snp));
571   if (d > maxd) return kFALSE; 
572
573   //Propagate to the DCA
574   Double_t crv=0.299792458e-3*b*GetParameter()[4];
575   Double_t tgfv=-(crv*x - snp)/(crv*y + TMath::Sqrt(1.-snp*snp));
576   sn=tgfv/TMath::Sqrt(1.+ tgfv*tgfv); cs=TMath::Sqrt(1.- sn*sn);
577
578   x = xv*cs + yv*sn;
579   yv=-xv*sn + yv*cs; xv=x;
580
581   if (!Propagate(alpha+TMath::ASin(sn),xv,b)) return kFALSE;
582   return kTRUE;
583 }
584
585
586
587
588 Bool_t Local2GlobalMomentum(Double_t p[3],Double_t alpha) {
589   //----------------------------------------------------------------
590   // This function performs local->global transformation of the
591   // track momentum.
592   // When called, the arguments are:
593   //    p[0] = 1/pt of the track;
594   //    p[1] = sine of local azim. angle of the track momentum;
595   //    p[2] = tangent of the track momentum dip angle;
596   //   alpha - rotation angle. 
597   // The result is returned as:
598   //    p[0] = px
599   //    p[1] = py
600   //    p[2] = pz
601   // Results for (nearly) straight tracks are meaningless !
602   //----------------------------------------------------------------
603   if (TMath::Abs(p[0])<=0)        return kFALSE;
604   if (TMath::Abs(p[1])> kAlmost1) return kFALSE;
605
606   Double_t pt=1./TMath::Abs(p[0]);
607   Double_t cs=TMath::Cos(alpha), sn=TMath::Sin(alpha);
608   Double_t r=TMath::Sqrt(1 - p[1]*p[1]);
609   p[0]=pt*(r*cs - p[1]*sn); p[1]=pt*(p[1]*cs + r*sn); p[2]=pt*p[2];
610
611   return kTRUE;
612 }
613
614 Bool_t Local2GlobalPosition(Double_t r[3],Double_t alpha) {
615   //----------------------------------------------------------------
616   // This function performs local->global transformation of the
617   // track position.
618   // When called, the arguments are:
619   //    r[0] = local x
620   //    r[1] = local y
621   //    r[2] = local z
622   //   alpha - rotation angle. 
623   // The result is returned as:
624   //    r[0] = global x
625   //    r[1] = global y
626   //    r[2] = global z
627   //----------------------------------------------------------------
628   Double_t cs=TMath::Cos(alpha), sn=TMath::Sin(alpha), x=r[0];
629   r[0]=x*cs - r[1]*sn; r[1]=x*sn + r[1]*cs;
630
631   return kTRUE;
632 }
633
634 Bool_t AliExternalTrackParam::GetPxPyPz(Double_t *p) const {
635   //---------------------------------------------------------------------
636   // This function returns the global track momentum components
637   // Results for (nearly) straight tracks are meaningless !
638   //---------------------------------------------------------------------
639   p[0]=fP[4]; p[1]=fP[2]; p[2]=fP[3];
640   return Local2GlobalMomentum(p,fAlpha);
641 }
642
643 Bool_t AliExternalTrackParam::GetXYZ(Double_t *r) const {
644   //---------------------------------------------------------------------
645   // This function returns the global track position
646   //---------------------------------------------------------------------
647   r[0]=fX; r[1]=fP[0]; r[2]=fP[1];
648   return Local2GlobalPosition(r,fAlpha);
649 }
650
651 Bool_t AliExternalTrackParam::GetCovarianceXYZPxPyPz(Double_t cv[21]) const {
652   //---------------------------------------------------------------------
653   // This function returns the global covariance matrix of the track params
654   // 
655   // Cov(x,x) ... :   cv[0]
656   // Cov(y,x) ... :   cv[1]  cv[2]
657   // Cov(z,x) ... :   cv[3]  cv[4]  cv[5]
658   // Cov(px,x)... :   cv[6]  cv[7]  cv[8]  cv[9]
659   // Cov(py,x)... :   cv[10] cv[11] cv[12] cv[13] cv[14]
660   // Cov(pz,x)... :   cv[15] cv[16] cv[17] cv[18] cv[19] cv[20]
661   //
662   // Results for (nearly) straight tracks are meaningless !
663   //---------------------------------------------------------------------
664   if (TMath::Abs(fP[4])<=0) {
665      for (Int_t i=0; i<21; i++) cv[i]=0.;
666      return kFALSE;
667   }
668   if (TMath::Abs(fP[2]) > kAlmost1) {
669      for (Int_t i=0; i<21; i++) cv[i]=0.;
670      return kFALSE;
671   }
672   Double_t pt=1./TMath::Abs(fP[4]);
673   Double_t cs=TMath::Cos(fAlpha), sn=TMath::Sin(fAlpha);
674   Double_t r=TMath::Sqrt(1-fP[2]*fP[2]);
675
676   Double_t m00=-sn, m10=cs;
677   Double_t m23=-pt*(sn + fP[2]*cs/r), m43=-pt*pt*(r*cs - fP[2]*sn);
678   Double_t m24= pt*(cs - fP[2]*sn/r), m44=-pt*pt*(r*sn + fP[2]*cs);
679   Double_t m35=pt, m45=-pt*pt*fP[3];
680
681   cv[0 ] = fC[0]*m00*m00;
682   cv[1 ] = fC[0]*m00*m10; 
683   cv[2 ] = fC[0]*m10*m10;
684   cv[3 ] = fC[1]*m00; 
685   cv[4 ] = fC[1]*m10; 
686   cv[5 ] = fC[2];
687   cv[6 ] = m00*(fC[3]*m23 + fC[10]*m43); 
688   cv[7 ] = m10*(fC[3]*m23 + fC[10]*m43); 
689   cv[8 ] = fC[4]*m23 + fC[11]*m43; 
690   cv[9 ] = m23*(fC[5]*m23 + fC[12]*m43)  +  m43*(fC[12]*m23 + fC[14]*m43);
691   cv[10] = m00*(fC[3]*m24 + fC[10]*m44); 
692   cv[11] = m10*(fC[3]*m24 + fC[10]*m44); 
693   cv[12] = fC[4]*m24 + fC[11]*m44; 
694   cv[13] = m23*(fC[5]*m24 + fC[12]*m44)  +  m43*(fC[12]*m24 + fC[14]*m44);
695   cv[14] = m24*(fC[5]*m24 + fC[12]*m44)  +  m44*(fC[12]*m24 + fC[14]*m44);
696   cv[15] = m00*(fC[6]*m35 + fC[10]*m45); 
697   cv[16] = m10*(fC[6]*m35 + fC[10]*m45); 
698   cv[17] = fC[7]*m35 + fC[11]*m45; 
699   cv[18] = m23*(fC[8]*m35 + fC[12]*m45)  +  m43*(fC[13]*m35 + fC[14]*m45);
700   cv[19] = m24*(fC[8]*m35 + fC[12]*m45)  +  m44*(fC[13]*m35 + fC[14]*m45); 
701   cv[20] = m35*(fC[9]*m35 + fC[13]*m45)  +  m45*(fC[13]*m35 + fC[14]*m45);
702
703   return kTRUE;
704 }
705
706
707 Bool_t 
708 AliExternalTrackParam::GetPxPyPzAt(Double_t x, Double_t b, Double_t *p) const {
709   //---------------------------------------------------------------------
710   // This function returns the global track momentum extrapolated to
711   // the radial position "x" (cm) in the magnetic field "b" (kG)
712   //---------------------------------------------------------------------
713   p[0]=fP[4]; 
714   p[1]=fP[2]+(x-fX)*fP[4]*b*kB2C; 
715   p[2]=fP[3];
716   return Local2GlobalMomentum(p,fAlpha);
717 }
718
719 Bool_t 
720 AliExternalTrackParam::GetXYZAt(Double_t x, Double_t b, Double_t *r) const {
721   //---------------------------------------------------------------------
722   // This function returns the global track position extrapolated to
723   // the radial position "x" (cm) in the magnetic field "b" (kG)
724   //---------------------------------------------------------------------
725   Double_t dx=x-fX;
726   Double_t f1=fP[2], f2=f1 + dx*fP[4]*b*kB2C;
727
728   if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
729   
730   Double_t r1=TMath::Sqrt(1.- f1*f1), r2=TMath::Sqrt(1.- f2*f2);
731   r[0] = x;
732   r[1] = fP[0] + dx*(f1+f2)/(r1+r2);
733   r[2] = fP[1] + dx*(f1+f2)/(f1*r2 + f2*r1)*fP[3];
734   return Local2GlobalPosition(r,fAlpha);
735 }
736
737
738 //_____________________________________________________________________________
739 void AliExternalTrackParam::ApproximateHelixWithLine(Double_t xk, Double_t b, AliStrLine *line)
740 {
741   //------------------------------------------------------------
742   // Approximate the track (helix) with a straight line tangent to the
743   // helix in the point defined by r (F. Prino, prino@to.infn.it)
744   //------------------------------------------------------------
745   Double_t mom[3];
746   Double_t azim = TMath::ASin(fP[2])+fAlpha;
747   Double_t theta = TMath::Pi()/2. - TMath::ATan(fP[3]);
748   mom[0] = TMath::Sin(theta)*TMath::Cos(azim);
749   mom[1] = TMath::Sin(theta)*TMath::Sin(azim);
750   mom[2] = TMath::Cos(theta);
751   Double_t pos[3];
752   GetXYZAt(xk,b,pos);
753   line->SetP0(pos);
754   line->SetCd(mom);
755 }
756 //_____________________________________________________________________________
757 void AliExternalTrackParam::Print(Option_t* /*option*/) const
758 {
759 // print the parameters and the covariance matrix
760
761   printf("AliExternalTrackParam: x = %-12g  alpha = %-12g\n", fX, fAlpha);
762   printf("  parameters: %12g %12g %12g %12g %12g\n",
763          fP[0], fP[1], fP[2], fP[3], fP[4]);
764   printf("  covariance: %12g\n", fC[0]);
765   printf("              %12g %12g\n", fC[1], fC[2]);
766   printf("              %12g %12g %12g\n", fC[3], fC[4], fC[5]);
767   printf("              %12g %12g %12g %12g\n", 
768          fC[6], fC[7], fC[8], fC[9]);
769   printf("              %12g %12g %12g %12g %12g\n", 
770          fC[10], fC[11], fC[12], fC[13], fC[14]);
771 }
772
773
774 Bool_t AliExternalTrackParam::PropagateTo(Double_t xToGo, Double_t mass, Double_t maxStep, Bool_t rotateTo){
775   //----------------------------------------------------------------
776   // Propagate this track to the plane X=xk (cm) 
777   // correction for unhomogenity of the magnetic field and the
778   // the correction for the material is included
779   //
780   //  Require acces to magnetic field and geomanager
781   //
782   // mass     - mass used in propagation - used for energy loss correction
783   // maxStep  - maximal step for propagation
784   //----------------------------------------------------------------
785   const Double_t kEpsilon = 0.00001;
786   Double_t xpos     = GetX();
787   Double_t dir      = (xpos<xToGo) ? 1.:-1.;
788   //
789   while ( (xToGo-xpos)*dir > kEpsilon){
790     Double_t step = dir*TMath::Min(TMath::Abs(xToGo-xpos), maxStep);
791     Double_t x    = xpos+step;
792     Double_t xyz0[3],xyz1[3],param[7];
793     GetXYZ(xyz0);   //starting global position
794     Float_t  pos0[3] = {xyz0[0],xyz0[1],xyz0[2]};
795     Double_t magZ = AliTracker::GetBz(pos0);
796     if (!GetXYZAt(x,magZ,xyz1)) return kFALSE;   // no prolongation
797     AliKalmanTrack::MeanMaterialBudget(xyz0,xyz1,param);        
798     if (!PropagateTo(x,magZ))  return kFALSE;
799     Double_t distance = param[4];
800     if (!CorrectForMaterial(distance,param[1],param[0],mass)) return kFALSE;
801     if (rotateTo){
802       GetXYZ(xyz0);   // global position
803       Double_t alphan = TMath::ATan2(xyz0[1], xyz0[0]);
804       if (!Rotate(alphan)) return kFALSE;
805     }
806     xpos = GetX();
807   }
808   return kTRUE;
809 }
810
811 //_____________________________________________________________________________
812 Bool_t AliExternalTrackParam::CorrectForMaterial(Double_t d, Double_t x0, Double_t rho, Double_t mass)
813 {
814   //
815   // Take into account material effects assuming:
816   // x0  - mean rad length
817   // rho - mean density
818
819   //
820   // multiple scattering
821   //
822   if (mass<=0) {
823     AliError("Non-positive mass");
824     return kFALSE;
825   }
826   Double_t p2=(1.+ fP[3]*fP[3])/(fP[4]*fP[4]);
827   Double_t beta2=p2/(p2 + mass*mass);
828   Double_t theta2=14.1*14.1/(beta2*p2*1e6)*d/x0*rho;
829   //
830   fC[5] += theta2*(1.- fP[2]*fP[2])*(1. + fP[3]*fP[3]);
831   fC[9] += theta2*(1. + fP[3]*fP[3])*(1. + fP[3]*fP[3]);
832   fC[13] += theta2*fP[3]*fP[4]*(1. + fP[3]*fP[3]);
833   fC[14] += theta2*fP[3]*fP[4]*fP[3]*fP[4];
834   //
835   Double_t dE=0.153e-3/beta2*(log(5940*beta2/(1-beta2+1e-10)) - beta2)*d*rho;  
836   fP[4] *=(1.- TMath::Sqrt(p2+mass*mass)/p2*dE);
837   //
838   Double_t sigmade = 0.02*TMath::Sqrt(TMath::Abs(dE));   // energy loss fluctuation 
839   Double_t sigmac2 = sigmade*sigmade*fP[4]*fP[4]*(p2+mass*mass)/(p2*p2);
840   fC[14] += sigmac2;
841   return kTRUE;
842 }
843
844