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