]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCtrackerMI.cxx
3212e4d0cc6691851851392731271ee36b3d0718
[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     FollowToNext(t,nr);                                                             
1912   }   
1913   return 1;
1914 }
1915
1916
1917
1918
1919    
1920 Float_t AliTPCtrackerMI::OverlapFactor(AliTPCseed * s1, AliTPCseed * s2, Int_t &sum1, Int_t & sum2)
1921 {
1922   //
1923   //
1924   sum1=0;
1925   sum2=0;
1926   Int_t sum=0;
1927   //
1928   Float_t dz2 =(s1->GetZ() - s2->GetZ());
1929   dz2*=dz2;  
1930
1931   Float_t dy2 =TMath::Abs((s1->GetY() - s2->GetY()));
1932   dy2*=dy2;
1933   Float_t distance = TMath::Sqrt(dz2+dy2);
1934   if (distance>4.) return 0; // if there are far away  - not overlap - to reduce combinatorics
1935  
1936   //  Int_t offset =0;
1937   Int_t firstpoint = TMath::Min(s1->fFirstPoint,s2->fFirstPoint);
1938   Int_t lastpoint = TMath::Max(s1->fLastPoint,s2->fLastPoint);
1939   if (lastpoint>160) 
1940     lastpoint =160;
1941   if (firstpoint<0) 
1942     firstpoint = 0;
1943   if (firstpoint>lastpoint) {
1944     firstpoint =lastpoint;
1945     //    lastpoint  =160;
1946   }
1947     
1948   
1949   for (Int_t i=firstpoint-1;i<lastpoint+1;i++){
1950     if (s1->GetClusterIndex2(i)>0) sum1++;
1951     if (s2->GetClusterIndex2(i)>0) sum2++;
1952     if (s1->GetClusterIndex2(i)==s2->GetClusterIndex2(i) && s1->GetClusterIndex2(i)>0) {
1953       sum++;
1954     }
1955   }
1956   if (sum<5) return 0;
1957
1958   Float_t summin = TMath::Min(sum1+1,sum2+1);
1959   Float_t ratio = (sum+1)/Float_t(summin);
1960   return ratio;
1961 }
1962
1963 void  AliTPCtrackerMI::SignShared(AliTPCseed * s1, AliTPCseed * s2)
1964 {
1965   //
1966   //
1967   if (TMath::Abs(s1->GetC()-s2->GetC())>0.004) return;
1968   if (TMath::Abs(s1->GetTgl()-s2->GetTgl())>0.6) return;
1969
1970   Float_t dz2 =(s1->GetZ() - s2->GetZ());
1971   dz2*=dz2;
1972   Float_t dy2 =(s1->GetY() - s2->GetY());
1973   dy2*=dy2;
1974   Float_t distance = dz2+dy2;
1975   if (distance>325.) return ; // if there are far away  - not overlap - to reduce combinatorics
1976   
1977   //
1978   Int_t sumshared=0;
1979   //
1980   Int_t firstpoint = TMath::Max(s1->fFirstPoint,s2->fFirstPoint);
1981   Int_t lastpoint = TMath::Min(s1->fLastPoint,s2->fLastPoint);
1982   //
1983   if (firstpoint>=lastpoint-5) return;;
1984
1985   for (Int_t i=firstpoint;i<lastpoint;i++){
1986     //    if ( (s1->GetClusterIndex2(i)&0xFFFF8FFF)==(s2->GetClusterIndex2(i)&0xFFFF8FFF) && s1->GetClusterIndex2(i)>0) {
1987     if ( (s1->GetClusterIndex2(i))==(s2->GetClusterIndex2(i)) && s1->GetClusterIndex2(i)>0) {
1988       sumshared++;
1989     }
1990   }
1991   if (sumshared>4){
1992     // sign clusters
1993     //
1994     for (Int_t i=firstpoint;i<lastpoint;i++){
1995       //      if ( (s1->GetClusterIndex2(i)&0xFFFF8FFF)==(s2->GetClusterIndex2(i)&0xFFFF8FFF) && s1->GetClusterIndex2(i)>0) {
1996       if ( (s1->GetClusterIndex2(i))==(s2->GetClusterIndex2(i)) && s1->GetClusterIndex2(i)>0) {
1997         AliTPCTrackerPoint *p1  = s1->GetTrackPoint(i);
1998         AliTPCTrackerPoint *p2  = s2->GetTrackPoint(i);; 
1999         if (s1->IsActive()&&s2->IsActive()){
2000           p1->fIsShared = kTRUE;
2001           p2->fIsShared = kTRUE;
2002         }       
2003       }
2004     }
2005   }
2006   //  
2007   if (sumshared>10){
2008     for (Int_t i=0;i<4;i++){
2009       if (s1->fOverlapLabels[3*i]==0){
2010         s1->fOverlapLabels[3*i] = s2->GetLabel();
2011         s1->fOverlapLabels[3*i+1] = sumshared;
2012         s1->fOverlapLabels[3*i+2] = s2->GetUniqueID();
2013         break;
2014       } 
2015     }
2016     for (Int_t i=0;i<4;i++){
2017       if (s2->fOverlapLabels[3*i]==0){
2018         s2->fOverlapLabels[3*i] = s1->GetLabel();
2019         s2->fOverlapLabels[3*i+1] = sumshared;
2020         s2->fOverlapLabels[3*i+2] = s1->GetUniqueID();
2021         break;
2022       } 
2023     }    
2024   }
2025   
2026 }
2027
2028 void  AliTPCtrackerMI::SignShared(TObjArray * arr)
2029 {
2030   //
2031   //sort trackss according sectors
2032   //  
2033   for (Int_t i=0; i<arr->GetEntriesFast(); i++) {
2034     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2035     if (!pt) continue;
2036     //if (pt) RotateToLocal(pt);
2037     pt->fSort = 0;
2038   }
2039   arr->UnSort();
2040   arr->Sort();  // sorting according z
2041   arr->Expand(arr->GetEntries());
2042   //
2043   //
2044   Int_t nseed=arr->GetEntriesFast();
2045   for (Int_t i=0; i<nseed; i++) {
2046     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2047     if (!pt) continue;
2048     for (Int_t j=0;j<=12;j++){
2049       pt->fOverlapLabels[j] =0;
2050     }
2051   }
2052   for (Int_t i=0; i<nseed; i++) {
2053     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2054     if (!pt) continue;
2055     if (pt->fRemoval>10) continue;
2056     for (Int_t j=i+1; j<nseed; j++){
2057       AliTPCseed *pt2=(AliTPCseed*)arr->UncheckedAt(j);
2058       //      if (pt2){
2059       if (pt2->fRemoval<=10) {
2060         if ( TMath::Abs(pt->fRelativeSector-pt2->fRelativeSector)>0) break;
2061         SignShared(pt,pt2);
2062       }
2063     }  
2064   }
2065 }
2066
2067 void  AliTPCtrackerMI::RemoveDouble(TObjArray * arr, Float_t factor1, Float_t factor2,  Int_t removalindex)
2068 {
2069   //
2070   //sort trackss according sectors
2071   //
2072   if (fDebug&1) {
2073     Info("RemoveDouble","Number of tracks before double removal- %d\n",arr->GetEntries());
2074   }
2075   //
2076   for (Int_t i=0; i<arr->GetEntriesFast(); i++) {
2077     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2078     if (!pt) continue;
2079     pt->fSort = 0;
2080   }
2081   arr->UnSort();
2082   arr->Sort();  // sorting according z
2083   arr->Expand(arr->GetEntries());
2084   //
2085   //reset overlap labels
2086   //
2087   Int_t nseed=arr->GetEntriesFast();
2088   for (Int_t i=0; i<nseed; i++) {
2089     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2090     if (!pt) continue;
2091     pt->SetUniqueID(i);
2092     for (Int_t j=0;j<=12;j++){
2093       pt->fOverlapLabels[j] =0;
2094     }
2095   }
2096   //
2097   //sign shared tracks
2098   for (Int_t i=0; i<nseed; i++) {
2099     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2100     if (!pt) continue;
2101     if (pt->fRemoval>10) continue;
2102     Float_t deltac = pt->GetC()*0.1;
2103     for (Int_t j=i+1; j<nseed; j++){
2104       AliTPCseed *pt2=(AliTPCseed*)arr->UncheckedAt(j);
2105       //      if (pt2){
2106       if (pt2->fRemoval<=10) {
2107         if ( TMath::Abs(pt->fRelativeSector-pt2->fRelativeSector)>0) break;
2108         if (TMath::Abs(pt->GetC()  -pt2->GetC())>deltac) continue;
2109         if (TMath::Abs(pt->GetTgl()-pt2->GetTgl())>0.05) continue;
2110         //
2111         SignShared(pt,pt2);
2112       }
2113     }
2114   }
2115   //
2116   // remove highly shared tracks
2117   for (Int_t i=0; i<nseed; i++) {
2118     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2119     if (!pt) continue;
2120     if (pt->fRemoval>10) continue;
2121     //
2122     Int_t sumshared =0;
2123     for (Int_t j=0;j<4;j++){
2124       sumshared = pt->fOverlapLabels[j*3+1];      
2125     }
2126     Float_t factor = factor1;
2127     if (pt->fRemoval>0) factor = factor2;
2128     if (sumshared/pt->GetNumberOfClusters()>factor){
2129       for (Int_t j=0;j<4;j++){
2130         if (pt->fOverlapLabels[3*j]==0) continue;
2131         if (pt->fOverlapLabels[3*j+1]<5) continue; 
2132         if (pt->fRemoval==removalindex) continue;      
2133         AliTPCseed * pt2 = (AliTPCseed*)arr->UncheckedAt(pt->fOverlapLabels[3*j+2]);
2134         if (!pt2) continue;
2135         if (pt2->GetSigma2C()<pt->GetSigma2C()){
2136           //      pt->fRemoval = removalindex;
2137           delete arr->RemoveAt(i);        
2138           break;
2139         }
2140       }      
2141     }
2142   }
2143   arr->Compress();
2144   if (fDebug&1) {
2145     Info("RemoveDouble","Number of tracks after double removal- %d\n",arr->GetEntries());
2146   }
2147 }
2148
2149
2150
2151
2152
2153
2154 void AliTPCtrackerMI::SortTracks(TObjArray * arr, Int_t mode) const
2155 {
2156   //
2157   //sort tracks in array according mode criteria
2158   Int_t nseed = arr->GetEntriesFast();    
2159   for (Int_t i=0; i<nseed; i++) {
2160     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2161     if (!pt) {
2162       continue;
2163     }
2164     pt->fSort = mode;
2165   }
2166   arr->UnSort();
2167   arr->Sort();
2168 }
2169
2170 void AliTPCtrackerMI::RemoveUsed(TObjArray * arr, Float_t factor1,  Float_t factor2, Int_t removalindex)
2171 {
2172
2173   //Loop over all tracks and remove "overlaps"
2174   //
2175   //
2176   Int_t nseed = arr->GetEntriesFast();  
2177   Int_t good =0;
2178
2179   for (Int_t i=0; i<nseed; i++) {
2180     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2181     if (!pt) {
2182       delete arr->RemoveAt(i);
2183     }
2184     else{
2185       pt->fSort =1;
2186       pt->fBSigned = kFALSE;
2187     }
2188   }
2189   arr->Compress();
2190   nseed = arr->GetEntriesFast();
2191   arr->UnSort();
2192   arr->Sort();
2193   //
2194   //unsign used
2195   UnsignClusters();
2196   //
2197   for (Int_t i=0; i<nseed; i++) {
2198     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2199     if (!pt) {
2200       continue;
2201     }    
2202     Int_t found,foundable,shared;
2203     if (pt->IsActive()) 
2204       pt->GetClusterStatistic(0,160,found, foundable,shared,kFALSE);
2205     else
2206       pt->GetClusterStatistic(0,160,found, foundable,shared,kTRUE); 
2207     //
2208     Double_t factor = factor2;
2209     if (pt->fBConstrain) factor = factor1;
2210
2211     if ((Float_t(shared)/Float_t(found))>factor){
2212       pt->Desactivate(removalindex);
2213       continue;
2214     }
2215
2216     good++;
2217     for (Int_t i=0; i<160; i++) {
2218       Int_t index=pt->GetClusterIndex2(i);
2219       if (index<0 || index&0x8000 ) continue;
2220       AliTPCclusterMI *c= pt->fClusterPointer[i];        
2221       if (!c) continue;
2222       //      if (!c->IsUsed(10)) c->Use(10);
2223       //if (pt->IsActive()) 
2224       c->Use(10);  
2225       //else
2226       //        c->Use(5);
2227     }
2228     
2229   }
2230   fNtracks = good;
2231   if (fDebug>0){
2232     Info("RemoveUsed","\n*****\nNumber of good tracks after shared removal\t%d\n",fNtracks);
2233   }
2234 }
2235
2236
2237 void AliTPCtrackerMI::RemoveUsed2(TObjArray * arr, Float_t factor1,  Float_t factor2, Int_t minimal)
2238 {
2239
2240   //Loop over all tracks and remove "overlaps"
2241   //
2242   //
2243   UnsignClusters();
2244   //
2245   Int_t nseed = arr->GetEntriesFast();  
2246   Float_t * quality = new Float_t[nseed];
2247   Int_t   * indexes = new Int_t[nseed];
2248   Int_t good =0;
2249   //
2250   //
2251   for (Int_t i=0; i<nseed; i++) {
2252     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2253     if (!pt){
2254       quality[i]=-1;
2255       continue;
2256     }
2257     pt->UpdatePoints();    //select first last max dens points
2258     Float_t * points = pt->GetPoints();
2259     if (points[3]<0.8) quality[i] =-1;
2260     //
2261     quality[i] = (points[2]-points[0])+pt->GetNumberOfClusters();
2262   }
2263   TMath::Sort(nseed,quality,indexes);
2264   //
2265   //
2266   for (Int_t itrack=0; itrack<nseed; itrack++) {
2267     Int_t trackindex = indexes[itrack];
2268     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(trackindex);    
2269     if (quality[trackindex]<0){
2270       if (pt) {
2271         delete arr->RemoveAt(trackindex);
2272       }
2273       else{
2274         arr->RemoveAt(trackindex);
2275       }
2276       continue;
2277     }
2278     //
2279     Int_t first = Int_t(pt->GetPoints()[0]);
2280     Int_t last  = Int_t(pt->GetPoints()[2]);
2281     Double_t factor = (pt->fBConstrain) ? factor1: factor2;
2282     //
2283     Int_t found,foundable,shared;
2284     pt->GetClusterStatistic(first,last, found, foundable,shared,kFALSE);
2285     Float_t sharedfactor = Float_t(shared+1)/Float_t(found+1);
2286     Bool_t itsgold =kFALSE;
2287     if (pt->fEsd){
2288       Int_t dummy[12];
2289       if (pt->fEsd->GetITSclusters(dummy)>4) itsgold= kTRUE;
2290     }
2291     if (!itsgold){
2292       //
2293       if (Float_t(shared+1)/Float_t(found+1)>factor){
2294         if (pt->GetKinkIndexes()[0]!=0) continue;  //don't remove tracks  - part of the kinks
2295         delete arr->RemoveAt(trackindex);
2296         continue;
2297       }      
2298       if (pt->GetNumberOfClusters()<50&&(found-0.5*shared)<minimal){  //remove short tracks
2299         if (pt->GetKinkIndexes()[0]!=0) continue;  //don't remove tracks  - part of the kinks
2300         delete arr->RemoveAt(trackindex);
2301         continue;
2302       }
2303     }
2304
2305     good++;
2306     if (sharedfactor>0.4) continue;
2307     if (pt->GetKinkIndexes()[0]>0) continue;
2308     for (Int_t i=first; i<last; i++) {
2309       Int_t index=pt->GetClusterIndex2(i);
2310       // if (index<0 || index&0x8000 ) continue;
2311       if (index<0 || index&0x8000 ) continue;
2312       AliTPCclusterMI *c= pt->fClusterPointer[i];        
2313       if (!c) continue;
2314       c->Use(10);  
2315     }    
2316   }
2317   fNtracks = good;
2318   if (fDebug>0){
2319     Info("RemoveUsed","\n*****\nNumber of good tracks after shared removal\t%d\n",fNtracks);
2320   }
2321   delete []quality;
2322   delete []indexes;
2323 }
2324
2325 void AliTPCtrackerMI::UnsignClusters() 
2326 {
2327   //
2328   // loop over all clusters and unsign them
2329   //
2330   
2331   for (Int_t sec=0;sec<fkNIS;sec++){
2332     for (Int_t row=0;row<fInnerSec->GetNRows();row++){
2333       AliTPCclusterMI *cl = fInnerSec[sec][row].fClusters1;
2334       for (Int_t icl =0;icl< fInnerSec[sec][row].fN1;icl++)
2335         //      if (cl[icl].IsUsed(10))         
2336         cl[icl].Use(-1);
2337       cl = fInnerSec[sec][row].fClusters2;
2338       for (Int_t icl =0;icl< fInnerSec[sec][row].fN2;icl++)
2339         //if (cl[icl].IsUsed(10))       
2340           cl[icl].Use(-1);      
2341     }
2342   }
2343   
2344   for (Int_t sec=0;sec<fkNOS;sec++){
2345     for (Int_t row=0;row<fOuterSec->GetNRows();row++){
2346       AliTPCclusterMI *cl = fOuterSec[sec][row].fClusters1;
2347       for (Int_t icl =0;icl< fOuterSec[sec][row].fN1;icl++)
2348         //if (cl[icl].IsUsed(10))       
2349           cl[icl].Use(-1);
2350       cl = fOuterSec[sec][row].fClusters2;
2351       for (Int_t icl =0;icl< fOuterSec[sec][row].fN2;icl++)
2352         //if (cl[icl].IsUsed(10))       
2353         cl[icl].Use(-1);      
2354     }
2355   }
2356   
2357 }
2358
2359
2360
2361 void AliTPCtrackerMI::SignClusters(TObjArray * arr, Float_t fnumber, Float_t fdensity)
2362 {
2363   //
2364   //sign clusters to be "used"
2365   //
2366   // snumber and sdensity sign number of sigmas - bellow mean value to be accepted
2367   // loop over "primaries"
2368   
2369   Float_t sumdens=0;
2370   Float_t sumdens2=0;
2371   Float_t sumn   =0;
2372   Float_t sumn2  =0;
2373   Float_t sumchi =0;
2374   Float_t sumchi2 =0;
2375
2376   Float_t sum    =0;
2377
2378   TStopwatch timer;
2379   timer.Start();
2380
2381   Int_t nseed = arr->GetEntriesFast();
2382   for (Int_t i=0; i<nseed; i++) {
2383     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2384     if (!pt) {
2385       continue;
2386     }    
2387     if (!(pt->IsActive())) continue;
2388     Float_t dens = pt->GetNumberOfClusters()/Float_t(pt->fNFoundable);
2389     if ( (dens>0.7) && (pt->GetNumberOfClusters()>70)){
2390       sumdens += dens;
2391       sumdens2+= dens*dens;
2392       sumn    += pt->GetNumberOfClusters();
2393       sumn2   += pt->GetNumberOfClusters()*pt->GetNumberOfClusters();
2394       Float_t chi2 = pt->GetChi2()/pt->GetNumberOfClusters();
2395       if (chi2>5) chi2=5;
2396       sumchi  +=chi2;
2397       sumchi2 +=chi2*chi2;
2398       sum++;
2399     }
2400   }
2401
2402   Float_t mdensity = 0.9;
2403   Float_t meann    = 130;
2404   Float_t meanchi  = 1;
2405   Float_t sdensity = 0.1;
2406   Float_t smeann    = 10;
2407   Float_t smeanchi  =0.4;
2408   
2409
2410   if (sum>20){
2411     mdensity = sumdens/sum;
2412     meann    = sumn/sum;
2413     meanchi  = sumchi/sum;
2414     //
2415     sdensity = sumdens2/sum-mdensity*mdensity;
2416     sdensity = TMath::Sqrt(sdensity);
2417     //
2418     smeann   = sumn2/sum-meann*meann;
2419     smeann   = TMath::Sqrt(smeann);
2420     //
2421     smeanchi = sumchi2/sum - meanchi*meanchi;
2422     smeanchi = TMath::Sqrt(smeanchi);
2423   }
2424
2425
2426   //REMOVE  SHORT DELTAS or tracks going out of sensitive volume of TPC
2427   //
2428   for (Int_t i=0; i<nseed; i++) {
2429     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2430     if (!pt) {
2431       continue;
2432     }
2433     if (pt->fBSigned) continue;
2434     if (pt->fBConstrain) continue;    
2435     //if (!(pt->IsActive())) continue;
2436     /*
2437     Int_t found,foundable,shared;    
2438     pt->GetClusterStatistic(0,160,found, foundable,shared);
2439     if (shared/float(found)>0.3) {
2440       if (shared/float(found)>0.9 ){
2441         //delete arr->RemoveAt(i);
2442       }
2443       continue;
2444     }
2445     */
2446     Bool_t isok =kFALSE;
2447     if ( (pt->fNShared/pt->GetNumberOfClusters()<0.5) &&pt->GetNumberOfClusters()>60)
2448       isok = kTRUE;
2449     if ((TMath::Abs(1/pt->GetC())<100.) && (pt->fNShared/pt->GetNumberOfClusters()<0.7))
2450       isok =kTRUE;
2451     if  (TMath::Abs(pt->GetZ()/pt->GetX())>1.1)
2452       isok =kTRUE;
2453     if ( (TMath::Abs(pt->GetSnp()>0.7) && pt->GetD(0,0)>60.))
2454       isok =kTRUE;
2455     
2456     if (isok)     
2457       for (Int_t i=0; i<160; i++) {     
2458         Int_t index=pt->GetClusterIndex2(i);
2459         if (index<0) continue;
2460         AliTPCclusterMI *c= pt->fClusterPointer[i];
2461         if (!c) continue;
2462         //if (!(c->IsUsed(10))) c->Use();  
2463         c->Use(10);  
2464       }
2465   }
2466   
2467   
2468   //
2469   Double_t maxchi  = meanchi+2.*smeanchi;
2470
2471   for (Int_t i=0; i<nseed; i++) {
2472     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2473     if (!pt) {
2474       continue;
2475     }    
2476     //if (!(pt->IsActive())) continue;
2477     if (pt->fBSigned) continue;
2478     Double_t chi     = pt->GetChi2()/pt->GetNumberOfClusters();
2479     if (chi>maxchi) continue;
2480
2481     Float_t bfactor=1;
2482     Float_t dens = pt->GetNumberOfClusters()/Float_t(pt->fNFoundable);
2483    
2484     //sign only tracks with enoug big density at the beginning
2485     
2486     if ((pt->GetDensityFirst(40)<0.75) && pt->GetNumberOfClusters()<meann) continue; 
2487     
2488     
2489     Double_t mindens = TMath::Max(double(mdensity-sdensity*fdensity*bfactor),0.65);
2490     Double_t minn    = TMath::Max(Int_t(meann-fnumber*smeann*bfactor),50);
2491    
2492     //    if (pt->fBConstrain) mindens = TMath::Max(mdensity-sdensity*fdensity*bfactor,0.65);
2493     if ( (pt->fRemoval==10) && (pt->GetSnp()>0.8)&&(dens>mindens))
2494       minn=0;
2495
2496     if ((dens>mindens && pt->GetNumberOfClusters()>minn) && chi<maxchi ){
2497       //Int_t noc=pt->GetNumberOfClusters();
2498       pt->fBSigned = kTRUE;
2499       for (Int_t i=0; i<160; i++) {
2500
2501         Int_t index=pt->GetClusterIndex2(i);
2502         if (index<0) continue;
2503         AliTPCclusterMI *c= pt->fClusterPointer[i];
2504         if (!c) continue;
2505         //      if (!(c->IsUsed(10))) c->Use();  
2506         c->Use(10);  
2507       }
2508     }
2509   }
2510   //  gLastCheck = nseed;
2511   //  arr->Compress();
2512   if (fDebug>0){
2513     timer.Print();
2514   }
2515 }
2516
2517
2518 void  AliTPCtrackerMI::StopNotActive(TObjArray * arr, Int_t row0, Float_t th0, Float_t th1, Float_t th2) const
2519 {
2520   // stop not active tracks
2521   // take th1 as threshold for number of founded to number of foundable on last 10 active rows
2522   // take th2 as threshold for number of founded to number of foundable on last 20 active rows 
2523   Int_t nseed = arr->GetEntriesFast();  
2524   //
2525   for (Int_t i=0; i<nseed; i++) {
2526     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2527     if (!pt) {
2528       continue;
2529     }
2530     if (!(pt->IsActive())) continue;
2531     StopNotActive(pt,row0,th0, th1,th2);
2532   }
2533 }
2534
2535
2536
2537 void  AliTPCtrackerMI::StopNotActive(AliTPCseed * seed, Int_t row0, Float_t th0, Float_t th1,
2538  Float_t th2) const
2539 {
2540   // stop not active tracks
2541   // take th1 as threshold for number of founded to number of foundable on last 10 active rows
2542   // take th2 as threshold for number of founded to number of foundable on last 20 active rows 
2543   Int_t sumgood1  = 0;
2544   Int_t sumgood2  = 0;
2545   Int_t foundable = 0;
2546   Int_t maxindex = seed->fLastPoint;  //last foundable row
2547   if (seed->fNFoundable*th0 > seed->GetNumberOfClusters()) {
2548     seed->Desactivate(10) ;
2549     return;
2550   }
2551
2552   for (Int_t i=row0; i<maxindex; i++){
2553     Int_t index = seed->GetClusterIndex2(i);
2554     if (index!=-1) foundable++;
2555     //if (!c) continue;
2556     if (foundable<=30) sumgood1++;
2557     if (foundable<=50) {
2558       sumgood2++;
2559     }
2560     else{ 
2561       break;
2562     }        
2563   }
2564   if (foundable>=30.){ 
2565      if (sumgood1<(th1*30.)) seed->Desactivate(10);
2566   }
2567   if (foundable>=50)
2568     if (sumgood2<(th2*50.)) seed->Desactivate(10);
2569 }
2570
2571
2572 Int_t AliTPCtrackerMI::RefitInward(AliESD *event)
2573 {
2574   //
2575   // back propagation of ESD tracks
2576   //
2577   //return 0;
2578   fEvent = event;
2579   ReadSeeds(event,2);
2580   fIteration=2;
2581   //PrepareForProlongation(fSeeds,1);
2582   PropagateForward2(fSeeds);
2583
2584   Int_t ntracks=0;
2585   Int_t nseed = fSeeds->GetEntriesFast();
2586   for (Int_t i=0;i<nseed;i++){
2587     AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
2588     if (!seed) continue;
2589     if (seed->GetKinkIndex(0)>0)  UpdateKinkQualityD(seed);  // update quality informations for kinks
2590
2591     seed->PropagateTo(fParam->GetInnerRadiusLow());
2592     seed->UpdatePoints();
2593     AliESDtrack *esd=event->GetTrack(i);
2594     seed->CookdEdx(0.02,0.6);
2595     CookLabel(seed,0.1); //For comparison only
2596     //
2597     if (AliTPCReconstructor::StreamLevel()>0 && seed!=0&&esd!=0) {
2598       TTreeSRedirector &cstream = *fDebugStreamer;
2599       cstream<<"Crefit"<<
2600         "Esd.="<<esd<<
2601         "Track.="<<seed<<
2602         "\n"; 
2603     }
2604     if (seed->GetNumberOfClusters()>15){
2605       esd->UpdateTrackParams(seed,AliESDtrack::kTPCrefit); 
2606       esd->SetTPCPoints(seed->GetPoints());
2607       esd->SetTPCPointsF(seed->fNFoundable);
2608       Int_t ndedx   = seed->fNCDEDX[0]+seed->fNCDEDX[1]+seed->fNCDEDX[2]+seed->fNCDEDX[3];
2609       Float_t sdedx = (seed->fSDEDX[0]+seed->fSDEDX[1]+seed->fSDEDX[2]+seed->fSDEDX[3])*0.25;
2610       Float_t dedx  = seed->GetdEdx();
2611       esd->SetTPCsignal(dedx, sdedx, ndedx);
2612       //
2613       // add seed to the esd track in Calib level
2614       //
2615       if (AliTPCReconstructor::StreamLevel()>0){
2616         AliTPCseed * seedCopy = new AliTPCseed(*seed, kTRUE); 
2617         esd->AddCalibObject(seedCopy);
2618       }
2619       ntracks++;
2620     }
2621     else{
2622       //printf("problem\n");
2623     }
2624   }
2625   //FindKinks(fSeeds,event);
2626   Info("RefitInward","Number of refitted tracks %d",ntracks);
2627   fEvent =0;
2628   //WriteTracks();
2629   return 0;
2630 }
2631
2632
2633 Int_t AliTPCtrackerMI::PropagateBack(AliESD *event)
2634 {
2635   //
2636   // back propagation of ESD tracks
2637   //
2638
2639   fEvent = event;
2640   fIteration = 1;
2641   ReadSeeds(event,1);
2642   PropagateBack(fSeeds); 
2643   RemoveUsed2(fSeeds,0.4,0.4,20);
2644   //
2645   Int_t nseed = fSeeds->GetEntriesFast();
2646   Int_t ntracks=0;
2647   for (Int_t i=0;i<nseed;i++){
2648     AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
2649     if (!seed) continue;
2650     if (seed->GetKinkIndex(0)<0)  UpdateKinkQualityM(seed);  // update quality informations for kinks
2651     seed->UpdatePoints();
2652     AliESDtrack *esd=event->GetTrack(i);
2653     seed->CookdEdx(0.02,0.6);
2654     CookLabel(seed,0.1); //For comparison only
2655     if (seed->GetNumberOfClusters()>15){
2656       esd->UpdateTrackParams(seed,AliESDtrack::kTPCout);
2657       esd->SetTPCPoints(seed->GetPoints());
2658       esd->SetTPCPointsF(seed->fNFoundable);
2659       Int_t ndedx   = seed->fNCDEDX[0]+seed->fNCDEDX[1]+seed->fNCDEDX[2]+seed->fNCDEDX[3];
2660       Float_t sdedx = (seed->fSDEDX[0]+seed->fSDEDX[1]+seed->fSDEDX[2]+seed->fSDEDX[3])*0.25;
2661       Float_t dedx  = seed->GetdEdx();
2662       esd->SetTPCsignal(dedx, sdedx, ndedx);
2663       ntracks++;
2664       Int_t eventnumber = event->GetEventNumber();// patch 28 fev 06
2665       (*fDebugStreamer)<<"Cback"<<
2666         "Tr0.="<<seed<<
2667         "EventNr="<<eventnumber<<
2668         "\n"; // patch 28 fev 06   
2669     }
2670   }
2671   //FindKinks(fSeeds,event);
2672   Info("PropagateBack","Number of back propagated tracks %d",ntracks);
2673   fEvent =0;
2674   //WriteTracks();
2675
2676   return 0;
2677 }
2678
2679
2680 void AliTPCtrackerMI::DeleteSeeds()
2681 {
2682   //
2683   //delete Seeds
2684
2685   Int_t nseed = fSeeds->GetEntriesFast();
2686   for (Int_t i=0;i<nseed;i++){
2687     AliTPCseed * seed = (AliTPCseed*)fSeeds->At(i);
2688     if (seed) delete fSeeds->RemoveAt(i);
2689   }
2690   delete fSeeds;
2691
2692   fSeeds =0;
2693 }
2694
2695 void AliTPCtrackerMI::ReadSeeds(AliESD *event, Int_t direction)
2696 {
2697   //
2698   //read seeds from the event
2699   
2700   Int_t nentr=event->GetNumberOfTracks();
2701   if (fDebug>0){
2702     Info("ReadSeeds", "Number of ESD tracks: %d\n", nentr);
2703   }
2704   if (fSeeds) 
2705     DeleteSeeds();
2706   if (!fSeeds){   
2707     fSeeds = new TObjArray(nentr);
2708   }
2709   UnsignClusters();
2710   //  Int_t ntrk=0;  
2711   for (Int_t i=0; i<nentr; i++) {
2712     AliESDtrack *esd=event->GetTrack(i);
2713     ULong_t status=esd->GetStatus();
2714     if (!(status&AliESDtrack::kTPCin)) continue;
2715     AliTPCtrack t(*esd);
2716     t.SetNumberOfClusters(0);
2717     //    AliTPCseed *seed = new AliTPCseed(t,t.GetAlpha());
2718     AliTPCseed *seed = new AliTPCseed(t/*,t.GetAlpha()*/);
2719     for (Int_t ikink=0;ikink<3;ikink++) {
2720       Int_t index = esd->GetKinkIndex(ikink);
2721       seed->GetKinkIndexes()[ikink] = index;
2722       if (index==0) continue;
2723       index = TMath::Abs(index);
2724       AliESDkink * kink = fEvent->GetKink(index-1);
2725       if (kink&&esd->GetKinkIndex(ikink)<0){
2726         if ((status & AliESDtrack::kTRDrefit) != 0) kink->SetStatus(1,2);
2727         if ((status & AliESDtrack::kITSout) != 0)   kink->SetStatus(1,0);
2728       }
2729       if (kink&&esd->GetKinkIndex(ikink)>0){
2730         if ((status & AliESDtrack::kTRDrefit) != 0) kink->SetStatus(1,6);
2731         if ((status & AliESDtrack::kITSout) != 0)   kink->SetStatus(1,4);
2732       }
2733
2734     }
2735     if (((status&AliESDtrack::kITSout)==0)&&(direction==1)) seed->ResetCovariance(10.); 
2736     if ( direction ==2 &&(status & AliESDtrack::kTRDrefit) == 0 ) seed->ResetCovariance(10.);
2737     if ( direction ==2 && ((status & AliESDtrack::kTPCout) == 0) ) {
2738       fSeeds->AddAt(0,i);
2739       delete seed;
2740       continue;    
2741     }
2742     if ( direction ==2 &&(status & AliESDtrack::kTRDrefit) > 0 )  {
2743       Double_t par0[5],par1[5],alpha,x;
2744       esd->GetInnerExternalParameters(alpha,x,par0);
2745       esd->GetExternalParameters(x,par1);
2746       Double_t delta1 = TMath::Abs(par0[4]-par1[4])/(0.000000001+TMath::Abs(par0[4]+par1[4]));
2747       Double_t delta2 = TMath::Abs(par0[3]-par1[3]);
2748       Double_t trdchi2=0;
2749       if (esd->GetTRDncls()>0) trdchi2 = esd->GetTRDchi2()/esd->GetTRDncls();
2750       //reset covariance if suspicious 
2751       if ( (delta1>0.1) || (delta2>0.006) ||trdchi2>7.)
2752         seed->ResetCovariance(10.);
2753     }
2754
2755     //
2756     //
2757     // rotate to the local coordinate system
2758     //   
2759     fSectors=fInnerSec; fN=fkNIS;    
2760     Double_t alpha=seed->GetAlpha() - fSectors->GetAlphaShift();
2761     if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
2762     if (alpha < 0.            ) alpha += 2.*TMath::Pi();
2763     Int_t ns=Int_t(alpha/fSectors->GetAlpha())%fN;
2764     alpha =ns*fSectors->GetAlpha() + fSectors->GetAlphaShift();
2765     if (alpha<-TMath::Pi()) alpha += 2*TMath::Pi();
2766     if (alpha>=TMath::Pi()) alpha -= 2*TMath::Pi();
2767     alpha-=seed->GetAlpha();  
2768     if (!seed->Rotate(alpha)) {
2769       delete seed;
2770       continue;
2771     }
2772     seed->fEsd = esd;
2773     // sign clusters
2774     if (esd->GetKinkIndex(0)<=0){
2775       for (Int_t irow=0;irow<160;irow++){
2776         Int_t index = seed->GetClusterIndex2(irow);    
2777         if (index>0){ 
2778           //
2779           AliTPCclusterMI * cl = GetClusterMI(index);
2780           seed->fClusterPointer[irow] = cl;
2781           if (cl){
2782             if ((index & 0x8000)==0){
2783               cl->Use(10);  // accepted cluster   
2784             }else{
2785               cl->Use(6);   // close cluster not accepted
2786             }   
2787           }else{
2788             Info("ReadSeeds","Not found cluster");
2789           }
2790         }
2791       }
2792     }
2793     fSeeds->AddAt(seed,i);
2794   }
2795 }
2796
2797
2798
2799 //_____________________________________________________________________________
2800 void AliTPCtrackerMI::MakeSeeds3(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,  Float_t cuts[4],
2801                                  Float_t deltay, Int_t ddsec) {
2802   //-----------------------------------------------------------------
2803   // This function creates track seeds.
2804   // SEEDING WITH VERTEX CONSTRAIN 
2805   //-----------------------------------------------------------------
2806   // cuts[0]   - fP4 cut
2807   // cuts[1]   - tan(phi)  cut
2808   // cuts[2]   - zvertex cut
2809   // cuts[3]   - fP3 cut
2810   Int_t nin0  = 0;
2811   Int_t nin1  = 0;
2812   Int_t nin2  = 0;
2813   Int_t nin   = 0;
2814   Int_t nout1 = 0;
2815   Int_t nout2 = 0;
2816
2817   Double_t x[5], c[15];
2818   //  Int_t di = i1-i2;
2819   //
2820   AliTPCseed * seed = new AliTPCseed;
2821   Double_t alpha=fSectors->GetAlpha(), shift=fSectors->GetAlphaShift();
2822   Double_t cs=cos(alpha), sn=sin(alpha);
2823   //
2824   //  Double_t x1 =fOuterSec->GetX(i1);
2825   //Double_t xx2=fOuterSec->GetX(i2);
2826   
2827   Double_t x1 =GetXrow(i1);
2828   Double_t xx2=GetXrow(i2);
2829
2830   Double_t x3=GetX(), y3=GetY(), z3=GetZ();
2831
2832   Int_t imiddle = (i2+i1)/2;    //middle pad row index
2833   Double_t xm = GetXrow(imiddle); // radius of middle pad-row
2834   const AliTPCRow& krm=GetRow(sec,imiddle); //middle pad -row
2835   //
2836   Int_t ns =sec;   
2837
2838   const AliTPCRow& kr1=GetRow(ns,i1);
2839   Double_t ymax  = GetMaxY(i1)-kr1.fDeadZone-1.5;  
2840   Double_t ymaxm = GetMaxY(imiddle)-kr1.fDeadZone-1.5;  
2841
2842   //
2843   // change cut on curvature if it can't reach this layer
2844   // maximal curvature set to reach it
2845   Double_t dvertexmax  = TMath::Sqrt((x1-x3)*(x1-x3)+(ymax+5-y3)*(ymax+5-y3));
2846   if (dvertexmax*0.5*cuts[0]>0.85){
2847     cuts[0] = 0.85/(dvertexmax*0.5+1.);
2848   }
2849   Double_t r2min = 1/(cuts[0]*cuts[0]);  //minimal square of radius given by cut
2850
2851   //  Int_t ddsec = 1;
2852   if (deltay>0) ddsec = 0; 
2853   // loop over clusters  
2854   for (Int_t is=0; is < kr1; is++) {
2855     //
2856     if (kr1[is]->IsUsed(10)) continue;
2857     Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();    
2858     //if (TMath::Abs(y1)>ymax) continue;
2859
2860     if (deltay>0 && TMath::Abs(ymax-TMath::Abs(y1))> deltay ) continue;  // seed only at the edge
2861
2862     // find possible directions    
2863     Float_t anglez = (z1-z3)/(x1-x3); 
2864     Float_t extraz = z1 - anglez*(x1-xx2);  // extrapolated z      
2865     //
2866     //
2867     //find   rotation angles relative to line given by vertex and point 1
2868     Double_t dvertex2 = (x1-x3)*(x1-x3)+(y1-y3)*(y1-y3);
2869     Double_t dvertex  = TMath::Sqrt(dvertex2);
2870     Double_t angle13  = TMath::ATan((y1-y3)/(x1-x3));
2871     Double_t cs13     = cos(-angle13), sn13 = sin(-angle13);            
2872     
2873     //
2874     // loop over 2 sectors
2875     Int_t dsec1=-ddsec;
2876     Int_t dsec2= ddsec;
2877     if (y1<0)  dsec2= 0;
2878     if (y1>0)  dsec1= 0;
2879     
2880     Double_t dddz1=0;  // direction of delta inclination in z axis
2881     Double_t dddz2=0;
2882     if ( (z1-z3)>0)
2883       dddz1 =1;    
2884     else
2885       dddz2 =1;
2886     //
2887     for (Int_t dsec = dsec1; dsec<=dsec2;dsec++){
2888       Int_t sec2 = sec + dsec;
2889       // 
2890       //      AliTPCRow&  kr2  = fOuterSec[(sec2+fkNOS)%fkNOS][i2];
2891       //AliTPCRow&  kr2m = fOuterSec[(sec2+fkNOS)%fkNOS][imiddle];
2892       AliTPCRow&  kr2  = GetRow((sec2+fkNOS)%fkNOS,i2);
2893       AliTPCRow&  kr2m = GetRow((sec2+fkNOS)%fkNOS,imiddle);
2894       Int_t  index1 = TMath::Max(kr2.Find(extraz-0.6-dddz1*TMath::Abs(z1)*0.05)-1,0);
2895       Int_t  index2 = TMath::Min(kr2.Find(extraz+0.6+dddz2*TMath::Abs(z1)*0.05)+1,kr2);
2896
2897       // rotation angles to p1-p3
2898       Double_t cs13r     = cos(-angle13+dsec*alpha)/dvertex, sn13r = sin(-angle13+dsec*alpha)/dvertex;            
2899       Double_t x2,   y2,   z2; 
2900       //
2901       //      Double_t dymax = maxangle*TMath::Abs(x1-xx2);
2902
2903       //
2904       Double_t dxx0 =  (xx2-x3)*cs13r;
2905       Double_t dyy0 =  (xx2-x3)*sn13r;
2906       for (Int_t js=index1; js < index2; js++) {
2907         const AliTPCclusterMI *kcl = kr2[js];
2908         if (kcl->IsUsed(10)) continue;  
2909         //
2910         //calcutate parameters
2911         //      
2912         Double_t yy0 =  dyy0 +(kcl->GetY()-y3)*cs13r;
2913         // stright track
2914         if (TMath::Abs(yy0)<0.000001) continue;
2915         Double_t xx0 =  dxx0 -(kcl->GetY()-y3)*sn13r;
2916         Double_t y0  =  0.5*(xx0*xx0+yy0*yy0-xx0)/yy0;
2917         Double_t r02 = (0.25+y0*y0)*dvertex2;   
2918         //curvature (radius) cut
2919         if (r02<r2min) continue;                
2920        
2921         nin0++; 
2922         //
2923         Double_t c0  = 1/TMath::Sqrt(r02);
2924         if (yy0>0) c0*=-1.;     
2925                
2926        
2927         //Double_t dfi0   = 2.*TMath::ASin(dvertex*c0*0.5);
2928         //Double_t dfi1   = 2.*TMath::ASin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);
2929         Double_t dfi0   = 2.*AliTPCFastMath::FastAsin(dvertex*c0*0.5);
2930         Double_t dfi1   = 2.*AliTPCFastMath::FastAsin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);  
2931         //
2932         //
2933         Double_t z0  =  kcl->GetZ();  
2934         Double_t zzzz2    = z1-(z1-z3)*dfi1/dfi0;
2935         if (TMath::Abs(zzzz2-z0)>0.5) continue;       
2936         nin1++;              
2937         //      
2938         Double_t dip    = (z1-z0)*c0/dfi1;        
2939         Double_t x0 = (0.5*cs13+y0*sn13)*dvertex*c0;
2940         //
2941         y2 = kcl->GetY(); 
2942         if (dsec==0){
2943           x2 = xx2; 
2944           z2 = kcl->GetZ();       
2945         }
2946         else
2947           {
2948             // rotation 
2949             z2 = kcl->GetZ();  
2950             x2= xx2*cs-y2*sn*dsec;
2951             y2=+xx2*sn*dsec+y2*cs;
2952           }
2953         
2954         x[0] = y1;
2955         x[1] = z1;
2956         x[2] = x0;
2957         x[3] = dip;
2958         x[4] = c0;
2959         //
2960         //
2961         // do we have cluster at the middle ?
2962         Double_t ym,zm;
2963         GetProlongation(x1,xm,x,ym,zm);
2964         UInt_t dummy; 
2965         AliTPCclusterMI * cm=0;
2966         if (TMath::Abs(ym)-ymaxm<0){      
2967           cm = krm.FindNearest2(ym,zm,1.0,0.6,dummy);
2968           if ((!cm) || (cm->IsUsed(10))) {        
2969             continue;
2970           }
2971         }
2972         else{     
2973           // rotate y1 to system 0
2974           // get state vector in rotated system 
2975           Double_t yr1  = (-0.5*sn13+y0*cs13)*dvertex*c0;
2976           Double_t xr2  =  x0*cs+yr1*sn*dsec;
2977           Double_t xr[5]={kcl->GetY(),kcl->GetZ(), xr2, dip, c0};
2978           //
2979           GetProlongation(xx2,xm,xr,ym,zm);
2980           if (TMath::Abs(ym)-ymaxm<0){
2981             cm = kr2m.FindNearest2(ym,zm,1.0,0.6,dummy);
2982             if ((!cm) || (cm->IsUsed(10))) {      
2983               continue;
2984             }
2985           }
2986         }
2987        
2988
2989         Double_t dym = 0;
2990         Double_t dzm = 0;
2991         if (cm){
2992           dym = ym - cm->GetY();
2993           dzm = zm - cm->GetZ();
2994         }
2995         nin2++;
2996
2997
2998         //
2999         //
3000         Double_t sy1=kr1[is]->GetSigmaY2()*2., sz1=kr1[is]->GetSigmaZ2()*2.;
3001         Double_t sy2=kcl->GetSigmaY2()*2.,     sz2=kcl->GetSigmaZ2()*2.;
3002         //Double_t sy3=400*3./12., sy=0.1, sz=0.1;
3003         Double_t sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
3004         //Double_t sy3=25000*x[4]*x[4]*60+0.5, sy=0.1, sz=0.1;
3005
3006         Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
3007         Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
3008         Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
3009         Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
3010         Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
3011         Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
3012         
3013         Double_t f30=(F3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
3014         Double_t f31=(F3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
3015         Double_t f32=(F3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
3016         Double_t f34=(F3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
3017         
3018         c[0]=sy1;
3019         c[1]=0.;       c[2]=sz1;
3020         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3021         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3022                        c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3023         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3024         c[13]=f30*sy1*f40+f32*sy2*f42;
3025         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3026         
3027         //      if (!BuildSeed(kr1[is],kcl,0,x1,x2,x3,x,c)) continue;
3028         
3029         UInt_t index=kr1.GetIndex(is);
3030         AliTPCseed *track=new(seed) AliTPCseed(x1, ns*alpha+shift, x, c, index);
3031         
3032         track->fIsSeeding = kTRUE;
3033         track->fSeed1 = i1;
3034         track->fSeed2 = i2;
3035         track->fSeedType=3;
3036
3037        
3038         //if (dsec==0) {
3039           FollowProlongation(*track, (i1+i2)/2,1);
3040           Int_t foundable,found,shared;
3041           track->GetClusterStatistic((i1+i2)/2,i1, found, foundable, shared, kTRUE);
3042           if ((found<0.55*foundable)  || shared>0.5*found || (track->GetSigmaY2()+track->GetSigmaZ2())>0.5){
3043             seed->Reset();
3044             seed->~AliTPCseed();
3045             continue;
3046           }
3047           //}
3048         
3049         nin++;
3050         FollowProlongation(*track, i2,1);
3051         
3052         
3053         //Int_t rc = 1;
3054         track->fBConstrain =1;
3055         //      track->fLastPoint = i1+fInnerSec->GetNRows();  // first cluster in track position
3056         track->fLastPoint = i1;  // first cluster in track position
3057         track->fFirstPoint = track->fLastPoint;
3058         
3059         if (track->GetNumberOfClusters()<(i1-i2)*0.5 || 
3060             track->GetNumberOfClusters() < track->fNFoundable*0.6 || 
3061             track->fNShared>0.4*track->GetNumberOfClusters() ) {
3062           seed->Reset();
3063           seed->~AliTPCseed();
3064           continue;
3065         }
3066         nout1++;
3067         // Z VERTEX CONDITION
3068         Double_t zv;
3069         zv = track->GetZ()+track->GetTgl()/track->GetC()*
3070           ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
3071         if (TMath::Abs(zv-z3)>cuts[2]) {
3072           FollowProlongation(*track, TMath::Max(i2-20,0));
3073           zv = track->GetZ()+track->GetTgl()/track->GetC()*
3074             ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
3075           if (TMath::Abs(zv-z3)>cuts[2]){
3076             FollowProlongation(*track, TMath::Max(i2-40,0));
3077             zv = track->GetZ()+track->GetTgl()/track->GetC()*
3078               ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
3079             if (TMath::Abs(zv-z3)>cuts[2] &&(track->GetNumberOfClusters() > track->fNFoundable*0.7)){
3080               // make seed without constrain
3081               AliTPCseed * track2 = MakeSeed(track,0.2,0.5,1.);
3082               FollowProlongation(*track2, i2,1);
3083               track2->fBConstrain = kFALSE;
3084               track2->fSeedType = 1;
3085               arr->AddLast(track2); 
3086               seed->Reset();
3087               seed->~AliTPCseed();
3088               continue;         
3089             }
3090             else{
3091               seed->Reset();
3092               seed->~AliTPCseed();
3093               continue;
3094             
3095             }
3096           }
3097         }
3098         
3099         track->fSeedType =0;
3100         arr->AddLast(track); 
3101         seed = new AliTPCseed;  
3102         nout2++;
3103         // don't consider other combinations
3104         if (track->GetNumberOfClusters() > track->fNFoundable*0.8)
3105           break;
3106       }
3107     }
3108   }
3109   if (fDebug>3){
3110     Info("MakeSeeds3","\nSeeding statistic:\t%d\t%d\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin,nout1,nout2);
3111   }
3112   delete seed;
3113 }
3114
3115
3116 void AliTPCtrackerMI::MakeSeeds5(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,  Float_t cuts[4],
3117                                  Float_t deltay) {
3118   
3119
3120
3121   //-----------------------------------------------------------------
3122   // This function creates track seeds.
3123   //-----------------------------------------------------------------
3124   // cuts[0]   - fP4 cut
3125   // cuts[1]   - tan(phi)  cut
3126   // cuts[2]   - zvertex cut
3127   // cuts[3]   - fP3 cut
3128
3129
3130   Int_t nin0  = 0;
3131   Int_t nin1  = 0;
3132   Int_t nin2  = 0;
3133   Int_t nin   = 0;
3134   Int_t nout1 = 0;
3135   Int_t nout2 = 0;
3136   Int_t nout3 =0;
3137   Double_t x[5], c[15];
3138   //
3139   // make temporary seed
3140   AliTPCseed * seed = new AliTPCseed;
3141   Double_t alpha=fOuterSec->GetAlpha(), shift=fOuterSec->GetAlphaShift();
3142   //  Double_t cs=cos(alpha), sn=sin(alpha);
3143   //
3144   //
3145
3146   // first 3 padrows
3147   Double_t x1 = GetXrow(i1-1);
3148   const    AliTPCRow& kr1=GetRow(sec,i1-1);
3149   Double_t y1max  = GetMaxY(i1-1)-kr1.fDeadZone-1.5;  
3150   //
3151   Double_t x1p = GetXrow(i1);
3152   const    AliTPCRow& kr1p=GetRow(sec,i1);
3153   //
3154   Double_t x1m = GetXrow(i1-2);
3155   const    AliTPCRow& kr1m=GetRow(sec,i1-2);
3156
3157   //
3158   //last 3 padrow for seeding
3159   AliTPCRow&  kr3  = GetRow((sec+fkNOS)%fkNOS,i1-7);
3160   Double_t    x3   =  GetXrow(i1-7);
3161   //  Double_t    y3max= GetMaxY(i1-7)-kr3.fDeadZone-1.5;  
3162   //
3163   AliTPCRow&  kr3p  = GetRow((sec+fkNOS)%fkNOS,i1-6);
3164   Double_t    x3p   = GetXrow(i1-6);
3165   //
3166   AliTPCRow&  kr3m  = GetRow((sec+fkNOS)%fkNOS,i1-8);
3167   Double_t    x3m   = GetXrow(i1-8);
3168
3169   //
3170   //
3171   // middle padrow
3172   Int_t im = i1-4;                           //middle pad row index
3173   Double_t xm         = GetXrow(im);         // radius of middle pad-row
3174   const AliTPCRow& krm=GetRow(sec,im);   //middle pad -row
3175   //  Double_t ymmax = GetMaxY(im)-kr1.fDeadZone-1.5;  
3176   //
3177   //
3178   Double_t deltax  = x1-x3;
3179   Double_t dymax   = deltax*cuts[1];
3180   Double_t dzmax   = deltax*cuts[3];
3181   //
3182   // loop over clusters  
3183   for (Int_t is=0; is < kr1; is++) {
3184     //
3185     if (kr1[is]->IsUsed(10)) continue;
3186     Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();    
3187     //
3188     if (deltay>0 && TMath::Abs(y1max-TMath::Abs(y1))> deltay ) continue;  // seed only at the edge    
3189     // 
3190     Int_t  index1 = TMath::Max(kr3.Find(z1-dzmax)-1,0);
3191     Int_t  index2 = TMath::Min(kr3.Find(z1+dzmax)+1,kr3);
3192     //    
3193     Double_t y3,   z3;
3194     //
3195     //
3196     UInt_t index;
3197     for (Int_t js=index1; js < index2; js++) {
3198       const AliTPCclusterMI *kcl = kr3[js];
3199       if (kcl->IsUsed(10)) continue;
3200       y3 = kcl->GetY(); 
3201       // apply angular cuts
3202       if (TMath::Abs(y1-y3)>dymax) continue;
3203       x3 = x3; 
3204       z3 = kcl->GetZ(); 
3205       if (TMath::Abs(z1-z3)>dzmax) continue;
3206       //
3207       Double_t angley = (y1-y3)/(x1-x3);
3208       Double_t anglez = (z1-z3)/(x1-x3);
3209       //
3210       Double_t erry = TMath::Abs(angley)*(x1-x1m)*0.5+0.5;
3211       Double_t errz = TMath::Abs(anglez)*(x1-x1m)*0.5+0.5;
3212       //
3213       Double_t yyym = angley*(xm-x1)+y1;
3214       Double_t zzzm = anglez*(xm-x1)+z1;
3215
3216       const AliTPCclusterMI *kcm = krm.FindNearest2(yyym,zzzm,erry,errz,index);
3217       if (!kcm) continue;
3218       if (kcm->IsUsed(10)) continue;
3219       
3220       erry = TMath::Abs(angley)*(x1-x1m)*0.4+0.5;
3221       errz = TMath::Abs(anglez)*(x1-x1m)*0.4+0.5;
3222       //
3223       //
3224       //
3225       Int_t used  =0;
3226       Int_t found =0;
3227       //
3228       // look around first
3229       const AliTPCclusterMI *kc1m = kr1m.FindNearest2(angley*(x1m-x1)+y1,
3230                                                       anglez*(x1m-x1)+z1,
3231                                                       erry,errz,index);
3232       //
3233       if (kc1m){
3234         found++;
3235         if (kc1m->IsUsed(10)) used++;
3236       }
3237       const AliTPCclusterMI *kc1p = kr1p.FindNearest2(angley*(x1p-x1)+y1,
3238                                                       anglez*(x1p-x1)+z1,
3239                                                       erry,errz,index);
3240       //
3241       if (kc1p){
3242         found++;
3243         if (kc1p->IsUsed(10)) used++;
3244       }
3245       if (used>1)  continue;
3246       if (found<1) continue; 
3247
3248       //
3249       // look around last
3250       const AliTPCclusterMI *kc3m = kr3m.FindNearest2(angley*(x3m-x3)+y3,
3251                                                       anglez*(x3m-x3)+z3,
3252                                                       erry,errz,index);
3253       //
3254       if (kc3m){
3255         found++;
3256         if (kc3m->IsUsed(10)) used++;
3257       }
3258       else 
3259         continue;
3260       const AliTPCclusterMI *kc3p = kr3p.FindNearest2(angley*(x3p-x3)+y3,
3261                                                       anglez*(x3p-x3)+z3,
3262                                                       erry,errz,index);
3263       //
3264       if (kc3p){
3265         found++;
3266         if (kc3p->IsUsed(10)) used++;
3267       }
3268       else 
3269         continue;
3270       if (used>1)  continue;
3271       if (found<3) continue;       
3272       //
3273       Double_t x2,y2,z2;
3274       x2 = xm;
3275       y2 = kcm->GetY();
3276       z2 = kcm->GetZ();
3277       //
3278                         
3279       x[0]=y1;
3280       x[1]=z1;
3281       x[4]=F1(x1,y1,x2,y2,x3,y3);
3282       //if (TMath::Abs(x[4]) >= cuts[0]) continue;
3283       nin0++;
3284       //
3285       x[2]=F2(x1,y1,x2,y2,x3,y3);
3286       nin1++;
3287       //
3288       x[3]=F3n(x1,y1,x2,y2,z1,z2,x[4]);
3289       //if (TMath::Abs(x[3]) > cuts[3]) continue;
3290       nin2++;
3291       //
3292       //
3293       Double_t sy1=0.1,  sz1=0.1;
3294       Double_t sy2=0.1,  sz2=0.1;
3295       Double_t sy3=0.1,  sy=0.1, sz=0.1;
3296       
3297       Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
3298       Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
3299       Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
3300       Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
3301       Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
3302       Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
3303       
3304       Double_t f30=(F3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
3305       Double_t f31=(F3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
3306       Double_t f32=(F3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
3307       Double_t f34=(F3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
3308       
3309       c[0]=sy1;
3310       c[1]=0.;       c[2]=sz1; 
3311       c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3312       c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3313       c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3314       c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3315       c[13]=f30*sy1*f40+f32*sy2*f42;
3316       c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3317       
3318       //        if (!BuildSeed(kr1[is],kcl,0,x1,x2,x3,x,c)) continue;
3319       
3320       UInt_t index=kr1.GetIndex(is);
3321       AliTPCseed *track=new(seed) AliTPCseed(x1, sec*alpha+shift, x, c, index);
3322       
3323       track->fIsSeeding = kTRUE;
3324
3325       nin++;      
3326       FollowProlongation(*track, i1-7,1);
3327       if (track->GetNumberOfClusters() < track->fNFoundable*0.75 || 
3328           track->fNShared>0.6*track->GetNumberOfClusters() || ( track->GetSigmaY2()+ track->GetSigmaZ2())>0.6){
3329         seed->Reset();
3330         seed->~AliTPCseed();
3331         continue;
3332       }
3333       nout1++;
3334       nout2++;  
3335       //Int_t rc = 1;
3336       FollowProlongation(*track, i2,1);
3337       track->fBConstrain =0;
3338       track->fLastPoint = i1+fInnerSec->GetNRows();  // first cluster in track position
3339       track->fFirstPoint = track->fLastPoint;
3340       
3341       if (track->GetNumberOfClusters()<(i1-i2)*0.5 || 
3342           track->GetNumberOfClusters()<track->fNFoundable*0.7 || 
3343           track->fNShared>2. || track->GetChi2()/track->GetNumberOfClusters()>6 || ( track->GetSigmaY2()+ track->GetSigmaZ2())>0.5 ) {
3344         seed->Reset();
3345         seed->~AliTPCseed();
3346         continue;
3347       }
3348    
3349       {
3350         FollowProlongation(*track, TMath::Max(i2-10,0),1);
3351         AliTPCseed * track2 = MakeSeed(track,0.2,0.5,0.9);
3352         FollowProlongation(*track2, i2,1);
3353         track2->fBConstrain = kFALSE;
3354         track2->fSeedType = 4;
3355         arr->AddLast(track2); 
3356         seed->Reset();
3357         seed->~AliTPCseed();
3358       }
3359       
3360    
3361       //arr->AddLast(track); 
3362       //seed = new AliTPCseed;  
3363       nout3++;
3364     }
3365   }
3366   
3367   if (fDebug>3){
3368     Info("MakeSeeds5","\nSeeding statiistic:\t%d\t%d\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin,nout1,nout2,nout3);
3369   }
3370   delete seed;
3371 }
3372
3373
3374 //_____________________________________________________________________________
3375 void AliTPCtrackerMI::MakeSeeds2(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2, Float_t */*cuts[4]*/,
3376                                  Float_t deltay, Bool_t /*bconstrain*/) {
3377   //-----------------------------------------------------------------
3378   // This function creates track seeds - without vertex constraint
3379   //-----------------------------------------------------------------
3380   // cuts[0]   - fP4 cut        - not applied
3381   // cuts[1]   - tan(phi)  cut
3382   // cuts[2]   - zvertex cut    - not applied 
3383   // cuts[3]   - fP3 cut
3384   Int_t nin0=0;
3385   Int_t nin1=0;
3386   Int_t nin2=0;
3387   Int_t nin3=0;
3388   //  Int_t nin4=0;
3389   //Int_t nin5=0;
3390
3391   
3392
3393   Double_t alpha=fOuterSec->GetAlpha(), shift=fOuterSec->GetAlphaShift();
3394   //  Double_t cs=cos(alpha), sn=sin(alpha);
3395   Int_t row0 = (i1+i2)/2;
3396   Int_t drow = (i1-i2)/2;
3397   const AliTPCRow& kr0=fSectors[sec][row0];
3398   AliTPCRow * kr=0;
3399
3400   AliTPCpolyTrack polytrack;
3401   Int_t nclusters=fSectors[sec][row0];
3402   AliTPCseed * seed = new AliTPCseed;
3403
3404   Int_t sumused=0;
3405   Int_t cused=0;
3406   Int_t cnused=0;
3407   for (Int_t is=0; is < nclusters; is++) {  //LOOP over clusters
3408     Int_t nfound =0;
3409     Int_t nfoundable =0;
3410     for (Int_t iter =1; iter<2; iter++){   //iterations
3411       const AliTPCRow& krm=fSectors[sec][row0-iter];
3412       const AliTPCRow& krp=fSectors[sec][row0+iter];      
3413       const AliTPCclusterMI * cl= kr0[is];
3414       
3415       if (cl->IsUsed(10)) {
3416         cused++;
3417       }
3418       else{
3419         cnused++;
3420       }
3421       Double_t x = kr0.GetX();
3422       // Initialization of the polytrack
3423       nfound =0;
3424       nfoundable =0;
3425       polytrack.Reset();
3426       //
3427       Double_t y0= cl->GetY();
3428       Double_t z0= cl->GetZ();
3429       Float_t erry = 0;
3430       Float_t errz = 0;
3431       
3432       Double_t ymax = fSectors->GetMaxY(row0)-kr0.fDeadZone-1.5;
3433       if (deltay>0 && TMath::Abs(ymax-TMath::Abs(y0))> deltay ) continue;  // seed only at the edge
3434       
3435       erry = (0.5)*cl->GetSigmaY2()/TMath::Sqrt(cl->GetQ())*6;      
3436       errz = (0.5)*cl->GetSigmaZ2()/TMath::Sqrt(cl->GetQ())*6;      
3437       polytrack.AddPoint(x,y0,z0,erry, errz);
3438
3439       sumused=0;
3440       if (cl->IsUsed(10)) sumused++;
3441
3442
3443       Float_t roady = (5*TMath::Sqrt(cl->GetSigmaY2()+0.2)+1.)*iter;
3444       Float_t roadz = (5*TMath::Sqrt(cl->GetSigmaZ2()+0.2)+1.)*iter;
3445       //
3446       x = krm.GetX();
3447       AliTPCclusterMI * cl1 = krm.FindNearest(y0,z0,roady,roadz);
3448       if (cl1 && TMath::Abs(ymax-TMath::Abs(y0))) {
3449         erry = (0.5)*cl1->GetSigmaY2()/TMath::Sqrt(cl1->GetQ())*3;          
3450         errz = (0.5)*cl1->GetSigmaZ2()/TMath::Sqrt(cl1->GetQ())*3;
3451         if (cl1->IsUsed(10))  sumused++;
3452         polytrack.AddPoint(x,cl1->GetY(),cl1->GetZ(),erry,errz);
3453       }
3454       //
3455       x = krp.GetX();
3456       AliTPCclusterMI * cl2 = krp.FindNearest(y0,z0,roady,roadz);
3457       if (cl2) {
3458         erry = (0.5)*cl2->GetSigmaY2()/TMath::Sqrt(cl2->GetQ())*3;          
3459         errz = (0.5)*cl2->GetSigmaZ2()/TMath::Sqrt(cl2->GetQ())*3;
3460         if (cl2->IsUsed(10)) sumused++;  
3461         polytrack.AddPoint(x,cl2->GetY(),cl2->GetZ(),erry,errz);
3462       }
3463       //
3464       if (sumused>0) continue;
3465       nin0++;
3466       polytrack.UpdateParameters();
3467       // follow polytrack
3468       roadz = 1.2;
3469       roady = 1.2;
3470       //
3471       Double_t yn,zn;
3472       nfoundable = polytrack.GetN();
3473       nfound     = nfoundable; 
3474       //
3475       for (Int_t ddrow = iter+1; ddrow<drow;ddrow++){
3476         Float_t maxdist = 0.8*(1.+3./(ddrow));
3477         for (Int_t delta = -1;delta<=1;delta+=2){
3478           Int_t row = row0+ddrow*delta;
3479           kr = &(fSectors[sec][row]);
3480           Double_t xn = kr->GetX();
3481           Double_t ymax = fSectors->GetMaxY(row)-kr->fDeadZone-1.5;
3482           polytrack.GetFitPoint(xn,yn,zn);
3483           if (TMath::Abs(yn)>ymax) continue;
3484           nfoundable++;
3485           AliTPCclusterMI * cln = kr->FindNearest(yn,zn,roady,roadz);
3486           if (cln) {
3487             Float_t dist =  TMath::Sqrt(  (yn-cln->GetY())*(yn-cln->GetY())+(zn-cln->GetZ())*(zn-cln->GetZ()));
3488             if (dist<maxdist){
3489               /*
3490               erry = (dist+0.3)*cln->GetSigmaY2()/TMath::Sqrt(cln->GetQ())*(1.+1./(ddrow));         
3491               errz = (dist+0.3)*cln->GetSigmaZ2()/TMath::Sqrt(cln->GetQ())*(1.+1./(ddrow));
3492               if (cln->IsUsed(10)) {
3493                 //      printf("used\n");
3494                 sumused++;
3495                 erry*=2;
3496                 errz*=2;
3497               }
3498               */
3499               erry=0.1;
3500               errz=0.1;
3501               polytrack.AddPoint(xn,cln->GetY(),cln->GetZ(),erry, errz);
3502               nfound++;
3503             }
3504           }
3505         }
3506         if ( (sumused>3) || (sumused>0.5*nfound) || (nfound<0.6*nfoundable))  break;     
3507         polytrack.UpdateParameters();
3508       }           
3509     }
3510     if ( (sumused>3) || (sumused>0.5*nfound))  {
3511       //printf("sumused   %d\n",sumused);
3512       continue;
3513     }
3514     nin1++;
3515     Double_t dy,dz;
3516     polytrack.GetFitDerivation(kr0.GetX(),dy,dz);
3517     AliTPCpolyTrack track2;
3518     
3519     polytrack.Refit(track2,0.5+TMath::Abs(dy)*0.3,0.4+TMath::Abs(dz)*0.3);
3520     if (track2.GetN()<0.5*nfoundable) continue;
3521     nin2++;
3522
3523     if ((nfound>0.6*nfoundable) &&( nfoundable>0.4*(i1-i2))) {
3524       //
3525       // test seed with and without constrain
3526       for (Int_t constrain=0; constrain<=0;constrain++){
3527         // add polytrack candidate
3528
3529         Double_t x[5], c[15];
3530         Double_t x1,x2,x3,y1,y2,y3,z1,z2,z3;
3531         track2.GetBoundaries(x3,x1);    
3532         x2 = (x1+x3)/2.;
3533         track2.GetFitPoint(x1,y1,z1);
3534         track2.GetFitPoint(x2,y2,z2);
3535         track2.GetFitPoint(x3,y3,z3);
3536         //
3537         //is track pointing to the vertex ?
3538         Double_t x0,y0,z0;
3539         x0=0;
3540         polytrack.GetFitPoint(x0,y0,z0);
3541
3542         if (constrain) {
3543           x2 = x3;
3544           y2 = y3;
3545           z2 = z3;
3546           
3547           x3 = 0;
3548           y3 = 0;
3549           z3 = 0;
3550         }
3551         x[0]=y1;
3552         x[1]=z1;
3553         x[4]=F1(x1,y1,x2,y2,x3,y3);
3554                 
3555         //      if (TMath::Abs(x[4]) >= cuts[0]) continue;  //
3556         x[2]=F2(x1,y1,x2,y2,x3,y3);
3557         
3558         //if (TMath::Abs(x[4]*x1-x[2]) >= cuts[1]) continue;
3559         //x[3]=F3(x1,y1,x2,y2,z1,z2);
3560         x[3]=F3n(x1,y1,x3,y3,z1,z3,x[4]);
3561         //if (TMath::Abs(x[3]) > cuts[3]) continue;
3562
3563         
3564         Double_t sy =0.1, sz =0.1;
3565         Double_t sy1=0.02, sz1=0.02;
3566         Double_t sy2=0.02, sz2=0.02;
3567         Double_t sy3=0.02;
3568
3569         if (constrain){
3570           sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
3571         }
3572         
3573         Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
3574         Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
3575         Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
3576         Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
3577         Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
3578         Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
3579
3580         Double_t f30=(F3(x1,y1+sy,x3,y3,z1,z3)-x[3])/sy;
3581         Double_t f31=(F3(x1,y1,x3,y3,z1+sz,z3)-x[3])/sz;
3582         Double_t f32=(F3(x1,y1,x3,y3+sy,z1,z3)-x[3])/sy;
3583         Double_t f34=(F3(x1,y1,x3,y3,z1,z3+sz)-x[3])/sz;
3584
3585         
3586         c[0]=sy1;
3587         c[1]=0.;       c[2]=sz1;
3588         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3589         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3590         c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3591         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3592         c[13]=f30*sy1*f40+f32*sy2*f42;
3593         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3594         
3595         //Int_t row1 = fSectors->GetRowNumber(x1);
3596         Int_t row1 = GetRowNumber(x1);
3597
3598         UInt_t index=0;
3599         //kr0.GetIndex(is);
3600         AliTPCseed *track=new (seed) AliTPCseed(x1,sec*alpha+shift,x,c,index);
3601         track->fIsSeeding = kTRUE;
3602         Int_t rc=FollowProlongation(*track, i2);        
3603         if (constrain) track->fBConstrain =1;
3604         else
3605           track->fBConstrain =0;
3606         track->fLastPoint = row1+fInnerSec->GetNRows();  // first cluster in track position
3607         track->fFirstPoint = track->fLastPoint;
3608
3609         if (rc==0 || track->GetNumberOfClusters()<(i1-i2)*0.5 || 
3610             track->GetNumberOfClusters() < track->fNFoundable*0.6 || 
3611             track->fNShared>0.4*track->GetNumberOfClusters()) {
3612           //delete track;
3613           seed->Reset();
3614           seed->~AliTPCseed();
3615         }
3616         else {
3617           arr->AddLast(track);
3618           seed = new AliTPCseed;
3619         }
3620         nin3++;
3621       }
3622     }  // if accepted seed
3623   }
3624   if (fDebug>3){
3625     Info("MakeSeeds2","\nSeeding statiistic:\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin3);
3626   }
3627   delete seed;
3628 }
3629
3630
3631 AliTPCseed *AliTPCtrackerMI::MakeSeed(AliTPCseed *track, Float_t r0, Float_t r1, Float_t r2)
3632 {
3633   //
3634   //
3635   //reseed using track points
3636   Int_t p0 = int(r0*track->GetNumberOfClusters());     // point 0 
3637   Int_t p1 = int(r1*track->GetNumberOfClusters());
3638   Int_t p2 = int(r2*track->GetNumberOfClusters());   // last point
3639   Int_t pp2=0;
3640   Double_t  x0[3],x1[3],x2[3];
3641   for (Int_t i=0;i<3;i++){
3642     x0[i]=-1;
3643     x1[i]=-1;
3644     x2[i]=-1;
3645   }
3646
3647   // find track position at given ratio of the length
3648   Int_t  sec0=0, sec1=0, sec2=0;
3649   Int_t index=-1;
3650   Int_t clindex;
3651   for (Int_t i=0;i<160;i++){
3652     if (track->fClusterPointer[i]){
3653       index++;
3654       AliTPCTrackerPoint   *trpoint =track->GetTrackPoint(i);
3655       if ( (index<p0) || x0[0]<0 ){
3656         if (trpoint->GetX()>1){
3657           clindex = track->GetClusterIndex2(i);
3658           if (clindex>0){       
3659             x0[0] = trpoint->GetX();
3660             x0[1] = trpoint->GetY();
3661             x0[2] = trpoint->GetZ();
3662             sec0  = ((clindex&0xff000000)>>24)%18;
3663           }
3664         }
3665       }
3666
3667       if ( (index<p1) &&(trpoint->GetX()>1)){
3668         clindex = track->GetClusterIndex2(i);
3669         if (clindex>0){
3670           x1[0] = trpoint->GetX();
3671           x1[1] = trpoint->GetY();
3672           x1[2] = trpoint->GetZ();
3673           sec1  = ((clindex&0xff000000)>>24)%18;
3674         }
3675       }
3676       if ( (index<p2) &&(trpoint->GetX()>1)){
3677         clindex = track->GetClusterIndex2(i);
3678         if (clindex>0){
3679           x2[0] = trpoint->GetX();
3680           x2[1] = trpoint->GetY();
3681           x2[2] = trpoint->GetZ(); 
3682           sec2  = ((clindex&0xff000000)>>24)%18;
3683           pp2 = i;
3684         }
3685       }
3686     }
3687   }
3688   
3689   Double_t alpha, cs,sn, xx2,yy2;
3690   //
3691   alpha = (sec1-sec2)*fSectors->GetAlpha();
3692   cs = TMath::Cos(alpha);
3693   sn = TMath::Sin(alpha); 
3694   xx2= x1[0]*cs-x1[1]*sn;
3695   yy2= x1[0]*sn+x1[1]*cs;
3696   x1[0] = xx2;
3697   x1[1] = yy2;
3698   //
3699   alpha = (sec0-sec2)*fSectors->GetAlpha();
3700   cs = TMath::Cos(alpha);
3701   sn = TMath::Sin(alpha); 
3702   xx2= x0[0]*cs-x0[1]*sn;
3703   yy2= x0[0]*sn+x0[1]*cs;
3704   x0[0] = xx2;
3705   x0[1] = yy2;
3706   //
3707   //
3708   //
3709   Double_t x[5],c[15];
3710   //
3711   x[0]=x2[1];
3712   x[1]=x2[2];
3713   x[4]=F1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
3714   //  if (x[4]>1) return 0;
3715   x[2]=F2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
3716   x[3]=F3n(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2],x[4]);
3717   //if (TMath::Abs(x[3]) > 2.2)  return 0;
3718   //if (TMath::Abs(x[2]) > 1.99) return 0;
3719   //  
3720   Double_t sy =0.1,  sz =0.1;
3721   //
3722   Double_t sy1=0.02+track->GetSigmaY2(), sz1=0.02+track->GetSigmaZ2();
3723   Double_t sy2=0.01+track->GetSigmaY2(), sz2=0.01+track->GetSigmaZ2();
3724   Double_t sy3=0.01+track->GetSigmaY2();
3725   //
3726   Double_t f40=(F1(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[4])/sy;
3727   Double_t f42=(F1(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[4])/sy;
3728   Double_t f43=(F1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[4])/sy;
3729   Double_t f20=(F2(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[2])/sy;
3730   Double_t f22=(F2(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[2])/sy;
3731   Double_t f23=(F2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[2])/sy;
3732   //
3733   Double_t f30=(F3(x2[0],x2[1]+sy,x0[0],x0[1],x2[2],x0[2])-x[3])/sy;
3734   Double_t f31=(F3(x2[0],x2[1],x0[0],x0[1],x2[2]+sz,x0[2])-x[3])/sz;
3735   Double_t f32=(F3(x2[0],x2[1],x0[0],x0[1]+sy,x2[2],x0[2])-x[3])/sy;
3736   Double_t f34=(F3(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2]+sz)-x[3])/sz;
3737   
3738   
3739   c[0]=sy1;
3740   c[1]=0.;       c[2]=sz1;
3741   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3742   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3743   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3744   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3745   c[13]=f30*sy1*f40+f32*sy2*f42;
3746   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3747   
3748   //  Int_t row1 = fSectors->GetRowNumber(x2[0]);
3749   AliTPCseed *seed=new  AliTPCseed(x2[0], sec2*fSectors->GetAlpha()+fSectors->GetAlphaShift(), x, c, 0);
3750   //  Double_t y0,z0,y1,z1, y2,z2;
3751   //seed->GetProlongation(x0[0],y0,z0);
3752   // seed->GetProlongation(x1[0],y1,z1);
3753   //seed->GetProlongation(x2[0],y2,z2);
3754   //  seed =0;
3755   seed->fLastPoint  = pp2;
3756   seed->fFirstPoint = pp2;
3757   
3758
3759   return seed;
3760 }
3761
3762
3763 AliTPCseed *AliTPCtrackerMI::ReSeed(AliTPCseed *track, Float_t r0, Float_t r1, Float_t r2)
3764 {
3765   //
3766   //
3767   //reseed using founded clusters 
3768   //
3769   // Find the number of clusters
3770   Int_t nclusters = 0;
3771   for (Int_t irow=0;irow<160;irow++){
3772     if (track->GetClusterIndex(irow)>0) nclusters++;
3773   }
3774   //
3775   Int_t ipos[3];
3776   ipos[0] = TMath::Max(int(r0*nclusters),0);             // point 0 cluster
3777   ipos[1] = TMath::Min(int(r1*nclusters),nclusters-1);   // 
3778   ipos[2] = TMath::Min(int(r2*nclusters),nclusters-1);   // last point
3779   //
3780   //
3781   Double_t  xyz[3][3];
3782   Int_t     row[3],sec[3]={0,0,0};
3783   //
3784   // find track row position at given ratio of the length
3785   Int_t index=-1;
3786   for (Int_t irow=0;irow<160;irow++){    
3787     if (track->GetClusterIndex2(irow)<0) continue;
3788     index++;
3789     for (Int_t ipoint=0;ipoint<3;ipoint++){
3790       if (index<=ipos[ipoint]) row[ipoint] = irow;
3791     }        
3792   }
3793   //
3794   //Get cluster and sector position
3795   for (Int_t ipoint=0;ipoint<3;ipoint++){
3796     Int_t clindex = track->GetClusterIndex2(row[ipoint]);
3797     AliTPCclusterMI * cl = GetClusterMI(clindex);
3798     if (cl==0) {
3799       //Error("Bug\n");
3800       //      AliTPCclusterMI * cl = GetClusterMI(clindex);
3801       return 0;
3802     }
3803     sec[ipoint]     = ((clindex&0xff000000)>>24)%18;
3804     xyz[ipoint][0]  = GetXrow(row[ipoint]);
3805     xyz[ipoint][1]  = cl->GetY();
3806     xyz[ipoint][2]  = cl->GetZ();
3807   }
3808   //
3809   //
3810   // Calculate seed state vector and covariance matrix
3811
3812   Double_t alpha, cs,sn, xx2,yy2;
3813   //
3814   alpha = (sec[1]-sec[2])*fSectors->GetAlpha();
3815   cs = TMath::Cos(alpha);
3816   sn = TMath::Sin(alpha); 
3817   xx2= xyz[1][0]*cs-xyz[1][1]*sn;
3818   yy2= xyz[1][0]*sn+xyz[1][1]*cs;
3819   xyz[1][0] = xx2;
3820   xyz[1][1] = yy2;
3821   //
3822   alpha = (sec[0]-sec[2])*fSectors->GetAlpha();
3823   cs = TMath::Cos(alpha);
3824   sn = TMath::Sin(alpha); 
3825   xx2= xyz[0][0]*cs-xyz[0][1]*sn;
3826   yy2= xyz[0][0]*sn+xyz[0][1]*cs;
3827   xyz[0][0] = xx2;
3828   xyz[0][1] = yy2;
3829   //
3830   //
3831   //
3832   Double_t x[5],c[15];
3833   //
3834   x[0]=xyz[2][1];
3835   x[1]=xyz[2][2];
3836   x[4]=F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3837   x[2]=F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3838   x[3]=F3n(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2],x[4]);
3839   //  
3840   Double_t sy =0.1,  sz =0.1;
3841   //
3842   Double_t sy1=0.2, sz1=0.2;
3843   Double_t sy2=0.2, sz2=0.2;
3844   Double_t sy3=0.2;
3845   //
3846   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;
3847   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;
3848   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;
3849   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;
3850   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;
3851   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;
3852   //
3853   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;
3854   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;
3855   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;
3856   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;
3857   
3858   
3859   c[0]=sy1;
3860   c[1]=0.;       c[2]=sz1;
3861   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3862   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3863   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3864   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3865   c[13]=f30*sy1*f40+f32*sy2*f42;
3866   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3867   
3868   //  Int_t row1 = fSectors->GetRowNumber(xyz[2][0]);
3869   AliTPCseed *seed=new  AliTPCseed(xyz[2][0], sec[2]*fSectors->GetAlpha()+fSectors->GetAlphaShift(), x, c, 0);
3870   seed->fLastPoint  = row[2];
3871   seed->fFirstPoint = row[2];  
3872   return seed;
3873 }
3874
3875
3876 AliTPCseed *AliTPCtrackerMI::ReSeed(AliTPCseed *track,Int_t r0, Bool_t forward)
3877 {
3878   //
3879   //
3880   //reseed using founded clusters 
3881   //
3882   Double_t  xyz[3][3];
3883   Int_t     row[3]={0,0,0};
3884   Int_t     sec[3]={0,0,0};
3885   //
3886   // forward direction
3887   if (forward){
3888     for (Int_t irow=r0;irow<160;irow++){
3889       if (track->GetClusterIndex(irow)>0){
3890         row[0] = irow;
3891         break;
3892       }
3893     }
3894     for (Int_t irow=160;irow>r0;irow--){
3895       if (track->GetClusterIndex(irow)>0){
3896         row[2] = irow;
3897         break;
3898       }
3899     }
3900     for (Int_t irow=row[2]-15;irow>row[0];irow--){
3901       if (track->GetClusterIndex(irow)>0){
3902         row[1] = irow;
3903         break;
3904       }
3905     }
3906     //
3907   }
3908   if (!forward){
3909     for (Int_t irow=0;irow<r0;irow++){
3910       if (track->GetClusterIndex(irow)>0){
3911         row[0] = irow;
3912         break;
3913       }
3914     }
3915     for (Int_t irow=r0;irow>0;irow--){
3916       if (track->GetClusterIndex(irow)>0){
3917         row[2] = irow;
3918         break;
3919       }
3920     }    
3921     for (Int_t irow=row[2]-15;irow>row[0];irow--){
3922       if (track->GetClusterIndex(irow)>0){
3923         row[1] = irow;
3924         break;
3925       }
3926     } 
3927   }
3928   //
3929   if ((row[2]-row[0])<20) return 0;
3930   if (row[1]==0) return 0;
3931   //
3932   //
3933   //Get cluster and sector position
3934   for (Int_t ipoint=0;ipoint<3;ipoint++){
3935     Int_t clindex = track->GetClusterIndex2(row[ipoint]);
3936     AliTPCclusterMI * cl = GetClusterMI(clindex);
3937     if (cl==0) {
3938       //Error("Bug\n");
3939       //      AliTPCclusterMI * cl = GetClusterMI(clindex);
3940       return 0;
3941     }
3942     sec[ipoint]     = ((clindex&0xff000000)>>24)%18;
3943     xyz[ipoint][0]  = GetXrow(row[ipoint]);
3944     AliTPCTrackerPoint * point = track->GetTrackPoint(row[ipoint]);    
3945     if (point&&ipoint<2){
3946       //
3947        xyz[ipoint][1]  = point->GetY();
3948        xyz[ipoint][2]  = point->GetZ();
3949     }
3950     else{
3951       xyz[ipoint][1]  = cl->GetY();
3952       xyz[ipoint][2]  = cl->GetZ();
3953     }
3954   }
3955   //
3956   //
3957   //
3958   //
3959   // Calculate seed state vector and covariance matrix
3960
3961   Double_t alpha, cs,sn, xx2,yy2;
3962   //
3963   alpha = (sec[1]-sec[2])*fSectors->GetAlpha();
3964   cs = TMath::Cos(alpha);
3965   sn = TMath::Sin(alpha); 
3966   xx2= xyz[1][0]*cs-xyz[1][1]*sn;
3967   yy2= xyz[1][0]*sn+xyz[1][1]*cs;
3968   xyz[1][0] = xx2;
3969   xyz[1][1] = yy2;
3970   //
3971   alpha = (sec[0]-sec[2])*fSectors->GetAlpha();
3972   cs = TMath::Cos(alpha);
3973   sn = TMath::Sin(alpha); 
3974   xx2= xyz[0][0]*cs-xyz[0][1]*sn;
3975   yy2= xyz[0][0]*sn+xyz[0][1]*cs;
3976   xyz[0][0] = xx2;
3977   xyz[0][1] = yy2;
3978   //
3979   //
3980   //
3981   Double_t x[5],c[15];
3982   //
3983   x[0]=xyz[2][1];
3984   x[1]=xyz[2][2];
3985   x[4]=F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3986   x[2]=F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3987   x[3]=F3n(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2],x[4]);
3988   //  
3989   Double_t sy =0.1,  sz =0.1;
3990   //
3991   Double_t sy1=0.2, sz1=0.2;
3992   Double_t sy2=0.2, sz2=0.2;
3993   Double_t sy3=0.2;
3994   //
3995   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;
3996   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;
3997   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;
3998   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;
3999   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;
4000   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;
4001   //
4002   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;
4003   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;
4004   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;
4005   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;
4006   
4007   
4008   c[0]=sy1;
4009   c[1]=0.;       c[2]=sz1;
4010   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
4011   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
4012   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
4013   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
4014   c[13]=f30*sy1*f40+f32*sy2*f42;
4015   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
4016   
4017   //  Int_t row1 = fSectors->GetRowNumber(xyz[2][0]);
4018   AliTPCseed *seed=new  AliTPCseed(xyz[2][0], sec[2]*fSectors->GetAlpha()+fSectors->GetAlphaShift(), x, c, 0);
4019   seed->fLastPoint  = row[2];
4020   seed->fFirstPoint = row[2];  
4021   for (Int_t i=row[0];i<row[2];i++){
4022     seed->fIndex[i] = track->fIndex[i];
4023   }
4024
4025   return seed;
4026 }
4027
4028 void  AliTPCtrackerMI::FindKinks(TObjArray * array, AliESD *esd)
4029 {
4030   //
4031   //  find kinks
4032   //
4033   //
4034
4035   TObjArray *kinks= new TObjArray(10000);
4036   //  TObjArray *v0s= new TObjArray(10000);
4037   Int_t nentries = array->GetEntriesFast();
4038   AliHelix *helixes      = new AliHelix[nentries];
4039   Int_t    *sign         = new Int_t[nentries];
4040   Int_t    *nclusters    = new Int_t[nentries];
4041   Float_t  *alpha        = new Float_t[nentries];
4042   AliKink  *kink         = new AliKink();
4043   Int_t      * usage     = new Int_t[nentries];
4044   Float_t  *zm           = new Float_t[nentries];
4045   Float_t  *z0           = new Float_t[nentries]; 
4046   Float_t  *fim          = new Float_t[nentries];
4047   Float_t  *shared       = new Float_t[nentries];
4048   Bool_t   *circular     = new Bool_t[nentries];
4049   Float_t *dca          = new Float_t[nentries];
4050   //const AliESDVertex * primvertex = esd->GetVertex();
4051   //
4052   //  nentries = array->GetEntriesFast();
4053   //
4054   
4055   //
4056   //
4057   for (Int_t i=0;i<nentries;i++){
4058     sign[i]=0;
4059     usage[i]=0;
4060     AliTPCseed* track = (AliTPCseed*)array->At(i);    
4061     if (!track) continue;
4062     track->fCircular =0;
4063     shared[i] = kFALSE;
4064     track->UpdatePoints();
4065     if (( track->GetPoints()[2]- track->GetPoints()[0])>5 && track->GetPoints()[3]>0.8){
4066     }
4067     nclusters[i]=track->GetNumberOfClusters();
4068     alpha[i] = track->GetAlpha();
4069     new (&helixes[i]) AliHelix(*track);
4070     Double_t xyz[3];
4071     helixes[i].Evaluate(0,xyz);
4072     sign[i] = (track->GetC()>0) ? -1:1;
4073     Double_t x,y,z;
4074     x=160;
4075     if (track->GetProlongation(x,y,z)){
4076       zm[i]  = z;
4077       fim[i] = alpha[i]+TMath::ATan2(y,x);
4078     }
4079     else{
4080       zm[i]  = track->GetZ();
4081       fim[i] = alpha[i];
4082     }   
4083     z0[i]=1000;
4084     circular[i]= kFALSE;
4085     if (track->GetProlongation(0,y,z))  z0[i] = z;
4086     dca[i] = track->GetD(0,0);    
4087   }
4088   //
4089   //
4090   TStopwatch timer;
4091   timer.Start();
4092   Int_t ncandidates =0;
4093   Int_t nall =0;
4094   Int_t ntracks=0; 
4095   Double_t phase[2][2],radius[2];
4096
4097   //
4098   // Find circling track
4099   TTreeSRedirector &cstream = *fDebugStreamer;
4100   //
4101   for (Int_t i0=0;i0<nentries;i0++){
4102     AliTPCseed * track0 = (AliTPCseed*)array->At(i0);
4103     if (!track0) continue;    
4104     if (track0->fN<40) continue;
4105     if (TMath::Abs(1./track0->GetC())>200) continue;
4106     for (Int_t i1=i0+1;i1<nentries;i1++){
4107       AliTPCseed * track1 = (AliTPCseed*)array->At(i1);
4108       if (!track1) continue;
4109       if (track1->fN<40)                  continue;
4110       if ( TMath::Abs(track1->GetTgl()+track0->GetTgl())>0.1) continue;
4111       if (track0->fBConstrain&&track1->fBConstrain) continue;
4112       if (TMath::Abs(1./track1->GetC())>200) continue;
4113       if (track1->Get1Pt()*track0->Get1Pt()>0)      continue;
4114       if (track1->GetTgl()*track0->GetTgl()>0)      continue;
4115       if (max(TMath::Abs(1./track0->GetC()),TMath::Abs(1./track1->GetC()))>190) continue;
4116       if (track0->fBConstrain&&TMath::Abs(track1->Get1Pt())<TMath::Abs(track0->Get1Pt())) continue; //returning - lower momenta
4117       if (track1->fBConstrain&&TMath::Abs(track0->Get1Pt())<TMath::Abs(track1->Get1Pt())) continue; //returning - lower momenta
4118       //
4119       Float_t mindcar = TMath::Min(TMath::Abs(dca[i0]),TMath::Abs(dca[i1]));
4120       if (mindcar<5)   continue;
4121       Float_t mindcaz = TMath::Min(TMath::Abs(z0[i0]-GetZ()),TMath::Abs(z0[i1]-GetZ()));
4122       if (mindcaz<5) continue;
4123       if (mindcar+mindcaz<20) continue;
4124       //
4125       //
4126       Float_t xc0 = helixes[i0].GetHelix(6);
4127       Float_t yc0 = helixes[i0].GetHelix(7);
4128       Float_t r0  = helixes[i0].GetHelix(8);
4129       Float_t xc1 = helixes[i1].GetHelix(6);
4130       Float_t yc1 = helixes[i1].GetHelix(7);
4131       Float_t r1  = helixes[i1].GetHelix(8);
4132         
4133       Float_t rmean = (r0+r1)*0.5;
4134       Float_t delta =TMath::Sqrt((xc1-xc0)*(xc1-xc0)+(yc1-yc0)*(yc1-yc0));
4135       //if (delta>30) continue;
4136       if (delta>rmean*0.25) continue;
4137       if (TMath::Abs(r0-r1)/rmean>0.3) continue; 
4138       //
4139       Int_t npoints = helixes[i0].GetRPHIintersections(helixes[i1], phase, radius,10);
4140       if (npoints==0) continue;
4141       helixes[i0].GetClosestPhases(helixes[i1], phase);
4142       //
4143       Double_t xyz0[3];
4144       Double_t xyz1[3];
4145       Double_t hangles[3];
4146       helixes[i0].Evaluate(phase[0][0],xyz0);
4147       helixes[i1].Evaluate(phase[0][1],xyz1);
4148
4149       helixes[i0].GetAngle(phase[0][0],helixes[i1],phase[0][1],hangles);
4150       Double_t deltah[2],deltabest;
4151       if (hangles[2]<2.8) continue;
4152       /*
4153       cstream<<"C"<<track0->fLab<<track1->fLab<<
4154         track0->fP3<<track1->fP3<<
4155         track0->fP4<<track1->fP4<<
4156         delta<<rmean<<npoints<<
4157         hangles[0]<<hangles[2]<<
4158         xyz0[2]<<xyz1[2]<<radius[0]<<"\n"; 
4159       */
4160       if (npoints>0){
4161         Int_t ibest=0;
4162         helixes[i0].ParabolicDCA(helixes[i1],phase[0][0],phase[0][1],radius[0],deltah[0],2);
4163         if (npoints==2){
4164           helixes[i0].ParabolicDCA(helixes[i1],phase[1][0],phase[1][1],radius[1],deltah[1],2);
4165           if (deltah[1]<deltah[0]) ibest=1;
4166         }
4167         deltabest  = TMath::Sqrt(deltah[ibest]);
4168         helixes[i0].Evaluate(phase[ibest][0],xyz0);
4169         helixes[i1].Evaluate(phase[ibest][1],xyz1);
4170         helixes[i0].GetAngle(phase[ibest][0],helixes[i1],phase[ibest][1],hangles);
4171         Double_t radiusbest = TMath::Sqrt(radius[ibest]);
4172         //
4173         if (deltabest>6) continue;
4174         if (mindcar+mindcaz<40 && (hangles[2]<3.12||deltabest>3)) continue;
4175         Bool_t sign =kFALSE;
4176         if (hangles[2]>3.06) sign =kTRUE;
4177         //
4178         if (sign){
4179           circular[i0] = kTRUE;
4180           circular[i1] = kTRUE;
4181           if (TMath::Abs(track0->Get1Pt())<TMath::Abs(track1->Get1Pt())){
4182             track0->fCircular += 1;
4183             track1->fCircular += 2;
4184           }
4185           else{
4186             track1->fCircular += 1;
4187             track0->fCircular += 2;
4188           }
4189         }               
4190         if (sign&&AliTPCReconstructor::StreamLevel()>1){          
4191           //debug stream          
4192           cstream<<"Curling"<<
4193             "lab0="<<track0->fLab<<
4194             "lab1="<<track1->fLab<<   
4195             "Tr0.="<<track0<<
4196             "Tr1.="<<track1<<      
4197             "dca0="<<dca[i0]<<
4198             "dca1="<<dca[i1]<<
4199             "mindcar="<<mindcar<<
4200             "mindcaz="<<mindcaz<<
4201             "delta="<<delta<<
4202             "rmean="<<rmean<<
4203             "npoints="<<npoints<<                      
4204             "hangles0="<<hangles[0]<<
4205             "hangles2="<<hangles[2]<<                    
4206             "xyz0="<<xyz0[2]<<
4207             "xyzz1="<<xyz1[2]<<
4208             "z0="<<z0[i0]<<
4209             "z1="<<z0[i1]<<
4210             "radius="<<radiusbest<<
4211             "deltabest="<<deltabest<< 
4212             "phase0="<<phase[ibest][0]<<
4213             "phase1="<<phase[ibest][1]<<
4214             "\n";                 
4215         }
4216       }
4217     }
4218   }
4219   //
4220   //  Finf kinks loop
4221   // 
4222   //
4223   for (Int_t i =0;i<nentries;i++){
4224     if (sign[i]==0) continue;
4225     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4226     ntracks++;
4227     //
4228     Double_t cradius0 = 40*40;
4229     Double_t cradius1 = 270*270;
4230     Double_t cdist1=8.;
4231     Double_t cdist2=8.;
4232     Double_t cdist3=0.55; 
4233     for (Int_t j =i+1;j<nentries;j++){
4234       nall++;
4235       if (sign[j]*sign[i]<1) continue;
4236       if ( (nclusters[i]+nclusters[j])>200) continue;
4237       if ( (nclusters[i]+nclusters[j])<80) continue;
4238       if ( TMath::Abs(zm[i]-zm[j])>60.) continue;
4239       if ( TMath::Abs(fim[i]-fim[j])>0.6 && TMath::Abs(fim[i]-fim[j])<5.7 ) continue;
4240       //AliTPCseed * track1 = (AliTPCseed*)array->At(j);  Double_t phase[2][2],radius[2];    
4241       Int_t npoints = helixes[i].GetRPHIintersections(helixes[j], phase, radius,20);
4242       if (npoints<1) continue;
4243       // cuts on radius      
4244       if (npoints==1){
4245         if (radius[0]<cradius0||radius[0]>cradius1) continue;
4246       }
4247       else{
4248         if ( (radius[0]<cradius0||radius[0]>cradius1) && (radius[1]<cradius0||radius[1]>cradius1) ) continue;
4249       }
4250       //      
4251       Double_t delta1=10000,delta2=10000;
4252       // cuts on the intersection radius
4253       helixes[i].LinearDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta1);
4254       if (radius[0]<20&&delta1<1) continue; //intersection at vertex
4255       if (radius[0]<10&&delta1<3) continue; //intersection at vertex
4256       if (npoints==2){ 
4257         helixes[i].LinearDCA(helixes[j],phase[1][0],phase[1][1],radius[1],delta2);
4258         if (radius[1]<20&&delta2<1) continue;  //intersection at vertex
4259         if (radius[1]<10&&delta2<3) continue;  //intersection at vertex 
4260       }
4261       //
4262       Double_t distance1 = TMath::Min(delta1,delta2);
4263       if (distance1>cdist1) continue;  // cut on DCA linear approximation
4264       //
4265       npoints = helixes[i].GetRPHIintersections(helixes[j], phase, radius,20);
4266       helixes[i].ParabolicDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta1);
4267       if (radius[0]<20&&delta1<1) continue; //intersection at vertex
4268       if (radius[0]<10&&delta1<3) continue; //intersection at vertex
4269       //
4270       if (npoints==2){ 
4271         helixes[i].ParabolicDCA(helixes[j],phase[1][0],phase[1][1],radius[1],delta2);   
4272         if (radius[1]<20&&delta2<1) continue;  //intersection at vertex
4273         if (radius[1]<10&&delta2<3) continue;  //intersection at vertex 
4274       }            
4275       distance1 = TMath::Min(delta1,delta2);
4276       Float_t rkink =0;
4277       if (delta1<delta2){
4278         rkink = TMath::Sqrt(radius[0]);
4279       }
4280       else{
4281         rkink = TMath::Sqrt(radius[1]);
4282       }
4283       if (distance1>cdist2) continue;
4284       //
4285       //
4286       AliTPCseed * track1 = (AliTPCseed*)array->At(j);
4287       //
4288       //
4289       Int_t row0 = GetRowNumber(rkink); 
4290       if (row0<10)  continue;
4291       if (row0>150) continue;
4292       //
4293       //
4294       Float_t dens00=-1,dens01=-1;
4295       Float_t dens10=-1,dens11=-1;
4296       //
4297       Int_t found,foundable,shared;
4298       track0->GetClusterStatistic(0,row0-5, found, foundable,shared,kFALSE);
4299       if (foundable>5) dens00 = Float_t(found)/Float_t(foundable);
4300       track0->GetClusterStatistic(row0+5,155, found, foundable,shared,kFALSE);
4301       if (foundable>5) dens01 = Float_t(found)/Float_t(foundable);
4302       //
4303       track1->GetClusterStatistic(0,row0-5, found, foundable,shared,kFALSE);
4304       if (foundable>10) dens10 = Float_t(found)/Float_t(foundable);
4305       track1->GetClusterStatistic(row0+5,155, found, foundable,shared,kFALSE);
4306       if (foundable>10) dens11 = Float_t(found)/Float_t(foundable);
4307       //     
4308       if (dens00<dens10 && dens01<dens11) continue;
4309       if (dens00>dens10 && dens01>dens11) continue;
4310       if (TMath::Max(dens00,dens10)<0.1)  continue;
4311       if (TMath::Max(dens01,dens11)<0.3)  continue;
4312       //
4313       if (TMath::Min(dens00,dens10)>0.6)  continue;
4314       if (TMath::Min(dens01,dens11)>0.6)  continue;
4315
4316       //
4317       AliTPCseed * ktrack0, *ktrack1;
4318       if (dens00>dens10){
4319         ktrack0 = track0;
4320         ktrack1 = track1;
4321       }
4322       else{
4323         ktrack0 = track1;
4324         ktrack1 = track0;
4325       }
4326       if (TMath::Abs(ktrack0->GetC())>5) continue; // cut on the curvature for mother particle
4327       AliExternalTrackParam paramm(*ktrack0);
4328       AliExternalTrackParam paramd(*ktrack1);
4329       if (row0>60&&ktrack1->GetReference().GetX()>90.) new (&paramd) AliExternalTrackParam(ktrack1->GetReference()); 
4330       //
4331       //
4332       kink->SetMother(paramm);
4333       kink->SetDaughter(paramd);
4334       kink->Update();
4335
4336       Float_t x[3] = { kink->GetPosition()[0],kink->GetPosition()[1],kink->GetPosition()[2]};
4337       Int_t index[4];
4338       fParam->Transform0to1(x,index);
4339       fParam->Transform1to2(x,index);
4340       row0 = GetRowNumber(x[0]); 
4341
4342       if (kink->GetR()<100) continue;
4343       if (kink->GetR()>240) continue;
4344       if (kink->GetPosition()[2]/kink->GetR()>AliTPCReconstructor::GetCtgRange()) continue;  //out of fiducial volume
4345       if (kink->GetDistance()>cdist3) continue;
4346       Float_t dird = kink->GetDaughterP()[0]*kink->GetPosition()[0]+kink->GetDaughterP()[1]*kink->GetPosition()[1];  // rough direction estimate
4347       if (dird<0) continue;
4348
4349       Float_t dirm = kink->GetMotherP()[0]*kink->GetPosition()[0]+kink->GetMotherP()[1]*kink->GetPosition()[1];  // rough direction estimate
4350       if (dirm<0) continue;
4351       Float_t mpt = TMath::Sqrt(kink->GetMotherP()[0]*kink->GetMotherP()[0]+kink->GetMotherP()[1]*kink->GetMotherP()[1]);
4352       if (mpt<0.2) continue;
4353
4354       if (mpt<1){
4355         //for high momenta momentum not defined well in first iteration
4356         Double_t qt   =  TMath::Sin(kink->GetAngle(2))*ktrack1->GetP();
4357         if (qt>0.35) continue; 
4358       }
4359       
4360       kink->SetLabel(CookLabel(ktrack0,0.4,0,row0),0);
4361       kink->SetLabel(CookLabel(ktrack1,0.4,row0,160),1);
4362       if (dens00>dens10){
4363         kink->SetTPCDensity(dens00,0,0);
4364         kink->SetTPCDensity(dens01,0,1);
4365         kink->SetTPCDensity(dens10,1,0);
4366         kink->SetTPCDensity(dens11,1,1);
4367         kink->SetIndex(i,0);
4368         kink->SetIndex(j,1);
4369       }
4370       else{
4371         kink->SetTPCDensity(dens10,0,0);
4372         kink->SetTPCDensity(dens11,0,1);
4373         kink->SetTPCDensity(dens00,1,0);
4374         kink->SetTPCDensity(dens01,1,1);
4375         kink->SetIndex(j,0);
4376         kink->SetIndex(i,1);
4377       }
4378
4379       if (mpt<1||kink->GetAngle(2)>0.1){
4380         //      angle and densities  not defined yet
4381         if (kink->GetTPCDensityFactor()<0.8) continue;
4382         if ((2-kink->GetTPCDensityFactor())*kink->GetDistance() >0.25) continue;
4383         if (kink->GetAngle(2)*ktrack0->GetP()<0.003) continue; //too small angle
4384         if (kink->GetAngle(2)>0.2&&kink->GetTPCDensityFactor()<1.15) continue;
4385         if (kink->GetAngle(2)>0.2&&kink->GetTPCDensity(0,1)>0.05) continue;
4386
4387         Float_t criticalangle = track0->GetSigmaSnp2()+track0->GetSigmaTgl2();
4388         criticalangle+= track1->GetSigmaSnp2()+track1->GetSigmaTgl2();
4389         criticalangle= 3*TMath::Sqrt(criticalangle);
4390         if (criticalangle>0.02) criticalangle=0.02;
4391         if (kink->GetAngle(2)<criticalangle) continue;
4392       }
4393       //
4394       Int_t drow = Int_t(2.+0.5/(0.05+kink->GetAngle(2)));  // overlap region defined
4395       Float_t shapesum =0;
4396       Float_t sum = 0;
4397       for ( Int_t row = row0-drow; row<row0+drow;row++){
4398         if (row<0) continue;
4399         if (row>155) continue;
4400         if (ktrack0->fClusterPointer[row]){
4401           AliTPCTrackerPoint *point =ktrack0->GetTrackPoint(row);
4402           shapesum+=point->GetSigmaY()+point->GetSigmaZ();
4403           sum++;
4404         }
4405         if (ktrack1->fClusterPointer[row]){
4406           AliTPCTrackerPoint *point =ktrack1->GetTrackPoint(row);
4407           shapesum+=point->GetSigmaY()+point->GetSigmaZ();
4408           sum++;
4409         }       
4410       }
4411       if (sum<4){
4412         kink->SetShapeFactor(-1.);
4413       }
4414       else{
4415         kink->SetShapeFactor(shapesum/sum);
4416       }      
4417       //      esd->AddKink(kink);
4418       kinks->AddLast(kink);
4419       kink = new AliKink;
4420       ncandidates++;
4421     }
4422   }
4423   //
4424   // sort the kinks according quality - and refit them towards vertex
4425   //
4426   Int_t       nkinks    = kinks->GetEntriesFast();
4427   Float_t    *quality   = new Float_t[nkinks];
4428   Int_t      *indexes   = new Int_t[nkinks];
4429   AliTPCseed *mothers   = new AliTPCseed[nkinks];
4430   AliTPCseed *daughters = new AliTPCseed[nkinks];
4431   //
4432   //
4433   for (Int_t i=0;i<nkinks;i++){
4434     quality[i] =100000;
4435     AliKink *kink = (AliKink*)kinks->At(i);
4436     //
4437     // refit kinks towards vertex
4438     // 
4439     Int_t index0 = kink->GetIndex(0);
4440     Int_t index1 = kink->GetIndex(1);
4441     AliTPCseed * ktrack0 = (AliTPCseed*)array->At(index0);
4442     AliTPCseed * ktrack1 = (AliTPCseed*)array->At(index1);
4443     //
4444     Int_t sumn=ktrack0->fN+ktrack1->fN;
4445     //
4446     // Refit Kink under if too small angle
4447     //
4448     if (kink->GetAngle(2)<0.05){
4449       kink->SetTPCRow0(GetRowNumber(kink->GetR()));
4450       Int_t row0 = kink->GetTPCRow0();
4451       Int_t drow = Int_t(2.+0.5/(0.05+kink->GetAngle(2)));
4452       //
4453       //
4454       Int_t last  = row0-drow;
4455       if (last<40) last=40;
4456       if (last<ktrack0->fFirstPoint+25) last = ktrack0->fFirstPoint+25;
4457       AliTPCseed* seed0 = ReSeed(ktrack0,last,kFALSE);
4458       //
4459       //
4460       Int_t first = row0+drow;
4461       if (first>130) first=130;
4462       if (first>ktrack1->fLastPoint-25) first = TMath::Max(ktrack1->fLastPoint-25,30);
4463       AliTPCseed* seed1 = ReSeed(ktrack1,first,kTRUE);
4464       //
4465       if (seed0 && seed1){
4466         kink->SetStatus(1,8);
4467         if (RefitKink(*seed0,*seed1,*kink)) kink->SetStatus(1,9);
4468         row0 = GetRowNumber(kink->GetR());
4469         sumn = seed0->fN+seed1->fN;
4470         new (&mothers[i])   AliTPCseed(*seed0);
4471         new (&daughters[i]) AliTPCseed(*seed1); 
4472       }
4473       else{
4474         delete kinks->RemoveAt(i);
4475         if (seed0) delete seed0;
4476         if (seed1) delete seed1;
4477         continue;
4478       }
4479       if (kink->GetDistance()>0.5 || kink->GetR()<110 || kink->GetR()>240) {
4480         delete kinks->RemoveAt(i);
4481         if (seed0) delete seed0;
4482         if (seed1) delete seed1;
4483         continue;
4484       }
4485       //
4486       delete seed0;
4487       delete seed1;            
4488     }
4489     //
4490     if (kink) quality[i] = 160*((0.1+kink->GetDistance())*(2.-kink->GetTPCDensityFactor()))/(sumn+40.);  //the longest -clossest will win
4491   }
4492   TMath::Sort(nkinks,quality,indexes,kFALSE);
4493   //
4494   //remove double find kinks
4495   //
4496   for (Int_t ikink0=1;ikink0<nkinks;ikink0++){
4497     AliKink * kink0 = (AliKink*) kinks->At(indexes[ikink0]);
4498     if (!kink0) continue;
4499     //
4500     for (Int_t ikink1=0;ikink1<ikink0;ikink1++){
4501       if (!kink0) continue;
4502       AliKink * kink1 = (AliKink*) kinks->At(indexes[ikink1]);
4503       if (!kink1) continue;
4504       // if not close kink continue
4505       if (TMath::Abs(kink1->GetPosition()[2]-kink0->GetPosition()[2])>10) continue;
4506       if (TMath::Abs(kink1->GetPosition()[1]-kink0->GetPosition()[1])>10) continue;
4507       if (TMath::Abs(kink1->GetPosition()[0]-kink0->GetPosition()[0])>10) continue;
4508       //
4509       AliTPCseed &mother0   = mothers[indexes[ikink0]];
4510       AliTPCseed &daughter0 = daughters[indexes[ikink0]];
4511       AliTPCseed &mother1   = mothers[indexes[ikink1]];
4512       AliTPCseed &daughter1 = daughters[indexes[ikink1]];
4513       Int_t row0 = (kink0->GetTPCRow0()+kink1->GetTPCRow0())/2;
4514       //
4515       Int_t same  = 0;
4516       Int_t both  = 0;
4517       Int_t samem = 0;
4518       Int_t bothm = 0;
4519       Int_t samed = 0;
4520       Int_t bothd = 0;
4521       //
4522       for (Int_t i=0;i<row0;i++){
4523         if (mother0.fIndex[i]>0 && mother1.fIndex[i]>0){
4524           both++;
4525           bothm++;
4526           if (mother0.fIndex[i]==mother1.fIndex[i]){
4527             same++;
4528             samem++;
4529           }
4530         }
4531       }
4532
4533       for (Int_t i=row0;i<158;i++){
4534         if (daughter0.fIndex[i]>0 && daughter0.fIndex[i]>0){
4535           both++;
4536           bothd++;
4537           if (mother0.fIndex[i]==mother1.fIndex[i]){
4538             same++;
4539             samed++;
4540           }
4541         }
4542       }
4543       Float_t ratio = Float_t(same+1)/Float_t(both+1);
4544       Float_t ratiom = Float_t(samem+1)/Float_t(bothm+1);
4545       Float_t ratiod = Float_t(samed+1)/Float_t(bothd+1);
4546       if (ratio>0.3 && ratiom>0.5 &&ratiod>0.5) {
4547         Int_t sum0 = mother0.fN+daughter0.fN;
4548         Int_t sum1 = mother1.fN+daughter1.fN;
4549         if (sum1>sum0){
4550           shared[kink0->GetIndex(0)]= kTRUE;
4551           shared[kink0->GetIndex(1)]= kTRUE;      
4552           delete kinks->RemoveAt(indexes[ikink0]);
4553         }
4554         else{
4555           shared[kink1->GetIndex(0)]= kTRUE;
4556           shared[kink1->GetIndex(1)]= kTRUE;      
4557           delete kinks->RemoveAt(indexes[ikink1]);
4558         }
4559       }
4560     }
4561   }
4562
4563
4564   for (Int_t i=0;i<nkinks;i++){
4565     AliKink * kink = (AliKink*) kinks->At(indexes[i]);
4566     if (!kink) continue;
4567     kink->SetTPCRow0(GetRowNumber(kink->GetR()));
4568     Int_t index0 = kink->GetIndex(0);
4569     Int_t index1 = kink->GetIndex(1);
4570     if (circular[index0]||circular[index1]&&kink->GetDistance()>0.2) continue;
4571     kink->SetMultiple(usage[index0],0);
4572     kink->SetMultiple(usage[index1],1);
4573     if (kink->GetMultiple()[0]+kink->GetMultiple()[1]>2) continue;
4574     if (kink->GetMultiple()[0]+kink->GetMultiple()[1]>0 && quality[indexes[i]]>0.2) continue;
4575     if (kink->GetMultiple()[0]+kink->GetMultiple()[1]>0 && kink->GetDistance()>0.2) continue;
4576     if (circular[index0]||circular[index1]&&kink->GetDistance()>0.1) continue;
4577
4578     AliTPCseed * ktrack0 = (AliTPCseed*)array->At(index0);
4579     AliTPCseed * ktrack1 = (AliTPCseed*)array->At(index1);
4580     if (!ktrack0 || !ktrack1) continue;
4581     Int_t index = esd->AddKink(kink);
4582     //
4583     //
4584     if ( ktrack0->fKinkIndexes[0]==0 && ktrack1->fKinkIndexes[0]==0) {  //best kink
4585       if (mothers[indexes[i]].fN>20 && daughters[indexes[i]].fN>20 && (mothers[indexes[i]].fN+daughters[indexes[i]].fN)>100){
4586         new (ktrack0) AliTPCseed(mothers[indexes[i]]);
4587         new (ktrack1) AliTPCseed(daughters[indexes[i]]);
4588       }
4589     }
4590     //
4591     ktrack0->fKinkIndexes[usage[index0]] = -(index+1);
4592     ktrack1->fKinkIndexes[usage[index1]] =  (index+1);
4593     usage[index0]++;
4594     usage[index1]++;
4595   }
4596   //
4597   // Remove tracks corresponding to shared kink's
4598   //
4599   for (Int_t i=0;i<nentries;i++){
4600     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4601     if (!track0) continue;
4602     if (track0->fKinkIndexes[0]!=0) continue;
4603     if (shared[i]) delete array->RemoveAt(i);
4604   }
4605
4606   //
4607   //
4608   RemoveUsed2(array,0.5,0.4,30);
4609   UnsignClusters();
4610   for (Int_t i=0;i<nentries;i++){
4611     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4612     if (!track0) continue;
4613     track0->CookdEdx(0.02,0.6);
4614     track0->CookPID();
4615   }
4616   //
4617   for (Int_t i=0;i<nentries;i++){
4618     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4619     if (!track0) continue;
4620     if (track0->GetPt()<1.4) continue;
4621     //remove double high momenta tracks - overlapped with kink candidates
4622     Int_t shared=0;
4623     Int_t all   =0;
4624     for (Int_t icl=track0->fFirstPoint;icl<track0->fLastPoint; icl++){
4625       if (track0->fClusterPointer[icl]!=0){
4626         all++;
4627         if (track0->fClusterPointer[icl]->IsUsed(10)) shared++;
4628       }
4629     }
4630     if (Float_t(shared+1)/Float_t(nall+1)>0.5) {
4631       delete array->RemoveAt(i);
4632       continue;
4633     }
4634     //
4635     if (track0->fKinkIndexes[0]!=0) continue;
4636     if (track0->GetNumberOfClusters()<80) continue;
4637
4638     AliTPCseed *pmother = new AliTPCseed();
4639     AliTPCseed *pdaughter = new AliTPCseed();
4640     AliKink *pkink = new AliKink;
4641
4642     AliTPCseed & mother = *pmother;
4643     AliTPCseed & daughter = *pdaughter;
4644     AliKink & kink = *pkink;
4645     if (CheckKinkPoint(track0,mother,daughter, kink)){
4646       if (mother.fN<30||daughter.fN<20) {
4647         delete pmother;
4648         delete pdaughter;
4649         delete pkink;
4650         continue;  //too short tracks
4651       }
4652       if (mother.GetPt()<1.4) {
4653         delete pmother;
4654         delete pdaughter;
4655         delete pkink;
4656         continue;
4657       }
4658       Int_t row0= kink.GetTPCRow0();
4659       if (kink.GetDistance()>0.5 || kink.GetR()<110. || kink.GetR()>240.) {
4660         delete pmother;
4661         delete pdaughter;
4662         delete pkink;
4663         continue;
4664       }
4665       //
4666       Int_t index = esd->AddKink(&kink);      
4667       mother.fKinkIndexes[0] = -(index+1);
4668       daughter.fKinkIndexes[0] = index+1;
4669       if (mother.fN>50) {
4670         delete array->RemoveAt(i);
4671         array->AddAt(new AliTPCseed(mother),i);
4672       }
4673       else{
4674         array->AddLast(new AliTPCseed(mother));
4675       }
4676       array->AddLast(new AliTPCseed(daughter));      
4677       for (Int_t icl=0;icl<row0;icl++) {
4678         if (mother.fClusterPointer[icl]) mother.fClusterPointer[icl]->Use(20);
4679       }
4680       //
4681       for (Int_t icl=row0;icl<158;icl++) {
4682         if (daughter.fClusterPointer[icl]) daughter.fClusterPointer[icl]->Use(20);
4683       }
4684       //
4685     }
4686     delete pmother;
4687     delete pdaughter;
4688     delete pkink;
4689   }
4690
4691   delete [] daughters;
4692   delete [] mothers;
4693   //
4694   //
4695   delete [] dca;
4696   delete []circular;
4697   delete []shared;
4698   delete []quality;
4699   delete []indexes;
4700   //
4701   delete kink;
4702   delete[]fim;
4703   delete[] zm;
4704   delete[] z0;
4705   delete [] usage;
4706   delete[] alpha;
4707   delete[] nclusters;
4708   delete[] sign;
4709   delete[] helixes;
4710   kinks->Delete();
4711   delete kinks;
4712
4713   printf("Ncandidates=\t%d\t%d\t%d\t%d\n",esd->GetNumberOfKinks(),ncandidates,ntracks,nall);
4714   timer.Print();
4715 }
4716
4717 void  AliTPCtrackerMI::FindV0s(TObjArray * array, AliESD *esd)
4718 {
4719   //
4720   //  find V0s
4721   //
4722   //
4723   TObjArray *tpcv0s      = new TObjArray(100000);
4724   Int_t     nentries     = array->GetEntriesFast();
4725   AliHelix *helixes      = new AliHelix[nentries];
4726   Int_t    *sign         = new Int_t[nentries];
4727   Float_t  *alpha        = new Float_t[nentries];
4728   Float_t  *z0           = new Float_t[nentries]; 
4729   Float_t  *dca          = new Float_t[nentries];
4730   Float_t  *sdcar        = new Float_t[nentries];
4731   Float_t  *cdcar        = new Float_t[nentries];
4732   Float_t  *pulldcar     = new Float_t[nentries];
4733   Float_t  *pulldcaz     = new Float_t[nentries];
4734   Float_t  *pulldca      = new Float_t[nentries];
4735   Bool_t   *isPrim       = new Bool_t[nentries];  
4736   const AliESDVertex * primvertex = esd->GetVertex();
4737   Double_t             zvertex = primvertex->GetZv(); 
4738   //
4739   //  nentries = array->GetEntriesFast();
4740   //
4741   for (Int_t i=0;i<nentries;i++){
4742     sign[i]=0;
4743     isPrim[i]=0;
4744     AliTPCseed* track = (AliTPCseed*)array->At(i);    
4745     if (!track) continue;
4746     track->GetV0Indexes()[0] = 0;  //rest v0 indexes
4747     track->GetV0Indexes()[1] = 0;  //rest v0 indexes
4748     track->GetV0Indexes()[2] = 0;  //rest v0 indexes
4749     //
4750     alpha[i] = track->GetAlpha();
4751     new (&helixes[i]) AliHelix(*track);
4752     Double_t xyz[3];
4753     helixes[i].Evaluate(0,xyz);
4754     sign[i] = (track->GetC()>0) ? -1:1;
4755     Double_t x,y,z;
4756     x=160;
4757     z0[i]=1000;
4758     if (track->GetProlongation(0,y,z))  z0[i] = z;
4759     dca[i] = track->GetD(0,0);
4760     // 
4761     // dca error parrameterezation + pulls
4762     //
4763     sdcar[i]      = TMath::Sqrt(0.150*0.150+(100*track->GetC())*(100*track->GetC()));
4764     if (TMath::Abs(track->GetTgl())>1) sdcar[i]*=2.5;
4765     cdcar[i]      = TMath::Exp((TMath::Abs(track->GetC())-0.0106)*525.3);
4766     pulldcar[i]   = (dca[i]-cdcar[i])/sdcar[i];
4767     pulldcaz[i]   = (z0[i]-zvertex)/sdcar[i];
4768     pulldca[i]    = TMath::Sqrt(pulldcar[i]*pulldcar[i]+pulldcaz[i]*pulldcaz[i]);
4769     if (track->fTPCr[1]+track->fTPCr[2]+track->fTPCr[3]>0.5) {
4770       if (pulldca[i]<3.) isPrim[i]=kTRUE;  //pion, muon and Kaon  3 sigma cut
4771     }
4772     if (track->fTPCr[4]>0.5) {
4773       if (pulldca[i]<0.5) isPrim[i]=kTRUE;  //proton 0.5 sigma cut
4774     }
4775     if (track->fTPCr[0]>0.4) {
4776       isPrim[i]=kFALSE;  //electron no  sigma cut
4777     }
4778   }
4779   //
4780   //
4781   TStopwatch timer;
4782   timer.Start();
4783   Int_t ncandidates =0;
4784   Int_t nall =0;
4785   Int_t ntracks=0; 
4786   Double_t phase[2][2],radius[2];
4787   //
4788   //  Finf V0s loop
4789   // 
4790   //
4791   // //  
4792   TTreeSRedirector &cstream = *fDebugStreamer; 
4793   Float_t fprimvertex[3]={GetX(),GetY(),GetZ()};
4794   AliV0 vertex; 
4795   Double_t cradius0 = 10*10;
4796   Double_t cradius1 = 200*200;
4797   Double_t cdist1=3.;
4798   Double_t cdist2=4.;
4799   Double_t cpointAngle = 0.95;
4800   //
4801   Double_t delta[2]={10000,10000};
4802   for (Int_t i =0;i<nentries;i++){
4803     if (sign[i]==0) continue;
4804     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4805     if (!track0) continue;
4806     if (AliTPCReconstructor::StreamLevel()>0){
4807       cstream<<"Tracks"<<
4808         "Tr0.="<<track0<<
4809         "dca="<<dca[i]<<
4810         "z0="<<z0[i]<<
4811         "zvertex="<<zvertex<<
4812         "sdcar0="<<sdcar[i]<<
4813         "cdcar0="<<cdcar[i]<<
4814         "pulldcar0="<<pulldcar[i]<<
4815         "pulldcaz0="<<pulldcaz[i]<<
4816         "pulldca0="<<pulldca[i]<<
4817         "isPrim="<<isPrim[i]<<
4818         "\n";
4819     }
4820     //
4821     if (track0->Get1Pt()<0) continue;
4822     if (track0->GetKinkIndex(0)>0||isPrim[i]) continue;   //daughter kink
4823     //
4824     if (TMath::Abs(helixes[i].GetHelix(4))<0.000000001) continue;
4825     ntracks++;
4826     // debug output
4827     
4828     
4829     for (Int_t j =0;j<nentries;j++){
4830       AliTPCseed * track1 = (AliTPCseed*)array->At(j);
4831       if (!track1) continue;
4832       if (track1->GetKinkIndex(0)>0 || isPrim[j]) continue; //daughter kink
4833       if (sign[j]*sign[i]>0) continue; 
4834       if (TMath::Abs(helixes[j].GetHelix(4))<0.000001) continue;
4835       if (track0->fCircular+track1->fCircular>1) continue;    //circling -returning track
4836       nall++;
4837       //
4838       // DCA to prim vertex cut
4839       //
4840       //
4841       delta[0]=10000;
4842       delta[1]=10000;
4843
4844       Int_t npoints = helixes[i].GetRPHIintersections(helixes[j], phase, radius,cdist2);
4845       if (npoints<1) continue;
4846       Int_t iclosest=0;
4847       // cuts on radius      
4848       if (npoints==1){
4849         if (radius[0]<cradius0||radius[0]>cradius1) continue;
4850         helixes[i].LinearDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta[0]);
4851         if (delta[0]>cdist1) continue;
4852       }
4853       else{
4854         if (TMath::Max(radius[0],radius[1])<cradius0|| TMath::Min(radius[0],radius[1])>cradius1) continue;      
4855         helixes[i].LinearDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta[0]);    
4856         helixes[i].LinearDCA(helixes[j],phase[1][0],phase[1][1],radius[1],delta[1]);
4857         if (delta[1]<delta[0]) iclosest=1;
4858         if (delta[iclosest]>cdist1) continue;
4859       }
4860       helixes[i].ParabolicDCA(helixes[j],phase[iclosest][0],phase[iclosest][1],radius[iclosest],delta[iclosest]);
4861       if (radius[iclosest]<cradius0 || radius[iclosest]>cradius1 || delta[iclosest]>cdist1) continue;
4862       //
4863       Double_t pointAngle = helixes[i].GetPointAngle(helixes[j],phase[iclosest],fprimvertex);
4864       if (pointAngle<cpointAngle) continue;
4865       //
4866       Bool_t isGamma = kFALSE;
4867       vertex.SetP(*track0); //track0 - plus
4868       vertex.SetM(*track1); //track1 - minus
4869       vertex.Update(fprimvertex);
4870       if (track0->fTPCr[0]>0.3&&track1->fTPCr[0]>0.3&&vertex.GetAnglep()[2]<0.15) isGamma=kTRUE;              // gamma conversion candidate
4871       Double_t pointAngle2 = vertex.GetPointAngle();
4872       //continue;
4873       if (vertex.GetPointAngle()<cpointAngle && (!isGamma)) continue; // point angle cut
4874       if (vertex.GetDist2()>2&&(!isGamma)) continue;         // point angle cut
4875       Float_t sigmae     = 0.15*0.15;
4876       if (vertex.GetRr()<80) 
4877         sigmae += (sdcar[i]*sdcar[i]+sdcar[j]*sdcar[j])*(1.-vertex.GetRr()/80.)*(1.-vertex.GetRr()/80.);
4878       sigmae+= TMath::Sqrt(sigmae);
4879       if (vertex.GetDist2()/sigmae>3.&&(!isGamma)) continue; 
4880       Float_t densb0=0,densb1=0,densa0=0,densa1=0;
4881       Int_t row0 = GetRowNumber(vertex.GetRr());
4882       if (row0>15){
4883         if (vertex.GetDist2()>0.2) continue;             
4884         densb0     = track0->Density2(0,row0-5);          
4885         densb1     = track1->Density2(0,row0-5);         
4886         if (densb0>0.3|| densb1>0.3) continue;            //clusters before vertex
4887         densa0     = track0->Density2(row0+5,row0+40);    
4888         densa1     = track1->Density2(row0+5,row0+40);    
4889         if ((densa0<0.4|| densa1<0.4)&&(!isGamma)) continue;            //missing clusters after vertex
4890       }
4891       else{
4892         densa0     = track0->Density2(0,40);  //cluster density
4893         densa1     = track1->Density2(0,40);  //cluster density
4894         if ((vertex.GetRr()<80&&densa0+densa1<1.)&&(!isGamma)) continue;
4895       }
4896       vertex.SetLab(0,track0->GetLabel());
4897       vertex.SetLab(1,track1->GetLabel());
4898       vertex.SetChi2After((densa0+densa1)*0.5);
4899       vertex.SetChi2Before((densb0+densb1)*0.5);
4900       vertex.SetIndex(0,i);
4901       vertex.SetIndex(1,j);
4902       vertex.SetStatus(1); // TPC v0 candidate
4903       vertex.SetRp(track0->fTPCr);
4904       vertex.SetRm(track1->fTPCr);
4905       tpcv0s->AddLast(new AliESDV0MI(vertex));      
4906       ncandidates++;
4907       {
4908         Int_t eventNr = esd->GetEventNumber();
4909         Double_t radiusm= (delta[0]<delta[1])? TMath::Sqrt(radius[0]):TMath::Sqrt(radius[1]);  
4910         Double_t deltam= (delta[0]<delta[1])? TMath::Sqrt(delta[0]):TMath::Sqrt(delta[1]);  
4911         if (AliTPCReconstructor::StreamLevel()>0)
4912           cstream<<"V0"<<
4913           "Event="<<eventNr<<
4914           "vertex.="<<&vertex<<
4915           "Tr0.="<<track0<<
4916           "lab0="<<track0->fLab<<
4917           "Helix0.="<<&helixes[i]<<     
4918           "Tr1.="<<track1<<
4919           "lab1="<<track1->fLab<<
4920           "Helix1.="<<&helixes[j]<<
4921           "pointAngle="<<pointAngle<<
4922           "pointAngle2="<<pointAngle2<<
4923           "dca0="<<dca[i]<<
4924           "dca1="<<dca[j]<<
4925           "z0="<<z0[i]<<
4926           "z1="<<z0[j]<<
4927           "zvertex="<<zvertex<<
4928           "circular0="<<track0->fCircular<<
4929           "circular1="<<track1->fCircular<<
4930           "npoints="<<npoints<<
4931           "radius0="<<radius[0]<<
4932           "delta0="<<delta[0]<<
4933           "radius1="<<radius[1]<<
4934           "delta1="<<delta[1]<<
4935           "radiusm="<<radiusm<<
4936           "deltam="<<deltam<<
4937           "sdcar0="<<sdcar[i]<<
4938           "sdcar1="<<sdcar[j]<<
4939           "cdcar0="<<cdcar[i]<<
4940           "cdcar1="<<cdcar[j]<<
4941           "pulldcar0="<<pulldcar[i]<<
4942           "pulldcar1="<<pulldcar[j]<<
4943           "pulldcaz0="<<pulldcaz[i]<<
4944           "pulldcaz1="<<pulldcaz[j]<<
4945           "pulldca0="<<pulldca[i]<<
4946           "pulldca1="<<pulldca[j]<<
4947           "densb0="<<densb0<<
4948           "densb1="<<densb1<<
4949           "densa0="<<densa0<<
4950           "densa1="<<densa1<<
4951           "sigmae="<<sigmae<<
4952           "\n";
4953       }
4954     }
4955   }    
4956   Float_t *quality = new Float_t[ncandidates];
4957   Int_t *indexes = new Int_t[ncandidates];
4958   Int_t naccepted =0;
4959   for (Int_t i=0;i<ncandidates;i++){
4960     quality[i]     = 0; 
4961     AliESDV0MI *v0 = (AliESDV0MI*)tpcv0s->At(i);
4962     quality[i]     = 1./(1.00001-v0->GetPointAngle());   //base point angle
4963     // quality[i]    /= (0.5+v0->GetDist2());  
4964     // quality[i]    *= v0->GetChi2After();               //density factor
4965     Double_t minpulldca = TMath::Min(2.+pulldca[v0->GetIndex(0)],(2.+pulldca[v0->GetIndex(1)]) );     //pull
4966     Int_t index0 = v0->GetIndex(0);
4967     Int_t index1 = v0->GetIndex(1);
4968     AliTPCseed * track0 = (AliTPCseed*)array->At(index0);
4969     AliTPCseed * track1 = (AliTPCseed*)array->At(index1);
4970     if (track0->fTPCr[0]>0.3&&track1->fTPCr[0]>0.3&&v0->GetAnglep()[2]<0.15) quality[i]+=1000000;              // gamma conversion candidate
4971     if (track0->fTPCr[4]>0.9||track1->fTPCr[4]>0.9&&minpulldca>4) quality[i]*=10;    // lambda candidate candidate
4972   }
4973
4974   TMath::Sort(ncandidates,quality,indexes,kTRUE);
4975   //
4976   //
4977   for (Int_t i=0;i<ncandidates;i++){
4978     AliESDV0MI * v0 = (AliESDV0MI*)tpcv0s->At(indexes[i]);
4979     if (!v0) continue;
4980     Int_t index0 = v0->GetIndex(0);
4981     Int_t index1 = v0->GetIndex(1);
4982     AliTPCseed * track0 = (AliTPCseed*)array->At(index0);
4983     AliTPCseed * track1 = (AliTPCseed*)array->At(index1);
4984     if (!track0||!track1) {
4985       printf("Bug\n");
4986       continue;
4987     }
4988     Bool_t accept =kTRUE;  //default accept
4989     Int_t *v0indexes0 = track0->GetV0Indexes();
4990     Int_t *v0indexes1 = track1->GetV0Indexes();
4991     //
4992     Int_t order0 = (v0indexes0[0]!=0) ? 1:0;
4993     Int_t order1 = (v0indexes1[0]!=0) ? 1:0;    
4994     if (v0indexes0[1]!=0) order0 =2;
4995     if (v0indexes1[1]!=0) order1 =2;      
4996     //
4997     if (v0indexes0[2]!=0) {order0=3; accept=kFALSE;}
4998     if (v0indexes0[2]!=0) {order1=3; accept=kFALSE;}
4999     //
5000     AliESDV0MI * v02 = v0;
5001     if (accept){
5002       v0->SetOrder(0,order0);
5003       v0->SetOrder(1,order1);
5004       v0->SetOrder(1,order0+order1);     
5005       Int_t index = esd->AddV0MI(v0);
5006       v02 = esd->GetV0MI(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