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