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