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