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