]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDtracker.cxx
963de0103af71c34d03648ac256bff2f6560bfa1
[u/mrichter/AliRoot.git] / TRD / AliTRDtracker.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.12  2001/05/28 17:07:58  hristov
19 Last minute changes; ExB correction in AliTRDclusterizerV1; taking into account of material in G10 TEC frames and material between TEC planes (C.Blume,S.Sedykh)
20
21 Revision 1.8  2000/12/20 13:00:44  cblume
22 Modifications for the HP-compiler
23
24 Revision 1.7  2000/12/08 16:07:02  cblume
25 Update of the tracking by Sergei
26
27 Revision 1.6  2000/11/30 17:38:08  cblume
28 Changes to get in line with new STEER and EVGEN
29
30 Revision 1.5  2000/11/14 14:40:27  cblume
31 Correction for the Sun compiler (kTRUE and kFALSE)
32
33 Revision 1.4  2000/11/10 14:57:52  cblume
34 Changes in the geometry constants for the DEC compiler
35
36 Revision 1.3  2000/10/15 23:40:01  cblume
37 Remove AliTRDconst
38
39 Revision 1.2  2000/10/06 16:49:46  cblume
40 Made Getters const
41
42 Revision 1.1.2.2  2000/10/04 16:34:58  cblume
43 Replace include files by forward declarations
44
45 Revision 1.1.2.1  2000/09/22 14:47:52  cblume
46 Add the tracking code
47
48 */   
49
50 #include <iostream.h>
51
52 #include <TFile.h>
53 #include <TROOT.h>
54 #include <TBranch.h>
55 #include <TTree.h>
56
57 #include "AliRun.h"
58 #include "AliTRD.h"
59 #include "AliTRDgeometry.h"
60 #include "AliTRDrecPoint.h" 
61 #include "AliTRDcluster.h" 
62 #include "AliTRDtrack.h"
63 #include "AliTRDtrackingSector.h"
64 #include "AliTRDtimeBin.h"
65
66 #include "AliTRDtracker.h"
67
68 ClassImp(AliTRDtracker) 
69
70   const  Float_t     AliTRDtracker::fSeedDepth          = 0.5; 
71   const  Float_t     AliTRDtracker::fSeedStep           = 0.05;   
72   const  Float_t     AliTRDtracker::fSeedGap            = 0.25;  
73
74   const  Float_t     AliTRDtracker::fMaxSeedDeltaZ12    = 40.;  
75   const  Float_t     AliTRDtracker::fMaxSeedDeltaZ      = 25.;  
76   const  Float_t     AliTRDtracker::fMaxSeedC           = 0.0052; 
77   const  Float_t     AliTRDtracker::fMaxSeedTan         = 1.2;  
78   const  Float_t     AliTRDtracker::fMaxSeedVertexZ     = 150.; 
79
80   const  Double_t    AliTRDtracker::fSeedErrorSY        = 0.2;
81   const  Double_t    AliTRDtracker::fSeedErrorSY3       = 2.5;
82   const  Double_t    AliTRDtracker::fSeedErrorSZ        = 0.1;
83
84   const  Float_t     AliTRDtracker::fMinClustersInSeed  = 0.7;  
85
86   const  Float_t     AliTRDtracker::fMinClustersInTrack = 0.5;  
87   const  Float_t     AliTRDtracker::fSkipDepth          = 0.05;
88   const  Float_t     AliTRDtracker::fLabelFraction      = 0.5;  
89   const  Float_t     AliTRDtracker::fWideRoad           = 20.;
90
91   const  Double_t    AliTRDtracker::fMaxChi2            = 24.; 
92
93 //____________________________________________________________________
94 AliTRDtracker::AliTRDtracker()
95 {
96   //
97   // Default constructor
98   //   
99
100   fEvent     = 0;
101   fGeom      = NULL;
102
103   fNclusters = 0;
104   fClusters  = NULL; 
105   fNseeds    = 0;
106   fSeeds     = NULL;
107   fNtracks   = 0;
108   fTracks    = NULL;
109
110   fSY2corr = 0.025;
111 }   
112
113 //____________________________________________________________________
114 AliTRDtracker::AliTRDtracker(const Text_t* name, const Text_t* title)
115                   :TNamed(name, title)
116 {
117   fEvent     = 0;
118   fGeom      = NULL;
119
120   fNclusters = 0;
121   fClusters  = new TObjArray(2000); 
122   fNseeds    = 0;
123   fSeeds     = new TObjArray(20000);
124   fNtracks   = 0;
125   fTracks    = new TObjArray(10000);
126
127   fSY2corr = 0.025;
128 }   
129
130 //___________________________________________________________________
131 AliTRDtracker::~AliTRDtracker()
132 {
133   delete fClusters;
134   delete fTracks;
135   delete fSeeds;
136   delete fGeom;
137 }   
138
139 //___________________________________________________________________
140 void AliTRDtracker::Clusters2Tracks(TH1F *hs, TH1F *hd)
141 {
142   Int_t i;
143
144   Int_t inner, outer;
145   Int_t fTotalNofTimeBins = fGeom->GetTimeMax() * AliTRDgeometry::Nplan();
146   Int_t nSteps = (Int_t) (fSeedDepth / fSeedStep);
147   Int_t gap = (Int_t) (fTotalNofTimeBins * fSeedGap);
148   Int_t step = (Int_t) (fTotalNofTimeBins * fSeedStep);
149
150
151   //  nSteps = 1;
152
153   if (!fClusters) return; 
154
155   AliTRDtrackingSector fTrSec[AliTRDgeometry::kNsect];
156   SetUpSectors(fTrSec);
157
158   // make a first turn looking for seed ends in the same (n,n) 
159   // and in the adjacent sectors (n,n+1)
160
161   for(i=0; i<nSteps; i++) {
162     printf("step %d out of %d \n", i+1, nSteps);
163     outer=fTotalNofTimeBins-1-i*step; inner=outer-gap;
164     MakeSeeds(inner,outer, fTrSec, 1, hs, hd);
165     FindTracks(fTrSec, hs, hd);
166   } 
167
168   // make a second turn looking for seed ends in next-to-adjacent 
169   // sectors (n,n+2)
170
171   for(i=0; i<nSteps; i++) {
172     printf("step %d out of %d \n", i+1, nSteps);
173     outer=fTotalNofTimeBins-1-i*step; inner=outer-gap;
174     MakeSeeds(inner, outer, fTrSec, 2, hs, hd);
175     FindTracks(fTrSec,hs,hd);
176   } 
177
178 }          
179
180 //_____________________________________________________________________
181 Double_t AliTRDtracker::ExpectedSigmaY2(Double_t r, Double_t tgl, Double_t pt)
182 {
183   // Parametrised "expected" error of the cluster reconstruction in Y 
184
185   Double_t s = 0.08 * 0.08;    
186   return s;
187 }
188
189 //_____________________________________________________________________
190 Double_t AliTRDtracker::ExpectedSigmaZ2(Double_t r, Double_t tgl)
191 {
192   // Parametrised "expected" error of the cluster reconstruction in Z 
193
194   Double_t s = 6 * 6 /12.;  
195   return s;
196 }                  
197
198 //_____________________________________________________________________
199 Double_t f1trd(Double_t x1,Double_t y1,
200                Double_t x2,Double_t y2,
201                Double_t x3,Double_t y3)
202 {
203   // Initial approximation of the track curvature
204   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
205
206   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
207   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
208                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
209   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
210                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
211
212   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
213
214   return -xr*yr/sqrt(xr*xr+yr*yr);
215 }          
216
217 //_____________________________________________________________________
218 Double_t f2trd(Double_t x1,Double_t y1,
219                Double_t x2,Double_t y2,
220                Double_t x3,Double_t y3)
221 {
222   // Initial approximation of the track curvature times center of curvature
223   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
224
225   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
226   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
227                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
228   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
229                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
230
231   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
232
233   return -a/(d*y1-b)*xr/sqrt(xr*xr+yr*yr);
234 }          
235
236 //_____________________________________________________________________
237 Double_t f3trd(Double_t x1,Double_t y1,
238                Double_t x2,Double_t y2,
239                Double_t z1,Double_t z2)
240 {
241   // Initial approximation of the tangent of the track dip angle
242   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
243
244   return (z1 - z2)/sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
245 }            
246
247
248 //___________________________________________________________________
249
250 Int_t AliTRDtracker::FindProlongation(AliTRDtrack& t, AliTRDtrackingSector *sec,
251                             Int_t s, Int_t rf, Int_t matched_index, 
252                                       TH1F *hs, TH1F *hd)
253 {
254   // Starting from current position on track=t this function tries 
255   // to extrapolate the track up to timeBin=rf and to confirm prolongation
256   // if a close cluster is found. *sec is a pointer to allocated
257   // array of sectors, in which the initial sector has index=s. 
258
259
260   //  TH1F *hsame = hs;     
261   //  TH1F *hdiff = hd;   
262
263   //  Bool_t good_match;
264
265   const Int_t TIME_BINS_TO_SKIP=Int_t(fSkipDepth*sec->GetNtimeBins());
266   Int_t try_again=TIME_BINS_TO_SKIP;
267
268   Double_t alpha=AliTRDgeometry::GetAlpha();
269
270   Int_t ns=Int_t(2*TMath::Pi()/alpha+0.5);
271
272   Double_t x0, rho;
273
274   for (Int_t nr=sec->GetTimeBinNumber(t.GetX())-1; nr>=rf; nr--) {
275
276     Double_t x=sec->GetX(nr);
277     Double_t ymax=x*TMath::Tan(0.5*alpha);
278
279     rho = 0.00295; x0 = 11.09;  // TEC
280     if(sec->TECframe(nr,t.GetY(),t.GetZ())) { 
281       rho = 1.7; x0 = 33.0;     // G10 frame 
282     } 
283     if(TMath::Abs(x - t.GetX()) > 3) { 
284       rho = 0.0559; x0 = 55.6;  // radiator
285     }
286     if (!t.PropagateTo(x,x0,rho,0.139)) {
287       cerr<<"Can't propagate to x = "<<x<<endl;
288       return 0;
289     }
290
291     AliTRDtimeBin& time_bin=sec[s][nr];
292     Double_t sy2=ExpectedSigmaY2(t.GetX(),t.GetTgl(),t.GetPt());
293     Double_t sz2=ExpectedSigmaZ2(t.GetX(),t.GetTgl());
294     Double_t road=25.*sqrt(t.GetSigmaY2() + sy2), y=t.GetY(), z=t.GetZ();
295
296     if (road>fWideRoad) {
297       if (t.GetNclusters() > 4) {
298         cerr<<t.GetNclusters()<<" FindProlongation: Road is too wide !\n";
299       }
300       return 0;
301     }       
302
303     AliTRDcluster *cl=0;
304     UInt_t index=0;
305     //    Int_t ncl = 0;
306
307     Double_t max_chi2=fMaxChi2;
308
309     if (time_bin) {
310
311       for (Int_t i=time_bin.Find(y-road); i<time_bin; i++) {
312         AliTRDcluster* c=(AliTRDcluster*)(time_bin[i]);
313
314         //      good_match = kFALSE;
315         //      if((c->GetTrackIndex(0) == matched_index) ||
316         //   (c->GetTrackIndex(1) == matched_index) ||
317         //   (c->GetTrackIndex(2) == matched_index)) good_match = kTRUE;
318         //        if(hsame) hsame->Fill(TMath::Abs(c->GetY()-y)/road);
319         //        if(hdiff) hdiff->Fill(road);
320
321         if (c->GetY() > y+road) break;
322         if (c->IsUsed() > 0) continue;
323
324         //      if(good_match) hsame->Fill(TMath::Abs(c->GetZ()-z));
325         //      else hdiff->Fill(TMath::Abs(c->GetZ()-z));
326
327         //      if(!good_match) continue;
328
329         if((c->GetZ()-z)*(c->GetZ()-z) > 3 * 12 * sz2) continue;
330
331         Double_t chi2=t.GetPredictedChi2(c);
332
333         //      if((c->GetTrackIndex(0) == matched_index) ||
334         //         (c->GetTrackIndex(1) == matched_index) ||
335         //         (c->GetTrackIndex(2) == matched_index))
336         //        hdiff->Fill(chi2);
337
338         //      ncl++;
339
340         if (chi2 > max_chi2) continue;
341         max_chi2=chi2;
342         cl=c;
343         index=time_bin.GetIndex(i);
344       }   
345       
346       if(!cl) {
347
348         for (Int_t i=time_bin.Find(y-road); i<time_bin; i++) {
349           AliTRDcluster* c=(AliTRDcluster*)(time_bin[i]);
350
351           if (c->GetY() > y+road) break;
352           if (c->IsUsed() > 0) continue;          
353           if((c->GetZ()-z)*(c->GetZ()-z) > 3.25 * 12 * sz2) continue;
354           
355           Double_t chi2=t.GetPredictedChi2(c);
356           
357           //      ncl++;
358
359           if (chi2 > max_chi2) continue;
360           max_chi2=chi2;
361           cl=c;
362           index=time_bin.GetIndex(i);
363         }   
364       }
365       
366     }
367
368     if (cl) {
369
370       t.Update(cl,max_chi2,index);
371
372       try_again=TIME_BINS_TO_SKIP;
373     } else {
374       if (try_again==0) break;
375       if (y > ymax) {
376         //      cerr<<"y > ymax: "<<y<<" > "<<ymax<<endl;
377          s = (s+1) % ns;
378          if (!t.Rotate(alpha)) {
379            cerr<<"Failed to rotate, alpha = "<<alpha<<endl;
380            return 0;
381          }
382       } else if (y <-ymax) {
383         //      cerr<<"y < -ymax: "<<y<<" < "<<-ymax<<endl;
384          s = (s-1+ns) % ns;
385          if (!t.Rotate(-alpha)) {
386            cerr<<"Failed to rotate, alpha = "<<alpha<<endl;
387            return 0;
388          }
389       }
390       if(!sec->TECframe(nr,y,z)) try_again--;
391     }
392   }
393
394   return 1;
395 }          
396
397
398
399 //_____________________________________________________________________________
400 void AliTRDtracker::GetEvent(const Char_t *hitfile, const Char_t *clusterfile)
401 {
402   // Opens a ROOT-file with TRD-clusters and reads the cluster-tree in
403
404   ReadClusters(fClusters, clusterfile);
405
406   // get geometry from the file with hits
407
408   TFile *fInputFile = (TFile*) gROOT->GetListOfFiles()->FindObject(hitfile);
409   if (!fInputFile) {
410     printf("AliTRDtracker::Open -- ");
411     printf("Open the ALIROOT-file %s.\n",hitfile);
412     fInputFile = new TFile(hitfile,"UPDATE");
413   }
414   else {
415     printf("AliTRDtracker::Open -- ");
416     printf("%s is already open.\n",hitfile);
417   }
418
419   // Get AliRun object from file or create it if not on file
420
421   gAlice = (AliRun*) fInputFile->Get("gAlice");
422   if (gAlice) {
423     printf("AliTRDtracker::GetEvent -- ");
424     printf("AliRun object found on file.\n");
425   }
426   else {
427     printf("AliTRDtracker::GetEvent -- ");
428     printf("Could not find AliRun object.\n");
429   }
430
431   /*  
432   // Import the Trees for the event nEvent in the file
433   Int_t nparticles = gAlice->GetEvent(fEvent);
434   cerr<<"nparticles = "<<nparticles<<endl;
435
436   if (nparticles <= 0) {
437     printf("AliTRDtracker::GetEvent -- ");
438     printf("No entries in the trees for event %d.\n",fEvent);
439   }
440   */  
441
442   AliTRD *TRD = (AliTRD*) gAlice->GetDetector("TRD");
443   fGeom = TRD->GetGeometry();
444
445 }     
446
447
448 //_____________________________________________________________________________
449 void AliTRDtracker::SetUpSectors(AliTRDtrackingSector *sec)
450 {
451   // Fills clusters into TRD tracking_sectors 
452   // Note that the numbering scheme for the TRD tracking_sectors 
453   // differs from that of TRD sectors
454
455   for (Int_t i=0; i<AliTRDgeometry::Nsect(); i++) sec[i].SetUp();
456
457   //  Sort clusters into AliTRDtimeBin's within AliTRDtrackSector's 
458
459   cerr<<"SetUpSectors: sorting clusters"<<endl;
460               
461   Int_t ncl=fClusters->GetEntriesFast();
462   UInt_t index;
463   while (ncl--) {
464     printf("\r %d left  ",ncl); 
465     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(ncl);
466     Int_t detector=c->GetDetector(), local_time_bin=c->GetLocalTimeBin();
467     Int_t sector=fGeom->GetSector(detector);
468
469     Int_t tracking_sector = AliTRDgeometry::kNsect - sector - 1;
470
471     Int_t tb=sec[sector].GetTimeBin(detector,local_time_bin); 
472     index=ncl;
473     sec[tracking_sector][tb].InsertCluster(c,index);
474
475   }    
476   printf("\r\n");
477 }
478
479
480 //_____________________________________________________________________________
481 void AliTRDtracker::MakeSeeds(Int_t inner, Int_t outer, 
482                               AliTRDtrackingSector* fTrSec, Int_t turn,
483                               TH1F *hs, TH1F *hd)
484 {
485   // Creates track seeds using clusters in timeBins=i1,i2
486
487   Int_t i2 = inner, i1 = outer; 
488   Int_t ti[3], to[3];
489   Int_t nprim = 85600/2;
490
491
492   TH1F *hsame = hs;
493   TH1F *hdiff = hd;   
494   Bool_t match = false;
495   Int_t matched_index;
496
497   // find seeds
498
499   Double_t x[5], c[15];
500   Int_t max_sec=AliTRDgeometry::kNsect;
501
502   Double_t alpha=AliTRDgeometry::GetAlpha();
503   Double_t shift=AliTRDgeometry::GetAlpha()/2.;
504   Double_t cs=cos(alpha), sn=sin(alpha);  
505   Double_t cs2=cos(2.*alpha), sn2=sin(2.*alpha);  
506
507   Double_t x1 =fTrSec[0].GetX(i1);
508   Double_t xx2=fTrSec[0].GetX(i2);  
509
510
511   printf("\n");
512
513   if((turn != 1)&&(turn != 2)) {
514     printf("*** Error in MakeSeeds: unexpected turn = %d  \n", turn);
515     return;
516   }
517
518
519   for (Int_t ns=0; ns<max_sec; ns++) {
520
521     printf("\n MakeSeeds: sector %d \n", ns); 
522
523     Int_t nl2=fTrSec[(ns-2+max_sec)%max_sec][i2]; 
524     Int_t nl=fTrSec[(ns-1+max_sec)%max_sec][i2];
525     Int_t nm=fTrSec[ns][i2];
526     Int_t nu=fTrSec[(ns+1)%max_sec][i2];
527     Int_t nu2=fTrSec[(ns+2)%max_sec][i2]; 
528
529     AliTRDtimeBin& r1=fTrSec[ns][i1];
530
531     for (Int_t is=0; is < r1; is++) {
532       Double_t y1=r1[is]->GetY(), z1=r1[is]->GetZ();
533       for(Int_t ii=0; ii<3; ii++) to[ii] = r1[is]->GetTrackIndex(ii); 
534
535       for (Int_t js=0; js < nl2+nl+nm+nu+nu2; js++) {
536           
537         const AliTRDcluster *cl;
538         Double_t x2,   y2,   z2;
539         Double_t x3=0., y3=0.;  
540
541         if (js<nl2) {
542           if(turn != 2) continue;
543           AliTRDtimeBin& r2=fTrSec[(ns-2+max_sec)%max_sec][i2];
544           cl=r2[js];
545           y2=cl->GetY(); z2=cl->GetZ();
546           for(Int_t ii=0; ii<3; ii++) ti[ii] = cl->GetTrackIndex(ii);
547
548           x2= xx2*cs2+y2*sn2;
549           y2=-xx2*sn2+y2*cs2;
550         }        
551         else if (js<nl2+nl) {
552           if(turn != 1) continue;
553           AliTRDtimeBin& r2=fTrSec[(ns-1+max_sec)%max_sec][i2];
554           cl=r2[js-nl2];
555           y2=cl->GetY(); z2=cl->GetZ();
556           for(Int_t ii=0; ii<3; ii++) ti[ii] = cl->GetTrackIndex(ii);
557
558           x2= xx2*cs+y2*sn;
559           y2=-xx2*sn+y2*cs;
560
561         }
562         else if (js<nl2+nl+nm) {
563           if(turn != 1) continue;
564           AliTRDtimeBin& r2=fTrSec[ns][i2];
565           cl=r2[js-nl2-nl];
566           x2=xx2; y2=cl->GetY(); z2=cl->GetZ();
567           for(Int_t ii=0; ii<3; ii++) ti[ii] = cl->GetTrackIndex(ii);
568         }
569         else if (js<nl2+nl+nm+nu) {
570           if(turn != 1) continue;
571           AliTRDtimeBin& r2=fTrSec[(ns+1)%max_sec][i2];
572           cl=r2[js-nl2-nl-nm];
573           y2=cl->GetY(); z2=cl->GetZ();
574           for(Int_t ii=0; ii<3; ii++) ti[ii] = cl->GetTrackIndex(ii);
575
576           x2=xx2*cs-y2*sn;
577           y2=xx2*sn+y2*cs;
578
579         }
580         else {
581           if(turn != 2) continue;
582           AliTRDtimeBin& r2=fTrSec[(ns+2)%max_sec][i2];
583           cl=r2[js-nl2-nl-nm-nu];
584           y2=cl->GetY(); z2=cl->GetZ();
585           for(Int_t ii=0; ii<3; ii++) ti[ii] = cl->GetTrackIndex(ii);
586           
587           x2=xx2*cs2-y2*sn2;
588           y2=xx2*sn2+y2*cs2;
589         }         
590         
591
592         match = false;
593         matched_index = -1;
594         for (Int_t ii=0; ii<3; ii++) {
595           // cerr<<"ti["<<ii<<"] = "<<ti[ii]<<"; to["<<ii<<"] = "<<to[ii]<<endl;
596           if(ti[ii] < 0) continue;
597           if(ti[ii] >= nprim) continue;
598           for (Int_t kk=0; kk<3; kk++) {
599             if(to[kk] < 0) continue;
600             if(to[kk] >= nprim) continue;
601             if(ti[ii] == to[kk]) {
602               //cerr<<"ti["<<ii<<"] = "<<ti[ii]<<" = "<<to[kk]<<" = to["<<kk<<"]"<<endl;
603               matched_index = ti[ii];
604               match = true;
605             }
606           }
607         }                 
608         
609         if(TMath::Abs(z1-z2) > fMaxSeedDeltaZ12) continue;
610
611         Double_t zz=z1 - z1/x1*(x1-x2);
612
613         if (TMath::Abs(zz-z2)>fMaxSeedDeltaZ) continue;   
614
615         Double_t d=(x2-x1)*(0.-y2)-(0.-x2)*(y2-y1);
616         if (d==0.) {cerr<<"TRD MakeSeeds: Straight seed !\n"; continue;}
617
618         x[0]=y1;
619         x[1]=z1;
620         x[2]=f1trd(x1,y1,x2,y2,x3,y3);
621
622         if (TMath::Abs(x[2]) > fMaxSeedC) continue;
623
624         x[3]=f2trd(x1,y1,x2,y2,x3,y3);
625
626         if (TMath::Abs(x[2]*x1-x[3]) >= 0.99999) continue;
627
628         x[4]=f3trd(x1,y1,x2,y2,z1,z2);
629
630         if (TMath::Abs(x[4]) > fMaxSeedTan) continue;
631
632         Double_t a=asin(x[3]);
633         Double_t zv=z1 - x[4]/x[2]*(a+asin(x[2]*x1-x[3]));
634
635         if (TMath::Abs(zv)>fMaxSeedVertexZ) continue;    
636
637         Double_t sy1=r1[is]->GetSigmaY2(), sz1=r1[is]->GetSigmaZ2();
638         Double_t sy2=cl->GetSigmaY2(),     sz2=cl->GetSigmaZ2();
639         Double_t sy3=fSeedErrorSY3, sy=fSeedErrorSY, sz=fSeedErrorSZ;
640
641         Double_t f20=(f1trd(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
642         Double_t f22=(f1trd(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
643         Double_t f24=(f1trd(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
644         Double_t f30=(f2trd(x1,y1+sy,x2,y2,x3,y3)-x[3])/sy;
645         Double_t f32=(f2trd(x1,y1,x2,y2+sy,x3,y3)-x[3])/sy;
646         Double_t f34=(f2trd(x1,y1,x2,y2,x3,y3+sy)-x[3])/sy;
647         Double_t f40=(f3trd(x1,y1+sy,x2,y2,z1,z2)-x[4])/sy;
648         Double_t f41=(f3trd(x1,y1,x2,y2,z1+sz,z2)-x[4])/sz;
649         Double_t f42=(f3trd(x1,y1,x2,y2+sy,z1,z2)-x[4])/sy;
650         Double_t f43=(f3trd(x1,y1,x2,y2,z1,z2+sz)-x[4])/sz;
651
652         c[0]=sy1;
653         c[1]=0.;       c[2]=sz1;
654         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f24*sy3*f24;
655         c[6]=f30*sy1;  c[7]=0.;       c[8]=f30*sy1*f20+f32*sy2*f22+f34*sy3*f24;
656                                       c[9]=f30*sy1*f30+f32*sy2*f32+f34*sy3*f34;
657         c[10]=f40*sy1; c[11]=f41*sz1; c[12]=f40*sy1*f20+f42*sy2*f22;
658         c[13]=f40*sy1*f30+f42*sy2*f32;
659         c[14]=f40*sy1*f40+f41*sz1*f41+f42*sy2*f42+f43*sz2*f43;   
660
661         UInt_t index=r1.GetIndex(is);
662         
663         AliTRDtrack *track=new AliTRDtrack(r1[is],index,x,c,x1,ns*alpha+shift); 
664
665         Int_t rc=FindProlongation(*track,fTrSec,ns,i2,matched_index,hsame,hdiff);
666
667         //      if (match) hsame->Fill((Float_t) track->GetNclusters());
668         //      else hdiff->Fill((Float_t) track->GetNclusters());  
669         //      delete track;
670         //      continue;
671
672         if ((rc < 0) || 
673             (track->GetNclusters() < (i1-i2)*fMinClustersInSeed)) delete track;
674         else { 
675           fSeeds->AddLast(track); fNseeds++; 
676           printf("\r found seed %d  ", fNseeds);
677         }
678       }
679     }
680   }
681
682   fSeeds->Sort();
683 }          
684
685 //_____________________________________________________________________________
686 void AliTRDtracker::ReadClusters(TObjArray *array, const Char_t *filename) 
687 {
688   //
689   // Reads AliTRDclusters (option >= 0) or AliTRDrecPoints (option < 0) 
690   // from the file. The names of the cluster tree and branches 
691   // should match the ones used in AliTRDclusterizer::WriteClusters()
692   //
693
694   TDirectory *savedir=gDirectory; 
695
696   TFile *file = TFile::Open(filename);
697   if (!file->IsOpen()) {printf("Can't open file %s !\n",filename); return;} 
698
699   TTree *ClusterTree = (TTree*)file->Get("ClusterTree");
700
701   TObjArray *ClusterArray = new TObjArray(400); 
702  
703   ClusterTree->GetBranch("TRDcluster")->SetAddress(&ClusterArray); 
704   
705   Int_t nEntries = (Int_t) ClusterTree->GetEntries();
706   printf("found %d entries in %s.\n",nEntries,ClusterTree->GetName());
707
708   // Loop through all entries in the tree
709   Int_t nbytes;
710   AliTRDcluster *c = 0;
711
712   for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {    
713     
714     // Import the tree
715     nbytes += ClusterTree->GetEvent(iEntry);  
716
717     // Get the number of points in the detector
718     Int_t nCluster = ClusterArray->GetEntriesFast();  
719     printf("Read %d clusters from entry %d \n", nCluster, iEntry);
720
721     // Loop through all TRD digits
722     for (Int_t iCluster = 0; iCluster < nCluster; iCluster++) { 
723       c = (AliTRDcluster*)ClusterArray->UncheckedAt(iCluster);
724       AliTRDcluster *co = new AliTRDcluster(*c);
725       co->SetSigmaY2(c->GetSigmaY2() * fSY2corr);
726       array->AddLast(co);
727       delete ClusterArray->RemoveAt(iCluster); 
728     }
729   }
730
731   file->Close();                   
732   delete ClusterArray;
733   savedir->cd(); 
734   
735 }
736
737 //___________________________________________________________________
738 void AliTRDtracker::FindTracks(AliTRDtrackingSector* fTrSec, TH1F *hs, TH1F *hd) 
739 {
740   //
741   // Finds tracks in TRD
742   //
743
744   TH1F *hsame = hs;
745   TH1F *hdiff = hd;   
746
747   Int_t num_of_time_bins = fTrSec[0].GetNtimeBins(); 
748   Int_t nseed=fSeeds->GetEntriesFast();
749
750   Int_t nSeedClusters;
751   for (Int_t i=0; i<nseed; i++) {
752     cerr<<"FindTracks: seed "<<i+1<<" out of "<<nseed<<endl;
753
754     AliTRDtrack& t=*((AliTRDtrack*)fSeeds->UncheckedAt(i));
755
756     nSeedClusters = t.GetNclusters();
757     Double_t alpha=t.GetAlpha();
758
759     if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
760     if (alpha < 0.            ) alpha += 2.*TMath::Pi();
761     Int_t ns=Int_t(alpha/AliTRDgeometry::GetAlpha())%AliTRDgeometry::kNsect;
762
763     Int_t label = GetTrackLabel(t);
764
765     if (FindProlongation(t,fTrSec,ns,0,label,hsame,hdiff)) {
766       cerr<<"No of clusters in the track = "<<t.GetNclusters()<<endl; 
767       if (t.GetNclusters() >= Int_t(fMinClustersInTrack*num_of_time_bins)) {
768         Int_t label = GetTrackLabel(t);
769         t.SetLabel(label);
770         t.CookdEdx();
771         UseClusters(t);
772
773         AliTRDtrack *pt = new AliTRDtrack(t);
774         fTracks->AddLast(pt); fNtracks++;     
775
776         cerr<<"found track "<<fNtracks<<endl;
777       }                         
778     }     
779     delete fSeeds->RemoveAt(i);  
780     fNseeds--;
781   }            
782 }
783
784 //__________________________________________________________________
785 void AliTRDtracker::UseClusters(AliTRDtrack t) {
786   Int_t ncl=t.GetNclusters();
787   for (Int_t i=0; i<ncl; i++) {
788     Int_t index = t.GetClusterIndex(i);
789     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(index);
790     c->Use();
791   }
792 }
793
794 //__________________________________________________________________
795 Int_t AliTRDtracker::GetTrackLabel(AliTRDtrack t) {
796
797   Int_t label=123456789, index, i, j;
798   Int_t ncl=t.GetNclusters();
799   const Int_t range = AliTRDgeometry::kNplan * fGeom->GetTimeMax();
800   Bool_t label_added;
801
802   //  Int_t s[range][2];
803   Int_t **s = new Int_t* [range];
804   for (i=0; i<range; i++) {
805     s[i] = new Int_t[2];
806   }
807   for (i=0; i<range; i++) {
808     s[i][0]=-1;
809     s[i][1]=0;
810   }
811
812   Int_t t0,t1,t2;
813   for (i=0; i<ncl; i++) {
814     index=t.GetClusterIndex(i);
815     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(index);
816     t0=c->GetTrackIndex(0);
817     t1=c->GetTrackIndex(1);
818     t2=c->GetTrackIndex(2);
819   }
820
821   for (i=0; i<ncl; i++) {
822     index=t.GetClusterIndex(i);
823     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(index);
824     for (Int_t k=0; k<3; k++) { 
825       label=c->GetTrackIndex(k);
826       label_added=kFALSE; j=0;
827       if (label >= 0) {
828         while ( (!label_added) && ( j < range ) ) {
829           if (s[j][0]==label || s[j][1]==0) {
830             s[j][0]=label; 
831             s[j][1]=s[j][1]+1; 
832             label_added=kTRUE;
833           }
834           j++;
835         }
836       }
837     }
838   }
839
840   Int_t max=0;
841   label = -123456789;
842
843   for (i=0; i<range; i++) {
844     if (s[i][1]>max) {
845       max=s[i][1]; label=s[i][0];
846     }
847   }
848   delete []s;
849   if(max > ncl*fLabelFraction) return label;
850   else return -1;
851 }
852
853 //___________________________________________________________________
854
855 Int_t AliTRDtracker::WriteTracks(const Char_t *filename) {
856
857   TDirectory *savedir=gDirectory;   
858
859   //TFile *out=TFile::Open(filename,"RECREATE");
860   TFile *out = (TFile*) gROOT->GetListOfFiles()->FindObject(filename);
861   if (!out) {
862     printf("AliTRDtracker::Open -- ");
863     printf("Open the ALIROOT-file %s.\n",filename);
864     out = new TFile(filename,"RECREATE");
865   }
866   else {
867     printf("AliTRDtracker::Open -- ");
868     printf("%s is already open.\n",filename);
869   }
870
871   TTree tracktree("TreeT","Tree with TRD tracks");
872
873   AliTRDtrack *iotrack=0;
874   tracktree.Branch("tracks","AliTRDtrack",&iotrack,32000,0);  
875
876   Int_t ntracks=fTracks->GetEntriesFast();
877
878   for (Int_t i=0; i<ntracks; i++) {
879     AliTRDtrack *pt=(AliTRDtrack*)fTracks->UncheckedAt(i);
880     iotrack=pt;
881     tracktree.Fill(); 
882     cerr<<"WriteTracks: put track "<<i<<" in the tree"<<endl;
883   }
884
885   tracktree.Write(); 
886   out->Close(); 
887
888   savedir->cd();  
889
890   cerr<<"WriteTracks: done"<<endl;
891   return 1;
892 }
893