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