]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliKalmanTrack.cxx
Corrections to obey the coding conventions
[u/mrichter/AliRoot.git] / STEER / AliKalmanTrack.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 //                Implementation of the AliKalmanTrack class
20 //   that is the base for AliTPCtrack, AliITStrackV2 and AliTRDtrack
21 //        Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
22 //-------------------------------------------------------------------------
23
24 #include "AliKalmanTrack.h"
25 #include "AliPDG.h"
26 #include "TPDGCode.h"
27 #include "TDatabasePDG.h"
28 #include "AliRunLoader.h"
29 #include "AliRun.h"
30 #include "AliMagF.h"
31
32 ClassImp(AliKalmanTrack)
33
34 Double_t AliKalmanTrack::fgConvConst;
35
36 //_______________________________________________________________________
37 AliKalmanTrack::AliKalmanTrack():
38   fLab(-3141593),
39   fChi2(0),
40   fMass(0.13957),
41   fN(0)
42 {
43   //
44   // Default constructor
45   //
46     if (fgConvConst==0) {
47       Warning("AliKalmanTrack()", "The magnetic field has not been set!");
48       SetConvConst();
49     }
50     
51     fStartTimeIntegral = kFALSE;
52     fIntegratedLength = 0;
53     for(Int_t i=0; i<5; i++) fIntegratedTime[i] = 0;
54 }
55
56 //_______________________________________________________________________
57 AliKalmanTrack::AliKalmanTrack(const AliKalmanTrack &t):
58   TObject(t),
59   fLab(t.fLab),
60   fChi2(t.fChi2),
61   fMass(t.fMass),
62   fN(t.fN)
63 {
64   //
65   // Copy constructor
66   //
67   if (fgConvConst==0) {
68     Warning("AliKalmanTrack(const AliKalmanTrack&)", 
69             "The magnetic field has not been set!");
70     SetConvConst();
71   }
72
73   fStartTimeIntegral = t.fStartTimeIntegral;
74   fIntegratedLength = t.fIntegratedLength;
75   
76   for (Int_t i=0; i<5; i++) 
77     fIntegratedTime[i] = t.fIntegratedTime[i];
78 }
79
80
81 //_______________________________________________________________________
82 void AliKalmanTrack::SetConvConst()
83 {
84   // Sets the conversion constants for the magnetic field 
85   // (Momentum in GeV/c -> curvature in mm)
86   ::Info("SetConvConst()", "tryinig to get the magnetic field from the AliRun object..."); 
87   AliRunLoader* loader = AliRunLoader::GetRunLoader();
88   if (!loader) ::Fatal("SetConvConst()", "No run loader found"); 
89   if (!loader->GetAliRun()) loader->LoadgAlice();
90   AliRun* alirun = loader->GetAliRun();
91   if (!alirun) ::Fatal("SetConvConst()", "No AliRun object found");
92
93   Double_t field = alirun->Field()->SolenoidField();
94   SetConvConst(1000/0.299792458/field);
95   ::Info("SetConvConst()", "Magnetic field set to %f kGauss\n", field);
96 }
97
98 //_______________________________________________________________________
99 Double_t AliKalmanTrack::GetX() const
100 {
101   // Returns the X coordinate of the current track position
102   Warning("GetX()","Method must be overloaded !\n");
103   return 0.;
104 }
105 //_______________________________________________________________________
106 Double_t AliKalmanTrack::GetdEdx() const
107 {
108   // Returns the dE/dx of the track
109   Warning("GetdEdx()","Method must be overloaded !\n");
110   return 0.;
111 }
112
113 //_______________________________________________________________________
114 Double_t AliKalmanTrack::GetY() const
115 {
116   // Returns the Y coordinate of the current track position
117   Double_t par[5];
118   Double_t localX = GetX();
119   GetExternalParameters(localX, par);
120   return par[0];
121 }
122 //_______________________________________________________________________
123 Double_t AliKalmanTrack::GetZ() const
124 {
125   // Returns the Z coordinate of the current track position
126   Double_t par[5];
127   Double_t localX = GetX();
128   GetExternalParameters(localX, par);
129   return par[1];
130 }
131 //_______________________________________________________________________
132 Double_t AliKalmanTrack::GetSnp() const
133 {
134   // Returns the Sin(phi), where phi is the angle between the transverse
135   // momentum (in xOy plane) and the X axis
136   Double_t par[5];
137   Double_t localX = GetX();
138   GetExternalParameters(localX, par);
139   return par[2];
140 }
141 //_______________________________________________________________________
142 Double_t AliKalmanTrack::GetTgl() const
143 {
144   // Returns the Tan(lambda), where lambda is the dip angle (between 
145   // the bending plane (xOy) and the momentum of the track
146   Double_t par[5];
147   Double_t localX = GetX();
148   GetExternalParameters(localX, par);
149   return par[3];
150 }
151 //_______________________________________________________________________
152 Double_t AliKalmanTrack::Get1Pt() const
153 {
154   // Returns 1/pT
155   Double_t par[5];
156   Double_t localX = GetX();
157   GetExternalParameters(localX, par);
158   return par[4];
159 }
160
161 //_______________________________________________________________________
162 Double_t AliKalmanTrack::Phi() const
163 {
164 // return global phi of track
165
166   Double_t par[5];
167   Double_t localX = GetX();
168   GetExternalParameters(localX, par);
169   if (par[2] >  1.) par[2] =  1.;
170   if (par[2] < -1.) par[2] = -1.;
171   Double_t phi = TMath::ASin(par[2]) + GetAlpha();
172   while (phi < 0) phi += TMath::TwoPi();
173   while (phi > TMath::TwoPi()) phi -= TMath::TwoPi();
174   return phi;
175 }
176 //_______________________________________________________________________
177 Double_t AliKalmanTrack::SigmaPhi() const
178 {
179 // return error of global phi of track
180
181   Double_t par[5];
182   Double_t cov[15];
183   Double_t localX = GetX();
184   GetExternalParameters(localX, par);
185   GetExternalCovariance(cov);
186   return TMath::Sqrt(TMath::Abs(cov[5] / (1. - par[2]*par[2])));
187 }
188 //_______________________________________________________________________
189 Double_t AliKalmanTrack::Theta() const
190 {
191 // return global theta of track
192
193   Double_t par[5];
194   Double_t localX = GetX();
195   GetExternalParameters(localX, par);
196   return TMath::Pi()/2. - TMath::ATan(par[3]);
197 }
198 //_______________________________________________________________________
199 Double_t AliKalmanTrack::SigmaTheta() const
200 {
201 // return error of global theta of track
202
203   Double_t par[5];
204   Double_t cov[15];
205   Double_t localX = GetX();
206   GetExternalParameters(localX, par);
207   GetExternalCovariance(cov);
208   return TMath::Sqrt(TMath::Abs(cov[5])) / (1. + par[3]*par[3]);
209 }
210 //_______________________________________________________________________
211 Double_t AliKalmanTrack::Eta() const
212 {
213 // return global eta of track
214
215   return -TMath::Log(TMath::Tan(Theta()/2.));
216 }
217 //_______________________________________________________________________
218 Double_t AliKalmanTrack::Px() const
219 {
220 // return x component of track momentum
221
222   Double_t par[5];
223   Double_t localX = GetX();
224   GetExternalParameters(localX, par);
225   Double_t phi = TMath::ASin(par[2]) + GetAlpha();
226   return TMath::Cos(phi) / TMath::Abs(par[4]);
227 }
228 //_______________________________________________________________________
229 Double_t AliKalmanTrack::Py() const
230 {
231 // return y component of track momentum
232
233   Double_t par[5];
234   Double_t localX = GetX();
235   GetExternalParameters(localX, par);
236   Double_t phi = TMath::ASin(par[2]) + GetAlpha();
237   return TMath::Sin(phi) / TMath::Abs(par[4]);
238 }
239 //_______________________________________________________________________
240 Double_t AliKalmanTrack::Pz() const
241 {
242 // return z component of track momentum
243
244   Double_t par[5];
245   Double_t localX = GetX();
246   GetExternalParameters(localX, par);
247   return par[3] / TMath::Abs(par[4]);
248 }
249 //_______________________________________________________________________
250 Double_t AliKalmanTrack::Pt() const
251 {
252 // return transverse component of track momentum
253
254   Double_t par[5];
255   Double_t localX = GetX();
256   GetExternalParameters(localX, par);
257   return 1. / TMath::Abs(par[4]);
258 }
259 //_______________________________________________________________________
260 Double_t AliKalmanTrack::SigmaPt() const
261 {
262 // return error of transverse component of track momentum
263
264   Double_t par[5];
265   Double_t cov[15];
266   Double_t localX = GetX();
267   GetExternalParameters(localX, par);
268   GetExternalCovariance(cov);
269   return TMath::Sqrt(cov[14]) / TMath::Abs(par[4]);
270 }
271 //_______________________________________________________________________
272 Double_t AliKalmanTrack::P() const
273 {
274 // return total track momentum
275
276   Double_t par[5];
277   Double_t localX = GetX();
278   GetExternalParameters(localX, par);
279   return 1. / TMath::Abs(par[4] * TMath::Cos(TMath::ATan(par[3])));
280 }
281 //_______________________________________________________________________
282 void AliKalmanTrack::StartTimeIntegral() 
283 {
284   // Sylwester Radomski, GSI
285   // S.Radomski@gsi.de
286   //
287   // Start time integration
288   // To be called at Vertex by ITS tracker
289   //
290   
291   //if (fStartTimeIntegral) 
292   //  Warning("StartTimeIntegral", "Reseting Recorded Time.");
293
294   fStartTimeIntegral = kTRUE;
295   for(Int_t i=0; i<fgkTypes; i++) fIntegratedTime[i] = 0;  
296   fIntegratedLength = 0;
297 }
298 //_______________________________________________________________________
299 void AliKalmanTrack:: AddTimeStep(Double_t length) 
300 {
301   // 
302   // Add step to integrated time
303   // this method should be called by a sublasses at the end
304   // of the PropagateTo function or by a tracker
305   // each time step is made.
306   //
307   // If integration not started function does nothing
308   //
309   // Formula
310   // dt = dl * sqrt(p^2 + m^2) / p
311   // p = pT * (1 + tg^2 (lambda) )
312   //
313   // pt = 1/external parameter [4]
314   // tg lambda = external parameter [3]
315   //
316   //
317   // Sylwester Radomski, GSI
318   // S.Radomski@gsi.de
319   // 
320   
321   static const Double_t kcc = 2.99792458e-2;
322
323   if (!fStartTimeIntegral) return;
324   
325   fIntegratedLength += length;
326
327   static Int_t pdgCode[fgkTypes]  = {kElectron, kMuonMinus, kPiPlus, kKPlus, kProton};
328   TDatabasePDG *db = TDatabasePDG::Instance();
329
330   Double_t xr, param[5];
331   Double_t pt, tgl;
332   
333   GetExternalParameters(xr, param);
334   pt =  1/param[4] ;
335   tgl = param[3];
336
337   Double_t p = TMath::Abs(pt * TMath::Sqrt(1+tgl*tgl));
338
339   if (length > 100) return;
340
341   for (Int_t i=0; i<fgkTypes; i++) {
342     
343     Double_t mass = db->GetParticle(pdgCode[i])->Mass();
344     Double_t correction = TMath::Sqrt( pt*pt * (1 + tgl*tgl) + mass * mass ) / p;
345     Double_t time = length * correction / kcc;
346
347     //cout << mass << "\t" << pt << "\t" << p << "\t" 
348     //     << correction << endl;
349
350     fIntegratedTime[i] += time;
351   }
352 }
353
354 //_______________________________________________________________________
355
356 Double_t AliKalmanTrack::GetIntegratedTime(Int_t pdg) const 
357 {
358   // Sylwester Radomski, GSI
359   // S.Radomski@gsi.de
360   //
361   // Return integrated time hypothesis for a given particle
362   // type assumption.
363   //
364   // Input parameter:
365   // pdg - Pdg code of a particle type
366   //
367
368
369   if (!fStartTimeIntegral) {
370     Warning("GetIntegratedTime","Time integration not started");
371     return 0.;
372   }
373
374   static Int_t pdgCode[fgkTypes] = {kElectron, kMuonMinus, kPiPlus, kKPlus, kProton};
375
376   for (Int_t i=0; i<fgkTypes; i++)
377     if (pdgCode[i] == TMath::Abs(pdg)) return fIntegratedTime[i];
378
379   Warning(":GetIntegratedTime","Particle type [%d] not found", pdg);
380   return 0;
381 }
382
383 void AliKalmanTrack::GetIntegratedTimes(Double_t *times) const {
384   for (Int_t i=0; i<fgkTypes; i++) times[i]=fIntegratedTime[i];
385 }
386
387 void AliKalmanTrack::SetIntegratedTimes(const Double_t *times) {
388   for (Int_t i=0; i<fgkTypes; i++) fIntegratedTime[i]=times[i];
389 }
390
391 //_______________________________________________________________________
392
393 void AliKalmanTrack::PrintTime() const
394 {
395   // Sylwester Radomski, GSI
396   // S.Radomski@gsi.de
397   //
398   // For testing
399   // Prints time for all hypothesis
400   //
401
402   static Int_t pdgCode[fgkTypes] = {kElectron, kMuonMinus, kPiPlus, kKPlus, kProton};
403
404   for (Int_t i=0; i<fgkTypes; i++)
405     printf("%d: %.2f  ", pdgCode[i], fIntegratedTime[i]);
406   printf("\n");  
407 }
408
409 static void External2Helix(const AliKalmanTrack *t, Double_t helix[6]) { 
410   //--------------------------------------------------------------------
411   // External track parameters -> helix parameters 
412   //--------------------------------------------------------------------
413   Double_t alpha,x,cs,sn;
414   t->GetExternalParameters(x,helix); alpha=t->GetAlpha();
415
416   cs=TMath::Cos(alpha); sn=TMath::Sin(alpha);
417   helix[5]=x*cs - helix[0]*sn;            // x0
418   helix[0]=x*sn + helix[0]*cs;            // y0
419 //helix[1]=                               // z0
420   helix[2]=TMath::ASin(helix[2]) + alpha; // phi0
421 //helix[3]=                               // tgl
422   helix[4]=helix[4]/t->GetConvConst();    // C
423 }
424
425 static void Evaluate(const Double_t *h, Double_t t,
426                      Double_t r[3],  //radius vector
427                      Double_t g[3],  //first defivatives
428                      Double_t gg[3]) //second derivatives
429 {
430   //--------------------------------------------------------------------
431   // Calculate position of a point on a track and some derivatives
432   //--------------------------------------------------------------------
433   Double_t phase=h[4]*t+h[2];
434   Double_t sn=TMath::Sin(phase), cs=TMath::Cos(phase);
435
436   r[0] = h[5] + (sn - h[6])/h[4];
437   r[1] = h[0] - (cs - h[7])/h[4];  
438   r[2] = h[1] + h[3]*t;
439
440   g[0] = cs; g[1]=sn; g[2]=h[3];
441   
442   gg[0]=-h[4]*sn; gg[1]=h[4]*cs; gg[2]=0.;
443 }
444
445 Double_t AliKalmanTrack::
446 GetDCA(const AliKalmanTrack *p, Double_t &xthis, Double_t &xp) const {
447   //------------------------------------------------------------
448   // Returns the (weighed !) distance of closest approach between 
449   // this track and the track passed as the argument.
450   // Other returned values:
451   //   xthis, xt - coordinates of tracks' reference planes at the DCA 
452   //-----------------------------------------------------------
453   Double_t dy2=GetSigmaY2() + p->GetSigmaY2();
454   Double_t dz2=GetSigmaZ2() + p->GetSigmaZ2();
455   Double_t dx2=dy2; 
456
457   //dx2=dy2=dz2=1.;
458
459   Double_t p1[8]; External2Helix(this,p1);
460   p1[6]=TMath::Sin(p1[2]); p1[7]=TMath::Cos(p1[2]);
461   Double_t p2[8]; External2Helix(p,p2);
462   p2[6]=TMath::Sin(p2[2]); p2[7]=TMath::Cos(p2[2]);
463
464
465   Double_t r1[3],g1[3],gg1[3]; Double_t t1=0.;
466   Evaluate(p1,t1,r1,g1,gg1);
467   Double_t r2[3],g2[3],gg2[3]; Double_t t2=0.;
468   Evaluate(p2,t2,r2,g2,gg2);
469
470   Double_t dx=r2[0]-r1[0], dy=r2[1]-r1[1], dz=r2[2]-r1[2];
471   Double_t dm=dx*dx/dx2 + dy*dy/dy2 + dz*dz/dz2;
472
473   Int_t max=27;
474   while (max--) {
475      Double_t gt1=-(dx*g1[0]/dx2 + dy*g1[1]/dy2 + dz*g1[2]/dz2);
476      Double_t gt2=+(dx*g2[0]/dx2 + dy*g2[1]/dy2 + dz*g2[2]/dz2);
477      Double_t h11=(g1[0]*g1[0] - dx*gg1[0])/dx2 + 
478                   (g1[1]*g1[1] - dy*gg1[1])/dy2 +
479                   (g1[2]*g1[2] - dz*gg1[2])/dz2;
480      Double_t h22=(g2[0]*g2[0] + dx*gg2[0])/dx2 + 
481                   (g2[1]*g2[1] + dy*gg2[1])/dy2 +
482                   (g2[2]*g2[2] + dz*gg2[2])/dz2;
483      Double_t h12=-(g1[0]*g2[0]/dx2 + g1[1]*g2[1]/dy2 + g1[2]*g2[2]/dz2);
484
485      Double_t det=h11*h22-h12*h12;
486
487      Double_t dt1,dt2;
488      if (TMath::Abs(det)<1.e-33) {
489         //(quasi)singular Hessian
490         dt1=-gt1; dt2=-gt2;
491      } else {
492         dt1=-(gt1*h22 - gt2*h12)/det; 
493         dt2=-(h11*gt2 - h12*gt1)/det;
494      }
495
496      if ((dt1*gt1+dt2*gt2)>0) {dt1=-dt1; dt2=-dt2;}
497
498      //check delta(phase1) ?
499      //check delta(phase2) ?
500
501      if (TMath::Abs(dt1)/(TMath::Abs(t1)+1.e-3) < 1.e-4)
502      if (TMath::Abs(dt2)/(TMath::Abs(t2)+1.e-3) < 1.e-4) {
503         if ((gt1*gt1+gt2*gt2) > 1.e-4/dy2/dy2) 
504           Warning("GetDCA"," stopped at not a stationary point !\n");
505         Double_t lmb=h11+h22; lmb=lmb-TMath::Sqrt(lmb*lmb-4*det);
506         if (lmb < 0.) 
507           Warning("GetDCA"," stopped at not a minimum !\n");
508         break;
509      }
510
511      Double_t dd=dm;
512      for (Int_t div=1 ; ; div*=2) {
513         Evaluate(p1,t1+dt1,r1,g1,gg1);
514         Evaluate(p2,t2+dt2,r2,g2,gg2);
515         dx=r2[0]-r1[0]; dy=r2[1]-r1[1]; dz=r2[2]-r1[2];
516         dd=dx*dx/dx2 + dy*dy/dy2 + dz*dz/dz2;
517         if (dd<dm) break;
518         dt1*=0.5; dt2*=0.5;
519         if (div>512) {
520            Warning("GetDCA"," overshoot !\n"); break;
521         }   
522      }
523      dm=dd;
524
525      t1+=dt1;
526      t2+=dt2;
527
528   }
529
530   if (max<=0) Warning("GetDCA"," too many iterations !\n");  
531
532   Double_t cs=TMath::Cos(GetAlpha());
533   Double_t sn=TMath::Sin(GetAlpha());
534   xthis=r1[0]*cs + r1[1]*sn;
535
536   cs=TMath::Cos(p->GetAlpha());
537   sn=TMath::Sin(p->GetAlpha());
538   xp=r2[0]*cs + r2[1]*sn;
539
540   return TMath::Sqrt(dm*TMath::Sqrt(dy2*dz2));
541 }
542
543 Double_t AliKalmanTrack::
544 PropagateToDCA(AliKalmanTrack *p, Double_t d, Double_t x0) {
545   //--------------------------------------------------------------
546   // Propagates this track and the argument track to the position of the
547   // distance of closest approach. 
548   // Returns the (weighed !) distance of closest approach.
549   //--------------------------------------------------------------
550   Double_t xthis,xp;
551   Double_t dca=GetDCA(p,xthis,xp);
552
553   if (!PropagateTo(xthis,d,x0)) {
554     //Warning("PropagateToDCA"," propagation failed !\n");
555     return 1e+33;
556   }  
557
558   if (!p->PropagateTo(xp,d,x0)) {
559     //Warning("PropagateToDCA"," propagation failed !\n";
560     return 1e+33;
561   }  
562
563   return dca;
564 }