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