]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliKalmanTrack.cxx
Additional protection. The MeanMaterialBudget budget will be upgraded soon (A.Gheata)
[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 "TGeoManager.h"
26
27 ClassImp(AliKalmanTrack)
28
29 const AliMagF *AliKalmanTrack::fgkFieldMap=0;
30 Double_t AliKalmanTrack::fgConvConst=0.;
31
32 //_______________________________________________________________________
33 AliKalmanTrack::AliKalmanTrack():
34   fLab(-3141593),
35   fFakeRatio(0),
36   fChi2(0),
37   fMass(AliPID::ParticleMass(AliPID::kPion)),
38   fN(0),
39   fLocalConvConst(0),
40   fStartTimeIntegral(kFALSE),
41   fIntegratedLength(0)
42 {
43   //
44   // Default constructor
45   //
46     if (fgkFieldMap==0) {
47       AliFatal("The magnetic field has not been set!");
48     }
49
50     for(Int_t i=0; i<AliPID::kSPECIES; i++) fIntegratedTime[i] = 0;
51 }
52
53 //_______________________________________________________________________
54 AliKalmanTrack::AliKalmanTrack(const AliKalmanTrack &t):
55   TObject(t),
56   fLab(t.fLab),
57   fFakeRatio(t.fFakeRatio),
58   fChi2(t.fChi2),
59   fMass(t.fMass),
60   fN(t.fN),
61   fLocalConvConst(t.fLocalConvConst),
62   fStartTimeIntegral(t.fStartTimeIntegral),
63   fIntegratedLength(t.fIntegratedLength)
64 {
65   //
66   // Copy constructor
67   //
68   if (fgkFieldMap==0) {
69     AliFatal("The magnetic field has not been set!");
70   }
71   
72   for (Int_t i=0; i<AliPID::kSPECIES; i++)
73       fIntegratedTime[i] = t.fIntegratedTime[i];
74 }
75
76 //_______________________________________________________________________
77 void AliKalmanTrack::StartTimeIntegral() 
78 {
79   // Sylwester Radomski, GSI
80   // S.Radomski@gsi.de
81   //
82   // Start time integration
83   // To be called at Vertex by ITS tracker
84   //
85   
86   //if (fStartTimeIntegral) 
87   //  AliWarning("Reseting Recorded Time.");
88
89   fStartTimeIntegral = kTRUE;
90   for(Int_t i=0; i<AliPID::kSPECIES; i++) fIntegratedTime[i] = 0;  
91   fIntegratedLength = 0;
92 }
93 //_______________________________________________________________________
94 void AliKalmanTrack:: AddTimeStep(Double_t length) 
95 {
96   // 
97   // Add step to integrated time
98   // this method should be called by a sublasses at the end
99   // of the PropagateTo function or by a tracker
100   // each time step is made.
101   //
102   // If integration not started function does nothing
103   //
104   // Formula
105   // dt = dl * sqrt(p^2 + m^2) / p
106   // p = pT * (1 + tg^2 (lambda) )
107   //
108   // pt = 1/external parameter [4]
109   // tg lambda = external parameter [3]
110   //
111   //
112   // Sylwester Radomski, GSI
113   // S.Radomski@gsi.de
114   // 
115   
116   static const Double_t kcc = 2.99792458e-2;
117
118   if (!fStartTimeIntegral) return;
119   
120   fIntegratedLength += length;
121
122   Double_t xr, param[5];
123   Double_t pt, tgl;
124   
125   GetExternalParameters(xr, param);
126   pt =  1/param[4] ;
127   tgl = param[3];
128
129   Double_t p = TMath::Abs(pt * TMath::Sqrt(1+tgl*tgl));
130
131   if (length > 100) return;
132
133   for (Int_t i=0; i<AliPID::kSPECIES; i++) {
134     
135     Double_t mass = AliPID::ParticleMass(i);
136     Double_t correction = TMath::Sqrt( pt*pt * (1 + tgl*tgl) + mass * mass ) / p;
137     Double_t time = length * correction / kcc;
138
139     fIntegratedTime[i] += time;
140   }
141 }
142
143 //_______________________________________________________________________
144
145 Double_t AliKalmanTrack::GetIntegratedTime(Int_t pdg) const 
146 {
147   // Sylwester Radomski, GSI
148   // S.Radomski@gsi.de
149   //
150   // Return integrated time hypothesis for a given particle
151   // type assumption.
152   //
153   // Input parameter:
154   // pdg - Pdg code of a particle type
155   //
156
157
158   if (!fStartTimeIntegral) {
159     AliWarning("Time integration not started");
160     return 0.;
161   }
162
163   for (Int_t i=0; i<AliPID::kSPECIES; i++)
164     if (AliPID::ParticleCode(i) == TMath::Abs(pdg)) return fIntegratedTime[i];
165
166   AliWarning(Form("Particle type [%d] not found", pdg));
167   return 0;
168 }
169
170 void AliKalmanTrack::GetIntegratedTimes(Double_t *times) const {
171   for (Int_t i=0; i<AliPID::kSPECIES; i++) times[i]=fIntegratedTime[i];
172 }
173
174 void AliKalmanTrack::SetIntegratedTimes(const Double_t *times) {
175   for (Int_t i=0; i<AliPID::kSPECIES; i++) fIntegratedTime[i]=times[i];
176 }
177
178 //_______________________________________________________________________
179
180 void AliKalmanTrack::PrintTime() const
181 {
182   // Sylwester Radomski, GSI
183   // S.Radomski@gsi.de
184   //
185   // For testing
186   // Prints time for all hypothesis
187   //
188
189   for (Int_t i=0; i<AliPID::kSPECIES; i++)
190     printf("%d: %.2f  ", AliPID::ParticleCode(i), fIntegratedTime[i]);
191   printf("\n");  
192 }
193
194 void AliKalmanTrack::External2Helix(Double_t helix[6]) const { 
195   //--------------------------------------------------------------------
196   // External track parameters -> helix parameters 
197   //--------------------------------------------------------------------
198   Double_t alpha,x,cs,sn;
199   GetExternalParameters(x,helix); alpha=GetAlpha();
200
201   cs=TMath::Cos(alpha); sn=TMath::Sin(alpha);
202   helix[5]=x*cs - helix[0]*sn;            // x0
203   helix[0]=x*sn + helix[0]*cs;            // y0
204 //helix[1]=                               // z0
205   helix[2]=TMath::ASin(helix[2]) + alpha; // phi0
206 //helix[3]=                               // tgl
207   helix[4]=helix[4]/GetLocalConvConst();  // C
208 }
209
210 static void Evaluate(const Double_t *h, Double_t t,
211                      Double_t r[3],  //radius vector
212                      Double_t g[3],  //first defivatives
213                      Double_t gg[3]) //second derivatives
214 {
215   //--------------------------------------------------------------------
216   // Calculate position of a point on a track and some derivatives
217   //--------------------------------------------------------------------
218   Double_t phase=h[4]*t+h[2];
219   Double_t sn=TMath::Sin(phase), cs=TMath::Cos(phase);
220
221   r[0] = h[5] + (sn - h[6])/h[4];
222   r[1] = h[0] - (cs - h[7])/h[4];  
223   r[2] = h[1] + h[3]*t;
224
225   g[0] = cs; g[1]=sn; g[2]=h[3];
226   
227   gg[0]=-h[4]*sn; gg[1]=h[4]*cs; gg[2]=0.;
228 }
229
230 Double_t AliKalmanTrack::
231 GetDCA(const AliKalmanTrack *p, Double_t &xthis, Double_t &xp) const {
232   //------------------------------------------------------------
233   // Returns the (weighed !) distance of closest approach between 
234   // this track and the track passed as the argument.
235   // Other returned values:
236   //   xthis, xt - coordinates of tracks' reference planes at the DCA 
237   //-----------------------------------------------------------
238   Double_t dy2=GetSigmaY2() + p->GetSigmaY2();
239   Double_t dz2=GetSigmaZ2() + p->GetSigmaZ2();
240   Double_t dx2=dy2; 
241
242   //dx2=dy2=dz2=1.;
243
244   Double_t p1[8]; External2Helix(p1);
245   p1[6]=TMath::Sin(p1[2]); p1[7]=TMath::Cos(p1[2]);
246   Double_t p2[8]; p->External2Helix(p2);
247   p2[6]=TMath::Sin(p2[2]); p2[7]=TMath::Cos(p2[2]);
248
249
250   Double_t r1[3],g1[3],gg1[3]; Double_t t1=0.;
251   Evaluate(p1,t1,r1,g1,gg1);
252   Double_t r2[3],g2[3],gg2[3]; Double_t t2=0.;
253   Evaluate(p2,t2,r2,g2,gg2);
254
255   Double_t dx=r2[0]-r1[0], dy=r2[1]-r1[1], dz=r2[2]-r1[2];
256   Double_t dm=dx*dx/dx2 + dy*dy/dy2 + dz*dz/dz2;
257
258   Int_t max=27;
259   while (max--) {
260      Double_t gt1=-(dx*g1[0]/dx2 + dy*g1[1]/dy2 + dz*g1[2]/dz2);
261      Double_t gt2=+(dx*g2[0]/dx2 + dy*g2[1]/dy2 + dz*g2[2]/dz2);
262      Double_t h11=(g1[0]*g1[0] - dx*gg1[0])/dx2 + 
263                   (g1[1]*g1[1] - dy*gg1[1])/dy2 +
264                   (g1[2]*g1[2] - dz*gg1[2])/dz2;
265      Double_t h22=(g2[0]*g2[0] + dx*gg2[0])/dx2 + 
266                   (g2[1]*g2[1] + dy*gg2[1])/dy2 +
267                   (g2[2]*g2[2] + dz*gg2[2])/dz2;
268      Double_t h12=-(g1[0]*g2[0]/dx2 + g1[1]*g2[1]/dy2 + g1[2]*g2[2]/dz2);
269
270      Double_t det=h11*h22-h12*h12;
271
272      Double_t dt1,dt2;
273      if (TMath::Abs(det)<1.e-33) {
274         //(quasi)singular Hessian
275         dt1=-gt1; dt2=-gt2;
276      } else {
277         dt1=-(gt1*h22 - gt2*h12)/det; 
278         dt2=-(h11*gt2 - h12*gt1)/det;
279      }
280
281      if ((dt1*gt1+dt2*gt2)>0) {dt1=-dt1; dt2=-dt2;}
282
283      //check delta(phase1) ?
284      //check delta(phase2) ?
285
286      if (TMath::Abs(dt1)/(TMath::Abs(t1)+1.e-3) < 1.e-4)
287      if (TMath::Abs(dt2)/(TMath::Abs(t2)+1.e-3) < 1.e-4) {
288         if ((gt1*gt1+gt2*gt2) > 1.e-4/dy2/dy2) 
289           AliWarning(" stopped at not a stationary point !");
290         Double_t lmb=h11+h22; lmb=lmb-TMath::Sqrt(lmb*lmb-4*det);
291         if (lmb < 0.) 
292           AliWarning(" stopped at not a minimum !");
293         break;
294      }
295
296      Double_t dd=dm;
297      for (Int_t div=1 ; ; div*=2) {
298         Evaluate(p1,t1+dt1,r1,g1,gg1);
299         Evaluate(p2,t2+dt2,r2,g2,gg2);
300         dx=r2[0]-r1[0]; dy=r2[1]-r1[1]; dz=r2[2]-r1[2];
301         dd=dx*dx/dx2 + dy*dy/dy2 + dz*dz/dz2;
302         if (dd<dm) break;
303         dt1*=0.5; dt2*=0.5;
304         if (div>512) {
305            AliWarning(" overshoot !"); break;
306         }   
307      }
308      dm=dd;
309
310      t1+=dt1;
311      t2+=dt2;
312
313   }
314
315   if (max<=0) AliWarning(" too many iterations !");
316
317   Double_t cs=TMath::Cos(GetAlpha());
318   Double_t sn=TMath::Sin(GetAlpha());
319   xthis=r1[0]*cs + r1[1]*sn;
320
321   cs=TMath::Cos(p->GetAlpha());
322   sn=TMath::Sin(p->GetAlpha());
323   xp=r2[0]*cs + r2[1]*sn;
324
325   return TMath::Sqrt(dm*TMath::Sqrt(dy2*dz2));
326 }
327
328 Double_t AliKalmanTrack::
329 PropagateToDCA(AliKalmanTrack *p, Double_t d, Double_t x0) {
330   //--------------------------------------------------------------
331   // Propagates this track and the argument track to the position of the
332   // distance of closest approach. 
333   // Returns the (weighed !) distance of closest approach.
334   //--------------------------------------------------------------
335   Double_t xthis,xp;
336   Double_t dca=GetDCA(p,xthis,xp);
337
338   if (!PropagateTo(xthis,d,x0)) {
339     //AliWarning(" propagation failed !");
340     return 1e+33;
341   }  
342
343   if (!p->PropagateTo(xp,d,x0)) {
344     //AliWarning(" propagation failed !";
345     return 1e+33;
346   }  
347
348   return dca;
349 }
350
351
352
353
354
355 Double_t AliKalmanTrack::MeanMaterialBudget(Double_t *start, Double_t *end, Double_t *mparam)
356 {
357   //
358   // calculate mean material budget and material properties beween point start and end
359   // mparam - returns parameters used for dEdx and multiple scatering
360   //
361   // mparam[0] - density mean 
362   // mparam[1] - rad length
363   // mparam[2] - A mean
364   // mparam[3] - Z mean
365   // mparam[4] - length
366   // mparam[5] - Z/A mean
367   // mparam[6] - number of boundary crosses
368   //
369     mparam[0]=0; mparam[1]=1; mparam[2] =0; mparam[3] =0, mparam[4]=0, mparam[5]=0; mparam[6]=0; 
370   //
371   Double_t bparam[6], lparam[6];          // bparam - total param - lparam - local parameters
372   for (Int_t i=0;i<6;i++) bparam[i]=0;    // 
373
374   if (!gGeoManager) {
375     printf("ERROR: no TGeo\n");
376     return 0.;
377   }
378   //
379   Double_t length;
380   Double_t dir[3];
381   length = TMath::Sqrt((end[0]-start[0])*(end[0]-start[0])+
382                        (end[1]-start[1])*(end[1]-start[1])+
383                        (end[2]-start[2])*(end[2]-start[2]));
384   mparam[4]=length;
385   if (length<TGeoShape::Tolerance()) return 0.0;
386   Double_t invlen = 1./length;
387   dir[0] = (end[0]-start[0])*invlen;
388   dir[1] = (end[1]-start[1])*invlen;
389   dir[2] = (end[2]-start[2])*invlen;
390   // Initialize start point and direction
391   TGeoNode *currentnode = 0;
392   TGeoNode *startnode = gGeoManager->InitTrack(start, dir);
393   //  printf("%s length=%f\n",gGeoManager->GetPath(),length);
394   if (!startnode) {
395     printf("ERROR: start point out of geometry\n");
396     return 0.0;
397   }
398   TGeoMaterial *material = startnode->GetVolume()->GetMedium()->GetMaterial();
399   lparam[0] = material->GetDensity();
400   lparam[1]   = material->GetRadLen();
401   lparam[2]   = material->GetA();
402   lparam[3]   = material->GetZ();
403   lparam[5]   = lparam[3]/lparam[2];
404   if (material->IsMixture()) {
405     lparam[1]*=lparam[0];  // different normalization in the modeler for mixture
406     TGeoMixture * mixture = (TGeoMixture*)material;
407     lparam[5] =0;
408     Double_t sum =0;
409     for (Int_t iel=0;iel<mixture->GetNelements();iel++){
410       sum  += mixture->GetWmixt()[iel];
411       lparam[5]+= mixture->GetZmixt()[iel]*mixture->GetWmixt()[iel]/mixture->GetAmixt()[iel];
412     }
413     lparam[5]/=sum;
414   }
415   gGeoManager->FindNextBoundary(length);
416   Double_t snext = gGeoManager->GetStep();
417   Double_t step = 0.0;
418   // If no boundary within proposed length, return current density
419   if (snext>=length) {
420     for (Int_t ip=0;ip<5;ip++) mparam[ip] = lparam[ip];  
421     return lparam[0];
422   }
423   // Try to cross the boundary and see what is next
424   while (length>TGeoShape::Tolerance()) {
425     mparam[6]+=1.;
426     currentnode = gGeoManager->Step();
427     step += snext+1.E-6;
428     bparam[1]    += snext*lparam[1];
429     bparam[2]    += snext*lparam[2];
430     bparam[3]    += snext*lparam[3];
431     bparam[5]    += snext*lparam[5];    
432     bparam[0]    += snext*lparam[0];
433
434     if (snext>=length) break;
435     if (!currentnode) break;
436     //    printf("%s snext=%f  density=%f bparam[0]=%f\n", gGeoManager->GetPath(),snext,density,bparam[0]);
437     if (!gGeoManager->IsEntering()) {
438       gGeoManager->SetStep(1.E-3);
439       currentnode = gGeoManager->Step();
440       if (!gGeoManager->IsEntering() || !currentnode) {
441         //      printf("ERROR: cannot cross boundary\n"); 
442         mparam[0] = bparam[0]/step;
443         mparam[1] = bparam[1]/step;
444         mparam[2] = bparam[2]/step;
445         mparam[3] = bparam[3]/step;     
446         mparam[5] = bparam[5]/step;     
447         mparam[4] = step;
448         mparam[0] = 0.;             // if crash of navigation take mean density 0 
449         mparam[1] = 1000000;        // and infinite rad length
450         return bparam[0]/step;
451       }
452       step += 1.E-3;
453       snext += 1.E-3;
454       bparam[0] += lparam[0]*1.E-3;
455       bparam[1]    += lparam[1]*1.E-3;
456       bparam[2]    += lparam[2]*1.E-3;
457       bparam[3]    += lparam[3]*1.E-3;
458       bparam[5]    += lparam[5]*1.E-3;
459     }
460     length -= snext;
461     material = currentnode->GetVolume()->GetMedium()->GetMaterial();
462     lparam[0] = material->GetDensity();
463     lparam[1]  = material->GetRadLen();
464     lparam[2]  = material->GetA();
465     lparam[3]  = material->GetZ();
466     lparam[5]   = lparam[3]/lparam[2];
467     if (material->IsMixture()) {
468       lparam[1]*=lparam[0];
469       TGeoMixture * mixture = (TGeoMixture*)material;
470       lparam[5]=0; 
471       Double_t sum =0;
472       for (Int_t iel=0;iel<mixture->GetNelements();iel++){
473         sum+= mixture->GetWmixt()[iel];
474         lparam[5]+= mixture->GetZmixt()[iel]*mixture->GetWmixt()[iel]/mixture->GetAmixt()[iel];
475       }
476       lparam[5]/=sum;
477     }
478     gGeoManager->FindNextBoundary(length);
479     snext = gGeoManager->GetStep();
480   }   
481   mparam[0] = bparam[0]/step;
482   mparam[1] = bparam[1]/step;
483   mparam[2] = bparam[2]/step;
484   mparam[3] = bparam[3]/step;  
485   mparam[5] = bparam[5]/step;  
486   return bparam[0]/step;   
487   
488 }