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