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