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