]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDtrackV1.cxx
Update of reco param by Alex
[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 "AliESDtrack.h"
19 #include "AliTracker.h"
20
21 #include "AliTRDtrackV1.h"
22 #include "AliTRDcluster.h"
23 #include "AliTRDcalibDB.h"
24 #include "AliTRDReconstructor.h"
25 #include "AliTRDrecoParam.h"
26
27 ClassImp(AliTRDtrackV1)
28
29 ///////////////////////////////////////////////////////////////////////////////
30 //                                                                           //
31 //  Represents a reconstructed TRD track                                     //
32 //  Local TRD Kalman track                                                   //
33 //                                                                           //
34 //  Authors:                                                                 //
35 //    Alex Bercuci <A.Bercuci@gsi.de>                                        //
36 //    Markus Fasel <M.Fasel@gsi.de>                                          //
37 //                                                                           //
38 ///////////////////////////////////////////////////////////////////////////////
39
40 //_______________________________________________________________
41 AliTRDtrackV1::AliTRDtrackV1() : AliKalmanTrack()
42   ,fPIDquality(0)
43   ,fDE(0.)
44   ,fBackupTrack(0x0)
45 {
46   //
47   // Default constructor
48   //
49   for(int i =0; i<3; i++) fBudget[i] = 0.;
50   
51   Float_t pid = 1./AliPID::kSPECIES;
52   for(int is =0; is<AliPID::kSPECIES; is++) fPID[is] = pid;
53
54   for(int ip=0; ip<kNplane; ip++){
55     fTrackletIndex[ip] = 0xffff;
56     fTracklet[ip]      = 0x0;
57   }
58 }
59
60 //_______________________________________________________________
61 AliTRDtrackV1::AliTRDtrackV1(const AliTRDtrackV1 &ref) : AliKalmanTrack(ref)
62   ,fPIDquality(ref.fPIDquality)
63   ,fDE(ref.fDE)
64   ,fBackupTrack(0x0)
65 {
66   //
67   // Copy constructor
68   //
69
70   for(int ip=0; ip<kNplane; ip++){ 
71     fTrackletIndex[ip] = ref.fTrackletIndex[ip];
72     fTracklet[ip]      = ref.fTracklet[ip];
73   }
74
75   for (Int_t i = 0; i < 3;i++) fBudget[i]      = ref.fBudget[i];
76
77         for(Int_t is = 0; is<AliPID::kSPECIES; is++) fPID[is] = ref.fPID[is];
78   
79   AliKalmanTrack::SetNumberOfClusters(ref.GetNumberOfClusters());
80 }
81
82 //_______________________________________________________________
83 AliTRDtrackV1::AliTRDtrackV1(const AliESDtrack &t) : AliKalmanTrack()
84   ,fPIDquality(0)
85   ,fDE(0.)
86   ,fBackupTrack(0x0)
87 {
88   //
89   // Constructor from AliESDtrack
90   //
91
92   SetLabel(t.GetLabel());
93   SetChi2(0.0);
94   SetMass(t.GetMass());
95   AliKalmanTrack::SetNumberOfClusters(t.GetTRDncls()); 
96   Int_t ti[kNplane]; t.GetTRDtracklets(&ti[0]);
97   for(int ip=0; ip<kNplane; ip++){ 
98     fTrackletIndex[ip] = ti[ip] < 0 ? 0xffff : ti[ip];
99     fTracklet[ip]      = 0x0;
100   }
101   for(int i =0; i<3; i++) fBudget[i] = 0.;
102   
103   Float_t pid = 1./AliPID::kSPECIES;
104   for(int is =0; is<AliPID::kSPECIES; is++) fPID[is] = pid;
105
106   const AliExternalTrackParam *par = &t;
107   if (t.GetStatus() & AliESDtrack::kTRDbackup) { 
108     par = t.GetOuterParam();
109     if (!par) {
110       AliError("No backup info!"); 
111       par = &t;
112     }
113   }
114   Set(par->GetX() 
115      ,par->GetAlpha()
116      ,par->GetParameter()
117      ,par->GetCovariance());
118
119   if(t.GetStatus() & AliESDtrack::kTIME) {
120     StartTimeIntegral();
121     Double_t times[10]; 
122     t.GetIntegratedTimes(times); 
123     SetIntegratedTimes(times);
124     SetIntegratedLength(t.GetIntegratedLength());
125   }
126 }
127
128 //_______________________________________________________________
129 AliTRDtrackV1::AliTRDtrackV1(AliTRDseedV1 *trklts, const Double_t p[5], const Double_t cov[15]
130              , Double_t x, Double_t alpha) : AliKalmanTrack()
131   ,fPIDquality(0)
132   ,fDE(0.)
133   ,fBackupTrack(0x0)
134 {
135   //
136   // The stand alone tracking constructor
137   // TEMPORARY !!!!!!!!!!!
138   // to check :
139   // 1. covariance matrix
140   // 2. dQdl calculation
141   //
142
143   Double_t cnv = GetBz() < 1.e-5 ? 1.e5 : 1.0 / (GetBz() * kB2C);
144   //  Double_t cnv   = 1.0 / (GetBz() * kB2C);
145
146   Double_t pp[5] = { p[0]    
147                    , p[1]
148                    , x*p[4] - p[2]
149                    , p[3]
150                    , p[4]*cnv      };
151
152   Double_t c22 = x*x*cov[14] - 2*x*cov[12] + cov[ 5];
153   Double_t c32 =   x*cov[13] -     cov[ 8];
154   Double_t c20 =   x*cov[10] -     cov[ 3];
155   Double_t c21 =   x*cov[11] -     cov[ 4];
156   Double_t c42 =   x*cov[14] -     cov[12];
157
158   Double_t cc[15] = { cov[ 0]
159                     , cov[ 1],     cov[ 2]
160                     , c20,         c21,         c22
161                     , cov[ 6],     cov[ 7],     c32,     cov[ 9]
162                     , cov[10]*cnv, cov[11]*cnv, c42*cnv, cov[13]*cnv, cov[14]*cnv*cnv };
163
164   Double_t mostProbablePt=AliExternalTrackParam::GetMostProbablePt();
165   Double_t p0=TMath::Sign(1/mostProbablePt,pp[4]);
166   Double_t w0=cc[14]/(cc[14] + p0*p0), w1=p0*p0/(cc[14] + p0*p0);
167   pp[4] = w0*p0 + w1*pp[4];
168   cc[10]*=w1; cc[11]*=w1; cc[12]*=w1; cc[13]*=w1; cc[14]*=w1;
169
170         Set(x,alpha,pp,cc);
171   Int_t ncls = 0;
172         for(int iplane=0; iplane<kNplane; iplane++){
173     fTrackletIndex[iplane] = 0xffff;
174                 if(!trklts[iplane].IsOK()) fTracklet[iplane] = 0x0;
175     else{ 
176       fTracklet[iplane] = new AliTRDseedV1(trklts[iplane]);
177       ncls += fTracklet[iplane]->GetN();
178     }
179         }
180   AliKalmanTrack::SetNumberOfClusters(ncls);            
181   for(int i =0; i<3; i++) fBudget[i] = 0.;
182   
183   Float_t pid = 1./AliPID::kSPECIES;
184   for(int is =0; is<AliPID::kSPECIES; is++) fPID[is] = pid;
185
186 }
187
188 //_______________________________________________________________
189 AliTRDtrackV1::~AliTRDtrackV1()
190 {
191   if(fBackupTrack) {
192     delete fBackupTrack;
193   }
194   fBackupTrack = 0x0;
195
196   if(TestBit(1)){
197     for(Int_t ip=0; ip<kNplane; ip++){
198       if(fTracklet[ip]) delete fTracklet[ip];
199       fTracklet[ip] = 0x0;
200     }
201   }
202 }
203         
204 //_______________________________________________________________
205 Bool_t AliTRDtrackV1::CookLabel(Float_t wrong)
206 {
207   // set MC label for this track
208   
209   Int_t s[kMAXCLUSTERSPERTRACK][2];
210   for (Int_t i = 0; i < kMAXCLUSTERSPERTRACK; i++) {
211     s[i][0] = -1;
212     s[i][1] =  0;
213   }
214   
215   Bool_t labelAdded;
216   Int_t label;
217   AliTRDcluster *c    = 0x0;
218   for (Int_t ip = 0; ip < kNplane; ip++) {
219     if(fTrackletIndex[ip] == 0xffff) continue;
220     for (Int_t ic = 0; ic < AliTRDseed::knTimebins; ic++) {
221       if(!(c = fTracklet[ip]->GetClusters(ic))) continue;
222       for (Int_t k = 0; k < 3; k++) { 
223         label      = c->GetLabel(k);
224         labelAdded = kFALSE; 
225         Int_t j = 0;
226         if (label >= 0) {
227           while ((!labelAdded) && (j < kMAXCLUSTERSPERTRACK)) {
228             if ((s[j][0] == label) || 
229                 (s[j][1] ==     0)) {
230               s[j][0] = label; 
231               s[j][1]++; 
232               labelAdded = kTRUE;
233             }
234             j++;
235           }
236         }
237       }
238     }
239   }
240   
241   Int_t max = 0;
242   label = -123456789;
243   for (Int_t i = 0; i < kMAXCLUSTERSPERTRACK; i++) {
244     if (s[i][1] <= max) continue;
245     max   = s[i][1]; 
246     label = s[i][0];
247   }
248   
249   if ((1. - Float_t(max)/GetNumberOfClusters()) > wrong) label = -label;
250   
251   SetLabel(label); 
252   
253   return kTRUE;
254 }
255
256 //_______________________________________________________________
257 Bool_t AliTRDtrackV1::CookPID()
258 {
259   //
260   // Cook the PID information
261   //
262   
263   // Reset the a priori probabilities
264   Double_t pid = 1. / AliPID::kSPECIES;
265   for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) {
266     fPID[ispec] = pid;  
267   }
268   fPIDquality = 0;
269   
270   // steer PID calculation @ tracklet level
271   Double_t *prob = 0x0;
272   for(int ip=0; ip<kNplane; ip++){
273     if(fTrackletIndex[ip] == 0xffff) continue;
274     if(!fTracklet[ip]->IsOK()) continue;
275     if(!(prob = fTracklet[ip]->GetProbability())) return kFALSE;
276     
277     Int_t nspec = 0; // quality check of tracklet dEdx
278     for(int ispec=0; ispec<AliPID::kSPECIES; ispec++){
279       if(prob[ispec] < 0.) continue;
280       fPID[ispec] *= prob[ispec];
281       nspec++;
282     }
283     if(!nspec) continue;
284     
285     fPIDquality++;
286   }
287   
288   // no tracklet found for PID calculations
289   if(!fPIDquality) return kTRUE;
290   
291   // slot for PID calculation @ track level
292   
293   
294   // normalize probabilities
295   Double_t probTotal = 0.0;
296   for (Int_t is = 0; is < AliPID::kSPECIES; is++) probTotal += fPID[is];
297   
298   
299   if (probTotal <= 0.0) {
300     AliWarning("The total probability over all species <= 0. This may be caused by some error in the reference data.");
301     return kFALSE;
302   }
303   
304   for (Int_t iSpecies = 0; iSpecies < AliPID::kSPECIES; iSpecies++) fPID[iSpecies] /= probTotal;
305   
306   return kTRUE;
307 }
308
309 //_____________________________________________________________________________
310 Double_t AliTRDtrackV1::GetBz() const 
311 {
312   //
313   // Returns Bz component of the magnetic field (kG)
314   //
315
316   if (AliTracker::UniformField()) return AliTracker::GetBz();
317
318   Double_t r[3]; 
319   GetXYZ(r);
320   return AliTracker::GetBz(r);
321 }
322
323 //_______________________________________________________________
324 Int_t  AliTRDtrackV1::GetClusterIndex(Int_t id) const
325 {
326   Int_t n = 0;
327   for(Int_t ip=0; ip<kNplane; ip++){
328     if(!fTracklet[ip]) continue;
329     if(n+fTracklet[ip]->GetN() < id){ 
330       n+=fTracklet[ip]->GetN();
331       continue;
332     }
333     for(Int_t ic=34; ic>=0; ic--){
334       if(!fTracklet[ip]->GetClusters(ic)) continue;
335       n++;
336       if(n<id) continue;
337       return fTracklet[ip]->GetIndexes(ic);
338     }
339   }
340   return -1;
341 }
342
343 //_______________________________________________________________
344 Double_t AliTRDtrackV1::GetPredictedChi2(const AliTRDseedV1 *trklt) const
345 {
346   //
347   // Get the predicted chi2
348   //
349   
350   Double_t x      = trklt->GetX0();
351   Double_t p[2]   = { trklt->GetYat(x)
352                     , trklt->GetZat(x) };
353   Double_t cov[3];
354   trklt->GetCovAt(x, cov);
355   
356   return AliExternalTrackParam::GetPredictedChi2(p, cov);
357 }
358
359         
360 //_____________________________________________________________________________
361 void AliTRDtrackV1::MakeBackupTrack()
362 {
363   //
364   // Creates a backup track
365   //
366
367   if(fBackupTrack) {
368     fBackupTrack->~AliTRDtrackV1();
369     new(fBackupTrack) AliTRDtrackV1((AliTRDtrackV1&)(*this));
370     return;
371   }
372   fBackupTrack = new AliTRDtrackV1((AliTRDtrackV1&)(*this));
373 }
374
375 //_____________________________________________________________________________
376 Int_t AliTRDtrackV1::GetProlongation(Double_t xk, Double_t &y, Double_t &z)
377 {
378   //
379   // Find a prolongation at given x
380   // Return 0 if it does not exist
381   //  
382
383   Double_t bz = GetBz();
384   if (!AliExternalTrackParam::GetYAt(xk,bz,y)) return 0;
385   if (!AliExternalTrackParam::GetZAt(xk,bz,z)) return 0;
386
387   return 1;  
388
389 }
390
391 //_____________________________________________________________________________
392 Bool_t AliTRDtrackV1::PropagateTo(Double_t xk, Double_t xx0, Double_t xrho)
393 {
394   //
395   // Propagates this track to a reference plane defined by "xk" [cm] 
396   // correcting for the mean crossed material.
397   //
398   // "xx0"  - thickness/rad.length [units of the radiation length] 
399   // "xrho" - thickness*density    [g/cm^2] 
400   // 
401
402   if (xk == GetX()) {
403     return kTRUE;
404   }
405
406   Double_t oldX = GetX();
407   Double_t oldY = GetY();
408   Double_t oldZ = GetZ();
409
410   Double_t bz   = GetBz();
411
412   if (!AliExternalTrackParam::PropagateTo(xk,bz)) {
413     return kFALSE;
414   }
415
416   Double_t x = GetX();
417   Double_t y = GetY();
418   Double_t z = GetZ();
419
420   if (oldX < xk) {
421     xrho = -xrho;
422     if (IsStartedTimeIntegral()) {
423       Double_t l2  = TMath::Sqrt((x-oldX)*(x-oldX) 
424                                + (y-oldY)*(y-oldY) 
425                                + (z-oldZ)*(z-oldZ));
426       Double_t crv = AliExternalTrackParam::GetC(bz);
427       if (TMath::Abs(l2*crv) > 0.0001) {
428         // Make correction for curvature if neccesary
429         l2 = 0.5 * TMath::Sqrt((x-oldX)*(x-oldX) 
430                              + (y-oldY)*(y-oldY));
431         l2 = 2.0 * TMath::ASin(l2 * crv) / crv;
432         l2 = TMath::Sqrt(l2*l2 + (z-oldZ)*(z-oldZ));
433       }
434       AddTimeStep(l2);
435     }
436   }
437
438   if (!AliExternalTrackParam::CorrectForMeanMaterial(xx0,xrho,GetMass())) { 
439     return kFALSE;
440   }
441
442   {
443
444     // Energy losses
445     Double_t p2    = (1.0 + GetTgl()*GetTgl()) / (GetSigned1Pt()*GetSigned1Pt());
446     Double_t beta2 = p2 / (p2 + GetMass()*GetMass());
447     if ((beta2 < 1.0e-10) || 
448         ((5940.0 * beta2/(1.0 - beta2 + 1.0e-10) - beta2) < 0.0)) {
449       return kFALSE;
450     }
451
452     Double_t dE    = 0.153e-3 / beta2 
453                    * (TMath::Log(5940.0 * beta2/(1.0 - beta2 + 1.0e-10)) - beta2)
454                    * xrho;
455     fBudget[0] += xrho;
456
457     /*
458     // Suspicious part - think about it ?
459     Double_t kinE =  TMath::Sqrt(p2);
460     if (dE > 0.8*kinE) dE = 0.8 * kinE;  //      
461     if (dE < 0)        dE = 0.0;         // Not valid region for Bethe bloch 
462     */
463  
464     fDE += dE;
465
466     /*
467     // Suspicious ! I.B.
468     Double_t sigmade = 0.07 * TMath::Sqrt(TMath::Abs(dE));   // Energy loss fluctuation 
469     Double_t sigmac2 = sigmade*sigmade*fC*fC*(p2+GetMass()*GetMass())/(p2*p2);
470     fCcc += sigmac2;
471     fCee += fX*fX * sigmac2;  
472     */
473
474   }
475
476   return kTRUE;
477 }
478
479 //_____________________________________________________________________________
480 Int_t   AliTRDtrackV1::PropagateToR(Double_t r,Double_t step)
481 {
482   //
483   // Propagate track to the radial position
484   // Rotation always connected to the last track position
485   //
486
487   Double_t xyz0[3];
488   Double_t xyz1[3];
489   Double_t y;
490   Double_t z; 
491
492   Double_t radius = TMath::Sqrt(GetX()*GetX() + GetY()*GetY());
493   // Direction +-
494   Double_t dir    = (radius > r) ? -1.0 : 1.0;   
495
496   for (Double_t x = radius+dir*step; dir*x < dir*r; x += dir*step) {
497
498     GetXYZ(xyz0);       
499     Double_t alpha = TMath::ATan2(xyz0[1],xyz0[0]);
500     Rotate(alpha,kTRUE);
501     GetXYZ(xyz0);       
502     GetProlongation(x,y,z);
503     xyz1[0] = x * TMath::Cos(alpha) + y * TMath::Sin(alpha); 
504     xyz1[1] = x * TMath::Sin(alpha) - y * TMath::Cos(alpha);
505     xyz1[2] = z;
506     Double_t param[7];
507     AliTracker::MeanMaterialBudget(xyz0,xyz1,param);
508     if (param[1] <= 0) {
509       param[1] = 100000000;
510     }
511     PropagateTo(x,param[1],param[0]*param[4]);
512
513   } 
514
515   GetXYZ(xyz0); 
516   Double_t alpha = TMath::ATan2(xyz0[1],xyz0[0]);
517   Rotate(alpha,kTRUE);
518   GetXYZ(xyz0); 
519   GetProlongation(r,y,z);
520   xyz1[0] = r * TMath::Cos(alpha) + y * TMath::Sin(alpha); 
521   xyz1[1] = r * TMath::Sin(alpha) - y * TMath::Cos(alpha);
522   xyz1[2] = z;
523   Double_t param[7];
524   AliTracker::MeanMaterialBudget(xyz0,xyz1,param);
525
526   if (param[1] <= 0) {
527     param[1] = 100000000;
528   }
529   PropagateTo(r,param[1],param[0]*param[4]);
530
531   return 0;
532
533 }
534
535 //_____________________________________________________________________________
536 Bool_t AliTRDtrackV1::Rotate(Double_t alpha, Bool_t absolute)
537 {
538   //
539   // Rotates track parameters in R*phi plane
540   // if absolute rotation alpha is in global system
541   // otherwise alpha rotation is relative to the current rotation angle
542   //  
543
544   if (absolute) alpha -= GetAlpha();
545   //else fNRotate++;
546
547   return AliExternalTrackParam::Rotate(GetAlpha()+alpha);
548 }
549
550 //___________________________________________________________
551 void AliTRDtrackV1::SetNumberOfClusters() 
552 {
553 // Calculate the number of clusters attached to this track
554         
555   Int_t ncls = 0;
556   for(int ip=0; ip<kNplane; ip++){
557     if(fTracklet[ip] && fTrackletIndex[ip] != 0xffff) ncls += fTracklet[ip]->GetN();
558   }
559   AliKalmanTrack::SetNumberOfClusters(ncls);    
560 }
561
562         
563 //_______________________________________________________________
564 void AliTRDtrackV1::SetOwner()
565 {
566   //
567   // Toggle ownership of tracklets
568   //
569
570   if(TestBit(1)) return;
571   for (Int_t ip = 0; ip < kNplane; ip++) {
572     if(fTrackletIndex[ip] == 0xffff) continue;
573     fTracklet[ip] = new AliTRDseedV1(*fTracklet[ip]);
574     fTracklet[ip]->SetOwner();
575   }
576   SetBit(1);
577 }
578
579 //_______________________________________________________________
580 void AliTRDtrackV1::SetTracklet(AliTRDseedV1 *trklt, Int_t index)
581 {
582   //
583   // Set the tracklets
584   //
585   Int_t plane = trklt->GetPlane();
586   if(fTrackletIndex[plane]==0xffff && fTracklet[plane]){ 
587     delete fTracklet[plane];
588   }
589   fTracklet[plane]      = trklt;
590   fTrackletIndex[plane] = index;
591 }
592
593 //_______________________________________________________________
594 Bool_t  AliTRDtrackV1::Update(AliTRDseedV1 *trklt, Double_t chisq)
595 {
596   //
597   // Update track and tracklet parameters 
598   //
599   
600   Double_t x      = trklt->GetX0();
601   Double_t p[2]   = { trklt->GetYat(x)
602                     , trklt->GetZat(x) };
603   Double_t cov[3];
604   trklt->GetCovAt(x, cov);
605   
606   if(!AliExternalTrackParam::Update(p, cov)) return kFALSE;
607   
608   AliTRDcluster *c = 0x0;
609   Int_t ic = 0; while(!(c = trklt->GetClusters(ic))) ic++;
610   AliTracker::FillResiduals(this, p, cov, c->GetVolumeId());
611   
612   
613   // Register info to track
614   SetNumberOfClusters();
615   SetChi2(GetChi2() + chisq);
616   
617   // update tracklet
618   trklt->SetMomentum(GetP());
619   trklt->SetSnp(GetSnp());
620   trklt->SetTgl(GetTgl());
621   return kTRUE;
622 }
623
624 //_______________________________________________________________
625 void AliTRDtrackV1::UpdateESDtrack(AliESDtrack *track)
626 {
627   //
628   // Update the TRD PID information in the ESD track
629   //
630
631   track->SetNumberOfTRDslices(kNslice);
632
633   for (Int_t ip = 0; ip < kNplane; ip++) {
634     if(fTrackletIndex[ip] == 0xffff) continue;
635     fTracklet[ip]->CookdEdx(kNslice);
636     Float_t *dedx = fTracklet[ip]->GetdEdx();
637     for (Int_t js = 0; js < kNslice; js++) track->SetTRDslice(dedx[js], ip, js);
638   }
639
640   // copy PID to ESD
641   track->SetTRDpid(fPID);
642   track->SetTRDpidQuality(fPIDquality);
643 }