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