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