]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCtrackerMI.cxx
ESD track based on AliExternalTrackParam. Class redesign and clean-up (Yu.Belikov)
[u/mrichter/AliRoot.git] / TPC / AliTPCtrackerMI.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 //-------------------------------------------------------
18 //          Implementation of the TPC tracker
19 //
20 //   Origin: Marian Ivanov   Marian.Ivanov@cern.ch
21 // 
22 //  AliTPC parallel tracker - 
23 //  How to use?  - 
24 //  run AliTPCFindClusters.C macro - clusters neccessary for tracker are founded
25 //  run AliTPCFindTracksMI.C macro - to find tracks
26 //  tracks are written to AliTPCtracks.root file
27 //  for comparison also seeds are written to the same file - to special branch
28 //-------------------------------------------------------
29
30
31 /* $Id$ */
32
33 #include "Riostream.h"
34 #include <TClonesArray.h>
35 #include <TFile.h>
36 #include <TObjArray.h>
37 #include <TTree.h>
38
39 #include "AliComplexCluster.h"
40 #include "AliESD.h"
41 #include "AliESDkink.h"
42 #include "AliHelix.h"
43 #include "AliRunLoader.h"
44 #include "AliTPCClustersRow.h"
45 #include "AliTPCParam.h"
46 #include "AliTPCReconstructor.h"
47 #include "AliTPCclusterMI.h"
48 #include "AliTPCpolyTrack.h"
49 #include "AliTPCreco.h"
50 #include "AliTPCseed.h" 
51 #include "AliTPCtrackerMI.h"
52 #include "TStopwatch.h"
53 #include "AliTPCReconstructor.h"
54 #include "AliESDkink.h"
55 #include "AliPID.h"
56 #include "TTreeStream.h"
57 //
58
59 ClassImp(AliTPCtrackerMI)
60
61
62 class AliTPCFastMath {
63 public:
64   AliTPCFastMath();  
65   static Double_t FastAsin(Double_t x);   
66  private: 
67   static Double_t fgFastAsin[20000];  //lookup table for fast asin computation
68 };
69
70 Double_t AliTPCFastMath::fgFastAsin[20000];
71 AliTPCFastMath gAliTPCFastMath; // needed to fill the LUT
72
73 AliTPCFastMath::AliTPCFastMath(){
74   //
75   // initialized lookup table;
76   for (Int_t i=0;i<10000;i++){
77     fgFastAsin[2*i] = TMath::ASin(i/10000.);
78     fgFastAsin[2*i+1] = (TMath::ASin((i+1)/10000.)-fgFastAsin[2*i]);
79   }
80 }
81
82 Double_t AliTPCFastMath::FastAsin(Double_t x){
83   //
84   // return asin using lookup table
85   if (x>0){
86     Int_t index = int(x*10000);
87     return fgFastAsin[2*index]+(x*10000.-index)*fgFastAsin[2*index+1];
88   }
89   x*=-1;
90   Int_t index = int(x*10000);
91   return -(fgFastAsin[2*index]+(x*10000.-index)*fgFastAsin[2*index+1]);
92 }
93
94
95
96
97 Int_t AliTPCtrackerMI::UpdateTrack(AliTPCseed * track, Int_t accept){
98   //
99   //update track information using current cluster - track->fCurrentCluster
100
101
102   AliTPCclusterMI* c =track->fCurrentCluster;
103   if (accept>0) track->fCurrentClusterIndex1 |=0x8000;  //sign not accepted clusters
104
105   UInt_t i = track->fCurrentClusterIndex1;
106
107   Int_t sec=(i&0xff000000)>>24; 
108   //Int_t row = (i&0x00ff0000)>>16; 
109   track->fRow=(i&0x00ff0000)>>16;
110   track->fSector = sec;
111   //  Int_t index = i&0xFFFF;
112   if (sec>=fParam->GetNInnerSector()) track->fRow += fParam->GetNRowLow(); 
113   track->SetClusterIndex2(track->fRow, i);  
114   //track->fFirstPoint = row;
115   //if ( track->fLastPoint<row) track->fLastPoint =row;
116   //  if (track->fRow<0 || track->fRow>160) {
117   //  printf("problem\n");
118   //}
119   if (track->fFirstPoint>track->fRow) 
120     track->fFirstPoint = track->fRow;
121   if (track->fLastPoint<track->fRow) 
122     track->fLastPoint  = track->fRow;
123   
124
125   track->fClusterPointer[track->fRow] = c;  
126   //
127
128   Float_t angle2 = track->GetSnp()*track->GetSnp();
129   angle2 = TMath::Sqrt(angle2/(1-angle2)); 
130   //
131   //SET NEW Track Point
132   //
133   //  if (debug)
134   {
135     AliTPCTrackerPoint   &point =*(track->GetTrackPoint(track->fRow));
136     //
137     point.SetSigmaY(c->GetSigmaY2()/track->fCurrentSigmaY2);
138     point.SetSigmaZ(c->GetSigmaZ2()/track->fCurrentSigmaZ2);
139     point.SetErrY(sqrt(track->fErrorY2));
140     point.SetErrZ(sqrt(track->fErrorZ2));
141     //
142     point.SetX(track->GetX());
143     point.SetY(track->GetY());
144     point.SetZ(track->GetZ());
145     point.SetAngleY(angle2);
146     point.SetAngleZ(track->GetTgl());
147     if (point.fIsShared){
148       track->fErrorY2 *= 4;
149       track->fErrorZ2 *= 4;
150     }
151   }  
152
153   Double_t chi2 = track->GetPredictedChi2(track->fCurrentCluster);
154   //
155   track->fErrorY2 *= 1.3;
156   track->fErrorY2 += 0.01;    
157   track->fErrorZ2 *= 1.3;   
158   track->fErrorZ2 += 0.005;      
159     //}
160   if (accept>0) return 0;
161   if (track->GetNumberOfClusters()%20==0){
162     //    if (track->fHelixIn){
163     //  TClonesArray & larr = *(track->fHelixIn);    
164     //  Int_t ihelix = larr.GetEntriesFast();
165     //  new(larr[ihelix]) AliHelix(*track) ;    
166     //}
167   }
168   track->fNoCluster =0;
169   return track->Update(c,chi2,i);
170 }
171
172
173
174 Int_t AliTPCtrackerMI::AcceptCluster(AliTPCseed * seed, AliTPCclusterMI * cluster, Float_t factor, 
175                                       Float_t cory, Float_t corz)
176 {
177   //
178   // decide according desired precision to accept given 
179   // cluster for tracking
180   Double_t sy2=ErrY2(seed,cluster)*cory;
181   Double_t sz2=ErrZ2(seed,cluster)*corz;
182   //sy2=ErrY2(seed,cluster)*cory;
183   //sz2=ErrZ2(seed,cluster)*cory;
184   
185   Double_t sdistancey2 = sy2+seed->GetSigmaY2();
186   Double_t sdistancez2 = sz2+seed->GetSigmaZ2();
187   
188   Double_t rdistancey2 = (seed->fCurrentCluster->GetY()-seed->GetY())*
189     (seed->fCurrentCluster->GetY()-seed->GetY())/sdistancey2;
190   Double_t rdistancez2 = (seed->fCurrentCluster->GetZ()-seed->GetZ())*
191     (seed->fCurrentCluster->GetZ()-seed->GetZ())/sdistancez2;
192   
193   Double_t rdistance2  = rdistancey2+rdistancez2;
194   //Int_t  accept =0;
195   
196   if (rdistance2>16) return 3;
197   
198   
199   if ((rdistancey2>9.*factor || rdistancez2>9.*factor) && cluster->GetType()==0)  
200     return 2;  //suspisiouce - will be changed
201   
202   if ((rdistancey2>6.25*factor || rdistancez2>6.25*factor) && cluster->GetType()>0)  
203     // strict cut on overlaped cluster
204     return  2;  //suspisiouce - will be changed
205   
206   if ( (rdistancey2>1.*factor || rdistancez2>6.25*factor ) 
207        && cluster->GetType()<0){
208     seed->fNFoundable--;
209     return 2;    
210   }
211   return 0;
212 }
213
214
215
216
217 //_____________________________________________________________________________
218 AliTPCtrackerMI::AliTPCtrackerMI(const AliTPCParam *par): 
219 AliTracker(), fkNIS(par->GetNInnerSector()/2), fkNOS(par->GetNOuterSector()/2)
220 {
221   //---------------------------------------------------------------------
222   // The main TPC tracker constructor
223   //---------------------------------------------------------------------
224   fInnerSec=new AliTPCSector[fkNIS];         
225   fOuterSec=new AliTPCSector[fkNOS];
226  
227   Int_t i;
228   for (i=0; i<fkNIS; i++) fInnerSec[i].Setup(par,0);
229   for (i=0; i<fkNOS; i++) fOuterSec[i].Setup(par,1);
230
231   fN=0;  fSectors=0;
232
233   fSeeds=0;
234   fNtracks = 0;
235   fParam = par;  
236   Int_t nrowlow = par->GetNRowLow();
237   Int_t nrowup = par->GetNRowUp();
238
239   
240   for (Int_t i=0;i<nrowlow;i++){
241     fXRow[i]     = par->GetPadRowRadiiLow(i);
242     fPadLength[i]= par->GetPadPitchLength(0,i);
243     fYMax[i]     = fXRow[i]*TMath::Tan(0.5*par->GetInnerAngle());
244   }
245
246   
247   for (Int_t i=0;i<nrowup;i++){
248     fXRow[i+nrowlow]      = par->GetPadRowRadiiUp(i);
249     fPadLength[i+nrowlow] = par->GetPadPitchLength(60,i);
250     fYMax[i+nrowlow]      = fXRow[i+nrowlow]*TMath::Tan(0.5*par->GetOuterAngle());
251   }
252   fSeeds=0;
253   //
254   fInput    = 0;
255   fOutput   = 0;
256   fSeedTree = 0;
257   fTreeDebug =0;
258   fNewIO     =0;
259   fDebug     =0;
260   fEvent     =0;
261   fDebugStreamer = new TTreeSRedirector("TPCdebug.root");
262 }
263 //________________________________________________________________________
264 AliTPCtrackerMI::AliTPCtrackerMI(const AliTPCtrackerMI &t):
265   AliTracker(t),
266   fkNIS(t.fkNIS),
267   fkNOS(t.fkNOS)
268 {
269   //------------------------------------
270   // dummy copy constructor
271   //------------------------------------------------------------------
272 }
273 AliTPCtrackerMI & AliTPCtrackerMI::operator=(const AliTPCtrackerMI& /*r*/){
274   //------------------------------
275   // dummy 
276   //--------------------------------------------------------------
277   return *this;
278 }
279 //_____________________________________________________________________________
280 AliTPCtrackerMI::~AliTPCtrackerMI() {
281   //------------------------------------------------------------------
282   // TPC tracker destructor
283   //------------------------------------------------------------------
284   delete[] fInnerSec;
285   delete[] fOuterSec;
286   if (fSeeds) {
287     fSeeds->Delete(); 
288     delete fSeeds;
289   }
290   if (fDebugStreamer) delete fDebugStreamer;
291 }
292
293 void AliTPCtrackerMI::SetIO()
294 {
295   //
296   fNewIO   =  kTRUE;
297   fInput   =  AliRunLoader::GetTreeR("TPC", kFALSE,AliConfig::GetDefaultEventFolderName());
298   
299   fOutput  =  AliRunLoader::GetTreeT("TPC", kTRUE,AliConfig::GetDefaultEventFolderName());
300   if (fOutput){
301     AliTPCtrack *iotrack= new AliTPCtrack;
302     fOutput->Branch("tracks","AliTPCtrack",&iotrack,32000,100);
303     delete iotrack;
304   }
305 }
306
307
308 void AliTPCtrackerMI::SetIO(TTree * input, TTree * output, AliESD * event)
309 {
310
311   // set input
312   fNewIO = kFALSE;
313   fInput    = 0;
314   fOutput   = 0;
315   fSeedTree = 0;
316   fTreeDebug =0;
317   fInput = input;
318   if (input==0){
319     return;
320   }  
321   //set output
322   fOutput = output;
323   if (output){
324     AliTPCtrack *iotrack= new AliTPCtrack;
325     //    iotrack->fHelixIn   = new TClonesArray("AliHelix");
326     //iotrack->fHelixOut  = new TClonesArray("AliHelix");    
327     fOutput->Branch("tracks","AliTPCtrack",&iotrack,32000,100);
328     delete iotrack;
329   }
330   if (output && (fDebug&2)){
331     //write the full seed information if specified in debug mode
332     //
333     fSeedTree =  new TTree("Seeds","Seeds");
334     AliTPCseed * vseed = new AliTPCseed;
335     //
336     TClonesArray * arrtr = new TClonesArray("AliTPCTrackPoint",160);
337     arrtr->ExpandCreateFast(160);
338     TClonesArray * arre = new TClonesArray("AliTPCExactPoint",160);
339     //
340     vseed->fPoints = arrtr;
341     vseed->fEPoints = arre;
342     //    vseed->fClusterPoints = arrcl;
343     fSeedTree->Branch("seeds","AliTPCseed",&vseed,32000,99);
344     delete arrtr;
345     delete arre;    
346     fTreeDebug = new TTree("trackDebug","trackDebug");
347     TClonesArray * arrd = new TClonesArray("AliTPCTrackPoint2",0);
348     fTreeDebug->Branch("debug",&arrd,32000,99);
349   }
350
351
352   //set ESD event  
353   fEvent  = event;  
354 }
355
356 void AliTPCtrackerMI::FillESD(TObjArray* arr)
357 {
358   //
359   //
360   //fill esds using updated tracks
361   if (fEvent){
362     // write tracks to the event
363     // store index of the track
364     Int_t nseed=arr->GetEntriesFast();
365     //FindKinks(arr,fEvent);
366     for (Int_t i=0; i<nseed; i++) {
367       AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
368       if (!pt) continue; 
369       pt->UpdatePoints();
370       //      pt->PropagateTo(fParam->GetInnerRadiusLow());
371       if (pt->GetKinkIndex(0)<=0){  //don't propagate daughter tracks 
372         pt->PropagateTo(fParam->GetInnerRadiusLow());
373       }
374  
375       if (( pt->GetPoints()[2]- pt->GetPoints()[0])>5 && pt->GetPoints()[3]>0.8){
376         AliESDtrack iotrack;
377         iotrack.UpdateTrackParams(pt,AliESDtrack::kTPCin);
378         iotrack.SetTPCPoints(pt->GetPoints());
379         iotrack.SetKinkIndexes(pt->GetKinkIndexes());
380         iotrack.SetV0Indexes(pt->GetV0Indexes());
381         //      iotrack.SetTPCpid(pt->fTPCr);
382         //iotrack.SetTPCindex(i);
383         fEvent->AddTrack(&iotrack);
384         continue;
385       }
386        
387       if ( (pt->GetNumberOfClusters()>70)&& (Float_t(pt->GetNumberOfClusters())/Float_t(pt->fNFoundable))>0.55) {
388         AliESDtrack iotrack;
389         iotrack.UpdateTrackParams(pt,AliESDtrack::kTPCin);
390         iotrack.SetTPCPoints(pt->GetPoints());
391         //iotrack.SetTPCindex(i);
392         iotrack.SetKinkIndexes(pt->GetKinkIndexes());
393         iotrack.SetV0Indexes(pt->GetV0Indexes());
394         //      iotrack.SetTPCpid(pt->fTPCr);
395         fEvent->AddTrack(&iotrack);
396         continue;
397       } 
398       //
399       // short tracks  - maybe decays
400
401       if ( (pt->GetNumberOfClusters()>30) && (Float_t(pt->GetNumberOfClusters())/Float_t(pt->fNFoundable))>0.70) {
402         Int_t found,foundable,shared;
403         pt->GetClusterStatistic(0,60,found, foundable,shared,kFALSE);
404         if ( (found>20) && (pt->fNShared/float(pt->GetNumberOfClusters())<0.2)){
405           AliESDtrack iotrack;
406           iotrack.UpdateTrackParams(pt,AliESDtrack::kTPCin);    
407           //iotrack.SetTPCindex(i);
408           iotrack.SetTPCPoints(pt->GetPoints());
409           iotrack.SetKinkIndexes(pt->GetKinkIndexes());
410           iotrack.SetV0Indexes(pt->GetV0Indexes());
411           //iotrack.SetTPCpid(pt->fTPCr);
412           fEvent->AddTrack(&iotrack);
413           continue;
414         }
415       }       
416       
417       if ( (pt->GetNumberOfClusters()>20) && (Float_t(pt->GetNumberOfClusters())/Float_t(pt->fNFoundable))>0.8) {
418         Int_t found,foundable,shared;
419         pt->GetClusterStatistic(0,60,found, foundable,shared,kFALSE);
420         if (found<20) continue;
421         if (pt->fNShared/float(pt->GetNumberOfClusters())>0.2) continue;
422         //
423         AliESDtrack iotrack;
424         iotrack.UpdateTrackParams(pt,AliESDtrack::kTPCin);      
425         iotrack.SetTPCPoints(pt->GetPoints());
426         iotrack.SetKinkIndexes(pt->GetKinkIndexes());
427         iotrack.SetV0Indexes(pt->GetV0Indexes());
428         //iotrack.SetTPCpid(pt->fTPCr);
429         //iotrack.SetTPCindex(i);
430         fEvent->AddTrack(&iotrack);
431         continue;
432       }   
433       // short tracks  - secondaties
434       //
435       if ( (pt->GetNumberOfClusters()>30) ) {
436         Int_t found,foundable,shared;
437         pt->GetClusterStatistic(128,158,found, foundable,shared,kFALSE);
438         if ( (found>20) && (pt->fNShared/float(pt->GetNumberOfClusters())<0.2) &&float(found)/float(foundable)>0.8){
439           AliESDtrack iotrack;
440           iotrack.UpdateTrackParams(pt,AliESDtrack::kTPCin);    
441           iotrack.SetTPCPoints(pt->GetPoints());
442           iotrack.SetKinkIndexes(pt->GetKinkIndexes());
443           iotrack.SetV0Indexes(pt->GetV0Indexes());
444           //iotrack.SetTPCpid(pt->fTPCr);       
445           //iotrack.SetTPCindex(i);
446           fEvent->AddTrack(&iotrack);
447           continue;
448         }
449       }       
450       
451       if ( (pt->GetNumberOfClusters()>15)) {
452         Int_t found,foundable,shared;
453         pt->GetClusterStatistic(138,158,found, foundable,shared,kFALSE);
454         if (found<15) continue;
455         if (pt->fNShared/float(pt->GetNumberOfClusters())>0.2) continue;
456         if (float(found)/float(foundable)<0.8) continue;
457         //
458         AliESDtrack iotrack;
459         iotrack.UpdateTrackParams(pt,AliESDtrack::kTPCin);      
460         iotrack.SetTPCPoints(pt->GetPoints());
461         iotrack.SetKinkIndexes(pt->GetKinkIndexes());
462         iotrack.SetV0Indexes(pt->GetV0Indexes());
463         //      iotrack.SetTPCpid(pt->fTPCr);
464         //iotrack.SetTPCindex(i);
465         fEvent->AddTrack(&iotrack);
466         continue;
467       }   
468     }
469   }
470   printf("Number of filled ESDs-\t%d\n",fEvent->GetNumberOfTracks());
471 }
472
473 void AliTPCtrackerMI::WriteTracks(TTree * tree)
474 {
475   //
476   // write tracks from seed array to selected tree
477   //
478   fOutput  = tree;
479   if (fOutput){
480     AliTPCtrack *iotrack= new AliTPCtrack;
481     fOutput->Branch("tracks","AliTPCtrack",&iotrack,32000,100);
482   }
483   WriteTracks();
484 }
485
486 void AliTPCtrackerMI::WriteTracks()
487 {
488   //
489   // write tracks to the given output tree -
490   // output specified with SetIO routine
491   if (!fSeeds)  return;
492   if (!fOutput){
493     SetIO();
494   }
495
496   if (fOutput){
497     AliTPCtrack *iotrack= 0;
498     Int_t nseed=fSeeds->GetEntriesFast();
499     //for (Int_t i=0; i<nseed; i++) {
500     //  iotrack= (AliTPCtrack*)fSeeds->UncheckedAt(i);
501     //  if (iotrack) break;      
502     //}    
503     //TBranch * br = fOutput->Branch("tracks","AliTPCtrack",&iotrack,32000,100);
504     TBranch * br = fOutput->GetBranch("tracks");
505     br->SetAddress(&iotrack);
506     //
507     for (Int_t i=0; i<nseed; i++) {
508       AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i);    
509       if (!pt) continue;    
510       AliTPCtrack * track = new AliTPCtrack(*pt);
511       iotrack = track;
512       pt->fLab2 =i; 
513       //      br->SetAddress(&iotrack);
514       fOutput->Fill();
515       delete track;
516       iotrack =0;
517     }
518     //fOutput->GetDirectory()->cd();
519     //fOutput->Write();
520   }
521   // delete iotrack;
522   //
523   if (fSeedTree){
524     //write the full seed information if specified in debug mode
525       
526     AliTPCseed * vseed = new AliTPCseed;
527     //
528     TClonesArray * arrtr = new TClonesArray("AliTPCTrackPoint",160);
529     arrtr->ExpandCreateFast(160);
530     //TClonesArray * arrcl = new TClonesArray("AliTPCclusterMI",160);
531     //arrcl->ExpandCreateFast(160);
532     TClonesArray * arre = new TClonesArray("AliTPCExactPoint",160);
533     //
534     vseed->fPoints = arrtr;
535     vseed->fEPoints = arre;
536     //    vseed->fClusterPoints = arrcl;
537     //TBranch * brseed = seedtree->Branch("seeds","AliTPCseed",&vseed,32000,99);
538     TBranch * brseed = fSeedTree->GetBranch("seeds");
539     
540     Int_t nseed=fSeeds->GetEntriesFast();
541     
542     for (Int_t i=0; i<nseed; i++) {
543       AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i);    
544       if (!pt) continue;     
545       pt->fPoints = arrtr;
546       //      pt->fClusterPoints = arrcl;
547       pt->fEPoints       = arre;
548       pt->RebuildSeed();
549       vseed = pt;
550       brseed->SetAddress(&vseed);
551       fSeedTree->Fill();
552       pt->fPoints  = 0;
553       pt->fEPoints = 0;
554       //      pt->fClusterPoints = 0;
555     }
556     fSeedTree->Write();
557     if (fTreeDebug) fTreeDebug->Write();
558   }
559
560 }
561   
562
563
564
565 Double_t AliTPCtrackerMI::ErrY2(AliTPCseed* seed, AliTPCclusterMI * cl){
566   //
567   //
568   //seed->SetErrorY2(0.1);
569   //return 0.1;
570   //calculate look-up table at the beginning
571   static Bool_t  ginit = kFALSE;
572   static Float_t gnoise1,gnoise2,gnoise3;
573   static Float_t ggg1[10000];
574   static Float_t ggg2[10000];
575   static Float_t ggg3[10000];
576   static Float_t glandau1[10000];
577   static Float_t glandau2[10000];
578   static Float_t glandau3[10000];
579   //
580   static Float_t gcor01[500];
581   static Float_t gcor02[500];
582   static Float_t gcorp[500];
583   //
584
585   //
586   if (ginit==kFALSE){
587     for (Int_t i=1;i<500;i++){
588       Float_t rsigma = float(i)/100.;
589       gcor02[i] = TMath::Max(0.78 +TMath::Exp(7.4*(rsigma-1.2)),0.6);
590       gcor01[i] = TMath::Max(0.72 +TMath::Exp(3.36*(rsigma-1.2)),0.6);
591       gcorp[i]  = TMath::Max(TMath::Power((rsigma+0.5),1.5),1.2);
592     }
593
594     //
595     for (Int_t i=3;i<10000;i++){
596       //
597       //
598       // inner sector
599       Float_t amp = float(i);
600       Float_t padlength =0.75;
601       gnoise1 = 0.0004/padlength;
602       Float_t nel     = 0.268*amp;
603       Float_t nprim   = 0.155*amp;
604       ggg1[i]          = fParam->GetDiffT()*fParam->GetDiffT()*(2+0.001*nel/(padlength*padlength))/nel;
605       glandau1[i]      = (2.+0.12*nprim)*0.5* (2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
606       if (glandau1[i]>1) glandau1[i]=1;
607       glandau1[i]*=padlength*padlength/12.;      
608       //
609       // outer short
610       padlength =1.;
611       gnoise2   = 0.0004/padlength;
612       nel       = 0.3*amp;
613       nprim     = 0.133*amp;
614       ggg2[i]      = fParam->GetDiffT()*fParam->GetDiffT()*(2+0.0008*nel/(padlength*padlength))/nel;
615       glandau2[i]  = (2.+0.12*nprim)*0.5*(2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
616       if (glandau2[i]>1) glandau2[i]=1;
617       glandau2[i]*=padlength*padlength/12.;
618       //
619       //
620       // outer long
621       padlength =1.5;
622       gnoise3   = 0.0004/padlength;
623       nel       = 0.3*amp;
624       nprim     = 0.133*amp;
625       ggg3[i]      = fParam->GetDiffT()*fParam->GetDiffT()*(2+0.0008*nel/(padlength*padlength))/nel;
626       glandau3[i]  = (2.+0.12*nprim)*0.5*(2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
627       if (glandau3[i]>1) glandau3[i]=1;
628       glandau3[i]*=padlength*padlength/12.;
629       //
630     }
631     ginit = kTRUE;
632   }
633   //
634   //
635   //
636   Int_t amp = int(TMath::Abs(cl->GetQ()));  
637   if (amp>9999) {
638     seed->SetErrorY2(1.);
639     return 1.;
640   }
641   Float_t snoise2;
642   Float_t z = TMath::Abs(fParam->GetZLength()-TMath::Abs(seed->GetZ()));
643   Int_t ctype = cl->GetType();  
644   Float_t padlength= GetPadPitchLength(seed->fRow);
645   Float_t angle2 = seed->GetSnp()*seed->GetSnp();
646   angle2 = angle2/(1-angle2); 
647   //
648   //cluster "quality"
649   Int_t rsigmay = int(100.*cl->GetSigmaY2()/(seed->fCurrentSigmaY2));
650   Float_t res;
651   //
652   if (fSectors==fInnerSec){
653     snoise2 = gnoise1;
654     res     = ggg1[amp]*z+glandau1[amp]*angle2;     
655     if (ctype==0) res *= gcor01[rsigmay];
656     if ((ctype>0)){
657       res+=0.002;
658       res*= gcorp[rsigmay];
659     }
660   }
661   else {
662     if (padlength<1.1){
663       snoise2 = gnoise2;
664       res     = ggg2[amp]*z+glandau2[amp]*angle2; 
665       if (ctype==0) res *= gcor02[rsigmay];      
666       if ((ctype>0)){
667         res+=0.002;
668         res*= gcorp[rsigmay];
669       }
670     }
671     else{
672       snoise2 = gnoise3;      
673       res     = ggg3[amp]*z+glandau3[amp]*angle2; 
674       if (ctype==0) res *= gcor02[rsigmay];
675       if ((ctype>0)){
676         res+=0.002;
677         res*= gcorp[rsigmay];
678       }
679     }
680   }  
681
682   if (ctype<0){
683     res+=0.005;
684     res*=2.4;  // overestimate error 2 times
685   }
686   res+= snoise2;
687  
688   if (res<2*snoise2)
689     res = 2*snoise2;
690   
691   seed->SetErrorY2(res);
692   return res;
693
694
695 }
696
697
698
699 Double_t AliTPCtrackerMI::ErrZ2(AliTPCseed* seed, AliTPCclusterMI * cl){
700   //
701   //
702   //seed->SetErrorY2(0.1);
703   //return 0.1;
704   //calculate look-up table at the beginning
705   static Bool_t  ginit = kFALSE;
706   static Float_t gnoise1,gnoise2,gnoise3;
707   static Float_t ggg1[10000];
708   static Float_t ggg2[10000];
709   static Float_t ggg3[10000];
710   static Float_t glandau1[10000];
711   static Float_t glandau2[10000];
712   static Float_t glandau3[10000];
713   //
714   static Float_t gcor01[1000];
715   static Float_t gcor02[1000];
716   static Float_t gcorp[1000];
717   //
718
719   //
720   if (ginit==kFALSE){
721     for (Int_t i=1;i<1000;i++){
722       Float_t rsigma = float(i)/100.;
723       gcor02[i] = TMath::Max(0.81 +TMath::Exp(6.8*(rsigma-1.2)),0.6);
724       gcor01[i] = TMath::Max(0.72 +TMath::Exp(2.04*(rsigma-1.2)),0.6);
725       gcorp[i]  = TMath::Max(TMath::Power((rsigma+0.5),1.5),1.2);
726     }
727
728     //
729     for (Int_t i=3;i<10000;i++){
730       //
731       //
732       // inner sector
733       Float_t amp = float(i);
734       Float_t padlength =0.75;
735       gnoise1 = 0.0004/padlength;
736       Float_t nel     = 0.268*amp;
737       Float_t nprim   = 0.155*amp;
738       ggg1[i]          = fParam->GetDiffT()*fParam->GetDiffT()*(2+0.001*nel/(padlength*padlength))/nel;
739       glandau1[i]      = (2.+0.12*nprim)*0.5* (2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
740       if (glandau1[i]>1) glandau1[i]=1;
741       glandau1[i]*=padlength*padlength/12.;      
742       //
743       // outer short
744       padlength =1.;
745       gnoise2   = 0.0004/padlength;
746       nel       = 0.3*amp;
747       nprim     = 0.133*amp;
748       ggg2[i]      = fParam->GetDiffT()*fParam->GetDiffT()*(2+0.0008*nel/(padlength*padlength))/nel;
749       glandau2[i]  = (2.+0.12*nprim)*0.5*(2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
750       if (glandau2[i]>1) glandau2[i]=1;
751       glandau2[i]*=padlength*padlength/12.;
752       //
753       //
754       // outer long
755       padlength =1.5;
756       gnoise3   = 0.0004/padlength;
757       nel       = 0.3*amp;
758       nprim     = 0.133*amp;
759       ggg3[i]      = fParam->GetDiffT()*fParam->GetDiffT()*(2+0.0008*nel/(padlength*padlength))/nel;
760       glandau3[i]  = (2.+0.12*nprim)*0.5*(2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
761       if (glandau3[i]>1) glandau3[i]=1;
762       glandau3[i]*=padlength*padlength/12.;
763       //
764     }
765     ginit = kTRUE;
766   }
767   //
768   //
769   //
770   Int_t amp = int(TMath::Abs(cl->GetQ()));  
771   if (amp>9999) {
772     seed->SetErrorY2(1.);
773     return 1.;
774   }
775   Float_t snoise2;
776   Float_t z = TMath::Abs(fParam->GetZLength()-TMath::Abs(seed->GetZ()));
777   Int_t ctype = cl->GetType();  
778   Float_t padlength= GetPadPitchLength(seed->fRow);
779   //
780   Float_t angle2 = seed->GetSnp()*seed->GetSnp();
781   //  if (angle2<0.6) angle2 = 0.6;
782   angle2 = seed->GetTgl()*seed->GetTgl()*(1+angle2/(1-angle2)); 
783   //
784   //cluster "quality"
785   Int_t rsigmaz = int(100.*cl->GetSigmaZ2()/(seed->fCurrentSigmaZ2));
786   Float_t res;
787   //
788   if (fSectors==fInnerSec){
789     snoise2 = gnoise1;
790     res     = ggg1[amp]*z+glandau1[amp]*angle2;     
791     if (ctype==0) res *= gcor01[rsigmaz];
792     if ((ctype>0)){
793       res+=0.002;
794       res*= gcorp[rsigmaz];
795     }
796   }
797   else {
798     if (padlength<1.1){
799       snoise2 = gnoise2;
800       res     = ggg2[amp]*z+glandau2[amp]*angle2; 
801       if (ctype==0) res *= gcor02[rsigmaz];      
802       if ((ctype>0)){
803         res+=0.002;
804         res*= gcorp[rsigmaz];
805       }
806     }
807     else{
808       snoise2 = gnoise3;      
809       res     = ggg3[amp]*z+glandau3[amp]*angle2; 
810       if (ctype==0) res *= gcor02[rsigmaz];
811       if ((ctype>0)){
812         res+=0.002;
813         res*= gcorp[rsigmaz];
814       }
815     }
816   }  
817
818   if (ctype<0){
819     res+=0.002;
820     res*=1.3;
821   }
822   if ((ctype<0) &&amp<70){
823     res+=0.002;
824     res*=1.3;  
825   }
826   res += snoise2;
827   if (res<2*snoise2)
828      res = 2*snoise2;
829   if (res>3) res =3;
830   seed->SetErrorZ2(res);
831   return res;
832 }
833
834
835
836 /*
837 Double_t AliTPCtrackerMI::ErrZ2(AliTPCseed* seed, AliTPCclusterMI * cl){
838   //
839   //
840   //seed->SetErrorZ2(0.1);
841   //return 0.1;
842
843   Float_t snoise2;
844   Float_t z = TMath::Abs(fParam->GetZLength()-TMath::Abs(seed->GetZ()));
845   //
846   Float_t rsigmaz = cl->GetSigmaZ2()/(seed->fCurrentSigmaZ2);
847   Int_t ctype = cl->GetType();
848   Float_t amp = TMath::Abs(cl->GetQ());
849   
850   Float_t nel;
851   Float_t nprim;
852   //
853   Float_t landau=2 ;    //landau fluctuation part
854   Float_t gg=2;         // gg fluctuation part
855   Float_t padlength= GetPadPitchLength(seed->GetX());
856  
857   if (fSectors==fInnerSec){
858     snoise2 = 0.0004/padlength;
859     nel     = 0.268*amp;
860     nprim   = 0.155*amp;
861     gg      = (2+0.001*nel/(padlength*padlength))/nel;
862     landau  = (2.+0.12*nprim)*0.5*(2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
863     if (landau>1) landau=1;
864   }
865   else {
866     snoise2 = 0.0004/padlength;
867     nel     = 0.3*amp;
868     nprim   = 0.133*amp;
869     gg      = (2+0.0008*nel/(padlength*padlength))/nel;
870     landau  = (2.+0.12*nprim)*0.5*(2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
871     if (landau>1) landau=1;
872   }
873   Float_t sdiff = gg*fParam->GetDiffT()*fParam->GetDiffT()*z;
874
875   //
876   Float_t angle2 = seed->GetSnp()*seed->GetSnp();
877   angle2 = TMath::Sqrt((1-angle2));
878   if (angle2<0.6) angle2 = 0.6;
879   //angle2 = 1;
880
881   Float_t angle = seed->GetTgl()/angle2;
882   Float_t angular = landau*angle*angle*padlength*padlength/12.;
883   Float_t res = sdiff + angular;
884
885   
886   if ((ctype==0) && (fSectors ==fOuterSec))
887     res *= 0.81 +TMath::Exp(6.8*(rsigmaz-1.2));
888
889   if ((ctype==0) && (fSectors ==fInnerSec))
890     res *= 0.72 +TMath::Exp(2.04*(rsigmaz-1.2));
891   
892   if ((ctype>0)){
893     res+=0.005;
894     res*= TMath::Power(rsigmaz+0.5,1.5);  //0.31+0.147*ctype;
895   }
896   if (ctype<0){
897     res+=0.002;
898     res*=1.3;
899   }
900   if ((ctype<0) &&amp<70){
901     res+=0.002;
902     res*=1.3;  
903   }
904   res += snoise2;
905   if (res<2*snoise2)
906      res = 2*snoise2;
907
908   seed->SetErrorZ2(res);
909   return res;
910 }
911 */
912
913
914
915
916 void AliTPCtrackerMI::RotateToLocal(AliTPCseed *seed)
917 {
918   //rotate to track "local coordinata
919   Float_t x = seed->GetX();
920   Float_t y = seed->GetY();
921   Float_t ymax = x*TMath::Tan(0.5*fSectors->GetAlpha());
922   
923   if (y > ymax) {
924     seed->fRelativeSector= (seed->fRelativeSector+1) % fN;
925     if (!seed->Rotate(fSectors->GetAlpha())) 
926       return;
927   } else if (y <-ymax) {
928     seed->fRelativeSector= (seed->fRelativeSector-1+fN) % fN;
929     if (!seed->Rotate(-fSectors->GetAlpha())) 
930       return;
931   }   
932
933 }
934
935
936
937 //_____________________________________________________________________________
938 Double_t AliTPCtrackerMI::F1old(Double_t x1,Double_t y1,
939                    Double_t x2,Double_t y2,
940                    Double_t x3,Double_t y3) 
941 {
942   //-----------------------------------------------------------------
943   // Initial approximation of the track curvature
944   //-----------------------------------------------------------------
945   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
946   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
947                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
948   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
949                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
950
951   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
952   if ( xr*xr+yr*yr<=0.00000000000001) return 100;
953   return -xr*yr/sqrt(xr*xr+yr*yr); 
954 }
955
956
957
958 //_____________________________________________________________________________
959 Double_t AliTPCtrackerMI::F1(Double_t x1,Double_t y1,
960                    Double_t x2,Double_t y2,
961                    Double_t x3,Double_t y3) 
962 {
963   //-----------------------------------------------------------------
964   // Initial approximation of the track curvature
965   //-----------------------------------------------------------------
966   x3 -=x1;
967   x2 -=x1;
968   y3 -=y1;
969   y2 -=y1;
970   //  
971   Double_t det = x3*y2-x2*y3;
972   if (det==0) {
973     return 100;
974   }
975   //
976   Double_t u = 0.5* (x2*(x2-x3)+y2*(y2-y3))/det;
977   Double_t x0 = x3*0.5-y3*u;
978   Double_t y0 = y3*0.5+x3*u;
979   Double_t c2 = 1/TMath::Sqrt(x0*x0+y0*y0);
980   if (det<0) c2*=-1;
981   return c2;
982 }
983
984
985 Double_t AliTPCtrackerMI::F2(Double_t x1,Double_t y1,
986                    Double_t x2,Double_t y2,
987                    Double_t x3,Double_t y3) 
988 {
989   //-----------------------------------------------------------------
990   // Initial approximation of the track curvature
991   //-----------------------------------------------------------------
992   x3 -=x1;
993   x2 -=x1;
994   y3 -=y1;
995   y2 -=y1;
996   //  
997   Double_t det = x3*y2-x2*y3;
998   if (det==0) {
999     return 100;
1000   }
1001   //
1002   Double_t u = 0.5* (x2*(x2-x3)+y2*(y2-y3))/det;
1003   Double_t x0 = x3*0.5-y3*u; 
1004   Double_t y0 = y3*0.5+x3*u;
1005   Double_t c2 = 1/TMath::Sqrt(x0*x0+y0*y0);
1006   if (det<0) c2*=-1;
1007   x0+=x1;
1008   x0*=c2;  
1009   return x0;
1010 }
1011
1012
1013
1014 //_____________________________________________________________________________
1015 Double_t AliTPCtrackerMI::F2old(Double_t x1,Double_t y1,
1016                    Double_t x2,Double_t y2,
1017                    Double_t x3,Double_t y3) 
1018 {
1019   //-----------------------------------------------------------------
1020   // Initial approximation of the track curvature times center of curvature
1021   //-----------------------------------------------------------------
1022   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
1023   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
1024                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
1025   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
1026                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
1027
1028   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
1029   
1030   return -a/(d*y1-b)*xr/sqrt(xr*xr+yr*yr);
1031 }
1032
1033 //_____________________________________________________________________________
1034 Double_t AliTPCtrackerMI::F3(Double_t x1,Double_t y1, 
1035                    Double_t x2,Double_t y2,
1036                    Double_t z1,Double_t z2) 
1037 {
1038   //-----------------------------------------------------------------
1039   // Initial approximation of the tangent of the track dip angle
1040   //-----------------------------------------------------------------
1041   return (z1 - z2)/sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
1042 }
1043
1044
1045 Double_t AliTPCtrackerMI::F3n(Double_t x1,Double_t y1, 
1046                    Double_t x2,Double_t y2,
1047                    Double_t z1,Double_t z2, Double_t c) 
1048 {
1049   //-----------------------------------------------------------------
1050   // Initial approximation of the tangent of the track dip angle
1051   //-----------------------------------------------------------------
1052
1053   //  Double_t angle1;
1054   
1055   //angle1    =  (z1-z2)*c/(TMath::ASin(c*x1-ni)-TMath::ASin(c*x2-ni));
1056   //
1057   Double_t d  =  TMath::Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
1058   if (TMath::Abs(d*c*0.5)>1) return 0;
1059   //  Double_t   angle2    =  TMath::ASin(d*c*0.5);
1060   //  Double_t   angle2    =  AliTPCFastMath::FastAsin(d*c*0.5);
1061   Double_t   angle2    = (d*c*0.5>0.1)? TMath::ASin(d*c*0.5): AliTPCFastMath::FastAsin(d*c*0.5);
1062
1063   angle2  = (z1-z2)*c/(angle2*2.);
1064   return angle2;
1065 }
1066
1067 Bool_t   AliTPCtrackerMI::GetProlongation(Double_t x1, Double_t x2, Double_t x[5], Double_t &y, Double_t &z)
1068 {//-----------------------------------------------------------------
1069   // This function find proloncation of a track to a reference plane x=x2.
1070   //-----------------------------------------------------------------
1071   
1072   Double_t dx=x2-x1;
1073
1074   if (TMath::Abs(x[4]*x1 - x[2]) >= 0.999) {   
1075     return kFALSE;
1076   }
1077
1078   Double_t c1=x[4]*x1 - x[2], r1=sqrt(1.- c1*c1);
1079   Double_t c2=x[4]*x2 - x[2], r2=sqrt(1.- c2*c2);  
1080   y = x[0];
1081   z = x[1];
1082   
1083   Double_t dy = dx*(c1+c2)/(r1+r2);
1084   Double_t dz = 0;
1085   //
1086   Double_t delta = x[4]*dx*(c1+c2)/(c1*r2 + c2*r1);
1087   
1088   if (TMath::Abs(delta)>0.01){
1089     dz = x[3]*TMath::ASin(delta)/x[4];
1090   }else{
1091     dz = x[3]*AliTPCFastMath::FastAsin(delta)/x[4];
1092   }
1093   
1094   //dz = x[3]*AliTPCFastMath::FastAsin(delta)/x[4];
1095
1096   y+=dy;
1097   z+=dz;
1098   
1099   return kTRUE;  
1100 }
1101
1102 Int_t  AliTPCtrackerMI::LoadClusters (TTree *tree)
1103 {
1104   //
1105   //
1106   fInput = tree;
1107   return LoadClusters();
1108 }
1109
1110 Int_t  AliTPCtrackerMI::LoadClusters()
1111 {
1112   //
1113   // load clusters to the memory
1114   AliTPCClustersRow *clrow= new AliTPCClustersRow;
1115   clrow->SetClass("AliTPCclusterMI");
1116   clrow->SetArray(0);
1117   clrow->GetArray()->ExpandCreateFast(10000);
1118   //
1119   //  TTree * tree = fClustersArray.GetTree();
1120
1121   TTree * tree = fInput;
1122   TBranch * br = tree->GetBranch("Segment");
1123   br->SetAddress(&clrow);
1124   //
1125   Int_t j=Int_t(tree->GetEntries());
1126   for (Int_t i=0; i<j; i++) {
1127     br->GetEntry(i);
1128     //  
1129     Int_t sec,row;
1130     fParam->AdjustSectorRow(clrow->GetID(),sec,row);
1131     //
1132     AliTPCRow * tpcrow=0;
1133     Int_t left=0;
1134     if (sec<fkNIS*2){
1135       tpcrow = &(fInnerSec[sec%fkNIS][row]);    
1136       left = sec/fkNIS;
1137     }
1138     else{
1139       tpcrow = &(fOuterSec[(sec-fkNIS*2)%fkNOS][row]);
1140       left = (sec-fkNIS*2)/fkNOS;
1141     }
1142     if (left ==0){
1143       tpcrow->fN1 = clrow->GetArray()->GetEntriesFast();
1144       tpcrow->fClusters1 = new AliTPCclusterMI[tpcrow->fN1];
1145       for (Int_t i=0;i<tpcrow->fN1;i++) 
1146         tpcrow->fClusters1[i] = *(AliTPCclusterMI*)(clrow->GetArray()->At(i));
1147     }
1148     if (left ==1){
1149       tpcrow->fN2 = clrow->GetArray()->GetEntriesFast();
1150       tpcrow->fClusters2 = new AliTPCclusterMI[tpcrow->fN2];
1151       for (Int_t i=0;i<tpcrow->fN2;i++) 
1152         tpcrow->fClusters2[i] = *(AliTPCclusterMI*)(clrow->GetArray()->At(i));
1153     }
1154   }
1155   //
1156   delete clrow;
1157   LoadOuterSectors();
1158   LoadInnerSectors();
1159   return 0;
1160 }
1161
1162
1163 void AliTPCtrackerMI::UnloadClusters()
1164 {
1165   //
1166   // unload clusters from the memory
1167   //
1168   Int_t nrows = fOuterSec->GetNRows();
1169   for (Int_t sec = 0;sec<fkNOS;sec++)
1170     for (Int_t row = 0;row<nrows;row++){
1171       AliTPCRow*  tpcrow = &(fOuterSec[sec%fkNOS][row]);
1172       //      if (tpcrow){
1173       //        if (tpcrow->fClusters1) delete []tpcrow->fClusters1; 
1174       //        if (tpcrow->fClusters2) delete []tpcrow->fClusters2; 
1175       //}
1176       tpcrow->ResetClusters();
1177     }
1178   //
1179   nrows = fInnerSec->GetNRows();
1180   for (Int_t sec = 0;sec<fkNIS;sec++)
1181     for (Int_t row = 0;row<nrows;row++){
1182       AliTPCRow*  tpcrow = &(fInnerSec[sec%fkNIS][row]);
1183       //if (tpcrow){
1184       //        if (tpcrow->fClusters1) delete []tpcrow->fClusters1; 
1185       //if (tpcrow->fClusters2) delete []tpcrow->fClusters2; 
1186       //}
1187       tpcrow->ResetClusters();
1188     }
1189
1190   return ;
1191 }
1192
1193
1194 //_____________________________________________________________________________
1195 Int_t AliTPCtrackerMI::LoadOuterSectors() {
1196   //-----------------------------------------------------------------
1197   // This function fills outer TPC sectors with clusters.
1198   //-----------------------------------------------------------------
1199   Int_t nrows = fOuterSec->GetNRows();
1200   UInt_t index=0;
1201   for (Int_t sec = 0;sec<fkNOS;sec++)
1202     for (Int_t row = 0;row<nrows;row++){
1203       AliTPCRow*  tpcrow = &(fOuterSec[sec%fkNOS][row]);  
1204       Int_t sec2 = sec+2*fkNIS;
1205       //left
1206       Int_t ncl = tpcrow->fN1;
1207       while (ncl--) {
1208         AliTPCclusterMI *c= &(tpcrow->fClusters1[ncl]);
1209         index=(((sec2<<8)+row)<<16)+ncl;
1210         tpcrow->InsertCluster(c,index);
1211       }
1212       //right
1213       ncl = tpcrow->fN2;
1214       while (ncl--) {
1215         AliTPCclusterMI *c= &(tpcrow->fClusters2[ncl]);
1216         index=((((sec2+fkNOS)<<8)+row)<<16)+ncl;
1217         tpcrow->InsertCluster(c,index);
1218       }
1219       //
1220       // write indexes for fast acces
1221       //
1222       for (Int_t i=0;i<510;i++)
1223         tpcrow->fFastCluster[i]=-1;
1224       for (Int_t i=0;i<tpcrow->GetN();i++){
1225         Int_t zi = Int_t((*tpcrow)[i]->GetZ()+255.);
1226         tpcrow->fFastCluster[zi]=i;  // write index
1227       }
1228       Int_t last = 0;
1229       for (Int_t i=0;i<510;i++){
1230         if (tpcrow->fFastCluster[i]<0)
1231           tpcrow->fFastCluster[i] = last;
1232         else
1233           last = tpcrow->fFastCluster[i];
1234       }
1235     }  
1236   fN=fkNOS;
1237   fSectors=fOuterSec;
1238   return 0;
1239 }
1240
1241
1242 //_____________________________________________________________________________
1243 Int_t  AliTPCtrackerMI::LoadInnerSectors() {
1244   //-----------------------------------------------------------------
1245   // This function fills inner TPC sectors with clusters.
1246   //-----------------------------------------------------------------
1247   Int_t nrows = fInnerSec->GetNRows();
1248   UInt_t index=0;
1249   for (Int_t sec = 0;sec<fkNIS;sec++)
1250     for (Int_t row = 0;row<nrows;row++){
1251       AliTPCRow*  tpcrow = &(fInnerSec[sec%fkNIS][row]);
1252       //
1253       //left
1254       Int_t ncl = tpcrow->fN1;
1255       while (ncl--) {
1256         AliTPCclusterMI *c= &(tpcrow->fClusters1[ncl]);
1257         index=(((sec<<8)+row)<<16)+ncl;
1258         tpcrow->InsertCluster(c,index);
1259       }
1260       //right
1261       ncl = tpcrow->fN2;
1262       while (ncl--) {
1263         AliTPCclusterMI *c= &(tpcrow->fClusters2[ncl]);
1264         index=((((sec+fkNIS)<<8)+row)<<16)+ncl;
1265         tpcrow->InsertCluster(c,index);
1266       }
1267       //
1268       // write indexes for fast acces
1269       //
1270       for (Int_t i=0;i<510;i++)
1271         tpcrow->fFastCluster[i]=-1;
1272       for (Int_t i=0;i<tpcrow->GetN();i++){
1273         Int_t zi = Int_t((*tpcrow)[i]->GetZ()+255.);
1274         tpcrow->fFastCluster[zi]=i;  // write index
1275       }
1276       Int_t last = 0;
1277       for (Int_t i=0;i<510;i++){
1278         if (tpcrow->fFastCluster[i]<0)
1279           tpcrow->fFastCluster[i] = last;
1280         else
1281           last = tpcrow->fFastCluster[i];
1282       }
1283
1284     }  
1285    
1286   fN=fkNIS;
1287   fSectors=fInnerSec;
1288   return 0;
1289 }
1290
1291
1292
1293 //_________________________________________________________________________
1294 AliTPCclusterMI *AliTPCtrackerMI::GetClusterMI(Int_t index) const {
1295   //--------------------------------------------------------------------
1296   //       Return pointer to a given cluster
1297   //--------------------------------------------------------------------
1298   Int_t sec=(index&0xff000000)>>24; 
1299   Int_t row=(index&0x00ff0000)>>16; 
1300   Int_t ncl=(index&0x00007fff)>>00;
1301
1302   const AliTPCRow * tpcrow=0;
1303   AliTPCclusterMI * clrow =0;
1304
1305   if (sec<fkNIS*2){
1306     tpcrow = &(fInnerSec[sec%fkNIS][row]);
1307     if (tpcrow==0) return 0;
1308
1309     if (sec<fkNIS) {
1310       if (tpcrow->fN1<=ncl) return 0;
1311       clrow = tpcrow->fClusters1;
1312     }
1313     else {
1314       if (tpcrow->fN2<=ncl) return 0;
1315       clrow = tpcrow->fClusters2;
1316     }
1317   }
1318   else {
1319     tpcrow = &(fOuterSec[(sec-fkNIS*2)%fkNOS][row]);
1320     if (tpcrow==0) return 0;
1321
1322     if (sec-2*fkNIS<fkNOS) {
1323       if (tpcrow->fN1<=ncl) return 0;
1324       clrow = tpcrow->fClusters1;
1325     }
1326     else {
1327       if (tpcrow->fN2<=ncl) return 0;
1328       clrow = tpcrow->fClusters2;
1329     }
1330   }
1331
1332   return &(clrow[ncl]);      
1333   
1334 }
1335
1336
1337
1338 Int_t AliTPCtrackerMI::FollowToNext(AliTPCseed& t, Int_t nr) {
1339   //-----------------------------------------------------------------
1340   // This function tries to find a track prolongation to next pad row
1341   //-----------------------------------------------------------------
1342   //
1343   Double_t  x= GetXrow(nr), ymax=GetMaxY(nr);
1344   AliTPCclusterMI *cl=0;
1345   Int_t tpcindex= t.GetClusterIndex2(nr);
1346   //
1347   // update current shape info every 5 pad-row
1348   //  if ( (nr%5==0) || t.GetNumberOfClusters()<2 || (t.fCurrentSigmaY2<0.0001) ){
1349     GetShape(&t,nr);    
1350     //}
1351   //  
1352   if (fIteration>0 && tpcindex>=-1){  //if we have already clusters 
1353     //        
1354     if (tpcindex==-1) return 0; //track in dead zone
1355     if (tpcindex>0){     //
1356       cl = t.fClusterPointer[nr];
1357       if ( (cl==0) ) cl = GetClusterMI(tpcindex);
1358       t.fCurrentClusterIndex1 = tpcindex; 
1359     }
1360     if (cl){      
1361       Int_t relativesector = ((tpcindex&0xff000000)>>24)%18;  // if previously accepted cluster in different sector
1362       Float_t angle = relativesector*fSectors->GetAlpha()+fSectors->GetAlphaShift();
1363       //
1364       if (angle<-TMath::Pi()) angle += 2*TMath::Pi();
1365       if (angle>=TMath::Pi()) angle -= 2*TMath::Pi();
1366       
1367       if (TMath::Abs(angle-t.GetAlpha())>0.001){
1368         Double_t rotation = angle-t.GetAlpha();
1369         t.fRelativeSector= relativesector;
1370         t.Rotate(rotation);     
1371       }
1372       t.PropagateTo(x);
1373       //
1374       t.fCurrentCluster = cl; 
1375       t.fRow = nr;
1376       Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
1377       if ((tpcindex&0x8000)==0) accept =0;
1378       if (accept<3) { 
1379         //if founded cluster is acceptible
1380         if (cl->IsUsed(11)) {  // id cluster is shared inrease uncertainty
1381           t.fErrorY2 += 0.03;
1382           t.fErrorZ2 += 0.03; 
1383           t.fErrorY2 *= 3;
1384           t.fErrorZ2 *= 3; 
1385         }
1386         t.fNFoundable++;
1387         UpdateTrack(&t,accept);
1388         return 1;
1389       }    
1390     }
1391   }
1392   if (fIteration>1) return 0;  // not look for new cluster during refitting
1393   //
1394   UInt_t index=0;
1395   if (TMath::Abs(t.GetSnp())>0.95 || TMath::Abs(x*t.GetC()-t.GetEta())>0.95) return 0;
1396   Double_t  y=t.GetYat(x);
1397   if (TMath::Abs(y)>ymax){
1398     if (y > ymax) {
1399       t.fRelativeSector= (t.fRelativeSector+1) % fN;
1400       if (!t.Rotate(fSectors->GetAlpha())) 
1401         return 0;
1402     } else if (y <-ymax) {
1403       t.fRelativeSector= (t.fRelativeSector-1+fN) % fN;
1404       if (!t.Rotate(-fSectors->GetAlpha())) 
1405         return 0;
1406     }
1407     //return 1;
1408   }
1409   //
1410   if (!t.PropagateTo(x)) {
1411     if (fIteration==0) t.fRemoval = 10;
1412     return 0;
1413   }
1414   y=t.GetY(); 
1415   Double_t z=t.GetZ();
1416   //
1417   const AliTPCRow &krow=GetRow(t.fRelativeSector,nr);
1418   if ( (t.GetSigmaY2()<0) || t.GetSigmaZ2()<0) return 0;
1419   Double_t  roady  =1.;
1420   Double_t  roadz = 1.;
1421   //
1422   if (TMath::Abs(TMath::Abs(y)-ymax)<krow.fDeadZone){
1423     t.fInDead = kTRUE;
1424     t.SetClusterIndex2(nr,-1); 
1425     return 0;
1426   } 
1427   else
1428     {
1429       if (TMath::Abs(z)<(AliTPCReconstructor::GetCtgRange()*x+10) && TMath::Abs(z)<fParam->GetZLength() ) t.fNFoundable++;
1430       else
1431         return 0;
1432     }   
1433   //calculate 
1434   if (krow) {
1435     //    cl = krow.FindNearest2(y+10.,z,roady,roadz,index);    
1436     cl = krow.FindNearest2(y,z,roady,roadz,index);    
1437     if (cl) t.fCurrentClusterIndex1 = krow.GetIndex(index);       
1438   }  
1439   if (cl) {
1440     t.fCurrentCluster = cl; 
1441     t.fRow = nr;
1442     if (fIteration==2&&cl->IsUsed(10)) return 0; 
1443     Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
1444     if (fIteration==2&&cl->IsUsed(11)) {
1445       t.fErrorY2 += 0.03;
1446       t.fErrorZ2 += 0.03; 
1447       t.fErrorY2 *= 3;
1448       t.fErrorZ2 *= 3; 
1449     }
1450     /*    
1451     if (t.fCurrentCluster->IsUsed(10)){
1452       //
1453       //     
1454
1455       t.fNShared++;
1456       if (t.fNShared>0.7*t.GetNumberOfClusters()) {
1457         t.fRemoval =10;
1458         return 0;
1459       }
1460     }
1461     */
1462     if (accept<3) UpdateTrack(&t,accept);
1463
1464   } else {  
1465     if ( fIteration==0 && t.fNFoundable*0.5 > t.GetNumberOfClusters()) t.fRemoval=10;
1466     
1467   }
1468   return 1;
1469 }
1470
1471 Int_t AliTPCtrackerMI::FollowToNextFast(AliTPCseed& t, Int_t nr) {
1472   //-----------------------------------------------------------------
1473   // This function tries to find a track prolongation to next pad row
1474   //-----------------------------------------------------------------
1475   //
1476   Double_t  x= GetXrow(nr), ymax=GetMaxY(nr);
1477   Double_t y,z; 
1478   if (!t.GetProlongation(x,y,z)) {
1479     t.fRemoval = 10;
1480     return 0;
1481   }
1482   //
1483   //
1484   if (TMath::Abs(y)>ymax){
1485     
1486     if (y > ymax) {
1487       t.fRelativeSector= (t.fRelativeSector+1) % fN;
1488       if (!t.Rotate(fSectors->GetAlpha())) 
1489         return 0;
1490     } else if (y <-ymax) {
1491       t.fRelativeSector= (t.fRelativeSector-1+fN) % fN;
1492       if (!t.Rotate(-fSectors->GetAlpha())) 
1493         return 0;
1494     }
1495     if (!t.PropagateTo(x)) {
1496       return 0;
1497     } 
1498     t.GetProlongation(x,y,z);
1499   }
1500   //
1501   // update current shape info every 3 pad-row
1502   if ( (nr%6==0) || t.GetNumberOfClusters()<2 || (t.fCurrentSigmaY2<0.0001) ){
1503     //    t.fCurrentSigmaY = GetSigmaY(&t);
1504     //t.fCurrentSigmaZ = GetSigmaZ(&t);
1505     GetShape(&t,nr);
1506   }
1507   //  
1508   AliTPCclusterMI *cl=0;
1509   UInt_t index=0;
1510   
1511   
1512   //Int_t nr2 = nr;
1513   const AliTPCRow &krow=GetRow(t.fRelativeSector,nr);
1514   if ( (t.GetSigmaY2()<0) || t.GetSigmaZ2()<0) return 0;
1515   Double_t  roady  =1.;
1516   Double_t  roadz = 1.;
1517   //
1518   Int_t row = nr;
1519   if (TMath::Abs(TMath::Abs(y)-ymax)<krow.fDeadZone){
1520     t.fInDead = kTRUE;
1521     t.SetClusterIndex2(row,-1); 
1522     return 0;
1523   } 
1524   else
1525     {
1526       if (TMath::Abs(z)>(AliTPCReconstructor::GetCtgRange()*x+10)) t.SetClusterIndex2(row,-1);
1527     }   
1528   //calculate 
1529   
1530   if ((cl==0)&&(krow)) {
1531     //    cl = krow.FindNearest2(y+10,z,roady,roadz,index);    
1532     cl = krow.FindNearest2(y,z,roady,roadz,index);    
1533
1534     if (cl) t.fCurrentClusterIndex1 = krow.GetIndex(index);       
1535   }  
1536
1537   if (cl) {
1538     t.fCurrentCluster = cl; 
1539     //    Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);        
1540     //if (accept<3){
1541       t.SetClusterIndex2(row,index);
1542       t.fClusterPointer[row] = cl;
1543       //}
1544   }
1545   return 1;
1546 }
1547
1548
1549
1550 Int_t AliTPCtrackerMI::UpdateClusters(AliTPCseed& t,  Int_t nr) {
1551   //-----------------------------------------------------------------
1552   // This function tries to find a track prolongation to next pad row
1553   //-----------------------------------------------------------------
1554   t.fCurrentCluster  = 0;
1555   t.fCurrentClusterIndex1 = 0;   
1556    
1557   Double_t xt=t.GetX();
1558   Int_t     row = GetRowNumber(xt)-1; 
1559   Double_t  ymax= GetMaxY(nr);
1560
1561   if (row < nr) return 1; // don't prolongate if not information until now -
1562   if (TMath::Abs(t.GetSnp())>0.9 && t.GetNumberOfClusters()>40. && fIteration!=2) {
1563     t.fRemoval =10;
1564     return 0;  // not prolongate strongly inclined tracks
1565   } 
1566   if (TMath::Abs(t.GetSnp())>0.95) {
1567     t.fRemoval =10;
1568     return 0;  // not prolongate strongly inclined tracks
1569   }
1570
1571   Double_t x= GetXrow(nr);
1572   Double_t y,z;
1573   //t.PropagateTo(x+0.02);
1574   //t.PropagateTo(x+0.01);
1575   if (!t.PropagateTo(x)){
1576     return 0;
1577   }
1578   //
1579   y=t.GetY();
1580   z=t.GetZ();
1581
1582   if (TMath::Abs(y)>ymax){
1583     if (y > ymax) {
1584       t.fRelativeSector= (t.fRelativeSector+1) % fN;
1585       if (!t.Rotate(fSectors->GetAlpha())) 
1586         return 0;
1587     } else if (y <-ymax) {
1588       t.fRelativeSector= (t.fRelativeSector-1+fN) % fN;
1589       if (!t.Rotate(-fSectors->GetAlpha())) 
1590         return 0;
1591     }
1592     //    if (!t.PropagateTo(x)){
1593     //  return 0;
1594     //}
1595     return 1;
1596     //y = t.GetY();    
1597   }
1598   //
1599
1600   AliTPCRow &krow=GetRow(t.fRelativeSector,nr);
1601
1602   if (TMath::Abs(TMath::Abs(y)-ymax)<krow.fDeadZone){
1603     t.fInDead = kTRUE;
1604     t.SetClusterIndex2(nr,-1); 
1605     return 0;
1606   } 
1607   else
1608     {
1609       if (TMath::Abs(t.GetZ())<(AliTPCReconstructor::GetCtgRange()*t.GetX()+10)) t.fNFoundable++;
1610       else
1611         return 0;      
1612     }
1613
1614   // update current
1615   if ( (nr%6==0) || t.GetNumberOfClusters()<2){
1616     //    t.fCurrentSigmaY = GetSigmaY(&t);
1617     //t.fCurrentSigmaZ = GetSigmaZ(&t);
1618     GetShape(&t,nr);
1619   }
1620     
1621   AliTPCclusterMI *cl=0;
1622   Int_t index=0;
1623   //
1624   Double_t roady = 1.;
1625   Double_t roadz = 1.;
1626   //
1627
1628   if (!cl){
1629     index = t.GetClusterIndex2(nr);    
1630     if ( (index>0) && (index&0x8000)==0){
1631       cl = t.fClusterPointer[nr];
1632       if ( (cl==0) && (index>0)) cl = GetClusterMI(index);
1633       t.fCurrentClusterIndex1 = index;
1634       if (cl) {
1635         t.fCurrentCluster  = cl;
1636         return 1;
1637       }
1638     }
1639   }
1640
1641   //  if (index<0) return 0;
1642   UInt_t uindex = TMath::Abs(index);
1643
1644   if (krow) {    
1645     //cl = krow.FindNearest2(y+10,z,roady,roadz,uindex);      
1646     cl = krow.FindNearest2(y,z,roady,roadz,uindex);      
1647   }
1648
1649   if (cl) t.fCurrentClusterIndex1 = krow.GetIndex(uindex);   
1650   t.fCurrentCluster  = cl;
1651
1652   return 1;
1653 }
1654
1655
1656 Int_t AliTPCtrackerMI::FollowToNextCluster(AliTPCseed & t, Int_t nr) {
1657   //-----------------------------------------------------------------
1658   // This function tries to find a track prolongation to next pad row
1659   //-----------------------------------------------------------------
1660
1661   //update error according neighborhoud
1662
1663   if (t.fCurrentCluster) {
1664     t.fRow = nr; 
1665     Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
1666     
1667     if (t.fCurrentCluster->IsUsed(10)){
1668       //
1669       //
1670       //  t.fErrorZ2*=2;
1671       //  t.fErrorY2*=2;
1672       t.fNShared++;
1673       if (t.fNShared>0.7*t.GetNumberOfClusters()) {
1674         t.fRemoval =10;
1675         return 0;
1676       }
1677     }   
1678     if (fIteration>0) accept = 0;
1679     if (accept<3)  UpdateTrack(&t,accept);  
1680  
1681   } else {
1682     if (fIteration==0){
1683       if ( ( (t.GetSigmaY2()+t.GetSigmaZ2())>0.16)&& t.GetNumberOfClusters()>18) t.fRemoval=10;      
1684       if (  t.GetChi2()/t.GetNumberOfClusters()>6 &&t.GetNumberOfClusters()>18) t.fRemoval=10;      
1685
1686       if (( (t.fNFoundable*0.5 > t.GetNumberOfClusters()) || t.fNoCluster>15)) t.fRemoval=10;
1687     }
1688   }
1689   return 1;
1690 }
1691
1692
1693
1694 //_____________________________________________________________________________
1695 Int_t AliTPCtrackerMI::FollowProlongation(AliTPCseed& t, Int_t rf, Int_t step) {
1696   //-----------------------------------------------------------------
1697   // This function tries to find a track prolongation.
1698   //-----------------------------------------------------------------
1699   Double_t xt=t.GetX();
1700   //
1701   Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
1702   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
1703   if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
1704   //
1705   t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
1706     
1707   Int_t first = GetRowNumber(xt)-1;
1708   for (Int_t nr= first; nr>=rf; nr-=step) {
1709     // update kink info
1710     if (t.GetKinkIndexes()[0]>0){
1711       for (Int_t i=0;i<3;i++){
1712         Int_t index = t.GetKinkIndexes()[i];
1713         if (index==0) break;
1714         if (index<0) continue;
1715         //
1716         AliESDkink * kink = fEvent->GetKink(index-1);
1717         if (!kink){
1718           printf("PROBLEM\n");
1719         }
1720         else{
1721           Int_t kinkrow = kink->GetTPCRow0()+2+Int_t(0.5/(0.05+kink->GetAngle(2)));
1722           if (kinkrow==nr){
1723             AliExternalTrackParam paramd(t);
1724             kink->SetDaughter(paramd);
1725             kink->SetStatus(2,5);
1726             kink->Update();
1727           }
1728         }
1729       }
1730     }
1731
1732     if (nr==80) t.UpdateReference();
1733     if (nr<fInnerSec->GetNRows()) 
1734       fSectors = fInnerSec;
1735     else
1736       fSectors = fOuterSec;
1737     if (FollowToNext(t,nr)==0) 
1738       if (!t.IsActive()) 
1739         return 0;
1740     
1741   }   
1742   return 1;
1743 }
1744
1745
1746 //_____________________________________________________________________________
1747 Int_t AliTPCtrackerMI::FollowProlongationFast(AliTPCseed& t, Int_t rf, Int_t step) {
1748   //-----------------------------------------------------------------
1749   // This function tries to find a track prolongation.
1750   //-----------------------------------------------------------------
1751   Double_t xt=t.GetX();
1752   //
1753   Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
1754   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
1755   if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
1756   t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
1757     
1758   for (Int_t nr=GetRowNumber(xt)-1; nr>=rf; nr-=step) {
1759     
1760     if (FollowToNextFast(t,nr)==0) 
1761       if (!t.IsActive()) return 0;
1762     
1763   }   
1764   return 1;
1765 }
1766
1767
1768
1769
1770
1771 Int_t AliTPCtrackerMI::FollowBackProlongation(AliTPCseed& t, Int_t rf) {
1772   //-----------------------------------------------------------------
1773   // This function tries to find a track prolongation.
1774   //-----------------------------------------------------------------
1775   //
1776   Double_t xt=t.GetX();  
1777   Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
1778   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
1779   if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
1780   t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
1781     
1782   Int_t first = t.fFirstPoint;
1783   if (first<GetRowNumber(xt)+1) first = GetRowNumber(xt)+1;
1784   //
1785   if (first<0) first=0;
1786   for (Int_t nr=first; nr<=rf; nr++) {
1787     if ( (TMath::Abs(t.GetSnp())>0.95)) break;
1788     if (t.GetKinkIndexes()[0]<0){
1789       for (Int_t i=0;i<3;i++){
1790         Int_t index = t.GetKinkIndexes()[i];
1791         if (index==0) break;
1792         if (index>0) continue;
1793         index = TMath::Abs(index);
1794         AliESDkink * kink = fEvent->GetKink(index-1);
1795         if (!kink){
1796           printf("PROBLEM\n");
1797         }
1798         else{
1799           Int_t kinkrow = kink->GetTPCRow0()-2-Int_t(0.5/(0.05+kink->GetAngle(2)));
1800           if (kinkrow==nr){
1801             AliExternalTrackParam paramm(t);
1802             kink->SetMother(paramm);
1803             kink->SetStatus(2,1);
1804             kink->Update();
1805           }
1806         }
1807       }      
1808     }
1809     //
1810     if (nr<fInnerSec->GetNRows()) 
1811       fSectors = fInnerSec;
1812     else
1813       fSectors = fOuterSec;
1814     FollowToNext(t,nr);                                                             
1815   }   
1816   return 1;
1817 }
1818
1819
1820
1821
1822    
1823 Float_t AliTPCtrackerMI::OverlapFactor(AliTPCseed * s1, AliTPCseed * s2, Int_t &sum1, Int_t & sum2)
1824 {
1825   //
1826   //
1827   sum1=0;
1828   sum2=0;
1829   Int_t sum=0;
1830   //
1831   Float_t dz2 =(s1->GetZ() - s2->GetZ());
1832   dz2*=dz2;  
1833
1834   Float_t dy2 =TMath::Abs((s1->GetY() - s2->GetY()));
1835   dy2*=dy2;
1836   Float_t distance = TMath::Sqrt(dz2+dy2);
1837   if (distance>4.) return 0; // if there are far away  - not overlap - to reduce combinatorics
1838  
1839   //  Int_t offset =0;
1840   Int_t firstpoint = TMath::Min(s1->fFirstPoint,s2->fFirstPoint);
1841   Int_t lastpoint = TMath::Max(s1->fLastPoint,s2->fLastPoint);
1842   if (lastpoint>160) 
1843     lastpoint =160;
1844   if (firstpoint<0) 
1845     firstpoint = 0;
1846   if (firstpoint>lastpoint) {
1847     firstpoint =lastpoint;
1848     //    lastpoint  =160;
1849   }
1850     
1851   
1852   for (Int_t i=firstpoint-1;i<lastpoint+1;i++){
1853     if (s1->GetClusterIndex2(i)>0) sum1++;
1854     if (s2->GetClusterIndex2(i)>0) sum2++;
1855     if (s1->GetClusterIndex2(i)==s2->GetClusterIndex2(i) && s1->GetClusterIndex2(i)>0) {
1856       sum++;
1857     }
1858   }
1859   if (sum<5) return 0;
1860
1861   Float_t summin = TMath::Min(sum1+1,sum2+1);
1862   Float_t ratio = (sum+1)/Float_t(summin);
1863   return ratio;
1864 }
1865
1866 void  AliTPCtrackerMI::SignShared(AliTPCseed * s1, AliTPCseed * s2)
1867 {
1868   //
1869   //
1870   if (TMath::Abs(s1->GetC()-s2->GetC())>0.004) return;
1871   if (TMath::Abs(s1->GetTgl()-s2->GetTgl())>0.6) return;
1872
1873   Float_t dz2 =(s1->GetZ() - s2->GetZ());
1874   dz2*=dz2;
1875   Float_t dy2 =(s1->GetY() - s2->GetY());
1876   dy2*=dy2;
1877   Float_t distance = dz2+dy2;
1878   if (distance>325.) return ; // if there are far away  - not overlap - to reduce combinatorics
1879   
1880   //
1881   Int_t sumshared=0;
1882   //
1883   Int_t firstpoint = TMath::Max(s1->fFirstPoint,s2->fFirstPoint);
1884   Int_t lastpoint = TMath::Min(s1->fLastPoint,s2->fLastPoint);
1885   //
1886   if (firstpoint>=lastpoint-5) return;;
1887
1888   for (Int_t i=firstpoint;i<lastpoint;i++){
1889     //    if ( (s1->GetClusterIndex2(i)&0xFFFF8FFF)==(s2->GetClusterIndex2(i)&0xFFFF8FFF) && s1->GetClusterIndex2(i)>0) {
1890     if ( (s1->GetClusterIndex2(i))==(s2->GetClusterIndex2(i)) && s1->GetClusterIndex2(i)>0) {
1891       sumshared++;
1892     }
1893   }
1894   if (sumshared>4){
1895     // sign clusters
1896     //
1897     for (Int_t i=firstpoint;i<lastpoint;i++){
1898       //      if ( (s1->GetClusterIndex2(i)&0xFFFF8FFF)==(s2->GetClusterIndex2(i)&0xFFFF8FFF) && s1->GetClusterIndex2(i)>0) {
1899       if ( (s1->GetClusterIndex2(i))==(s2->GetClusterIndex2(i)) && s1->GetClusterIndex2(i)>0) {
1900         AliTPCTrackerPoint *p1  = s1->GetTrackPoint(i);
1901         AliTPCTrackerPoint *p2  = s2->GetTrackPoint(i);; 
1902         if (s1->IsActive()&&s2->IsActive()){
1903           p1->fIsShared = kTRUE;
1904           p2->fIsShared = kTRUE;
1905         }       
1906       }
1907     }
1908   }
1909   //  
1910   if (sumshared>10){
1911     for (Int_t i=0;i<4;i++){
1912       if (s1->fOverlapLabels[3*i]==0){
1913         s1->fOverlapLabels[3*i] = s2->GetLabel();
1914         s1->fOverlapLabels[3*i+1] = sumshared;
1915         s1->fOverlapLabels[3*i+2] = s2->GetUniqueID();
1916         break;
1917       } 
1918     }
1919     for (Int_t i=0;i<4;i++){
1920       if (s2->fOverlapLabels[3*i]==0){
1921         s2->fOverlapLabels[3*i] = s1->GetLabel();
1922         s2->fOverlapLabels[3*i+1] = sumshared;
1923         s2->fOverlapLabels[3*i+2] = s1->GetUniqueID();
1924         break;
1925       } 
1926     }    
1927   }
1928   
1929 }
1930
1931 void  AliTPCtrackerMI::SignShared(TObjArray * arr)
1932 {
1933   //
1934   //sort trackss according sectors
1935   //  
1936   for (Int_t i=0; i<arr->GetEntriesFast(); i++) {
1937     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1938     if (!pt) continue;
1939     //if (pt) RotateToLocal(pt);
1940     pt->fSort = 0;
1941   }
1942   arr->UnSort();
1943   arr->Sort();  // sorting according z
1944   arr->Expand(arr->GetEntries());
1945   //
1946   //
1947   Int_t nseed=arr->GetEntriesFast();
1948   for (Int_t i=0; i<nseed; i++) {
1949     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1950     if (!pt) continue;
1951     for (Int_t j=0;j<=12;j++){
1952       pt->fOverlapLabels[j] =0;
1953     }
1954   }
1955   for (Int_t i=0; i<nseed; i++) {
1956     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1957     if (!pt) continue;
1958     if (pt->fRemoval>10) continue;
1959     for (Int_t j=i+1; j<nseed; j++){
1960       AliTPCseed *pt2=(AliTPCseed*)arr->UncheckedAt(j);
1961       //      if (pt2){
1962       if (pt2->fRemoval<=10) {
1963         if ( TMath::Abs(pt->fRelativeSector-pt2->fRelativeSector)>0) break;
1964         SignShared(pt,pt2);
1965       }
1966     }  
1967   }
1968 }
1969
1970 void  AliTPCtrackerMI::RemoveDouble(TObjArray * arr, Float_t factor1, Float_t factor2,  Int_t removalindex)
1971 {
1972   //
1973   //sort trackss according sectors
1974   //
1975   if (fDebug&1) {
1976     Info("RemoveDouble","Number of tracks before double removal- %d\n",arr->GetEntries());
1977   }
1978   //
1979   for (Int_t i=0; i<arr->GetEntriesFast(); i++) {
1980     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1981     if (!pt) continue;
1982     pt->fSort = 0;
1983   }
1984   arr->UnSort();
1985   arr->Sort();  // sorting according z
1986   arr->Expand(arr->GetEntries());
1987   //
1988   //reset overlap labels
1989   //
1990   Int_t nseed=arr->GetEntriesFast();
1991   for (Int_t i=0; i<nseed; i++) {
1992     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1993     if (!pt) continue;
1994     pt->SetUniqueID(i);
1995     for (Int_t j=0;j<=12;j++){
1996       pt->fOverlapLabels[j] =0;
1997     }
1998   }
1999   //
2000   //sign shared tracks
2001   for (Int_t i=0; i<nseed; i++) {
2002     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2003     if (!pt) continue;
2004     if (pt->fRemoval>10) continue;
2005     Float_t deltac = pt->GetC()*0.1;
2006     for (Int_t j=i+1; j<nseed; j++){
2007       AliTPCseed *pt2=(AliTPCseed*)arr->UncheckedAt(j);
2008       //      if (pt2){
2009       if (pt2->fRemoval<=10) {
2010         if ( TMath::Abs(pt->fRelativeSector-pt2->fRelativeSector)>0) break;
2011         if (TMath::Abs(pt->GetC()  -pt2->GetC())>deltac) continue;
2012         if (TMath::Abs(pt->GetTgl()-pt2->GetTgl())>0.05) continue;
2013         //
2014         SignShared(pt,pt2);
2015       }
2016     }
2017   }
2018   //
2019   // remove highly shared tracks
2020   for (Int_t i=0; i<nseed; i++) {
2021     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2022     if (!pt) continue;
2023     if (pt->fRemoval>10) continue;
2024     //
2025     Int_t sumshared =0;
2026     for (Int_t j=0;j<4;j++){
2027       sumshared = pt->fOverlapLabels[j*3+1];      
2028     }
2029     Float_t factor = factor1;
2030     if (pt->fRemoval>0) factor = factor2;
2031     if (sumshared/pt->GetNumberOfClusters()>factor){
2032       for (Int_t j=0;j<4;j++){
2033         if (pt->fOverlapLabels[3*j]==0) continue;
2034         if (pt->fOverlapLabels[3*j+1]<5) continue; 
2035         if (pt->fRemoval==removalindex) continue;      
2036         AliTPCseed * pt2 = (AliTPCseed*)arr->UncheckedAt(pt->fOverlapLabels[3*j+2]);
2037         if (!pt2) continue;
2038         if (pt2->GetSigma2C()<pt->GetSigma2C()){
2039           //      pt->fRemoval = removalindex;
2040           delete arr->RemoveAt(i);        
2041           break;
2042         }
2043       }      
2044     }
2045   }
2046   arr->Compress();
2047   if (fDebug&1) {
2048     Info("RemoveDouble","Number of tracks after double removal- %d\n",arr->GetEntries());
2049   }
2050 }
2051
2052
2053
2054
2055
2056
2057 void AliTPCtrackerMI::SortTracks(TObjArray * arr, Int_t mode) const
2058 {
2059   //
2060   //sort tracks in array according mode criteria
2061   Int_t nseed = arr->GetEntriesFast();    
2062   for (Int_t i=0; i<nseed; i++) {
2063     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2064     if (!pt) {
2065       continue;
2066     }
2067     pt->fSort = mode;
2068   }
2069   arr->UnSort();
2070   arr->Sort();
2071 }
2072
2073 void AliTPCtrackerMI::RemoveUsed(TObjArray * arr, Float_t factor1,  Float_t factor2, Int_t removalindex)
2074 {
2075
2076   //Loop over all tracks and remove "overlaps"
2077   //
2078   //
2079   Int_t nseed = arr->GetEntriesFast();  
2080   Int_t good =0;
2081
2082   for (Int_t i=0; i<nseed; i++) {
2083     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2084     if (!pt) {
2085       delete arr->RemoveAt(i);
2086     }
2087     else{
2088       pt->fSort =1;
2089       pt->fBSigned = kFALSE;
2090     }
2091   }
2092   arr->Compress();
2093   nseed = arr->GetEntriesFast();
2094   arr->UnSort();
2095   arr->Sort();
2096   //
2097   //unsign used
2098   UnsignClusters();
2099   //
2100   for (Int_t i=0; i<nseed; i++) {
2101     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2102     if (!pt) {
2103       continue;
2104     }    
2105     Int_t found,foundable,shared;
2106     if (pt->IsActive()) 
2107       pt->GetClusterStatistic(0,160,found, foundable,shared,kFALSE);
2108     else
2109       pt->GetClusterStatistic(0,160,found, foundable,shared,kTRUE); 
2110     //
2111     Double_t factor = factor2;
2112     if (pt->fBConstrain) factor = factor1;
2113
2114     if ((Float_t(shared)/Float_t(found))>factor){
2115       pt->Desactivate(removalindex);
2116       continue;
2117     }
2118
2119     good++;
2120     for (Int_t i=0; i<160; i++) {
2121       Int_t index=pt->GetClusterIndex2(i);
2122       if (index<0 || index&0x8000 ) continue;
2123       AliTPCclusterMI *c= pt->fClusterPointer[i];        
2124       if (!c) continue;
2125       //      if (!c->IsUsed(10)) c->Use(10);
2126       //if (pt->IsActive()) 
2127       c->Use(10);  
2128       //else
2129       //        c->Use(5);
2130     }
2131     
2132   }
2133   fNtracks = good;
2134   if (fDebug>0){
2135     Info("RemoveUsed","\n*****\nNumber of good tracks after shared removal\t%d\n",fNtracks);
2136   }
2137 }
2138
2139
2140 void AliTPCtrackerMI::RemoveUsed2(TObjArray * arr, Float_t factor1,  Float_t factor2, Int_t minimal)
2141 {
2142
2143   //Loop over all tracks and remove "overlaps"
2144   //
2145   //
2146   UnsignClusters();
2147   //
2148   Int_t nseed = arr->GetEntriesFast();  
2149   Float_t * quality = new Float_t[nseed];
2150   Int_t   * indexes = new Int_t[nseed];
2151   Int_t good =0;
2152   //
2153   //
2154   for (Int_t i=0; i<nseed; i++) {
2155     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2156     if (!pt){
2157       quality[i]=-1;
2158       continue;
2159     }
2160     pt->UpdatePoints();    //select first last max dens points
2161     Float_t * points = pt->GetPoints();
2162     if (points[3]<0.8) quality[i] =-1;
2163     //
2164     quality[i] = (points[2]-points[0])+pt->GetNumberOfClusters();
2165   }
2166   TMath::Sort(nseed,quality,indexes);
2167   //
2168   //
2169   for (Int_t itrack=0; itrack<nseed; itrack++) {
2170     Int_t trackindex = indexes[itrack];
2171     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(trackindex);    
2172     if (quality[trackindex]<0){
2173       if (pt) {
2174         delete arr->RemoveAt(trackindex);
2175       }
2176       else{
2177         arr->RemoveAt(trackindex);
2178       }
2179       continue;
2180     }
2181     //
2182     Int_t first = Int_t(pt->GetPoints()[0]);
2183     Int_t last  = Int_t(pt->GetPoints()[2]);
2184     Double_t factor = (pt->fBConstrain) ? factor1: factor2;
2185     //
2186     Int_t found,foundable,shared;
2187     pt->GetClusterStatistic(first,last, found, foundable,shared,kFALSE);
2188     Float_t sharedfactor = Float_t(shared+1)/Float_t(found+1);
2189     //
2190     if (Float_t(shared+1)/Float_t(found+1)>factor){
2191       if (pt->GetKinkIndexes()[0]!=0) continue;  //don't remove tracks  - part of the kinks
2192       delete arr->RemoveAt(trackindex);
2193       continue;
2194     }
2195
2196     if (pt->GetNumberOfClusters()<50&&(found-0.5*shared)<minimal){  //remove short tracks
2197       if (pt->GetKinkIndexes()[0]!=0) continue;  //don't remove tracks  - part of the kinks
2198       delete arr->RemoveAt(trackindex);
2199       continue;
2200     }
2201
2202     good++;
2203     if (sharedfactor>0.4) continue;
2204     for (Int_t i=first; i<last; i++) {
2205       Int_t index=pt->GetClusterIndex2(i);
2206       // if (index<0 || index&0x8000 ) continue;
2207       if (index<0 || index&0x8000 ) continue;
2208       AliTPCclusterMI *c= pt->fClusterPointer[i];        
2209       if (!c) continue;
2210       c->Use(10);  
2211     }    
2212   }
2213   fNtracks = good;
2214   if (fDebug>0){
2215     Info("RemoveUsed","\n*****\nNumber of good tracks after shared removal\t%d\n",fNtracks);
2216   }
2217   delete []quality;
2218   delete []indexes;
2219 }
2220
2221 void AliTPCtrackerMI::UnsignClusters() 
2222 {
2223   //
2224   // loop over all clusters and unsign them
2225   //
2226   
2227   for (Int_t sec=0;sec<fkNIS;sec++){
2228     for (Int_t row=0;row<fInnerSec->GetNRows();row++){
2229       AliTPCclusterMI *cl = fInnerSec[sec][row].fClusters1;
2230       for (Int_t icl =0;icl< fInnerSec[sec][row].fN1;icl++)
2231         //      if (cl[icl].IsUsed(10))         
2232         cl[icl].Use(-1);
2233       cl = fInnerSec[sec][row].fClusters2;
2234       for (Int_t icl =0;icl< fInnerSec[sec][row].fN2;icl++)
2235         //if (cl[icl].IsUsed(10))       
2236           cl[icl].Use(-1);      
2237     }
2238   }
2239   
2240   for (Int_t sec=0;sec<fkNOS;sec++){
2241     for (Int_t row=0;row<fOuterSec->GetNRows();row++){
2242       AliTPCclusterMI *cl = fOuterSec[sec][row].fClusters1;
2243       for (Int_t icl =0;icl< fOuterSec[sec][row].fN1;icl++)
2244         //if (cl[icl].IsUsed(10))       
2245           cl[icl].Use(-1);
2246       cl = fOuterSec[sec][row].fClusters2;
2247       for (Int_t icl =0;icl< fOuterSec[sec][row].fN2;icl++)
2248         //if (cl[icl].IsUsed(10))       
2249         cl[icl].Use(-1);      
2250     }
2251   }
2252   
2253 }
2254
2255
2256
2257 void AliTPCtrackerMI::SignClusters(TObjArray * arr, Float_t fnumber, Float_t fdensity)
2258 {
2259   //
2260   //sign clusters to be "used"
2261   //
2262   // snumber and sdensity sign number of sigmas - bellow mean value to be accepted
2263   // loop over "primaries"
2264   
2265   Float_t sumdens=0;
2266   Float_t sumdens2=0;
2267   Float_t sumn   =0;
2268   Float_t sumn2  =0;
2269   Float_t sumchi =0;
2270   Float_t sumchi2 =0;
2271
2272   Float_t sum    =0;
2273
2274   TStopwatch timer;
2275   timer.Start();
2276
2277   Int_t nseed = arr->GetEntriesFast();
2278   for (Int_t i=0; i<nseed; i++) {
2279     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2280     if (!pt) {
2281       continue;
2282     }    
2283     if (!(pt->IsActive())) continue;
2284     Float_t dens = pt->GetNumberOfClusters()/Float_t(pt->fNFoundable);
2285     if ( (dens>0.7) && (pt->GetNumberOfClusters()>70)){
2286       sumdens += dens;
2287       sumdens2+= dens*dens;
2288       sumn    += pt->GetNumberOfClusters();
2289       sumn2   += pt->GetNumberOfClusters()*pt->GetNumberOfClusters();
2290       Float_t chi2 = pt->GetChi2()/pt->GetNumberOfClusters();
2291       if (chi2>5) chi2=5;
2292       sumchi  +=chi2;
2293       sumchi2 +=chi2*chi2;
2294       sum++;
2295     }
2296   }
2297
2298   Float_t mdensity = 0.9;
2299   Float_t meann    = 130;
2300   Float_t meanchi  = 1;
2301   Float_t sdensity = 0.1;
2302   Float_t smeann    = 10;
2303   Float_t smeanchi  =0.4;
2304   
2305
2306   if (sum>20){
2307     mdensity = sumdens/sum;
2308     meann    = sumn/sum;
2309     meanchi  = sumchi/sum;
2310     //
2311     sdensity = sumdens2/sum-mdensity*mdensity;
2312     sdensity = TMath::Sqrt(sdensity);
2313     //
2314     smeann   = sumn2/sum-meann*meann;
2315     smeann   = TMath::Sqrt(smeann);
2316     //
2317     smeanchi = sumchi2/sum - meanchi*meanchi;
2318     smeanchi = TMath::Sqrt(smeanchi);
2319   }
2320
2321
2322   //REMOVE  SHORT DELTAS or tracks going out of sensitive volume of TPC
2323   //
2324   for (Int_t i=0; i<nseed; i++) {
2325     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2326     if (!pt) {
2327       continue;
2328     }
2329     if (pt->fBSigned) continue;
2330     if (pt->fBConstrain) continue;    
2331     //if (!(pt->IsActive())) continue;
2332     /*
2333     Int_t found,foundable,shared;    
2334     pt->GetClusterStatistic(0,160,found, foundable,shared);
2335     if (shared/float(found)>0.3) {
2336       if (shared/float(found)>0.9 ){
2337         //delete arr->RemoveAt(i);
2338       }
2339       continue;
2340     }
2341     */
2342     Bool_t isok =kFALSE;
2343     if ( (pt->fNShared/pt->GetNumberOfClusters()<0.5) &&pt->GetNumberOfClusters()>60)
2344       isok = kTRUE;
2345     if ((TMath::Abs(1/pt->GetC())<100.) && (pt->fNShared/pt->GetNumberOfClusters()<0.7))
2346       isok =kTRUE;
2347     if  (TMath::Abs(pt->GetZ()/pt->GetX())>1.1)
2348       isok =kTRUE;
2349     if ( (TMath::Abs(pt->GetSnp()>0.7) && pt->GetD(0,0)>60.))
2350       isok =kTRUE;
2351     
2352     if (isok)     
2353       for (Int_t i=0; i<160; i++) {     
2354         Int_t index=pt->GetClusterIndex2(i);
2355         if (index<0) continue;
2356         AliTPCclusterMI *c= pt->fClusterPointer[i];
2357         if (!c) continue;
2358         //if (!(c->IsUsed(10))) c->Use();  
2359         c->Use(10);  
2360       }
2361   }
2362   
2363   
2364   //
2365   Double_t maxchi  = meanchi+2.*smeanchi;
2366
2367   for (Int_t i=0; i<nseed; i++) {
2368     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2369     if (!pt) {
2370       continue;
2371     }    
2372     //if (!(pt->IsActive())) continue;
2373     if (pt->fBSigned) continue;
2374     Double_t chi     = pt->GetChi2()/pt->GetNumberOfClusters();
2375     if (chi>maxchi) continue;
2376
2377     Float_t bfactor=1;
2378     Float_t dens = pt->GetNumberOfClusters()/Float_t(pt->fNFoundable);
2379    
2380     //sign only tracks with enoug big density at the beginning
2381     
2382     if ((pt->GetDensityFirst(40)<0.75) && pt->GetNumberOfClusters()<meann) continue; 
2383     
2384     
2385     Double_t mindens = TMath::Max(double(mdensity-sdensity*fdensity*bfactor),0.65);
2386     Double_t minn    = TMath::Max(Int_t(meann-fnumber*smeann*bfactor),50);
2387    
2388     //    if (pt->fBConstrain) mindens = TMath::Max(mdensity-sdensity*fdensity*bfactor,0.65);
2389     if ( (pt->fRemoval==10) && (pt->GetSnp()>0.8)&&(dens>mindens))
2390       minn=0;
2391
2392     if ((dens>mindens && pt->GetNumberOfClusters()>minn) && chi<maxchi ){
2393       //Int_t noc=pt->GetNumberOfClusters();
2394       pt->fBSigned = kTRUE;
2395       for (Int_t i=0; i<160; i++) {
2396
2397         Int_t index=pt->GetClusterIndex2(i);
2398         if (index<0) continue;
2399         AliTPCclusterMI *c= pt->fClusterPointer[i];
2400         if (!c) continue;
2401         //      if (!(c->IsUsed(10))) c->Use();  
2402         c->Use(10);  
2403       }
2404     }
2405   }
2406   //  gLastCheck = nseed;
2407   //  arr->Compress();
2408   if (fDebug>0){
2409     timer.Print();
2410   }
2411 }
2412
2413
2414 void  AliTPCtrackerMI::StopNotActive(TObjArray * arr, Int_t row0, Float_t th0, Float_t th1, Float_t th2) const
2415 {
2416   // stop not active tracks
2417   // take th1 as threshold for number of founded to number of foundable on last 10 active rows
2418   // take th2 as threshold for number of founded to number of foundable on last 20 active rows 
2419   Int_t nseed = arr->GetEntriesFast();  
2420   //
2421   for (Int_t i=0; i<nseed; i++) {
2422     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2423     if (!pt) {
2424       continue;
2425     }
2426     if (!(pt->IsActive())) continue;
2427     StopNotActive(pt,row0,th0, th1,th2);
2428   }
2429 }
2430
2431
2432
2433 void  AliTPCtrackerMI::StopNotActive(AliTPCseed * seed, Int_t row0, Float_t th0, Float_t th1,
2434  Float_t th2) const
2435 {
2436   // stop not active tracks
2437   // take th1 as threshold for number of founded to number of foundable on last 10 active rows
2438   // take th2 as threshold for number of founded to number of foundable on last 20 active rows 
2439   Int_t sumgood1  = 0;
2440   Int_t sumgood2  = 0;
2441   Int_t foundable = 0;
2442   Int_t maxindex = seed->fLastPoint;  //last foundable row
2443   if (seed->fNFoundable*th0 > seed->GetNumberOfClusters()) {
2444     seed->Desactivate(10) ;
2445     return;
2446   }
2447
2448   for (Int_t i=row0; i<maxindex; i++){
2449     Int_t index = seed->GetClusterIndex2(i);
2450     if (index!=-1) foundable++;
2451     //if (!c) continue;
2452     if (foundable<=30) sumgood1++;
2453     if (foundable<=50) {
2454       sumgood2++;
2455     }
2456     else{ 
2457       break;
2458     }        
2459   }
2460   if (foundable>=30.){ 
2461      if (sumgood1<(th1*30.)) seed->Desactivate(10);
2462   }
2463   if (foundable>=50)
2464     if (sumgood2<(th2*50.)) seed->Desactivate(10);
2465 }
2466
2467
2468 Int_t AliTPCtrackerMI::RefitInward(AliESD *event)
2469 {
2470   //
2471   // back propagation of ESD tracks
2472   //
2473   //return 0;
2474   fEvent = event;
2475   ReadSeeds(event,2);
2476   fIteration=2;
2477   //PrepareForProlongation(fSeeds,1);
2478   PropagateForward2(fSeeds);
2479
2480   Int_t ntracks=0;
2481   Int_t nseed = fSeeds->GetEntriesFast();
2482   for (Int_t i=0;i<nseed;i++){
2483     AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
2484     if (!seed) continue;
2485     if (seed->GetKinkIndex(0)>0)  UpdateKinkQualityD(seed);  // update quality informations for kinks
2486
2487     seed->PropagateTo(fParam->GetInnerRadiusLow());
2488     seed->UpdatePoints();
2489     AliESDtrack *esd=event->GetTrack(i);
2490     seed->CookdEdx(0.02,0.6);
2491     CookLabel(seed,0.1); //For comparison only
2492     //
2493     if (0 && seed!=0&&esd!=0) {
2494       TTreeSRedirector &cstream = *fDebugStreamer;
2495       cstream<<"Crefit"<<
2496         "Esd.="<<esd<<
2497         "Track.="<<seed<<
2498         "\n"; 
2499     }
2500     if (seed->GetNumberOfClusters()>15){
2501       esd->UpdateTrackParams(seed,AliESDtrack::kTPCrefit); 
2502       esd->SetTPCPoints(seed->GetPoints());
2503       ntracks++;
2504     }
2505     else{
2506       //printf("problem\n");
2507     }
2508   }
2509   //FindKinks(fSeeds,event);
2510   Info("RefitInward","Number of refitted tracks %d",ntracks);
2511   fEvent =0;
2512   //WriteTracks();
2513   return 0;
2514 }
2515
2516
2517 Int_t AliTPCtrackerMI::PropagateBack(AliESD *event)
2518 {
2519   //
2520   // back propagation of ESD tracks
2521   //
2522
2523   fEvent = event;
2524   fIteration = 1;
2525   ReadSeeds(event,0);
2526   PropagateBack(fSeeds);
2527   Int_t nseed = fSeeds->GetEntriesFast();
2528   Int_t ntracks=0;
2529   for (Int_t i=0;i<nseed;i++){
2530     AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
2531     if (!seed) continue;
2532     if (seed->GetKinkIndex(0)<0)  UpdateKinkQualityM(seed);  // update quality informations for kinks
2533     seed->UpdatePoints();
2534     AliESDtrack *esd=event->GetTrack(i);
2535     seed->CookdEdx(0.02,0.6);
2536     CookLabel(seed,0.1); //For comparison only
2537     if (seed->GetNumberOfClusters()>15){
2538       esd->UpdateTrackParams(seed,AliESDtrack::kTPCout);
2539       esd->SetTPCPoints(seed->GetPoints());
2540       ntracks++;
2541     }
2542   }
2543   //FindKinks(fSeeds,event);
2544   Info("PropagateBack","Number of back propagated tracks %d",ntracks);
2545   fEvent =0;
2546   //WriteTracks();
2547   return 0;
2548 }
2549
2550
2551 void AliTPCtrackerMI::DeleteSeeds()
2552 {
2553   //
2554   //delete Seeds
2555   Int_t nseed = fSeeds->GetEntriesFast();
2556   for (Int_t i=0;i<nseed;i++){
2557     AliTPCseed * seed = (AliTPCseed*)fSeeds->At(i);
2558     if (seed) delete fSeeds->RemoveAt(i);
2559   }
2560   delete fSeeds;
2561   fSeeds =0;
2562 }
2563
2564 void AliTPCtrackerMI::ReadSeeds(AliESD *event, Int_t direction)
2565 {
2566   //
2567   //read seeds from the event
2568   
2569   Int_t nentr=event->GetNumberOfTracks();
2570   if (fDebug>0){
2571     Info("ReadSeeds", "Number of ESD tracks: %d\n", nentr);
2572   }
2573   if (fSeeds) 
2574     DeleteSeeds();
2575   if (!fSeeds){   
2576     fSeeds = new TObjArray(nentr);
2577   }
2578   UnsignClusters();
2579   //  Int_t ntrk=0;  
2580   for (Int_t i=0; i<nentr; i++) {
2581     AliESDtrack *esd=event->GetTrack(i);
2582     ULong_t status=esd->GetStatus();
2583     if (!(status&AliESDtrack::kTPCin)) continue;
2584     AliTPCtrack t(*esd);
2585     //    AliTPCseed *seed = new AliTPCseed(t,t.GetAlpha());
2586     AliTPCseed *seed = new AliTPCseed(t/*,t.GetAlpha()*/);
2587     for (Int_t ikink=0;ikink<3;ikink++) {
2588       Int_t index = esd->GetKinkIndex(ikink);
2589       seed->GetKinkIndexes()[ikink] = index;
2590       if (index==0) continue;
2591       index = TMath::Abs(index);
2592       AliESDkink * kink = fEvent->GetKink(index-1);
2593       if (kink&&esd->GetKinkIndex(ikink)<0){
2594         if ((status & AliESDtrack::kTRDrefit) != 0) kink->SetStatus(1,2);
2595         if ((status & AliESDtrack::kITSout) != 0)   kink->SetStatus(1,0);
2596       }
2597       if (kink&&esd->GetKinkIndex(ikink)>0){
2598         if ((status & AliESDtrack::kTRDrefit) != 0) kink->SetStatus(1,6);
2599         if ((status & AliESDtrack::kITSout) != 0)   kink->SetStatus(1,4);
2600       }
2601
2602     }
2603     if ((status==AliESDtrack::kTPCin)&&(direction==1)) seed->ResetCovariance(); 
2604     if ( direction ==2 &&(status & AliESDtrack::kTRDrefit) == 0 ) seed->ResetCovariance();
2605     if ( direction ==2 && ((status & AliESDtrack::kTPCout) == 0) ) {
2606       fSeeds->AddAt(0,i);
2607       delete seed;
2608       continue;    
2609     }
2610     if ( direction ==2 &&(status & AliESDtrack::kTRDrefit) > 0 )  {
2611       Double_t par0[5],par1[5],x;
2612       esd->GetInnerExternalParameters(x,par0);
2613       esd->GetExternalParameters(x,par1);
2614       Double_t delta1 = TMath::Abs(par0[4]-par1[4])/(0.000000001+TMath::Abs(par0[4]+par1[4]));
2615       Double_t delta2 = TMath::Abs(par0[3]-par1[3]);
2616       Double_t trdchi2=0;
2617       if (esd->GetTRDncls()>0) trdchi2 = esd->GetTRDchi2()/esd->GetTRDncls();
2618       //reset covariance if suspicious 
2619       if ( (delta1>0.1) || (delta2>0.006) ||trdchi2>7.)
2620         seed->ResetCovariance();
2621     }
2622
2623     //
2624     //
2625     // rotate to the local coordinate system
2626     //   
2627     fSectors=fInnerSec; fN=fkNIS;    
2628     Double_t alpha=seed->GetAlpha() - fSectors->GetAlphaShift();
2629     if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
2630     if (alpha < 0.            ) alpha += 2.*TMath::Pi();
2631     Int_t ns=Int_t(alpha/fSectors->GetAlpha())%fN;
2632     alpha =ns*fSectors->GetAlpha() + fSectors->GetAlphaShift();
2633     if (alpha<-TMath::Pi()) alpha += 2*TMath::Pi();
2634     if (alpha>=TMath::Pi()) alpha -= 2*TMath::Pi();
2635     alpha-=seed->GetAlpha();  
2636     if (!seed->Rotate(alpha)) {
2637       delete seed;
2638       continue;
2639     }
2640     seed->fEsd = esd;
2641     // sign clusters
2642     for (Int_t irow=0;irow<160;irow++){
2643       Int_t index = seed->GetClusterIndex2(irow);    
2644       if (index>0){ 
2645         //
2646         AliTPCclusterMI * cl = GetClusterMI(index);
2647         seed->fClusterPointer[irow] = cl;
2648         if (cl){
2649           if ((index & 0x8000)==0){
2650             cl->Use(10);  // accepted cluster     
2651           }else{
2652             cl->Use(6);   // close cluster not accepted
2653           }     
2654         }else{
2655            Info("ReadSeeds","Not found cluster");
2656         }
2657       }
2658     }
2659     fSeeds->AddAt(seed,i);
2660   }
2661 }
2662
2663
2664
2665 //_____________________________________________________________________________
2666 void AliTPCtrackerMI::MakeSeeds3(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,  Float_t cuts[4],
2667                                  Float_t deltay, Int_t ddsec) {
2668   //-----------------------------------------------------------------
2669   // This function creates track seeds.
2670   // SEEDING WITH VERTEX CONSTRAIN 
2671   //-----------------------------------------------------------------
2672   // cuts[0]   - fP4 cut
2673   // cuts[1]   - tan(phi)  cut
2674   // cuts[2]   - zvertex cut
2675   // cuts[3]   - fP3 cut
2676   Int_t nin0  = 0;
2677   Int_t nin1  = 0;
2678   Int_t nin2  = 0;
2679   Int_t nin   = 0;
2680   Int_t nout1 = 0;
2681   Int_t nout2 = 0;
2682
2683   Double_t x[5], c[15];
2684   //  Int_t di = i1-i2;
2685   //
2686   AliTPCseed * seed = new AliTPCseed;
2687   Double_t alpha=fSectors->GetAlpha(), shift=fSectors->GetAlphaShift();
2688   Double_t cs=cos(alpha), sn=sin(alpha);
2689   //
2690   //  Double_t x1 =fOuterSec->GetX(i1);
2691   //Double_t xx2=fOuterSec->GetX(i2);
2692   
2693   Double_t x1 =GetXrow(i1);
2694   Double_t xx2=GetXrow(i2);
2695
2696   Double_t x3=GetX(), y3=GetY(), z3=GetZ();
2697
2698   Int_t imiddle = (i2+i1)/2;    //middle pad row index
2699   Double_t xm = GetXrow(imiddle); // radius of middle pad-row
2700   const AliTPCRow& krm=GetRow(sec,imiddle); //middle pad -row
2701   //
2702   Int_t ns =sec;   
2703
2704   const AliTPCRow& kr1=GetRow(ns,i1);
2705   Double_t ymax  = GetMaxY(i1)-kr1.fDeadZone-1.5;  
2706   Double_t ymaxm = GetMaxY(imiddle)-kr1.fDeadZone-1.5;  
2707
2708   //
2709   // change cut on curvature if it can't reach this layer
2710   // maximal curvature set to reach it
2711   Double_t dvertexmax  = TMath::Sqrt((x1-x3)*(x1-x3)+(ymax+5-y3)*(ymax+5-y3));
2712   if (dvertexmax*0.5*cuts[0]>0.85){
2713     cuts[0] = 0.85/(dvertexmax*0.5+1.);
2714   }
2715   Double_t r2min = 1/(cuts[0]*cuts[0]);  //minimal square of radius given by cut
2716
2717   //  Int_t ddsec = 1;
2718   if (deltay>0) ddsec = 0; 
2719   // loop over clusters  
2720   for (Int_t is=0; is < kr1; is++) {
2721     //
2722     if (kr1[is]->IsUsed(10)) continue;
2723     Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();    
2724     //if (TMath::Abs(y1)>ymax) continue;
2725
2726     if (deltay>0 && TMath::Abs(ymax-TMath::Abs(y1))> deltay ) continue;  // seed only at the edge
2727
2728     // find possible directions    
2729     Float_t anglez = (z1-z3)/(x1-x3); 
2730     Float_t extraz = z1 - anglez*(x1-xx2);  // extrapolated z      
2731     //
2732     //
2733     //find   rotation angles relative to line given by vertex and point 1
2734     Double_t dvertex2 = (x1-x3)*(x1-x3)+(y1-y3)*(y1-y3);
2735     Double_t dvertex  = TMath::Sqrt(dvertex2);
2736     Double_t angle13  = TMath::ATan((y1-y3)/(x1-x3));
2737     Double_t cs13     = cos(-angle13), sn13 = sin(-angle13);            
2738     
2739     //
2740     // loop over 2 sectors
2741     Int_t dsec1=-ddsec;
2742     Int_t dsec2= ddsec;
2743     if (y1<0)  dsec2= 0;
2744     if (y1>0)  dsec1= 0;
2745     
2746     Double_t dddz1=0;  // direction of delta inclination in z axis
2747     Double_t dddz2=0;
2748     if ( (z1-z3)>0)
2749       dddz1 =1;    
2750     else
2751       dddz2 =1;
2752     //
2753     for (Int_t dsec = dsec1; dsec<=dsec2;dsec++){
2754       Int_t sec2 = sec + dsec;
2755       // 
2756       //      AliTPCRow&  kr2  = fOuterSec[(sec2+fkNOS)%fkNOS][i2];
2757       //AliTPCRow&  kr2m = fOuterSec[(sec2+fkNOS)%fkNOS][imiddle];
2758       AliTPCRow&  kr2  = GetRow((sec2+fkNOS)%fkNOS,i2);
2759       AliTPCRow&  kr2m = GetRow((sec2+fkNOS)%fkNOS,imiddle);
2760       Int_t  index1 = TMath::Max(kr2.Find(extraz-0.6-dddz1*TMath::Abs(z1)*0.05)-1,0);
2761       Int_t  index2 = TMath::Min(kr2.Find(extraz+0.6+dddz2*TMath::Abs(z1)*0.05)+1,kr2);
2762
2763       // rotation angles to p1-p3
2764       Double_t cs13r     = cos(-angle13+dsec*alpha)/dvertex, sn13r = sin(-angle13+dsec*alpha)/dvertex;            
2765       Double_t x2,   y2,   z2; 
2766       //
2767       //      Double_t dymax = maxangle*TMath::Abs(x1-xx2);
2768
2769       //
2770       Double_t dxx0 =  (xx2-x3)*cs13r;
2771       Double_t dyy0 =  (xx2-x3)*sn13r;
2772       for (Int_t js=index1; js < index2; js++) {
2773         const AliTPCclusterMI *kcl = kr2[js];
2774         if (kcl->IsUsed(10)) continue;  
2775         //
2776         //calcutate parameters
2777         //      
2778         Double_t yy0 =  dyy0 +(kcl->GetY()-y3)*cs13r;
2779         // stright track
2780         if (TMath::Abs(yy0)<0.000001) continue;
2781         Double_t xx0 =  dxx0 -(kcl->GetY()-y3)*sn13r;
2782         Double_t y0  =  0.5*(xx0*xx0+yy0*yy0-xx0)/yy0;
2783         Double_t r02 = (0.25+y0*y0)*dvertex2;   
2784         //curvature (radius) cut
2785         if (r02<r2min) continue;                
2786        
2787         nin0++; 
2788         //
2789         Double_t c0  = 1/TMath::Sqrt(r02);
2790         if (yy0>0) c0*=-1.;     
2791                
2792        
2793         //Double_t dfi0   = 2.*TMath::ASin(dvertex*c0*0.5);
2794         //Double_t dfi1   = 2.*TMath::ASin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);
2795         Double_t dfi0   = 2.*AliTPCFastMath::FastAsin(dvertex*c0*0.5);
2796         Double_t dfi1   = 2.*AliTPCFastMath::FastAsin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);  
2797         //
2798         //
2799         Double_t z0  =  kcl->GetZ();  
2800         Double_t zzzz2    = z1-(z1-z3)*dfi1/dfi0;
2801         if (TMath::Abs(zzzz2-z0)>0.5) continue;       
2802         nin1++;              
2803         //      
2804         Double_t dip    = (z1-z0)*c0/dfi1;        
2805         Double_t x0 = (0.5*cs13+y0*sn13)*dvertex*c0;
2806         //
2807         y2 = kcl->GetY(); 
2808         if (dsec==0){
2809           x2 = xx2; 
2810           z2 = kcl->GetZ();       
2811         }
2812         else
2813           {
2814             // rotation 
2815             z2 = kcl->GetZ();  
2816             x2= xx2*cs-y2*sn*dsec;
2817             y2=+xx2*sn*dsec+y2*cs;
2818           }
2819         
2820         x[0] = y1;
2821         x[1] = z1;
2822         x[2] = x0;
2823         x[3] = dip;
2824         x[4] = c0;
2825         //
2826         //
2827         // do we have cluster at the middle ?
2828         Double_t ym,zm;
2829         GetProlongation(x1,xm,x,ym,zm);
2830         UInt_t dummy; 
2831         AliTPCclusterMI * cm=0;
2832         if (TMath::Abs(ym)-ymaxm<0){      
2833           cm = krm.FindNearest2(ym,zm,1.0,0.6,dummy);
2834           if ((!cm) || (cm->IsUsed(10))) {        
2835             continue;
2836           }
2837         }
2838         else{     
2839           // rotate y1 to system 0
2840           // get state vector in rotated system 
2841           Double_t yr1  = (-0.5*sn13+y0*cs13)*dvertex*c0;
2842           Double_t xr2  =  x0*cs+yr1*sn*dsec;
2843           Double_t xr[5]={kcl->GetY(),kcl->GetZ(), xr2, dip, c0};
2844           //
2845           GetProlongation(xx2,xm,xr,ym,zm);
2846           if (TMath::Abs(ym)-ymaxm<0){
2847             cm = kr2m.FindNearest2(ym,zm,1.0,0.6,dummy);
2848             if ((!cm) || (cm->IsUsed(10))) {      
2849               continue;
2850             }
2851           }
2852         }
2853        
2854
2855         Double_t dym = 0;
2856         Double_t dzm = 0;
2857         if (cm){
2858           dym = ym - cm->GetY();
2859           dzm = zm - cm->GetZ();
2860         }
2861         nin2++;
2862
2863
2864         //
2865         //
2866         Double_t sy1=kr1[is]->GetSigmaY2()*2., sz1=kr1[is]->GetSigmaZ2()*2.;
2867         Double_t sy2=kcl->GetSigmaY2()*2.,     sz2=kcl->GetSigmaZ2()*2.;
2868         //Double_t sy3=400*3./12., sy=0.1, sz=0.1;
2869         Double_t sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
2870         //Double_t sy3=25000*x[4]*x[4]*60+0.5, sy=0.1, sz=0.1;
2871
2872         Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
2873         Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
2874         Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
2875         Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
2876         Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
2877         Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
2878         
2879         Double_t f30=(F3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
2880         Double_t f31=(F3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
2881         Double_t f32=(F3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
2882         Double_t f34=(F3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
2883         
2884         c[0]=sy1;
2885         c[1]=0.;       c[2]=sz1;
2886         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
2887         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
2888                        c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
2889         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
2890         c[13]=f30*sy1*f40+f32*sy2*f42;
2891         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
2892         
2893         //      if (!BuildSeed(kr1[is],kcl,0,x1,x2,x3,x,c)) continue;
2894         
2895         UInt_t index=kr1.GetIndex(is);
2896         AliTPCseed *track=new(seed) AliTPCseed(index, x, c, x1, ns*alpha+shift);
2897         
2898         track->fIsSeeding = kTRUE;
2899         track->fSeed1 = i1;
2900         track->fSeed2 = i2;
2901         track->fSeedType=3;
2902
2903        
2904         //if (dsec==0) {
2905           FollowProlongation(*track, (i1+i2)/2,1);
2906           Int_t foundable,found,shared;
2907           track->GetClusterStatistic((i1+i2)/2,i1, found, foundable, shared, kTRUE);
2908           if ((found<0.55*foundable)  || shared>0.5*found || (track->GetSigmaY2()+track->GetSigmaZ2())>0.5){
2909             seed->Reset();
2910             seed->~AliTPCseed();
2911             continue;
2912           }
2913           //}
2914         
2915         nin++;
2916         FollowProlongation(*track, i2,1);
2917         
2918         
2919         //Int_t rc = 1;
2920         track->fBConstrain =1;
2921         //      track->fLastPoint = i1+fInnerSec->GetNRows();  // first cluster in track position
2922         track->fLastPoint = i1;  // first cluster in track position
2923         track->fFirstPoint = track->fLastPoint;
2924         
2925         if (track->GetNumberOfClusters()<(i1-i2)*0.5 || 
2926             track->GetNumberOfClusters() < track->fNFoundable*0.6 || 
2927             track->fNShared>0.4*track->GetNumberOfClusters() ) {
2928           seed->Reset();
2929           seed->~AliTPCseed();
2930           continue;
2931         }
2932         nout1++;
2933         // Z VERTEX CONDITION
2934         Double_t zv;
2935         zv = track->GetZ()+track->GetTgl()/track->GetC()*
2936           ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2937         if (TMath::Abs(zv-z3)>cuts[2]) {
2938           FollowProlongation(*track, TMath::Max(i2-20,0));
2939           zv = track->GetZ()+track->GetTgl()/track->GetC()*
2940             ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2941           if (TMath::Abs(zv-z3)>cuts[2]){
2942             FollowProlongation(*track, TMath::Max(i2-40,0));
2943             zv = track->GetZ()+track->GetTgl()/track->GetC()*
2944               ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2945             if (TMath::Abs(zv-z3)>cuts[2] &&(track->GetNumberOfClusters() > track->fNFoundable*0.7)){
2946               // make seed without constrain
2947               AliTPCseed * track2 = MakeSeed(track,0.2,0.5,1.);
2948               FollowProlongation(*track2, i2,1);
2949               track2->fBConstrain = kFALSE;
2950               track2->fSeedType = 1;
2951               arr->AddLast(track2); 
2952               seed->Reset();
2953               seed->~AliTPCseed();
2954               continue;         
2955             }
2956             else{
2957               seed->Reset();
2958               seed->~AliTPCseed();
2959               continue;
2960             
2961             }
2962           }
2963         }
2964         
2965         track->fSeedType =0;
2966         arr->AddLast(track); 
2967         seed = new AliTPCseed;  
2968         nout2++;
2969         // don't consider other combinations
2970         if (track->GetNumberOfClusters() > track->fNFoundable*0.8)
2971           break;
2972       }
2973     }
2974   }
2975   if (fDebug>3){
2976     Info("MakeSeeds3","\nSeeding statistic:\t%d\t%d\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin,nout1,nout2);
2977   }
2978   delete seed;
2979 }
2980
2981
2982 void AliTPCtrackerMI::MakeSeeds5(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,  Float_t cuts[4],
2983                                  Float_t deltay) {
2984   
2985
2986
2987   //-----------------------------------------------------------------
2988   // This function creates track seeds.
2989   //-----------------------------------------------------------------
2990   // cuts[0]   - fP4 cut
2991   // cuts[1]   - tan(phi)  cut
2992   // cuts[2]   - zvertex cut
2993   // cuts[3]   - fP3 cut
2994
2995
2996   Int_t nin0  = 0;
2997   Int_t nin1  = 0;
2998   Int_t nin2  = 0;
2999   Int_t nin   = 0;
3000   Int_t nout1 = 0;
3001   Int_t nout2 = 0;
3002   Int_t nout3 =0;
3003   Double_t x[5], c[15];
3004   //
3005   // make temporary seed
3006   AliTPCseed * seed = new AliTPCseed;
3007   Double_t alpha=fOuterSec->GetAlpha(), shift=fOuterSec->GetAlphaShift();
3008   //  Double_t cs=cos(alpha), sn=sin(alpha);
3009   //
3010   //
3011
3012   // first 3 padrows
3013   Double_t x1 = GetXrow(i1-1);
3014   const    AliTPCRow& kr1=GetRow(sec,i1-1);
3015   Double_t y1max  = GetMaxY(i1-1)-kr1.fDeadZone-1.5;  
3016   //
3017   Double_t x1p = GetXrow(i1);
3018   const    AliTPCRow& kr1p=GetRow(sec,i1);
3019   //
3020   Double_t x1m = GetXrow(i1-2);
3021   const    AliTPCRow& kr1m=GetRow(sec,i1-2);
3022
3023   //
3024   //last 3 padrow for seeding
3025   AliTPCRow&  kr3  = GetRow((sec+fkNOS)%fkNOS,i1-7);
3026   Double_t    x3   =  GetXrow(i1-7);
3027   //  Double_t    y3max= GetMaxY(i1-7)-kr3.fDeadZone-1.5;  
3028   //
3029   AliTPCRow&  kr3p  = GetRow((sec+fkNOS)%fkNOS,i1-6);
3030   Double_t    x3p   = GetXrow(i1-6);
3031   //
3032   AliTPCRow&  kr3m  = GetRow((sec+fkNOS)%fkNOS,i1-8);
3033   Double_t    x3m   = GetXrow(i1-8);
3034
3035   //
3036   //
3037   // middle padrow
3038   Int_t im = i1-4;                           //middle pad row index
3039   Double_t xm         = GetXrow(im);         // radius of middle pad-row
3040   const AliTPCRow& krm=GetRow(sec,im);   //middle pad -row
3041   //  Double_t ymmax = GetMaxY(im)-kr1.fDeadZone-1.5;  
3042   //
3043   //
3044   Double_t deltax  = x1-x3;
3045   Double_t dymax   = deltax*cuts[1];
3046   Double_t dzmax   = deltax*cuts[3];
3047   //
3048   // loop over clusters  
3049   for (Int_t is=0; is < kr1; is++) {
3050     //
3051     if (kr1[is]->IsUsed(10)) continue;
3052     Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();    
3053     //
3054     if (deltay>0 && TMath::Abs(y1max-TMath::Abs(y1))> deltay ) continue;  // seed only at the edge    
3055     // 
3056     Int_t  index1 = TMath::Max(kr3.Find(z1-dzmax)-1,0);
3057     Int_t  index2 = TMath::Min(kr3.Find(z1+dzmax)+1,kr3);
3058     //    
3059     Double_t y3,   z3;
3060     //
3061     //
3062     UInt_t index;
3063     for (Int_t js=index1; js < index2; js++) {
3064       const AliTPCclusterMI *kcl = kr3[js];
3065       if (kcl->IsUsed(10)) continue;
3066       y3 = kcl->GetY(); 
3067       // apply angular cuts
3068       if (TMath::Abs(y1-y3)>dymax) continue;
3069       x3 = x3; 
3070       z3 = kcl->GetZ(); 
3071       if (TMath::Abs(z1-z3)>dzmax) continue;
3072       //
3073       Double_t angley = (y1-y3)/(x1-x3);
3074       Double_t anglez = (z1-z3)/(x1-x3);
3075       //
3076       Double_t erry = TMath::Abs(angley)*(x1-x1m)*0.5+0.5;
3077       Double_t errz = TMath::Abs(anglez)*(x1-x1m)*0.5+0.5;
3078       //
3079       Double_t yyym = angley*(xm-x1)+y1;
3080       Double_t zzzm = anglez*(xm-x1)+z1;
3081
3082       const AliTPCclusterMI *kcm = krm.FindNearest2(yyym,zzzm,erry,errz,index);
3083       if (!kcm) continue;
3084       if (kcm->IsUsed(10)) continue;
3085       
3086       erry = TMath::Abs(angley)*(x1-x1m)*0.4+0.5;
3087       errz = TMath::Abs(anglez)*(x1-x1m)*0.4+0.5;
3088       //
3089       //
3090       //
3091       Int_t used  =0;
3092       Int_t found =0;
3093       //
3094       // look around first
3095       const AliTPCclusterMI *kc1m = kr1m.FindNearest2(angley*(x1m-x1)+y1,
3096                                                       anglez*(x1m-x1)+z1,
3097                                                       erry,errz,index);
3098       //
3099       if (kc1m){
3100         found++;
3101         if (kc1m->IsUsed(10)) used++;
3102       }
3103       const AliTPCclusterMI *kc1p = kr1p.FindNearest2(angley*(x1p-x1)+y1,
3104                                                       anglez*(x1p-x1)+z1,
3105                                                       erry,errz,index);
3106       //
3107       if (kc1p){
3108         found++;
3109         if (kc1p->IsUsed(10)) used++;
3110       }
3111       if (used>1)  continue;
3112       if (found<1) continue; 
3113
3114       //
3115       // look around last
3116       const AliTPCclusterMI *kc3m = kr3m.FindNearest2(angley*(x3m-x3)+y3,
3117                                                       anglez*(x3m-x3)+z3,
3118                                                       erry,errz,index);
3119       //
3120       if (kc3m){
3121         found++;
3122         if (kc3m->IsUsed(10)) used++;
3123       }
3124       else 
3125         continue;
3126       const AliTPCclusterMI *kc3p = kr3p.FindNearest2(angley*(x3p-x3)+y3,
3127                                                       anglez*(x3p-x3)+z3,
3128                                                       erry,errz,index);
3129       //
3130       if (kc3p){
3131         found++;
3132         if (kc3p->IsUsed(10)) used++;
3133       }
3134       else 
3135         continue;
3136       if (used>1)  continue;
3137       if (found<3) continue;       
3138       //
3139       Double_t x2,y2,z2;
3140       x2 = xm;
3141       y2 = kcm->GetY();
3142       z2 = kcm->GetZ();
3143       //
3144                         
3145       x[0]=y1;
3146       x[1]=z1;
3147       x[4]=F1(x1,y1,x2,y2,x3,y3);
3148       //if (TMath::Abs(x[4]) >= cuts[0]) continue;
3149       nin0++;
3150       //
3151       x[2]=F2(x1,y1,x2,y2,x3,y3);
3152       nin1++;
3153       //
3154       x[3]=F3n(x1,y1,x2,y2,z1,z2,x[4]);
3155       //if (TMath::Abs(x[3]) > cuts[3]) continue;
3156       nin2++;
3157       //
3158       //
3159       Double_t sy1=0.1,  sz1=0.1;
3160       Double_t sy2=0.1,  sz2=0.1;
3161       Double_t sy3=0.1,  sy=0.1, sz=0.1;
3162       
3163       Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
3164       Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
3165       Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
3166       Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
3167       Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
3168       Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
3169       
3170       Double_t f30=(F3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
3171       Double_t f31=(F3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
3172       Double_t f32=(F3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
3173       Double_t f34=(F3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
3174       
3175       c[0]=sy1;
3176       c[1]=0.;       c[2]=sz1; 
3177       c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3178       c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3179       c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3180       c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3181       c[13]=f30*sy1*f40+f32*sy2*f42;
3182       c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3183       
3184       //        if (!BuildSeed(kr1[is],kcl,0,x1,x2,x3,x,c)) continue;
3185       
3186       UInt_t index=kr1.GetIndex(is);
3187       AliTPCseed *track=new(seed) AliTPCseed(index, x, c, x1, sec*alpha+shift);
3188       
3189       track->fIsSeeding = kTRUE;
3190
3191       nin++;      
3192       FollowProlongation(*track, i1-7,1);
3193       if (track->GetNumberOfClusters() < track->fNFoundable*0.75 || 
3194           track->fNShared>0.6*track->GetNumberOfClusters() || ( track->GetSigmaY2()+ track->GetSigmaZ2())>0.6){
3195         seed->Reset();
3196         seed->~AliTPCseed();
3197         continue;
3198       }
3199       nout1++;
3200       nout2++;  
3201       //Int_t rc = 1;
3202       FollowProlongation(*track, i2,1);
3203       track->fBConstrain =0;
3204       track->fLastPoint = i1+fInnerSec->GetNRows();  // first cluster in track position
3205       track->fFirstPoint = track->fLastPoint;
3206       
3207       if (track->GetNumberOfClusters()<(i1-i2)*0.5 || 
3208           track->GetNumberOfClusters()<track->fNFoundable*0.7 || 
3209           track->fNShared>2. || track->GetChi2()/track->GetNumberOfClusters()>6 || ( track->GetSigmaY2()+ track->GetSigmaZ2())>0.5 ) {
3210         seed->Reset();
3211         seed->~AliTPCseed();
3212         continue;
3213       }
3214    
3215       {
3216         FollowProlongation(*track, TMath::Max(i2-10,0),1);
3217         AliTPCseed * track2 = MakeSeed(track,0.2,0.5,0.9);
3218         FollowProlongation(*track2, i2,1);
3219         track2->fBConstrain = kFALSE;
3220         track2->fSeedType = 4;
3221         arr->AddLast(track2); 
3222         seed->Reset();
3223         seed->~AliTPCseed();
3224       }
3225       
3226    
3227       //arr->AddLast(track); 
3228       //seed = new AliTPCseed;  
3229       nout3++;
3230     }
3231   }
3232   
3233   if (fDebug>3){
3234     Info("MakeSeeds5","\nSeeding statiistic:\t%d\t%d\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin,nout1,nout2,nout3);
3235   }
3236   delete seed;
3237 }
3238
3239
3240 //_____________________________________________________________________________
3241 void AliTPCtrackerMI::MakeSeeds2(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2, Float_t */*cuts[4]*/,
3242                                  Float_t deltay, Bool_t /*bconstrain*/) {
3243   //-----------------------------------------------------------------
3244   // This function creates track seeds - without vertex constraint
3245   //-----------------------------------------------------------------
3246   // cuts[0]   - fP4 cut        - not applied
3247   // cuts[1]   - tan(phi)  cut
3248   // cuts[2]   - zvertex cut    - not applied 
3249   // cuts[3]   - fP3 cut
3250   Int_t nin0=0;
3251   Int_t nin1=0;
3252   Int_t nin2=0;
3253   Int_t nin3=0;
3254   //  Int_t nin4=0;
3255   //Int_t nin5=0;
3256
3257   
3258
3259   Double_t alpha=fOuterSec->GetAlpha(), shift=fOuterSec->GetAlphaShift();
3260   //  Double_t cs=cos(alpha), sn=sin(alpha);
3261   Int_t row0 = (i1+i2)/2;
3262   Int_t drow = (i1-i2)/2;
3263   const AliTPCRow& kr0=fSectors[sec][row0];
3264   AliTPCRow * kr=0;
3265
3266   AliTPCpolyTrack polytrack;
3267   Int_t nclusters=fSectors[sec][row0];
3268   AliTPCseed * seed = new AliTPCseed;
3269
3270   Int_t sumused=0;
3271   Int_t cused=0;
3272   Int_t cnused=0;
3273   for (Int_t is=0; is < nclusters; is++) {  //LOOP over clusters
3274     Int_t nfound =0;
3275     Int_t nfoundable =0;
3276     for (Int_t iter =1; iter<2; iter++){   //iterations
3277       const AliTPCRow& krm=fSectors[sec][row0-iter];
3278       const AliTPCRow& krp=fSectors[sec][row0+iter];      
3279       const AliTPCclusterMI * cl= kr0[is];
3280       
3281       if (cl->IsUsed(10)) {
3282         cused++;
3283       }
3284       else{
3285         cnused++;
3286       }
3287       Double_t x = kr0.GetX();
3288       // Initialization of the polytrack
3289       nfound =0;
3290       nfoundable =0;
3291       polytrack.Reset();
3292       //
3293       Double_t y0= cl->GetY();
3294       Double_t z0= cl->GetZ();
3295       Float_t erry = 0;
3296       Float_t errz = 0;
3297       
3298       Double_t ymax = fSectors->GetMaxY(row0)-kr0.fDeadZone-1.5;
3299       if (deltay>0 && TMath::Abs(ymax-TMath::Abs(y0))> deltay ) continue;  // seed only at the edge
3300       
3301       erry = (0.5)*cl->GetSigmaY2()/TMath::Sqrt(cl->GetQ())*6;      
3302       errz = (0.5)*cl->GetSigmaZ2()/TMath::Sqrt(cl->GetQ())*6;      
3303       polytrack.AddPoint(x,y0,z0,erry, errz);
3304
3305       sumused=0;
3306       if (cl->IsUsed(10)) sumused++;
3307
3308
3309       Float_t roady = (5*TMath::Sqrt(cl->GetSigmaY2()+0.2)+1.)*iter;
3310       Float_t roadz = (5*TMath::Sqrt(cl->GetSigmaZ2()+0.2)+1.)*iter;
3311       //
3312       x = krm.GetX();
3313       AliTPCclusterMI * cl1 = krm.FindNearest(y0,z0,roady,roadz);
3314       if (cl1 && TMath::Abs(ymax-TMath::Abs(y0))) {
3315         erry = (0.5)*cl1->GetSigmaY2()/TMath::Sqrt(cl1->GetQ())*3;          
3316         errz = (0.5)*cl1->GetSigmaZ2()/TMath::Sqrt(cl1->GetQ())*3;
3317         if (cl1->IsUsed(10))  sumused++;
3318         polytrack.AddPoint(x,cl1->GetY(),cl1->GetZ(),erry,errz);
3319       }
3320       //
3321       x = krp.GetX();
3322       AliTPCclusterMI * cl2 = krp.FindNearest(y0,z0,roady,roadz);
3323       if (cl2) {
3324         erry = (0.5)*cl2->GetSigmaY2()/TMath::Sqrt(cl2->GetQ())*3;          
3325         errz = (0.5)*cl2->GetSigmaZ2()/TMath::Sqrt(cl2->GetQ())*3;
3326         if (cl2->IsUsed(10)) sumused++;  
3327         polytrack.AddPoint(x,cl2->GetY(),cl2->GetZ(),erry,errz);
3328       }
3329       //
3330       if (sumused>0) continue;
3331       nin0++;
3332       polytrack.UpdateParameters();
3333       // follow polytrack
3334       roadz = 1.2;
3335       roady = 1.2;
3336       //
3337       Double_t yn,zn;
3338       nfoundable = polytrack.GetN();
3339       nfound     = nfoundable; 
3340       //
3341       for (Int_t ddrow = iter+1; ddrow<drow;ddrow++){
3342         Float_t maxdist = 0.8*(1.+3./(ddrow));
3343         for (Int_t delta = -1;delta<=1;delta+=2){
3344           Int_t row = row0+ddrow*delta;
3345           kr = &(fSectors[sec][row]);
3346           Double_t xn = kr->GetX();
3347           Double_t ymax = fSectors->GetMaxY(row)-kr->fDeadZone-1.5;
3348           polytrack.GetFitPoint(xn,yn,zn);
3349           if (TMath::Abs(yn)>ymax) continue;
3350           nfoundable++;
3351           AliTPCclusterMI * cln = kr->FindNearest(yn,zn,roady,roadz);
3352           if (cln) {
3353             Float_t dist =  TMath::Sqrt(  (yn-cln->GetY())*(yn-cln->GetY())+(zn-cln->GetZ())*(zn-cln->GetZ()));
3354             if (dist<maxdist){
3355               /*
3356               erry = (dist+0.3)*cln->GetSigmaY2()/TMath::Sqrt(cln->GetQ())*(1.+1./(ddrow));         
3357               errz = (dist+0.3)*cln->GetSigmaZ2()/TMath::Sqrt(cln->GetQ())*(1.+1./(ddrow));
3358               if (cln->IsUsed(10)) {
3359                 //      printf("used\n");
3360                 sumused++;
3361                 erry*=2;
3362                 errz*=2;
3363               }
3364               */
3365               erry=0.1;
3366               errz=0.1;
3367               polytrack.AddPoint(xn,cln->GetY(),cln->GetZ(),erry, errz);
3368               nfound++;
3369             }
3370           }
3371         }
3372         if ( (sumused>3) || (sumused>0.5*nfound) || (nfound<0.6*nfoundable))  break;     
3373         polytrack.UpdateParameters();
3374       }           
3375     }
3376     if ( (sumused>3) || (sumused>0.5*nfound))  {
3377       //printf("sumused   %d\n",sumused);
3378       continue;
3379     }
3380     nin1++;
3381     Double_t dy,dz;
3382     polytrack.GetFitDerivation(kr0.GetX(),dy,dz);
3383     AliTPCpolyTrack track2;
3384     
3385     polytrack.Refit(track2,0.5+TMath::Abs(dy)*0.3,0.4+TMath::Abs(dz)*0.3);
3386     if (track2.GetN()<0.5*nfoundable) continue;
3387     nin2++;
3388
3389     if ((nfound>0.6*nfoundable) &&( nfoundable>0.4*(i1-i2))) {
3390       //
3391       // test seed with and without constrain
3392       for (Int_t constrain=0; constrain<=0;constrain++){
3393         // add polytrack candidate
3394
3395         Double_t x[5], c[15];
3396         Double_t x1,x2,x3,y1,y2,y3,z1,z2,z3;
3397         track2.GetBoundaries(x3,x1);    
3398         x2 = (x1+x3)/2.;
3399         track2.GetFitPoint(x1,y1,z1);
3400         track2.GetFitPoint(x2,y2,z2);
3401         track2.GetFitPoint(x3,y3,z3);
3402         //
3403         //is track pointing to the vertex ?
3404         Double_t x0,y0,z0;
3405         x0=0;
3406         polytrack.GetFitPoint(x0,y0,z0);
3407
3408         if (constrain) {
3409           x2 = x3;
3410           y2 = y3;
3411           z2 = z3;
3412           
3413           x3 = 0;
3414           y3 = 0;
3415           z3 = 0;
3416         }
3417         x[0]=y1;
3418         x[1]=z1;
3419         x[4]=F1(x1,y1,x2,y2,x3,y3);
3420                 
3421         //      if (TMath::Abs(x[4]) >= cuts[0]) continue;  //
3422         x[2]=F2(x1,y1,x2,y2,x3,y3);
3423         
3424         //if (TMath::Abs(x[4]*x1-x[2]) >= cuts[1]) continue;
3425         //x[3]=F3(x1,y1,x2,y2,z1,z2);
3426         x[3]=F3n(x1,y1,x3,y3,z1,z3,x[4]);
3427         //if (TMath::Abs(x[3]) > cuts[3]) continue;
3428
3429         
3430         Double_t sy =0.1, sz =0.1;
3431         Double_t sy1=0.02, sz1=0.02;
3432         Double_t sy2=0.02, sz2=0.02;
3433         Double_t sy3=0.02;
3434
3435         if (constrain){
3436           sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
3437         }
3438         
3439         Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
3440         Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
3441         Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
3442         Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
3443         Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
3444         Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
3445
3446         Double_t f30=(F3(x1,y1+sy,x3,y3,z1,z3)-x[3])/sy;
3447         Double_t f31=(F3(x1,y1,x3,y3,z1+sz,z3)-x[3])/sz;
3448         Double_t f32=(F3(x1,y1,x3,y3+sy,z1,z3)-x[3])/sy;
3449         Double_t f34=(F3(x1,y1,x3,y3,z1,z3+sz)-x[3])/sz;
3450
3451         
3452         c[0]=sy1;
3453         c[1]=0.;       c[2]=sz1;
3454         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3455         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3456         c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3457         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3458         c[13]=f30*sy1*f40+f32*sy2*f42;
3459         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3460         
3461         //Int_t row1 = fSectors->GetRowNumber(x1);
3462         Int_t row1 = GetRowNumber(x1);
3463
3464         UInt_t index=0;
3465         //kr0.GetIndex(is);
3466         AliTPCseed *track=new (seed) AliTPCseed(index, x, c, x1, sec*alpha+shift);
3467         track->fIsSeeding = kTRUE;
3468         Int_t rc=FollowProlongation(*track, i2);        
3469         if (constrain) track->fBConstrain =1;
3470         else
3471           track->fBConstrain =0;
3472         track->fLastPoint = row1+fInnerSec->GetNRows();  // first cluster in track position
3473         track->fFirstPoint = track->fLastPoint;
3474
3475         if (rc==0 || track->GetNumberOfClusters()<(i1-i2)*0.5 || 
3476             track->GetNumberOfClusters() < track->fNFoundable*0.6 || 
3477             track->fNShared>0.4*track->GetNumberOfClusters()) {
3478           //delete track;
3479           seed->Reset();
3480           seed->~AliTPCseed();
3481         }
3482         else {
3483           arr->AddLast(track);
3484           seed = new AliTPCseed;
3485         }
3486         nin3++;
3487       }
3488     }  // if accepted seed
3489   }
3490   if (fDebug>3){
3491     Info("MakeSeeds2","\nSeeding statiistic:\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin3);
3492   }
3493   delete seed;
3494 }
3495
3496
3497 AliTPCseed *AliTPCtrackerMI::MakeSeed(AliTPCseed *track, Float_t r0, Float_t r1, Float_t r2)
3498 {
3499   //
3500   //
3501   //reseed using track points
3502   Int_t p0 = int(r0*track->GetNumberOfClusters());     // point 0 
3503   Int_t p1 = int(r1*track->GetNumberOfClusters());
3504   Int_t p2 = int(r2*track->GetNumberOfClusters());   // last point
3505   Int_t pp2=0;
3506   Double_t  x0[3],x1[3],x2[3];
3507   for (Int_t i=0;i<3;i++){
3508     x0[i]=-1;
3509     x1[i]=-1;
3510     x2[i]=-1;
3511   }
3512
3513   // find track position at given ratio of the length
3514   Int_t  sec0=0, sec1=0, sec2=0;
3515   Int_t index=-1;
3516   Int_t clindex;
3517   for (Int_t i=0;i<160;i++){
3518     if (track->fClusterPointer[i]){
3519       index++;
3520       AliTPCTrackerPoint   *trpoint =track->GetTrackPoint(i);
3521       if ( (index<p0) || x0[0]<0 ){
3522         if (trpoint->GetX()>1){
3523           clindex = track->GetClusterIndex2(i);
3524           if (clindex>0){       
3525             x0[0] = trpoint->GetX();
3526             x0[1] = trpoint->GetY();
3527             x0[2] = trpoint->GetZ();
3528             sec0  = ((clindex&0xff000000)>>24)%18;
3529           }
3530         }
3531       }
3532
3533       if ( (index<p1) &&(trpoint->GetX()>1)){
3534         clindex = track->GetClusterIndex2(i);
3535         if (clindex>0){
3536           x1[0] = trpoint->GetX();
3537           x1[1] = trpoint->GetY();
3538           x1[2] = trpoint->GetZ();
3539           sec1  = ((clindex&0xff000000)>>24)%18;
3540         }
3541       }
3542       if ( (index<p2) &&(trpoint->GetX()>1)){
3543         clindex = track->GetClusterIndex2(i);
3544         if (clindex>0){
3545           x2[0] = trpoint->GetX();
3546           x2[1] = trpoint->GetY();
3547           x2[2] = trpoint->GetZ(); 
3548           sec2  = ((clindex&0xff000000)>>24)%18;
3549           pp2 = i;
3550         }
3551       }
3552     }
3553   }
3554   
3555   Double_t alpha, cs,sn, xx2,yy2;
3556   //
3557   alpha = (sec1-sec2)*fSectors->GetAlpha();
3558   cs = TMath::Cos(alpha);
3559   sn = TMath::Sin(alpha); 
3560   xx2= x1[0]*cs-x1[1]*sn;
3561   yy2= x1[0]*sn+x1[1]*cs;
3562   x1[0] = xx2;
3563   x1[1] = yy2;
3564   //
3565   alpha = (sec0-sec2)*fSectors->GetAlpha();
3566   cs = TMath::Cos(alpha);
3567   sn = TMath::Sin(alpha); 
3568   xx2= x0[0]*cs-x0[1]*sn;
3569   yy2= x0[0]*sn+x0[1]*cs;
3570   x0[0] = xx2;
3571   x0[1] = yy2;
3572   //
3573   //
3574   //
3575   Double_t x[5],c[15];
3576   //
3577   x[0]=x2[1];
3578   x[1]=x2[2];
3579   x[4]=F1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
3580   //  if (x[4]>1) return 0;
3581   x[2]=F2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
3582   x[3]=F3n(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2],x[4]);
3583   //if (TMath::Abs(x[3]) > 2.2)  return 0;
3584   //if (TMath::Abs(x[2]) > 1.99) return 0;
3585   //  
3586   Double_t sy =0.1,  sz =0.1;
3587   //
3588   Double_t sy1=0.02+track->GetSigmaY2(), sz1=0.02+track->GetSigmaZ2();
3589   Double_t sy2=0.01+track->GetSigmaY2(), sz2=0.01+track->GetSigmaZ2();
3590   Double_t sy3=0.01+track->GetSigmaY2();
3591   //
3592   Double_t f40=(F1(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[4])/sy;
3593   Double_t f42=(F1(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[4])/sy;
3594   Double_t f43=(F1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[4])/sy;
3595   Double_t f20=(F2(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[2])/sy;
3596   Double_t f22=(F2(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[2])/sy;
3597   Double_t f23=(F2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[2])/sy;
3598   //
3599   Double_t f30=(F3(x2[0],x2[1]+sy,x0[0],x0[1],x2[2],x0[2])-x[3])/sy;
3600   Double_t f31=(F3(x2[0],x2[1],x0[0],x0[1],x2[2]+sz,x0[2])-x[3])/sz;
3601   Double_t f32=(F3(x2[0],x2[1],x0[0],x0[1]+sy,x2[2],x0[2])-x[3])/sy;
3602   Double_t f34=(F3(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2]+sz)-x[3])/sz;
3603   
3604   
3605   c[0]=sy1;
3606   c[1]=0.;       c[2]=sz1;
3607   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3608   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3609   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3610   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3611   c[13]=f30*sy1*f40+f32*sy2*f42;
3612   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3613   
3614   //  Int_t row1 = fSectors->GetRowNumber(x2[0]);
3615   AliTPCseed *seed=new  AliTPCseed(0, x, c, x2[0], sec2*fSectors->GetAlpha()+fSectors->GetAlphaShift());
3616   //  Double_t y0,z0,y1,z1, y2,z2;
3617   //seed->GetProlongation(x0[0],y0,z0);
3618   // seed->GetProlongation(x1[0],y1,z1);
3619   //seed->GetProlongation(x2[0],y2,z2);
3620   //  seed =0;
3621   seed->fLastPoint  = pp2;
3622   seed->fFirstPoint = pp2;
3623   
3624
3625   return seed;
3626 }
3627
3628
3629 AliTPCseed *AliTPCtrackerMI::ReSeed(AliTPCseed *track, Float_t r0, Float_t r1, Float_t r2)
3630 {
3631   //
3632   //
3633   //reseed using founded clusters 
3634   //
3635   // Find the number of clusters
3636   Int_t nclusters = 0;
3637   for (Int_t irow=0;irow<160;irow++){
3638     if (track->GetClusterIndex(irow)>0) nclusters++;
3639   }
3640   //
3641   Int_t ipos[3];
3642   ipos[0] = TMath::Max(int(r0*nclusters),0);             // point 0 cluster
3643   ipos[1] = TMath::Min(int(r1*nclusters),nclusters-1);   // 
3644   ipos[2] = TMath::Min(int(r2*nclusters),nclusters-1);   // last point
3645   //
3646   //
3647   Double_t  xyz[3][3];
3648   Int_t     row[3],sec[3]={0,0,0};
3649   //
3650   // find track row position at given ratio of the length
3651   Int_t index=-1;
3652   for (Int_t irow=0;irow<160;irow++){    
3653     if (track->GetClusterIndex2(irow)<0) continue;
3654     index++;
3655     for (Int_t ipoint=0;ipoint<3;ipoint++){
3656       if (index<=ipos[ipoint]) row[ipoint] = irow;
3657     }        
3658   }
3659   //
3660   //Get cluster and sector position
3661   for (Int_t ipoint=0;ipoint<3;ipoint++){
3662     Int_t clindex = track->GetClusterIndex2(row[ipoint]);
3663     AliTPCclusterMI * cl = GetClusterMI(clindex);
3664     if (cl==0) {
3665       //Error("Bug\n");
3666       //      AliTPCclusterMI * cl = GetClusterMI(clindex);
3667       return 0;
3668     }
3669     sec[ipoint]     = ((clindex&0xff000000)>>24)%18;
3670     xyz[ipoint][0]  = GetXrow(row[ipoint]);
3671     xyz[ipoint][1]  = cl->GetY();
3672     xyz[ipoint][2]  = cl->GetZ();
3673   }
3674   //
3675   //
3676   // Calculate seed state vector and covariance matrix
3677
3678   Double_t alpha, cs,sn, xx2,yy2;
3679   //
3680   alpha = (sec[1]-sec[2])*fSectors->GetAlpha();
3681   cs = TMath::Cos(alpha);
3682   sn = TMath::Sin(alpha); 
3683   xx2= xyz[1][0]*cs-xyz[1][1]*sn;
3684   yy2= xyz[1][0]*sn+xyz[1][1]*cs;
3685   xyz[1][0] = xx2;
3686   xyz[1][1] = yy2;
3687   //
3688   alpha = (sec[0]-sec[2])*fSectors->GetAlpha();
3689   cs = TMath::Cos(alpha);
3690   sn = TMath::Sin(alpha); 
3691   xx2= xyz[0][0]*cs-xyz[0][1]*sn;
3692   yy2= xyz[0][0]*sn+xyz[0][1]*cs;
3693   xyz[0][0] = xx2;
3694   xyz[0][1] = yy2;
3695   //
3696   //
3697   //
3698   Double_t x[5],c[15];
3699   //
3700   x[0]=xyz[2][1];
3701   x[1]=xyz[2][2];
3702   x[4]=F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3703   x[2]=F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3704   x[3]=F3n(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2],x[4]);
3705   //  
3706   Double_t sy =0.1,  sz =0.1;
3707   //
3708   Double_t sy1=0.2, sz1=0.2;
3709   Double_t sy2=0.2, sz2=0.2;
3710   Double_t sy3=0.2;
3711   //
3712   Double_t f40=(F1(xyz[2][0],xyz[2][1]+sy,xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1])-x[4])/sy;
3713   Double_t f42=(F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1]+sy,xyz[0][0],xyz[0][1])-x[4])/sy;
3714   Double_t f43=(F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]+sy)-x[4])/sy;
3715   Double_t f20=(F2(xyz[2][0],xyz[2][1]+sy,xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1])-x[2])/sy;
3716   Double_t f22=(F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1]+sy,xyz[0][0],xyz[0][1])-x[2])/sy;
3717   Double_t f23=(F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]+sy)-x[2])/sy;
3718   //
3719   Double_t f30=(F3(xyz[2][0],xyz[2][1]+sy,xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2])-x[3])/sy;
3720   Double_t f31=(F3(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2]+sz,xyz[0][2])-x[3])/sz;
3721   Double_t f32=(F3(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1]+sy,xyz[2][2],xyz[0][2])-x[3])/sy;
3722   Double_t f34=(F3(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2]+sz)-x[3])/sz;
3723   
3724   
3725   c[0]=sy1;
3726   c[1]=0.;       c[2]=sz1;
3727   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3728   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3729   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3730   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3731   c[13]=f30*sy1*f40+f32*sy2*f42;
3732   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3733   
3734   //  Int_t row1 = fSectors->GetRowNumber(xyz[2][0]);
3735   AliTPCseed *seed=new  AliTPCseed(0, x, c, xyz[2][0], sec[2]*fSectors->GetAlpha()+fSectors->GetAlphaShift());
3736   seed->fLastPoint  = row[2];
3737   seed->fFirstPoint = row[2];  
3738   return seed;
3739 }
3740
3741
3742 AliTPCseed *AliTPCtrackerMI::ReSeed(AliTPCseed *track,Int_t r0, Bool_t forward)
3743 {
3744   //
3745   //
3746   //reseed using founded clusters 
3747   //
3748   Double_t  xyz[3][3];
3749   Int_t     row[3]={0,0,0};
3750   Int_t     sec[3]={0,0,0};
3751   //
3752   // forward direction
3753   if (forward){
3754     for (Int_t irow=r0;irow<160;irow++){
3755       if (track->GetClusterIndex(irow)>0){
3756         row[0] = irow;
3757         break;
3758       }
3759     }
3760     for (Int_t irow=160;irow>r0;irow--){
3761       if (track->GetClusterIndex(irow)>0){
3762         row[2] = irow;
3763         break;
3764       }
3765     }
3766     for (Int_t irow=row[2]-15;irow>row[0];irow--){
3767       if (track->GetClusterIndex(irow)>0){
3768         row[1] = irow;
3769         break;
3770       }
3771     }
3772     //
3773   }
3774   if (!forward){
3775     for (Int_t irow=0;irow<r0;irow++){
3776       if (track->GetClusterIndex(irow)>0){
3777         row[0] = irow;
3778         break;
3779       }
3780     }
3781     for (Int_t irow=r0;irow>0;irow--){
3782       if (track->GetClusterIndex(irow)>0){
3783         row[2] = irow;
3784         break;
3785       }
3786     }    
3787     for (Int_t irow=row[2]-15;irow>row[0];irow--){
3788       if (track->GetClusterIndex(irow)>0){
3789         row[1] = irow;
3790         break;
3791       }
3792     } 
3793   }
3794   //
3795   if ((row[2]-row[0])<20) return 0;
3796   if (row[1]==0) return 0;
3797   //
3798   //
3799   //Get cluster and sector position
3800   for (Int_t ipoint=0;ipoint<3;ipoint++){
3801     Int_t clindex = track->GetClusterIndex2(row[ipoint]);
3802     AliTPCclusterMI * cl = GetClusterMI(clindex);
3803     if (cl==0) {
3804       //Error("Bug\n");
3805       //      AliTPCclusterMI * cl = GetClusterMI(clindex);
3806       return 0;
3807     }
3808     sec[ipoint]     = ((clindex&0xff000000)>>24)%18;
3809     xyz[ipoint][0]  = GetXrow(row[ipoint]);
3810     AliTPCTrackerPoint * point = track->GetTrackPoint(row[ipoint]);    
3811     if (point&&ipoint<2){
3812       //
3813        xyz[ipoint][1]  = point->GetY();
3814        xyz[ipoint][2]  = point->GetZ();
3815     }
3816     else{
3817       xyz[ipoint][1]  = cl->GetY();
3818       xyz[ipoint][2]  = cl->GetZ();
3819     }
3820   }
3821   //
3822   //
3823   //
3824   //
3825   // Calculate seed state vector and covariance matrix
3826
3827   Double_t alpha, cs,sn, xx2,yy2;
3828   //
3829   alpha = (sec[1]-sec[2])*fSectors->GetAlpha();
3830   cs = TMath::Cos(alpha);
3831   sn = TMath::Sin(alpha); 
3832   xx2= xyz[1][0]*cs-xyz[1][1]*sn;
3833   yy2= xyz[1][0]*sn+xyz[1][1]*cs;
3834   xyz[1][0] = xx2;
3835   xyz[1][1] = yy2;
3836   //
3837   alpha = (sec[0]-sec[2])*fSectors->GetAlpha();
3838   cs = TMath::Cos(alpha);
3839   sn = TMath::Sin(alpha); 
3840   xx2= xyz[0][0]*cs-xyz[0][1]*sn;
3841   yy2= xyz[0][0]*sn+xyz[0][1]*cs;
3842   xyz[0][0] = xx2;
3843   xyz[0][1] = yy2;
3844   //
3845   //
3846   //
3847   Double_t x[5],c[15];
3848   //
3849   x[0]=xyz[2][1];
3850   x[1]=xyz[2][2];
3851   x[4]=F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3852   x[2]=F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3853   x[3]=F3n(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2],x[4]);
3854   //  
3855   Double_t sy =0.1,  sz =0.1;
3856   //
3857   Double_t sy1=0.2, sz1=0.2;
3858   Double_t sy2=0.2, sz2=0.2;
3859   Double_t sy3=0.2;
3860   //
3861   Double_t f40=(F1(xyz[2][0],xyz[2][1]+sy,xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1])-x[4])/sy;
3862   Double_t f42=(F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1]+sy,xyz[0][0],xyz[0][1])-x[4])/sy;
3863   Double_t f43=(F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]+sy)-x[4])/sy;
3864   Double_t f20=(F2(xyz[2][0],xyz[2][1]+sy,xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1])-x[2])/sy;
3865   Double_t f22=(F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1]+sy,xyz[0][0],xyz[0][1])-x[2])/sy;
3866   Double_t f23=(F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]+sy)-x[2])/sy;
3867   //
3868   Double_t f30=(F3(xyz[2][0],xyz[2][1]+sy,xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2])-x[3])/sy;
3869   Double_t f31=(F3(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2]+sz,xyz[0][2])-x[3])/sz;
3870   Double_t f32=(F3(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1]+sy,xyz[2][2],xyz[0][2])-x[3])/sy;
3871   Double_t f34=(F3(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2]+sz)-x[3])/sz;
3872   
3873   
3874   c[0]=sy1;
3875   c[1]=0.;       c[2]=sz1;
3876   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3877   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3878   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3879   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3880   c[13]=f30*sy1*f40+f32*sy2*f42;
3881   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3882   
3883   //  Int_t row1 = fSectors->GetRowNumber(xyz[2][0]);
3884   AliTPCseed *seed=new  AliTPCseed(0, x, c, xyz[2][0], sec[2]*fSectors->GetAlpha()+fSectors->GetAlphaShift());
3885   seed->fLastPoint  = row[2];
3886   seed->fFirstPoint = row[2];  
3887   for (Int_t i=row[0];i<row[2];i++){
3888     seed->fIndex[i] = track->fIndex[i];
3889   }
3890
3891   return seed;
3892 }
3893
3894 void  AliTPCtrackerMI::FindKinks(TObjArray * array, AliESD *esd)
3895 {
3896   //
3897   //  find kinks
3898   //
3899   //
3900
3901   TObjArray *kinks= new TObjArray(10000);
3902   //  TObjArray *v0s= new TObjArray(10000);
3903   Int_t nentries = array->GetEntriesFast();
3904   AliHelix *helixes      = new AliHelix[nentries];
3905   Int_t    *sign         = new Int_t[nentries];
3906   Int_t    *nclusters    = new Int_t[nentries];
3907   Float_t  *alpha        = new Float_t[nentries];
3908   AliESDkink * kink      = new AliESDkink();
3909   Int_t      * usage     = new Int_t[nentries];
3910   Float_t  *zm           = new Float_t[nentries];
3911   Float_t  *z0           = new Float_t[nentries]; 
3912   Float_t  *fim          = new Float_t[nentries];
3913   Float_t  *shared       = new Float_t[nentries];
3914   Bool_t   *circular     = new Bool_t[nentries];
3915   Float_t *dca          = new Float_t[nentries];
3916   //const AliESDVertex * primvertex = esd->GetVertex();
3917   //
3918   //  nentries = array->GetEntriesFast();
3919   //
3920   
3921   //
3922   //
3923   for (Int_t i=0;i<nentries;i++){
3924     sign[i]=0;
3925     usage[i]=0;
3926     AliTPCseed* track = (AliTPCseed*)array->At(i);    
3927     if (!track) continue;
3928     track->fCircular =0;
3929     shared[i] = kFALSE;
3930     track->UpdatePoints();
3931     if (( track->GetPoints()[2]- track->GetPoints()[0])>5 && track->GetPoints()[3]>0.8){
3932     }
3933     nclusters[i]=track->GetNumberOfClusters();
3934     alpha[i] = track->GetAlpha();
3935     new (&helixes[i]) AliHelix(*track);
3936     Double_t xyz[3];
3937     helixes[i].Evaluate(0,xyz);
3938     sign[i] = (track->GetC()>0) ? -1:1;
3939     Double_t x,y,z;
3940     x=160;
3941     if (track->GetProlongation(x,y,z)){
3942       zm[i]  = z;
3943       fim[i] = alpha[i]+TMath::ATan2(y,x);
3944     }
3945     else{
3946       zm[i]  = track->GetZ();
3947       fim[i] = alpha[i];
3948     }   
3949     z0[i]=1000;
3950     circular[i]= kFALSE;
3951     if (track->GetProlongation(0,y,z))  z0[i] = z;
3952     dca[i] = track->GetD(0,0);    
3953   }
3954   //
3955   //
3956   TStopwatch timer;
3957   timer.Start();
3958   Int_t ncandidates =0;
3959   Int_t nall =0;
3960   Int_t ntracks=0; 
3961   Double_t phase[2][2],radius[2];
3962
3963   //
3964   // Find circling track
3965   TTreeSRedirector &cstream = *fDebugStreamer;
3966   //
3967   for (Int_t i0=0;i0<nentries;i0++){
3968     AliTPCseed * track0 = (AliTPCseed*)array->At(i0);
3969     if (!track0) continue;    
3970     if (track0->fN<40) continue;
3971     if (TMath::Abs(1./track0->fP4)>200) continue;
3972     for (Int_t i1=i0+1;i1<nentries;i1++){
3973       AliTPCseed * track1 = (AliTPCseed*)array->At(i1);
3974       if (!track1) continue;
3975       if (track1->fN<40)                  continue;
3976       if ( TMath::Abs(track1->fP3+track0->fP3)>0.1) continue;
3977       if (track0->fBConstrain&&track1->fBConstrain) continue;
3978       if (TMath::Abs(1./track1->fP4)>200) continue;
3979       if (track1->fP4*track0->fP4>0)      continue;
3980       if (track1->fP3*track0->fP3>0)      continue;
3981       if (max(TMath::Abs(1./track0->fP4),TMath::Abs(1./track1->fP4))>190) continue;
3982       if (track0->fBConstrain&&TMath::Abs(track1->fP4)<TMath::Abs(track0->fP4)) continue; //returning - lower momenta
3983       if (track1->fBConstrain&&TMath::Abs(track0->fP4)<TMath::Abs(track1->fP4)) continue; //returning - lower momenta
3984       //
3985       Float_t mindcar = TMath::Min(TMath::Abs(dca[i0]),TMath::Abs(dca[i1]));
3986       if (mindcar<5)   continue;
3987       Float_t mindcaz = TMath::Min(TMath::Abs(z0[i0]-GetZ()),TMath::Abs(z0[i1]-GetZ()));
3988       if (mindcaz<5) continue;
3989       if (mindcar+mindcaz<20) continue;
3990       //
3991       //
3992       Float_t xc0 = helixes[i0].GetHelix(6);
3993       Float_t yc0 = helixes[i0].GetHelix(7);
3994       Float_t r0  = helixes[i0].GetHelix(8);
3995       Float_t xc1 = helixes[i1].GetHelix(6);
3996       Float_t yc1 = helixes[i1].GetHelix(7);
3997       Float_t r1  = helixes[i1].GetHelix(8);
3998         
3999       Float_t rmean = (r0+r1)*0.5;
4000       Float_t delta =TMath::Sqrt((xc1-xc0)*(xc1-xc0)+(yc1-yc0)*(yc1-yc0));
4001       //if (delta>30) continue;
4002       if (delta>rmean*0.25) continue;
4003       if (TMath::Abs(r0-r1)/rmean>0.3) continue; 
4004       //
4005       Int_t npoints = helixes[i0].GetRPHIintersections(helixes[i1], phase, radius,10);
4006       if (npoints==0) continue;
4007       helixes[i0].GetClosestPhases(helixes[i1], phase);
4008       //
4009       Double_t xyz0[3];
4010       Double_t xyz1[3];
4011       Double_t hangles[3];
4012       helixes[i0].Evaluate(phase[0][0],xyz0);
4013       helixes[i1].Evaluate(phase[0][1],xyz1);
4014
4015       helixes[i0].GetAngle(phase[0][0],helixes[i1],phase[0][1],hangles);
4016       Double_t deltah[2],deltabest;
4017       if (hangles[2]<2.8) continue;
4018       /*
4019       cstream<<"C"<<track0->fLab<<track1->fLab<<
4020         track0->fP3<<track1->fP3<<
4021         track0->fP4<<track1->fP4<<
4022         delta<<rmean<<npoints<<
4023         hangles[0]<<hangles[2]<<
4024         xyz0[2]<<xyz1[2]<<radius[0]<<"\n"; 
4025       */
4026       if (npoints>0){
4027         Int_t ibest=0;
4028         helixes[i0].ParabolicDCA(helixes[i1],phase[0][0],phase[0][1],radius[0],deltah[0],2);
4029         if (npoints==2){
4030           helixes[i0].ParabolicDCA(helixes[i1],phase[1][0],phase[1][1],radius[1],deltah[1],2);
4031           if (deltah[1]<deltah[0]) ibest=1;
4032         }
4033         deltabest  = TMath::Sqrt(deltah[ibest]);
4034         helixes[i0].Evaluate(phase[ibest][0],xyz0);
4035         helixes[i1].Evaluate(phase[ibest][1],xyz1);
4036         helixes[i0].GetAngle(phase[ibest][0],helixes[i1],phase[ibest][1],hangles);
4037         Double_t radiusbest = TMath::Sqrt(radius[ibest]);
4038         //
4039         if (deltabest>6) continue;
4040         if (mindcar+mindcaz<40 && (hangles[2]<3.12||deltabest>3)) continue;
4041         Bool_t sign =kFALSE;
4042         if (hangles[2]>3.06) sign =kTRUE;
4043         //
4044         if (sign){
4045           circular[i0] = kTRUE;
4046           circular[i1] = kTRUE;
4047           if (TMath::Abs(track0->fP4)<TMath::Abs(track1->fP4)){
4048             track0->fCircular += 1;
4049             track1->fCircular += 2;
4050           }
4051           else{
4052             track1->fCircular += 1;
4053             track0->fCircular += 2;
4054           }
4055         }               
4056         if (sign&&0){     
4057           //debug stream
4058           cstream<<"Curling"<<
4059             "lab0="<<track0->fLab<<
4060             "lab1="<<track1->fLab<<   
4061             "Tr0.="<<track0<<
4062             "Tr1.="<<track1<<      
4063             "dca0="<<dca[i0]<<
4064             "dca1="<<dca[i1]<<
4065             "mindcar="<<mindcar<<
4066             "mindcaz="<<mindcaz<<
4067             "delta="<<delta<<
4068             "rmean="<<rmean<<
4069             "npoints="<<npoints<<                      
4070             "hangles0="<<hangles[0]<<
4071             "hangles2="<<hangles[2]<<                    
4072             "xyz0="<<xyz0[2]<<
4073             "xyzz1="<<xyz1[2]<<
4074             "z0="<<z0[i0]<<
4075             "z1="<<z0[i1]<<
4076             "radius="<<radiusbest<<
4077             "deltabest="<<deltabest<< 
4078             "phase0="<<phase[ibest][0]<<
4079             "phase1="<<phase[ibest][1]<<
4080             "\n";                 
4081         }
4082       }
4083     }
4084   }
4085   //
4086   //  Finf kinks loop
4087   // 
4088   //
4089   for (Int_t i =0;i<nentries;i++){
4090     if (sign[i]==0) continue;
4091     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4092     ntracks++;
4093     //
4094     Double_t cradius0 = 40*40;
4095     Double_t cradius1 = 270*270;
4096     Double_t cdist1=8.;
4097     Double_t cdist2=8.;
4098     Double_t cdist3=0.55; 
4099     for (Int_t j =i+1;j<nentries;j++){
4100       nall++;
4101       if (sign[j]*sign[i]<1) continue;
4102       if ( (nclusters[i]+nclusters[j])>200) continue;
4103       if ( (nclusters[i]+nclusters[j])<80) continue;
4104       if ( TMath::Abs(zm[i]-zm[j])>60.) continue;
4105       if ( TMath::Abs(fim[i]-fim[j])>0.6 && TMath::Abs(fim[i]-fim[j])<5.7 ) continue;
4106       //AliTPCseed * track1 = (AliTPCseed*)array->At(j);  Double_t phase[2][2],radius[2];    
4107       Int_t npoints = helixes[i].GetRPHIintersections(helixes[j], phase, radius,20);
4108       if (npoints<1) continue;
4109       // cuts on radius      
4110       if (npoints==1){
4111         if (radius[0]<cradius0||radius[0]>cradius1) continue;
4112       }
4113       else{
4114         if ( (radius[0]<cradius0||radius[0]>cradius1) && (radius[1]<cradius0||radius[1]>cradius1) ) continue;
4115       }
4116       //      
4117       Double_t delta1=10000,delta2=10000;
4118       // cuts on the intersection radius
4119       helixes[i].LinearDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta1);
4120       if (radius[0]<20&&delta1<1) continue; //intersection at vertex
4121       if (radius[0]<10&&delta1<3) continue; //intersection at vertex
4122       if (npoints==2){ 
4123         helixes[i].LinearDCA(helixes[j],phase[1][0],phase[1][1],radius[1],delta2);
4124         if (radius[1]<20&&delta2<1) continue;  //intersection at vertex
4125         if (radius[1]<10&&delta2<3) continue;  //intersection at vertex 
4126       }
4127       //
4128       Double_t distance1 = TMath::Min(delta1,delta2);
4129       if (distance1>cdist1) continue;  // cut on DCA linear approximation
4130       //
4131       npoints = helixes[i].GetRPHIintersections(helixes[j], phase, radius,20);
4132       helixes[i].ParabolicDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta1);
4133       if (radius[0]<20&&delta1<1) continue; //intersection at vertex
4134       if (radius[0]<10&&delta1<3) continue; //intersection at vertex
4135       //
4136       if (npoints==2){ 
4137         helixes[i].ParabolicDCA(helixes[j],phase[1][0],phase[1][1],radius[1],delta2);   
4138         if (radius[1]<20&&delta2<1) continue;  //intersection at vertex
4139         if (radius[1]<10&&delta2<3) continue;  //intersection at vertex 
4140       }            
4141       distance1 = TMath::Min(delta1,delta2);
4142       Float_t rkink =0;
4143       if (delta1<delta2){
4144         rkink = TMath::Sqrt(radius[0]);
4145       }
4146       else{
4147         rkink = TMath::Sqrt(radius[1]);
4148       }
4149       if (distance1>cdist2) continue;
4150       //
4151       //
4152       AliTPCseed * track1 = (AliTPCseed*)array->At(j);
4153       //
4154       //
4155       Int_t row0 = GetRowNumber(rkink); 
4156       if (row0<10)  continue;
4157       if (row0>150) continue;
4158       //
4159       //
4160       Float_t dens00=-1,dens01=-1;
4161       Float_t dens10=-1,dens11=-1;
4162       //
4163       Int_t found,foundable,shared;
4164       track0->GetClusterStatistic(0,row0-5, found, foundable,shared,kFALSE);
4165       if (foundable>5) dens00 = Float_t(found)/Float_t(foundable);
4166       track0->GetClusterStatistic(row0+5,155, found, foundable,shared,kFALSE);
4167       if (foundable>5) dens01 = Float_t(found)/Float_t(foundable);
4168       //
4169       track1->GetClusterStatistic(0,row0-5, found, foundable,shared,kFALSE);
4170       if (foundable>10) dens10 = Float_t(found)/Float_t(foundable);
4171       track1->GetClusterStatistic(row0+5,155, found, foundable,shared,kFALSE);
4172       if (foundable>10) dens11 = Float_t(found)/Float_t(foundable);
4173       //     
4174       if (dens00<dens10 && dens01<dens11) continue;
4175       if (dens00>dens10 && dens01>dens11) continue;
4176       if (TMath::Max(dens00,dens10)<0.1)  continue;
4177       if (TMath::Max(dens01,dens11)<0.3)  continue;
4178       //
4179       if (TMath::Min(dens00,dens10)>0.6)  continue;
4180       if (TMath::Min(dens01,dens11)>0.6)  continue;
4181
4182       //
4183       AliTPCseed * ktrack0, *ktrack1;
4184       if (dens00>dens10){
4185         ktrack0 = track0;
4186         ktrack1 = track1;
4187       }
4188       else{
4189         ktrack0 = track1;
4190         ktrack1 = track0;
4191       }
4192       if (TMath::Abs(ktrack0->GetC())>5) continue; // cut on the curvature for mother particle
4193       AliExternalTrackParam paramm(*ktrack0);
4194       AliExternalTrackParam paramd(*ktrack1);
4195       if (row0>60&&ktrack1->GetReference().GetX()>90.) new (&paramd) AliExternalTrackParam(ktrack1->GetReference()); 
4196       //
4197       //
4198       kink->SetMother(paramm);
4199       kink->SetDaughter(paramd);
4200       kink->Update();
4201
4202       Float_t x[3] = { kink->GetPosition()[0],kink->GetPosition()[1],kink->GetPosition()[2]};
4203       Int_t index[4];
4204       fParam->Transform0to1(x,index);
4205       fParam->Transform1to2(x,index);
4206       row0 = GetRowNumber(x[0]); 
4207
4208       if (kink->GetR()<100) continue;
4209       if (kink->GetR()>240) continue;
4210       if (kink->GetPosition()[2]/kink->GetR()>AliTPCReconstructor::GetCtgRange()) continue;  //out of fiducial volume
4211       if (kink->GetDistance()>cdist3) continue;
4212       Float_t dird = kink->GetDaughterP()[0]*kink->GetPosition()[0]+kink->GetDaughterP()[1]*kink->GetPosition()[1];  // rough direction estimate
4213       if (dird<0) continue;
4214
4215       Float_t dirm = kink->GetMotherP()[0]*kink->GetPosition()[0]+kink->GetMotherP()[1]*kink->GetPosition()[1];  // rough direction estimate
4216       if (dirm<0) continue;
4217       Float_t mpt = TMath::Sqrt(kink->GetMotherP()[0]*kink->GetMotherP()[0]+kink->GetMotherP()[1]*kink->GetMotherP()[1]);
4218       if (mpt<0.2) continue;
4219
4220       if (mpt<1){
4221         //for high momenta momentum not defined well in first iteration
4222         Double_t qt   =  TMath::Sin(kink->GetAngle(2))*ktrack1->P();
4223         if (qt>0.35) continue; 
4224       }
4225       
4226       kink->SetLabel(CookLabel(ktrack0,0.4,0,row0),0);
4227       kink->SetLabel(CookLabel(ktrack1,0.4,row0,160),1);
4228       if (dens00>dens10){
4229         kink->SetTPCDensity(dens00,0,0);
4230         kink->SetTPCDensity(dens01,0,1);
4231         kink->SetTPCDensity(dens10,1,0);
4232         kink->SetTPCDensity(dens11,1,1);
4233         kink->SetIndex(i,0);
4234         kink->SetIndex(j,1);
4235       }
4236       else{
4237         kink->SetTPCDensity(dens10,0,0);
4238         kink->SetTPCDensity(dens11,0,1);
4239         kink->SetTPCDensity(dens00,1,0);
4240         kink->SetTPCDensity(dens01,1,1);
4241         kink->SetIndex(j,0);
4242         kink->SetIndex(i,1);
4243       }
4244
4245       if (mpt<1||kink->GetAngle(2)>0.1){
4246         //      angle and densities  not defined yet
4247         if (kink->GetTPCDensityFactor()<0.8) continue;
4248         if ((2-kink->GetTPCDensityFactor())*kink->GetDistance() >0.25) continue;
4249         if (kink->GetAngle(2)*ktrack0->P()<0.003) continue; //too small angle
4250         if (kink->GetAngle(2)>0.2&&kink->GetTPCDensityFactor()<1.15) continue;
4251         if (kink->GetAngle(2)>0.2&&kink->GetTPCDensity(0,1)>0.05) continue;
4252
4253         Float_t criticalangle = track0->fC22+track0->fC33;
4254         criticalangle+= track1->fC22+track1->fC33;
4255         criticalangle= 3*TMath::Sqrt(criticalangle);
4256         if (criticalangle>0.02) criticalangle=0.02;
4257         if (kink->GetAngle(2)<criticalangle) continue;
4258       }
4259       //
4260       Int_t drow = Int_t(2.+0.5/(0.05+kink->GetAngle(2)));  // overlap region defined
4261       Float_t shapesum =0;
4262       Float_t sum = 0;
4263       for ( Int_t row = row0-drow; row<row0+drow;row++){
4264         if (row<0) continue;
4265         if (row>155) continue;
4266         if (ktrack0->fClusterPointer[row]){
4267           AliTPCTrackerPoint *point =ktrack0->GetTrackPoint(row);
4268           shapesum+=point->GetSigmaY()+point->GetSigmaZ();
4269           sum++;
4270         }
4271         if (ktrack1->fClusterPointer[row]){
4272           AliTPCTrackerPoint *point =ktrack1->GetTrackPoint(row);
4273           shapesum+=point->GetSigmaY()+point->GetSigmaZ();
4274           sum++;
4275         }       
4276       }
4277       if (sum<4){
4278         kink->SetShapeFactor(-1.);
4279       }
4280       else{
4281         kink->SetShapeFactor(shapesum/sum);
4282       }      
4283       //      esd->AddKink(kink);
4284       kinks->AddLast(kink);
4285       kink = new AliESDkink;
4286       ncandidates++;
4287     }
4288   }
4289   //
4290   // sort the kinks according quality - and refit them towards vertex
4291   //
4292   Int_t       nkinks    = kinks->GetEntriesFast();
4293   Float_t    *quality   = new Float_t[nkinks];
4294   Int_t      *indexes   = new Int_t[nkinks];
4295   AliTPCseed *mothers   = new AliTPCseed[nkinks];
4296   AliTPCseed *daughters = new AliTPCseed[nkinks];
4297   //
4298   //
4299   for (Int_t i=0;i<nkinks;i++){
4300     quality[i] =100000;
4301     AliESDkink *kink = (AliESDkink*)kinks->At(i);
4302     //
4303     // refit kinks towards vertex
4304     // 
4305     Int_t index0 = kink->GetIndex(0);
4306     Int_t index1 = kink->GetIndex(1);
4307     AliTPCseed * ktrack0 = (AliTPCseed*)array->At(index0);
4308     AliTPCseed * ktrack1 = (AliTPCseed*)array->At(index1);
4309     //
4310     Int_t sumn=ktrack0->fN+ktrack1->fN;
4311     //
4312     // Refit Kink under if too small angle
4313     //
4314     if (kink->GetAngle(2)<0.05){
4315       kink->SetTPCRow0(GetRowNumber(kink->GetR()));
4316       Int_t row0 = kink->GetTPCRow0();
4317       Int_t drow = Int_t(2.+0.5/(0.05+kink->GetAngle(2)));
4318       //
4319       //
4320       Int_t last  = row0-drow;
4321       if (last<40) last=40;
4322       if (last<ktrack0->fFirstPoint+25) last = ktrack0->fFirstPoint+25;
4323       AliTPCseed* seed0 = ReSeed(ktrack0,last,kFALSE);
4324       //
4325       //
4326       Int_t first = row0+drow;
4327       if (first>130) first=130;
4328       if (first>ktrack1->fLastPoint-25) first = TMath::Max(ktrack1->fLastPoint-25,30);
4329       AliTPCseed* seed1 = ReSeed(ktrack1,first,kTRUE);
4330       //
4331       if (seed0 && seed1){
4332         kink->SetStatus(1,8);
4333         if (RefitKink(*seed0,*seed1,*kink)) kink->SetStatus(1,9);
4334         row0 = GetRowNumber(kink->GetR());
4335         sumn = seed0->fN+seed1->fN;
4336         new (&mothers[i])   AliTPCseed(*seed0);
4337         new (&daughters[i]) AliTPCseed(*seed1); 
4338       }
4339       else{
4340         delete kinks->RemoveAt(i);
4341         if (seed0) delete seed0;
4342         if (seed1) delete seed1;
4343         continue;
4344       }
4345       if (kink->GetDistance()>0.5 || kink->GetR()<110 || kink->GetR()>240) {
4346         delete kinks->RemoveAt(i);
4347         if (seed0) delete seed0;
4348         if (seed1) delete seed1;
4349         continue;
4350       }
4351       //
4352       delete seed0;
4353       delete seed1;            
4354     }
4355     //
4356     if (kink) quality[i] = 160*((0.1+kink->GetDistance())*(2.-kink->GetTPCDensityFactor()))/(sumn+40.);  //the longest -clossest will win
4357   }
4358   TMath::Sort(nkinks,quality,indexes,kFALSE);
4359   //
4360   //remove double find kinks
4361   //
4362   for (Int_t ikink0=1;ikink0<nkinks;ikink0++){
4363     AliESDkink * kink0 = (AliESDkink*) kinks->At(indexes[ikink0]);
4364     if (!kink0) continue;
4365     //
4366     for (Int_t ikink1=0;ikink1<ikink0;ikink1++){
4367       if (!kink0) continue;
4368       AliESDkink * kink1 = (AliESDkink*) kinks->At(indexes[ikink1]);
4369       if (!kink1) continue;
4370       // if not close kink continue
4371       if (TMath::Abs(kink1->GetPosition()[2]-kink0->GetPosition()[2])>10) continue;
4372       if (TMath::Abs(kink1->GetPosition()[1]-kink0->GetPosition()[1])>10) continue;
4373       if (TMath::Abs(kink1->GetPosition()[0]-kink0->GetPosition()[0])>10) continue;
4374       //
4375       AliTPCseed &mother0   = mothers[indexes[ikink0]];
4376       AliTPCseed &daughter0 = daughters[indexes[ikink0]];
4377       AliTPCseed &mother1   = mothers[indexes[ikink1]];
4378       AliTPCseed &daughter1 = daughters[indexes[ikink1]];
4379       Int_t row0 = (kink0->GetTPCRow0()+kink1->GetTPCRow0())/2;
4380       //
4381       Int_t same  = 0;
4382       Int_t both  = 0;
4383       Int_t samem = 0;
4384       Int_t bothm = 0;
4385       Int_t samed = 0;
4386       Int_t bothd = 0;
4387       //
4388       for (Int_t i=0;i<row0;i++){
4389         if (mother0.fIndex[i]>0 && mother1.fIndex[i]>0){
4390           both++;
4391           bothm++;
4392           if (mother0.fIndex[i]==mother1.fIndex[i]){
4393             same++;
4394             samem++;
4395           }
4396         }
4397       }
4398
4399       for (Int_t i=row0;i<158;i++){
4400         if (daughter0.fIndex[i]>0 && daughter0.fIndex[i]>0){
4401           both++;
4402           bothd++;
4403           if (mother0.fIndex[i]==mother1.fIndex[i]){
4404             same++;
4405             samed++;
4406           }
4407         }
4408       }
4409       Float_t ratio = Float_t(same+1)/Float_t(both+1);
4410       Float_t ratiom = Float_t(samem+1)/Float_t(bothm+1);
4411       Float_t ratiod = Float_t(samed+1)/Float_t(bothd+1);
4412       if (ratio>0.3 && ratiom>0.5 &&ratiod>0.5) {
4413         Int_t sum0 = mother0.fN+daughter0.fN;
4414         Int_t sum1 = mother1.fN+daughter1.fN;
4415         if (sum1>sum0){
4416           shared[kink0->GetIndex(0)]= kTRUE;
4417           shared[kink0->GetIndex(1)]= kTRUE;      
4418           delete kinks->RemoveAt(indexes[ikink0]);
4419         }
4420         else{
4421           shared[kink1->GetIndex(0)]= kTRUE;
4422           shared[kink1->GetIndex(1)]= kTRUE;      
4423           delete kinks->RemoveAt(indexes[ikink1]);
4424         }
4425       }
4426     }
4427   }
4428
4429
4430   for (Int_t i=0;i<nkinks;i++){
4431     AliESDkink * kink = (AliESDkink*) kinks->At(indexes[i]);
4432     if (!kink) continue;
4433     kink->SetTPCRow0(GetRowNumber(kink->GetR()));
4434     Int_t index0 = kink->GetIndex(0);
4435     Int_t index1 = kink->GetIndex(1);
4436     if (circular[index0]||circular[index1]&&kink->GetDistance()>0.2) continue;
4437     kink->SetMultiple(usage[index0],0);
4438     kink->SetMultiple(usage[index1],1);
4439     if (kink->GetMultiple()[0]+kink->GetMultiple()[1]>2) continue;
4440     if (kink->GetMultiple()[0]+kink->GetMultiple()[1]>0 && quality[indexes[i]]>0.2) continue;
4441     if (kink->GetMultiple()[0]+kink->GetMultiple()[1]>0 && kink->GetDistance()>0.2) continue;
4442     if (circular[index0]||circular[index1]&&kink->GetDistance()>0.1) continue;
4443
4444     AliTPCseed * ktrack0 = (AliTPCseed*)array->At(index0);
4445     AliTPCseed * ktrack1 = (AliTPCseed*)array->At(index1);
4446     if (!ktrack0 || !ktrack1) continue;
4447     Int_t index = esd->AddKink(kink);
4448     //
4449     //
4450     if ( ktrack0->fKinkIndexes[0]==0 && ktrack1->fKinkIndexes[0]==0) {  //best kink
4451       if (mothers[indexes[i]].fN>20 && daughters[indexes[i]].fN>20 && (mothers[indexes[i]].fN+daughters[indexes[i]].fN)>100){
4452         new (ktrack0) AliTPCseed(mothers[indexes[i]]);
4453         new (ktrack1) AliTPCseed(daughters[indexes[i]]);
4454       }
4455     }
4456     //
4457     ktrack0->fKinkIndexes[usage[index0]] = -(index+1);
4458     ktrack1->fKinkIndexes[usage[index1]] =  (index+1);
4459     usage[index0]++;
4460     usage[index1]++;
4461   }
4462   //
4463   // Remove tracks corresponding to shared kink's
4464   //
4465   for (Int_t i=0;i<nentries;i++){
4466     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4467     if (!track0) continue;
4468     if (track0->fKinkIndexes[0]!=0) continue;
4469     if (shared[i]) delete array->RemoveAt(i);
4470   }
4471
4472   //
4473   //
4474   RemoveUsed2(array,0.5,0.4,30);
4475   UnsignClusters();
4476   for (Int_t i=0;i<nentries;i++){
4477     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4478     if (!track0) continue;
4479     track0->CookdEdx(0.02,0.6);
4480     track0->CookPID();
4481   }
4482   //
4483   for (Int_t i=0;i<nentries;i++){
4484     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4485     if (!track0) continue;
4486     if (track0->Pt()<1.4) continue;
4487     //remove double high momenta tracks - overlapped with kink candidates
4488     Int_t shared=0;
4489     Int_t all   =0;
4490     for (Int_t icl=track0->fFirstPoint;icl<track0->fLastPoint; icl++){
4491       if (track0->fClusterPointer[icl]!=0){
4492         all++;
4493         if (track0->fClusterPointer[icl]->IsUsed(10)) shared++;
4494       }
4495     }
4496     if (Float_t(shared+1)/Float_t(nall+1)>0.5) {
4497       delete array->RemoveAt(i);
4498     }
4499     //
4500     if (track0->fKinkIndexes[0]!=0) continue;
4501     if (track0->GetNumberOfClusters()<80) continue;
4502
4503     AliTPCseed *pmother = new AliTPCseed();
4504     AliTPCseed *pdaughter = new AliTPCseed();
4505     AliESDkink *pkink = new AliESDkink;
4506
4507     AliTPCseed & mother = *pmother;
4508     AliTPCseed & daughter = *pdaughter;
4509     AliESDkink & kink = *pkink;
4510     if (CheckKinkPoint(track0,mother,daughter, kink)){
4511       if (mother.fN<30||daughter.fN<20) {
4512         delete pmother;
4513         delete pdaughter;
4514         delete pkink;
4515         continue;  //too short tracks
4516       }
4517       if (mother.Pt()<1.4) {
4518         delete pmother;
4519         delete pdaughter;
4520         delete pkink;
4521         continue;
4522       }
4523       Int_t row0= kink.GetTPCRow0();
4524       if (kink.GetDistance()>0.5 || kink.GetR()<110. || kink.GetR()>240.) {
4525         delete pmother;
4526         delete pdaughter;
4527         delete pkink;
4528         continue;
4529       }
4530       //
4531       Int_t index = esd->AddKink(&kink);      
4532       mother.fKinkIndexes[0] = -(index+1);
4533       daughter.fKinkIndexes[0] = index+1;
4534       if (mother.fN>50) {
4535         delete array->RemoveAt(i);
4536         array->AddAt(new AliTPCseed(mother),i);
4537       }
4538       else{
4539         array->AddLast(new AliTPCseed(mother));
4540       }
4541       array->AddLast(new AliTPCseed(daughter));      
4542       for (Int_t icl=0;icl<row0;icl++) {
4543         if (mother.fClusterPointer[icl]) mother.fClusterPointer[icl]->Use(20);
4544       }
4545       //
4546       for (Int_t icl=row0;icl<158;icl++) {
4547         if (daughter.fClusterPointer[icl]) daughter.fClusterPointer[icl]->Use(20);
4548       }
4549       //
4550     }
4551     delete pmother;
4552     delete pdaughter;
4553     delete pkink;
4554   }
4555
4556   delete [] daughters;
4557   delete [] mothers;
4558   //
4559   //
4560   delete [] dca;
4561   delete []circular;
4562   delete []shared;
4563   delete []quality;
4564   delete []indexes;
4565   //
4566   delete kink;
4567   delete[]fim;
4568   delete[] zm;
4569   delete[] z0;
4570   delete [] usage;
4571   delete[] alpha;
4572   delete[] nclusters;
4573   delete[] sign;
4574   delete[] helixes;
4575   kinks->Delete();
4576   delete kinks;
4577
4578   printf("Ncandidates=\t%d\t%d\t%d\t%d\n",esd->GetNumberOfKinks(),ncandidates,ntracks,nall);
4579   timer.Print();
4580 }
4581
4582 void  AliTPCtrackerMI::FindV0s(TObjArray * array, AliESD *esd)
4583 {
4584   //
4585   //  find V0s
4586   //
4587   //
4588   TObjArray *tpcv0s      = new TObjArray(100000);
4589   Int_t     nentries     = array->GetEntriesFast();
4590   AliHelix *helixes      = new AliHelix[nentries];
4591   Int_t    *sign         = new Int_t[nentries];
4592   Float_t  *alpha        = new Float_t[nentries];
4593   Float_t  *z0           = new Float_t[nentries]; 
4594   Float_t  *dca          = new Float_t[nentries];
4595   Float_t  *sdcar        = new Float_t[nentries];
4596   Float_t  *cdcar        = new Float_t[nentries];
4597   Float_t  *pulldcar     = new Float_t[nentries];
4598   Float_t  *pulldcaz     = new Float_t[nentries];
4599   Float_t  *pulldca      = new Float_t[nentries];
4600   Bool_t   *isPrim       = new Bool_t[nentries];  
4601   const AliESDVertex * primvertex = esd->GetVertex();
4602   Double_t             zvertex = primvertex->GetZv(); 
4603   //
4604   //  nentries = array->GetEntriesFast();
4605   //
4606   for (Int_t i=0;i<nentries;i++){
4607     sign[i]=0;
4608     isPrim[i]=0;
4609     AliTPCseed* track = (AliTPCseed*)array->At(i);    
4610     if (!track) continue;
4611     track->GetV0Indexes()[0] = 0;  //rest v0 indexes
4612     track->GetV0Indexes()[1] = 0;  //rest v0 indexes
4613     track->GetV0Indexes()[2] = 0;  //rest v0 indexes
4614     //
4615     alpha[i] = track->GetAlpha();
4616     new (&helixes[i]) AliHelix(*track);
4617     Double_t xyz[3];
4618     helixes[i].Evaluate(0,xyz);
4619     sign[i] = (track->GetC()>0) ? -1:1;
4620     Double_t x,y,z;
4621     x=160;
4622     z0[i]=1000;
4623     if (track->GetProlongation(0,y,z))  z0[i] = z;
4624     dca[i] = track->GetD(0,0);
4625     // 
4626     // dca error parrameterezation + pulls
4627     //
4628     sdcar[i]      = TMath::Sqrt(0.150*0.150+(100*track->fP4)*(100*track->fP4));
4629     if (TMath::Abs(track->fP3)>1) sdcar[i]*=2.5;
4630     cdcar[i]      = TMath::Exp((TMath::Abs(track->fP4)-0.0106)*525.3);
4631     pulldcar[i]   = (dca[i]-cdcar[i])/sdcar[i];
4632     pulldcaz[i]   = (z0[i]-zvertex)/sdcar[i];
4633     pulldca[i]    = TMath::Sqrt(pulldcar[i]*pulldcar[i]+pulldcaz[i]*pulldcaz[i]);
4634     if (track->fTPCr[1]+track->fTPCr[2]+track->fTPCr[3]>0.5) {
4635       if (pulldca[i]<3.) isPrim[i]=kTRUE;  //pion, muon and Kaon  3 sigma cut
4636     }
4637     if (track->fTPCr[4]>0.5) {
4638       if (pulldca[i]<0.5) isPrim[i]=kTRUE;  //proton 0.5 sigma cut
4639     }
4640     if (track->fTPCr[0]>0.4) {
4641       isPrim[i]=kFALSE;  //electron no  sigma cut
4642     }
4643   }
4644   //
4645   //
4646   TStopwatch timer;
4647   timer.Start();
4648   Int_t ncandidates =0;
4649   Int_t nall =0;
4650   Int_t ntracks=0; 
4651   Double_t phase[2][2],radius[2];
4652   //
4653   //  Finf V0s loop
4654   // 
4655   //
4656   // //  
4657   TTreeSRedirector &cstream = *fDebugStreamer; 
4658   Float_t fprimvertex[3]={GetX(),GetY(),GetZ()};
4659   AliESDV0MI vertex; 
4660   Double_t cradius0 = 10*10;
4661   Double_t cradius1 = 200*200;
4662   Double_t cdist1=3.;
4663   Double_t cdist2=4.;
4664   Double_t cpointAngle = 0.95;
4665   //
4666   Double_t delta[2]={10000,10000};
4667   for (Int_t i =0;i<nentries;i++){
4668     if (sign[i]==0) continue;
4669     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4670     if (!track0) continue;
4671     cstream<<"Tracks"<<
4672       "Tr0.="<<track0<<
4673       "dca="<<dca[i]<<
4674       "z0="<<z0[i]<<
4675       "zvertex="<<zvertex<<
4676       "sdcar0="<<sdcar[i]<<
4677       "cdcar0="<<cdcar[i]<<
4678       "pulldcar0="<<pulldcar[i]<<
4679       "pulldcaz0="<<pulldcaz[i]<<
4680       "pulldca0="<<pulldca[i]<<
4681       "isPrim="<<isPrim[i]<<
4682       "\n";
4683     //
4684     if (track0->fP4<0) continue;
4685     if (track0->GetKinkIndex(0)>0||isPrim[i]) continue;   //daughter kink
4686     //
4687     if (TMath::Abs(helixes[i].GetHelix(4))<0.000000001) continue;
4688     ntracks++;
4689     // debug output
4690     
4691     
4692     for (Int_t j =0;j<nentries;j++){
4693       AliTPCseed * track1 = (AliTPCseed*)array->At(j);
4694       if (!track1) continue;
4695       if (track1->GetKinkIndex(0)>0 || isPrim[j]) continue; //daughter kink
4696       if (sign[j]*sign[i]>0) continue; 
4697       if (TMath::Abs(helixes[j].GetHelix(4))<0.000001) continue;
4698       if (track0->fCircular+track1->fCircular>1) continue;    //circling -returning track
4699       nall++;
4700       //
4701       // DCA to prim vertex cut
4702       //
4703       //
4704       delta[0]=10000;
4705       delta[1]=10000;
4706
4707       Int_t npoints = helixes[i].GetRPHIintersections(helixes[j], phase, radius,cdist2);
4708       if (npoints<1) continue;
4709       Int_t iclosest=0;
4710       // cuts on radius      
4711       if (npoints==1){
4712         if (radius[0]<cradius0||radius[0]>cradius1) continue;
4713         helixes[i].LinearDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta[0]);
4714         if (delta[0]>cdist1) continue;
4715       }
4716       else{
4717         if (TMath::Max(radius[0],radius[1])<cradius0|| TMath::Min(radius[0],radius[1])>cradius1) continue;      
4718         helixes[i].LinearDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta[0]);    
4719         helixes[i].LinearDCA(helixes[j],phase[1][0],phase[1][1],radius[1],delta[1]);
4720         if (delta[1]<delta[0]) iclosest=1;
4721         if (delta[iclosest]>cdist1) continue;
4722       }
4723       helixes[i].ParabolicDCA(helixes[j],phase[iclosest][0],phase[iclosest][1],radius[iclosest],delta[iclosest]);
4724       if (radius[iclosest]<cradius0 || radius[iclosest]>cradius1 || delta[iclosest]>cdist1) continue;
4725       //
4726       Double_t pointAngle = helixes[i].GetPointAngle(helixes[j],phase[iclosest],fprimvertex);
4727       if (pointAngle<cpointAngle) continue;
4728       //
4729       Bool_t isGamma = kFALSE;
4730       vertex.SetP(*track0); //track0 - plus
4731       vertex.SetM(*track1); //track1 - minus
4732       vertex.Update(fprimvertex);
4733       if (track0->fTPCr[0]>0.3&&track1->fTPCr[0]>0.3&&vertex.GetAnglep()[2]<0.15) isGamma=kTRUE;              // gamma conversion candidate
4734       Double_t pointAngle2 = vertex.GetPointAngle();
4735       //continue;
4736       if (vertex.GetPointAngle()<cpointAngle && (!isGamma)) continue; // point angle cut
4737       if (vertex.GetDist2()>2&&(!isGamma)) continue;         // point angle cut
4738       Float_t sigmae     = 0.15*0.15;
4739       if (vertex.GetRr()<80) 
4740         sigmae += (sdcar[i]*sdcar[i]+sdcar[j]*sdcar[j])*(1.-vertex.GetRr()/80.)*(1.-vertex.GetRr()/80.);
4741       sigmae+= TMath::Sqrt(sigmae);
4742       if (vertex.GetDist2()/sigmae>3.&&(!isGamma)) continue; 
4743       Float_t densb0=0,densb1=0,densa0=0,densa1=0;
4744       Int_t row0 = GetRowNumber(vertex.GetRr());
4745       if (row0>15){
4746         if (vertex.GetDist2()>0.2) continue;             
4747         densb0     = track0->Density2(0,row0-5);          
4748         densb1     = track1->Density2(0,row0-5);         
4749         if (densb0>0.3|| densb1>0.3) continue;            //clusters before vertex
4750         densa0     = track0->Density2(row0+5,row0+40);    
4751         densa1     = track1->Density2(row0+5,row0+40);    
4752         if ((densa0<0.4|| densa1<0.4)&&(!isGamma)) continue;            //missing clusters after vertex
4753       }
4754       else{
4755         densa0     = track0->Density2(0,40);  //cluster density
4756         densa1     = track1->Density2(0,40);  //cluster density
4757         if ((vertex.GetRr()<80&&densa0+densa1<1.)&&(!isGamma)) continue;
4758       }
4759       vertex.SetLab(0,track0->GetLabel());
4760       vertex.SetLab(1,track1->GetLabel());
4761       vertex.SetChi2After((densa0+densa1)*0.5);
4762       vertex.SetChi2Before((densb0+densb1)*0.5);
4763       vertex.SetIndex(0,i);
4764       vertex.SetIndex(1,j);
4765       vertex.SetStatus(1); // TPC v0 candidate
4766       vertex.SetRp(track0->fTPCr);
4767       vertex.SetRm(track1->fTPCr);
4768       tpcv0s->AddLast(new AliESDV0MI(vertex));      
4769       ncandidates++;
4770       {
4771         Int_t eventNr = esd->GetEventNumber();
4772         Double_t radiusm= (delta[0]<delta[1])? TMath::Sqrt(radius[0]):TMath::Sqrt(radius[1]);  
4773         Double_t deltam= (delta[0]<delta[1])? TMath::Sqrt(delta[0]):TMath::Sqrt(delta[1]);  
4774         cstream<<"V0"<<
4775           "Event="<<eventNr<<
4776           "vertex.="<<&vertex<<
4777           "Tr0.="<<track0<<
4778           "lab0="<<track0->fLab<<
4779           "Helix0.="<<&helixes[i]<<     
4780           "Tr1.="<<track1<<
4781           "lab1="<<track1->fLab<<
4782           "Helix1.="<<&helixes[j]<<
4783           "pointAngle="<<pointAngle<<
4784           "pointAngle2="<<pointAngle2<<
4785           "dca0="<<dca[i]<<
4786           "dca1="<<dca[j]<<
4787           "z0="<<z0[i]<<
4788           "z1="<<z0[j]<<
4789           "zvertex="<<zvertex<<
4790           "circular0="<<track0->fCircular<<
4791           "circular1="<<track1->fCircular<<
4792           "npoints="<<npoints<<
4793           "radius0="<<radius[0]<<
4794           "delta0="<<delta[0]<<
4795           "radius1="<<radius[1]<<
4796           "delta1="<<delta[1]<<
4797           "radiusm="<<radiusm<<
4798           "deltam="<<deltam<<
4799           "sdcar0="<<sdcar[i]<<
4800           "sdcar1="<<sdcar[j]<<
4801           "cdcar0="<<cdcar[i]<<
4802           "cdcar1="<<cdcar[j]<<
4803           "pulldcar0="<<pulldcar[i]<<
4804           "pulldcar1="<<pulldcar[j]<<
4805           "pulldcaz0="<<pulldcaz[i]<<
4806           "pulldcaz1="<<pulldcaz[j]<<
4807           "pulldca0="<<pulldca[i]<<
4808           "pulldca1="<<pulldca[j]<<
4809           "densb0="<<densb0<<
4810           "densb1="<<densb1<<
4811           "densa0="<<densa0<<
4812           "densa1="<<densa1<<
4813           "sigmae="<<sigmae<<
4814           "\n";
4815       }
4816     }
4817   }    
4818   Float_t *quality = new Float_t[ncandidates];
4819   Int_t *indexes = new Int_t[ncandidates];
4820   Int_t naccepted =0;
4821   for (Int_t i=0;i<ncandidates;i++){
4822     quality[i]     = 0; 
4823     AliESDV0MI *v0 = (AliESDV0MI*)tpcv0s->At(i);
4824     quality[i]     = 1./(1.00001-v0->GetPointAngle());   //base point angle
4825     // quality[i]    /= (0.5+v0->GetDist2());  
4826     // quality[i]    *= v0->GetChi2After();               //density factor
4827     Double_t minpulldca = TMath::Min(2.+pulldca[v0->GetIndex(0)],(2.+pulldca[v0->GetIndex(1)]) );     //pull
4828     Int_t index0 = v0->GetIndex(0);
4829     Int_t index1 = v0->GetIndex(1);
4830     AliTPCseed * track0 = (AliTPCseed*)array->At(index0);
4831     AliTPCseed * track1 = (AliTPCseed*)array->At(index1);
4832     if (track0->fTPCr[0]>0.3&&track1->fTPCr[0]>0.3&&v0->GetAnglep()[2]<0.15) quality[i]+=1000000;              // gamma conversion candidate
4833     if (track0->fTPCr[4]>0.9||track1->fTPCr[4]>0.9&&minpulldca>4) quality[i]*=10;    // lambda candidate candidate
4834   }
4835
4836   TMath::Sort(ncandidates,quality,indexes,kTRUE);
4837   //
4838   //
4839   for (Int_t i=0;i<ncandidates;i++){
4840     AliESDV0MI * v0 = (AliESDV0MI*)tpcv0s->At(indexes[i]);
4841     if (!v0) continue;
4842     Int_t index0 = v0->GetIndex(0);
4843     Int_t index1 = v0->GetIndex(1);
4844     AliTPCseed * track0 = (AliTPCseed*)array->At(index0);
4845     AliTPCseed * track1 = (AliTPCseed*)array->At(index1);
4846     if (!track0||!track1) {
4847       printf("Bug\n");
4848       continue;
4849     }
4850     Bool_t accept =kTRUE;  //default accept
4851     Int_t *v0indexes0 = track0->GetV0Indexes();
4852     Int_t *v0indexes1 = track1->GetV0Indexes();
4853     //
4854     Int_t order0 = (v0indexes0[0]!=0) ? 1:0;
4855     Int_t order1 = (v0indexes1[0]!=0) ? 1:0;    
4856     if (v0indexes0[1]!=0) order0 =2;
4857     if (v0indexes1[1]!=0) order1 =2;      
4858     //
4859     if (v0indexes0[2]!=0) {order0=3; accept=kFALSE;}
4860     if (v0indexes0[2]!=0) {order1=3; accept=kFALSE;}
4861     //
4862     AliESDV0MI * v02 = v0;
4863     if (accept){
4864       v0->SetOrder(0,order0);
4865       v0->SetOrder(1,order1);
4866       v0->SetOrder(1,order0+order1);     
4867       Int_t index = esd->AddV0MI(v0);
4868       v02 = esd->GetV0MI(index);
4869       v0indexes0[order0]=index;
4870       v0indexes1[order1]=index;
4871       naccepted++;
4872     }
4873     {
4874       Int_t eventNr = esd->GetEventNumber();
4875       cstream<<"V02"<<
4876         "Event="<<eventNr<<
4877         "vertex.="<<v0<<        
4878         "vertex2.="<<v02<<
4879         "Tr0.="<<track0<<
4880         "lab0="<<track0->fLab<<
4881         "Tr1.="<<track1<<
4882         "lab1="<<track1->fLab<<
4883         "dca0="<<dca[index0]<<
4884         "dca1="<<dca[index1]<<
4885         "order0="<<order0<<
4886         "order1="<<order1<<
4887         "accept="<<accept<<
4888         "quality="<<quality[i]<<
4889         "pulldca0="<<pulldca[index0]<<
4890         "pulldca1="<<pulldca[index1]<<
4891         "index="<<i<<
4892         "\n";
4893     }
4894   }    
4895
4896
4897   //
4898   //
4899   delete []quality;
4900   delete []indexes;
4901 //
4902   delete [] isPrim;
4903   delete [] pulldca;
4904   delete [] pulldcaz;
4905   delete [] pulldcar;
4906   delete [] cdcar;
4907   delete [] sdcar;
4908   delete [] dca;
4909   //
4910   delete[] z0;
4911   delete[] alpha;
4912   delete[] sign;
4913   delete[] helixes;
4914   printf("TPC V0 finder : naccepted\t%d\tncandidates\t%d\tntracks\t%d\tnall\t%d\n",naccepted,ncandidates,ntracks,nall);
4915   timer.Print();
4916 }
4917
4918 Int_t AliTPCtrackerMI::RefitKink(AliTPCseed &mother, AliTPCseed &daughter, AliESDkink &kink)
4919 {
4920   //
4921   // refit kink towards to the vertex
4922   //
4923   //
4924   Int_t row0 = GetRowNumber(kink.GetR());
4925   FollowProlongation(mother,0);
4926   mother.Reset(kFALSE);
4927   //
4928   FollowProlongation(daughter,row0);
4929   daughter.Reset(kFALSE);
4930   FollowBackProlongation(daughter,158);
4931   daughter.Reset(kFALSE);
4932   Int_t first = TMath::Max(row0-20,30); 
4933   Int_t last  = TMath::Min(row0+20,140);
4934   //
4935   const Int_t kNdiv =5;
4936   AliTPCseed  param0[kNdiv];  // parameters along the track
4937   AliTPCseed  param1[kNdiv];  // parameters along the track
4938   AliESDkink   kinks[kNdiv];   // corresponding kink parameters
4939   //
4940   Int_t rows[kNdiv];
4941   for (Int_t irow=0; irow<kNdiv;irow++){
4942     rows[irow] = first +((last-first)*irow)/(kNdiv-1);
4943   }
4944   // store parameters along the track
4945   //
4946   for (Int_t irow=0;irow<kNdiv;irow++){
4947     FollowBackProlongation(mother, rows[irow]);
4948     FollowProlongation(daughter,rows[kNdiv-1-irow]);       
4949     new(&param0[irow])     AliTPCseed(mother);
4950     new(&param1[kNdiv-1-irow])   AliTPCseed(daughter);
4951   }
4952   //
4953   // define kinks 
4954   for (Int_t irow=0; irow<kNdiv-1;irow++){
4955     if (param0[irow].fN<kNdiv||param1[irow].fN<kNdiv) continue;
4956     kinks[irow].SetMother(param0[irow]);
4957     kinks[irow].SetDaughter(param1[irow]);
4958     kinks[irow].Update();
4959   }
4960   //
4961   // choose kink with best "quality"
4962   Int_t index =-1;
4963   Double_t mindist = 10000;
4964   for (Int_t irow=0;irow<kNdiv;irow++){
4965     if (param0[irow].fN<20||param1[irow].fN<20) continue;
4966     if (TMath::Abs(kinks[irow].GetR())>240.) continue;
4967     if (TMath::Abs(kinks[irow].GetR())<100.) continue;
4968     //
4969     Float_t normdist = TMath::Abs(param0[irow].fX-kinks[irow].GetR())*(0.1+kink.GetDistance());
4970     normdist/= (param0[irow].fN+param1[irow].fN+40.);
4971     if (normdist < mindist){
4972       mindist = normdist;
4973       index = irow;
4974     }
4975   }
4976   //
4977   if (index==-1) return 0;
4978   //
4979   //
4980   param0[index].Reset(kTRUE);
4981   FollowProlongation(param0[index],0);
4982   //
4983   new (&mother) AliTPCseed(param0[index]);
4984   new (&daughter) AliTPCseed(param1[index]);  // daughter in vertex
4985   //
4986   kink.SetMother(mother);
4987   kink.SetDaughter(daughter);
4988   kink.Update();
4989   kink.SetTPCRow0(GetRowNumber(kink.GetR()));
4990   kink.SetTPCncls(param0[index].fN,0);
4991   kink.SetTPCncls(param1[index].fN,1);
4992   kink.SetLabel(CookLabel(&mother,0.4, 0,kink.GetTPCRow0()),0);
4993   kink.SetLabel(CookLabel(&daughter,0.4, kink.GetTPCRow0(),160),1);
4994   mother.SetLabel(kink.GetLabel(0));
4995   daughter.SetLabel(kink.GetLabel(1));
4996
4997   return 1;
4998 }
4999
5000
5001 void AliTPCtrackerMI::UpdateKinkQualityM(AliTPCseed * seed){
5002   //
5003   // update Kink quality information for mother after back propagation
5004   //
5005   if (seed->GetKinkIndex(0)>=0) return; 
5006   for (Int_t ikink=0;ikink<3;ikink++){
5007     Int_t index = seed->GetKinkIndex(ikink);
5008     if (index>=0) break;
5009     index = TMath::Abs(index)-1;
5010     AliESDkink * kink = fEvent->GetKink(index);
5011     //kink->fTPCdensity2[0][0]=-1;
5012     //kink->fTPCdensity2[0][1]=-1;
5013     kink->SetTPCDensity2(-1,0,0);
5014     kink->SetTPCDensity2(1,0,1);
5015     //
5016     Int_t row0 = kink->GetTPCRow0() - 2 - Int_t( 0.5/ (0.05+kink->GetAngle(2)));
5017     if (row0<15) row0=15;
5018     //
5019     Int_t row1 = kink->GetTPCRow0() + 2 +  Int_t( 0.5/ (0.05+kink->GetAngle(2)));
5020     if (row1>145) row1=145;
5021     //
5022     Int_t found,foundable,shared;
5023     seed->GetClusterStatistic(0,row0, found, foundable,shared,kFALSE);
5024     if (foundable>5)   kink->SetTPCDensity2(Float_t(found)/Float_t(foundable),0,0);
5025     seed->GetClusterStatistic(row1,155, found, foundable,shared,kFALSE);
5026     if (foundable>5)   kink->SetTPCDensity2(Float_t(found)/Float_t(foundable),0,1);
5027   }
5028     
5029 }
5030
5031 void AliTPCtrackerMI::UpdateKinkQualityD(AliTPCseed * seed){
5032   //
5033   // update Kink quality information for daughter after refit
5034   //
5035   if (seed->GetKinkIndex(0)<=0) return; 
5036   for (Int_t ikink=0;ikink<3;ikink++){
5037     Int_t index = seed->GetKinkIndex(ikink);
5038     if (index<=0) break;
5039     index = TMath::Abs(index)-1;
5040     AliESDkink * kink = fEvent->GetKink(index);
5041     kink->SetTPCDensity2(-1,1,0);
5042     kink->SetTPCDensity2(-1,1,1);
5043     //
5044     Int_t row0 = kink->GetTPCRow0() -2 - Int_t( 0.5/ (0.05+kink->GetAngle(2)));
5045     if (row0<15) row0=15;
5046     //
5047     Int_t row1 = kink->GetTPCRow0() +2 +  Int_t( 0.5/ (0.05+kink->GetAngle(2)));
5048     if (row1>145) row1=145;
5049     //
5050     Int_t found,foundable,shared;
5051     seed->GetClusterStatistic(0,row0, found, foundable,shared,kFALSE);
5052     if (foundable>5)   kink->SetTPCDensity2(Float_t(found)/Float_t(foundable),1,0);
5053     seed->GetClusterStatistic(row1,155, found, foundable,shared,kFALSE);
5054     if (foundable>5)   kink->SetTPCDensity2(Float_t(found)/Float_t(foundable),1,1);
5055   }
5056     
5057 }
5058
5059
5060 Int_t  AliTPCtrackerMI::CheckKinkPoint(AliTPCseed*seed,AliTPCseed &mother, AliTPCseed &daughter, AliESDkink &kink)
5061 {
5062   //
5063   // check kink point for given track
5064   // if return value=0 kink point not found
5065   // otherwise seed0 correspond to mother particle
5066   //           seed1 correspond to daughter particle
5067   //           kink  parameter of kink point
5068
5069   Int_t middlerow = (seed->fFirstPoint+seed->fLastPoint)/2;
5070   Int_t first = seed->fFirstPoint; 
5071   Int_t last  = seed->fLastPoint;
5072   if (last-first<20) return 0;          // shortest length - 2*30 = 60 pad-rows
5073
5074   
5075   AliTPCseed *seed1 = ReSeed(seed,middlerow+20, kTRUE);  //middle of chamber
5076   if (!seed1) return 0;
5077   FollowProlongation(*seed1,seed->fLastPoint-20);
5078   seed1->Reset(kTRUE);
5079   FollowProlongation(*seed1,158);
5080   seed1->Reset(kTRUE);  
5081   last = seed1->fLastPoint;
5082   //
5083   AliTPCseed *seed0 = new AliTPCseed(*seed);
5084   seed0->Reset(kFALSE);
5085   seed0->Reset();
5086   //
5087   AliTPCseed  param0[20];  // parameters along the track
5088   AliTPCseed  param1[20];  // parameters along the track
5089   AliESDkink            kinks[20];   // corresponding kink parameters
5090   Int_t rows[20];
5091   for (Int_t irow=0; irow<20;irow++){
5092     rows[irow] = first +((last-first)*irow)/19;
5093   }
5094   // store parameters along the track
5095   //
5096   for (Int_t irow=0;irow<20;irow++){
5097     FollowBackProlongation(*seed0, rows[irow]);
5098     FollowProlongation(*seed1,rows[19-irow]);       
5099     new(&param0[irow])     AliTPCseed(*seed0);
5100     new(&param1[19-irow])   AliTPCseed(*seed1);
5101   }
5102   //
5103   // define kinks 
5104   for (Int_t irow=0; irow<19;irow++){
5105     kinks[irow].SetMother(param0[irow]);
5106     kinks[irow].SetDaughter(param1[irow]);
5107     kinks[irow].Update();
5108   }
5109   //
5110   // choose kink with biggest change of angle
5111   Int_t index =-1;
5112   Double_t maxchange= 0;
5113   for (Int_t irow=1;irow<19;irow++){
5114     if (TMath::Abs(kinks[irow].GetR())>240.) continue;
5115     if (TMath::Abs(kinks[irow].GetR())<110.) continue;
5116     Float_t quality = TMath::Abs(kinks[irow].GetAngle(2))/(3.+TMath::Abs(kinks[irow].GetR()-param0[irow].fX));
5117     if ( quality > maxchange){
5118       maxchange = quality;
5119       index = irow;
5120       //
5121     }
5122   }
5123   delete seed0;
5124   delete seed1;
5125   if (index<0) return 0;
5126   //
5127   Int_t row0    = GetRowNumber(kinks[index].GetR());   //row 0 estimate
5128   seed0 = new AliTPCseed(param0[index]);
5129   seed1 = new AliTPCseed(param1[index]);
5130   seed0->Reset(kFALSE);
5131   seed1->Reset(kFALSE);
5132   seed0->ResetCovariance();
5133   seed1->ResetCovariance();
5134   FollowProlongation(*seed0,0);
5135   FollowBackProlongation(*seed1,158);
5136   new (&mother) AliTPCseed(*seed0);  // backup mother at position 0
5137   seed0->Reset(kFALSE);  
5138   seed1->Reset(kFALSE);
5139   seed0->ResetCovariance();
5140   seed1->ResetCovariance();
5141   //
5142   first = TMath::Max(row0-20,0);
5143   last  = TMath::Min(row0+20,158);
5144   //
5145   for (Int_t irow=0; irow<20;irow++){
5146     rows[irow] = first +((last-first)*irow)/19;
5147   }
5148   // store parameters along the track
5149   //
5150   for (Int_t irow=0;irow<20;irow++){
5151     FollowBackProlongation(*seed0, rows[irow]);
5152     FollowProlongation(*seed1,rows[19-irow]);       
5153     new(&param0[irow])     AliTPCseed(*seed0);
5154     new(&param1[19-irow])   AliTPCseed(*seed1);
5155   }
5156   //
5157   // define kinks 
5158   for (Int_t irow=0; irow<19;irow++){
5159     kinks[irow].SetMother(param0[irow]);
5160     kinks[irow].SetDaughter(param1[irow]);
5161     //    param0[irow].Dump();
5162     //param1[irow].Dump();
5163     kinks[irow].Update();
5164   }
5165   //
5166   // choose kink with biggest change of angle
5167   index =-1;
5168   maxchange= 0;
5169   for (Int_t irow=0;irow<20;irow++){
5170     if (TMath::Abs(kinks[irow].GetR())>250.) continue;
5171     if (TMath::Abs(kinks[irow].GetR())<90.) continue;
5172     Float_t quality = TMath::Abs(kinks[irow].GetAngle(2))/(3.+TMath::Abs(kinks[irow].GetR()-param0[irow].fX));
5173     if ( quality > maxchange){
5174       maxchange = quality;
5175       index = irow;
5176       //
5177     }
5178   }
5179   //
5180   //
5181   if (index==-1 || param0[index].fN+param1[index].fN<100){
5182     delete seed0;
5183     delete seed1;
5184     return 0;
5185   }
5186   //  Float_t anglesigma = TMath::Sqrt(param0[index].fC22+param0[index].fC33+param1[index].fC22+param1[index].fC33);
5187   
5188   kink.SetMother(param0[index]);
5189   kink.SetDaughter(param1[index]);
5190   kink.Update();
5191   row0    = GetRowNumber(kink.GetR());   
5192   kink.SetTPCRow0(row0);
5193   kink.SetLabel(CookLabel(seed0,0.5,0,row0),0);
5194   kink.SetLabel(CookLabel(seed1,0.5,row0,158),1);
5195   kink.SetIndex(-10,0);
5196   kink.SetIndex(int(param0[index].fN+param1[index].fN),1);
5197   kink.SetTPCncls(param0[index].fN,0);
5198   kink.SetTPCncls(param1[index].fN,1);
5199   //
5200   //
5201   //  new (&mother) AliTPCseed(param0[index]);
5202   new (&daughter) AliTPCseed(param1[index]);
5203   daughter.SetLabel(kink.GetLabel(1));  
5204   param0[index].Reset(kTRUE);
5205   FollowProlongation(param0[index],0);  
5206   new (&mother) AliTPCseed(param0[index]);
5207   mother.SetLabel(kink.GetLabel(0));
5208   delete seed0;
5209   delete seed1;
5210   //
5211   return 1;
5212 }
5213
5214
5215
5216
5217 AliTPCseed*  AliTPCtrackerMI::ReSeed(AliTPCseed *t)
5218 {
5219   //
5220   // reseed - refit -  track
5221   //
5222   Int_t first = 0;
5223   //  Int_t last  = fSectors->GetNRows()-1;
5224   //
5225   if (fSectors == fOuterSec){
5226     first = TMath::Max(first, t->fFirstPoint-fInnerSec->GetNRows());
5227     //last  = 
5228   }
5229   else
5230     first = t->fFirstPoint;
5231   //
5232   AliTPCseed * seed = MakeSeed(t,0.1,0.5,0.9);
5233   FollowBackProlongation(*t,fSectors->GetNRows()-1);
5234   t->Reset(kFALSE);
5235   FollowProlongation(*t,first);
5236   return seed;
5237 }
5238
5239
5240
5241
5242
5243
5244
5245 //_____________________________________________________________________________
5246 Int_t AliTPCtrackerMI::ReadSeeds(const TFile *inp) {
5247   //-----------------------------------------------------------------
5248   // This function reades track seeds.
5249   //-----------------------------------------------------------------
5250   TDirectory *savedir=gDirectory; 
5251
5252   TFile *in=(TFile*)inp;
5253   if (!in->IsOpen()) {
5254      cerr<<"AliTPCtrackerMI::ReadSeeds(): input file is not open !\n";
5255      return 1;
5256   }
5257
5258   in->cd();
5259   TTree *seedTree=(TTree*)in->Get("Seeds");
5260   if (!seedTree) {
5261      cerr<<"AliTPCtrackerMI::ReadSeeds(): ";
5262      cerr<<"can't get a tree with track seeds !\n";
5263      return 2;
5264   }
5265   AliTPCtrack *seed=new AliTPCtrack; 
5266   seedTree->SetBranchAddress("tracks",&seed);
5267   
5268   if (fSeeds==0) fSeeds=new TObjArray(15000);
5269
5270   Int_t n=(Int_t)seedTree->GetEntries();
5271   for (Int_t i=0; i<n; i++) {
5272      seedTree->GetEvent(i);
5273      fSeeds->AddLast(new AliTPCseed(*seed/*,seed->GetAlpha()*/));
5274   }
5275   
5276   delete seed;
5277   delete seedTree; 
5278   savedir->cd();
5279   return 0;
5280 }
5281
5282 Int_t AliTPCtrackerMI::Clusters2Tracks (AliESD *esd)
5283 {
5284   //
5285   if (fSeeds) DeleteSeeds();
5286   fEvent = esd;
5287   Clusters2Tracks();
5288   if (!fSeeds) return 1;
5289   FillESD(fSeeds);
5290   return 0;
5291   //
5292 }
5293
5294
5295 //_____________________________________________________________________________
5296 Int_t AliTPCtrackerMI::Clusters2Tracks() {
5297   //-----------------------------------------------------------------
5298   // This is a track finder.
5299   //-----------------------------------------------------------------
5300   TDirectory *savedir=gDirectory; 
5301   TStopwatch timer;
5302
5303   fIteration = 0;
5304   fSeeds = Tracking();
5305
5306   if (fDebug>0){
5307     Info("Clusters2Tracks","Time for tracking: \t");timer.Print();timer.Start();
5308   }
5309   //activate again some tracks
5310   for (Int_t i=0; i<fSeeds->GetEntriesFast(); i++) {
5311     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
5312     if (!pt) continue;    
5313     Int_t nc=t.GetNumberOfClusters();
5314     if (nc<20) {
5315       delete fSeeds->RemoveAt(i);
5316       continue;
5317     } 
5318     CookLabel(pt,0.1); 
5319     if (pt->fRemoval==10) {
5320       if (pt->GetDensityFirst(20)>0.8 || pt->GetDensityFirst(30)>0.8 || pt->GetDensityFirst(40)>0.7)
5321         pt->Desactivate(10);  // make track again active
5322       else{
5323         pt->Desactivate(20);    
5324         delete fSeeds->RemoveAt(i);
5325       }
5326     } 
5327   }
5328   //
5329   RemoveUsed2(fSeeds,0.85,0.85,0);
5330   FindKinks(fSeeds,fEvent);
5331   RemoveUsed2(fSeeds,0.5,0.4,20);
5332  //  //
5333 //   // refit short tracks
5334 //   //
5335   Int_t nseed=fSeeds->GetEntriesFast();
5336 //   for (Int_t i=0; i<nseed; i++) {
5337 //     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
5338 //     if (!pt) continue;    
5339 //     Int_t nc=t.GetNumberOfClusters();
5340 //     if (nc<15) {
5341 //       delete fSeeds->RemoveAt(i);
5342 //       continue;
5343 //     }
5344 //     if (pt->GetKinkIndexes()[0]!=0) continue; // ignore kink candidates
5345 //     if (nc>100) continue;                     // hopefully, enough for ITS
5346 //     AliTPCseed *seed2 = new AliTPCseed(*pt);
5347 //     //seed2->Reset(kFALSE);
5348 //     //pt->ResetCovariance();
5349 //     seed2->Modify(1);
5350 //     FollowBackProlongation(*seed2,158);
5351 //     //seed2->Reset(kFALSE);
5352 //     seed2->Modify(10);
5353 //     FollowProlongation(*seed2,0);
5354 //     TTreeSRedirector &cstream = *fDebugStreamer;
5355 //     cstream<<"Crefit"<<
5356 //       "Tr0.="<<pt<<
5357 //       "Tr1.="<<seed2<<
5358 //       "\n";     
5359 //     if (seed2->fN>pt->fN){
5360 //       delete fSeeds->RemoveAt(i);
5361 //       fSeeds->AddAt(seed2,i);
5362 //     }else{
5363 //       delete seed2;
5364 //     }
5365 //   }
5366 //   RemoveUsed2(fSeeds,0.6,0.6,50);
5367
5368 //  FindV0s(fSeeds,fEvent);  
5369   //RemoveDouble(fSeeds,0.2,0.6,11);
5370
5371   //
5372   Int_t found = 0;
5373   for (Int_t i=0; i<nseed; i++) {
5374     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
5375     if (!pt) continue;    
5376     Int_t nc=t.GetNumberOfClusters();
5377     if (nc<15) {
5378       delete fSeeds->RemoveAt(i);
5379       continue;
5380     }
5381     CookLabel(pt,0.1); //For comparison only
5382     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
5383     if ((pt->IsActive() || (pt->fRemoval==10) )){
5384       found++;      
5385       if (fDebug>0) cerr<<found<<'\r';      
5386       pt->fLab2 = i;
5387     }
5388     else
5389       delete fSeeds->RemoveAt(i);
5390   }
5391
5392   
5393   //RemoveOverlap(fSeeds,0.99,7,kTRUE);  
5394   SignShared(fSeeds);  
5395   //RemoveUsed(fSeeds,0.9,0.9,6);
5396   // 
5397   nseed=fSeeds->GetEntriesFast();
5398   found = 0;
5399   for (Int_t i=0; i<nseed; i++) {
5400     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
5401     if (!pt) continue;    
5402     Int_t nc=t.GetNumberOfClusters();
5403     if (nc<15) {
5404       delete fSeeds->RemoveAt(i);
5405       continue;
5406     }
5407     t.SetUniqueID(i);
5408     t.CookdEdx(0.02,0.6);
5409     //    CheckKinkPoint(&t,0.05);
5410     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
5411     if ((pt->IsActive() || (pt->fRemoval==10) )){
5412       found++;
5413       if (fDebug>0){
5414         cerr<<found<<'\r';      
5415       }
5416       pt->fLab2 = i;
5417     }
5418     else
5419       delete fSeeds->RemoveAt(i);
5420     //AliTPCseed * seed1 = ReSeed(pt,0.05,0.5,1);
5421     //if (seed1){
5422     //  FollowProlongation(*seed1,0);
5423     //  Int_t n = seed1->GetNumberOfClusters();
5424     //  printf("fP4\t%f\t%f\n",seed1->GetC(),pt->GetC());
5425     //  printf("fN\t%d\t%d\n", seed1->GetNumberOfClusters(),pt->GetNumberOfClusters());
5426     //
5427     //}
5428     //AliTPCseed * seed2 = ReSeed(pt,0.95,0.5,0.05);
5429     
5430   }
5431
5432   SortTracks(fSeeds, 1);
5433   
5434   /*    
5435   fIteration = 1;
5436   PrepareForBackProlongation(fSeeds,5.);
5437   PropagateBack(fSeeds);
5438   printf("Time for back propagation: \t");timer.Print();timer.Start();
5439   
5440   fIteration = 2;
5441   
5442   PrepareForProlongation(fSeeds,5.);
5443   PropagateForward2(fSeeds);
5444    
5445   printf("Time for FORWARD propagation: \t");timer.Print();timer.Start();
5446   // RemoveUsed(fSeeds,0.7,0.7,6);
5447   //RemoveOverlap(fSeeds,0.9,7,kTRUE);
5448    
5449   nseed=fSeeds->GetEntriesFast();
5450   found = 0;
5451   for (Int_t i=0; i<nseed; i++) {
5452     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
5453     if (!pt) continue;    
5454     Int_t nc=t.GetNumberOfClusters();
5455     if (nc<15) {
5456       delete fSeeds->RemoveAt(i);
5457       continue;
5458     }
5459     t.CookdEdx(0.02,0.6);
5460     //    CookLabel(pt,0.1); //For comparison only
5461     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
5462     if ((pt->IsActive() || (pt->fRemoval==10) )){
5463       cerr<<found++<<'\r';      
5464     }
5465     else
5466       delete fSeeds->RemoveAt(i);
5467     pt->fLab2 = i;
5468   }
5469   */
5470  
5471   //  fNTracks = found;
5472   if (fDebug>0){
5473     Info("Clusters2Tracks","Time for overlap removal, track writing and dedx cooking: \t"); timer.Print();timer.Start();
5474   }
5475   //
5476   //  cerr<<"Number of found tracks : "<<"\t"<<found<<endl;  
5477   Info("Clusters2Tracks","Number of found tracks %d",found);  
5478   savedir->cd();
5479   //  UnloadClusters();
5480   //  
5481   return 0;
5482 }
5483
5484 void AliTPCtrackerMI::Tracking(TObjArray * arr)
5485 {
5486   //
5487   // tracking of the seeds
5488   //
5489
5490   fSectors = fOuterSec;
5491   ParallelTracking(arr,150,63);
5492   fSectors = fOuterSec;
5493   ParallelTracking(arr,63,0);
5494 }
5495
5496 TObjArray * AliTPCtrackerMI::Tracking(Int_t seedtype, Int_t i1, Int_t i2, Float_t cuts[4], Float_t dy, Int_t dsec)
5497 {
5498   //
5499   //
5500   //tracking routine
5501   TObjArray * arr = new TObjArray;
5502   // 
5503   fSectors = fOuterSec;
5504   TStopwatch timer;
5505   timer.Start();
5506   for (Int_t sec=0;sec<fkNOS;sec++){
5507     if (seedtype==3) MakeSeeds3(arr,sec,i1,i2,cuts,dy, dsec);
5508     if (seedtype==4) MakeSeeds5(arr,sec,i1,i2,cuts,dy);    
5509     if (seedtype==2) MakeSeeds2(arr,sec,i1,i2,cuts,dy);
5510   }
5511   if (fDebug>0){
5512     Info("Tracking","\nSeeding - %d\t%d\t%d\t%d\n",seedtype,i1,i2,arr->GetEntriesFast());
5513     timer.Print();
5514     timer.Start();
5515   }
5516   Tracking(arr);  
5517   if (fDebug>0){
5518     timer.Print();
5519   }
5520
5521   return arr;
5522 }
5523
5524 TObjArray * AliTPCtrackerMI::Tracking()
5525 {
5526   //
5527   //
5528   TStopwatch timer;
5529   timer.Start();
5530   Int_t nup=fOuterSec->GetNRows()+fInnerSec->GetNRows();
5531
5532   TObjArray * seeds = new TObjArray;
5533   TObjArray * arr=0;
5534   
5535   Int_t gap =20;
5536   Float_t cuts[4];
5537   cuts[0] = 0.002;
5538   cuts[1] = 1.5;
5539   cuts[2] = 3.;
5540   cuts[3] = 3.;
5541   Float_t fnumber  = 3.0;
5542   Float_t fdensity = 3.0;
5543   
5544   //  
5545   //find primaries  
5546   cuts[0]=0.0066;
5547   for (Int_t delta = 0; delta<18; delta+=6){
5548     //
5549     cuts[0]=0.0070;
5550     cuts[1] = 1.5;
5551     arr = Tracking(3,nup-1-delta,nup-1-delta-gap,cuts,-1,1);
5552     SumTracks(seeds,arr);   
5553     SignClusters(seeds,fnumber,fdensity); 
5554     //
5555     for (Int_t i=2;i<6;i+=2){
5556       // seed high pt tracks
5557       cuts[0]=0.0022;
5558       cuts[1]=0.3;
5559       arr = Tracking(3,nup-i-delta,nup-i-delta-gap,cuts,-1,0);
5560       SumTracks(seeds,arr);   
5561       SignClusters(seeds,fnumber,fdensity);        
5562     }
5563   }
5564   fnumber  = 4;
5565   fdensity = 4.;
5566   //  RemoveUsed(seeds,0.9,0.9,1);
5567   //  UnsignClusters();
5568   //  SignClusters(seeds,fnumber,fdensity);    
5569
5570   //find primaries  
5571   cuts[0]=0.0077;
5572   for (Int_t delta = 20; delta<120; delta+=10){
5573     //
5574     // seed high pt tracks
5575     cuts[0]=0.0060;
5576     cuts[1]=0.3;
5577     cuts[2]=6.;
5578     arr = Tracking(3,nup-delta,nup-delta-gap,cuts,-1);
5579     SumTracks(seeds,arr);   
5580     SignClusters(seeds,fnumber,fdensity);            
5581
5582     cuts[0]=0.003;
5583     cuts[1]=0.3;
5584     cuts[2]=6.;
5585     arr = Tracking(3,nup-delta-5,nup-delta-5-gap,cuts,-1);
5586     SumTracks(seeds,arr);   
5587     SignClusters(seeds,fnumber,fdensity);            
5588   }
5589
5590   cuts[0] = 0.01;
5591   cuts[1] = 2.0;
5592   cuts[2] = 3.;
5593   cuts[3] = 2.0;
5594   fnumber  = 2.;
5595   fdensity = 2.;
5596   
5597   if (fDebug>0){
5598     Info("Tracking()","\n\nPrimary seeding\t%d\n\n",seeds->GetEntriesFast());
5599     timer.Print();
5600     timer.Start();
5601   }
5602   //  RemoveUsed(seeds,0.75,0.75,1);
5603   //UnsignClusters();
5604   //SignClusters(seeds,fnumber,fdensity);
5605   
5606   // find secondaries
5607
5608   cuts[0] = 0.3;
5609   cuts[1] = 1.5;
5610   cuts[2] = 3.;
5611   cuts[3] = 1.5;
5612
5613   arr = Tracking(4,nup-1,nup-1-gap,cuts,-1);
5614   SumTracks(seeds,arr);   
5615   SignClusters(seeds,fnumber,fdensity);   
5616   //
5617   arr = Tracking(4,nup-2,nup-2-gap,cuts,-1);
5618   SumTracks(seeds,arr);   
5619   SignClusters(seeds,fnumber,fdensity);   
5620   //
5621   arr = Tracking(4,nup-3,nup-3-gap,cuts,-1);
5622   SumTracks(seeds,arr);   
5623   SignClusters(seeds,fnumber,fdensity);   
5624   //
5625
5626
5627   for (Int_t delta = 3; delta<30; delta+=5){
5628     //
5629     cuts[0] = 0.3;
5630     cuts[1] = 1.5;
5631     cuts[2] = 3.;
5632     cuts[3] = 1.5;
5633     arr = Tracking(4,nup-1-delta,nup-1-delta-gap,cuts,-1);
5634     SumTracks(seeds,arr);   
5635     SignClusters(seeds,fnumber,fdensity);   
5636     //
5637     arr = Tracking(4,nup-3-delta,nup-5-delta-gap,cuts,4);
5638     SumTracks(seeds,arr);   
5639     SignClusters(seeds,fnumber,fdensity); 
5640     //
5641   } 
5642   fnumber  = 1;
5643   fdensity = 1;
5644   //
5645   // change cuts
5646   fnumber  = 2.;
5647   fdensity = 2.;
5648   cuts[0]=0.0080;
5649
5650   // find secondaries
5651   for (Int_t delta = 30; delta<90; delta+=10){
5652     //
5653     cuts[0] = 0.3;
5654     cuts[1] = 3.5;
5655     cuts[2] = 3.;
5656     cuts[3] = 3.5;
5657     arr = Tracking(4,nup-1-delta,nup-1-delta-gap,cuts,-1);
5658     SumTracks(seeds,arr);   
5659     SignClusters(seeds,fnumber,fdensity);   
5660     //
5661     arr = Tracking(4,nup-5-delta,nup-5-delta-gap,cuts,5 );
5662     SumTracks(seeds,arr);   
5663     SignClusters(seeds,fnumber,fdensity);   
5664   }
5665  
5666   if (fDebug>0){
5667     Info("Tracking()","\n\nSecondary seeding\t%d\n\n",seeds->GetEntriesFast());
5668     timer.Print();
5669     timer.Start();
5670   }
5671
5672   return seeds;
5673   //
5674       
5675 }
5676
5677
5678 void AliTPCtrackerMI::SumTracks(TObjArray *arr1,TObjArray *arr2) const
5679 {
5680   //
5681   //sum tracks to common container
5682   //remove suspicious tracks
5683   Int_t nseed = arr2->GetEntriesFast();
5684   for (Int_t i=0;i<nseed;i++){
5685     AliTPCseed *pt=(AliTPCseed*)arr2->UncheckedAt(i);    
5686     if (pt){
5687       
5688       // NORMAL ACTIVE TRACK
5689       if (pt->IsActive()){
5690         arr1->AddLast(arr2->RemoveAt(i));
5691         continue;
5692       }
5693       //remove not usable tracks
5694       if (pt->fRemoval!=10){
5695         delete arr2->RemoveAt(i);
5696         continue;
5697       }
5698       // REMOVE VERY SHORT  TRACKS
5699       if (pt->GetNumberOfClusters()<20){ 
5700         delete arr2->RemoveAt(i);
5701         continue;
5702       }
5703       // ENABLE ONLY ENOUGH GOOD STOPPED TRACKS
5704       if (pt->GetDensityFirst(20)>0.8 || pt->GetDensityFirst(30)>0.8 || pt->GetDensityFirst(40)>0.7)
5705         arr1->AddLast(arr2->RemoveAt(i));
5706       else{      
5707         delete arr2->RemoveAt(i);
5708       }
5709     }
5710   }
5711   delete arr2;  
5712 }
5713
5714
5715
5716 void  AliTPCtrackerMI::ParallelTracking(TObjArray * arr, Int_t rfirst, Int_t rlast)
5717 {
5718   //
5719   // try to track in parralel
5720
5721   Int_t nseed=arr->GetEntriesFast();
5722   //prepare seeds for tracking
5723   for (Int_t i=0; i<nseed; i++) {
5724     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i), &t=*pt; 
5725     if (!pt) continue;
5726     if (!t.IsActive()) continue;
5727     // follow prolongation to the first layer
5728     if ( (fSectors ==fInnerSec) || (t.fFirstPoint-fParam->GetNRowLow()>rfirst+1) )  
5729       FollowProlongation(t, rfirst+1);
5730   }
5731
5732
5733   //
5734   for (Int_t nr=rfirst; nr>=rlast; nr--){ 
5735     if (nr<fInnerSec->GetNRows()) 
5736       fSectors = fInnerSec;
5737     else
5738       fSectors = fOuterSec;
5739     // make indexes with the cluster tracks for given       
5740
5741     // find nearest cluster
5742     for (Int_t i=0; i<nseed; i++) {
5743       AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i), &t=*pt;       
5744       if (!pt) continue;
5745       if (nr==80) pt->UpdateReference();
5746       if (!pt->IsActive()) continue;
5747       //      if ( (fSectors ==fOuterSec) && (pt->fFirstPoint-fParam->GetNRowLow())<nr) continue;
5748       if (pt->fRelativeSector>17) {
5749         continue;
5750       }
5751       UpdateClusters(t,nr);
5752     }
5753     // prolonagate to the nearest cluster - if founded
5754     for (Int_t i=0; i<nseed; i++) {
5755       AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i); 
5756       if (!pt) continue;
5757       if (!pt->IsActive()) continue; 
5758       // if ((fSectors ==fOuterSec) && (pt->fFirstPoint-fParam->GetNRowLow())<nr) continue;
5759       if (pt->fRelativeSector>17) {
5760         continue;
5761       }
5762       FollowToNextCluster(*pt,nr);
5763     }
5764   }    
5765 }
5766
5767 void AliTPCtrackerMI::PrepareForBackProlongation(TObjArray * arr,Float_t fac) const
5768 {
5769   //
5770   //
5771   // if we use TPC track itself we have to "update" covariance
5772   //
5773   Int_t nseed= arr->GetEntriesFast();
5774   for (Int_t i=0;i<nseed;i++){
5775     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
5776     if (pt) {
5777       pt->Modify(fac);
5778       //
5779       //rotate to current local system at first accepted  point    
5780       Int_t index  = pt->GetClusterIndex2(pt->fFirstPoint); 
5781       Int_t sec    = (index&0xff000000)>>24;
5782       sec = sec%18;
5783       Float_t angle1 = fInnerSec->GetAlpha()*sec+fInnerSec->GetAlphaShift();
5784       if (angle1>TMath::Pi()) 
5785         angle1-=2.*TMath::Pi();
5786       Float_t angle2 = pt->GetAlpha();
5787       
5788       if (TMath::Abs(angle1-angle2)>0.001){
5789         pt->Rotate(angle1-angle2);
5790         //angle2 = pt->GetAlpha();
5791         //pt->fRelativeSector = pt->GetAlpha()/fInnerSec->GetAlpha();
5792         //if (pt->GetAlpha()<0) 
5793         //  pt->fRelativeSector+=18;
5794         //sec = pt->fRelativeSector;
5795       }
5796         
5797     }
5798     
5799   }
5800
5801
5802 }
5803 void AliTPCtrackerMI::PrepareForProlongation(TObjArray * arr, Float_t fac) const
5804 {
5805   //
5806   //
5807   // if we use TPC track itself we have to "update" covariance
5808   //
5809   Int_t nseed= arr->GetEntriesFast();
5810   for (Int_t i=0;i<nseed;i++){
5811     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
5812     if (pt) {
5813       pt->Modify(fac);
5814       pt->fFirstPoint = pt->fLastPoint; 
5815     }
5816     
5817   }
5818
5819
5820 }
5821
5822 Int_t AliTPCtrackerMI::PropagateBack(TObjArray * arr)
5823 {
5824   //
5825   // make back propagation
5826   //
5827   Int_t nseed= arr->GetEntriesFast();
5828   for (Int_t i=0;i<nseed;i++){
5829     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
5830     if (pt&& pt->GetKinkIndex(0)<=0) { 
5831       //AliTPCseed *pt2 = new AliTPCseed(*pt);
5832       fSectors = fInnerSec;
5833       //FollowBackProlongation(*pt,fInnerSec->GetNRows()-1);
5834       //fSectors = fOuterSec;
5835       FollowBackProlongation(*pt,fInnerSec->GetNRows()+fOuterSec->GetNRows()-1);     
5836       //if (pt->GetNumberOfClusters()<(pt->fEsd->GetTPCclusters(0)) ){
5837       //        Error("PropagateBack","Not prolonged track %d",pt->GetLabel());
5838       //        FollowBackProlongation(*pt2,fInnerSec->GetNRows()+fOuterSec->GetNRows()-1);
5839       //}
5840     }
5841     if (pt&& pt->GetKinkIndex(0)>0) {
5842       AliESDkink * kink = fEvent->GetKink(pt->GetKinkIndex(0)-1);
5843       pt->fFirstPoint = kink->GetTPCRow0();
5844       fSectors = fInnerSec;
5845       FollowBackProlongation(*pt,fInnerSec->GetNRows()+fOuterSec->GetNRows()-1);  
5846     }
5847     
5848   }
5849   return 0;
5850 }
5851
5852
5853 Int_t AliTPCtrackerMI::PropagateForward2(TObjArray * arr)
5854 {
5855   //
5856   // make forward propagation
5857   //
5858   Int_t nseed= arr->GetEntriesFast();
5859   //
5860   for (Int_t i=0;i<nseed;i++){
5861     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
5862     if (pt) { 
5863       FollowProlongation(*pt,0);
5864     }
5865   }
5866   return 0;
5867 }
5868
5869
5870 Int_t AliTPCtrackerMI::PropagateForward()
5871 {
5872   //
5873   // propagate track forward
5874   //UnsignClusters();
5875   Int_t nseed = fSeeds->GetEntriesFast();
5876   for (Int_t i=0;i<nseed;i++){
5877     AliTPCseed *pt = (AliTPCseed*)fSeeds->UncheckedAt(i);
5878     if (pt){
5879       AliTPCseed &t = *pt;
5880       Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
5881       if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
5882       if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
5883       t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
5884     }
5885   }
5886   
5887   fSectors = fOuterSec;
5888   ParallelTracking(fSeeds,fOuterSec->GetNRows()+fInnerSec->GetNRows()-1,fInnerSec->GetNRows());
5889   fSectors = fInnerSec;
5890   ParallelTracking(fSeeds,fInnerSec->GetNRows()-1,0);
5891   //WriteTracks();
5892   return 1;
5893 }
5894
5895
5896
5897
5898
5899
5900 Int_t AliTPCtrackerMI::PropagateBack(AliTPCseed * pt, Int_t row0, Int_t row1)
5901 {
5902   //
5903   // make back propagation, in between row0 and row1
5904   //
5905   
5906   if (pt) { 
5907     fSectors = fInnerSec;
5908     Int_t  r1;
5909     //
5910     if (row1<fSectors->GetNRows()) 
5911       r1 = row1;
5912     else 
5913       r1 = fSectors->GetNRows()-1;
5914
5915     if (row0<fSectors->GetNRows()&& r1>0 )
5916       FollowBackProlongation(*pt,r1);
5917     if (row1<=fSectors->GetNRows())
5918       return 0;
5919     //
5920     r1 = row1 - fSectors->GetNRows();
5921     if (r1<=0) return 0;
5922     if (r1>=fOuterSec->GetNRows()) return 0;
5923     fSectors = fOuterSec;
5924     return FollowBackProlongation(*pt,r1);
5925   }        
5926   return 0;
5927 }
5928
5929
5930
5931
5932 void  AliTPCtrackerMI::GetShape(AliTPCseed * seed, Int_t row)
5933 {
5934   //
5935   //
5936   Float_t sd2 = TMath::Abs((fParam->GetZLength()-TMath::Abs(seed->GetZ())))*fParam->GetDiffL()*fParam->GetDiffL();
5937   //  Float_t padlength =  fParam->GetPadPitchLength(seed->fSector);
5938   Float_t padlength =  GetPadPitchLength(row);
5939   //
5940   Float_t sresy = (seed->fSector < fParam->GetNSector()/2) ? 0.2 :0.3;
5941   Float_t angulary  = seed->GetSnp();
5942   angulary = angulary*angulary/(1-angulary*angulary);
5943   seed->fCurrentSigmaY2 = sd2+padlength*padlength*angulary/12.+sresy*sresy;  
5944   //
5945   Float_t sresz = fParam->GetZSigma();
5946   Float_t angularz  = seed->GetTgl();
5947   seed->fCurrentSigmaZ2 = sd2+padlength*padlength*angularz*angularz*(1+angulary)/12.+sresz*sresz;
5948   /*
5949   Float_t wy = GetSigmaY(seed);
5950   Float_t wz = GetSigmaZ(seed);
5951   wy*=wy;
5952   wz*=wz;
5953   if (TMath::Abs(wy/seed->fCurrentSigmaY2-1)>0.0001 || TMath::Abs(wz/seed->fCurrentSigmaZ2-1)>0.0001 ){
5954     printf("problem\n");
5955   }
5956   */
5957 }
5958
5959
5960 Float_t  AliTPCtrackerMI::GetSigmaY(AliTPCseed * seed)
5961 {
5962   //
5963   //  
5964   Float_t sd2 = TMath::Abs((fParam->GetZLength()-TMath::Abs(seed->GetZ())))*fParam->GetDiffL()*fParam->GetDiffL();
5965   Float_t padlength =  fParam->GetPadPitchLength(seed->fSector);
5966   Float_t sres = (seed->fSector < fParam->GetNSector()/2) ? 0.2 :0.3;
5967   Float_t angular  = seed->GetSnp();
5968   angular = angular*angular/(1-angular*angular);
5969   //  angular*=angular;
5970   //angular  = TMath::Sqrt(angular/(1-angular));
5971   Float_t res = TMath::Sqrt(sd2+padlength*padlength*angular/12.+sres*sres);
5972   return res;
5973 }
5974 Float_t  AliTPCtrackerMI::GetSigmaZ(AliTPCseed * seed)
5975 {
5976   //
5977   //
5978   Float_t sd2 = TMath::Abs((fParam->GetZLength()-TMath::Abs(seed->GetZ())))*fParam->GetDiffL()*fParam->GetDiffL();
5979   Float_t padlength =  fParam->GetPadPitchLength(seed->fSector);
5980   Float_t sres = fParam->GetZSigma();
5981   Float_t angular  = seed->GetTgl();
5982   Float_t res = TMath::Sqrt(sd2+padlength*padlength*angular*angular/12.+sres*sres);
5983   return res;
5984 }
5985
5986
5987 //__________________________________________________________________________
5988 void AliTPCtrackerMI::CookLabel(AliKalmanTrack *tk, Float_t wrong) const {
5989   //--------------------------------------------------------------------
5990   //This function "cooks" a track label. If label<0, this track is fake.
5991   //--------------------------------------------------------------------
5992   AliTPCseed * t = (AliTPCseed*)tk;
5993   Int_t noc=t->GetNumberOfClusters();
5994   if (noc<10){
5995     //printf("\nnot founded prolongation\n\n\n");
5996     //t->Dump();
5997     return ;
5998   }
5999   Int_t lb[160];
6000   Int_t mx[160];
6001   AliTPCclusterMI *clusters[160];
6002   //
6003   for (Int_t i=0;i<160;i++) {
6004     clusters[i]=0;
6005     lb[i]=mx[i]=0;
6006   }
6007
6008   Int_t i;
6009   Int_t current=0;
6010   for (i=0; i<160 && current<noc; i++) {
6011      
6012      Int_t index=t->GetClusterIndex2(i);
6013      if (index<=0) continue; 
6014      if (index&0x8000) continue;
6015      //     
6016      //clusters[current]=GetClusterMI(index);
6017      if (t->fClusterPointer[i]){
6018        clusters[current]=t->fClusterPointer[i];     
6019        current++;
6020      }
6021   }
6022   noc = current;
6023
6024   Int_t lab=123456789;
6025   for (i=0; i<noc; i++) {
6026     AliTPCclusterMI *c=clusters[i];
6027     if (!c) continue;
6028     lab=TMath::Abs(c->GetLabel(0));
6029     Int_t j;
6030     for (j=0; j<noc; j++) if (lb[j]==lab || mx[j]==0) break;
6031     lb[j]=lab;
6032     (mx[j])++;
6033   }
6034
6035   Int_t max=0;
6036   for (i=0; i<noc; i++) if (mx[i]>max) {max=mx[i]; lab=lb[i];}
6037     
6038   for (i=0; i<noc; i++) {
6039     AliTPCclusterMI *c=clusters[i]; 
6040     if (!c) continue;
6041     if (TMath::Abs(c->GetLabel(1)) == lab ||
6042         TMath::Abs(c->GetLabel(2)) == lab ) max++;
6043   }
6044
6045   if ((1.- Float_t(max)/noc) > wrong) lab=-lab;
6046
6047   else {
6048      Int_t tail=Int_t(0.10*noc);
6049      max=0;
6050      Int_t ind=0;
6051      for (i=1; i<=160&&ind<tail; i++) {
6052        //       AliTPCclusterMI *c=clusters[noc-i];
6053        AliTPCclusterMI *c=clusters[i];
6054        if (!c) continue;
6055        if (lab == TMath::Abs(c->GetLabel(0)) ||
6056            lab == TMath::Abs(c->GetLabel(1)) ||
6057            lab == TMath::Abs(c->GetLabel(2))) max++;
6058        ind++;
6059      }
6060      if (max < Int_t(0.5*tail)) lab=-lab;
6061   }
6062
6063   t->SetLabel(lab);
6064
6065   //  delete[] lb;
6066   //delete[] mx;
6067   //delete[] clusters;
6068 }
6069
6070
6071 //__________________________________________________________________________
6072 Int_t AliTPCtrackerMI::CookLabel(AliTPCseed *t, Float_t wrong,Int_t first, Int_t last) const {
6073   //--------------------------------------------------------------------
6074   //This function "cooks" a track label. If label<0, this track is fake.
6075   //--------------------------------------------------------------------
6076   Int_t noc=t->GetNumberOfClusters();
6077   if (noc<10){
6078     //printf("\nnot founded prolongation\n\n\n");
6079     //t->Dump();
6080     return -1;
6081   }
6082   Int_t lb[160];
6083   Int_t mx[160];
6084   AliTPCclusterMI *clusters[160];
6085   //
6086   for (Int_t i=0;i<160;i++) {
6087     clusters[i]=0;
6088     lb[i]=mx[i]=0;
6089   }
6090
6091   Int_t i;
6092   Int_t current=0;
6093   for (i=0; i<160 && current<noc; i++) {
6094     if (i<first) continue;
6095     if (i>last)  continue;
6096      Int_t index=t->GetClusterIndex2(i);
6097      if (index<=0) continue; 
6098      if (index&0x8000) continue;
6099      //     
6100      //clusters[current]=GetClusterMI(index);
6101      if (t->fClusterPointer[i]){
6102        clusters[current]=t->fClusterPointer[i];     
6103        current++;
6104      }
6105   }
6106   noc = current;
6107   if (noc<5) return -1;
6108   Int_t lab=123456789;
6109   for (i=0; i<noc; i++) {
6110     AliTPCclusterMI *c=clusters[i];
6111     if (!c) continue;
6112     lab=TMath::Abs(c->GetLabel(0));
6113     Int_t j;
6114     for (j=0; j<noc; j++) if (lb[j]==lab || mx[j]==0) break;
6115     lb[j]=lab;
6116     (mx[j])++;
6117   }
6118
6119   Int_t max=0;
6120   for (i=0; i<noc; i++) if (mx[i]>max) {max=mx[i]; lab=lb[i];}
6121     
6122   for (i=0; i<noc; i++) {
6123     AliTPCclusterMI *c=clusters[i]; 
6124     if (!c) continue;
6125     if (TMath::Abs(c->GetLabel(1)) == lab ||
6126         TMath::Abs(c->GetLabel(2)) == lab ) max++;
6127   }
6128
6129   if ((1.- Float_t(max)/noc) > wrong) lab=-lab;
6130
6131   else {
6132      Int_t tail=Int_t(0.10*noc);
6133      max=0;
6134      Int_t ind=0;
6135      for (i=1; i<=160&&ind<tail; i++) {
6136        //       AliTPCclusterMI *c=clusters[noc-i];
6137        AliTPCclusterMI *c=clusters[i];
6138        if (!c) continue;
6139        if (lab == TMath::Abs(c->GetLabel(0)) ||
6140            lab == TMath::Abs(c->GetLabel(1)) ||
6141            lab == TMath::Abs(c->GetLabel(2))) max++;
6142        ind++;
6143      }
6144      if (max < Int_t(0.5*tail)) lab=-lab;
6145   }
6146
6147   //  t->SetLabel(lab);
6148   return lab;
6149   //  delete[] lb;
6150   //delete[] mx;
6151   //delete[] clusters;
6152 }
6153
6154
6155 Int_t  AliTPCtrackerMI::AliTPCSector::GetRowNumber(Double_t x) const 
6156 {
6157   //return pad row number for this x
6158   Double_t r;
6159   if (fN < 64){
6160     r=fRow[fN-1].GetX();
6161     if (x > r) return fN;
6162     r=fRow[0].GetX();
6163     if (x < r) return -1;
6164     return Int_t((x-r)/fPadPitchLength + 0.5);}
6165   else{    
6166     r=fRow[fN-1].GetX();
6167     if (x > r) return fN;
6168     r=fRow[0].GetX();
6169     if (x < r) return -1;
6170     Double_t r1=fRow[64].GetX();
6171     if(x<r1){       
6172       return Int_t((x-r)/f1PadPitchLength + 0.5);}
6173     else{
6174       return (Int_t((x-r1)/f2PadPitchLength + 0.5)+64);} 
6175   }
6176 }
6177
6178 Int_t  AliTPCtrackerMI::GetRowNumber(Double_t x[3]) const 
6179 {
6180   //return pad row number for given x vector
6181   Float_t phi = TMath::ATan2(x[1],x[0]);
6182   if(phi<0) phi=2.*TMath::Pi()+phi;
6183   //  Get the local angle in the sector philoc
6184   const Float_t kRaddeg = 180/3.14159265358979312;
6185   Float_t phiangle   = (Int_t (phi*kRaddeg/20.) + 0.5)*20./kRaddeg;
6186   Double_t localx    = x[0]*TMath::Cos(phiangle)-x[1]*TMath::Sin(phiangle);
6187   return GetRowNumber(localx);
6188 }
6189
6190 //_________________________________________________________________________
6191 void AliTPCtrackerMI::AliTPCSector::Setup(const AliTPCParam *par, Int_t f) {
6192   //-----------------------------------------------------------------------
6193   // Setup inner sector
6194   //-----------------------------------------------------------------------
6195   if (f==0) {
6196      fAlpha=par->GetInnerAngle();
6197      fAlphaShift=par->GetInnerAngleShift();
6198      fPadPitchWidth=par->GetInnerPadPitchWidth();
6199      fPadPitchLength=par->GetInnerPadPitchLength();
6200      fN=par->GetNRowLow();
6201      fRow=new AliTPCRow[fN];
6202      for (Int_t i=0; i<fN; i++) {
6203        fRow[i].SetX(par->GetPadRowRadiiLow(i));
6204        fRow[i].fDeadZone =1.5;  //1.5 cm of dead zone
6205      }
6206   } else {
6207      fAlpha=par->GetOuterAngle();
6208      fAlphaShift=par->GetOuterAngleShift();
6209      fPadPitchWidth  = par->GetOuterPadPitchWidth();
6210      fPadPitchLength = par->GetOuter1PadPitchLength();
6211      f1PadPitchLength = par->GetOuter1PadPitchLength();
6212      f2PadPitchLength = par->GetOuter2PadPitchLength();
6213
6214      fN=par->GetNRowUp();
6215      fRow=new AliTPCRow[fN];
6216      for (Int_t i=0; i<fN; i++) {
6217        fRow[i].SetX(par->GetPadRowRadiiUp(i)); 
6218        fRow[i].fDeadZone =1.5;  // 1.5 cm of dead zone
6219      }
6220   } 
6221 }
6222
6223 AliTPCtrackerMI::AliTPCRow::AliTPCRow() {
6224   //
6225   // default constructor
6226   fN=0;
6227   fN1=0;
6228   fN2=0;
6229   fClusters1=0;
6230   fClusters2=0;
6231 }
6232
6233 AliTPCtrackerMI::AliTPCRow::~AliTPCRow(){
6234   //
6235
6236 }
6237
6238
6239
6240 //_________________________________________________________________________
6241 void 
6242 AliTPCtrackerMI::AliTPCRow::InsertCluster(const AliTPCclusterMI* c, UInt_t index) {
6243   //-----------------------------------------------------------------------
6244   // Insert a cluster into this pad row in accordence with its y-coordinate
6245   //-----------------------------------------------------------------------
6246   if (fN==kMaxClusterPerRow) {
6247     cerr<<"AliTPCRow::InsertCluster(): Too many clusters !\n"; return;
6248   }
6249   if (fN==0) {fIndex[0]=index; fClusters[fN++]=c; return;}
6250   Int_t i=Find(c->GetZ());
6251   memmove(fClusters+i+1 ,fClusters+i,(fN-i)*sizeof(AliTPCclusterMI*));
6252   memmove(fIndex   +i+1 ,fIndex   +i,(fN-i)*sizeof(UInt_t));
6253   fIndex[i]=index; fClusters[i]=c; fN++;
6254 }
6255
6256 void AliTPCtrackerMI::AliTPCRow::ResetClusters() {
6257    //
6258    // reset clusters
6259    fN  = 0; 
6260    fN1 = 0;
6261    fN2 = 0;
6262    //delete[] fClusterArray; 
6263    if (fClusters1) delete []fClusters1; 
6264    if (fClusters2) delete []fClusters2; 
6265    //fClusterArray=0;
6266    fClusters1 = 0;
6267    fClusters2 = 0;
6268 }
6269
6270
6271 //___________________________________________________________________
6272 Int_t AliTPCtrackerMI::AliTPCRow::Find(Double_t z) const {
6273   //-----------------------------------------------------------------------
6274   // Return the index of the nearest cluster 
6275   //-----------------------------------------------------------------------
6276   if (fN==0) return 0;
6277   if (z <= fClusters[0]->GetZ()) return 0;
6278   if (z > fClusters[fN-1]->GetZ()) return fN;
6279   Int_t b=0, e=fN-1, m=(b+e)/2;
6280   for (; b<e; m=(b+e)/2) {
6281     if (z > fClusters[m]->GetZ()) b=m+1;
6282     else e=m; 
6283   }
6284   return m;
6285 }
6286
6287
6288
6289 //___________________________________________________________________
6290 AliTPCclusterMI * AliTPCtrackerMI::AliTPCRow::FindNearest(Double_t y, Double_t z, Double_t roady, Double_t roadz) const {
6291   //-----------------------------------------------------------------------
6292   // Return the index of the nearest cluster in z y 
6293   //-----------------------------------------------------------------------
6294   Float_t maxdistance = roady*roady + roadz*roadz;
6295
6296   AliTPCclusterMI *cl =0;
6297   for (Int_t i=Find(z-roadz); i<fN; i++) {
6298       AliTPCclusterMI *c=(AliTPCclusterMI*)(fClusters[i]);
6299       if (c->GetZ() > z+roadz) break;
6300       if ( (c->GetY()-y) >  roady ) continue;
6301       Float_t distance = (c->GetZ()-z)*(c->GetZ()-z)+(c->GetY()-y)*(c->GetY()-y);
6302       if (maxdistance>distance) {
6303         maxdistance = distance;
6304         cl=c;       
6305       }
6306   }
6307   return cl;      
6308 }
6309
6310 AliTPCclusterMI * AliTPCtrackerMI::AliTPCRow::FindNearest2(Double_t y, Double_t z, Double_t roady, Double_t roadz,UInt_t & index) const 
6311 {
6312   //-----------------------------------------------------------------------
6313   // Return the index of the nearest cluster in z y 
6314   //-----------------------------------------------------------------------
6315   Float_t maxdistance = roady*roady + roadz*roadz;
6316   AliTPCclusterMI *cl =0;
6317
6318   //PH Check boundaries. 510 is the size of fFastCluster
6319   Int_t iz1 = Int_t(z-roadz+254.5);
6320   if (iz1<0 || iz1>=510) return cl;
6321   iz1 = TMath::Max(fFastCluster[iz1]-1,0);
6322   Int_t iz2 = Int_t(z+roadz+255.5);
6323   if (iz2<0 || iz2>=510) return cl;
6324   iz2 = TMath::Min(fFastCluster[iz2]+1,fN);
6325
6326   //FindNearest3(y,z,roady,roadz,index);
6327   //  for (Int_t i=Find(z-roadz); i<fN; i++) {
6328   for (Int_t i=iz1; i<iz2; i++) {
6329       AliTPCclusterMI *c=(AliTPCclusterMI*)(fClusters[i]);
6330       if (c->GetZ() > z+roadz) break;
6331       if ( c->GetY()-y >  roady ) continue;
6332       if ( y-c->GetY() >  roady ) continue;
6333       Float_t distance = (c->GetZ()-z)*(c->GetZ()-z)+(c->GetY()-y)*(c->GetY()-y);
6334       if (maxdistance>distance) {
6335         maxdistance = distance;
6336         cl=c;       
6337         index =i;
6338         //roady = TMath::Sqrt(maxdistance);
6339       }
6340   }
6341   return cl;      
6342 }
6343
6344
6345
6346 AliTPCclusterMI * AliTPCtrackerMI::AliTPCRow::FindNearest3(Double_t y, Double_t z, Double_t roady, Double_t roadz,UInt_t & index) const 
6347 {
6348   //-----------------------------------------------------------------------
6349   // Return the index of the nearest cluster in z y 
6350   //-----------------------------------------------------------------------
6351   Float_t maxdistance = roady*roady + roadz*roadz;
6352   //  Int_t iz = Int_t(z+255.);
6353   AliTPCclusterMI *cl =0;
6354   for (Int_t i=Find(z-roadz); i<fN; i++) {
6355     //for (Int_t i=fFastCluster[iz-2]; i<fFastCluster[iz+2]; i++) {
6356       AliTPCclusterMI *c=(AliTPCclusterMI*)(fClusters[i]);
6357       if (c->GetZ() > z+roadz) break;
6358       if ( c->GetY()-y >  roady ) continue;
6359       if ( y-c->GetY() >  roady ) continue;
6360       Float_t distance = (c->GetZ()-z)*(c->GetZ()-z)+(c->GetY()-y)*(c->GetY()-y);
6361       if (maxdistance>distance) {
6362         maxdistance = distance;
6363         cl=c;       
6364         index =i;
6365         //roady = TMath::Sqrt(maxdistance);
6366       }
6367   }
6368   return cl;      
6369 }
6370
6371
6372
6373
6374
6375 // AliTPCTrackerPoint * AliTPCseed::GetTrackPoint(Int_t i)
6376 // {
6377 //   //
6378 //   // 
6379 //   return &fTrackPoints[i];
6380 // }
6381
6382