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