]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDtracker.cxx
New classes to handle the vertex in simulation (T.Kuhr)
[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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  The standard TRD tracker                                                 //
21 //                                                                           //
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include <Riostream.h>
25
26 #include <TFile.h>
27 #include <TBranch.h>
28 #include <TTree.h>  
29 #include <TObjArray.h> 
30
31 #include "AliTRDgeometry.h"
32 #include "AliTRDparameter.h"
33 #include "AliTRDgeometryDetail.h"
34 #include "AliTRDcluster.h" 
35 #include "AliTRDtrack.h"
36 #include "AliTRDPartID.h"
37 #include "../TPC/AliTPCtrack.h"
38
39 #include "AliTRDtracker.h"
40
41 ClassImp(AliTRDtracker) 
42
43   const  Float_t     AliTRDtracker::fgkSeedDepth          = 0.5; 
44   const  Float_t     AliTRDtracker::fgkSeedStep           = 0.10;   
45   const  Float_t     AliTRDtracker::fgkSeedGap            = 0.25;  
46
47   const  Float_t     AliTRDtracker::fgkMaxSeedDeltaZ12    = 40.;  
48   const  Float_t     AliTRDtracker::fgkMaxSeedDeltaZ      = 25.;  
49   const  Float_t     AliTRDtracker::fgkMaxSeedC           = 0.0052; 
50   const  Float_t     AliTRDtracker::fgkMaxSeedTan         = 1.2;  
51   const  Float_t     AliTRDtracker::fgkMaxSeedVertexZ     = 150.; 
52
53   const  Double_t    AliTRDtracker::fgkSeedErrorSY        = 0.2;
54   const  Double_t    AliTRDtracker::fgkSeedErrorSY3       = 2.5;
55   const  Double_t    AliTRDtracker::fgkSeedErrorSZ        = 0.1;
56
57   const  Float_t     AliTRDtracker::fgkMinClustersInSeed  = 0.7;  
58
59   const  Float_t     AliTRDtracker::fgkMinClustersInTrack = 0.5;  
60   const  Float_t     AliTRDtracker::fgkMinFractionOfFoundClusters = 0.8;  
61
62   const  Float_t     AliTRDtracker::fgkSkipDepth          = 0.3;
63   const  Float_t     AliTRDtracker::fgkLabelFraction      = 0.8;  
64   const  Float_t     AliTRDtracker::fgkWideRoad           = 20.;
65
66   const  Double_t    AliTRDtracker::fgkMaxChi2            = 12.; 
67
68 const Int_t AliTRDtracker::fgkFirstPlane = 5;
69 const Int_t AliTRDtracker::fgkLastPlane = 17;
70
71
72 //____________________________________________________________________
73 AliTRDtracker::AliTRDtracker(const TFile *geomfile):AliTracker()
74 {
75   // 
76   //  Main constructor
77   //  
78
79   //Float_t fTzero = 0;
80    
81   fAddTRDseeds = kFALSE;
82   fGeom = NULL;
83   fNoTilt = kFALSE;
84   
85   TDirectory *savedir=gDirectory; 
86   TFile *in=(TFile*)geomfile;  
87   if (!in->IsOpen()) {
88     printf("AliTRDtracker::AliTRDtracker(): geometry file is not open!\n");
89     printf("    DETAIL TRD geometry and DEFAULT TRD parameter will be used\n");
90   }
91   else {
92     in->cd();  
93 //    in->ls();
94     fGeom = (AliTRDgeometry*) in->Get("TRDgeometry");
95     fPar  = (AliTRDparameter*) in->Get("TRDparameter");
96 //    fGeom->Dump();
97   }
98
99   if(fGeom) {
100     //    fTzero = geo->GetT0();
101     printf("Found geometry version %d on file \n", fGeom->IsVersion());
102   }
103   else { 
104     printf("AliTRDtracker::AliTRDtracker(): can't find TRD geometry!\n");
105     printf("The DETAIL TRD geometry will be used\n");
106     fGeom = new AliTRDgeometryDetail();
107   } 
108
109   if (!fPar) {  
110     printf("AliTRDtracker::AliTRDtracker(): can't find TRD parameter!\n");
111     printf("The DEFAULT TRD parameter will be used\n");
112     fPar = new AliTRDparameter();
113   }
114   fPar->ReInit();
115
116   savedir->cd();  
117
118
119   //  fGeom->SetT0(fTzero);
120
121   fNclusters = 0;
122   fClusters  = new TObjArray(2000); 
123   fNseeds    = 0;
124   fSeeds     = new TObjArray(2000);
125   fNtracks   = 0;
126   fTracks    = new TObjArray(1000);
127
128   for(Int_t geomS = 0; geomS < kTrackingSectors; geomS++) {
129     Int_t trS = CookSectorIndex(geomS);
130     fTrSec[trS] = new AliTRDtrackingSector(fGeom, geomS, fPar);
131   }
132
133   Float_t tiltAngle = TMath::Abs(fPar->GetTiltingAngle()); 
134   if(tiltAngle < 0.1) {
135     fNoTilt = kTRUE;
136   }
137
138   fSY2corr = 0.2;
139   fSZ2corr = 120.;      
140
141   if(fNoTilt && (tiltAngle > 0.1)) fSY2corr = fSY2corr + tiltAngle * 0.05; 
142
143
144   // calculate max gap on track
145
146   Double_t dxAmp = (Double_t) fGeom->CamHght();   // Amplification region
147   Double_t dxDrift = (Double_t) fGeom->CdrHght(); // Drift region
148
149   Double_t dx = (Double_t) fPar->GetTimeBinSize();   
150   Int_t tbAmp = fPar->GetTimeBefore();
151   Int_t maxAmp = (Int_t) ((dxAmp+0.000001)/dx);
152   if(kTRUE) maxAmp = 0;  // intentional until we change the parameter class 
153   Int_t tbDrift = fPar->GetTimeMax();
154   Int_t maxDrift = (Int_t) ((dxDrift+0.000001)/dx);
155
156   tbDrift = TMath::Min(tbDrift,maxDrift);
157   tbAmp = TMath::Min(tbAmp,maxAmp);
158
159   fTimeBinsPerPlane = tbAmp + tbDrift;
160   fMaxGap = (Int_t) (fTimeBinsPerPlane * fGeom->Nplan() * fgkSkipDepth);
161
162   fVocal = kFALSE;
163
164
165   // Barrel Tracks [SR, 03.04.2003]
166
167   fBarrelFile = 0;
168   fBarrelTree = 0;
169   fBarrelArray = 0;
170   fBarrelTrack = 0;
171
172   savedir->cd();
173 }   
174
175 //___________________________________________________________________
176 AliTRDtracker::~AliTRDtracker()
177 {
178   //
179   // Destructor of AliTRDtracker 
180   //
181
182   delete fClusters;
183   delete fTracks;
184   delete fSeeds;
185   delete fGeom;  
186   delete fPar;  
187
188   for(Int_t geomS = 0; geomS < kTrackingSectors; geomS++) {
189     delete fTrSec[geomS];
190   }
191 }   
192
193 //_____________________________________________________________________
194
195 void AliTRDtracker::SetBarrelTree(const char *mode) {
196   //
197   //
198   //
199
200   if (!IsStoringBarrel()) return;
201
202   TDirectory *sav = gDirectory;
203   if (!fBarrelFile) fBarrelFile = new TFile("AliBarrelTracks.root", "UPDATE");
204
205   char buff[40];
206   sprintf(buff,  "BarrelTRD_%d_%s", GetEventNumber(), mode);
207
208   fBarrelFile->cd();
209   fBarrelTree = new TTree(buff, "Barrel TPC tracks");
210   
211   Int_t nRefs = fgkLastPlane - fgkFirstPlane + 1;
212
213   if (!fBarrelArray) fBarrelArray = new TClonesArray("AliBarrelTrack", nRefs);
214   for(Int_t i=0; i<nRefs; i++) new((*fBarrelArray)[i]) AliBarrelTrack();
215   
216   fBarrelTree->Branch("tracks", &fBarrelArray);
217   sav->cd();
218 }
219   
220 //_____________________________________________________________________
221
222 void AliTRDtracker::StoreBarrelTrack(AliTRDtrack *ps, Int_t refPlane, Int_t isIn) {
223   //
224   //
225   //
226   
227   if (!IsStoringBarrel()) return;
228   
229   static Int_t nClusters;
230   static Int_t nWrong;
231   static Double_t chi2;
232   static Int_t index;
233   static Bool_t wasLast = kTRUE;
234   
235   Int_t newClusters, newWrong;
236   Double_t newChi2;
237   
238   if (wasLast) {   
239  
240     fBarrelArray->Clear();
241     nClusters = nWrong = 0;
242     chi2 = 0.0;
243     index = 0;
244     wasLast = kFALSE;
245   }
246   
247   fBarrelTrack = (AliBarrelTrack*)(*fBarrelArray)[index++];
248   ps->GetBarrelTrack(fBarrelTrack);
249   
250   newClusters = ps->GetNumberOfClusters() - nClusters; 
251   newWrong = ps->GetNWrong() - nWrong;
252   newChi2 = ps->GetChi2() - chi2;
253   
254   nClusters =  ps->GetNumberOfClusters();
255   nWrong = ps->GetNWrong();
256   chi2 = ps->GetChi2();  
257
258   if (refPlane != fgkLastPlane) {
259     fBarrelTrack->SetNClusters(newClusters, newChi2);
260     fBarrelTrack->SetNWrongClusters(newWrong);
261   } else {
262     wasLast = kTRUE;
263   } 
264
265   fBarrelTrack->SetRefPlane(refPlane, isIn);
266 }
267
268 //_____________________________________________________________________
269
270 Bool_t AliTRDtracker::AdjustSector(AliTRDtrack *track) {
271   //
272   // Rotates the track when necessary
273   //
274
275   Double_t alpha = AliTRDgeometry::GetAlpha(); 
276   Double_t y = track->GetY();
277   Double_t ymax = track->GetX()*TMath::Tan(0.5*alpha);
278
279   //Int_t ns = AliTRDgeometry::kNsect;
280   //Int_t s=Int_t(track->GetAlpha()/alpha)%ns; 
281
282   if (y > ymax) {
283     //s = (s+1) % ns;
284     if (!track->Rotate(alpha)) return kFALSE;
285   } else if (y <-ymax) {
286     //s = (s-1+ns) % ns;                           
287     if (!track->Rotate(-alpha)) return kFALSE;   
288   } 
289
290   return kTRUE;
291 }
292
293 //_____________________________________________________________________
294 inline Double_t f1trd(Double_t x1,Double_t y1,
295                       Double_t x2,Double_t y2,
296                       Double_t x3,Double_t y3)
297 {
298   //
299   // Initial approximation of the track curvature
300   //
301   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
302   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
303                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
304   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
305                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
306
307   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
308
309   return -xr*yr/sqrt(xr*xr+yr*yr);
310 }          
311
312 //_____________________________________________________________________
313 inline Double_t f2trd(Double_t x1,Double_t y1,
314                       Double_t x2,Double_t y2,
315                       Double_t x3,Double_t y3)
316 {
317   //
318   // Initial approximation of the track curvature times X coordinate
319   // of the center of curvature
320   //
321
322   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
323   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
324                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
325   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
326                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
327
328   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
329
330   return -a/(d*y1-b)*xr/sqrt(xr*xr+yr*yr);
331 }          
332
333 //_____________________________________________________________________
334 inline Double_t f3trd(Double_t x1,Double_t y1,
335                       Double_t x2,Double_t y2,
336                       Double_t z1,Double_t z2)
337 {
338   //
339   // Initial approximation of the tangent of the track dip angle
340   //
341
342   return (z1 - z2)/sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
343 }            
344
345 //___________________________________________________________________
346 Int_t AliTRDtracker::Clusters2Tracks(const TFile *inp, TFile *out)
347 {
348   //
349   // Finds tracks within the TRD. File <inp> is expected to contain seeds 
350   // at the outer part of the TRD. If <inp> is NULL, the seeds
351   // are found within the TRD if fAddTRDseeds is TRUE. 
352   // The tracks are propagated to the innermost time bin 
353   // of the TRD and stored in file <out>. 
354   //
355
356   LoadEvent();
357  
358   TDirectory *savedir=gDirectory;
359
360   char   tname[100];
361
362   if (!out->IsOpen()) {
363     cerr<<"AliTRDtracker::Clusters2Tracks(): output file is not open !\n";
364     return 1;
365   }    
366
367   sprintf(tname,"seedTRDtoTPC_%d",GetEventNumber()); 
368   TTree tpcTree(tname,"Tree with seeds from TRD at outer TPC pad row");
369   AliTPCtrack *iotrack=0;
370   tpcTree.Branch("tracks","AliTPCtrack",&iotrack,32000,0); 
371
372   sprintf(tname,"TreeT%d_TRD",GetEventNumber());
373   TTree trdTree(tname,"TRD tracks at inner TRD time bin");
374   AliTRDtrack *iotrackTRD=0;
375   trdTree.Branch("tracks","AliTRDtrack",&iotrackTRD,32000,0);  
376
377   Int_t timeBins = fTrSec[0]->GetNumberOfTimeBins();
378   Float_t foundMin = fgkMinClustersInTrack * timeBins; 
379
380   if (inp) {
381      TFile *in=(TFile*)inp;
382      if (!in->IsOpen()) {
383         cerr<<
384           "AliTRDtracker::Clusters2Tracks(): file with seeds is not open !\n";
385         cerr<<" ... going for seeds finding inside the TRD\n";
386      }
387      else {
388        in->cd();
389        sprintf(tname,"TRDb_%d",GetEventNumber());  
390        TTree *seedTree=(TTree*)in->Get(tname);  
391        if (!seedTree) {
392          cerr<<"AliTRDtracker::Clusters2Tracks(): ";
393          cerr<<"can't get a tree with track seeds !\n";
394          return 3;
395        }  
396        AliTRDtrack *seed=new AliTRDtrack;
397        seedTree->SetBranchAddress("tracks",&seed);
398
399        Int_t n=(Int_t)seedTree->GetEntries();
400        for (Int_t i=0; i<n; i++) {
401          seedTree->GetEvent(i);
402          seed->ResetCovariance(); 
403          AliTRDtrack *tr = new AliTRDtrack(*seed,seed->GetAlpha());
404          fSeeds->AddLast(tr);
405          fNseeds++;
406        }          
407        delete seed;
408        delete seedTree;
409      }
410   }
411
412   out->cd();
413
414
415   // find tracks from loaded seeds
416
417   Int_t nseed=fSeeds->GetEntriesFast();
418   Int_t i, found = 0;
419   Int_t innerTB = fTrSec[0]->GetInnerTimeBin();
420
421   for (i=0; i<nseed; i++) {   
422     AliTRDtrack *pt=(AliTRDtrack*)fSeeds->UncheckedAt(i), &t=*pt; 
423     FollowProlongation(t, innerTB); 
424     if (t.GetNumberOfClusters() >= foundMin) {
425       UseClusters(&t);
426       CookLabel(pt, 1-fgkLabelFraction);
427       //      t.CookdEdx();
428     }
429     iotrackTRD = pt;
430     trdTree.Fill();
431     found++;
432 //    cout<<found<<'\r';     
433
434     if(PropagateToTPC(t)) {
435       AliTPCtrack *tpc = new AliTPCtrack(*pt,pt->GetAlpha());
436       iotrack = tpc;
437       tpcTree.Fill();
438       delete tpc;
439     }  
440     delete fSeeds->RemoveAt(i);
441     fNseeds--;
442   }     
443
444   cout<<"Number of loaded seeds: "<<nseed<<endl;  
445   cout<<"Number of found tracks from loaded seeds: "<<found<<endl;
446
447   // after tracks from loaded seeds are found and the corresponding 
448   // clusters are used, look for additional seeds from TRD
449
450   if(fAddTRDseeds) { 
451     // Find tracks for the seeds in the TRD
452     Int_t timeBins = fTrSec[0]->GetNumberOfTimeBins();
453   
454     Int_t nSteps = (Int_t) (fgkSeedDepth / fgkSeedStep);
455     Int_t gap = (Int_t) (timeBins * fgkSeedGap);
456     Int_t step = (Int_t) (timeBins * fgkSeedStep);
457   
458     // make a first turn with tight cut on initial curvature
459     for(Int_t turn = 1; turn <= 2; turn++) {
460       if(turn == 2) {
461         nSteps = (Int_t) (fgkSeedDepth / (3*fgkSeedStep));
462         step = (Int_t) (timeBins * (3*fgkSeedStep));
463       }
464       for(Int_t i=0; i<nSteps; i++) {
465         Int_t outer=timeBins-1-i*step; 
466         Int_t inner=outer-gap;
467
468         nseed=fSeeds->GetEntriesFast();
469       
470         MakeSeeds(inner, outer, turn);
471       
472         nseed=fSeeds->GetEntriesFast();
473         printf("\n turn %d, step %d: number of seeds for TRD inward %d\n", 
474                turn, i, nseed); 
475               
476         for (Int_t i=0; i<nseed; i++) {   
477           AliTRDtrack *pt=(AliTRDtrack*)fSeeds->UncheckedAt(i), &t=*pt; 
478           FollowProlongation(t,innerTB); 
479           if (t.GetNumberOfClusters() >= foundMin) {
480             UseClusters(&t);
481             CookLabel(pt, 1-fgkLabelFraction);
482             t.CookdEdx();
483             found++;
484 //            cout<<found<<'\r';     
485             iotrackTRD = pt;
486             trdTree.Fill();
487             if(PropagateToTPC(t)) {
488               AliTPCtrack *tpc = new AliTPCtrack(*pt,pt->GetAlpha());
489               iotrack = tpc;
490               tpcTree.Fill();
491               delete tpc;
492             }        
493           }
494           delete fSeeds->RemoveAt(i);
495           fNseeds--;
496         }
497       }
498     }
499   }
500   tpcTree.Write(); 
501   trdTree.Write(); 
502   
503   cout<<"Total number of found tracks: "<<found<<endl;
504     
505   UnloadEvent();
506     
507   savedir->cd();  
508   
509   return 0;    
510 }     
511           
512 //___________________________________________________________________
513 Int_t AliTRDtracker::Clusters2Tracks(AliESD* event)
514 {
515   //
516   // Finds tracks within the TRD. The ESD event is expected to contain seeds 
517   // at the outer part of the TRD. The seeds
518   // are found within the TRD if fAddTRDseeds is TRUE. 
519   // The tracks are propagated to the innermost time bin 
520   // of the TRD and the ESD event is updated
521   //
522
523   Int_t timeBins = fTrSec[0]->GetNumberOfTimeBins();
524   Float_t foundMin = fgkMinClustersInTrack * timeBins; 
525   Int_t nseed = 0;
526   Int_t found = 0;
527   Int_t innerTB = fTrSec[0]->GetInnerTimeBin();
528
529   Int_t n = event->GetNumberOfTracks();
530   for (Int_t i=0; i<n; i++) {
531     AliESDtrack* seed=event->GetTrack(i);
532     ULong_t status=seed->GetStatus();
533     if ( (status & AliESDtrack::kTRDout ) == 0 ) continue;
534     if ( (status & AliESDtrack::kTRDin) != 0 ) continue;
535     nseed++;
536
537     AliTRDtrack* seed2 = new AliTRDtrack(*seed);
538     seed2->ResetCovariance(); 
539     AliTRDtrack *pt = new AliTRDtrack(*seed2,seed2->GetAlpha());
540     AliTRDtrack &t=*pt; 
541     FollowProlongation(t, innerTB); 
542     if (t.GetNumberOfClusters() >= foundMin) {
543       UseClusters(&t);
544       CookLabel(pt, 1-fgkLabelFraction);
545       //      t.CookdEdx();
546     }
547     found++;
548 //    cout<<found<<'\r';     
549
550     if(PropagateToTPC(t)) {
551       seed->UpdateTrackParams(pt, AliESDtrack::kTRDin);
552     }  
553     delete seed2;
554     delete pt;
555   }     
556
557   cout<<"Number of loaded seeds: "<<nseed<<endl;  
558   cout<<"Number of found tracks from loaded seeds: "<<found<<endl;
559
560   // after tracks from loaded seeds are found and the corresponding 
561   // clusters are used, look for additional seeds from TRD
562
563   if(fAddTRDseeds) { 
564     // Find tracks for the seeds in the TRD
565     Int_t timeBins = fTrSec[0]->GetNumberOfTimeBins();
566   
567     Int_t nSteps = (Int_t) (fgkSeedDepth / fgkSeedStep);
568     Int_t gap = (Int_t) (timeBins * fgkSeedGap);
569     Int_t step = (Int_t) (timeBins * fgkSeedStep);
570   
571     // make a first turn with tight cut on initial curvature
572     for(Int_t turn = 1; turn <= 2; turn++) {
573       if(turn == 2) {
574         nSteps = (Int_t) (fgkSeedDepth / (3*fgkSeedStep));
575         step = (Int_t) (timeBins * (3*fgkSeedStep));
576       }
577       for(Int_t i=0; i<nSteps; i++) {
578         Int_t outer=timeBins-1-i*step; 
579         Int_t inner=outer-gap;
580
581         nseed=fSeeds->GetEntriesFast();
582       
583         MakeSeeds(inner, outer, turn);
584       
585         nseed=fSeeds->GetEntriesFast();
586         printf("\n turn %d, step %d: number of seeds for TRD inward %d\n", 
587                turn, i, nseed); 
588               
589         for (Int_t i=0; i<nseed; i++) {   
590           AliTRDtrack *pt=(AliTRDtrack*)fSeeds->UncheckedAt(i), &t=*pt; 
591           FollowProlongation(t,innerTB); 
592           if (t.GetNumberOfClusters() >= foundMin) {
593             UseClusters(&t);
594             CookLabel(pt, 1-fgkLabelFraction);
595             t.CookdEdx();
596             found++;
597 //            cout<<found<<'\r';     
598             if(PropagateToTPC(t)) {
599               AliESDtrack track;
600               track.UpdateTrackParams(pt,AliESDtrack::kTRDin);
601               event->AddTrack(&track);
602             }        
603           }
604           delete fSeeds->RemoveAt(i);
605           fNseeds--;
606         }
607       }
608     }
609   }
610   
611   cout<<"Total number of found tracks: "<<found<<endl;
612     
613   return 0;    
614 }     
615      
616   
617
618 //_____________________________________________________________________________
619 Int_t AliTRDtracker::PropagateBack(const TFile *inp, TFile *out) {
620   //
621   // Reads seeds from file <inp>. The seeds are AliTPCtrack's found and
622   // backpropagated by the TPC tracker. Each seed is first propagated 
623   // to the TRD, and then its prolongation is searched in the TRD.
624   // If sufficiently long continuation of the track is found in the TRD
625   // the track is updated, otherwise it's stored as originaly defined 
626   // by the TPC tracker.   
627   //  
628
629   LoadEvent();
630
631   TDirectory *savedir=gDirectory;
632
633   TFile *in=(TFile*)inp;
634
635   if (!in->IsOpen()) {
636      cerr<<"AliTRDtracker::PropagateBack(): ";
637      cerr<<"file with back propagated TPC tracks is not open !\n";
638      return 1;
639   }                   
640
641   if (!out->IsOpen()) {
642      cerr<<"AliTRDtracker::PropagateBack(): ";
643      cerr<<"file for back propagated TRD tracks is not open !\n";
644      return 2;
645   }      
646
647   in->cd();
648   char   tname[100];
649   sprintf(tname,"seedsTPCtoTRD_%d",GetEventNumber());       
650   TTree *seedTree=(TTree*)in->Get(tname);
651   if (!seedTree) {
652      cerr<<"AliTRDtracker::PropagateBack(): ";
653      cerr<<"can't get a tree with seeds from TPC !\n";
654      cerr<<"check if your version of TPC tracker creates tree "<<tname<<"\n";
655      return 3;
656   }
657
658   AliTPCtrack *seed=new AliTPCtrack;
659   seedTree->SetBranchAddress("tracks",&seed);
660
661   Int_t n=(Int_t)seedTree->GetEntries();
662   for (Int_t i=0; i<n; i++) {
663      seedTree->GetEvent(i);
664      Int_t lbl = seed->GetLabel();
665      AliTRDtrack *tr = new AliTRDtrack(*seed,seed->GetAlpha());
666      tr->SetSeedLabel(lbl);
667      fSeeds->AddLast(tr);
668      fNseeds++;
669   }
670
671   delete seed;
672   delete seedTree;
673
674   out->cd();
675
676   AliTPCtrack *otrack=0;
677
678   sprintf(tname,"seedsTRDtoTOF1_%d",GetEventNumber());  
679   TTree tofTree1(tname,"Tracks back propagated through TPC and TRD");
680   tofTree1.Branch("tracks","AliTPCtrack",&otrack,32000,0);  
681
682   sprintf(tname,"seedsTRDtoTOF2_%d",GetEventNumber());  
683   TTree tofTree2(tname,"Tracks back propagated through TPC and TRD");
684   tofTree2.Branch("tracks","AliTPCtrack",&otrack,32000,0);  
685
686   sprintf(tname,"seedsTRDtoPHOS_%d",GetEventNumber());  
687   TTree phosTree(tname,"Tracks back propagated through TPC and TRD");
688   phosTree.Branch("tracks","AliTPCtrack",&otrack,32000,0);  
689
690   sprintf(tname,"seedsTRDtoRICH_%d",GetEventNumber());  
691   TTree richTree(tname,"Tracks back propagated through TPC and TRD");
692   richTree.Branch("tracks","AliTPCtrack",&otrack,32000,0);  
693
694   sprintf(tname,"TRDb_%d",GetEventNumber());  
695   TTree trdTree(tname,"Back propagated TRD tracks at outer TRD time bin");
696   AliTRDtrack *otrackTRD=0;
697   trdTree.Branch("tracks","AliTRDtrack",&otrackTRD,32000,0);   
698      
699   if (IsStoringBarrel()) SetBarrelTree("back");
700   out->cd();
701
702   Int_t found=0;  
703   Int_t nseed=fSeeds->GetEntriesFast();
704
705   //  Float_t foundMin = fgkMinClustersInTrack * fTimeBinsPerPlane * fGeom->Nplan(); 
706   Float_t foundMin = 40;
707
708   Int_t outermostTB  = fTrSec[0]->GetOuterTimeBin();
709
710   for (Int_t i=0; i<nseed; i++) {  
711
712     AliTRDtrack *ps=(AliTRDtrack*)fSeeds->UncheckedAt(i), &s=*ps;
713     Int_t expectedClr = FollowBackProlongation(s);
714
715     if (IsStoringBarrel()) {
716       StoreBarrelTrack(ps, fgkLastPlane, kTrackBack);
717       fBarrelTree->Fill();        
718     }
719
720     Int_t foundClr = s.GetNumberOfClusters();
721     Int_t lastTB = fTrSec[0]->GetLayerNumber(s.GetX());
722
723     //    printf("seed %d: found %d out of %d expected clusters, Min is %f\n",
724     //     i, foundClr, expectedClr, foundMin);
725
726     if (foundClr >= foundMin) {
727       if(foundClr >= 2) {
728         s.CookdEdx(); 
729         CookLabel(ps, 1-fgkLabelFraction);
730         UseClusters(ps);
731       }
732       
733       // Propagate to outer reference plane [SR, GSI, 18.02.2003]
734       ps->PropagateTo(364.8);
735       otrackTRD=ps;
736       trdTree.Fill();
737       found++;
738 //      cout<<found<<'\r';
739     }
740
741     if(((expectedClr < 10) && (lastTB == outermostTB)) ||
742        ((expectedClr >= 10) && 
743         (((Float_t) foundClr) / ((Float_t) expectedClr) >= 
744          fgkMinFractionOfFoundClusters) && (lastTB == outermostTB))) {
745
746       Double_t xTOF = 375.5;
747     
748       if(PropagateToOuterPlane(s,xTOF)) {
749         AliTPCtrack *pt = new AliTPCtrack(*ps,ps->GetAlpha());
750         otrack = pt;
751         tofTree1.Fill();
752         delete pt;
753
754         xTOF = 381.5;
755     
756         if(PropagateToOuterPlane(s,xTOF)) {
757           AliTPCtrack *pt = new AliTPCtrack(*ps,ps->GetAlpha());
758           otrack = pt;
759           tofTree2.Fill();
760           delete pt;
761
762           Double_t xPHOS = 460.;
763           
764           if(PropagateToOuterPlane(s,xPHOS)) {
765             AliTPCtrack *pt = new AliTPCtrack(*ps,ps->GetAlpha());
766             otrack = pt;
767             phosTree.Fill();
768             delete pt;
769             
770             Double_t xRICH = 490+1.267;
771             
772             if(PropagateToOuterPlane(s,xRICH)) {
773               AliTPCtrack *pt = new AliTPCtrack(*ps,ps->GetAlpha());
774               otrack = pt;
775               richTree.Fill();
776               delete pt;
777             }   
778           }
779         }
780       }      
781     }
782   }
783   
784   
785   out->cd();
786   tofTree1.Write(); 
787   tofTree2.Write(); 
788   phosTree.Write(); 
789   richTree.Write(); 
790   trdTree.Write(); 
791
792   if (IsStoringBarrel()) { // [SR, 03.04.2003]
793     fBarrelFile->cd();
794     fBarrelTree->Write();
795     fBarrelFile->Flush();
796   }
797
798   savedir->cd();  
799   cerr<<"Number of seeds: "<<nseed<<endl;  
800   cerr<<"Number of back propagated TRD tracks: "<<found<<endl;
801
802   UnloadEvent();
803
804   return 0;
805
806 }
807
808 //_____________________________________________________________________________
809 Int_t AliTRDtracker::PropagateBack(AliESD* event) {
810   //
811   // Gets seeds from ESD event. The seeds are AliTPCtrack's found and
812   // backpropagated by the TPC tracker. Each seed is first propagated 
813   // to the TRD, and then its prolongation is searched in the TRD.
814   // If sufficiently long continuation of the track is found in the TRD
815   // the track is updated, otherwise it's stored as originaly defined 
816   // by the TPC tracker.   
817   //  
818
819   Int_t found=0;  
820   Float_t foundMin = 40;
821
822   Int_t n = event->GetNumberOfTracks();
823   for (Int_t i=0; i<n; i++) {
824     AliESDtrack* seed=event->GetTrack(i);
825     ULong_t status=seed->GetStatus();
826     if ( (status & AliESDtrack::kTPCout ) == 0 ) continue;
827     if ( (status & AliESDtrack::kTRDout) != 0 ) continue;
828
829     Int_t lbl = seed->GetLabel();
830     AliTRDtrack *track = new AliTRDtrack(*seed);
831     track->SetSeedLabel(lbl);
832     fNseeds++;
833
834     /*Int_t expectedClr = */FollowBackProlongation(*track);
835
836     Int_t foundClr = track->GetNumberOfClusters();
837     if (foundClr >= foundMin) {
838       if(foundClr >= 2) {
839         track->CookdEdx(); 
840 //      CookLabel(track, 1-fgkLabelFraction);
841         UseClusters(track);
842       }
843       
844       // Propagate to outer reference plane [SR, GSI, 18.02.2003]
845 //      track->PropagateTo(364.8);  why?
846       
847       //seed->UpdateTrackParams(track, AliESDtrack::kTRDout);
848       //found++;
849     }
850
851     //Propagation to the TOF (I.Belikov)
852     Double_t xtof=378.;
853     Double_t c2=track->GetC()*xtof - track->GetEta();
854     if (TMath::Abs(c2)>=0.9999999) continue;
855
856     Double_t ymax=xtof*TMath::Tan(0.5*AliTRDgeometry::GetAlpha());
857     Double_t y=track->GetYat(xtof);
858     if (y > ymax) {
859        if (!track->Rotate(AliTRDgeometry::GetAlpha())) return 1;
860     } else if (y <-ymax) {
861        if (!track->Rotate(-AliTRDgeometry::GetAlpha())) return 1;
862     }
863
864     if (track->PropagateTo(xtof)) {
865        seed->UpdateTrackParams(track, AliESDtrack::kTRDout);
866        found++;
867     }
868     //End of propagation to the TOF
869
870   }
871   
872   cerr<<"Number of seeds: "<<fNseeds<<endl;  
873   cerr<<"Number of back propagated TRD tracks: "<<found<<endl;
874
875   fSeeds->Clear(); fNseeds=0;
876
877   return 0;
878
879 }
880
881 //_____________________________________________________________________________
882 Int_t AliTRDtracker::RefitInward(AliESD* event)
883 {
884   //
885   // Refits tracks within the TRD. The ESD event is expected to contain seeds 
886   // at the outer part of the TRD. 
887   // The tracks are propagated to the innermost time bin 
888   // of the TRD and the ESD event is updated
889   // Origin: Thomas KUHR (Thomas.Kuhr@cern.ch)
890   //
891
892   Int_t timeBins = fTrSec[0]->GetNumberOfTimeBins();
893   Float_t foundMin = fgkMinClustersInTrack * timeBins; 
894   Int_t nseed = 0;
895   Int_t found = 0;
896   Int_t innerTB = fTrSec[0]->GetInnerTimeBin();
897
898   Int_t n = event->GetNumberOfTracks();
899   for (Int_t i=0; i<n; i++) {
900     AliESDtrack* seed=event->GetTrack(i);
901     ULong_t status=seed->GetStatus();
902     if ( (status & AliESDtrack::kTRDout ) == 0 ) continue;
903     if ( (status & AliESDtrack::kTRDin) != 0 ) continue;
904     nseed++;
905
906     AliTRDtrack* seed2 = new AliTRDtrack(*seed);
907     seed2->ResetCovariance(); 
908     AliTRDtrack *pt = new AliTRDtrack(*seed2,seed2->GetAlpha());
909     AliTRDtrack &t=*pt; 
910     FollowProlongation(t, innerTB); 
911     if (t.GetNumberOfClusters() >= foundMin) {
912       UseClusters(&t);
913       CookLabel(pt, 1-fgkLabelFraction);
914       //      t.CookdEdx();
915     }
916     found++;
917 //    cout<<found<<'\r';     
918
919     if(PropagateToTPC(t)) {
920       seed->UpdateTrackParams(pt, AliESDtrack::kTRDrefit);
921     }  
922     delete seed2;
923     delete pt;
924   }     
925
926   cout<<"Number of loaded seeds: "<<nseed<<endl;  
927   cout<<"Number of found tracks from loaded seeds: "<<found<<endl;
928
929   return 0;
930
931 }
932
933
934 //---------------------------------------------------------------------------
935 Int_t AliTRDtracker::FollowProlongation(AliTRDtrack& t, Int_t rf)
936 {
937   // Starting from current position on track=t this function tries
938   // to extrapolate the track up to timeBin=0 and to confirm prolongation
939   // if a close cluster is found. Returns the number of clusters
940   // expected to be found in sensitive layers
941
942   Float_t  wIndex, wTB, wChi2;
943   Float_t  wYrt, wYclosest, wYcorrect, wYwindow;
944   Float_t  wZrt, wZclosest, wZcorrect, wZwindow;
945   Float_t  wPx, wPy, wPz, wC;
946   Double_t px, py, pz;
947   Float_t  wSigmaC2, wSigmaTgl2, wSigmaY2, wSigmaZ2;
948
949   Int_t trackIndex = t.GetLabel();  
950
951   Int_t ns=Int_t(2*TMath::Pi()/AliTRDgeometry::GetAlpha()+0.5);     
952
953   Int_t tryAgain=fMaxGap;
954
955   Double_t alpha=t.GetAlpha();
956   alpha = TVector2::Phi_0_2pi(alpha);
957
958   Int_t s=Int_t(alpha/AliTRDgeometry::GetAlpha())%AliTRDgeometry::kNsect;  
959   Double_t radLength, rho, x, dx, y, ymax, z;
960
961   Int_t expectedNumberOfClusters = 0;
962   Bool_t lookForCluster;
963
964   alpha=AliTRDgeometry::GetAlpha();  // note: change in meaning
965
966  
967   for (Int_t nr=fTrSec[0]->GetLayerNumber(t.GetX()); nr>rf; nr--) { 
968
969     y = t.GetY(); z = t.GetZ();
970
971     // first propagate to the inner surface of the current time bin 
972     fTrSec[s]->GetLayer(nr)->GetPropagationParameters(y,z,dx,rho,radLength,lookForCluster);
973     x = fTrSec[s]->GetLayer(nr)->GetX()-dx/2; y = t.GetY(); z = t.GetZ();
974     if(!t.PropagateTo(x,radLength,rho)) break;
975     y = t.GetY();
976     ymax = x*TMath::Tan(0.5*alpha);
977     if (y > ymax) {
978       s = (s+1) % ns;
979       if (!t.Rotate(alpha)) break;
980       if(!t.PropagateTo(x,radLength,rho)) break;
981     } else if (y <-ymax) {
982       s = (s-1+ns) % ns;                           
983       if (!t.Rotate(-alpha)) break;   
984       if(!t.PropagateTo(x,radLength,rho)) break;
985     } 
986
987     y = t.GetY(); z = t.GetZ();
988
989     // now propagate to the middle plane of the next time bin 
990     fTrSec[s]->GetLayer(nr-1)->GetPropagationParameters(y,z,dx,rho,radLength,lookForCluster);
991     x = fTrSec[s]->GetLayer(nr-1)->GetX(); y = t.GetY(); z = t.GetZ();
992     if(!t.PropagateTo(x,radLength,rho)) break;
993     y = t.GetY();
994     ymax = x*TMath::Tan(0.5*alpha);
995     if (y > ymax) {
996       s = (s+1) % ns;
997       if (!t.Rotate(alpha)) break;
998       if(!t.PropagateTo(x,radLength,rho)) break;
999     } else if (y <-ymax) {
1000       s = (s-1+ns) % ns;                           
1001       if (!t.Rotate(-alpha)) break;   
1002       if(!t.PropagateTo(x,radLength,rho)) break;
1003     } 
1004
1005
1006     if(lookForCluster) {
1007
1008       expectedNumberOfClusters++;       
1009       wIndex = (Float_t) t.GetLabel();
1010       wTB = nr;
1011
1012       AliTRDpropagationLayer& timeBin=*(fTrSec[s]->GetLayer(nr-1));
1013
1014       Double_t sy2=ExpectedSigmaY2(x,t.GetTgl(),t.GetPt());
1015       Double_t sz2=ExpectedSigmaZ2(x,t.GetTgl());
1016
1017       Double_t road;
1018       if((t.GetSigmaY2() + sy2) > 0) road=10.*sqrt(t.GetSigmaY2() + sy2);
1019       else return expectedNumberOfClusters;
1020       
1021       wYrt = (Float_t) y;
1022       wZrt = (Float_t) z;
1023       wYwindow = (Float_t) road;
1024       t.GetPxPyPz(px,py,pz);
1025       wPx = (Float_t) px;
1026       wPy = (Float_t) py;
1027       wPz = (Float_t) pz;
1028       wC  = (Float_t) t.GetC();
1029       wSigmaC2 = (Float_t) t.GetSigmaC2();
1030       wSigmaTgl2    = (Float_t) t.GetSigmaTgl2();
1031       wSigmaY2 = (Float_t) t.GetSigmaY2();
1032       wSigmaZ2 = (Float_t) t.GetSigmaZ2();
1033       wChi2 = -1;            
1034       
1035       if (road>fgkWideRoad) {
1036         if (t.GetNumberOfClusters()>4)
1037           cerr<<t.GetNumberOfClusters()
1038               <<"FindProlongation warning: Too broad road !\n";
1039         return 0;
1040       }             
1041
1042       AliTRDcluster *cl=0;
1043       UInt_t index=0;
1044
1045       Double_t maxChi2=fgkMaxChi2;
1046
1047       wYclosest = 12345678;
1048       wYcorrect = 12345678;
1049       wZclosest = 12345678;
1050       wZcorrect = 12345678;
1051       wZwindow  = TMath::Sqrt(2.25 * 12 * sz2);   
1052
1053       // Find the closest correct cluster for debugging purposes
1054       if (timeBin) {
1055         Float_t minDY = 1000000;
1056         for (Int_t i=0; i<timeBin; i++) {
1057           AliTRDcluster* c=(AliTRDcluster*)(timeBin[i]);
1058           if((c->GetLabel(0) != trackIndex) &&
1059              (c->GetLabel(1) != trackIndex) &&
1060              (c->GetLabel(2) != trackIndex)) continue;
1061           if(TMath::Abs(c->GetY() - y) > minDY) continue;
1062           minDY = TMath::Abs(c->GetY() - y);
1063           wYcorrect = c->GetY();
1064           wZcorrect = c->GetZ();
1065
1066           Double_t h01 = GetTiltFactor(c);
1067           wChi2 = t.GetPredictedChi2(c, h01);
1068         }
1069       }                    
1070
1071       // Now go for the real cluster search
1072
1073       if (timeBin) {
1074
1075         for (Int_t i=timeBin.Find(y-road); i<timeBin; i++) {
1076           AliTRDcluster* c=(AliTRDcluster*)(timeBin[i]);
1077           if (c->GetY() > y+road) break;
1078           if (c->IsUsed() > 0) continue;
1079           if((c->GetZ()-z)*(c->GetZ()-z) > 3 * sz2) continue;
1080
1081           Double_t h01 = GetTiltFactor(c);
1082           Double_t chi2=t.GetPredictedChi2(c,h01);
1083           
1084           if (chi2 > maxChi2) continue;
1085           maxChi2=chi2;
1086           cl=c;
1087           index=timeBin.GetIndex(i);
1088         }               
1089
1090         if(!cl) {
1091
1092           for (Int_t i=timeBin.Find(y-road); i<timeBin; i++) {
1093             AliTRDcluster* c=(AliTRDcluster*)(timeBin[i]);
1094             
1095             if (c->GetY() > y+road) break;
1096             if (c->IsUsed() > 0) continue;
1097             if((c->GetZ()-z)*(c->GetZ()-z) > 12 * sz2) continue;
1098             
1099             Double_t h01 = GetTiltFactor(c);
1100             Double_t chi2=t.GetPredictedChi2(c, h01);
1101             
1102             if (chi2 > maxChi2) continue;
1103             maxChi2=chi2;
1104             cl=c;
1105             index=timeBin.GetIndex(i);
1106           }
1107         }        
1108         
1109
1110         if (cl) {
1111           wYclosest = cl->GetY();
1112           wZclosest = cl->GetZ();
1113           Double_t h01 = GetTiltFactor(cl);
1114
1115           t.SetSampledEdx(cl->GetQ()/dx,t.GetNumberOfClusters()); 
1116           if(!t.Update(cl,maxChi2,index,h01)) {
1117             if(!tryAgain--) return 0;
1118           }  
1119           else tryAgain=fMaxGap;
1120         }
1121         else {
1122           if (tryAgain==0) break; 
1123           tryAgain--;
1124         }
1125
1126         /*
1127         if((((Int_t) wTB)%15 == 0) || (((Int_t) wTB)%15 == 14)) {
1128           
1129           printf(" %f", wIndex);       //1
1130           printf(" %f", wTB);          //2
1131           printf(" %f", wYrt);         //3
1132           printf(" %f", wYclosest);    //4
1133           printf(" %f", wYcorrect);    //5
1134           printf(" %f", wYwindow);     //6
1135           printf(" %f", wZrt);         //7
1136           printf(" %f", wZclosest);    //8
1137           printf(" %f", wZcorrect);    //9
1138           printf(" %f", wZwindow);     //10
1139           printf(" %f", wPx);          //11
1140           printf(" %f", wPy);          //12
1141           printf(" %f", wPz);          //13
1142           printf(" %f", wSigmaC2*1000000);  //14
1143           printf(" %f", wSigmaTgl2*1000);   //15
1144           printf(" %f", wSigmaY2);     //16
1145           //      printf(" %f", wSigmaZ2);     //17
1146           printf(" %f", wChi2);     //17
1147           printf(" %f", wC);           //18
1148           printf("\n");
1149         } 
1150         */                        
1151       }
1152     }  
1153   }
1154   return expectedNumberOfClusters;
1155   
1156   
1157 }                
1158
1159 //___________________________________________________________________
1160
1161 Int_t AliTRDtracker::FollowBackProlongation(AliTRDtrack& t)
1162 {
1163   // Starting from current radial position of track <t> this function
1164   // extrapolates the track up to outer timebin and in the sensitive
1165   // layers confirms prolongation if a close cluster is found. 
1166   // Returns the number of clusters expected to be found in sensitive layers
1167
1168   Float_t  wIndex, wTB, wChi2;
1169   Float_t  wYrt, wYclosest, wYcorrect, wYwindow;
1170   Float_t  wZrt, wZclosest, wZcorrect, wZwindow;
1171   Float_t  wPx, wPy, wPz, wC;
1172   Double_t px, py, pz;
1173   Float_t  wSigmaC2, wSigmaTgl2, wSigmaY2, wSigmaZ2;
1174
1175   Int_t trackIndex = t.GetLabel();  
1176   Int_t tryAgain=fMaxGap;
1177
1178   Double_t alpha=t.GetAlpha();
1179   TVector2::Phi_0_2pi(alpha);
1180
1181   Int_t s;
1182
1183   Int_t outerTB = fTrSec[0]->GetOuterTimeBin();
1184   Double_t radLength, rho, x, dx, y, ymax = 0, z;
1185   Bool_t lookForCluster;
1186
1187   Int_t expectedNumberOfClusters = 0;
1188   x = t.GetX();
1189
1190   alpha=AliTRDgeometry::GetAlpha();  // note: change in meaning
1191
1192   Int_t nRefPlane = fgkFirstPlane;
1193   Bool_t isNewLayer = kFALSE; 
1194
1195   Double_t chi2;
1196   Double_t minDY;
1197
1198   for (Int_t nr=fTrSec[0]->GetLayerNumber(t.GetX()); nr<outerTB+1; nr++) { 
1199     
1200     y = t.GetY(); 
1201     z = t.GetZ();
1202
1203     // first propagate to the outer surface of the current time bin 
1204
1205     s = t.GetSector();
1206     fTrSec[s]->GetLayer(nr)->GetPropagationParameters(y,z,dx,rho,radLength,lookForCluster);
1207     x = fTrSec[s]->GetLayer(nr)->GetX()+dx/2; 
1208     y = t.GetY(); 
1209     z = t.GetZ();
1210
1211     if(!t.PropagateTo(x,radLength,rho)) break;
1212     if (!AdjustSector(&t)) break;
1213     s = t.GetSector();
1214     if (!t.PropagateTo(x,radLength,rho)) break;
1215
1216     y = t.GetY();
1217     z = t.GetZ();
1218
1219     // Barrel Tracks [SR, 04.04.2003]
1220
1221     s = t.GetSector();
1222     if (fTrSec[s]->GetLayer(nr)->IsSensitive() != 
1223         fTrSec[s]->GetLayer(nr+1)->IsSensitive() ) {
1224
1225 //      if (IsStoringBarrel()) StoreBarrelTrack(&t, nRefPlane++, kTrackBack);
1226     }
1227
1228     if (fTrSec[s]->GetLayer(nr-1)->IsSensitive() && 
1229           ! fTrSec[s]->GetLayer(nr)->IsSensitive()) {
1230       isNewLayer = kTRUE;
1231     } else {isNewLayer = kFALSE;}
1232
1233     y = t.GetY();
1234     z = t.GetZ();
1235
1236     // now propagate to the middle plane of the next time bin 
1237     fTrSec[s]->GetLayer(nr+1)->GetPropagationParameters(y,z,dx,rho,radLength,lookForCluster);
1238
1239     x = fTrSec[s]->GetLayer(nr+1)->GetX(); 
1240       if(!t.PropagateTo(x,radLength,rho)) break;
1241     if (!AdjustSector(&t)) break;
1242     s = t.GetSector();
1243       if(!t.PropagateTo(x,radLength,rho)) break;
1244
1245     y = t.GetY();
1246     z = t.GetZ();
1247
1248     if(fVocal) printf("nr+1=%d, x %f, z %f, y %f, ymax %f\n",nr+1,x,z,y,ymax);
1249     //    printf("label %d, pl %d, lookForCluster %d \n",
1250     //     trackIndex, nr+1, lookForCluster);
1251
1252     if(lookForCluster) {
1253       expectedNumberOfClusters++;       
1254
1255       wIndex = (Float_t) t.GetLabel();
1256       wTB = fTrSec[s]->GetLayer(nr+1)->GetTimeBinIndex();
1257
1258       AliTRDpropagationLayer& timeBin=*(fTrSec[s]->GetLayer(nr+1));
1259       Double_t sy2=ExpectedSigmaY2(t.GetX(),t.GetTgl(),t.GetPt());
1260       Double_t sz2=ExpectedSigmaZ2(t.GetX(),t.GetTgl());
1261       if((t.GetSigmaY2() + sy2) < 0) break;
1262       Double_t road = 10.*sqrt(t.GetSigmaY2() + sy2); 
1263       Double_t y=t.GetY(), z=t.GetZ();
1264
1265       wYrt = (Float_t) y;
1266       wZrt = (Float_t) z;
1267       wYwindow = (Float_t) road;
1268       t.GetPxPyPz(px,py,pz);
1269       wPx = (Float_t) px;
1270       wPy = (Float_t) py;
1271       wPz = (Float_t) pz;
1272       wC  = (Float_t) t.GetC();
1273       wSigmaC2 = (Float_t) t.GetSigmaC2();
1274       wSigmaTgl2    = (Float_t) t.GetSigmaTgl2();
1275       wSigmaY2 = (Float_t) t.GetSigmaY2();
1276       wSigmaZ2 = (Float_t) t.GetSigmaZ2();
1277       wChi2 = -1;            
1278       
1279       if (road>fgkWideRoad) {
1280         if (t.GetNumberOfClusters()>4)
1281           cerr<<t.GetNumberOfClusters()
1282               <<"FindProlongation warning: Too broad road !\n";
1283         return 0;
1284       }      
1285
1286       AliTRDcluster *cl=0;
1287       UInt_t index=0;
1288
1289       Double_t maxChi2=fgkMaxChi2;
1290
1291       if (isNewLayer) { 
1292         road = 3 * road;
1293         //sz2 = 3 * sz2;
1294         maxChi2 = 10 * fgkMaxChi2;
1295       }
1296       
1297       if (nRefPlane == fgkFirstPlane) maxChi2 = 20 * fgkMaxChi2; 
1298       if (nRefPlane == fgkFirstPlane+2) maxChi2 = 15 * fgkMaxChi2;
1299       if (t.GetNRotate() > 0) maxChi2 = 3 * maxChi2;
1300       
1301
1302       wYclosest = 12345678;
1303       wYcorrect = 12345678;
1304       wZclosest = 12345678;
1305       wZcorrect = 12345678;
1306       wZwindow  = TMath::Sqrt(2.25 * 12 * sz2);   
1307
1308       // Find the closest correct cluster for debugging purposes
1309       if (timeBin) {
1310         minDY = 1000000;
1311         for (Int_t i=0; i<timeBin; i++) {
1312           AliTRDcluster* c=(AliTRDcluster*)(timeBin[i]);
1313           if((c->GetLabel(0) != trackIndex) &&
1314              (c->GetLabel(1) != trackIndex) &&
1315              (c->GetLabel(2) != trackIndex)) continue;
1316           if(TMath::Abs(c->GetY() - y) > minDY) continue;
1317           //minDY = TMath::Abs(c->GetY() - y);
1318           minDY = c->GetY() - y;
1319           wYcorrect = c->GetY();
1320           wZcorrect = c->GetZ();
1321
1322           Double_t h01 = GetTiltFactor(c);
1323           wChi2 = t.GetPredictedChi2(c, h01);
1324         }
1325       }                    
1326
1327       // Now go for the real cluster search
1328
1329       if (timeBin) {
1330
1331         for (Int_t i=timeBin.Find(y-road); i<timeBin; i++) {
1332           AliTRDcluster* c=(AliTRDcluster*)(timeBin[i]);
1333           if (c->GetY() > y+road) break;
1334           if (c->IsUsed() > 0) continue;
1335           if((c->GetZ()-z)*(c->GetZ()-z) > 3 * sz2) continue;
1336
1337           Double_t h01 = GetTiltFactor(c);
1338           chi2=t.GetPredictedChi2(c,h01);
1339           
1340           if (chi2 > maxChi2) continue;
1341           maxChi2=chi2;
1342           cl=c;
1343           index=timeBin.GetIndex(i);
1344
1345           //check is correct
1346           if((c->GetLabel(0) != trackIndex) &&
1347              (c->GetLabel(1) != trackIndex) &&
1348              (c->GetLabel(2) != trackIndex)) t.AddNWrong();
1349         }               
1350         
1351         if(!cl) {
1352
1353           for (Int_t i=timeBin.Find(y-road); i<timeBin; i++) {
1354             AliTRDcluster* c=(AliTRDcluster*)(timeBin[i]);
1355             
1356             if (c->GetY() > y+road) break;
1357             if (c->IsUsed() > 0) continue;
1358             if((c->GetZ()-z)*(c->GetZ()-z) > 2.25 * 12 * sz2) continue;
1359             
1360             Double_t h01 = GetTiltFactor(c);
1361             chi2=t.GetPredictedChi2(c,h01);
1362             
1363             if (chi2 > maxChi2) continue;
1364             maxChi2=chi2;
1365             cl=c;
1366             index=timeBin.GetIndex(i);
1367           }
1368         }        
1369         
1370         if (cl) {
1371           wYclosest = cl->GetY();
1372           wZclosest = cl->GetZ();
1373
1374           t.SetSampledEdx(cl->GetQ()/dx,t.GetNumberOfClusters()); 
1375           Double_t h01 = GetTiltFactor(cl);
1376           if(!t.Update(cl,maxChi2,index,h01)) {
1377             if(!tryAgain--) return 0;
1378           }  
1379           else tryAgain=fMaxGap;
1380         }
1381         else {
1382           if (tryAgain==0) break; 
1383           tryAgain--;
1384           
1385           //if (minDY < 1000000 && isNewLayer) 
1386             //cout << "\t" << nRefPlane << "\t" << "\t" << t.GetNRotate() <<  "\t" << 
1387             //  road << "\t" << minDY << "\t" << chi2 << "\t" << wChi2 << "\t" << maxChi2 << endl;
1388                                                                      
1389         }
1390
1391         isNewLayer = kFALSE;
1392
1393         /*
1394         if((((Int_t) wTB)%15 == 0) || (((Int_t) wTB)%15 == 14)) {
1395           
1396           printf(" %f", wIndex);       //1
1397           printf(" %f", wTB);          //2
1398           printf(" %f", wYrt);         //3
1399           printf(" %f", wYclosest);    //4
1400           printf(" %f", wYcorrect);    //5
1401           printf(" %f", wYwindow);     //6
1402           printf(" %f", wZrt);         //7
1403           printf(" %f", wZclosest);    //8
1404           printf(" %f", wZcorrect);    //9
1405           printf(" %f", wZwindow);     //10
1406           printf(" %f", wPx);          //11
1407           printf(" %f", wPy);          //12
1408           printf(" %f", wPz);          //13
1409           printf(" %f", wSigmaC2*1000000);  //14
1410           printf(" %f", wSigmaTgl2*1000);   //15
1411           printf(" %f", wSigmaY2);     //16
1412           //      printf(" %f", wSigmaZ2);     //17
1413           printf(" %f", wChi2);     //17
1414           printf(" %f", wC);           //18
1415           printf("\n");
1416         } 
1417         */                        
1418       }
1419     }  
1420   }
1421   return expectedNumberOfClusters;
1422
1423
1424 }         
1425
1426 //---------------------------------------------------------------------------
1427 Int_t AliTRDtracker::Refit(AliTRDtrack& t, Int_t rf)
1428 {
1429   // Starting from current position on track=t this function tries
1430   // to extrapolate the track up to timeBin=0 and to reuse already
1431   // assigned clusters. Returns the number of clusters
1432   // expected to be found in sensitive layers
1433   // get indices of assigned clusters for each layer
1434   // Origin: Thomas KUHR (Thomas.Kuhr@cern.ch)
1435
1436   Int_t iCluster[90];
1437   for (Int_t i = 0; i < 90; i++) iCluster[i] = 0;
1438   for (Int_t i = 0; i < t.GetNumberOfClusters(); i++) {
1439     Int_t index = t.GetClusterIndex(i);
1440     AliTRDcluster *cl=(AliTRDcluster*) GetCluster(index);
1441     if (!cl) continue;
1442     Int_t detector=cl->GetDetector();
1443     Int_t localTimeBin=cl->GetLocalTimeBin();
1444     Int_t sector=fGeom->GetSector(detector);
1445     Int_t plane=fGeom->GetPlane(detector);
1446
1447     Int_t trackingSector = CookSectorIndex(sector);
1448
1449     Int_t gtb = fTrSec[trackingSector]->CookTimeBinIndex(plane,localTimeBin);
1450     if(gtb < 0) continue; 
1451     Int_t layer = fTrSec[trackingSector]->GetLayerNumber(gtb);
1452     iCluster[layer] = index;
1453   }
1454   t.ResetClusters();
1455
1456   Int_t ns=Int_t(2*TMath::Pi()/AliTRDgeometry::GetAlpha()+0.5);     
1457
1458   Double_t alpha=t.GetAlpha();
1459   alpha = TVector2::Phi_0_2pi(alpha);
1460
1461   Int_t s=Int_t(alpha/AliTRDgeometry::GetAlpha())%AliTRDgeometry::kNsect;  
1462   Double_t radLength, rho, x, dx, y, ymax, z;
1463
1464   Int_t expectedNumberOfClusters = 0;
1465   Bool_t lookForCluster;
1466
1467   alpha=AliTRDgeometry::GetAlpha();  // note: change in meaning
1468
1469  
1470   for (Int_t nr=fTrSec[0]->GetLayerNumber(t.GetX()); nr>rf; nr--) { 
1471
1472     y = t.GetY(); z = t.GetZ();
1473
1474     // first propagate to the inner surface of the current time bin 
1475     fTrSec[s]->GetLayer(nr)->GetPropagationParameters(y,z,dx,rho,radLength,lookForCluster);
1476     x = fTrSec[s]->GetLayer(nr)->GetX()-dx/2; y = t.GetY(); z = t.GetZ();
1477     if(!t.PropagateTo(x,radLength,rho)) break;
1478     y = t.GetY();
1479     ymax = x*TMath::Tan(0.5*alpha);
1480     if (y > ymax) {
1481       s = (s+1) % ns;
1482       if (!t.Rotate(alpha)) break;
1483       if(!t.PropagateTo(x,radLength,rho)) break;
1484     } else if (y <-ymax) {
1485       s = (s-1+ns) % ns;                           
1486       if (!t.Rotate(-alpha)) break;   
1487       if(!t.PropagateTo(x,radLength,rho)) break;
1488     } 
1489
1490     y = t.GetY(); z = t.GetZ();
1491
1492     // now propagate to the middle plane of the next time bin 
1493     fTrSec[s]->GetLayer(nr-1)->GetPropagationParameters(y,z,dx,rho,radLength,lookForCluster);
1494     x = fTrSec[s]->GetLayer(nr-1)->GetX(); y = t.GetY(); z = t.GetZ();
1495     if(!t.PropagateTo(x,radLength,rho)) break;
1496     y = t.GetY();
1497     ymax = x*TMath::Tan(0.5*alpha);
1498     if (y > ymax) {
1499       s = (s+1) % ns;
1500       if (!t.Rotate(alpha)) break;
1501       if(!t.PropagateTo(x,radLength,rho)) break;
1502     } else if (y <-ymax) {
1503       s = (s-1+ns) % ns;                           
1504       if (!t.Rotate(-alpha)) break;   
1505       if(!t.PropagateTo(x,radLength,rho)) break;
1506     } 
1507
1508     if(lookForCluster) expectedNumberOfClusters++;       
1509
1510     // use assigned cluster
1511     if (!iCluster[nr-1]) continue;
1512     AliTRDcluster *cl=(AliTRDcluster*)GetCluster(iCluster[nr-1]);
1513     Double_t h01 = GetTiltFactor(cl);
1514     Double_t chi2=t.GetPredictedChi2(cl, h01);
1515     t.SetSampledEdx(cl->GetQ()/dx,t.GetNumberOfClusters()); 
1516     t.Update(cl,chi2,iCluster[nr-1],h01);
1517   }
1518
1519   return expectedNumberOfClusters;
1520 }                
1521
1522 //___________________________________________________________________
1523
1524 Int_t AliTRDtracker::PropagateToOuterPlane(AliTRDtrack& t, Double_t xToGo)
1525 {
1526   // Starting from current radial position of track <t> this function
1527   // extrapolates the track up to radial position <xToGo>. 
1528   // Returns 1 if track reaches the plane, and 0 otherwise 
1529
1530   Int_t ns=Int_t(2*TMath::Pi()/AliTRDgeometry::GetAlpha()+0.5);     
1531
1532   Double_t alpha=t.GetAlpha();
1533
1534   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
1535   if (alpha < 0.            ) alpha += 2.*TMath::Pi();
1536
1537   Int_t s=Int_t(alpha/AliTRDgeometry::GetAlpha())%AliTRDgeometry::kNsect;  
1538
1539   Bool_t lookForCluster;
1540   Double_t radLength, rho, x, dx, y, ymax, z;
1541
1542   x = t.GetX();
1543
1544   alpha=AliTRDgeometry::GetAlpha();  // note: change in meaning
1545
1546   Int_t plToGo = fTrSec[0]->GetLayerNumber(xToGo);
1547
1548   for (Int_t nr=fTrSec[0]->GetLayerNumber(x); nr<plToGo; nr++) { 
1549
1550     y = t.GetY(); z = t.GetZ();
1551
1552     // first propagate to the outer surface of the current time bin 
1553     fTrSec[s]->GetLayer(nr)->GetPropagationParameters(y,z,dx,rho,radLength,lookForCluster);
1554     x = fTrSec[s]->GetLayer(nr)->GetX()+dx/2; y = t.GetY(); z = t.GetZ();
1555     if(!t.PropagateTo(x,radLength,rho)) return 0;
1556     y = t.GetY();
1557     ymax = x*TMath::Tan(0.5*alpha);
1558     if (y > ymax) {
1559       s = (s+1) % ns;
1560       if (!t.Rotate(alpha)) return 0;
1561     } else if (y <-ymax) {
1562       s = (s-1+ns) % ns;                           
1563       if (!t.Rotate(-alpha)) return 0;   
1564     } 
1565     if(!t.PropagateTo(x,radLength,rho)) return 0;
1566
1567     y = t.GetY(); z = t.GetZ();
1568
1569     // now propagate to the middle plane of the next time bin 
1570     fTrSec[s]->GetLayer(nr+1)->GetPropagationParameters(y,z,dx,rho,radLength,lookForCluster);
1571     x = fTrSec[s]->GetLayer(nr+1)->GetX(); y = t.GetY(); z = t.GetZ();
1572     if(!t.PropagateTo(x,radLength,rho)) return 0;
1573     y = t.GetY();
1574     ymax = x*TMath::Tan(0.5*alpha);
1575     if (y > ymax) {
1576       s = (s+1) % ns;
1577       if (!t.Rotate(alpha)) return 0;
1578     } else if (y <-ymax) {
1579       s = (s-1+ns) % ns;                           
1580       if (!t.Rotate(-alpha)) return 0;   
1581     } 
1582     if(!t.PropagateTo(x,radLength,rho)) return 0;
1583   }
1584   return 1;
1585 }         
1586
1587 //___________________________________________________________________
1588
1589 Int_t AliTRDtracker::PropagateToTPC(AliTRDtrack& t)
1590 {
1591   // Starting from current radial position of track <t> this function
1592   // extrapolates the track up to radial position of the outermost
1593   // padrow of the TPC. 
1594   // Returns 1 if track reaches the TPC, and 0 otherwise 
1595
1596   //Int_t ns=Int_t(2*TMath::Pi()/AliTRDgeometry::GetAlpha()+0.5);     
1597
1598   Double_t alpha=t.GetAlpha();
1599   alpha = TVector2::Phi_0_2pi(alpha);
1600
1601   Int_t s=Int_t(alpha/AliTRDgeometry::GetAlpha())%AliTRDgeometry::kNsect;  
1602
1603   Bool_t lookForCluster;
1604   Double_t radLength, rho, x, dx, y, /*ymax,*/ z;
1605
1606   x = t.GetX();
1607
1608   alpha=AliTRDgeometry::GetAlpha();  // note: change in meaning
1609   Int_t plTPC = fTrSec[0]->GetLayerNumber(246.055);
1610
1611   for (Int_t nr=fTrSec[0]->GetLayerNumber(x); nr>plTPC; nr--) { 
1612
1613     y = t.GetY(); 
1614     z = t.GetZ();
1615
1616     // first propagate to the outer surface of the current time bin 
1617     fTrSec[s]->GetLayer(nr)->GetPropagationParameters(y,z,dx,rho,radLength,lookForCluster);
1618     x = fTrSec[s]->GetLayer(nr)->GetX()-dx/2; 
1619     
1620     if(!t.PropagateTo(x,radLength,rho)) return 0;
1621     AdjustSector(&t);
1622     if(!t.PropagateTo(x,radLength,rho)) return 0;
1623
1624     y = t.GetY(); 
1625     z = t.GetZ();
1626
1627     // now propagate to the middle plane of the next time bin 
1628     fTrSec[s]->GetLayer(nr-1)->GetPropagationParameters(y,z,dx,rho,radLength,lookForCluster);
1629     x = fTrSec[s]->GetLayer(nr-1)->GetX(); 
1630     
1631     if(!t.PropagateTo(x,radLength,rho)) return 0;
1632     AdjustSector(&t);
1633     if(!t.PropagateTo(x,radLength,rho)) return 0;
1634   }
1635   return 1;
1636 }         
1637
1638 void AliTRDtracker::LoadEvent()
1639 {
1640   // Fills clusters into TRD tracking_sectors 
1641   // Note that the numbering scheme for the TRD tracking_sectors 
1642   // differs from that of TRD sectors
1643
1644   ReadClusters(fClusters);
1645   Int_t ncl=fClusters->GetEntriesFast();
1646   cout<<"\n LoadSectors: sorting "<<ncl<<" clusters"<<endl;
1647               
1648   UInt_t index;
1649   while (ncl--) {
1650 //    printf("\r %d left  ",ncl); 
1651     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(ncl);
1652     Int_t detector=c->GetDetector();
1653     Int_t localTimeBin=c->GetLocalTimeBin();
1654     Int_t sector=fGeom->GetSector(detector);
1655     Int_t plane=fGeom->GetPlane(detector);
1656
1657     Int_t trackingSector = CookSectorIndex(sector);
1658
1659     Int_t gtb = fTrSec[trackingSector]->CookTimeBinIndex(plane,localTimeBin);
1660     if(gtb < 0) continue; 
1661     Int_t layer = fTrSec[trackingSector]->GetLayerNumber(gtb);
1662
1663     index=ncl;
1664     fTrSec[trackingSector]->GetLayer(layer)->InsertCluster(c,index);
1665   }    
1666   printf("\r\n");
1667
1668 }
1669
1670 //_____________________________________________________________________________
1671 Int_t AliTRDtracker::LoadClusters(TTree *cTree)
1672 {
1673   // Fills clusters into TRD tracking_sectors 
1674   // Note that the numbering scheme for the TRD tracking_sectors 
1675   // differs from that of TRD sectors
1676
1677   if (ReadClusters(fClusters,cTree)) {
1678      Error("LoadClusters","Problem with reading the clusters !");
1679      return 1;
1680   }
1681   Int_t ncl=fClusters->GetEntriesFast();
1682   cout<<"\n LoadSectors: sorting "<<ncl<<" clusters"<<endl;
1683               
1684   UInt_t index;
1685   while (ncl--) {
1686 //    printf("\r %d left  ",ncl); 
1687     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(ncl);
1688     Int_t detector=c->GetDetector();
1689     Int_t localTimeBin=c->GetLocalTimeBin();
1690     Int_t sector=fGeom->GetSector(detector);
1691     Int_t plane=fGeom->GetPlane(detector);
1692
1693     Int_t trackingSector = CookSectorIndex(sector);
1694
1695     Int_t gtb = fTrSec[trackingSector]->CookTimeBinIndex(plane,localTimeBin);
1696     if(gtb < 0) continue; 
1697     Int_t layer = fTrSec[trackingSector]->GetLayerNumber(gtb);
1698
1699     index=ncl;
1700     fTrSec[trackingSector]->GetLayer(layer)->InsertCluster(c,index);
1701   }    
1702   printf("\r\n");
1703
1704   return 0;
1705 }
1706
1707 //_____________________________________________________________________________
1708 void AliTRDtracker::UnloadEvent() 
1709
1710   //
1711   // Clears the arrays of clusters and tracks. Resets sectors and timebins 
1712   //
1713
1714   Int_t i, nentr;
1715
1716   nentr = fClusters->GetEntriesFast();
1717   for (i = 0; i < nentr; i++) delete fClusters->RemoveAt(i);
1718
1719   nentr = fSeeds->GetEntriesFast();
1720   for (i = 0; i < nentr; i++) delete fSeeds->RemoveAt(i);
1721
1722   nentr = fTracks->GetEntriesFast();
1723   for (i = 0; i < nentr; i++) delete fTracks->RemoveAt(i);
1724
1725   Int_t nsec = AliTRDgeometry::kNsect;
1726
1727   for (i = 0; i < nsec; i++) {    
1728     for(Int_t pl = 0; pl < fTrSec[i]->GetNumberOfLayers(); pl++) {
1729       fTrSec[i]->GetLayer(pl)->Clear();
1730     }
1731   }
1732
1733 }
1734
1735 //__________________________________________________________________________
1736 void AliTRDtracker::MakeSeeds(Int_t inner, Int_t outer, Int_t turn)
1737 {
1738   // Creates track seeds using clusters in timeBins=i1,i2
1739
1740   if(turn > 2) {
1741     cerr<<"MakeSeeds: turn "<<turn<<" exceeds the limit of 2"<<endl;
1742     return;
1743   }
1744
1745   Double_t x[5], c[15];
1746   Int_t maxSec=AliTRDgeometry::kNsect;
1747   
1748   Double_t alpha=AliTRDgeometry::GetAlpha();
1749   Double_t shift=AliTRDgeometry::GetAlpha()/2.;
1750   Double_t cs=cos(alpha), sn=sin(alpha);
1751   Double_t cs2=cos(2.*alpha), sn2=sin(2.*alpha);
1752     
1753       
1754   Int_t i2 = fTrSec[0]->GetLayerNumber(inner);
1755   Int_t i1 = fTrSec[0]->GetLayerNumber(outer);
1756       
1757   Double_t x1 =fTrSec[0]->GetX(i1);
1758   Double_t xx2=fTrSec[0]->GetX(i2);
1759       
1760   for (Int_t ns=0; ns<maxSec; ns++) {
1761     
1762     Int_t nl2 = *(fTrSec[(ns-2+maxSec)%maxSec]->GetLayer(i2));
1763     Int_t nl=(*fTrSec[(ns-1+maxSec)%maxSec]->GetLayer(i2));
1764     Int_t nm=(*fTrSec[ns]->GetLayer(i2));
1765     Int_t nu=(*fTrSec[(ns+1)%maxSec]->GetLayer(i2));
1766     Int_t nu2=(*fTrSec[(ns+2)%maxSec]->GetLayer(i2));
1767     
1768     AliTRDpropagationLayer& r1=*(fTrSec[ns]->GetLayer(i1));
1769     
1770     for (Int_t is=0; is < r1; is++) {
1771       Double_t y1=r1[is]->GetY(), z1=r1[is]->GetZ();
1772       
1773       for (Int_t js=0; js < nl2+nl+nm+nu+nu2; js++) {
1774         
1775         const AliTRDcluster *cl;
1776         Double_t x2,   y2,   z2;
1777         Double_t x3=0., y3=0.;   
1778         
1779         if (js<nl2) {
1780           if(turn != 2) continue;
1781           AliTRDpropagationLayer& r2=*(fTrSec[(ns-2+maxSec)%maxSec]->GetLayer(i2));
1782           cl=r2[js];
1783           y2=cl->GetY(); z2=cl->GetZ();
1784           
1785           x2= xx2*cs2+y2*sn2;
1786           y2=-xx2*sn2+y2*cs2;
1787         }
1788         else if (js<nl2+nl) {
1789           if(turn != 1) continue;
1790           AliTRDpropagationLayer& r2=*(fTrSec[(ns-1+maxSec)%maxSec]->GetLayer(i2));
1791           cl=r2[js-nl2];
1792           y2=cl->GetY(); z2=cl->GetZ();
1793           
1794           x2= xx2*cs+y2*sn;
1795           y2=-xx2*sn+y2*cs;
1796         }                                
1797         else if (js<nl2+nl+nm) {
1798           if(turn != 1) continue;
1799           AliTRDpropagationLayer& r2=*(fTrSec[ns]->GetLayer(i2));
1800           cl=r2[js-nl2-nl];
1801           x2=xx2; y2=cl->GetY(); z2=cl->GetZ();
1802         }
1803         else if (js<nl2+nl+nm+nu) {
1804           if(turn != 1) continue;
1805           AliTRDpropagationLayer& r2=*(fTrSec[(ns+1)%maxSec]->GetLayer(i2));
1806           cl=r2[js-nl2-nl-nm];
1807           y2=cl->GetY(); z2=cl->GetZ();
1808           
1809           x2=xx2*cs-y2*sn;
1810           y2=xx2*sn+y2*cs;
1811         }              
1812         else {
1813           if(turn != 2) continue;
1814           AliTRDpropagationLayer& r2=*(fTrSec[(ns+2)%maxSec]->GetLayer(i2));
1815           cl=r2[js-nl2-nl-nm-nu];
1816           y2=cl->GetY(); z2=cl->GetZ();
1817           
1818           x2=xx2*cs2-y2*sn2;
1819           y2=xx2*sn2+y2*cs2;
1820         }
1821         
1822         if(TMath::Abs(z1-z2) > fgkMaxSeedDeltaZ12) continue;
1823         
1824         Double_t zz=z1 - z1/x1*(x1-x2);
1825         
1826         if (TMath::Abs(zz-z2)>fgkMaxSeedDeltaZ) continue;
1827         
1828         Double_t d=(x2-x1)*(0.-y2)-(0.-x2)*(y2-y1);
1829         if (d==0.) {cerr<<"TRD MakeSeeds: Straight seed !\n"; continue;}
1830         
1831         x[0]=y1;
1832         x[1]=z1;
1833         x[4]=f1trd(x1,y1,x2,y2,x3,y3);
1834         
1835         if (TMath::Abs(x[4]) > fgkMaxSeedC) continue;      
1836         
1837         x[2]=f2trd(x1,y1,x2,y2,x3,y3);
1838         
1839         if (TMath::Abs(x[4]*x1-x[2]) >= 0.99999) continue;
1840         
1841         x[3]=f3trd(x1,y1,x2,y2,z1,z2);
1842         
1843         if (TMath::Abs(x[3]) > fgkMaxSeedTan) continue;
1844         
1845         Double_t a=asin(x[2]);
1846         Double_t zv=z1 - x[3]/x[4]*(a+asin(x[4]*x1-x[2]));
1847         
1848         if (TMath::Abs(zv)>fgkMaxSeedVertexZ) continue;
1849         
1850         Double_t sy1=r1[is]->GetSigmaY2(), sz1=r1[is]->GetSigmaZ2();
1851         Double_t sy2=cl->GetSigmaY2(),     sz2=cl->GetSigmaZ2();
1852         Double_t sy3=fgkSeedErrorSY3, sy=fgkSeedErrorSY, sz=fgkSeedErrorSZ;  
1853
1854         // Tilt changes
1855         Double_t h01 = GetTiltFactor(r1[is]);
1856         Double_t xuFactor = 100.;
1857         if(fNoTilt) { 
1858           h01 = 0;
1859           xuFactor = 1;
1860         }
1861
1862         sy1=sy1+sz1*h01*h01;
1863         Double_t syz=sz1*(-h01);
1864         // end of tilt changes
1865         
1866         Double_t f40=(f1trd(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
1867         Double_t f42=(f1trd(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
1868         Double_t f43=(f1trd(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
1869         Double_t f20=(f2trd(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
1870         Double_t f22=(f2trd(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
1871         Double_t f23=(f2trd(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
1872         Double_t f30=(f3trd(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
1873         Double_t f31=(f3trd(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
1874         Double_t f32=(f3trd(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
1875         Double_t f34=(f3trd(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;    
1876
1877         
1878         c[0]=sy1;
1879         //        c[1]=0.;       c[2]=sz1;
1880         c[1]=syz;       c[2]=sz1*xuFactor;
1881         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
1882         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
1883                        c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
1884         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
1885         c[13]=f30*sy1*f40+f32*sy2*f42;
1886         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;      
1887         
1888         UInt_t index=r1.GetIndex(is);
1889         
1890         AliTRDtrack *track=new AliTRDtrack(r1[is],index,x,c,x1,ns*alpha+shift);
1891
1892         Int_t rc=FollowProlongation(*track, i2);     
1893         
1894         if ((rc < 1) ||
1895             (track->GetNumberOfClusters() < 
1896              (outer-inner)*fgkMinClustersInSeed)) delete track;
1897         else {
1898           fSeeds->AddLast(track); fNseeds++;
1899 //          cerr<<"\r found seed "<<fNseeds;
1900         }
1901       }
1902     }
1903   }
1904 }            
1905
1906 //_____________________________________________________________________________
1907 Int_t AliTRDtracker::ReadClusters(TObjArray *array, TTree *ClusterTree) 
1908 {
1909   //
1910   // Reads AliTRDclusters (option >= 0) or AliTRDrecPoints (option < 0) 
1911   // from the file. The names of the cluster tree and branches 
1912   // should match the ones used in AliTRDclusterizer::WriteClusters()
1913   //
1914   TObjArray *clusterArray = new TObjArray(400); 
1915   
1916   TBranch *branch=ClusterTree->GetBranch("TRDcluster");
1917   if (!branch) {
1918     Error("ReadClusters","Can't get the branch !");
1919     return 1;
1920   }
1921   branch->SetAddress(&clusterArray); 
1922   
1923   Int_t nEntries = (Int_t) ClusterTree->GetEntries();
1924   printf("found %d entries in %s.\n",nEntries,ClusterTree->GetName());
1925   
1926   // Loop through all entries in the tree
1927   Int_t nbytes;
1928   AliTRDcluster *c = 0;
1929   printf("\n");
1930
1931   for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {    
1932     
1933     // Import the tree
1934     nbytes += ClusterTree->GetEvent(iEntry);  
1935     
1936     // Get the number of points in the detector
1937     Int_t nCluster = clusterArray->GetEntriesFast();  
1938 //    printf("\r Read %d clusters from entry %d", nCluster, iEntry);
1939     
1940     // Loop through all TRD digits
1941     for (Int_t iCluster = 0; iCluster < nCluster; iCluster++) { 
1942       c = (AliTRDcluster*)clusterArray->UncheckedAt(iCluster);
1943       AliTRDcluster *co = new AliTRDcluster(*c);
1944       co->SetSigmaY2(c->GetSigmaY2() * fSY2corr);
1945       Int_t ltb = co->GetLocalTimeBin();
1946       if(ltb == 19) co->SetSigmaZ2(c->GetSigmaZ2());
1947       else if(fNoTilt) co->SetSigmaZ2(c->GetSigmaZ2() * fSZ2corr);
1948       array->AddLast(co);
1949       delete clusterArray->RemoveAt(iCluster); 
1950     }
1951   }
1952
1953   delete clusterArray;
1954
1955   return 0;
1956 }
1957
1958 //______________________________________________________________________
1959 void AliTRDtracker::ReadClusters(TObjArray *array, const Char_t *filename)
1960 {
1961   //
1962   // Reads AliTRDclusters from file <filename>. The names of the cluster
1963   // tree and branches should match the ones used in
1964   // AliTRDclusterizer::WriteClusters()
1965   // if <array> == 0, clusters are added into AliTRDtracker fCluster array
1966   //
1967
1968   TDirectory *savedir=gDirectory;
1969
1970   TFile *file = TFile::Open(filename);
1971   if (!file->IsOpen()) {
1972     cerr<<"Can't open file with TRD clusters"<<endl;
1973     return;
1974   }
1975
1976   Char_t treeName[12];
1977   sprintf(treeName,"TreeR%d_TRD",GetEventNumber());
1978   TTree *clusterTree = (TTree*) gDirectory->Get(treeName);
1979
1980   if (!clusterTree) {
1981      cerr<<"AliTRDtracker::ReadClusters(): ";
1982      cerr<<"can't get a tree with clusters !\n";
1983      return;
1984   }
1985
1986   TObjArray *clusterArray = new TObjArray(400);
1987
1988   clusterTree->GetBranch("TRDcluster")->SetAddress(&clusterArray);
1989
1990   Int_t nEntries = (Int_t) clusterTree->GetEntries();
1991   cout<<"found "<<nEntries<<" in clusterTree"<<endl;   
1992
1993   // Loop through all entries in the tree
1994   Int_t nbytes;
1995   AliTRDcluster *c = 0;
1996
1997   printf("\n");
1998
1999   for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
2000
2001     // Import the tree
2002     nbytes += clusterTree->GetEvent(iEntry);
2003
2004     // Get the number of points in the detector
2005     Int_t nCluster = clusterArray->GetEntriesFast();
2006     printf("\n Read %d clusters from entry %d", nCluster, iEntry);
2007
2008     // Loop through all TRD digits
2009     for (Int_t iCluster = 0; iCluster < nCluster; iCluster++) {
2010       c = (AliTRDcluster*)clusterArray->UncheckedAt(iCluster);
2011       AliTRDcluster *co = new AliTRDcluster(*c);
2012       co->SetSigmaY2(c->GetSigmaY2() * fSY2corr);
2013       Int_t ltb = co->GetLocalTimeBin();
2014       if(ltb == 19) co->SetSigmaZ2(c->GetSigmaZ2());
2015       else if(fNoTilt) co->SetSigmaZ2(c->GetSigmaZ2() * fSZ2corr);
2016       array->AddLast(co);
2017       delete clusterArray->RemoveAt(iCluster);
2018     }
2019   }
2020
2021   file->Close();
2022   delete clusterArray;
2023   savedir->cd();
2024
2025 }                      
2026
2027 void AliTRDtracker::ReadClusters(TObjArray *array, const TFile *inp) 
2028 {
2029   //
2030   // Reads AliTRDclusters (option >= 0) or AliTRDrecPoints (option < 0) 
2031   // from the file. The names of the cluster tree and branches 
2032   // should match the ones used in AliTRDclusterizer::WriteClusters()
2033   //
2034
2035   TDirectory *savedir=gDirectory; 
2036
2037   if (inp) {
2038      TFile *in=(TFile*)inp;
2039      if (!in->IsOpen()) {
2040         cerr<<"AliTRDtracker::ReadClusters(): input file is not open !\n";
2041         return;
2042      }
2043      else{
2044        in->cd();
2045      }
2046   }
2047
2048   Char_t treeName[12];
2049   sprintf(treeName,"TreeR%d_TRD",GetEventNumber());
2050   TTree *clusterTree = (TTree*) gDirectory->Get(treeName);
2051   
2052   TObjArray *clusterArray = new TObjArray(400); 
2053   
2054   clusterTree->GetBranch("TRDcluster")->SetAddress(&clusterArray); 
2055   
2056   Int_t nEntries = (Int_t) clusterTree->GetEntries();
2057   printf("found %d entries in %s.\n",nEntries,clusterTree->GetName());
2058   
2059   // Loop through all entries in the tree
2060   Int_t nbytes;
2061   AliTRDcluster *c = 0;
2062   printf("\n");
2063
2064   for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {    
2065     
2066     // Import the tree
2067     nbytes += clusterTree->GetEvent(iEntry);  
2068     
2069     // Get the number of points in the detector
2070     Int_t nCluster = clusterArray->GetEntriesFast();  
2071 //    printf("\r Read %d clusters from entry %d", nCluster, iEntry);
2072     
2073     // Loop through all TRD digits
2074     for (Int_t iCluster = 0; iCluster < nCluster; iCluster++) { 
2075       c = (AliTRDcluster*)clusterArray->UncheckedAt(iCluster);
2076       AliTRDcluster *co = new AliTRDcluster(*c);
2077       co->SetSigmaY2(c->GetSigmaY2() * fSY2corr);
2078       Int_t ltb = co->GetLocalTimeBin();
2079       if(ltb == 19) co->SetSigmaZ2(c->GetSigmaZ2());
2080       else if(fNoTilt) co->SetSigmaZ2(c->GetSigmaZ2() * fSZ2corr);
2081       array->AddLast(co);
2082       delete clusterArray->RemoveAt(iCluster); 
2083     }
2084   }
2085
2086   delete clusterArray;
2087   savedir->cd();   
2088
2089 }
2090
2091 //__________________________________________________________________
2092 void AliTRDtracker::CookLabel(AliKalmanTrack* pt, Float_t wrong) const 
2093 {
2094   //
2095   // This cooks a label. Mmmmh, smells good...
2096   //
2097
2098   Int_t label=123456789, index, i, j;
2099   Int_t ncl=pt->GetNumberOfClusters();
2100   const Int_t kRange = fTrSec[0]->GetOuterTimeBin()+1;
2101
2102   Bool_t labelAdded;
2103
2104   //  Int_t s[kRange][2];
2105   Int_t **s = new Int_t* [kRange];
2106   for (i=0; i<kRange; i++) {
2107     s[i] = new Int_t[2];
2108   }
2109   for (i=0; i<kRange; i++) {
2110     s[i][0]=-1;
2111     s[i][1]=0;
2112   }
2113
2114   Int_t t0,t1,t2;
2115   for (i=0; i<ncl; i++) {
2116     index=pt->GetClusterIndex(i);
2117     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(index);
2118     t0=c->GetLabel(0);
2119     t1=c->GetLabel(1);
2120     t2=c->GetLabel(2);
2121   }
2122
2123   for (i=0; i<ncl; i++) {
2124     index=pt->GetClusterIndex(i);
2125     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(index);
2126     for (Int_t k=0; k<3; k++) { 
2127       label=c->GetLabel(k);
2128       labelAdded=kFALSE; j=0;
2129       if (label >= 0) {
2130         while ( (!labelAdded) && ( j < kRange ) ) {
2131           if (s[j][0]==label || s[j][1]==0) {
2132             s[j][0]=label; 
2133             s[j][1]=s[j][1]+1; 
2134             labelAdded=kTRUE;
2135           }
2136           j++;
2137         }
2138       }
2139     }
2140   }
2141
2142   Int_t max=0;
2143   label = -123456789;
2144
2145   for (i=0; i<kRange; i++) {
2146     if (s[i][1]>max) {
2147       max=s[i][1]; label=s[i][0];
2148     }
2149   }
2150
2151   for (i=0; i<kRange; i++) {
2152     delete []s[i];
2153   }        
2154
2155   delete []s;
2156
2157   if ((1.- Float_t(max)/ncl) > wrong) label=-label;   
2158
2159   pt->SetLabel(label); 
2160
2161 }
2162
2163
2164 //__________________________________________________________________
2165 void AliTRDtracker::UseClusters(const AliKalmanTrack* t, Int_t from) const 
2166 {
2167   //
2168   // Use clusters, but don't abuse them!
2169   //
2170
2171   Int_t ncl=t->GetNumberOfClusters();
2172   for (Int_t i=from; i<ncl; i++) {
2173     Int_t index = t->GetClusterIndex(i);
2174     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(index);
2175     c->Use();
2176   }
2177 }
2178
2179
2180 //_____________________________________________________________________
2181 Double_t AliTRDtracker::ExpectedSigmaY2(Double_t , Double_t , Double_t ) const
2182 {
2183   // Parametrised "expected" error of the cluster reconstruction in Y 
2184
2185   Double_t s = 0.08 * 0.08;    
2186   return s;
2187 }
2188
2189 //_____________________________________________________________________
2190 Double_t AliTRDtracker::ExpectedSigmaZ2(Double_t , Double_t ) const
2191 {
2192   // Parametrised "expected" error of the cluster reconstruction in Z 
2193
2194   Double_t s = 9 * 9 /12.;  
2195   return s;
2196 }                  
2197
2198 //_____________________________________________________________________
2199 Double_t AliTRDtracker::GetX(Int_t sector, Int_t plane, Int_t localTB) const 
2200 {
2201   //
2202   // Returns radial position which corresponds to time bin <localTB>
2203   // in tracking sector <sector> and plane <plane>
2204   //
2205
2206   Int_t index = fTrSec[sector]->CookTimeBinIndex(plane, localTB); 
2207   Int_t pl = fTrSec[sector]->GetLayerNumber(index);
2208   return fTrSec[sector]->GetLayer(pl)->GetX();
2209
2210 }
2211
2212
2213 //_______________________________________________________
2214 AliTRDtracker::AliTRDpropagationLayer::AliTRDpropagationLayer(Double_t x, 
2215                Double_t dx, Double_t rho, Double_t radLength, Int_t tbIndex)
2216
2217   //
2218   // AliTRDpropagationLayer constructor
2219   //
2220
2221   fN = 0; fX = x; fdX = dx; fRho = rho; fX0 = radLength;
2222   fClusters = NULL; fIndex = NULL; fTimeBinIndex = tbIndex;
2223
2224
2225   for(Int_t i=0; i < (Int_t) kZones; i++) {
2226     fZc[i]=0; fZmax[i] = 0;
2227   }
2228
2229   fYmax = 0;
2230
2231   if(fTimeBinIndex >= 0) { 
2232     fClusters = new AliTRDcluster*[kMaxClusterPerTimeBin];
2233     fIndex = new UInt_t[kMaxClusterPerTimeBin];
2234   }
2235
2236   fHole = kFALSE;
2237   fHoleZc = 0;
2238   fHoleZmax = 0;
2239   fHoleYc = 0;
2240   fHoleYmax = 0;
2241   fHoleRho = 0;
2242   fHoleX0 = 0;
2243
2244 }
2245
2246 //_______________________________________________________
2247 void AliTRDtracker::AliTRDpropagationLayer::SetHole(
2248           Double_t Zmax, Double_t Ymax, Double_t rho, 
2249           Double_t radLength, Double_t Yc, Double_t Zc) 
2250 {
2251   //
2252   // Sets hole in the layer 
2253   //
2254
2255   fHole = kTRUE;
2256   fHoleZc = Zc;
2257   fHoleZmax = Zmax;
2258   fHoleYc = Yc;
2259   fHoleYmax = Ymax;
2260   fHoleRho = rho;
2261   fHoleX0 = radLength;
2262 }
2263   
2264
2265 //_______________________________________________________
2266 AliTRDtracker::AliTRDtrackingSector::AliTRDtrackingSector(AliTRDgeometry* geo, Int_t gs, AliTRDparameter* par)
2267 {
2268   //
2269   // AliTRDtrackingSector Constructor
2270   //
2271
2272   fGeom = geo;
2273   fPar = par;
2274   fGeomSector = gs;
2275   fTzeroShift = 0.13;
2276   fN = 0;
2277
2278   for(UInt_t i=0; i < kMaxTimeBinIndex; i++) fTimeBinIndex[i] = -1;
2279
2280
2281   AliTRDpropagationLayer* ppl;
2282
2283   Double_t x, xin, xout, dx, rho, radLength;
2284   Int_t    steps;
2285
2286   // set time bins in the gas of the TPC
2287
2288   xin = 246.055; xout = 254.055; steps = 20; dx = (xout-xin)/steps;
2289   rho = 0.9e-3;  radLength = 28.94;
2290
2291   for(Int_t i=0; i<steps; i++) {
2292     x = xin + i*dx + dx/2;
2293     ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,-1);
2294     InsertLayer(ppl);
2295   }
2296
2297   // set time bins in the outer field cage vessel
2298
2299   dx = 50e-4; xin = xout; xout = xin + dx; rho = 1.71; radLength = 44.77; // Tedlar
2300   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,radLength,-1);
2301   InsertLayer(ppl);
2302
2303   dx = 0.02; xin = xout; xout = xin + dx; rho = 1.45; radLength = 44.86; // prepreg
2304   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,radLength,-1);
2305   InsertLayer(ppl);
2306
2307   dx = 2.; xin = xout; xout = xin + dx; rho = 1.45*0.02; radLength = 41.28; // Nomex
2308   steps = 5; dx = (xout - xin)/steps;
2309   for(Int_t i=0; i<steps; i++) {
2310     x = xin + i*dx + dx/2;
2311     ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,-1);
2312     InsertLayer(ppl);
2313   }
2314
2315   dx = 0.02; xin = xout; xout = xin + dx; rho = 1.45; radLength = 44.86; // prepreg
2316   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,radLength,-1);
2317   InsertLayer(ppl);
2318
2319   dx = 50e-4; xin = xout; xout = xin + dx; rho = 1.71; radLength = 44.77; // Tedlar
2320   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,radLength,-1);
2321   InsertLayer(ppl);
2322
2323
2324   // set time bins in CO2
2325
2326   xin = xout; xout = 275.0; 
2327   steps = 50; dx = (xout - xin)/steps;
2328   rho = 1.977e-3;  radLength = 36.2;
2329   
2330   for(Int_t i=0; i<steps; i++) {
2331     x = xin + i*dx + dx/2;
2332     ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,-1);
2333     InsertLayer(ppl);
2334   }
2335
2336   // set time bins in the outer containment vessel
2337
2338   dx = 50e-4; xin = xout; xout = xin + dx; rho = 2.7; radLength = 24.01; // Al
2339   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,radLength,-1);
2340   InsertLayer(ppl);
2341
2342   dx = 50e-4; xin = xout; xout = xin + dx; rho = 1.71; radLength = 44.77; // Tedlar
2343   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,radLength,-1);
2344   InsertLayer(ppl);
2345
2346   dx = 0.06; xin = xout; xout = xin + dx; rho = 1.45; radLength = 44.86; // prepreg
2347   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,radLength,-1);
2348   InsertLayer(ppl);
2349
2350   dx = 3.; xin = xout; xout = xin + dx; rho = 1.45*0.02; radLength = 41.28; // Nomex
2351   steps = 10; dx = (xout - xin)/steps;
2352   for(Int_t i=0; i<steps; i++) {
2353     x = xin + i*dx + dx/2;
2354     ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,-1);
2355     InsertLayer(ppl);
2356   }
2357
2358   dx = 0.06; xin = xout; xout = xin + dx; rho = 1.45; radLength = 44.86; // prepreg
2359   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,radLength,-1);
2360   InsertLayer(ppl);
2361
2362   dx = 50e-4; xin = xout; xout = xin + dx; rho = 1.71; radLength = 44.77; // Tedlar
2363   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,radLength,-1);
2364   InsertLayer(ppl);
2365   
2366   dx = 50e-4; xin = xout; xout = xin + dx; rho = 2.7; radLength = 24.01; // Al
2367   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,radLength,-1);
2368   InsertLayer(ppl);
2369
2370   Double_t xtrd = (Double_t) fGeom->Rmin();  
2371
2372   // add layers between TPC and TRD (Air temporarily)
2373   xin = xout; xout = xtrd;
2374   steps = 50; dx = (xout - xin)/steps;
2375   rho = 1.2e-3;  radLength = 36.66;
2376   
2377   for(Int_t i=0; i<steps; i++) {
2378     x = xin + i*dx + dx/2;
2379     ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,-1);
2380     InsertLayer(ppl);
2381   }
2382
2383
2384   Double_t alpha=AliTRDgeometry::GetAlpha();
2385
2386   // add layers for each of the planes
2387
2388   Double_t dxRo = (Double_t) fGeom->CroHght();    // Rohacell 
2389   Double_t dxSpace = (Double_t) fGeom->Cspace();  // Spacing between planes
2390   Double_t dxAmp = (Double_t) fGeom->CamHght();   // Amplification region
2391   Double_t dxDrift = (Double_t) fGeom->CdrHght(); // Drift region  
2392   Double_t dxRad = (Double_t) fGeom->CraHght();   // Radiator
2393   Double_t dxTEC = dxRad + dxDrift + dxAmp + dxRo; 
2394   Double_t dxPlane = dxTEC + dxSpace; 
2395
2396   Int_t tb, tbIndex;
2397   const Int_t  kNchambers = AliTRDgeometry::Ncham();
2398   Double_t  ymax = 0, holeYmax = 0;
2399   Double_t *zc = new Double_t[kNchambers];
2400   Double_t *zmax = new Double_t[kNchambers];
2401   Double_t  holeZmax = 1000.;   // the whole sector is missing
2402
2403   for(Int_t plane = 0; plane < AliTRDgeometry::Nplan(); plane++) {
2404
2405     // Radiator 
2406     xin = xtrd + plane * dxPlane; xout = xin + dxRad;
2407     steps = 12; dx = (xout - xin)/steps; rho = 0.074; radLength = 40.6; 
2408     for(Int_t i=0; i<steps; i++) {
2409       x = xin + i*dx + dx/2;
2410       ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,-1);
2411       if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
2412         holeYmax = x*TMath::Tan(0.5*alpha);
2413         ppl->SetHole(holeYmax, holeZmax);
2414       }
2415       if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
2416         holeYmax = x*TMath::Tan(0.5*alpha);
2417         ppl->SetHole(holeYmax, holeZmax);
2418       }
2419       InsertLayer(ppl);
2420     }
2421
2422     ymax = fGeom->GetChamberWidth(plane)/2;
2423     for(Int_t ch = 0; ch < kNchambers; ch++) {
2424       zmax[ch] = fGeom->GetChamberLength(plane,ch)/2;
2425       Float_t pad = fPar->GetRowPadSize(plane,ch,0);
2426       Float_t row0 = fPar->GetRow0(plane,ch,0);
2427       Int_t nPads = fPar->GetRowMax(plane,ch,0);
2428       zc[ch] = (pad * nPads)/2 + row0 - pad/2;
2429     }
2430
2431     dx = fPar->GetTimeBinSize(); 
2432     rho = 0.00295 * 0.85; radLength = 11.0;  
2433
2434     Double_t x0 = (Double_t) fPar->GetTime0(plane);
2435     Double_t xbottom = x0 - dxDrift;
2436     Double_t xtop = x0 + dxAmp;
2437
2438     // Amplification region
2439
2440     steps = (Int_t) (dxAmp/dx);
2441
2442     for(tb = 0; tb < steps; tb++) {
2443       x = x0 + tb * dx + dx/2;
2444       tbIndex = CookTimeBinIndex(plane, -tb-1);
2445       ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,tbIndex);
2446       ppl->SetYmax(ymax);
2447       for(Int_t ch = 0; ch < kNchambers; ch++) {
2448         ppl->SetZmax(ch, zc[ch], zmax[ch]);
2449       }
2450       if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
2451         holeYmax = x*TMath::Tan(0.5*alpha);
2452         ppl->SetHole(holeYmax, holeZmax);
2453       }
2454       if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
2455         holeYmax = x*TMath::Tan(0.5*alpha);
2456         ppl->SetHole(holeYmax, holeZmax);
2457       }
2458       InsertLayer(ppl);
2459     }
2460     tbIndex = CookTimeBinIndex(plane, -steps);
2461     x = (x + dx/2 + xtop)/2;
2462     dx = 2*(xtop-x);
2463     ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,tbIndex);
2464     ppl->SetYmax(ymax);
2465     for(Int_t ch = 0; ch < kNchambers; ch++) {
2466       ppl->SetZmax(ch, zc[ch], zmax[ch]);
2467     }
2468     if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
2469       holeYmax = x*TMath::Tan(0.5*alpha);
2470       ppl->SetHole(holeYmax, holeZmax);
2471     }
2472     if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
2473       holeYmax = x*TMath::Tan(0.5*alpha);
2474       ppl->SetHole(holeYmax, holeZmax);
2475     }
2476     InsertLayer(ppl);
2477
2478     // Drift region
2479     dx = fPar->GetTimeBinSize();
2480     steps = (Int_t) (dxDrift/dx);
2481
2482     for(tb = 0; tb < steps; tb++) {
2483       x = x0 - tb * dx - dx/2;
2484       tbIndex = CookTimeBinIndex(plane, tb);
2485
2486       ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,tbIndex);
2487       ppl->SetYmax(ymax);
2488       for(Int_t ch = 0; ch < kNchambers; ch++) {
2489         ppl->SetZmax(ch, zc[ch], zmax[ch]);
2490       }
2491       if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
2492         holeYmax = x*TMath::Tan(0.5*alpha);
2493         ppl->SetHole(holeYmax, holeZmax);
2494       }
2495       if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
2496         holeYmax = x*TMath::Tan(0.5*alpha);
2497         ppl->SetHole(holeYmax, holeZmax);
2498       }
2499       InsertLayer(ppl);
2500     }
2501     tbIndex = CookTimeBinIndex(plane, steps);
2502     x = (x - dx/2 + xbottom)/2;
2503     dx = 2*(x-xbottom);
2504     ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,tbIndex);
2505     ppl->SetYmax(ymax);
2506     for(Int_t ch = 0; ch < kNchambers; ch++) {
2507       ppl->SetZmax(ch, zc[ch], zmax[ch]);
2508     }
2509     if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
2510       holeYmax = x*TMath::Tan(0.5*alpha);
2511       ppl->SetHole(holeYmax, holeZmax);
2512     }
2513     if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
2514       holeYmax = x*TMath::Tan(0.5*alpha);
2515       ppl->SetHole(holeYmax, holeZmax);
2516     }
2517     InsertLayer(ppl);
2518
2519     // Pad Plane
2520     xin = xtop; dx = 0.025; xout = xin + dx; rho = 1.7; radLength = 33.0;
2521     ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,radLength,-1);
2522     if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
2523       holeYmax = (xin+dx/2)*TMath::Tan(0.5*alpha);
2524       ppl->SetHole(holeYmax, holeZmax);
2525     }
2526     if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
2527       holeYmax = (xin+dx/2)*TMath::Tan(0.5*alpha);
2528       ppl->SetHole(holeYmax, holeZmax);
2529     }
2530     InsertLayer(ppl);
2531
2532     // Rohacell
2533     xin = xout; xout = xtrd + (plane + 1) * dxPlane - dxSpace;
2534     steps = 5; dx = (xout - xin)/steps; rho = 0.074; radLength = 40.6; 
2535     for(Int_t i=0; i<steps; i++) {
2536       x = xin + i*dx + dx/2;
2537       ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,-1);
2538       if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
2539         holeYmax = x*TMath::Tan(0.5*alpha);
2540         ppl->SetHole(holeYmax, holeZmax);
2541       }
2542       if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
2543         holeYmax = x*TMath::Tan(0.5*alpha);
2544         ppl->SetHole(holeYmax, holeZmax);
2545       }
2546       InsertLayer(ppl);
2547     }
2548
2549     // Space between the chambers, air
2550     xin = xout; xout = xtrd + (plane + 1) * dxPlane;
2551     steps = 5; dx = (xout - xin)/steps; rho = 1.29e-3; radLength = 36.66; 
2552     for(Int_t i=0; i<steps; i++) {
2553       x = xin + i*dx + dx/2;
2554       ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,-1);
2555       if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
2556         holeYmax = x*TMath::Tan(0.5*alpha);
2557         ppl->SetHole(holeYmax, holeZmax);
2558       }
2559       if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
2560         holeYmax = x*TMath::Tan(0.5*alpha);
2561         ppl->SetHole(holeYmax, holeZmax);
2562       }
2563       InsertLayer(ppl);
2564     }
2565   }    
2566
2567   // Space between the TRD and RICH
2568   Double_t xRICH = 500.;
2569   xin = xout; xout = xRICH;
2570   steps = 200; dx = (xout - xin)/steps; rho = 1.29e-3; radLength = 36.66; 
2571   for(Int_t i=0; i<steps; i++) {
2572     x = xin + i*dx + dx/2;
2573     ppl = new AliTRDpropagationLayer(x,dx,rho,radLength,-1);
2574     InsertLayer(ppl);
2575   }
2576
2577   MapTimeBinLayers();
2578   delete [] zc;
2579   delete [] zmax;
2580
2581 }
2582
2583 //______________________________________________________
2584
2585 Int_t  AliTRDtracker::AliTRDtrackingSector::CookTimeBinIndex(Int_t plane, Int_t localTB) const
2586 {
2587   //
2588   // depending on the digitization parameters calculates "global"
2589   // time bin index for timebin <localTB> in plane <plane>
2590   //
2591
2592   Double_t dxAmp = (Double_t) fGeom->CamHght();   // Amplification region
2593   Double_t dxDrift = (Double_t) fGeom->CdrHght(); // Drift region  
2594   Double_t dx = (Double_t) fPar->GetTimeBinSize();  
2595
2596   Int_t tbAmp = fPar->GetTimeBefore();
2597   Int_t maxAmp = (Int_t) ((dxAmp+0.000001)/dx);
2598   if(kTRUE) maxAmp = 0;   // intentional until we change parameter class 
2599   Int_t tbDrift = fPar->GetTimeMax();
2600   Int_t maxDrift = (Int_t) ((dxDrift+0.000001)/dx);
2601
2602   Int_t tbPerPlane = TMath::Min(tbAmp,maxAmp) + TMath::Min(tbDrift,maxDrift);
2603
2604   Int_t gtb = (plane+1) * tbPerPlane - localTB - 1 - TMath::Min(tbAmp,maxAmp);
2605
2606   if((localTB < 0) && 
2607      (TMath::Abs(localTB) > TMath::Min(tbAmp,maxAmp))) return -1;
2608   if(localTB >= TMath::Min(tbDrift,maxDrift)) return -1;
2609
2610   return gtb;
2611
2612
2613 }
2614
2615 //______________________________________________________
2616
2617 void AliTRDtracker::AliTRDtrackingSector::MapTimeBinLayers() 
2618 {
2619   //
2620   // For all sensitive time bins sets corresponding layer index
2621   // in the array fTimeBins 
2622   //
2623
2624   Int_t index;
2625
2626   for(Int_t i = 0; i < fN; i++) {
2627     index = fLayers[i]->GetTimeBinIndex();
2628     
2629     //    printf("gtb %d -> pl %d -> x %f \n", index, i, fLayers[i]->GetX());
2630
2631     if(index < 0) continue;
2632     if(index >= (Int_t) kMaxTimeBinIndex) {
2633       printf("*** AliTRDtracker::MapTimeBinLayers: \n");
2634       printf("    index %d exceeds allowed maximum of %d!\n",
2635              index, kMaxTimeBinIndex-1);
2636       continue;
2637     }
2638     fTimeBinIndex[index] = i;
2639   }
2640
2641   Double_t x1, dx1, x2, dx2, gap;
2642
2643   for(Int_t i = 0; i < fN-1; i++) {
2644     x1 = fLayers[i]->GetX();
2645     dx1 = fLayers[i]->GetdX();
2646     x2 = fLayers[i+1]->GetX();
2647     dx2 = fLayers[i+1]->GetdX();
2648     gap = (x2 - dx2/2) - (x1 + dx1/2);
2649     if(gap < -0.01) {
2650       printf("*** warning: layers %d and %d are overlayed:\n",i,i+1);
2651       printf("             %f + %f + %f > %f\n", x1, dx1/2, dx2/2, x2);
2652     }
2653     if(gap > 0.01) { 
2654       printf("*** warning: layers %d and %d have a large gap:\n",i,i+1);
2655       printf("             (%f - %f) - (%f + %f) = %f\n", 
2656              x2, dx2/2, x1, dx1, gap);
2657     }
2658   }
2659 }
2660   
2661
2662 //______________________________________________________
2663
2664
2665 Int_t AliTRDtracker::AliTRDtrackingSector::GetLayerNumber(Double_t x) const
2666 {
2667   // 
2668   // Returns the number of time bin which in radial position is closest to <x>
2669   //
2670
2671   if(x >= fLayers[fN-1]->GetX()) return fN-1; 
2672   if(x <= fLayers[0]->GetX()) return 0; 
2673
2674   Int_t b=0, e=fN-1, m=(b+e)/2;
2675   for (; b<e; m=(b+e)/2) {
2676     if (x > fLayers[m]->GetX()) b=m+1;
2677     else e=m;
2678   }
2679   if(TMath::Abs(x - fLayers[m]->GetX()) > 
2680      TMath::Abs(x - fLayers[m+1]->GetX())) return m+1;
2681   else return m;
2682
2683 }
2684
2685 //______________________________________________________
2686
2687 Int_t AliTRDtracker::AliTRDtrackingSector::GetInnerTimeBin() const 
2688 {
2689   // 
2690   // Returns number of the innermost SENSITIVE propagation layer
2691   //
2692
2693   return GetLayerNumber(0);
2694 }
2695
2696 //______________________________________________________
2697
2698 Int_t AliTRDtracker::AliTRDtrackingSector::GetOuterTimeBin() const 
2699 {
2700   // 
2701   // Returns number of the outermost SENSITIVE time bin
2702   //
2703
2704   return GetLayerNumber(GetNumberOfTimeBins() - 1);
2705 }
2706
2707 //______________________________________________________
2708
2709 Int_t AliTRDtracker::AliTRDtrackingSector::GetNumberOfTimeBins() const 
2710 {
2711   // 
2712   // Returns number of SENSITIVE time bins
2713   //
2714
2715   Int_t tb, layer;
2716   for(tb = kMaxTimeBinIndex-1; tb >=0; tb--) {
2717     layer = GetLayerNumber(tb);
2718     if(layer>=0) break;
2719   }
2720   return tb+1;
2721 }
2722
2723 //______________________________________________________
2724
2725 void AliTRDtracker::AliTRDtrackingSector::InsertLayer(AliTRDpropagationLayer* pl)
2726
2727   //
2728   // Insert layer <pl> in fLayers array.
2729   // Layers are sorted according to X coordinate.
2730
2731   if ( fN == ((Int_t) kMaxLayersPerSector)) {
2732     printf("AliTRDtrackingSector::InsertLayer(): Too many layers !\n");
2733     return;
2734   }
2735   if (fN==0) {fLayers[fN++] = pl; return;}
2736   Int_t i=Find(pl->GetX());
2737
2738   memmove(fLayers+i+1 ,fLayers+i,(fN-i)*sizeof(AliTRDpropagationLayer*));
2739   fLayers[i]=pl; fN++;
2740
2741 }              
2742
2743 //______________________________________________________
2744
2745 Int_t AliTRDtracker::AliTRDtrackingSector::Find(Double_t x) const 
2746 {
2747   //
2748   // Returns index of the propagation layer nearest to X 
2749   //
2750
2751   if (x <= fLayers[0]->GetX()) return 0;
2752   if (x > fLayers[fN-1]->GetX()) return fN;
2753   Int_t b=0, e=fN-1, m=(b+e)/2;
2754   for (; b<e; m=(b+e)/2) {
2755     if (x > fLayers[m]->GetX()) b=m+1;
2756     else e=m;
2757   }
2758   return m;
2759 }             
2760
2761 //______________________________________________________
2762
2763 void AliTRDtracker::AliTRDpropagationLayer::GetPropagationParameters(
2764         Double_t y, Double_t z, Double_t &dx, Double_t &rho, Double_t &radLength, 
2765         Bool_t &lookForCluster) const
2766 {
2767   //
2768   // Returns radial step <dx>, density <rho>, rad. length <radLength>,
2769   // and sensitivity <lookForCluster> in point <y,z>  
2770   //
2771
2772   dx  = fdX;
2773   rho = fRho;
2774   radLength  = fX0;
2775   lookForCluster = kFALSE;
2776
2777   // check dead regions
2778   if(fTimeBinIndex >= 0) {
2779     for(Int_t ch = 0; ch < (Int_t) kZones; ch++) {
2780       if(TMath::Abs(z - fZc[ch]) < fZmax[ch]) 
2781         lookForCluster = kTRUE;
2782       //  else { rho = 1.7; radLength = 33.0; } // G10 
2783     }
2784     if(TMath::Abs(y) > fYmax) lookForCluster = kFALSE;
2785     if(!lookForCluster) { 
2786       //      rho = 1.7; radLength = 33.0; // G10 
2787     }
2788   }
2789
2790   // check hole
2791   if(fHole && (TMath::Abs(y - fHoleYc) < fHoleYmax) && 
2792               (TMath::Abs(z - fHoleZc) < fHoleZmax)) {
2793     lookForCluster = kFALSE;
2794     rho = fHoleRho;
2795     radLength  = fHoleX0;
2796   }         
2797
2798   return;
2799 }
2800
2801 //______________________________________________________
2802
2803 void AliTRDtracker::AliTRDpropagationLayer::InsertCluster(AliTRDcluster* c, 
2804                                                           UInt_t index) {
2805
2806 // Insert cluster in cluster array.
2807 // Clusters are sorted according to Y coordinate.  
2808
2809   if(fTimeBinIndex < 0) { 
2810     printf("*** attempt to insert cluster into non-sensitive time bin!\n");
2811     return;
2812   }
2813
2814   if (fN== (Int_t) kMaxClusterPerTimeBin) {
2815     printf("AliTRDpropagationLayer::InsertCluster(): Too many clusters !\n"); 
2816     return;
2817   }
2818   if (fN==0) {fIndex[0]=index; fClusters[fN++]=c; return;}
2819   Int_t i=Find(c->GetY());
2820   memmove(fClusters+i+1 ,fClusters+i,(fN-i)*sizeof(AliTRDcluster*));
2821   memmove(fIndex   +i+1 ,fIndex   +i,(fN-i)*sizeof(UInt_t)); 
2822   fIndex[i]=index; fClusters[i]=c; fN++;
2823 }  
2824
2825 //______________________________________________________
2826
2827 Int_t AliTRDtracker::AliTRDpropagationLayer::Find(Double_t y) const {
2828
2829 // Returns index of the cluster nearest in Y    
2830
2831   if (y <= fClusters[0]->GetY()) return 0;
2832   if (y > fClusters[fN-1]->GetY()) return fN;
2833   Int_t b=0, e=fN-1, m=(b+e)/2;
2834   for (; b<e; m=(b+e)/2) {
2835     if (y > fClusters[m]->GetY()) b=m+1;
2836     else e=m;
2837   }
2838   return m;
2839 }    
2840
2841 //---------------------------------------------------------
2842
2843 Double_t AliTRDtracker::GetTiltFactor(const AliTRDcluster* c) {
2844 //
2845 //  Returns correction factor for tilted pads geometry 
2846 //
2847
2848   Double_t h01 = sin(TMath::Pi() / 180.0 * fPar->GetTiltingAngle());
2849   Int_t det = c->GetDetector();    
2850   Int_t plane = fGeom->GetPlane(det);
2851
2852   if((plane == 1) || (plane == 3) || (plane == 5)) h01=-h01;
2853
2854   if(fNoTilt) h01 = 0;
2855   
2856   return h01;
2857 }
2858
2859
2860
2861
2862
2863