]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCtrackerMI.cxx
Additional protection
[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   Int_t ntracks=0;
2480   Int_t nseed = fSeeds->GetEntriesFast();
2481   for (Int_t i=0;i<nseed;i++){
2482     AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
2483     if (!seed) continue;
2484     if (seed->GetKinkIndex(0)>0)  UpdateKinkQualityD(seed);  // update quality informations for kinks
2485
2486     seed->PropagateTo(fParam->GetInnerRadiusLow());
2487     seed->UpdatePoints();
2488     AliESDtrack *esd=event->GetTrack(i);
2489     seed->CookdEdx(0.02,0.6);
2490     CookLabel(seed,0.1); //For comparison only
2491     if (seed->GetNumberOfClusters()>15){
2492       esd->UpdateTrackParams(seed,AliESDtrack::kTPCrefit); 
2493       esd->SetTPCPoints(seed->GetPoints());
2494       ntracks++;
2495     }
2496     else{
2497       //printf("problem\n");
2498     }
2499   }
2500   //FindKinks(fSeeds,event);
2501   Info("RefitInward","Number of refitted tracks %d",ntracks);
2502   fEvent =0;
2503   //WriteTracks();
2504   return 0;
2505 }
2506
2507
2508 Int_t AliTPCtrackerMI::PropagateBack(AliESD *event)
2509 {
2510   //
2511   // back propagation of ESD tracks
2512   //
2513
2514   fEvent = event;
2515   fIteration = 1;
2516   ReadSeeds(event,0);
2517   PropagateBack(fSeeds);
2518   Int_t nseed = fSeeds->GetEntriesFast();
2519   Int_t ntracks=0;
2520   for (Int_t i=0;i<nseed;i++){
2521     AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
2522     if (!seed) continue;
2523     if (seed->GetKinkIndex(0)<0)  UpdateKinkQualityM(seed);  // update quality informations for kinks
2524     seed->UpdatePoints();
2525     AliESDtrack *esd=event->GetTrack(i);
2526     seed->CookdEdx(0.02,0.6);
2527     CookLabel(seed,0.1); //For comparison only
2528     if (seed->GetNumberOfClusters()>15){
2529       esd->UpdateTrackParams(seed,AliESDtrack::kTPCout);
2530       esd->SetTPCPoints(seed->GetPoints());
2531       ntracks++;
2532     }
2533   }
2534   //FindKinks(fSeeds,event);
2535   Info("PropagateBack","Number of back propagated tracks %d",ntracks);
2536   fEvent =0;
2537   //WriteTracks();
2538   return 0;
2539 }
2540
2541
2542 void AliTPCtrackerMI::DeleteSeeds()
2543 {
2544   //
2545   //delete Seeds
2546   Int_t nseed = fSeeds->GetEntriesFast();
2547   for (Int_t i=0;i<nseed;i++){
2548     AliTPCseed * seed = (AliTPCseed*)fSeeds->At(i);
2549     if (seed) delete fSeeds->RemoveAt(i);
2550   }
2551   delete fSeeds;
2552   fSeeds =0;
2553 }
2554
2555 void AliTPCtrackerMI::ReadSeeds(AliESD *event, Int_t direction)
2556 {
2557   //
2558   //read seeds from the event
2559   
2560   Int_t nentr=event->GetNumberOfTracks();
2561   if (fDebug>0){
2562     Info("ReadSeeds", "Number of ESD tracks: %d\n", nentr);
2563   }
2564   if (fSeeds) 
2565     DeleteSeeds();
2566   if (!fSeeds){   
2567     fSeeds = new TObjArray(nentr);
2568   }
2569   UnsignClusters();
2570   //  Int_t ntrk=0;  
2571   for (Int_t i=0; i<nentr; i++) {
2572     AliESDtrack *esd=event->GetTrack(i);
2573     ULong_t status=esd->GetStatus();
2574     if (!(status&AliESDtrack::kTPCin)) continue;
2575     AliTPCtrack t(*esd);
2576     //    AliTPCseed *seed = new AliTPCseed(t,t.GetAlpha());
2577     AliTPCseed *seed = new AliTPCseed(t/*,t.GetAlpha()*/);
2578     for (Int_t ikink=0;ikink<3;ikink++) {
2579       Int_t index = esd->GetKinkIndex(ikink);
2580       seed->GetKinkIndexes()[ikink] = index;
2581       if (index==0) continue;
2582       index = TMath::Abs(index);
2583       AliESDkink * kink = fEvent->GetKink(index-1);
2584       if (kink&&esd->GetKinkIndex(ikink)<0){
2585         if ((status & AliESDtrack::kTRDrefit) != 0) kink->SetStatus(1,2);
2586         if ((status & AliESDtrack::kITSout) != 0)   kink->SetStatus(1,0);
2587       }
2588       if (kink&&esd->GetKinkIndex(ikink)>0){
2589         if ((status & AliESDtrack::kTRDrefit) != 0) kink->SetStatus(1,6);
2590         if ((status & AliESDtrack::kITSout) != 0)   kink->SetStatus(1,4);
2591       }
2592
2593     }
2594     if ((status==AliESDtrack::kTPCin)&&(direction==1)) seed->ResetCovariance(); 
2595     if ( direction ==2 &&(status & AliESDtrack::kTRDrefit) == 0 ) seed->ResetCovariance();
2596     if ( direction ==2 && ((status & AliESDtrack::kTPCout) == 0) ) {
2597       fSeeds->AddAt(0,i);
2598       delete seed;
2599       continue;    
2600     }
2601     if ( direction ==2 &&(status & AliESDtrack::kTRDrefit) > 0 )  {
2602       Double_t par0[5],par1[5],x;
2603       esd->GetInnerExternalParameters(x,par0);
2604       esd->GetExternalParameters(x,par1);
2605       Double_t delta1 = TMath::Abs(par0[4]-par1[4])/(0.000000001+TMath::Abs(par0[4]+par1[4]));
2606       Double_t delta2 = TMath::Abs(par0[3]-par1[3]);
2607       Double_t trdchi2=0;
2608       if (esd->GetTRDncls()>0) trdchi2 = esd->GetTRDchi2()/esd->GetTRDncls();
2609       //reset covariance if suspicious 
2610       if ( (delta1>0.1) || (delta2>0.006) ||trdchi2>7.)
2611         seed->ResetCovariance();
2612     }
2613
2614     //
2615     //
2616     // rotate to the local coordinate system
2617     //   
2618     fSectors=fInnerSec; fN=fkNIS;    
2619     Double_t alpha=seed->GetAlpha() - fSectors->GetAlphaShift();
2620     if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
2621     if (alpha < 0.            ) alpha += 2.*TMath::Pi();
2622     Int_t ns=Int_t(alpha/fSectors->GetAlpha())%fN;
2623     alpha =ns*fSectors->GetAlpha() + fSectors->GetAlphaShift();
2624     if (alpha<-TMath::Pi()) alpha += 2*TMath::Pi();
2625     if (alpha>=TMath::Pi()) alpha -= 2*TMath::Pi();
2626     alpha-=seed->GetAlpha();  
2627     if (!seed->Rotate(alpha)) {
2628       delete seed;
2629       continue;
2630     }
2631     seed->fEsd = esd;
2632     // sign clusters
2633     for (Int_t irow=0;irow<160;irow++){
2634       Int_t index = seed->GetClusterIndex2(irow);    
2635       if (index>0){ 
2636         //
2637         AliTPCclusterMI * cl = GetClusterMI(index);
2638         seed->fClusterPointer[irow] = cl;
2639         if (cl){
2640           if ((index & 0x8000)==0){
2641             cl->Use(10);  // accepted cluster     
2642           }else{
2643             cl->Use(6);   // close cluster not accepted
2644           }     
2645         }else{
2646            Info("ReadSeeds","Not found cluster");
2647         }
2648       }
2649     }
2650     fSeeds->AddAt(seed,i);
2651   }
2652 }
2653
2654
2655
2656 //_____________________________________________________________________________
2657 void AliTPCtrackerMI::MakeSeeds3(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,  Float_t cuts[4],
2658                                  Float_t deltay, Int_t ddsec) {
2659   //-----------------------------------------------------------------
2660   // This function creates track seeds.
2661   // SEEDING WITH VERTEX CONSTRAIN 
2662   //-----------------------------------------------------------------
2663   // cuts[0]   - fP4 cut
2664   // cuts[1]   - tan(phi)  cut
2665   // cuts[2]   - zvertex cut
2666   // cuts[3]   - fP3 cut
2667   Int_t nin0  = 0;
2668   Int_t nin1  = 0;
2669   Int_t nin2  = 0;
2670   Int_t nin   = 0;
2671   Int_t nout1 = 0;
2672   Int_t nout2 = 0;
2673
2674   Double_t x[5], c[15];
2675   //  Int_t di = i1-i2;
2676   //
2677   AliTPCseed * seed = new AliTPCseed;
2678   Double_t alpha=fSectors->GetAlpha(), shift=fSectors->GetAlphaShift();
2679   Double_t cs=cos(alpha), sn=sin(alpha);
2680   //
2681   //  Double_t x1 =fOuterSec->GetX(i1);
2682   //Double_t xx2=fOuterSec->GetX(i2);
2683   
2684   Double_t x1 =GetXrow(i1);
2685   Double_t xx2=GetXrow(i2);
2686
2687   Double_t x3=GetX(), y3=GetY(), z3=GetZ();
2688
2689   Int_t imiddle = (i2+i1)/2;    //middle pad row index
2690   Double_t xm = GetXrow(imiddle); // radius of middle pad-row
2691   const AliTPCRow& krm=GetRow(sec,imiddle); //middle pad -row
2692   //
2693   Int_t ns =sec;   
2694
2695   const AliTPCRow& kr1=GetRow(ns,i1);
2696   Double_t ymax  = GetMaxY(i1)-kr1.fDeadZone-1.5;  
2697   Double_t ymaxm = GetMaxY(imiddle)-kr1.fDeadZone-1.5;  
2698
2699   //
2700   // change cut on curvature if it can't reach this layer
2701   // maximal curvature set to reach it
2702   Double_t dvertexmax  = TMath::Sqrt((x1-x3)*(x1-x3)+(ymax+5-y3)*(ymax+5-y3));
2703   if (dvertexmax*0.5*cuts[0]>0.85){
2704     cuts[0] = 0.85/(dvertexmax*0.5+1.);
2705   }
2706   Double_t r2min = 1/(cuts[0]*cuts[0]);  //minimal square of radius given by cut
2707
2708   //  Int_t ddsec = 1;
2709   if (deltay>0) ddsec = 0; 
2710   // loop over clusters  
2711   for (Int_t is=0; is < kr1; is++) {
2712     //
2713     if (kr1[is]->IsUsed(10)) continue;
2714     Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();    
2715     //if (TMath::Abs(y1)>ymax) continue;
2716
2717     if (deltay>0 && TMath::Abs(ymax-TMath::Abs(y1))> deltay ) continue;  // seed only at the edge
2718
2719     // find possible directions    
2720     Float_t anglez = (z1-z3)/(x1-x3); 
2721     Float_t extraz = z1 - anglez*(x1-xx2);  // extrapolated z      
2722     //
2723     //
2724     //find   rotation angles relative to line given by vertex and point 1
2725     Double_t dvertex2 = (x1-x3)*(x1-x3)+(y1-y3)*(y1-y3);
2726     Double_t dvertex  = TMath::Sqrt(dvertex2);
2727     Double_t angle13  = TMath::ATan((y1-y3)/(x1-x3));
2728     Double_t cs13     = cos(-angle13), sn13 = sin(-angle13);            
2729     
2730     //
2731     // loop over 2 sectors
2732     Int_t dsec1=-ddsec;
2733     Int_t dsec2= ddsec;
2734     if (y1<0)  dsec2= 0;
2735     if (y1>0)  dsec1= 0;
2736     
2737     Double_t dddz1=0;  // direction of delta inclination in z axis
2738     Double_t dddz2=0;
2739     if ( (z1-z3)>0)
2740       dddz1 =1;    
2741     else
2742       dddz2 =1;
2743     //
2744     for (Int_t dsec = dsec1; dsec<=dsec2;dsec++){
2745       Int_t sec2 = sec + dsec;
2746       // 
2747       //      AliTPCRow&  kr2  = fOuterSec[(sec2+fkNOS)%fkNOS][i2];
2748       //AliTPCRow&  kr2m = fOuterSec[(sec2+fkNOS)%fkNOS][imiddle];
2749       AliTPCRow&  kr2  = GetRow((sec2+fkNOS)%fkNOS,i2);
2750       AliTPCRow&  kr2m = GetRow((sec2+fkNOS)%fkNOS,imiddle);
2751       Int_t  index1 = TMath::Max(kr2.Find(extraz-0.6-dddz1*TMath::Abs(z1)*0.05)-1,0);
2752       Int_t  index2 = TMath::Min(kr2.Find(extraz+0.6+dddz2*TMath::Abs(z1)*0.05)+1,kr2);
2753
2754       // rotation angles to p1-p3
2755       Double_t cs13r     = cos(-angle13+dsec*alpha)/dvertex, sn13r = sin(-angle13+dsec*alpha)/dvertex;            
2756       Double_t x2,   y2,   z2; 
2757       //
2758       //      Double_t dymax = maxangle*TMath::Abs(x1-xx2);
2759
2760       //
2761       Double_t dxx0 =  (xx2-x3)*cs13r;
2762       Double_t dyy0 =  (xx2-x3)*sn13r;
2763       for (Int_t js=index1; js < index2; js++) {
2764         const AliTPCclusterMI *kcl = kr2[js];
2765         if (kcl->IsUsed(10)) continue;  
2766         //
2767         //calcutate parameters
2768         //      
2769         Double_t yy0 =  dyy0 +(kcl->GetY()-y3)*cs13r;
2770         // stright track
2771         if (TMath::Abs(yy0)<0.000001) continue;
2772         Double_t xx0 =  dxx0 -(kcl->GetY()-y3)*sn13r;
2773         Double_t y0  =  0.5*(xx0*xx0+yy0*yy0-xx0)/yy0;
2774         Double_t r02 = (0.25+y0*y0)*dvertex2;   
2775         //curvature (radius) cut
2776         if (r02<r2min) continue;                
2777        
2778         nin0++; 
2779         //
2780         Double_t c0  = 1/TMath::Sqrt(r02);
2781         if (yy0>0) c0*=-1.;     
2782                
2783        
2784         //Double_t dfi0   = 2.*TMath::ASin(dvertex*c0*0.5);
2785         //Double_t dfi1   = 2.*TMath::ASin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);
2786         Double_t dfi0   = 2.*AliTPCFastMath::FastAsin(dvertex*c0*0.5);
2787         Double_t dfi1   = 2.*AliTPCFastMath::FastAsin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);  
2788         //
2789         //
2790         Double_t z0  =  kcl->GetZ();  
2791         Double_t zzzz2    = z1-(z1-z3)*dfi1/dfi0;
2792         if (TMath::Abs(zzzz2-z0)>0.5) continue;       
2793         nin1++;              
2794         //      
2795         Double_t dip    = (z1-z0)*c0/dfi1;        
2796         Double_t x0 = (0.5*cs13+y0*sn13)*dvertex*c0;
2797         //
2798         y2 = kcl->GetY(); 
2799         if (dsec==0){
2800           x2 = xx2; 
2801           z2 = kcl->GetZ();       
2802         }
2803         else
2804           {
2805             // rotation 
2806             z2 = kcl->GetZ();  
2807             x2= xx2*cs-y2*sn*dsec;
2808             y2=+xx2*sn*dsec+y2*cs;
2809           }
2810         
2811         x[0] = y1;
2812         x[1] = z1;
2813         x[2] = x0;
2814         x[3] = dip;
2815         x[4] = c0;
2816         //
2817         //
2818         // do we have cluster at the middle ?
2819         Double_t ym,zm;
2820         GetProlongation(x1,xm,x,ym,zm);
2821         UInt_t dummy; 
2822         AliTPCclusterMI * cm=0;
2823         if (TMath::Abs(ym)-ymaxm<0){      
2824           cm = krm.FindNearest2(ym,zm,1.0,0.6,dummy);
2825           if ((!cm) || (cm->IsUsed(10))) {        
2826             continue;
2827           }
2828         }
2829         else{     
2830           // rotate y1 to system 0
2831           // get state vector in rotated system 
2832           Double_t yr1  = (-0.5*sn13+y0*cs13)*dvertex*c0;
2833           Double_t xr2  =  x0*cs+yr1*sn*dsec;
2834           Double_t xr[5]={kcl->GetY(),kcl->GetZ(), xr2, dip, c0};
2835           //
2836           GetProlongation(xx2,xm,xr,ym,zm);
2837           if (TMath::Abs(ym)-ymaxm<0){
2838             cm = kr2m.FindNearest2(ym,zm,1.0,0.6,dummy);
2839             if ((!cm) || (cm->IsUsed(10))) {      
2840               continue;
2841             }
2842           }
2843         }
2844        
2845
2846         Double_t dym = 0;
2847         Double_t dzm = 0;
2848         if (cm){
2849           dym = ym - cm->GetY();
2850           dzm = zm - cm->GetZ();
2851         }
2852         nin2++;
2853
2854
2855         //
2856         //
2857         Double_t sy1=kr1[is]->GetSigmaY2()*2., sz1=kr1[is]->GetSigmaZ2()*2.;
2858         Double_t sy2=kcl->GetSigmaY2()*2.,     sz2=kcl->GetSigmaZ2()*2.;
2859         //Double_t sy3=400*3./12., sy=0.1, sz=0.1;
2860         Double_t sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
2861         //Double_t sy3=25000*x[4]*x[4]*60+0.5, sy=0.1, sz=0.1;
2862
2863         Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
2864         Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
2865         Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
2866         Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
2867         Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
2868         Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
2869         
2870         Double_t f30=(F3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
2871         Double_t f31=(F3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
2872         Double_t f32=(F3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
2873         Double_t f34=(F3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
2874         
2875         c[0]=sy1;
2876         c[1]=0.;       c[2]=sz1;
2877         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
2878         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
2879                        c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
2880         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
2881         c[13]=f30*sy1*f40+f32*sy2*f42;
2882         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
2883         
2884         //      if (!BuildSeed(kr1[is],kcl,0,x1,x2,x3,x,c)) continue;
2885         
2886         UInt_t index=kr1.GetIndex(is);
2887         AliTPCseed *track=new(seed) AliTPCseed(index, x, c, x1, ns*alpha+shift);
2888         
2889         track->fIsSeeding = kTRUE;
2890         track->fSeed1 = i1;
2891         track->fSeed2 = i2;
2892         track->fSeedType=3;
2893
2894        
2895         //if (dsec==0) {
2896           FollowProlongation(*track, (i1+i2)/2,1);
2897           Int_t foundable,found,shared;
2898           track->GetClusterStatistic((i1+i2)/2,i1, found, foundable, shared, kTRUE);
2899           if ((found<0.55*foundable)  || shared>0.5*found || (track->GetSigmaY2()+track->GetSigmaZ2())>0.5){
2900             seed->Reset();
2901             seed->~AliTPCseed();
2902             continue;
2903           }
2904           //}
2905         
2906         nin++;
2907         FollowProlongation(*track, i2,1);
2908         
2909         
2910         //Int_t rc = 1;
2911         track->fBConstrain =1;
2912         //      track->fLastPoint = i1+fInnerSec->GetNRows();  // first cluster in track position
2913         track->fLastPoint = i1;  // first cluster in track position
2914         track->fFirstPoint = track->fLastPoint;
2915         
2916         if (track->GetNumberOfClusters()<(i1-i2)*0.5 || 
2917             track->GetNumberOfClusters() < track->fNFoundable*0.6 || 
2918             track->fNShared>0.4*track->GetNumberOfClusters() ) {
2919           seed->Reset();
2920           seed->~AliTPCseed();
2921           continue;
2922         }
2923         nout1++;
2924         // Z VERTEX CONDITION
2925         Double_t zv;
2926         zv = track->GetZ()+track->GetTgl()/track->GetC()*
2927           ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2928         if (TMath::Abs(zv-z3)>cuts[2]) {
2929           FollowProlongation(*track, TMath::Max(i2-20,0));
2930           zv = track->GetZ()+track->GetTgl()/track->GetC()*
2931             ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2932           if (TMath::Abs(zv-z3)>cuts[2]){
2933             FollowProlongation(*track, TMath::Max(i2-40,0));
2934             zv = track->GetZ()+track->GetTgl()/track->GetC()*
2935               ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2936             if (TMath::Abs(zv-z3)>cuts[2] &&(track->GetNumberOfClusters() > track->fNFoundable*0.7)){
2937               // make seed without constrain
2938               AliTPCseed * track2 = MakeSeed(track,0.2,0.5,1.);
2939               FollowProlongation(*track2, i2,1);
2940               track2->fBConstrain = kFALSE;
2941               track2->fSeedType = 1;
2942               arr->AddLast(track2); 
2943               seed->Reset();
2944               seed->~AliTPCseed();
2945               continue;         
2946             }
2947             else{
2948               seed->Reset();
2949               seed->~AliTPCseed();
2950               continue;
2951             
2952             }
2953           }
2954         }
2955         
2956         track->fSeedType =0;
2957         arr->AddLast(track); 
2958         seed = new AliTPCseed;  
2959         nout2++;
2960         // don't consider other combinations
2961         if (track->GetNumberOfClusters() > track->fNFoundable*0.8)
2962           break;
2963       }
2964     }
2965   }
2966   if (fDebug>3){
2967     Info("MakeSeeds3","\nSeeding statistic:\t%d\t%d\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin,nout1,nout2);
2968   }
2969   delete seed;
2970 }
2971
2972
2973 void AliTPCtrackerMI::MakeSeeds5(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,  Float_t cuts[4],
2974                                  Float_t deltay) {
2975   
2976
2977
2978   //-----------------------------------------------------------------
2979   // This function creates track seeds.
2980   //-----------------------------------------------------------------
2981   // cuts[0]   - fP4 cut
2982   // cuts[1]   - tan(phi)  cut
2983   // cuts[2]   - zvertex cut
2984   // cuts[3]   - fP3 cut
2985
2986
2987   Int_t nin0  = 0;
2988   Int_t nin1  = 0;
2989   Int_t nin2  = 0;
2990   Int_t nin   = 0;
2991   Int_t nout1 = 0;
2992   Int_t nout2 = 0;
2993   Int_t nout3 =0;
2994   Double_t x[5], c[15];
2995   //
2996   // make temporary seed
2997   AliTPCseed * seed = new AliTPCseed;
2998   Double_t alpha=fOuterSec->GetAlpha(), shift=fOuterSec->GetAlphaShift();
2999   //  Double_t cs=cos(alpha), sn=sin(alpha);
3000   //
3001   //
3002
3003   // first 3 padrows
3004   Double_t x1 = GetXrow(i1-1);
3005   const    AliTPCRow& kr1=GetRow(sec,i1-1);
3006   Double_t y1max  = GetMaxY(i1-1)-kr1.fDeadZone-1.5;  
3007   //
3008   Double_t x1p = GetXrow(i1);
3009   const    AliTPCRow& kr1p=GetRow(sec,i1);
3010   //
3011   Double_t x1m = GetXrow(i1-2);
3012   const    AliTPCRow& kr1m=GetRow(sec,i1-2);
3013
3014   //
3015   //last 3 padrow for seeding
3016   AliTPCRow&  kr3  = GetRow((sec+fkNOS)%fkNOS,i1-7);
3017   Double_t    x3   =  GetXrow(i1-7);
3018   //  Double_t    y3max= GetMaxY(i1-7)-kr3.fDeadZone-1.5;  
3019   //
3020   AliTPCRow&  kr3p  = GetRow((sec+fkNOS)%fkNOS,i1-6);
3021   Double_t    x3p   = GetXrow(i1-6);
3022   //
3023   AliTPCRow&  kr3m  = GetRow((sec+fkNOS)%fkNOS,i1-8);
3024   Double_t    x3m   = GetXrow(i1-8);
3025
3026   //
3027   //
3028   // middle padrow
3029   Int_t im = i1-4;                           //middle pad row index
3030   Double_t xm         = GetXrow(im);         // radius of middle pad-row
3031   const AliTPCRow& krm=GetRow(sec,im);   //middle pad -row
3032   //  Double_t ymmax = GetMaxY(im)-kr1.fDeadZone-1.5;  
3033   //
3034   //
3035   Double_t deltax  = x1-x3;
3036   Double_t dymax   = deltax*cuts[1];
3037   Double_t dzmax   = deltax*cuts[3];
3038   //
3039   // loop over clusters  
3040   for (Int_t is=0; is < kr1; is++) {
3041     //
3042     if (kr1[is]->IsUsed(10)) continue;
3043     Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();    
3044     //
3045     if (deltay>0 && TMath::Abs(y1max-TMath::Abs(y1))> deltay ) continue;  // seed only at the edge    
3046     // 
3047     Int_t  index1 = TMath::Max(kr3.Find(z1-dzmax)-1,0);
3048     Int_t  index2 = TMath::Min(kr3.Find(z1+dzmax)+1,kr3);
3049     //    
3050     Double_t y3,   z3;
3051     //
3052     //
3053     UInt_t index;
3054     for (Int_t js=index1; js < index2; js++) {
3055       const AliTPCclusterMI *kcl = kr3[js];
3056       if (kcl->IsUsed(10)) continue;
3057       y3 = kcl->GetY(); 
3058       // apply angular cuts
3059       if (TMath::Abs(y1-y3)>dymax) continue;
3060       x3 = x3; 
3061       z3 = kcl->GetZ(); 
3062       if (TMath::Abs(z1-z3)>dzmax) continue;
3063       //
3064       Double_t angley = (y1-y3)/(x1-x3);
3065       Double_t anglez = (z1-z3)/(x1-x3);
3066       //
3067       Double_t erry = TMath::Abs(angley)*(x1-x1m)*0.5+0.5;
3068       Double_t errz = TMath::Abs(anglez)*(x1-x1m)*0.5+0.5;
3069       //
3070       Double_t yyym = angley*(xm-x1)+y1;
3071       Double_t zzzm = anglez*(xm-x1)+z1;
3072
3073       const AliTPCclusterMI *kcm = krm.FindNearest2(yyym,zzzm,erry,errz,index);
3074       if (!kcm) continue;
3075       if (kcm->IsUsed(10)) continue;
3076       
3077       erry = TMath::Abs(angley)*(x1-x1m)*0.4+0.5;
3078       errz = TMath::Abs(anglez)*(x1-x1m)*0.4+0.5;
3079       //
3080       //
3081       //
3082       Int_t used  =0;
3083       Int_t found =0;
3084       //
3085       // look around first
3086       const AliTPCclusterMI *kc1m = kr1m.FindNearest2(angley*(x1m-x1)+y1,
3087                                                       anglez*(x1m-x1)+z1,
3088                                                       erry,errz,index);
3089       //
3090       if (kc1m){
3091         found++;
3092         if (kc1m->IsUsed(10)) used++;
3093       }
3094       const AliTPCclusterMI *kc1p = kr1p.FindNearest2(angley*(x1p-x1)+y1,
3095                                                       anglez*(x1p-x1)+z1,
3096                                                       erry,errz,index);
3097       //
3098       if (kc1p){
3099         found++;
3100         if (kc1p->IsUsed(10)) used++;
3101       }
3102       if (used>1)  continue;
3103       if (found<1) continue; 
3104
3105       //
3106       // look around last
3107       const AliTPCclusterMI *kc3m = kr3m.FindNearest2(angley*(x3m-x3)+y3,
3108                                                       anglez*(x3m-x3)+z3,
3109                                                       erry,errz,index);
3110       //
3111       if (kc3m){
3112         found++;
3113         if (kc3m->IsUsed(10)) used++;
3114       }
3115       else 
3116         continue;
3117       const AliTPCclusterMI *kc3p = kr3p.FindNearest2(angley*(x3p-x3)+y3,
3118                                                       anglez*(x3p-x3)+z3,
3119                                                       erry,errz,index);
3120       //
3121       if (kc3p){
3122         found++;
3123         if (kc3p->IsUsed(10)) used++;
3124       }
3125       else 
3126         continue;
3127       if (used>1)  continue;
3128       if (found<3) continue;       
3129       //
3130       Double_t x2,y2,z2;
3131       x2 = xm;
3132       y2 = kcm->GetY();
3133       z2 = kcm->GetZ();
3134       //
3135                         
3136       x[0]=y1;
3137       x[1]=z1;
3138       x[4]=F1(x1,y1,x2,y2,x3,y3);
3139       //if (TMath::Abs(x[4]) >= cuts[0]) continue;
3140       nin0++;
3141       //
3142       x[2]=F2(x1,y1,x2,y2,x3,y3);
3143       nin1++;
3144       //
3145       x[3]=F3n(x1,y1,x2,y2,z1,z2,x[4]);
3146       //if (TMath::Abs(x[3]) > cuts[3]) continue;
3147       nin2++;
3148       //
3149       //
3150       Double_t sy1=0.1,  sz1=0.1;
3151       Double_t sy2=0.1,  sz2=0.1;
3152       Double_t sy3=0.1,  sy=0.1, sz=0.1;
3153       
3154       Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
3155       Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
3156       Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
3157       Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
3158       Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
3159       Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
3160       
3161       Double_t f30=(F3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
3162       Double_t f31=(F3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
3163       Double_t f32=(F3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
3164       Double_t f34=(F3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
3165       
3166       c[0]=sy1;
3167       c[1]=0.;       c[2]=sz1; 
3168       c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3169       c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3170       c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3171       c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3172       c[13]=f30*sy1*f40+f32*sy2*f42;
3173       c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3174       
3175       //        if (!BuildSeed(kr1[is],kcl,0,x1,x2,x3,x,c)) continue;
3176       
3177       UInt_t index=kr1.GetIndex(is);
3178       AliTPCseed *track=new(seed) AliTPCseed(index, x, c, x1, sec*alpha+shift);
3179       
3180       track->fIsSeeding = kTRUE;
3181
3182       nin++;      
3183       FollowProlongation(*track, i1-7,1);
3184       if (track->GetNumberOfClusters() < track->fNFoundable*0.75 || 
3185           track->fNShared>0.6*track->GetNumberOfClusters() || ( track->GetSigmaY2()+ track->GetSigmaZ2())>0.6){
3186         seed->Reset();
3187         seed->~AliTPCseed();
3188         continue;
3189       }
3190       nout1++;
3191       nout2++;  
3192       //Int_t rc = 1;
3193       FollowProlongation(*track, i2,1);
3194       track->fBConstrain =0;
3195       track->fLastPoint = i1+fInnerSec->GetNRows();  // first cluster in track position
3196       track->fFirstPoint = track->fLastPoint;
3197       
3198       if (track->GetNumberOfClusters()<(i1-i2)*0.5 || 
3199           track->GetNumberOfClusters()<track->fNFoundable*0.7 || 
3200           track->fNShared>2. || track->GetChi2()/track->GetNumberOfClusters()>6 || ( track->GetSigmaY2()+ track->GetSigmaZ2())>0.5 ) {
3201         seed->Reset();
3202         seed->~AliTPCseed();
3203         continue;
3204       }
3205    
3206       {
3207         FollowProlongation(*track, TMath::Max(i2-10,0),1);
3208         AliTPCseed * track2 = MakeSeed(track,0.2,0.5,0.9);
3209         FollowProlongation(*track2, i2,1);
3210         track2->fBConstrain = kFALSE;
3211         track2->fSeedType = 4;
3212         arr->AddLast(track2); 
3213         seed->Reset();
3214         seed->~AliTPCseed();
3215       }
3216       
3217    
3218       //arr->AddLast(track); 
3219       //seed = new AliTPCseed;  
3220       nout3++;
3221     }
3222   }
3223   
3224   if (fDebug>3){
3225     Info("MakeSeeds5","\nSeeding statiistic:\t%d\t%d\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin,nout1,nout2,nout3);
3226   }
3227   delete seed;
3228 }
3229
3230
3231 //_____________________________________________________________________________
3232 void AliTPCtrackerMI::MakeSeeds2(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2, Float_t */*cuts[4]*/,
3233                                  Float_t deltay, Bool_t /*bconstrain*/) {
3234   //-----------------------------------------------------------------
3235   // This function creates track seeds - without vertex constraint
3236   //-----------------------------------------------------------------
3237   // cuts[0]   - fP4 cut        - not applied
3238   // cuts[1]   - tan(phi)  cut
3239   // cuts[2]   - zvertex cut    - not applied 
3240   // cuts[3]   - fP3 cut
3241   Int_t nin0=0;
3242   Int_t nin1=0;
3243   Int_t nin2=0;
3244   Int_t nin3=0;
3245   //  Int_t nin4=0;
3246   //Int_t nin5=0;
3247
3248   
3249
3250   Double_t alpha=fOuterSec->GetAlpha(), shift=fOuterSec->GetAlphaShift();
3251   //  Double_t cs=cos(alpha), sn=sin(alpha);
3252   Int_t row0 = (i1+i2)/2;
3253   Int_t drow = (i1-i2)/2;
3254   const AliTPCRow& kr0=fSectors[sec][row0];
3255   AliTPCRow * kr=0;
3256
3257   AliTPCpolyTrack polytrack;
3258   Int_t nclusters=fSectors[sec][row0];
3259   AliTPCseed * seed = new AliTPCseed;
3260
3261   Int_t sumused=0;
3262   Int_t cused=0;
3263   Int_t cnused=0;
3264   for (Int_t is=0; is < nclusters; is++) {  //LOOP over clusters
3265     Int_t nfound =0;
3266     Int_t nfoundable =0;
3267     for (Int_t iter =1; iter<2; iter++){   //iterations
3268       const AliTPCRow& krm=fSectors[sec][row0-iter];
3269       const AliTPCRow& krp=fSectors[sec][row0+iter];      
3270       const AliTPCclusterMI * cl= kr0[is];
3271       
3272       if (cl->IsUsed(10)) {
3273         cused++;
3274       }
3275       else{
3276         cnused++;
3277       }
3278       Double_t x = kr0.GetX();
3279       // Initialization of the polytrack
3280       nfound =0;
3281       nfoundable =0;
3282       polytrack.Reset();
3283       //
3284       Double_t y0= cl->GetY();
3285       Double_t z0= cl->GetZ();
3286       Float_t erry = 0;
3287       Float_t errz = 0;
3288       
3289       Double_t ymax = fSectors->GetMaxY(row0)-kr0.fDeadZone-1.5;
3290       if (deltay>0 && TMath::Abs(ymax-TMath::Abs(y0))> deltay ) continue;  // seed only at the edge
3291       
3292       erry = (0.5)*cl->GetSigmaY2()/TMath::Sqrt(cl->GetQ())*6;      
3293       errz = (0.5)*cl->GetSigmaZ2()/TMath::Sqrt(cl->GetQ())*6;      
3294       polytrack.AddPoint(x,y0,z0,erry, errz);
3295
3296       sumused=0;
3297       if (cl->IsUsed(10)) sumused++;
3298
3299
3300       Float_t roady = (5*TMath::Sqrt(cl->GetSigmaY2()+0.2)+1.)*iter;
3301       Float_t roadz = (5*TMath::Sqrt(cl->GetSigmaZ2()+0.2)+1.)*iter;
3302       //
3303       x = krm.GetX();
3304       AliTPCclusterMI * cl1 = krm.FindNearest(y0,z0,roady,roadz);
3305       if (cl1 && TMath::Abs(ymax-TMath::Abs(y0))) {
3306         erry = (0.5)*cl1->GetSigmaY2()/TMath::Sqrt(cl1->GetQ())*3;          
3307         errz = (0.5)*cl1->GetSigmaZ2()/TMath::Sqrt(cl1->GetQ())*3;
3308         if (cl1->IsUsed(10))  sumused++;
3309         polytrack.AddPoint(x,cl1->GetY(),cl1->GetZ(),erry,errz);
3310       }
3311       //
3312       x = krp.GetX();
3313       AliTPCclusterMI * cl2 = krp.FindNearest(y0,z0,roady,roadz);
3314       if (cl2) {
3315         erry = (0.5)*cl2->GetSigmaY2()/TMath::Sqrt(cl2->GetQ())*3;          
3316         errz = (0.5)*cl2->GetSigmaZ2()/TMath::Sqrt(cl2->GetQ())*3;
3317         if (cl2->IsUsed(10)) sumused++;  
3318         polytrack.AddPoint(x,cl2->GetY(),cl2->GetZ(),erry,errz);
3319       }
3320       //
3321       if (sumused>0) continue;
3322       nin0++;
3323       polytrack.UpdateParameters();
3324       // follow polytrack
3325       roadz = 1.2;
3326       roady = 1.2;
3327       //
3328       Double_t yn,zn;
3329       nfoundable = polytrack.GetN();
3330       nfound     = nfoundable; 
3331       //
3332       for (Int_t ddrow = iter+1; ddrow<drow;ddrow++){
3333         Float_t maxdist = 0.8*(1.+3./(ddrow));
3334         for (Int_t delta = -1;delta<=1;delta+=2){
3335           Int_t row = row0+ddrow*delta;
3336           kr = &(fSectors[sec][row]);
3337           Double_t xn = kr->GetX();
3338           Double_t ymax = fSectors->GetMaxY(row)-kr->fDeadZone-1.5;
3339           polytrack.GetFitPoint(xn,yn,zn);
3340           if (TMath::Abs(yn)>ymax) continue;
3341           nfoundable++;
3342           AliTPCclusterMI * cln = kr->FindNearest(yn,zn,roady,roadz);
3343           if (cln) {
3344             Float_t dist =  TMath::Sqrt(  (yn-cln->GetY())*(yn-cln->GetY())+(zn-cln->GetZ())*(zn-cln->GetZ()));
3345             if (dist<maxdist){
3346               /*
3347               erry = (dist+0.3)*cln->GetSigmaY2()/TMath::Sqrt(cln->GetQ())*(1.+1./(ddrow));         
3348               errz = (dist+0.3)*cln->GetSigmaZ2()/TMath::Sqrt(cln->GetQ())*(1.+1./(ddrow));
3349               if (cln->IsUsed(10)) {
3350                 //      printf("used\n");
3351                 sumused++;
3352                 erry*=2;
3353                 errz*=2;
3354               }
3355               */
3356               erry=0.1;
3357               errz=0.1;
3358               polytrack.AddPoint(xn,cln->GetY(),cln->GetZ(),erry, errz);
3359               nfound++;
3360             }
3361           }
3362         }
3363         if ( (sumused>3) || (sumused>0.5*nfound) || (nfound<0.6*nfoundable))  break;     
3364         polytrack.UpdateParameters();
3365       }           
3366     }
3367     if ( (sumused>3) || (sumused>0.5*nfound))  {
3368       //printf("sumused   %d\n",sumused);
3369       continue;
3370     }
3371     nin1++;
3372     Double_t dy,dz;
3373     polytrack.GetFitDerivation(kr0.GetX(),dy,dz);
3374     AliTPCpolyTrack track2;
3375     
3376     polytrack.Refit(track2,0.5+TMath::Abs(dy)*0.3,0.4+TMath::Abs(dz)*0.3);
3377     if (track2.GetN()<0.5*nfoundable) continue;
3378     nin2++;
3379
3380     if ((nfound>0.6*nfoundable) &&( nfoundable>0.4*(i1-i2))) {
3381       //
3382       // test seed with and without constrain
3383       for (Int_t constrain=0; constrain<=0;constrain++){
3384         // add polytrack candidate
3385
3386         Double_t x[5], c[15];
3387         Double_t x1,x2,x3,y1,y2,y3,z1,z2,z3;
3388         track2.GetBoundaries(x3,x1);    
3389         x2 = (x1+x3)/2.;
3390         track2.GetFitPoint(x1,y1,z1);
3391         track2.GetFitPoint(x2,y2,z2);
3392         track2.GetFitPoint(x3,y3,z3);
3393         //
3394         //is track pointing to the vertex ?
3395         Double_t x0,y0,z0;
3396         x0=0;
3397         polytrack.GetFitPoint(x0,y0,z0);
3398
3399         if (constrain) {
3400           x2 = x3;
3401           y2 = y3;
3402           z2 = z3;
3403           
3404           x3 = 0;
3405           y3 = 0;
3406           z3 = 0;
3407         }
3408         x[0]=y1;
3409         x[1]=z1;
3410         x[4]=F1(x1,y1,x2,y2,x3,y3);
3411                 
3412         //      if (TMath::Abs(x[4]) >= cuts[0]) continue;  //
3413         x[2]=F2(x1,y1,x2,y2,x3,y3);
3414         
3415         //if (TMath::Abs(x[4]*x1-x[2]) >= cuts[1]) continue;
3416         //x[3]=F3(x1,y1,x2,y2,z1,z2);
3417         x[3]=F3n(x1,y1,x3,y3,z1,z3,x[4]);
3418         //if (TMath::Abs(x[3]) > cuts[3]) continue;
3419
3420         
3421         Double_t sy =0.1, sz =0.1;
3422         Double_t sy1=0.02, sz1=0.02;
3423         Double_t sy2=0.02, sz2=0.02;
3424         Double_t sy3=0.02;
3425
3426         if (constrain){
3427           sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
3428         }
3429         
3430         Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
3431         Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
3432         Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
3433         Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
3434         Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
3435         Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
3436
3437         Double_t f30=(F3(x1,y1+sy,x3,y3,z1,z3)-x[3])/sy;
3438         Double_t f31=(F3(x1,y1,x3,y3,z1+sz,z3)-x[3])/sz;
3439         Double_t f32=(F3(x1,y1,x3,y3+sy,z1,z3)-x[3])/sy;
3440         Double_t f34=(F3(x1,y1,x3,y3,z1,z3+sz)-x[3])/sz;
3441
3442         
3443         c[0]=sy1;
3444         c[1]=0.;       c[2]=sz1;
3445         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3446         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3447         c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3448         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3449         c[13]=f30*sy1*f40+f32*sy2*f42;
3450         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3451         
3452         //Int_t row1 = fSectors->GetRowNumber(x1);
3453         Int_t row1 = GetRowNumber(x1);
3454
3455         UInt_t index=0;
3456         //kr0.GetIndex(is);
3457         AliTPCseed *track=new (seed) AliTPCseed(index, x, c, x1, sec*alpha+shift);
3458         track->fIsSeeding = kTRUE;
3459         Int_t rc=FollowProlongation(*track, i2);        
3460         if (constrain) track->fBConstrain =1;
3461         else
3462           track->fBConstrain =0;
3463         track->fLastPoint = row1+fInnerSec->GetNRows();  // first cluster in track position
3464         track->fFirstPoint = track->fLastPoint;
3465
3466         if (rc==0 || track->GetNumberOfClusters()<(i1-i2)*0.5 || 
3467             track->GetNumberOfClusters() < track->fNFoundable*0.6 || 
3468             track->fNShared>0.4*track->GetNumberOfClusters()) {
3469           //delete track;
3470           seed->Reset();
3471           seed->~AliTPCseed();
3472         }
3473         else {
3474           arr->AddLast(track);
3475           seed = new AliTPCseed;
3476         }
3477         nin3++;
3478       }
3479     }  // if accepted seed
3480   }
3481   if (fDebug>3){
3482     Info("MakeSeeds2","\nSeeding statiistic:\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin3);
3483   }
3484   delete seed;
3485 }
3486
3487
3488 AliTPCseed *AliTPCtrackerMI::MakeSeed(AliTPCseed *track, Float_t r0, Float_t r1, Float_t r2)
3489 {
3490   //
3491   //
3492   //reseed using track points
3493   Int_t p0 = int(r0*track->GetNumberOfClusters());     // point 0 
3494   Int_t p1 = int(r1*track->GetNumberOfClusters());
3495   Int_t p2 = int(r2*track->GetNumberOfClusters());   // last point
3496   Int_t pp2=0;
3497   Double_t  x0[3],x1[3],x2[3];
3498   for (Int_t i=0;i<3;i++){
3499     x0[i]=-1;
3500     x1[i]=-1;
3501     x2[i]=-1;
3502   }
3503
3504   // find track position at given ratio of the length
3505   Int_t  sec0=0, sec1=0, sec2=0;
3506   Int_t index=-1;
3507   Int_t clindex;
3508   for (Int_t i=0;i<160;i++){
3509     if (track->fClusterPointer[i]){
3510       index++;
3511       AliTPCTrackerPoint   *trpoint =track->GetTrackPoint(i);
3512       if ( (index<p0) || x0[0]<0 ){
3513         if (trpoint->GetX()>1){
3514           clindex = track->GetClusterIndex2(i);
3515           if (clindex>0){       
3516             x0[0] = trpoint->GetX();
3517             x0[1] = trpoint->GetY();
3518             x0[2] = trpoint->GetZ();
3519             sec0  = ((clindex&0xff000000)>>24)%18;
3520           }
3521         }
3522       }
3523
3524       if ( (index<p1) &&(trpoint->GetX()>1)){
3525         clindex = track->GetClusterIndex2(i);
3526         if (clindex>0){
3527           x1[0] = trpoint->GetX();
3528           x1[1] = trpoint->GetY();
3529           x1[2] = trpoint->GetZ();
3530           sec1  = ((clindex&0xff000000)>>24)%18;
3531         }
3532       }
3533       if ( (index<p2) &&(trpoint->GetX()>1)){
3534         clindex = track->GetClusterIndex2(i);
3535         if (clindex>0){
3536           x2[0] = trpoint->GetX();
3537           x2[1] = trpoint->GetY();
3538           x2[2] = trpoint->GetZ(); 
3539           sec2  = ((clindex&0xff000000)>>24)%18;
3540           pp2 = i;
3541         }
3542       }
3543     }
3544   }
3545   
3546   Double_t alpha, cs,sn, xx2,yy2;
3547   //
3548   alpha = (sec1-sec2)*fSectors->GetAlpha();
3549   cs = TMath::Cos(alpha);
3550   sn = TMath::Sin(alpha); 
3551   xx2= x1[0]*cs-x1[1]*sn;
3552   yy2= x1[0]*sn+x1[1]*cs;
3553   x1[0] = xx2;
3554   x1[1] = yy2;
3555   //
3556   alpha = (sec0-sec2)*fSectors->GetAlpha();
3557   cs = TMath::Cos(alpha);
3558   sn = TMath::Sin(alpha); 
3559   xx2= x0[0]*cs-x0[1]*sn;
3560   yy2= x0[0]*sn+x0[1]*cs;
3561   x0[0] = xx2;
3562   x0[1] = yy2;
3563   //
3564   //
3565   //
3566   Double_t x[5],c[15];
3567   //
3568   x[0]=x2[1];
3569   x[1]=x2[2];
3570   x[4]=F1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
3571   //  if (x[4]>1) return 0;
3572   x[2]=F2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
3573   x[3]=F3n(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2],x[4]);
3574   //if (TMath::Abs(x[3]) > 2.2)  return 0;
3575   //if (TMath::Abs(x[2]) > 1.99) return 0;
3576   //  
3577   Double_t sy =0.1,  sz =0.1;
3578   //
3579   Double_t sy1=0.02+track->GetSigmaY2(), sz1=0.02+track->GetSigmaZ2();
3580   Double_t sy2=0.01+track->GetSigmaY2(), sz2=0.01+track->GetSigmaZ2();
3581   Double_t sy3=0.01+track->GetSigmaY2();
3582   //
3583   Double_t f40=(F1(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[4])/sy;
3584   Double_t f42=(F1(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[4])/sy;
3585   Double_t f43=(F1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[4])/sy;
3586   Double_t f20=(F2(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[2])/sy;
3587   Double_t f22=(F2(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[2])/sy;
3588   Double_t f23=(F2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[2])/sy;
3589   //
3590   Double_t f30=(F3(x2[0],x2[1]+sy,x0[0],x0[1],x2[2],x0[2])-x[3])/sy;
3591   Double_t f31=(F3(x2[0],x2[1],x0[0],x0[1],x2[2]+sz,x0[2])-x[3])/sz;
3592   Double_t f32=(F3(x2[0],x2[1],x0[0],x0[1]+sy,x2[2],x0[2])-x[3])/sy;
3593   Double_t f34=(F3(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2]+sz)-x[3])/sz;
3594   
3595   
3596   c[0]=sy1;
3597   c[1]=0.;       c[2]=sz1;
3598   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3599   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3600   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3601   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3602   c[13]=f30*sy1*f40+f32*sy2*f42;
3603   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3604   
3605   //  Int_t row1 = fSectors->GetRowNumber(x2[0]);
3606   AliTPCseed *seed=new  AliTPCseed(0, x, c, x2[0], sec2*fSectors->GetAlpha()+fSectors->GetAlphaShift());
3607   //  Double_t y0,z0,y1,z1, y2,z2;
3608   //seed->GetProlongation(x0[0],y0,z0);
3609   // seed->GetProlongation(x1[0],y1,z1);
3610   //seed->GetProlongation(x2[0],y2,z2);
3611   //  seed =0;
3612   seed->fLastPoint  = pp2;
3613   seed->fFirstPoint = pp2;
3614   
3615
3616   return seed;
3617 }
3618
3619
3620 AliTPCseed *AliTPCtrackerMI::ReSeed(AliTPCseed *track, Float_t r0, Float_t r1, Float_t r2)
3621 {
3622   //
3623   //
3624   //reseed using founded clusters 
3625   //
3626   // Find the number of clusters
3627   Int_t nclusters = 0;
3628   for (Int_t irow=0;irow<160;irow++){
3629     if (track->GetClusterIndex(irow)>0) nclusters++;
3630   }
3631   //
3632   Int_t ipos[3];
3633   ipos[0] = TMath::Max(int(r0*nclusters),0);             // point 0 cluster
3634   ipos[1] = TMath::Min(int(r1*nclusters),nclusters-1);   // 
3635   ipos[2] = TMath::Min(int(r2*nclusters),nclusters-1);   // last point
3636   //
3637   //
3638   Double_t  xyz[3][3];
3639   Int_t     row[3],sec[3]={0,0,0};
3640   //
3641   // find track row position at given ratio of the length
3642   Int_t index=-1;
3643   for (Int_t irow=0;irow<160;irow++){    
3644     if (track->GetClusterIndex2(irow)<0) continue;
3645     index++;
3646     for (Int_t ipoint=0;ipoint<3;ipoint++){
3647       if (index<=ipos[ipoint]) row[ipoint] = irow;
3648     }        
3649   }
3650   //
3651   //Get cluster and sector position
3652   for (Int_t ipoint=0;ipoint<3;ipoint++){
3653     Int_t clindex = track->GetClusterIndex2(row[ipoint]);
3654     AliTPCclusterMI * cl = GetClusterMI(clindex);
3655     if (cl==0) {
3656       //Error("Bug\n");
3657       //      AliTPCclusterMI * cl = GetClusterMI(clindex);
3658       return 0;
3659     }
3660     sec[ipoint]     = ((clindex&0xff000000)>>24)%18;
3661     xyz[ipoint][0]  = GetXrow(row[ipoint]);
3662     xyz[ipoint][1]  = cl->GetY();
3663     xyz[ipoint][2]  = cl->GetZ();
3664   }
3665   //
3666   //
3667   // Calculate seed state vector and covariance matrix
3668
3669   Double_t alpha, cs,sn, xx2,yy2;
3670   //
3671   alpha = (sec[1]-sec[2])*fSectors->GetAlpha();
3672   cs = TMath::Cos(alpha);
3673   sn = TMath::Sin(alpha); 
3674   xx2= xyz[1][0]*cs-xyz[1][1]*sn;
3675   yy2= xyz[1][0]*sn+xyz[1][1]*cs;
3676   xyz[1][0] = xx2;
3677   xyz[1][1] = yy2;
3678   //
3679   alpha = (sec[0]-sec[2])*fSectors->GetAlpha();
3680   cs = TMath::Cos(alpha);
3681   sn = TMath::Sin(alpha); 
3682   xx2= xyz[0][0]*cs-xyz[0][1]*sn;
3683   yy2= xyz[0][0]*sn+xyz[0][1]*cs;
3684   xyz[0][0] = xx2;
3685   xyz[0][1] = yy2;
3686   //
3687   //
3688   //
3689   Double_t x[5],c[15];
3690   //
3691   x[0]=xyz[2][1];
3692   x[1]=xyz[2][2];
3693   x[4]=F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3694   x[2]=F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3695   x[3]=F3n(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2],x[4]);
3696   //  
3697   Double_t sy =0.1,  sz =0.1;
3698   //
3699   Double_t sy1=0.2, sz1=0.2;
3700   Double_t sy2=0.2, sz2=0.2;
3701   Double_t sy3=0.2;
3702   //
3703   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;
3704   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;
3705   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;
3706   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;
3707   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;
3708   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;
3709   //
3710   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;
3711   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;
3712   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;
3713   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;
3714   
3715   
3716   c[0]=sy1;
3717   c[1]=0.;       c[2]=sz1;
3718   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3719   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3720   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3721   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3722   c[13]=f30*sy1*f40+f32*sy2*f42;
3723   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3724   
3725   //  Int_t row1 = fSectors->GetRowNumber(xyz[2][0]);
3726   AliTPCseed *seed=new  AliTPCseed(0, x, c, xyz[2][0], sec[2]*fSectors->GetAlpha()+fSectors->GetAlphaShift());
3727   seed->fLastPoint  = row[2];
3728   seed->fFirstPoint = row[2];  
3729   return seed;
3730 }
3731
3732
3733 AliTPCseed *AliTPCtrackerMI::ReSeed(AliTPCseed *track,Int_t r0, Bool_t forward)
3734 {
3735   //
3736   //
3737   //reseed using founded clusters 
3738   //
3739   Double_t  xyz[3][3];
3740   Int_t     row[3]={0,0,0};
3741   Int_t     sec[3]={0,0,0};
3742   //
3743   // forward direction
3744   if (forward){
3745     for (Int_t irow=r0;irow<160;irow++){
3746       if (track->GetClusterIndex(irow)>0){
3747         row[0] = irow;
3748         break;
3749       }
3750     }
3751     for (Int_t irow=160;irow>r0;irow--){
3752       if (track->GetClusterIndex(irow)>0){
3753         row[2] = irow;
3754         break;
3755       }
3756     }
3757     for (Int_t irow=row[2]-15;irow>row[0];irow--){
3758       if (track->GetClusterIndex(irow)>0){
3759         row[1] = irow;
3760         break;
3761       }
3762     }
3763     //
3764   }
3765   if (!forward){
3766     for (Int_t irow=0;irow<r0;irow++){
3767       if (track->GetClusterIndex(irow)>0){
3768         row[0] = irow;
3769         break;
3770       }
3771     }
3772     for (Int_t irow=r0;irow>0;irow--){
3773       if (track->GetClusterIndex(irow)>0){
3774         row[2] = irow;
3775         break;
3776       }
3777     }    
3778     for (Int_t irow=row[2]-15;irow>row[0];irow--){
3779       if (track->GetClusterIndex(irow)>0){
3780         row[1] = irow;
3781         break;
3782       }
3783     } 
3784   }
3785   //
3786   if ((row[2]-row[0])<20) return 0;
3787   if (row[1]==0) return 0;
3788   //
3789   //
3790   //Get cluster and sector position
3791   for (Int_t ipoint=0;ipoint<3;ipoint++){
3792     Int_t clindex = track->GetClusterIndex2(row[ipoint]);
3793     AliTPCclusterMI * cl = GetClusterMI(clindex);
3794     if (cl==0) {
3795       //Error("Bug\n");
3796       //      AliTPCclusterMI * cl = GetClusterMI(clindex);
3797       return 0;
3798     }
3799     sec[ipoint]     = ((clindex&0xff000000)>>24)%18;
3800     xyz[ipoint][0]  = GetXrow(row[ipoint]);
3801     AliTPCTrackerPoint * point = track->GetTrackPoint(row[ipoint]);    
3802     if (point&&ipoint<2){
3803       //
3804        xyz[ipoint][1]  = point->GetY();
3805        xyz[ipoint][2]  = point->GetZ();
3806     }
3807     else{
3808       xyz[ipoint][1]  = cl->GetY();
3809       xyz[ipoint][2]  = cl->GetZ();
3810     }
3811   }
3812   //
3813   //
3814   //
3815   //
3816   // Calculate seed state vector and covariance matrix
3817
3818   Double_t alpha, cs,sn, xx2,yy2;
3819   //
3820   alpha = (sec[1]-sec[2])*fSectors->GetAlpha();
3821   cs = TMath::Cos(alpha);
3822   sn = TMath::Sin(alpha); 
3823   xx2= xyz[1][0]*cs-xyz[1][1]*sn;
3824   yy2= xyz[1][0]*sn+xyz[1][1]*cs;
3825   xyz[1][0] = xx2;
3826   xyz[1][1] = yy2;
3827   //
3828   alpha = (sec[0]-sec[2])*fSectors->GetAlpha();
3829   cs = TMath::Cos(alpha);
3830   sn = TMath::Sin(alpha); 
3831   xx2= xyz[0][0]*cs-xyz[0][1]*sn;
3832   yy2= xyz[0][0]*sn+xyz[0][1]*cs;
3833   xyz[0][0] = xx2;
3834   xyz[0][1] = yy2;
3835   //
3836   //
3837   //
3838   Double_t x[5],c[15];
3839   //
3840   x[0]=xyz[2][1];
3841   x[1]=xyz[2][2];
3842   x[4]=F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3843   x[2]=F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3844   x[3]=F3n(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2],x[4]);
3845   //  
3846   Double_t sy =0.1,  sz =0.1;
3847   //
3848   Double_t sy1=0.2, sz1=0.2;
3849   Double_t sy2=0.2, sz2=0.2;
3850   Double_t sy3=0.2;
3851   //
3852   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;
3853   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;
3854   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;
3855   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;
3856   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;
3857   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;
3858   //
3859   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;
3860   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;
3861   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;
3862   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;
3863   
3864   
3865   c[0]=sy1;
3866   c[1]=0.;       c[2]=sz1;
3867   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3868   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3869   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3870   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3871   c[13]=f30*sy1*f40+f32*sy2*f42;
3872   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3873   
3874   //  Int_t row1 = fSectors->GetRowNumber(xyz[2][0]);
3875   AliTPCseed *seed=new  AliTPCseed(0, x, c, xyz[2][0], sec[2]*fSectors->GetAlpha()+fSectors->GetAlphaShift());
3876   seed->fLastPoint  = row[2];
3877   seed->fFirstPoint = row[2];  
3878   for (Int_t i=row[0];i<row[2];i++){
3879     seed->fIndex[i] = track->fIndex[i];
3880   }
3881
3882   return seed;
3883 }
3884
3885 void  AliTPCtrackerMI::FindKinks(TObjArray * array, AliESD *esd)
3886 {
3887   //
3888   //  find kinks
3889   //
3890   //
3891
3892   TObjArray *kinks= new TObjArray(10000);
3893   //  TObjArray *v0s= new TObjArray(10000);
3894   Int_t nentries = array->GetEntriesFast();
3895   AliHelix *helixes      = new AliHelix[nentries];
3896   Int_t    *sign         = new Int_t[nentries];
3897   Int_t    *nclusters    = new Int_t[nentries];
3898   Float_t  *alpha        = new Float_t[nentries];
3899   AliESDkink * kink      = new AliESDkink();
3900   Int_t      * usage     = new Int_t[nentries];
3901   Float_t  *zm           = new Float_t[nentries];
3902   Float_t  *z0           = new Float_t[nentries]; 
3903   Float_t  *fim          = new Float_t[nentries];
3904   Float_t  *shared       = new Float_t[nentries];
3905   Bool_t   *circular     = new Bool_t[nentries];
3906   Float_t *dca          = new Float_t[nentries];
3907   //const AliESDVertex * primvertex = esd->GetVertex();
3908   //
3909   //  nentries = array->GetEntriesFast();
3910   //
3911   
3912   //
3913   //
3914   for (Int_t i=0;i<nentries;i++){
3915     sign[i]=0;
3916     usage[i]=0;
3917     AliTPCseed* track = (AliTPCseed*)array->At(i);    
3918     if (!track) continue;
3919     track->fCircular =0;
3920     shared[i] = kFALSE;
3921     track->UpdatePoints();
3922     if (( track->GetPoints()[2]- track->GetPoints()[0])>5 && track->GetPoints()[3]>0.8){
3923     }
3924     nclusters[i]=track->GetNumberOfClusters();
3925     alpha[i] = track->GetAlpha();
3926     new (&helixes[i]) AliHelix(*track);
3927     Double_t xyz[3];
3928     helixes[i].Evaluate(0,xyz);
3929     sign[i] = (track->GetC()>0) ? -1:1;
3930     Double_t x,y,z;
3931     x=160;
3932     if (track->GetProlongation(x,y,z)){
3933       zm[i]  = z;
3934       fim[i] = alpha[i]+TMath::ATan2(y,x);
3935     }
3936     else{
3937       zm[i]  = track->GetZ();
3938       fim[i] = alpha[i];
3939     }   
3940     z0[i]=1000;
3941     circular[i]= kFALSE;
3942     if (track->GetProlongation(0,y,z))  z0[i] = z;
3943     dca[i] = track->GetD(0,0);    
3944   }
3945   //
3946   //
3947   TStopwatch timer;
3948   timer.Start();
3949   Int_t ncandidates =0;
3950   Int_t nall =0;
3951   Int_t ntracks=0; 
3952   Double_t phase[2][2],radius[2];
3953
3954   //
3955   // Find circling track
3956   TTreeSRedirector &cstream = *fDebugStreamer;
3957   //
3958   for (Int_t i0=0;i0<nentries;i0++){
3959     AliTPCseed * track0 = (AliTPCseed*)array->At(i0);
3960     if (!track0) continue;    
3961     if (track0->fN<40) continue;
3962     if (TMath::Abs(1./track0->fP4)>200) continue;
3963     for (Int_t i1=i0+1;i1<nentries;i1++){
3964       AliTPCseed * track1 = (AliTPCseed*)array->At(i1);
3965       if (!track1) continue;
3966       if (track1->fN<40)                  continue;
3967       if ( TMath::Abs(track1->fP3+track0->fP3)>0.1) continue;
3968       if (track0->fBConstrain&&track1->fBConstrain) continue;
3969       if (TMath::Abs(1./track1->fP4)>200) continue;
3970       if (track1->fP4*track0->fP4>0)      continue;
3971       if (track1->fP3*track0->fP3>0)      continue;
3972       if (max(TMath::Abs(1./track0->fP4),TMath::Abs(1./track1->fP4))>190) continue;
3973       if (track0->fBConstrain&&TMath::Abs(track1->fP4)<TMath::Abs(track0->fP4)) continue; //returning - lower momenta
3974       if (track1->fBConstrain&&TMath::Abs(track0->fP4)<TMath::Abs(track1->fP4)) continue; //returning - lower momenta
3975       //
3976       Float_t mindcar = TMath::Min(TMath::Abs(dca[i0]),TMath::Abs(dca[i1]));
3977       if (mindcar<5)   continue;
3978       Float_t mindcaz = TMath::Min(TMath::Abs(z0[i0]-GetZ()),TMath::Abs(z0[i1]-GetZ()));
3979       if (mindcaz<5) continue;
3980       if (mindcar+mindcaz<20) continue;
3981       //
3982       //
3983       Float_t xc0 = helixes[i0].GetHelix(6);
3984       Float_t yc0 = helixes[i0].GetHelix(7);
3985       Float_t r0  = helixes[i0].GetHelix(8);
3986       Float_t xc1 = helixes[i1].GetHelix(6);
3987       Float_t yc1 = helixes[i1].GetHelix(7);
3988       Float_t r1  = helixes[i1].GetHelix(8);
3989         
3990       Float_t rmean = (r0+r1)*0.5;
3991       Float_t delta =TMath::Sqrt((xc1-xc0)*(xc1-xc0)+(yc1-yc0)*(yc1-yc0));
3992       //if (delta>30) continue;
3993       if (delta>rmean*0.25) continue;
3994       if (TMath::Abs(r0-r1)/rmean>0.3) continue; 
3995       //
3996       Int_t npoints = helixes[i0].GetRPHIintersections(helixes[i1], phase, radius,10);
3997       if (npoints==0) continue;
3998       helixes[i0].GetClosestPhases(helixes[i1], phase);
3999       //
4000       Double_t xyz0[3];
4001       Double_t xyz1[3];
4002       Double_t hangles[3];
4003       helixes[i0].Evaluate(phase[0][0],xyz0);
4004       helixes[i1].Evaluate(phase[0][1],xyz1);
4005
4006       helixes[i0].GetAngle(phase[0][0],helixes[i1],phase[0][1],hangles);
4007       Double_t deltah[2],deltabest;
4008       if (hangles[2]<2.8) continue;
4009       /*
4010       cstream<<"C"<<track0->fLab<<track1->fLab<<
4011         track0->fP3<<track1->fP3<<
4012         track0->fP4<<track1->fP4<<
4013         delta<<rmean<<npoints<<
4014         hangles[0]<<hangles[2]<<
4015         xyz0[2]<<xyz1[2]<<radius[0]<<"\n"; 
4016       */
4017       if (npoints>0){
4018         Int_t ibest=0;
4019         helixes[i0].ParabolicDCA(helixes[i1],phase[0][0],phase[0][1],radius[0],deltah[0],2);
4020         if (npoints==2){
4021           helixes[i0].ParabolicDCA(helixes[i1],phase[1][0],phase[1][1],radius[1],deltah[1],2);
4022           if (deltah[1]<deltah[0]) ibest=1;
4023         }
4024         deltabest  = TMath::Sqrt(deltah[ibest]);
4025         helixes[i0].Evaluate(phase[ibest][0],xyz0);
4026         helixes[i1].Evaluate(phase[ibest][1],xyz1);
4027         helixes[i0].GetAngle(phase[ibest][0],helixes[i1],phase[ibest][1],hangles);
4028         Double_t radiusbest = TMath::Sqrt(radius[ibest]);
4029         //
4030         if (deltabest>6) continue;
4031         if (mindcar+mindcaz<40 && (hangles[2]<3.12||deltabest>3)) continue;
4032         Bool_t sign =kFALSE;
4033         if (hangles[2]>3.06) sign =kTRUE;
4034         //
4035         if (sign){
4036           circular[i0] = kTRUE;
4037           circular[i1] = kTRUE;
4038           if (TMath::Abs(track0->fP4)<TMath::Abs(track1->fP4)){
4039             track0->fCircular += 1;
4040             track1->fCircular += 2;
4041           }
4042           else{
4043             track1->fCircular += 1;
4044             track0->fCircular += 2;
4045           }
4046         }               
4047         if (sign){        
4048           //debug stream
4049           cstream<<"Curling"<<
4050             "lab0="<<track0->fLab<<
4051             "lab1="<<track1->fLab<<   
4052             "Tr0.="<<track0<<
4053             "Tr1.="<<track1<<      
4054             "dca0="<<dca[i0]<<
4055             "dca1="<<dca[i1]<<
4056             "mindcar="<<mindcar<<
4057             "mindcaz="<<mindcaz<<
4058             "delta="<<delta<<
4059             "rmean="<<rmean<<
4060             "npoints="<<npoints<<                      
4061             "hangles0="<<hangles[0]<<
4062             "hangles2="<<hangles[2]<<                    
4063             "xyz0="<<xyz0[2]<<
4064             "xyzz1="<<xyz1[2]<<
4065             "z0="<<z0[i0]<<
4066             "z1="<<z0[i1]<<
4067             "radius="<<radiusbest<<
4068             "deltabest="<<deltabest<< 
4069             "phase0="<<phase[ibest][0]<<
4070             "phase1="<<phase[ibest][1]<<
4071             "\n";                 
4072         }
4073       }
4074     }
4075   }
4076   //
4077   //  Finf kinks loop
4078   // 
4079   //
4080   for (Int_t i =0;i<nentries;i++){
4081     if (sign[i]==0) continue;
4082     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4083     ntracks++;
4084     //
4085     Double_t cradius0 = 40*40;
4086     Double_t cradius1 = 270*270;
4087     Double_t cdist1=8.;
4088     Double_t cdist2=8.;
4089     Double_t cdist3=0.55; 
4090     for (Int_t j =i+1;j<nentries;j++){
4091       nall++;
4092       if (sign[j]*sign[i]<1) continue;
4093       if ( (nclusters[i]+nclusters[j])>200) continue;
4094       if ( (nclusters[i]+nclusters[j])<80) continue;
4095       if ( TMath::Abs(zm[i]-zm[j])>60.) continue;
4096       if ( TMath::Abs(fim[i]-fim[j])>0.6 && TMath::Abs(fim[i]-fim[j])<5.7 ) continue;
4097       //AliTPCseed * track1 = (AliTPCseed*)array->At(j);  Double_t phase[2][2],radius[2];    
4098       Int_t npoints = helixes[i].GetRPHIintersections(helixes[j], phase, radius,20);
4099       if (npoints<1) continue;
4100       // cuts on radius      
4101       if (npoints==1){
4102         if (radius[0]<cradius0||radius[0]>cradius1) continue;
4103       }
4104       else{
4105         if ( (radius[0]<cradius0||radius[0]>cradius1) && (radius[1]<cradius0||radius[1]>cradius1) ) continue;
4106       }
4107       //      
4108       Double_t delta1=10000,delta2=10000;
4109       // cuts on the intersection radius
4110       helixes[i].LinearDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta1);
4111       if (radius[0]<20&&delta1<1) continue; //intersection at vertex
4112       if (radius[0]<10&&delta1<3) continue; //intersection at vertex
4113       if (npoints==2){ 
4114         helixes[i].LinearDCA(helixes[j],phase[1][0],phase[1][1],radius[1],delta2);
4115         if (radius[1]<20&&delta2<1) continue;  //intersection at vertex
4116         if (radius[1]<10&&delta2<3) continue;  //intersection at vertex 
4117       }
4118       //
4119       Double_t distance1 = TMath::Min(delta1,delta2);
4120       if (distance1>cdist1) continue;  // cut on DCA linear approximation
4121       //
4122       npoints = helixes[i].GetRPHIintersections(helixes[j], phase, radius,20);
4123       helixes[i].ParabolicDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta1);
4124       if (radius[0]<20&&delta1<1) continue; //intersection at vertex
4125       if (radius[0]<10&&delta1<3) continue; //intersection at vertex
4126       //
4127       if (npoints==2){ 
4128         helixes[i].ParabolicDCA(helixes[j],phase[1][0],phase[1][1],radius[1],delta2);   
4129         if (radius[1]<20&&delta2<1) continue;  //intersection at vertex
4130         if (radius[1]<10&&delta2<3) continue;  //intersection at vertex 
4131       }            
4132       distance1 = TMath::Min(delta1,delta2);
4133       Float_t rkink =0;
4134       if (delta1<delta2){
4135         rkink = TMath::Sqrt(radius[0]);
4136       }
4137       else{
4138         rkink = TMath::Sqrt(radius[1]);
4139       }
4140       if (distance1>cdist2) continue;
4141       //
4142       //
4143       AliTPCseed * track1 = (AliTPCseed*)array->At(j);
4144       //
4145       //
4146       Int_t row0 = GetRowNumber(rkink); 
4147       if (row0<10)  continue;
4148       if (row0>150) continue;
4149       //
4150       //
4151       Float_t dens00=-1,dens01=-1;
4152       Float_t dens10=-1,dens11=-1;
4153       //
4154       Int_t found,foundable,shared;
4155       track0->GetClusterStatistic(0,row0-5, found, foundable,shared,kFALSE);
4156       if (foundable>5) dens00 = Float_t(found)/Float_t(foundable);
4157       track0->GetClusterStatistic(row0+5,155, found, foundable,shared,kFALSE);
4158       if (foundable>5) dens01 = Float_t(found)/Float_t(foundable);
4159       //
4160       track1->GetClusterStatistic(0,row0-5, found, foundable,shared,kFALSE);
4161       if (foundable>10) dens10 = Float_t(found)/Float_t(foundable);
4162       track1->GetClusterStatistic(row0+5,155, found, foundable,shared,kFALSE);
4163       if (foundable>10) dens11 = Float_t(found)/Float_t(foundable);
4164       //     
4165       if (dens00<dens10 && dens01<dens11) continue;
4166       if (dens00>dens10 && dens01>dens11) continue;
4167       if (TMath::Max(dens00,dens10)<0.1)  continue;
4168       if (TMath::Max(dens01,dens11)<0.3)  continue;
4169       //
4170       if (TMath::Min(dens00,dens10)>0.6)  continue;
4171       if (TMath::Min(dens01,dens11)>0.6)  continue;
4172
4173       //
4174       AliTPCseed * ktrack0, *ktrack1;
4175       if (dens00>dens10){
4176         ktrack0 = track0;
4177         ktrack1 = track1;
4178       }
4179       else{
4180         ktrack0 = track1;
4181         ktrack1 = track0;
4182       }
4183       if (TMath::Abs(ktrack0->GetC())>5) continue; // cut on the curvature for mother particle
4184       AliExternalTrackParam paramm(*ktrack0);
4185       AliExternalTrackParam paramd(*ktrack1);
4186       if (row0>60&&ktrack1->GetReference().X()>90.) new (&paramd) AliExternalTrackParam(ktrack1->GetReference()); 
4187       //
4188       //
4189       kink->SetMother(paramm);
4190       kink->SetDaughter(paramd);
4191       kink->Update();
4192
4193       Float_t x[3] = { kink->GetPosition()[0],kink->GetPosition()[1],kink->GetPosition()[2]};
4194       Int_t index[4];
4195       fParam->Transform0to1(x,index);
4196       fParam->Transform1to2(x,index);
4197       row0 = GetRowNumber(x[0]); 
4198
4199       if (kink->GetR()<100) continue;
4200       if (kink->GetR()>240) continue;
4201       if (kink->GetPosition()[2]/kink->GetR()>AliTPCReconstructor::GetCtgRange()) continue;  //out of fiducial volume
4202       if (kink->GetDistance()>cdist3) continue;
4203       Float_t dird = kink->GetDaughterP()[0]*kink->GetPosition()[0]+kink->GetDaughterP()[1]*kink->GetPosition()[1];  // rough direction estimate
4204       if (dird<0) continue;
4205
4206       Float_t dirm = kink->GetMotherP()[0]*kink->GetPosition()[0]+kink->GetMotherP()[1]*kink->GetPosition()[1];  // rough direction estimate
4207       if (dirm<0) continue;
4208       Float_t mpt = TMath::Sqrt(kink->GetMotherP()[0]*kink->GetMotherP()[0]+kink->GetMotherP()[1]*kink->GetMotherP()[1]);
4209       if (mpt<0.2) continue;
4210
4211       if (mpt<1){
4212         //for high momenta momentum not defined well in first iteration
4213         Double_t qt   =  TMath::Sin(kink->GetAngle(2))*ktrack1->P();
4214         if (qt>0.35) continue; 
4215       }
4216       
4217       kink->SetLabel(CookLabel(ktrack0,0.4,0,row0),0);
4218       kink->SetLabel(CookLabel(ktrack1,0.4,row0,160),1);
4219       if (dens00>dens10){
4220         kink->SetTPCDensity(dens00,0,0);
4221         kink->SetTPCDensity(dens01,0,1);
4222         kink->SetTPCDensity(dens10,1,0);
4223         kink->SetTPCDensity(dens11,1,1);
4224         kink->SetIndex(i,0);
4225         kink->SetIndex(j,1);
4226       }
4227       else{
4228         kink->SetTPCDensity(dens10,0,0);
4229         kink->SetTPCDensity(dens11,0,1);
4230         kink->SetTPCDensity(dens00,1,0);
4231         kink->SetTPCDensity(dens01,1,1);
4232         kink->SetIndex(j,0);
4233         kink->SetIndex(i,1);
4234       }
4235
4236       if (mpt<1||kink->GetAngle(2)>0.1){
4237         //      angle and densities  not defined yet
4238         if (kink->GetTPCDensityFactor()<0.8) continue;
4239         if ((2-kink->GetTPCDensityFactor())*kink->GetDistance() >0.25) continue;
4240         if (kink->GetAngle(2)*ktrack0->P()<0.003) continue; //too small angle
4241         if (kink->GetAngle(2)>0.2&&kink->GetTPCDensityFactor()<1.15) continue;
4242         if (kink->GetAngle(2)>0.2&&kink->GetTPCDensity(0,1)>0.05) continue;
4243
4244         Float_t criticalangle = track0->fC22+track0->fC33;
4245         criticalangle+= track1->fC22+track1->fC33;
4246         criticalangle= 3*TMath::Sqrt(criticalangle);
4247         if (criticalangle>0.02) criticalangle=0.02;
4248         if (kink->GetAngle(2)<criticalangle) continue;
4249       }
4250       //
4251       Int_t drow = Int_t(2.+0.5/(0.05+kink->GetAngle(2)));  // overlap region defined
4252       Float_t shapesum =0;
4253       Float_t sum = 0;
4254       for ( Int_t row = row0-drow; row<row0+drow;row++){
4255         if (row<0) continue;
4256         if (row>155) continue;
4257         if (ktrack0->fClusterPointer[row]){
4258           AliTPCTrackerPoint *point =ktrack0->GetTrackPoint(row);
4259           shapesum+=point->GetSigmaY()+point->GetSigmaZ();
4260           sum++;
4261         }
4262         if (ktrack1->fClusterPointer[row]){
4263           AliTPCTrackerPoint *point =ktrack1->GetTrackPoint(row);
4264           shapesum+=point->GetSigmaY()+point->GetSigmaZ();
4265           sum++;
4266         }       
4267       }
4268       if (sum<4){
4269         kink->SetShapeFactor(-1.);
4270       }
4271       else{
4272         kink->SetShapeFactor(shapesum/sum);
4273       }      
4274       //      esd->AddKink(kink);
4275       kinks->AddLast(kink);
4276       kink = new AliESDkink;
4277       ncandidates++;
4278     }
4279   }
4280   //
4281   // sort the kinks according quality - and refit them towards vertex
4282   //
4283   Int_t       nkinks    = kinks->GetEntriesFast();
4284   Float_t    *quality   = new Float_t[nkinks];
4285   Int_t      *indexes   = new Int_t[nkinks];
4286   AliTPCseed *mothers   = new AliTPCseed[nkinks];
4287   AliTPCseed *daughters = new AliTPCseed[nkinks];
4288   //
4289   //
4290   for (Int_t i=0;i<nkinks;i++){
4291     quality[i] =100000;
4292     AliESDkink *kink = (AliESDkink*)kinks->At(i);
4293     //
4294     // refit kinks towards vertex
4295     // 
4296     Int_t index0 = kink->GetIndex(0);
4297     Int_t index1 = kink->GetIndex(1);
4298     AliTPCseed * ktrack0 = (AliTPCseed*)array->At(index0);
4299     AliTPCseed * ktrack1 = (AliTPCseed*)array->At(index1);
4300     //
4301     Int_t sumn=ktrack0->fN+ktrack1->fN;
4302     //
4303     // Refit Kink under if too small angle
4304     //
4305     if (kink->GetAngle(2)<0.05){
4306       kink->SetTPCRow0(GetRowNumber(kink->GetR()));
4307       Int_t row0 = kink->GetTPCRow0();
4308       Int_t drow = Int_t(2.+0.5/(0.05+kink->GetAngle(2)));
4309       //
4310       //
4311       Int_t last  = row0-drow;
4312       if (last<40) last=40;
4313       if (last<ktrack0->fFirstPoint+25) last = ktrack0->fFirstPoint+25;
4314       AliTPCseed* seed0 = ReSeed(ktrack0,last,kFALSE);
4315       //
4316       //
4317       Int_t first = row0+drow;
4318       if (first>130) first=130;
4319       if (first>ktrack1->fLastPoint-25) first = TMath::Max(ktrack1->fLastPoint-25,30);
4320       AliTPCseed* seed1 = ReSeed(ktrack1,first,kTRUE);
4321       //
4322       if (seed0 && seed1){
4323         kink->SetStatus(1,8);
4324         if (RefitKink(*seed0,*seed1,*kink)) kink->SetStatus(1,9);
4325         row0 = GetRowNumber(kink->GetR());
4326         sumn = seed0->fN+seed1->fN;
4327         new (&mothers[i])   AliTPCseed(*seed0);
4328         new (&daughters[i]) AliTPCseed(*seed1); 
4329       }
4330       else{
4331         delete kinks->RemoveAt(i);
4332         if (seed0) delete seed0;
4333         if (seed1) delete seed1;
4334         continue;
4335       }
4336       if (kink->GetDistance()>0.5 || kink->GetR()<110 || kink->GetR()>240) {
4337         delete kinks->RemoveAt(i);
4338         if (seed0) delete seed0;
4339         if (seed1) delete seed1;
4340         continue;
4341       }
4342       //
4343       delete seed0;
4344       delete seed1;            
4345     }
4346     //
4347     if (kink) quality[i] = 160*((0.1+kink->GetDistance())*(2.-kink->GetTPCDensityFactor()))/(sumn+40.);  //the longest -clossest will win
4348   }
4349   TMath::Sort(nkinks,quality,indexes,kFALSE);
4350   //
4351   //remove double find kinks
4352   //
4353   for (Int_t ikink0=1;ikink0<nkinks;ikink0++){
4354     AliESDkink * kink0 = (AliESDkink*) kinks->At(indexes[ikink0]);
4355     if (!kink0) continue;
4356     //
4357     for (Int_t ikink1=0;ikink1<ikink0;ikink1++){
4358       if (!kink0) continue;
4359       AliESDkink * kink1 = (AliESDkink*) kinks->At(indexes[ikink1]);
4360       if (!kink1) continue;
4361       // if not close kink continue
4362       if (TMath::Abs(kink1->GetPosition()[2]-kink0->GetPosition()[2])>10) continue;
4363       if (TMath::Abs(kink1->GetPosition()[1]-kink0->GetPosition()[1])>10) continue;
4364       if (TMath::Abs(kink1->GetPosition()[0]-kink0->GetPosition()[0])>10) continue;
4365       //
4366       AliTPCseed &mother0   = mothers[indexes[ikink0]];
4367       AliTPCseed &daughter0 = daughters[indexes[ikink0]];
4368       AliTPCseed &mother1   = mothers[indexes[ikink1]];
4369       AliTPCseed &daughter1 = daughters[indexes[ikink1]];
4370       Int_t row0 = (kink0->GetTPCRow0()+kink1->GetTPCRow0())/2;
4371       //
4372       Int_t same  = 0;
4373       Int_t both  = 0;
4374       Int_t samem = 0;
4375       Int_t bothm = 0;
4376       Int_t samed = 0;
4377       Int_t bothd = 0;
4378       //
4379       for (Int_t i=0;i<row0;i++){
4380         if (mother0.fIndex[i]>0 && mother1.fIndex[i]>0){
4381           both++;
4382           bothm++;
4383           if (mother0.fIndex[i]==mother1.fIndex[i]){
4384             same++;
4385             samem++;
4386           }
4387         }
4388       }
4389
4390       for (Int_t i=row0;i<158;i++){
4391         if (daughter0.fIndex[i]>0 && daughter0.fIndex[i]>0){
4392           both++;
4393           bothd++;
4394           if (mother0.fIndex[i]==mother1.fIndex[i]){
4395             same++;
4396             samed++;
4397           }
4398         }
4399       }
4400       Float_t ratio = Float_t(same+1)/Float_t(both+1);
4401       Float_t ratiom = Float_t(samem+1)/Float_t(bothm+1);
4402       Float_t ratiod = Float_t(samed+1)/Float_t(bothd+1);
4403       if (ratio>0.3 && ratiom>0.5 &&ratiod>0.5) {
4404         Int_t sum0 = mother0.fN+daughter0.fN;
4405         Int_t sum1 = mother1.fN+daughter1.fN;
4406         if (sum1>sum0){
4407           shared[kink0->GetIndex(0)]= kTRUE;
4408           shared[kink0->GetIndex(1)]= kTRUE;      
4409           delete kinks->RemoveAt(indexes[ikink0]);
4410         }
4411         else{
4412           shared[kink1->GetIndex(0)]= kTRUE;
4413           shared[kink1->GetIndex(1)]= kTRUE;      
4414           delete kinks->RemoveAt(indexes[ikink1]);
4415         }
4416       }
4417     }
4418   }
4419
4420
4421   for (Int_t i=0;i<nkinks;i++){
4422     AliESDkink * kink = (AliESDkink*) kinks->At(indexes[i]);
4423     if (!kink) continue;
4424     kink->SetTPCRow0(GetRowNumber(kink->GetR()));
4425     Int_t index0 = kink->GetIndex(0);
4426     Int_t index1 = kink->GetIndex(1);
4427     if (circular[index0]||circular[index1]&&kink->GetDistance()>0.2) continue;
4428     kink->SetMultiple(usage[index0],0);
4429     kink->SetMultiple(usage[index1],1);
4430     if (kink->GetMultiple()[0]+kink->GetMultiple()[1]>2) continue;
4431     if (kink->GetMultiple()[0]+kink->GetMultiple()[1]>0 && quality[indexes[i]]>0.2) continue;
4432     if (kink->GetMultiple()[0]+kink->GetMultiple()[1]>0 && kink->GetDistance()>0.2) continue;
4433     if (circular[index0]||circular[index1]&&kink->GetDistance()>0.1) continue;
4434
4435     AliTPCseed * ktrack0 = (AliTPCseed*)array->At(index0);
4436     AliTPCseed * ktrack1 = (AliTPCseed*)array->At(index1);
4437     if (!ktrack0 || !ktrack1) continue;
4438     Int_t index = esd->AddKink(kink);
4439     //
4440     //
4441     if ( ktrack0->fKinkIndexes[0]==0 && ktrack1->fKinkIndexes[0]==0) {  //best kink
4442       if (mothers[indexes[i]].fN>20 && daughters[indexes[i]].fN>20 && (mothers[indexes[i]].fN+daughters[indexes[i]].fN)>100){
4443         new (ktrack0) AliTPCseed(mothers[indexes[i]]);
4444         new (ktrack1) AliTPCseed(daughters[indexes[i]]);
4445       }
4446     }
4447     //
4448     ktrack0->fKinkIndexes[usage[index0]] = -(index+1);
4449     ktrack1->fKinkIndexes[usage[index1]] =  (index+1);
4450     usage[index0]++;
4451     usage[index1]++;
4452   }
4453   //
4454   // Remove tracks corresponding to shared kink's
4455   //
4456   for (Int_t i=0;i<nentries;i++){
4457     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4458     if (!track0) continue;
4459     if (track0->fKinkIndexes[0]!=0) continue;
4460     if (shared[i]) delete array->RemoveAt(i);
4461   }
4462
4463   //
4464   //
4465   RemoveUsed2(array,0.5,0.4,30);
4466   UnsignClusters();
4467   for (Int_t i=0;i<nentries;i++){
4468     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4469     if (!track0) continue;
4470     track0->CookdEdx(0.02,0.6);
4471     track0->CookPID();
4472   }
4473   //
4474   for (Int_t i=0;i<nentries;i++){
4475     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4476     if (!track0) continue;
4477     if (track0->Pt()<1.4) continue;
4478     //remove double high momenta tracks - overlapped with kink candidates
4479     Int_t shared=0;
4480     Int_t all   =0;
4481     for (Int_t icl=track0->fFirstPoint;icl<track0->fLastPoint; icl++){
4482       if (track0->fClusterPointer[icl]!=0){
4483         all++;
4484         if (track0->fClusterPointer[icl]->IsUsed(10)) shared++;
4485       }
4486     }
4487     if (Float_t(shared+1)/Float_t(nall+1)>0.5) {
4488       delete array->RemoveAt(i);
4489     }
4490     //
4491     if (track0->fKinkIndexes[0]!=0) continue;
4492     if (track0->GetNumberOfClusters()<80) continue;
4493
4494     AliTPCseed *pmother = new AliTPCseed();
4495     AliTPCseed *pdaughter = new AliTPCseed();
4496     AliESDkink *pkink = new AliESDkink;
4497
4498     AliTPCseed & mother = *pmother;
4499     AliTPCseed & daughter = *pdaughter;
4500     AliESDkink & kink = *pkink;
4501     if (CheckKinkPoint(track0,mother,daughter, kink)){
4502       if (mother.fN<30||daughter.fN<20) {
4503         delete pmother;
4504         delete pdaughter;
4505         delete pkink;
4506         continue;  //too short tracks
4507       }
4508       if (mother.Pt()<1.4) {
4509         delete pmother;
4510         delete pdaughter;
4511         delete pkink;
4512         continue;
4513       }
4514       Int_t row0= kink.GetTPCRow0();
4515       if (kink.GetDistance()>0.5 || kink.GetR()<110. || kink.GetR()>240.) {
4516         delete pmother;
4517         delete pdaughter;
4518         delete pkink;
4519         continue;
4520       }
4521       //
4522       Int_t index = esd->AddKink(&kink);      
4523       mother.fKinkIndexes[0] = -(index+1);
4524       daughter.fKinkIndexes[0] = index+1;
4525       if (mother.fN>50) {
4526         delete array->RemoveAt(i);
4527         array->AddAt(new AliTPCseed(mother),i);
4528       }
4529       else{
4530         array->AddLast(new AliTPCseed(mother));
4531       }
4532       array->AddLast(new AliTPCseed(daughter));      
4533       for (Int_t icl=0;icl<row0;icl++) {
4534         if (mother.fClusterPointer[icl]) mother.fClusterPointer[icl]->Use(20);
4535       }
4536       //
4537       for (Int_t icl=row0;icl<158;icl++) {
4538         if (daughter.fClusterPointer[icl]) daughter.fClusterPointer[icl]->Use(20);
4539       }
4540       //
4541     }
4542     delete pmother;
4543     delete pdaughter;
4544     delete pkink;
4545   }
4546
4547   delete [] daughters;
4548   delete [] mothers;
4549   //
4550   //
4551   delete [] dca;
4552   delete []circular;
4553   delete []shared;
4554   delete []quality;
4555   delete []indexes;
4556   //
4557   delete kink;
4558   delete[]fim;
4559   delete[] zm;
4560   delete[] z0;
4561   delete [] usage;
4562   delete[] alpha;
4563   delete[] nclusters;
4564   delete[] sign;
4565   delete[] helixes;
4566   kinks->Delete();
4567   delete kinks;
4568
4569   printf("Ncandidates=\t%d\t%d\t%d\t%d\n",esd->GetNumberOfKinks(),ncandidates,ntracks,nall);
4570   timer.Print();
4571 }
4572
4573 void  AliTPCtrackerMI::FindV0s(TObjArray * array, AliESD *esd)
4574 {
4575   //
4576   //  find V0s
4577   //
4578   //
4579   TObjArray *tpcv0s      = new TObjArray(100000);
4580   Int_t     nentries     = array->GetEntriesFast();
4581   AliHelix *helixes      = new AliHelix[nentries];
4582   Int_t    *sign         = new Int_t[nentries];
4583   Float_t  *alpha        = new Float_t[nentries];
4584   Float_t  *z0           = new Float_t[nentries]; 
4585   Float_t  *dca          = new Float_t[nentries];
4586   Float_t  *sdcar        = new Float_t[nentries];
4587   Float_t  *cdcar        = new Float_t[nentries];
4588   Float_t  *pulldcar     = new Float_t[nentries];
4589   Float_t  *pulldcaz     = new Float_t[nentries];
4590   Float_t  *pulldca      = new Float_t[nentries];
4591   Bool_t   *isPrim       = new Bool_t[nentries];  
4592   const AliESDVertex * primvertex = esd->GetVertex();
4593   Double_t             zvertex = primvertex->GetZv(); 
4594   //
4595   //  nentries = array->GetEntriesFast();
4596   //
4597   for (Int_t i=0;i<nentries;i++){
4598     sign[i]=0;
4599     isPrim[i]=0;
4600     AliTPCseed* track = (AliTPCseed*)array->At(i);    
4601     if (!track) continue;
4602     track->GetV0Indexes()[0] = 0;  //rest v0 indexes
4603     track->GetV0Indexes()[1] = 0;  //rest v0 indexes
4604     track->GetV0Indexes()[2] = 0;  //rest v0 indexes
4605     //
4606     alpha[i] = track->GetAlpha();
4607     new (&helixes[i]) AliHelix(*track);
4608     Double_t xyz[3];
4609     helixes[i].Evaluate(0,xyz);
4610     sign[i] = (track->GetC()>0) ? -1:1;
4611     Double_t x,y,z;
4612     x=160;
4613     z0[i]=1000;
4614     if (track->GetProlongation(0,y,z))  z0[i] = z;
4615     dca[i] = track->GetD(0,0);
4616     // 
4617     // dca error parrameterezation + pulls
4618     //
4619     sdcar[i]      = TMath::Sqrt(0.150*0.150+(100*track->fP4)*(100*track->fP4));
4620     if (TMath::Abs(track->fP3)>1) sdcar[i]*=2.5;
4621     cdcar[i]      = TMath::Exp((TMath::Abs(track->fP4)-0.0106)*525.3);
4622     pulldcar[i]   = (dca[i]-cdcar[i])/sdcar[i];
4623     pulldcaz[i]   = (z0[i]-zvertex)/sdcar[i];
4624     pulldca[i]    = TMath::Sqrt(pulldcar[i]*pulldcar[i]+pulldcaz[i]*pulldcaz[i]);
4625     if (track->fTPCr[1]+track->fTPCr[2]+track->fTPCr[3]>0.5) {
4626       if (pulldca[i]<3.) isPrim[i]=kTRUE;  //pion, muon and Kaon  3 sigma cut
4627     }
4628     if (track->fTPCr[4]>0.5) {
4629       if (pulldca[i]<0.5) isPrim[i]=kTRUE;  //proton 0.5 sigma cut
4630     }
4631     if (track->fTPCr[0]>0.4) {
4632       isPrim[i]=kFALSE;  //electron no  sigma cut
4633     }
4634   }
4635   //
4636   //
4637   TStopwatch timer;
4638   timer.Start();
4639   Int_t ncandidates =0;
4640   Int_t nall =0;
4641   Int_t ntracks=0; 
4642   Double_t phase[2][2],radius[2];
4643   //
4644   //  Finf V0s loop
4645   // 
4646   //
4647   // //  
4648   TTreeSRedirector &cstream = *fDebugStreamer; 
4649   Float_t fprimvertex[3]={GetX(),GetY(),GetZ()};
4650   AliESDV0MI vertex; 
4651   Double_t cradius0 = 10*10;
4652   Double_t cradius1 = 200*200;
4653   Double_t cdist1=3.;
4654   Double_t cdist2=4.;
4655   Double_t cpointAngle = 0.95;
4656   //
4657   Double_t delta[2]={10000,10000};
4658   for (Int_t i =0;i<nentries;i++){
4659     if (sign[i]==0) continue;
4660     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4661     if (!track0) continue;
4662     cstream<<"Tracks"<<
4663       "Tr0.="<<track0<<
4664       "dca="<<dca[i]<<
4665       "z0="<<z0[i]<<
4666       "zvertex="<<zvertex<<
4667       "sdcar0="<<sdcar[i]<<
4668       "cdcar0="<<cdcar[i]<<
4669       "pulldcar0="<<pulldcar[i]<<
4670       "pulldcaz0="<<pulldcaz[i]<<
4671       "pulldca0="<<pulldca[i]<<
4672       "isPrim="<<isPrim[i]<<
4673       "\n";
4674     //
4675     if (track0->fP4<0) continue;
4676     if (track0->GetKinkIndex(0)>0||isPrim[i]) continue;   //daughter kink
4677     //
4678     if (TMath::Abs(helixes[i].GetHelix(4))<0.000000001) continue;
4679     ntracks++;
4680     // debug output
4681     
4682     
4683     for (Int_t j =0;j<nentries;j++){
4684       AliTPCseed * track1 = (AliTPCseed*)array->At(j);
4685       if (!track1) continue;
4686       if (track1->GetKinkIndex(0)>0 || isPrim[j]) continue; //daughter kink
4687       if (sign[j]*sign[i]>0) continue; 
4688       if (TMath::Abs(helixes[j].GetHelix(4))<0.000001) continue;
4689       if (track0->fCircular+track1->fCircular>1) continue;    //circling -returning track
4690       nall++;
4691       //
4692       // DCA to prim vertex cut
4693       //
4694       //
4695       delta[0]=10000;
4696       delta[1]=10000;
4697
4698       Int_t npoints = helixes[i].GetRPHIintersections(helixes[j], phase, radius,cdist2);
4699       if (npoints<1) continue;
4700       Int_t iclosest=0;
4701       // cuts on radius      
4702       if (npoints==1){
4703         if (radius[0]<cradius0||radius[0]>cradius1) continue;
4704         helixes[i].LinearDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta[0]);
4705         if (delta[0]>cdist1) continue;
4706       }
4707       else{
4708         if (TMath::Max(radius[0],radius[1])<cradius0|| TMath::Min(radius[0],radius[1])>cradius1) continue;      
4709         helixes[i].LinearDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta[0]);    
4710         helixes[i].LinearDCA(helixes[j],phase[1][0],phase[1][1],radius[1],delta[1]);
4711         if (delta[1]<delta[0]) iclosest=1;
4712         if (delta[iclosest]>cdist1) continue;
4713       }
4714       helixes[i].ParabolicDCA(helixes[j],phase[iclosest][0],phase[iclosest][1],radius[iclosest],delta[iclosest]);
4715       if (radius[iclosest]<cradius0 || radius[iclosest]>cradius1 || delta[iclosest]>cdist1) continue;
4716       //
4717       Double_t pointAngle = helixes[i].GetPointAngle(helixes[j],phase[iclosest],fprimvertex);
4718       if (pointAngle<cpointAngle) continue;
4719       //
4720       Bool_t isGamma = kFALSE;
4721       vertex.SetP(*track0); //track0 - plus
4722       vertex.SetM(*track1); //track1 - minus
4723       vertex.Update(fprimvertex);
4724       if (track0->fTPCr[0]>0.3&&track1->fTPCr[0]>0.3&&vertex.GetAnglep()[2]<0.15) isGamma=kTRUE;              // gamma conversion candidate
4725       Double_t pointAngle2 = vertex.GetPointAngle();
4726       //continue;
4727       if (vertex.GetPointAngle()<cpointAngle && (!isGamma)) continue; // point angle cut
4728       if (vertex.GetDist2()>2&&(!isGamma)) continue;         // point angle cut
4729       Float_t sigmae     = 0.15*0.15;
4730       if (vertex.GetRr()<80) 
4731         sigmae += (sdcar[i]*sdcar[i]+sdcar[j]*sdcar[j])*(1.-vertex.GetRr()/80.)*(1.-vertex.GetRr()/80.);
4732       sigmae+= TMath::Sqrt(sigmae);
4733       if (vertex.GetDist2()/sigmae>3.&&(!isGamma)) continue; 
4734       Float_t densb0=0,densb1=0,densa0=0,densa1=0;
4735       Int_t row0 = GetRowNumber(vertex.GetRr());
4736       if (row0>15){
4737         if (vertex.GetDist2()>0.2) continue;             
4738         densb0     = track0->Density2(0,row0-5);          
4739         densb1     = track1->Density2(0,row0-5);         
4740         if (densb0>0.3|| densb1>0.3) continue;            //clusters before vertex
4741         densa0     = track0->Density2(row0+5,row0+40);    
4742         densa1     = track1->Density2(row0+5,row0+40);    
4743         if ((densa0<0.4|| densa1<0.4)&&(!isGamma)) continue;            //missing clusters after vertex
4744       }
4745       else{
4746         densa0     = track0->Density2(0,40);  //cluster density
4747         densa1     = track1->Density2(0,40);  //cluster density
4748         if ((vertex.GetRr()<80&&densa0+densa1<1.)&&(!isGamma)) continue;
4749       }
4750       vertex.SetLab(0,track0->GetLabel());
4751       vertex.SetLab(1,track1->GetLabel());
4752       vertex.SetChi2After((densa0+densa1)*0.5);
4753       vertex.SetChi2Before((densb0+densb1)*0.5);
4754       vertex.SetIndex(0,i);
4755       vertex.SetIndex(1,j);
4756       vertex.SetStatus(1); // TPC v0 candidate
4757       vertex.SetRp(track0->fTPCr);
4758       vertex.SetRm(track1->fTPCr);
4759       tpcv0s->AddLast(new AliESDV0MI(vertex));      
4760       ncandidates++;
4761       {
4762         Int_t eventNr = esd->GetEventNumber();
4763         Double_t radiusm= (delta[0]<delta[1])? TMath::Sqrt(radius[0]):TMath::Sqrt(radius[1]);  
4764         Double_t deltam= (delta[0]<delta[1])? TMath::Sqrt(delta[0]):TMath::Sqrt(delta[1]);  
4765         cstream<<"V0"<<
4766           "Event="<<eventNr<<
4767           "vertex.="<<&vertex<<
4768           "Tr0.="<<track0<<
4769           "lab0="<<track0->fLab<<
4770           "Helix0.="<<&helixes[i]<<     
4771           "Tr1.="<<track1<<
4772           "lab1="<<track1->fLab<<
4773           "Helix1.="<<&helixes[j]<<
4774           "pointAngle="<<pointAngle<<
4775           "pointAngle2="<<pointAngle2<<
4776           "dca0="<<dca[i]<<
4777           "dca1="<<dca[j]<<
4778           "z0="<<z0[i]<<
4779           "z1="<<z0[j]<<
4780           "zvertex="<<zvertex<<
4781           "circular0="<<track0->fCircular<<
4782           "circular1="<<track1->fCircular<<
4783           "npoints="<<npoints<<
4784           "radius0="<<radius[0]<<
4785           "delta0="<<delta[0]<<
4786           "radius1="<<radius[1]<<
4787           "delta1="<<delta[1]<<
4788           "radiusm="<<radiusm<<
4789           "deltam="<<deltam<<
4790           "sdcar0="<<sdcar[i]<<
4791           "sdcar1="<<sdcar[j]<<
4792           "cdcar0="<<cdcar[i]<<
4793           "cdcar1="<<cdcar[j]<<
4794           "pulldcar0="<<pulldcar[i]<<
4795           "pulldcar1="<<pulldcar[j]<<
4796           "pulldcaz0="<<pulldcaz[i]<<
4797           "pulldcaz1="<<pulldcaz[j]<<
4798           "pulldca0="<<pulldca[i]<<
4799           "pulldca1="<<pulldca[j]<<
4800           "densb0="<<densb0<<
4801           "densb1="<<densb1<<
4802           "densa0="<<densa0<<
4803           "densa1="<<densa1<<
4804           "sigmae="<<sigmae<<
4805           "\n";
4806       }
4807     }
4808   }    
4809   Float_t *quality = new Float_t[ncandidates];
4810   Int_t *indexes = new Int_t[ncandidates];
4811   Int_t naccepted =0;
4812   for (Int_t i=0;i<ncandidates;i++){
4813     quality[i]     = 0; 
4814     AliESDV0MI *v0 = (AliESDV0MI*)tpcv0s->At(i);
4815     quality[i]     = 1./(1.00001-v0->GetPointAngle());   //base point angle
4816     // quality[i]    /= (0.5+v0->GetDist2());  
4817     // quality[i]    *= v0->GetChi2After();               //density factor
4818     Double_t minpulldca = TMath::Min(2.+pulldca[v0->GetIndex(0)],(2.+pulldca[v0->GetIndex(1)]) );     //pull
4819     Int_t index0 = v0->GetIndex(0);
4820     Int_t index1 = v0->GetIndex(1);
4821     AliTPCseed * track0 = (AliTPCseed*)array->At(index0);
4822     AliTPCseed * track1 = (AliTPCseed*)array->At(index1);
4823     if (track0->fTPCr[0]>0.3&&track1->fTPCr[0]>0.3&&v0->GetAnglep()[2]<0.15) quality[i]+=1000000;              // gamma conversion candidate
4824     if (track0->fTPCr[4]>0.9||track1->fTPCr[4]>0.9&&minpulldca>4) quality[i]*=10;    // lambda candidate candidate
4825   }
4826
4827   TMath::Sort(ncandidates,quality,indexes,kTRUE);
4828   //
4829   //
4830   for (Int_t i=0;i<ncandidates;i++){
4831     AliESDV0MI * v0 = (AliESDV0MI*)tpcv0s->At(indexes[i]);
4832     if (!v0) continue;
4833     Int_t index0 = v0->GetIndex(0);
4834     Int_t index1 = v0->GetIndex(1);
4835     AliTPCseed * track0 = (AliTPCseed*)array->At(index0);
4836     AliTPCseed * track1 = (AliTPCseed*)array->At(index1);
4837     if (!track0||!track1) {
4838       printf("Bug\n");
4839       continue;
4840     }
4841     Bool_t accept =kTRUE;  //default accept
4842     Int_t *v0indexes0 = track0->GetV0Indexes();
4843     Int_t *v0indexes1 = track1->GetV0Indexes();
4844     //
4845     Int_t order0 = (v0indexes0[0]!=0) ? 1:0;
4846     Int_t order1 = (v0indexes1[0]!=0) ? 1:0;    
4847     if (v0indexes0[1]!=0) order0 =2;
4848     if (v0indexes1[1]!=0) order1 =2;      
4849     //
4850     if (v0indexes0[2]!=0) {order0=3; accept=kFALSE;}
4851     if (v0indexes0[2]!=0) {order1=3; accept=kFALSE;}
4852     //
4853     AliESDV0MI * v02 = v0;
4854     if (accept){
4855       v0->SetOrder(0,order0);
4856       v0->SetOrder(1,order1);
4857       v0->SetOrder(1,order0+order1);     
4858       Int_t index = esd->AddV0MI(v0);
4859       v02 = esd->GetV0MI(index);
4860       v0indexes0[order0]=index;
4861       v0indexes1[order1]=index;
4862       naccepted++;
4863     }
4864     {
4865       Int_t eventNr = esd->GetEventNumber();
4866       cstream<<"V02"<<
4867         "Event="<<eventNr<<
4868         "vertex.="<<v0<<        
4869         "vertex2.="<<v02<<
4870         "Tr0.="<<track0<<
4871         "lab0="<<track0->fLab<<
4872         "Tr1.="<<track1<<
4873         "lab1="<<track1->fLab<<
4874         "dca0="<<dca[index0]<<
4875         "dca1="<<dca[index1]<<
4876         "order0="<<order0<<
4877         "order1="<<order1<<
4878         "accept="<<accept<<
4879         "quality="<<quality[i]<<
4880         "pulldca0="<<pulldca[index0]<<
4881         "pulldca1="<<pulldca[index1]<<
4882         "index="<<i<<
4883         "\n";
4884     }
4885   }    
4886
4887
4888   //
4889   //
4890   delete []quality;
4891   delete []indexes;
4892 //
4893   delete [] isPrim;
4894   delete [] pulldca;
4895   delete [] pulldcaz;
4896   delete [] pulldcar;
4897   delete [] cdcar;
4898   delete [] sdcar;
4899   delete [] dca;
4900   //
4901   delete[] z0;
4902   delete[] alpha;
4903   delete[] sign;
4904   delete[] helixes;
4905   printf("TPC V0 finder : naccepted\t%d\tncandidates\t%d\tntracks\t%d\tnall\t%d\n",naccepted,ncandidates,ntracks,nall);
4906   timer.Print();
4907 }
4908
4909 Int_t AliTPCtrackerMI::RefitKink(AliTPCseed &mother, AliTPCseed &daughter, AliESDkink &kink)
4910 {
4911   //
4912   // refit kink towards to the vertex
4913   //
4914   //
4915   Int_t row0 = GetRowNumber(kink.GetR());
4916   FollowProlongation(mother,0);
4917   mother.Reset(kFALSE);
4918   //
4919   FollowProlongation(daughter,row0);
4920   daughter.Reset(kFALSE);
4921   FollowBackProlongation(daughter,158);
4922   daughter.Reset(kFALSE);
4923   Int_t first = TMath::Max(row0-20,30); 
4924   Int_t last  = TMath::Min(row0+20,140);
4925   //
4926   const Int_t kNdiv =5;
4927   AliTPCseed  param0[kNdiv];  // parameters along the track
4928   AliTPCseed  param1[kNdiv];  // parameters along the track
4929   AliESDkink   kinks[kNdiv];   // corresponding kink parameters
4930   //
4931   Int_t rows[kNdiv];
4932   for (Int_t irow=0; irow<kNdiv;irow++){
4933     rows[irow] = first +((last-first)*irow)/(kNdiv-1);
4934   }
4935   // store parameters along the track
4936   //
4937   for (Int_t irow=0;irow<kNdiv;irow++){
4938     FollowBackProlongation(mother, rows[irow]);
4939     FollowProlongation(daughter,rows[kNdiv-1-irow]);       
4940     new(&param0[irow])     AliTPCseed(mother);
4941     new(&param1[kNdiv-1-irow])   AliTPCseed(daughter);
4942   }
4943   //
4944   // define kinks 
4945   for (Int_t irow=0; irow<kNdiv-1;irow++){
4946     if (param0[irow].fN<kNdiv||param1[irow].fN<kNdiv) continue;
4947     kinks[irow].SetMother(param0[irow]);
4948     kinks[irow].SetDaughter(param1[irow]);
4949     kinks[irow].Update();
4950   }
4951   //
4952   // choose kink with best "quality"
4953   Int_t index =-1;
4954   Double_t mindist = 10000;
4955   for (Int_t irow=0;irow<kNdiv;irow++){
4956     if (param0[irow].fN<20||param1[irow].fN<20) continue;
4957     if (TMath::Abs(kinks[irow].GetR())>240.) continue;
4958     if (TMath::Abs(kinks[irow].GetR())<100.) continue;
4959     //
4960     Float_t normdist = TMath::Abs(param0[irow].fX-kinks[irow].GetR())*(0.1+kink.GetDistance());
4961     normdist/= (param0[irow].fN+param1[irow].fN+40.);
4962     if (normdist < mindist){
4963       mindist = normdist;
4964       index = irow;
4965     }
4966   }
4967   //
4968   if (index==-1) return 0;
4969   //
4970   //
4971   param0[index].Reset(kTRUE);
4972   FollowProlongation(param0[index],0);
4973   //
4974   new (&mother) AliTPCseed(param0[index]);
4975   new (&daughter) AliTPCseed(param1[index]);  // daughter in vertex
4976   //
4977   kink.SetMother(mother);
4978   kink.SetDaughter(daughter);
4979   kink.Update();
4980   kink.SetTPCRow0(GetRowNumber(kink.GetR()));
4981   kink.SetTPCncls(param0[index].fN,0);
4982   kink.SetTPCncls(param1[index].fN,1);
4983   kink.SetLabel(CookLabel(&mother,0.4, 0,kink.GetTPCRow0()),0);
4984   kink.SetLabel(CookLabel(&daughter,0.4, kink.GetTPCRow0(),160),1);
4985   mother.SetLabel(kink.GetLabel(0));
4986   daughter.SetLabel(kink.GetLabel(1));
4987
4988   return 1;
4989 }
4990
4991
4992 void AliTPCtrackerMI::UpdateKinkQualityM(AliTPCseed * seed){
4993   //
4994   // update Kink quality information for mother after back propagation
4995   //
4996   if (seed->GetKinkIndex(0)>=0) return; 
4997   for (Int_t ikink=0;ikink<3;ikink++){
4998     Int_t index = seed->GetKinkIndex(ikink);
4999     if (index>=0) break;
5000     index = TMath::Abs(index)-1;
5001     AliESDkink * kink = fEvent->GetKink(index);
5002     //kink->fTPCdensity2[0][0]=-1;
5003     //kink->fTPCdensity2[0][1]=-1;
5004     kink->SetTPCDensity2(-1,0,0);
5005     kink->SetTPCDensity2(1,0,1);
5006     //
5007     Int_t row0 = kink->GetTPCRow0() - 2 - Int_t( 0.5/ (0.05+kink->GetAngle(2)));
5008     if (row0<15) row0=15;
5009     //
5010     Int_t row1 = kink->GetTPCRow0() + 2 +  Int_t( 0.5/ (0.05+kink->GetAngle(2)));
5011     if (row1>145) row1=145;
5012     //
5013     Int_t found,foundable,shared;
5014     seed->GetClusterStatistic(0,row0, found, foundable,shared,kFALSE);
5015     if (foundable>5)   kink->SetTPCDensity2(Float_t(found)/Float_t(foundable),0,0);
5016     seed->GetClusterStatistic(row1,155, found, foundable,shared,kFALSE);
5017     if (foundable>5)   kink->SetTPCDensity2(Float_t(found)/Float_t(foundable),0,1);
5018   }
5019     
5020 }
5021
5022 void AliTPCtrackerMI::UpdateKinkQualityD(AliTPCseed * seed){
5023   //
5024   // update Kink quality information for daughter after refit
5025   //
5026   if (seed->GetKinkIndex(0)<=0) return; 
5027   for (Int_t ikink=0;ikink<3;ikink++){
5028     Int_t index = seed->GetKinkIndex(ikink);
5029     if (index<=0) break;
5030     index = TMath::Abs(index)-1;
5031     AliESDkink * kink = fEvent->GetKink(index);
5032     kink->SetTPCDensity2(-1,1,0);
5033     kink->SetTPCDensity2(-1,1,1);
5034     //
5035     Int_t row0 = kink->GetTPCRow0() -2 - Int_t( 0.5/ (0.05+kink->GetAngle(2)));
5036     if (row0<15) row0=15;
5037     //
5038     Int_t row1 = kink->GetTPCRow0() +2 +  Int_t( 0.5/ (0.05+kink->GetAngle(2)));
5039     if (row1>145) row1=145;
5040     //
5041     Int_t found,foundable,shared;
5042     seed->GetClusterStatistic(0,row0, found, foundable,shared,kFALSE);
5043     if (foundable>5)   kink->SetTPCDensity2(Float_t(found)/Float_t(foundable),1,0);
5044     seed->GetClusterStatistic(row1,155, found, foundable,shared,kFALSE);
5045     if (foundable>5)   kink->SetTPCDensity2(Float_t(found)/Float_t(foundable),1,1);
5046   }
5047     
5048 }
5049
5050
5051 Int_t  AliTPCtrackerMI::CheckKinkPoint(AliTPCseed*seed,AliTPCseed &mother, AliTPCseed &daughter, AliESDkink &kink)
5052 {
5053   //
5054   // check kink point for given track
5055   // if return value=0 kink point not found
5056   // otherwise seed0 correspond to mother particle
5057   //           seed1 correspond to daughter particle
5058   //           kink  parameter of kink point
5059
5060   Int_t middlerow = (seed->fFirstPoint+seed->fLastPoint)/2;
5061   Int_t first = seed->fFirstPoint; 
5062   Int_t last  = seed->fLastPoint;
5063   if (last-first<20) return 0;          // shortest length - 2*30 = 60 pad-rows
5064
5065   
5066   AliTPCseed *seed1 = ReSeed(seed,middlerow+20, kTRUE);  //middle of chamber
5067   if (!seed1) return 0;
5068   FollowProlongation(*seed1,seed->fLastPoint-20);
5069   seed1->Reset(kTRUE);
5070   FollowProlongation(*seed1,158);
5071   seed1->Reset(kTRUE);  
5072   last = seed1->fLastPoint;
5073   //
5074   AliTPCseed *seed0 = new AliTPCseed(*seed);
5075   seed0->Reset(kFALSE);
5076   seed0->Reset();
5077   //
5078   AliTPCseed  param0[20];  // parameters along the track
5079   AliTPCseed  param1[20];  // parameters along the track
5080   AliESDkink            kinks[20];   // corresponding kink parameters
5081   Int_t rows[20];
5082   for (Int_t irow=0; irow<20;irow++){
5083     rows[irow] = first +((last-first)*irow)/19;
5084   }
5085   // store parameters along the track
5086   //
5087   for (Int_t irow=0;irow<20;irow++){
5088     FollowBackProlongation(*seed0, rows[irow]);
5089     FollowProlongation(*seed1,rows[19-irow]);       
5090     new(&param0[irow])     AliTPCseed(*seed0);
5091     new(&param1[19-irow])   AliTPCseed(*seed1);
5092   }
5093   //
5094   // define kinks 
5095   for (Int_t irow=0; irow<19;irow++){
5096     kinks[irow].SetMother(param0[irow]);
5097     kinks[irow].SetDaughter(param1[irow]);
5098     kinks[irow].Update();
5099   }
5100   //
5101   // choose kink with biggest change of angle
5102   Int_t index =-1;
5103   Double_t maxchange= 0;
5104   for (Int_t irow=1;irow<19;irow++){
5105     if (TMath::Abs(kinks[irow].GetR())>240.) continue;
5106     if (TMath::Abs(kinks[irow].GetR())<110.) continue;
5107     Float_t quality = TMath::Abs(kinks[irow].GetAngle(2))/(3.+TMath::Abs(kinks[irow].GetR()-param0[irow].fX));
5108     if ( quality > maxchange){
5109       maxchange = quality;
5110       index = irow;
5111       //
5112     }
5113   }
5114   delete seed0;
5115   delete seed1;
5116   if (index<0) return 0;
5117   //
5118   Int_t row0    = GetRowNumber(kinks[index].GetR());   //row 0 estimate
5119   seed0 = new AliTPCseed(param0[index]);
5120   seed1 = new AliTPCseed(param1[index]);
5121   seed0->Reset(kFALSE);
5122   seed1->Reset(kFALSE);
5123   seed0->ResetCovariance();
5124   seed1->ResetCovariance();
5125   FollowProlongation(*seed0,0);
5126   FollowBackProlongation(*seed1,158);
5127   new (&mother) AliTPCseed(*seed0);  // backup mother at position 0
5128   seed0->Reset(kFALSE);  
5129   seed1->Reset(kFALSE);
5130   seed0->ResetCovariance();
5131   seed1->ResetCovariance();
5132   //
5133   first = TMath::Max(row0-20,0);
5134   last  = TMath::Min(row0+20,158);
5135   //
5136   for (Int_t irow=0; irow<20;irow++){
5137     rows[irow] = first +((last-first)*irow)/19;
5138   }
5139   // store parameters along the track
5140   //
5141   for (Int_t irow=0;irow<20;irow++){
5142     FollowBackProlongation(*seed0, rows[irow]);
5143     FollowProlongation(*seed1,rows[19-irow]);       
5144     new(&param0[irow])     AliTPCseed(*seed0);
5145     new(&param1[19-irow])   AliTPCseed(*seed1);
5146   }
5147   //
5148   // define kinks 
5149   for (Int_t irow=0; irow<19;irow++){
5150     kinks[irow].SetMother(param0[irow]);
5151     kinks[irow].SetDaughter(param1[irow]);
5152     //    param0[irow].Dump();
5153     //param1[irow].Dump();
5154     kinks[irow].Update();
5155   }
5156   //
5157   // choose kink with biggest change of angle
5158   index =-1;
5159   maxchange= 0;
5160   for (Int_t irow=0;irow<20;irow++){
5161     if (TMath::Abs(kinks[irow].GetR())>250.) continue;
5162     if (TMath::Abs(kinks[irow].GetR())<90.) continue;
5163     Float_t quality = TMath::Abs(kinks[irow].GetAngle(2))/(3.+TMath::Abs(kinks[irow].GetR()-param0[irow].fX));
5164     if ( quality > maxchange){
5165       maxchange = quality;
5166       index = irow;
5167       //
5168     }
5169   }
5170   //
5171   //
5172   if (index==-1 || param0[index].fN+param1[index].fN<100){
5173     delete seed0;
5174     delete seed1;
5175     return 0;
5176   }
5177   //  Float_t anglesigma = TMath::Sqrt(param0[index].fC22+param0[index].fC33+param1[index].fC22+param1[index].fC33);
5178   
5179   kink.SetMother(param0[index]);
5180   kink.SetDaughter(param1[index]);
5181   kink.Update();
5182   row0    = GetRowNumber(kink.GetR());   
5183   kink.SetTPCRow0(row0);
5184   kink.SetLabel(CookLabel(seed0,0.5,0,row0),0);
5185   kink.SetLabel(CookLabel(seed1,0.5,row0,158),1);
5186   kink.SetIndex(-10,0);
5187   kink.SetIndex(int(param0[index].fN+param1[index].fN),1);
5188   kink.SetTPCncls(param0[index].fN,0);
5189   kink.SetTPCncls(param1[index].fN,1);
5190   //
5191   //
5192   //  new (&mother) AliTPCseed(param0[index]);
5193   new (&daughter) AliTPCseed(param1[index]);
5194   daughter.SetLabel(kink.GetLabel(1));  
5195   param0[index].Reset(kTRUE);
5196   FollowProlongation(param0[index],0);  
5197   new (&mother) AliTPCseed(param0[index]);
5198   mother.SetLabel(kink.GetLabel(0));
5199   delete seed0;
5200   delete seed1;
5201   //
5202   return 1;
5203 }
5204
5205
5206
5207
5208 AliTPCseed*  AliTPCtrackerMI::ReSeed(AliTPCseed *t)
5209 {
5210   //
5211   // reseed - refit -  track
5212   //
5213   Int_t first = 0;
5214   //  Int_t last  = fSectors->GetNRows()-1;
5215   //
5216   if (fSectors == fOuterSec){
5217     first = TMath::Max(first, t->fFirstPoint-fInnerSec->GetNRows());
5218     //last  = 
5219   }
5220   else
5221     first = t->fFirstPoint;
5222   //
5223   AliTPCseed * seed = MakeSeed(t,0.1,0.5,0.9);
5224   FollowBackProlongation(*t,fSectors->GetNRows()-1);
5225   t->Reset(kFALSE);
5226   FollowProlongation(*t,first);
5227   return seed;
5228 }
5229
5230
5231
5232
5233
5234
5235
5236 //_____________________________________________________________________________
5237 Int_t AliTPCtrackerMI::ReadSeeds(const TFile *inp) {
5238   //-----------------------------------------------------------------
5239   // This function reades track seeds.
5240   //-----------------------------------------------------------------
5241   TDirectory *savedir=gDirectory; 
5242
5243   TFile *in=(TFile*)inp;
5244   if (!in->IsOpen()) {
5245      cerr<<"AliTPCtrackerMI::ReadSeeds(): input file is not open !\n";
5246      return 1;
5247   }
5248
5249   in->cd();
5250   TTree *seedTree=(TTree*)in->Get("Seeds");
5251   if (!seedTree) {
5252      cerr<<"AliTPCtrackerMI::ReadSeeds(): ";
5253      cerr<<"can't get a tree with track seeds !\n";
5254      return 2;
5255   }
5256   AliTPCtrack *seed=new AliTPCtrack; 
5257   seedTree->SetBranchAddress("tracks",&seed);
5258   
5259   if (fSeeds==0) fSeeds=new TObjArray(15000);
5260
5261   Int_t n=(Int_t)seedTree->GetEntries();
5262   for (Int_t i=0; i<n; i++) {
5263      seedTree->GetEvent(i);
5264      fSeeds->AddLast(new AliTPCseed(*seed/*,seed->GetAlpha()*/));
5265   }
5266   
5267   delete seed;
5268   delete seedTree; 
5269   savedir->cd();
5270   return 0;
5271 }
5272
5273 Int_t AliTPCtrackerMI::Clusters2Tracks (AliESD *esd)
5274 {
5275   //
5276   if (fSeeds) DeleteSeeds();
5277   fEvent = esd;
5278   Clusters2Tracks();
5279   if (!fSeeds) return 1;
5280   FillESD(fSeeds);
5281   return 0;
5282   //
5283 }
5284
5285
5286 //_____________________________________________________________________________
5287 Int_t AliTPCtrackerMI::Clusters2Tracks() {
5288   //-----------------------------------------------------------------
5289   // This is a track finder.
5290   //-----------------------------------------------------------------
5291   TDirectory *savedir=gDirectory; 
5292   TStopwatch timer;
5293
5294   fIteration = 0;
5295   fSeeds = Tracking();
5296
5297   if (fDebug>0){
5298     Info("Clusters2Tracks","Time for tracking: \t");timer.Print();timer.Start();
5299   }
5300   //activate again some tracks
5301   for (Int_t i=0; i<fSeeds->GetEntriesFast(); i++) {
5302     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
5303     if (!pt) continue;    
5304     Int_t nc=t.GetNumberOfClusters();
5305     if (nc<20) {
5306       delete fSeeds->RemoveAt(i);
5307       continue;
5308     } 
5309     CookLabel(pt,0.1); 
5310     if (pt->fRemoval==10) {
5311       if (pt->GetDensityFirst(20)>0.8 || pt->GetDensityFirst(30)>0.8 || pt->GetDensityFirst(40)>0.7)
5312         pt->Desactivate(10);  // make track again active
5313       else{
5314         pt->Desactivate(20);    
5315         delete fSeeds->RemoveAt(i);
5316       }
5317     } 
5318   }
5319   //
5320   RemoveUsed2(fSeeds,0.85,0.85,0);
5321   FindKinks(fSeeds,fEvent);
5322   RemoveUsed2(fSeeds,0.5,0.4,20);
5323  //  //
5324 //   // refit short tracks
5325 //   //
5326   Int_t nseed=fSeeds->GetEntriesFast();
5327 //   for (Int_t i=0; i<nseed; i++) {
5328 //     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
5329 //     if (!pt) continue;    
5330 //     Int_t nc=t.GetNumberOfClusters();
5331 //     if (nc<15) {
5332 //       delete fSeeds->RemoveAt(i);
5333 //       continue;
5334 //     }
5335 //     if (pt->GetKinkIndexes()[0]!=0) continue; // ignore kink candidates
5336 //     if (nc>100) continue;                     // hopefully, enough for ITS
5337 //     AliTPCseed *seed2 = new AliTPCseed(*pt);
5338 //     //seed2->Reset(kFALSE);
5339 //     //pt->ResetCovariance();
5340 //     seed2->Modify(1);
5341 //     FollowBackProlongation(*seed2,158);
5342 //     //seed2->Reset(kFALSE);
5343 //     seed2->Modify(10);
5344 //     FollowProlongation(*seed2,0);
5345 //     TTreeSRedirector &cstream = *fDebugStreamer;
5346 //     cstream<<"Crefit"<<
5347 //       "Tr0.="<<pt<<
5348 //       "Tr1.="<<seed2<<
5349 //       "\n";     
5350 //     if (seed2->fN>pt->fN){
5351 //       delete fSeeds->RemoveAt(i);
5352 //       fSeeds->AddAt(seed2,i);
5353 //     }else{
5354 //       delete seed2;
5355 //     }
5356 //   }
5357 //   RemoveUsed2(fSeeds,0.6,0.6,50);
5358
5359 //  FindV0s(fSeeds,fEvent);  
5360   //RemoveDouble(fSeeds,0.2,0.6,11);
5361
5362   //
5363   Int_t found = 0;
5364   for (Int_t i=0; i<nseed; i++) {
5365     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
5366     if (!pt) continue;    
5367     Int_t nc=t.GetNumberOfClusters();
5368     if (nc<15) {
5369       delete fSeeds->RemoveAt(i);
5370       continue;
5371     }
5372     CookLabel(pt,0.1); //For comparison only
5373     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
5374     if ((pt->IsActive() || (pt->fRemoval==10) )){
5375       found++;      
5376       if (fDebug>0) cerr<<found<<'\r';      
5377       pt->fLab2 = i;
5378     }
5379     else
5380       delete fSeeds->RemoveAt(i);
5381   }
5382
5383   
5384   //RemoveOverlap(fSeeds,0.99,7,kTRUE);  
5385   SignShared(fSeeds);  
5386   //RemoveUsed(fSeeds,0.9,0.9,6);
5387   // 
5388   nseed=fSeeds->GetEntriesFast();
5389   found = 0;
5390   for (Int_t i=0; i<nseed; i++) {
5391     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
5392     if (!pt) continue;    
5393     Int_t nc=t.GetNumberOfClusters();
5394     if (nc<15) {
5395       delete fSeeds->RemoveAt(i);
5396       continue;
5397     }
5398     t.SetUniqueID(i);
5399     t.CookdEdx(0.02,0.6);
5400     //    CheckKinkPoint(&t,0.05);
5401     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
5402     if ((pt->IsActive() || (pt->fRemoval==10) )){
5403       found++;
5404       if (fDebug>0){
5405         cerr<<found<<'\r';      
5406       }
5407       pt->fLab2 = i;
5408     }
5409     else
5410       delete fSeeds->RemoveAt(i);
5411     //AliTPCseed * seed1 = ReSeed(pt,0.05,0.5,1);
5412     //if (seed1){
5413     //  FollowProlongation(*seed1,0);
5414     //  Int_t n = seed1->GetNumberOfClusters();
5415     //  printf("fP4\t%f\t%f\n",seed1->GetC(),pt->GetC());
5416     //  printf("fN\t%d\t%d\n", seed1->GetNumberOfClusters(),pt->GetNumberOfClusters());
5417     //
5418     //}
5419     //AliTPCseed * seed2 = ReSeed(pt,0.95,0.5,0.05);
5420     
5421   }
5422
5423   SortTracks(fSeeds, 1);
5424   
5425   /*    
5426   fIteration = 1;
5427   PrepareForBackProlongation(fSeeds,5.);
5428   PropagateBack(fSeeds);
5429   printf("Time for back propagation: \t");timer.Print();timer.Start();
5430   
5431   fIteration = 2;
5432   
5433   PrepareForProlongation(fSeeds,5.);
5434   PropagateForward2(fSeeds);
5435    
5436   printf("Time for FORWARD propagation: \t");timer.Print();timer.Start();
5437   // RemoveUsed(fSeeds,0.7,0.7,6);
5438   //RemoveOverlap(fSeeds,0.9,7,kTRUE);
5439    
5440   nseed=fSeeds->GetEntriesFast();
5441   found = 0;
5442   for (Int_t i=0; i<nseed; i++) {
5443     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
5444     if (!pt) continue;    
5445     Int_t nc=t.GetNumberOfClusters();
5446     if (nc<15) {
5447       delete fSeeds->RemoveAt(i);
5448       continue;
5449     }
5450     t.CookdEdx(0.02,0.6);
5451     //    CookLabel(pt,0.1); //For comparison only
5452     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
5453     if ((pt->IsActive() || (pt->fRemoval==10) )){
5454       cerr<<found++<<'\r';      
5455     }
5456     else
5457       delete fSeeds->RemoveAt(i);
5458     pt->fLab2 = i;
5459   }
5460   */
5461  
5462   //  fNTracks = found;
5463   if (fDebug>0){
5464     Info("Clusters2Tracks","Time for overlap removal, track writing and dedx cooking: \t"); timer.Print();timer.Start();
5465   }
5466   //
5467   //  cerr<<"Number of found tracks : "<<"\t"<<found<<endl;  
5468   Info("Clusters2Tracks","Number of found tracks %d",found);  
5469   savedir->cd();
5470   //  UnloadClusters();
5471   //  
5472   return 0;
5473 }
5474
5475 void AliTPCtrackerMI::Tracking(TObjArray * arr)
5476 {
5477   //
5478   // tracking of the seeds
5479   //
5480
5481   fSectors = fOuterSec;
5482   ParallelTracking(arr,150,63);
5483   fSectors = fOuterSec;
5484   ParallelTracking(arr,63,0);
5485 }
5486
5487 TObjArray * AliTPCtrackerMI::Tracking(Int_t seedtype, Int_t i1, Int_t i2, Float_t cuts[4], Float_t dy, Int_t dsec)
5488 {
5489   //
5490   //
5491   //tracking routine
5492   TObjArray * arr = new TObjArray;
5493   // 
5494   fSectors = fOuterSec;
5495   TStopwatch timer;
5496   timer.Start();
5497   for (Int_t sec=0;sec<fkNOS;sec++){
5498     if (seedtype==3) MakeSeeds3(arr,sec,i1,i2,cuts,dy, dsec);
5499     if (seedtype==4) MakeSeeds5(arr,sec,i1,i2,cuts,dy);    
5500     if (seedtype==2) MakeSeeds2(arr,sec,i1,i2,cuts,dy);
5501   }
5502   if (fDebug>0){
5503     Info("Tracking","\nSeeding - %d\t%d\t%d\t%d\n",seedtype,i1,i2,arr->GetEntriesFast());
5504     timer.Print();
5505     timer.Start();
5506   }
5507   Tracking(arr);  
5508   if (fDebug>0){
5509     timer.Print();
5510   }
5511
5512   return arr;
5513 }
5514
5515 TObjArray * AliTPCtrackerMI::Tracking()
5516 {
5517   //
5518   //
5519   TStopwatch timer;
5520   timer.Start();
5521   Int_t nup=fOuterSec->GetNRows()+fInnerSec->GetNRows();
5522
5523   TObjArray * seeds = new TObjArray;
5524   TObjArray * arr=0;
5525   
5526   Int_t gap =20;
5527   Float_t cuts[4];
5528   cuts[0] = 0.002;
5529   cuts[1] = 1.5;
5530   cuts[2] = 3.;
5531   cuts[3] = 3.;
5532   Float_t fnumber  = 3.0;
5533   Float_t fdensity = 3.0;
5534   
5535   //  
5536   //find primaries  
5537   cuts[0]=0.0066;
5538   for (Int_t delta = 0; delta<18; delta+=6){
5539     //
5540     cuts[0]=0.0070;
5541     cuts[1] = 1.5;
5542     arr = Tracking(3,nup-1-delta,nup-1-delta-gap,cuts,-1,1);
5543     SumTracks(seeds,arr);   
5544     SignClusters(seeds,fnumber,fdensity); 
5545     //
5546     for (Int_t i=2;i<6;i+=2){
5547       // seed high pt tracks
5548       cuts[0]=0.0022;
5549       cuts[1]=0.3;
5550       arr = Tracking(3,nup-i-delta,nup-i-delta-gap,cuts,-1,0);
5551       SumTracks(seeds,arr);   
5552       SignClusters(seeds,fnumber,fdensity);        
5553     }
5554   }
5555   fnumber  = 4;
5556   fdensity = 4.;
5557   //  RemoveUsed(seeds,0.9,0.9,1);
5558   //  UnsignClusters();
5559   //  SignClusters(seeds,fnumber,fdensity);    
5560
5561   //find primaries  
5562   cuts[0]=0.0077;
5563   for (Int_t delta = 20; delta<120; delta+=10){
5564     //
5565     // seed high pt tracks
5566     cuts[0]=0.0060;
5567     cuts[1]=0.3;
5568     cuts[2]=6.;
5569     arr = Tracking(3,nup-delta,nup-delta-gap,cuts,-1);
5570     SumTracks(seeds,arr);   
5571     SignClusters(seeds,fnumber,fdensity);            
5572
5573     cuts[0]=0.003;
5574     cuts[1]=0.3;
5575     cuts[2]=6.;
5576     arr = Tracking(3,nup-delta-5,nup-delta-5-gap,cuts,-1);
5577     SumTracks(seeds,arr);   
5578     SignClusters(seeds,fnumber,fdensity);            
5579   }
5580
5581   cuts[0] = 0.01;
5582   cuts[1] = 2.0;
5583   cuts[2] = 3.;
5584   cuts[3] = 2.0;
5585   fnumber  = 2.;
5586   fdensity = 2.;
5587   
5588   if (fDebug>0){
5589     Info("Tracking()","\n\nPrimary seeding\t%d\n\n",seeds->GetEntriesFast());
5590     timer.Print();
5591     timer.Start();
5592   }
5593   //  RemoveUsed(seeds,0.75,0.75,1);
5594   //UnsignClusters();
5595   //SignClusters(seeds,fnumber,fdensity);
5596   
5597   // find secondaries
5598
5599   cuts[0] = 0.3;
5600   cuts[1] = 1.5;
5601   cuts[2] = 3.;
5602   cuts[3] = 1.5;
5603
5604   arr = Tracking(4,nup-1,nup-1-gap,cuts,-1);
5605   SumTracks(seeds,arr);   
5606   SignClusters(seeds,fnumber,fdensity);   
5607   //
5608   arr = Tracking(4,nup-2,nup-2-gap,cuts,-1);
5609   SumTracks(seeds,arr);   
5610   SignClusters(seeds,fnumber,fdensity);   
5611   //
5612   arr = Tracking(4,nup-3,nup-3-gap,cuts,-1);
5613   SumTracks(seeds,arr);   
5614   SignClusters(seeds,fnumber,fdensity);   
5615   //
5616
5617
5618   for (Int_t delta = 3; delta<30; delta+=5){
5619     //
5620     cuts[0] = 0.3;
5621     cuts[1] = 1.5;
5622     cuts[2] = 3.;
5623     cuts[3] = 1.5;
5624     arr = Tracking(4,nup-1-delta,nup-1-delta-gap,cuts,-1);
5625     SumTracks(seeds,arr);   
5626     SignClusters(seeds,fnumber,fdensity);   
5627     //
5628     arr = Tracking(4,nup-3-delta,nup-5-delta-gap,cuts,4);
5629     SumTracks(seeds,arr);   
5630     SignClusters(seeds,fnumber,fdensity); 
5631     //
5632   } 
5633   fnumber  = 1;
5634   fdensity = 1;
5635   //
5636   // change cuts
5637   fnumber  = 2.;
5638   fdensity = 2.;
5639   cuts[0]=0.0080;
5640
5641   // find secondaries
5642   for (Int_t delta = 30; delta<90; delta+=10){
5643     //
5644     cuts[0] = 0.3;
5645     cuts[1] = 3.5;
5646     cuts[2] = 3.;
5647     cuts[3] = 3.5;
5648     arr = Tracking(4,nup-1-delta,nup-1-delta-gap,cuts,-1);
5649     SumTracks(seeds,arr);   
5650     SignClusters(seeds,fnumber,fdensity);   
5651     //
5652     arr = Tracking(4,nup-5-delta,nup-5-delta-gap,cuts,5 );
5653     SumTracks(seeds,arr);   
5654     SignClusters(seeds,fnumber,fdensity);   
5655   }
5656  
5657   if (fDebug>0){
5658     Info("Tracking()","\n\nSecondary seeding\t%d\n\n",seeds->GetEntriesFast());
5659     timer.Print();
5660     timer.Start();
5661   }
5662
5663   return seeds;
5664   //
5665       
5666 }
5667
5668
5669 void AliTPCtrackerMI::SumTracks(TObjArray *arr1,TObjArray *arr2) const
5670 {
5671   //
5672   //sum tracks to common container
5673   //remove suspicious tracks
5674   Int_t nseed = arr2->GetEntriesFast();
5675   for (Int_t i=0;i<nseed;i++){
5676     AliTPCseed *pt=(AliTPCseed*)arr2->UncheckedAt(i);    
5677     if (pt){
5678       
5679       // NORMAL ACTIVE TRACK
5680       if (pt->IsActive()){
5681         arr1->AddLast(arr2->RemoveAt(i));
5682         continue;
5683       }
5684       //remove not usable tracks
5685       if (pt->fRemoval!=10){
5686         delete arr2->RemoveAt(i);
5687         continue;
5688       }
5689       // REMOVE VERY SHORT  TRACKS
5690       if (pt->GetNumberOfClusters()<20){ 
5691         delete arr2->RemoveAt(i);
5692         continue;
5693       }
5694       // ENABLE ONLY ENOUGH GOOD STOPPED TRACKS
5695       if (pt->GetDensityFirst(20)>0.8 || pt->GetDensityFirst(30)>0.8 || pt->GetDensityFirst(40)>0.7)
5696         arr1->AddLast(arr2->RemoveAt(i));
5697       else{      
5698         delete arr2->RemoveAt(i);
5699       }
5700     }
5701   }
5702   delete arr2;  
5703 }
5704
5705
5706
5707 void  AliTPCtrackerMI::ParallelTracking(TObjArray * arr, Int_t rfirst, Int_t rlast)
5708 {
5709   //
5710   // try to track in parralel
5711
5712   Int_t nseed=arr->GetEntriesFast();
5713   //prepare seeds for tracking
5714   for (Int_t i=0; i<nseed; i++) {
5715     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i), &t=*pt; 
5716     if (!pt) continue;
5717     if (!t.IsActive()) continue;
5718     // follow prolongation to the first layer
5719     if ( (fSectors ==fInnerSec) || (t.fFirstPoint-fParam->GetNRowLow()>rfirst+1) )  
5720       FollowProlongation(t, rfirst+1);
5721   }
5722
5723
5724   //
5725   for (Int_t nr=rfirst; nr>=rlast; nr--){ 
5726     if (nr<fInnerSec->GetNRows()) 
5727       fSectors = fInnerSec;
5728     else
5729       fSectors = fOuterSec;
5730     // make indexes with the cluster tracks for given       
5731
5732     // find nearest cluster
5733     for (Int_t i=0; i<nseed; i++) {
5734       AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i), &t=*pt;       
5735       if (!pt) continue;
5736       if (nr==80) pt->UpdateReference();
5737       if (!pt->IsActive()) continue;
5738       //      if ( (fSectors ==fOuterSec) && (pt->fFirstPoint-fParam->GetNRowLow())<nr) continue;
5739       if (pt->fRelativeSector>17) {
5740         continue;
5741       }
5742       UpdateClusters(t,nr);
5743     }
5744     // prolonagate to the nearest cluster - if founded
5745     for (Int_t i=0; i<nseed; i++) {
5746       AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i); 
5747       if (!pt) continue;
5748       if (!pt->IsActive()) continue; 
5749       // if ((fSectors ==fOuterSec) && (pt->fFirstPoint-fParam->GetNRowLow())<nr) continue;
5750       if (pt->fRelativeSector>17) {
5751         continue;
5752       }
5753       FollowToNextCluster(*pt,nr);
5754     }
5755   }    
5756 }
5757
5758 void AliTPCtrackerMI::PrepareForBackProlongation(TObjArray * arr,Float_t fac) const
5759 {
5760   //
5761   //
5762   // if we use TPC track itself we have to "update" covariance
5763   //
5764   Int_t nseed= arr->GetEntriesFast();
5765   for (Int_t i=0;i<nseed;i++){
5766     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
5767     if (pt) {
5768       pt->Modify(fac);
5769       //
5770       //rotate to current local system at first accepted  point    
5771       Int_t index  = pt->GetClusterIndex2(pt->fFirstPoint); 
5772       Int_t sec    = (index&0xff000000)>>24;
5773       sec = sec%18;
5774       Float_t angle1 = fInnerSec->GetAlpha()*sec+fInnerSec->GetAlphaShift();
5775       if (angle1>TMath::Pi()) 
5776         angle1-=2.*TMath::Pi();
5777       Float_t angle2 = pt->GetAlpha();
5778       
5779       if (TMath::Abs(angle1-angle2)>0.001){
5780         pt->Rotate(angle1-angle2);
5781         //angle2 = pt->GetAlpha();
5782         //pt->fRelativeSector = pt->GetAlpha()/fInnerSec->GetAlpha();
5783         //if (pt->GetAlpha()<0) 
5784         //  pt->fRelativeSector+=18;
5785         //sec = pt->fRelativeSector;
5786       }
5787         
5788     }
5789     
5790   }
5791
5792
5793 }
5794 void AliTPCtrackerMI::PrepareForProlongation(TObjArray * arr, Float_t fac) const
5795 {
5796   //
5797   //
5798   // if we use TPC track itself we have to "update" covariance
5799   //
5800   Int_t nseed= arr->GetEntriesFast();
5801   for (Int_t i=0;i<nseed;i++){
5802     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
5803     if (pt) {
5804       pt->Modify(fac);
5805       pt->fFirstPoint = pt->fLastPoint; 
5806     }
5807     
5808   }
5809
5810
5811 }
5812
5813 Int_t AliTPCtrackerMI::PropagateBack(TObjArray * arr)
5814 {
5815   //
5816   // make back propagation
5817   //
5818   Int_t nseed= arr->GetEntriesFast();
5819   for (Int_t i=0;i<nseed;i++){
5820     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
5821     if (pt&& pt->GetKinkIndex(0)<=0) { 
5822       //AliTPCseed *pt2 = new AliTPCseed(*pt);
5823       fSectors = fInnerSec;
5824       //FollowBackProlongation(*pt,fInnerSec->GetNRows()-1);
5825       //fSectors = fOuterSec;
5826       FollowBackProlongation(*pt,fInnerSec->GetNRows()+fOuterSec->GetNRows()-1);     
5827       //if (pt->GetNumberOfClusters()<(pt->fEsd->GetTPCclusters(0)) ){
5828       //        Error("PropagateBack","Not prolonged track %d",pt->GetLabel());
5829       //        FollowBackProlongation(*pt2,fInnerSec->GetNRows()+fOuterSec->GetNRows()-1);
5830       //}
5831     }
5832     if (pt&& pt->GetKinkIndex(0)>0) {
5833       AliESDkink * kink = fEvent->GetKink(pt->GetKinkIndex(0)-1);
5834       pt->fFirstPoint = kink->GetTPCRow0();
5835       fSectors = fInnerSec;
5836       FollowBackProlongation(*pt,fInnerSec->GetNRows()+fOuterSec->GetNRows()-1);  
5837     }
5838     
5839   }
5840   return 0;
5841 }
5842
5843
5844 Int_t AliTPCtrackerMI::PropagateForward2(TObjArray * arr)
5845 {
5846   //
5847   // make forward propagation
5848   //
5849   Int_t nseed= arr->GetEntriesFast();
5850   //
5851   for (Int_t i=0;i<nseed;i++){
5852     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
5853     if (pt) { 
5854       FollowProlongation(*pt,0);
5855     }
5856   }
5857   return 0;
5858 }
5859
5860
5861 Int_t AliTPCtrackerMI::PropagateForward()
5862 {
5863   //
5864   // propagate track forward
5865   //UnsignClusters();
5866   Int_t nseed = fSeeds->GetEntriesFast();
5867   for (Int_t i=0;i<nseed;i++){
5868     AliTPCseed *pt = (AliTPCseed*)fSeeds->UncheckedAt(i);
5869     if (pt){
5870       AliTPCseed &t = *pt;
5871       Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
5872       if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
5873       if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
5874       t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
5875     }
5876   }
5877   
5878   fSectors = fOuterSec;
5879   ParallelTracking(fSeeds,fOuterSec->GetNRows()+fInnerSec->GetNRows()-1,fInnerSec->GetNRows());
5880   fSectors = fInnerSec;
5881   ParallelTracking(fSeeds,fInnerSec->GetNRows()-1,0);
5882   //WriteTracks();
5883   return 1;
5884 }
5885
5886
5887
5888
5889
5890
5891 Int_t AliTPCtrackerMI::PropagateBack(AliTPCseed * pt, Int_t row0, Int_t row1)
5892 {
5893   //
5894   // make back propagation, in between row0 and row1
5895   //
5896   
5897   if (pt) { 
5898     fSectors = fInnerSec;
5899     Int_t  r1;
5900     //
5901     if (row1<fSectors->GetNRows()) 
5902       r1 = row1;
5903     else 
5904       r1 = fSectors->GetNRows()-1;
5905
5906     if (row0<fSectors->GetNRows()&& r1>0 )
5907       FollowBackProlongation(*pt,r1);
5908     if (row1<=fSectors->GetNRows())
5909       return 0;
5910     //
5911     r1 = row1 - fSectors->GetNRows();
5912     if (r1<=0) return 0;
5913     if (r1>=fOuterSec->GetNRows()) return 0;
5914     fSectors = fOuterSec;
5915     return FollowBackProlongation(*pt,r1);
5916   }        
5917   return 0;
5918 }
5919
5920
5921
5922
5923 void  AliTPCtrackerMI::GetShape(AliTPCseed * seed, Int_t row)
5924 {
5925   //
5926   //
5927   Float_t sd2 = TMath::Abs((fParam->GetZLength()-TMath::Abs(seed->GetZ())))*fParam->GetDiffL()*fParam->GetDiffL();
5928   //  Float_t padlength =  fParam->GetPadPitchLength(seed->fSector);
5929   Float_t padlength =  GetPadPitchLength(row);
5930   //
5931   Float_t sresy = (seed->fSector < fParam->GetNSector()/2) ? 0.2 :0.3;
5932   Float_t angulary  = seed->GetSnp();
5933   angulary = angulary*angulary/(1-angulary*angulary);
5934   seed->fCurrentSigmaY2 = sd2+padlength*padlength*angulary/12.+sresy*sresy;  
5935   //
5936   Float_t sresz = fParam->GetZSigma();
5937   Float_t angularz  = seed->GetTgl();
5938   seed->fCurrentSigmaZ2 = sd2+padlength*padlength*angularz*angularz*(1+angulary)/12.+sresz*sresz;
5939   /*
5940   Float_t wy = GetSigmaY(seed);
5941   Float_t wz = GetSigmaZ(seed);
5942   wy*=wy;
5943   wz*=wz;
5944   if (TMath::Abs(wy/seed->fCurrentSigmaY2-1)>0.0001 || TMath::Abs(wz/seed->fCurrentSigmaZ2-1)>0.0001 ){
5945     printf("problem\n");
5946   }
5947   */
5948 }
5949
5950
5951 Float_t  AliTPCtrackerMI::GetSigmaY(AliTPCseed * seed)
5952 {
5953   //
5954   //  
5955   Float_t sd2 = TMath::Abs((fParam->GetZLength()-TMath::Abs(seed->GetZ())))*fParam->GetDiffL()*fParam->GetDiffL();
5956   Float_t padlength =  fParam->GetPadPitchLength(seed->fSector);
5957   Float_t sres = (seed->fSector < fParam->GetNSector()/2) ? 0.2 :0.3;
5958   Float_t angular  = seed->GetSnp();
5959   angular = angular*angular/(1-angular*angular);
5960   //  angular*=angular;
5961   //angular  = TMath::Sqrt(angular/(1-angular));
5962   Float_t res = TMath::Sqrt(sd2+padlength*padlength*angular/12.+sres*sres);
5963   return res;
5964 }
5965 Float_t  AliTPCtrackerMI::GetSigmaZ(AliTPCseed * seed)
5966 {
5967   //
5968   //
5969   Float_t sd2 = TMath::Abs((fParam->GetZLength()-TMath::Abs(seed->GetZ())))*fParam->GetDiffL()*fParam->GetDiffL();
5970   Float_t padlength =  fParam->GetPadPitchLength(seed->fSector);
5971   Float_t sres = fParam->GetZSigma();
5972   Float_t angular  = seed->GetTgl();
5973   Float_t res = TMath::Sqrt(sd2+padlength*padlength*angular*angular/12.+sres*sres);
5974   return res;
5975 }
5976
5977
5978 //__________________________________________________________________________
5979 void AliTPCtrackerMI::CookLabel(AliTPCseed *t, Float_t wrong) const {
5980   //--------------------------------------------------------------------
5981   //This function "cooks" a track label. If label<0, this track is fake.
5982   //--------------------------------------------------------------------
5983   Int_t noc=t->GetNumberOfClusters();
5984   if (noc<10){
5985     //printf("\nnot founded prolongation\n\n\n");
5986     //t->Dump();
5987     return ;
5988   }
5989   Int_t lb[160];
5990   Int_t mx[160];
5991   AliTPCclusterMI *clusters[160];
5992   //
5993   for (Int_t i=0;i<160;i++) {
5994     clusters[i]=0;
5995     lb[i]=mx[i]=0;
5996   }
5997
5998   Int_t i;
5999   Int_t current=0;
6000   for (i=0; i<160 && current<noc; i++) {
6001      
6002      Int_t index=t->GetClusterIndex2(i);
6003      if (index<=0) continue; 
6004      if (index&0x8000) continue;
6005      //     
6006      //clusters[current]=GetClusterMI(index);
6007      if (t->fClusterPointer[i]){
6008        clusters[current]=t->fClusterPointer[i];     
6009        current++;
6010      }
6011   }
6012   noc = current;
6013
6014   Int_t lab=123456789;
6015   for (i=0; i<noc; i++) {
6016     AliTPCclusterMI *c=clusters[i];
6017     if (!c) continue;
6018     lab=TMath::Abs(c->GetLabel(0));
6019     Int_t j;
6020     for (j=0; j<noc; j++) if (lb[j]==lab || mx[j]==0) break;
6021     lb[j]=lab;
6022     (mx[j])++;
6023   }
6024
6025   Int_t max=0;
6026   for (i=0; i<noc; i++) if (mx[i]>max) {max=mx[i]; lab=lb[i];}
6027     
6028   for (i=0; i<noc; i++) {
6029     AliTPCclusterMI *c=clusters[i]; 
6030     if (!c) continue;
6031     if (TMath::Abs(c->GetLabel(1)) == lab ||
6032         TMath::Abs(c->GetLabel(2)) == lab ) max++;
6033   }
6034
6035   if ((1.- Float_t(max)/noc) > wrong) lab=-lab;
6036
6037   else {
6038      Int_t tail=Int_t(0.10*noc);
6039      max=0;
6040      Int_t ind=0;
6041      for (i=1; i<=160&&ind<tail; i++) {
6042        //       AliTPCclusterMI *c=clusters[noc-i];
6043        AliTPCclusterMI *c=clusters[i];
6044        if (!c) continue;
6045        if (lab == TMath::Abs(c->GetLabel(0)) ||
6046            lab == TMath::Abs(c->GetLabel(1)) ||
6047            lab == TMath::Abs(c->GetLabel(2))) max++;
6048        ind++;
6049      }
6050      if (max < Int_t(0.5*tail)) lab=-lab;
6051   }
6052
6053   t->SetLabel(lab);
6054
6055   //  delete[] lb;
6056   //delete[] mx;
6057   //delete[] clusters;
6058 }
6059
6060
6061 //__________________________________________________________________________
6062 Int_t AliTPCtrackerMI::CookLabel(AliTPCseed *t, Float_t wrong,Int_t first, Int_t last) const {
6063   //--------------------------------------------------------------------
6064   //This function "cooks" a track label. If label<0, this track is fake.
6065   //--------------------------------------------------------------------
6066   Int_t noc=t->GetNumberOfClusters();
6067   if (noc<10){
6068     //printf("\nnot founded prolongation\n\n\n");
6069     //t->Dump();
6070     return -1;
6071   }
6072   Int_t lb[160];
6073   Int_t mx[160];
6074   AliTPCclusterMI *clusters[160];
6075   //
6076   for (Int_t i=0;i<160;i++) {
6077     clusters[i]=0;
6078     lb[i]=mx[i]=0;
6079   }
6080
6081   Int_t i;
6082   Int_t current=0;
6083   for (i=0; i<160 && current<noc; i++) {
6084     if (i<first) continue;
6085     if (i>last)  continue;
6086      Int_t index=t->GetClusterIndex2(i);
6087      if (index<=0) continue; 
6088      if (index&0x8000) continue;
6089      //     
6090      //clusters[current]=GetClusterMI(index);
6091      if (t->fClusterPointer[i]){
6092        clusters[current]=t->fClusterPointer[i];     
6093        current++;
6094      }
6095   }
6096   noc = current;
6097   if (noc<5) return -1;
6098   Int_t lab=123456789;
6099   for (i=0; i<noc; i++) {
6100     AliTPCclusterMI *c=clusters[i];
6101     if (!c) continue;
6102     lab=TMath::Abs(c->GetLabel(0));
6103     Int_t j;
6104     for (j=0; j<noc; j++) if (lb[j]==lab || mx[j]==0) break;
6105     lb[j]=lab;
6106     (mx[j])++;
6107   }
6108
6109   Int_t max=0;
6110   for (i=0; i<noc; i++) if (mx[i]>max) {max=mx[i]; lab=lb[i];}
6111     
6112   for (i=0; i<noc; i++) {
6113     AliTPCclusterMI *c=clusters[i]; 
6114     if (!c) continue;
6115     if (TMath::Abs(c->GetLabel(1)) == lab ||
6116         TMath::Abs(c->GetLabel(2)) == lab ) max++;
6117   }
6118
6119   if ((1.- Float_t(max)/noc) > wrong) lab=-lab;
6120
6121   else {
6122      Int_t tail=Int_t(0.10*noc);
6123      max=0;
6124      Int_t ind=0;
6125      for (i=1; i<=160&&ind<tail; i++) {
6126        //       AliTPCclusterMI *c=clusters[noc-i];
6127        AliTPCclusterMI *c=clusters[i];
6128        if (!c) continue;
6129        if (lab == TMath::Abs(c->GetLabel(0)) ||
6130            lab == TMath::Abs(c->GetLabel(1)) ||
6131            lab == TMath::Abs(c->GetLabel(2))) max++;
6132        ind++;
6133      }
6134      if (max < Int_t(0.5*tail)) lab=-lab;
6135   }
6136
6137   //  t->SetLabel(lab);
6138   return lab;
6139   //  delete[] lb;
6140   //delete[] mx;
6141   //delete[] clusters;
6142 }
6143
6144
6145 Int_t  AliTPCtrackerMI::AliTPCSector::GetRowNumber(Double_t x) const 
6146 {
6147   //return pad row number for this x
6148   Double_t r;
6149   if (fN < 64){
6150     r=fRow[fN-1].GetX();
6151     if (x > r) return fN;
6152     r=fRow[0].GetX();
6153     if (x < r) return -1;
6154     return Int_t((x-r)/fPadPitchLength + 0.5);}
6155   else{    
6156     r=fRow[fN-1].GetX();
6157     if (x > r) return fN;
6158     r=fRow[0].GetX();
6159     if (x < r) return -1;
6160     Double_t r1=fRow[64].GetX();
6161     if(x<r1){       
6162       return Int_t((x-r)/f1PadPitchLength + 0.5);}
6163     else{
6164       return (Int_t((x-r1)/f2PadPitchLength + 0.5)+64);} 
6165   }
6166 }
6167
6168 Int_t  AliTPCtrackerMI::GetRowNumber(Double_t x[3]) const 
6169 {
6170   //return pad row number for given x vector
6171   Float_t phi = TMath::ATan2(x[1],x[0]);
6172   if(phi<0) phi=2.*TMath::Pi()+phi;
6173   //  Get the local angle in the sector philoc
6174   const Float_t kRaddeg = 180/3.14159265358979312;
6175   Float_t phiangle   = (Int_t (phi*kRaddeg/20.) + 0.5)*20./kRaddeg;
6176   Double_t localx    = x[0]*TMath::Cos(phiangle)-x[1]*TMath::Sin(phiangle);
6177   return GetRowNumber(localx);
6178 }
6179
6180 //_________________________________________________________________________
6181 void AliTPCtrackerMI::AliTPCSector::Setup(const AliTPCParam *par, Int_t f) {
6182   //-----------------------------------------------------------------------
6183   // Setup inner sector
6184   //-----------------------------------------------------------------------
6185   if (f==0) {
6186      fAlpha=par->GetInnerAngle();
6187      fAlphaShift=par->GetInnerAngleShift();
6188      fPadPitchWidth=par->GetInnerPadPitchWidth();
6189      fPadPitchLength=par->GetInnerPadPitchLength();
6190      fN=par->GetNRowLow();
6191      fRow=new AliTPCRow[fN];
6192      for (Int_t i=0; i<fN; i++) {
6193        fRow[i].SetX(par->GetPadRowRadiiLow(i));
6194        fRow[i].fDeadZone =1.5;  //1.5 cm of dead zone
6195      }
6196   } else {
6197      fAlpha=par->GetOuterAngle();
6198      fAlphaShift=par->GetOuterAngleShift();
6199      fPadPitchWidth  = par->GetOuterPadPitchWidth();
6200      fPadPitchLength = par->GetOuter1PadPitchLength();
6201      f1PadPitchLength = par->GetOuter1PadPitchLength();
6202      f2PadPitchLength = par->GetOuter2PadPitchLength();
6203
6204      fN=par->GetNRowUp();
6205      fRow=new AliTPCRow[fN];
6206      for (Int_t i=0; i<fN; i++) {
6207        fRow[i].SetX(par->GetPadRowRadiiUp(i)); 
6208        fRow[i].fDeadZone =1.5;  // 1.5 cm of dead zone
6209      }
6210   } 
6211 }
6212
6213 AliTPCtrackerMI::AliTPCRow::AliTPCRow() {
6214   //
6215   // default constructor
6216   fN=0;
6217   fN1=0;
6218   fN2=0;
6219   fClusters1=0;
6220   fClusters2=0;
6221 }
6222
6223 AliTPCtrackerMI::AliTPCRow::~AliTPCRow(){
6224   //
6225
6226 }
6227
6228
6229
6230 //_________________________________________________________________________
6231 void 
6232 AliTPCtrackerMI::AliTPCRow::InsertCluster(const AliTPCclusterMI* c, UInt_t index) {
6233   //-----------------------------------------------------------------------
6234   // Insert a cluster into this pad row in accordence with its y-coordinate
6235   //-----------------------------------------------------------------------
6236   if (fN==kMaxClusterPerRow) {
6237     cerr<<"AliTPCRow::InsertCluster(): Too many clusters !\n"; return;
6238   }
6239   if (fN==0) {fIndex[0]=index; fClusters[fN++]=c; return;}
6240   Int_t i=Find(c->GetZ());
6241   memmove(fClusters+i+1 ,fClusters+i,(fN-i)*sizeof(AliTPCclusterMI*));
6242   memmove(fIndex   +i+1 ,fIndex   +i,(fN-i)*sizeof(UInt_t));
6243   fIndex[i]=index; fClusters[i]=c; fN++;
6244 }
6245
6246 void AliTPCtrackerMI::AliTPCRow::ResetClusters() {
6247    //
6248    // reset clusters
6249    fN  = 0; 
6250    fN1 = 0;
6251    fN2 = 0;
6252    //delete[] fClusterArray; 
6253    if (fClusters1) delete []fClusters1; 
6254    if (fClusters2) delete []fClusters2; 
6255    //fClusterArray=0;
6256    fClusters1 = 0;
6257    fClusters2 = 0;
6258 }
6259
6260
6261 //___________________________________________________________________
6262 Int_t AliTPCtrackerMI::AliTPCRow::Find(Double_t z) const {
6263   //-----------------------------------------------------------------------
6264   // Return the index of the nearest cluster 
6265   //-----------------------------------------------------------------------
6266   if (fN==0) return 0;
6267   if (z <= fClusters[0]->GetZ()) return 0;
6268   if (z > fClusters[fN-1]->GetZ()) return fN;
6269   Int_t b=0, e=fN-1, m=(b+e)/2;
6270   for (; b<e; m=(b+e)/2) {
6271     if (z > fClusters[m]->GetZ()) b=m+1;
6272     else e=m; 
6273   }
6274   return m;
6275 }
6276
6277
6278
6279 //___________________________________________________________________
6280 AliTPCclusterMI * AliTPCtrackerMI::AliTPCRow::FindNearest(Double_t y, Double_t z, Double_t roady, Double_t roadz) const {
6281   //-----------------------------------------------------------------------
6282   // Return the index of the nearest cluster in z y 
6283   //-----------------------------------------------------------------------
6284   Float_t maxdistance = roady*roady + roadz*roadz;
6285
6286   AliTPCclusterMI *cl =0;
6287   for (Int_t i=Find(z-roadz); i<fN; i++) {
6288       AliTPCclusterMI *c=(AliTPCclusterMI*)(fClusters[i]);
6289       if (c->GetZ() > z+roadz) break;
6290       if ( (c->GetY()-y) >  roady ) continue;
6291       Float_t distance = (c->GetZ()-z)*(c->GetZ()-z)+(c->GetY()-y)*(c->GetY()-y);
6292       if (maxdistance>distance) {
6293         maxdistance = distance;
6294         cl=c;       
6295       }
6296   }
6297   return cl;      
6298 }
6299
6300 AliTPCclusterMI * AliTPCtrackerMI::AliTPCRow::FindNearest2(Double_t y, Double_t z, Double_t roady, Double_t roadz,UInt_t & index) const 
6301 {
6302   //-----------------------------------------------------------------------
6303   // Return the index of the nearest cluster in z y 
6304   //-----------------------------------------------------------------------
6305   Float_t maxdistance = roady*roady + roadz*roadz;
6306   AliTPCclusterMI *cl =0;
6307
6308   //PH Check boundaries. 510 is the size of fFastCluster
6309   Int_t iz1 = Int_t(z-roadz+254.5);
6310   if (iz1<0 || iz1>=510) return cl;
6311   iz1 = TMath::Max(fFastCluster[iz1]-1,0);
6312   Int_t iz2 = Int_t(z+roadz+255.5);
6313   if (iz2<0 || iz2>=510) return cl;
6314   iz2 = TMath::Min(fFastCluster[iz2]+1,fN);
6315
6316   //FindNearest3(y,z,roady,roadz,index);
6317   //  for (Int_t i=Find(z-roadz); i<fN; i++) {
6318   for (Int_t i=iz1; i<iz2; i++) {
6319       AliTPCclusterMI *c=(AliTPCclusterMI*)(fClusters[i]);
6320       if (c->GetZ() > z+roadz) break;
6321       if ( c->GetY()-y >  roady ) continue;
6322       if ( y-c->GetY() >  roady ) continue;
6323       Float_t distance = (c->GetZ()-z)*(c->GetZ()-z)+(c->GetY()-y)*(c->GetY()-y);
6324       if (maxdistance>distance) {
6325         maxdistance = distance;
6326         cl=c;       
6327         index =i;
6328         //roady = TMath::Sqrt(maxdistance);
6329       }
6330   }
6331   return cl;      
6332 }
6333
6334
6335
6336 AliTPCclusterMI * AliTPCtrackerMI::AliTPCRow::FindNearest3(Double_t y, Double_t z, Double_t roady, Double_t roadz,UInt_t & index) const 
6337 {
6338   //-----------------------------------------------------------------------
6339   // Return the index of the nearest cluster in z y 
6340   //-----------------------------------------------------------------------
6341   Float_t maxdistance = roady*roady + roadz*roadz;
6342   //  Int_t iz = Int_t(z+255.);
6343   AliTPCclusterMI *cl =0;
6344   for (Int_t i=Find(z-roadz); i<fN; i++) {
6345     //for (Int_t i=fFastCluster[iz-2]; i<fFastCluster[iz+2]; i++) {
6346       AliTPCclusterMI *c=(AliTPCclusterMI*)(fClusters[i]);
6347       if (c->GetZ() > z+roadz) break;
6348       if ( c->GetY()-y >  roady ) continue;
6349       if ( y-c->GetY() >  roady ) continue;
6350       Float_t distance = (c->GetZ()-z)*(c->GetZ()-z)+(c->GetY()-y)*(c->GetY()-y);
6351       if (maxdistance>distance) {
6352         maxdistance = distance;
6353         cl=c;       
6354         index =i;
6355         //roady = TMath::Sqrt(maxdistance);
6356       }
6357   }
6358   return cl;      
6359 }
6360
6361
6362
6363
6364
6365 // AliTPCTrackerPoint * AliTPCseed::GetTrackPoint(Int_t i)
6366 // {
6367 //   //
6368 //   // 
6369 //   return &fTrackPoints[i];
6370 // }
6371
6372