]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITStrackerV2.cxx
VertexerZ updated: no histograms for Z finding, new definition of errors
[u/mrichter/AliRoot.git] / ITS / AliITStrackerV2.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 //               Implementation of the ITS tracker class
18 //    It reads AliITSRecPoint clusters and creates AliITStrackV2 tracks
19 //                   and fills with them the ESD
20 //          Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
21 //     dEdx analysis by: Boris Batyunya, JINR, Boris.Batiounia@cern.ch
22 //-------------------------------------------------------------------------
23
24 #include <new>
25
26 #include <TFile.h>
27 #include <TTree.h>
28 #include <TRandom.h>
29 #include <TGeoMatrix.h>
30
31 #include "AliITSgeom.h"
32 #include "AliAlignObj.h"
33 #include "AliITSRecPoint.h"
34 #include "AliESD.h"
35 #include "AliITSRecPoint.h"
36 #include "AliITStrackerV2.h"
37
38 ClassImp(AliITStrackerV2)
39
40 AliITStrackerV2::AliITSlayer AliITStrackerV2::fgLayers[kMaxLayer]; //ITS layers
41
42 AliITStrackerV2::AliITStrackerV2(): 
43   AliTracker(), 
44   fI(kMaxLayer),
45   fBestTrack(),
46   fTrackToFollow(),
47   fPass(0),
48   fLastLayerToTrackTo(kLastLayerToTrackTo)
49 {
50   //--------------------------------------------------------------------
51   //This is the AliITStrackerV2 default constructor
52   //--------------------------------------------------------------------
53
54   for (Int_t i=1; i<kMaxLayer+1; i++) new(fgLayers+i-1) AliITSlayer();
55
56   fConstraint[0]=1; fConstraint[1]=0;
57
58   Double_t xyz[]={kXV,kYV,kZV}, ers[]={kSigmaXV,kSigmaYV,kSigmaZV}; 
59   SetVertex(xyz,ers);
60
61   for (Int_t i=0; i<kMaxLayer; i++) fLayersNotToSkip[i]=kLayersNotToSkip[i];
62
63 }
64
65 AliITStrackerV2::AliITStrackerV2(const AliITStrackerV2 &t): 
66   AliTracker(t), 
67   fI(t.fI),
68   fBestTrack(t.fBestTrack),
69   fTrackToFollow(t.fTrackToFollow),
70   fPass(t.fPass),
71   fLastLayerToTrackTo(t.fLastLayerToTrackTo)
72 {
73   //--------------------------------------------------------------------
74   //This is the AliITStrackerV2 copy constructor
75   //--------------------------------------------------------------------
76
77   //for (Int_t i=1; i<kMaxLayer+1; i++) new(fgLayers+i-1) AliITSlayer();
78
79   fConstraint[0]=t.fConstraint[0]; fConstraint[1]=t.fConstraint[1];
80
81   Double_t xyz[]={kXV,kYV,kZV}, ers[]={kSigmaXV,kSigmaYV,kSigmaZV};
82   xyz[0]=t.GetX(); xyz[1]=t.GetY(); xyz[2]=t.GetZ(); 
83   ers[0]=t.GetSigmaX(); ers[1]=t.GetSigmaY(); ers[2]=t.GetSigmaZ(); 
84   SetVertex(xyz,ers);
85
86   for (Int_t i=0; i<kMaxLayer; i++) fLayersNotToSkip[i]=t.fLayersNotToSkip[i];
87
88 }
89
90 AliITStrackerV2::AliITStrackerV2(const AliITSgeom *geom) : 
91   AliTracker(), 
92   fI(kMaxLayer),
93   fBestTrack(),
94   fTrackToFollow(),
95   fPass(0),
96   fLastLayerToTrackTo(kLastLayerToTrackTo)
97 {
98   //--------------------------------------------------------------------
99   //This is the AliITStrackerV2 constructor
100   //--------------------------------------------------------------------
101   AliITSgeom *g=(AliITSgeom*)geom;
102
103   Float_t x,y,z;  Int_t i;
104   for (i=1; i<kMaxLayer+1; i++) {
105     Int_t nlad=g->GetNladders(i);
106     Int_t ndet=g->GetNdetectors(i);
107
108     g->GetTrans(i,1,1,x,y,z); 
109     Double_t poff=TMath::ATan2(y,x);
110     Double_t zoff=z;
111     Double_t r=TMath::Sqrt(x*x + y*y);
112
113     g->GetTrans(i,1,2,x,y,z);
114     r += TMath::Sqrt(x*x + y*y);
115     g->GetTrans(i,2,1,x,y,z);
116     r += TMath::Sqrt(x*x + y*y);
117     g->GetTrans(i,2,2,x,y,z);
118     r += TMath::Sqrt(x*x + y*y);
119     r*=0.25;
120
121     new (fgLayers+i-1) AliITSlayer(r,poff,zoff,nlad,ndet);
122
123     for (Int_t j=1; j<nlad+1; j++) {
124       for (Int_t k=1; k<ndet+1; k++) { //Fill this layer with detectors
125         Float_t x,y,zshift; g->GetTrans(i,j,k,x,y,zshift); 
126         Double_t rot[9]; g->GetRotMatrix(i,j,k,rot);
127
128         Double_t phi=TMath::ATan2(rot[1],rot[0])+TMath::Pi();
129         phi+=TMath::Pi()/2;
130         if (i==1) phi+=TMath::Pi();
131
132         if (phi<0) phi+=TMath::TwoPi();
133         else if (phi>=TMath::TwoPi()) phi-=TMath::TwoPi();
134
135         Double_t cp=TMath::Cos(phi), sp=TMath::Sin(phi);
136         Double_t r=x*cp+y*sp;
137
138         AliITSdetector &det=fgLayers[i-1].GetDetector((j-1)*ndet + k-1); 
139         new(&det) AliITSdetector(r,phi); 
140       } 
141     }  
142
143   }
144
145   fConstraint[0]=1; fConstraint[1]=0;
146
147   Double_t xyz[]={kXV,kYV,kZV}, ers[]={kSigmaXV,kSigmaYV,kSigmaZV}; 
148   SetVertex(xyz,ers);
149
150   for (Int_t i=0; i<kMaxLayer; i++) fLayersNotToSkip[i]=kLayersNotToSkip[i];
151
152 }
153
154 void AliITStrackerV2::SetLayersNotToSkip(Int_t *l) {
155   //--------------------------------------------------------------------
156   //This function set masks of the layers which must be not skipped
157   //--------------------------------------------------------------------
158   for (Int_t i=0; i<kMaxLayer; i++) fLayersNotToSkip[i]=l[i];
159 }
160
161 Int_t AliITStrackerV2::LoadClusters(TTree *cTree) {
162   //--------------------------------------------------------------------
163   //This function loads ITS clusters
164   //--------------------------------------------------------------------
165   TBranch *branch=cTree->GetBranch("ITSRecPoints");
166   if (!branch) { 
167     Error("LoadClusters"," can't get the branch !\n");
168     return 1;
169   }
170
171   TClonesArray dummy("AliITSRecPoint",10000), *clusters=&dummy;
172   branch->SetAddress(&clusters);
173
174   Int_t j=0;
175   for (Int_t i=0; i<kMaxLayer; i++) {
176     Int_t ndet=fgLayers[i].GetNdetectors();
177     Int_t jmax = j + fgLayers[i].GetNladders()*ndet;
178
179     Double_t r=fgLayers[i].GetR();
180     Double_t circ=TMath::TwoPi()*r;
181
182     for (; j<jmax; j++) {           
183       if (!cTree->GetEvent(j)) continue;
184       Int_t ncl=clusters->GetEntriesFast();
185       while (ncl--) {
186         AliITSRecPoint *c=(AliITSRecPoint*)clusters->UncheckedAt(ncl);
187
188         Int_t idx=c->GetDetectorIndex();
189         AliITSdetector &det=fgLayers[i].GetDetector(idx);
190    
191         //Shift the cluster to the misaligned position
192         Double_t x=det.GetR(); //y=...;      z=...;
193         c->SetX(x);            //c->SetY(y); c->SetZ(z); 
194
195         Double_t y=r*det.GetPhi()+c->GetY();
196         if (y>circ) y-=circ; else if (y<0) y+=circ;
197         c->SetPhiR(y);
198
199         fgLayers[i].InsertCluster(new AliITSRecPoint(*c));
200       }
201       clusters->Delete();
202     }
203     fgLayers[i].ResetRoad(); //road defined by the cluster density
204   }
205
206   return 0;
207 }
208
209 void AliITStrackerV2::UnloadClusters() {
210   //--------------------------------------------------------------------
211   //This function unloads ITS clusters
212   //--------------------------------------------------------------------
213   for (Int_t i=0; i<kMaxLayer; i++) fgLayers[i].ResetClusters();
214 }
215
216 static Int_t CorrectForDeadZoneMaterial(AliITStrackV2 *t) {
217   //--------------------------------------------------------------------
218   // Correction for the material between the TPC and the ITS
219   // (should it belong to the TPC code ?)
220   //--------------------------------------------------------------------
221   Double_t riw=80., diw=0.0053, x0iw=30; // TPC inner wall ? 
222   Double_t rcd=61., dcd=0.0053, x0cd=30; // TPC "central drum" ?
223   Double_t yr=12.8, dr=0.03; // rods ?
224   Double_t zm=0.2, dm=0.40;  // membrane
225   //Double_t rr=52., dr=0.19, x0r=24., yyr=7.77; //rails
226   Double_t rs=50., ds=0.001; // something belonging to the ITS (screen ?)
227
228   if (t->GetX() > riw) {
229      if (!t->PropagateTo(riw,diw,x0iw)) return 1;
230      if (TMath::Abs(t->GetY())>yr) t->CorrectForMaterial(dr);
231      if (TMath::Abs(t->GetZ())<zm) t->CorrectForMaterial(dm);
232      if (!t->PropagateTo(rcd,dcd,x0cd)) return 1;
233      //Double_t x,y,z; t->GetGlobalXYZat(rr,x,y,z);
234      //if (TMath::Abs(y)<yyr) t->PropagateTo(rr,dr,x0r); 
235      if (!t->PropagateTo(rs,ds)) return 1;
236   } else if (t->GetX() < rs) {
237      if (!t->PropagateTo(rs,-ds)) return 1;
238      //Double_t x,y,z; t->GetGlobalXYZat(rr,x,y,z);
239      //if (TMath::Abs(y)<yyr) t->PropagateTo(rr,-dr,x0r); 
240      if (!t->PropagateTo(rcd,-dcd,x0cd)) return 1;
241      if (!t->PropagateTo(riw+0.001,-diw,x0iw)) return 1;
242   } else {
243   ::Error("CorrectForDeadZoneMaterial","track is already in the dead zone !");
244     return 1;
245   }
246   
247   return 0;
248 }
249
250 Int_t AliITStrackerV2::Clusters2Tracks(AliESD *event) {
251   //--------------------------------------------------------------------
252   // This functions reconstructs ITS tracks
253   // The clusters must be already loaded !
254   //--------------------------------------------------------------------
255   TObjArray itsTracks(15000);
256
257   {/* Read ESD tracks */
258     Int_t nentr=event->GetNumberOfTracks();
259     Info("Clusters2Tracks", "Number of ESD tracks: %d\n", nentr);
260     while (nentr--) {
261       AliESDtrack *esd=event->GetTrack(nentr);
262
263       if ((esd->GetStatus()&AliESDtrack::kTPCin)==0) continue;
264       if (esd->GetStatus()&AliESDtrack::kTPCout) continue;
265       if (esd->GetStatus()&AliESDtrack::kITSin) continue;
266
267       AliITStrackV2 *t=0;
268       try {
269         t=new AliITStrackV2(*esd);
270       } catch (const Char_t *msg) {
271         Warning("Clusters2Tracks",msg);
272         delete t;
273         continue;
274       }
275       if (TMath::Abs(t->GetD(GetX(),GetY()))>4) {
276         delete t;
277         continue;
278       }
279
280       if (CorrectForDeadZoneMaterial(t)!=0) {
281          Warning("Clusters2Tracks",
282                  "failed to correct for the material in the dead zone !\n");
283          delete t;
284          continue;
285       }
286       itsTracks.AddLast(t);
287     }
288   } /* End Read ESD tracks */
289
290   itsTracks.Sort();
291   Int_t nentr=itsTracks.GetEntriesFast();
292
293   Int_t ntrk=0;
294   for (fPass=0; fPass<2; fPass++) {
295      Int_t &constraint=fConstraint[fPass]; if (constraint<0) continue;
296      for (Int_t i=0; i<nentr; i++) {
297        AliITStrackV2 *t=(AliITStrackV2*)itsTracks.UncheckedAt(i);
298        if (t==0) continue;           //this track has been already tracked
299        Int_t tpcLabel=t->GetLabel(); //save the TPC track label
300
301        ResetTrackToFollow(*t);
302        ResetBestTrack();
303
304        for (FollowProlongation(); fI<kMaxLayer; fI++) {
305           while (TakeNextProlongation()) FollowProlongation();
306        }
307
308        if (fBestTrack.GetNumberOfClusters() == 0) continue;
309
310        if (fConstraint[fPass]) {
311           ResetTrackToFollow(*t);
312           if (!RefitAt(3.7, &fTrackToFollow, &fBestTrack)) continue;
313           ResetBestTrack();
314        }
315
316        fBestTrack.SetLabel(tpcLabel);
317        fBestTrack.CookdEdx();
318        CookLabel(&fBestTrack,0.); //For comparison only
319        fBestTrack.UpdateESDtrack(AliESDtrack::kITSin);
320        UseClusters(&fBestTrack);
321        delete itsTracks.RemoveAt(i);
322        ntrk++;
323      }
324   }
325
326   itsTracks.Delete();
327
328   Info("Clusters2Tracks","Number of prolonged tracks: %d\n",ntrk);
329
330   return 0;
331 }
332
333 Int_t AliITStrackerV2::PropagateBack(AliESD *event) {
334   //--------------------------------------------------------------------
335   // This functions propagates reconstructed ITS tracks back
336   // The clusters must be loaded !
337   //--------------------------------------------------------------------
338   Int_t nentr=event->GetNumberOfTracks();
339   Info("PropagateBack", "Number of ESD tracks: %d\n", nentr);
340
341   Int_t ntrk=0;
342   for (Int_t i=0; i<nentr; i++) {
343      AliESDtrack *esd=event->GetTrack(i);
344
345      if ((esd->GetStatus()&AliESDtrack::kITSin)==0) continue;
346      if (esd->GetStatus()&AliESDtrack::kITSout) continue;
347
348      AliITStrackV2 *t=0;
349      try {
350         t=new AliITStrackV2(*esd);
351      } catch (const Char_t *msg) {
352         Warning("PropagateBack",msg);
353         delete t;
354         continue;
355      }
356
357      ResetTrackToFollow(*t);
358
359      // propagete to vertex [SR, GSI 17.02.2003]
360      // Start Time measurement [SR, GSI 17.02.2003], corrected by I.Belikov
361      if (fTrackToFollow.PropagateTo(3.,0.0028,65.19)) {
362        if (fTrackToFollow.PropagateToVertex(event->GetVertex())) {
363           fTrackToFollow.StartTimeIntegral();
364        }
365        fTrackToFollow.PropagateTo(3.,-0.0028,65.19);
366      }
367
368      fTrackToFollow.ResetCovariance(10.); fTrackToFollow.ResetClusters();
369      if (RefitAt(49.,&fTrackToFollow,t)) {
370         if (CorrectForDeadZoneMaterial(&fTrackToFollow)!=0) {
371           Warning("PropagateBack",
372                   "failed to correct for the material in the dead zone !\n");
373           delete t;
374           continue;
375         }
376         fTrackToFollow.SetLabel(t->GetLabel());
377         //fTrackToFollow.CookdEdx();
378         CookLabel(&fTrackToFollow,0.); //For comparison only
379         fTrackToFollow.UpdateESDtrack(AliESDtrack::kITSout);
380         UseClusters(&fTrackToFollow);
381         ntrk++;
382      }
383      delete t;
384   }
385
386   Info("PropagateBack","Number of back propagated ITS tracks: %d\n",ntrk);
387
388   return 0;
389 }
390
391 Int_t AliITStrackerV2::RefitInward(AliESD *event) {
392   //--------------------------------------------------------------------
393   // This functions refits ITS tracks using the 
394   // "inward propagated" TPC tracks
395   // The clusters must be loaded !
396   //--------------------------------------------------------------------
397   Int_t nentr=event->GetNumberOfTracks();
398   Info("RefitInward", "Number of ESD tracks: %d\n", nentr);
399
400   Int_t ntrk=0;
401   for (Int_t i=0; i<nentr; i++) {
402     AliESDtrack *esd=event->GetTrack(i);
403
404     if ((esd->GetStatus()&AliESDtrack::kITSout) == 0) continue;
405     if (esd->GetStatus()&AliESDtrack::kITSrefit) continue;
406     if (esd->GetStatus()&AliESDtrack::kTPCout)
407     if ((esd->GetStatus()&AliESDtrack::kTPCrefit)==0) continue;
408
409     AliITStrackV2 *t=0;
410     try {
411         t=new AliITStrackV2(*esd);
412     } catch (const Char_t *msg) {
413         Warning("RefitInward",msg);
414         delete t;
415         continue;
416     }
417
418     if (CorrectForDeadZoneMaterial(t)!=0) {
419        Warning("RefitInward",
420                "failed to correct for the material in the dead zone !\n");
421        delete t;
422        continue;
423     }
424
425     ResetTrackToFollow(*t);
426     fTrackToFollow.ResetClusters();
427
428     //Refitting...
429     if (RefitAt(3.7, &fTrackToFollow, t, kTRUE)) {
430        fTrackToFollow.SetLabel(t->GetLabel());
431        fTrackToFollow.CookdEdx();
432        CookLabel(&fTrackToFollow,0.); //For comparison only
433
434        if (fTrackToFollow.PropagateTo(3.,0.0028,65.19)) {//The beam pipe 
435          AliESDtrack  *esdTrack =fTrackToFollow.GetESDtrack();
436          esdTrack->UpdateTrackParams(&fTrackToFollow,AliESDtrack::kITSrefit);
437          Float_t r[3]={0.,0.,0.};
438          Double_t maxD=3.;
439          esdTrack->RelateToVertex(event->GetVertex(),GetBz(r),maxD);
440          ntrk++;
441        }
442     }
443     delete t;
444   }
445
446   Info("RefitInward","Number of refitted tracks: %d\n",ntrk);
447
448   return 0;
449 }
450
451 AliCluster *AliITStrackerV2::GetCluster(Int_t index) const {
452   //--------------------------------------------------------------------
453   //       Return pointer to a given cluster
454   //--------------------------------------------------------------------
455   Int_t l=(index & 0xf0000000) >> 28;
456   Int_t c=(index & 0x0fffffff) >> 00;
457   return fgLayers[l].GetCluster(c);
458 }
459
460
461 void AliITStrackerV2::FollowProlongation() {
462   //--------------------------------------------------------------------
463   //This function finds a track prolongation 
464   //--------------------------------------------------------------------
465   while (fI>fLastLayerToTrackTo) {
466     Int_t i=fI-1;
467
468     AliITSlayer &layer=fgLayers[i];
469     AliITStrackV2 &track=fTracks[i];
470
471     Double_t r=layer.GetR();
472
473     if (i==3 || i==1) {
474        Double_t rs=0.5*(fgLayers[i+1].GetR() + r);
475        Double_t d=0.0034, x0=38.6;
476        if (i==1) {rs=9.; d=0.0097; x0=42;}
477        if (!fTrackToFollow.PropagateTo(rs,d,x0)) {
478          //Warning("FollowProlongation","propagation failed !\n");
479          return;
480        }
481     }
482
483     //find intersection
484     Double_t x,y,z;  
485     if (!fTrackToFollow.GetGlobalXYZat(r,x,y,z)) {
486       //Warning("FollowProlongation","failed to estimate track !\n");
487       return;
488     }
489     Double_t phi=TMath::ATan2(y,x);
490
491     Int_t idet=layer.FindDetectorIndex(phi,z);
492     if (idet<0) {
493       //Warning("FollowProlongation","failed to find a detector !\n");
494       return;
495     }
496
497     //propagate to the intersection
498     const AliITSdetector &det=layer.GetDetector(idet);
499     phi=det.GetPhi();
500     if (!fTrackToFollow.Propagate(phi,det.GetR())) {
501       //Warning("FollowProlongation","propagation failed !\n");
502       return;
503     }
504     fTrackToFollow.SetDetectorIndex(idet);
505
506     //Select possible prolongations and store the current track estimation
507     track.~AliITStrackV2(); new(&track) AliITStrackV2(fTrackToFollow);
508     Double_t dz=7*TMath::Sqrt(track.GetSigmaZ2() + kSigmaZ2[i]);
509     Double_t dy=7*TMath::Sqrt(track.GetSigmaY2() + kSigmaY2[i]);
510     Double_t road=layer.GetRoad();
511     if (dz*dy>road*road) {
512        Double_t dd=TMath::Sqrt(dz*dy), scz=dz/dd, scy=dy/dd;
513        dz=road*scz; dy=road*scy;
514     } 
515
516     //Double_t dz=4*TMath::Sqrt(track.GetSigmaZ2() + kSigmaZ2[i]);
517     if (dz < 0.5*TMath::Abs(track.GetTgl())) dz=0.5*TMath::Abs(track.GetTgl());
518     if (dz > kMaxRoad) {
519       //Warning("FollowProlongation","too broad road in Z !\n");
520       return;
521     }
522
523     if (TMath::Abs(fTrackToFollow.GetZ()-GetZ()) > r+dz) return;
524
525     //Double_t dy=4*TMath::Sqrt(track.GetSigmaY2() + kSigmaY2[i]);
526     if (dy < 0.5*TMath::Abs(track.GetSnp())) dy=0.5*TMath::Abs(track.GetSnp());
527     if (dy > kMaxRoad) {
528       //Warning("FollowProlongation","too broad road in Y !\n");
529       return;
530     }
531
532     fI--;
533
534     Double_t zmin=track.GetZ() - dz; 
535     Double_t zmax=track.GetZ() + dz;
536     Double_t ymin=track.GetY() + r*phi - dy;
537     Double_t ymax=track.GetY() + r*phi + dy;
538     if (layer.SelectClusters(zmin,zmax,ymin,ymax)==0) 
539        if (fLayersNotToSkip[fI]) return;  
540
541     if (!TakeNextProlongation()) 
542        if (fLayersNotToSkip[fI]) return;
543
544   } 
545
546   //deal with the best track
547   Int_t ncl=fTrackToFollow.GetNumberOfClusters();
548   Int_t nclb=fBestTrack.GetNumberOfClusters();
549   if (ncl)
550   if (ncl >= nclb) {
551      Double_t chi2=fTrackToFollow.GetChi2();
552      if (chi2/ncl < kChi2PerCluster) {        
553         if (ncl > nclb || chi2 < fBestTrack.GetChi2()) {
554            ResetBestTrack();
555         }
556      }
557   }
558
559 }
560
561 Int_t AliITStrackerV2::TakeNextProlongation() {
562   //--------------------------------------------------------------------
563   // This function takes another track prolongation 
564   //
565   //  dEdx analysis by: Boris Batyunya, JINR, Boris.Batiounia@cern.ch 
566   //--------------------------------------------------------------------
567   AliITSlayer &layer=fgLayers[fI];
568   ResetTrackToFollow(fTracks[fI]);
569
570   Double_t dz=7*TMath::Sqrt(fTrackToFollow.GetSigmaZ2() + kSigmaZ2[fI]);
571   Double_t dy=7*TMath::Sqrt(fTrackToFollow.GetSigmaY2() + kSigmaY2[fI]);
572   Double_t road=layer.GetRoad();
573   if (dz*dy>road*road) {
574      Double_t dd=TMath::Sqrt(dz*dy), scz=dz/dd, scy=dy/dd;
575      dz=road*scz; dy=road*scy;
576   } 
577
578   const AliITSRecPoint *c=0; Int_t ci=-1;
579   const AliITSRecPoint *cc=0; Int_t cci=-1;
580   Double_t chi2=kMaxChi2;
581   while ((c=layer.GetNextCluster(ci))!=0) {
582     Int_t idet=c->GetDetectorIndex();
583
584     if (fTrackToFollow.GetDetectorIndex()!=idet) {
585        const AliITSdetector &det=layer.GetDetector(idet);
586        ResetTrackToFollow(fTracks[fI]);
587        if (!fTrackToFollow.Propagate(det.GetPhi(),det.GetR())) {
588          //Warning("TakeNextProlongation","propagation failed !\n");
589          continue;
590        }
591        fTrackToFollow.SetDetectorIndex(idet);
592        if (TMath::Abs(fTrackToFollow.GetZ()-GetZ())>layer.GetR()+dz) continue;
593     }
594
595     if (TMath::Abs(fTrackToFollow.GetZ() - c->GetZ()) > dz) continue;
596     if (TMath::Abs(fTrackToFollow.GetY() - c->GetY()) > dy) continue;
597
598     Double_t ch2=fTrackToFollow.GetPredictedChi2(c); 
599     if (ch2 > chi2) continue;
600     chi2=ch2;
601     cc=c; cci=ci;
602     break;
603   }
604
605   if (!cc) return 0;
606
607   if (!fTrackToFollow.PropagateTo(cc->GetX(),0.,0.)) return 0; // Alignment
608   if (!fTrackToFollow.Update(cc,chi2,(fI<<28)+cci)) {
609      //Warning("TakeNextProlongation","filtering failed !\n");
610      return 0;
611   }
612
613   if (fTrackToFollow.GetNumberOfClusters()>1)
614     if (TMath::Abs(fTrackToFollow.GetD(GetX(),GetY()))>4) return 0;
615
616   fTrackToFollow.
617     SetSampledEdx(cc->GetQ(),fTrackToFollow.GetNumberOfClusters()-1); //b.b.
618
619   {
620   Double_t x0;
621  Double_t d=layer.GetThickness(fTrackToFollow.GetY(),fTrackToFollow.GetZ(),x0);
622   fTrackToFollow.CorrectForMaterial(d,x0);
623   }
624
625   if (fConstraint[fPass]) {
626     Double_t d=GetEffectiveThickness(0,0); //Think of this !!!!
627     Double_t xyz[]={GetX(),GetY(),GetZ()};
628     Double_t ers[]={GetSigmaX(),GetSigmaY(),GetSigmaZ()};
629     fTrackToFollow.Improve(d,xyz,ers);
630   }
631
632   return 1;
633 }
634
635
636 AliITStrackerV2::AliITSlayer::AliITSlayer():
637   fR(0.),
638   fPhiOffset(0.),
639   fNladders(0),
640   fZOffset(0.),
641   fNdetectors(0),
642   fDetectors(0),
643   fNsel(0),
644   fRoad(2*fR*TMath::Sqrt(3.14/1.)) //assuming that there's only one cluster
645 {
646   //--------------------------------------------------------------------
647   //default AliITSlayer constructor
648   //--------------------------------------------------------------------
649   
650   for (Int_t i=0; i<kNsector; i++) fN[i]=0;
651
652 }
653
654 AliITStrackerV2::AliITSlayer::
655 AliITSlayer(Double_t r,Double_t p,Double_t z,Int_t nl,Int_t nd): 
656   fR(r), 
657   fPhiOffset(p), 
658   fNladders(nl),
659   fZOffset(z),
660   fNdetectors(nd),
661   fDetectors(new AliITSdetector[nl*nd]),
662   fNsel(0),
663   fRoad(2*r*TMath::Sqrt(3.14/1.)) //assuming that there's only one cluster
664 {
665   //--------------------------------------------------------------------
666   //main AliITSlayer constructor
667   //--------------------------------------------------------------------
668
669   for (Int_t i=0; i<kNsector; i++) fN[i]=0;
670
671   for (Int_t i=0; i<kMaxClusterPerLayer; i++) fClusters[i]=0;
672
673 }
674
675 AliITStrackerV2::AliITSlayer::~AliITSlayer() {
676   //--------------------------------------------------------------------
677   // AliITSlayer destructor
678   //--------------------------------------------------------------------
679   delete[] fDetectors;
680   ResetClusters();
681 }
682
683 void AliITStrackerV2::AliITSlayer::ResetClusters() {
684   //--------------------------------------------------------------------
685   // This function removes loaded clusters
686   //--------------------------------------------------------------------
687    for (Int_t s=0; s<kNsector; s++) {
688        Int_t &n=fN[s];
689        while (n) {
690           n--;
691           delete fClusters[s*kMaxClusterPerSector+n];
692        }
693    }
694 }
695
696 void AliITStrackerV2::AliITSlayer::ResetRoad() {
697   //--------------------------------------------------------------------
698   // This function calculates the road defined by the cluster density
699   //--------------------------------------------------------------------
700   Int_t n=0;
701   for (Int_t s=0; s<kNsector; s++) {
702     Int_t i=fN[s];
703     while (i--) 
704        if (TMath::Abs(fClusters[s*kMaxClusterPerSector+i]->GetZ())<fR) n++;
705   }
706   if (n>1) fRoad=2*fR*TMath::Sqrt(3.14/n);
707 }
708
709 Int_t AliITStrackerV2::AliITSlayer::InsertCluster(AliITSRecPoint *c) {
710   //--------------------------------------------------------------------
711   // This function inserts a cluster to this layer in increasing
712   // order of the cluster's fZ
713   //--------------------------------------------------------------------
714   Float_t circ=TMath::TwoPi()*fR;
715   Int_t sec=Int_t(kNsector*c->GetPhiR()/circ);
716   if (sec>=kNsector) {
717      ::Error("InsertCluster","Wrong sector !\n");
718      return 1;
719   }
720   Int_t &n=fN[sec];
721   if (n>=kMaxClusterPerSector) {
722      ::Error("InsertCluster","Too many clusters !\n");
723      return 1;
724   }
725   if (n==0) fClusters[sec*kMaxClusterPerSector]=c;
726   else {
727      Int_t i=FindClusterIndex(c->GetZ(),sec);
728      Int_t k=n-i+sec*kMaxClusterPerSector;
729      memmove(fClusters+i+1 ,fClusters+i,k*sizeof(AliITSRecPoint*));
730      fClusters[i]=c;
731   }
732   n++;
733   return 0;
734 }
735
736 Int_t 
737 AliITStrackerV2::AliITSlayer::FindClusterIndex(Float_t z,Int_t s) const {
738   //--------------------------------------------------------------------
739   // For the sector "s", this function returns the index of the first 
740   // with its fZ >= "z". 
741   //--------------------------------------------------------------------
742   Int_t nc=fN[s];
743   if (nc==0) return kMaxClusterPerSector*s;
744
745   Int_t b=kMaxClusterPerSector*s;
746   if (z <= fClusters[b]->GetZ()) return b;
747
748   Int_t e=b+nc-1;
749   if (z > fClusters[e]->GetZ()) return e+1;
750
751   Int_t m=(b+e)/2;
752   for (; b<e; m=(b+e)/2) {
753     if (z > fClusters[m]->GetZ()) b=m+1;
754     else e=m; 
755   }
756   return m;
757 }
758
759 Int_t AliITStrackerV2::AliITSlayer::
760 SelectClusters(Float_t zmin,Float_t zmax,Float_t ymin, Float_t ymax) {
761   //--------------------------------------------------------------------
762   // This function selects clusters within the "window"
763   //--------------------------------------------------------------------
764     Float_t circ=fR*TMath::TwoPi();
765
766     if (ymin>circ) ymin-=circ; else if (ymin<0) ymin+=circ;
767     if (ymax>circ) ymax-=circ; else if (ymax<0) ymax+=circ;
768
769     Int_t i1=Int_t(kNsector*ymin/circ); if (i1==kNsector) i1--;
770     if (fN[i1]!=0) {
771        Float_t ym = (ymax<ymin) ? ymax+circ : ymax;
772        Int_t i=FindClusterIndex(zmin,i1), imax=i1*kMaxClusterPerSector+fN[i1];
773        for (; i<imax; i++) {
774            AliITSRecPoint *c=fClusters[i];
775            if (c->IsUsed()) continue;
776            if (c->GetZ()>zmax) break;
777            if (c->GetPhiR()<=ymin) continue;
778            if (c->GetPhiR()>ym) continue;
779            fIndex[fNsel++]=i;
780        }
781     }
782
783     Int_t i2=Int_t(kNsector*ymax/circ); if (i2==kNsector) i2--;
784     if (i2==i1) return fNsel;
785
786     if (fN[i2]!=0) {
787        Float_t ym = (ymin>ymax) ? ymin-circ : ymin;
788        Int_t i=FindClusterIndex(zmin,i2), imax=i2*kMaxClusterPerSector+fN[i2];
789        for (; i<imax; i++) {
790            AliITSRecPoint *c=fClusters[i];
791            if (c->IsUsed()) continue;
792            if (c->GetZ()>zmax) break;
793            if (c->GetPhiR()<=ym) continue;
794            if (c->GetPhiR()>ymax) continue;
795            fIndex[fNsel++]=i;
796        }
797     }
798
799     return fNsel;
800 }
801
802 const AliITSRecPoint *AliITStrackerV2::AliITSlayer::GetNextCluster(Int_t &ci){
803   //--------------------------------------------------------------------
804   // This function returns clusters within the "window" 
805   //--------------------------------------------------------------------
806   AliITSRecPoint *c=0;
807   ci=-1;
808   if (fNsel) {
809      fNsel--;
810      ci=fIndex[fNsel]; 
811      c=fClusters[ci];
812   }
813   return c; 
814 }
815
816 Int_t AliITStrackerV2::AliITSlayer::GetNumberOfClusters() const {
817   Int_t n=0;
818   for (Int_t s=0; s<kNsector; s++) n+=fN[s];
819   return n; 
820 }
821
822 Int_t 
823 AliITStrackerV2::AliITSlayer::FindDetectorIndex(Double_t phi,Double_t z)const {
824   //--------------------------------------------------------------------
825   //This function finds the detector crossed by the track
826   //--------------------------------------------------------------------
827   Double_t dphi=-(phi-fPhiOffset);
828   if      (dphi <  0) dphi += 2*TMath::Pi();
829   else if (dphi >= 2*TMath::Pi()) dphi -= 2*TMath::Pi();
830   Int_t np=Int_t(dphi*fNladders*0.5/TMath::Pi()+0.5);
831   if (np>=fNladders) np-=fNladders;
832   if (np<0)          np+=fNladders;
833
834   Double_t dz=fZOffset-z;
835   Int_t nz=Int_t(dz*(fNdetectors-1)*0.5/fZOffset+0.5);
836   if (nz>=fNdetectors) return -1;
837   if (nz<0)            return -1;
838
839   return np*fNdetectors + nz;
840 }
841
842 Double_t 
843 AliITStrackerV2::AliITSlayer::GetThickness(Double_t y,Double_t z,Double_t &x0)
844 const {
845   //--------------------------------------------------------------------
846   //This function returns the layer thickness at this point (units X0)
847   //--------------------------------------------------------------------
848   Double_t d=0.0085;
849   x0=21.82;
850
851   if (43<fR&&fR<45) { //SSD2
852      Double_t dd=0.0034;
853      d=dd;
854      if (TMath::Abs(y-0.00)>3.40) d+=dd;
855      if (TMath::Abs(y-1.90)<0.45) {d+=(0.013-0.0034);}
856      if (TMath::Abs(y+1.90)<0.45) {d+=(0.013-0.0034);}
857      for (Int_t i=0; i<12; i++) {
858        if (TMath::Abs(z-3.9*(i+0.5))<0.15) {
859           if (TMath::Abs(y-0.00)>3.40) d+=dd;
860           d+=0.0034; 
861           break;
862        }
863        if (TMath::Abs(z+3.9*(i+0.5))<0.15) {
864           if (TMath::Abs(y-0.00)>3.40) d+=dd;
865           d+=0.0034; 
866           break;
867        }         
868        if (TMath::Abs(z-3.4-3.9*i)<0.50) {d+=(0.016-0.0034); break;}
869        if (TMath::Abs(z+0.5+3.9*i)<0.50) {d+=(0.016-0.0034); break;}
870      }
871   } else 
872   if (37<fR&&fR<41) { //SSD1
873      Double_t dd=0.0034;
874      d=dd;
875      if (TMath::Abs(y-0.00)>3.40) d+=dd;
876      if (TMath::Abs(y-1.90)<0.45) {d+=(0.013-0.0034);}
877      if (TMath::Abs(y+1.90)<0.45) {d+=(0.013-0.0034);}
878      for (Int_t i=0; i<11; i++) {
879        if (TMath::Abs(z-3.9*i)<0.15) {
880           if (TMath::Abs(y-0.00)>3.40) d+=dd;
881           d+=dd; 
882           break;
883        }
884        if (TMath::Abs(z+3.9*i)<0.15) {
885           if (TMath::Abs(y-0.00)>3.40) d+=dd;
886           d+=dd; 
887           break;
888        }         
889        if (TMath::Abs(z-1.85-3.9*i)<0.50) {d+=(0.016-0.0034); break;}
890        if (TMath::Abs(z+2.05+3.9*i)<0.50) {d+=(0.016-0.0034); break;}         
891      }
892   } else
893   if (13<fR&&fR<26) { //SDD
894      Double_t dd=0.0033;
895      d=dd;
896      if (TMath::Abs(y-0.00)>3.30) d+=dd;
897
898      if (TMath::Abs(y-1.80)<0.55) {
899         d+=0.016;
900         for (Int_t j=0; j<20; j++) {
901           if (TMath::Abs(z+0.7+1.47*j)<0.12) {d+=0.08; x0=9.; break;}
902           if (TMath::Abs(z-0.7-1.47*j)<0.12) {d+=0.08; x0=9.; break;}
903         } 
904      }
905      if (TMath::Abs(y+1.80)<0.55) {
906         d+=0.016;
907         for (Int_t j=0; j<20; j++) {
908           if (TMath::Abs(z-0.7-1.47*j)<0.12) {d+=0.08; x0=9.; break;}
909           if (TMath::Abs(z+0.7+1.47*j)<0.12) {d+=0.08; x0=9.; break;}
910         } 
911      }
912
913      for (Int_t i=0; i<4; i++) {
914        if (TMath::Abs(z-7.3*i)<0.60) {
915           d+=dd;
916           if (TMath::Abs(y-0.00)>3.30) d+=dd; 
917           break;
918        }
919        if (TMath::Abs(z+7.3*i)<0.60) {
920           d+=dd; 
921           if (TMath::Abs(y-0.00)>3.30) d+=dd; 
922           break;
923        }
924      }
925   } else
926   if (6<fR&&fR<8) {   //SPD2
927      Double_t dd=0.0063; x0=21.5;
928      d=dd;
929      if (TMath::Abs(y-3.08)>0.5) d+=dd;
930      //if (TMath::Abs(y-3.08)>0.45) d+=dd;
931      if (TMath::Abs(y-3.03)<0.10) {d+=0.014;}
932   } else
933   if (3<fR&&fR<5) {   //SPD1
934      Double_t dd=0.0063; x0=21.5;
935      d=dd;
936      if (TMath::Abs(y+0.21)>0.6) d+=dd;
937      //if (TMath::Abs(y+0.21)>0.45) d+=dd;
938      if (TMath::Abs(y+0.10)<0.10) {d+=0.014;}
939   }
940
941   return d;
942 }
943
944 Double_t AliITStrackerV2::GetEffectiveThickness(Double_t y,Double_t z) const
945 {
946   //--------------------------------------------------------------------
947   //Returns the thickness between the current layer and the vertex (units X0)
948   //--------------------------------------------------------------------
949   Double_t d=0.0028*3*3; //beam pipe
950   Double_t x0=0;
951
952   Double_t xn=fgLayers[fI].GetR();
953   for (Int_t i=0; i<fI; i++) {
954     Double_t xi=fgLayers[i].GetR();
955     d+=fgLayers[i].GetThickness(y,z,x0)*xi*xi;
956   }
957
958   if (fI>1) {
959     Double_t xi=9.;
960     d+=0.0097*xi*xi;
961   }
962
963   if (fI>3) {
964     Double_t xi=0.5*(fgLayers[3].GetR()+fgLayers[4].GetR());
965     d+=0.0034*xi*xi;
966   }
967
968   return d/(xn*xn);
969 }
970
971 Bool_t AliITStrackerV2::RefitAt(Double_t xx,AliITStrackV2 *t,
972                                 const AliITStrackV2 *c, Bool_t extra) {
973   //--------------------------------------------------------------------
974   // This function refits the track "t" at the position "x" using
975   // the clusters from "c"
976   // If "extra"==kTRUE, 
977   //    the clusters from overlapped modules get attached to "t" 
978   //--------------------------------------------------------------------
979   Int_t index[kMaxLayer];
980   Int_t k;
981   for (k=0; k<kMaxLayer; k++) index[k]=-1;
982   Int_t nc=c->GetNumberOfClusters();
983   for (k=0; k<nc; k++) { 
984     Int_t idx=c->GetClusterIndex(k),nl=(idx&0xf0000000)>>28;
985     index[nl]=idx; 
986   }
987
988   Int_t from, to, step;
989   if (xx > t->GetX()) {
990       from=0; to=kMaxLayer;
991       step=+1;
992   } else {
993       from=kMaxLayer-1; to=-1;
994       step=-1;
995   }
996
997   for (Int_t i=from; i != to; i += step) {
998      AliITSlayer &layer=fgLayers[i];
999      Double_t r=layer.GetR();
1000  
1001      {
1002      Double_t hI=i-0.5*step; 
1003      if (TMath::Abs(hI-1.5)<0.01 || TMath::Abs(hI-3.5)<0.01) {             
1004         Double_t rs=0.5*(fgLayers[i-step].GetR() + r);
1005         Double_t d=0.0034, x0=38.6; 
1006         if (TMath::Abs(hI-1.5)<0.01) {rs=9.; d=0.0097; x0=42;}
1007         if (!t->PropagateTo(rs,-step*d,x0)) {
1008           return kFALSE;
1009         }
1010      }
1011      }
1012
1013      // remember old position [SR, GSI 18.02.2003]
1014      Double_t oldX=0., oldY=0., oldZ=0.;
1015      if (t->IsStartedTimeIntegral() && step==1) {
1016         t->GetGlobalXYZat(t->GetX(),oldX,oldY,oldZ);
1017      }
1018      //
1019
1020      Double_t x,y,z;
1021      if (!t->GetGlobalXYZat(r,x,y,z)) { 
1022        return kFALSE;
1023      }
1024      Double_t phi=TMath::ATan2(y,x);
1025      Int_t idet=layer.FindDetectorIndex(phi,z);
1026      if (idet<0) { 
1027        return kFALSE;
1028      }
1029      const AliITSdetector &det=layer.GetDetector(idet);
1030      phi=det.GetPhi();
1031      if (!t->Propagate(phi,det.GetR())) {
1032        return kFALSE;
1033      }
1034      t->SetDetectorIndex(idet);
1035
1036      const AliITSRecPoint *cl=0;
1037      Double_t maxchi2=kMaxChi2;
1038
1039      Int_t idx=index[i];
1040      if (idx>0) {
1041         const AliITSRecPoint *c=(AliITSRecPoint *)GetCluster(idx); 
1042         if (idet != c->GetDetectorIndex()) {
1043            idet=c->GetDetectorIndex();
1044            const AliITSdetector &det=layer.GetDetector(idet);
1045            if (!t->Propagate(det.GetPhi(),det.GetR())) {
1046              return kFALSE;
1047            }
1048            t->SetDetectorIndex(idet);
1049         }
1050         Double_t chi2=t->GetPredictedChi2(c);
1051         if (chi2<maxchi2) { 
1052           cl=c; 
1053           maxchi2=chi2; 
1054         } else {
1055           return kFALSE;
1056         }
1057      }
1058  
1059      if (cl) {
1060        if (!t->PropagateTo(cl->GetX(),0.,0.)) return kFALSE; //Alignment
1061        if (!t->Update(cl,maxchi2,idx)) {
1062           return kFALSE;
1063        }
1064        t->SetSampledEdx(cl->GetQ(),t->GetNumberOfClusters()-1);
1065      }
1066
1067      {
1068      Double_t x0;
1069      Double_t d=layer.GetThickness(t->GetY(),t->GetZ(),x0);
1070      t->CorrectForMaterial(-step*d,x0);
1071      }
1072                  
1073      if (extra) { //search for extra clusters
1074         AliITStrackV2 tmp(*t);
1075         Double_t dz=4*TMath::Sqrt(tmp.GetSigmaZ2()+kSigmaZ2[i]);
1076         if (dz < 0.5*TMath::Abs(tmp.GetTgl())) dz=0.5*TMath::Abs(tmp.GetTgl());
1077         Double_t dy=4*TMath::Sqrt(t->GetSigmaY2()+kSigmaY2[i]);
1078         if (dy < 0.5*TMath::Abs(tmp.GetSnp())) dy=0.5*TMath::Abs(tmp.GetSnp());
1079         Double_t zmin=t->GetZ() - dz;
1080         Double_t zmax=t->GetZ() + dz;
1081         Double_t ymin=t->GetY() + phi*r - dy;
1082         Double_t ymax=t->GetY() + phi*r + dy;
1083         layer.SelectClusters(zmin,zmax,ymin,ymax);
1084
1085         const AliITSRecPoint *c=0; Int_t ci=-1,cci=-1;
1086         Double_t maxchi2=1000.*kMaxChi2, tolerance=0.1;
1087         while ((c=layer.GetNextCluster(ci))!=0) {
1088            if (idet == c->GetDetectorIndex()) continue;
1089
1090            const AliITSdetector &det=layer.GetDetector(c->GetDetectorIndex());
1091
1092            if (!tmp.Propagate(det.GetPhi(),det.GetR())) continue;
1093            
1094            if (TMath::Abs(tmp.GetZ() - c->GetZ()) > tolerance) continue;
1095            if (TMath::Abs(tmp.GetY() - c->GetY()) > tolerance) continue;
1096
1097            Double_t chi2=tmp.GetPredictedChi2(c);
1098            if (chi2<maxchi2) { maxchi2=chi2; cci=ci; }
1099         }
1100         if (cci>=0) t->SetExtraCluster(i,(i<<28)+cci);
1101      }
1102
1103      // track time update [SR, GSI 17.02.2003]
1104      if (t->IsStartedTimeIntegral() && step==1) {
1105         Double_t newX, newY, newZ;
1106         t->GetGlobalXYZat(t->GetX(),newX,newY,newZ);
1107         Double_t dL2 = (oldX-newX)*(oldX-newX) + (oldY-newY)*(oldY-newY) + 
1108                        (oldZ-newZ)*(oldZ-newZ);
1109         t->AddTimeStep(TMath::Sqrt(dL2));
1110      }
1111      //
1112
1113   }
1114
1115   if (!t->PropagateTo(xx,0.,0.)) return kFALSE;
1116   return kTRUE;
1117 }
1118
1119 void AliITStrackerV2::UseClusters(const AliKalmanTrack *t, Int_t from) const {
1120   //--------------------------------------------------------------------
1121   // This function marks clusters assigned to the track
1122   //--------------------------------------------------------------------
1123   AliTracker::UseClusters(t,from);
1124
1125   Int_t clusterIndex = t->GetClusterIndex(0);
1126   AliITSRecPoint *c= 0x0;
1127
1128   if (clusterIndex>-1)
1129     c = (AliITSRecPoint *)GetCluster(clusterIndex);
1130   if (c && c->GetSigmaZ2()>0.1) c->UnUse();
1131
1132   c = 0x0;
1133   clusterIndex = t->GetClusterIndex(1);
1134   if (clusterIndex>-1)
1135     c=(AliITSRecPoint *)GetCluster(clusterIndex);
1136   if (c && c->GetSigmaZ2()>0.1) c->UnUse();
1137
1138 }