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