]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDtrackV1.cxx
e26ed2b68d2f3d1e4ca4d98d8a3f918951a39551
[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 AliTRDtrackV1 &AliTRDtrackV1::operator=(const AliTRDtrackV1 &t)
234 {
235   //
236   // Assignment operator
237   //
238
239   if (this != &t) {
240     ((AliTRDtrackV1 &) t).Copy(*this);
241   }
242
243   return *this;
244
245 }
246
247 //_____________________________________________________________________________
248 void AliTRDtrackV1::Copy(TObject &t) const
249 {
250   //
251   // Copy function
252   //
253
254   ((AliTRDtrackV1 &) t).fStatus         = fStatus;
255   ((AliTRDtrackV1 &) t).fESDid          = fESDid;
256   ((AliTRDtrackV1 &) t).fDE             = fDE;
257   ((AliTRDtrackV1 &) t).fkReconstructor = fkReconstructor;
258   ((AliTRDtrackV1 &) t).fBackupTrack    = 0x0;
259   ((AliTRDtrackV1 &) t).fTrackLow       = 0x0;
260   ((AliTRDtrackV1 &) t).fTrackHigh      = 0x0;
261
262   for(Int_t ip = 0; ip < kNplane; ip++) { 
263     ((AliTRDtrackV1 &) t).fTrackletIndex[ip] = fTrackletIndex[ip];
264     ((AliTRDtrackV1 &) t).fTracklet[ip]      = fTracklet[ip];
265   }
266   if (fTrackLow) {
267     ((AliTRDtrackV1 &) t).fTrackLow  = new AliExternalTrackParam(*fTrackLow);
268   }
269   if (fTrackHigh){
270     ((AliTRDtrackV1 &) t).fTrackHigh = new AliExternalTrackParam(*fTrackHigh);
271   }
272  
273   for (Int_t i = 0; i < 3; i++) {
274     ((AliTRDtrackV1 &) t).fBudget[i] = fBudget[i];
275   }
276   for (Int_t is = 0; is < AliPID::kSPECIES; is++) {
277     ((AliTRDtrackV1 &) t).fPID[is] = fPID[is];
278   }  
279
280 }
281
282 //_______________________________________________________________
283 Bool_t AliTRDtrackV1::CookLabel(Float_t wrong)
284 {
285   // set MC label for this track
286   if(!GetNumberOfClusters()) return kFALSE;
287
288   Int_t s[kMAXCLUSTERSPERTRACK][2];
289   for (Int_t i = 0; i < kMAXCLUSTERSPERTRACK; i++) {
290     s[i][0] = -1;
291     s[i][1] =  0;
292   }
293   
294   Bool_t labelAdded;
295   Int_t label;
296   AliTRDcluster *c    = NULL;
297   for (Int_t ip = 0; ip < kNplane; ip++) {
298     if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
299     for (Int_t ic = 0; ic < AliTRDseedV1::kNclusters; ic++) {
300       if(!(c = fTracklet[ip]->GetClusters(ic))) continue;
301       for (Int_t k = 0; k < 3; k++) { 
302         label      = c->GetLabel(k);
303         labelAdded = kFALSE; 
304         Int_t j = 0;
305         if (label >= 0) {
306           while ((!labelAdded) && (j < kMAXCLUSTERSPERTRACK)) {
307             if ((s[j][0] == label) || 
308                 (s[j][1] ==     0)) {
309               s[j][0] = label; 
310               s[j][1]++; 
311               labelAdded = kTRUE;
312             }
313             j++;
314           }
315         }
316       }
317     }
318   }
319   
320   Int_t max = 0;
321   label = -123456789;
322   for (Int_t i = 0; i < kMAXCLUSTERSPERTRACK; i++) {
323     if (s[i][1] <= max) continue;
324     max   = s[i][1]; 
325     label = s[i][0];
326   }
327   if ((1. - Float_t(max)/GetNumberOfClusters()) > wrong) label = -label;
328   
329   SetLabel(label); 
330   
331   return kTRUE;
332 }
333
334 //_______________________________________________________________
335 Bool_t AliTRDtrackV1::CookPID()
336 {
337 //
338 // Cook the PID information for the track by delegating the omonim function of the tracklets. 
339 // Computes the number of tracklets used. The tracklet information are considered independent. 
340 // For the moment no global track measurement of PID is performed as for example to estimate 
341 // bremsstrahlung probability based on global chi2 of the track.
342 //
343 // The status bit AliESDtrack::kTRDpid is set during the call of AliTRDtrackV1::UpdateESDtrack().The PID performance of the 
344 //TRD for tracks with 6 tacklets is displayed below.
345 //Begin_Html
346 //<img src="TRD/trackPID.gif">
347 //End_Html
348 //
349   
350   /*Reset the a priori probabilities*/
351   Double_t pid = 1. / AliPID::kSPECIES;
352   for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) fPID[ispec] = pid;  
353
354   UChar_t fPIDquality = SetNumberOfTrackletsPID(kTRUE);
355   // no tracklet found for PID calculations
356   if(!fPIDquality) return kFALSE;
357   
358   // slot for PID calculation @ track level for bremsstrahlung TODO
359   
360   // normalize probabilities
361   Double_t probTotal = 0.0;
362   for (Int_t is = 0; is < AliPID::kSPECIES; is++) probTotal += fPID[is];
363   
364   
365   if (probTotal <= 0.0) {
366     AliWarning("The total probability over all species <= 0. This may be caused by some error in the reference data.");
367     return kFALSE;
368   }
369   
370   for (Int_t iSpecies = 0; iSpecies < AliPID::kSPECIES; iSpecies++) fPID[iSpecies] /= probTotal;
371   
372   return kTRUE;
373 }
374
375 //___________________________________________________________
376 UChar_t AliTRDtrackV1::GetNumberOfTrackletsPID() const
377 {
378 // Retrieve number of tracklets used for PID calculation. 
379
380   UChar_t nPID = 0;
381   Float_t *prob = NULL;
382   for(int ip=0; ip<kNplane; ip++){
383     if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
384     if(!fTracklet[ip]->IsOK()) continue;
385     if(!(prob = fTracklet[ip]->GetProbability(kFALSE))) continue;
386
387     Int_t nspec = 0; // quality check of tracklet dEdx
388     for(int ispec=0; ispec<AliPID::kSPECIES; ispec++){
389       if(prob[ispec] < 0.) continue;
390       nspec++;
391     }
392     if(!nspec) continue;
393     
394     fTracklet[ip]->SetPID();
395     nPID++;
396   }
397   return nPID;
398 }
399
400 //___________________________________________________________
401 UChar_t AliTRDtrackV1::SetNumberOfTrackletsPID(Bool_t recalc)
402 {
403 // Retrieve number of tracklets used for PID calculation. // Recalculated PID at tracklet level by quering the PID DB.
404
405   UChar_t fPIDquality(0);
406   
407   // steer PID calculation @ tracklet level
408   Float_t *prob(NULL);
409   for(int ip=0; ip<kNplane; ip++){
410     if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
411     if(!fTracklet[ip]->IsOK()) continue;
412     if(!(prob = fTracklet[ip]->GetProbability(recalc))) return 0;
413
414     Int_t nspec = 0; // quality check of tracklet dEdx
415     for(int ispec=0; ispec<AliPID::kSPECIES; ispec++){
416       if(prob[ispec] < 0.) continue;
417       fPID[ispec] *= prob[ispec];
418       nspec++;
419     }
420     if(!nspec) continue;
421     
422     fPIDquality++;
423   }
424   return fPIDquality;
425 }
426
427 //_______________________________________________________________
428 AliTRDcluster* AliTRDtrackV1::GetCluster(Int_t id)
429 {
430   // Get the cluster at a certain position in the track
431   Int_t n = 0;
432   for(Int_t ip=0; ip<kNplane; ip++){
433     if(!fTracklet[ip]) continue;
434     if(n+fTracklet[ip]->GetN() <= id){ 
435       n+=fTracklet[ip]->GetN();
436       continue;
437     }
438     AliTRDcluster *c = NULL;
439     for(Int_t ic=AliTRDseedV1::kNclusters; ic--;){
440       if(!(c = fTracklet[ip]->GetClusters(ic))) continue;
441
442       if(n<id){n++; continue;}
443       return c;
444     }
445   }
446   return NULL;
447 }
448
449 //_______________________________________________________________
450 Int_t  AliTRDtrackV1::GetClusterIndex(Int_t id) const
451 {
452   // Get the cluster index at a certain position in the track
453   Int_t n = 0;
454   for(Int_t ip=0; ip<kNplane; ip++){
455     if(!fTracklet[ip]) continue;
456     if(n+fTracklet[ip]->GetN() <= id){ 
457       n+=fTracklet[ip]->GetN();
458       continue;
459     }
460     for(Int_t ic=AliTRDseedV1::kNclusters; ic--;){
461       if(!(fTracklet[ip]->GetClusters(ic))) continue;
462       if(n<id){n++; continue;}
463       return fTracklet[ip]->GetIndexes(ic);
464     }
465   }
466   return -1;
467 }
468
469 //_______________________________________________________________
470 Double_t AliTRDtrackV1::GetPredictedChi2(const AliTRDseedV1 *trklt, Double_t *cov) const
471 {
472 // Compute chi2 between tracklet and track. The value is calculated at the radial position of the track
473 // equal to the reference radial position of the tracklet (see AliTRDseedV1)
474 // 
475 // The chi2 estimator is computed according to the following formula
476 // BEGIN_LATEX
477 // #chi^{2}=(X_{trklt}-X_{track})(C_{trklt}+C_{track})^{-1}(X_{trklt}-X_{track})^{T}
478 // END_LATEX
479 // where X=(y z), the position of the track/tracklet in the yz plane
480 // 
481
482   Double_t p[2]   = { trklt->GetY(), trklt->GetZ()};
483   trklt->GetCovAt(trklt->GetX(), cov);
484   return AliExternalTrackParam::GetPredictedChi2(p, cov);
485 }
486
487 //_______________________________________________________________
488 Int_t AliTRDtrackV1::GetSector() const
489 {
490   return Int_t(GetAlpha()/AliTRDgeometry::GetAlpha() + (GetAlpha()>0. ? 0 : AliTRDgeometry::kNsector));
491 }
492
493 //_______________________________________________________________
494 Bool_t AliTRDtrackV1::IsEqual(const TObject *o) const
495 {
496   // Checks whether two tracks are equal
497   if (!o) return kFALSE;
498   const AliTRDtrackV1 *inTrack = dynamic_cast<const AliTRDtrackV1*>(o);
499   if (!inTrack) return kFALSE;
500   
501   //if ( fPIDquality != inTrack->GetPIDquality() ) return kFALSE;
502   
503   for(Int_t i = 0; i < AliPID::kSPECIES; i++){
504     if ( fPID[i] != inTrack->GetPID(i) ) return kFALSE;
505   }
506   
507   for (Int_t i = 0; i < 3; i++){
508     if ( fBudget[i] != inTrack->GetBudget(i) ) return kFALSE;
509   }
510   if ( fDE != inTrack->GetEdep() ) return kFALSE;
511   if ( fFakeRatio != inTrack->GetFakeRatio() ) return kFALSE;
512   if ( fChi2 != inTrack->GetChi2() ) return kFALSE;
513   if ( fMass != inTrack->GetMass() ) return kFALSE;
514   if ( fLab != inTrack->GetLabel() ) return kFALSE;
515   if ( fN != inTrack->GetNumberOfClusters() ) return kFALSE;
516   if ( AliKalmanTrack::GetIntegratedLength() != inTrack->GetIntegratedLength() ) return kFALSE;
517   
518   if ( GetX() != inTrack->GetX() ) return kFALSE;
519   if ( GetAlpha() != inTrack->GetAlpha() ) return kFALSE;
520   const Double_t *inP = inTrack->GetParameter();
521   const Double_t *curP = GetParameter();
522   for (Int_t i = 0; i < 5; i++){
523     if ( curP[i] != inP[i]) return kFALSE;
524   }
525   const Double_t *inC = inTrack->GetCovariance();
526   const Double_t *curC = GetCovariance();
527   for (Int_t i = 0; i < 15; i++){
528     if ( curC[i] != inC[i]) return kFALSE;
529   }
530   
531   for (Int_t iTracklet = 0; iTracklet < kNplane; iTracklet++){
532     AliTRDseedV1 *curTracklet = fTracklet[iTracklet];
533     AliTRDseedV1 *inTracklet = inTrack->GetTracklet(iTracklet);
534     if (curTracklet && inTracklet){
535       if (! curTracklet->IsEqual(inTracklet) ) {
536         curTracklet->Print();
537         inTracklet->Print();
538         return kFALSE;
539       }
540     } else {
541       // if one tracklet exists, and corresponding 
542       // in other track doesn't - return kFALSE
543       if(inTracklet || curTracklet) return kFALSE;
544     }
545   }
546
547   return kTRUE;
548 }
549
550 //_______________________________________________________________
551 Bool_t AliTRDtrackV1::IsElectron() const
552 {
553   if(GetPID(0) > fkReconstructor->GetRecoParam()->GetPIDThreshold(GetP())) return kTRUE;
554   return kFALSE;
555 }
556
557         
558 //_____________________________________________________________________________
559 Int_t AliTRDtrackV1::MakeBackupTrack()
560 {
561 //
562 // Creates a backup track
563 // TO DO update quality check of the track.
564 //
565
566   Float_t occupancy(0.); Int_t n(0), ncls(0);
567   for(Int_t il(AliTRDgeometry::kNlayer); il--;){ 
568     if(!fTracklet[il]) continue;
569     n++; 
570     occupancy+=fTracklet[il]->GetOccupancyTB();
571     ncls += fTracklet[il]->GetN();
572   }
573   if(!n) return -1;
574   occupancy/=n;
575
576   //Float_t ratio1 = Float_t(t.GetNumberOfClusters()+1) / Float_t(t.GetNExpected()+1);  
577   
578   Int_t failedCutId(0);
579   if(GetChi2()/n > 5.0) failedCutId=1; 
580   if(occupancy < 0.7) failedCutId=2;
581   //if(ratio1 >   0.6) && 
582   //if(ratio0+ratio1  >   1.5) && 
583   if(GetNCross() != 0)  failedCutId=3;
584   if(TMath::Abs(GetSnp()) > 0.85) failedCutId=4;
585   if(ncls < 20) failedCutId=5;
586
587   if(failedCutId){ 
588     AliDebug(2, Form("\n"
589       "chi2/tracklet < 5.0   [%c] %5.2f\n"
590       "occupancy > 0.7       [%c] %4.2f\n"
591       "NCross == 0           [%c] %d\n"
592       "Abs(snp) < 0.85       [%c] %4.2f\n"
593       "NClusters > 20        [%c] %d"
594       ,(GetChi2()/n<5.0)?'y':'n', GetChi2()/n
595       ,(occupancy>0.7)?'y':'n', occupancy
596       ,(GetNCross()==0)?'y':'n', GetNCross()
597       ,(TMath::Abs(GetSnp())<0.85)?'y':'n', TMath::Abs(GetSnp())
598       ,(ncls>20)?'y':'n', ncls
599     ));
600     return failedCutId;
601   }
602
603   if(fBackupTrack) {
604     fBackupTrack->~AliTRDtrackV1();
605     new(fBackupTrack) AliTRDtrackV1((AliTRDtrackV1&)(*this));
606     return 0;
607   }
608   fBackupTrack = new AliTRDtrackV1((AliTRDtrackV1&)(*this));
609   return 0;
610 }
611
612 //_____________________________________________________________________________
613 Int_t AliTRDtrackV1::GetProlongation(Double_t xk, Double_t &y, Double_t &z) const
614 {
615   //
616   // Find a prolongation at given x
617   // Return -1 if it does not exist
618   //  
619
620   Double_t bz = GetBz();
621   if (!AliExternalTrackParam::GetYAt(xk,bz,y)) return -1;
622   if (!AliExternalTrackParam::GetZAt(xk,bz,z)) return -1;
623
624   return 1;  
625
626 }
627
628 //_____________________________________________________________________________
629 Bool_t AliTRDtrackV1::PropagateTo(Double_t xk, Double_t xx0, Double_t xrho)
630 {
631   //
632   // Propagates this track to a reference plane defined by "xk" [cm] 
633   // correcting for the mean crossed material.
634   //
635   // "xx0"  - thickness/rad.length [units of the radiation length] 
636   // "xrho" - thickness*density    [g/cm^2] 
637   // 
638
639   if (xk == GetX()) return kTRUE;
640
641   Double_t xyz0[3] = {GetX(), GetY(), GetZ()}, // track position BEFORE propagation 
642            b[3];    // magnetic field 
643   GetBxByBz(b);
644   if(!AliExternalTrackParam::PropagateToBxByBz(xk,b)) return kFALSE;
645  
646   // local track position AFTER propagation 
647   Double_t xyz1[3] = {GetX(), GetY(), GetZ()};
648   if(xyz0[0] < xk) {
649     xrho = -xrho;
650     if (IsStartedTimeIntegral()) {
651       Double_t l2  = TMath::Sqrt((xyz1[0]-xyz0[0])*(xyz1[0]-xyz0[0]) 
652                                + (xyz1[1]-xyz0[1])*(xyz1[1]-xyz0[1]) 
653                                + (xyz1[2]-xyz0[2])*(xyz1[2]-xyz0[2]));
654       Double_t crv = AliExternalTrackParam::GetC(b[2]);
655       if (TMath::Abs(l2*crv) > 0.0001) {
656         // Make correction for curvature if neccesary
657         l2 = 0.5 * TMath::Sqrt((xyz1[0]-xyz0[0])*(xyz1[0]-xyz0[0]) 
658                              + (xyz1[1]-xyz0[1])*(xyz1[1]-xyz0[1]));
659         l2 = 2.0 * TMath::ASin(l2 * crv) / crv;
660         l2 = TMath::Sqrt(l2*l2 + (xyz1[2]-xyz0[2])*(xyz1[2]-xyz0[2]));
661       }
662       AddTimeStep(l2);
663     }
664   }
665
666   if (!AliExternalTrackParam::CorrectForMeanMaterial(xx0, xrho, GetMass())) return kFALSE;
667
668
669   {
670
671     // Energy losses
672     Double_t p2    = (1.0 + GetTgl()*GetTgl()) / (GetSigned1Pt()*GetSigned1Pt());
673     Double_t beta2 = p2 / (p2 + GetMass()*GetMass());
674     if ((beta2 < 1.0e-10) || 
675         ((5940.0 * beta2/(1.0 - beta2 + 1.0e-10) - beta2) < 0.0)) {
676       return kFALSE;
677     }
678
679     Double_t dE    = 0.153e-3 / beta2 
680                    * (TMath::Log(5940.0 * beta2/(1.0 - beta2 + 1.0e-10)) - beta2)
681                    * xrho;
682     fBudget[0] += xrho;
683
684     /*
685     // Suspicious part - think about it ?
686     Double_t kinE =  TMath::Sqrt(p2);
687     if (dE > 0.8*kinE) dE = 0.8 * kinE;  //      
688     if (dE < 0)        dE = 0.0;         // Not valid region for Bethe bloch 
689     */
690  
691     fDE += dE;
692
693     /*
694     // Suspicious ! I.B.
695     Double_t sigmade = 0.07 * TMath::Sqrt(TMath::Abs(dE));   // Energy loss fluctuation 
696     Double_t sigmac2 = sigmade*sigmade*fC*fC*(p2+GetMass()*GetMass())/(p2*p2);
697     fCcc += sigmac2;
698     fCee += fX*fX * sigmac2;  
699     */
700
701   }
702
703   return kTRUE;
704 }
705
706 //_____________________________________________________________________________
707 Int_t   AliTRDtrackV1::PropagateToR(Double_t r,Double_t step)
708 {
709   //
710   // Propagate track to the radial position
711   // Rotation always connected to the last track position
712   //
713
714   Double_t xyz0[3];
715   Double_t xyz1[3];
716   Double_t y;
717   Double_t z; 
718
719   Double_t radius = TMath::Sqrt(GetX()*GetX() + GetY()*GetY());
720   // Direction +-
721   Double_t dir    = (radius > r) ? -1.0 : 1.0;   
722
723   for (Double_t x = radius+dir*step; dir*x < dir*r; x += dir*step) {
724
725     GetXYZ(xyz0);       
726     Double_t alpha = TMath::ATan2(xyz0[1],xyz0[0]);
727     Rotate(alpha,kTRUE);
728     GetXYZ(xyz0);       
729     if(GetProlongation(x,y,z)<0) return -1;
730     xyz1[0] = x * TMath::Cos(alpha) + y * TMath::Sin(alpha); 
731     xyz1[1] = x * TMath::Sin(alpha) - y * TMath::Cos(alpha);
732     xyz1[2] = z;
733     Double_t param[7];
734     if(AliTracker::MeanMaterialBudget(xyz0,xyz1,param)<=0.) return -1;
735     if (param[1] <= 0) {
736       param[1] = 100000000;
737     }
738     PropagateTo(x,param[1],param[0]*param[4]);
739
740   } 
741
742   GetXYZ(xyz0); 
743   Double_t alpha = TMath::ATan2(xyz0[1],xyz0[0]);
744   Rotate(alpha,kTRUE);
745   GetXYZ(xyz0); 
746   if(GetProlongation(r,y,z)<0) return -1;
747   xyz1[0] = r * TMath::Cos(alpha) + y * TMath::Sin(alpha); 
748   xyz1[1] = r * TMath::Sin(alpha) - y * TMath::Cos(alpha);
749   xyz1[2] = z;
750   Double_t param[7];
751   if(AliTracker::MeanMaterialBudget(xyz0,xyz1,param) <= 0.) return -1;
752
753   if (param[1] <= 0) {
754     param[1] = 100000000;
755   }
756   PropagateTo(r,param[1],param[0]*param[4]);
757
758   return 0;
759
760 }
761
762 //_____________________________________________________________________________
763 void AliTRDtrackV1::Print(Option_t *o) const
764 {
765   // Print track status
766   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]));
767   AliInfo(Form("Material[%5.2f %5.2f %5.2f]", fBudget[0], fBudget[1], fBudget[2]));
768
769   AliInfo(Form("x[%7.2f] t[%7.4f] alpha[%f] mass[%f]", GetX(), GetIntegratedLength(), GetAlpha(), fMass));
770   AliInfo(Form("Ntr[%1d] NtrPID[%1d] Ncl[%3d] lab[%3d]", GetNumberOfTracklets(), GetNumberOfTrackletsPID(), fN, fLab));
771
772   printf("|X| = (");
773   const Double_t *curP = GetParameter();
774   for (Int_t i = 0; i < 5; i++) printf("%7.2f ", curP[i]);
775   printf(")\n");
776
777   printf("|V| = \n");
778   const Double_t *curC = GetCovariance();
779   for (Int_t i = 0, j=4, k=0; i<15; i++, k++){
780     printf("%7.2f ", curC[i]);
781     if(k==j){ 
782       printf("\n");
783       k=-1; j--;
784     }
785   }
786   if(strcmp(o, "a")!=0) return;
787
788   for(Int_t ip=0; ip<kNplane; ip++){
789     if(!fTracklet[ip]) continue;
790     fTracklet[ip]->Print(o);
791   }
792 }
793
794
795 //_____________________________________________________________________________
796 Bool_t AliTRDtrackV1::Rotate(Double_t alpha, Bool_t absolute)
797 {
798   //
799   // Rotates track parameters in R*phi plane
800   // if absolute rotation alpha is in global system
801   // otherwise alpha rotation is relative to the current rotation angle
802   //  
803
804   if (absolute) alpha -= GetAlpha();
805   //else fNRotate++;
806
807   return AliExternalTrackParam::Rotate(GetAlpha()+alpha);
808 }
809
810 //___________________________________________________________
811 void AliTRDtrackV1::SetNumberOfClusters() 
812 {
813 // Calculate the number of clusters attached to this track
814         
815   Int_t ncls = 0;
816   for(int ip=0; ip<kNplane; ip++){
817     if(fTracklet[ip] && fTrackletIndex[ip] >= 0) ncls += fTracklet[ip]->GetN();
818   }
819   AliKalmanTrack::SetNumberOfClusters(ncls);    
820 }
821
822         
823 //_______________________________________________________________
824 void AliTRDtrackV1::SetOwner()
825 {
826   //
827   // Toggle ownership of tracklets
828   //
829
830   if(TestBit(kOwner)) return;
831   for (Int_t ip = 0; ip < kNplane; ip++) {
832     if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
833     fTracklet[ip] = new AliTRDseedV1(*fTracklet[ip]);
834     fTracklet[ip]->SetOwner();
835   }
836   SetBit(kOwner);
837 }
838
839 //_______________________________________________________________
840 void AliTRDtrackV1::SetTracklet(AliTRDseedV1 *const trklt, Int_t index)
841 {
842   //
843   // Set the tracklets
844   //
845   Int_t plane = trklt->GetPlane();
846
847   fTracklet[plane]      = trklt;
848   fTrackletIndex[plane] = index;
849 }
850
851 //_______________________________________________________________
852 void AliTRDtrackV1::SetTrackIn()
853 {
854 //  Save location of birth for the TRD track
855 //  If the pointer is not valid allocate memory
856 //
857   const AliExternalTrackParam *op = dynamic_cast<const AliExternalTrackParam*>(this);
858
859   printf("SetTrackIn() : fTrackLow[%p]\n", (void*)fTrackLow);
860   if(fTrackLow){
861     fTrackLow->~AliExternalTrackParam();
862     new(fTrackLow) AliExternalTrackParam(*op);
863   } else fTrackLow = new AliExternalTrackParam(*op);
864 }
865
866 //_______________________________________________________________
867 void AliTRDtrackV1::SetTrackOut(const AliExternalTrackParam *op)
868 {
869 //  Save location of death for the TRD track
870 //  If the pointer is not valid allocate memory
871 //
872   if(!op) op = dynamic_cast<const AliExternalTrackParam*>(this);
873   if(fTrackHigh){
874     fTrackHigh->~AliExternalTrackParam();
875     new(fTrackHigh) AliExternalTrackParam(*op);
876   } else fTrackHigh = new AliExternalTrackParam(*op);
877 }
878
879 //_______________________________________________________________
880 void AliTRDtrackV1::UnsetTracklet(Int_t plane)
881 {
882   if(plane<0) return;
883   fTrackletIndex[plane] = -1;
884   fTracklet[plane] = NULL;
885 }
886
887
888 //_______________________________________________________________
889 void AliTRDtrackV1::UpdateChi2(Float_t chi2)
890 {
891 // Update chi2/track with one tracklet contribution
892   SetChi2(GetChi2() + chi2);
893 }
894
895 //_______________________________________________________________
896 void AliTRDtrackV1::UpdateESDtrack(AliESDtrack *track)
897 {
898   //
899   // Update the TRD PID information in the ESD track
900   //
901
902   Int_t nslices = fkReconstructor->GetRecoParam()->IsEightSlices() ? (Int_t)AliTRDpidUtil::kNNslices : (Int_t)AliTRDpidUtil::kLQslices;
903   // number of tracklets used for PID calculation
904   UChar_t nPID = GetNumberOfTrackletsPID();
905   // number of tracklets attached to the track
906   UChar_t nTrk = GetNumberOfTracklets();
907   // pack the two numbers together and store them in the ESD
908   track->SetTRDntracklets(nPID | (nTrk<<3));
909   // allocate space to store raw PID signals dEdx & momentum
910   track->SetNumberOfTRDslices((nslices+2)*AliTRDgeometry::kNlayer);
911   // store raw signals
912   Float_t p, sp; Double_t spd;
913   for (Int_t ip = 0; ip < kNplane; ip++) {
914     if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
915     if(!fTracklet[ip]->HasPID()) continue;
916     const Float_t *dedx = fTracklet[ip]->GetdEdx();
917     for (Int_t js = 0; js < nslices; js++, dedx++) track->SetTRDslice(*dedx, ip, js);
918     p = fTracklet[ip]->GetMomentum(&sp); spd = sp;
919     track->SetTRDmomentum(p, ip, &spd);
920   }
921   // store PID probabilities
922   track->SetTRDpid(fPID);
923 }