]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDtrackV1.cxx
totEt updates from Christine
[u/mrichter/AliRoot.git] / TRD / AliTRDtrackV1.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 #include "AliLog.h"
19 #include "AliESDtrack.h"
20 #include "AliTracker.h"
21
22 #include "AliTRDtrackV1.h"
23 #include "AliTRDcluster.h"
24 #include "AliTRDcalibDB.h"
25 #include "AliTRDReconstructor.h"
26 #include "AliTRDrecoParam.h"
27
28 ClassImp(AliTRDtrackV1)
29
30 ///////////////////////////////////////////////////////////////////////////////
31 //                                                                           //
32 //  Represents a reconstructed TRD track                                     //
33 //  Local TRD Kalman track                                                   //
34 //                                                                           //
35 //  Authors:                                                                 //
36 //    Alex Bercuci <A.Bercuci@gsi.de>                                        //
37 //    Markus Fasel <M.Fasel@gsi.de>                                          //
38 //                                                                           //
39 ///////////////////////////////////////////////////////////////////////////////
40
41 //_______________________________________________________________
42 AliTRDtrackV1::AliTRDtrackV1() : AliKalmanTrack()
43   ,fStatus(0)
44   ,fESDid(0)
45   ,fDE(0.)
46   ,fkReconstructor(NULL)
47   ,fBackupTrack(NULL)
48   ,fTrackLow(NULL)
49   ,fTrackHigh(NULL)
50 {
51   //
52   // Default constructor
53   //
54   //printf("AliTRDtrackV1::AliTRDtrackV1()\n");
55
56   for(int i =0; i<3; i++) fBudget[i] = 0.;
57   
58   Float_t pid = 1./AliPID::kSPECIES;
59   for(int is =0; is<AliPID::kSPECIES; is++) fPID[is] = pid;
60
61   for(int ip=0; ip<kNplane; ip++){
62     fTrackletIndex[ip] = -1;
63     fTracklet[ip]      = NULL;
64   }
65 }
66
67 //_______________________________________________________________
68 AliTRDtrackV1::AliTRDtrackV1(const AliTRDtrackV1 &ref) : AliKalmanTrack(ref)
69   ,fStatus(ref.fStatus)
70   ,fESDid(ref.fESDid)
71   ,fDE(ref.fDE)
72   ,fkReconstructor(ref.fkReconstructor)
73   ,fBackupTrack(NULL)
74   ,fTrackLow(NULL)
75   ,fTrackHigh(NULL)
76 {
77   //
78   // Copy constructor
79   //
80
81   //printf("AliTRDtrackV1::AliTRDtrackV1(const AliTRDtrackV1 &)\n");
82   SetBit(kOwner, kFALSE);
83   for(int ip=0; ip<kNplane; ip++){ 
84     fTrackletIndex[ip] = ref.fTrackletIndex[ip];
85     fTracklet[ip]      = ref.fTracklet[ip];
86   }
87   if(ref.fTrackLow) fTrackLow = new AliExternalTrackParam(*ref.fTrackLow);
88   if(ref.fTrackHigh) fTrackHigh = new AliExternalTrackParam(*ref.fTrackHigh);
89  
90   for (Int_t i = 0; i < 3;i++) fBudget[i]      = ref.fBudget[i];
91
92         for(Int_t is = 0; is<AliPID::kSPECIES; is++) fPID[is] = ref.fPID[is];
93   
94   AliKalmanTrack::SetNumberOfClusters(ref.GetNumberOfClusters());
95 }
96
97 //_______________________________________________________________
98 AliTRDtrackV1::AliTRDtrackV1(const AliESDtrack &t) : AliKalmanTrack()
99   ,fStatus(0)
100   ,fESDid(0)
101   ,fDE(0.)
102   ,fkReconstructor(NULL)
103   ,fBackupTrack(NULL)
104   ,fTrackLow(NULL)
105   ,fTrackHigh(NULL)
106 {
107   //
108   // Constructor from AliESDtrack
109   //
110
111   SetESDid(t.GetID());
112   SetLabel(t.GetLabel());
113   SetChi2(0.0);
114
115   SetMass(t.GetMass()/*0.000510*/);
116   AliKalmanTrack::SetNumberOfClusters(t.GetTRDncls()); 
117   Int_t ti[]={-1, -1, -1, -1, -1, -1}; t.GetTRDtracklets(&ti[0]);
118   for(int ip=0; ip<kNplane; ip++){ 
119     fTrackletIndex[ip] = ti[ip];
120     fTracklet[ip]      = NULL;
121   }
122   for(int i =0; i<3; i++) fBudget[i] = 0.;
123   
124   Float_t pid = 1./AliPID::kSPECIES;
125   for(int is =0; is<AliPID::kSPECIES; is++) fPID[is] = pid;
126
127   const AliExternalTrackParam *par = &t;
128   if (t.GetStatus() & AliESDtrack::kTRDbackup) { 
129     par = t.GetOuterParam();
130     if (!par) {
131       AliError("No backup info!"); 
132       par = &t;
133     }
134   }
135   Set(par->GetX() 
136      ,par->GetAlpha()
137      ,par->GetParameter()
138      ,par->GetCovariance());
139
140   if(t.GetStatus() & AliESDtrack::kTIME) {
141     StartTimeIntegral();
142     Double_t times[10]; 
143     t.GetIntegratedTimes(times); 
144     SetIntegratedTimes(times);
145     SetIntegratedLength(t.GetIntegratedLength());
146   }
147 }
148
149 //_______________________________________________________________
150 AliTRDtrackV1::AliTRDtrackV1(AliTRDseedV1 * const trklts, const Double_t p[5], const Double_t cov[15]
151              , Double_t x, Double_t alpha) : AliKalmanTrack()
152   ,fStatus(0)
153   ,fESDid(0)
154   ,fDE(0.)
155   ,fkReconstructor(NULL)
156   ,fBackupTrack(NULL)
157   ,fTrackLow(NULL)
158   ,fTrackHigh(NULL)
159 {
160   //
161   // The stand alone tracking constructor
162   // TEMPORARY !!!!!!!!!!!
163   // to check :
164   // 1. covariance matrix
165   // 2. dQdl calculation
166   //
167
168   Double_t b(GetBz());
169   Double_t cnv = (TMath::Abs(b) < 1.e-5) ? 1.e5 : 1./GetBz()/kB2C;
170   
171   Double_t pp[5] = { p[0]    
172                     , p[1]
173                     , p[2]
174                     , p[3]
175                     , p[4]*cnv      };
176   
177   Double_t c22 = x*x*cov[14] - 2*x*cov[12] + cov[ 5];
178   Double_t c32 =   x*cov[13] -     cov[ 8];
179   Double_t c20 =   x*cov[10] -     cov[ 3];
180   Double_t c21 =   x*cov[11] -     cov[ 4];
181   Double_t c42 =   x*cov[14] -     cov[12];
182   
183   Double_t cc[15] = { cov[ 0]
184                     , cov[ 1],     cov[ 2]
185                     , c20,         c21,         c22
186                     , cov[ 6],     cov[ 7],     c32,     cov[ 9]
187                     , cov[10]*cnv, cov[11]*cnv, c42*cnv, cov[13]*cnv, cov[14]*cnv*cnv };
188   
189   Double_t mostProbablePt=AliExternalTrackParam::GetMostProbablePt();
190   Double_t p0=TMath::Sign(1/mostProbablePt,pp[4]);
191   Double_t w0=cc[14]/(cc[14] + p0*p0), w1=p0*p0/(cc[14] + p0*p0);
192   AliDebug(3, Form("Pt mixing : w0[%4.2f] pt0[%5.3f] w1[%4.2f] pt[%5.3f]", w0, 1./p0, w1, 1./pp[4]));
193   
194   pp[4] = w0*p0 + w1*pp[4];
195   cc[10]*=w1; cc[11]*=w1; cc[12]*=w1; cc[13]*=w1; cc[14]*=w1;
196   Set(x,alpha,pp,cc);
197   AliDebug(2, Form("Init @ x[%6.2f] pt[%5.3f]", x, 1./pp[4]));
198   Int_t ncls = 0;
199   for(int iplane=0; iplane<kNplane; iplane++){
200     fTrackletIndex[iplane] = -1;
201           if(!trklts[iplane].IsOK()) fTracklet[iplane] = NULL;
202     else{ 
203       fTracklet[iplane] = &trklts[iplane];
204       ncls += fTracklet[iplane]->GetN();
205     }
206   }
207   AliKalmanTrack::SetNumberOfClusters(ncls);            
208   for(int i =0; i<3; i++) fBudget[i] = 0.;
209   
210   Float_t pid = 1./AliPID::kSPECIES;
211   for(int is =0; is<AliPID::kSPECIES; is++) fPID[is] = pid;
212 }
213
214 //_______________________________________________________________
215 AliTRDtrackV1::~AliTRDtrackV1()
216 {
217   //AliInfo("");
218   //printf("I-AliTRDtrackV1::~AliTRDtrackV1() : Owner[%s]\n", TestBit(kOwner)?"YES":"NO");
219
220   if(fBackupTrack) delete fBackupTrack; fBackupTrack = NULL;
221
222   if(fTrackLow) delete fTrackLow; fTrackLow = NULL;
223   if(fTrackHigh) delete fTrackHigh; fTrackHigh = NULL;
224
225   for(Int_t ip=0; ip<kNplane; ip++){
226     if(TestBit(kOwner) && fTracklet[ip]) delete fTracklet[ip];
227     fTracklet[ip] = NULL;
228     fTrackletIndex[ip] = -1;
229   }
230 }
231         
232 //_______________________________________________________________
233 Bool_t AliTRDtrackV1::CookLabel(Float_t wrong)
234 {
235   // set MC label for this track
236   if(!GetNumberOfClusters()) return kFALSE;
237
238   Int_t s[kMAXCLUSTERSPERTRACK][2];
239   for (Int_t i = 0; i < kMAXCLUSTERSPERTRACK; i++) {
240     s[i][0] = -1;
241     s[i][1] =  0;
242   }
243   
244   Bool_t labelAdded;
245   Int_t label;
246   AliTRDcluster *c    = NULL;
247   for (Int_t ip = 0; ip < kNplane; ip++) {
248     if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
249     for (Int_t ic = 0; ic < AliTRDseedV1::kNclusters; ic++) {
250       if(!(c = fTracklet[ip]->GetClusters(ic))) continue;
251       for (Int_t k = 0; k < 3; k++) { 
252         label      = c->GetLabel(k);
253         labelAdded = kFALSE; 
254         Int_t j = 0;
255         if (label >= 0) {
256           while ((!labelAdded) && (j < kMAXCLUSTERSPERTRACK)) {
257             if ((s[j][0] == label) || 
258                 (s[j][1] ==     0)) {
259               s[j][0] = label; 
260               s[j][1]++; 
261               labelAdded = kTRUE;
262             }
263             j++;
264           }
265         }
266       }
267     }
268   }
269   
270   Int_t max = 0;
271   label = -123456789;
272   for (Int_t i = 0; i < kMAXCLUSTERSPERTRACK; i++) {
273     if (s[i][1] <= max) continue;
274     max   = s[i][1]; 
275     label = s[i][0];
276   }
277   if ((1. - Float_t(max)/GetNumberOfClusters()) > wrong) label = -label;
278   
279   SetLabel(label); 
280   
281   return kTRUE;
282 }
283
284 //_______________________________________________________________
285 Bool_t AliTRDtrackV1::CookPID()
286 {
287 //
288 // Cook the PID information for the track by delegating the omonim function of the tracklets. 
289 // Computes the number of tracklets used. The tracklet information are considered independent. 
290 // For the moment no global track measurement of PID is performed as for example to estimate 
291 // bremsstrahlung probability based on global chi2 of the track.
292 //
293 // The status bit AliESDtrack::kTRDpid is set during the call of AliTRDtrackV1::UpdateESDtrack().The PID performance of the 
294 //TRD for tracks with 6 tacklets is displayed below.
295 //Begin_Html
296 //<img src="TRD/trackPID.gif">
297 //End_Html
298 //
299   
300   /*Reset the a priori probabilities*/
301   Double_t pid = 1. / AliPID::kSPECIES;
302   for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) fPID[ispec] = pid;  
303
304   UChar_t fPIDquality = SetNumberOfTrackletsPID(kTRUE);
305   // no tracklet found for PID calculations
306   if(!fPIDquality) return kFALSE;
307   
308   // slot for PID calculation @ track level for bremsstrahlung TODO
309   
310   // normalize probabilities
311   Double_t probTotal = 0.0;
312   for (Int_t is = 0; is < AliPID::kSPECIES; is++) probTotal += fPID[is];
313   
314   
315   if (probTotal <= 0.0) {
316     AliWarning("The total probability over all species <= 0. This may be caused by some error in the reference data.");
317     return kFALSE;
318   }
319   
320   for (Int_t iSpecies = 0; iSpecies < AliPID::kSPECIES; iSpecies++) fPID[iSpecies] /= probTotal;
321   
322   return kTRUE;
323 }
324
325 //___________________________________________________________
326 UChar_t AliTRDtrackV1::GetNumberOfTrackletsPID() const
327 {
328 // Retrieve number of tracklets used for PID calculation. 
329
330   UChar_t nPID = 0;
331   Float_t *prob = NULL;
332   for(int ip=0; ip<kNplane; ip++){
333     if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
334     if(!fTracklet[ip]->IsOK()) continue;
335     if(!(prob = fTracklet[ip]->GetProbability(kFALSE))) continue;
336
337     Int_t nspec = 0; // quality check of tracklet dEdx
338     for(int ispec=0; ispec<AliPID::kSPECIES; ispec++){
339       if(prob[ispec] < 0.) continue;
340       nspec++;
341     }
342     if(!nspec) continue;
343     
344     fTracklet[ip]->SetPID();
345     nPID++;
346   }
347   return nPID;
348 }
349
350 //___________________________________________________________
351 UChar_t AliTRDtrackV1::SetNumberOfTrackletsPID(Bool_t recalc)
352 {
353 // Retrieve number of tracklets used for PID calculation. // Recalculated PID at tracklet level by quering the PID DB.
354
355   UChar_t fPIDquality(0);
356   
357   // steer PID calculation @ tracklet level
358   Float_t *prob(NULL);
359   for(int ip=0; ip<kNplane; ip++){
360     if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
361     if(!fTracklet[ip]->IsOK()) continue;
362     if(!(prob = fTracklet[ip]->GetProbability(recalc))) return 0;
363
364     Int_t nspec = 0; // quality check of tracklet dEdx
365     for(int ispec=0; ispec<AliPID::kSPECIES; ispec++){
366       if(prob[ispec] < 0.) continue;
367       fPID[ispec] *= prob[ispec];
368       nspec++;
369     }
370     if(!nspec) continue;
371     
372     fPIDquality++;
373   }
374   return fPIDquality;
375 }
376
377 //_______________________________________________________________
378 AliTRDcluster* AliTRDtrackV1::GetCluster(Int_t id)
379 {
380   // Get the cluster at a certain position in the track
381   Int_t n = 0;
382   for(Int_t ip=0; ip<kNplane; ip++){
383     if(!fTracklet[ip]) continue;
384     if(n+fTracklet[ip]->GetN() <= id){ 
385       n+=fTracklet[ip]->GetN();
386       continue;
387     }
388     AliTRDcluster *c = NULL;
389     for(Int_t ic=AliTRDseedV1::kNclusters; ic--;){
390       if(!(c = fTracklet[ip]->GetClusters(ic))) continue;
391
392       if(n<id){n++; continue;}
393       return c;
394     }
395   }
396   return NULL;
397 }
398
399 //_______________________________________________________________
400 Int_t  AliTRDtrackV1::GetClusterIndex(Int_t id) const
401 {
402   // Get the cluster index at a certain position in the track
403   Int_t n = 0;
404   for(Int_t ip=0; ip<kNplane; ip++){
405     if(!fTracklet[ip]) continue;
406     if(n+fTracklet[ip]->GetN() <= id){ 
407       n+=fTracklet[ip]->GetN();
408       continue;
409     }
410     AliTRDcluster *c = NULL;
411     for(Int_t ic=AliTRDseedV1::kNclusters; ic--;){
412       if(!(c = fTracklet[ip]->GetClusters(ic))) continue;
413
414       if(n<id){n++; continue;}
415       return fTracklet[ip]->GetIndexes(ic);
416     }
417   }
418   return -1;
419 }
420
421 //_______________________________________________________________
422 Double_t AliTRDtrackV1::GetPredictedChi2(const AliTRDseedV1 *trklt, Double_t *cov) const
423 {
424 // Compute chi2 between tracklet and track. The value is calculated at the radial position of the track
425 // equal to the reference radial position of the tracklet (see AliTRDseedV1)
426 // 
427 // The chi2 estimator is computed according to the following formula
428 // BEGIN_LATEX
429 // #chi^{2}=(X_{trklt}-X_{track})(C_{trklt}+C_{track})^{-1}(X_{trklt}-X_{track})^{T}
430 // END_LATEX
431 // where X=(y z), the position of the track/tracklet in the yz plane
432 // 
433
434   Double_t p[2]   = { trklt->GetY(), trklt->GetZ()};
435   trklt->GetCovAt(trklt->GetX(), cov);
436   return AliExternalTrackParam::GetPredictedChi2(p, cov);
437 }
438
439 //_______________________________________________________________
440 Int_t AliTRDtrackV1::GetSector() const
441 {
442   return Int_t(GetAlpha()/AliTRDgeometry::GetAlpha() + (GetAlpha()>0. ? 0 : AliTRDgeometry::kNsector));
443 }
444
445 //_______________________________________________________________
446 Bool_t AliTRDtrackV1::IsEqual(const TObject *o) const
447 {
448   // Checks whether two tracks are equal
449   if (!o) return kFALSE;
450   const AliTRDtrackV1 *inTrack = dynamic_cast<const AliTRDtrackV1*>(o);
451   if (!inTrack) return kFALSE;
452   
453   //if ( fPIDquality != inTrack->GetPIDquality() ) return kFALSE;
454   
455   for(Int_t i = 0; i < AliPID::kSPECIES; i++){
456     if ( fPID[i] != inTrack->GetPID(i) ) return kFALSE;
457   }
458   
459   for (Int_t i = 0; i < 3; i++){
460     if ( fBudget[i] != inTrack->GetBudget(i) ) return kFALSE;
461   }
462   if ( fDE != inTrack->GetEdep() ) return kFALSE;
463   if ( fFakeRatio != inTrack->GetFakeRatio() ) return kFALSE;
464   if ( fChi2 != inTrack->GetChi2() ) return kFALSE;
465   if ( fMass != inTrack->GetMass() ) return kFALSE;
466   if ( fLab != inTrack->GetLabel() ) return kFALSE;
467   if ( fN != inTrack->GetNumberOfClusters() ) return kFALSE;
468   if ( AliKalmanTrack::GetIntegratedLength() != inTrack->GetIntegratedLength() ) return kFALSE;
469   
470   if ( GetX() != inTrack->GetX() ) return kFALSE;
471   if ( GetAlpha() != inTrack->GetAlpha() ) return kFALSE;
472   const Double_t *inP = inTrack->GetParameter();
473   const Double_t *curP = GetParameter();
474   for (Int_t i = 0; i < 5; i++){
475     if ( curP[i] != inP[i]) return kFALSE;
476   }
477   const Double_t *inC = inTrack->GetCovariance();
478   const Double_t *curC = GetCovariance();
479   for (Int_t i = 0; i < 15; i++){
480     if ( curC[i] != inC[i]) return kFALSE;
481   }
482   
483   for (Int_t iTracklet = 0; iTracklet < kNplane; iTracklet++){
484     AliTRDseedV1 *curTracklet = fTracklet[iTracklet];
485     AliTRDseedV1 *inTracklet = inTrack->GetTracklet(iTracklet);
486     if (curTracklet && inTracklet){
487       if (! curTracklet->IsEqual(inTracklet) ) {
488         curTracklet->Print();
489         inTracklet->Print();
490         return kFALSE;
491       }
492     } else {
493       // if one tracklet exists, and corresponding 
494       // in other track doesn't - return kFALSE
495       if(inTracklet || curTracklet) return kFALSE;
496     }
497   }
498
499   return kTRUE;
500 }
501
502 //_______________________________________________________________
503 Bool_t AliTRDtrackV1::IsElectron() const
504 {
505   if(GetPID(0) > fkReconstructor->GetRecoParam()->GetPIDThreshold(GetP())) return kTRUE;
506   return kFALSE;
507 }
508
509         
510 //_____________________________________________________________________________
511 Int_t AliTRDtrackV1::MakeBackupTrack()
512 {
513 //
514 // Creates a backup track
515 // TO DO update quality check of the track.
516 //
517
518   Float_t occupancy(0.); Int_t n(0), ncls(0);
519   for(Int_t il(AliTRDgeometry::kNlayer); il--;){ 
520     if(!fTracklet[il]) continue;
521     n++; 
522     occupancy+=fTracklet[il]->GetOccupancyTB();
523     ncls += fTracklet[il]->GetN();
524   }
525   if(!n) return -1;
526   occupancy/=n;
527
528   //Float_t ratio1 = Float_t(t.GetNumberOfClusters()+1) / Float_t(t.GetNExpected()+1);  
529   
530   Int_t failedCutId(0);
531   if(GetChi2()/n > 5.0) failedCutId=1; 
532   if(occupancy < 0.7) failedCutId=2;
533   //if(ratio1 >   0.6) && 
534   //if(ratio0+ratio1  >   1.5) && 
535   if(GetNCross() != 0)  failedCutId=3;
536   if(TMath::Abs(GetSnp()) > 0.85) failedCutId=4;
537   if(ncls < 20) failedCutId=5;
538
539   if(failedCutId){ 
540     AliDebug(2, Form("\n"
541       "chi2/tracklet < 5.0   [%c] %5.2f\n"
542       "occupancy > 0.7       [%c] %4.2f\n"
543       "NCross == 0           [%c] %d\n"
544       "Abs(snp) < 0.85       [%c] %4.2f\n"
545       "NClusters > 20        [%c] %d"
546       ,(GetChi2()/n<5.0)?'y':'n', GetChi2()/n
547       ,(occupancy>0.7)?'y':'n', occupancy
548       ,(GetNCross()==0)?'y':'n', GetNCross()
549       ,(TMath::Abs(GetSnp())<0.85)?'y':'n', TMath::Abs(GetSnp())
550       ,(ncls>20)?'y':'n', ncls
551     ));
552     return failedCutId;
553   }
554
555   if(fBackupTrack) {
556     fBackupTrack->~AliTRDtrackV1();
557     new(fBackupTrack) AliTRDtrackV1((AliTRDtrackV1&)(*this));
558     return 0;
559   }
560   fBackupTrack = new AliTRDtrackV1((AliTRDtrackV1&)(*this));
561   return 0;
562 }
563
564 //_____________________________________________________________________________
565 Int_t AliTRDtrackV1::GetProlongation(Double_t xk, Double_t &y, Double_t &z) const
566 {
567   //
568   // Find a prolongation at given x
569   // Return -1 if it does not exist
570   //  
571
572   Double_t bz = GetBz();
573   if (!AliExternalTrackParam::GetYAt(xk,bz,y)) return -1;
574   if (!AliExternalTrackParam::GetZAt(xk,bz,z)) return -1;
575
576   return 1;  
577
578 }
579
580 //_____________________________________________________________________________
581 Bool_t AliTRDtrackV1::PropagateTo(Double_t xk, Double_t xx0, Double_t xrho)
582 {
583   //
584   // Propagates this track to a reference plane defined by "xk" [cm] 
585   // correcting for the mean crossed material.
586   //
587   // "xx0"  - thickness/rad.length [units of the radiation length] 
588   // "xrho" - thickness*density    [g/cm^2] 
589   // 
590
591   if (xk == GetX()) return kTRUE;
592
593   Double_t xyz0[3] = {GetX(), GetY(), GetZ()}, // track position BEFORE propagation 
594            b[3];    // magnetic field 
595   GetBxByBz(b);
596   if(!AliExternalTrackParam::PropagateToBxByBz(xk,b)) return kFALSE;
597  
598   // local track position AFTER propagation 
599   Double_t xyz1[3] = {GetX(), GetY(), GetZ()};
600   if(xyz0[0] < xk) {
601     xrho = -xrho;
602     if (IsStartedTimeIntegral()) {
603       Double_t l2  = TMath::Sqrt((xyz1[0]-xyz0[0])*(xyz1[0]-xyz0[0]) 
604                                + (xyz1[1]-xyz0[1])*(xyz1[1]-xyz0[1]) 
605                                + (xyz1[2]-xyz0[2])*(xyz1[2]-xyz0[2]));
606       Double_t crv = AliExternalTrackParam::GetC(b[2]);
607       if (TMath::Abs(l2*crv) > 0.0001) {
608         // Make correction for curvature if neccesary
609         l2 = 0.5 * TMath::Sqrt((xyz1[0]-xyz0[0])*(xyz1[0]-xyz0[0]) 
610                              + (xyz1[1]-xyz0[1])*(xyz1[1]-xyz0[1]));
611         l2 = 2.0 * TMath::ASin(l2 * crv) / crv;
612         l2 = TMath::Sqrt(l2*l2 + (xyz1[2]-xyz0[2])*(xyz1[2]-xyz0[2]));
613       }
614       AddTimeStep(l2);
615     }
616   }
617
618   if (!AliExternalTrackParam::CorrectForMeanMaterial(xx0, xrho, GetMass())) return kFALSE;
619
620
621   {
622
623     // Energy losses
624     Double_t p2    = (1.0 + GetTgl()*GetTgl()) / (GetSigned1Pt()*GetSigned1Pt());
625     Double_t beta2 = p2 / (p2 + GetMass()*GetMass());
626     if ((beta2 < 1.0e-10) || 
627         ((5940.0 * beta2/(1.0 - beta2 + 1.0e-10) - beta2) < 0.0)) {
628       return kFALSE;
629     }
630
631     Double_t dE    = 0.153e-3 / beta2 
632                    * (TMath::Log(5940.0 * beta2/(1.0 - beta2 + 1.0e-10)) - beta2)
633                    * xrho;
634     fBudget[0] += xrho;
635
636     /*
637     // Suspicious part - think about it ?
638     Double_t kinE =  TMath::Sqrt(p2);
639     if (dE > 0.8*kinE) dE = 0.8 * kinE;  //      
640     if (dE < 0)        dE = 0.0;         // Not valid region for Bethe bloch 
641     */
642  
643     fDE += dE;
644
645     /*
646     // Suspicious ! I.B.
647     Double_t sigmade = 0.07 * TMath::Sqrt(TMath::Abs(dE));   // Energy loss fluctuation 
648     Double_t sigmac2 = sigmade*sigmade*fC*fC*(p2+GetMass()*GetMass())/(p2*p2);
649     fCcc += sigmac2;
650     fCee += fX*fX * sigmac2;  
651     */
652
653   }
654
655   return kTRUE;
656 }
657
658 //_____________________________________________________________________________
659 Int_t   AliTRDtrackV1::PropagateToR(Double_t r,Double_t step)
660 {
661   //
662   // Propagate track to the radial position
663   // Rotation always connected to the last track position
664   //
665
666   Double_t xyz0[3];
667   Double_t xyz1[3];
668   Double_t y;
669   Double_t z; 
670
671   Double_t radius = TMath::Sqrt(GetX()*GetX() + GetY()*GetY());
672   // Direction +-
673   Double_t dir    = (radius > r) ? -1.0 : 1.0;   
674
675   for (Double_t x = radius+dir*step; dir*x < dir*r; x += dir*step) {
676
677     GetXYZ(xyz0);       
678     Double_t alpha = TMath::ATan2(xyz0[1],xyz0[0]);
679     Rotate(alpha,kTRUE);
680     GetXYZ(xyz0);       
681     if(GetProlongation(x,y,z)<0) return -1;
682     xyz1[0] = x * TMath::Cos(alpha) + y * TMath::Sin(alpha); 
683     xyz1[1] = x * TMath::Sin(alpha) - y * TMath::Cos(alpha);
684     xyz1[2] = z;
685     Double_t param[7];
686     if(AliTracker::MeanMaterialBudget(xyz0,xyz1,param)<=0.) return -1;
687     if (param[1] <= 0) {
688       param[1] = 100000000;
689     }
690     PropagateTo(x,param[1],param[0]*param[4]);
691
692   } 
693
694   GetXYZ(xyz0); 
695   Double_t alpha = TMath::ATan2(xyz0[1],xyz0[0]);
696   Rotate(alpha,kTRUE);
697   GetXYZ(xyz0); 
698   if(GetProlongation(r,y,z)<0) return -1;
699   xyz1[0] = r * TMath::Cos(alpha) + y * TMath::Sin(alpha); 
700   xyz1[1] = r * TMath::Sin(alpha) - y * TMath::Cos(alpha);
701   xyz1[2] = z;
702   Double_t param[7];
703   if(AliTracker::MeanMaterialBudget(xyz0,xyz1,param) <= 0.) return -1;
704
705   if (param[1] <= 0) {
706     param[1] = 100000000;
707   }
708   PropagateTo(r,param[1],param[0]*param[4]);
709
710   return 0;
711
712 }
713
714 //_____________________________________________________________________________
715 void AliTRDtrackV1::Print(Option_t *o) const
716 {
717   // Print track status
718   AliInfo(Form("PID [%4.1f %4.1f %4.1f %4.1f %4.1f]", 1.E2*fPID[0], 1.E2*fPID[1], 1.E2*fPID[2], 1.E2*fPID[3], 1.E2*fPID[4]));
719   AliInfo(Form("Material[%5.2f %5.2f %5.2f]", fBudget[0], fBudget[1], fBudget[2]));
720
721   AliInfo(Form("x[%7.2f] t[%7.4f] alpha[%f] mass[%f]", GetX(), GetIntegratedLength(), GetAlpha(), fMass));
722   AliInfo(Form("Ntr[%1d] NtrPID[%1d] Ncl[%3d] lab[%3d]", GetNumberOfTracklets(), GetNumberOfTrackletsPID(), fN, fLab));
723
724   printf("|X| = (");
725   const Double_t *curP = GetParameter();
726   for (Int_t i = 0; i < 5; i++) printf("%7.2f ", curP[i]);
727   printf(")\n");
728
729   printf("|V| = \n");
730   const Double_t *curC = GetCovariance();
731   for (Int_t i = 0, j=4, k=0; i<15; i++, k++){
732     printf("%7.2f ", curC[i]);
733     if(k==j){ 
734       printf("\n");
735       k=-1; j--;
736     }
737   }
738   if(strcmp(o, "a")!=0) return;
739
740   for(Int_t ip=0; ip<kNplane; ip++){
741     if(!fTracklet[ip]) continue;
742     fTracklet[ip]->Print(o);
743   }
744 }
745
746
747 //_____________________________________________________________________________
748 Bool_t AliTRDtrackV1::Rotate(Double_t alpha, Bool_t absolute)
749 {
750   //
751   // Rotates track parameters in R*phi plane
752   // if absolute rotation alpha is in global system
753   // otherwise alpha rotation is relative to the current rotation angle
754   //  
755
756   if (absolute) alpha -= GetAlpha();
757   //else fNRotate++;
758
759   return AliExternalTrackParam::Rotate(GetAlpha()+alpha);
760 }
761
762 //___________________________________________________________
763 void AliTRDtrackV1::SetNumberOfClusters() 
764 {
765 // Calculate the number of clusters attached to this track
766         
767   Int_t ncls = 0;
768   for(int ip=0; ip<kNplane; ip++){
769     if(fTracklet[ip] && fTrackletIndex[ip] >= 0) ncls += fTracklet[ip]->GetN();
770   }
771   AliKalmanTrack::SetNumberOfClusters(ncls);    
772 }
773
774         
775 //_______________________________________________________________
776 void AliTRDtrackV1::SetOwner()
777 {
778   //
779   // Toggle ownership of tracklets
780   //
781
782   if(TestBit(kOwner)) return;
783   for (Int_t ip = 0; ip < kNplane; ip++) {
784     if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
785     fTracklet[ip] = new AliTRDseedV1(*fTracklet[ip]);
786     fTracklet[ip]->SetOwner();
787   }
788   SetBit(kOwner);
789 }
790
791 //_______________________________________________________________
792 void AliTRDtrackV1::SetTracklet(AliTRDseedV1 *const trklt, Int_t index)
793 {
794   //
795   // Set the tracklets
796   //
797   Int_t plane = trklt->GetPlane();
798
799   fTracklet[plane]      = trklt;
800   fTrackletIndex[plane] = index;
801 }
802
803 //_______________________________________________________________
804 void AliTRDtrackV1::SetTrackIn()
805 {
806   const AliExternalTrackParam *op = dynamic_cast<const AliExternalTrackParam*>(this);
807   fTrackLow = fTrackLow ? new(fTrackLow) AliExternalTrackParam(*op) : new AliExternalTrackParam(*op);
808 }
809
810 //_______________________________________________________________
811 void AliTRDtrackV1::SetTrackOut(const AliExternalTrackParam *op)
812 {
813   if(!op) op = dynamic_cast<const AliExternalTrackParam*>(this);
814   fTrackHigh = fTrackHigh ? new(fTrackHigh) AliExternalTrackParam(*op) : new AliExternalTrackParam(*op);
815 }
816
817 //_______________________________________________________________
818 void AliTRDtrackV1::UnsetTracklet(Int_t plane)
819 {
820   if(plane<0 && plane >= kNplane) return;
821   fTrackletIndex[plane] = -1;
822   fTracklet[plane] = NULL;
823 }
824
825
826 //_______________________________________________________________
827 void AliTRDtrackV1::UpdateChi2(Float_t chi2)
828 {
829 // Update chi2/track with one tracklet contribution
830   SetChi2(GetChi2() + chi2);
831 }
832
833 //_______________________________________________________________
834 void AliTRDtrackV1::UpdateESDtrack(AliESDtrack *track)
835 {
836   //
837   // Update the TRD PID information in the ESD track
838   //
839
840   Int_t nslices = fkReconstructor->GetRecoParam()->IsEightSlices() ? (Int_t)AliTRDpidUtil::kNNslices : (Int_t)AliTRDpidUtil::kLQslices;
841   // number of tracklets used for PID calculation
842   UChar_t nPID = GetNumberOfTrackletsPID();
843   // number of tracklets attached to the track
844   UChar_t nTrk = GetNumberOfTracklets();
845   // pack the two numbers together and store them in the ESD
846   track->SetTRDntracklets(nPID | (nTrk<<3));
847   // allocate space to store raw PID signals dEdx & momentum
848   track->SetNumberOfTRDslices((nslices+2)*AliTRDgeometry::kNlayer);
849   // store raw signals
850   Float_t p, sp; Double_t spd;
851   for (Int_t ip = 0; ip < kNplane; ip++) {
852     if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
853     if(!fTracklet[ip]->HasPID()) continue;
854     const Float_t *dedx = fTracklet[ip]->GetdEdx();
855     for (Int_t js = 0; js < nslices; js++, dedx++) track->SetTRDslice(*dedx, ip, js);
856     p = fTracklet[ip]->GetMomentum(&sp); spd = sp;
857     track->SetTRDmomentum(p, ip, &spd);
858   }
859   // store PID probabilities
860   track->SetTRDpid(fPID);
861 }