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