track labels added
[u/mrichter/AliRoot.git] / ITS / UPGRADE / AliITStrackerUpgrade.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ////////////////////////////////////////////////////
19 //  ITS Upgrade Stand alone tracker class         //
20 //  Authors: A.Mastroserio C.Terrevoli            //
21 //  e-mail:annalisa.mastroserio@cern.ch           //
22 //         cristina.terrevoli@ba.infn.it          //
23 ////////////////////////////////////////////////////
24
25 #include <stdlib.h>
26
27 #include <TArrayI.h>
28 #include <TBranch.h>
29 #include <TObjArray.h>
30 #include <TTree.h>
31 #include "AliRunLoader.h"
32 #include "AliITSLoader.h"
33 #include "AliESDEvent.h"
34 #include "AliESDVertex.h"
35 #include "AliESDtrack.h"
36 #include "AliITSVertexer.h"
37 #include "AliITSclusterTable.h"
38 #include "AliITSRecPoint.h"
39 #include "AliITStrackSA.h"
40 #include "AliITStrackerMI.h"
41 #include "AliITSlayerUpgrade.h"
42 #include "AliITSsegmentationUpgrade.h"
43 #include "AliITStrackerUpgrade.h"
44 #include "AliITSReconstructor.h"
45 #include "AliLog.h"
46 #include "AliRun.h"
47
48 ClassImp(AliITStrackerUpgrade)
49
50 //____________________________________________________________________________
51   AliITStrackerUpgrade::AliITStrackerUpgrade():AliITStrackerMI(),
52                                                fNLayer(6),
53                                                fPhiEstimate(0),
54                                                fITSStandAlone(0),
55                                                fLambdac(0),
56                                                fPhic(0),
57                                                fCoef1(0),
58                                                fCoef2(0),
59                                                fCoef3(0),
60                                                fNloop(0),
61                                                fPhiWin(0),
62                                                fLambdaWin(0),
63                                                fVert(0),
64                                                fVertexer(0),
65                                                fListOfTracks(0),
66                                                fListOfSATracks(0),
67                                                fITSclusters(0),
68                                                fInwardFlag(0),
69                                                fOuterStartLayer(0),
70                                                fInnerStartLayer(5),
71                                                fMinNPoints(0),
72                                                fMinQ(0.),
73                                                fLayers(0),
74                                                fSegmentation(0x0),
75                                                fCluLayer(0),
76                                                fCluCoord(0){
77     // Default constructor
78     Init();
79  
80   }
81 //________________________________________________________________________
82 AliITStrackerUpgrade::AliITStrackerUpgrade(const AliITStrackerUpgrade& tracker):AliITStrackerMI(),
83                                                                                 fNLayer(tracker.fNLayer),
84                                                                                 fPhiEstimate(tracker.fPhiEstimate),
85                                                                                 fITSStandAlone(tracker.fITSStandAlone),
86                                                                                 fLambdac(tracker.fLambdac),
87                                                                                 fPhic(tracker.fPhic),
88                                                                                 fCoef1(tracker.fCoef1),
89                                                                                 fCoef2(tracker.fCoef2),
90                                                                                 fCoef3(tracker.fCoef3),
91                                                                                 fNloop(tracker.fNloop),
92                                                                                 fPhiWin(tracker.fPhiWin),
93                                                                                 fLambdaWin(tracker.fLambdaWin),
94                                                                                 fVert(tracker.fVert),
95                                                                                 fVertexer(tracker.fVertexer),
96                                                                                 fListOfTracks(tracker.fListOfTracks),
97                                                                                 fListOfSATracks(tracker.fListOfSATracks),
98                                                                                 fITSclusters(tracker.fITSclusters),
99                                                                                 fInwardFlag(tracker.fInwardFlag),
100                                                                                 fOuterStartLayer(tracker.fOuterStartLayer),
101                                                                                 fInnerStartLayer(tracker.fInnerStartLayer),
102                                                                                 fMinNPoints(tracker.fMinNPoints),
103                                                                                 fMinQ(tracker.fMinQ),
104                                                                                 fLayers(tracker.fLayers),
105                                                                                 fSegmentation(tracker.fSegmentation),
106                                                                                 fCluLayer(tracker.fCluLayer),
107                                                                                 fCluCoord(tracker.fCluCoord) {
108   // Copy constructor
109   for(Int_t i=0;i<2;i++){
110     fPoint1[i]=tracker.fPoint1[i];
111     fPoint2[i]=tracker.fPoint2[i];
112     fPoint3[i]=tracker.fPoint3[i];
113     fPointc[i]=tracker.fPointc[i];
114   }
115   if(tracker.fVertexer && tracker.fVert){
116     fVert = new AliESDVertex(*tracker.fVert);
117   }
118   else {
119     fVert = tracker.fVert;
120   }
121   for(Int_t i=0;i<6;i++){
122     fCluLayer[i] = tracker.fCluLayer[i];
123     fCluCoord[i] = tracker.fCluCoord[i];
124   }
125 }
126 //______________________________________________________________________
127 AliITStrackerUpgrade& AliITStrackerUpgrade::operator=(const AliITStrackerUpgrade& source){
128   // Assignment operator. 
129   this->~AliITStrackerUpgrade();
130   new(this) AliITStrackerUpgrade(source);
131   return *this;
132  
133 }
134
135 //____________________________________________________________________________
136 AliITStrackerUpgrade::~AliITStrackerUpgrade(){
137   // destructor
138   // if fVertexer is not null, the AliESDVertex obj. is owned by this class
139   // and is deleted here
140   if(fVertexer){
141     if(fVert)delete fVert;
142   }
143   fVert = 0;
144   fVertexer = 0;
145  
146   if(fPhiWin)delete []fPhiWin;
147   if(fLambdaWin)delete []fLambdaWin;
148   fListOfTracks->Delete();
149   delete fListOfTracks;
150   fListOfSATracks->Delete();
151   delete fListOfSATracks;
152   if(fCluLayer){
153     for(Int_t i=0;i<6;i++){
154       if(fCluLayer[i]){
155         fCluLayer[i]->Delete();
156         delete fCluLayer[i];
157       }
158     }
159     delete [] fCluLayer;
160   }
161   if(fCluCoord){
162     for(Int_t i=0;i<AliITSgeomTGeo::GetNLayers();i++){
163       if(fCluCoord[i]){
164         fCluCoord[i]->Delete();
165         delete fCluCoord[i];
166       }
167     }
168     delete [] fCluCoord;
169   }
170   
171  if(fSegmentation) delete fSegmentation;
172 }
173
174 //____________________________________________________________________________
175 Int_t AliITStrackerUpgrade::Clusters2Tracks(AliESDEvent *event){
176   // This method is used to find and fit the tracks. By default the corresponding
177   // method in the parent class is invoked. In this way a combined tracking
178   // TPC+ITS is performed. If the flag fITSStandAlone is true, the tracking
179   // is done in the ITS only. In the standard reconstruction chain this option
180   // can be set via AliReconstruction::SetOption("ITS","onlyITS")
181   Int_t rc=0;
182   if(!fITSStandAlone){
183     rc=AliITStrackerMI::Clusters2Tracks(event);
184   }
185   else {
186     AliDebug(1,"Stand Alone flag set: doing tracking in ITS alone\n");
187   }
188   if(!rc){
189     rc=FindTracks(event,kFALSE);
190     if(AliITSReconstructor::GetRecoParam()->GetSAUseAllClusters()==kTRUE) {
191       rc=FindTracks(event,kTRUE);
192     }
193   }
194   return rc;
195 }
196 //_________________________________________________-
197 void AliITStrackerUpgrade::Init(){
198   //  Reset all data members
199   fPhiEstimate=0;
200   for(Int_t i=0;i<3;i++){fPoint1[i]=0;fPoint2[i]=0;fPoint3[i]=0;}
201   fLambdac=0;
202   fPhic=0;
203   fCoef1=0;
204   fCoef2=0;
205   fCoef3=0;
206   fPointc[0]=0;
207   fPointc[1]=0;
208   fVert = 0;
209   fVertexer = 0;
210   Int_t nLoops=AliITSReconstructor::GetRecoParam()->GetNLoopsSA();
211   if(nLoops==33){
212     SetFixedWindowSizes();
213   }else{
214     Double_t phimin=AliITSReconstructor::GetRecoParam()->GetMinPhiSA();
215     Double_t phimax=AliITSReconstructor::GetRecoParam()->GetMaxPhiSA();
216     Double_t lambmin=AliITSReconstructor::GetRecoParam()->GetMinLambdaSA();
217     Double_t lambmax=AliITSReconstructor::GetRecoParam()->GetMaxLambdaSA();
218     SetCalculatedWindowSizes(nLoops,phimin,phimax,lambmin,lambmax);
219   }
220   fMinQ=AliITSReconstructor::GetRecoParam()->GetSAMinClusterCharge();
221   fITSclusters = 0;
222   SetOuterStartLayer(1);
223   SetSAFlag(kFALSE);
224   fListOfTracks=new TClonesArray("AliITStrackMI",100);
225   fListOfSATracks=new TClonesArray("AliITStrackSA",100);
226   fLayers=new AliITSlayerUpgrade*[6];//to be fixed
227   fCluLayer = 0;
228   fCluCoord = 0;
229   fMinNPoints = 3;
230   for(Int_t layer=0; layer<6; layer++){
231     Double_t p=0.;
232     Double_t zC= 0.;
233     fLayers[layer] = new AliITSlayerUpgrade(p,zC);
234   }
235
236   fSegmentation = new AliITSsegmentationUpgrade();
237         
238 }
239 //_______________________________________________________________________
240 Int_t AliITStrackerUpgrade::LoadClusters(TTree *clusTree){ 
241 //
242 // Load clusters for tracking
243 // 
244
245   TClonesArray statITSCluster("AliITSRecPoint");
246   TClonesArray *ITSCluster = &statITSCluster;
247
248     TBranch* itsClusterBranch=clusTree->GetBranch("ITSRecPoints");
249     if (!itsClusterBranch){
250       AliError("can't get the branch with the ITS clusters ! \n");
251       return 1;
252     }
253     itsClusterBranch->SetAddress(&ITSCluster);
254     clusTree->GetEvent(0);
255     Int_t nCluster = ITSCluster->GetEntriesFast();
256     for(Int_t i=0; i<nCluster; i++){
257       AliITSRecPoint *recp = (AliITSRecPoint*)ITSCluster->UncheckedAt(i);
258       fLayers[recp->GetLayer()]->InsertCluster(new AliITSRecPoint(*recp));
259     }//loop clusters
260
261   SetClusterTree(clusTree);
262   return 0;
263 }
264
265 //_______________________________________________________
266 void AliITStrackerUpgrade::ResetForFinding(){
267   //  Reset data members used in all loops during track finding
268   fPhiEstimate=0;
269   for(Int_t i=0;i<3;i++){fPoint1[i]=0;fPoint2[i]=0;fPoint3[i]=0;}
270   fLambdac=0;
271   fPhic=0;
272   fCoef1=0;
273   fCoef2=0;
274   fCoef3=0;
275   fPointc[0]=0;
276   fPointc[1]=0;
277   fListOfTracks->Clear();
278   fListOfSATracks->Clear();
279 }
280
281  
282
283 //______________________________________________________________________
284 Int_t AliITStrackerUpgrade::FindTracks(AliESDEvent* event,Bool_t useAllClusters){
285
286   // Track finder using the ESD object
287   AliDebug(2,Form(" field is %f",event->GetMagneticField()));
288   printf(" field is %f",event->GetMagneticField());
289   AliDebug(2,Form("SKIPPING %d %d %d %d %d %d",ForceSkippingOfLayer(0),ForceSkippingOfLayer(1),ForceSkippingOfLayer(2),ForceSkippingOfLayer(3),ForceSkippingOfLayer(4),ForceSkippingOfLayer(5)));
290   if(!fITSclusters){
291     Fatal("FindTracks","ITS cluster tree is not accessed - Abort!!!\n Please use method SetClusterTree to pass the pointer to the tree\n");
292     return -1;
293   }
294
295   //Reads event and mark clusters of traks already found, with flag kITSin
296   Int_t nentr=event->GetNumberOfTracks();
297   if(!useAllClusters) {
298     while (nentr--) {
299       AliESDtrack *track=event->GetTrack(nentr);
300       if ((track->GetStatus()&AliESDtrack::kITSin) == AliESDtrack::kITSin){
301         Int_t idx[12];
302         Int_t ncl = track->GetITSclusters(idx);
303         for(Int_t k=0;k<ncl;k++){
304           AliITSRecPoint* cll = (AliITSRecPoint*)GetCluster(idx[k]);
305           cll->SetBit(kSAflag);
306         }
307       }
308     }
309   }else{
310     while (nentr--) {
311       AliESDtrack *track=event->GetTrack(nentr);
312       if ((track->GetStatus()&AliESDtrack::kITSin) == AliESDtrack::kITSin){
313         Int_t idx[12];
314         Int_t ncl = track->GetITSclusters(idx);
315         for(Int_t k=0;k<ncl;k++){
316           AliITSRecPoint* cll = (AliITSRecPoint*)GetCluster(idx[k]);
317           cll->ResetBit(kSAflag);
318         }
319       }
320     }
321   }
322   //Get primary vertex
323   Double_t primaryVertex[3];
324   event->GetVertex()->GetXYZ(primaryVertex);
325   Int_t nclusters[/*AliITSsegmentationUpgrade::GetNLayers()*/6]={0,0,0,0,0,0};
326   Int_t dmar[/*AliITSsegmentationUpgrade::GetNLayers()*/6]={0,0,0,0,0,0};
327   if (fCluLayer == 0) {
328     fCluLayer = new TClonesArray*[6];
329     fCluCoord = new TClonesArray*[6];
330     for(Int_t i=0;i<6;i++) {
331       fCluLayer[i]=0;
332       fCluCoord[i]=0;
333     }
334   }
335   for(Int_t i=0;i<6;i++){
336     if (!ForceSkippingOfLayer(i)) {
337       for(Int_t cli=0;cli<fLayers[i]->GetNumberOfClusters();cli++){
338         AliITSRecPoint* cls = (AliITSRecPoint*)fLayers[i]->GetCluster(cli);
339         if(cls->TestBit(kSAflag)==kTRUE) continue; //clusters used by TPC prol.
340         if(cls->GetQ()==0) continue; //fake clusters dead zones
341         if(i>1 && cls->GetQ()<=fMinQ) continue; // cut on SDD and SSD cluster charge
342         nclusters[i]++;
343       }
344     } 
345     dmar[i]=0;
346     if(!fCluLayer[i]){
347       fCluLayer[i] = new TClonesArray("AliITSRecPoint",nclusters[i]);
348     }else{
349       fCluLayer[i]->Delete();
350       fCluLayer[i]->Expand(nclusters[i]);
351     }
352     if(!fCluCoord[i]){
353       fCluCoord[i] = new TClonesArray("AliITSclusterTable",nclusters[i]);
354     }else{
355       fCluCoord[i]->Delete();
356       fCluCoord[i]->Expand(nclusters[i]);
357     }
358   }
359   for(Int_t ilay=0;ilay<AliITSgeomTGeo::GetNLayers();ilay++){
360     TClonesArray &clulay = *fCluLayer[ilay];
361     TClonesArray &clucoo = *fCluCoord[ilay];
362     if (!ForceSkippingOfLayer(ilay)){
363     AliDebug(2,Form("number of clusters in layer %i : %i",ilay,fLayers[ilay]->GetNumberOfClusters()));
364       for(Int_t cli=0;cli<fLayers[ilay]->GetNumberOfClusters();cli++){
365         AliITSRecPoint* cls = (AliITSRecPoint*)fLayers[ilay]->GetCluster(cli);
366         if(cls->TestBit(kSAflag)==kTRUE) continue;
367         if(cls->GetQ()==0) continue;
368         Double_t phi=0;Double_t lambda=0;
369         Double_t x=0;Double_t y=0;Double_t z=0;
370         Float_t sx=0;Float_t sy=0;Float_t sz=0;
371         GetCoorAngles(cls,phi,lambda,x,y,z,primaryVertex);
372         GetCoorErrors(cls,sx,sy,sz);
373         new (clulay[dmar[ilay]]) AliITSRecPoint(*cls);
374         new (clucoo[dmar[ilay]]) AliITSclusterTable(x,y,z,sx,sy,sz,phi,lambda,cli);
375         dmar[ilay]++;
376       }
377     } else AliDebug(2,Form("Force skipping layer %i",ilay));
378   }
379
380
381   // track counter
382   Int_t ntrack=0;
383
384   static Int_t nClusLay[6];//counter for clusters on each layer
385   Int_t startLayForSeed=0;
386   Int_t lastLayForSeed=fOuterStartLayer;
387   Int_t nSeedSteps=lastLayForSeed-startLayForSeed;
388   Int_t seedStep=1;
389   if(fInwardFlag){
390     startLayForSeed=6-1;
391     lastLayForSeed=fInnerStartLayer;
392     nSeedSteps=startLayForSeed-lastLayForSeed;
393     seedStep=-1;
394   }
395   // loop on minimum number of points
396   for(Int_t iMinNPoints=6; iMinNPoints>=fMinNPoints; iMinNPoints--) {
397     // loop on starting layer for track finding
398     for(Int_t iSeedLay=0; iSeedLay<=nSeedSteps; iSeedLay++) {
399       Int_t theLay=startLayForSeed+iSeedLay*seedStep;
400       if(ForceSkippingOfLayer(theLay)) continue;
401       Int_t minNPoints=iMinNPoints-theLay;
402       if(fInwardFlag) minNPoints=iMinNPoints-(6-1-theLay);
403       for(Int_t i=theLay+1;i<6;i++)
404         if(ForceSkippingOfLayer(i))
405           minNPoints--;
406       if(minNPoints<fMinNPoints) continue;
407
408       // loop on phi and lambda window size
409       for(Int_t nloop=0;nloop<fNloop;nloop++){
410         Int_t nclTheLay=fCluLayer[theLay]->GetEntries();
411         while(nclTheLay--){
412           ResetForFinding();
413           Bool_t useRP=SetFirstPoint(theLay,nclTheLay,primaryVertex);
414           if(!useRP) continue;
415           AliITStrackSA trs;
416
417           Int_t pflag=0;
418           Int_t kk;
419           for(kk=0;kk<6;kk++) nClusLay[kk] = 0;
420
421           kk=0;
422           nClusLay[kk] = SearchClusters(theLay,fPhiWin[nloop],fLambdaWin[nloop],
423                                         &trs,primaryVertex[2],pflag);
424           Int_t nextLay=theLay+seedStep;
425           Bool_t goon=kTRUE;
426           while(goon){
427
428             kk++;
429             nClusLay[kk] = SearchClusters(nextLay,fPhiWin[nloop],fLambdaWin[nloop],
430                                           &trs,primaryVertex[2],pflag);
431             if(nClusLay[kk]!=0){
432
433               pflag=1;
434               if(kk==1) {
435                 fPoint3[0]=fPointc[0];
436                 fPoint3[1]=fPointc[1];
437               } else {
438                 UpdatePoints();
439               }
440             }
441             nextLay+=seedStep;
442             if(nextLay<0 || nextLay==6) goon=kFALSE;
443           }
444
445
446           Int_t layOK=0;
447           if(!fInwardFlag){
448             for(Int_t nnp=0;nnp<6-theLay;nnp++){
449               if(nClusLay[nnp]!=0) layOK+=1;
450             }
451           }else{
452             for(Int_t nnp=theLay; nnp>=0; nnp--){
453               if(nClusLay[nnp]!=0) layOK+=1;
454             }
455           }
456           if(layOK>=minNPoints){
457             AliDebug(2,Form("---NPOINTS: %d; MAP: %d %d %d %d %d %d\n",layOK,nClusLay[0],nClusLay[1],nClusLay[2],nClusLay[3],nClusLay[4],nClusLay[5]));
458             AliITStrackV2* tr2 = 0;
459             Bool_t onePoint = kFALSE;
460             tr2 = FitTrack(&trs,primaryVertex,onePoint);
461             if(!tr2){
462               continue;
463             }
464             AliDebug(2,Form("---NPOINTS fit: %d\n",tr2->GetNumberOfClusters()));
465         
466             StoreTrack(tr2,event,useAllClusters);
467             ntrack++;
468
469           }
470
471         }//end loop on clusters of theLay
472       } //end loop on window sizes
473     } //end loop on theLay
474   }//end loop on min points
475   if(!useAllClusters) AliInfo(Form("Number of found tracks: %d",event->GetNumberOfTracks()));
476
477   ResetForFinding();
478   return 0;
479
480 }
481  
482 //________________________________________________________________________
483
484 AliITStrackV2* AliITStrackerUpgrade::FitTrack(AliITStrackSA* tr,Double_t *primaryVertex,Bool_t onePoint) {
485
486   
487   const Int_t kMaxClu=AliITStrackSA::kMaxNumberOfClusters;
488
489   
490   static Int_t clind[6][kMaxClu];
491   static Int_t clmark[6][kMaxClu];
492   static Int_t end[6];
493   static AliITSRecPoint *listlayer[6][kMaxClu];
494
495   for(Int_t i=0;i<6;i++) {
496     end[i]=0;
497     for(Int_t j=0;j<kMaxClu; j++){
498       clind[i][j]=0;
499       clmark[i][j]=0;
500       listlayer[i][j]=0;
501     }
502   }
503   Int_t inx[6]; 
504   for (Int_t k=0; k<6; k++) inx[k]=-1;
505   Int_t nclusters = tr->GetNumberOfClustersSA();
506   for(Int_t ncl=0;ncl<nclusters;ncl++){
507     Int_t index = tr->GetClusterIndexSA(ncl); 
508     Int_t lay = (index & 0xf0000000) >> 28;
509     Int_t cli = index&0x0fffffff;
510     AliITSRecPoint* cl = (AliITSRecPoint*)fLayers[lay]->GetCluster(cli);
511     if(cl->TestBit(kSAflag)==kTRUE) cl->ResetBit(kSAflag);  
512     inx[lay]=index;
513  
514     Int_t nInLay=end[lay];
515     listlayer[lay][nInLay]=cl;
516     clind[lay][nInLay]=index;
517     end[lay]++;
518   }
519
520   for(Int_t nlay=0;nlay<6;nlay++){
521     for(Int_t ncl=0;ncl<tr->GetNumberOfMarked(nlay);ncl++){
522       Int_t mark = tr->GetClusterMark(nlay,ncl);
523       clmark[nlay][ncl]=mark;
524     }
525   }
526
527
528   Int_t firstLay=-1,secondLay=-1;
529   for(Int_t i=0;i<6;i++) {
530     if(end[i]==0) {
531       end[i]=1;
532     }else{
533       if(firstLay==-1) {
534         firstLay=i;
535       } else if(secondLay==-1) {
536         secondLay=i;
537       }
538     }
539   }
540
541   if(firstLay==-1 || (secondLay==-1 && !onePoint)) return 0;
542   TClonesArray &arrMI= *fListOfTracks;
543   TClonesArray &arrSA= *fListOfSATracks;
544   Int_t nFoundTracks=0;
545
546   for(Int_t l0=0;l0<end[0];l0++){ //loop on layer 1
547     AliITSRecPoint* cl0 = (AliITSRecPoint*)listlayer[0][l0];
548     for(Int_t l1=0;l1<end[1];l1++){ //loop on layer 2
549       AliITSRecPoint* cl1 = (AliITSRecPoint*)listlayer[1][l1];
550       for(Int_t l2=0;l2<end[2];l2++){  //loop on layer 3
551         AliITSRecPoint* cl2 = (AliITSRecPoint*)listlayer[2][l2];
552         for(Int_t l3=0;l3<end[3];l3++){ //loop on layer 4   
553           AliITSRecPoint* cl3 = (AliITSRecPoint*)listlayer[3][l3];
554           for(Int_t l4=0;l4<end[4];l4++){ //loop on layer 5
555             AliITSRecPoint* cl4 = (AliITSRecPoint*)listlayer[4][l4];
556             for(Int_t l5=0;l5<end[5];l5++){ //loop on layer 6  
557               AliITSRecPoint* cl5 = (AliITSRecPoint*)listlayer[5][l5];
558
559
560               Double_t x1,y1,z1,sx1,sy1,sz1;
561               Double_t x2,y2,z2,sx2,sy2,sz2;
562               AliITSRecPoint* p1=0;
563               AliITSRecPoint* p2=0;
564               Int_t index1=0,index2=0;
565               Int_t mrk1=0,mrk2=0;
566
567               switch(firstLay) {
568               case 0:
569                 p1=cl0;
570                 index1=clind[0][l0];mrk1=clmark[0][l0];
571                 break;
572               case 1:
573                 p1=cl1;
574                 index1=clind[1][l1];mrk1=clmark[1][l1];
575                 break;
576               case 2:
577                 p1=cl2;
578                 index1=clind[2][l2];mrk1=clmark[2][l2];
579                 break;
580               case 3:
581                 p1=cl3;
582                 index1=clind[3][l3];mrk1=clmark[3][l3];
583                 break;
584               case 4:
585                 p1=cl4;
586                 index1=clind[4][l4];mrk1=clmark[4][l4];
587                 break;
588               }
589
590               switch(secondLay) {
591               case 1:
592                 p2=cl1;
593                 index2=clind[1][l1];mrk2=clmark[1][l1];
594                 break;
595               case 2:
596                 p2=cl2;
597                 index2=clind[2][l2];mrk2=clmark[2][l2];
598                 break;
599               case 3:
600                 p2=cl3;
601                 index2=clind[3][l3];mrk2=clmark[3][l3];
602                 break;
603               case 4:
604                 p2=cl4;
605                 index2=clind[4][l4];mrk2=clmark[4][l4];
606                 break;
607               case 5:
608                 p2=cl5;
609                 index2=clind[5][l5];mrk2=clmark[5][l5];
610                 break;
611               default:
612                 p2=0;
613                 index2=-1;mrk2=-1;
614                 break;
615               }
616
617               Int_t layer = 0;
618               Double_t radius = 0.;
619               Double_t yclu1 = p1->GetY();
620               Double_t zclu1 = p1->GetZ();
621               layer=p1->GetLayer();
622               fSegmentation->GetRadius(layer);
623               Double_t cv=0.,tgl2=0.,phi2=0.;
624               Int_t cln1=mrk1;
625               AliITSclusterTable* arr1 = (AliITSclusterTable*)GetClusterCoord(firstLay,cln1);
626               x1 = arr1->GetX();
627               y1 = arr1->GetY();
628               z1 = arr1->GetZ();
629               sx1 = arr1->GetSx();
630               sy1 = arr1->GetSy();
631               sz1 = arr1->GetSz();
632
633               if(secondLay>0) {
634                 Int_t cln2=mrk2;
635                 AliITSclusterTable* arr2 = (AliITSclusterTable*)GetClusterCoord(secondLay,cln2);
636                 x2 = arr2->GetX();
637                 y2 = arr2->GetY();
638                 z2 = arr2->GetZ();
639                 sx2 = arr2->GetSx();
640                 sy2 = arr2->GetSy();
641                 sz2 = arr2->GetSz();
642                 cv = Curvature(primaryVertex[0],primaryVertex[1],x1,y1,x2,y2);
643                 tgl2 = (z2-z1)/TMath::Sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
644                 phi2 = TMath::ATan2((y2-y1),(x2-x1));
645               }
646               Double_t x,y,z = 0.;
647               Double_t xz[2];
648               xz[0]= p1->GetDetLocalX(); 
649               xz[1]= p1->GetDetLocalZ();   
650               Bool_t check2;
651               check2 = fSegmentation->DetToGlobal(layer,xz[0], xz[1],x,y,z);
652
653
654               Double_t phiclrad, phicldeg;
655               phiclrad=TMath::ATan2(y,x); 
656               if (phiclrad<0) phiclrad+=TMath::TwoPi();
657               else if (phiclrad>=TMath::TwoPi()) phiclrad-=TMath::TwoPi();
658
659               phicldeg=180.*phiclrad/TMath::Pi();
660               Int_t ladder=(Int_t)(phicldeg/18.);//virtual segmentation
661               Double_t alpha= (ladder*18.+9.)*TMath::Pi()/180.;
662
663
664               AliITStrackSA trac(alpha,radius,yclu1,zclu1,phi2,tgl2,cv,1);
665
666
667               if(cl5!=0) {
668                 trac.AddClusterV2(5,(clind[5][l5] & 0x0fffffff)>>0);
669                 trac.AddClusterMark(5,clmark[5][l5]);
670               }
671               if(cl4!=0){
672                 trac.AddClusterV2(4,(clind[4][l4] & 0x0fffffff)>>0);
673                 trac.AddClusterMark(4,clmark[4][l4]);
674               }
675               if(cl3!=0){
676                 trac.AddClusterV2(3,(clind[3][l3] & 0x0fffffff)>>0);
677                 trac.AddClusterMark(3,clmark[3][l3]);
678               }
679               if(cl2!=0){
680                 trac.AddClusterV2(2,(clind[2][l2] & 0x0fffffff)>>0);
681                 trac.AddClusterMark(2,clmark[2][l2]);
682               }
683               if(cl1!=0){
684                 trac.AddClusterV2(1,(clind[1][l1] & 0x0fffffff)>>0);
685                 trac.AddClusterMark(1,clmark[1][l1]);
686               }
687               if(cl0!=0){
688                 trac.AddClusterV2(0,(clind[0][l0] & 0x0fffffff)>>0);
689                 trac.AddClusterMark(0,clmark[0][l0]);
690               }
691   
692               //fit with Kalman filter using AliITStrackerMI::RefitAtBase()
693               AliITStrackSA ot(trac);
694               ot.ResetCovariance(10.);
695               ot.ResetClusters();
696               
697               // Propagate inside the innermost layer with a cluster 
698               if(ot.Propagate(ot.GetX()-0.1*ot.GetX())) {
699                 Double_t rt=0.;
700                 rt=fSegmentation->GetRadius(5);
701                 if(RefitAtBase(rt,&ot,inx)){ //fit from layer 1 to layer 6
702                   AliITStrackMI otrack2(ot);
703                   otrack2.ResetCovariance(10.); 
704                   otrack2.ResetClusters();
705                   
706                   //fit from layer 6 to layer 1
707                   if(RefitAtBase(/*AliITSRecoParam::GetrInsideSPD1()*/fSegmentation->GetRadius(0),&otrack2,inx)) {//check clind
708                     new(arrMI[nFoundTracks]) AliITStrackMI(otrack2);
709                     new(arrSA[nFoundTracks]) AliITStrackSA(trac);
710                     ++nFoundTracks;
711                   }
712                               
713                 }       
714               }
715             }//end loop layer 6
716           }//end loop layer 5
717         }//end loop layer 4        
718       }//end loop layer 3
719     }//end loop layer 2 
720   }//end loop layer 1
721
722
723
724
725   if(fListOfTracks->GetEntries()==0) return 0;
726
727   Int_t lowchi2 = FindTrackLowChiSquare();
728   AliITStrackV2* otrack =(AliITStrackV2*)fListOfTracks->At(lowchi2);
729   AliITStrackSA* trsa = (AliITStrackSA*)fListOfSATracks->At(lowchi2);
730  
731   if(otrack==0) {
732     return 0;
733   }
734
735   CookLabel(otrack,0.); //MI change - to see fake ratio
736   Int_t label=FindLabel(otrack);
737   otrack->SetLabel(label);
738
739
740   Int_t indexc[6];
741   for(Int_t i=0;i<6;i++) indexc[i]=0;
742   for(Int_t nind=0;nind<otrack->GetNumberOfClusters();nind++){
743     indexc[nind] = otrack->GetClusterIndex(nind);
744   }      
745  
746   //remove clusters of found track
747   for(Int_t nlay=0;nlay<6;nlay++){
748     for(Int_t cln=0;cln<trsa->GetNumberOfMarked(nlay);cln++){
749       Int_t index = trsa->GetClusterMark(nlay,cln);
750       fCluLayer[nlay]->RemoveAt(index);
751       RemoveClusterCoord(nlay,index);
752       fCluLayer[nlay]->Compress();
753     }    
754   }
755   return otrack;
756
757 }
758
759 //_______________________________________________________
760 void AliITStrackerUpgrade::StoreTrack(AliITStrackV2 *t,AliESDEvent *event, Bool_t pureSA) /*const*/ 
761 {
762   //
763   // Add new track to the ESD
764   //
765   AliESDtrack outtrack;
766   outtrack.UpdateTrackParams(t,AliESDtrack::kITSin);
767   if(pureSA) outtrack.SetStatus(AliESDtrack::kITSpureSA);
768   outtrack.SetStatus(AliESDtrack::kITSout);
769   outtrack.SetStatus(AliESDtrack::kITSrefit);
770   Double_t sdedx[4]={0.,0.,0.,0.};
771   for(Int_t i=0; i<4; i++) sdedx[i]=t->GetSampledEdx(i);
772   outtrack.SetITSdEdxSamples(sdedx);
773   event->AddTrack(&outtrack);
774   return;
775 }
776
777
778 //_______________________________________________________
779 Int_t AliITStrackerUpgrade::SearchClusters(Int_t layer,Double_t phiwindow,Double_t lambdawindow, AliITStrackSA* trs,Double_t /*zvertex*/,Int_t pflag){
780   //function used to to find the clusters associated to the track
781
782   AliDebug(2,"Starting...");
783   if(ForceSkippingOfLayer(layer)) {
784   AliDebug(2,Form("Forcing skipping of layer %i. Exiting",layer));
785   return 0;
786   }
787
788   Int_t nc=0;
789   Double_t r=fSegmentation->GetRadius(layer);
790   if(pflag==1){      
791     Float_t cx1,cx2,cy1,cy2;
792     FindEquation(fPoint1[0],fPoint1[1],fPoint2[0],fPoint2[1],fPoint3[0],fPoint3[1],fCoef1,fCoef2,fCoef3);
793     if (FindIntersection(fCoef1,fCoef2,fCoef3,-r*r,cx1,cy1,cx2,cy2)==0)
794       return 0;
795     Double_t fi1=TMath::ATan2(cy1-fPoint1[1],cx1-fPoint1[0]);
796     Double_t fi2=TMath::ATan2(cy2-fPoint1[1],cx2-fPoint1[0]);
797     fPhiEstimate=ChoosePoint(fi1,fi2,fPhic);
798   }
799
800  
801   Int_t ncl = fCluLayer[layer]->GetEntries();
802   AliDebug(2,Form(" Number of clusters %i in layer %i.",ncl,layer));
803   for (Int_t index=0; index<ncl; index++) {
804     AliITSRecPoint *c = (AliITSRecPoint*)fCluLayer[layer]->At(index);
805     if (!c) continue;
806     if (c->GetQ()<=0) continue;
807     if(layer>1 && c->GetQ()<=fMinQ) continue;
808     
809     AliITSclusterTable* arr = (AliITSclusterTable*)GetClusterCoord(layer,index);
810     
811     Double_t phi;
812     Double_t xT,yT;
813     xT=arr->GetX();
814     yT=arr->GetY();
815     phi=arr->GetPhi();
816     if (TMath::Abs(phi-fPhiEstimate)>phiwindow)
817       {
818         continue;
819       }
820
821     Double_t lambda = arr->GetLambda();
822     if (TMath::Abs(lambda-fLambdac)>lambdawindow) continue;
823
824     if(trs->GetNumberOfClustersSA()==trs->GetMaxNumberOfClusters()) return 0;
825     if(trs->GetNumberOfMarked(layer)==trs->GetMaxNMarkedPerLayer()) return 0;
826     Int_t orind = arr->GetOrInd();
827     trs->AddClusterSA(layer,orind);
828     trs->AddClusterMark(layer,index);
829        
830     nc++;
831     fLambdac=lambda;
832     fPhiEstimate=phi;
833
834     fPointc[0]=arr->GetX();
835     fPointc[1]=arr->GetY();
836   }
837   return nc;
838 }
839
840 //________________________________________________________________
841 Bool_t AliITStrackerUpgrade::SetFirstPoint(Int_t lay, Int_t clu, Double_t* primaryVertex){
842   // Sets the first point (seed) for tracking
843
844   AliITSRecPoint* cl = (AliITSRecPoint*)fCluLayer[lay]->At(clu);
845   if(!cl) return kFALSE;
846   if (cl->GetQ()<=0) return kFALSE;
847   if(lay>1 && cl->GetQ()<=fMinQ) return kFALSE;
848   AliITSclusterTable* arr = (AliITSclusterTable*)GetClusterCoord(lay,clu);
849   fPhic = arr->GetPhi();
850   fLambdac = arr->GetLambda();
851   fPhiEstimate = fPhic;
852   fPoint1[0]=primaryVertex[0];
853   fPoint1[1]=primaryVertex[1];
854   fPoint2[0]=arr->GetX();
855   fPoint2[1]=arr->GetY();
856   return kTRUE; 
857 }
858
859 //________________________________________________________________
860 void AliITStrackerUpgrade::UpdatePoints(){
861   //update of points for the estimation of the curvature  
862
863   fPoint2[0]=fPoint3[0];
864   fPoint2[1]=fPoint3[1];
865   fPoint3[0]=fPointc[0];
866   fPoint3[1]=fPointc[1];
867
868   
869 }
870
871 //___________________________________________________________________
872 Int_t AliITStrackerUpgrade::FindEquation(Float_t x1, Float_t y1, Float_t x2, Float_t y2, Float_t x3, Float_t y3,Float_t& a, Float_t& b, Float_t& c){
873
874   //given (x,y) of three recpoints (in global coordinates) 
875   //returns the parameters a,b,c of circonference x*x + y*y +a*x + b*y +c
876
877   Float_t den = (x3-x1)*(y2-y1)-(x2-x1)*(y3-y1);
878   if(den==0) return 0;
879   a = ((y3-y1)*(x2*x2+y2*y2-x1*x1-y1*y1)-(y2-y1)*(x3*x3+y3*y3-x1*x1-y1*y1))/den;
880   b = -(x2*x2-x1*x1+y2*y2-y1*y1+a*(x2-x1))/(y2-y1);
881   c = -x1*x1-y1*y1-a*x1-b*y1;
882   return 1;
883 }
884 //__________________________________________________________________________
885 Int_t AliITStrackerUpgrade::FindIntersection(Float_t a1, Float_t b1, Float_t c1, Float_t c2,Float_t& x1,Float_t& y1, Float_t& x2, Float_t& y2){
886  
887   //Finds the intersection between the circonference of the track and the circonference centered in (0,0) represented by one layer
888   //c2 is -rlayer*rlayer
889
890   if(a1==0) return 0;
891   Double_t m = c2-c1; 
892   Double_t aA = (b1*b1)/(a1*a1)+1;
893   Double_t bB = (-2*m*b1/(a1*a1));
894   Double_t cC = c2+(m*m)/(a1*a1);
895   Double_t dD = bB*bB-4*aA*cC;
896   if(dD<0) return 0;
897  
898   y1 = (-bB+TMath::Sqrt(dD))/(2*aA); 
899   y2 = (-bB-TMath::Sqrt(dD))/(2*aA); 
900   x1 = (c2-c1-b1*y1)/a1;
901   x2 = (c2-c1-b1*y2)/a1;
902
903   return 1; 
904 }
905 //____________________________________________________________________
906 Double_t AliITStrackerUpgrade::Curvature(Double_t x1,Double_t y1,Double_t 
907                                          x2,Double_t y2,Double_t x3,Double_t y3){
908
909   //calculates the curvature of track  
910   Double_t den = (x3-x1)*(y2-y1)-(x2-x1)*(y3-y1);
911   if(den==0) return 0;
912   Double_t a = ((y3-y1)*(x2*x2+y2*y2-x1*x1-y1*y1)-(y2-y1)*(x3*x3+y3*y3-x1*x1-y1*y1))/den;
913   Double_t b = -(x2*x2-x1*x1+y2*y2-y1*y1+a*(x2-x1))/(y2-y1);
914   Double_t c = -x1*x1-y1*y1-a*x1-b*y1;
915   Double_t xc=-a/2.;
916
917   if((a*a+b*b-4*c)<0) return 0;
918   Double_t rad = TMath::Sqrt(a*a+b*b-4*c)/2.;
919   if(rad==0) return 0;
920   
921   if((x1>0 && y1>0 && x1<xc)) rad*=-1;
922   if((x1<0 && y1>0 && x1<xc)) rad*=-1;
923   //  if((x1<0 && y1<0 && x1<xc)) rad*=-1;
924   // if((x1>0 && y1<0 && x1<xc)) rad*=-1;
925   
926   return 1/rad;
927  
928 }
929
930
931 //____________________________________________________________________
932 Double_t AliITStrackerUpgrade::ChoosePoint(Double_t p1, Double_t p2, Double_t pp){
933
934   //Returns the point closest to pp
935
936   Double_t diff1 = p1-pp;
937   Double_t diff2 = p2-pp;
938   
939   if(TMath::Abs(diff1)<TMath::Abs(diff2)) fPhiEstimate=p1;
940   else fPhiEstimate=p2;  
941   return fPhiEstimate;
942   
943 }
944
945
946 //_________________________________________________________________
947 Int_t AliITStrackerUpgrade::FindTrackLowChiSquare() const {
948   // returns track with lowest chi square  
949   Int_t dim=fListOfTracks->GetEntries();
950   if(dim<=1) return 0;
951   AliITStrackV2* trk = (AliITStrackV2*)fListOfTracks->At(0);
952   Double_t minChi2=trk->GetChi2();
953   Int_t index=0;
954   for(Int_t i=1;i<dim;i++){
955     trk = (AliITStrackV2*)fListOfTracks->At(i);
956     Double_t chi2=trk->GetChi2();
957     if(chi2<minChi2){
958       minChi2=chi2;
959       index=i;
960     }
961   }
962   return index;
963 }
964
965 //__________________________________________________________
966 Int_t AliITStrackerUpgrade::FindLabel(AliITStrackV2* track) const {
967   //
968
969   Int_t labl[AliITSgeomTGeo::kNLayers][3];
970   Int_t cnts[AliITSgeomTGeo::kNLayers][3];
971   for(Int_t j=0;j<AliITSgeomTGeo::GetNLayers();j++){
972     for(Int_t k=0;k<3;k++){
973       labl[j][k]=-2;
974       cnts[j][k]=1;
975     }
976   }
977   Int_t iNotLabel=0;
978   for(Int_t i=0;i<track->GetNumberOfClusters(); i++) {
979     Int_t indexc = track->GetClusterIndex(i);
980     AliITSRecPoint* cl = (AliITSRecPoint*)GetCluster(indexc);
981     AliDebug(2,Form(" cluster index %i  ",indexc));
982     Int_t iLayer=cl->GetLayer();
983     for(Int_t k=0;k<3;k++){
984       labl[iLayer][k]=cl->GetLabel(k);
985       if(labl[iLayer][k]<0) iNotLabel++;
986     }
987   }
988   if(iNotLabel==3*track->GetNumberOfClusters()) return -2;
989
990   for(Int_t j1=0;j1<AliITSgeomTGeo::kNLayers; j1++) {
991     for(Int_t j2=0; j2<j1;  j2++){
992       for(Int_t k1=0; k1<3; k1++){
993         for(Int_t k2=0; k2<3; k2++){
994           if(labl[j1][k1]>=0 && labl[j1][k1]==labl[j2][k2] && cnts[j2][k2]>0){
995             cnts[j2][k2]++;
996             cnts[j1][k1]=0;
997           }
998         }
999       }
1000     }
1001   }
1002
1003
1004   Int_t cntMax=0;
1005   Int_t label=-1;
1006   for(Int_t j=0;j<AliITSgeomTGeo::kNLayers;j++){
1007     for(Int_t k=0;k<3;k++){
1008       if(cnts[j][k]>cntMax && labl[j][k]>=0){
1009         cntMax=cnts[j][k];
1010         label=labl[j][k];
1011       }
1012     }
1013   }
1014
1015   Int_t lflag=0;
1016   for(Int_t i=0;i<AliITSgeomTGeo::kNLayers;i++)
1017   if(labl[i][0]==label || labl[i][1]==label || labl[i][2]==label) lflag++;
1018   if(lflag<track->GetNumberOfClusters()) label = -label;
1019   return label;
1020 }
1021 //_____________________________________________________________________________
1022 /*Int_t AliITStrackerUpgrade::Label(Int_t gl1, Int_t gl2, Int_t gl3, Int_t gl4, Int_t gl5, Int_t gl6,Int_t gl7, Int_t gl8, Int_t gl9, Int_t gl10,Int_t gl11,
1023   Int_t gl12, Int_t gl13, Int_t gl14,Int_t gl15, Int_t gl16, Int_t gl17, Int_t gl18, Int_t minNPoints){
1024
1025  
1026   //function used to assign label to the found track. If track is fake, the label is negative
1027
1028   Int_t lb0[6] = {gl1,gl2,gl3,gl4,gl5,gl6};
1029   Int_t lb1[6] = {gl7,gl8,gl9,gl10,gl11,gl12};
1030   Int_t lb2[6] = {gl13,gl14,gl15,gl16,gl17,gl18};
1031   Int_t ll=FindLabel(lb0[0],lb0[1],lb0[2],lb0[3],lb0[4],lb0[5]);
1032   Int_t lflag=0;Int_t num=6;
1033   if(lb0[5]==-1 && lb1[5]==-1 && lb2[5]==-1) num=5;
1034
1035   for(Int_t i=0;i<num;i++){
1036   if(lb0[i]==ll || lb1[i]==ll || lb2[i]==ll) lflag+=1;
1037   }
1038
1039   if(lflag>=minNPoints) return ll;
1040   else return -ll;
1041
1042   
1043   }
1044 */
1045 //_____________________________________________________________________________
1046 void AliITStrackerUpgrade::SetCalculatedWindowSizes(Int_t n, Float_t phimin, Float_t phimax, Float_t lambdamin, Float_t lambdamax){
1047   // Set sizes of the phi and lambda windows used for track finding
1048   fNloop = n;
1049   if(fPhiWin) delete [] fPhiWin;
1050   if(fLambdaWin) delete [] fLambdaWin;
1051   fPhiWin = new Double_t[fNloop];
1052   fLambdaWin = new Double_t[fNloop];
1053   Float_t stepPhi=(phimax-phimin)/(Float_t)(fNloop-1);
1054   Float_t stepLambda=(lambdamax-lambdamin)/(Float_t)(fNloop-1);
1055   for(Int_t k=0;k<fNloop;k++){
1056     Float_t phi=phimin+k*stepPhi;
1057     Float_t lam=lambdamin+k*stepLambda;
1058     fPhiWin[k]=phi;
1059     fLambdaWin[k]=lam;
1060   }
1061 }
1062 //_____________________________________________________________________________
1063 void AliITStrackerUpgrade::SetFixedWindowSizes(Int_t n, Double_t *phi, Double_t *lam){
1064   // Set sizes of the phi and lambda windows used for track finding
1065   fNloop = n;
1066   if(phi){ // user defined values
1067     fPhiWin = new Double_t[fNloop];
1068     fLambdaWin = new Double_t[fNloop];
1069     for(Int_t k=0;k<fNloop;k++){
1070       fPhiWin[k]=phi[k];
1071       fLambdaWin[k]=lam[k];
1072     }
1073   }
1074   else {  // default values
1075             
1076     Double_t phid[33]   = {0.002,0.003,0.004,0.0045,0.0047,
1077                            0.005,0.0053,0.0055,
1078                            0.006,0.0063,0.0065,0.007,0.0073,0.0075,0.0077,
1079                            0.008,0.0083,0.0085,0.0087,0.009,0.0095,0.0097,
1080                            0.01,0.0105,0.011,0.0115,0.012,0.0125,0.013,0.0135,0.0140,0.0145};
1081     Double_t lambdad[33] = {0.003,0.004,0.005,0.005,0.005,
1082                             0.005,0.005,0.006,
1083                             0.006,0.006,0.006,0.007,0.007,0.007,0.007,
1084                             0.007,0.007,0.007,0.007,0.007,0.007,0.007,
1085                             0.008,0.008,0.008,0.008,0.008,0.008,0.008,0.008,0.008,0.008};
1086     
1087     if(fNloop!=33){
1088       fNloop = 33;
1089     }
1090     
1091     
1092     fPhiWin = new Double_t[fNloop];
1093     fLambdaWin = new Double_t[fNloop];
1094
1095     Double_t factor=AliITSReconstructor::GetRecoParam()->GetFactorSAWindowSizes(); 
1096   
1097     for(Int_t k=0;k<fNloop;k++){
1098       fPhiWin[k]=phid[k]*factor;
1099       fLambdaWin[k]=lambdad[k]*factor;
1100     }
1101   
1102   }
1103
1104 }
1105 //_______________________________________________________________________
1106 void AliITStrackerUpgrade::GetCoorAngles(AliITSRecPoint* cl,Double_t &phi,Double_t &lambda, Double_t &x, Double_t &y,Double_t &z,Double_t* vertex){
1107   //Returns values of phi (azimuthal) and lambda angles for a given cluster
1108   /*  
1109       Double_t rot[9];     fGeom->GetRotMatrix(module,rot);
1110       Int_t lay,lad,det; fGeom->GetModuleId(module,lay,lad,det);
1111       Float_t tx,ty,tz;  fGeom->GetTrans(lay,lad,det,tx,ty,tz);     
1112
1113       Double_t alpha=TMath::ATan2(rot[1],rot[0])+TMath::Pi();
1114       Double_t phi1=TMath::Pi()/2+alpha;
1115       if (lay==1) phi1+=TMath::Pi();
1116
1117       Float_t cp=TMath::Cos(phi1), sp=TMath::Sin(phi1);
1118       Float_t r=tx*cp+ty*sp;
1119
1120       xyz= r*cp - cl->GetY()*sp;
1121       y= r*sp + cl->GetY()*cp;
1122       z=cl->GetZ();
1123   */
1124   
1125   Double_t xz[2];
1126   xz[0]= cl->GetDetLocalX(); 
1127   xz[1]= cl->GetDetLocalZ() ; 
1128   Bool_t check2;
1129   Int_t ilayer;
1130   ilayer = cl->GetLayer();
1131   check2 = fSegmentation->DetToGlobal(ilayer,xz[0], xz[1],x,y,z);
1132
1133   if(x!=0 && y!=0)  
1134     phi=TMath::ATan2(y-vertex[1],x-vertex[0]);
1135   lambda=TMath::ATan2(z-vertex[2],TMath::Sqrt((x-vertex[0])*(x-vertex[0])+(y-vertex[1])*(y-vertex[1])));
1136 }
1137
1138 //________________________________________________________________________
1139 void AliITStrackerUpgrade::GetCoorErrors(AliITSRecPoint* cl,Float_t &sx,Float_t &sy, Float_t &sz){
1140
1141   //returns sigmax, y, z of cluster in global coordinates
1142   /*
1143     Double_t rot[9];     fGeom->GetRotMatrix(module,rot);
1144     Int_t lay,lad,det; 
1145     AliITSgeomTGeo::GetModuleId(module,lay,lad,det);
1146  
1147     Double_t alpha=TMath::ATan2(rot[1],rot[0])+TMath::Pi();
1148     Double_t phi=TMath::Pi()/2+alpha;
1149     if (lay==1) phi+=TMath::Pi();
1150
1151     Float_t cp=TMath::Cos(phi), sp=TMath::Sin(phi);
1152   */
1153   Float_t covm[6];
1154   cl->GetGlobalCov(covm);
1155   sx=TMath::Sqrt(covm[0]);
1156   sy=TMath::Sqrt(covm[3]);
1157   sz=TMath::Sqrt(covm[5]);
1158
1159
1160
1161   //  Float_t covm[6];
1162   //  cl->GetGlobalCov(covm);
1163   //  sx=12*1e-04;//TMath::Sqrt(covm[0]);
1164   //  sy=12*1e-04;//TMath::Sqrt(covm[3]);
1165   //  sz=40*1e-04;//TMath::Sqrt(covm[5]);
1166   /*
1167     sx = TMath::Sqrt(sp*sp*cl->GetSigmaY2());
1168     sy = TMath::Sqrt(cp*cp*cl->GetSigmaY2());
1169     sz = TMath::Sqrt(cl->GetSigmaZ2());
1170   */
1171 }
1172 //_____________________________________________________________
1173 void AliITStrackerUpgrade::UnloadClusters() {
1174   //--------------------------------------------------------------------
1175   //This function unloads ITS clusters
1176   //--------------------------------------------------------------------
1177   for (Int_t i=0; i<6; i++) fLayers[i]->ResetClusters();
1178 }
1179 //______________________________________________________________________
1180 Bool_t AliITStrackerUpgrade::RefitAtBase(Double_t xx,AliITStrackMI *track,
1181                                          const Int_t *clusters)
1182 {
1183   //--------------------------------------------------------------------
1184   // Simplified version for ITS upgrade studies -- does not use module info
1185   //--------------------------------------------------------------------
1186   Int_t index[AliITSgeomTGeo::kNLayers];
1187   Int_t k;
1188   for (k=0; k<AliITSgeomTGeo::GetNLayers(); k++) index[k]=-1;
1189   //
1190   for (k=0; k<AliITSgeomTGeo::GetNLayers(); k++) {
1191     index[k]=clusters[k];
1192   }
1193
1194   ULong_t trStatus=0;
1195   if(track->GetESDtrack()) trStatus=track->GetStatus();
1196   Int_t innermostlayer=0;
1197   if(trStatus&AliESDtrack::kTPCin)  {
1198     innermostlayer=5;
1199     Double_t drphi = TMath::Abs(track->GetD(0.,0.));
1200     for(innermostlayer=0; innermostlayer<AliITSgeomTGeo::GetNLayers(); innermostlayer++) {
1201       if( (drphi < (fSegmentation->GetRadius(innermostlayer)+1.)) ||
1202           index[innermostlayer] >= 0 ) break;
1203     }
1204     AliDebug(2,Form(" drphi  %f  innermost %d",drphi,innermostlayer));
1205   }
1206
1207   Int_t from, to, step;
1208   if (xx > track->GetX()) {
1209     from=innermostlayer; to=6;
1210     step=+1;
1211   } else {
1212     from=AliITSgeomTGeo::GetNLayers()-1; to=innermostlayer-1;
1213     step=-1;
1214   }
1215   TString dir = (step>0 ? "outward" : "inward");
1216
1217   for (Int_t ilayer = from; ilayer != to; ilayer += step) {
1218     Double_t r=0.;
1219     r=fSegmentation->GetRadius(ilayer);
1220
1221     if (step<0 && xx>r){
1222       break;
1223     }
1224
1225     // material between SSD and SDD, SDD and SPD
1226     //      Double_t hI=ilayer-0.5*step; 
1227     //      if (TMath::Abs(hI-3.5)<0.01) // SDDouter
1228     //        if(!CorrectForShieldMaterial(track,"SDD",dir)) return kFALSE;
1229     //      if (TMath::Abs(hI-1.5)<0.01) // SPDouter
1230     //        if(!CorrectForShieldMaterial(track,"SPD",dir)) return kFALSE;
1231
1232
1233     Double_t oldGlobXYZ[3];
1234
1235     if (!track->GetXYZ(oldGlobXYZ)) {
1236       return kFALSE;
1237     }
1238     // continue if we are already beyond this layer
1239     Double_t oldGlobR = TMath::Sqrt(oldGlobXYZ[0]*oldGlobXYZ[0]+oldGlobXYZ[1]*oldGlobXYZ[1]);
1240     if(step>0 && oldGlobR > r){
1241       continue; // going outward
1242     }
1243     if(step<0 && oldGlobR < r) {
1244       continue; // going inward
1245     }
1246     Double_t phi,z;
1247     if (!track->GetPhiZat(r,phi,z)){
1248       return kFALSE;
1249     }
1250     // only for ITS-SA tracks refit
1251     track->SetCheckInvariant(kFALSE);
1252       
1253     if (phi<0) phi+=TMath::TwoPi();//from 0 to 360 (rad) 
1254     else if (phi>=TMath::TwoPi()) phi-=TMath::TwoPi();//
1255     Double_t phideg=0.;  
1256     phideg=180.*phi/TMath::Pi();// in deg  
1257     Int_t ladder=(Int_t)(phideg/18.);// virtual segmentation 
1258     Double_t alpha= (ladder*18.+9.)*TMath::Pi()/180.;
1259     
1260     if (!track->Propagate(alpha,r)) {
1261       return kFALSE;
1262     }
1263
1264     const AliITSRecPoint *clAcc=0;
1265     Double_t maxchi2=1000.*AliITSReconstructor::GetRecoParam()->GetMaxChi2();
1266
1267     Int_t idx=index[ilayer];
1268     if (idx>=0) { // cluster in this layer   
1269       Int_t cli = idx&0x0fffffff;
1270       const AliITSRecPoint *cl=(AliITSRecPoint *)fLayers[ilayer]->GetCluster(cli);
1271
1272       if (cl) {                                                                  
1273         Int_t cllayer = (idx & 0xf0000000) >> 28;;                               
1274         Double_t chi2=GetPredictedChi2MI(track,cl,cllayer);                      
1275         if (chi2<maxchi2) {                                                      
1276           clAcc=cl;                                                              
1277           maxchi2=chi2;                                                          
1278         } else {                                                                 
1279            return kFALSE;                                                         
1280         }                                                                        
1281       }                                                                          
1282     }              
1283
1284     if (clAcc) {
1285       if (!UpdateMI(track,clAcc,maxchi2,idx)){
1286         return kFALSE;
1287       }
1288       track->SetSampledEdx(clAcc->GetQ(),ilayer-2);
1289     }
1290
1291
1292     // Correct for material of the current layer
1293     // cross material
1294     // add time if going outward
1295     if(!CorrectForLayerMaterial(track,ilayer,oldGlobXYZ,dir)){
1296       return kFALSE;
1297     }
1298     track->SetCheckInvariant(kFALSE);
1299   } // end loop on layers
1300
1301   if (!track->PropagateTo(xx,0.,0.)){
1302     return kFALSE;
1303   } 
1304   return kTRUE;
1305 }
1306
1307 //_____________________________________________________________________
1308 Int_t AliITStrackerUpgrade::UpdateMI(AliITStrackMI* track, const AliITSRecPoint* cl,Double_t chi2,Int_t index) const
1309 {
1310   //
1311   // Update ITS track
1312   //
1313   Int_t layer = (index & 0xf0000000) >> 28;
1314   track->SetClIndex(layer, index);
1315   if (TMath::Abs(cl->GetQ())<1.e-13) return 0;  // ingore the "virtual" clusters
1316
1317
1318   // Take into account the mis-alignment (bring track to cluster plane)
1319   Double_t xTrOrig=track->GetX();
1320   Double_t trxyz[3]; track->GetXYZ(trxyz);
1321   Float_t xclu1 = cl->GetX();//upgrade clusters tracking coordinate
1322   //Float_t yclu1 = cl->GetY();
1323   //Float_t zclu1 = cl->GetZ();
1324   if (!track->Propagate(xTrOrig+xclu1)){
1325     return 0;
1326   }
1327   AliCluster c(*cl);
1328   c.SetSigmaY2(track->GetSigmaY(layer)*track->GetSigmaY(layer));
1329   c.SetSigmaZ2(track->GetSigmaZ(layer)*track->GetSigmaZ(layer));
1330   c.SetSigmaYZ(track->GetSigmaYZ(layer));
1331
1332   // Bring the track back to detector plane in ideal geometry  
1333   Int_t updated = track->UpdateMI(&c,chi2,index);
1334   if (!track->Propagate(xTrOrig)) {
1335     return 0;
1336   }
1337   if(!updated){
1338     AliDebug(2,"update failed");
1339   }
1340   return updated;
1341 }
1342 //____________________________________________________________________-
1343 Int_t AliITStrackerUpgrade::CorrectForLayerMaterial(AliITStrackMI *t,
1344                                                     Int_t layerindex,
1345                                                     Double_t oldGlobXYZ[3],
1346                                                     TString direction) {
1347   //-------------------------------------------------------------------
1348   // Propagate beyond layer and correct for material
1349   // (material budget in different ways according to fUseTGeo value)
1350   // Add time if going outward (PropagateTo or PropagateToTGeo)
1351   //-------------------------------------------------------------------
1352
1353   // Define budget mode:
1354   // 0: material from AliITSRecoParam (hard coded)
1355   // 1: material from TGeo in stepsof X cm (on the fly)
1356   //    X = AliITSRecoParam::GetStepSizeTGeo()
1357   // 2: material from lut
1358   // 3: material from TGeo in one step (same for all hypotheses)
1359   Int_t mode;
1360   switch(fUseTGeo) {
1361   case 0:
1362     mode=0;
1363     break;
1364   case 1:
1365     mode=1;
1366     break;
1367   case 2:
1368     mode=2;
1369     break;
1370   case 3:
1371     if(fTrackingPhase.Contains("Clusters2Tracks"))
1372       { mode=3; } else { mode=1; }
1373     break;
1374   case 4:
1375     if(fTrackingPhase.Contains("Clusters2Tracks"))
1376       { mode=3; } else { mode=2; }
1377     break;
1378   default:
1379     mode=0;
1380     break;
1381   }
1382   if(fTrackingPhase.Contains("Default")) {
1383     mode=0;
1384   }
1385   Float_t  dir = (direction.Contains("inward") ? 1. : -1.);
1386   Double_t r = 0.;
1387   r=fSegmentation->GetRadius(layerindex);
1388   Double_t deltar=(layerindex<2 ? 0.10*r : 0.05*r);
1389   Double_t rToGo=TMath::Sqrt(t->GetX()*t->GetX()+t->GetY()*t->GetY())-deltar*dir;
1390   Double_t xToGo;
1391   if (!t->GetLocalXat(rToGo,xToGo)) {
1392     return 0;
1393   }
1394   Int_t index=6*fCurrentEsdTrack+layerindex;
1395
1396
1397   Double_t xOverX0=0.0,x0=0.0,lengthTimesMeanDensity=0.0;
1398   Int_t nsteps=1;
1399   // back before material (no correction)
1400   Double_t rOld,xOld;
1401   rOld=TMath::Sqrt(oldGlobXYZ[0]*oldGlobXYZ[0]+oldGlobXYZ[1]*oldGlobXYZ[1]);
1402   if (!t->GetLocalXat(rOld,xOld)) return 0;
1403   if (!t->Propagate(xOld)) return 0;
1404
1405   switch(mode) {
1406   case 1:
1407     x0=21.82;
1408     xOverX0 = fSegmentation->GetThickness(layerindex)/x0;
1409
1410     lengthTimesMeanDensity = xOverX0*x0;
1411     lengthTimesMeanDensity *= dir;
1412     // Bring the track beyond the material
1413     if (!t->PropagateTo(xToGo,xOverX0,lengthTimesMeanDensity/xOverX0)){
1414       return 0;
1415     }
1416     break;
1417   case 0:
1418     nsteps = (Int_t)(TMath::Abs(xOld-xToGo)/AliITSReconstructor::GetRecoParam()->GetStepSizeTGeo())+1;
1419     if (!t->PropagateToTGeo(xToGo,nsteps)) return 0; // cross the material and apply correction
1420     break;
1421   case 2:
1422     if(fxOverX0Layer[layerindex]<0) BuildMaterialLUT("Layers");
1423     xOverX0 = fxOverX0Layer[layerindex];
1424     lengthTimesMeanDensity = fxTimesRhoLayer[layerindex];
1425     lengthTimesMeanDensity *= dir;
1426     // Bring the track beyond the material
1427     if (!t->PropagateTo(xToGo,xOverX0,lengthTimesMeanDensity/xOverX0)) return 0;
1428     break;
1429   case 3:
1430     if(!fxOverX0LayerTrks || index<0 || index>=6*fNtracks) Error("CorrectForLayerMaterial","Incorrect usage of UseTGeo option!\n");
1431     if(fxOverX0LayerTrks[index]<0) {
1432       nsteps = (Int_t)(TMath::Abs(xOld-xToGo)/AliITSReconstructor::GetRecoParam()->GetStepSizeTGeo())+1;
1433       if (!t->PropagateToTGeo(xToGo,nsteps,xOverX0,lengthTimesMeanDensity)) return 0;
1434       Double_t angle=TMath::Sqrt((1.+t->GetTgl()*t->GetTgl())/
1435                                  ((1.-t->GetSnp())*(1.+t->GetSnp())));
1436       fxOverX0LayerTrks[index] = TMath::Abs(xOverX0)/angle;
1437       fxTimesRhoLayerTrks[index] = TMath::Abs(lengthTimesMeanDensity)/angle;
1438       return 1;
1439     }
1440     xOverX0 = fxOverX0LayerTrks[index];
1441     lengthTimesMeanDensity = fxTimesRhoLayerTrks[index];
1442     lengthTimesMeanDensity *= dir;
1443     // Bring the track beyond the material
1444     if (!t->PropagateTo(xToGo,xOverX0,lengthTimesMeanDensity/xOverX0)){
1445       return 0;
1446     }
1447     break;
1448   }
1449
1450
1451   return 1;
1452 }
1453
1454
1455 //_____________________________________________________________________________
1456 Double_t AliITStrackerUpgrade::GetPredictedChi2MI(AliITStrackMI* track, const AliITSRecPoint *cluster,Int_t layer)
1457 {
1458   //
1459   // Compute predicted chi2
1460   //
1461   Float_t erry,errz,covyz;
1462   Float_t theta = track->GetTgl();
1463   Float_t phi   = track->GetSnp();
1464   phi = TMath::Abs(phi)*TMath::Sqrt(1./((1.-phi)*(1.+phi)));
1465   GetError(layer,cluster,theta,phi,track->GetExpQ(),erry,errz,covyz);
1466   AliDebug(2,Form(" chi2: tr-cl   %f  %f   tr X %f cl X %f",track->GetY()-cluster->GetY(),track->GetZ()-cluster->GetZ(),track->GetX(),cluster->GetX()));
1467   // Take into account the mis-alignment (bring track to cluster plane)
1468   Double_t xTrOrig=track->GetX();
1469   if (!track->Propagate(xTrOrig+cluster->GetX())) return 1000.;
1470   AliDebug(2,Form(" chi2: tr-cl   %f  %f   tr X %f cl X %f",track->GetY()-cluster->GetY(),track->GetZ()-cluster->GetZ(),track->GetX(),cluster->GetX()));
1471   Double_t chi2 = track->GetPredictedChi2MI(cluster->GetY(),cluster->GetZ(),erry,errz,covyz);
1472   // Bring the track back to detector plane in ideal geometry
1473   // [mis-alignment will be accounted for in UpdateMI()]
1474   if (!track->Propagate(xTrOrig)) return 1000.;
1475   Float_t ny=0.,nz=0.;
1476   //GetNTeor(layer,cluster,theta,phi,ny,nz);
1477   Double_t delta = cluster->GetNy()+cluster->GetNz()-nz-ny;
1478   if (delta>1){
1479     chi2+=0.5*TMath::Min(delta/2,2.);
1480     chi2+=2.*cluster->GetDeltaProbability();
1481   }
1482   //
1483   track->SetNy(layer,ny);
1484   track->SetNz(layer,nz);
1485   track->SetSigmaY(layer,erry);
1486   track->SetSigmaZ(layer, errz);
1487   track->SetSigmaYZ(layer,covyz);
1488   //track->fNormQ[layer] = cluster->GetQ()/TMath::Sqrt(1+theta*theta+phi*phi);
1489   track->SetNormQ(layer,cluster->GetQ()/TMath::Sqrt((1.+ track->GetTgl()*track->GetTgl())/((1.-track->GetSnp())*(1.+track->GetSnp()))));
1490   return chi2;
1491 }
1492 //________________________________________________________________
1493 Int_t AliITStrackerUpgrade::GetError(Int_t /*layer*/,
1494                                      const AliITSRecPoint *cl,
1495                                      Float_t /*tgl*/,Float_t /*tgphitr*/,Float_t /*expQ*/,
1496                                      Float_t &erry,Float_t &errz,Float_t &covyz,
1497                                      Bool_t /*addMisalErr*/)
1498 {
1499   //
1500   // Calculate cluster position error for the upgrade
1501   //
1502   Int_t retval=0;
1503   covyz=0.;
1504   retval = GetErrorOrigRecPoint(cl,erry,errz,covyz);
1505
1506
1507
1508   return retval;
1509                                                                                    
1510 }
1511 //____________________________________________________________________-
1512
1513 Int_t AliITStrackerUpgrade::GetErrorOrigRecPoint(const AliITSRecPoint*cl,
1514                                                  Float_t &erry,Float_t &errz,Float_t &covyz)
1515 {
1516   //
1517   // Calculate cluster position error (just take error from AliITSRecPoint)
1518   //
1519   erry   = TMath::Sqrt(cl->GetSigmaY2());
1520   errz   = TMath::Sqrt(cl->GetSigmaZ2());
1521   covyz  = cl->GetSigmaYZ();
1522   return 1;
1523 }
1524 //__________________________
1525 void AliITStrackerUpgrade::GetNTeor(Int_t layer,const AliITSRecPoint* /*cl*/,
1526                                     Float_t tgl,Float_t tgphitr,
1527                                     Float_t &ny,Float_t &nz)
1528 {
1529   //
1530   // Get "mean shape" (original parametrization from AliITStrackerMI)
1531   //
1532   tgl = TMath::Abs(tgl);
1533   tgphitr = TMath::Abs(tgphitr);
1534
1535   // SPD
1536   if (layer==0) {
1537     ny = 1.+tgphitr*3.2;
1538     nz = 1.+tgl*0.34;
1539     return;
1540   }
1541   if (layer==1) {
1542     ny = 1.+tgphitr*3.2;
1543     nz = 1.+tgl*0.28;
1544     return;
1545   }
1546   // SSD
1547   if (layer==4 || layer==5) {
1548     ny = 2.02+tgphitr*1.95;
1549     nz = 2.02+tgphitr*2.35;
1550     return;
1551   }
1552   // SDD
1553   ny  = 6.6-2.7*tgphitr;
1554   nz  = 2.8-3.11*tgphitr+0.45*tgl;
1555   return;
1556 }
1557 //_________________________________________________________________
1558 Int_t AliITStrackerUpgrade::PropagateBack(AliESDEvent *event) {
1559   //--------------------------------------------------------------------
1560   // This functions propagates reconstructed ITS tracks back
1561   // The clusters must be loaded !
1562   //--------------------------------------------------------------------
1563   fTrackingPhase="PropagateBack";
1564   Int_t nentr=event->GetNumberOfTracks();
1565   Info("PropagateBack", "Number of ESD tracks: %d\n", nentr);
1566   Int_t ntrk=0;
1567   for (Int_t i=0; i<nentr; i++) {
1568     AliESDtrack *esd=event->GetTrack(i);
1569
1570     if ((esd->GetStatus()&AliESDtrack::kITSin)==0) continue;
1571     if (esd->GetStatus()&AliESDtrack::kITSout) continue;
1572
1573 /*
1574     AliITStrackMI *t=0;
1575     try {
1576       t=new AliITStrackMI(*esd);
1577     } catch (const Char_t *msg) {
1578       //Warning("PropagateBack",msg);
1579       delete t;
1580       continue;
1581     }
1582 */
1583     AliITStrackMI *t = new AliITStrackMI(*esd);
1584     t->SetExpQ(TMath::Max(0.8*t->GetESDtrack()->GetTPCsignal(),30.));
1585
1586     ResetTrackToFollow(*t);
1587
1588     /*
1589     // propagate to vertex [SR, GSI 17.02.2003]
1590     // Start Time measurement [SR, GSI 17.02.2003], corrected by I.Belikov
1591     if (CorrectForPipeMaterial(&fTrackToFollow,"inward")) {
1592     if (fTrackToFollow.PropagateToVertex(event->GetVertex()))
1593     fTrackToFollow.StartTimeIntegral();
1594     // from vertex to outside pipe
1595     CorrectForPipeMaterial(&fTrackToFollow,"outward");
1596     }*/
1597     // Start time integral and add distance from current position to vertex
1598     Double_t xyzTrk[3],xyzVtx[3]={GetX(),GetY(),GetZ()};
1599     fTrackToFollow.GetXYZ(xyzTrk);
1600     Double_t dst2 = 0.;
1601     for (Int_t icoord=0; icoord<3; icoord++) {
1602       Double_t di = xyzTrk[icoord] - xyzVtx[icoord];
1603       dst2 += di*di;
1604     }
1605     fTrackToFollow.StartTimeIntegral();
1606     fTrackToFollow.AddTimeStep(TMath::Sqrt(dst2));
1607
1608     fTrackToFollow.ResetCovariance(10.); 
1609     fTrackToFollow.ResetClusters();
1610     //
1611     Int_t inx[6];
1612     for (Int_t k=0; k<6; k++) inx[k]=-1;
1613     Int_t nclusters = t->GetNumberOfClusters();
1614     for(Int_t ncl=0;ncl<nclusters;ncl++){
1615       Int_t index = t-> GetClIndex(ncl);
1616       Int_t lay = (index & 0xf0000000) >> 28;
1617       inx[lay]=index;
1618     }
1619     //   
1620     if (RefitAtBase(43.6,&fTrackToFollow,inx)) {
1621       //fTrackToFollow.SetLabel(t->GetLabel());//
1622       //fTrackToFollow.CookdEdx();
1623       //CookLabel(&fTrackToFollow,0.); //For comparison only
1624       fTrackToFollow.UpdateESDtrack(AliESDtrack::kITSout);
1625       //UseClusters(&fTrackToFollow);
1626       ntrk++;
1627     }
1628     delete t;
1629   }
1630
1631   AliInfo(Form("Number of back propagated ITS tracks: %d out of %d ESD tracks",ntrk,nentr));
1632
1633   fTrackingPhase="Default";
1634
1635   return 0;
1636 }
1637 //_________________________________________________________________________
1638 AliCluster *AliITStrackerUpgrade::GetCluster(Int_t index) const {
1639   //--------------------------------------------------------------------
1640   //       Return pointer to a given cluster
1641   //--------------------------------------------------------------------
1642   Int_t l=(index & 0xf0000000) >> 28;
1643   Int_t c=(index & 0x0fffffff) >> 0;
1644   AliInfo(Form("index %i  cluster index %i layer %i", index,index & 0x0fffffff,index & 0xf0000000));
1645   return fLayers[l]->GetCluster(c);
1646 }
1647 //______________________________________________________________________________
1648 Int_t AliITStrackerUpgrade::CorrectForPipeMaterial(AliITStrackMI *t, TString direction) {
1649 //-------------------------------------------------------------------
1650 // Propagate beyond beam pipe and correct for material
1651 // (material budget in different ways according to fUseTGeo value)
1652 // Add time if going outward (PropagateTo or PropagateToTGeo)
1653 //-------------------------------------------------------------------
1654
1655 // Define budget mode:
1656 // 0: material from AliITSRecoParam (hard coded)
1657 // 1: material from TGeo in one step (on the fly)
1658 // 2: material from lut
1659 // 3: material from TGeo in one step (same for all hypotheses)
1660 Int_t mode;
1661 switch(fUseTGeo) {
1662  case 0:
1663    mode=0;
1664    break;
1665  case 1:
1666    mode=1;
1667    break;
1668  case 2:
1669    mode=2;
1670    break;
1671  case 3:
1672    if(fTrackingPhase.Contains("Clusters2Tracks"))
1673      { mode=3; } else { mode=1; }
1674    break;
1675  case 4:
1676    if(fTrackingPhase.Contains("Clusters2Tracks"))
1677      { mode=3; } else { mode=2; }
1678    break;
1679  default:
1680    mode=0;
1681    break;
1682  }
1683 if(fTrackingPhase.Contains("Default")) mode=0;
1684 Int_t index=fCurrentEsdTrack;
1685
1686 Float_t  dir = (direction.Contains("inward") ? 1. : -1.);
1687 Double_t rToGo=(dir>0 ? AliITSRecoParam::GetrInsidePipe() : AliITSRecoParam::GetrOutsidePipe());
1688 Double_t xToGo;
1689 if (!t->GetLocalXat(rToGo,xToGo)) return 0;
1690
1691 Double_t xOverX0,x0,lengthTimesMeanDensity;
1692
1693 switch(mode) {
1694  case 0:
1695    xOverX0 = AliITSRecoParam::GetdPipe();
1696    x0 = AliITSRecoParam::GetX0Be();
1697    lengthTimesMeanDensity = xOverX0*x0;
1698    lengthTimesMeanDensity *= dir;
1699    if (!t->PropagateTo(xToGo,xOverX0,lengthTimesMeanDensity/xOverX0)) return 0;
1700    break;
1701  case 1:
1702    if (!t->PropagateToTGeo(xToGo,20)) return 0; // "20" removes the d0 mean shift of ~20 um -> To be understood.   
1703    break;
1704  case 2:
1705    if(fxOverX0Pipe<0) BuildMaterialLUT("Pipe");
1706    xOverX0 = fxOverX0Pipe;
1707    lengthTimesMeanDensity = fxTimesRhoPipe;
1708    lengthTimesMeanDensity *= dir;
1709    if (!t->PropagateTo(xToGo,xOverX0,lengthTimesMeanDensity/xOverX0)) return 0;
1710    break;
1711  case 3:
1712    if(!fxOverX0PipeTrks || index<0 || index>=fNtracks) Error("CorrectForPipeMaterial","Incorrect usage of UseTGeo option!\n");
1713    if(fxOverX0PipeTrks[index]<0) {
1714      if (!t->PropagateToTGeo(xToGo,1,xOverX0,lengthTimesMeanDensity)) return 0;
1715      Double_t angle=TMath::Sqrt((1.+t->GetTgl()*t->GetTgl())/
1716                                 ((1.-t->GetSnp())*(1.+t->GetSnp())));
1717      fxOverX0PipeTrks[index] = TMath::Abs(xOverX0)/angle;
1718      fxTimesRhoPipeTrks[index] = TMath::Abs(lengthTimesMeanDensity)/angle;
1719      return 1;
1720    }
1721    xOverX0 = fxOverX0PipeTrks[index];
1722    lengthTimesMeanDensity = fxTimesRhoPipeTrks[index];
1723    lengthTimesMeanDensity *= dir;
1724    if (!t->PropagateTo(xToGo,xOverX0,lengthTimesMeanDensity/xOverX0)) return 0;
1725    break;
1726  }
1727
1728 return 1;
1729 }
1730 //__________________________________________________________________________
1731 Int_t AliITStrackerUpgrade::RefitInward(AliESDEvent *event) {
1732   //--------------------------------------------------------------------
1733   // This functions refits ITS tracks using the
1734   // "inward propagated" TPC tracks
1735   // The clusters must be loaded !
1736   //--------------------------------------------------------------------
1737   fTrackingPhase="RefitInward";
1738   Int_t nentr=event->GetNumberOfTracks();
1739   for (Int_t i=0; i<nentr; i++) {
1740     AliESDtrack *esd=event->GetTrack(i);
1741
1742     // if ((esd->GetStatus()&AliESDtrack::kITSout) == 0) continue;
1743     // if (esd->GetStatus()&AliESDtrack::kITSrefit) continue;
1744     //if (esd->GetStatus()&AliESDtrack::kTPCout)
1745     //  if ((esd->GetStatus()&AliESDtrack::kTPCrefit)==0) continue;
1746     AliITStrackMI *t=0;
1747     try {
1748       t=new AliITStrackMI(*esd);
1749     } catch (const Char_t *msg) {
1750       //Warning("RefitInward",msg);
1751       delete t;
1752       continue;
1753     }
1754
1755     ResetTrackToFollow(*t);
1756     // fTrackToFollow.ResetClusters();
1757     //  if ((esd->GetStatus()&AliESDtrack::kTPCin)==0)
1758     //  fTrackToFollow.ResetCovariance(10.);
1759     //Refitting...
1760     //The beam pipe
1761     // if (CorrectForPipeMaterial(&fTrackToFollow,"inward")) {
1762     fTrackToFollow.UpdateESDtrack(AliESDtrack::kITSrefit);
1763     AliESDtrack  *esdTrack =fTrackToFollow.GetESDtrack();
1764     Double_t r[3]={0.,0.,0.};
1765     Double_t maxD=3.;
1766     esdTrack->RelateToVertex(event->GetVertex(),GetBz(r),maxD);
1767     // }
1768     delete t;
1769   }
1770
1771   fTrackingPhase="Default";
1772   return 0;
1773 }
1774
1775
1776