]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDtracker.cxx
Load/UnloadClusters added to the base class and the derived classes changed correspon...
[u/mrichter/AliRoot.git] / TRD / AliTRDtracker.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15                                                       
16 /*
17 $Log$
18 Revision 1.24  2003/02/19 09:02:28  hristov
19 Track time measurement (S.Radomski)
20
21 Revision 1.23  2003/02/10 14:06:10  cblume
22 Add tracking without tilted pads as option
23
24 Revision 1.22  2003/01/30 15:19:58  cblume
25 New set of  parameters
26
27 Revision 1.21  2003/01/27 16:34:49  cblume
28 Update of tracking by Sergei and Chuncheng
29
30 Revision 1.20  2002/11/07 15:52:09  cblume
31 Update of tracking code for tilted pads
32
33 Revision 1.19  2002/10/22 15:53:08  alibrary
34 Introducing Riostream.h
35
36 Revision 1.18  2002/10/14 14:57:44  hristov
37 Merging the VirtualMC branch to the main development branch (HEAD)
38
39 Revision 1.14.6.2  2002/07/24 10:09:31  alibrary
40 Updating VirtualMC
41
42 Revision 1.17  2002/06/13 12:09:58  hristov
43 Minor corrections
44
45 Revision 1.16  2002/06/12 09:54:36  cblume
46 Update of tracking code provided by Sergei
47
48 Revision 1.14  2001/11/14 10:50:46  cblume
49 Changes in digits IO. Add merging of summable digits
50
51 Revision 1.13  2001/05/30 12:17:47  hristov
52 Loop variables declared once
53
54 Revision 1.12  2001/05/28 17:07:58  hristov
55 Last minute changes; ExB correction in AliTRDclusterizerV1; taking into account of material in G10 TEC frames and material between TEC planes (C.Blume,S.Sedykh)
56
57 Revision 1.8  2000/12/20 13:00:44  cblume
58 Modifications for the HP-compiler
59
60 Revision 1.7  2000/12/08 16:07:02  cblume
61 Update of the tracking by Sergei
62
63 Revision 1.6  2000/11/30 17:38:08  cblume
64 Changes to get in line with new STEER and EVGEN
65
66 Revision 1.5  2000/11/14 14:40:27  cblume
67 Correction for the Sun compiler (kTRUE and kFALSE)
68
69 Revision 1.4  2000/11/10 14:57:52  cblume
70 Changes in the geometry constants for the DEC compiler
71
72 Revision 1.3  2000/10/15 23:40:01  cblume
73 Remove AliTRDconst
74
75 Revision 1.2  2000/10/06 16:49:46  cblume
76 Made Getters const
77
78 Revision 1.1.2.2  2000/10/04 16:34:58  cblume
79 Replace include files by forward declarations
80
81 Revision 1.1.2.1  2000/09/22 14:47:52  cblume
82 Add the tracking code
83
84 */   
85
86 #include <Riostream.h>
87
88 #include <TFile.h>
89 #include <TBranch.h>
90 #include <TTree.h>  
91 #include <TObjArray.h> 
92
93 #include "AliTRDgeometry.h"
94 #include "AliTRDparameter.h"
95 #include "AliTRDgeometryDetail.h"
96 #include "AliTRDcluster.h" 
97 #include "AliTRDtrack.h"
98 #include "../TPC/AliTPCtrack.h"
99
100 #include "AliTRDtracker.h"
101
102 ClassImp(AliTRDtracker) 
103
104   const  Float_t     AliTRDtracker::fSeedDepth          = 0.5; 
105   const  Float_t     AliTRDtracker::fSeedStep           = 0.10;   
106   const  Float_t     AliTRDtracker::fSeedGap            = 0.25;  
107
108   const  Float_t     AliTRDtracker::fMaxSeedDeltaZ12    = 40.;  
109   const  Float_t     AliTRDtracker::fMaxSeedDeltaZ      = 25.;  
110   const  Float_t     AliTRDtracker::fMaxSeedC           = 0.0052; 
111   const  Float_t     AliTRDtracker::fMaxSeedTan         = 1.2;  
112   const  Float_t     AliTRDtracker::fMaxSeedVertexZ     = 150.; 
113
114   const  Double_t    AliTRDtracker::fSeedErrorSY        = 0.2;
115   const  Double_t    AliTRDtracker::fSeedErrorSY3       = 2.5;
116   const  Double_t    AliTRDtracker::fSeedErrorSZ        = 0.1;
117
118   const  Float_t     AliTRDtracker::fMinClustersInSeed  = 0.7;  
119
120   const  Float_t     AliTRDtracker::fMinClustersInTrack = 0.5;  
121   const  Float_t     AliTRDtracker::fMinFractionOfFoundClusters = 0.8;  
122
123   const  Float_t     AliTRDtracker::fSkipDepth          = 0.3;
124   const  Float_t     AliTRDtracker::fLabelFraction      = 0.8;  
125   const  Float_t     AliTRDtracker::fWideRoad           = 20.;
126
127   const  Double_t    AliTRDtracker::fMaxChi2            = 12.; 
128
129
130 //____________________________________________________________________
131 AliTRDtracker::AliTRDtracker(const TFile *geomfile)
132 {
133   // 
134   //  Main constructor
135   //  
136
137   Float_t fTzero = 0;
138    
139   fAddTRDseeds = kFALSE;
140   fGeom = NULL;
141   fNoTilt = kFALSE;
142   
143   TDirectory *savedir=gDirectory; 
144   TFile *in=(TFile*)geomfile;  
145   if (!in->IsOpen()) {
146     printf("AliTRDtracker::AliTRDtracker(): geometry file is not open!\n");
147     printf("    DETAIL TRD geometry and DEFAULT TRD parameter will be used\n");
148   }
149   else {
150     in->cd();  
151     in->ls();
152     fGeom = (AliTRDgeometry*) in->Get("TRDgeometry");
153     fPar  = (AliTRDparameter*) in->Get("TRDparameter");
154     fGeom->Dump();
155   }
156
157   if(fGeom) {
158     //    fTzero = geo->GetT0();
159     printf("Found geometry version %d on file \n", fGeom->IsVersion());
160   }
161   else { 
162     printf("AliTRDtracker::AliTRDtracker(): cann't find TRD geometry!\n");
163     printf("    DETAIL TRD geometry and DEFAULT TRD parameter will be used\n");
164     fGeom = new AliTRDgeometryDetail(); 
165     fPar = new AliTRDparameter();
166   }
167
168   savedir->cd();  
169
170
171   //  fGeom->SetT0(fTzero);
172
173   fNclusters = 0;
174   fClusters  = new TObjArray(2000); 
175   fNseeds    = 0;
176   fSeeds     = new TObjArray(2000);
177   fNtracks   = 0;
178   fTracks    = new TObjArray(1000);
179
180   for(Int_t geom_s = 0; geom_s < kTRACKING_SECTORS; geom_s++) {
181     Int_t tr_s = CookSectorIndex(geom_s);
182     fTrSec[tr_s] = new AliTRDtrackingSector(fGeom, geom_s, fPar);
183   }
184
185   Float_t tilt_angle = TMath::Abs(fPar->GetTiltingAngle()); 
186   if(tilt_angle < 0.1) {
187     fNoTilt = kTRUE;
188   }
189
190   fSY2corr = 0.2;
191   fSZ2corr = 120.;      
192
193   if(fNoTilt && (tilt_angle > 0.1)) fSY2corr = fSY2corr + tilt_angle * 0.05; 
194
195
196   // calculate max gap on track
197
198   Double_t dxAmp = (Double_t) fGeom->CamHght();   // Amplification region
199   Double_t dxDrift = (Double_t) fGeom->CdrHght(); // Drift region
200
201   Double_t dx = (Double_t) fPar->GetTimeBinSize();   
202   Int_t tbAmp = fPar->GetTimeBefore();
203   Int_t maxAmp = (Int_t) ((dxAmp+0.000001)/dx);
204   if(kTRUE) maxAmp = 0;  // intentional until we change the parameter class 
205   Int_t tbDrift = fPar->GetTimeMax();
206   Int_t maxDrift = (Int_t) ((dxDrift+0.000001)/dx);
207
208   tbDrift = TMath::Min(tbDrift,maxDrift);
209   tbAmp = TMath::Min(tbAmp,maxAmp);
210
211   fTimeBinsPerPlane = tbAmp + tbDrift;
212   fMaxGap = (Int_t) (fTimeBinsPerPlane * fGeom->Nplan() * fSkipDepth);
213
214   fVocal = kFALSE;
215
216 }   
217
218 //___________________________________________________________________
219 AliTRDtracker::~AliTRDtracker()
220 {
221   delete fClusters;
222   delete fTracks;
223   delete fSeeds;
224   delete fGeom;  
225   delete fPar;  
226
227   for(Int_t geom_s = 0; geom_s < kTRACKING_SECTORS; geom_s++) {
228     delete fTrSec[geom_s];
229   }
230 }   
231
232 //_____________________________________________________________________
233 inline Double_t f1trd(Double_t x1,Double_t y1,
234                       Double_t x2,Double_t y2,
235                       Double_t x3,Double_t y3)
236 {
237   //
238   // Initial approximation of the track curvature
239   //
240   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
241   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
242                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
243   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
244                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
245
246   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
247
248   return -xr*yr/sqrt(xr*xr+yr*yr);
249 }          
250
251 //_____________________________________________________________________
252 inline Double_t f2trd(Double_t x1,Double_t y1,
253                       Double_t x2,Double_t y2,
254                       Double_t x3,Double_t y3)
255 {
256   //
257   // Initial approximation of the track curvature times X coordinate
258   // of the center of curvature
259   //
260
261   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
262   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
263                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
264   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
265                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
266
267   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
268
269   return -a/(d*y1-b)*xr/sqrt(xr*xr+yr*yr);
270 }          
271
272 //_____________________________________________________________________
273 inline Double_t f3trd(Double_t x1,Double_t y1,
274                       Double_t x2,Double_t y2,
275                       Double_t z1,Double_t z2)
276 {
277   //
278   // Initial approximation of the tangent of the track dip angle
279   //
280
281   return (z1 - z2)/sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
282 }            
283
284 //___________________________________________________________________
285 Int_t AliTRDtracker::Clusters2Tracks(const TFile *inp, TFile *out)
286 {
287   //
288   // Finds tracks within the TRD. File <inp> is expected to contain seeds 
289   // at the outer part of the TRD. If <inp> is NULL, the seeds
290   // are found within the TRD if fAddTRDseeds is TRUE. 
291   // The tracks are propagated to the innermost time bin 
292   // of the TRD and stored in file <out>. 
293   //
294
295   LoadEvent();
296  
297   TDirectory *savedir=gDirectory;
298
299   char   tname[100];
300
301   if (!out->IsOpen()) {
302     cerr<<"AliTRDtracker::Clusters2Tracks(): output file is not open !\n";
303     return 1;
304   }    
305
306   sprintf(tname,"seedTRDtoTPC_%d",GetEventNumber()); 
307   TTree tpc_tree(tname,"Tree with seeds from TRD at outer TPC pad row");
308   AliTPCtrack *iotrack=0;
309   tpc_tree.Branch("tracks","AliTPCtrack",&iotrack,32000,0); 
310
311   sprintf(tname,"TreeT%d_TRD",GetEventNumber());
312   TTree trd_tree(tname,"TRD tracks at inner TRD time bin");
313   AliTRDtrack *iotrack_trd=0;
314   trd_tree.Branch("tracks","AliTRDtrack",&iotrack_trd,32000,0);  
315
316   Int_t timeBins = fTrSec[0]->GetNumberOfTimeBins();
317   Float_t foundMin = fMinClustersInTrack * timeBins; 
318
319   if (inp) {
320      TFile *in=(TFile*)inp;
321      if (!in->IsOpen()) {
322         cerr<<"AliTRDtracker::Clusters2Tracks(): file with seeds is not open !\n";
323         cerr<<" ... going for seeds finding inside the TRD\n";
324      }
325      else {
326        in->cd();
327        sprintf(tname,"TRDb_%d",GetEventNumber());  
328        TTree *seedTree=(TTree*)in->Get(tname);  
329        if (!seedTree) {
330          cerr<<"AliTRDtracker::Clusters2Tracks(): ";
331          cerr<<"can't get a tree with track seeds !\n";
332          return 3;
333        }  
334        AliTRDtrack *seed=new AliTRDtrack;
335        seedTree->SetBranchAddress("tracks",&seed);
336
337        Int_t n=(Int_t)seedTree->GetEntries();
338        for (Int_t i=0; i<n; i++) {
339          seedTree->GetEvent(i);
340          seed->ResetCovariance(); 
341          AliTRDtrack *tr = new AliTRDtrack(*seed,seed->GetAlpha());
342          fSeeds->AddLast(tr);
343          fNseeds++;
344        }          
345        delete seed;
346        delete seedTree;
347      }
348   }
349
350   out->cd();
351
352
353   // find tracks from loaded seeds
354
355   Int_t nseed=fSeeds->GetEntriesFast();
356   Int_t i, found = 0;
357   Int_t innerTB = fTrSec[0]->GetInnerTimeBin();
358
359   for (i=0; i<nseed; i++) {   
360     AliTRDtrack *pt=(AliTRDtrack*)fSeeds->UncheckedAt(i), &t=*pt; 
361     FollowProlongation(t, innerTB); 
362     if (t.GetNumberOfClusters() >= foundMin) {
363       UseClusters(&t);
364       CookLabel(pt, 1-fLabelFraction);
365       //      t.CookdEdx();
366     }
367     iotrack_trd = pt;
368     trd_tree.Fill();
369     found++;
370 //    cout<<found<<'\r';     
371
372     if(PropagateToTPC(t)) {
373       AliTPCtrack *tpc = new AliTPCtrack(*pt,pt->GetAlpha());
374       iotrack = tpc;
375       tpc_tree.Fill();
376       delete tpc;
377     }  
378     delete fSeeds->RemoveAt(i);
379     fNseeds--;
380   }     
381
382   cout<<"Number of loaded seeds: "<<nseed<<endl;  
383   cout<<"Number of found tracks from loaded seeds: "<<found<<endl;
384
385   // after tracks from loaded seeds are found and the corresponding 
386   // clusters are used, look for additional seeds from TRD
387
388   if(fAddTRDseeds) { 
389     // Find tracks for the seeds in the TRD
390     Int_t timeBins = fTrSec[0]->GetNumberOfTimeBins();
391   
392     Int_t nSteps = (Int_t) (fSeedDepth / fSeedStep);
393     Int_t gap = (Int_t) (timeBins * fSeedGap);
394     Int_t step = (Int_t) (timeBins * fSeedStep);
395   
396     // make a first turn with tight cut on initial curvature
397     for(Int_t turn = 1; turn <= 2; turn++) {
398       if(turn == 2) {
399         nSteps = (Int_t) (fSeedDepth / (3*fSeedStep));
400         step = (Int_t) (timeBins * (3*fSeedStep));
401       }
402       for(Int_t i=0; i<nSteps; i++) {
403         Int_t outer=timeBins-1-i*step; 
404         Int_t inner=outer-gap;
405
406         nseed=fSeeds->GetEntriesFast();
407       
408         MakeSeeds(inner, outer, turn);
409       
410         nseed=fSeeds->GetEntriesFast();
411         printf("\n turn %d, step %d: number of seeds for TRD inward %d\n", 
412                turn, i, nseed); 
413               
414         for (Int_t i=0; i<nseed; i++) {   
415           AliTRDtrack *pt=(AliTRDtrack*)fSeeds->UncheckedAt(i), &t=*pt; 
416           FollowProlongation(t,innerTB); 
417           if (t.GetNumberOfClusters() >= foundMin) {
418             UseClusters(&t);
419             CookLabel(pt, 1-fLabelFraction);
420             t.CookdEdx();
421             found++;
422 //            cout<<found<<'\r';     
423             iotrack_trd = pt;
424             trd_tree.Fill();
425             if(PropagateToTPC(t)) {
426               AliTPCtrack *tpc = new AliTPCtrack(*pt,pt->GetAlpha());
427               iotrack = tpc;
428               tpc_tree.Fill();
429               delete tpc;
430             }        
431           }
432           delete fSeeds->RemoveAt(i);
433           fNseeds--;
434         }
435       }
436     }
437   }
438   tpc_tree.Write(); 
439   trd_tree.Write(); 
440   
441   cout<<"Total number of found tracks: "<<found<<endl;
442     
443   UnloadEvent();
444     
445   savedir->cd();  
446   
447   return 0;    
448 }     
449      
450   
451
452 //_____________________________________________________________________________
453 Int_t AliTRDtracker::PropagateBack(const TFile *inp, TFile *out) {
454   //
455   // Reads seeds from file <inp>. The seeds are AliTPCtrack's found and
456   // backpropagated by the TPC tracker. Each seed is first propagated 
457   // to the TRD, and then its prolongation is searched in the TRD.
458   // If sufficiently long continuation of the track is found in the TRD
459   // the track is updated, otherwise it's stored as originaly defined 
460   // by the TPC tracker.   
461   //  
462
463   LoadEvent();
464
465   TDirectory *savedir=gDirectory;
466
467   TFile *in=(TFile*)inp;
468
469   if (!in->IsOpen()) {
470      cerr<<"AliTRDtracker::PropagateBack(): ";
471      cerr<<"file with back propagated TPC tracks is not open !\n";
472      return 1;
473   }                   
474
475   if (!out->IsOpen()) {
476      cerr<<"AliTRDtracker::PropagateBack(): ";
477      cerr<<"file for back propagated TRD tracks is not open !\n";
478      return 2;
479   }      
480
481   in->cd();
482   char   tname[100];
483   sprintf(tname,"seedsTPCtoTRD_%d",GetEventNumber());       
484   TTree *seedTree=(TTree*)in->Get(tname);
485   if (!seedTree) {
486      cerr<<"AliTRDtracker::PropagateBack(): ";
487      cerr<<"can't get a tree with seeds from TPC !\n";
488      cerr<<"check if your version of TPC tracker creates tree "<<tname<<"\n";
489      return 3;
490   }
491
492   AliTPCtrack *seed=new AliTPCtrack;
493   seedTree->SetBranchAddress("tracks",&seed);
494
495   Int_t n=(Int_t)seedTree->GetEntries();
496   for (Int_t i=0; i<n; i++) {
497      seedTree->GetEvent(i);
498      Int_t lbl = seed->GetLabel();
499      AliTRDtrack *tr = new AliTRDtrack(*seed,seed->GetAlpha());
500      tr->SetSeedLabel(lbl);
501      fSeeds->AddLast(tr);
502      fNseeds++;
503   }
504
505   delete seed;
506   delete seedTree;
507
508   out->cd();
509
510   AliTPCtrack *otrack=0;
511
512   sprintf(tname,"seedsTRDtoTOF1_%d",GetEventNumber());  
513   TTree tofTree1(tname,"Tracks back propagated through TPC and TRD");
514   tofTree1.Branch("tracks","AliTPCtrack",&otrack,32000,0);  
515
516   sprintf(tname,"seedsTRDtoTOF2_%d",GetEventNumber());  
517   TTree tofTree2(tname,"Tracks back propagated through TPC and TRD");
518   tofTree2.Branch("tracks","AliTPCtrack",&otrack,32000,0);  
519
520   sprintf(tname,"seedsTRDtoPHOS_%d",GetEventNumber());  
521   TTree phosTree(tname,"Tracks back propagated through TPC and TRD");
522   phosTree.Branch("tracks","AliTPCtrack",&otrack,32000,0);  
523
524   sprintf(tname,"seedsTRDtoRICH_%d",GetEventNumber());  
525   TTree richTree(tname,"Tracks back propagated through TPC and TRD");
526   richTree.Branch("tracks","AliTPCtrack",&otrack,32000,0);  
527
528   sprintf(tname,"TRDb_%d",GetEventNumber());  
529   TTree trdTree(tname,"Back propagated TRD tracks at outer TRD time bin");
530   AliTRDtrack *otrack_trd=0;
531   trdTree.Branch("tracks","AliTRDtrack",&otrack_trd,32000,0);   
532      
533   Int_t found=0;  
534
535   Int_t nseed=fSeeds->GetEntriesFast();
536
537   //  Float_t foundMin = fMinClustersInTrack * fTimeBinsPerPlane * fGeom->Nplan(); 
538   Float_t foundMin = 0;
539
540   Int_t outermost_tb  = fTrSec[0]->GetOuterTimeBin();
541
542   for (Int_t i=0; i<nseed; i++) {  
543
544     AliTRDtrack *ps=(AliTRDtrack*)fSeeds->UncheckedAt(i), &s=*ps;
545     Int_t expectedClr = FollowBackProlongation(s);
546     Int_t foundClr = s.GetNumberOfClusters();
547     Int_t last_tb = fTrSec[0]->GetLayerNumber(s.GetX());
548
549     //    printf("seed %d: found %d out of %d expected clusters, Min is %f\n",
550     //     i, foundClr, expectedClr, foundMin);
551
552     if (foundClr >= foundMin) {
553       if(foundClr >= 2) {
554         s.CookdEdx(); 
555         CookLabel(ps, 1-fLabelFraction);
556         UseClusters(ps);
557       }
558       
559       // Propagate to outer reference plane [SR, GSI, 18.02.2003]
560       ps->PropagateTo(364.8);
561       otrack_trd=ps;
562       trdTree.Fill();
563       found++;
564 //      cout<<found<<'\r';
565     }
566
567     if(((expectedClr < 10) && (last_tb == outermost_tb)) ||
568        ((expectedClr >= 10) && 
569         (((Float_t) foundClr) / ((Float_t) expectedClr) >= 
570          fMinFractionOfFoundClusters) && (last_tb == outermost_tb))) {
571
572       Double_t x_tof = 375.5;
573     
574       if(PropagateToOuterPlane(s,x_tof)) {
575         AliTPCtrack *pt = new AliTPCtrack(*ps,ps->GetAlpha());
576         otrack = pt;
577         tofTree1.Fill();
578         delete pt;
579
580         x_tof = 381.5;
581     
582         if(PropagateToOuterPlane(s,x_tof)) {
583           AliTPCtrack *pt = new AliTPCtrack(*ps,ps->GetAlpha());
584           otrack = pt;
585           tofTree2.Fill();
586           delete pt;
587
588           Double_t x_phos = 460.;
589           
590           if(PropagateToOuterPlane(s,x_phos)) {
591             AliTPCtrack *pt = new AliTPCtrack(*ps,ps->GetAlpha());
592             otrack = pt;
593             phosTree.Fill();
594             delete pt;
595             
596             Double_t x_rich = 490+1.267;
597             
598             if(PropagateToOuterPlane(s,x_rich)) {
599               AliTPCtrack *pt = new AliTPCtrack(*ps,ps->GetAlpha());
600               otrack = pt;
601               richTree.Fill();
602               delete pt;
603             }   
604           }
605         }
606       }      
607     }
608   }
609   
610   tofTree1.Write(); 
611   tofTree2.Write(); 
612   phosTree.Write(); 
613   richTree.Write(); 
614   trdTree.Write(); 
615
616   savedir->cd();  
617   cerr<<"Number of seeds: "<<nseed<<endl;  
618   cerr<<"Number of back propagated TRD tracks: "<<found<<endl;
619
620   UnloadEvent();
621
622   return 0;
623
624 }
625
626
627 //---------------------------------------------------------------------------
628 Int_t AliTRDtracker::FollowProlongation(AliTRDtrack& t, Int_t rf)
629 {
630   // Starting from current position on track=t this function tries
631   // to extrapolate the track up to timeBin=0 and to confirm prolongation
632   // if a close cluster is found. Returns the number of clusters
633   // expected to be found in sensitive layers
634
635   Float_t  wIndex, wTB, wChi2;
636   Float_t  wYrt, wYclosest, wYcorrect, wYwindow;
637   Float_t  wZrt, wZclosest, wZcorrect, wZwindow;
638   Float_t  wPx, wPy, wPz, wC;
639   Double_t Px, Py, Pz;
640   Float_t  wSigmaC2, wSigmaTgl2, wSigmaY2, wSigmaZ2;
641
642   Int_t trackIndex = t.GetLabel();  
643
644   Int_t ns=Int_t(2*TMath::Pi()/AliTRDgeometry::GetAlpha()+0.5);     
645
646   Int_t try_again=fMaxGap;
647
648   Double_t alpha=t.GetAlpha();
649
650   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
651   if (alpha < 0.            ) alpha += 2.*TMath::Pi();
652
653   Int_t s=Int_t(alpha/AliTRDgeometry::GetAlpha())%AliTRDgeometry::kNsect;  
654
655   Double_t rad_length, rho, x, dx, y, ymax, z;
656
657   Int_t expectedNumberOfClusters = 0;
658   Bool_t lookForCluster;
659
660   alpha=AliTRDgeometry::GetAlpha();  // note: change in meaning
661
662  
663   for (Int_t nr=fTrSec[0]->GetLayerNumber(t.GetX()); nr>rf; nr--) { 
664
665     y = t.GetY(); z = t.GetZ();
666
667     // first propagate to the inner surface of the current time bin 
668     fTrSec[s]->GetLayer(nr)->GetPropagationParameters(y,z,dx,rho,rad_length,lookForCluster);
669     x = fTrSec[s]->GetLayer(nr)->GetX()-dx/2; y = t.GetY(); z = t.GetZ();
670     if(!t.PropagateTo(x,rad_length,rho)) break;
671     y = t.GetY();
672     ymax = x*TMath::Tan(0.5*alpha);
673     if (y > ymax) {
674       s = (s+1) % ns;
675       if (!t.Rotate(alpha)) break;
676       if(!t.PropagateTo(x,rad_length,rho)) break;
677     } else if (y <-ymax) {
678       s = (s-1+ns) % ns;                           
679       if (!t.Rotate(-alpha)) break;   
680       if(!t.PropagateTo(x,rad_length,rho)) break;
681     } 
682
683     y = t.GetY(); z = t.GetZ();
684
685     // now propagate to the middle plane of the next time bin 
686     fTrSec[s]->GetLayer(nr-1)->GetPropagationParameters(y,z,dx,rho,rad_length,lookForCluster);
687     x = fTrSec[s]->GetLayer(nr-1)->GetX(); y = t.GetY(); z = t.GetZ();
688     if(!t.PropagateTo(x,rad_length,rho)) break;
689     y = t.GetY();
690     ymax = x*TMath::Tan(0.5*alpha);
691     if (y > ymax) {
692       s = (s+1) % ns;
693       if (!t.Rotate(alpha)) break;
694       if(!t.PropagateTo(x,rad_length,rho)) break;
695     } else if (y <-ymax) {
696       s = (s-1+ns) % ns;                           
697       if (!t.Rotate(-alpha)) break;   
698       if(!t.PropagateTo(x,rad_length,rho)) break;
699     } 
700
701
702     if(lookForCluster) {
703
704       expectedNumberOfClusters++;       
705       wIndex = (Float_t) t.GetLabel();
706       wTB = nr;
707
708       AliTRDpropagationLayer& time_bin=*(fTrSec[s]->GetLayer(nr-1));
709
710       Double_t sy2=ExpectedSigmaY2(x,t.GetTgl(),t.GetPt());
711       Double_t sz2=ExpectedSigmaZ2(x,t.GetTgl());
712
713       Double_t road;
714       if((t.GetSigmaY2() + sy2) > 0) road=10.*sqrt(t.GetSigmaY2() + sy2);
715       else return expectedNumberOfClusters;
716       
717       wYrt = (Float_t) y;
718       wZrt = (Float_t) z;
719       wYwindow = (Float_t) road;
720       t.GetPxPyPz(Px,Py,Pz);
721       wPx = (Float_t) Px;
722       wPy = (Float_t) Py;
723       wPz = (Float_t) Pz;
724       wC  = (Float_t) t.GetC();
725       wSigmaC2 = (Float_t) t.GetSigmaC2();
726       wSigmaTgl2    = (Float_t) t.GetSigmaTgl2();
727       wSigmaY2 = (Float_t) t.GetSigmaY2();
728       wSigmaZ2 = (Float_t) t.GetSigmaZ2();
729       wChi2 = -1;            
730       
731       if (road>fWideRoad) {
732         if (t.GetNumberOfClusters()>4)
733           cerr<<t.GetNumberOfClusters()
734               <<"FindProlongation warning: Too broad road !\n";
735         return 0;
736       }             
737
738       AliTRDcluster *cl=0;
739       UInt_t index=0;
740
741       Double_t max_chi2=fMaxChi2;
742
743       wYclosest = 12345678;
744       wYcorrect = 12345678;
745       wZclosest = 12345678;
746       wZcorrect = 12345678;
747       wZwindow  = TMath::Sqrt(2.25 * 12 * sz2);   
748
749       // Find the closest correct cluster for debugging purposes
750       if (time_bin) {
751         Float_t minDY = 1000000;
752         for (Int_t i=0; i<time_bin; i++) {
753           AliTRDcluster* c=(AliTRDcluster*)(time_bin[i]);
754           if((c->GetLabel(0) != trackIndex) &&
755              (c->GetLabel(1) != trackIndex) &&
756              (c->GetLabel(2) != trackIndex)) continue;
757           if(TMath::Abs(c->GetY() - y) > minDY) continue;
758           minDY = TMath::Abs(c->GetY() - y);
759           wYcorrect = c->GetY();
760           wZcorrect = c->GetZ();
761
762           Double_t h01 = GetTiltFactor(c);
763           wChi2 = t.GetPredictedChi2(c, h01);
764         }
765       }                    
766
767       // Now go for the real cluster search
768
769       if (time_bin) {
770
771         for (Int_t i=time_bin.Find(y-road); i<time_bin; i++) {
772           AliTRDcluster* c=(AliTRDcluster*)(time_bin[i]);
773           if (c->GetY() > y+road) break;
774           if (c->IsUsed() > 0) continue;
775           if((c->GetZ()-z)*(c->GetZ()-z) > 3 * sz2) continue;
776
777           Double_t h01 = GetTiltFactor(c);
778           Double_t chi2=t.GetPredictedChi2(c,h01);
779           
780           if (chi2 > max_chi2) continue;
781           max_chi2=chi2;
782           cl=c;
783           index=time_bin.GetIndex(i);
784         }               
785
786         if(!cl) {
787
788           for (Int_t i=time_bin.Find(y-road); i<time_bin; i++) {
789             AliTRDcluster* c=(AliTRDcluster*)(time_bin[i]);
790             
791             if (c->GetY() > y+road) break;
792             if (c->IsUsed() > 0) continue;
793             if((c->GetZ()-z)*(c->GetZ()-z) > 12 * sz2) continue;
794             
795             Double_t h01 = GetTiltFactor(c);
796             Double_t chi2=t.GetPredictedChi2(c, h01);
797             
798             if (chi2 > max_chi2) continue;
799             max_chi2=chi2;
800             cl=c;
801             index=time_bin.GetIndex(i);
802           }
803         }        
804         
805
806         if (cl) {
807           wYclosest = cl->GetY();
808           wZclosest = cl->GetZ();
809           Double_t h01 = GetTiltFactor(cl);
810
811           t.SetSampledEdx(cl->GetQ()/dx,t.GetNumberOfClusters()); 
812           if(!t.Update(cl,max_chi2,index,h01)) {
813             if(!try_again--) return 0;
814           }  
815           else try_again=fMaxGap;
816         }
817         else {
818           if (try_again==0) break; 
819           try_again--;
820         }
821
822         /*
823         if((((Int_t) wTB)%15 == 0) || (((Int_t) wTB)%15 == 14)) {
824           
825           printf(" %f", wIndex);       //1
826           printf(" %f", wTB);          //2
827           printf(" %f", wYrt);         //3
828           printf(" %f", wYclosest);    //4
829           printf(" %f", wYcorrect);    //5
830           printf(" %f", wYwindow);     //6
831           printf(" %f", wZrt);         //7
832           printf(" %f", wZclosest);    //8
833           printf(" %f", wZcorrect);    //9
834           printf(" %f", wZwindow);     //10
835           printf(" %f", wPx);          //11
836           printf(" %f", wPy);          //12
837           printf(" %f", wPz);          //13
838           printf(" %f", wSigmaC2*1000000);  //14
839           printf(" %f", wSigmaTgl2*1000);   //15
840           printf(" %f", wSigmaY2);     //16
841           //      printf(" %f", wSigmaZ2);     //17
842           printf(" %f", wChi2);     //17
843           printf(" %f", wC);           //18
844           printf("\n");
845         } 
846         */                        
847       }
848     }  
849   }
850   return expectedNumberOfClusters;
851   
852   
853 }                
854
855 //___________________________________________________________________
856
857 Int_t AliTRDtracker::FollowBackProlongation(AliTRDtrack& t)
858 {
859   // Starting from current radial position of track <t> this function
860   // extrapolates the track up to outer timebin and in the sensitive
861   // layers confirms prolongation if a close cluster is found. 
862   // Returns the number of clusters expected to be found in sensitive layers
863
864   Float_t  wIndex, wTB, wChi2;
865   Float_t  wYrt, wYclosest, wYcorrect, wYwindow;
866   Float_t  wZrt, wZclosest, wZcorrect, wZwindow;
867   Float_t  wPx, wPy, wPz, wC;
868   Double_t Px, Py, Pz;
869   Float_t  wSigmaC2, wSigmaTgl2, wSigmaY2, wSigmaZ2;
870
871   Int_t trackIndex = t.GetLabel();  
872
873   Int_t ns=Int_t(2*TMath::Pi()/AliTRDgeometry::GetAlpha()+0.5);     
874
875   Int_t try_again=fMaxGap;
876
877   Double_t alpha=t.GetAlpha();
878
879   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
880   if (alpha < 0.            ) alpha += 2.*TMath::Pi();
881
882   Int_t s=Int_t(alpha/AliTRDgeometry::GetAlpha())%AliTRDgeometry::kNsect;  
883
884   Int_t outerTB = fTrSec[0]->GetOuterTimeBin();
885
886   Double_t rad_length, rho, x, dx, y, ymax, z;
887
888   Bool_t lookForCluster;
889
890   Int_t expectedNumberOfClusters = 0;
891   x = t.GetX();
892
893   alpha=AliTRDgeometry::GetAlpha();  // note: change in meaning
894
895
896   for (Int_t nr=fTrSec[0]->GetLayerNumber(t.GetX()); nr<outerTB; nr++) { 
897
898     y = t.GetY(); z = t.GetZ();
899
900     // first propagate to the outer surface of the current time bin 
901
902     fTrSec[s]->GetLayer(nr)->GetPropagationParameters(y,z,dx,rho,rad_length,lookForCluster);
903     x = fTrSec[s]->GetLayer(nr)->GetX()+dx/2; y = t.GetY(); z = t.GetZ();
904
905     if(!t.PropagateTo(x,rad_length,rho)) break;
906
907     y = t.GetY();
908     ymax = x*TMath::Tan(0.5*alpha);
909
910     if (y > ymax) {
911       s = (s+1) % ns;
912       if (!t.Rotate(alpha)) break;
913       if(!t.PropagateTo(x,rad_length,rho)) break;
914     } else if (y <-ymax) {
915       s = (s-1+ns) % ns;                           
916       if (!t.Rotate(-alpha)) break;   
917       if(!t.PropagateTo(x,rad_length,rho)) break;
918     } 
919     y = t.GetY(); z = t.GetZ();
920
921     // now propagate to the middle plane of the next time bin 
922     fTrSec[s]->GetLayer(nr+1)->GetPropagationParameters(y,z,dx,rho,rad_length,lookForCluster);
923
924     x = fTrSec[s]->GetLayer(nr+1)->GetX(); y = t.GetY(); z = t.GetZ();
925
926     if(!t.PropagateTo(x,rad_length,rho)) break;
927
928     y = t.GetY();
929
930     ymax = x*TMath::Tan(0.5*alpha);
931
932     if(fVocal) printf("nr+1=%d, x %f, z %f, y %f, ymax %f\n",nr+1,x,z,y,ymax);
933
934     if (y > ymax) {
935       s = (s+1) % ns;
936       if (!t.Rotate(alpha)) break;
937       if(!t.PropagateTo(x,rad_length,rho)) break;
938     } else if (y <-ymax) {
939       s = (s-1+ns) % ns;              
940       if (!t.Rotate(-alpha)) break;   
941       if(!t.PropagateTo(x,rad_length,rho)) break;
942     } 
943
944     //    printf("label %d, pl %d, lookForCluster %d \n",
945     //     trackIndex, nr+1, lookForCluster);
946
947     if(lookForCluster) {
948       expectedNumberOfClusters++;       
949
950       wIndex = (Float_t) t.GetLabel();
951       wTB = fTrSec[s]->GetLayer(nr+1)->GetTimeBinIndex();
952
953       AliTRDpropagationLayer& time_bin=*(fTrSec[s]->GetLayer(nr+1));
954       Double_t sy2=ExpectedSigmaY2(t.GetX(),t.GetTgl(),t.GetPt());
955       Double_t sz2=ExpectedSigmaZ2(t.GetX(),t.GetTgl());
956       if((t.GetSigmaY2() + sy2) < 0) break;
957       Double_t road = 10.*sqrt(t.GetSigmaY2() + sy2); 
958       Double_t y=t.GetY(), z=t.GetZ();
959
960       wYrt = (Float_t) y;
961       wZrt = (Float_t) z;
962       wYwindow = (Float_t) road;
963       t.GetPxPyPz(Px,Py,Pz);
964       wPx = (Float_t) Px;
965       wPy = (Float_t) Py;
966       wPz = (Float_t) Pz;
967       wC  = (Float_t) t.GetC();
968       wSigmaC2 = (Float_t) t.GetSigmaC2();
969       wSigmaTgl2    = (Float_t) t.GetSigmaTgl2();
970       wSigmaY2 = (Float_t) t.GetSigmaY2();
971       wSigmaZ2 = (Float_t) t.GetSigmaZ2();
972       wChi2 = -1;            
973       
974       if (road>fWideRoad) {
975         if (t.GetNumberOfClusters()>4)
976           cerr<<t.GetNumberOfClusters()
977               <<"FindProlongation warning: Too broad road !\n";
978         return 0;
979       }      
980
981       AliTRDcluster *cl=0;
982       UInt_t index=0;
983
984       Double_t max_chi2=fMaxChi2;
985
986       wYclosest = 12345678;
987       wYcorrect = 12345678;
988       wZclosest = 12345678;
989       wZcorrect = 12345678;
990       wZwindow  = TMath::Sqrt(2.25 * 12 * sz2);   
991
992       // Find the closest correct cluster for debugging purposes
993       if (time_bin) {
994         Float_t minDY = 1000000;
995         for (Int_t i=0; i<time_bin; i++) {
996           AliTRDcluster* c=(AliTRDcluster*)(time_bin[i]);
997           if((c->GetLabel(0) != trackIndex) &&
998              (c->GetLabel(1) != trackIndex) &&
999              (c->GetLabel(2) != trackIndex)) continue;
1000           if(TMath::Abs(c->GetY() - y) > minDY) continue;
1001           minDY = TMath::Abs(c->GetY() - y);
1002           wYcorrect = c->GetY();
1003           wZcorrect = c->GetZ();
1004
1005           Double_t h01 = GetTiltFactor(c);
1006           wChi2 = t.GetPredictedChi2(c, h01);
1007         }
1008       }                    
1009
1010       // Now go for the real cluster search
1011
1012       if (time_bin) {
1013
1014         for (Int_t i=time_bin.Find(y-road); i<time_bin; i++) {
1015           AliTRDcluster* c=(AliTRDcluster*)(time_bin[i]);
1016           if (c->GetY() > y+road) break;
1017           //          if (c->IsUsed() > 0) continue;
1018           if((c->GetZ()-z)*(c->GetZ()-z) > 3 * sz2) continue;
1019
1020           Double_t h01 = GetTiltFactor(c);
1021           Double_t chi2=t.GetPredictedChi2(c,h01);
1022           
1023           if (chi2 > max_chi2) continue;
1024           max_chi2=chi2;
1025           cl=c;
1026           index=time_bin.GetIndex(i);
1027         }               
1028         
1029         if(!cl) {
1030
1031           for (Int_t i=time_bin.Find(y-road); i<time_bin; i++) {
1032             AliTRDcluster* c=(AliTRDcluster*)(time_bin[i]);
1033             
1034             if (c->GetY() > y+road) break;
1035             //            if (c->IsUsed() > 0) continue;
1036             if((c->GetZ()-z)*(c->GetZ()-z) > 2.25 * 12 * sz2) continue;
1037             
1038             Double_t h01 = GetTiltFactor(c);
1039             Double_t chi2=t.GetPredictedChi2(c,h01);
1040             
1041             if (chi2 > max_chi2) continue;
1042             max_chi2=chi2;
1043             cl=c;
1044             index=time_bin.GetIndex(i);
1045           }
1046         }        
1047         
1048         if (cl) {
1049           wYclosest = cl->GetY();
1050           wZclosest = cl->GetZ();
1051
1052           t.SetSampledEdx(cl->GetQ()/dx,t.GetNumberOfClusters()); 
1053           Double_t h01 = GetTiltFactor(cl);
1054           if(!t.Update(cl,max_chi2,index,h01)) {
1055             if(!try_again--) return 0;
1056           }  
1057           else try_again=fMaxGap;
1058         }
1059         else {
1060           if (try_again==0) break; 
1061           try_again--;
1062         }
1063
1064         /*
1065         if((((Int_t) wTB)%15 == 0) || (((Int_t) wTB)%15 == 14)) {
1066           
1067           printf(" %f", wIndex);       //1
1068           printf(" %f", wTB);          //2
1069           printf(" %f", wYrt);         //3
1070           printf(" %f", wYclosest);    //4
1071           printf(" %f", wYcorrect);    //5
1072           printf(" %f", wYwindow);     //6
1073           printf(" %f", wZrt);         //7
1074           printf(" %f", wZclosest);    //8
1075           printf(" %f", wZcorrect);    //9
1076           printf(" %f", wZwindow);     //10
1077           printf(" %f", wPx);          //11
1078           printf(" %f", wPy);          //12
1079           printf(" %f", wPz);          //13
1080           printf(" %f", wSigmaC2*1000000);  //14
1081           printf(" %f", wSigmaTgl2*1000);   //15
1082           printf(" %f", wSigmaY2);     //16
1083           //      printf(" %f", wSigmaZ2);     //17
1084           printf(" %f", wChi2);     //17
1085           printf(" %f", wC);           //18
1086           printf("\n");
1087         } 
1088         */                        
1089       }
1090     }  
1091   }
1092   return expectedNumberOfClusters;
1093 }         
1094
1095 //___________________________________________________________________
1096
1097 Int_t AliTRDtracker::PropagateToOuterPlane(AliTRDtrack& t, Double_t xToGo)
1098 {
1099   // Starting from current radial position of track <t> this function
1100   // extrapolates the track up to radial position <xToGo>. 
1101   // Returns 1 if track reaches the plane, and 0 otherwise 
1102
1103   Int_t ns=Int_t(2*TMath::Pi()/AliTRDgeometry::GetAlpha()+0.5);     
1104
1105   Double_t alpha=t.GetAlpha();
1106
1107   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
1108   if (alpha < 0.            ) alpha += 2.*TMath::Pi();
1109
1110   Int_t s=Int_t(alpha/AliTRDgeometry::GetAlpha())%AliTRDgeometry::kNsect;  
1111
1112   Bool_t lookForCluster;
1113   Double_t rad_length, rho, x, dx, y, ymax, z;
1114
1115   x = t.GetX();
1116
1117   alpha=AliTRDgeometry::GetAlpha();  // note: change in meaning
1118
1119   Int_t plToGo = fTrSec[0]->GetLayerNumber(xToGo);
1120
1121   for (Int_t nr=fTrSec[0]->GetLayerNumber(x); nr<plToGo; nr++) { 
1122
1123     y = t.GetY(); z = t.GetZ();
1124
1125     // first propagate to the outer surface of the current time bin 
1126     fTrSec[s]->GetLayer(nr)->GetPropagationParameters(y,z,dx,rho,rad_length,lookForCluster);
1127     x = fTrSec[s]->GetLayer(nr)->GetX()+dx/2; y = t.GetY(); z = t.GetZ();
1128     if(!t.PropagateTo(x,rad_length,rho)) return 0;
1129     y = t.GetY();
1130     ymax = x*TMath::Tan(0.5*alpha);
1131     if (y > ymax) {
1132       s = (s+1) % ns;
1133       if (!t.Rotate(alpha)) return 0;
1134     } else if (y <-ymax) {
1135       s = (s-1+ns) % ns;                           
1136       if (!t.Rotate(-alpha)) return 0;   
1137     } 
1138     if(!t.PropagateTo(x,rad_length,rho)) return 0;
1139
1140     y = t.GetY(); z = t.GetZ();
1141
1142     // now propagate to the middle plane of the next time bin 
1143     fTrSec[s]->GetLayer(nr+1)->GetPropagationParameters(y,z,dx,rho,rad_length,lookForCluster);
1144     x = fTrSec[s]->GetLayer(nr+1)->GetX(); y = t.GetY(); z = t.GetZ();
1145     if(!t.PropagateTo(x,rad_length,rho)) return 0;
1146     y = t.GetY();
1147     ymax = x*TMath::Tan(0.5*alpha);
1148     if (y > ymax) {
1149       s = (s+1) % ns;
1150       if (!t.Rotate(alpha)) return 0;
1151     } else if (y <-ymax) {
1152       s = (s-1+ns) % ns;                           
1153       if (!t.Rotate(-alpha)) return 0;   
1154     } 
1155     if(!t.PropagateTo(x,rad_length,rho)) return 0;
1156   }
1157   return 1;
1158 }         
1159
1160 //___________________________________________________________________
1161
1162 Int_t AliTRDtracker::PropagateToTPC(AliTRDtrack& t)
1163 {
1164   // Starting from current radial position of track <t> this function
1165   // extrapolates the track up to radial position of the outermost
1166   // padrow of the TPC. 
1167   // Returns 1 if track reaches the TPC, and 0 otherwise 
1168
1169   Int_t ns=Int_t(2*TMath::Pi()/AliTRDgeometry::GetAlpha()+0.5);     
1170
1171   Double_t alpha=t.GetAlpha();
1172
1173   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
1174   if (alpha < 0.            ) alpha += 2.*TMath::Pi();
1175
1176   Int_t s=Int_t(alpha/AliTRDgeometry::GetAlpha())%AliTRDgeometry::kNsect;  
1177
1178   Bool_t lookForCluster;
1179   Double_t rad_length, rho, x, dx, y, ymax, z;
1180
1181   x = t.GetX();
1182
1183   alpha=AliTRDgeometry::GetAlpha();  // note: change in meaning
1184
1185   Int_t plTPC = fTrSec[0]->GetLayerNumber(246.055);
1186
1187   for (Int_t nr=fTrSec[0]->GetLayerNumber(x); nr>plTPC; nr--) { 
1188
1189     y = t.GetY(); z = t.GetZ();
1190
1191     // first propagate to the outer surface of the current time bin 
1192     fTrSec[s]->GetLayer(nr)->GetPropagationParameters(y,z,dx,rho,rad_length,lookForCluster);
1193     x = fTrSec[s]->GetLayer(nr)->GetX()-dx/2; y = t.GetY(); z = t.GetZ();
1194     if(!t.PropagateTo(x,rad_length,rho)) return 0;
1195     y = t.GetY();
1196     ymax = x*TMath::Tan(0.5*alpha);
1197     if (y > ymax) {
1198       s = (s+1) % ns;
1199       if (!t.Rotate(alpha)) return 0;
1200     } else if (y <-ymax) {
1201       s = (s-1+ns) % ns;                           
1202       if (!t.Rotate(-alpha)) return 0;   
1203     } 
1204     if(!t.PropagateTo(x,rad_length,rho)) return 0;
1205
1206     y = t.GetY(); z = t.GetZ();
1207
1208     // now propagate to the middle plane of the next time bin 
1209     fTrSec[s]->GetLayer(nr-1)->GetPropagationParameters(y,z,dx,rho,rad_length,lookForCluster);
1210     x = fTrSec[s]->GetLayer(nr-1)->GetX(); y = t.GetY(); z = t.GetZ();
1211     if(!t.PropagateTo(x,rad_length,rho)) return 0;
1212     y = t.GetY();
1213     ymax = x*TMath::Tan(0.5*alpha);
1214     if (y > ymax) {
1215       s = (s+1) % ns;
1216       if (!t.Rotate(alpha)) return 0;
1217     } else if (y <-ymax) {
1218       s = (s-1+ns) % ns;                           
1219       if (!t.Rotate(-alpha)) return 0;   
1220     } 
1221     if(!t.PropagateTo(x,rad_length,rho)) return 0;
1222   }
1223   return 1;
1224 }         
1225
1226
1227 //_____________________________________________________________________________
1228 void AliTRDtracker::LoadEvent()
1229 {
1230   // Fills clusters into TRD tracking_sectors 
1231   // Note that the numbering scheme for the TRD tracking_sectors 
1232   // differs from that of TRD sectors
1233
1234   ReadClusters(fClusters);
1235   Int_t ncl=fClusters->GetEntriesFast();
1236   cout<<"\n LoadSectors: sorting "<<ncl<<" clusters"<<endl;
1237               
1238   UInt_t index;
1239   while (ncl--) {
1240 //    printf("\r %d left  ",ncl); 
1241     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(ncl);
1242     Int_t detector=c->GetDetector(), local_time_bin=c->GetLocalTimeBin();
1243     Int_t sector=fGeom->GetSector(detector);
1244     Int_t plane=fGeom->GetPlane(detector);
1245
1246     Int_t tracking_sector = CookSectorIndex(sector);
1247
1248     Int_t gtb = fTrSec[tracking_sector]->CookTimeBinIndex(plane,local_time_bin);
1249     if(gtb < 0) continue; 
1250     Int_t layer = fTrSec[tracking_sector]->GetLayerNumber(gtb);
1251
1252     index=ncl;
1253     fTrSec[tracking_sector]->GetLayer(layer)->InsertCluster(c,index);
1254   }    
1255   printf("\r\n");
1256
1257 }
1258
1259 //_____________________________________________________________________________
1260 void AliTRDtracker::UnloadEvent() 
1261
1262   //
1263   // Clears the arrays of clusters and tracks. Resets sectors and timebins 
1264   //
1265
1266   Int_t i, nentr;
1267
1268   nentr = fClusters->GetEntriesFast();
1269   for (i = 0; i < nentr; i++) delete fClusters->RemoveAt(i);
1270
1271   nentr = fSeeds->GetEntriesFast();
1272   for (i = 0; i < nentr; i++) delete fSeeds->RemoveAt(i);
1273
1274   nentr = fTracks->GetEntriesFast();
1275   for (i = 0; i < nentr; i++) delete fTracks->RemoveAt(i);
1276
1277   Int_t nsec = AliTRDgeometry::kNsect;
1278
1279   for (i = 0; i < nsec; i++) {    
1280     for(Int_t pl = 0; pl < fTrSec[i]->GetNumberOfLayers(); pl++) {
1281       fTrSec[i]->GetLayer(pl)->Clear();
1282     }
1283   }
1284
1285 }
1286
1287 //__________________________________________________________________________
1288 void AliTRDtracker::MakeSeeds(Int_t inner, Int_t outer, Int_t turn)
1289 {
1290   // Creates track seeds using clusters in timeBins=i1,i2
1291
1292   if(turn > 2) {
1293     cerr<<"MakeSeeds: turn "<<turn<<" exceeds the limit of 2"<<endl;
1294     return;
1295   }
1296
1297   Double_t x[5], c[15];
1298   Int_t max_sec=AliTRDgeometry::kNsect;
1299   
1300   Double_t alpha=AliTRDgeometry::GetAlpha();
1301   Double_t shift=AliTRDgeometry::GetAlpha()/2.;
1302   Double_t cs=cos(alpha), sn=sin(alpha);
1303   Double_t cs2=cos(2.*alpha), sn2=sin(2.*alpha);
1304     
1305       
1306   Int_t i2 = fTrSec[0]->GetLayerNumber(inner);
1307   Int_t i1 = fTrSec[0]->GetLayerNumber(outer);
1308       
1309   Double_t x1 =fTrSec[0]->GetX(i1);
1310   Double_t xx2=fTrSec[0]->GetX(i2);
1311       
1312   for (Int_t ns=0; ns<max_sec; ns++) {
1313     
1314     Int_t nl2 = *(fTrSec[(ns-2+max_sec)%max_sec]->GetLayer(i2));
1315     Int_t nl=(*fTrSec[(ns-1+max_sec)%max_sec]->GetLayer(i2));
1316     Int_t nm=(*fTrSec[ns]->GetLayer(i2));
1317     Int_t nu=(*fTrSec[(ns+1)%max_sec]->GetLayer(i2));
1318     Int_t nu2=(*fTrSec[(ns+2)%max_sec]->GetLayer(i2));
1319     
1320     AliTRDpropagationLayer& r1=*(fTrSec[ns]->GetLayer(i1));
1321     
1322     for (Int_t is=0; is < r1; is++) {
1323       Double_t y1=r1[is]->GetY(), z1=r1[is]->GetZ();
1324       
1325       for (Int_t js=0; js < nl2+nl+nm+nu+nu2; js++) {
1326         
1327         const AliTRDcluster *cl;
1328         Double_t x2,   y2,   z2;
1329         Double_t x3=0., y3=0.;   
1330         
1331         if (js<nl2) {
1332           if(turn != 2) continue;
1333           AliTRDpropagationLayer& r2=*(fTrSec[(ns-2+max_sec)%max_sec]->GetLayer(i2));
1334           cl=r2[js];
1335           y2=cl->GetY(); z2=cl->GetZ();
1336           
1337           x2= xx2*cs2+y2*sn2;
1338           y2=-xx2*sn2+y2*cs2;
1339         }
1340         else if (js<nl2+nl) {
1341           if(turn != 1) continue;
1342           AliTRDpropagationLayer& r2=*(fTrSec[(ns-1+max_sec)%max_sec]->GetLayer(i2));
1343           cl=r2[js-nl2];
1344           y2=cl->GetY(); z2=cl->GetZ();
1345           
1346           x2= xx2*cs+y2*sn;
1347           y2=-xx2*sn+y2*cs;
1348         }                                
1349         else if (js<nl2+nl+nm) {
1350           if(turn != 1) continue;
1351           AliTRDpropagationLayer& r2=*(fTrSec[ns]->GetLayer(i2));
1352           cl=r2[js-nl2-nl];
1353           x2=xx2; y2=cl->GetY(); z2=cl->GetZ();
1354         }
1355         else if (js<nl2+nl+nm+nu) {
1356           if(turn != 1) continue;
1357           AliTRDpropagationLayer& r2=*(fTrSec[(ns+1)%max_sec]->GetLayer(i2));
1358           cl=r2[js-nl2-nl-nm];
1359           y2=cl->GetY(); z2=cl->GetZ();
1360           
1361           x2=xx2*cs-y2*sn;
1362           y2=xx2*sn+y2*cs;
1363         }              
1364         else {
1365           if(turn != 2) continue;
1366           AliTRDpropagationLayer& r2=*(fTrSec[(ns+2)%max_sec]->GetLayer(i2));
1367           cl=r2[js-nl2-nl-nm-nu];
1368           y2=cl->GetY(); z2=cl->GetZ();
1369           
1370           x2=xx2*cs2-y2*sn2;
1371           y2=xx2*sn2+y2*cs2;
1372         }
1373         
1374         if(TMath::Abs(z1-z2) > fMaxSeedDeltaZ12) continue;
1375         
1376         Double_t zz=z1 - z1/x1*(x1-x2);
1377         
1378         if (TMath::Abs(zz-z2)>fMaxSeedDeltaZ) continue;
1379         
1380         Double_t d=(x2-x1)*(0.-y2)-(0.-x2)*(y2-y1);
1381         if (d==0.) {cerr<<"TRD MakeSeeds: Straight seed !\n"; continue;}
1382         
1383         x[0]=y1;
1384         x[1]=z1;
1385         x[4]=f1trd(x1,y1,x2,y2,x3,y3);
1386         
1387         if (TMath::Abs(x[4]) > fMaxSeedC) continue;      
1388         
1389         x[2]=f2trd(x1,y1,x2,y2,x3,y3);
1390         
1391         if (TMath::Abs(x[4]*x1-x[2]) >= 0.99999) continue;
1392         
1393         x[3]=f3trd(x1,y1,x2,y2,z1,z2);
1394         
1395         if (TMath::Abs(x[3]) > fMaxSeedTan) continue;
1396         
1397         Double_t a=asin(x[2]);
1398         Double_t zv=z1 - x[3]/x[4]*(a+asin(x[4]*x1-x[2]));
1399         
1400         if (TMath::Abs(zv)>fMaxSeedVertexZ) continue;
1401         
1402         Double_t sy1=r1[is]->GetSigmaY2(), sz1=r1[is]->GetSigmaZ2();
1403         Double_t sy2=cl->GetSigmaY2(),     sz2=cl->GetSigmaZ2();
1404         Double_t sy3=fSeedErrorSY3, sy=fSeedErrorSY, sz=fSeedErrorSZ;  
1405
1406         // Tilt changes
1407         Double_t h01 = GetTiltFactor(r1[is]);
1408         Double_t xu_factor = 100.;
1409         if(fNoTilt) { 
1410           h01 = 0;
1411           xu_factor = 1;
1412         }
1413
1414         sy1=sy1+sz1*h01*h01;
1415         Double_t syz=sz1*(-h01);
1416         // end of tilt changes
1417         
1418         Double_t f40=(f1trd(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
1419         Double_t f42=(f1trd(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
1420         Double_t f43=(f1trd(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
1421         Double_t f20=(f2trd(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
1422         Double_t f22=(f2trd(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
1423         Double_t f23=(f2trd(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
1424         Double_t f30=(f3trd(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
1425         Double_t f31=(f3trd(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
1426         Double_t f32=(f3trd(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
1427         Double_t f34=(f3trd(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;    
1428
1429         
1430         c[0]=sy1;
1431         //        c[1]=0.;       c[2]=sz1;
1432         c[1]=syz;       c[2]=sz1*xu_factor;
1433         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
1434         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
1435                        c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
1436         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
1437         c[13]=f30*sy1*f40+f32*sy2*f42;
1438         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;      
1439         
1440         UInt_t index=r1.GetIndex(is);
1441         
1442         AliTRDtrack *track=new AliTRDtrack(r1[is],index,x,c,x1,ns*alpha+shift);
1443
1444         Int_t rc=FollowProlongation(*track, i2);     
1445         
1446         if ((rc < 1) ||
1447             (track->GetNumberOfClusters() < 
1448              (outer-inner)*fMinClustersInSeed)) delete track;
1449         else {
1450           fSeeds->AddLast(track); fNseeds++;
1451 //          cerr<<"\r found seed "<<fNseeds;
1452         }
1453       }
1454     }
1455   }
1456 }            
1457
1458 //_____________________________________________________________________________
1459 void AliTRDtracker::ReadClusters(TObjArray *array, const TFile *inp) 
1460 {
1461   //
1462   // Reads AliTRDclusters (option >= 0) or AliTRDrecPoints (option < 0) 
1463   // from the file. The names of the cluster tree and branches 
1464   // should match the ones used in AliTRDclusterizer::WriteClusters()
1465   //
1466
1467   TDirectory *savedir=gDirectory; 
1468
1469   if (inp) {
1470      TFile *in=(TFile*)inp;
1471      if (!in->IsOpen()) {
1472         cerr<<"AliTRDtracker::ReadClusters(): input file is not open !\n";
1473         return;
1474      }
1475      else{
1476        in->cd();
1477      }
1478   }
1479
1480   Char_t treeName[12];
1481   sprintf(treeName,"TreeR%d_TRD",GetEventNumber());
1482   TTree *ClusterTree = (TTree*) gDirectory->Get(treeName);
1483   
1484   TObjArray *ClusterArray = new TObjArray(400); 
1485   
1486   ClusterTree->GetBranch("TRDcluster")->SetAddress(&ClusterArray); 
1487   
1488   Int_t nEntries = (Int_t) ClusterTree->GetEntries();
1489   printf("found %d entries in %s.\n",nEntries,ClusterTree->GetName());
1490   
1491   // Loop through all entries in the tree
1492   Int_t nbytes;
1493   AliTRDcluster *c = 0;
1494   printf("\n");
1495
1496   for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {    
1497     
1498     // Import the tree
1499     nbytes += ClusterTree->GetEvent(iEntry);  
1500     
1501     // Get the number of points in the detector
1502     Int_t nCluster = ClusterArray->GetEntriesFast();  
1503 //    printf("\r Read %d clusters from entry %d", nCluster, iEntry);
1504     
1505     // Loop through all TRD digits
1506     for (Int_t iCluster = 0; iCluster < nCluster; iCluster++) { 
1507       c = (AliTRDcluster*)ClusterArray->UncheckedAt(iCluster);
1508       AliTRDcluster *co = new AliTRDcluster(*c);
1509       co->SetSigmaY2(c->GetSigmaY2() * fSY2corr);
1510       Int_t ltb = co->GetLocalTimeBin();
1511       if(ltb == 19) co->SetSigmaZ2(c->GetSigmaZ2());
1512       else if(fNoTilt) co->SetSigmaZ2(c->GetSigmaZ2() * fSZ2corr);
1513       array->AddLast(co);
1514       delete ClusterArray->RemoveAt(iCluster); 
1515     }
1516   }
1517
1518   delete ClusterArray;
1519   savedir->cd();   
1520
1521 }
1522
1523 //______________________________________________________________________
1524 void AliTRDtracker::ReadClusters(TObjArray *array, const Char_t *filename)
1525 {
1526   //
1527   // Reads AliTRDclusters from file <filename>. The names of the cluster
1528   // tree and branches should match the ones used in
1529   // AliTRDclusterizer::WriteClusters()
1530   // if <array> == 0, clusters are added into AliTRDtracker fCluster array
1531   //
1532
1533   TDirectory *savedir=gDirectory;
1534
1535   TFile *file = TFile::Open(filename);
1536   if (!file->IsOpen()) {
1537     cerr<<"Can't open file with TRD clusters"<<endl;
1538     return;
1539   }
1540
1541   Char_t treeName[12];
1542   sprintf(treeName,"TreeR%d_TRD",GetEventNumber());
1543   TTree *ClusterTree = (TTree*) gDirectory->Get(treeName);
1544
1545   if (!ClusterTree) {
1546      cerr<<"AliTRDtracker::ReadClusters(): ";
1547      cerr<<"can't get a tree with clusters !\n";
1548      return;
1549   }
1550
1551   TObjArray *ClusterArray = new TObjArray(400);
1552
1553   ClusterTree->GetBranch("TRDcluster")->SetAddress(&ClusterArray);
1554
1555   Int_t nEntries = (Int_t) ClusterTree->GetEntries();
1556   cout<<"found "<<nEntries<<" in ClusterTree"<<endl;   
1557
1558   // Loop through all entries in the tree
1559   Int_t nbytes;
1560   AliTRDcluster *c = 0;
1561
1562   printf("\n");
1563
1564   for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
1565
1566     // Import the tree
1567     nbytes += ClusterTree->GetEvent(iEntry);
1568
1569     // Get the number of points in the detector
1570     Int_t nCluster = ClusterArray->GetEntriesFast();
1571     printf("\n Read %d clusters from entry %d", nCluster, iEntry);
1572
1573     // Loop through all TRD digits
1574     for (Int_t iCluster = 0; iCluster < nCluster; iCluster++) {
1575       c = (AliTRDcluster*)ClusterArray->UncheckedAt(iCluster);
1576       AliTRDcluster *co = new AliTRDcluster(*c);
1577       co->SetSigmaY2(c->GetSigmaY2() * fSY2corr);
1578       Int_t ltb = co->GetLocalTimeBin();
1579       if(ltb == 19) co->SetSigmaZ2(c->GetSigmaZ2());
1580       else if(fNoTilt) co->SetSigmaZ2(c->GetSigmaZ2() * fSZ2corr);
1581       array->AddLast(co);
1582       delete ClusterArray->RemoveAt(iCluster);
1583     }
1584   }
1585
1586   file->Close();
1587   delete ClusterArray;
1588   savedir->cd();
1589
1590 }                      
1591
1592
1593 //__________________________________________________________________
1594 void AliTRDtracker::CookLabel(AliKalmanTrack* pt, Float_t wrong) const {
1595
1596   Int_t label=123456789, index, i, j;
1597   Int_t ncl=pt->GetNumberOfClusters();
1598   const Int_t range = fTrSec[0]->GetOuterTimeBin()+1;
1599
1600   Bool_t label_added;
1601
1602   //  Int_t s[range][2];
1603   Int_t **s = new Int_t* [range];
1604   for (i=0; i<range; i++) {
1605     s[i] = new Int_t[2];
1606   }
1607   for (i=0; i<range; i++) {
1608     s[i][0]=-1;
1609     s[i][1]=0;
1610   }
1611
1612   Int_t t0,t1,t2;
1613   for (i=0; i<ncl; i++) {
1614     index=pt->GetClusterIndex(i);
1615     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(index);
1616     t0=c->GetLabel(0);
1617     t1=c->GetLabel(1);
1618     t2=c->GetLabel(2);
1619   }
1620
1621   for (i=0; i<ncl; i++) {
1622     index=pt->GetClusterIndex(i);
1623     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(index);
1624     for (Int_t k=0; k<3; k++) { 
1625       label=c->GetLabel(k);
1626       label_added=kFALSE; j=0;
1627       if (label >= 0) {
1628         while ( (!label_added) && ( j < range ) ) {
1629           if (s[j][0]==label || s[j][1]==0) {
1630             s[j][0]=label; 
1631             s[j][1]=s[j][1]+1; 
1632             label_added=kTRUE;
1633           }
1634           j++;
1635         }
1636       }
1637     }
1638   }
1639
1640   Int_t max=0;
1641   label = -123456789;
1642
1643   for (i=0; i<range; i++) {
1644     if (s[i][1]>max) {
1645       max=s[i][1]; label=s[i][0];
1646     }
1647   }
1648
1649   for (i=0; i<range; i++) {
1650     delete []s[i];
1651   }        
1652
1653   delete []s;
1654
1655   if ((1.- Float_t(max)/ncl) > wrong) label=-label;   
1656
1657   pt->SetLabel(label); 
1658
1659 }
1660
1661
1662 //__________________________________________________________________
1663 void AliTRDtracker::UseClusters(const AliKalmanTrack* t, Int_t from) const {
1664   Int_t ncl=t->GetNumberOfClusters();
1665   for (Int_t i=from; i<ncl; i++) {
1666     Int_t index = t->GetClusterIndex(i);
1667     AliTRDcluster *c=(AliTRDcluster*)fClusters->UncheckedAt(index);
1668     c->Use();
1669   }
1670 }
1671
1672
1673 //_____________________________________________________________________
1674 Double_t AliTRDtracker::ExpectedSigmaY2(Double_t r, Double_t tgl, Double_t pt)
1675 {
1676   // Parametrised "expected" error of the cluster reconstruction in Y 
1677
1678   Double_t s = 0.08 * 0.08;    
1679   return s;
1680 }
1681
1682 //_____________________________________________________________________
1683 Double_t AliTRDtracker::ExpectedSigmaZ2(Double_t r, Double_t tgl)
1684 {
1685   // Parametrised "expected" error of the cluster reconstruction in Z 
1686
1687   Double_t s = 9 * 9 /12.;  
1688   return s;
1689 }                  
1690
1691
1692 //_____________________________________________________________________
1693 Double_t AliTRDtracker::GetX(Int_t sector, Int_t plane, Int_t local_tb) const 
1694 {
1695   //
1696   // Returns radial position which corresponds to time bin <local_tb>
1697   // in tracking sector <sector> and plane <plane>
1698   //
1699
1700   Int_t index = fTrSec[sector]->CookTimeBinIndex(plane, local_tb); 
1701   Int_t pl = fTrSec[sector]->GetLayerNumber(index);
1702   return fTrSec[sector]->GetLayer(pl)->GetX();
1703
1704 }
1705
1706
1707 //_______________________________________________________
1708 AliTRDtracker::AliTRDpropagationLayer::AliTRDpropagationLayer(Double_t x, 
1709                Double_t dx, Double_t rho, Double_t rad_length, Int_t tb_index)
1710
1711   //
1712   // AliTRDpropagationLayer constructor
1713   //
1714
1715   fN = 0; fX = x; fdX = dx; fRho = rho; fX0 = rad_length;
1716   fClusters = NULL; fIndex = NULL; fTimeBinIndex = tb_index;
1717
1718
1719   for(Int_t i=0; i < (Int_t) kZONES; i++) {
1720     fZc[i]=0; fZmax[i] = 0;
1721   }
1722
1723   fYmax = 0;
1724
1725   if(fTimeBinIndex >= 0) { 
1726     fClusters = new AliTRDcluster*[kMAX_CLUSTER_PER_TIME_BIN];
1727     fIndex = new UInt_t[kMAX_CLUSTER_PER_TIME_BIN];
1728   }
1729
1730   fHole = kFALSE;
1731   fHoleZc = 0;
1732   fHoleZmax = 0;
1733   fHoleYc = 0;
1734   fHoleYmax = 0;
1735   fHoleRho = 0;
1736   fHoleX0 = 0;
1737
1738 }
1739
1740 //_______________________________________________________
1741 void AliTRDtracker::AliTRDpropagationLayer::SetHole(
1742           Double_t Zmax, Double_t Ymax, Double_t rho, 
1743           Double_t rad_length, Double_t Yc, Double_t Zc) 
1744 {
1745   //
1746   // Sets hole in the layer 
1747   //
1748
1749   fHole = kTRUE;
1750   fHoleZc = Zc;
1751   fHoleZmax = Zmax;
1752   fHoleYc = Yc;
1753   fHoleYmax = Ymax;
1754   fHoleRho = rho;
1755   fHoleX0 = rad_length;
1756 }
1757   
1758
1759 //_______________________________________________________
1760 AliTRDtracker::AliTRDtrackingSector::AliTRDtrackingSector(AliTRDgeometry* geo, Int_t gs, AliTRDparameter* par)
1761 {
1762   //
1763   // AliTRDtrackingSector Constructor
1764   //
1765
1766   fGeom = geo;
1767   fPar = par;
1768   fGeomSector = gs;
1769   fTzeroShift = 0.13;
1770   fN = 0;
1771
1772   for(UInt_t i=0; i < kMAX_TIME_BIN_INDEX; i++) fTimeBinIndex[i] = -1;
1773
1774
1775   AliTRDpropagationLayer* ppl;
1776
1777   Double_t x, xin, xout, dx, rho, rad_length;
1778   Int_t    steps;
1779
1780   // set time bins in the gas of the TPC
1781
1782   xin = 246.055; xout = 254.055; steps = 20; dx = (xout-xin)/steps;
1783   rho = 0.9e-3;  rad_length = 28.94;
1784
1785   for(Int_t i=0; i<steps; i++) {
1786     x = xin + i*dx + dx/2;
1787     ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,-1);
1788     InsertLayer(ppl);
1789   }
1790
1791   // set time bins in the outer field cage vessel
1792
1793   dx = 50e-4; xin = xout; xout = xin + dx; rho = 1.71; rad_length = 44.77; // Tedlar
1794   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,rad_length,-1);
1795   InsertLayer(ppl);
1796
1797   dx = 0.02; xin = xout; xout = xin + dx; rho = 1.45; rad_length = 44.86; // prepreg
1798   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,rad_length,-1);
1799   InsertLayer(ppl);
1800
1801   dx = 2.; xin = xout; xout = xin + dx; rho = 1.45*0.02; rad_length = 41.28; // Nomex
1802   steps = 5; dx = (xout - xin)/steps;
1803   for(Int_t i=0; i<steps; i++) {
1804     x = xin + i*dx + dx/2;
1805     ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,-1);
1806     InsertLayer(ppl);
1807   }
1808
1809   dx = 0.02; xin = xout; xout = xin + dx; rho = 1.45; rad_length = 44.86; // prepreg
1810   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,rad_length,-1);
1811   InsertLayer(ppl);
1812
1813   dx = 50e-4; xin = xout; xout = xin + dx; rho = 1.71; rad_length = 44.77; // Tedlar
1814   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,rad_length,-1);
1815   InsertLayer(ppl);
1816
1817
1818   // set time bins in CO2
1819
1820   xin = xout; xout = 275.0; 
1821   steps = 50; dx = (xout - xin)/steps;
1822   rho = 1.977e-3;  rad_length = 36.2;
1823   
1824   for(Int_t i=0; i<steps; i++) {
1825     x = xin + i*dx + dx/2;
1826     ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,-1);
1827     InsertLayer(ppl);
1828   }
1829
1830   // set time bins in the outer containment vessel
1831
1832   dx = 50e-4; xin = xout; xout = xin + dx; rho = 2.7; rad_length = 24.01; // Al
1833   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,rad_length,-1);
1834   InsertLayer(ppl);
1835
1836   dx = 50e-4; xin = xout; xout = xin + dx; rho = 1.71; rad_length = 44.77; // Tedlar
1837   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,rad_length,-1);
1838   InsertLayer(ppl);
1839
1840   dx = 0.06; xin = xout; xout = xin + dx; rho = 1.45; rad_length = 44.86; // prepreg
1841   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,rad_length,-1);
1842   InsertLayer(ppl);
1843
1844   dx = 3.; xin = xout; xout = xin + dx; rho = 1.45*0.02; rad_length = 41.28; // Nomex
1845   steps = 10; dx = (xout - xin)/steps;
1846   for(Int_t i=0; i<steps; i++) {
1847     x = xin + i*dx + dx/2;
1848     ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,-1);
1849     InsertLayer(ppl);
1850   }
1851
1852   dx = 0.06; xin = xout; xout = xin + dx; rho = 1.45; rad_length = 44.86; // prepreg
1853   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,rad_length,-1);
1854   InsertLayer(ppl);
1855
1856   dx = 50e-4; xin = xout; xout = xin + dx; rho = 1.71; rad_length = 44.77; // Tedlar
1857   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,rad_length,-1);
1858   InsertLayer(ppl);
1859   
1860   dx = 50e-4; xin = xout; xout = xin + dx; rho = 2.7; rad_length = 24.01; // Al
1861   ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,rad_length,-1);
1862   InsertLayer(ppl);
1863
1864   Double_t xtrd = (Double_t) fGeom->Rmin();  
1865
1866   // add layers between TPC and TRD (Air temporarily)
1867   xin = xout; xout = xtrd;
1868   steps = 50; dx = (xout - xin)/steps;
1869   rho = 1.2e-3;  rad_length = 36.66;
1870   
1871   for(Int_t i=0; i<steps; i++) {
1872     x = xin + i*dx + dx/2;
1873     ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,-1);
1874     InsertLayer(ppl);
1875   }
1876
1877
1878   Double_t alpha=AliTRDgeometry::GetAlpha();
1879
1880   // add layers for each of the planes
1881
1882   Double_t dxRo = (Double_t) fGeom->CroHght();    // Rohacell 
1883   Double_t dxSpace = (Double_t) fGeom->Cspace();  // Spacing between planes
1884   Double_t dxAmp = (Double_t) fGeom->CamHght();   // Amplification region
1885   Double_t dxDrift = (Double_t) fGeom->CdrHght(); // Drift region  
1886   Double_t dxRad = (Double_t) fGeom->CraHght();   // Radiator
1887   Double_t dxTEC = dxRad + dxDrift + dxAmp + dxRo; 
1888   Double_t dxPlane = dxTEC + dxSpace; 
1889
1890   Int_t tb, tb_index;
1891   const Int_t  nChambers = AliTRDgeometry::Ncham();
1892   Double_t  Ymax = 0, holeYmax = 0;
1893   Double_t *  Zc  = new Double_t[nChambers];
1894   Double_t * Zmax = new Double_t[nChambers];
1895   Double_t  holeZmax = 1000.;   // the whole sector is missing
1896
1897   for(Int_t plane = 0; plane < AliTRDgeometry::Nplan(); plane++) {
1898
1899     // Radiator 
1900     xin = xtrd + plane * dxPlane; xout = xin + dxRad;
1901     steps = 12; dx = (xout - xin)/steps; rho = 0.074; rad_length = 40.6; 
1902     for(Int_t i=0; i<steps; i++) {
1903       x = xin + i*dx + dx/2;
1904       ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,-1);
1905       if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
1906         holeYmax = x*TMath::Tan(0.5*alpha);
1907         ppl->SetHole(holeYmax, holeZmax);
1908       }
1909       if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
1910         holeYmax = x*TMath::Tan(0.5*alpha);
1911         ppl->SetHole(holeYmax, holeZmax);
1912       }
1913       InsertLayer(ppl);
1914     }
1915
1916     Ymax = fGeom->GetChamberWidth(plane)/2;
1917     for(Int_t ch = 0; ch < nChambers; ch++) {
1918       Zmax[ch] = fGeom->GetChamberLength(plane,ch)/2;
1919       Float_t pad = fPar->GetRowPadSize(plane,ch,0);
1920       Float_t row0 = fPar->GetRow0(plane,ch,0);
1921       Int_t nPads = fPar->GetRowMax(plane,ch,0);
1922       Zc[ch] = (pad * nPads)/2 + row0 - pad/2;
1923     }
1924
1925     dx = fPar->GetTimeBinSize(); 
1926     rho = 0.00295 * 0.85; rad_length = 11.0;  
1927
1928     Double_t x0 = (Double_t) fPar->GetTime0(plane);
1929     Double_t xbottom = x0 - dxDrift;
1930     Double_t xtop = x0 + dxAmp;
1931
1932     // Amplification region
1933
1934     steps = (Int_t) (dxAmp/dx);
1935
1936     for(tb = 0; tb < steps; tb++) {
1937       x = x0 + tb * dx + dx/2;
1938       tb_index = CookTimeBinIndex(plane, -tb-1);
1939       ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,tb_index);
1940       ppl->SetYmax(Ymax);
1941       for(Int_t ch = 0; ch < nChambers; ch++) {
1942         ppl->SetZmax(ch, Zc[ch], Zmax[ch]);
1943       }
1944       if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
1945         holeYmax = x*TMath::Tan(0.5*alpha);
1946         ppl->SetHole(holeYmax, holeZmax);
1947       }
1948       if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
1949         holeYmax = x*TMath::Tan(0.5*alpha);
1950         ppl->SetHole(holeYmax, holeZmax);
1951       }
1952       InsertLayer(ppl);
1953     }
1954     tb_index = CookTimeBinIndex(plane, -steps);
1955     x = (x + dx/2 + xtop)/2;
1956     dx = 2*(xtop-x);
1957     ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,tb_index);
1958     ppl->SetYmax(Ymax);
1959     for(Int_t ch = 0; ch < nChambers; ch++) {
1960       ppl->SetZmax(ch, Zc[ch], Zmax[ch]);
1961     }
1962     if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
1963       holeYmax = x*TMath::Tan(0.5*alpha);
1964       ppl->SetHole(holeYmax, holeZmax);
1965     }
1966     if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
1967       holeYmax = x*TMath::Tan(0.5*alpha);
1968       ppl->SetHole(holeYmax, holeZmax);
1969     }
1970     InsertLayer(ppl);
1971
1972     // Drift region
1973     dx = fPar->GetTimeBinSize();
1974     steps = (Int_t) (dxDrift/dx);
1975
1976     for(tb = 0; tb < steps; tb++) {
1977       x = x0 - tb * dx - dx/2;
1978       tb_index = CookTimeBinIndex(plane, tb);
1979
1980       ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,tb_index);
1981       ppl->SetYmax(Ymax);
1982       for(Int_t ch = 0; ch < nChambers; ch++) {
1983         ppl->SetZmax(ch, Zc[ch], Zmax[ch]);
1984       }
1985       if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
1986         holeYmax = x*TMath::Tan(0.5*alpha);
1987         ppl->SetHole(holeYmax, holeZmax);
1988       }
1989       if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
1990         holeYmax = x*TMath::Tan(0.5*alpha);
1991         ppl->SetHole(holeYmax, holeZmax);
1992       }
1993       InsertLayer(ppl);
1994     }
1995     tb_index = CookTimeBinIndex(plane, steps);
1996     x = (x - dx/2 + xbottom)/2;
1997     dx = 2*(x-xbottom);
1998     ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,tb_index);
1999     ppl->SetYmax(Ymax);
2000     for(Int_t ch = 0; ch < nChambers; ch++) {
2001       ppl->SetZmax(ch, Zc[ch], Zmax[ch]);
2002     }
2003     if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
2004       holeYmax = x*TMath::Tan(0.5*alpha);
2005       ppl->SetHole(holeYmax, holeZmax);
2006     }
2007     if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
2008       holeYmax = x*TMath::Tan(0.5*alpha);
2009       ppl->SetHole(holeYmax, holeZmax);
2010     }
2011     InsertLayer(ppl);
2012
2013     // Pad Plane
2014     xin = xtop; dx = 0.025; xout = xin + dx; rho = 1.7; rad_length = 33.0;
2015     ppl = new AliTRDpropagationLayer(xin+dx/2,dx,rho,rad_length,-1);
2016     if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
2017       holeYmax = (xin+dx/2)*TMath::Tan(0.5*alpha);
2018       ppl->SetHole(holeYmax, holeZmax);
2019     }
2020     if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
2021       holeYmax = (xin+dx/2)*TMath::Tan(0.5*alpha);
2022       ppl->SetHole(holeYmax, holeZmax);
2023     }
2024     InsertLayer(ppl);
2025
2026     // Rohacell
2027     xin = xout; xout = xtrd + (plane + 1) * dxPlane - dxSpace;
2028     steps = 5; dx = (xout - xin)/steps; rho = 0.074; rad_length = 40.6; 
2029     for(Int_t i=0; i<steps; i++) {
2030       x = xin + i*dx + dx/2;
2031       ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,-1);
2032       if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
2033         holeYmax = x*TMath::Tan(0.5*alpha);
2034         ppl->SetHole(holeYmax, holeZmax);
2035       }
2036       if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
2037         holeYmax = x*TMath::Tan(0.5*alpha);
2038         ppl->SetHole(holeYmax, holeZmax);
2039       }
2040       InsertLayer(ppl);
2041     }
2042
2043     // Space between the chambers, air
2044     xin = xout; xout = xtrd + (plane + 1) * dxPlane;
2045     steps = 5; dx = (xout - xin)/steps; rho = 1.29e-3; rad_length = 36.66; 
2046     for(Int_t i=0; i<steps; i++) {
2047       x = xin + i*dx + dx/2;
2048       ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,-1);
2049       if((fGeom->GetPHOShole()) && (fGeomSector >= 2) && (fGeomSector <= 6)) {
2050         holeYmax = x*TMath::Tan(0.5*alpha);
2051         ppl->SetHole(holeYmax, holeZmax);
2052       }
2053       if((fGeom->GetRICHhole()) && (fGeomSector >= 12) && (fGeomSector <= 14)) {
2054         holeYmax = x*TMath::Tan(0.5*alpha);
2055         ppl->SetHole(holeYmax, holeZmax);
2056       }
2057       InsertLayer(ppl);
2058     }
2059   }    
2060
2061   // Space between the TRD and RICH
2062   Double_t xRICH = 500.;
2063   xin = xout; xout = xRICH;
2064   steps = 200; dx = (xout - xin)/steps; rho = 1.29e-3; rad_length = 36.66; 
2065   for(Int_t i=0; i<steps; i++) {
2066     x = xin + i*dx + dx/2;
2067     ppl = new AliTRDpropagationLayer(x,dx,rho,rad_length,-1);
2068     InsertLayer(ppl);
2069   }
2070
2071   MapTimeBinLayers();
2072   delete [] Zc;
2073   delete [] Zmax;
2074
2075 }
2076
2077 //______________________________________________________
2078
2079 Int_t  AliTRDtracker::AliTRDtrackingSector::CookTimeBinIndex(Int_t plane, Int_t local_tb) const
2080 {
2081   //
2082   // depending on the digitization parameters calculates "global"
2083   // time bin index for timebin <local_tb> in plane <plane>
2084   //
2085
2086   Double_t dxAmp = (Double_t) fGeom->CamHght();   // Amplification region
2087   Double_t dxDrift = (Double_t) fGeom->CdrHght(); // Drift region  
2088   Double_t dx = (Double_t) fPar->GetTimeBinSize();  
2089
2090   Int_t tbAmp = fPar->GetTimeBefore();
2091   Int_t maxAmp = (Int_t) ((dxAmp+0.000001)/dx);
2092   if(kTRUE) maxAmp = 0;   // intentional until we change parameter class 
2093   Int_t tbDrift = fPar->GetTimeMax();
2094   Int_t maxDrift = (Int_t) ((dxDrift+0.000001)/dx);
2095
2096   Int_t tb_per_plane = TMath::Min(tbAmp,maxAmp) + TMath::Min(tbDrift,maxDrift);
2097
2098   Int_t gtb = (plane+1) * tb_per_plane - local_tb - 1 - TMath::Min(tbAmp,maxAmp);
2099
2100   if((local_tb < 0) && 
2101      (TMath::Abs(local_tb) > TMath::Min(tbAmp,maxAmp))) return -1;
2102   if(local_tb >= TMath::Min(tbDrift,maxDrift)) return -1;
2103
2104   return gtb;
2105
2106
2107 }
2108
2109 //______________________________________________________
2110
2111 void AliTRDtracker::AliTRDtrackingSector::MapTimeBinLayers() 
2112 {
2113   //
2114   // For all sensitive time bins sets corresponding layer index
2115   // in the array fTimeBins 
2116   //
2117
2118   Int_t index;
2119
2120   for(Int_t i = 0; i < fN; i++) {
2121     index = fLayers[i]->GetTimeBinIndex();
2122     
2123     //    printf("gtb %d -> pl %d -> x %f \n", index, i, fLayers[i]->GetX());
2124
2125     if(index < 0) continue;
2126     if(index >= (Int_t) kMAX_TIME_BIN_INDEX) {
2127       printf("*** AliTRDtracker::MapTimeBinLayers: \n");
2128       printf("    index %d exceeds allowed maximum of %d!\n",
2129              index, kMAX_TIME_BIN_INDEX-1);
2130       continue;
2131     }
2132     fTimeBinIndex[index] = i;
2133   }
2134
2135   Double_t x1, dx1, x2, dx2, gap;
2136
2137   for(Int_t i = 0; i < fN-1; i++) {
2138     x1 = fLayers[i]->GetX();
2139     dx1 = fLayers[i]->GetdX();
2140     x2 = fLayers[i+1]->GetX();
2141     dx2 = fLayers[i+1]->GetdX();
2142     gap = (x2 - dx2/2) - (x1 + dx1/2);
2143     if(gap < -0.01) {
2144       printf("*** warning: layers %d and %d are overlayed:\n",i,i+1);
2145       printf("             %f + %f + %f > %f\n", x1, dx1/2, dx2/2, x2);
2146     }
2147     if(gap > 0.01) { 
2148       printf("*** warning: layers %d and %d have a large gap:\n",i,i+1);
2149       printf("             (%f - %f) - (%f + %f) = %f\n", 
2150              x2, dx2/2, x1, dx1, gap);
2151     }
2152   }
2153 }
2154   
2155
2156 //______________________________________________________
2157
2158
2159 Int_t AliTRDtracker::AliTRDtrackingSector::GetLayerNumber(Double_t x) const
2160 {
2161   // 
2162   // Returns the number of time bin which in radial position is closest to <x>
2163   //
2164
2165   if(x >= fLayers[fN-1]->GetX()) return fN-1; 
2166   if(x <= fLayers[0]->GetX()) return 0; 
2167
2168   Int_t b=0, e=fN-1, m=(b+e)/2;
2169   for (; b<e; m=(b+e)/2) {
2170     if (x > fLayers[m]->GetX()) b=m+1;
2171     else e=m;
2172   }
2173   if(TMath::Abs(x - fLayers[m]->GetX()) > 
2174      TMath::Abs(x - fLayers[m+1]->GetX())) return m+1;
2175   else return m;
2176
2177 }
2178
2179 //______________________________________________________
2180
2181 Int_t AliTRDtracker::AliTRDtrackingSector::GetInnerTimeBin() const 
2182 {
2183   // 
2184   // Returns number of the innermost SENSITIVE propagation layer
2185   //
2186
2187   return GetLayerNumber(0);
2188 }
2189
2190 //______________________________________________________
2191
2192 Int_t AliTRDtracker::AliTRDtrackingSector::GetOuterTimeBin() const 
2193 {
2194   // 
2195   // Returns number of the outermost SENSITIVE time bin
2196   //
2197
2198   return GetLayerNumber(GetNumberOfTimeBins() - 1);
2199 }
2200
2201 //______________________________________________________
2202
2203 Int_t AliTRDtracker::AliTRDtrackingSector::GetNumberOfTimeBins() const 
2204 {
2205   // 
2206   // Returns number of SENSITIVE time bins
2207   //
2208
2209   Int_t tb, layer;
2210   for(tb = kMAX_TIME_BIN_INDEX-1; tb >=0; tb--) {
2211     layer = GetLayerNumber(tb);
2212     if(layer>=0) break;
2213   }
2214   return tb+1;
2215 }
2216
2217 //______________________________________________________
2218
2219 void AliTRDtracker::AliTRDtrackingSector::InsertLayer(AliTRDpropagationLayer* pl)
2220
2221   //
2222   // Insert layer <pl> in fLayers array.
2223   // Layers are sorted according to X coordinate.
2224
2225   if ( fN == ((Int_t) kMAX_LAYERS_PER_SECTOR)) {
2226     printf("AliTRDtrackingSector::InsertLayer(): Too many layers !\n");
2227     return;
2228   }
2229   if (fN==0) {fLayers[fN++] = pl; return;}
2230   Int_t i=Find(pl->GetX());
2231
2232   memmove(fLayers+i+1 ,fLayers+i,(fN-i)*sizeof(AliTRDpropagationLayer*));
2233   fLayers[i]=pl; fN++;
2234
2235 }              
2236
2237 //______________________________________________________
2238
2239 Int_t AliTRDtracker::AliTRDtrackingSector::Find(Double_t x) const 
2240 {
2241   //
2242   // Returns index of the propagation layer nearest to X 
2243   //
2244
2245   if (x <= fLayers[0]->GetX()) return 0;
2246   if (x > fLayers[fN-1]->GetX()) return fN;
2247   Int_t b=0, e=fN-1, m=(b+e)/2;
2248   for (; b<e; m=(b+e)/2) {
2249     if (x > fLayers[m]->GetX()) b=m+1;
2250     else e=m;
2251   }
2252   return m;
2253 }             
2254
2255 //______________________________________________________
2256
2257 void AliTRDtracker::AliTRDpropagationLayer::GetPropagationParameters(
2258         Double_t y, Double_t z, Double_t &dx, Double_t &rho, Double_t &rad_length, 
2259         Bool_t &lookForCluster) const
2260 {
2261   //
2262   // Returns radial step <dx>, density <rho>, rad. length <rad_length>,
2263   // and sensitivity <lookForCluster> in point <y,z>  
2264   //
2265
2266   dx  = fdX;
2267   rho = fRho;
2268   rad_length  = fX0;
2269   lookForCluster = kFALSE;
2270
2271   // check dead regions
2272   if(fTimeBinIndex >= 0) {
2273     for(Int_t ch = 0; ch < (Int_t) kZONES; ch++) {
2274       if(TMath::Abs(z - fZc[ch]) < fZmax[ch]) 
2275         lookForCluster = kTRUE;
2276       //  else { rho = 1.7; rad_length = 33.0; } // G10 
2277     }
2278     if(TMath::Abs(y) > fYmax) lookForCluster = kFALSE;
2279     if(!lookForCluster) { 
2280       //      rho = 1.7; rad_length = 33.0; // G10 
2281     }
2282   }
2283
2284   // check hole
2285   if(fHole && (TMath::Abs(y - fHoleYc) < fHoleYmax) && 
2286               (TMath::Abs(z - fHoleZc) < fHoleZmax)) {
2287     lookForCluster = kFALSE;
2288     rho = fHoleRho;
2289     rad_length  = fHoleX0;
2290   }         
2291
2292   return;
2293 }
2294
2295 //______________________________________________________
2296
2297 void AliTRDtracker::AliTRDpropagationLayer::InsertCluster(AliTRDcluster* c, 
2298                                                           UInt_t index) {
2299
2300 // Insert cluster in cluster array.
2301 // Clusters are sorted according to Y coordinate.  
2302
2303   if(fTimeBinIndex < 0) { 
2304     printf("*** attempt to insert cluster into non-sensitive time bin!\n");
2305     return;
2306   }
2307
2308   if (fN== (Int_t) kMAX_CLUSTER_PER_TIME_BIN) {
2309     printf("AliTRDpropagationLayer::InsertCluster(): Too many clusters !\n"); 
2310     return;
2311   }
2312   if (fN==0) {fIndex[0]=index; fClusters[fN++]=c; return;}
2313   Int_t i=Find(c->GetY());
2314   memmove(fClusters+i+1 ,fClusters+i,(fN-i)*sizeof(AliTRDcluster*));
2315   memmove(fIndex   +i+1 ,fIndex   +i,(fN-i)*sizeof(UInt_t)); 
2316   fIndex[i]=index; fClusters[i]=c; fN++;
2317 }  
2318
2319 //______________________________________________________
2320
2321 Int_t AliTRDtracker::AliTRDpropagationLayer::Find(Double_t y) const {
2322
2323 // Returns index of the cluster nearest in Y    
2324
2325   if (y <= fClusters[0]->GetY()) return 0;
2326   if (y > fClusters[fN-1]->GetY()) return fN;
2327   Int_t b=0, e=fN-1, m=(b+e)/2;
2328   for (; b<e; m=(b+e)/2) {
2329     if (y > fClusters[m]->GetY()) b=m+1;
2330     else e=m;
2331   }
2332   return m;
2333 }    
2334
2335 //---------------------------------------------------------
2336
2337 Double_t AliTRDtracker::GetTiltFactor(const AliTRDcluster* c) {
2338 //
2339 //  Returns correction factor for tilted pads geometry 
2340 //
2341
2342   Double_t h01 = sin(TMath::Pi() / 180.0 * fPar->GetTiltingAngle());
2343   Int_t det = c->GetDetector();    
2344   Int_t plane = fGeom->GetPlane(det);
2345
2346   if((plane == 1) || (plane == 3) || (plane == 5)) h01=-h01;
2347
2348   if(fNoTilt) h01 = 0;
2349   
2350   return h01;
2351 }
2352
2353
2354
2355
2356
2357