]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCtracker.cxx
5cc4601b4e3f64a0731374d6c8bac8a938cd6211
[u/mrichter/AliRoot.git] / TPC / AliTPCtracker.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 /*
17 $Log$
18 Revision 1.5  2000/12/20 07:51:59  kowal2
19 Changes suggested by Alessandra and Paolo to avoid overlapped
20 data fields in encapsulated classes.
21
22 Revision 1.4  2000/11/02 07:27:16  kowal2
23 code corrections
24
25 Revision 1.2  2000/06/30 12:07:50  kowal2
26 Updated from the TPC-PreRelease branch
27
28 Revision 1.1.2.1  2000/06/25 08:53:55  kowal2
29 Splitted from AliTPCtracking
30
31 */
32
33 //-------------------------------------------------------
34 //          Implementation of the TPC tracker
35 //
36 //   Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch 
37 //-------------------------------------------------------
38
39 #include <TObjArray.h>
40 #include <TFile.h>
41 #include <TTree.h>
42 #include <iostream.h>
43
44 #include "AliTPCtracker.h"
45 #include "AliTPCcluster.h"
46 #include "AliTPCClustersArray.h"
47 #include "AliTPCClustersRow.h"
48
49 const AliTPCParam *AliTPCtracker::AliTPCSector::fgParam;
50
51 //_____________________________________________________________________________
52 Double_t SigmaY2(Double_t r, Double_t tgl, Double_t pt)
53 {
54   //
55   // Parametrised error of the cluster reconstruction (pad direction)   
56   //
57   // Sigma rphi
58   const Float_t kArphi=0.41818e-2;
59   const Float_t kBrphi=0.17460e-4;
60   const Float_t kCrphi=0.30993e-2;
61   const Float_t kDrphi=0.41061e-3;
62   
63   pt=TMath::Abs(pt)*1000.;
64   Double_t x=r/pt;
65   tgl=TMath::Abs(tgl);
66   Double_t s=kArphi - kBrphi*r*tgl + kCrphi*x*x + kDrphi*x;
67   if (s<0.4e-3) s=0.4e-3;
68   s*=1.3; //Iouri Belikov
69
70   return s;
71 }
72
73 //_____________________________________________________________________________
74 Double_t SigmaZ2(Double_t r, Double_t tgl) 
75 {
76   //
77   // Parametrised error of the cluster reconstruction (drift direction)
78   //
79   // Sigma z
80   const Float_t kAz=0.39614e-2;
81   const Float_t kBz=0.22443e-4;
82   const Float_t kCz=0.51504e-1;
83   
84
85   tgl=TMath::Abs(tgl);
86   Double_t s=kAz - kBz*r*tgl + kCz*tgl*tgl;
87   if (s<0.4e-3) s=0.4e-3;
88   s*=1.3; //Iouri Belikov
89
90   return s;
91 }
92
93 //_____________________________________________________________________________
94 inline Double_t f1(Double_t x1,Double_t y1,
95                    Double_t x2,Double_t y2,
96                    Double_t x3,Double_t y3) 
97 {
98   //-----------------------------------------------------------------
99   // Initial approximation of the track curvature
100   //-----------------------------------------------------------------
101   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
102   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
103                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
104   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
105                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
106
107   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
108
109   return -xr*yr/sqrt(xr*xr+yr*yr); 
110 }
111
112
113 //_____________________________________________________________________________
114 inline Double_t f2(Double_t x1,Double_t y1,
115                    Double_t x2,Double_t y2,
116                    Double_t x3,Double_t y3) 
117 {
118   //-----------------------------------------------------------------
119   // Initial approximation of the track curvature times center of curvature
120   //-----------------------------------------------------------------
121   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
122   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
123                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
124   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
125                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
126
127   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
128   
129   return -a/(d*y1-b)*xr/sqrt(xr*xr+yr*yr);
130 }
131
132 //_____________________________________________________________________________
133 inline Double_t f3(Double_t x1,Double_t y1, 
134                    Double_t x2,Double_t y2,
135                    Double_t z1,Double_t z2) 
136 {
137   //-----------------------------------------------------------------
138   // Initial approximation of the tangent of the track dip angle
139   //-----------------------------------------------------------------
140   return (z1 - z2)/sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
141 }
142
143 //_____________________________________________________________________________
144 Int_t AliTPCtracker::FindProlongation(AliTPCseed& t, const AliTPCSector *sec,
145 Int_t s, Int_t rf) 
146 {
147   //-----------------------------------------------------------------
148   // This function tries to find a track prolongation.
149   //-----------------------------------------------------------------
150   const Int_t kSKIP=(t.GetNumberOfClusters()<10) ? 10 : 
151                     Int_t(0.5*sec->GetNRows());
152   Int_t tryAgain=kSKIP;
153   Double_t alpha=sec->GetAlpha();
154   Int_t ns=Int_t(2*TMath::Pi()/alpha+0.5);
155
156   for (Int_t nr=sec->GetRowNumber(t.GetX())-1; nr>=rf; nr--) {
157     Double_t x=sec->GetX(nr), ymax=sec->GetMaxY(nr);
158     if (!t.PropagateTo(x)) return 0;
159
160     AliTPCcluster *cl=0;
161     UInt_t index=0;
162     Double_t maxchi2=12.;
163     const AliTPCRow &krow=sec[s][nr];
164     Double_t sy2=SigmaY2(t.GetX(),t.GetTgl(),1./t.Get1Pt());
165     Double_t sz2=SigmaZ2(t.GetX(),t.GetTgl());
166     Double_t road=4.*sqrt(t.GetSigmaY2() + sy2), y=t.GetY(), z=t.GetZ();
167
168     if (road>30) {
169       if (t.GetNumberOfClusters()>4) 
170         cerr<<t.GetNumberOfClusters()
171         <<"FindProlongation warning: Too broad road !\n"; 
172       return 0;
173     }
174
175     if (krow) {
176       for (Int_t i=krow.Find(y-road); i<krow; i++) {
177         AliTPCcluster *c=(AliTPCcluster*)(krow[i]);
178         if (c->GetY() > y+road) break;
179         if (c->IsUsed()) continue;
180         if ((c->GetZ()-z)*(c->GetZ()-z) > 16.*(t.GetSigmaZ2()+sz2)) continue;
181         Double_t chi2=t.GetPredictedChi2(c);
182         if (chi2 > maxchi2) continue;
183         maxchi2=chi2;
184         cl=c;
185         index=krow.GetIndex(i);       
186       }
187     }
188     if (cl) {
189       Float_t l=sec->GetPadPitchWidth();
190       t.SetSampledEdx(cl->GetQ()/l,t.GetNumberOfClusters());
191       if (!t.Update(cl,maxchi2,index)) {
192          if (!tryAgain--) return 0;
193       } else tryAgain=kSKIP;
194     } else {
195       if (tryAgain==0) break;
196       if (y > ymax) {
197          s = (s+1) % ns;
198          if (!t.Rotate(alpha)) return 0;
199       } else if (y <-ymax) {
200          s = (s-1+ns) % ns;
201          if (!t.Rotate(-alpha)) return 0;
202       }
203       tryAgain--;
204     }
205   }
206
207   return 1;
208
209 }
210
211 //_____________________________________________________________________________
212 void 
213 AliTPCtracker::MakeSeeds(TObjArray& seeds,const AliTPCSector *sec, Int_t max,
214 Int_t i1, Int_t i2)
215 {
216   //-----------------------------------------------------------------
217   // This function creates track seeds.
218   //-----------------------------------------------------------------
219   Double_t x[5], c[15];
220
221   Double_t alpha=sec->GetAlpha(), shift=sec->GetAlphaShift();
222   Double_t cs=cos(alpha), sn=sin(alpha);
223
224   Double_t x1 =sec->GetX(i1);
225   Double_t xx2=sec->GetX(i2);
226
227   for (Int_t ns=0; ns<max; ns++) {
228     Int_t nl=sec[(ns-1+max)%max][i2];
229     Int_t nm=sec[ns][i2];
230     Int_t nu=sec[(ns+1)%max][i2];
231     const AliTPCRow& kr1=sec[ns][i1];
232     for (Int_t is=0; is < kr1; is++) {
233       Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();
234       for (Int_t js=0; js < nl+nm+nu; js++) {
235         const AliTPCcluster *kcl;
236         Double_t x2,   y2,   z2;
237         Double_t x3=0.,y3=0.;
238
239         if (js<nl) {
240           const AliTPCRow& kr2=sec[(ns-1+max)%max][i2];
241           kcl=kr2[js];
242           y2=kcl->GetY(); z2=kcl->GetZ();
243           x2= xx2*cs+y2*sn;
244           y2=-xx2*sn+y2*cs;
245         } else 
246           if (js<nl+nm) {
247             const AliTPCRow& kr2=sec[ns][i2];
248             kcl=kr2[js-nl];
249             x2=xx2; y2=kcl->GetY(); z2=kcl->GetZ();
250           } else {
251             const AliTPCRow& kr2=sec[(ns+1)%max][i2];
252             kcl=kr2[js-nl-nm];
253             y2=kcl->GetY(); z2=kcl->GetZ();
254             x2=xx2*cs-y2*sn;
255             y2=xx2*sn+y2*cs;
256           }
257
258         Double_t zz=z1 - z1/x1*(x1-x2); 
259         if (TMath::Abs(zz-z2)>5.) continue;
260
261         Double_t d=(x2-x1)*(0.-y2)-(0.-x2)*(y2-y1);
262         if (d==0.) {cerr<<"MakeSeeds warning: Straight seed !\n"; continue;}
263
264         x[0]=y1;
265         x[1]=z1;
266         x[3]=f1(x1,y1,x2,y2,x3,y3);
267         if (TMath::Abs(x[3]) >= 0.0066) continue;
268         x[2]=f2(x1,y1,x2,y2,x3,y3);
269         //if (TMath::Abs(x[3]*x1-x[2]) >= 0.99999) continue;
270         x[4]=f3(x1,y1,x2,y2,z1,z2);
271         if (TMath::Abs(x[4]) > 1.2) continue;
272         Double_t a=asin(x[2]);
273         Double_t zv=z1 - x[4]/x[3]*(a+asin(x[3]*x1-x[2]));
274         if (TMath::Abs(zv)>10.) continue; 
275
276         Double_t sy1=kr1[is]->GetSigmaY2(), sz1=kr1[is]->GetSigmaZ2();
277         Double_t sy2=kcl->GetSigmaY2(),     sz2=kcl->GetSigmaZ2();
278         Double_t sy3=100*0.025, sy=0.1, sz=0.1;
279
280         Double_t f30=(f1(x1,y1+sy,x2,y2,x3,y3)-x[3])/sy;
281         Double_t f32=(f1(x1,y1,x2,y2+sy,x3,y3)-x[3])/sy;
282         Double_t f34=(f1(x1,y1,x2,y2,x3,y3+sy)-x[3])/sy;
283         Double_t f20=(f2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
284         Double_t f22=(f2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
285         Double_t f24=(f2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
286         Double_t f40=(f3(x1,y1+sy,x2,y2,z1,z2)-x[4])/sy;
287         Double_t f41=(f3(x1,y1,x2,y2,z1+sz,z2)-x[4])/sz;
288         Double_t f42=(f3(x1,y1,x2,y2+sy,z1,z2)-x[4])/sy;
289         Double_t f43=(f3(x1,y1,x2,y2,z1,z2+sz)-x[4])/sz;
290
291         c[0]=sy1;
292         c[1]=0.;       c[2]=sz1;
293         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f24*sy3*f24;
294         c[6]=f30*sy1;  c[7]=0.;       c[8]=f30*sy1*f20+f32*sy2*f22+f34*sy3*f24;
295                                       c[9]=f30*sy1*f30+f32*sy2*f32+f34*sy3*f34;
296         c[10]=f40*sy1; c[11]=f41*sz1; c[12]=f40*sy1*f20+f42*sy2*f22;
297         c[13]=f40*sy1*f30+f42*sy2*f32;
298         c[14]=f40*sy1*f40+f41*sz1*f41+f42*sy2*f42+f43*sz2*f43;
299
300         UInt_t index=kr1.GetIndex(is);
301         AliTPCseed *track=new AliTPCseed(index, x, c, x1, ns*alpha+shift);
302         Float_t l=sec->GetPadPitchWidth();
303         track->SetSampledEdx(kr1[is]->GetQ()/l,0);
304
305         Int_t rc=FindProlongation(*track,sec,ns,i2);
306         if (rc==0 || track->GetNumberOfClusters()<(i1-i2)/2) delete track;
307         else seeds.AddLast(track); 
308       }
309     }
310   }
311 }
312
313 //_____________________________________________________________________________
314 Int_t AliTPCtracker::Clusters2Tracks(const AliTPCParam *par, TFile *of) {
315   //-----------------------------------------------------------------
316   // This is a track finder.
317   //-----------------------------------------------------------------
318   TDirectory *savedir=gDirectory; 
319
320   if (!of->IsOpen()) {
321      cerr<<"AliTPCtracker::Clusters2Tracks(): output file not open !\n";
322      return 1;
323   }
324
325   AliTPCClustersArray carray;
326   carray.Setup(par);
327   carray.SetClusterType("AliTPCcluster");
328   carray.ConnectTree("Segment Tree");
329
330   of->cd();
331   TTree tracktree("TreeT","Tree with TPC tracks");
332   AliTPCtrack *iotrack=0;
333   tracktree.Branch("tracks","AliTPCtrack",&iotrack,32000,0);
334
335   AliTPCSector::SetParam(par);
336
337   const Int_t kNIS=par->GetNInnerSector()/2;
338   AliTPCSSector *ssec=new AliTPCSSector[kNIS];         
339   Int_t nlow=ssec->GetNRows();     
340
341   const Int_t kNOS=par->GetNOuterSector()/2;
342   AliTPCLSector *lsec=new AliTPCLSector[kNOS];
343   Int_t nup=lsec->GetNRows();
344     
345   //Load outer sectors
346   UInt_t index;
347   Int_t i,j;
348
349   j=Int_t(carray.GetTree()->GetEntries());
350   for (i=0; i<j; i++) {
351       AliSegmentID *s=carray.LoadEntry(i);
352       Int_t sec,row;
353       par->AdjustSectorRow(s->GetID(),sec,row);
354       if (sec<kNIS*2) continue;
355       AliTPCClustersRow *clrow=carray.GetRow(sec,row);
356       Int_t ncl=clrow->GetArray()->GetEntriesFast();
357       while (ncl--) {
358          AliTPCcluster *c=(AliTPCcluster*)(*clrow)[ncl];
359          index=(((sec<<8)+row)<<16)+ncl;
360          lsec[(sec-kNIS*2)%kNOS][row].InsertCluster(c,index);
361       }
362   }
363
364   //find track seeds
365   TObjArray seeds(20000);
366   Int_t nrows=nlow+nup;
367   Int_t gap=Int_t(0.125*nrows), shift=Int_t(0.5*gap);
368   MakeSeeds(seeds, lsec, kNOS, nup-1, nup-1-gap);
369   MakeSeeds(seeds, lsec, kNOS, nup-1-shift, nup-1-shift-gap);    
370   seeds.Sort();
371
372   //tracking in outer sectors
373   Int_t nseed=seeds.GetEntriesFast();
374   for (i=0; i<nseed; i++) {
375     AliTPCseed *pt=(AliTPCseed*)seeds.UncheckedAt(i), &t=*pt;
376     Double_t alpha=t.GetAlpha();
377     if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
378     if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
379     Int_t ns=Int_t(alpha/lsec->GetAlpha())%kNOS;
380
381     if (FindProlongation(t,lsec,ns)) {
382        t.UseClusters(&carray);
383        continue;
384     }
385     delete seeds.RemoveAt(i);
386   }  
387
388   //unload outer sectors
389   for (i=0; i<kNOS; i++) {
390       for (j=0; j<nup; j++) {
391           if (carray.GetRow(i+kNIS*2,j)) carray.ClearRow(i+kNIS*2,j);
392           if (carray.GetRow(i+kNIS*2+kNOS,j)) carray.ClearRow(i+kNIS*2+kNOS,j);
393       }
394   }
395
396   //load inner sectors
397   j=Int_t(carray.GetTree()->GetEntries());
398   for (i=0; i<j; i++) {
399       AliSegmentID *s=carray.LoadEntry(i);
400       Int_t sec,row;
401       par->AdjustSectorRow(s->GetID(),sec,row);
402       if (sec>=kNIS*2) continue;
403       AliTPCClustersRow *clrow=carray.GetRow(sec,row);
404       Int_t ncl=clrow->GetArray()->GetEntriesFast();
405       while (ncl--) {
406          AliTPCcluster *c=(AliTPCcluster*)(*clrow)[ncl];
407          index=(((sec<<8)+row)<<16)+ncl;
408          ssec[sec%kNIS][row].InsertCluster(c,index);
409       }
410   }
411
412   //tracking in inner sectors
413   Int_t found=0;
414   for (i=0; i<nseed; i++) {
415     AliTPCseed *pt=(AliTPCseed*)seeds.UncheckedAt(i), &t=*pt;
416     if (!pt) continue;
417     Int_t nc=t.GetNumberOfClusters();
418
419     Double_t alpha=t.GetAlpha() - ssec->GetAlphaShift();
420     if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
421     if (alpha < 0.            ) alpha += 2.*TMath::Pi();
422     Int_t ns=Int_t(alpha/ssec->GetAlpha())%kNIS;
423
424     alpha=ns*ssec->GetAlpha() + ssec->GetAlphaShift() - t.GetAlpha();
425
426     if (t.Rotate(alpha)) {
427        if (FindProlongation(t,ssec,ns)) {
428           if (t.GetNumberOfClusters() >= Int_t(0.4*nrows)) {
429              t.CookdEdx();
430              //t.CookLabel(&carray);
431              iotrack=pt;
432              tracktree.Fill();
433              t.UseClusters(&carray,nc);
434              cerr<<found++<<'\r';
435           }
436        }
437     }
438     delete seeds.RemoveAt(i); 
439   }  
440   tracktree.Write();
441
442   //unload inner sectors
443   for (i=0; i<kNIS; i++) {
444       for (j=0; j<nlow; j++) {
445           if (carray.GetRow(i,j)) carray.ClearRow(i,j);
446           if (carray.GetRow(i+kNIS,j)) carray.ClearRow(i+kNIS,j);
447       }
448   }
449
450   cerr<<"Number of found tracks : "<<found<<endl;
451
452   delete[] ssec;
453   delete[] lsec;
454
455   savedir->cd();
456
457   return 0;
458 }
459
460 //_________________________________________________________________________
461 void 
462 AliTPCtracker::AliTPCRow::InsertCluster(const AliTPCcluster* c, UInt_t index) {
463   //-----------------------------------------------------------------------
464   // Insert a cluster into this pad row in accordence with its y-coordinate
465   //-----------------------------------------------------------------------
466   if (fN==kMAXCLUSTER) {
467     cerr<<"AliTPCRow::InsertCluster(): Too many clusters !\n"; return;
468   }
469   if (fN==0) {fIndex[0]=index; fClusters[fN++]=c; return;}
470   Int_t i=Find(c->GetY());
471   memmove(fClusters+i+1 ,fClusters+i,(fN-i)*sizeof(AliTPCcluster*));
472   memmove(fIndex   +i+1 ,fIndex   +i,(fN-i)*sizeof(UInt_t));
473   fIndex[i]=index; fClusters[i]=c; fN++;
474 }
475
476 //___________________________________________________________________
477 Int_t AliTPCtracker::AliTPCRow::Find(Double_t y) const {
478   //-----------------------------------------------------------------------
479   // Return the index of the nearest cluster 
480   //-----------------------------------------------------------------------
481   if (y <= fClusters[0]->GetY()) return 0;
482   if (y > fClusters[fN-1]->GetY()) return fN;
483   Int_t b=0, e=fN-1, m=(b+e)/2;
484   for (; b<e; m=(b+e)/2) {
485     if (y > fClusters[m]->GetY()) b=m+1;
486     else e=m; 
487   }
488   return m;
489 }
490
491 //_____________________________________________________________________________
492 void AliTPCtracker::AliTPCseed::CookdEdx(Double_t low, Double_t up) {
493   //-----------------------------------------------------------------
494   // This funtion calculates dE/dX within the "low" and "up" cuts.
495   //-----------------------------------------------------------------
496   Int_t i;
497   Int_t nc=GetNumberOfClusters();
498
499   Int_t swap;//stupid sorting
500   do {
501     swap=0;
502     for (i=0; i<nc-1; i++) {
503       if (fdEdxSample[i]<=fdEdxSample[i+1]) continue;
504       Float_t tmp=fdEdxSample[i];
505       fdEdxSample[i]=fdEdxSample[i+1]; fdEdxSample[i+1]=tmp;
506       swap++;
507     }
508   } while (swap);
509
510   Int_t nl=Int_t(low*nc), nu=Int_t(up*nc);
511   Float_t dedx=0;
512   for (i=nl; i<=nu; i++) dedx += fdEdxSample[i];
513   dedx /= (nu-nl+1);
514   SetdEdx(dedx);
515 }
516
517 //_____________________________________________________________________________
518 void AliTPCtracker::AliTPCseed::UseClusters(AliTPCClustersArray *ca, Int_t n) {
519   //-----------------------------------------------------------------
520   // This function marks clusters associated with this track.
521   //-----------------------------------------------------------------
522   Int_t nc=GetNumberOfClusters();
523   Int_t sec,row,ncl;
524
525   for (Int_t i=n; i<nc; i++) {
526      GetCluster(i,sec,row,ncl);
527      AliTPCClustersRow *clrow=ca->GetRow(sec,row);
528      AliTPCcluster *c=(AliTPCcluster*)(*clrow)[ncl]; 
529      c->Use();   
530   }
531 }
532
533