]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCtrackerMI.cxx
c309267e82c2cd6f0b8f4473aef5906c6f6085b4
[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) && TMath::Abs(z)<fParam->GetZLength() ) 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   Int_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 (index<0) return 0;
1642   UInt_t uindex = TMath::Abs(index);
1643
1644   if (krow) {    
1645     //cl = krow.FindNearest2(y+10,z,roady,roadz,uindex);      
1646     cl = krow.FindNearest2(y,z,roady,roadz,uindex);      
1647   }
1648
1649   if (cl) t.fCurrentClusterIndex1 = krow.GetIndex(uindex);   
1650   t.fCurrentCluster  = cl;
1651
1652   return 1;
1653 }
1654
1655
1656 Int_t AliTPCtrackerMI::FollowToNextCluster(AliTPCseed & t, Int_t nr) {
1657   //-----------------------------------------------------------------
1658   // This function tries to find a track prolongation to next pad row
1659   //-----------------------------------------------------------------
1660
1661   //update error according neighborhoud
1662
1663   if (t.fCurrentCluster) {
1664     t.fRow = nr; 
1665     Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
1666     
1667     if (t.fCurrentCluster->IsUsed(10)){
1668       //
1669       //
1670       //  t.fErrorZ2*=2;
1671       //  t.fErrorY2*=2;
1672       t.fNShared++;
1673       if (t.fNShared>0.7*t.GetNumberOfClusters()) {
1674         t.fRemoval =10;
1675         return 0;
1676       }
1677     }   
1678     if (fIteration>0) accept = 0;
1679     if (accept<3)  UpdateTrack(&t,accept);  
1680  
1681   } else {
1682     if (fIteration==0){
1683       if ( ( (t.GetSigmaY2()+t.GetSigmaZ2())>0.16)&& t.GetNumberOfClusters()>18) t.fRemoval=10;      
1684       if (  t.GetChi2()/t.GetNumberOfClusters()>6 &&t.GetNumberOfClusters()>18) t.fRemoval=10;      
1685
1686       if (( (t.fNFoundable*0.5 > t.GetNumberOfClusters()) || t.fNoCluster>15)) t.fRemoval=10;
1687     }
1688   }
1689   return 1;
1690 }
1691
1692
1693
1694 //_____________________________________________________________________________
1695 Int_t AliTPCtrackerMI::FollowProlongation(AliTPCseed& t, Int_t rf, Int_t step) {
1696   //-----------------------------------------------------------------
1697   // This function tries to find a track prolongation.
1698   //-----------------------------------------------------------------
1699   Double_t xt=t.GetX();
1700   //
1701   Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
1702   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
1703   if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
1704   //
1705   t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
1706     
1707   Int_t first = GetRowNumber(xt)-1;
1708   for (Int_t nr= first; nr>=rf; nr-=step) {
1709     // update kink info
1710     if (t.GetKinkIndexes()[0]>0){
1711       for (Int_t i=0;i<3;i++){
1712         Int_t index = t.GetKinkIndexes()[i];
1713         if (index==0) break;
1714         if (index<0) continue;
1715         //
1716         AliESDkink * kink = fEvent->GetKink(index-1);
1717         if (!kink){
1718           printf("PROBLEM\n");
1719         }
1720         else{
1721           Int_t kinkrow = kink->GetTPCRow0()+2+Int_t(0.5/(0.05+kink->GetAngle(2)));
1722           if (kinkrow==nr){
1723             AliExternalTrackParam paramd(t);
1724             kink->SetDaughter(paramd);
1725             kink->SetStatus(2,5);
1726             kink->Update();
1727           }
1728         }
1729       }
1730     }
1731
1732     if (nr==80) t.UpdateReference();
1733     if (nr<fInnerSec->GetNRows()) 
1734       fSectors = fInnerSec;
1735     else
1736       fSectors = fOuterSec;
1737     if (FollowToNext(t,nr)==0) 
1738       if (!t.IsActive()) 
1739         return 0;
1740     
1741   }   
1742   return 1;
1743 }
1744
1745
1746 //_____________________________________________________________________________
1747 Int_t AliTPCtrackerMI::FollowProlongationFast(AliTPCseed& t, Int_t rf, Int_t step) {
1748   //-----------------------------------------------------------------
1749   // This function tries to find a track prolongation.
1750   //-----------------------------------------------------------------
1751   Double_t xt=t.GetX();
1752   //
1753   Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
1754   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
1755   if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
1756   t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
1757     
1758   for (Int_t nr=GetRowNumber(xt)-1; nr>=rf; nr-=step) {
1759     
1760     if (FollowToNextFast(t,nr)==0) 
1761       if (!t.IsActive()) return 0;
1762     
1763   }   
1764   return 1;
1765 }
1766
1767
1768
1769
1770
1771 Int_t AliTPCtrackerMI::FollowBackProlongation(AliTPCseed& t, Int_t rf) {
1772   //-----------------------------------------------------------------
1773   // This function tries to find a track prolongation.
1774   //-----------------------------------------------------------------
1775   //
1776   Double_t xt=t.GetX();  
1777   Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
1778   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
1779   if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
1780   t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
1781     
1782   Int_t first = t.fFirstPoint;
1783   if (first<GetRowNumber(xt)+1) first = GetRowNumber(xt)+1;
1784   //
1785   if (first<0) first=0;
1786   for (Int_t nr=first; nr<=rf; nr++) {
1787     if ( (TMath::Abs(t.GetSnp())>0.95)) break;
1788     if (t.GetKinkIndexes()[0]<0){
1789       for (Int_t i=0;i<3;i++){
1790         Int_t index = t.GetKinkIndexes()[i];
1791         if (index==0) break;
1792         if (index>0) continue;
1793         index = TMath::Abs(index);
1794         AliESDkink * kink = fEvent->GetKink(index-1);
1795         if (!kink){
1796           printf("PROBLEM\n");
1797         }
1798         else{
1799           Int_t kinkrow = kink->GetTPCRow0()-2-Int_t(0.5/(0.05+kink->GetAngle(2)));
1800           if (kinkrow==nr){
1801             AliExternalTrackParam paramm(t);
1802             kink->SetMother(paramm);
1803             kink->SetStatus(2,1);
1804             kink->Update();
1805           }
1806         }
1807       }      
1808     }
1809     //
1810     if (nr<fInnerSec->GetNRows()) 
1811       fSectors = fInnerSec;
1812     else
1813       fSectors = fOuterSec;
1814     FollowToNext(t,nr);                                                             
1815   }   
1816   return 1;
1817 }
1818
1819
1820
1821
1822    
1823 Float_t AliTPCtrackerMI::OverlapFactor(AliTPCseed * s1, AliTPCseed * s2, Int_t &sum1, Int_t & sum2)
1824 {
1825   //
1826   //
1827   sum1=0;
1828   sum2=0;
1829   Int_t sum=0;
1830   //
1831   Float_t dz2 =(s1->GetZ() - s2->GetZ());
1832   dz2*=dz2;  
1833
1834   Float_t dy2 =TMath::Abs((s1->GetY() - s2->GetY()));
1835   dy2*=dy2;
1836   Float_t distance = TMath::Sqrt(dz2+dy2);
1837   if (distance>4.) return 0; // if there are far away  - not overlap - to reduce combinatorics
1838  
1839   //  Int_t offset =0;
1840   Int_t firstpoint = TMath::Min(s1->fFirstPoint,s2->fFirstPoint);
1841   Int_t lastpoint = TMath::Max(s1->fLastPoint,s2->fLastPoint);
1842   if (lastpoint>160) 
1843     lastpoint =160;
1844   if (firstpoint<0) 
1845     firstpoint = 0;
1846   if (firstpoint>lastpoint) {
1847     firstpoint =lastpoint;
1848     //    lastpoint  =160;
1849   }
1850     
1851   
1852   for (Int_t i=firstpoint-1;i<lastpoint+1;i++){
1853     if (s1->GetClusterIndex2(i)>0) sum1++;
1854     if (s2->GetClusterIndex2(i)>0) sum2++;
1855     if (s1->GetClusterIndex2(i)==s2->GetClusterIndex2(i) && s1->GetClusterIndex2(i)>0) {
1856       sum++;
1857     }
1858   }
1859   if (sum<5) return 0;
1860
1861   Float_t summin = TMath::Min(sum1+1,sum2+1);
1862   Float_t ratio = (sum+1)/Float_t(summin);
1863   return ratio;
1864 }
1865
1866 void  AliTPCtrackerMI::SignShared(AliTPCseed * s1, AliTPCseed * s2)
1867 {
1868   //
1869   //
1870   if (TMath::Abs(s1->GetC()-s2->GetC())>0.004) return;
1871   if (TMath::Abs(s1->GetTgl()-s2->GetTgl())>0.6) return;
1872
1873   Float_t dz2 =(s1->GetZ() - s2->GetZ());
1874   dz2*=dz2;
1875   Float_t dy2 =(s1->GetY() - s2->GetY());
1876   dy2*=dy2;
1877   Float_t distance = dz2+dy2;
1878   if (distance>325.) return ; // if there are far away  - not overlap - to reduce combinatorics
1879   
1880   //
1881   Int_t sumshared=0;
1882   //
1883   Int_t firstpoint = TMath::Max(s1->fFirstPoint,s2->fFirstPoint);
1884   Int_t lastpoint = TMath::Min(s1->fLastPoint,s2->fLastPoint);
1885   //
1886   if (firstpoint>=lastpoint-5) return;;
1887
1888   for (Int_t i=firstpoint;i<lastpoint;i++){
1889     //    if ( (s1->GetClusterIndex2(i)&0xFFFF8FFF)==(s2->GetClusterIndex2(i)&0xFFFF8FFF) && s1->GetClusterIndex2(i)>0) {
1890     if ( (s1->GetClusterIndex2(i))==(s2->GetClusterIndex2(i)) && s1->GetClusterIndex2(i)>0) {
1891       sumshared++;
1892     }
1893   }
1894   if (sumshared>4){
1895     // sign clusters
1896     //
1897     for (Int_t i=firstpoint;i<lastpoint;i++){
1898       //      if ( (s1->GetClusterIndex2(i)&0xFFFF8FFF)==(s2->GetClusterIndex2(i)&0xFFFF8FFF) && s1->GetClusterIndex2(i)>0) {
1899       if ( (s1->GetClusterIndex2(i))==(s2->GetClusterIndex2(i)) && s1->GetClusterIndex2(i)>0) {
1900         AliTPCTrackerPoint *p1  = s1->GetTrackPoint(i);
1901         AliTPCTrackerPoint *p2  = s2->GetTrackPoint(i);; 
1902         if (s1->IsActive()&&s2->IsActive()){
1903           p1->fIsShared = kTRUE;
1904           p2->fIsShared = kTRUE;
1905         }       
1906       }
1907     }
1908   }
1909   //  
1910   if (sumshared>10){
1911     for (Int_t i=0;i<4;i++){
1912       if (s1->fOverlapLabels[3*i]==0){
1913         s1->fOverlapLabels[3*i] = s2->GetLabel();
1914         s1->fOverlapLabels[3*i+1] = sumshared;
1915         s1->fOverlapLabels[3*i+2] = s2->GetUniqueID();
1916         break;
1917       } 
1918     }
1919     for (Int_t i=0;i<4;i++){
1920       if (s2->fOverlapLabels[3*i]==0){
1921         s2->fOverlapLabels[3*i] = s1->GetLabel();
1922         s2->fOverlapLabels[3*i+1] = sumshared;
1923         s2->fOverlapLabels[3*i+2] = s1->GetUniqueID();
1924         break;
1925       } 
1926     }    
1927   }
1928   
1929 }
1930
1931 void  AliTPCtrackerMI::SignShared(TObjArray * arr)
1932 {
1933   //
1934   //sort trackss according sectors
1935   //  
1936   for (Int_t i=0; i<arr->GetEntriesFast(); i++) {
1937     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1938     if (!pt) continue;
1939     //if (pt) RotateToLocal(pt);
1940     pt->fSort = 0;
1941   }
1942   arr->UnSort();
1943   arr->Sort();  // sorting according z
1944   arr->Expand(arr->GetEntries());
1945   //
1946   //
1947   Int_t nseed=arr->GetEntriesFast();
1948   for (Int_t i=0; i<nseed; i++) {
1949     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1950     if (!pt) continue;
1951     for (Int_t j=0;j<=12;j++){
1952       pt->fOverlapLabels[j] =0;
1953     }
1954   }
1955   for (Int_t i=0; i<nseed; i++) {
1956     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1957     if (!pt) continue;
1958     if (pt->fRemoval>10) continue;
1959     for (Int_t j=i+1; j<nseed; j++){
1960       AliTPCseed *pt2=(AliTPCseed*)arr->UncheckedAt(j);
1961       //      if (pt2){
1962       if (pt2->fRemoval<=10) {
1963         if ( TMath::Abs(pt->fRelativeSector-pt2->fRelativeSector)>0) break;
1964         SignShared(pt,pt2);
1965       }
1966     }  
1967   }
1968 }
1969
1970 void  AliTPCtrackerMI::RemoveDouble(TObjArray * arr, Float_t factor1, Float_t factor2,  Int_t removalindex)
1971 {
1972   //
1973   //sort trackss according sectors
1974   //
1975   if (fDebug&1) {
1976     Info("RemoveDouble","Number of tracks before double removal- %d\n",arr->GetEntries());
1977   }
1978   //
1979   for (Int_t i=0; i<arr->GetEntriesFast(); i++) {
1980     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1981     if (!pt) continue;
1982     pt->fSort = 0;
1983   }
1984   arr->UnSort();
1985   arr->Sort();  // sorting according z
1986   arr->Expand(arr->GetEntries());
1987   //
1988   //reset overlap labels
1989   //
1990   Int_t nseed=arr->GetEntriesFast();
1991   for (Int_t i=0; i<nseed; i++) {
1992     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1993     if (!pt) continue;
1994     pt->SetUniqueID(i);
1995     for (Int_t j=0;j<=12;j++){
1996       pt->fOverlapLabels[j] =0;
1997     }
1998   }
1999   //
2000   //sign shared tracks
2001   for (Int_t i=0; i<nseed; i++) {
2002     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2003     if (!pt) continue;
2004     if (pt->fRemoval>10) continue;
2005     Float_t deltac = pt->GetC()*0.1;
2006     for (Int_t j=i+1; j<nseed; j++){
2007       AliTPCseed *pt2=(AliTPCseed*)arr->UncheckedAt(j);
2008       //      if (pt2){
2009       if (pt2->fRemoval<=10) {
2010         if ( TMath::Abs(pt->fRelativeSector-pt2->fRelativeSector)>0) break;
2011         if (TMath::Abs(pt->GetC()  -pt2->GetC())>deltac) continue;
2012         if (TMath::Abs(pt->GetTgl()-pt2->GetTgl())>0.05) continue;
2013         //
2014         SignShared(pt,pt2);
2015       }
2016     }
2017   }
2018   //
2019   // remove highly shared tracks
2020   for (Int_t i=0; i<nseed; i++) {
2021     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2022     if (!pt) continue;
2023     if (pt->fRemoval>10) continue;
2024     //
2025     Int_t sumshared =0;
2026     for (Int_t j=0;j<4;j++){
2027       sumshared = pt->fOverlapLabels[j*3+1];      
2028     }
2029     Float_t factor = factor1;
2030     if (pt->fRemoval>0) factor = factor2;
2031     if (sumshared/pt->GetNumberOfClusters()>factor){
2032       for (Int_t j=0;j<4;j++){
2033         if (pt->fOverlapLabels[3*j]==0) continue;
2034         if (pt->fOverlapLabels[3*j+1]<5) continue; 
2035         if (pt->fRemoval==removalindex) continue;      
2036         AliTPCseed * pt2 = (AliTPCseed*)arr->UncheckedAt(pt->fOverlapLabels[3*j+2]);
2037         if (!pt2) continue;
2038         if (pt2->GetSigma2C()<pt->GetSigma2C()){
2039           //      pt->fRemoval = removalindex;
2040           delete arr->RemoveAt(i);        
2041           break;
2042         }
2043       }      
2044     }
2045   }
2046   arr->Compress();
2047   if (fDebug&1) {
2048     Info("RemoveDouble","Number of tracks after double removal- %d\n",arr->GetEntries());
2049   }
2050 }
2051
2052
2053
2054
2055
2056
2057 void AliTPCtrackerMI::SortTracks(TObjArray * arr, Int_t mode) const
2058 {
2059   //
2060   //sort tracks in array according mode criteria
2061   Int_t nseed = arr->GetEntriesFast();    
2062   for (Int_t i=0; i<nseed; i++) {
2063     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2064     if (!pt) {
2065       continue;
2066     }
2067     pt->fSort = mode;
2068   }
2069   arr->UnSort();
2070   arr->Sort();
2071 }
2072
2073 void AliTPCtrackerMI::RemoveUsed(TObjArray * arr, Float_t factor1,  Float_t factor2, Int_t removalindex)
2074 {
2075
2076   //Loop over all tracks and remove "overlaps"
2077   //
2078   //
2079   Int_t nseed = arr->GetEntriesFast();  
2080   Int_t good =0;
2081
2082   for (Int_t i=0; i<nseed; i++) {
2083     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2084     if (!pt) {
2085       delete arr->RemoveAt(i);
2086     }
2087     else{
2088       pt->fSort =1;
2089       pt->fBSigned = kFALSE;
2090     }
2091   }
2092   arr->Compress();
2093   nseed = arr->GetEntriesFast();
2094   arr->UnSort();
2095   arr->Sort();
2096   //
2097   //unsign used
2098   UnsignClusters();
2099   //
2100   for (Int_t i=0; i<nseed; i++) {
2101     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2102     if (!pt) {
2103       continue;
2104     }    
2105     Int_t found,foundable,shared;
2106     if (pt->IsActive()) 
2107       pt->GetClusterStatistic(0,160,found, foundable,shared,kFALSE);
2108     else
2109       pt->GetClusterStatistic(0,160,found, foundable,shared,kTRUE); 
2110     //
2111     Double_t factor = factor2;
2112     if (pt->fBConstrain) factor = factor1;
2113
2114     if ((Float_t(shared)/Float_t(found))>factor){
2115       pt->Desactivate(removalindex);
2116       continue;
2117     }
2118
2119     good++;
2120     for (Int_t i=0; i<160; i++) {
2121       Int_t index=pt->GetClusterIndex2(i);
2122       if (index<0 || index&0x8000 ) continue;
2123       AliTPCclusterMI *c= pt->fClusterPointer[i];        
2124       if (!c) continue;
2125       //      if (!c->IsUsed(10)) c->Use(10);
2126       //if (pt->IsActive()) 
2127       c->Use(10);  
2128       //else
2129       //        c->Use(5);
2130     }
2131     
2132   }
2133   fNtracks = good;
2134   if (fDebug>0){
2135     Info("RemoveUsed","\n*****\nNumber of good tracks after shared removal\t%d\n",fNtracks);
2136   }
2137 }
2138
2139
2140 void AliTPCtrackerMI::RemoveUsed2(TObjArray * arr, Float_t factor1,  Float_t factor2, Int_t minimal)
2141 {
2142
2143   //Loop over all tracks and remove "overlaps"
2144   //
2145   //
2146   UnsignClusters();
2147   //
2148   Int_t nseed = arr->GetEntriesFast();  
2149   Float_t * quality = new Float_t[nseed];
2150   Int_t   * indexes = new Int_t[nseed];
2151   Int_t good =0;
2152   //
2153   //
2154   for (Int_t i=0; i<nseed; i++) {
2155     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2156     if (!pt){
2157       quality[i]=-1;
2158       continue;
2159     }
2160     pt->UpdatePoints();    //select first last max dens points
2161     Float_t * points = pt->GetPoints();
2162     if (points[3]<0.8) quality[i] =-1;
2163     //
2164     quality[i] = (points[2]-points[0])+pt->GetNumberOfClusters();
2165   }
2166   TMath::Sort(nseed,quality,indexes);
2167   //
2168   //
2169   for (Int_t itrack=0; itrack<nseed; itrack++) {
2170     Int_t trackindex = indexes[itrack];
2171     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(trackindex);    
2172     if (quality[trackindex]<0){
2173       if (pt) {
2174         delete arr->RemoveAt(trackindex);
2175       }
2176       else{
2177         arr->RemoveAt(trackindex);
2178       }
2179       continue;
2180     }
2181     //
2182     Int_t first = Int_t(pt->GetPoints()[0]);
2183     Int_t last  = Int_t(pt->GetPoints()[2]);
2184     Double_t factor = (pt->fBConstrain) ? factor1: factor2;
2185     //
2186     Int_t found,foundable,shared;
2187     pt->GetClusterStatistic(first,last, found, foundable,shared,kFALSE);
2188     Float_t sharedfactor = Float_t(shared+1)/Float_t(found+1);
2189     //
2190     if (Float_t(shared+1)/Float_t(found+1)>factor){
2191       if (pt->GetKinkIndexes()[0]!=0) continue;  //don't remove tracks  - part of the kinks
2192       delete arr->RemoveAt(trackindex);
2193       continue;
2194     }
2195
2196     if (pt->GetNumberOfClusters()<50&&(found-0.5*shared)<minimal){  //remove short tracks
2197       if (pt->GetKinkIndexes()[0]!=0) continue;  //don't remove tracks  - part of the kinks
2198       delete arr->RemoveAt(trackindex);
2199       continue;
2200     }
2201
2202     good++;
2203     if (sharedfactor>0.4) continue;
2204     for (Int_t i=first; i<last; i++) {
2205       Int_t index=pt->GetClusterIndex2(i);
2206       // if (index<0 || index&0x8000 ) continue;
2207       if (index<0 || index&0x8000 ) continue;
2208       AliTPCclusterMI *c= pt->fClusterPointer[i];        
2209       if (!c) continue;
2210       c->Use(10);  
2211     }    
2212   }
2213   fNtracks = good;
2214   if (fDebug>0){
2215     Info("RemoveUsed","\n*****\nNumber of good tracks after shared removal\t%d\n",fNtracks);
2216   }
2217   delete []quality;
2218   delete []indexes;
2219 }
2220
2221 void AliTPCtrackerMI::UnsignClusters() 
2222 {
2223   //
2224   // loop over all clusters and unsign them
2225   //
2226   
2227   for (Int_t sec=0;sec<fkNIS;sec++){
2228     for (Int_t row=0;row<fInnerSec->GetNRows();row++){
2229       AliTPCclusterMI *cl = fInnerSec[sec][row].fClusters1;
2230       for (Int_t icl =0;icl< fInnerSec[sec][row].fN1;icl++)
2231         //      if (cl[icl].IsUsed(10))         
2232         cl[icl].Use(-1);
2233       cl = fInnerSec[sec][row].fClusters2;
2234       for (Int_t icl =0;icl< fInnerSec[sec][row].fN2;icl++)
2235         //if (cl[icl].IsUsed(10))       
2236           cl[icl].Use(-1);      
2237     }
2238   }
2239   
2240   for (Int_t sec=0;sec<fkNOS;sec++){
2241     for (Int_t row=0;row<fOuterSec->GetNRows();row++){
2242       AliTPCclusterMI *cl = fOuterSec[sec][row].fClusters1;
2243       for (Int_t icl =0;icl< fOuterSec[sec][row].fN1;icl++)
2244         //if (cl[icl].IsUsed(10))       
2245           cl[icl].Use(-1);
2246       cl = fOuterSec[sec][row].fClusters2;
2247       for (Int_t icl =0;icl< fOuterSec[sec][row].fN2;icl++)
2248         //if (cl[icl].IsUsed(10))       
2249         cl[icl].Use(-1);      
2250     }
2251   }
2252   
2253 }
2254
2255
2256
2257 void AliTPCtrackerMI::SignClusters(TObjArray * arr, Float_t fnumber, Float_t fdensity)
2258 {
2259   //
2260   //sign clusters to be "used"
2261   //
2262   // snumber and sdensity sign number of sigmas - bellow mean value to be accepted
2263   // loop over "primaries"
2264   
2265   Float_t sumdens=0;
2266   Float_t sumdens2=0;
2267   Float_t sumn   =0;
2268   Float_t sumn2  =0;
2269   Float_t sumchi =0;
2270   Float_t sumchi2 =0;
2271
2272   Float_t sum    =0;
2273
2274   TStopwatch timer;
2275   timer.Start();
2276
2277   Int_t nseed = arr->GetEntriesFast();
2278   for (Int_t i=0; i<nseed; i++) {
2279     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2280     if (!pt) {
2281       continue;
2282     }    
2283     if (!(pt->IsActive())) continue;
2284     Float_t dens = pt->GetNumberOfClusters()/Float_t(pt->fNFoundable);
2285     if ( (dens>0.7) && (pt->GetNumberOfClusters()>70)){
2286       sumdens += dens;
2287       sumdens2+= dens*dens;
2288       sumn    += pt->GetNumberOfClusters();
2289       sumn2   += pt->GetNumberOfClusters()*pt->GetNumberOfClusters();
2290       Float_t chi2 = pt->GetChi2()/pt->GetNumberOfClusters();
2291       if (chi2>5) chi2=5;
2292       sumchi  +=chi2;
2293       sumchi2 +=chi2*chi2;
2294       sum++;
2295     }
2296   }
2297
2298   Float_t mdensity = 0.9;
2299   Float_t meann    = 130;
2300   Float_t meanchi  = 1;
2301   Float_t sdensity = 0.1;
2302   Float_t smeann    = 10;
2303   Float_t smeanchi  =0.4;
2304   
2305
2306   if (sum>20){
2307     mdensity = sumdens/sum;
2308     meann    = sumn/sum;
2309     meanchi  = sumchi/sum;
2310     //
2311     sdensity = sumdens2/sum-mdensity*mdensity;
2312     sdensity = TMath::Sqrt(sdensity);
2313     //
2314     smeann   = sumn2/sum-meann*meann;
2315     smeann   = TMath::Sqrt(smeann);
2316     //
2317     smeanchi = sumchi2/sum - meanchi*meanchi;
2318     smeanchi = TMath::Sqrt(smeanchi);
2319   }
2320
2321
2322   //REMOVE  SHORT DELTAS or tracks going out of sensitive volume of TPC
2323   //
2324   for (Int_t i=0; i<nseed; i++) {
2325     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2326     if (!pt) {
2327       continue;
2328     }
2329     if (pt->fBSigned) continue;
2330     if (pt->fBConstrain) continue;    
2331     //if (!(pt->IsActive())) continue;
2332     /*
2333     Int_t found,foundable,shared;    
2334     pt->GetClusterStatistic(0,160,found, foundable,shared);
2335     if (shared/float(found)>0.3) {
2336       if (shared/float(found)>0.9 ){
2337         //delete arr->RemoveAt(i);
2338       }
2339       continue;
2340     }
2341     */
2342     Bool_t isok =kFALSE;
2343     if ( (pt->fNShared/pt->GetNumberOfClusters()<0.5) &&pt->GetNumberOfClusters()>60)
2344       isok = kTRUE;
2345     if ((TMath::Abs(1/pt->GetC())<100.) && (pt->fNShared/pt->GetNumberOfClusters()<0.7))
2346       isok =kTRUE;
2347     if  (TMath::Abs(pt->GetZ()/pt->GetX())>1.1)
2348       isok =kTRUE;
2349     if ( (TMath::Abs(pt->GetSnp()>0.7) && pt->GetD(0,0)>60.))
2350       isok =kTRUE;
2351     
2352     if (isok)     
2353       for (Int_t i=0; i<160; i++) {     
2354         Int_t index=pt->GetClusterIndex2(i);
2355         if (index<0) continue;
2356         AliTPCclusterMI *c= pt->fClusterPointer[i];
2357         if (!c) continue;
2358         //if (!(c->IsUsed(10))) c->Use();  
2359         c->Use(10);  
2360       }
2361   }
2362   
2363   
2364   //
2365   Double_t maxchi  = meanchi+2.*smeanchi;
2366
2367   for (Int_t i=0; i<nseed; i++) {
2368     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2369     if (!pt) {
2370       continue;
2371     }    
2372     //if (!(pt->IsActive())) continue;
2373     if (pt->fBSigned) continue;
2374     Double_t chi     = pt->GetChi2()/pt->GetNumberOfClusters();
2375     if (chi>maxchi) continue;
2376
2377     Float_t bfactor=1;
2378     Float_t dens = pt->GetNumberOfClusters()/Float_t(pt->fNFoundable);
2379    
2380     //sign only tracks with enoug big density at the beginning
2381     
2382     if ((pt->GetDensityFirst(40)<0.75) && pt->GetNumberOfClusters()<meann) continue; 
2383     
2384     
2385     Double_t mindens = TMath::Max(double(mdensity-sdensity*fdensity*bfactor),0.65);
2386     Double_t minn    = TMath::Max(Int_t(meann-fnumber*smeann*bfactor),50);
2387    
2388     //    if (pt->fBConstrain) mindens = TMath::Max(mdensity-sdensity*fdensity*bfactor,0.65);
2389     if ( (pt->fRemoval==10) && (pt->GetSnp()>0.8)&&(dens>mindens))
2390       minn=0;
2391
2392     if ((dens>mindens && pt->GetNumberOfClusters()>minn) && chi<maxchi ){
2393       //Int_t noc=pt->GetNumberOfClusters();
2394       pt->fBSigned = kTRUE;
2395       for (Int_t i=0; i<160; i++) {
2396
2397         Int_t index=pt->GetClusterIndex2(i);
2398         if (index<0) continue;
2399         AliTPCclusterMI *c= pt->fClusterPointer[i];
2400         if (!c) continue;
2401         //      if (!(c->IsUsed(10))) c->Use();  
2402         c->Use(10);  
2403       }
2404     }
2405   }
2406   //  gLastCheck = nseed;
2407   //  arr->Compress();
2408   if (fDebug>0){
2409     timer.Print();
2410   }
2411 }
2412
2413
2414 void  AliTPCtrackerMI::StopNotActive(TObjArray * arr, Int_t row0, Float_t th0, Float_t th1, Float_t th2) const
2415 {
2416   // stop not active tracks
2417   // take th1 as threshold for number of founded to number of foundable on last 10 active rows
2418   // take th2 as threshold for number of founded to number of foundable on last 20 active rows 
2419   Int_t nseed = arr->GetEntriesFast();  
2420   //
2421   for (Int_t i=0; i<nseed; i++) {
2422     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2423     if (!pt) {
2424       continue;
2425     }
2426     if (!(pt->IsActive())) continue;
2427     StopNotActive(pt,row0,th0, th1,th2);
2428   }
2429 }
2430
2431
2432
2433 void  AliTPCtrackerMI::StopNotActive(AliTPCseed * seed, Int_t row0, Float_t th0, Float_t th1,
2434  Float_t th2) const
2435 {
2436   // stop not active tracks
2437   // take th1 as threshold for number of founded to number of foundable on last 10 active rows
2438   // take th2 as threshold for number of founded to number of foundable on last 20 active rows 
2439   Int_t sumgood1  = 0;
2440   Int_t sumgood2  = 0;
2441   Int_t foundable = 0;
2442   Int_t maxindex = seed->fLastPoint;  //last foundable row
2443   if (seed->fNFoundable*th0 > seed->GetNumberOfClusters()) {
2444     seed->Desactivate(10) ;
2445     return;
2446   }
2447
2448   for (Int_t i=row0; i<maxindex; i++){
2449     Int_t index = seed->GetClusterIndex2(i);
2450     if (index!=-1) foundable++;
2451     //if (!c) continue;
2452     if (foundable<=30) sumgood1++;
2453     if (foundable<=50) {
2454       sumgood2++;
2455     }
2456     else{ 
2457       break;
2458     }        
2459   }
2460   if (foundable>=30.){ 
2461      if (sumgood1<(th1*30.)) seed->Desactivate(10);
2462   }
2463   if (foundable>=50)
2464     if (sumgood2<(th2*50.)) seed->Desactivate(10);
2465 }
2466
2467
2468 Int_t AliTPCtrackerMI::RefitInward(AliESD *event)
2469 {
2470   //
2471   // back propagation of ESD tracks
2472   //
2473   //return 0;
2474   fEvent = event;
2475   ReadSeeds(event,2);
2476   fIteration=2;
2477   //PrepareForProlongation(fSeeds,1);
2478   PropagateForward2(fSeeds);
2479   Int_t ntracks=0;
2480   Int_t nseed = fSeeds->GetEntriesFast();
2481   for (Int_t i=0;i<nseed;i++){
2482     AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
2483     if (!seed) continue;
2484     if (seed->GetKinkIndex(0)>0)  UpdateKinkQualityD(seed);  // update quality informations for kinks
2485
2486     seed->PropagateTo(fParam->GetInnerRadiusLow());
2487     seed->UpdatePoints();
2488     AliESDtrack *esd=event->GetTrack(i);
2489     seed->CookdEdx(0.02,0.6);
2490     CookLabel(seed,0.1); //For comparison only
2491     if (seed->GetNumberOfClusters()>15){
2492       esd->UpdateTrackParams(seed,AliESDtrack::kTPCrefit); 
2493       esd->SetTPCPoints(seed->GetPoints());
2494       ntracks++;
2495     }
2496     else{
2497       //printf("problem\n");
2498     }
2499   }
2500   //FindKinks(fSeeds,event);
2501   Info("RefitInward","Number of refitted tracks %d",ntracks);
2502   fEvent =0;
2503   //WriteTracks();
2504   return 0;
2505 }
2506
2507
2508 Int_t AliTPCtrackerMI::PropagateBack(AliESD *event)
2509 {
2510   //
2511   // back propagation of ESD tracks
2512   //
2513
2514   fEvent = event;
2515   fIteration = 1;
2516   ReadSeeds(event,0);
2517   PropagateBack(fSeeds);
2518   Int_t nseed = fSeeds->GetEntriesFast();
2519   Int_t ntracks=0;
2520   for (Int_t i=0;i<nseed;i++){
2521     AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
2522     if (!seed) continue;
2523     if (seed->GetKinkIndex(0)<0)  UpdateKinkQualityM(seed);  // update quality informations for kinks
2524     seed->UpdatePoints();
2525     AliESDtrack *esd=event->GetTrack(i);
2526     seed->CookdEdx(0.02,0.6);
2527     CookLabel(seed,0.1); //For comparison only
2528     if (seed->GetNumberOfClusters()>15){
2529       esd->UpdateTrackParams(seed,AliESDtrack::kTPCout);
2530       esd->SetTPCPoints(seed->GetPoints());
2531       ntracks++;
2532     }
2533   }
2534   //FindKinks(fSeeds,event);
2535   Info("PropagateBack","Number of back propagated tracks %d",ntracks);
2536   fEvent =0;
2537   //WriteTracks();
2538   return 0;
2539 }
2540
2541
2542 void AliTPCtrackerMI::DeleteSeeds()
2543 {
2544   //
2545   //delete Seeds
2546   Int_t nseed = fSeeds->GetEntriesFast();
2547   for (Int_t i=0;i<nseed;i++){
2548     AliTPCseed * seed = (AliTPCseed*)fSeeds->At(i);
2549     if (seed) delete fSeeds->RemoveAt(i);
2550   }
2551   delete fSeeds;
2552   fSeeds =0;
2553 }
2554
2555 void AliTPCtrackerMI::ReadSeeds(AliESD *event, Int_t direction)
2556 {
2557   //
2558   //read seeds from the event
2559   
2560   Int_t nentr=event->GetNumberOfTracks();
2561   if (fDebug>0){
2562     Info("ReadSeeds", "Number of ESD tracks: %d\n", nentr);
2563   }
2564   if (fSeeds) 
2565     DeleteSeeds();
2566   if (!fSeeds){   
2567     fSeeds = new TObjArray(nentr);
2568   }
2569   UnsignClusters();
2570   //  Int_t ntrk=0;  
2571   for (Int_t i=0; i<nentr; i++) {
2572     AliESDtrack *esd=event->GetTrack(i);
2573     ULong_t status=esd->GetStatus();
2574     if (!(status&AliESDtrack::kTPCin)) continue;
2575     AliTPCtrack t(*esd);
2576     //    AliTPCseed *seed = new AliTPCseed(t,t.GetAlpha());
2577     AliTPCseed *seed = new AliTPCseed(t/*,t.GetAlpha()*/);
2578     for (Int_t ikink=0;ikink<3;ikink++) {
2579       Int_t index = esd->GetKinkIndex(ikink);
2580       seed->GetKinkIndexes()[ikink] = index;
2581       if (index==0) continue;
2582       index = TMath::Abs(index);
2583       AliESDkink * kink = fEvent->GetKink(index-1);
2584       if (kink&&esd->GetKinkIndex(ikink)<0){
2585         if ((status & AliESDtrack::kTRDrefit) != 0) kink->SetStatus(1,2);
2586         if ((status & AliESDtrack::kITSout) != 0)   kink->SetStatus(1,0);
2587       }
2588       if (kink&&esd->GetKinkIndex(ikink)>0){
2589         if ((status & AliESDtrack::kTRDrefit) != 0) kink->SetStatus(1,6);
2590         if ((status & AliESDtrack::kITSout) != 0)   kink->SetStatus(1,4);
2591       }
2592
2593     }
2594     if ((status==AliESDtrack::kTPCin)&&(direction==1)) seed->ResetCovariance(); 
2595     if ( direction ==2 &&(status & AliESDtrack::kTRDrefit) == 0 ) seed->ResetCovariance();
2596     if ( direction ==2 && ((status & AliESDtrack::kTPCout) == 0) ) {
2597       fSeeds->AddAt(0,i);
2598       delete seed;
2599       continue;    
2600     }
2601     if ( direction ==2 &&(status & AliESDtrack::kTRDrefit) > 0 )  {
2602       Double_t par0[5],par1[5],x;
2603       esd->GetInnerExternalParameters(x,par0);
2604       esd->GetExternalParameters(x,par1);
2605       Double_t delta1 = TMath::Abs(par0[4]-par1[4])/(0.000000001+TMath::Abs(par0[4]+par1[4]));
2606       Double_t delta2 = TMath::Abs(par0[3]-par1[3]);
2607       Double_t trdchi2=0;
2608       if (esd->GetTRDncls()>0) trdchi2 = esd->GetTRDchi2()/esd->GetTRDncls();
2609       //reset covariance if suspicious 
2610       if ( (delta1>0.1) || (delta2>0.006) ||trdchi2>7.)
2611         seed->ResetCovariance();
2612     }
2613
2614     //
2615     //
2616     // rotate to the local coordinate system
2617     //   
2618     fSectors=fInnerSec; fN=fkNIS;    
2619     Double_t alpha=seed->GetAlpha() - fSectors->GetAlphaShift();
2620     if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
2621     if (alpha < 0.            ) alpha += 2.*TMath::Pi();
2622     Int_t ns=Int_t(alpha/fSectors->GetAlpha())%fN;
2623     alpha =ns*fSectors->GetAlpha() + fSectors->GetAlphaShift();
2624     if (alpha<-TMath::Pi()) alpha += 2*TMath::Pi();
2625     if (alpha>=TMath::Pi()) alpha -= 2*TMath::Pi();
2626     alpha-=seed->GetAlpha();  
2627     if (!seed->Rotate(alpha)) {
2628       delete seed;
2629       continue;
2630     }
2631     seed->fEsd = esd;
2632     // sign clusters
2633     for (Int_t irow=0;irow<160;irow++){
2634       Int_t index = seed->GetClusterIndex2(irow);    
2635       if (index>0){ 
2636         //
2637         AliTPCclusterMI * cl = GetClusterMI(index);
2638         seed->fClusterPointer[irow] = cl;
2639         if (cl){
2640           if ((index & 0x8000)==0){
2641             cl->Use(10);  // accepted cluster     
2642           }else{
2643             cl->Use(6);   // close cluster not accepted
2644           }     
2645         }else{
2646            Info("ReadSeeds","Not found cluster");
2647         }
2648       }
2649     }
2650     fSeeds->AddAt(seed,i);
2651   }
2652 }
2653
2654
2655
2656 //_____________________________________________________________________________
2657 void AliTPCtrackerMI::MakeSeeds3(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,  Float_t cuts[4],
2658                                  Float_t deltay, Int_t ddsec) {
2659   //-----------------------------------------------------------------
2660   // This function creates track seeds.
2661   // SEEDING WITH VERTEX CONSTRAIN 
2662   //-----------------------------------------------------------------
2663   // cuts[0]   - fP4 cut
2664   // cuts[1]   - tan(phi)  cut
2665   // cuts[2]   - zvertex cut
2666   // cuts[3]   - fP3 cut
2667   Int_t nin0  = 0;
2668   Int_t nin1  = 0;
2669   Int_t nin2  = 0;
2670   Int_t nin   = 0;
2671   Int_t nout1 = 0;
2672   Int_t nout2 = 0;
2673
2674   Double_t x[5], c[15];
2675   //  Int_t di = i1-i2;
2676   //
2677   AliTPCseed * seed = new AliTPCseed;
2678   Double_t alpha=fSectors->GetAlpha(), shift=fSectors->GetAlphaShift();
2679   Double_t cs=cos(alpha), sn=sin(alpha);
2680   //
2681   //  Double_t x1 =fOuterSec->GetX(i1);
2682   //Double_t xx2=fOuterSec->GetX(i2);
2683   
2684   Double_t x1 =GetXrow(i1);
2685   Double_t xx2=GetXrow(i2);
2686
2687   Double_t x3=GetX(), y3=GetY(), z3=GetZ();
2688
2689   Int_t imiddle = (i2+i1)/2;    //middle pad row index
2690   Double_t xm = GetXrow(imiddle); // radius of middle pad-row
2691   const AliTPCRow& krm=GetRow(sec,imiddle); //middle pad -row
2692   //
2693   Int_t ns =sec;   
2694
2695   const AliTPCRow& kr1=GetRow(ns,i1);
2696   Double_t ymax  = GetMaxY(i1)-kr1.fDeadZone-1.5;  
2697   Double_t ymaxm = GetMaxY(imiddle)-kr1.fDeadZone-1.5;  
2698
2699   //
2700   // change cut on curvature if it can't reach this layer
2701   // maximal curvature set to reach it
2702   Double_t dvertexmax  = TMath::Sqrt((x1-x3)*(x1-x3)+(ymax+5-y3)*(ymax+5-y3));
2703   if (dvertexmax*0.5*cuts[0]>0.85){
2704     cuts[0] = 0.85/(dvertexmax*0.5+1.);
2705   }
2706   Double_t r2min = 1/(cuts[0]*cuts[0]);  //minimal square of radius given by cut
2707
2708   //  Int_t ddsec = 1;
2709   if (deltay>0) ddsec = 0; 
2710   // loop over clusters  
2711   for (Int_t is=0; is < kr1; is++) {
2712     //
2713     if (kr1[is]->IsUsed(10)) continue;
2714     Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();    
2715     //if (TMath::Abs(y1)>ymax) continue;
2716
2717     if (deltay>0 && TMath::Abs(ymax-TMath::Abs(y1))> deltay ) continue;  // seed only at the edge
2718
2719     // find possible directions    
2720     Float_t anglez = (z1-z3)/(x1-x3); 
2721     Float_t extraz = z1 - anglez*(x1-xx2);  // extrapolated z      
2722     //
2723     //
2724     //find   rotation angles relative to line given by vertex and point 1
2725     Double_t dvertex2 = (x1-x3)*(x1-x3)+(y1-y3)*(y1-y3);
2726     Double_t dvertex  = TMath::Sqrt(dvertex2);
2727     Double_t angle13  = TMath::ATan((y1-y3)/(x1-x3));
2728     Double_t cs13     = cos(-angle13), sn13 = sin(-angle13);            
2729     
2730     //
2731     // loop over 2 sectors
2732     Int_t dsec1=-ddsec;
2733     Int_t dsec2= ddsec;
2734     if (y1<0)  dsec2= 0;
2735     if (y1>0)  dsec1= 0;
2736     
2737     Double_t dddz1=0;  // direction of delta inclination in z axis
2738     Double_t dddz2=0;
2739     if ( (z1-z3)>0)
2740       dddz1 =1;    
2741     else
2742       dddz2 =1;
2743     //
2744     for (Int_t dsec = dsec1; dsec<=dsec2;dsec++){
2745       Int_t sec2 = sec + dsec;
2746       // 
2747       //      AliTPCRow&  kr2  = fOuterSec[(sec2+fkNOS)%fkNOS][i2];
2748       //AliTPCRow&  kr2m = fOuterSec[(sec2+fkNOS)%fkNOS][imiddle];
2749       AliTPCRow&  kr2  = GetRow((sec2+fkNOS)%fkNOS,i2);
2750       AliTPCRow&  kr2m = GetRow((sec2+fkNOS)%fkNOS,imiddle);
2751       Int_t  index1 = TMath::Max(kr2.Find(extraz-0.6-dddz1*TMath::Abs(z1)*0.05)-1,0);
2752       Int_t  index2 = TMath::Min(kr2.Find(extraz+0.6+dddz2*TMath::Abs(z1)*0.05)+1,kr2);
2753
2754       // rotation angles to p1-p3
2755       Double_t cs13r     = cos(-angle13+dsec*alpha)/dvertex, sn13r = sin(-angle13+dsec*alpha)/dvertex;            
2756       Double_t x2,   y2,   z2; 
2757       //
2758       //      Double_t dymax = maxangle*TMath::Abs(x1-xx2);
2759
2760       //
2761       Double_t dxx0 =  (xx2-x3)*cs13r;
2762       Double_t dyy0 =  (xx2-x3)*sn13r;
2763       for (Int_t js=index1; js < index2; js++) {
2764         const AliTPCclusterMI *kcl = kr2[js];
2765         if (kcl->IsUsed(10)) continue;  
2766         //
2767         //calcutate parameters
2768         //      
2769         Double_t yy0 =  dyy0 +(kcl->GetY()-y3)*cs13r;
2770         // stright track
2771         if (TMath::Abs(yy0)<0.000001) continue;
2772         Double_t xx0 =  dxx0 -(kcl->GetY()-y3)*sn13r;
2773         Double_t y0  =  0.5*(xx0*xx0+yy0*yy0-xx0)/yy0;
2774         Double_t r02 = (0.25+y0*y0)*dvertex2;   
2775         //curvature (radius) cut
2776         if (r02<r2min) continue;                
2777        
2778         nin0++; 
2779         //
2780         Double_t c0  = 1/TMath::Sqrt(r02);
2781         if (yy0>0) c0*=-1.;     
2782                
2783        
2784         //Double_t dfi0   = 2.*TMath::ASin(dvertex*c0*0.5);
2785         //Double_t dfi1   = 2.*TMath::ASin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);
2786         Double_t dfi0   = 2.*AliTPCFastMath::FastAsin(dvertex*c0*0.5);
2787         Double_t dfi1   = 2.*AliTPCFastMath::FastAsin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);  
2788         //
2789         //
2790         Double_t z0  =  kcl->GetZ();  
2791         Double_t zzzz2    = z1-(z1-z3)*dfi1/dfi0;
2792         if (TMath::Abs(zzzz2-z0)>0.5) continue;       
2793         nin1++;              
2794         //      
2795         Double_t dip    = (z1-z0)*c0/dfi1;        
2796         Double_t x0 = (0.5*cs13+y0*sn13)*dvertex*c0;
2797         //
2798         y2 = kcl->GetY(); 
2799         if (dsec==0){
2800           x2 = xx2; 
2801           z2 = kcl->GetZ();       
2802         }
2803         else
2804           {
2805             // rotation 
2806             z2 = kcl->GetZ();  
2807             x2= xx2*cs-y2*sn*dsec;
2808             y2=+xx2*sn*dsec+y2*cs;
2809           }
2810         
2811         x[0] = y1;
2812         x[1] = z1;
2813         x[2] = x0;
2814         x[3] = dip;
2815         x[4] = c0;
2816         //
2817         //
2818         // do we have cluster at the middle ?
2819         Double_t ym,zm;
2820         GetProlongation(x1,xm,x,ym,zm);
2821         UInt_t dummy; 
2822         AliTPCclusterMI * cm=0;
2823         if (TMath::Abs(ym)-ymaxm<0){      
2824           cm = krm.FindNearest2(ym,zm,1.0,0.6,dummy);
2825           if ((!cm) || (cm->IsUsed(10))) {        
2826             continue;
2827           }
2828         }
2829         else{     
2830           // rotate y1 to system 0
2831           // get state vector in rotated system 
2832           Double_t yr1  = (-0.5*sn13+y0*cs13)*dvertex*c0;
2833           Double_t xr2  =  x0*cs+yr1*sn*dsec;
2834           Double_t xr[5]={kcl->GetY(),kcl->GetZ(), xr2, dip, c0};
2835           //
2836           GetProlongation(xx2,xm,xr,ym,zm);
2837           if (TMath::Abs(ym)-ymaxm<0){
2838             cm = kr2m.FindNearest2(ym,zm,1.0,0.6,dummy);
2839             if ((!cm) || (cm->IsUsed(10))) {      
2840               continue;
2841             }
2842           }
2843         }
2844        
2845
2846         Double_t dym = 0;
2847         Double_t dzm = 0;
2848         if (cm){
2849           dym = ym - cm->GetY();
2850           dzm = zm - cm->GetZ();
2851         }
2852         nin2++;
2853
2854
2855         //
2856         //
2857         Double_t sy1=kr1[is]->GetSigmaY2()*2., sz1=kr1[is]->GetSigmaZ2()*2.;
2858         Double_t sy2=kcl->GetSigmaY2()*2.,     sz2=kcl->GetSigmaZ2()*2.;
2859         //Double_t sy3=400*3./12., sy=0.1, sz=0.1;
2860         Double_t sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
2861         //Double_t sy3=25000*x[4]*x[4]*60+0.5, sy=0.1, sz=0.1;
2862
2863         Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
2864         Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
2865         Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
2866         Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
2867         Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
2868         Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
2869         
2870         Double_t f30=(F3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
2871         Double_t f31=(F3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
2872         Double_t f32=(F3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
2873         Double_t f34=(F3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
2874         
2875         c[0]=sy1;
2876         c[1]=0.;       c[2]=sz1;
2877         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
2878         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
2879                        c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
2880         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
2881         c[13]=f30*sy1*f40+f32*sy2*f42;
2882         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
2883         
2884         //      if (!BuildSeed(kr1[is],kcl,0,x1,x2,x3,x,c)) continue;
2885         
2886         UInt_t index=kr1.GetIndex(is);
2887         AliTPCseed *track=new(seed) AliTPCseed(index, x, c, x1, ns*alpha+shift);
2888         
2889         track->fIsSeeding = kTRUE;
2890         track->fSeed1 = i1;
2891         track->fSeed2 = i2;
2892         track->fSeedType=3;
2893
2894        
2895         //if (dsec==0) {
2896           FollowProlongation(*track, (i1+i2)/2,1);
2897           Int_t foundable,found,shared;
2898           track->GetClusterStatistic((i1+i2)/2,i1, found, foundable, shared, kTRUE);
2899           if ((found<0.55*foundable)  || shared>0.5*found || (track->GetSigmaY2()+track->GetSigmaZ2())>0.5){
2900             seed->Reset();
2901             seed->~AliTPCseed();
2902             continue;
2903           }
2904           //}
2905         
2906         nin++;
2907         FollowProlongation(*track, i2,1);
2908         
2909         
2910         //Int_t rc = 1;
2911         track->fBConstrain =1;
2912         //      track->fLastPoint = i1+fInnerSec->GetNRows();  // first cluster in track position
2913         track->fLastPoint = i1;  // first cluster in track position
2914         track->fFirstPoint = track->fLastPoint;
2915         
2916         if (track->GetNumberOfClusters()<(i1-i2)*0.5 || 
2917             track->GetNumberOfClusters() < track->fNFoundable*0.6 || 
2918             track->fNShared>0.4*track->GetNumberOfClusters() ) {
2919           seed->Reset();
2920           seed->~AliTPCseed();
2921           continue;
2922         }
2923         nout1++;
2924         // Z VERTEX CONDITION
2925         Double_t zv;
2926         zv = track->GetZ()+track->GetTgl()/track->GetC()*
2927           ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2928         if (TMath::Abs(zv-z3)>cuts[2]) {
2929           FollowProlongation(*track, TMath::Max(i2-20,0));
2930           zv = track->GetZ()+track->GetTgl()/track->GetC()*
2931             ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2932           if (TMath::Abs(zv-z3)>cuts[2]){
2933             FollowProlongation(*track, TMath::Max(i2-40,0));
2934             zv = track->GetZ()+track->GetTgl()/track->GetC()*
2935               ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2936             if (TMath::Abs(zv-z3)>cuts[2] &&(track->GetNumberOfClusters() > track->fNFoundable*0.7)){
2937               // make seed without constrain
2938               AliTPCseed * track2 = MakeSeed(track,0.2,0.5,1.);
2939               FollowProlongation(*track2, i2,1);
2940               track2->fBConstrain = kFALSE;
2941               track2->fSeedType = 1;
2942               arr->AddLast(track2); 
2943               seed->Reset();
2944               seed->~AliTPCseed();
2945               continue;         
2946             }
2947             else{
2948               seed->Reset();
2949               seed->~AliTPCseed();
2950               continue;
2951             
2952             }
2953           }
2954         }
2955         
2956         track->fSeedType =0;
2957         arr->AddLast(track); 
2958         seed = new AliTPCseed;  
2959         nout2++;
2960         // don't consider other combinations
2961         if (track->GetNumberOfClusters() > track->fNFoundable*0.8)
2962           break;
2963       }
2964     }
2965   }
2966   if (fDebug>3){
2967     Info("MakeSeeds3","\nSeeding statistic:\t%d\t%d\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin,nout1,nout2);
2968   }
2969   delete seed;
2970 }
2971
2972
2973 void AliTPCtrackerMI::MakeSeeds5(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,  Float_t cuts[4],
2974                                  Float_t deltay) {
2975   
2976
2977
2978   //-----------------------------------------------------------------
2979   // This function creates track seeds.
2980   //-----------------------------------------------------------------
2981   // cuts[0]   - fP4 cut
2982   // cuts[1]   - tan(phi)  cut
2983   // cuts[2]   - zvertex cut
2984   // cuts[3]   - fP3 cut
2985
2986
2987   Int_t nin0  = 0;
2988   Int_t nin1  = 0;
2989   Int_t nin2  = 0;
2990   Int_t nin   = 0;
2991   Int_t nout1 = 0;
2992   Int_t nout2 = 0;
2993   Int_t nout3 =0;
2994   Double_t x[5], c[15];
2995   //
2996   // make temporary seed
2997   AliTPCseed * seed = new AliTPCseed;
2998   Double_t alpha=fOuterSec->GetAlpha(), shift=fOuterSec->GetAlphaShift();
2999   //  Double_t cs=cos(alpha), sn=sin(alpha);
3000   //
3001   //
3002
3003   // first 3 padrows
3004   Double_t x1 = GetXrow(i1-1);
3005   const    AliTPCRow& kr1=GetRow(sec,i1-1);
3006   Double_t y1max  = GetMaxY(i1-1)-kr1.fDeadZone-1.5;  
3007   //
3008   Double_t x1p = GetXrow(i1);
3009   const    AliTPCRow& kr1p=GetRow(sec,i1);
3010   //
3011   Double_t x1m = GetXrow(i1-2);
3012   const    AliTPCRow& kr1m=GetRow(sec,i1-2);
3013
3014   //
3015   //last 3 padrow for seeding
3016   AliTPCRow&  kr3  = GetRow((sec+fkNOS)%fkNOS,i1-7);
3017   Double_t    x3   =  GetXrow(i1-7);
3018   //  Double_t    y3max= GetMaxY(i1-7)-kr3.fDeadZone-1.5;  
3019   //
3020   AliTPCRow&  kr3p  = GetRow((sec+fkNOS)%fkNOS,i1-6);
3021   Double_t    x3p   = GetXrow(i1-6);
3022   //
3023   AliTPCRow&  kr3m  = GetRow((sec+fkNOS)%fkNOS,i1-8);
3024   Double_t    x3m   = GetXrow(i1-8);
3025
3026   //
3027   //
3028   // middle padrow
3029   Int_t im = i1-4;                           //middle pad row index
3030   Double_t xm         = GetXrow(im);         // radius of middle pad-row
3031   const AliTPCRow& krm=GetRow(sec,im);   //middle pad -row
3032   //  Double_t ymmax = GetMaxY(im)-kr1.fDeadZone-1.5;  
3033   //
3034   //
3035   Double_t deltax  = x1-x3;
3036   Double_t dymax   = deltax*cuts[1];
3037   Double_t dzmax   = deltax*cuts[3];
3038   //
3039   // loop over clusters  
3040   for (Int_t is=0; is < kr1; is++) {
3041     //
3042     if (kr1[is]->IsUsed(10)) continue;
3043     Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();    
3044     //
3045     if (deltay>0 && TMath::Abs(y1max-TMath::Abs(y1))> deltay ) continue;  // seed only at the edge    
3046     // 
3047     Int_t  index1 = TMath::Max(kr3.Find(z1-dzmax)-1,0);
3048     Int_t  index2 = TMath::Min(kr3.Find(z1+dzmax)+1,kr3);
3049     //    
3050     Double_t y3,   z3;
3051     //
3052     //
3053     UInt_t index;
3054     for (Int_t js=index1; js < index2; js++) {
3055       const AliTPCclusterMI *kcl = kr3[js];
3056       if (kcl->IsUsed(10)) continue;
3057       y3 = kcl->GetY(); 
3058       // apply angular cuts
3059       if (TMath::Abs(y1-y3)>dymax) continue;
3060       x3 = x3; 
3061       z3 = kcl->GetZ(); 
3062       if (TMath::Abs(z1-z3)>dzmax) continue;
3063       //
3064       Double_t angley = (y1-y3)/(x1-x3);
3065       Double_t anglez = (z1-z3)/(x1-x3);
3066       //
3067       Double_t erry = TMath::Abs(angley)*(x1-x1m)*0.5+0.5;
3068       Double_t errz = TMath::Abs(anglez)*(x1-x1m)*0.5+0.5;
3069       //
3070       Double_t yyym = angley*(xm-x1)+y1;
3071       Double_t zzzm = anglez*(xm-x1)+z1;
3072
3073       const AliTPCclusterMI *kcm = krm.FindNearest2(yyym,zzzm,erry,errz,index);
3074       if (!kcm) continue;
3075       if (kcm->IsUsed(10)) continue;
3076       
3077       erry = TMath::Abs(angley)*(x1-x1m)*0.4+0.5;
3078       errz = TMath::Abs(anglez)*(x1-x1m)*0.4+0.5;
3079       //
3080       //
3081       //
3082       Int_t used  =0;
3083       Int_t found =0;
3084       //
3085       // look around first
3086       const AliTPCclusterMI *kc1m = kr1m.FindNearest2(angley*(x1m-x1)+y1,
3087                                                       anglez*(x1m-x1)+z1,
3088                                                       erry,errz,index);
3089       //
3090       if (kc1m){
3091         found++;
3092         if (kc1m->IsUsed(10)) used++;
3093       }
3094       const AliTPCclusterMI *kc1p = kr1p.FindNearest2(angley*(x1p-x1)+y1,
3095                                                       anglez*(x1p-x1)+z1,
3096                                                       erry,errz,index);
3097       //
3098       if (kc1p){
3099         found++;
3100         if (kc1p->IsUsed(10)) used++;
3101       }
3102       if (used>1)  continue;
3103       if (found<1) continue; 
3104
3105       //
3106       // look around last
3107       const AliTPCclusterMI *kc3m = kr3m.FindNearest2(angley*(x3m-x3)+y3,
3108                                                       anglez*(x3m-x3)+z3,
3109                                                       erry,errz,index);
3110       //
3111       if (kc3m){
3112         found++;
3113         if (kc3m->IsUsed(10)) used++;
3114       }
3115       else 
3116         continue;
3117       const AliTPCclusterMI *kc3p = kr3p.FindNearest2(angley*(x3p-x3)+y3,
3118                                                       anglez*(x3p-x3)+z3,
3119                                                       erry,errz,index);
3120       //
3121       if (kc3p){
3122         found++;
3123         if (kc3p->IsUsed(10)) used++;
3124       }
3125       else 
3126         continue;
3127       if (used>1)  continue;
3128       if (found<3) continue;       
3129       //
3130       Double_t x2,y2,z2;
3131       x2 = xm;
3132       y2 = kcm->GetY();
3133       z2 = kcm->GetZ();
3134       //
3135                         
3136       x[0]=y1;
3137       x[1]=z1;
3138       x[4]=F1(x1,y1,x2,y2,x3,y3);
3139       //if (TMath::Abs(x[4]) >= cuts[0]) continue;
3140       nin0++;
3141       //
3142       x[2]=F2(x1,y1,x2,y2,x3,y3);
3143       nin1++;
3144       //
3145       x[3]=F3n(x1,y1,x2,y2,z1,z2,x[4]);
3146       //if (TMath::Abs(x[3]) > cuts[3]) continue;
3147       nin2++;
3148       //
3149       //
3150       Double_t sy1=0.1,  sz1=0.1;
3151       Double_t sy2=0.1,  sz2=0.1;
3152       Double_t sy3=0.1,  sy=0.1, sz=0.1;
3153       
3154       Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
3155       Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
3156       Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
3157       Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
3158       Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
3159       Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
3160       
3161       Double_t f30=(F3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
3162       Double_t f31=(F3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
3163       Double_t f32=(F3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
3164       Double_t f34=(F3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
3165       
3166       c[0]=sy1;
3167       c[1]=0.;       c[2]=sz1; 
3168       c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3169       c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3170       c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3171       c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3172       c[13]=f30*sy1*f40+f32*sy2*f42;
3173       c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3174       
3175       //        if (!BuildSeed(kr1[is],kcl,0,x1,x2,x3,x,c)) continue;
3176       
3177       UInt_t index=kr1.GetIndex(is);
3178       AliTPCseed *track=new(seed) AliTPCseed(index, x, c, x1, sec*alpha+shift);
3179       
3180       track->fIsSeeding = kTRUE;
3181
3182       nin++;      
3183       FollowProlongation(*track, i1-7,1);
3184       if (track->GetNumberOfClusters() < track->fNFoundable*0.75 || 
3185           track->fNShared>0.6*track->GetNumberOfClusters() || ( track->GetSigmaY2()+ track->GetSigmaZ2())>0.6){
3186         seed->Reset();
3187         seed->~AliTPCseed();
3188         continue;
3189       }
3190       nout1++;
3191       nout2++;  
3192       //Int_t rc = 1;
3193       FollowProlongation(*track, i2,1);
3194       track->fBConstrain =0;
3195       track->fLastPoint = i1+fInnerSec->GetNRows();  // first cluster in track position
3196       track->fFirstPoint = track->fLastPoint;
3197       
3198       if (track->GetNumberOfClusters()<(i1-i2)*0.5 || 
3199           track->GetNumberOfClusters()<track->fNFoundable*0.7 || 
3200           track->fNShared>2. || track->GetChi2()/track->GetNumberOfClusters()>6 || ( track->GetSigmaY2()+ track->GetSigmaZ2())>0.5 ) {
3201         seed->Reset();
3202         seed->~AliTPCseed();
3203         continue;
3204       }
3205    
3206       {
3207         FollowProlongation(*track, TMath::Max(i2-10,0),1);
3208         AliTPCseed * track2 = MakeSeed(track,0.2,0.5,0.9);
3209         FollowProlongation(*track2, i2,1);
3210         track2->fBConstrain = kFALSE;
3211         track2->fSeedType = 4;
3212         arr->AddLast(track2); 
3213         seed->Reset();
3214         seed->~AliTPCseed();
3215       }
3216       
3217    
3218       //arr->AddLast(track); 
3219       //seed = new AliTPCseed;  
3220       nout3++;
3221     }
3222   }
3223   
3224   if (fDebug>3){
3225     Info("MakeSeeds5","\nSeeding statiistic:\t%d\t%d\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin,nout1,nout2,nout3);
3226   }
3227   delete seed;
3228 }
3229
3230
3231 //_____________________________________________________________________________
3232 void AliTPCtrackerMI::MakeSeeds2(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2, Float_t */*cuts[4]*/,
3233                                  Float_t deltay, Bool_t /*bconstrain*/) {
3234   //-----------------------------------------------------------------
3235   // This function creates track seeds - without vertex constraint
3236   //-----------------------------------------------------------------
3237   // cuts[0]   - fP4 cut        - not applied
3238   // cuts[1]   - tan(phi)  cut
3239   // cuts[2]   - zvertex cut    - not applied 
3240   // cuts[3]   - fP3 cut
3241   Int_t nin0=0;
3242   Int_t nin1=0;
3243   Int_t nin2=0;
3244   Int_t nin3=0;
3245   //  Int_t nin4=0;
3246   //Int_t nin5=0;
3247
3248   
3249
3250   Double_t alpha=fOuterSec->GetAlpha(), shift=fOuterSec->GetAlphaShift();
3251   //  Double_t cs=cos(alpha), sn=sin(alpha);
3252   Int_t row0 = (i1+i2)/2;
3253   Int_t drow = (i1-i2)/2;
3254   const AliTPCRow& kr0=fSectors[sec][row0];
3255   AliTPCRow * kr=0;
3256
3257   AliTPCpolyTrack polytrack;
3258   Int_t nclusters=fSectors[sec][row0];
3259   AliTPCseed * seed = new AliTPCseed;
3260
3261   Int_t sumused=0;
3262   Int_t cused=0;
3263   Int_t cnused=0;
3264   for (Int_t is=0; is < nclusters; is++) {  //LOOP over clusters
3265     Int_t nfound =0;
3266     Int_t nfoundable =0;
3267     for (Int_t iter =1; iter<2; iter++){   //iterations
3268       const AliTPCRow& krm=fSectors[sec][row0-iter];
3269       const AliTPCRow& krp=fSectors[sec][row0+iter];      
3270       const AliTPCclusterMI * cl= kr0[is];
3271       
3272       if (cl->IsUsed(10)) {
3273         cused++;
3274       }
3275       else{
3276         cnused++;
3277       }
3278       Double_t x = kr0.GetX();
3279       // Initialization of the polytrack
3280       nfound =0;
3281       nfoundable =0;
3282       polytrack.Reset();
3283       //
3284       Double_t y0= cl->GetY();
3285       Double_t z0= cl->GetZ();
3286       Float_t erry = 0;
3287       Float_t errz = 0;
3288       
3289       Double_t ymax = fSectors->GetMaxY(row0)-kr0.fDeadZone-1.5;
3290       if (deltay>0 && TMath::Abs(ymax-TMath::Abs(y0))> deltay ) continue;  // seed only at the edge
3291       
3292       erry = (0.5)*cl->GetSigmaY2()/TMath::Sqrt(cl->GetQ())*6;      
3293       errz = (0.5)*cl->GetSigmaZ2()/TMath::Sqrt(cl->GetQ())*6;      
3294       polytrack.AddPoint(x,y0,z0,erry, errz);
3295
3296       sumused=0;
3297       if (cl->IsUsed(10)) sumused++;
3298
3299
3300       Float_t roady = (5*TMath::Sqrt(cl->GetSigmaY2()+0.2)+1.)*iter;
3301       Float_t roadz = (5*TMath::Sqrt(cl->GetSigmaZ2()+0.2)+1.)*iter;
3302       //
3303       x = krm.GetX();
3304       AliTPCclusterMI * cl1 = krm.FindNearest(y0,z0,roady,roadz);
3305       if (cl1 && TMath::Abs(ymax-TMath::Abs(y0))) {
3306         erry = (0.5)*cl1->GetSigmaY2()/TMath::Sqrt(cl1->GetQ())*3;          
3307         errz = (0.5)*cl1->GetSigmaZ2()/TMath::Sqrt(cl1->GetQ())*3;
3308         if (cl1->IsUsed(10))  sumused++;
3309         polytrack.AddPoint(x,cl1->GetY(),cl1->GetZ(),erry,errz);
3310       }
3311       //
3312       x = krp.GetX();
3313       AliTPCclusterMI * cl2 = krp.FindNearest(y0,z0,roady,roadz);
3314       if (cl2) {
3315         erry = (0.5)*cl2->GetSigmaY2()/TMath::Sqrt(cl2->GetQ())*3;          
3316         errz = (0.5)*cl2->GetSigmaZ2()/TMath::Sqrt(cl2->GetQ())*3;
3317         if (cl2->IsUsed(10)) sumused++;  
3318         polytrack.AddPoint(x,cl2->GetY(),cl2->GetZ(),erry,errz);
3319       }
3320       //
3321       if (sumused>0) continue;
3322       nin0++;
3323       polytrack.UpdateParameters();
3324       // follow polytrack
3325       roadz = 1.2;
3326       roady = 1.2;
3327       //
3328       Double_t yn,zn;
3329       nfoundable = polytrack.GetN();
3330       nfound     = nfoundable; 
3331       //
3332       for (Int_t ddrow = iter+1; ddrow<drow;ddrow++){
3333         Float_t maxdist = 0.8*(1.+3./(ddrow));
3334         for (Int_t delta = -1;delta<=1;delta+=2){
3335           Int_t row = row0+ddrow*delta;
3336           kr = &(fSectors[sec][row]);
3337           Double_t xn = kr->GetX();
3338           Double_t ymax = fSectors->GetMaxY(row)-kr->fDeadZone-1.5;
3339           polytrack.GetFitPoint(xn,yn,zn);
3340           if (TMath::Abs(yn)>ymax) continue;
3341           nfoundable++;
3342           AliTPCclusterMI * cln = kr->FindNearest(yn,zn,roady,roadz);
3343           if (cln) {
3344             Float_t dist =  TMath::Sqrt(  (yn-cln->GetY())*(yn-cln->GetY())+(zn-cln->GetZ())*(zn-cln->GetZ()));
3345             if (dist<maxdist){
3346               /*
3347               erry = (dist+0.3)*cln->GetSigmaY2()/TMath::Sqrt(cln->GetQ())*(1.+1./(ddrow));         
3348               errz = (dist+0.3)*cln->GetSigmaZ2()/TMath::Sqrt(cln->GetQ())*(1.+1./(ddrow));
3349               if (cln->IsUsed(10)) {
3350                 //      printf("used\n");
3351                 sumused++;
3352                 erry*=2;
3353                 errz*=2;
3354               }
3355               */
3356               erry=0.1;
3357               errz=0.1;
3358               polytrack.AddPoint(xn,cln->GetY(),cln->GetZ(),erry, errz);
3359               nfound++;
3360             }
3361           }
3362         }
3363         if ( (sumused>3) || (sumused>0.5*nfound) || (nfound<0.6*nfoundable))  break;     
3364         polytrack.UpdateParameters();
3365       }           
3366     }
3367     if ( (sumused>3) || (sumused>0.5*nfound))  {
3368       //printf("sumused   %d\n",sumused);
3369       continue;
3370     }
3371     nin1++;
3372     Double_t dy,dz;
3373     polytrack.GetFitDerivation(kr0.GetX(),dy,dz);
3374     AliTPCpolyTrack track2;
3375     
3376     polytrack.Refit(track2,0.5+TMath::Abs(dy)*0.3,0.4+TMath::Abs(dz)*0.3);
3377     if (track2.GetN()<0.5*nfoundable) continue;
3378     nin2++;
3379
3380     if ((nfound>0.6*nfoundable) &&( nfoundable>0.4*(i1-i2))) {
3381       //
3382       // test seed with and without constrain
3383       for (Int_t constrain=0; constrain<=0;constrain++){
3384         // add polytrack candidate
3385
3386         Double_t x[5], c[15];
3387         Double_t x1,x2,x3,y1,y2,y3,z1,z2,z3;
3388         track2.GetBoundaries(x3,x1);    
3389         x2 = (x1+x3)/2.;
3390         track2.GetFitPoint(x1,y1,z1);
3391         track2.GetFitPoint(x2,y2,z2);
3392         track2.GetFitPoint(x3,y3,z3);
3393         //
3394         //is track pointing to the vertex ?
3395         Double_t x0,y0,z0;
3396         x0=0;
3397         polytrack.GetFitPoint(x0,y0,z0);
3398
3399         if (constrain) {
3400           x2 = x3;
3401           y2 = y3;
3402           z2 = z3;
3403           
3404           x3 = 0;
3405           y3 = 0;
3406           z3 = 0;
3407         }
3408         x[0]=y1;
3409         x[1]=z1;
3410         x[4]=F1(x1,y1,x2,y2,x3,y3);
3411                 
3412         //      if (TMath::Abs(x[4]) >= cuts[0]) continue;  //
3413         x[2]=F2(x1,y1,x2,y2,x3,y3);
3414         
3415         //if (TMath::Abs(x[4]*x1-x[2]) >= cuts[1]) continue;
3416         //x[3]=F3(x1,y1,x2,y2,z1,z2);
3417         x[3]=F3n(x1,y1,x3,y3,z1,z3,x[4]);
3418         //if (TMath::Abs(x[3]) > cuts[3]) continue;
3419
3420         
3421         Double_t sy =0.1, sz =0.1;
3422         Double_t sy1=0.02, sz1=0.02;
3423         Double_t sy2=0.02, sz2=0.02;
3424         Double_t sy3=0.02;
3425
3426         if (constrain){
3427           sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
3428         }
3429         
3430         Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
3431         Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
3432         Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
3433         Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
3434         Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
3435         Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
3436
3437         Double_t f30=(F3(x1,y1+sy,x3,y3,z1,z3)-x[3])/sy;
3438         Double_t f31=(F3(x1,y1,x3,y3,z1+sz,z3)-x[3])/sz;
3439         Double_t f32=(F3(x1,y1,x3,y3+sy,z1,z3)-x[3])/sy;
3440         Double_t f34=(F3(x1,y1,x3,y3,z1,z3+sz)-x[3])/sz;
3441
3442         
3443         c[0]=sy1;
3444         c[1]=0.;       c[2]=sz1;
3445         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3446         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3447         c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3448         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3449         c[13]=f30*sy1*f40+f32*sy2*f42;
3450         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3451         
3452         //Int_t row1 = fSectors->GetRowNumber(x1);
3453         Int_t row1 = GetRowNumber(x1);
3454
3455         UInt_t index=0;
3456         //kr0.GetIndex(is);
3457         AliTPCseed *track=new (seed) AliTPCseed(index, x, c, x1, sec*alpha+shift);
3458         track->fIsSeeding = kTRUE;
3459         Int_t rc=FollowProlongation(*track, i2);        
3460         if (constrain) track->fBConstrain =1;
3461         else
3462           track->fBConstrain =0;
3463         track->fLastPoint = row1+fInnerSec->GetNRows();  // first cluster in track position
3464         track->fFirstPoint = track->fLastPoint;
3465
3466         if (rc==0 || track->GetNumberOfClusters()<(i1-i2)*0.5 || 
3467             track->GetNumberOfClusters() < track->fNFoundable*0.6 || 
3468             track->fNShared>0.4*track->GetNumberOfClusters()) {
3469           //delete track;
3470           seed->Reset();
3471           seed->~AliTPCseed();
3472         }
3473         else {
3474           arr->AddLast(track);
3475           seed = new AliTPCseed;
3476         }
3477         nin3++;
3478       }
3479     }  // if accepted seed
3480   }
3481   if (fDebug>3){
3482     Info("MakeSeeds2","\nSeeding statiistic:\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin3);
3483   }
3484   delete seed;
3485 }
3486
3487
3488 AliTPCseed *AliTPCtrackerMI::MakeSeed(AliTPCseed *track, Float_t r0, Float_t r1, Float_t r2)
3489 {
3490   //
3491   //
3492   //reseed using track points
3493   Int_t p0 = int(r0*track->GetNumberOfClusters());     // point 0 
3494   Int_t p1 = int(r1*track->GetNumberOfClusters());
3495   Int_t p2 = int(r2*track->GetNumberOfClusters());   // last point
3496   Int_t pp2=0;
3497   Double_t  x0[3],x1[3],x2[3];
3498   for (Int_t i=0;i<3;i++){
3499     x0[i]=-1;
3500     x1[i]=-1;
3501     x2[i]=-1;
3502   }
3503
3504   // find track position at given ratio of the length
3505   Int_t  sec0=0, sec1=0, sec2=0;
3506   Int_t index=-1;
3507   Int_t clindex;
3508   for (Int_t i=0;i<160;i++){
3509     if (track->fClusterPointer[i]){
3510       index++;
3511       AliTPCTrackerPoint   *trpoint =track->GetTrackPoint(i);
3512       if ( (index<p0) || x0[0]<0 ){
3513         if (trpoint->GetX()>1){
3514           clindex = track->GetClusterIndex2(i);
3515           if (clindex>0){       
3516             x0[0] = trpoint->GetX();
3517             x0[1] = trpoint->GetY();
3518             x0[2] = trpoint->GetZ();
3519             sec0  = ((clindex&0xff000000)>>24)%18;
3520           }
3521         }
3522       }
3523
3524       if ( (index<p1) &&(trpoint->GetX()>1)){
3525         clindex = track->GetClusterIndex2(i);
3526         if (clindex>0){
3527           x1[0] = trpoint->GetX();
3528           x1[1] = trpoint->GetY();
3529           x1[2] = trpoint->GetZ();
3530           sec1  = ((clindex&0xff000000)>>24)%18;
3531         }
3532       }
3533       if ( (index<p2) &&(trpoint->GetX()>1)){
3534         clindex = track->GetClusterIndex2(i);
3535         if (clindex>0){
3536           x2[0] = trpoint->GetX();
3537           x2[1] = trpoint->GetY();
3538           x2[2] = trpoint->GetZ(); 
3539           sec2  = ((clindex&0xff000000)>>24)%18;
3540           pp2 = i;
3541         }
3542       }
3543     }
3544   }
3545   
3546   Double_t alpha, cs,sn, xx2,yy2;
3547   //
3548   alpha = (sec1-sec2)*fSectors->GetAlpha();
3549   cs = TMath::Cos(alpha);
3550   sn = TMath::Sin(alpha); 
3551   xx2= x1[0]*cs-x1[1]*sn;
3552   yy2= x1[0]*sn+x1[1]*cs;
3553   x1[0] = xx2;
3554   x1[1] = yy2;
3555   //
3556   alpha = (sec0-sec2)*fSectors->GetAlpha();
3557   cs = TMath::Cos(alpha);
3558   sn = TMath::Sin(alpha); 
3559   xx2= x0[0]*cs-x0[1]*sn;
3560   yy2= x0[0]*sn+x0[1]*cs;
3561   x0[0] = xx2;
3562   x0[1] = yy2;
3563   //
3564   //
3565   //
3566   Double_t x[5],c[15];
3567   //
3568   x[0]=x2[1];
3569   x[1]=x2[2];
3570   x[4]=F1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
3571   //  if (x[4]>1) return 0;
3572   x[2]=F2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
3573   x[3]=F3n(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2],x[4]);
3574   //if (TMath::Abs(x[3]) > 2.2)  return 0;
3575   //if (TMath::Abs(x[2]) > 1.99) return 0;
3576   //  
3577   Double_t sy =0.1,  sz =0.1;
3578   //
3579   Double_t sy1=0.02+track->GetSigmaY2(), sz1=0.02+track->GetSigmaZ2();
3580   Double_t sy2=0.01+track->GetSigmaY2(), sz2=0.01+track->GetSigmaZ2();
3581   Double_t sy3=0.01+track->GetSigmaY2();
3582   //
3583   Double_t f40=(F1(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[4])/sy;
3584   Double_t f42=(F1(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[4])/sy;
3585   Double_t f43=(F1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[4])/sy;
3586   Double_t f20=(F2(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[2])/sy;
3587   Double_t f22=(F2(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[2])/sy;
3588   Double_t f23=(F2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[2])/sy;
3589   //
3590   Double_t f30=(F3(x2[0],x2[1]+sy,x0[0],x0[1],x2[2],x0[2])-x[3])/sy;
3591   Double_t f31=(F3(x2[0],x2[1],x0[0],x0[1],x2[2]+sz,x0[2])-x[3])/sz;
3592   Double_t f32=(F3(x2[0],x2[1],x0[0],x0[1]+sy,x2[2],x0[2])-x[3])/sy;
3593   Double_t f34=(F3(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2]+sz)-x[3])/sz;
3594   
3595   
3596   c[0]=sy1;
3597   c[1]=0.;       c[2]=sz1;
3598   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3599   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3600   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3601   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3602   c[13]=f30*sy1*f40+f32*sy2*f42;
3603   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3604   
3605   //  Int_t row1 = fSectors->GetRowNumber(x2[0]);
3606   AliTPCseed *seed=new  AliTPCseed(0, x, c, x2[0], sec2*fSectors->GetAlpha()+fSectors->GetAlphaShift());
3607   //  Double_t y0,z0,y1,z1, y2,z2;
3608   //seed->GetProlongation(x0[0],y0,z0);
3609   // seed->GetProlongation(x1[0],y1,z1);
3610   //seed->GetProlongation(x2[0],y2,z2);
3611   //  seed =0;
3612   seed->fLastPoint  = pp2;
3613   seed->fFirstPoint = pp2;
3614   
3615
3616   return seed;
3617 }
3618
3619
3620 AliTPCseed *AliTPCtrackerMI::ReSeed(AliTPCseed *track, Float_t r0, Float_t r1, Float_t r2)
3621 {
3622   //
3623   //
3624   //reseed using founded clusters 
3625   //
3626   // Find the number of clusters
3627   Int_t nclusters = 0;
3628   for (Int_t irow=0;irow<160;irow++){
3629     if (track->GetClusterIndex(irow)>0) nclusters++;
3630   }
3631   //
3632   Int_t ipos[3];
3633   ipos[0] = TMath::Max(int(r0*nclusters),0);             // point 0 cluster
3634   ipos[1] = TMath::Min(int(r1*nclusters),nclusters-1);   // 
3635   ipos[2] = TMath::Min(int(r2*nclusters),nclusters-1);   // last point
3636   //
3637   //
3638   Double_t  xyz[3][3];
3639   Int_t     row[3],sec[3]={0,0,0};
3640   //
3641   // find track row position at given ratio of the length
3642   Int_t index=-1;
3643   for (Int_t irow=0;irow<160;irow++){    
3644     if (track->GetClusterIndex2(irow)<0) continue;
3645     index++;
3646     for (Int_t ipoint=0;ipoint<3;ipoint++){
3647       if (index<=ipos[ipoint]) row[ipoint] = irow;
3648     }        
3649   }
3650   //
3651   //Get cluster and sector position
3652   for (Int_t ipoint=0;ipoint<3;ipoint++){
3653     Int_t clindex = track->GetClusterIndex2(row[ipoint]);
3654     AliTPCclusterMI * cl = GetClusterMI(clindex);
3655     if (cl==0) {
3656       //Error("Bug\n");
3657       //      AliTPCclusterMI * cl = GetClusterMI(clindex);
3658       return 0;
3659     }
3660     sec[ipoint]     = ((clindex&0xff000000)>>24)%18;
3661     xyz[ipoint][0]  = GetXrow(row[ipoint]);
3662     xyz[ipoint][1]  = cl->GetY();
3663     xyz[ipoint][2]  = cl->GetZ();
3664   }
3665   //
3666   //
3667   // Calculate seed state vector and covariance matrix
3668
3669   Double_t alpha, cs,sn, xx2,yy2;
3670   //
3671   alpha = (sec[1]-sec[2])*fSectors->GetAlpha();
3672   cs = TMath::Cos(alpha);
3673   sn = TMath::Sin(alpha); 
3674   xx2= xyz[1][0]*cs-xyz[1][1]*sn;
3675   yy2= xyz[1][0]*sn+xyz[1][1]*cs;
3676   xyz[1][0] = xx2;
3677   xyz[1][1] = yy2;
3678   //
3679   alpha = (sec[0]-sec[2])*fSectors->GetAlpha();
3680   cs = TMath::Cos(alpha);
3681   sn = TMath::Sin(alpha); 
3682   xx2= xyz[0][0]*cs-xyz[0][1]*sn;
3683   yy2= xyz[0][0]*sn+xyz[0][1]*cs;
3684   xyz[0][0] = xx2;
3685   xyz[0][1] = yy2;
3686   //
3687   //
3688   //
3689   Double_t x[5],c[15];
3690   //
3691   x[0]=xyz[2][1];
3692   x[1]=xyz[2][2];
3693   x[4]=F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3694   x[2]=F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3695   x[3]=F3n(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2],x[4]);
3696   //  
3697   Double_t sy =0.1,  sz =0.1;
3698   //
3699   Double_t sy1=0.2, sz1=0.2;
3700   Double_t sy2=0.2, sz2=0.2;
3701   Double_t sy3=0.2;
3702   //
3703   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;
3704   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;
3705   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;
3706   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;
3707   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;
3708   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;
3709   //
3710   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;
3711   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;
3712   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;
3713   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;
3714   
3715   
3716   c[0]=sy1;
3717   c[1]=0.;       c[2]=sz1;
3718   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3719   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3720   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3721   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3722   c[13]=f30*sy1*f40+f32*sy2*f42;
3723   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3724   
3725   //  Int_t row1 = fSectors->GetRowNumber(xyz[2][0]);
3726   AliTPCseed *seed=new  AliTPCseed(0, x, c, xyz[2][0], sec[2]*fSectors->GetAlpha()+fSectors->GetAlphaShift());
3727   seed->fLastPoint  = row[2];
3728   seed->fFirstPoint = row[2];  
3729   return seed;
3730 }
3731
3732
3733 AliTPCseed *AliTPCtrackerMI::ReSeed(AliTPCseed *track,Int_t r0, Bool_t forward)
3734 {
3735   //
3736   //
3737   //reseed using founded clusters 
3738   //
3739   Double_t  xyz[3][3];
3740   Int_t     row[3]={0,0,0};
3741   Int_t     sec[3]={0,0,0};
3742   //
3743   // forward direction
3744   if (forward){
3745     for (Int_t irow=r0;irow<160;irow++){
3746       if (track->GetClusterIndex(irow)>0){
3747         row[0] = irow;
3748         break;
3749       }
3750     }
3751     for (Int_t irow=160;irow>r0;irow--){
3752       if (track->GetClusterIndex(irow)>0){
3753         row[2] = irow;
3754         break;
3755       }
3756     }
3757     for (Int_t irow=row[2]-15;irow>row[0];irow--){
3758       if (track->GetClusterIndex(irow)>0){
3759         row[1] = irow;
3760         break;
3761       }
3762     }
3763     //
3764   }
3765   if (!forward){
3766     for (Int_t irow=0;irow<r0;irow++){
3767       if (track->GetClusterIndex(irow)>0){
3768         row[0] = irow;
3769         break;
3770       }
3771     }
3772     for (Int_t irow=r0;irow>0;irow--){
3773       if (track->GetClusterIndex(irow)>0){
3774         row[2] = irow;
3775         break;
3776       }
3777     }    
3778     for (Int_t irow=row[2]-15;irow>row[0];irow--){
3779       if (track->GetClusterIndex(irow)>0){
3780         row[1] = irow;
3781         break;
3782       }
3783     } 
3784   }
3785   //
3786   if ((row[2]-row[0])<20) return 0;
3787   if (row[1]==0) return 0;
3788   //
3789   //
3790   //Get cluster and sector position
3791   for (Int_t ipoint=0;ipoint<3;ipoint++){
3792     Int_t clindex = track->GetClusterIndex2(row[ipoint]);
3793     AliTPCclusterMI * cl = GetClusterMI(clindex);
3794     if (cl==0) {
3795       //Error("Bug\n");
3796       //      AliTPCclusterMI * cl = GetClusterMI(clindex);
3797       return 0;
3798     }
3799     sec[ipoint]     = ((clindex&0xff000000)>>24)%18;
3800     xyz[ipoint][0]  = GetXrow(row[ipoint]);
3801     AliTPCTrackerPoint * point = track->GetTrackPoint(row[ipoint]);    
3802     if (point&&ipoint<2){
3803       //
3804        xyz[ipoint][1]  = point->GetY();
3805        xyz[ipoint][2]  = point->GetZ();
3806     }
3807     else{
3808       xyz[ipoint][1]  = cl->GetY();
3809       xyz[ipoint][2]  = cl->GetZ();
3810     }
3811   }
3812   //
3813   //
3814   //
3815   //
3816   // Calculate seed state vector and covariance matrix
3817
3818   Double_t alpha, cs,sn, xx2,yy2;
3819   //
3820   alpha = (sec[1]-sec[2])*fSectors->GetAlpha();
3821   cs = TMath::Cos(alpha);
3822   sn = TMath::Sin(alpha); 
3823   xx2= xyz[1][0]*cs-xyz[1][1]*sn;
3824   yy2= xyz[1][0]*sn+xyz[1][1]*cs;
3825   xyz[1][0] = xx2;
3826   xyz[1][1] = yy2;
3827   //
3828   alpha = (sec[0]-sec[2])*fSectors->GetAlpha();
3829   cs = TMath::Cos(alpha);
3830   sn = TMath::Sin(alpha); 
3831   xx2= xyz[0][0]*cs-xyz[0][1]*sn;
3832   yy2= xyz[0][0]*sn+xyz[0][1]*cs;
3833   xyz[0][0] = xx2;
3834   xyz[0][1] = yy2;
3835   //
3836   //
3837   //
3838   Double_t x[5],c[15];
3839   //
3840   x[0]=xyz[2][1];
3841   x[1]=xyz[2][2];
3842   x[4]=F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3843   x[2]=F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3844   x[3]=F3n(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2],x[4]);
3845   //  
3846   Double_t sy =0.1,  sz =0.1;
3847   //
3848   Double_t sy1=0.2, sz1=0.2;
3849   Double_t sy2=0.2, sz2=0.2;
3850   Double_t sy3=0.2;
3851   //
3852   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;
3853   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;
3854   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;
3855   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;
3856   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;
3857   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;
3858   //
3859   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;
3860   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;
3861   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;
3862   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;
3863   
3864   
3865   c[0]=sy1;
3866   c[1]=0.;       c[2]=sz1;
3867   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3868   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3869   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3870   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3871   c[13]=f30*sy1*f40+f32*sy2*f42;
3872   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3873   
3874   //  Int_t row1 = fSectors->GetRowNumber(xyz[2][0]);
3875   AliTPCseed *seed=new  AliTPCseed(0, x, c, xyz[2][0], sec[2]*fSectors->GetAlpha()+fSectors->GetAlphaShift());
3876   seed->fLastPoint  = row[2];
3877   seed->fFirstPoint = row[2];  
3878   for (Int_t i=row[0];i<row[2];i++){
3879     seed->fIndex[i] = track->fIndex[i];
3880   }
3881
3882   return seed;
3883 }
3884
3885 void  AliTPCtrackerMI::FindKinks(TObjArray * array, AliESD *esd)
3886 {
3887   //
3888   //  find kinks
3889   //
3890   //
3891
3892   TObjArray *kinks= new TObjArray(10000);
3893   //  TObjArray *v0s= new TObjArray(10000);
3894   Int_t nentries = array->GetEntriesFast();
3895   AliHelix *helixes      = new AliHelix[nentries];
3896   Int_t    *sign         = new Int_t[nentries];
3897   Int_t    *nclusters    = new Int_t[nentries];
3898   Float_t  *alpha        = new Float_t[nentries];
3899   AliESDkink * kink      = new AliESDkink();
3900   Int_t      * usage     = new Int_t[nentries];
3901   Float_t  *zm           = new Float_t[nentries];
3902   Float_t  *z0           = new Float_t[nentries]; 
3903   Float_t  *fim          = new Float_t[nentries];
3904   Float_t  *shared       = new Float_t[nentries];
3905   Bool_t   *circular     = new Bool_t[nentries];
3906   Float_t *dca          = new Float_t[nentries];
3907   //const AliESDVertex * primvertex = esd->GetVertex();
3908   //
3909   //  nentries = array->GetEntriesFast();
3910   //
3911   
3912   //
3913   //
3914   for (Int_t i=0;i<nentries;i++){
3915     sign[i]=0;
3916     usage[i]=0;
3917     AliTPCseed* track = (AliTPCseed*)array->At(i);    
3918     if (!track) continue;
3919     track->fCircular =0;
3920     shared[i] = kFALSE;
3921     track->UpdatePoints();
3922     if (( track->GetPoints()[2]- track->GetPoints()[0])>5 && track->GetPoints()[3]>0.8){
3923     }
3924     nclusters[i]=track->GetNumberOfClusters();
3925     alpha[i] = track->GetAlpha();
3926     new (&helixes[i]) AliHelix(*track);
3927     Double_t xyz[3];
3928     helixes[i].Evaluate(0,xyz);
3929     sign[i] = (track->GetC()>0) ? -1:1;
3930     Double_t x,y,z;
3931     x=160;
3932     if (track->GetProlongation(x,y,z)){
3933       zm[i]  = z;
3934       fim[i] = alpha[i]+TMath::ATan2(y,x);
3935     }
3936     else{
3937       zm[i]  = track->GetZ();
3938       fim[i] = alpha[i];
3939     }   
3940     z0[i]=1000;
3941     circular[i]= kFALSE;
3942     if (track->GetProlongation(0,y,z))  z0[i] = z;
3943     dca[i] = track->GetD(0,0);    
3944   }
3945   //
3946   //
3947   TStopwatch timer;
3948   timer.Start();
3949   Int_t ncandidates =0;
3950   Int_t nall =0;
3951   Int_t ntracks=0; 
3952   Double_t phase[2][2],radius[2];
3953
3954   //
3955   // Find circling track
3956   TTreeSRedirector &cstream = *fDebugStreamer;
3957   //
3958   for (Int_t i0=0;i0<nentries;i0++){
3959     AliTPCseed * track0 = (AliTPCseed*)array->At(i0);
3960     if (!track0) continue;    
3961     if (track0->fN<40) continue;
3962     if (TMath::Abs(1./track0->fP4)>200) continue;
3963     for (Int_t i1=i0+1;i1<nentries;i1++){
3964       AliTPCseed * track1 = (AliTPCseed*)array->At(i1);
3965       if (!track1) continue;
3966       if (track1->fN<40)                  continue;
3967       if ( TMath::Abs(track1->fP3+track0->fP3)>0.1) continue;
3968       if (track0->fBConstrain&&track1->fBConstrain) continue;
3969       if (TMath::Abs(1./track1->fP4)>200) continue;
3970       if (track1->fP4*track0->fP4>0)      continue;
3971       if (track1->fP3*track0->fP3>0)      continue;
3972       if (max(TMath::Abs(1./track0->fP4),TMath::Abs(1./track1->fP4))>190) continue;
3973       if (track0->fBConstrain&&TMath::Abs(track1->fP4)<TMath::Abs(track0->fP4)) continue; //returning - lower momenta
3974       if (track1->fBConstrain&&TMath::Abs(track0->fP4)<TMath::Abs(track1->fP4)) continue; //returning - lower momenta
3975       //
3976       Float_t mindcar = TMath::Min(TMath::Abs(dca[i0]),TMath::Abs(dca[i1]));
3977       if (mindcar<5)   continue;
3978       Float_t mindcaz = TMath::Min(TMath::Abs(z0[i0]-GetZ()),TMath::Abs(z0[i1]-GetZ()));
3979       if (mindcaz<5) continue;
3980       if (mindcar+mindcaz<20) continue;
3981       //
3982       //
3983       Float_t xc0 = helixes[i0].GetHelix(6);
3984       Float_t yc0 = helixes[i0].GetHelix(7);
3985       Float_t r0  = helixes[i0].GetHelix(8);
3986       Float_t xc1 = helixes[i1].GetHelix(6);
3987       Float_t yc1 = helixes[i1].GetHelix(7);
3988       Float_t r1  = helixes[i1].GetHelix(8);
3989         
3990       Float_t rmean = (r0+r1)*0.5;
3991       Float_t delta =TMath::Sqrt((xc1-xc0)*(xc1-xc0)+(yc1-yc0)*(yc1-yc0));
3992       //if (delta>30) continue;
3993       if (delta>rmean*0.25) continue;
3994       if (TMath::Abs(r0-r1)/rmean>0.3) continue; 
3995       //
3996       Int_t npoints = helixes[i0].GetRPHIintersections(helixes[i1], phase, radius,10);
3997       if (npoints==0) continue;
3998       helixes[i0].GetClosestPhases(helixes[i1], phase);
3999       //
4000       Double_t xyz0[3];
4001       Double_t xyz1[3];
4002       Double_t hangles[3];
4003       helixes[i0].Evaluate(phase[0][0],xyz0);
4004       helixes[i1].Evaluate(phase[0][1],xyz1);
4005
4006       helixes[i0].GetAngle(phase[0][0],helixes[i1],phase[0][1],hangles);
4007       Double_t deltah[2],deltabest;
4008       if (hangles[2]<2.8) continue;
4009       /*
4010       cstream<<"C"<<track0->fLab<<track1->fLab<<
4011         track0->fP3<<track1->fP3<<
4012         track0->fP4<<track1->fP4<<
4013         delta<<rmean<<npoints<<
4014         hangles[0]<<hangles[2]<<
4015         xyz0[2]<<xyz1[2]<<radius[0]<<"\n"; 
4016       */
4017       if (npoints>0){
4018         Int_t ibest=0;
4019         helixes[i0].ParabolicDCA(helixes[i1],phase[0][0],phase[0][1],radius[0],deltah[0],2);
4020         if (npoints==2){
4021           helixes[i0].ParabolicDCA(helixes[i1],phase[1][0],phase[1][1],radius[1],deltah[1],2);
4022           if (deltah[1]<deltah[0]) ibest=1;
4023         }
4024         deltabest  = TMath::Sqrt(deltah[ibest]);
4025         helixes[i0].Evaluate(phase[ibest][0],xyz0);
4026         helixes[i1].Evaluate(phase[ibest][1],xyz1);
4027         helixes[i0].GetAngle(phase[ibest][0],helixes[i1],phase[ibest][1],hangles);
4028         Double_t radiusbest = TMath::Sqrt(radius[ibest]);
4029         //
4030         if (deltabest>6) continue;
4031         if (mindcar+mindcaz<40 && (hangles[2]<3.12||deltabest>3)) continue;
4032         Bool_t sign =kFALSE;
4033         if (hangles[2]>3.06) sign =kTRUE;
4034         //
4035         if (sign){
4036           circular[i0] = kTRUE;
4037           circular[i1] = kTRUE;
4038           if (TMath::Abs(track0->fP4)<TMath::Abs(track1->fP4)){
4039             track0->fCircular += 1;
4040             track1->fCircular += 2;
4041           }
4042           else{
4043             track1->fCircular += 1;
4044             track0->fCircular += 2;
4045           }
4046         }               
4047         if (sign){        
4048           //debug stream
4049           cstream<<"Curling"<<
4050             "lab0="<<track0->fLab<<
4051             "lab1="<<track1->fLab<<   
4052             "Tr0.="<<track0<<
4053             "Tr1.="<<track1<<      
4054             "dca0="<<dca[i0]<<
4055             "dca1="<<dca[i1]<<
4056             "mindcar="<<mindcar<<
4057             "mindcaz="<<mindcaz<<
4058             "delta="<<delta<<
4059             "rmean="<<rmean<<
4060             "npoints="<<npoints<<                      
4061             "hangles0="<<hangles[0]<<
4062             "hangles2="<<hangles[2]<<                    
4063             "xyz0="<<xyz0[2]<<
4064             "xyzz1="<<xyz1[2]<<
4065             "z0="<<z0[i0]<<
4066             "z1="<<z0[i1]<<
4067             "radius="<<radiusbest<<
4068             "deltabest="<<deltabest<< 
4069             "phase0="<<phase[ibest][0]<<
4070             "phase1="<<phase[ibest][1]<<
4071             "\n";                 
4072         }
4073       }
4074     }
4075   }
4076   //
4077   //  Finf kinks loop
4078   // 
4079   //
4080   for (Int_t i =0;i<nentries;i++){
4081     if (sign[i]==0) continue;
4082     AliTPCseed * track0 = (AliTPCseed*)array->At(i);
4083     ntracks++;
4084     //
4085     Double_t cradius0 = 40*40;
4086     Double_t cradius1 = 270*270;
4087     Double_t cdist1=8.;
4088     Double_t cdist2=8.;
4089     Double_t cdist3=0.55; 
4090     for (Int_t j =i+1;j<nentries;j++){
4091       nall++;
4092       if (sign[j]*sign[i]<1) continue;
4093       if ( (nclusters[i]+nclusters[j])>200) continue;
4094       if ( (nclusters[i]+nclusters[j])<80) continue;
4095       if ( TMath::Abs(zm[i]-zm[j])>60.) continue;
4096       if ( TMath::Abs(fim[i]-fim[j])>0.6 && TMath::Abs(fim[i]-fim[j])<5.7 ) continue;
4097       //AliTPCseed * track1 = (AliTPCseed*)array->At(j);  Double_t phase[2][2],radius[2];    
4098       Int_t npoints = helixes[i].GetRPHIintersections(helixes[j], phase, radius,20);
4099       if (npoints<1) continue;
4100       // cuts on radius      
4101       if (npoints==1){
4102         if (radius[0]<cradius0||radius[0]>cradius1) continue;
4103       }
4104       else{
4105         if ( (radius[0]<cradius0||radius[0]>cradius1) && (radius[1]<cradius0||radius[1]>cradius1) ) continue;
4106       }
4107       //      
4108       Double_t delta1=10000,delta2=10000;
4109       // cuts on the intersection radius
4110       helixes[i].LinearDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta1);
4111       if (radius[0]<20&&delta1<1) continue; //intersection at vertex
4112       if (radius[0]<10&&delta1<3) continue; //intersection at vertex
4113       if (npoints==2){ 
4114         helixes[i].LinearDCA(helixes[j],phase[1][0],phase[1][1],radius[1],delta2);
4115         if (radius[1]<20&&delta2<1) continue;  //intersection at vertex
4116         if (radius[1]<10&&delta2<3) continue;  //intersection at vertex 
4117       }
4118       //
4119       Double_t distance1 = TMath::Min(delta1,delta2);
4120       if (distance1>cdist1) continue;  // cut on DCA linear approximation
4121       //
4122       npoints = helixes[i].GetRPHIintersections(helixes[j], phase, radius,20);
4123       helixes[i].ParabolicDCA(helixes[j],phase[0][0],phase[0][1],radius[0],delta1);
4124       if (radius[0]<20&&delta1<1) continue; //intersection at vertex
4125       if (radius[0]<10&&delta1<3) continue; //intersection at vertex
4126       //
4127       if (npoints==2){ 
4128         helixes[i].ParabolicDCA(helixes[j],phase[1][0],phase[1][1],radius[1],delta2);   
4129         if (radius[1]<20&&delta2<1) continue;  //intersection at vertex
4130         if (radius[1]<10&&delta2<3) continue;  //intersection at vertex 
4131       }            
4132       distance1 = TMath::Min(delta1,delta2);
4133       Float_t rkink =0;
4134       if (delta1<delta2){
4135         rkink = TMath::Sqrt(radius[0]);
4136       }
4137       else{
4138         rkink = TMath::Sqrt(radius[1]);
4139       }
4140       if (distance1>cdist2) continue;
4141       //
4142       //
4143       AliTPCseed * track1 = (AliTPCseed*)array->At(j);
4144       //
4145       //
4146       Int_t row0 = GetRowNumber(rkink); 
4147       if (row0<10)  continue;
4148       if (row0>150) continue;
4149       //
4150       //
4151       Float_t dens00=-1,dens01=-1;
4152       Float_t dens10=-1,dens11=-1;
4153       //
4154       Int_t found,foundable,shared;
4155       track0->GetClusterStatistic(0,row0-5, found, foundable,shared,kFALSE);
4156       if (foundable>5) dens00 = Float_t(found)/Float_t(foundable);
4157       track0->GetClusterStatistic(row0+5,155, found, foundable,shared,kFALSE);
4158       if (foundable>5) dens01 = Float_t(found)/Float_t(foundable);
4159       //
4160       track1->GetClusterStatistic(0,row0-5, found, foundable,shared,kFALSE);
4161       if (foundable>10) dens10 = Float_t(found)/Float_t(foundable);
4162       track1->GetClusterStatistic(row0+5,155, found, foundable,shared,kFALSE);
4163       if (foundable>10) dens11 = Float_t(found)/Float_t(foundable);
4164       //     
4165       if (dens00<dens10 && dens01<dens11) continue;
4166       if (dens00>dens10 && dens01>dens11) continue;
4167       if (TMath::Max(dens00,dens10)<0.1)  continue;
4168       if (TMath::Max(dens01,dens11)<0.3)  continue;
4169       //
4170       if (TMath::Min(dens00,dens10)>0.6)  continue;
4171       if (TMath::Min(dens01,dens11)>0.6)  continue;
4172
4173       //
4174       AliTPCseed * ktrack0, *ktrack1;
4175       if (dens00>dens10){
4176         ktrack0 = track0;
4177         ktrack1 = track1;
4178       }
4179       else{
4180         ktrack0 = track1;
4181         ktrack1 = track0;
4182       }
4183       if (TMath::Abs(ktrack0->GetC())>5) continue; // cut on the curvature for mother particle
4184       AliExternalTrackParam paramm(*ktrack0);
4185       AliExternalTrackParam paramd(*ktrack1);
4186       if (row0>60&&ktrack1->GetReference().X()>90.) new (&paramd) AliExternalTrackParam(ktrack1->GetReference()); 
4187       //
4188       //
4189       kink->SetMother(paramm);
4190       kink->SetDaughter(paramd);
4191       kink->Update();
4192
4193       Float_t x[3] = { kink->GetPosition()[0],kink->GetPosition()[1],kink->GetPosition()[2]};
4194       Int_t index[4];
4195       fParam->Transform0to1(x,index);
4196       fParam->Transform1to2(x,index);
4197       row0 = GetRowNumber(x[0]); 
4198
4199       if (kink->GetR()<100) continue;
4200       if (kink->GetR()>240) continue;
4201       if (kink->GetPosition()[2]/kink->GetR()>AliTPCReconstructor::GetCtgRange()) continue;  //out of fiducial volume
4202       if (kink->GetDistance()>cdist3) continue;
4203       Float_t dird = kink->GetDaughterP()[0]*kink->GetPosition()[0]+kink->GetDaughterP()[1]*kink->GetPosition()[1];  // rough direction estimate
4204       if (dird<0) continue;
4205
4206       Float_t dirm = kink->GetMotherP()[0]*kink->GetPosition()[0]+kink->GetMotherP()[1]*kink->GetPosition()[1];  // rough direction estimate
4207       if (dirm<0) continue;
4208       Float_t mpt = TMath::Sqrt(kink->GetMotherP()[0]*kink->GetMotherP()[0]+kink->GetMotherP()[1]*kink->GetMotherP()[1]);
4209       if (mpt<0.2) continue;
4210
4211       if (mpt<1){
4212         //for high momenta momentum not defined well in first iteration
4213         Double_t qt   =  TMath::Sin(kink->GetAngle(2))*ktrack1->P();
4214         if (qt>0.35) continue; 
4215       }
4216       
4217       kink->SetLabel(CookLabel(ktrack0,0.4,0,row0),0);
4218       kink->SetLabel(CookLabel(ktrack1,0.4,row0,160),1);
4219       if (dens00>dens10){
4220         kink->SetTPCDensity(dens00,0,0);
4221         kink->SetTPCDensity(dens01,0,1);
4222         kink->SetTPCDensity(dens10,1,0);
4223         kink->SetTPCDensity(dens11,1,1);
4224         kink->SetIndex(i,0);
4225         kink->SetIndex(j,1);
4226       }
4227       else{
4228         kink->SetTPCDensity(dens10,0,0);
4229         kink->SetTPCDensity(dens11,0,1);
4230         kink->SetTPCDensity(dens00,1,0);
4231         kink->SetTPCDensity(dens01,1,1);
4232         kink->SetIndex(j,0);
4233         kink->SetIndex(i,1);
4234       }
4235
4236       if (mpt<1||kink->GetAngle(2)>0.1){
4237         //      angle and densities  not defined yet
4238         if (kink->GetTPCDensityFactor()<0.8) continue;
4239         if ((2-kink->GetTPCDensityFactor())*kink->GetDistance() >0.25) continue;
4240         if (kink->GetAngle(2)*ktrack0->P()<0.003) continue; //too small angle
4241         if (kink->GetAngle(2)>0.2&&kink->GetTPCDensityFactor()<1.15) continue;
4242         if (kink->GetAngle(2)>0.2&&kink->GetTPCDensity(0,1)>0.05) continue;
4243
4244         Float_t criticalangle = track0->fC22+track0->fC33;
4245         criticalangle+= track1->fC22+track1->fC33;
4246         criticalangle= 3*TMath::Sqrt(criticalangle);
4247         if (criticalangle>0.02) criticalangle=0.02;
4248         if (kink->GetAngle(2)<criticalangle) continue;
4249       }
4250       //
4251       Int_t drow = Int_t(2.+0.5/(0.05+kink->GetAngle(2)));  // overlap region defined
4252       Float_t shapesum =0;
4253       Float_t sum = 0;
4254       for ( Int_t row = row0-drow; row<row0+drow;row++){
4255         if (row<0) continue;
4256         if (row>155) continue;