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