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