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