]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCtrackerMI.cxx
7559e077f5287034a651890d366894d14b3f0a8d
[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 $Log$
18 Revision 1.16  2003/10/17 12:01:16  kowal2
19 Removed compiler warning.
20
21 Revision 1.15  2003/09/29 11:56:58  kowal2
22 bug fix2
23
24 Revision 1.14  2003/09/29 11:39:43  kowal2
25 bug fix
26
27 Revision 1.13  2003/09/29 11:28:19  kowal2
28 completly rewritten
29
30 Revision 1.9.4.3  2003/06/23 14:47:10  hristov
31 Minor fix
32
33 Revision 1.9.4.2  2003/06/23 10:06:13  hristov
34 Updated information about the overlapping clusters (M.Ivanov)
35
36 Revision 1.9.4.1  2003/06/19 06:59:58  hristov
37 Updated version of parallel tracking (M.Ivanov)
38
39 Revision 1.9  2003/03/19 17:14:11  hristov
40 Load/UnloadClusters added to the base class and the derived classes changed correspondingly. Possibility to give 2 input files for ITS and TPC tracks in PropagateBack. TRD tracker uses fEventN from the base class (T.Kuhr)
41
42 Revision 1.8  2003/03/05 11:16:15  kowal2
43 Logs added
44
45 */
46
47
48
49
50
51
52
53 /*
54   AliTPC parallel tracker - 
55   How to use?  - 
56   run AliTPCFindClusters.C macro - clusters neccessary for tracker are founded
57   run AliTPCFindTracksMI.C macro - to find tracks
58   tracks are written to AliTPCtracks.root file
59   for comparison also seeds are written to the same file - to special branch
60 */
61
62 //-------------------------------------------------------
63 //          Implementation of the TPC tracker
64 //
65 //   Origin: Marian Ivanov   Marian.Ivanov@cern.ch
66 // 
67 //-------------------------------------------------------
68 #include <TObjArray.h>
69 #include <TFile.h>
70 #include <TTree.h>
71 #include "Riostream.h"
72
73 #include "AliTPCtrackerMI.h"
74 #include "AliTPCclusterMI.h"
75 #include "AliTPCParam.h"
76 #include "AliTPCClustersRow.h"
77 #include "AliComplexCluster.h"
78 #include "AliTPCpolyTrack.h"
79 #include "TStopwatch.h"
80 #include "AliESD.h"
81 #include "AliHelix.h"
82 #include "TGraph.h"
83 //
84 #include "AliRunLoader.h"
85
86
87
88 ClassImp(AliTPCseed)
89 ClassImp(AliTPCtrackerMI)
90
91
92 class TPCFastMath {
93 public:
94   TPCFastMath();  
95   static Double_t FastAsin(Double_t x);   
96  private: 
97   static Double_t fgFastAsin[20000];
98 };
99
100 Double_t TPCFastMath::fgFastAsin[20000];
101
102 TPCFastMath::TPCFastMath(){
103   for (Int_t i=0;i<10000;i++){
104     fgFastAsin[2*i] = TMath::ASin(i/10000.);
105     fgFastAsin[2*i+1] = (TMath::ASin((i+1)/10000.)-fgFastAsin[2*i]);
106   }
107 }
108
109 Double_t TPCFastMath::FastAsin(Double_t x){
110   if (x>0){
111     Int_t index = int(x*10000);
112     return fgFastAsin[2*index]+(x*10000.-index)*fgFastAsin[2*index+1];
113   }
114   x*=-1;
115   Int_t index = int(x*10000);
116   return -(fgFastAsin[2*index]+(x*10000.-index)*fgFastAsin[2*index+1]);
117 }
118
119 TPCFastMath gTPCMath;
120
121
122
123 Int_t AliTPCtrackerMI::UpdateTrack(AliTPCseed * track, Int_t accept){
124
125   AliTPCclusterMI* c =track->fCurrentCluster;
126   if (accept>0) track->fCurrentClusterIndex1 |=0x8000;  //sign not accepted clusters
127
128   UInt_t i = track->fCurrentClusterIndex1;
129
130   Int_t sec=(i&0xff000000)>>24; 
131   //Int_t row = (i&0x00ff0000)>>16; 
132   track->fRow=(i&0x00ff0000)>>16;
133   track->fSector = sec;
134   //  Int_t index = i&0xFFFF;
135   if (sec>=fParam->GetNInnerSector()) track->fRow += fParam->GetNRowLow(); 
136   track->SetClusterIndex2(track->fRow, i);
137   
138   //track->fFirstPoint = row;
139   //if ( track->fLastPoint<row) track->fLastPoint =row;
140   //  if (track->fRow<0 || track->fRow>160) {
141   //  printf("problem\n");
142   //}
143   if (track->fFirstPoint>track->fRow) 
144     track->fFirstPoint = track->fRow;
145   if (track->fLastPoint<track->fRow) 
146     track->fLastPoint  = track->fRow;
147   
148
149   track->fClusterPointer[track->fRow] = c;  
150   //
151
152   Float_t angle2 = track->GetSnp()*track->GetSnp();
153   angle2 = TMath::Sqrt(angle2/(1-angle2)); 
154   //
155   //SET NEW Track Point
156   //
157   //  if (debug)
158   {
159     AliTPCTrackerPoint   &point =*(track->GetTrackPoint(track->fRow));
160     //
161     point.SetSigmaY(c->GetSigmaY2()/track->fCurrentSigmaY2);
162     point.SetSigmaZ(c->GetSigmaZ2()/track->fCurrentSigmaZ2);
163     point.SetErrY(sqrt(track->fErrorY2));
164     point.SetErrZ(sqrt(track->fErrorZ2));
165     //
166     point.SetX(track->GetX());
167     point.SetY(track->GetY());
168     point.SetZ(track->GetZ());
169     point.SetAngleY(angle2);
170     point.SetAngleZ(track->GetTgl());
171     if (point.fIsShared){
172       track->fErrorY2 *= 4;
173       track->fErrorZ2 *= 4;
174     }
175   }  
176
177   Double_t chi2 = track->GetPredictedChi2(track->fCurrentCluster);
178   //
179   track->fErrorY2 *= 1.3;
180   track->fErrorY2 += 0.01;    
181   track->fErrorZ2 *= 1.3;   
182   track->fErrorZ2 += 0.005;      
183     //}
184   if (accept>0) return 0;
185   if (track->GetNumberOfClusters()%20==0){
186     //    if (track->fHelixIn){
187     //  TClonesArray & larr = *(track->fHelixIn);    
188     //  Int_t ihelix = larr.GetEntriesFast();
189     //  new(larr[ihelix]) AliHelix(*track) ;    
190     //}
191   }
192   track->fNoCluster =0;
193   return track->Update(c,chi2,i);
194 }
195
196
197
198 Int_t AliTPCtrackerMI::AcceptCluster(AliTPCseed * seed, AliTPCclusterMI * cluster, Float_t factor, 
199                                       Float_t cory, Float_t corz)
200 {
201   //
202   // decide according desired precision to accept given 
203   // cluster for tracking
204   Double_t sy2=ErrY2(seed,cluster)*cory;
205   Double_t sz2=ErrZ2(seed,cluster)*corz;
206   //sy2=ErrY2(seed,cluster)*cory;
207   //sz2=ErrZ2(seed,cluster)*cory;
208   
209   Double_t sdistancey2 = sy2+seed->GetSigmaY2();
210   Double_t sdistancez2 = sz2+seed->GetSigmaZ2();
211   
212   Double_t rdistancey2 = (seed->fCurrentCluster->GetY()-seed->GetY())*
213     (seed->fCurrentCluster->GetY()-seed->GetY())/sdistancey2;
214   Double_t rdistancez2 = (seed->fCurrentCluster->GetZ()-seed->GetZ())*
215     (seed->fCurrentCluster->GetZ()-seed->GetZ())/sdistancez2;
216   
217   Double_t rdistance2  = rdistancey2+rdistancez2;
218   //Int_t  accept =0;
219   
220   if (rdistance2>16) return 3;
221   
222   
223   if ((rdistancey2>9.*factor || rdistancez2>9.*factor) && cluster->GetType()==0)  
224     return 2;  //suspisiouce - will be changed
225   
226   if ((rdistancey2>6.25*factor || rdistancez2>6.25*factor) && cluster->GetType()>0)  
227     // strict cut on overlaped cluster
228     return  2;  //suspisiouce - will be changed
229   
230   if ( (rdistancey2>1.*factor || rdistancez2>6.25*factor ) 
231        && cluster->GetType()<0){
232     seed->fNFoundable--;
233     return 2;    
234   }
235   return 0;
236 }
237
238
239
240
241 //_____________________________________________________________________________
242 AliTPCtrackerMI::AliTPCtrackerMI(const AliTPCParam *par): 
243 AliTracker(), fkNIS(par->GetNInnerSector()/2), fkNOS(par->GetNOuterSector()/2)
244 {
245   //---------------------------------------------------------------------
246   // The main TPC tracker constructor
247   //---------------------------------------------------------------------
248   fInnerSec=new AliTPCSector[fkNIS];         
249   fOuterSec=new AliTPCSector[fkNOS];
250  
251   Int_t i;
252   for (i=0; i<fkNIS; i++) fInnerSec[i].Setup(par,0);
253   for (i=0; i<fkNOS; i++) fOuterSec[i].Setup(par,1);
254
255   fN=0;  fSectors=0;
256
257   fSeeds=0;
258   fNtracks = 0;
259   fParam = par;  
260   Int_t nrowlow = par->GetNRowLow();
261   Int_t nrowup = par->GetNRowUp();
262
263   
264   for (Int_t i=0;i<nrowlow;i++){
265     fXRow[i]     = par->GetPadRowRadiiLow(i);
266     fPadLength[i]= par->GetPadPitchLength(0,i);
267     fYMax[i]     = fXRow[i]*TMath::Tan(0.5*par->GetInnerAngle());
268   }
269
270   
271   for (Int_t i=0;i<nrowup;i++){
272     fXRow[i+nrowlow]      = par->GetPadRowRadiiUp(i);
273     fPadLength[i+nrowlow] = par->GetPadPitchLength(60,i);
274     fYMax[i+nrowlow]      = fXRow[i+nrowlow]*TMath::Tan(0.5*par->GetOuterAngle());
275   }
276   fSeeds=0;
277   //
278   fInput    = 0;
279   fOutput   = 0;
280   fSeedTree = 0;
281   fTreeDebug =0;
282   fNewIO     =0;
283   fDebug     =0;
284   fEvent     =0;
285 }
286
287 //_____________________________________________________________________________
288 AliTPCtrackerMI::~AliTPCtrackerMI() {
289   //------------------------------------------------------------------
290   // TPC tracker destructor
291   //------------------------------------------------------------------
292   delete[] fInnerSec;
293   delete[] fOuterSec;
294   if (fSeeds) {
295     fSeeds->Delete(); 
296     delete fSeeds;
297   }
298 }
299
300 void AliTPCtrackerMI::SetIO()
301 {
302   //
303   fNewIO   =  kTRUE;
304   fInput   =  AliRunLoader::GetTreeR("TPC", kFALSE,AliConfig::fgkDefaultEventFolderName);
305   fOutput  =  AliRunLoader::GetTreeT("TPC", kTRUE,AliConfig::fgkDefaultEventFolderName);
306   AliTPCtrack *iotrack= new AliTPCtrack;
307   //    iotrack->fHelixIn   = new TClonesArray("AliHelix");
308   //iotrack->fHelixOut  = new TClonesArray("AliHelix");    
309   fOutput->Branch("tracks","AliTPCtrack",&iotrack,32000,100);
310   delete iotrack;
311 }
312
313 void AliTPCtrackerMI::SetIO(TTree * input, TTree * output, AliESD * event)
314 {
315
316   // set input
317   fNewIO = kFALSE;
318   fInput    = 0;
319   fOutput   = 0;
320   fSeedTree = 0;
321   fTreeDebug =0;
322   fInput = input;
323   if (input==0){
324     return;
325   }  
326   //set output
327   fOutput = output;
328   if (output){
329     AliTPCtrack *iotrack= new AliTPCtrack;
330     //    iotrack->fHelixIn   = new TClonesArray("AliHelix");
331     //iotrack->fHelixOut  = new TClonesArray("AliHelix");    
332     fOutput->Branch("tracks","AliTPCtrack",&iotrack,32000,100);
333     delete iotrack;
334   }
335   if (output && (fDebug&2)){
336     //write the full seed information if specified in debug mode
337     //
338     fSeedTree =  new TTree("Seeds","Seeds");
339     AliTPCseed * vseed = new AliTPCseed;
340     //
341     TClonesArray * arrtr = new TClonesArray("AliTPCTrackPoint",160);
342     arrtr->ExpandCreateFast(160);
343     TClonesArray * arre = new TClonesArray("AliTPCExactPoint",160);
344     //
345     vseed->fPoints = arrtr;
346     vseed->fEPoints = arre;
347     //    vseed->fClusterPoints = arrcl;
348     fSeedTree->Branch("seeds","AliTPCseed",&vseed,32000,99);
349     delete arrtr;
350     delete arre;    
351     fTreeDebug = new TTree("trackDebug","trackDebug");
352     TClonesArray * arrd = new TClonesArray("AliTPCTrackPoint2",0);
353     fTreeDebug->Branch("debug",&arrd,32000,99);
354   }
355
356
357   //set ESD event  
358   fEvent  = event;  
359 }
360
361 void AliTPCtrackerMI::WriteTracks()
362 {
363   //
364   // write tracks to the given output tree -
365   // output specified with SetIO routine
366   if (!fSeeds)  return;
367   if (fEvent){
368     // write tracks to the event
369     // store index of the track
370     Int_t nseed=fSeeds->GetEntriesFast();
371     for (Int_t i=0; i<nseed; i++) {
372       AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i);    
373       if (!pt) continue; 
374       AliESDtrack iotrack;
375       iotrack.UpdateTrackParams(pt,AliESDtrack::kTPCin);        
376       //iotrack.SetTPCindex(i);
377       fEvent->AddTrack(&iotrack);         
378     }
379   }
380
381   
382   if (fOutput){
383     AliTPCtrack *iotrack= 0;
384     Int_t nseed=fSeeds->GetEntriesFast();
385     for (Int_t i=0; i<nseed; i++) {
386       iotrack= (AliTPCtrack*)fSeeds->UncheckedAt(i);
387       if (iotrack) break;      
388     }
389     
390     //TBranch * br = fOutput->Branch("tracks","AliTPCtrack",&iotrack,32000,100);
391     TBranch * br = fOutput->GetBranch("tracks");
392     br->SetAddress(&iotrack);
393     //
394     for (Int_t i=0; i<nseed; i++) {
395       AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i);    
396       if (!pt) continue;    
397       iotrack = pt;
398       pt->fLab2 =i; 
399       //      br->SetAddress(&iotrack);
400       fOutput->Fill();
401       iotrack =0;
402     }
403   }
404     // delete iotrack;
405     //
406   if (fSeedTree){
407     //write the full seed information if specified in debug mode
408       
409     AliTPCseed * vseed = new AliTPCseed;
410     //
411     TClonesArray * arrtr = new TClonesArray("AliTPCTrackPoint",160);
412     arrtr->ExpandCreateFast(160);
413     //TClonesArray * arrcl = new TClonesArray("AliTPCclusterMI",160);
414     //arrcl->ExpandCreateFast(160);
415     TClonesArray * arre = new TClonesArray("AliTPCExactPoint",160);
416     //
417     vseed->fPoints = arrtr;
418     vseed->fEPoints = arre;
419     //    vseed->fClusterPoints = arrcl;
420     //TBranch * brseed = seedtree->Branch("seeds","AliTPCseed",&vseed,32000,99);
421     TBranch * brseed = fSeedTree->GetBranch("seeds");
422     
423     Int_t nseed=fSeeds->GetEntriesFast();
424     
425     for (Int_t i=0; i<nseed; i++) {
426       AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i);    
427       if (!pt) continue;     
428       pt->fPoints = arrtr;
429       //      pt->fClusterPoints = arrcl;
430       pt->fEPoints       = arre;
431       pt->RebuildSeed();
432       vseed = pt;
433       brseed->SetAddress(&vseed);
434       fSeedTree->Fill();
435       pt->fPoints  = 0;
436       pt->fEPoints = 0;
437       //      pt->fClusterPoints = 0;
438     }
439     fSeedTree->Write();
440     if (fTreeDebug) fTreeDebug->Write();
441   }
442
443 }
444   
445
446
447
448 Double_t AliTPCtrackerMI::ErrY2(AliTPCseed* seed, AliTPCclusterMI * cl){
449   //
450   //
451   //seed->SetErrorY2(0.1);
452   //return 0.1;
453   //calculate look-up table at the beginning
454   static Bool_t  ginit = kFALSE;
455   static Float_t gnoise1,gnoise2,gnoise3;
456   static Float_t ggg1[10000];
457   static Float_t ggg2[10000];
458   static Float_t ggg3[10000];
459   static Float_t glandau1[10000];
460   static Float_t glandau2[10000];
461   static Float_t glandau3[10000];
462   //
463   static Float_t gcor01[500];
464   static Float_t gcor02[500];
465   static Float_t gcorp[500];
466   //
467
468   //
469   if (ginit==kFALSE){
470     for (Int_t i=1;i<500;i++){
471       Float_t rsigma = float(i)/100.;
472       gcor02[i] = TMath::Max(0.78 +TMath::Exp(7.4*(rsigma-1.2)),0.6);
473       gcor01[i] = TMath::Max(0.72 +TMath::Exp(3.36*(rsigma-1.2)),0.6);
474       gcorp[i]  = TMath::Max(TMath::Power((rsigma+0.5),1.5),1.2);
475     }
476
477     //
478     for (Int_t i=3;i<10000;i++){
479       //
480       //
481       // inner sector
482       Float_t amp = float(i);
483       Float_t padlength =0.75;
484       gnoise1 = 0.0004/padlength;
485       Float_t nel     = 0.268*amp;
486       Float_t nprim   = 0.155*amp;
487       ggg1[i]          = fParam->GetDiffT()*fParam->GetDiffT()*(2+0.001*nel/(padlength*padlength))/nel;
488       glandau1[i]      = (2.+0.12*nprim)*0.5* (2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
489       if (glandau1[i]>1) glandau1[i]=1;
490       glandau1[i]*=padlength*padlength/12.;      
491       //
492       // outer short
493       padlength =1.;
494       gnoise2   = 0.0004/padlength;
495       nel       = 0.3*amp;
496       nprim     = 0.133*amp;
497       ggg2[i]      = fParam->GetDiffT()*fParam->GetDiffT()*(2+0.0008*nel/(padlength*padlength))/nel;
498       glandau2[i]  = (2.+0.12*nprim)*0.5*(2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
499       if (glandau2[i]>1) glandau2[i]=1;
500       glandau2[i]*=padlength*padlength/12.;
501       //
502       //
503       // outer long
504       padlength =1.5;
505       gnoise3   = 0.0004/padlength;
506       nel       = 0.3*amp;
507       nprim     = 0.133*amp;
508       ggg3[i]      = fParam->GetDiffT()*fParam->GetDiffT()*(2+0.0008*nel/(padlength*padlength))/nel;
509       glandau3[i]  = (2.+0.12*nprim)*0.5*(2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
510       if (glandau3[i]>1) glandau3[i]=1;
511       glandau3[i]*=padlength*padlength/12.;
512       //
513     }
514     ginit = kTRUE;
515   }
516   //
517   //
518   //
519   Int_t amp = int(TMath::Abs(cl->GetQ()));  
520   if (amp>9999) {
521     seed->SetErrorY2(1.);
522     return 1.;
523   }
524   Float_t snoise2;
525   Float_t z = TMath::Abs(fParam->GetZLength()-TMath::Abs(seed->GetZ()));
526   Int_t ctype = cl->GetType();  
527   Float_t padlength= GetPadPitchLength(seed->fRow);
528   Float_t angle2 = seed->GetSnp()*seed->GetSnp();
529   angle2 = angle2/(1-angle2); 
530   //
531   //cluster "quality"
532   Int_t rsigmay = int(100.*cl->GetSigmaY2()/(seed->fCurrentSigmaY2));
533   Float_t res;
534   //
535   if (fSectors==fInnerSec){
536     snoise2 = gnoise1;
537     res     = ggg1[amp]*z+glandau1[amp]*angle2;     
538     if (ctype==0) res *= gcor01[rsigmay];
539     if ((ctype>0)){
540       res+=0.002;
541       res*= gcorp[rsigmay];
542     }
543   }
544   else {
545     if (padlength<1.1){
546       snoise2 = gnoise2;
547       res     = ggg2[amp]*z+glandau2[amp]*angle2; 
548       if (ctype==0) res *= gcor02[rsigmay];      
549       if ((ctype>0)){
550         res+=0.002;
551         res*= gcorp[rsigmay];
552       }
553     }
554     else{
555       snoise2 = gnoise3;      
556       res     = ggg3[amp]*z+glandau3[amp]*angle2; 
557       if (ctype==0) res *= gcor02[rsigmay];
558       if ((ctype>0)){
559         res+=0.002;
560         res*= gcorp[rsigmay];
561       }
562     }
563   }  
564
565   if (ctype<0){
566     res+=0.005;
567     res*=2.4;  // overestimate error 2 times
568   }
569   res+= snoise2;
570  
571   if (res<2*snoise2)
572     res = 2*snoise2;
573   
574   seed->SetErrorY2(res);
575   return res;
576
577
578 }
579
580
581
582 Double_t AliTPCtrackerMI::ErrZ2(AliTPCseed* seed, AliTPCclusterMI * cl){
583   //
584   //
585   //seed->SetErrorY2(0.1);
586   //return 0.1;
587   //calculate look-up table at the beginning
588   static Bool_t  ginit = kFALSE;
589   static Float_t gnoise1,gnoise2,gnoise3;
590   static Float_t ggg1[10000];
591   static Float_t ggg2[10000];
592   static Float_t ggg3[10000];
593   static Float_t glandau1[10000];
594   static Float_t glandau2[10000];
595   static Float_t glandau3[10000];
596   //
597   static Float_t gcor01[1000];
598   static Float_t gcor02[1000];
599   static Float_t gcorp[1000];
600   //
601
602   //
603   if (ginit==kFALSE){
604     for (Int_t i=1;i<1000;i++){
605       Float_t rsigma = float(i)/100.;
606       gcor02[i] = TMath::Max(0.81 +TMath::Exp(6.8*(rsigma-1.2)),0.6);
607       gcor01[i] = TMath::Max(0.72 +TMath::Exp(2.04*(rsigma-1.2)),0.6);
608       gcorp[i]  = TMath::Max(TMath::Power((rsigma+0.5),1.5),1.2);
609     }
610
611     //
612     for (Int_t i=3;i<10000;i++){
613       //
614       //
615       // inner sector
616       Float_t amp = float(i);
617       Float_t padlength =0.75;
618       gnoise1 = 0.0004/padlength;
619       Float_t nel     = 0.268*amp;
620       Float_t nprim   = 0.155*amp;
621       ggg1[i]          = fParam->GetDiffT()*fParam->GetDiffT()*(2+0.001*nel/(padlength*padlength))/nel;
622       glandau1[i]      = (2.+0.12*nprim)*0.5* (2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
623       if (glandau1[i]>1) glandau1[i]=1;
624       glandau1[i]*=padlength*padlength/12.;      
625       //
626       // outer short
627       padlength =1.;
628       gnoise2   = 0.0004/padlength;
629       nel       = 0.3*amp;
630       nprim     = 0.133*amp;
631       ggg2[i]      = fParam->GetDiffT()*fParam->GetDiffT()*(2+0.0008*nel/(padlength*padlength))/nel;
632       glandau2[i]  = (2.+0.12*nprim)*0.5*(2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
633       if (glandau2[i]>1) glandau2[i]=1;
634       glandau2[i]*=padlength*padlength/12.;
635       //
636       //
637       // outer long
638       padlength =1.5;
639       gnoise3   = 0.0004/padlength;
640       nel       = 0.3*amp;
641       nprim     = 0.133*amp;
642       ggg3[i]      = fParam->GetDiffT()*fParam->GetDiffT()*(2+0.0008*nel/(padlength*padlength))/nel;
643       glandau3[i]  = (2.+0.12*nprim)*0.5*(2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
644       if (glandau3[i]>1) glandau3[i]=1;
645       glandau3[i]*=padlength*padlength/12.;
646       //
647     }
648     ginit = kTRUE;
649   }
650   //
651   //
652   //
653   Int_t amp = int(TMath::Abs(cl->GetQ()));  
654   if (amp>9999) {
655     seed->SetErrorY2(1.);
656     return 1.;
657   }
658   Float_t snoise2;
659   Float_t z = TMath::Abs(fParam->GetZLength()-TMath::Abs(seed->GetZ()));
660   Int_t ctype = cl->GetType();  
661   Float_t padlength= GetPadPitchLength(seed->fRow);
662   //
663   Float_t angle2 = seed->GetSnp()*seed->GetSnp();
664   //  if (angle2<0.6) angle2 = 0.6;
665   angle2 = seed->GetTgl()*seed->GetTgl()*(1+angle2/(1-angle2)); 
666   //
667   //cluster "quality"
668   Int_t rsigmaz = int(100.*cl->GetSigmaZ2()/(seed->fCurrentSigmaZ2));
669   Float_t res;
670   //
671   if (fSectors==fInnerSec){
672     snoise2 = gnoise1;
673     res     = ggg1[amp]*z+glandau1[amp]*angle2;     
674     if (ctype==0) res *= gcor01[rsigmaz];
675     if ((ctype>0)){
676       res+=0.002;
677       res*= gcorp[rsigmaz];
678     }
679   }
680   else {
681     if (padlength<1.1){
682       snoise2 = gnoise2;
683       res     = ggg2[amp]*z+glandau2[amp]*angle2; 
684       if (ctype==0) res *= gcor02[rsigmaz];      
685       if ((ctype>0)){
686         res+=0.002;
687         res*= gcorp[rsigmaz];
688       }
689     }
690     else{
691       snoise2 = gnoise3;      
692       res     = ggg3[amp]*z+glandau3[amp]*angle2; 
693       if (ctype==0) res *= gcor02[rsigmaz];
694       if ((ctype>0)){
695         res+=0.002;
696         res*= gcorp[rsigmaz];
697       }
698     }
699   }  
700
701   if (ctype<0){
702     res+=0.002;
703     res*=1.3;
704   }
705   if ((ctype<0) &&amp<70){
706     res+=0.002;
707     res*=1.3;  
708   }
709   res += snoise2;
710   if (res<2*snoise2)
711      res = 2*snoise2;
712   if (res>3) res =3;
713   seed->SetErrorZ2(res);
714   return res;
715 }
716
717
718
719 /*
720 Double_t AliTPCtrackerMI::ErrZ2(AliTPCseed* seed, AliTPCclusterMI * cl){
721   //
722   //
723   //seed->SetErrorZ2(0.1);
724   //return 0.1;
725
726   Float_t snoise2;
727   Float_t z = TMath::Abs(fParam->GetZLength()-TMath::Abs(seed->GetZ()));
728   //
729   Float_t rsigmaz = cl->GetSigmaZ2()/(seed->fCurrentSigmaZ2);
730   Int_t ctype = cl->GetType();
731   Float_t amp = TMath::Abs(cl->GetQ());
732   
733   Float_t nel;
734   Float_t nprim;
735   //
736   Float_t landau=2 ;    //landau fluctuation part
737   Float_t gg=2;         // gg fluctuation part
738   Float_t padlength= GetPadPitchLength(seed->GetX());
739  
740   if (fSectors==fInnerSec){
741     snoise2 = 0.0004/padlength;
742     nel     = 0.268*amp;
743     nprim   = 0.155*amp;
744     gg      = (2+0.001*nel/(padlength*padlength))/nel;
745     landau  = (2.+0.12*nprim)*0.5*(2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
746     if (landau>1) landau=1;
747   }
748   else {
749     snoise2 = 0.0004/padlength;
750     nel     = 0.3*amp;
751     nprim   = 0.133*amp;
752     gg      = (2+0.0008*nel/(padlength*padlength))/nel;
753     landau  = (2.+0.12*nprim)*0.5*(2.+nprim*nprim*0.001/(padlength*padlength))/nprim;
754     if (landau>1) landau=1;
755   }
756   Float_t sdiff = gg*fParam->GetDiffT()*fParam->GetDiffT()*z;
757
758   //
759   Float_t angle2 = seed->GetSnp()*seed->GetSnp();
760   angle2 = TMath::Sqrt((1-angle2));
761   if (angle2<0.6) angle2 = 0.6;
762   //angle2 = 1;
763
764   Float_t angle = seed->GetTgl()/angle2;
765   Float_t angular = landau*angle*angle*padlength*padlength/12.;
766   Float_t res = sdiff + angular;
767
768   
769   if ((ctype==0) && (fSectors ==fOuterSec))
770     res *= 0.81 +TMath::Exp(6.8*(rsigmaz-1.2));
771
772   if ((ctype==0) && (fSectors ==fInnerSec))
773     res *= 0.72 +TMath::Exp(2.04*(rsigmaz-1.2));
774   
775   if ((ctype>0)){
776     res+=0.005;
777     res*= TMath::Power(rsigmaz+0.5,1.5);  //0.31+0.147*ctype;
778   }
779   if (ctype<0){
780     res+=0.002;
781     res*=1.3;
782   }
783   if ((ctype<0) &&amp<70){
784     res+=0.002;
785     res*=1.3;  
786   }
787   res += snoise2;
788   if (res<2*snoise2)
789      res = 2*snoise2;
790
791   seed->SetErrorZ2(res);
792   return res;
793 }
794 */
795
796
797
798 void AliTPCseed::Reset(Bool_t all)
799 {
800   //
801   //
802   SetNumberOfClusters(0);
803   fNFoundable = 0;
804   SetChi2(0);
805   ResetCovariance();
806   /*
807   if (fTrackPoints){
808     for (Int_t i=0;i<8;i++){
809       delete [] fTrackPoints[i];
810     }
811     delete fTrackPoints;
812     fTrackPoints =0;
813   }
814   */
815
816   if (all){   
817     for (Int_t i=0;i<200;i++) SetClusterIndex2(i,-3);
818     for (Int_t i=0;i<160;i++) fClusterPointer[i]=0;
819   }
820
821 }
822
823
824 void AliTPCseed::Modify(Double_t factor)
825 {
826
827   //------------------------------------------------------------------
828   //This function makes a track forget its history :)  
829   //------------------------------------------------------------------
830   if (factor<=0) {
831     ResetCovariance();
832     return;
833   }
834   fC00*=factor;
835   fC10*=factor;  fC11*=factor;
836   fC20*=factor;  fC21*=factor;  fC22*=factor;
837   fC30*=factor;  fC31*=factor;  fC32*=factor;  fC33*=factor;
838   fC40*=factor;  fC41*=factor;  fC42*=factor;  fC43*=factor;  fC44*=factor;
839   SetNumberOfClusters(0);
840   fNFoundable =0;
841   SetChi2(0);
842   fRemoval = 0;
843   fCurrentSigmaY2 = 0.000005;
844   fCurrentSigmaZ2 = 0.000005;
845   fNoCluster     = 0;
846   //fFirstPoint = 160;
847   //fLastPoint  = 0;
848 }
849
850
851
852
853 Int_t  AliTPCseed::GetProlongation(Double_t xk, Double_t &y, Double_t & z) const
854 {
855   //-----------------------------------------------------------------
856   // This function find proloncation of a track to a reference plane x=xk.
857   // doesn't change internal state of the track
858   //-----------------------------------------------------------------
859   
860   Double_t x1=fX, x2=x1+(xk-x1), dx=x2-x1;
861
862   if (TMath::Abs(fP4*xk - fP2) >= 0.999) {   
863     return 0;
864   }
865
866   //  Double_t y1=fP0, z1=fP1;
867   Double_t c1=fP4*x1 - fP2, r1=sqrt(1.- c1*c1);
868   Double_t c2=fP4*x2 - fP2, r2=sqrt(1.- c2*c2);
869   
870   y = fP0;
871   z = fP1;
872   //y += dx*(c1+c2)/(r1+r2);
873   //z += dx*(c1+c2)/(c1*r2 + c2*r1)*fP3;
874   
875   Double_t dy = dx*(c1+c2)/(r1+r2);
876   Double_t dz = 0;
877   //
878   Double_t delta = fP4*dx*(c1+c2)/(c1*r2 + c2*r1);
879   /*
880   if (TMath::Abs(delta)>0.0001){
881     dz = fP3*TMath::ASin(delta)/fP4;
882   }else{
883     dz = dx*fP3*(c1+c2)/(c1*r2 + c2*r1);
884   }
885   */
886   dz =  fP3*TPCFastMath::FastAsin(delta)/fP4;
887   //
888   y+=dy;
889   z+=dz;
890   
891
892   return 1;  
893 }
894
895
896 //_____________________________________________________________________________
897 Double_t AliTPCseed::GetPredictedChi2(const AliTPCclusterMI *c) const 
898 {
899   //-----------------------------------------------------------------
900   // This function calculates a predicted chi2 increment.
901   //-----------------------------------------------------------------
902   //Double_t r00=c->GetSigmaY2(), r01=0., r11=c->GetSigmaZ2();
903   Double_t r00=fErrorY2, r01=0., r11=fErrorZ2;
904   r00+=fC00; r01+=fC10; r11+=fC11;
905
906   Double_t det=r00*r11 - r01*r01;
907   if (TMath::Abs(det) < 1.e-10) {
908     Int_t n=GetNumberOfClusters();
909     if (n>4) cerr<<n<<" AliKalmanTrack warning: Singular matrix !\n";
910     return 1e10;
911   }
912   Double_t tmp=r00; r00=r11; r11=tmp; r01=-r01;
913   
914   Double_t dy=c->GetY() - fP0, dz=c->GetZ() - fP1;
915   
916   return (dy*r00*dy + 2*r01*dy*dz + dz*r11*dz)/det;
917 }
918
919
920 //_________________________________________________________________________________________
921
922
923 Int_t AliTPCseed::Compare(const TObject *o) const {
924   //-----------------------------------------------------------------
925   // This function compares tracks according to the sector - for given sector according z
926   //-----------------------------------------------------------------
927   AliTPCseed *t=(AliTPCseed*)o;
928
929   if (fSort == 0){
930     if (t->fRelativeSector>fRelativeSector) return -1;
931     if (t->fRelativeSector<fRelativeSector) return 1;
932     Double_t z2 = t->GetZ();
933     Double_t z1 = GetZ();
934     if (z2>z1) return 1;
935     if (z2<z1) return -1;
936     return 0;
937   }
938   else {
939     Float_t f2 =1;
940     f2 = 1-20*TMath::Sqrt(t->fC44)/(TMath::Abs(t->GetC())+0.0066);
941     if (t->fBConstrain) f2=1.2;
942
943     Float_t f1 =1;
944     f1 = 1-20*TMath::Sqrt(fC44)/(TMath::Abs(GetC())+0.0066);
945
946     if (fBConstrain)   f1=1.2;
947  
948     if (t->GetNumberOfClusters()*f2 <GetNumberOfClusters()*f1) return -1;
949     else return +1;
950   }
951 }
952
953 void AliTPCtrackerMI::RotateToLocal(AliTPCseed *seed)
954 {
955   //rotate to track "local coordinata
956   Float_t x = seed->GetX();
957   Float_t y = seed->GetY();
958   Float_t ymax = x*TMath::Tan(0.5*fSectors->GetAlpha());
959   
960   if (y > ymax) {
961     seed->fRelativeSector= (seed->fRelativeSector+1) % fN;
962     if (!seed->Rotate(fSectors->GetAlpha())) 
963       return;
964   } else if (y <-ymax) {
965     seed->fRelativeSector= (seed->fRelativeSector-1+fN) % fN;
966     if (!seed->Rotate(-fSectors->GetAlpha())) 
967       return;
968   }   
969
970 }
971
972
973
974
975 //_____________________________________________________________________________
976 Int_t AliTPCseed::Update(const AliTPCclusterMI *c, Double_t chisq, UInt_t /*index*/) {
977   //-----------------------------------------------------------------
978   // This function associates a cluster with this track.
979   //-----------------------------------------------------------------
980   Double_t r00=fErrorY2, r01=0., r11=fErrorZ2;
981
982   r00+=fC00; r01+=fC10; r11+=fC11;
983   Double_t det=r00*r11 - r01*r01;
984   Double_t tmp=r00; r00=r11/det; r11=tmp/det; r01=-r01/det;
985
986   Double_t k00=fC00*r00+fC10*r01, k01=fC00*r01+fC10*r11;
987   Double_t k10=fC10*r00+fC11*r01, k11=fC10*r01+fC11*r11;
988   Double_t k20=fC20*r00+fC21*r01, k21=fC20*r01+fC21*r11;
989   Double_t k30=fC30*r00+fC31*r01, k31=fC30*r01+fC31*r11;
990   Double_t k40=fC40*r00+fC41*r01, k41=fC40*r01+fC41*r11;
991
992   Double_t dy=c->GetY() - fP0, dz=c->GetZ() - fP1;
993   Double_t cur=fP4 + k40*dy + k41*dz, eta=fP2 + k20*dy + k21*dz;
994   if (TMath::Abs(cur*fX-eta) >= 0.9) {
995     return 0;
996   }
997
998   fP0 += k00*dy + k01*dz;
999   fP1 += k10*dy + k11*dz;
1000   fP2  = eta;
1001   fP3 += k30*dy + k31*dz;
1002   fP4  = cur;
1003
1004   Double_t c01=fC10, c02=fC20, c03=fC30, c04=fC40;
1005   Double_t c12=fC21, c13=fC31, c14=fC41;
1006
1007   fC00-=k00*fC00+k01*fC10; fC10-=k00*c01+k01*fC11;
1008   fC20-=k00*c02+k01*c12;   fC30-=k00*c03+k01*c13;
1009   fC40-=k00*c04+k01*c14; 
1010
1011   fC11-=k10*c01+k11*fC11;
1012   fC21-=k10*c02+k11*c12;   fC31-=k10*c03+k11*c13;
1013   fC41-=k10*c04+k11*c14; 
1014
1015   fC22-=k20*c02+k21*c12;   fC32-=k20*c03+k21*c13;
1016   fC42-=k20*c04+k21*c14; 
1017
1018   fC33-=k30*c03+k31*c13;
1019   fC43-=k40*c03+k41*c13; 
1020
1021   fC44-=k40*c04+k41*c14; 
1022
1023   Int_t n=GetNumberOfClusters();
1024   //  fIndex[n]=index;
1025   SetNumberOfClusters(n+1);
1026   SetChi2(GetChi2()+chisq);
1027
1028   return 1;
1029 }
1030
1031
1032
1033 //_____________________________________________________________________________
1034 Double_t AliTPCtrackerMI::f1old(Double_t x1,Double_t y1,
1035                    Double_t x2,Double_t y2,
1036                    Double_t x3,Double_t y3) 
1037 {
1038   //-----------------------------------------------------------------
1039   // Initial approximation of the track curvature
1040   //-----------------------------------------------------------------
1041   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
1042   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
1043                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
1044   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
1045                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
1046
1047   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
1048   if ( xr*xr+yr*yr<=0.00000000000001) return 100;
1049   return -xr*yr/sqrt(xr*xr+yr*yr); 
1050 }
1051
1052
1053
1054 //_____________________________________________________________________________
1055 Double_t AliTPCtrackerMI::f1(Double_t x1,Double_t y1,
1056                    Double_t x2,Double_t y2,
1057                    Double_t x3,Double_t y3) 
1058 {
1059   //-----------------------------------------------------------------
1060   // Initial approximation of the track curvature
1061   //-----------------------------------------------------------------
1062   x3 -=x1;
1063   x2 -=x1;
1064   y3 -=y1;
1065   y2 -=y1;
1066   //  
1067   Double_t det = x3*y2-x2*y3;
1068   if (det==0) {
1069     return 100;
1070   }
1071   //
1072   Double_t u = 0.5* (x2*(x2-x3)+y2*(y2-y3))/det;
1073   Double_t x0 = x3*0.5-y3*u;
1074   Double_t y0 = y3*0.5+x3*u;
1075   Double_t c2 = 1/TMath::Sqrt(x0*x0+y0*y0);
1076   if (det<0) c2*=-1;
1077   return c2;
1078 }
1079
1080
1081 Double_t AliTPCtrackerMI::f2(Double_t x1,Double_t y1,
1082                    Double_t x2,Double_t y2,
1083                    Double_t x3,Double_t y3) 
1084 {
1085   //-----------------------------------------------------------------
1086   // Initial approximation of the track curvature
1087   //-----------------------------------------------------------------
1088   x3 -=x1;
1089   x2 -=x1;
1090   y3 -=y1;
1091   y2 -=y1;
1092   //  
1093   Double_t det = x3*y2-x2*y3;
1094   if (det==0) {
1095     return 100;
1096   }
1097   //
1098   Double_t u = 0.5* (x2*(x2-x3)+y2*(y2-y3))/det;
1099   Double_t x0 = x3*0.5-y3*u; 
1100   Double_t y0 = y3*0.5+x3*u;
1101   Double_t c2 = 1/TMath::Sqrt(x0*x0+y0*y0);
1102   if (det<0) c2*=-1;
1103   x0+=x1;
1104   x0*=c2;  
1105   return x0;
1106 }
1107
1108
1109
1110 //_____________________________________________________________________________
1111 Double_t AliTPCtrackerMI::f2old(Double_t x1,Double_t y1,
1112                    Double_t x2,Double_t y2,
1113                    Double_t x3,Double_t y3) 
1114 {
1115   //-----------------------------------------------------------------
1116   // Initial approximation of the track curvature times center of curvature
1117   //-----------------------------------------------------------------
1118   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
1119   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
1120                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
1121   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
1122                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
1123
1124   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
1125   
1126   return -a/(d*y1-b)*xr/sqrt(xr*xr+yr*yr);
1127 }
1128
1129 //_____________________________________________________________________________
1130 Double_t AliTPCtrackerMI::f3(Double_t x1,Double_t y1, 
1131                    Double_t x2,Double_t y2,
1132                    Double_t z1,Double_t z2) 
1133 {
1134   //-----------------------------------------------------------------
1135   // Initial approximation of the tangent of the track dip angle
1136   //-----------------------------------------------------------------
1137   return (z1 - z2)/sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
1138 }
1139
1140
1141 Double_t AliTPCtrackerMI::f3n(Double_t x1,Double_t y1, 
1142                    Double_t x2,Double_t y2,
1143                    Double_t z1,Double_t z2, Double_t c) 
1144 {
1145   //-----------------------------------------------------------------
1146   // Initial approximation of the tangent of the track dip angle
1147   //-----------------------------------------------------------------
1148
1149   //  Double_t angle1;
1150   
1151   //angle1    =  (z1-z2)*c/(TMath::ASin(c*x1-ni)-TMath::ASin(c*x2-ni));
1152   //
1153   Double_t d  =  TMath::Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
1154   if (TMath::Abs(d*c*0.5)>1) return 0;
1155   //  Double_t   angle2    =  TMath::ASin(d*c*0.5);
1156   //  Double_t   angle2    =  TPCFastMath::FastAsin(d*c*0.5);
1157   Double_t   angle2    = (d*c*0.5>0.1)? TMath::ASin(d*c*0.5): TPCFastMath::FastAsin(d*c*0.5);
1158
1159   angle2  = (z1-z2)*c/(angle2*2.);
1160   return angle2;
1161 }
1162
1163 Bool_t   AliTPCtrackerMI::GetProlongation(Double_t x1, Double_t x2, Double_t x[5], Double_t &y, Double_t &z)
1164 {//-----------------------------------------------------------------
1165   // This function find proloncation of a track to a reference plane x=x2.
1166   //-----------------------------------------------------------------
1167   
1168   Double_t dx=x2-x1;
1169
1170   if (TMath::Abs(x[4]*x1 - x[2]) >= 0.999) {   
1171     return kFALSE;
1172   }
1173
1174   Double_t c1=x[4]*x1 - x[2], r1=sqrt(1.- c1*c1);
1175   Double_t c2=x[4]*x2 - x[2], r2=sqrt(1.- c2*c2);  
1176   y = x[0];
1177   z = x[1];
1178   
1179   Double_t dy = dx*(c1+c2)/(r1+r2);
1180   Double_t dz = 0;
1181   //
1182   Double_t delta = x[4]*dx*(c1+c2)/(c1*r2 + c2*r1);
1183   
1184   if (TMath::Abs(delta)>0.01){
1185     dz = x[3]*TMath::ASin(delta)/x[4];
1186   }else{
1187     dz = x[3]*TPCFastMath::FastAsin(delta)/x[4];
1188   }
1189   
1190   //dz = x[3]*TPCFastMath::FastAsin(delta)/x[4];
1191
1192   y+=dy;
1193   z+=dz;
1194   
1195   return kTRUE;  
1196 }
1197
1198
1199
1200 Int_t  AliTPCtrackerMI::LoadClusters()
1201 {
1202   //
1203   // load clusters to the memory
1204   AliTPCClustersRow *clrow= new AliTPCClustersRow;
1205   clrow->SetClass("AliTPCclusterMI");
1206   clrow->SetArray(0);
1207   clrow->GetArray()->ExpandCreateFast(10000);
1208   //
1209   //  TTree * tree = fClustersArray.GetTree();
1210
1211   TTree * tree = fInput;
1212   TBranch * br = tree->GetBranch("Segment");
1213   br->SetAddress(&clrow);
1214   //
1215   Int_t j=Int_t(tree->GetEntries());
1216   for (Int_t i=0; i<j; i++) {
1217     br->GetEntry(i);
1218     //  
1219     Int_t sec,row;
1220     fParam->AdjustSectorRow(clrow->GetID(),sec,row);
1221     //
1222     AliTPCRow * tpcrow=0;
1223     Int_t left=0;
1224     if (sec<fkNIS*2){
1225       tpcrow = &(fInnerSec[sec%fkNIS][row]);    
1226       left = sec/fkNIS;
1227     }
1228     else{
1229       tpcrow = &(fOuterSec[(sec-fkNIS*2)%fkNOS][row]);
1230       left = (sec-fkNIS*2)/fkNOS;
1231     }
1232     if (left ==0){
1233       tpcrow->fN1 = clrow->GetArray()->GetEntriesFast();
1234       tpcrow->fClusters1 = new AliTPCclusterMI[tpcrow->fN1];
1235       for (Int_t i=0;i<tpcrow->fN1;i++) 
1236         tpcrow->fClusters1[i] = *(AliTPCclusterMI*)(clrow->GetArray()->At(i));
1237     }
1238     if (left ==1){
1239       tpcrow->fN2 = clrow->GetArray()->GetEntriesFast();
1240       tpcrow->fClusters2 = new AliTPCclusterMI[tpcrow->fN2];
1241       for (Int_t i=0;i<tpcrow->fN2;i++) 
1242         tpcrow->fClusters2[i] = *(AliTPCclusterMI*)(clrow->GetArray()->At(i));
1243     }
1244   }
1245   //
1246   delete clrow;
1247   LoadOuterSectors();
1248   LoadInnerSectors();
1249   return 0;
1250 }
1251
1252
1253 void AliTPCtrackerMI::UnloadClusters()
1254 {
1255   //
1256   // unload clusters from the memory
1257   //
1258   Int_t nrows = fOuterSec->GetNRows();
1259   for (Int_t sec = 0;sec<fkNOS;sec++)
1260     for (Int_t row = 0;row<nrows;row++){
1261       AliTPCRow*  tpcrow = &(fOuterSec[sec%fkNOS][row]);
1262       if (tpcrow){
1263         if (tpcrow->fClusters1) delete []tpcrow->fClusters1; 
1264         if (tpcrow->fClusters2) delete []tpcrow->fClusters2; 
1265       }
1266     }
1267   //
1268   nrows = fInnerSec->GetNRows();
1269   for (Int_t sec = 0;sec<fkNIS;sec++)
1270     for (Int_t row = 0;row<nrows;row++){
1271       AliTPCRow*  tpcrow = &(fInnerSec[sec%fkNIS][row]);
1272       if (tpcrow){
1273         if (tpcrow->fClusters1) delete []tpcrow->fClusters1; 
1274         if (tpcrow->fClusters2) delete []tpcrow->fClusters2; 
1275       }
1276     }
1277
1278   return ;
1279 }
1280
1281
1282 //_____________________________________________________________________________
1283 Int_t AliTPCtrackerMI::LoadOuterSectors() {
1284   //-----------------------------------------------------------------
1285   // This function fills outer TPC sectors with clusters.
1286   //-----------------------------------------------------------------
1287   Int_t nrows = fOuterSec->GetNRows();
1288   UInt_t index=0;
1289   for (Int_t sec = 0;sec<fkNOS;sec++)
1290     for (Int_t row = 0;row<nrows;row++){
1291       AliTPCRow*  tpcrow = &(fOuterSec[sec%fkNOS][row]);  
1292       Int_t sec2 = sec+2*fkNIS;
1293       //left
1294       Int_t ncl = tpcrow->fN1;
1295       while (ncl--) {
1296         AliTPCclusterMI *c= &(tpcrow->fClusters1[ncl]);
1297         index=(((sec2<<8)+row)<<16)+ncl;
1298         tpcrow->InsertCluster(c,index);
1299       }
1300       //right
1301       ncl = tpcrow->fN2;
1302       while (ncl--) {
1303         AliTPCclusterMI *c= &(tpcrow->fClusters2[ncl]);
1304         index=((((sec2+fkNOS)<<8)+row)<<16)+ncl;
1305         tpcrow->InsertCluster(c,index);
1306       }
1307       //
1308       // write indexes for fast acces
1309       //
1310       for (Int_t i=0;i<510;i++)
1311         tpcrow->fFastCluster[i]=-1;
1312       for (Int_t i=0;i<tpcrow->GetN();i++){
1313         Int_t zi = Int_t((*tpcrow)[i]->GetZ()+255.);
1314         tpcrow->fFastCluster[zi]=i;  // write index
1315       }
1316       Int_t last = 0;
1317       for (Int_t i=0;i<510;i++){
1318         if (tpcrow->fFastCluster[i]<0)
1319           tpcrow->fFastCluster[i] = last;
1320         else
1321           last = tpcrow->fFastCluster[i];
1322       }
1323     }  
1324   fN=fkNOS;
1325   fSectors=fOuterSec;
1326   return 0;
1327 }
1328
1329
1330 //_____________________________________________________________________________
1331 Int_t  AliTPCtrackerMI::LoadInnerSectors() {
1332   //-----------------------------------------------------------------
1333   // This function fills inner TPC sectors with clusters.
1334   //-----------------------------------------------------------------
1335   Int_t nrows = fInnerSec->GetNRows();
1336   UInt_t index=0;
1337   for (Int_t sec = 0;sec<fkNIS;sec++)
1338     for (Int_t row = 0;row<nrows;row++){
1339       AliTPCRow*  tpcrow = &(fInnerSec[sec%fkNIS][row]);
1340       //
1341       //left
1342       Int_t ncl = tpcrow->fN1;
1343       while (ncl--) {
1344         AliTPCclusterMI *c= &(tpcrow->fClusters1[ncl]);
1345         index=(((sec<<8)+row)<<16)+ncl;
1346         tpcrow->InsertCluster(c,index);
1347       }
1348       //right
1349       ncl = tpcrow->fN2;
1350       while (ncl--) {
1351         AliTPCclusterMI *c= &(tpcrow->fClusters2[ncl]);
1352         index=((((sec+fkNIS)<<8)+row)<<16)+ncl;
1353         tpcrow->InsertCluster(c,index);
1354       }
1355       //
1356       // write indexes for fast acces
1357       //
1358       for (Int_t i=0;i<510;i++)
1359         tpcrow->fFastCluster[i]=-1;
1360       for (Int_t i=0;i<tpcrow->GetN();i++){
1361         Int_t zi = Int_t((*tpcrow)[i]->GetZ()+255.);
1362         tpcrow->fFastCluster[zi]=i;  // write index
1363       }
1364       Int_t last = 0;
1365       for (Int_t i=0;i<510;i++){
1366         if (tpcrow->fFastCluster[i]<0)
1367           tpcrow->fFastCluster[i] = last;
1368         else
1369           last = tpcrow->fFastCluster[i];
1370       }
1371
1372     }  
1373    
1374   fN=fkNIS;
1375   fSectors=fInnerSec;
1376   return 0;
1377 }
1378
1379
1380
1381 //_________________________________________________________________________
1382 AliTPCclusterMI *AliTPCtrackerMI::GetClusterMI(Int_t index) const {
1383   //--------------------------------------------------------------------
1384   //       Return pointer to a given cluster
1385   //--------------------------------------------------------------------
1386   Int_t sec=(index&0xff000000)>>24; 
1387   Int_t row=(index&0x00ff0000)>>16; 
1388   Int_t ncl=(index&0x0000ffff)>>00;
1389
1390   const AliTPCRow * tpcrow=0;
1391   AliTPCclusterMI * clrow =0;
1392   if (sec<fkNIS*2){
1393     tpcrow = &(fInnerSec[sec%fkNIS][row]);
1394     if (sec<fkNIS) 
1395       clrow = tpcrow->fClusters1;
1396     else
1397       clrow = tpcrow->fClusters2;
1398   }
1399   else{
1400     tpcrow = &(fOuterSec[(sec-fkNIS*2)%fkNOS][row]);
1401     if (sec-2*fkNIS<fkNOS)
1402       clrow = tpcrow->fClusters1;
1403     else
1404       clrow = tpcrow->fClusters2;
1405   }
1406   if (tpcrow==0) return 0;
1407   if (tpcrow->GetN()<=ncl) return 0;
1408   //  return (AliTPCclusterMI*)(*tpcrow)[ncl];      
1409   return &(clrow[ncl]);      
1410   
1411 }
1412
1413
1414
1415 Int_t AliTPCtrackerMI::FollowToNext(AliTPCseed& t, Int_t nr) {
1416   //-----------------------------------------------------------------
1417   // This function tries to find a track prolongation to next pad row
1418   //-----------------------------------------------------------------
1419   //
1420   Double_t  x= GetXrow(nr), ymax=GetMaxY(nr);
1421
1422   //  if (t.GetRadius()>x+10 ) return 0;
1423   //  t.PropagateTo(x+0.02);
1424   //t.PropagateTo(x+0.01);
1425   if (!t.PropagateTo(x)) {
1426     t.fRemoval = 10;
1427     return 0;
1428   }
1429   //
1430   Double_t  y=t.GetY(), z=t.GetZ();
1431   if (TMath::Abs(y)>ymax){
1432     if (y > ymax) {
1433       t.fRelativeSector= (t.fRelativeSector+1) % fN;
1434       if (!t.Rotate(fSectors->GetAlpha())) 
1435         return 0;
1436     } else if (y <-ymax) {
1437       t.fRelativeSector= (t.fRelativeSector-1+fN) % fN;
1438       if (!t.Rotate(-fSectors->GetAlpha())) 
1439         return 0;
1440     }
1441     if (!t.PropagateTo(x)) {
1442       return 0;
1443     } 
1444     y=t.GetY();
1445   }
1446   //
1447   // update current shape info every 3 pad-row
1448   if ( (nr%5==0) || t.GetNumberOfClusters()<2 || (t.fCurrentSigmaY2<0.0001) ){
1449     //t.fCurrentSigmaY = GetSigmaY(&t);
1450     //t.fCurrentSigmaZ = GetSigmaZ(&t);
1451     GetShape(&t,nr);    
1452   }
1453   //  
1454   AliTPCclusterMI *cl=0;
1455   UInt_t index=0;
1456   
1457   
1458   //Int_t nr2 = nr;
1459   if (t.GetClusterIndex2(nr)>0){ 
1460     //
1461     //cl = GetClusterMI(t.GetClusterIndex2(nr));
1462     index = t.GetClusterIndex2(nr);    
1463     cl = t.fClusterPointer[nr];
1464     if ( (cl==0) && (index>0)) cl = GetClusterMI(index);
1465     t.fCurrentClusterIndex1 = index; 
1466   }
1467   
1468   const AliTPCRow &krow=GetRow(t.fRelativeSector,nr);
1469   if ( (t.GetSigmaY2()<0) || t.GetSigmaZ2()<0) return 0;
1470   Double_t  roady  =1.;
1471   Double_t  roadz = 1.;
1472   //
1473   if (TMath::Abs(TMath::Abs(y)-ymax)<krow.fDeadZone){
1474     t.fInDead = kTRUE;
1475     t.SetClusterIndex2(nr,-1); 
1476     return 0;
1477   } 
1478   else
1479     {
1480       if (TMath::Abs(z)<(1.05*x+10)) t.fNFoundable++;
1481       else
1482         return 0;
1483     }   
1484   //calculate 
1485   if (cl){
1486     t.fCurrentCluster = cl; 
1487     t.fRow = nr;
1488     Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
1489     if (accept<3) { 
1490       //if founded cluster is acceptible
1491       UpdateTrack(&t,accept);
1492       return 1;
1493     }    
1494   }
1495
1496   if (krow) {
1497     //    cl = krow.FindNearest2(y+10.,z,roady,roadz,index);    
1498     cl = krow.FindNearest2(y,z,roady,roadz,index);    
1499     if (cl) t.fCurrentClusterIndex1 = krow.GetIndex(index);       
1500   }  
1501   //  t.fNoCluster++;
1502
1503   if (cl) {
1504     t.fCurrentCluster = cl; 
1505     t.fRow = nr;
1506     Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
1507     
1508     if (t.fCurrentCluster->IsUsed(10)){
1509       //
1510       //     
1511
1512       t.fNShared++;
1513       if (t.fNShared>0.7*t.GetNumberOfClusters()) {
1514         t.fRemoval =10;
1515         return 0;
1516       }
1517     }
1518     
1519     if (accept<3) UpdateTrack(&t,accept);
1520
1521   } else {  
1522     if (t.fNFoundable*0.5 > t.GetNumberOfClusters()) t.fRemoval=10;
1523     
1524   }
1525   return 1;
1526 }
1527
1528 Int_t AliTPCtrackerMI::FollowToNextFast(AliTPCseed& t, Int_t nr) {
1529   //-----------------------------------------------------------------
1530   // This function tries to find a track prolongation to next pad row
1531   //-----------------------------------------------------------------
1532   //
1533   Double_t  x= GetXrow(nr), ymax=GetMaxY(nr);
1534   Double_t y,z; 
1535   if (!t.GetProlongation(x,y,z)) {
1536     t.fRemoval = 10;
1537     return 0;
1538   }
1539   //
1540   //
1541   if (TMath::Abs(y)>ymax){
1542     return 0;
1543     
1544     if (y > ymax) {
1545       t.fRelativeSector= (t.fRelativeSector+1) % fN;
1546       if (!t.Rotate(fSectors->GetAlpha())) 
1547         return 0;
1548     } else if (y <-ymax) {
1549       t.fRelativeSector= (t.fRelativeSector-1+fN) % fN;
1550       if (!t.Rotate(-fSectors->GetAlpha())) 
1551         return 0;
1552     }
1553     if (!t.PropagateTo(x)) {
1554       return 0;
1555     } 
1556     t.GetProlongation(x,y,z);
1557   }
1558   //
1559   // update current shape info every 3 pad-row
1560   if ( (nr%6==0) || t.GetNumberOfClusters()<2 || (t.fCurrentSigmaY2<0.0001) ){
1561     //    t.fCurrentSigmaY = GetSigmaY(&t);
1562     //t.fCurrentSigmaZ = GetSigmaZ(&t);
1563     GetShape(&t,nr);
1564   }
1565   //  
1566   AliTPCclusterMI *cl=0;
1567   UInt_t index=0;
1568   
1569   
1570   //Int_t nr2 = nr;
1571   const AliTPCRow &krow=GetRow(t.fRelativeSector,nr);
1572   if ( (t.GetSigmaY2()<0) || t.GetSigmaZ2()<0) return 0;
1573   Double_t  roady  =1.;
1574   Double_t  roadz = 1.;
1575   //
1576   Int_t row = nr;
1577   if (TMath::Abs(TMath::Abs(y)-ymax)<krow.fDeadZone){
1578     t.fInDead = kTRUE;
1579     t.SetClusterIndex2(row,-1); 
1580     return 0;
1581   } 
1582   else
1583     {
1584       if (TMath::Abs(z)>(1.05*x+10)) t.SetClusterIndex2(row,-1);
1585     }   
1586   //calculate 
1587   
1588   if ((cl==0)&&(krow)) {
1589     //    cl = krow.FindNearest2(y+10,z,roady,roadz,index);    
1590     cl = krow.FindNearest2(y,z,roady,roadz,index);    
1591
1592     if (cl) t.fCurrentClusterIndex1 = krow.GetIndex(index);       
1593   }  
1594
1595   if (cl) {
1596     t.fCurrentCluster = cl; 
1597     //    Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);        
1598     //if (accept<3){
1599       t.SetClusterIndex2(row,index);
1600       t.fClusterPointer[row] = cl;
1601       //}
1602   }
1603   return 1;
1604 }
1605
1606
1607
1608 Int_t AliTPCtrackerMI::UpdateClusters(AliTPCseed& t,  Int_t nr) {
1609   //-----------------------------------------------------------------
1610   // This function tries to find a track prolongation to next pad row
1611   //-----------------------------------------------------------------
1612   t.fCurrentCluster  = 0;
1613   t.fCurrentClusterIndex1 = 0;   
1614    
1615   Double_t xt=t.GetX();
1616   Int_t     row = GetRowNumber(xt)-1; 
1617   Double_t  ymax= GetMaxY(nr);
1618
1619   if (row < nr) return 1; // don't prolongate if not information until now -
1620   if (TMath::Abs(t.GetSnp())>0.9 && t.GetNumberOfClusters()>40. && fIteration!=2) {
1621     t.fRemoval =10;
1622     return 0;  // not prolongate strongly inclined tracks
1623   } 
1624   if (TMath::Abs(t.GetSnp())>0.95) {
1625     t.fRemoval =10;
1626     return 0;  // not prolongate strongly inclined tracks
1627   }
1628
1629   Double_t x= GetXrow(nr);
1630   Double_t y,z;
1631   //t.PropagateTo(x+0.02);
1632   //t.PropagateTo(x+0.01);
1633   if (!t.PropagateTo(x)){
1634     return 0;
1635   }
1636   //
1637   y=t.GetY();
1638   z=t.GetZ();
1639
1640   if (TMath::Abs(y)>ymax){
1641     if (y > ymax) {
1642       t.fRelativeSector= (t.fRelativeSector+1) % fN;
1643       if (!t.Rotate(fSectors->GetAlpha())) 
1644         return 0;
1645     } else if (y <-ymax) {
1646       t.fRelativeSector= (t.fRelativeSector-1+fN) % fN;
1647       if (!t.Rotate(-fSectors->GetAlpha())) 
1648         return 0;
1649     }
1650     if (!t.PropagateTo(x)){
1651       return 0;
1652     }
1653     y = t.GetY();    
1654   }
1655   //
1656
1657   AliTPCRow &krow=GetRow(t.fRelativeSector,nr);
1658
1659   if (TMath::Abs(TMath::Abs(y)-ymax)<krow.fDeadZone){
1660     t.fInDead = kTRUE;
1661     t.SetClusterIndex2(nr,-1); 
1662     return 0;
1663   } 
1664   else
1665     {
1666       if (TMath::Abs(t.GetZ())<(1.05*t.GetX()+10)) t.fNFoundable++;
1667       else
1668         return 0;      
1669     }
1670
1671   // update current
1672   if ( (nr%6==0) || t.GetNumberOfClusters()<2){
1673     //    t.fCurrentSigmaY = GetSigmaY(&t);
1674     //t.fCurrentSigmaZ = GetSigmaZ(&t);
1675     GetShape(&t,nr);
1676   }
1677     
1678   AliTPCclusterMI *cl=0;
1679   UInt_t index=0;
1680   //
1681   Double_t roady = 1.;
1682   Double_t roadz = 1.;
1683   //
1684   if (krow) {    
1685     //cl = krow.FindNearest2(y+10,z,roady,roadz,index);      
1686     cl = krow.FindNearest2(y,z,roady,roadz,index);      
1687   }
1688   t.fCurrentCluster  = cl;
1689   if (cl) t.fCurrentClusterIndex1 = krow.GetIndex(index);   
1690   return 1;
1691 }
1692
1693
1694 Int_t AliTPCtrackerMI::FollowToNextCluster(AliTPCseed & t, Int_t nr) {
1695   //-----------------------------------------------------------------
1696   // This function tries to find a track prolongation to next pad row
1697   //-----------------------------------------------------------------
1698
1699   //update error according neighborhoud
1700
1701   if (t.fCurrentCluster) {
1702     t.fRow = nr; 
1703     Bool_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
1704     
1705     if (t.fCurrentCluster->IsUsed(10)){
1706       //
1707       //
1708       //  t.fErrorZ2*=2;
1709       //  t.fErrorY2*=2;
1710       t.fNShared++;
1711       if (t.fNShared>0.7*t.GetNumberOfClusters()) {
1712         t.fRemoval =10;
1713         return 0;
1714       }
1715     }
1716     
1717     /*if (accept<3) */ UpdateTrack(&t,accept);
1718    
1719   } else {
1720     if (fIteration==0){
1721       if ( ( (t.GetSigmaY2()+t.GetSigmaZ2())>0.16)&& t.GetNumberOfClusters()>18) t.fRemoval=10;      
1722       if (  t.GetChi2()/t.GetNumberOfClusters()>6 &&t.GetNumberOfClusters()>18) t.fRemoval=10;      
1723
1724       if (( (t.fNFoundable*0.5 > t.GetNumberOfClusters()) || t.fNoCluster>15)) t.fRemoval=10;
1725     }
1726   }
1727   return 1;
1728 }
1729
1730
1731
1732 //_____________________________________________________________________________
1733 Int_t AliTPCtrackerMI::FollowProlongation(AliTPCseed& t, Int_t rf, Int_t step) {
1734   //-----------------------------------------------------------------
1735   // This function tries to find a track prolongation.
1736   //-----------------------------------------------------------------
1737   Double_t xt=t.GetX();
1738   //
1739   Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
1740   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
1741   if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
1742   //
1743   t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
1744     
1745   Int_t first = GetRowNumber(xt)-1;
1746   for (Int_t nr= first; nr>=rf; nr-=step) {    
1747     if (FollowToNext(t,nr)==0) 
1748       if (!t.IsActive()) return 0;
1749     
1750   }   
1751   return 1;
1752 }
1753
1754
1755 //_____________________________________________________________________________
1756 Int_t AliTPCtrackerMI::FollowProlongationFast(AliTPCseed& t, Int_t rf, Int_t step) {
1757   //-----------------------------------------------------------------
1758   // This function tries to find a track prolongation.
1759   //-----------------------------------------------------------------
1760   Double_t xt=t.GetX();
1761   //
1762   Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
1763   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
1764   if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
1765   t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
1766     
1767   for (Int_t nr=GetRowNumber(xt)-1; nr>=rf; nr-=step) {
1768     
1769     if (FollowToNextFast(t,nr)==0) 
1770       if (!t.IsActive()) return 0;
1771     
1772   }   
1773   return 1;
1774 }
1775
1776
1777
1778
1779
1780 Int_t AliTPCtrackerMI::FollowBackProlongation(AliTPCseed& t, Int_t rf) {
1781   //-----------------------------------------------------------------
1782   // This function tries to find a track prolongation.
1783   //-----------------------------------------------------------------
1784   //  Double_t xt=t.GetX();  
1785   //
1786   Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
1787   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
1788   if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
1789   t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
1790     
1791   Int_t first = 0;
1792   first = t.fFirstPoint+3;
1793   //
1794   if (first<0) first=0;
1795   for (Int_t nr=first+1; nr<=rf; nr++) {
1796     //if ( (t.GetSnp()<0.9))
1797       FollowToNext(t,nr);                                                             
1798   }   
1799   return 1;
1800 }
1801
1802
1803
1804
1805    
1806 Float_t AliTPCtrackerMI::OverlapFactor(AliTPCseed * s1, AliTPCseed * s2, Int_t &sum1, Int_t & sum2)
1807 {
1808   //
1809   //
1810   sum1=0;
1811   sum2=0;
1812   Int_t sum=0;
1813   //
1814   Float_t dz2 =(s1->GetZ() - s2->GetZ());
1815   dz2*=dz2;  
1816
1817   Float_t dy2 =TMath::Abs((s1->GetY() - s2->GetY()));
1818   dy2*=dy2;
1819   Float_t distance = TMath::Sqrt(dz2+dy2);
1820   if (distance>4.) return 0; // if there are far away  - not overlap - to reduce combinatorics
1821  
1822   //  Int_t offset =0;
1823   Int_t firstpoint = TMath::Min(s1->fFirstPoint,s2->fFirstPoint);
1824   Int_t lastpoint = TMath::Max(s1->fLastPoint,s2->fLastPoint);
1825   if (lastpoint>160) 
1826     lastpoint =160;
1827   if (firstpoint<0) 
1828     firstpoint = 0;
1829   if (firstpoint>lastpoint) {
1830     firstpoint =lastpoint;
1831     //    lastpoint  =160;
1832   }
1833     
1834   
1835   for (Int_t i=firstpoint-1;i<lastpoint+1;i++){
1836     if (s1->GetClusterIndex2(i)>0) sum1++;
1837     if (s2->GetClusterIndex2(i)>0) sum2++;
1838     if (s1->GetClusterIndex2(i)==s2->GetClusterIndex2(i) && s1->GetClusterIndex2(i)>0) {
1839       sum++;
1840     }
1841   }
1842   if (sum<5) return 0;
1843
1844   Float_t summin = TMath::Min(sum1+1,sum2+1);
1845   Float_t ratio = (sum+1)/Float_t(summin);
1846   return ratio;
1847 }
1848
1849 void  AliTPCtrackerMI::SignShared(AliTPCseed * s1, AliTPCseed * s2)
1850 {
1851   //
1852   //
1853   if (TMath::Abs(s1->GetC()-s2->GetC())>0.004) return;
1854   if (TMath::Abs(s1->GetTgl()-s2->GetTgl())>0.6) return;
1855
1856   Float_t dz2 =(s1->GetZ() - s2->GetZ());
1857   dz2*=dz2;
1858   Float_t dy2 =(s1->GetY() - s2->GetY());
1859   dy2*=dy2;
1860   Float_t distance = dz2+dy2;
1861   if (distance>325.) return ; // if there are far away  - not overlap - to reduce combinatorics
1862   
1863   //
1864   Int_t sumshared=0;
1865   //
1866   Int_t firstpoint = TMath::Max(s1->fFirstPoint,s2->fFirstPoint);
1867   Int_t lastpoint = TMath::Min(s1->fLastPoint,s2->fLastPoint);
1868   //
1869   if (firstpoint>=lastpoint-5) return;;
1870
1871   for (Int_t i=firstpoint;i<lastpoint;i++){
1872     //    if ( (s1->GetClusterIndex2(i)&0xFFFF8FFF)==(s2->GetClusterIndex2(i)&0xFFFF8FFF) && s1->GetClusterIndex2(i)>0) {
1873     if ( (s1->GetClusterIndex2(i))==(s2->GetClusterIndex2(i)) && s1->GetClusterIndex2(i)>0) {
1874       sumshared++;
1875     }
1876   }
1877   if (sumshared>4){
1878     // sign clusters
1879     //
1880     for (Int_t i=firstpoint;i<lastpoint;i++){
1881       //      if ( (s1->GetClusterIndex2(i)&0xFFFF8FFF)==(s2->GetClusterIndex2(i)&0xFFFF8FFF) && s1->GetClusterIndex2(i)>0) {
1882       if ( (s1->GetClusterIndex2(i))==(s2->GetClusterIndex2(i)) && s1->GetClusterIndex2(i)>0) {
1883         AliTPCTrackerPoint *p1  = s1->GetTrackPoint(i);
1884         AliTPCTrackerPoint *p2  = s2->GetTrackPoint(i);; 
1885         if (s1->IsActive()&&s2->IsActive()){
1886           p1->fIsShared = kTRUE;
1887           p2->fIsShared = kTRUE;
1888         }       
1889       }
1890     }
1891   }
1892   //  
1893   if (sumshared>10){
1894     for (Int_t i=0;i<4;i++){
1895       if (s1->fOverlapLabels[3*i]==0){
1896         s1->fOverlapLabels[3*i] = s2->GetLabel();
1897         s1->fOverlapLabels[3*i+1] = sumshared;
1898         s1->fOverlapLabels[3*i+2] = s2->GetUniqueID();
1899         break;
1900       } 
1901     }
1902     for (Int_t i=0;i<4;i++){
1903       if (s2->fOverlapLabels[3*i]==0){
1904         s2->fOverlapLabels[3*i] = s1->GetLabel();
1905         s2->fOverlapLabels[3*i+1] = sumshared;
1906         s2->fOverlapLabels[3*i+2] = s1->GetUniqueID();
1907         break;
1908       } 
1909     }    
1910   }
1911   
1912 }
1913
1914 void  AliTPCtrackerMI::SignShared(TObjArray * arr)
1915 {
1916   //
1917   //sort trackss according sectors
1918   //  
1919   for (Int_t i=0; i<arr->GetEntriesFast(); i++) {
1920     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1921     if (!pt) continue;
1922     //if (pt) RotateToLocal(pt);
1923     pt->fSort = 0;
1924   }
1925   arr->UnSort();
1926   arr->Sort();  // sorting according z
1927   arr->Expand(arr->GetEntries());
1928   //
1929   //
1930   Int_t nseed=arr->GetEntriesFast();
1931   for (Int_t i=0; i<nseed; i++) {
1932     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1933     if (!pt) continue;
1934     for (Int_t j=0;j<=12;j++){
1935       pt->fOverlapLabels[j] =0;
1936     }
1937   }
1938   for (Int_t i=0; i<nseed; i++) {
1939     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1940     if (!pt) continue;
1941     if (pt->fRemoval>10) continue;
1942     for (Int_t j=i+1; j<nseed; j++){
1943       AliTPCseed *pt2=(AliTPCseed*)arr->UncheckedAt(j);
1944       //      if (pt2){
1945       if (pt2->fRemoval<=10) {
1946         if ( TMath::Abs(pt->fRelativeSector-pt2->fRelativeSector)>0) break;
1947         SignShared(pt,pt2);
1948       }
1949     }  
1950   }
1951 }
1952
1953 void  AliTPCtrackerMI::RemoveDouble(TObjArray * arr, Float_t factor1, Float_t factor2,  Int_t removalindex)
1954 {
1955   //
1956   //sort trackss according sectors
1957   //
1958   if (fDebug&1) {
1959     printf("Number of tracks before double removal- %d\n",arr->GetEntries());
1960   }
1961   //
1962   for (Int_t i=0; i<arr->GetEntriesFast(); i++) {
1963     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1964     if (!pt) continue;
1965     pt->fSort = 0;
1966   }
1967   arr->UnSort();
1968   arr->Sort();  // sorting according z
1969   arr->Expand(arr->GetEntries());
1970   //
1971   //reset overlap labels
1972   //
1973   Int_t nseed=arr->GetEntriesFast();
1974   for (Int_t i=0; i<nseed; i++) {
1975     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1976     if (!pt) continue;
1977     pt->SetUniqueID(i);
1978     for (Int_t j=0;j<=12;j++){
1979       pt->fOverlapLabels[j] =0;
1980     }
1981   }
1982   //
1983   //sign shared tracks
1984   for (Int_t i=0; i<nseed; i++) {
1985     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1986     if (!pt) continue;
1987     if (pt->fRemoval>10) continue;
1988     Float_t deltac = pt->GetC()*0.1;
1989     for (Int_t j=i+1; j<nseed; j++){
1990       AliTPCseed *pt2=(AliTPCseed*)arr->UncheckedAt(j);
1991       //      if (pt2){
1992       if (pt2->fRemoval<=10) {
1993         if ( TMath::Abs(pt->fRelativeSector-pt2->fRelativeSector)>0) break;
1994         if (TMath::Abs(pt->GetC()  -pt2->GetC())>deltac) continue;
1995         if (TMath::Abs(pt->GetTgl()-pt2->GetTgl())>0.05) continue;
1996         //
1997         SignShared(pt,pt2);
1998       }
1999     }
2000   }
2001   //
2002   // remove highly shared tracks
2003   for (Int_t i=0; i<nseed; i++) {
2004     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2005     if (!pt) continue;
2006     if (pt->fRemoval>10) continue;
2007     //
2008     Int_t sumshared =0;
2009     for (Int_t j=0;j<4;j++){
2010       sumshared = pt->fOverlapLabels[j*3+1];      
2011     }
2012     Float_t factor = factor1;
2013     if (pt->fRemoval>0) factor = factor2;
2014     if (sumshared/pt->GetNumberOfClusters()>factor){
2015       for (Int_t j=0;j<4;j++){
2016         if (pt->fOverlapLabels[3*j]==0) continue;
2017         if (pt->fOverlapLabels[3*j+1]<5) continue; 
2018         if (pt->fRemoval==removalindex) continue;      
2019         AliTPCseed * pt2 = (AliTPCseed*)arr->UncheckedAt(pt->fOverlapLabels[3*j+2]);
2020         if (!pt2) continue;
2021         if (pt2->GetSigma2C()<pt->GetSigma2C()){
2022           //      pt->fRemoval = removalindex;
2023           delete arr->RemoveAt(i);        
2024           break;
2025         }
2026       }      
2027     }
2028   }
2029   arr->Compress();
2030   if (fDebug&1) {
2031     printf("Number of tracks after double removal- %d\n",arr->GetEntries());
2032   }
2033 }
2034
2035
2036
2037
2038
2039
2040 void AliTPCtrackerMI::SortTracks(TObjArray * arr, Int_t mode)
2041 {
2042   //
2043   //sort tracks in array according mode criteria
2044   Int_t nseed = arr->GetEntriesFast();    
2045   for (Int_t i=0; i<nseed; i++) {
2046     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2047     if (!pt) {
2048       continue;
2049     }
2050     pt->fSort = mode;
2051   }
2052   arr->UnSort();
2053   arr->Sort();
2054 }
2055
2056 void AliTPCtrackerMI::RemoveUsed(TObjArray * arr, Float_t factor1,  Float_t factor2, Int_t removalindex)
2057 {
2058
2059   //Loop over all tracks and remove "overlaps"
2060   //
2061   //
2062   Int_t nseed = arr->GetEntriesFast();  
2063   Int_t good =0;
2064
2065   for (Int_t i=0; i<nseed; i++) {
2066     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2067     if (!pt) {
2068       delete arr->RemoveAt(i);
2069     }
2070     else{
2071       pt->fSort =1;
2072       pt->fBSigned = kFALSE;
2073     }
2074   }
2075   arr->Compress();
2076   nseed = arr->GetEntriesFast();
2077   arr->UnSort();
2078   arr->Sort();
2079   //
2080   //unsign used
2081   UnsignClusters();
2082   //
2083   for (Int_t i=0; i<nseed; i++) {
2084     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2085     if (!pt) {
2086       continue;
2087     }    
2088     Int_t found,foundable,shared;
2089     if (pt->IsActive()) 
2090       pt->GetClusterStatistic(0,160,found, foundable,shared,kFALSE);
2091     else
2092       pt->GetClusterStatistic(0,160,found, foundable,shared,kTRUE); 
2093     //
2094     Double_t factor = factor2;
2095     if (pt->fBConstrain) factor = factor1;
2096
2097     if ((Float_t(shared)/Float_t(found))>factor){
2098       pt->Desactivate(removalindex);
2099       continue;
2100     }
2101
2102     good++;
2103     for (Int_t i=0; i<160; i++) {
2104       Int_t index=pt->GetClusterIndex2(i);
2105       if (index<0 || index&0x8000 ) continue;
2106       AliTPCclusterMI *c= pt->fClusterPointer[i];        
2107       if (!c) continue;
2108       //      if (!c->IsUsed(10)) c->Use(10);
2109       //if (pt->IsActive()) 
2110       c->Use(10);  
2111       //else
2112       //        c->Use(5);
2113     }
2114     
2115   }
2116   fNtracks = good;
2117
2118   printf("\n*****\nNumber of good tracks after shared removal\t%d\n",fNtracks);
2119 }
2120
2121 void AliTPCtrackerMI::UnsignClusters() 
2122 {
2123   //
2124   // loop over all clusters and unsign them
2125   //
2126   
2127   for (Int_t sec=0;sec<fkNIS;sec++){
2128     for (Int_t row=0;row<fInnerSec->GetNRows();row++){
2129       AliTPCclusterMI *cl = fInnerSec[sec][row].fClusters1;
2130       for (Int_t icl =0;icl< fInnerSec[sec][row].fN1;icl++)
2131         //      if (cl[icl].IsUsed(10))         
2132         cl[icl].Use(-1);
2133       cl = fInnerSec[sec][row].fClusters2;
2134       for (Int_t icl =0;icl< fInnerSec[sec][row].fN2;icl++)
2135         //if (cl[icl].IsUsed(10))       
2136           cl[icl].Use(-1);      
2137     }
2138   }
2139   
2140   for (Int_t sec=0;sec<fkNOS;sec++){
2141     for (Int_t row=0;row<fOuterSec->GetNRows();row++){
2142       AliTPCclusterMI *cl = fOuterSec[sec][row].fClusters1;
2143       for (Int_t icl =0;icl< fOuterSec[sec][row].fN1;icl++)
2144         //if (cl[icl].IsUsed(10))       
2145           cl[icl].Use(-1);
2146       cl = fOuterSec[sec][row].fClusters2;
2147       for (Int_t icl =0;icl< fOuterSec[sec][row].fN2;icl++)
2148         //if (cl[icl].IsUsed(10))       
2149         cl[icl].Use(-1);      
2150     }
2151   }
2152   
2153 }
2154
2155
2156
2157 void AliTPCtrackerMI::SignClusters(TObjArray * arr, Float_t fnumber, Float_t fdensity)
2158 {
2159   //
2160   //sign clusters to be "used"
2161   //
2162   // snumber and sdensity sign number of sigmas - bellow mean value to be accepted
2163   // loop over "primaries"
2164   
2165   Float_t sumdens=0;
2166   Float_t sumdens2=0;
2167   Float_t sumn   =0;
2168   Float_t sumn2  =0;
2169   Float_t sumchi =0;
2170   Float_t sumchi2 =0;
2171
2172   Float_t sum    =0;
2173
2174   TStopwatch timer;
2175   timer.Start();
2176
2177   Int_t nseed = arr->GetEntriesFast();
2178   for (Int_t i=0; i<nseed; i++) {
2179     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2180     if (!pt) {
2181       continue;
2182     }    
2183     if (!(pt->IsActive())) continue;
2184     Float_t dens = pt->GetNumberOfClusters()/Float_t(pt->fNFoundable);
2185     if ( (dens>0.7) && (pt->GetNumberOfClusters()>70)){
2186       sumdens += dens;
2187       sumdens2+= dens*dens;
2188       sumn    += pt->GetNumberOfClusters();
2189       sumn2   += pt->GetNumberOfClusters()*pt->GetNumberOfClusters();
2190       Float_t chi2 = pt->GetChi2()/pt->GetNumberOfClusters();
2191       if (chi2>5) chi2=5;
2192       sumchi  +=chi2;
2193       sumchi2 +=chi2*chi2;
2194       sum++;
2195     }
2196   }
2197
2198   Float_t mdensity = 0.9;
2199   Float_t meann    = 130;
2200   Float_t meanchi  = 1;
2201   Float_t sdensity = 0.1;
2202   Float_t smeann    = 10;
2203   Float_t smeanchi  =0.4;
2204   
2205
2206   if (sum>20){
2207     mdensity = sumdens/sum;
2208     meann    = sumn/sum;
2209     meanchi  = sumchi/sum;
2210     //
2211     sdensity = sumdens2/sum-mdensity*mdensity;
2212     sdensity = TMath::Sqrt(sdensity);
2213     //
2214     smeann   = sumn2/sum-meann*meann;
2215     smeann   = TMath::Sqrt(smeann);
2216     //
2217     smeanchi = sumchi2/sum - meanchi*meanchi;
2218     smeanchi = TMath::Sqrt(smeanchi);
2219   }
2220
2221
2222   //REMOVE  SHORT DELTAS or tracks going out of sensitive volume of TPC
2223   //
2224   for (Int_t i=0; i<nseed; i++) {
2225     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2226     if (!pt) {
2227       continue;
2228     }
2229     if (pt->fBSigned) continue;
2230     if (pt->fBConstrain) continue;    
2231     //if (!(pt->IsActive())) continue;
2232     /*
2233     Int_t found,foundable,shared;    
2234     pt->GetClusterStatistic(0,160,found, foundable,shared);
2235     if (shared/float(found)>0.3) {
2236       if (shared/float(found)>0.9 ){
2237         //delete arr->RemoveAt(i);
2238       }
2239       continue;
2240     }
2241     */
2242     Bool_t isok =kFALSE;
2243     if ( (pt->fNShared/pt->GetNumberOfClusters()<0.5) &&pt->GetNumberOfClusters()>60)
2244       isok = kTRUE;
2245     if ((TMath::Abs(1/pt->GetC())<100.) && (pt->fNShared/pt->GetNumberOfClusters()<0.7))
2246       isok =kTRUE;
2247     if  (TMath::Abs(pt->GetZ()/pt->GetX())>1.1)
2248       isok =kTRUE;
2249     if ( (TMath::Abs(pt->GetSnp()>0.7) && pt->GetD(0,0)>60.))
2250       isok =kTRUE;
2251     
2252     if (isok)     
2253       for (Int_t i=0; i<160; i++) {     
2254         Int_t index=pt->GetClusterIndex2(i);
2255         if (index<0) continue;
2256         AliTPCclusterMI *c= pt->fClusterPointer[i];
2257         if (!c) continue;
2258         //if (!(c->IsUsed(10))) c->Use();  
2259         c->Use(10);  
2260       }
2261   }
2262   
2263   
2264   //
2265   Double_t maxchi  = meanchi+2.*smeanchi;
2266
2267   for (Int_t i=0; i<nseed; i++) {
2268     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2269     if (!pt) {
2270       continue;
2271     }    
2272     //if (!(pt->IsActive())) continue;
2273     if (pt->fBSigned) continue;
2274     Double_t chi     = pt->GetChi2()/pt->GetNumberOfClusters();
2275     if (chi>maxchi) continue;
2276
2277     Float_t bfactor=1;
2278     Float_t dens = pt->GetNumberOfClusters()/Float_t(pt->fNFoundable);
2279    
2280     //sign only tracks with enoug big density at the beginning
2281     
2282     if ((pt->GetDensityFirst(40)<0.75) && pt->GetNumberOfClusters()<meann) continue; 
2283     
2284     
2285     Double_t mindens = TMath::Max(double(mdensity-sdensity*fdensity*bfactor),0.65);
2286     Double_t minn    = TMath::Max(Int_t(meann-fnumber*smeann*bfactor),50);
2287    
2288     //    if (pt->fBConstrain) mindens = TMath::Max(mdensity-sdensity*fdensity*bfactor,0.65);
2289     if ( (pt->fRemoval==10) && (pt->GetSnp()>0.8)&&(dens>mindens))
2290       minn=0;
2291
2292     if ((dens>mindens && pt->GetNumberOfClusters()>minn) && chi<maxchi ){
2293       //Int_t noc=pt->GetNumberOfClusters();
2294       pt->fBSigned = kTRUE;
2295       for (Int_t i=0; i<160; i++) {
2296
2297         Int_t index=pt->GetClusterIndex2(i);
2298         if (index<0) continue;
2299         AliTPCclusterMI *c= pt->fClusterPointer[i];
2300         if (!c) continue;
2301         //      if (!(c->IsUsed(10))) c->Use();  
2302         c->Use(10);  
2303       }
2304     }
2305   }
2306   //  gLastCheck = nseed;
2307   //  arr->Compress();
2308   if (fDebug>0){
2309     timer.Print();
2310   }
2311 }
2312
2313
2314 void  AliTPCtrackerMI::StopNotActive(TObjArray * arr, Int_t row0, Float_t th0, Float_t th1, Float_t th2)
2315 {
2316   // stop not active tracks
2317   // take th1 as threshold for number of founded to number of foundable on last 10 active rows
2318   // take th2 as threshold for number of founded to number of foundable on last 20 active rows 
2319   Int_t nseed = arr->GetEntriesFast();  
2320   //
2321   for (Int_t i=0; i<nseed; i++) {
2322     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2323     if (!pt) {
2324       continue;
2325     }
2326     if (!(pt->IsActive())) continue;
2327     StopNotActive(pt,row0,th0, th1,th2);
2328   }
2329 }
2330
2331
2332
2333 void  AliTPCtrackerMI::StopNotActive(AliTPCseed * seed, Int_t row0, Float_t th0, Float_t th1,
2334  Float_t th2)
2335 {
2336   // stop not active tracks
2337   // take th1 as threshold for number of founded to number of foundable on last 10 active rows
2338   // take th2 as threshold for number of founded to number of foundable on last 20 active rows 
2339   Int_t sumgood1  = 0;
2340   Int_t sumgood2  = 0;
2341   Int_t foundable = 0;
2342   Int_t maxindex = seed->fLastPoint;  //last foundable row
2343   if (seed->fNFoundable*th0 > seed->GetNumberOfClusters()) {
2344     seed->Desactivate(10) ;
2345     return;
2346   }
2347
2348   for (Int_t i=row0; i<maxindex; i++){
2349     Int_t index = seed->GetClusterIndex2(i);
2350     if (index!=-1) foundable++;
2351     //if (!c) continue;
2352     if (foundable<=30) sumgood1++;
2353     if (foundable<=50) {
2354       sumgood2++;
2355     }
2356     else{ 
2357       break;
2358     }        
2359   }
2360   if (foundable>=30.){ 
2361      if (sumgood1<(th1*30.)) seed->Desactivate(10);
2362   }
2363   if (foundable>=50)
2364     if (sumgood2<(th2*50.)) seed->Desactivate(10);
2365 }
2366
2367
2368
2369 Int_t AliTPCtrackerMI::PropagateBack(AliESD *event)
2370 {
2371   //
2372   // back propagation of ESD tracks
2373   //
2374
2375   fEvent = event;
2376   ReadSeeds(event);
2377   PropagateBack(fSeeds);
2378   Int_t nseed = fSeeds->GetEntriesFast();
2379   for (Int_t i=0;i<nseed;i++){
2380     AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
2381     AliESDtrack *esd=event->GetTrack(i);
2382     seed->CookdEdx(0.02,0.06);
2383     CookLabel(seed,0.1); //For comparison only
2384     esd->UpdateTrackParams(seed,AliESDtrack::kTPCout);
2385   }
2386   fEvent =0;
2387   WriteTracks();
2388   return 0;
2389 }
2390
2391
2392 void AliTPCtrackerMI::DeleteSeeds()
2393 {
2394   Int_t nseed = fSeeds->GetEntriesFast();
2395   for (Int_t i=0;i<nseed;i++){
2396     AliTPCseed * seed = (AliTPCseed*)fSeeds->At(i);
2397     if (seed) delete fSeeds->RemoveAt(i);
2398   }
2399   delete fSeeds;
2400   fSeeds =0;
2401 }
2402
2403 void AliTPCtrackerMI::ReadSeeds(AliESD *event)
2404 {
2405   //
2406   //read seeds from the event
2407   
2408   Int_t nentr=event->GetNumberOfTracks();
2409   Info("PropagateBack", "Number of ESD tracks: %d\n", nentr);
2410   if (fSeeds) 
2411     DeleteSeeds();
2412   if (!fSeeds){   
2413     fSeeds = new TObjArray;
2414   }
2415   
2416   //  Int_t ntrk=0;
2417   for (Int_t i=0; i<nentr; i++) {
2418     AliESDtrack *esd=event->GetTrack(i);
2419     ULong_t status=esd->GetStatus();    
2420     const AliTPCtrack t(*esd);
2421     AliTPCseed *seed = new AliTPCseed(t,t.GetAlpha());
2422     if (status==AliESDtrack::kTPCin) seed->Modify(0.8);
2423     //
2424     //
2425     // rotate to the local coordinate system
2426    
2427     fSectors=fInnerSec; fN=fkNIS;
2428     
2429     Double_t alpha=seed->GetAlpha() - fSectors->GetAlphaShift();
2430     if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
2431     if (alpha < 0.            ) alpha += 2.*TMath::Pi();
2432     Int_t ns=Int_t(alpha/fSectors->GetAlpha())%fN;
2433     alpha =ns*fSectors->GetAlpha() + fSectors->GetAlphaShift();
2434     alpha-=seed->GetAlpha();  
2435     if (!seed->Rotate(alpha)) continue;
2436     //
2437     seed->PropagateTo(fSectors->GetX(0));
2438     //
2439     //    Int_t index = esd->GetTPCindex();
2440     //AliTPCseed * seed2= (AliTPCseed*)fSeeds->At(index);
2441     
2442     fSeeds->AddLast(seed);
2443   }
2444 }
2445
2446
2447
2448 //_____________________________________________________________________________
2449 void AliTPCtrackerMI::MakeSeeds3(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,  Float_t cuts[4],
2450                                  Float_t deltay, Int_t ddsec) {
2451   //-----------------------------------------------------------------
2452   // This function creates track seeds.
2453   // SEEDING WITH VERTEX CONSTRAIN 
2454   //-----------------------------------------------------------------
2455   // cuts[0]   - fP4 cut
2456   // cuts[1]   - tan(phi)  cut
2457   // cuts[2]   - zvertex cut
2458   // cuts[3]   - fP3 cut
2459   Int_t nin0  = 0;
2460   Int_t nin1  = 0;
2461   Int_t nin2  = 0;
2462   Int_t nin   = 0;
2463   Int_t nout1 = 0;
2464   Int_t nout2 = 0;
2465
2466   Double_t x[5], c[15];
2467   //  Int_t di = i1-i2;
2468   //
2469   AliTPCseed * seed = new AliTPCseed;
2470   Double_t alpha=fSectors->GetAlpha(), shift=fSectors->GetAlphaShift();
2471   Double_t cs=cos(alpha), sn=sin(alpha);
2472   //
2473   //  Double_t x1 =fOuterSec->GetX(i1);
2474   //Double_t xx2=fOuterSec->GetX(i2);
2475   
2476   Double_t x1 =GetXrow(i1);
2477   Double_t xx2=GetXrow(i2);
2478
2479   Double_t x3=GetX(), y3=GetY(), z3=GetZ();
2480
2481   Int_t imiddle = (i2+i1)/2;    //middle pad row index
2482   Double_t xm = GetXrow(imiddle); // radius of middle pad-row
2483   const AliTPCRow& krm=GetRow(sec,imiddle); //middle pad -row
2484   //
2485   Int_t ns =sec;   
2486
2487   const AliTPCRow& kr1=GetRow(ns,i1);
2488   Double_t ymax  = GetMaxY(i1)-kr1.fDeadZone-1.5;  
2489   Double_t ymaxm = GetMaxY(imiddle)-kr1.fDeadZone-1.5;  
2490
2491   //
2492   // change cut on curvature if it can't reach this layer
2493   // maximal curvature set to reach it
2494   Double_t dvertexmax  = TMath::Sqrt((x1-x3)*(x1-x3)+(ymax+5-y3)*(ymax+5-y3));
2495   if (dvertexmax*0.5*cuts[0]>0.85){
2496     cuts[0] = 0.85/(dvertexmax*0.5+1.);
2497   }
2498   Double_t r2min = 1/(cuts[0]*cuts[0]);  //minimal square of radius given by cut
2499
2500   //  Int_t ddsec = 1;
2501   if (deltay>0) ddsec = 0; 
2502   // loop over clusters  
2503   for (Int_t is=0; is < kr1; is++) {
2504     //
2505     if (kr1[is]->IsUsed(10)) continue;
2506     Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();    
2507     //if (TMath::Abs(y1)>ymax) continue;
2508
2509     if (deltay>0 && TMath::Abs(ymax-TMath::Abs(y1))> deltay ) continue;  // seed only at the edge
2510
2511     // find possible directions    
2512     Float_t anglez = (z1-z3)/(x1-x3); 
2513     Float_t extraz = z1 - anglez*(x1-xx2);  // extrapolated z      
2514     //
2515     //
2516     //find   rotation angles relative to line given by vertex and point 1
2517     Double_t dvertex2 = (x1-x3)*(x1-x3)+(y1-y3)*(y1-y3);
2518     Double_t dvertex  = TMath::Sqrt(dvertex2);
2519     Double_t angle13  = TMath::ATan((y1-y3)/(x1-x3));
2520     Double_t cs13     = cos(-angle13), sn13 = sin(-angle13);            
2521     
2522     //
2523     // loop over 2 sectors
2524     Int_t dsec1=-ddsec;
2525     Int_t dsec2= ddsec;
2526     if (y1<0)  dsec2= 0;
2527     if (y1>0)  dsec1= 0;
2528     
2529     Double_t dddz1=0;  // direction of delta inclination in z axis
2530     Double_t dddz2=0;
2531     if ( (z1-z3)>0)
2532       dddz1 =1;    
2533     else
2534       dddz2 =1;
2535     //
2536     for (Int_t dsec = dsec1; dsec<=dsec2;dsec++){
2537       Int_t sec2 = sec + dsec;
2538       // 
2539       //      AliTPCRow&  kr2  = fOuterSec[(sec2+fkNOS)%fkNOS][i2];
2540       //AliTPCRow&  kr2m = fOuterSec[(sec2+fkNOS)%fkNOS][imiddle];
2541       AliTPCRow&  kr2  = GetRow((sec2+fkNOS)%fkNOS,i2);
2542       AliTPCRow&  kr2m = GetRow((sec2+fkNOS)%fkNOS,imiddle);
2543       Int_t  index1 = TMath::Max(kr2.Find(extraz-0.6-dddz1*TMath::Abs(z1)*0.05)-1,0);
2544       Int_t  index2 = TMath::Min(kr2.Find(extraz+0.6+dddz2*TMath::Abs(z1)*0.05)+1,kr2);
2545
2546       // rotation angles to p1-p3
2547       Double_t cs13r     = cos(-angle13+dsec*alpha)/dvertex, sn13r = sin(-angle13+dsec*alpha)/dvertex;            
2548       Double_t x2,   y2,   z2; 
2549       //
2550       //      Double_t dymax = maxangle*TMath::Abs(x1-xx2);
2551
2552       //
2553       Double_t dxx0 =  (xx2-x3)*cs13r;
2554       Double_t dyy0 =  (xx2-x3)*sn13r;
2555       for (Int_t js=index1; js < index2; js++) {
2556         const AliTPCclusterMI *kcl = kr2[js];
2557         if (kcl->IsUsed(10)) continue;  
2558         //
2559         //calcutate parameters
2560         //      
2561         Double_t yy0 =  dyy0 +(kcl->GetY()-y3)*cs13r;
2562         // stright track
2563         if (TMath::Abs(yy0)<0.000001) continue;
2564         Double_t xx0 =  dxx0 -(kcl->GetY()-y3)*sn13r;
2565         Double_t y0  =  0.5*(xx0*xx0+yy0*yy0-xx0)/yy0;
2566         Double_t r02 = (0.25+y0*y0)*dvertex2;   
2567         //curvature (radius) cut
2568         if (r02<r2min) continue;                
2569        
2570         nin0++; 
2571         //
2572         Double_t c0  = 1/TMath::Sqrt(r02);
2573         if (yy0>0) c0*=-1.;     
2574                
2575        
2576         //Double_t dfi0   = 2.*TMath::ASin(dvertex*c0*0.5);
2577         //Double_t dfi1   = 2.*TMath::ASin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);
2578         Double_t dfi0   = 2.*TPCFastMath::FastAsin(dvertex*c0*0.5);
2579         Double_t dfi1   = 2.*TPCFastMath::FastAsin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);  
2580         //
2581         //
2582         Double_t z0  =  kcl->GetZ();  
2583         Double_t zzzz2    = z1-(z1-z3)*dfi1/dfi0;
2584         if (TMath::Abs(zzzz2-z0)>0.5) continue;       
2585         nin1++;              
2586         //      
2587         Double_t dip    = (z1-z0)*c0/dfi1;        
2588         Double_t x0 = (0.5*cs13+y0*sn13)*dvertex*c0;
2589         //
2590         y2 = kcl->GetY(); 
2591         if (dsec==0){
2592           x2 = xx2; 
2593           z2 = kcl->GetZ();       
2594         }
2595         else
2596           {
2597             // rotation 
2598             z2 = kcl->GetZ();  
2599             x2= xx2*cs-y2*sn*dsec;
2600             y2=+xx2*sn*dsec+y2*cs;
2601           }
2602         
2603         x[0] = y1;
2604         x[1] = z1;
2605         x[2] = x0;
2606         x[3] = dip;
2607         x[4] = c0;
2608         //
2609         //
2610         // do we have cluster at the middle ?
2611         Double_t ym,zm;
2612         GetProlongation(x1,xm,x,ym,zm);
2613         UInt_t dummy; 
2614         AliTPCclusterMI * cm=0;
2615         if (TMath::Abs(ym)-ymaxm<0){      
2616           cm = krm.FindNearest2(ym,zm,1.0,0.6,dummy);
2617           if ((!cm) || (cm->IsUsed(10))) {        
2618             continue;
2619           }
2620         }
2621         else{     
2622           // rotate y1 to system 0
2623           // get state vector in rotated system 
2624           Double_t yr1  = (-0.5*sn13+y0*cs13)*dvertex*c0;
2625           Double_t xr2  =  x0*cs+yr1*sn*dsec;
2626           Double_t xr[5]={kcl->GetY(),kcl->GetZ(), xr2, dip, c0};
2627           //
2628           GetProlongation(xx2,xm,xr,ym,zm);
2629           if (TMath::Abs(ym)-ymaxm<0){
2630             cm = kr2m.FindNearest2(ym,zm,1.0,0.6,dummy);
2631             if ((!cm) || (cm->IsUsed(10))) {      
2632               continue;
2633             }
2634           }
2635         }
2636        
2637
2638         Double_t dym = 0;
2639         Double_t dzm = 0;
2640         if (cm){
2641           dym = ym - cm->GetY();
2642           dzm = zm - cm->GetZ();
2643         }
2644         nin2++;
2645
2646
2647         //
2648         //
2649         Double_t sy1=kr1[is]->GetSigmaY2()*2., sz1=kr1[is]->GetSigmaZ2()*2.;
2650         Double_t sy2=kcl->GetSigmaY2()*2.,     sz2=kcl->GetSigmaZ2()*2.;
2651         //Double_t sy3=400*3./12., sy=0.1, sz=0.1;
2652         Double_t sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
2653         //Double_t sy3=25000*x[4]*x[4]*60+0.5, sy=0.1, sz=0.1;
2654
2655         Double_t f40=(f1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
2656         Double_t f42=(f1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
2657         Double_t f43=(f1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
2658         Double_t f20=(f2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
2659         Double_t f22=(f2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
2660         Double_t f23=(f2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
2661         
2662         Double_t f30=(f3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
2663         Double_t f31=(f3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
2664         Double_t f32=(f3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
2665         Double_t f34=(f3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
2666         
2667         c[0]=sy1;
2668         c[1]=0.;       c[2]=sz1;
2669         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
2670         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
2671                        c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
2672         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
2673         c[13]=f30*sy1*f40+f32*sy2*f42;
2674         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
2675         
2676         //      if (!BuildSeed(kr1[is],kcl,0,x1,x2,x3,x,c)) continue;
2677         
2678         UInt_t index=kr1.GetIndex(is);
2679         AliTPCseed *track=new(seed) AliTPCseed(index, x, c, x1, ns*alpha+shift);
2680         
2681         track->fIsSeeding = kTRUE;
2682         track->fSeed1 = i1;
2683         track->fSeed2 = i2;
2684         track->fSeedType=3;
2685
2686        
2687         //if (dsec==0) {
2688           FollowProlongation(*track, (i1+i2)/2,1);
2689           Int_t foundable,found,shared;
2690           track->GetClusterStatistic((i1+i2)/2,i1, found, foundable, shared, kTRUE);
2691           if ((found<0.55*foundable)  || shared>0.5*found || (track->GetSigmaY2()+track->GetSigmaZ2())>0.5){
2692             seed->Reset();
2693             seed->~AliTPCseed();
2694             continue;
2695           }
2696           //}
2697         
2698         nin++;
2699         FollowProlongation(*track, i2,1);
2700         
2701         
2702         //Int_t rc = 1;
2703         track->fBConstrain =1;
2704         //      track->fLastPoint = i1+fInnerSec->GetNRows();  // first cluster in track position
2705         track->fLastPoint = i1;  // first cluster in track position
2706         track->fFirstPoint = track->fLastPoint;
2707         
2708         if (track->GetNumberOfClusters()<(i1-i2)*0.5 || 
2709             track->GetNumberOfClusters() < track->fNFoundable*0.6 || 
2710             track->fNShared>0.4*track->GetNumberOfClusters() ) {
2711           seed->Reset();
2712           seed->~AliTPCseed();
2713           continue;
2714         }
2715         nout1++;
2716         // Z VERTEX CONDITION
2717         Double_t zv;
2718         zv = track->GetZ()+track->GetTgl()/track->GetC()*
2719           ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2720         if (TMath::Abs(zv-z3)>cuts[2]) {
2721           FollowProlongation(*track, TMath::Max(i2-20,0));
2722           zv = track->GetZ()+track->GetTgl()/track->GetC()*
2723             ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2724           if (TMath::Abs(zv-z3)>cuts[2]){
2725             FollowProlongation(*track, TMath::Max(i2-40,0));
2726             zv = track->GetZ()+track->GetTgl()/track->GetC()*
2727               ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2728             if (TMath::Abs(zv-z3)>cuts[2] &&(track->GetNumberOfClusters() > track->fNFoundable*0.7)){
2729               // make seed without constrain
2730               AliTPCseed * track2 = MakeSeed(track,0.2,0.5,1.);
2731               FollowProlongation(*track2, i2,1);
2732               track2->fBConstrain = kFALSE;
2733               track2->fSeedType = 1;
2734               arr->AddLast(track2); 
2735               seed->Reset();
2736               seed->~AliTPCseed();
2737               continue;         
2738             }
2739             else{
2740               seed->Reset();
2741               seed->~AliTPCseed();
2742               continue;
2743             
2744             }
2745           }
2746         }
2747         
2748         track->fSeedType =0;
2749         arr->AddLast(track); 
2750         seed = new AliTPCseed;  
2751         nout2++;
2752         // don't consider other combinations
2753         if (track->GetNumberOfClusters() > track->fNFoundable*0.8)
2754           break;
2755       }
2756     }
2757   }
2758   if (fDebug>1){
2759     //    printf("\nSeeding statiistic:\t%d\t%d\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin,nout1,nout2);
2760   }
2761   delete seed;
2762 }
2763
2764
2765 void AliTPCtrackerMI::MakeSeeds5(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,  Float_t cuts[4],
2766                                  Float_t deltay) {
2767   
2768
2769
2770   //-----------------------------------------------------------------
2771   // This function creates track seeds.
2772   //-----------------------------------------------------------------
2773   // cuts[0]   - fP4 cut
2774   // cuts[1]   - tan(phi)  cut
2775   // cuts[2]   - zvertex cut
2776   // cuts[3]   - fP3 cut
2777
2778
2779   Int_t nin0  = 0;
2780   Int_t nin1  = 0;
2781   Int_t nin2  = 0;
2782   Int_t nin   = 0;
2783   Int_t nout1 = 0;
2784   Int_t nout2 = 0;
2785   Int_t nout3 =0;
2786   Double_t x[5], c[15];
2787   //
2788   // make temporary seed
2789   AliTPCseed * seed = new AliTPCseed;
2790   Double_t alpha=fOuterSec->GetAlpha(), shift=fOuterSec->GetAlphaShift();
2791   //  Double_t cs=cos(alpha), sn=sin(alpha);
2792   //
2793   //
2794
2795   // first 3 padrows
2796   Double_t x1 = GetXrow(i1-1);
2797   const    AliTPCRow& kr1=GetRow(sec,i1-1);
2798   Double_t y1max  = GetMaxY(i1-1)-kr1.fDeadZone-1.5;  
2799   //
2800   Double_t x1p = GetXrow(i1);
2801   const    AliTPCRow& kr1p=GetRow(sec,i1);
2802   //
2803   Double_t x1m = GetXrow(i1-2);
2804   const    AliTPCRow& kr1m=GetRow(sec,i1-2);
2805
2806   //
2807   //last 3 padrow for seeding
2808   AliTPCRow&  kr3  = GetRow((sec+fkNOS)%fkNOS,i1-7);
2809   Double_t    x3   =  GetXrow(i1-7);
2810   //  Double_t    y3max= GetMaxY(i1-7)-kr3.fDeadZone-1.5;  
2811   //
2812   AliTPCRow&  kr3p  = GetRow((sec+fkNOS)%fkNOS,i1-6);
2813   Double_t    x3p   = GetXrow(i1-6);
2814   //
2815   AliTPCRow&  kr3m  = GetRow((sec+fkNOS)%fkNOS,i1-8);
2816   Double_t    x3m   = GetXrow(i1-8);
2817
2818   //
2819   //
2820   // middle padrow
2821   Int_t im = i1-4;                           //middle pad row index
2822   Double_t xm         = GetXrow(im);         // radius of middle pad-row
2823   const AliTPCRow& krm=GetRow(sec,im);   //middle pad -row
2824   //  Double_t ymmax = GetMaxY(im)-kr1.fDeadZone-1.5;  
2825   //
2826   //
2827   Double_t deltax  = x1-x3;
2828   Double_t dymax   = deltax*cuts[1];
2829   Double_t dzmax   = deltax*cuts[3];
2830   //
2831   // loop over clusters  
2832   for (Int_t is=0; is < kr1; is++) {
2833     //
2834     if (kr1[is]->IsUsed(10)) continue;
2835     Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();    
2836     //
2837     if (deltay>0 && TMath::Abs(y1max-TMath::Abs(y1))> deltay ) continue;  // seed only at the edge    
2838     // 
2839     Int_t  index1 = TMath::Max(kr3.Find(z1-dzmax)-1,0);
2840     Int_t  index2 = TMath::Min(kr3.Find(z1+dzmax)+1,kr3);
2841     //    
2842     Double_t y3,   z3;
2843     //
2844     //
2845     UInt_t index;
2846     for (Int_t js=index1; js < index2; js++) {
2847       const AliTPCclusterMI *kcl = kr3[js];
2848       if (kcl->IsUsed(10)) continue;
2849       y3 = kcl->GetY(); 
2850       // apply angular cuts
2851       if (TMath::Abs(y1-y3)>dymax) continue;
2852       x3 = x3; 
2853       z3 = kcl->GetZ(); 
2854       if (TMath::Abs(z1-z3)>dzmax) continue;
2855       //
2856       Double_t angley = (y1-y3)/(x1-x3);
2857       Double_t anglez = (z1-z3)/(x1-x3);
2858       //
2859       Double_t erry = TMath::Abs(angley)*(x1-x1m)*0.5+0.5;
2860       Double_t errz = TMath::Abs(anglez)*(x1-x1m)*0.5+0.5;
2861       //
2862       Double_t yyym = angley*(xm-x1)+y1;
2863       Double_t zzzm = anglez*(xm-x1)+z1;
2864
2865       const AliTPCclusterMI *kcm = krm.FindNearest2(yyym,zzzm,erry,errz,index);
2866       if (!kcm) continue;
2867       if (kcm->IsUsed(10)) continue;
2868       
2869       erry = TMath::Abs(angley)*(x1-x1m)*0.4+0.5;
2870       errz = TMath::Abs(anglez)*(x1-x1m)*0.4+0.5;
2871       //
2872       //
2873       //
2874       Int_t used  =0;
2875       Int_t found =0;
2876       //
2877       // look around first
2878       const AliTPCclusterMI *kc1m = kr1m.FindNearest2(angley*(x1m-x1)+y1,
2879                                                       anglez*(x1m-x1)+z1,
2880                                                       erry,errz,index);
2881       //
2882       if (kc1m){
2883         found++;
2884         if (kc1m->IsUsed(10)) used++;
2885       }
2886       const AliTPCclusterMI *kc1p = kr1p.FindNearest2(angley*(x1p-x1)+y1,
2887                                                       anglez*(x1p-x1)+z1,
2888                                                       erry,errz,index);
2889       //
2890       if (kc1p){
2891         found++;
2892         if (kc1p->IsUsed(10)) used++;
2893       }
2894       if (used>1)  continue;
2895       if (found<1) continue; 
2896
2897       //
2898       // look around last
2899       const AliTPCclusterMI *kc3m = kr3m.FindNearest2(angley*(x3m-x3)+y3,
2900                                                       anglez*(x3m-x3)+z3,
2901                                                       erry,errz,index);
2902       //
2903       if (kc3m){
2904         found++;
2905         if (kc3m->IsUsed(10)) used++;
2906       }
2907       else 
2908         continue;
2909       const AliTPCclusterMI *kc3p = kr3p.FindNearest2(angley*(x3p-x3)+y3,
2910                                                       anglez*(x3p-x3)+z3,
2911                                                       erry,errz,index);
2912       //
2913       if (kc3p){
2914         found++;
2915         if (kc3p->IsUsed(10)) used++;
2916       }
2917       else 
2918         continue;
2919       if (used>1)  continue;
2920       if (found<3) continue;       
2921       //
2922       Double_t x2,y2,z2;
2923       x2 = xm;
2924       y2 = kcm->GetY();
2925       z2 = kcm->GetZ();
2926       //
2927                         
2928       x[0]=y1;
2929       x[1]=z1;
2930       x[4]=f1(x1,y1,x2,y2,x3,y3);
2931       //if (TMath::Abs(x[4]) >= cuts[0]) continue;
2932       nin0++;
2933       //
2934       x[2]=f2(x1,y1,x2,y2,x3,y3);
2935       nin1++;
2936       //
2937       x[3]=f3n(x1,y1,x2,y2,z1,z2,x[4]);
2938       //if (TMath::Abs(x[3]) > cuts[3]) continue;
2939       nin2++;
2940       //
2941       //
2942       Double_t sy1=0.1,  sz1=0.1;
2943       Double_t sy2=0.1,  sz2=0.1;
2944       Double_t sy3=0.1,  sy=0.1, sz=0.1;
2945       
2946       Double_t f40=(f1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
2947       Double_t f42=(f1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
2948       Double_t f43=(f1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
2949       Double_t f20=(f2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
2950       Double_t f22=(f2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
2951       Double_t f23=(f2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
2952       
2953       Double_t f30=(f3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
2954       Double_t f31=(f3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
2955       Double_t f32=(f3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
2956       Double_t f34=(f3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
2957       
2958       c[0]=sy1;
2959       c[1]=0.;       c[2]=sz1; 
2960       c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
2961       c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
2962       c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
2963       c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
2964       c[13]=f30*sy1*f40+f32*sy2*f42;
2965       c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
2966       
2967       //        if (!BuildSeed(kr1[is],kcl,0,x1,x2,x3,x,c)) continue;
2968       
2969       UInt_t index=kr1.GetIndex(is);
2970       AliTPCseed *track=new(seed) AliTPCseed(index, x, c, x1, sec*alpha+shift);
2971       
2972       track->fIsSeeding = kTRUE;
2973
2974       nin++;      
2975       FollowProlongation(*track, i1-7,1);
2976       if (track->GetNumberOfClusters() < track->fNFoundable*0.75 || 
2977           track->fNShared>0.6*track->GetNumberOfClusters() || ( track->GetSigmaY2()+ track->GetSigmaZ2())>0.6){
2978         seed->Reset();
2979         seed->~AliTPCseed();
2980         continue;
2981       }
2982       nout1++;
2983       nout2++;  
2984       //Int_t rc = 1;
2985       FollowProlongation(*track, i2,1);
2986       track->fBConstrain =0;
2987       track->fLastPoint = i1+fInnerSec->GetNRows();  // first cluster in track position
2988       track->fFirstPoint = track->fLastPoint;
2989       
2990       if (track->GetNumberOfClusters()<(i1-i2)*0.5 || 
2991           track->GetNumberOfClusters()<track->fNFoundable*0.7 || 
2992           track->fNShared>2. || track->GetChi2()/track->GetNumberOfClusters()>6 || ( track->GetSigmaY2()+ track->GetSigmaZ2())>0.5 ) {
2993         seed->Reset();
2994         seed->~AliTPCseed();
2995         continue;
2996       }
2997    
2998       {
2999         FollowProlongation(*track, TMath::Max(i2-10,0),1);
3000         AliTPCseed * track2 = MakeSeed(track,0.2,0.5,0.9);
3001         FollowProlongation(*track2, i2,1);
3002         track2->fBConstrain = kFALSE;
3003         track2->fSeedType = 4;
3004         arr->AddLast(track2); 
3005         seed->Reset();
3006         seed->~AliTPCseed();
3007       }
3008       
3009    
3010       //arr->AddLast(track); 
3011       //seed = new AliTPCseed;  
3012       nout3++;
3013     }
3014   }
3015   
3016   if (fDebug>1){
3017     //    printf("\nSeeding statiistic:\t%d\t%d\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin,nout1,nout2,nout3);
3018   }
3019   delete seed;
3020 }
3021
3022
3023 //_____________________________________________________________________________
3024 void AliTPCtrackerMI::MakeSeeds2(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2, Float_t */*cuts[4]*/,
3025                                  Float_t deltay, Bool_t /*bconstrain*/) {
3026   //-----------------------------------------------------------------
3027   // This function creates track seeds - without vertex constraint
3028   //-----------------------------------------------------------------
3029   // cuts[0]   - fP4 cut        - not applied
3030   // cuts[1]   - tan(phi)  cut
3031   // cuts[2]   - zvertex cut    - not applied 
3032   // cuts[3]   - fP3 cut
3033   Int_t nin0=0;
3034   Int_t nin1=0;
3035   Int_t nin2=0;
3036   Int_t nin3=0;
3037   //  Int_t nin4=0;
3038   //Int_t nin5=0;
3039
3040   
3041
3042   Double_t alpha=fOuterSec->GetAlpha(), shift=fOuterSec->GetAlphaShift();
3043   //  Double_t cs=cos(alpha), sn=sin(alpha);
3044   Int_t row0 = (i1+i2)/2;
3045   Int_t drow = (i1-i2)/2;
3046   const AliTPCRow& kr0=fSectors[sec][row0];
3047   AliTPCRow * kr=0;
3048
3049   AliTPCpolyTrack polytrack;
3050   Int_t nclusters=fSectors[sec][row0];
3051   AliTPCseed * seed = new AliTPCseed;
3052
3053   Int_t sumused=0;
3054   Int_t cused=0;
3055   Int_t cnused=0;
3056   for (Int_t is=0; is < nclusters; is++) {  //LOOP over clusters
3057     Int_t nfound =0;
3058     Int_t nfoundable =0;
3059     for (Int_t iter =1; iter<2; iter++){   //iterations
3060       const AliTPCRow& krm=fSectors[sec][row0-iter];
3061       const AliTPCRow& krp=fSectors[sec][row0+iter];      
3062       const AliTPCclusterMI * cl= kr0[is];
3063       
3064       if (cl->IsUsed(10)) {
3065         cused++;
3066       }
3067       else{
3068         cnused++;
3069       }
3070       Double_t x = kr0.GetX();
3071       // Initialization of the polytrack
3072       nfound =0;
3073       nfoundable =0;
3074       polytrack.Reset();
3075       //
3076       Double_t y0= cl->GetY();
3077       Double_t z0= cl->GetZ();
3078       Float_t erry = 0;
3079       Float_t errz = 0;
3080       
3081       Double_t ymax = fSectors->GetMaxY(row0)-kr0.fDeadZone-1.5;
3082       if (deltay>0 && TMath::Abs(ymax-TMath::Abs(y0))> deltay ) continue;  // seed only at the edge
3083       
3084       erry = (0.5)*cl->GetSigmaY2()/TMath::Sqrt(cl->GetQ())*6;      
3085       errz = (0.5)*cl->GetSigmaZ2()/TMath::Sqrt(cl->GetQ())*6;      
3086       polytrack.AddPoint(x,y0,z0,erry, errz);
3087
3088       sumused=0;
3089       if (cl->IsUsed(10)) sumused++;
3090
3091
3092       Float_t roady = (5*TMath::Sqrt(cl->GetSigmaY2()+0.2)+1.)*iter;
3093       Float_t roadz = (5*TMath::Sqrt(cl->GetSigmaZ2()+0.2)+1.)*iter;
3094       //
3095       x = krm.GetX();
3096       AliTPCclusterMI * cl1 = krm.FindNearest(y0,z0,roady,roadz);
3097       if (cl1 && TMath::Abs(ymax-TMath::Abs(y0))) {
3098         erry = (0.5)*cl1->GetSigmaY2()/TMath::Sqrt(cl1->GetQ())*3;          
3099         errz = (0.5)*cl1->GetSigmaZ2()/TMath::Sqrt(cl1->GetQ())*3;
3100         if (cl1->IsUsed(10))  sumused++;
3101         polytrack.AddPoint(x,cl1->GetY(),cl1->GetZ(),erry,errz);
3102       }
3103       //
3104       x = krp.GetX();
3105       AliTPCclusterMI * cl2 = krp.FindNearest(y0,z0,roady,roadz);
3106       if (cl2) {
3107         erry = (0.5)*cl2->GetSigmaY2()/TMath::Sqrt(cl2->GetQ())*3;          
3108         errz = (0.5)*cl2->GetSigmaZ2()/TMath::Sqrt(cl2->GetQ())*3;
3109         if (cl2->IsUsed(10)) sumused++;  
3110         polytrack.AddPoint(x,cl2->GetY(),cl2->GetZ(),erry,errz);
3111       }
3112       //
3113       if (sumused>0) continue;
3114       nin0++;
3115       polytrack.UpdateParameters();
3116       // follow polytrack
3117       roadz = 1.2;
3118       roady = 1.2;
3119       //
3120       Double_t yn,zn;
3121       nfoundable = polytrack.GetN();
3122       nfound     = nfoundable; 
3123       //
3124       for (Int_t ddrow = iter+1; ddrow<drow;ddrow++){
3125         Float_t maxdist = 0.8*(1.+3./(ddrow));
3126         for (Int_t delta = -1;delta<=1;delta+=2){
3127           Int_t row = row0+ddrow*delta;
3128           kr = &(fSectors[sec][row]);
3129           Double_t xn = kr->GetX();
3130           Double_t ymax = fSectors->GetMaxY(row)-kr->fDeadZone-1.5;
3131           polytrack.GetFitPoint(xn,yn,zn);
3132           if (TMath::Abs(yn)>ymax) continue;
3133           nfoundable++;
3134           AliTPCclusterMI * cln = kr->FindNearest(yn,zn,roady,roadz);
3135           if (cln) {
3136             Float_t dist =  TMath::Sqrt(  (yn-cln->GetY())*(yn-cln->GetY())+(zn-cln->GetZ())*(zn-cln->GetZ()));
3137             if (dist<maxdist){
3138               /*
3139               erry = (dist+0.3)*cln->GetSigmaY2()/TMath::Sqrt(cln->GetQ())*(1.+1./(ddrow));         
3140               errz = (dist+0.3)*cln->GetSigmaZ2()/TMath::Sqrt(cln->GetQ())*(1.+1./(ddrow));
3141               if (cln->IsUsed(10)) {
3142                 //      printf("used\n");
3143                 sumused++;
3144                 erry*=2;
3145                 errz*=2;
3146               }
3147               */
3148               erry=0.1;
3149               errz=0.1;
3150               polytrack.AddPoint(xn,cln->GetY(),cln->GetZ(),erry, errz);
3151               nfound++;
3152             }
3153           }
3154         }
3155         if ( (sumused>3) || (sumused>0.5*nfound) || (nfound<0.6*nfoundable))  break;     
3156         polytrack.UpdateParameters();
3157       }           
3158     }
3159     if ( (sumused>3) || (sumused>0.5*nfound))  {
3160       //printf("sumused   %d\n",sumused);
3161       continue;
3162     }
3163     nin1++;
3164     Double_t dy,dz;
3165     polytrack.GetFitDerivation(kr0.GetX(),dy,dz);
3166     AliTPCpolyTrack track2;
3167     
3168     polytrack.Refit(track2,0.5+TMath::Abs(dy)*0.3,0.4+TMath::Abs(dz)*0.3);
3169     if (track2.GetN()<0.5*nfoundable) continue;
3170     nin2++;
3171
3172     if ((nfound>0.6*nfoundable) &&( nfoundable>0.4*(i1-i2))) {
3173       //
3174       // test seed with and without constrain
3175       for (Int_t constrain=0; constrain<=0;constrain++){
3176         // add polytrack candidate
3177
3178         Double_t x[5], c[15];
3179         Double_t x1,x2,x3,y1,y2,y3,z1,z2,z3;
3180         track2.GetBoundaries(x3,x1);    
3181         x2 = (x1+x3)/2.;
3182         track2.GetFitPoint(x1,y1,z1);
3183         track2.GetFitPoint(x2,y2,z2);
3184         track2.GetFitPoint(x3,y3,z3);
3185         //
3186         //is track pointing to the vertex ?
3187         Double_t x0,y0,z0;
3188         x0=0;
3189         polytrack.GetFitPoint(x0,y0,z0);
3190
3191         if (constrain) {
3192           x2 = x3;
3193           y2 = y3;
3194           z2 = z3;
3195           
3196           x3 = 0;
3197           y3 = 0;
3198           z3 = 0;
3199         }
3200         x[0]=y1;
3201         x[1]=z1;
3202         x[4]=f1(x1,y1,x2,y2,x3,y3);
3203                 
3204         //      if (TMath::Abs(x[4]) >= cuts[0]) continue;  //
3205         x[2]=f2(x1,y1,x2,y2,x3,y3);
3206         
3207         //if (TMath::Abs(x[4]*x1-x[2]) >= cuts[1]) continue;
3208         //x[3]=f3(x1,y1,x2,y2,z1,z2);
3209         x[3]=f3n(x1,y1,x3,y3,z1,z3,x[4]);
3210         //if (TMath::Abs(x[3]) > cuts[3]) continue;
3211
3212         
3213         Double_t sy =0.1, sz =0.1;
3214         Double_t sy1=0.02, sz1=0.02;
3215         Double_t sy2=0.02, sz2=0.02;
3216         Double_t sy3=0.02;
3217
3218         if (constrain){
3219           sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
3220         }
3221         
3222         Double_t f40=(f1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
3223         Double_t f42=(f1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
3224         Double_t f43=(f1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
3225         Double_t f20=(f2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
3226         Double_t f22=(f2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
3227         Double_t f23=(f2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
3228
3229         Double_t f30=(f3(x1,y1+sy,x3,y3,z1,z3)-x[3])/sy;
3230         Double_t f31=(f3(x1,y1,x3,y3,z1+sz,z3)-x[3])/sz;
3231         Double_t f32=(f3(x1,y1,x3,y3+sy,z1,z3)-x[3])/sy;
3232         Double_t f34=(f3(x1,y1,x3,y3,z1,z3+sz)-x[3])/sz;
3233
3234         
3235         c[0]=sy1;
3236         c[1]=0.;       c[2]=sz1;
3237         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3238         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3239         c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3240         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3241         c[13]=f30*sy1*f40+f32*sy2*f42;
3242         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3243         
3244         //Int_t row1 = fSectors->GetRowNumber(x1);
3245         Int_t row1 = GetRowNumber(x1);
3246
3247         UInt_t index=0;
3248         //kr0.GetIndex(is);
3249         AliTPCseed *track=new (seed) AliTPCseed(index, x, c, x1, sec*alpha+shift);
3250         track->fIsSeeding = kTRUE;
3251         Int_t rc=FollowProlongation(*track, i2);        
3252         if (constrain) track->fBConstrain =1;
3253         else
3254           track->fBConstrain =0;
3255         track->fLastPoint = row1+fInnerSec->GetNRows();  // first cluster in track position
3256         track->fFirstPoint = track->fLastPoint;
3257
3258         if (rc==0 || track->GetNumberOfClusters()<(i1-i2)*0.5 || 
3259             track->GetNumberOfClusters() < track->fNFoundable*0.6 || 
3260             track->fNShared>0.4*track->GetNumberOfClusters()) {
3261           //delete track;
3262           seed->Reset();
3263           seed->~AliTPCseed();
3264         }
3265         else {
3266           arr->AddLast(track);
3267           seed = new AliTPCseed;
3268         }
3269         nin3++;
3270       }
3271     }  // if accepted seed
3272   }
3273   if (fDebug>1){
3274     printf("\nSeeding statiistic:\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin3);
3275   }
3276   delete seed;
3277 }
3278
3279
3280 AliTPCseed *AliTPCtrackerMI::MakeSeed(AliTPCseed *track, Float_t r0, Float_t r1, Float_t r2)
3281 {
3282   //
3283   //
3284   //reseed
3285   Int_t p0 = int(r0*track->GetNumberOfClusters());     // point 0 
3286   Int_t p1 = int(r1*track->GetNumberOfClusters());
3287   Int_t p2 = int(r2*track->GetNumberOfClusters());   // last point
3288   Int_t pp2=0;
3289   Double_t  x0[3],x1[3],x2[3];
3290   x0[0]=-1;
3291   x0[0]=-1;
3292   x0[0]=-1;
3293
3294   // find track position at given ratio of the length
3295   Int_t  sec0, sec1, sec2;
3296   sec0=0;
3297   sec1=0;
3298   sec2=0;
3299   Int_t index=-1;
3300   Int_t clindex;
3301   for (Int_t i=0;i<160;i++){
3302     if (track->fClusterPointer[i]){
3303       index++;
3304       AliTPCTrackerPoint   *trpoint =track->GetTrackPoint(i);
3305       if ( (index<p0) || x0[0]<0 ){
3306         if (trpoint->GetX()>1){
3307           clindex = track->GetClusterIndex2(i);
3308           if (clindex>0){       
3309             x0[0] = trpoint->GetX();
3310             x0[1] = trpoint->GetY();
3311             x0[2] = trpoint->GetZ();
3312             sec0  = ((clindex&0xff000000)>>24)%18;
3313           }
3314         }
3315       }
3316
3317       if ( (index<p1) &&(trpoint->GetX()>1)){
3318         clindex = track->GetClusterIndex2(i);
3319         if (clindex>0){
3320           x1[0] = trpoint->GetX();
3321           x1[1] = trpoint->GetY();
3322           x1[2] = trpoint->GetZ();
3323           sec1  = ((clindex&0xff000000)>>24)%18;
3324         }
3325       }
3326       if ( (index<p2) &&(trpoint->GetX()>1)){
3327         clindex = track->GetClusterIndex2(i);
3328         if (clindex>0){
3329           x2[0] = trpoint->GetX();
3330           x2[1] = trpoint->GetY();
3331           x2[2] = trpoint->GetZ(); 
3332           sec2  = ((clindex&0xff000000)>>24)%18;
3333           pp2 = i;
3334         }
3335       }
3336     }
3337   }
3338   
3339   Double_t alpha, cs,sn, xx2,yy2;
3340   //
3341   alpha = (sec1-sec2)*fSectors->GetAlpha();
3342   cs = TMath::Cos(alpha);
3343   sn = TMath::Sin(alpha); 
3344   xx2= x1[0]*cs-x1[1]*sn;
3345   yy2= x1[0]*sn+x1[1]*cs;
3346   x1[0] = xx2;
3347   x1[1] = yy2;
3348   //
3349   alpha = (sec0-sec2)*fSectors->GetAlpha();
3350   cs = TMath::Cos(alpha);
3351   sn = TMath::Sin(alpha); 
3352   xx2= x0[0]*cs-x0[1]*sn;
3353   yy2= x0[0]*sn+x0[1]*cs;
3354   x0[0] = xx2;
3355   x0[1] = yy2;
3356   //
3357   //
3358   //
3359   Double_t x[5],c[15];
3360   //
3361   x[0]=x2[1];
3362   x[1]=x2[2];
3363   x[4]=f1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
3364   //  if (x[4]>1) return 0;
3365   x[2]=f2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
3366   x[3]=f3n(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2],x[4]);
3367   //if (TMath::Abs(x[3]) > 2.2)  return 0;
3368   //if (TMath::Abs(x[2]) > 1.99) return 0;
3369   //  
3370   Double_t sy =0.1,  sz =0.1;
3371   //
3372   Double_t sy1=0.02+track->GetSigmaY2(), sz1=0.02+track->GetSigmaZ2();
3373   Double_t sy2=0.01+track->GetSigmaY2(), sz2=0.01+track->GetSigmaZ2();
3374   Double_t sy3=0.01+track->GetSigmaY2();
3375   //
3376   Double_t f40=(f1(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[4])/sy;
3377   Double_t f42=(f1(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[4])/sy;
3378   Double_t f43=(f1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[4])/sy;
3379   Double_t f20=(f2(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[2])/sy;
3380   Double_t f22=(f2(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[2])/sy;
3381   Double_t f23=(f2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[2])/sy;
3382   //
3383   Double_t f30=(f3(x2[0],x2[1]+sy,x0[0],x0[1],x2[2],x0[2])-x[3])/sy;
3384   Double_t f31=(f3(x2[0],x2[1],x0[0],x0[1],x2[2]+sz,x0[2])-x[3])/sz;
3385   Double_t f32=(f3(x2[0],x2[1],x0[0],x0[1]+sy,x2[2],x0[2])-x[3])/sy;
3386   Double_t f34=(f3(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2]+sz)-x[3])/sz;
3387   
3388   
3389   c[0]=sy1;
3390   c[1]=0.;       c[2]=sz1;
3391   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3392   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3393   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3394   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3395   c[13]=f30*sy1*f40+f32*sy2*f42;
3396   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3397   
3398   //  Int_t row1 = fSectors->GetRowNumber(x2[0]);
3399   AliTPCseed *seed=new  AliTPCseed(0, x, c, x2[0], sec2*fSectors->GetAlpha()+fSectors->GetAlphaShift());
3400   //  Double_t y0,z0,y1,z1, y2,z2;
3401   //seed->GetProlongation(x0[0],y0,z0);
3402   // seed->GetProlongation(x1[0],y1,z1);
3403   //seed->GetProlongation(x2[0],y2,z2);
3404   //  seed =0;
3405   seed->fLastPoint  = pp2;
3406   seed->fFirstPoint = pp2;
3407   
3408
3409   return seed;
3410 }
3411
3412 Int_t  AliTPCtrackerMI::CheckKinkPoint(AliTPCseed*seed, Float_t th)
3413 {
3414   //
3415   //
3416   // 
3417   for (Int_t i=0;i<12;i++) seed->fKinkPoint[i]=0;
3418   //
3419   if (TMath::Abs(seed->GetC())>0.01) return 0;
3420   //
3421
3422   Float_t x[160], y[160], erry[160], z[160], errz[160];
3423   Int_t sec[160];
3424   Float_t xt[160], yt[160], zt[160];
3425   Int_t i1 = 200;
3426   Int_t i2 = 0;
3427   Int_t secm   = -1;
3428   Int_t padm   = -1;
3429   Int_t middle = seed->GetNumberOfClusters()/2;
3430   //
3431   //
3432   // find central sector, get local cooordinates
3433   Int_t count = 0;
3434   for (Int_t i=seed->fFirstPoint;i<=seed->fLastPoint;i++) {
3435     sec[i]= seed->GetClusterSector(i)%18;
3436     x[i]  = GetXrow(i);  
3437     if (sec[i]>=0) {
3438       AliTPCclusterMI * cl = seed->fClusterPointer[i];
3439       //      if (cl==0)        cl = GetClusterMI(seed->GetClusterIndex2(i));
3440       if (cl==0) {
3441         sec[i] = -1;
3442         continue;
3443       }
3444       //
3445       //
3446       if (i>i2)  i2 = i;  //last  point with cluster
3447       if (i2<i1) i1 = i;  //first point with cluster
3448       y[i] = cl->GetY();
3449       z[i] = cl->GetZ();
3450       AliTPCTrackerPoint * point = seed->GetTrackPoint(i);
3451       xt[i] = x[i];
3452       yt[i] = point->GetY();
3453       zt[i] = point->GetZ();
3454   
3455       if (point->GetX()>0){
3456         erry[i] = point->GetErrY();
3457         errz[i] = point->GetErrZ();     
3458       }
3459
3460       count++;
3461       if (count<middle) {
3462         secm = sec[i];  //central sector
3463         padm = i;       //middle point with cluster
3464       }
3465     }
3466   }
3467   //
3468   // rotate position to global coordinate system connected to  sector at last the point
3469   //
3470   for (Int_t i=i1;i<=i2;i++){
3471     //    
3472     if (sec[i]<0) continue;
3473     Double_t alpha = (sec[i2]-sec[i])*fSectors->GetAlpha();
3474     Double_t cs = TMath::Cos(alpha);
3475     Double_t sn = TMath::Sin(alpha);    
3476     Float_t xx2= x[i]*cs+y[i]*sn;
3477     Float_t yy2= -x[i]*sn+y[i]*cs;
3478     x[i] = xx2;
3479     y[i] = yy2;    
3480     //
3481     xx2= xt[i]*cs+yt[i]*sn;
3482     yy2= -xt[i]*sn+yt[i]*cs;
3483     xt[i] = xx2;
3484     yt[i] = yy2;    
3485
3486   }
3487   //get "state" vector
3488   Double_t xh[5],xm = x[padm];  
3489   xh[0]=yt[i2];
3490   xh[1]=zt[i2];
3491   xh[4]=f1(xt[i2],yt[i2],xt[padm],yt[padm],xt[i1],yt[i1]);  
3492   xh[2]=f2(xt[i2],yt[i2],xt[padm],yt[padm],xt[i1],yt[i1]);
3493   xh[3]=f3n(xt[i2],yt[i2],xt[i1],yt[i1],zt[i2],zt[i1],xh[4]);
3494   //
3495   //
3496   for (Int_t i=i1;i<=i2;i++){
3497     Double_t yy,zz;
3498     if (sec[i]<0) continue;    
3499     GetProlongation(x[i2], x[i],xh,yy,zz);
3500     if (TMath::Abs(y[i]-yy)>4||TMath::Abs(z[i]-zz)>4){
3501       //Double_t xxh[5];
3502       //xxh[4]=f1old(x[i2],y[i2],x[padm],y[padm],x[i1],y[i1]);  
3503       //xxh[2]=f2old(x[i2],y[i2],x[padm],y[padm],x[i1],y[i1]);
3504       printf("problem\n");
3505     }
3506     y[i] = y[i] - yy;
3507     z[i] = z[i] - zz;
3508   }
3509   Float_t dyup[160],dydown[160], dzup[160], dzdown[160];
3510   Float_t yup[160], ydown[160],  zup[160],  zdown[160];
3511  
3512   AliTPCpolyTrack ptrack1,ptrack2;
3513   //
3514   // derivation up
3515   for (Int_t i=i1;i<=i2;i++){
3516     AliTPCclusterMI * cl = seed->fClusterPointer[i];
3517     if (!cl) continue;
3518     if (cl->GetType()<0) continue;
3519     if (cl->GetType()>10) continue;
3520
3521     if (sec[i]>=0){
3522       ptrack1.AddPoint(x[i]-xm,y[i],z[i],0.1,0.1);
3523     }
3524     if (ptrack1.GetN()>4.){
3525       ptrack1.UpdateParameters();
3526       Double_t ddy,ddz;
3527       ptrack1.GetFitDerivation(x[i]-xm,ddy,ddz);
3528       Double_t yy,zz;
3529       ptrack1.GetFitPoint(x[i]-xm,yy,zz);
3530
3531       dyup[i] = ddy;
3532       dzup[i] = ddz;
3533       yup[i]  = yy;
3534       zup[i]  = zz;
3535
3536     }
3537     else{
3538       dyup[i]=0.;  //not enough points
3539     }
3540   }
3541   //
3542   // derivation down
3543   for (Int_t i=i2;i>=i1;i--){
3544     AliTPCclusterMI * cl = seed->fClusterPointer[i];
3545     if (!cl) continue;
3546     if (cl->GetType()<0) continue;
3547     if (cl->GetType()>10) continue;
3548     if (sec[i]>=0){
3549       ptrack2.AddPoint(x[i]-xm,y[i],z[i],0.1,0.1);
3550     }
3551     if (ptrack2.GetN()>4){
3552       ptrack2.UpdateParameters();
3553       Double_t ddy,ddz;
3554       ptrack2.GetFitDerivation(x[i]-xm,ddy,ddz);
3555       Double_t yy,zz;
3556       ptrack2.GetFitPoint(x[i]-xm,yy,zz);
3557
3558       dydown[i] = ddy;
3559       dzdown[i] = ddz;
3560       ydown[i]  = yy;
3561       zdown[i]  = zz;
3562     }
3563     else{
3564       dydown[i]=0.;  //not enough points
3565     }
3566   }
3567   //
3568   //
3569   // find maximal difference of the derivation
3570   for (Int_t i=0;i<12;i++) seed->fKinkPoint[i]=0;
3571
3572
3573   for (Int_t i=i1+10;i<i2-10;i++){
3574     if ( (TMath::Abs(dydown[i])<0.00000001)  ||  (TMath::Abs(dyup[i])<0.00000001) ||i<30)continue;
3575     //    printf("%f\t%f\t%f\t%f\t%f\n",x[i],dydown[i],dyup[i],dzdown[i],dzup[i]);
3576     //
3577     Float_t ddy = TMath::Abs(dydown[i]-dyup[i]);
3578     Float_t ddz = TMath::Abs(dzdown[i]-dzup[i]);    
3579     if ( (ddy+ddz)> th){
3580       seed->fKinkPoint[0] = i;
3581       seed->fKinkPoint[1] = ddy;
3582       seed->fKinkPoint[2] = ddz;
3583       th = ddy+ddz;      
3584     }
3585   }
3586
3587   if (fTreeDebug){
3588     //
3589     //write information to the debug tree
3590     TBranch * br = fTreeDebug->GetBranch("debug");
3591     TClonesArray * arr = new TClonesArray("AliTPCTrackPoint2");
3592     arr->ExpandCreateFast(i2-i1);
3593     br->SetAddress(&arr);
3594     //
3595     AliTPCclusterMI cldummy;
3596     cldummy.SetQ(0);
3597     AliTPCTrackPoint2 pdummy;
3598     pdummy.GetTPoint().fIsShared = 10;
3599     //
3600     Double_t alpha = sec[i2]*fSectors->GetAlpha();
3601     Double_t cs    = TMath::Cos(alpha);
3602     Double_t sn    = TMath::Sin(alpha);    
3603
3604     for (Int_t i=i1;i<i2;i++){
3605       AliTPCTrackPoint2 *trpoint = (AliTPCTrackPoint2*)arr->UncheckedAt(i-i1);
3606       //cluster info
3607       AliTPCclusterMI * cl0 = seed->fClusterPointer[i];
3608       //      
3609       AliTPCTrackerPoint * point = seed->GetTrackPoint(i);
3610       
3611       if (cl0){
3612         Double_t x = GetXrow(i);
3613         trpoint->GetTPoint() = *point;
3614         trpoint->GetCPoint() = *cl0;
3615         trpoint->GetCPoint().SetQ(TMath::Abs(cl0->GetQ()));
3616         trpoint->fID    = seed->GetUniqueID();
3617         trpoint->fLab   = seed->GetLabel();
3618         //
3619         trpoint->fGX =  cs *x + sn*point->GetY();
3620         trpoint->fGY = -sn *x + cs*point->GetY() ;
3621         trpoint->fGZ = point->GetZ();
3622         //
3623         trpoint->fDY = y[i];
3624         trpoint->fDZ = z[i];
3625         //
3626         trpoint->fDYU = dyup[i];
3627         trpoint->fDZU = dzup[i];
3628         //
3629         trpoint->fDYD = dydown[i];
3630         trpoint->fDZD = dzdown[i];
3631         //
3632         if (TMath::Abs(dyup[i])>0.00000000001 &&TMath::Abs(dydown[i])>0.00000000001){
3633           trpoint->fDDY = dydown[i]-dyup[i];
3634           trpoint->fDDZ = dzdown[i]-dzup[i];
3635         }else{
3636           trpoint->fDDY = 0.;
3637           trpoint->fDDZ = 0.;
3638         }       
3639       }
3640       else{
3641         *trpoint = pdummy;
3642         trpoint->GetCPoint()= cldummy;
3643         trpoint->fID = -1;
3644       }
3645       //     
3646     }
3647     fTreeDebug->Fill();
3648   }
3649   
3650   
3651   return 0;
3652   
3653 }
3654
3655
3656
3657
3658
3659 AliTPCseed*  AliTPCtrackerMI::ReSeed(AliTPCseed *t)
3660 {
3661   //
3662   // reseed - refit -  track
3663   //
3664   Int_t first = 0;
3665   //  Int_t last  = fSectors->GetNRows()-1;
3666   //
3667   if (fSectors == fOuterSec){
3668     first = TMath::Max(first, t->fFirstPoint-fInnerSec->GetNRows());
3669     //last  = 
3670   }
3671   else
3672     first = t->fFirstPoint;
3673   //
3674   AliTPCseed * seed = MakeSeed(t,0.1,0.5,0.9);
3675   FollowBackProlongation(*t,fSectors->GetNRows()-1);
3676   t->Reset(kFALSE);
3677   FollowProlongation(*t,first);
3678   return seed;
3679 }
3680
3681
3682
3683
3684
3685
3686
3687 //_____________________________________________________________________________
3688 Int_t AliTPCtrackerMI::ReadSeeds(const TFile *inp) {
3689   //-----------------------------------------------------------------
3690   // This function reades track seeds.
3691   //-----------------------------------------------------------------
3692   TDirectory *savedir=gDirectory; 
3693
3694   TFile *in=(TFile*)inp;
3695   if (!in->IsOpen()) {
3696      cerr<<"AliTPCtrackerMI::ReadSeeds(): input file is not open !\n";
3697      return 1;
3698   }
3699
3700   in->cd();
3701   TTree *seedTree=(TTree*)in->Get("Seeds");
3702   if (!seedTree) {
3703      cerr<<"AliTPCtrackerMI::ReadSeeds(): ";
3704      cerr<<"can't get a tree with track seeds !\n";
3705      return 2;
3706   }
3707   AliTPCtrack *seed=new AliTPCtrack; 
3708   seedTree->SetBranchAddress("tracks",&seed);
3709   
3710   if (fSeeds==0) fSeeds=new TObjArray(15000);
3711
3712   Int_t n=(Int_t)seedTree->GetEntries();
3713   for (Int_t i=0; i<n; i++) {
3714      seedTree->GetEvent(i);
3715      fSeeds->AddLast(new AliTPCseed(*seed,seed->GetAlpha()));
3716   }
3717   
3718   delete seed;
3719   delete seedTree; 
3720   savedir->cd();
3721   return 0;
3722 }
3723
3724 //_____________________________________________________________________________
3725 Int_t AliTPCtrackerMI::Clusters2Tracks() {
3726   //-----------------------------------------------------------------
3727   // This is a track finder.
3728   //-----------------------------------------------------------------
3729   TDirectory *savedir=gDirectory; 
3730   TStopwatch timer;
3731   //
3732   if (!fInput) SetIO();  //set default IO using loaders
3733   if (!fInput){
3734      cerr<<"AliTPCtrackerMI::Clusters2Tracks(): input file is not open !\n";
3735      return 1;
3736   }
3737   LoadClusters();
3738   //
3739   fIteration = 0;
3740   fSeeds = Tracking();
3741
3742
3743   printf("Time for tracking: \t");timer.Print();timer.Start();
3744
3745   //activate again some tracks
3746   for (Int_t i=0; i<fSeeds->GetEntriesFast(); i++) {
3747     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
3748     if (!pt) continue;    
3749     Int_t nc=t.GetNumberOfClusters();
3750     if (nc<20) {
3751       delete fSeeds->RemoveAt(i);
3752       continue;
3753     }
3754     if (pt->fRemoval==10) {
3755       if (pt->GetDensityFirst(20)>0.8 || pt->GetDensityFirst(30)>0.8 || pt->GetDensityFirst(40)>0.7)
3756         pt->Desactivate(10);  // make track again active
3757       else{
3758         pt->Desactivate(20);    
3759         delete fSeeds->RemoveAt(i);
3760       }
3761     } 
3762   }
3763   RemoveDouble(fSeeds,0.2,0.6,11);
3764   //RemoveUsed(fSeeds,0.9,0.9,6);
3765   //RemoveUsed(fSeeds,0.8,0.8,6);
3766   //RemoveUsed(fSeeds,0.7,0.7,6);
3767   RemoveUsed(fSeeds,0.5,0.5,6);
3768
3769   //
3770   Int_t nseed=fSeeds->GetEntriesFast();
3771   Int_t found = 0;
3772   for (Int_t i=0; i<nseed; i++) {
3773     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
3774     if (!pt) continue;    
3775     Int_t nc=t.GetNumberOfClusters();
3776     if (nc<15) {
3777       delete fSeeds->RemoveAt(i);
3778       continue;
3779     }
3780     CookLabel(pt,0.1); //For comparison only
3781     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
3782     if ((pt->IsActive() || (pt->fRemoval==10) )){
3783       cerr<<found++<<'\r';      
3784     }
3785     else
3786       delete fSeeds->RemoveAt(i);
3787     pt->fLab2 = i;
3788   }
3789
3790   
3791   //RemoveOverlap(fSeeds,0.99,7,kTRUE);  
3792   SignShared(fSeeds);  
3793   //RemoveUsed(fSeeds,0.9,0.9,6);
3794   // 
3795   nseed=fSeeds->GetEntriesFast();
3796   found = 0;
3797   for (Int_t i=0; i<nseed; i++) {
3798     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
3799     if (!pt) continue;    
3800     Int_t nc=t.GetNumberOfClusters();
3801     if (nc<15) {
3802       delete fSeeds->RemoveAt(i);
3803       continue;
3804     }
3805     t.SetUniqueID(i);
3806     t.CookdEdx(0.02,0.6);
3807     //    CheckKinkPoint(&t,0.05);
3808     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
3809     if ((pt->IsActive() || (pt->fRemoval==10) )){
3810       cerr<<found++<<'\r';      
3811     }
3812     else
3813       delete fSeeds->RemoveAt(i);
3814     pt->fLab2 = i;
3815   }
3816
3817   SortTracks(fSeeds, 1);
3818   
3819   /*
3820   fIteration = 1;
3821   PrepareForBackProlongation(fSeeds,0.5);
3822   PropagateBack(fSeeds);
3823   printf("Time for back propagation: \t");timer.Print();timer.Start();
3824   
3825   fIteration = 2;
3826   
3827   PrepareForProlongation(fSeeds,1.);
3828   PropagateForward();
3829   
3830   fSectors = fOuterSec;
3831   ParallelTracking(fSeeds,fSectors->GetNRows()-1,0);
3832   fSectors = fInnerSec;
3833   ParallelTracking(fSeeds,fSectors->GetNRows()-1,0);
3834   printf("Time for FORWARD propagation: \t");timer.Print();timer.Start();
3835   // RemoveUsed(fSeeds,0.7,0.7,6);
3836   //RemoveOverlap(fSeeds,0.9,7,kTRUE);
3837  
3838   nseed=fSeeds->GetEntriesFast();
3839   found = 0;
3840   for (Int_t i=0; i<nseed; i++) {
3841     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
3842     if (!pt) continue;    
3843     Int_t nc=t.GetNumberOfClusters();
3844     if (nc<15) {
3845       delete fSeeds->RemoveAt(i);
3846       continue;
3847     }
3848     t.CookdEdx(0.02,0.6);
3849     //    CookLabel(pt,0.1); //For comparison only
3850     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
3851     if ((pt->IsActive() || (pt->fRemoval==10) )){
3852       cerr<<found++<<'\r';      
3853     }
3854     else
3855       delete fSeeds->RemoveAt(i);
3856     pt->fLab2 = i;
3857   }
3858   */
3859  
3860   //  fNTracks = found;
3861   printf("Time for overlap removal, track writing and dedx cooking: \t"); timer.Print();timer.Start();
3862   //
3863   if (fOutput) {
3864     WriteTracks();
3865   }
3866   if (!fNewIO)  fOutput->Write();
3867   else
3868     AliRunLoader::GetDetectorLoader("TPC",AliConfig::fgkDefaultEventFolderName)->WriteTracks("OVERWRITE");
3869
3870
3871   cerr<<"Number of found tracks : "<<"\t"<<found<<endl;  
3872   savedir->cd();
3873   //if (seedtree) delete seedtree;
3874   //  UnloadClusters();
3875   //printf("Time for unloading cluster: \t"); timer.Print();timer.Start();
3876   
3877   return 0;
3878 }
3879
3880 void AliTPCtrackerMI::Tracking(TObjArray * arr)
3881 {
3882   //
3883   // tracking of the seeds
3884   //
3885
3886   fSectors = fOuterSec;
3887   ParallelTracking(arr,150,63);
3888   fSectors = fOuterSec;
3889   ParallelTracking(arr,63,0);
3890 }
3891
3892 TObjArray * AliTPCtrackerMI::Tracking(Int_t seedtype, Int_t i1, Int_t i2, Float_t cuts[4], Float_t dy, Int_t dsec)
3893 {
3894   //
3895   //
3896   //tracking routine
3897   TObjArray * arr = new TObjArray;
3898   // 
3899   fSectors = fOuterSec;
3900   TStopwatch timer;
3901   timer.Start();
3902   for (Int_t sec=0;sec<fkNOS;sec++){
3903     if (seedtype==3) MakeSeeds3(arr,sec,i1,i2,cuts,dy, dsec);
3904     if (seedtype==4) MakeSeeds5(arr,sec,i1,i2,cuts,dy);    
3905     if (seedtype==2) MakeSeeds2(arr,sec,i1,i2,cuts,dy);
3906   }
3907   if (fDebug>0){
3908     printf("\nSeeding - %d\t%d\t%d\t%d\n",seedtype,i1,i2,arr->GetEntriesFast());
3909     timer.Print();
3910     timer.Start();
3911   }
3912   Tracking(arr);  
3913   if (fDebug>0){
3914     timer.Print();
3915   }
3916
3917   return arr;
3918 }
3919
3920 TObjArray * AliTPCtrackerMI::Tracking()
3921 {
3922   //
3923   //
3924   TStopwatch timer;
3925   timer.Start();
3926   Int_t nup=fOuterSec->GetNRows()+fInnerSec->GetNRows();
3927
3928   TObjArray * seeds = new TObjArray;
3929   TObjArray * arr=0;
3930   
3931   Int_t gap =20;
3932   Float_t cuts[4];
3933   cuts[0] = 0.002;
3934   cuts[1] = 1.5;
3935   cuts[2] = 3.;
3936   cuts[3] = 3.;
3937   Float_t fnumber  = 3.0;
3938   Float_t fdensity = 3.0;
3939   
3940   //  
3941   //find primaries  
3942   cuts[0]=0.0066;
3943   for (Int_t delta = 0; delta<18; delta+=6){
3944     //
3945     cuts[0]=0.0070;
3946     cuts[1] = 1.5;
3947     arr = Tracking(3,nup-1-delta,nup-1-delta-gap,cuts,-1,1);
3948     SumTracks(seeds,arr);   
3949     SignClusters(seeds,fnumber,fdensity); 
3950     //
3951     for (Int_t i=2;i<6;i+=2){
3952       // seed high pt tracks
3953       cuts[0]=0.0022;
3954       cuts[1]=0.3;
3955       arr = Tracking(3,nup-i-delta,nup-i-delta-gap,cuts,-1,0);
3956       SumTracks(seeds,arr);   
3957       SignClusters(seeds,fnumber,fdensity);        
3958     }
3959   }
3960   fnumber  = 4;
3961   fdensity = 4.;
3962   //  RemoveUsed(seeds,0.9,0.9,1);
3963   //  UnsignClusters();
3964   //  SignClusters(seeds,fnumber,fdensity);    
3965
3966   //find primaries  
3967   cuts[0]=0.0077;
3968   for (Int_t delta = 20; delta<120; delta+=10){
3969     //
3970     // seed high pt tracks
3971     cuts[0]=0.0060;
3972     cuts[1]=0.3;
3973     cuts[2]=6.;
3974     arr = Tracking(3,nup-delta,nup-delta-gap,cuts,-1);
3975     SumTracks(seeds,arr);   
3976     SignClusters(seeds,fnumber,fdensity);            
3977
3978     cuts[0]=0.003;
3979     cuts[1]=0.3;
3980     cuts[2]=6.;
3981     arr = Tracking(3,nup-delta-5,nup-delta-5-gap,cuts,-1);
3982     SumTracks(seeds,arr);   
3983     SignClusters(seeds,fnumber,fdensity);            
3984   }
3985
3986   cuts[0] = 0.01;
3987   cuts[1] = 2.0;
3988   cuts[2] = 3.;
3989   cuts[3] = 2.0;
3990   fnumber  = 2.;
3991   fdensity = 2.;
3992   
3993   if (fDebug>0){
3994     printf("\n\nPrimary seeding\t%d\n\n",seeds->GetEntriesFast());
3995     timer.Print();
3996     timer.Start();
3997   }
3998   //  RemoveUsed(seeds,0.75,0.75,1);
3999   //UnsignClusters();
4000   //SignClusters(seeds,fnumber,fdensity);
4001   
4002   // find secondaries
4003
4004   cuts[0] = 0.3;
4005   cuts[1] = 1.5;
4006   cuts[2] = 3.;
4007   cuts[3] = 1.5;
4008
4009   arr = Tracking(4,nup-1,nup-1-gap,cuts,-1);
4010   SumTracks(seeds,arr);   
4011   SignClusters(seeds,fnumber,fdensity);   
4012   //
4013   arr = Tracking(4,nup-2,nup-2-gap,cuts,-1);
4014   SumTracks(seeds,arr);   
4015   SignClusters(seeds,fnumber,fdensity);   
4016   //
4017   arr = Tracking(4,nup-3,nup-3-gap,cuts,-1);
4018   SumTracks(seeds,arr);   
4019   SignClusters(seeds,fnumber,fdensity);   
4020   //
4021
4022
4023   for (Int_t delta = 3; delta<30; delta+=5){
4024     //
4025     cuts[0] = 0.3;
4026     cuts[1] = 1.5;
4027     cuts[2] = 3.;
4028     cuts[3] = 1.5;
4029     arr = Tracking(4,nup-1-delta,nup-1-delta-gap,cuts,-1);
4030     SumTracks(seeds,arr);   
4031     SignClusters(seeds,fnumber,fdensity);   
4032     //
4033     arr = Tracking(4,nup-3-delta,nup-5-delta-gap,cuts,4);
4034     SumTracks(seeds,arr);   
4035     SignClusters(seeds,fnumber,fdensity); 
4036     //
4037   } 
4038   fnumber  = 1;
4039   fdensity = 1;
4040   //
4041   // change cuts
4042   fnumber  = 2.;
4043   fdensity = 2.;
4044   cuts[0]=0.0080;
4045
4046   // find secondaries
4047   for (Int_t delta = 30; delta<70; delta+=10){
4048     //
4049     cuts[0] = 0.3;
4050     cuts[1] = 1.5;
4051     cuts[2] = 3.;
4052     cuts[3] = 1.5;
4053     arr = Tracking(4,nup-1-delta,nup-1-delta-gap,cuts,-1);
4054     SumTracks(seeds,arr);   
4055     SignClusters(seeds,fnumber,fdensity);   
4056     //
4057     arr = Tracking(4,nup-5-delta,nup-5-delta-gap,cuts,5 );
4058     SumTracks(seeds,arr);   
4059     SignClusters(seeds,fnumber,fdensity);   
4060   }
4061  
4062   if (fDebug>0){
4063     printf("\n\nSecondary seeding\t%d\n\n",seeds->GetEntriesFast());
4064     timer.Print();
4065     timer.Start();
4066   }
4067
4068   return seeds;
4069   //
4070       
4071 }
4072
4073
4074 void AliTPCtrackerMI::SumTracks(TObjArray *arr1,TObjArray *arr2)
4075 {
4076   //
4077   //sum tracks to common container
4078   //remove suspicious tracks
4079   Int_t nseed = arr2->GetEntriesFast();
4080   for (Int_t i=0;i<nseed;i++){
4081     AliTPCseed *pt=(AliTPCseed*)arr2->UncheckedAt(i);    
4082     if (pt){
4083       
4084       // NORMAL ACTIVE TRACK
4085       if (pt->IsActive()){
4086         arr1->AddLast(arr2->RemoveAt(i));
4087         continue;
4088       }
4089       //remove not usable tracks
4090       if (pt->fRemoval!=10){
4091         delete arr2->RemoveAt(i);
4092         continue;
4093       }
4094       // REMOVE VERY SHORT  TRACKS
4095       if (pt->GetNumberOfClusters()<20){ 
4096         delete arr2->RemoveAt(i);
4097         continue;
4098       }
4099       // ENABLE ONLY ENOUGH GOOD STOPPED TRACKS
4100       if (pt->GetDensityFirst(20)>0.8 || pt->GetDensityFirst(30)>0.8 || pt->GetDensityFirst(40)>0.7)
4101         arr1->AddLast(arr2->RemoveAt(i));
4102       else{      
4103         delete arr2->RemoveAt(i);
4104       }
4105     }
4106   }
4107   delete arr2;  
4108 }
4109
4110
4111
4112 void  AliTPCtrackerMI::ParallelTracking(TObjArray * arr, Int_t rfirst, Int_t rlast)
4113 {
4114   //
4115   // try to track in parralel
4116
4117   Int_t nseed=arr->GetEntriesFast();
4118   //prepare seeds for tracking
4119   for (Int_t i=0; i<nseed; i++) {
4120     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i), &t=*pt; 
4121     if (!pt) continue;
4122     if (!t.IsActive()) continue;
4123     // follow prolongation to the first layer
4124     if ( (fSectors ==fInnerSec) || (t.fFirstPoint-fParam->GetNRowLow()>rfirst+1) )  
4125       FollowProlongation(t, rfirst+1);
4126   }
4127
4128
4129   //
4130   for (Int_t nr=rfirst; nr>=rlast; nr--){      
4131     // make indexes with the cluster tracks for given       
4132
4133     // find nearest cluster
4134     for (Int_t i=0; i<nseed; i++) {
4135       AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i), &t=*pt;       
4136       if (!pt) continue;
4137       if (!pt->IsActive()) continue;
4138       //      if ( (fSectors ==fOuterSec) && (pt->fFirstPoint-fParam->GetNRowLow())<nr) continue;
4139       if (pt->fRelativeSector>17) {
4140         continue;
4141       }
4142       UpdateClusters(t,nr);
4143     }
4144     // prolonagate to the nearest cluster - if founded
4145     for (Int_t i=0; i<nseed; i++) {
4146       AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i); 
4147       if (!pt) continue;
4148       if (!pt->IsActive()) continue; 
4149       // if ((fSectors ==fOuterSec) && (pt->fFirstPoint-fParam->GetNRowLow())<nr) continue;
4150       if (pt->fRelativeSector>17) {
4151         continue;
4152       }
4153       FollowToNextCluster(*pt,nr);
4154     }
4155   }    
4156 }
4157
4158 void AliTPCtrackerMI::PrepareForBackProlongation(TObjArray * arr,Float_t fac)
4159 {
4160   //
4161   //
4162   // if we use TPC track itself we have to "update" covariance
4163   //
4164   Int_t nseed= arr->GetEntriesFast();
4165   for (Int_t i=0;i<nseed;i++){
4166     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
4167     if (pt) {
4168       pt->Modify(fac);
4169       //
4170       //rotate to current local system at first accepted  point    
4171       Int_t index  = pt->GetClusterIndex2(pt->fFirstPoint); 
4172       Int_t sec    = (index&0xff000000)>>24;
4173       sec = sec%18;
4174       Float_t angle1 = fInnerSec->GetAlpha()*sec+fInnerSec->GetAlphaShift();
4175       if (angle1>TMath::Pi()) 
4176         angle1-=2.*TMath::Pi();
4177       Float_t angle2 = pt->GetAlpha();
4178       
4179       if (TMath::Abs(angle1-angle2)>0.001){
4180         pt->Rotate(angle1-angle2);
4181         //angle2 = pt->GetAlpha();
4182         //pt->fRelativeSector = pt->GetAlpha()/fInnerSec->GetAlpha();
4183         //if (pt->GetAlpha()<0) 
4184         //  pt->fRelativeSector+=18;
4185         //sec = pt->fRelativeSector;
4186       }
4187         
4188     }
4189     
4190   }
4191
4192
4193 }
4194 void AliTPCtrackerMI::PrepareForProlongation(TObjArray * arr, Float_t fac)
4195 {
4196   //
4197   //
4198   // if we use TPC track itself we have to "update" covariance
4199   //
4200   Int_t nseed= arr->GetEntriesFast();
4201   for (Int_t i=0;i<nseed;i++){
4202     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
4203     if (pt) {
4204       pt->Modify(fac);
4205       pt->fFirstPoint = pt->fLastPoint; 
4206     }
4207     
4208   }
4209
4210
4211 }
4212
4213 Int_t AliTPCtrackerMI::PropagateBack(TObjArray * arr)
4214 {
4215   //
4216   // make back propagation
4217   //
4218   Int_t nseed= arr->GetEntriesFast();
4219   for (Int_t i=0;i<nseed;i++){
4220     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
4221     if (pt) { 
4222       AliTPCseed *pt2 = new AliTPCseed(*pt);
4223       fSectors = fInnerSec;
4224       FollowBackProlongation(*pt,fSectors->GetNRows()-1);
4225       fSectors = fOuterSec;
4226       FollowBackProlongation(*pt,fSectors->GetNRows()-1);
4227       fSectors = fOuterSec;
4228       if (pt->GetNumberOfClusters()<35 && pt->GetLabel()>0 ){
4229         printf("\n%d",pt->GetLabel());
4230         fSectors = fInnerSec;
4231         FollowBackProlongation(*pt2,fSectors->GetNRows()-1);
4232         fSectors = fOuterSec;
4233         FollowBackProlongation(*pt2,fSectors->GetNRows()-1);
4234         fSectors = fOuterSec;
4235       }
4236     }      
4237   }
4238   return 0;
4239 }
4240
4241
4242 Int_t AliTPCtrackerMI::PropagateForward2(TObjArray * arr)
4243 {
4244   //
4245   // make forward propagation
4246   //
4247   Int_t nseed= arr->GetEntriesFast();
4248   for (Int_t i=0;i<nseed;i++){
4249     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
4250     if (pt) { 
4251       AliTPCseed *pt2 = new AliTPCseed(*pt);
4252       fSectors = fOuterSec;
4253       FollowProlongation(*pt,0);
4254       fSectors = fOuterSec;
4255       FollowProlongation(*pt,0);
4256       fSectors = fInnerSec;
4257       if (pt->GetNumberOfClusters()<35 && pt->GetLabel()>0 ){
4258         printf("\n%d",pt->GetLabel());
4259         fSectors = fOuterSec;
4260         FollowProlongation(*pt2,0);
4261         fSectors = fOuterSec;
4262         FollowProlongation(*pt2,0);
4263         fSectors = fOuterSec;
4264       }
4265     }      
4266   }
4267   return 0;
4268 }
4269
4270
4271 Int_t AliTPCtrackerMI::PropagateForward()
4272 {
4273   fSectors = fOuterSec;
4274   ParallelTracking(fSeeds,fSectors->GetNRows()-1,0);
4275   fSectors = fInnerSec;
4276   ParallelTracking(fSeeds,fSectors->GetNRows()-1,0);
4277   //WriteTracks();
4278   return 1;
4279 }
4280
4281
4282
4283
4284
4285
4286 Int_t AliTPCtrackerMI::PropagateBack(AliTPCseed * pt, Int_t row0, Int_t row1)
4287 {
4288   //
4289   // make back propagation, in between row0 and row1
4290   //
4291   
4292   if (pt) { 
4293     fSectors = fInnerSec;
4294     Int_t  r1;
4295     //
4296     if (row1<fSectors->GetNRows()) 
4297       r1 = row1;
4298     else 
4299       r1 = fSectors->GetNRows()-1;
4300
4301     if (row0<fSectors->GetNRows()&& r1>0 )
4302       FollowBackProlongation(*pt,r1);
4303     if (row1<=fSectors->GetNRows())
4304       return 0;
4305     //
4306     r1 = row1 - fSectors->GetNRows();
4307     if (r1<=0) return 0;
4308     if (r1>=fOuterSec->GetNRows()) return 0;
4309     fSectors = fOuterSec;
4310     return FollowBackProlongation(*pt,r1);
4311   }        
4312   return 0;
4313 }
4314
4315
4316
4317
4318 void  AliTPCtrackerMI::GetShape(AliTPCseed * seed, Int_t row)
4319 {
4320   //
4321   //
4322   Float_t sd2 = TMath::Abs((fParam->GetZLength()-TMath::Abs(seed->GetZ())))*fParam->GetDiffL()*fParam->GetDiffL();
4323   //  Float_t padlength =  fParam->GetPadPitchLength(seed->fSector);
4324   Float_t padlength =  GetPadPitchLength(row);
4325   //
4326   Float_t sresy = (seed->fSector < fParam->GetNSector()/2) ? 0.2 :0.3;
4327   Float_t angulary  = seed->GetSnp();
4328   angulary = angulary*angulary/(1-angulary*angulary);
4329   seed->fCurrentSigmaY2 = sd2+padlength*padlength*angulary/12.+sresy*sresy;  
4330   //
4331   Float_t sresz = fParam->GetZSigma();
4332   Float_t angularz  = seed->GetTgl();
4333   seed->fCurrentSigmaZ2 = sd2+padlength*padlength*angularz*angularz*(1+angulary)/12.+sresz*sresz;
4334   /*
4335   Float_t wy = GetSigmaY(seed);
4336   Float_t wz = GetSigmaZ(seed);
4337   wy*=wy;
4338   wz*=wz;
4339   if (TMath::Abs(wy/seed->fCurrentSigmaY2-1)>0.0001 || TMath::Abs(wz/seed->fCurrentSigmaZ2-1)>0.0001 ){
4340     printf("problem\n");
4341   }
4342   */
4343 }
4344
4345
4346 Float_t  AliTPCtrackerMI::GetSigmaY(AliTPCseed * seed)
4347 {
4348   //
4349   //  
4350   Float_t sd2 = TMath::Abs((fParam->GetZLength()-TMath::Abs(seed->GetZ())))*fParam->GetDiffL()*fParam->GetDiffL();
4351   Float_t padlength =  fParam->GetPadPitchLength(seed->fSector);
4352   Float_t sres = (seed->fSector < fParam->GetNSector()/2) ? 0.2 :0.3;
4353   Float_t angular  = seed->GetSnp();
4354   angular = angular*angular/(1-angular*angular);
4355   //  angular*=angular;
4356   //angular  = TMath::Sqrt(angular/(1-angular));
4357   Float_t res = TMath::Sqrt(sd2+padlength*padlength*angular/12.+sres*sres);
4358   return res;
4359 }
4360 Float_t  AliTPCtrackerMI::GetSigmaZ(AliTPCseed * seed)
4361 {
4362   //
4363   //
4364   Float_t sd2 = TMath::Abs((fParam->GetZLength()-TMath::Abs(seed->GetZ())))*fParam->GetDiffL()*fParam->GetDiffL();
4365   Float_t padlength =  fParam->GetPadPitchLength(seed->fSector);
4366   Float_t sres = fParam->GetZSigma();
4367   Float_t angular  = seed->GetTgl();
4368   Float_t res = TMath::Sqrt(sd2+padlength*padlength*angular*angular/12.+sres*sres);
4369   return res;
4370 }
4371
4372
4373
4374
4375 //__________________________________________________________________________
4376 void AliTPCtrackerMI::CookLabel(AliTPCseed *t, Float_t wrong) const {
4377   //--------------------------------------------------------------------
4378   //This function "cooks" a track label. If label<0, this track is fake.
4379   //--------------------------------------------------------------------
4380   Int_t noc=t->GetNumberOfClusters();
4381   if (noc<10){
4382     printf("\nnot founded prolongation\n\n\n");
4383     t->Dump();
4384     return ;
4385   }
4386   Int_t lb[160];
4387   Int_t mx[160];
4388   AliTPCclusterMI *clusters[160];
4389   //
4390   for (Int_t i=0;i<160;i++) {
4391     clusters[i]=0;
4392     lb[i]=mx[i]=0;
4393   }
4394
4395   Int_t i;
4396   Int_t current=0;
4397   for (i=0; i<160 && current<noc; i++) {
4398      
4399      Int_t index=t->GetClusterIndex2(i);
4400      if (index<=0) continue; 
4401      if (index&0x8000) continue;
4402      //     
4403      //clusters[current]=GetClusterMI(index);
4404      if (t->fClusterPointer[i]){
4405        clusters[current]=t->fClusterPointer[i];     
4406        current++;
4407      }
4408   }
4409   noc = current;
4410
4411   Int_t lab=123456789;
4412   for (i=0; i<noc; i++) {
4413     AliTPCclusterMI *c=clusters[i];
4414     if (!c) continue;
4415     lab=TMath::Abs(c->GetLabel(0));
4416     Int_t j;
4417     for (j=0; j<noc; j++) if (lb[j]==lab || mx[j]==0) break;
4418     lb[j]=lab;
4419     (mx[j])++;
4420   }
4421
4422   Int_t max=0;
4423   for (i=0; i<noc; i++) if (mx[i]>max) {max=mx[i]; lab=lb[i];}
4424     
4425   for (i=0; i<noc; i++) {
4426     AliTPCclusterMI *c=clusters[i]; 
4427     if (!c) continue;
4428     if (TMath::Abs(c->GetLabel(1)) == lab ||
4429         TMath::Abs(c->GetLabel(2)) == lab ) max++;
4430   }
4431
4432   if ((1.- Float_t(max)/noc) > wrong) lab=-lab;
4433
4434   else {
4435      Int_t tail=Int_t(0.10*noc);
4436      max=0;
4437      Int_t ind=0;
4438      for (i=1; i<=160&&ind<tail; i++) {
4439        //       AliTPCclusterMI *c=clusters[noc-i];
4440        AliTPCclusterMI *c=clusters[i];
4441        if (!c) continue;
4442        if (lab == TMath::Abs(c->GetLabel(0)) ||
4443            lab == TMath::Abs(c->GetLabel(1)) ||
4444            lab == TMath::Abs(c->GetLabel(2))) max++;
4445        ind++;
4446      }
4447      if (max < Int_t(0.5*tail)) lab=-lab;
4448   }
4449
4450   t->SetLabel(lab);
4451
4452   //  delete[] lb;
4453   //delete[] mx;
4454   //delete[] clusters;
4455 }
4456
4457 //_________________________________________________________________________
4458 void AliTPCtrackerMI::AliTPCSector::Setup(const AliTPCParam *par, Int_t f) {
4459   //-----------------------------------------------------------------------
4460   // Setup inner sector
4461   //-----------------------------------------------------------------------
4462   if (f==0) {
4463      fAlpha=par->GetInnerAngle();
4464      fAlphaShift=par->GetInnerAngleShift();
4465      fPadPitchWidth=par->GetInnerPadPitchWidth();
4466      fPadPitchLength=par->GetInnerPadPitchLength();
4467      fN=par->GetNRowLow();
4468      fRow=new AliTPCRow[fN];
4469      for (Int_t i=0; i<fN; i++) {
4470        fRow[i].SetX(par->GetPadRowRadiiLow(i));
4471        fRow[i].fDeadZone =1.5;  //1.5 cm of dead zone
4472      }
4473   } else {
4474      fAlpha=par->GetOuterAngle();
4475      fAlphaShift=par->GetOuterAngleShift();
4476      fPadPitchWidth  = par->GetOuterPadPitchWidth();
4477      fPadPitchLength = par->GetOuter1PadPitchLength();
4478      f1PadPitchLength = par->GetOuter1PadPitchLength();
4479      f2PadPitchLength = par->GetOuter2PadPitchLength();
4480
4481      fN=par->GetNRowUp();
4482      fRow=new AliTPCRow[fN];
4483      for (Int_t i=0; i<fN; i++) {
4484        fRow[i].SetX(par->GetPadRowRadiiUp(i)); 
4485        fRow[i].fDeadZone =1.5;  // 1.5 cm of dead zone
4486      }
4487   } 
4488 }
4489
4490
4491 AliTPCtrackerMI::AliTPCRow::~AliTPCRow(){
4492   //
4493 }
4494
4495
4496
4497 //_________________________________________________________________________
4498 void 
4499 AliTPCtrackerMI::AliTPCRow::InsertCluster(const AliTPCclusterMI* c, UInt_t index) {
4500   //-----------------------------------------------------------------------
4501   // Insert a cluster into this pad row in accordence with its y-coordinate
4502   //-----------------------------------------------------------------------
4503   if (fN==kMaxClusterPerRow) {
4504     cerr<<"AliTPCRow::InsertCluster(): Too many clusters !\n"; return;
4505   }
4506   if (fN==0) {fIndex[0]=index; fClusters[fN++]=c; return;}
4507   Int_t i=Find(c->GetZ());
4508   memmove(fClusters+i+1 ,fClusters+i,(fN-i)*sizeof(AliTPCclusterMI*));
4509   memmove(fIndex   +i+1 ,fIndex   +i,(fN-i)*sizeof(UInt_t));
4510   fIndex[i]=index; fClusters[i]=c; fN++;
4511 }
4512
4513
4514 //___________________________________________________________________
4515 Int_t AliTPCtrackerMI::AliTPCRow::Find(Double_t z) const {
4516   //-----------------------------------------------------------------------
4517   // Return the index of the nearest cluster 
4518   //-----------------------------------------------------------------------
4519   if (fN==0) return 0;
4520   if (z <= fClusters[0]->GetZ()) return 0;
4521   if (z > fClusters[fN-1]->GetZ()) return fN;
4522   Int_t b=0, e=fN-1, m=(b+e)/2;
4523   for (; b<e; m=(b+e)/2) {
4524     if (z > fClusters[m]->GetZ()) b=m+1;
4525     else e=m; 
4526   }
4527   return m;
4528 }
4529
4530
4531
4532 //___________________________________________________________________
4533 AliTPCclusterMI * AliTPCtrackerMI::AliTPCRow::FindNearest(Double_t y, Double_t z, Double_t roady, Double_t roadz) const {
4534   //-----------------------------------------------------------------------
4535   // Return the index of the nearest cluster in z y 
4536   //-----------------------------------------------------------------------
4537   Float_t maxdistance = roady*roady + roadz*roadz;
4538
4539   AliTPCclusterMI *cl =0;
4540   for (Int_t i=Find(z-roadz); i<fN; i++) {
4541       AliTPCclusterMI *c=(AliTPCclusterMI*)(fClusters[i]);
4542       if (c->GetZ() > z+roadz) break;
4543       if ( (c->GetY()-y) >  roady ) continue;
4544       Float_t distance = (c->GetZ()-z)*(c->GetZ()-z)+(c->GetY()-y)*(c->GetY()-y);
4545       if (maxdistance>distance) {
4546         maxdistance = distance;
4547         cl=c;       
4548       }
4549   }
4550   return cl;      
4551 }
4552
4553 AliTPCclusterMI * AliTPCtrackerMI::AliTPCRow::FindNearest2(Double_t y, Double_t z, Double_t roady, Double_t roadz,UInt_t & index) const 
4554 {
4555   //-----------------------------------------------------------------------
4556   // Return the index of the nearest cluster in z y 
4557   //-----------------------------------------------------------------------
4558   Float_t maxdistance = roady*roady + roadz*roadz;
4559   Int_t iz1 = TMath::Max(fFastCluster[Int_t(z-roadz+254.5)]-1,0);
4560   Int_t iz2 = TMath::Min(fFastCluster[Int_t(z+roadz+255.5)]+1,fN);
4561
4562   AliTPCclusterMI *cl =0;
4563   //FindNearest3(y,z,roady,roadz,index);
4564   //  for (Int_t i=Find(z-roadz); i<fN; i++) {
4565   for (Int_t i=iz1; i<iz2; i++) {
4566       AliTPCclusterMI *c=(AliTPCclusterMI*)(fClusters[i]);
4567       if (c->GetZ() > z+roadz) break;
4568       if ( c->GetY()-y >  roady ) continue;
4569       if ( y-c->GetY() >  roady ) continue;
4570       Float_t distance = (c->GetZ()-z)*(c->GetZ()-z)+(c->GetY()-y)*(c->GetY()-y);
4571       if (maxdistance>distance) {
4572         maxdistance = distance;
4573         cl=c;       
4574         index =i;
4575         //roady = TMath::Sqrt(maxdistance);
4576       }
4577   }
4578   return cl;      
4579 }
4580
4581
4582
4583 AliTPCclusterMI * AliTPCtrackerMI::AliTPCRow::FindNearest3(Double_t y, Double_t z, Double_t roady, Double_t roadz,UInt_t & index) const 
4584 {
4585   //-----------------------------------------------------------------------
4586   // Return the index of the nearest cluster in z y 
4587   //-----------------------------------------------------------------------
4588   Float_t maxdistance = roady*roady + roadz*roadz;
4589   //  Int_t iz = Int_t(z+255.);
4590   AliTPCclusterMI *cl =0;
4591   for (Int_t i=Find(z-roadz); i<fN; i++) {
4592     //for (Int_t i=fFastCluster[iz-2]; i<fFastCluster[iz+2]; i++) {
4593       AliTPCclusterMI *c=(AliTPCclusterMI*)(fClusters[i]);
4594       if (c->GetZ() > z+roadz) break;
4595       if ( c->GetY()-y >  roady ) continue;
4596       if ( y-c->GetY() >  roady ) continue;
4597       Float_t distance = (c->GetZ()-z)*(c->GetZ()-z)+(c->GetY()-y)*(c->GetY()-y);
4598       if (maxdistance>distance) {
4599         maxdistance = distance;
4600         cl=c;       
4601         index =i;
4602         //roady = TMath::Sqrt(maxdistance);
4603       }
4604   }
4605   return cl;      
4606 }
4607
4608
4609
4610
4611 AliTPCseed::AliTPCseed():AliTPCtrack(){
4612   //
4613   fRow=0; 
4614   fRemoval =0; 
4615   for (Int_t i=0;i<200;i++) SetClusterIndex2(i,-3);
4616   for (Int_t i=0;i<160;i++) fClusterPointer[i]=0;
4617
4618   fPoints = 0;
4619   fEPoints = 0;
4620   fNFoundable =0;
4621   fNShared  =0;
4622   //  fTrackPoints =0;
4623   fRemoval = 0;
4624   fSort =0;
4625   fFirstPoint =0;
4626   fNoCluster =0;
4627   fBSigned = kFALSE;
4628   fSeed1 =-1;
4629   fSeed2 =-1;
4630 }
4631
4632 AliTPCseed::AliTPCseed(const AliTPCtrack &t):AliTPCtrack(t){
4633   fPoints = 0;
4634   fEPoints = 0;
4635   fNShared  =0; 
4636   //  fTrackPoints =0;
4637   fRemoval =0;
4638   fSort =0;
4639   for (Int_t i=0;i<160;i++) {
4640     fClusterPointer[i] = 0;
4641     Int_t index = t.GetClusterIndex(i);
4642     if (index>0) {
4643       SetClusterIndex2(i,index);
4644     }
4645     else{
4646       SetClusterIndex2(i,-3); 
4647     }    
4648   }
4649   fFirstPoint =0;
4650   fNoCluster =0;
4651   fBSigned = kFALSE;
4652   fSeed1 =-1;
4653   fSeed2 =-1;
4654 }
4655
4656 AliTPCseed::AliTPCseed(const AliKalmanTrack &t, Double_t a):AliTPCtrack(t,a){
4657   fRow=0;
4658   for (Int_t i=0;i<160;i++) {
4659     fClusterPointer[i] = 0;
4660     Int_t index = t.GetClusterIndex(i);
4661     SetClusterIndex2(i,index);
4662   }
4663   
4664   fPoints = 0;
4665   fEPoints = 0;
4666   fNFoundable =0; 
4667   fNShared  =0; 
4668   //  fTrackPoints =0;
4669   fRemoval =0;
4670   fSort = 0;
4671   fFirstPoint =0;
4672   fNoCluster =0;
4673   fBSigned = kFALSE;
4674   fSeed1 =-1;
4675   fSeed2 =-1;
4676 }
4677
4678 AliTPCseed::AliTPCseed(UInt_t index, const Double_t xx[5], const Double_t cc[15], 
4679                                         Double_t xr, Double_t alpha):      
4680   AliTPCtrack(index, xx, cc, xr, alpha) {
4681   //
4682   //
4683   fRow =0;
4684   for (Int_t i=0;i<200;i++) SetClusterIndex2(i,-3);
4685   for (Int_t i=0;i<160;i++) fClusterPointer[i]=0;
4686   fPoints = 0;
4687   fEPoints = 0;
4688   fNFoundable =0;
4689   fNShared  = 0;
4690   //  fTrackPoints =0;
4691   fRemoval =0;
4692   fSort =0;
4693   fFirstPoint =0;
4694   //  fHelixIn = new TClonesArray("AliHelix",0);
4695   //fHelixOut = new TClonesArray("AliHelix",0);
4696   fNoCluster =0;
4697   fBSigned = kFALSE;
4698   fSeed1 =-1;
4699   fSeed2 =-1;
4700 }
4701
4702 AliTPCseed::~AliTPCseed(){
4703   if (fPoints) delete fPoints;
4704   fPoints =0;
4705   if (fEPoints) delete fEPoints;
4706   fEPoints = 0;
4707   fNoCluster =0;
4708 }
4709
4710 AliTPCTrackerPoint * AliTPCseed::GetTrackPoint(Int_t i)
4711 {
4712   //
4713   // 
4714   return &fTrackPoints[i];
4715 }
4716
4717 void AliTPCseed::RebuildSeed()
4718 {
4719   //
4720   // rebuild seed to be ready for storing
4721   AliTPCclusterMI cldummy;
4722   cldummy.SetQ(0);
4723   AliTPCTrackPoint pdummy;
4724   pdummy.GetTPoint().fIsShared = 10;
4725   for (Int_t i=0;i<160;i++){
4726     AliTPCclusterMI * cl0 = fClusterPointer[i];
4727     AliTPCTrackPoint *trpoint = (AliTPCTrackPoint*)fPoints->UncheckedAt(i);     
4728     if (cl0){
4729       trpoint->GetTPoint() = *(GetTrackPoint(i));
4730       trpoint->GetCPoint() = *cl0;
4731       trpoint->GetCPoint().SetQ(TMath::Abs(cl0->GetQ()));
4732     }
4733     else{
4734       *trpoint = pdummy;
4735       trpoint->GetCPoint()= cldummy;
4736     }
4737     
4738   }
4739
4740 }
4741
4742
4743 Double_t AliTPCseed::GetDensityFirst(Int_t n)
4744 {
4745   //
4746   //
4747   // return cluster for n rows bellow first point
4748   Int_t nfoundable = 1;
4749   Int_t nfound      = 1;
4750   for (Int_t i=fLastPoint-1;i>0&&nfoundable<n; i--){
4751     Int_t index = GetClusterIndex2(i);
4752     if (index!=-1) nfoundable++;
4753     if (index>0) nfound++;
4754   }
4755   if (nfoundable<n) return 0;
4756   return Double_t(nfound)/Double_t(nfoundable);
4757
4758 }
4759
4760
4761 void AliTPCseed::GetClusterStatistic(Int_t first, Int_t last, Int_t &found, Int_t &foundable, Int_t &shared, Bool_t plus2)
4762 {
4763   // get cluster stat.  on given region
4764   //
4765   found       = 0;
4766   foundable   = 0;
4767   shared      =0;
4768   for (Int_t i=first;i<last; i++){
4769     Int_t index = GetClusterIndex2(i);
4770     if (index!=-1) foundable++;
4771     if (fClusterPointer[i]) {
4772       found++;
4773     }
4774     else 
4775       continue;
4776
4777     if (fClusterPointer[i]->IsUsed(10)) {
4778       shared++;
4779       continue;
4780     }
4781     if (!plus2) continue; //take also neighborhoud
4782     //
4783     if ( (i>0) && fClusterPointer[i-1]){
4784       if (fClusterPointer[i-1]->IsUsed(10)) {
4785         shared++;
4786         continue;
4787       }
4788     }
4789     if ( fClusterPointer[i+1]){
4790       if (fClusterPointer[i+1]->IsUsed(10)) {
4791         shared++;
4792         continue;
4793       }
4794     }
4795     
4796   }
4797   if (shared>found){
4798     printf("problem\n");
4799   }
4800 }
4801
4802 //_____________________________________________________________________________
4803 void AliTPCseed::CookdEdx(Double_t low, Double_t up,Int_t i1, Int_t i2, Bool_t onlyused) {
4804   //-----------------------------------------------------------------
4805   // This funtion calculates dE/dX within the "low" and "up" cuts.
4806   //-----------------------------------------------------------------
4807
4808   Float_t amp[200];
4809   Float_t angular[200];
4810   Float_t weight[200];
4811   Int_t index[200];
4812   //Int_t nc = 0;
4813   //  TClonesArray & arr = *fPoints; 
4814   Float_t meanlog = 100.;
4815   
4816   Float_t mean[4]  = {0,0,0,0};
4817   Float_t sigma[4] = {1000,1000,1000,1000};
4818   Int_t nc[4]      = {0,0,0,0};
4819   Float_t norm[4]    = {1000,1000,1000,1000};
4820   //
4821   //
4822   fNShared =0;
4823
4824   for (Int_t of =0; of<4; of++){    
4825     for (Int_t i=of+i1;i<i2;i+=4)
4826       {
4827         Int_t index = fIndex[i];
4828         if (index<0||index&0x8000) continue;
4829
4830         //AliTPCTrackPoint * point = (AliTPCTrackPoint *) arr.At(i);
4831         AliTPCTrackerPoint * point = GetTrackPoint(i);
4832         //AliTPCTrackerPoint * pointm = GetTrackPoint(i-1);
4833         //AliTPCTrackerPoint * pointp = 0;
4834         //if (i<159) pointp = GetTrackPoint(i+1);
4835
4836         if (point==0) continue;
4837         AliTPCclusterMI * cl = fClusterPointer[i];
4838         if (cl==0) continue;    
4839         if (onlyused && (!cl->IsUsed(10))) continue;
4840         if (cl->IsUsed(11)) {
4841           fNShared++;
4842           continue;
4843         }
4844         Int_t   type   = cl->GetType();
4845         //if (point->fIsShared){
4846         //  fNShared++;
4847         //  continue;
4848         //}
4849         //if (pointm) 
4850         //  if (pointm->fIsShared) continue;
4851         //if (pointp) 
4852         //  if (pointp->fIsShared) continue;
4853
4854         if (type<0) continue;
4855         //if (type>10) continue;       
4856         //if (point->GetErrY()==0) continue;
4857         //if (point->GetErrZ()==0) continue;
4858
4859         //Float_t ddy = (point->GetY()-cl->GetY())/point->GetErrY();
4860         //Float_t ddz = (point->GetZ()-cl->GetZ())/point->GetErrZ();
4861         //if ((ddy*ddy+ddz*ddz)>10) continue; 
4862
4863
4864         //      if (point->GetCPoint().GetMax()<5) continue;
4865         if (cl->GetMax()<5) continue;
4866         Float_t angley = point->GetAngleY();
4867         Float_t anglez = point->GetAngleZ();
4868
4869         Float_t rsigmay2 =  point->GetSigmaY();
4870         Float_t rsigmaz2 =  point->GetSigmaZ();
4871         /*
4872         Float_t ns = 1.;
4873         if (pointm){
4874           rsigmay +=  pointm->GetTPoint().GetSigmaY();
4875           rsigmaz +=  pointm->GetTPoint().GetSigmaZ();
4876           ns+=1.;
4877         }
4878         if (pointp){
4879           rsigmay +=  pointp->GetTPoint().GetSigmaY();
4880           rsigmaz +=  pointp->GetTPoint().GetSigmaZ();
4881           ns+=1.;
4882         }
4883         rsigmay/=ns;
4884         rsigmaz/=ns;
4885         */
4886
4887         Float_t rsigma = TMath::Sqrt(rsigmay2*rsigmaz2);
4888
4889         Float_t ampc   = 0;     // normalization to the number of electrons
4890         if (i>64){
4891           //      ampc = 1.*point->GetCPoint().GetMax();
4892           ampc = 1.*cl->GetMax();
4893           //ampc = 1.*point->GetCPoint().GetQ();          
4894           //      AliTPCClusterPoint & p = point->GetCPoint();
4895           //      Float_t dy = TMath::Abs(Int_t( TMath::Abs(p.GetY()/0.6)) - TMath::Abs(p.GetY()/0.6)+0.5);
4896           // Float_t iz =  (250.0-TMath::Abs(p.GetZ())+0.11)/0.566;
4897           //Float_t dz = 
4898           //  TMath::Abs( Int_t(iz) - iz + 0.5);
4899           //ampc *= 1.15*(1-0.3*dy);
4900           //ampc *= 1.15*(1-0.3*dz);
4901           //      Float_t zfactor = (1.05-0.0004*TMath::Abs(point->GetCPoint().GetZ()));
4902           //ampc               *=zfactor; 
4903         }
4904         else{ 
4905           //ampc = 1.0*point->GetCPoint().GetMax(); 
4906           ampc = 1.0*cl->GetMax(); 
4907           //ampc = 1.0*point->GetCPoint().GetQ(); 
4908           //AliTPCClusterPoint & p = point->GetCPoint();
4909           // Float_t dy = TMath::Abs(Int_t( TMath::Abs(p.GetY()/0.4)) - TMath::Abs(p.GetY()/0.4)+0.5);
4910           //Float_t iz =  (250.0-TMath::Abs(p.GetZ())+0.11)/0.566;
4911           //Float_t dz = 
4912           //  TMath::Abs( Int_t(iz) - iz + 0.5);
4913
4914           //ampc *= 1.15*(1-0.3*dy);
4915           //ampc *= 1.15*(1-0.3*dz);
4916           //    Float_t zfactor = (1.02-0.000*TMath::Abs(point->GetCPoint().GetZ()));
4917           //ampc               *=zfactor; 
4918
4919         }
4920         ampc *= 2.0;     // put mean value to channel 50
4921         //ampc *= 0.58;     // put mean value to channel 50
4922         Float_t w      =  1.;
4923         //      if (type>0)  w =  1./(type/2.-0.5); 
4924         //      Float_t z = TMath::Abs(cl->GetZ());
4925         if (i<64) {
4926           ampc /= 0.6;
4927           //ampc /= (1+0.0008*z);
4928         } else
4929           if (i>128){
4930             ampc /=1.5;
4931             //ampc /= (1+0.0008*z);
4932           }else{
4933             //ampc /= (1+0.0008*z);
4934           }
4935         
4936         if (type<0) {  //amp at the border - lower weight
4937           // w*= 2.;
4938           
4939           continue;
4940         }
4941         if (rsigma>1.5) ampc/=1.3;  // if big backround
4942         amp[nc[of]]        = ampc;
4943         angular[nc[of]]    = TMath::Sqrt(1.+angley*angley+anglez*anglez);
4944         weight[nc[of]]     = w;
4945         nc[of]++;
4946       }
4947     
4948     TMath::Sort(nc[of],amp,index,kFALSE);
4949     Float_t sumamp=0;
4950     Float_t sumamp2=0;
4951     Float_t sumw=0;
4952     //meanlog = amp[index[Int_t(nc[of]*0.33)]];
4953     meanlog = 50;
4954     for (Int_t i=int(nc[of]*low+0.5);i<int(nc[of]*up+0.5);i++){
4955       Float_t ampl      = amp[index[i]]/angular[index[i]];
4956       ampl              = meanlog*TMath::Log(1.+ampl/meanlog);
4957       //
4958       sumw    += weight[index[i]]; 
4959       sumamp  += weight[index[i]]*ampl;
4960       sumamp2 += weight[index[i]]*ampl*ampl;
4961       norm[of]    += angular[index[i]]*weight[index[i]];
4962     }
4963     if (sumw<1){ 
4964       SetdEdx(0);  
4965     }
4966     else {
4967       norm[of] /= sumw;
4968       mean[of]  = sumamp/sumw;
4969       sigma[of] = sumamp2/sumw-mean[of]*mean[of];
4970       if (sigma[of]>0.1) 
4971         sigma[of] = TMath::Sqrt(sigma[of]);
4972       else
4973         sigma[of] = 1000;
4974       
4975     mean[of] = (TMath::Exp(mean[of]/meanlog)-1)*meanlog;
4976     //mean  *=(1-0.02*(sigma/(mean*0.17)-1.));
4977     //mean *=(1-0.1*(norm-1.));
4978     }
4979   }
4980
4981   Float_t dedx =0;
4982   fSdEdx =0;
4983   fMAngular =0;
4984   //  mean[0]*= (1-0.05*(sigma[0]/(0.01+mean[1]*0.18)-1));
4985   //  mean[1]*= (1-0.05*(sigma[1]/(0.01+mean[0]*0.18)-1));
4986
4987   
4988   //  dedx = (mean[0]* TMath::Sqrt((1.+nc[0]))+ mean[1]* TMath::Sqrt((1.+nc[1])) )/ 
4989   //  (  TMath::Sqrt((1.+nc[0]))+TMath::Sqrt((1.+nc[1])));
4990
4991   Int_t norm2 = 0;
4992   Int_t norm3 = 0;
4993   for (Int_t i =0;i<4;i++){
4994     if (nc[i]>2&&nc[i]<1000){
4995       dedx      += mean[i] *nc[i];
4996       fSdEdx    += sigma[i]*(nc[i]-2);
4997       fMAngular += norm[i] *nc[i];    
4998       norm2     += nc[i];
4999       norm3     += nc[i]-2;
5000     }
5001     fDEDX[i]  = mean[i];             
5002     fSDEDX[i] = sigma[i];            
5003     fNCDEDX[i]= nc[i]; 
5004   }
5005
5006   if (norm3>0){
5007     dedx   /=norm2;
5008     fSdEdx /=norm3;
5009     fMAngular/=norm2;
5010   }
5011   else{
5012     SetdEdx(0);
5013     return;
5014   }
5015   //  Float_t dedx1 =dedx;
5016   /*
5017   dedx =0;
5018   for (Int_t i =0;i<4;i++){
5019     if (nc[i]>2&&nc[i]<1000){
5020       mean[i]   = mean[i]*(1-0.12*(sigma[i]/(fSdEdx)-1.));
5021       dedx      += mean[i] *nc[i];
5022     }
5023     fDEDX[i]  = mean[i];                
5024   }
5025   dedx /= norm2;
5026   */
5027
5028   
5029   SetdEdx(dedx);
5030     
5031   //mi deDX
5032
5033
5034
5035   //Very rough PID
5036   Double_t p=TMath::Sqrt((1.+ GetTgl()*GetTgl())/(Get1Pt()*Get1Pt()));
5037
5038   if (p<0.6) {
5039     if (dedx < 39.+ 12./(p+0.25)/(p+0.25)) { SetMass(0.13957); return;}
5040     if (dedx < 39.+ 12./p/p) { SetMass(0.49368); return;}
5041     SetMass(0.93827); return;
5042   }
5043
5044   if (p<1.2) {
5045     if (dedx < 39.+ 12./(p+0.25)/(p+0.25)) { SetMass(0.13957); return;}
5046     SetMass(0.93827); return;
5047   }
5048
5049   SetMass(0.13957); return;
5050
5051 }
5052
5053
5054
5055 /*
5056
5057
5058
5059 void AliTPCseed::CookdEdx2(Double_t low, Double_t up) {
5060   //-----------------------------------------------------------------
5061   // This funtion calculates dE/dX within the "low" and "up" cuts.
5062   //-----------------------------------------------------------------
5063
5064   Float_t amp[200];
5065   Float_t angular[200];
5066   Float_t weight[200];
5067   Int_t index[200];
5068   Bool_t inlimit[200];
5069   for (Int_t i=0;i<200;i++) inlimit[i]=kFALSE;
5070   for (Int_t i=0;i<200;i++) amp[i]=10000;
5071   for (Int_t i=0;i<200;i++) angular[i]= 1;;
5072   
5073
5074   //
5075   Float_t meanlog = 100.;
5076   Int_t indexde[4]={0,64,128,160};
5077
5078   Float_t amean     =0;
5079   Float_t asigma    =0;
5080   Float_t anc       =0;
5081   Float_t anorm     =0;
5082
5083   Float_t mean[4]  = {0,0,0,0};
5084   Float_t sigma[4] = {1000,1000,1000,1000};
5085   Int_t nc[4]      = {0,0,0,0};
5086   Float_t norm[4]    = {1000,1000,1000,1000};
5087   //
5088   //
5089   fNShared =0;
5090
5091   //  for (Int_t of =0; of<3; of++){    
5092   //  for (Int_t i=indexde[of];i<indexde[of+1];i++)
5093   for (Int_t i =0; i<160;i++)
5094     {
5095         AliTPCTrackPoint * point = GetTrackPoint(i);
5096         if (point==0) continue;
5097         if (point->fIsShared){
5098           fNShared++;     
5099           continue;
5100         }
5101         Int_t   type   = point->GetCPoint().GetType();
5102         if (type<0) continue;
5103         if (point->GetCPoint().GetMax()<5) continue;
5104         Float_t angley = point->GetTPoint().GetAngleY();
5105         Float_t anglez = point->GetTPoint().GetAngleZ();
5106         Float_t rsigmay =  point->GetCPoint().GetSigmaY();
5107         Float_t rsigmaz =  point->GetCPoint().GetSigmaZ();
5108         Float_t rsigma = TMath::Sqrt(rsigmay*rsigmaz);
5109
5110         Float_t ampc   = 0;     // normalization to the number of electrons
5111         if (i>64){
5112           ampc =  point->GetCPoint().GetMax();
5113         }
5114         else{ 
5115           ampc = point->GetCPoint().GetMax(); 
5116         }
5117         ampc *= 2.0;     // put mean value to channel 50
5118         //      ampc *= 0.565;     // put mean value to channel 50
5119
5120         Float_t w      =  1.;
5121         Float_t z = TMath::Abs(point->GetCPoint().GetZ());
5122         if (i<64) {
5123           ampc /= 0.63;
5124         } else
5125           if (i>128){
5126             ampc /=1.51;
5127           }             
5128         if (type<0) {  //amp at the border - lower weight                 
5129           continue;
5130         }
5131         if (rsigma>1.5) ampc/=1.3;  // if big backround
5132         angular[i]    = TMath::Sqrt(1.+angley*angley+anglez*anglez);
5133         amp[i]        = ampc/angular[i];
5134         weight[i]     = w;
5135         anc++;
5136     }
5137
5138   TMath::Sort(159,amp,index,kFALSE);
5139   for (Int_t i=int(anc*low+0.5);i<int(anc*up+0.5);i++){      
5140     inlimit[index[i]] = kTRUE;  // take all clusters
5141   }
5142   
5143   //  meanlog = amp[index[Int_t(anc*0.3)]];
5144   meanlog =10000.;
5145   for (Int_t of =0; of<3; of++){    
5146     Float_t sumamp=0;
5147     Float_t sumamp2=0;
5148     Float_t sumw=0;    
5149    for (Int_t i=indexde[of];i<indexde[of+1];i++)
5150       {
5151         if (inlimit[i]==kFALSE) continue;
5152         Float_t ampl      = amp[i];
5153         ///angular[i];
5154         ampl              = meanlog*TMath::Log(1.+ampl/meanlog);
5155         //
5156         sumw    += weight[i]; 
5157         sumamp  += weight[i]*ampl;
5158         sumamp2 += weight[i]*ampl*ampl;
5159         norm[of]    += angular[i]*weight[i];
5160         nc[of]++;
5161       }
5162    if (sumw<1){ 
5163      SetdEdx(0);  
5164    }
5165    else {
5166      norm[of] /= sumw;
5167      mean[of]  = sumamp/sumw;
5168      sigma[of] = sumamp2/sumw-mean[of]*mean[of];
5169      if (sigma[of]>0.1) 
5170        sigma[of] = TMath::Sqrt(sigma[of]);
5171      else
5172        sigma[of] = 1000;      
5173      mean[of] = (TMath::Exp(mean[of]/meanlog)-1)*meanlog;
5174    }
5175   }
5176     
5177   Float_t dedx =0;
5178   fSdEdx =0;
5179   fMAngular =0;
5180   //
5181   Int_t norm2 = 0;
5182   Int_t norm3 = 0;
5183   Float_t www[3] = {12.,14.,17.};
5184   //Float_t www[3] = {1.,1.,1.};
5185
5186   for (Int_t i =0;i<3;i++){
5187     if (nc[i]>2&&nc[i]<1000){
5188       dedx      += mean[i] *nc[i]*www[i]/sigma[i];
5189       fSdEdx    += sigma[i]*(nc[i]-2)*www[i]/sigma[i];
5190       fMAngular += norm[i] *nc[i];    
5191       norm2     += nc[i]*www[i]/sigma[i];
5192       norm3     += (nc[i]-2)*www[i]/sigma[i];
5193     }
5194     fDEDX[i]  = mean[i];             
5195     fSDEDX[i] = sigma[i];            
5196     fNCDEDX[i]= nc[i]; 
5197   }
5198
5199   if (norm3>0){
5200     dedx   /=norm2;
5201     fSdEdx /=norm3;
5202     fMAngular/=norm2;
5203   }
5204   else{
5205     SetdEdx(0);
5206     return;
5207   }
5208   //  Float_t dedx1 =dedx;
5209   
5210   dedx =0;
5211   Float_t norm4 = 0;
5212   for (Int_t i =0;i<3;i++){
5213     if (nc[i]>2&&nc[i]<1000&&sigma[i]>3){
5214       //mean[i]   = mean[i]*(1+0.08*(sigma[i]/(fSdEdx)-1.));
5215       dedx      += mean[i] *(nc[i])/(sigma[i]);
5216       norm4     += (nc[i])/(sigma[i]);
5217     }
5218     fDEDX[i]  = mean[i];                
5219   }
5220   if (norm4>0) dedx /= norm4;
5221   
5222
5223   
5224   SetdEdx(dedx);
5225     
5226   //mi deDX
5227
5228 }
5229
5230 */