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