]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCtrackerMI.cxx
Default values changed
[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   if (sec<fkNIS*2){
1418     tpcrow = &(fInnerSec[sec%fkNIS][row]);
1419     if (sec<fkNIS) 
1420       clrow = tpcrow->fClusters1;
1421     else
1422       clrow = tpcrow->fClusters2;
1423   }
1424   else{
1425     tpcrow = &(fOuterSec[(sec-fkNIS*2)%fkNOS][row]);
1426     if (sec-2*fkNIS<fkNOS)
1427       clrow = tpcrow->fClusters1;
1428     else
1429       clrow = tpcrow->fClusters2;
1430   }
1431   if (tpcrow==0) return 0;
1432   if (tpcrow->GetN()<=ncl) return 0;
1433   //  return (AliTPCclusterMI*)(*tpcrow)[ncl];      
1434   return &(clrow[ncl]);      
1435   
1436 }
1437
1438
1439
1440 Int_t AliTPCtrackerMI::FollowToNext(AliTPCseed& t, Int_t nr) {
1441   //-----------------------------------------------------------------
1442   // This function tries to find a track prolongation to next pad row
1443   //-----------------------------------------------------------------
1444   //
1445   Double_t  x= GetXrow(nr), ymax=GetMaxY(nr);
1446   AliTPCclusterMI *cl=0;
1447   Int_t tpcindex= t.GetClusterIndex2(nr);
1448   //
1449   // update current shape info every 5 pad-row
1450   //  if ( (nr%5==0) || t.GetNumberOfClusters()<2 || (t.fCurrentSigmaY2<0.0001) ){
1451     GetShape(&t,nr);    
1452     //}
1453   //  
1454   if (fIteration>0 && tpcindex>=-1){  //if we have already clusters 
1455     //        
1456     if (tpcindex==-1) return 0; //track in dead zone
1457     if (tpcindex>0){     //
1458       cl = t.fClusterPointer[nr];
1459       if ( (cl==0) ) cl = GetClusterMI(tpcindex);
1460       t.fCurrentClusterIndex1 = tpcindex; 
1461     }
1462     if (cl){      
1463       Int_t relativesector = ((tpcindex&0xff000000)>>24)%18;  // if previously accepted cluster in different sector
1464       Float_t angle = relativesector*fSectors->GetAlpha()+fSectors->GetAlphaShift();
1465       //
1466       if (angle<-TMath::Pi()) angle += 2*TMath::Pi();
1467       if (angle>=TMath::Pi()) angle -= 2*TMath::Pi();
1468       
1469       if (TMath::Abs(angle-t.GetAlpha())>0.001){
1470         Double_t rotation = angle-t.GetAlpha();
1471         t.fRelativeSector= relativesector;
1472         t.Rotate(rotation);     
1473       }
1474       t.PropagateTo(x);
1475       //
1476       t.fCurrentCluster = cl; 
1477       t.fRow = nr;
1478       Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
1479       if ((tpcindex&0x8000)==0) accept =0;
1480       if (accept<3) { 
1481         //if founded cluster is acceptible
1482         if (cl->IsUsed(11)) {  // id cluster is shared inrease uncertainty
1483           t.fErrorY2 += 0.03;
1484           t.fErrorZ2 += 0.03; 
1485           t.fErrorY2 *= 3;
1486           t.fErrorZ2 *= 3; 
1487         }
1488         t.fNFoundable++;
1489         UpdateTrack(&t,accept);
1490         return 1;
1491       }    
1492     }
1493   }
1494   if (fIteration>1) return 0;  // not look for new cluster during refitting
1495   //
1496   UInt_t index=0;
1497   if (TMath::Abs(t.GetSnp())>0.95 || TMath::Abs(x*t.GetC()-t.GetEta())>0.95) return 0;
1498   Double_t  y=t.GetYat(x);
1499   if (TMath::Abs(y)>ymax){
1500     if (y > ymax) {
1501       t.fRelativeSector= (t.fRelativeSector+1) % fN;
1502       if (!t.Rotate(fSectors->GetAlpha())) 
1503         return 0;
1504     } else if (y <-ymax) {
1505       t.fRelativeSector= (t.fRelativeSector-1+fN) % fN;
1506       if (!t.Rotate(-fSectors->GetAlpha())) 
1507         return 0;
1508     }
1509     //return 1;
1510   }
1511   //
1512   if (!t.PropagateTo(x)) {
1513     if (fIteration==0) t.fRemoval = 10;
1514     return 0;
1515   }
1516   y=t.GetY(); 
1517   Double_t z=t.GetZ();
1518   //
1519   const AliTPCRow &krow=GetRow(t.fRelativeSector,nr);
1520   if ( (t.GetSigmaY2()<0) || t.GetSigmaZ2()<0) return 0;
1521   Double_t  roady  =1.;
1522   Double_t  roadz = 1.;
1523   //
1524   if (TMath::Abs(TMath::Abs(y)-ymax)<krow.fDeadZone){
1525     t.fInDead = kTRUE;
1526     t.SetClusterIndex2(nr,-1); 
1527     return 0;
1528   } 
1529   else
1530     {
1531       if (TMath::Abs(z)<(1.05*x+10)) t.fNFoundable++;
1532       else
1533         return 0;
1534     }   
1535   //calculate 
1536   if (krow) {
1537     //    cl = krow.FindNearest2(y+10.,z,roady,roadz,index);    
1538     cl = krow.FindNearest2(y,z,roady,roadz,index);    
1539     if (cl) t.fCurrentClusterIndex1 = krow.GetIndex(index);       
1540   }  
1541   if (cl) {
1542     t.fCurrentCluster = cl; 
1543     t.fRow = nr;
1544     if (fIteration==2&&cl->IsUsed(10)) return 0; 
1545     Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
1546     if (fIteration==2&&cl->IsUsed(11)) {
1547       t.fErrorY2 += 0.03;
1548       t.fErrorZ2 += 0.03; 
1549       t.fErrorY2 *= 3;
1550       t.fErrorZ2 *= 3; 
1551     }
1552     /*    
1553     if (t.fCurrentCluster->IsUsed(10)){
1554       //
1555       //     
1556
1557       t.fNShared++;
1558       if (t.fNShared>0.7*t.GetNumberOfClusters()) {
1559         t.fRemoval =10;
1560         return 0;
1561       }
1562     }
1563     */
1564     if (accept<3) UpdateTrack(&t,accept);
1565
1566   } else {  
1567     if ( fIteration==0 && t.fNFoundable*0.5 > t.GetNumberOfClusters()) t.fRemoval=10;
1568     
1569   }
1570   return 1;
1571 }
1572
1573 Int_t AliTPCtrackerMI::FollowToNextFast(AliTPCseed& t, Int_t nr) {
1574   //-----------------------------------------------------------------
1575   // This function tries to find a track prolongation to next pad row
1576   //-----------------------------------------------------------------
1577   //
1578   Double_t  x= GetXrow(nr), ymax=GetMaxY(nr);
1579   Double_t y,z; 
1580   if (!t.GetProlongation(x,y,z)) {
1581     t.fRemoval = 10;
1582     return 0;
1583   }
1584   //
1585   //
1586   if (TMath::Abs(y)>ymax){
1587     
1588     if (y > ymax) {
1589       t.fRelativeSector= (t.fRelativeSector+1) % fN;
1590       if (!t.Rotate(fSectors->GetAlpha())) 
1591         return 0;
1592     } else if (y <-ymax) {
1593       t.fRelativeSector= (t.fRelativeSector-1+fN) % fN;
1594       if (!t.Rotate(-fSectors->GetAlpha())) 
1595         return 0;
1596     }
1597     if (!t.PropagateTo(x)) {
1598       return 0;
1599     } 
1600     t.GetProlongation(x,y,z);
1601   }
1602   //
1603   // update current shape info every 3 pad-row
1604   if ( (nr%6==0) || t.GetNumberOfClusters()<2 || (t.fCurrentSigmaY2<0.0001) ){
1605     //    t.fCurrentSigmaY = GetSigmaY(&t);
1606     //t.fCurrentSigmaZ = GetSigmaZ(&t);
1607     GetShape(&t,nr);
1608   }
1609   //  
1610   AliTPCclusterMI *cl=0;
1611   UInt_t index=0;
1612   
1613   
1614   //Int_t nr2 = nr;
1615   const AliTPCRow &krow=GetRow(t.fRelativeSector,nr);
1616   if ( (t.GetSigmaY2()<0) || t.GetSigmaZ2()<0) return 0;
1617   Double_t  roady  =1.;
1618   Double_t  roadz = 1.;
1619   //
1620   Int_t row = nr;
1621   if (TMath::Abs(TMath::Abs(y)-ymax)<krow.fDeadZone){
1622     t.fInDead = kTRUE;
1623     t.SetClusterIndex2(row,-1); 
1624     return 0;
1625   } 
1626   else
1627     {
1628       if (TMath::Abs(z)>(1.05*x+10)) t.SetClusterIndex2(row,-1);
1629     }   
1630   //calculate 
1631   
1632   if ((cl==0)&&(krow)) {
1633     //    cl = krow.FindNearest2(y+10,z,roady,roadz,index);    
1634     cl = krow.FindNearest2(y,z,roady,roadz,index);    
1635
1636     if (cl) t.fCurrentClusterIndex1 = krow.GetIndex(index);       
1637   }  
1638
1639   if (cl) {
1640     t.fCurrentCluster = cl; 
1641     //    Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);        
1642     //if (accept<3){
1643       t.SetClusterIndex2(row,index);
1644       t.fClusterPointer[row] = cl;
1645       //}
1646   }
1647   return 1;
1648 }
1649
1650
1651
1652 Int_t AliTPCtrackerMI::UpdateClusters(AliTPCseed& t,  Int_t nr) {
1653   //-----------------------------------------------------------------
1654   // This function tries to find a track prolongation to next pad row
1655   //-----------------------------------------------------------------
1656   t.fCurrentCluster  = 0;
1657   t.fCurrentClusterIndex1 = 0;   
1658    
1659   Double_t xt=t.GetX();
1660   Int_t     row = GetRowNumber(xt)-1; 
1661   Double_t  ymax= GetMaxY(nr);
1662
1663   if (row < nr) return 1; // don't prolongate if not information until now -
1664   if (TMath::Abs(t.GetSnp())>0.9 && t.GetNumberOfClusters()>40. && fIteration!=2) {
1665     t.fRemoval =10;
1666     return 0;  // not prolongate strongly inclined tracks
1667   } 
1668   if (TMath::Abs(t.GetSnp())>0.95) {
1669     t.fRemoval =10;
1670     return 0;  // not prolongate strongly inclined tracks
1671   }
1672
1673   Double_t x= GetXrow(nr);
1674   Double_t y,z;
1675   //t.PropagateTo(x+0.02);
1676   //t.PropagateTo(x+0.01);
1677   if (!t.PropagateTo(x)){
1678     return 0;
1679   }
1680   //
1681   y=t.GetY();
1682   z=t.GetZ();
1683
1684   if (TMath::Abs(y)>ymax){
1685     if (y > ymax) {
1686       t.fRelativeSector= (t.fRelativeSector+1) % fN;
1687       if (!t.Rotate(fSectors->GetAlpha())) 
1688         return 0;
1689     } else if (y <-ymax) {
1690       t.fRelativeSector= (t.fRelativeSector-1+fN) % fN;
1691       if (!t.Rotate(-fSectors->GetAlpha())) 
1692         return 0;
1693     }
1694     //    if (!t.PropagateTo(x)){
1695     //  return 0;
1696     //}
1697     return 1;
1698     //y = t.GetY();    
1699   }
1700   //
1701
1702   AliTPCRow &krow=GetRow(t.fRelativeSector,nr);
1703
1704   if (TMath::Abs(TMath::Abs(y)-ymax)<krow.fDeadZone){
1705     t.fInDead = kTRUE;
1706     t.SetClusterIndex2(nr,-1); 
1707     return 0;
1708   } 
1709   else
1710     {
1711       if (TMath::Abs(t.GetZ())<(1.05*t.GetX()+10)) t.fNFoundable++;
1712       else
1713         return 0;      
1714     }
1715
1716   // update current
1717   if ( (nr%6==0) || t.GetNumberOfClusters()<2){
1718     //    t.fCurrentSigmaY = GetSigmaY(&t);
1719     //t.fCurrentSigmaZ = GetSigmaZ(&t);
1720     GetShape(&t,nr);
1721   }
1722     
1723   AliTPCclusterMI *cl=0;
1724   UInt_t index=0;
1725   //
1726   Double_t roady = 1.;
1727   Double_t roadz = 1.;
1728   //
1729
1730   if (!cl){
1731     index = t.GetClusterIndex2(nr);    
1732     if ( (index>0) && (index&0x8000)==0){
1733       cl = t.fClusterPointer[nr];
1734       if ( (cl==0) && (index>0)) cl = GetClusterMI(index);
1735       t.fCurrentClusterIndex1 = index;
1736       if (cl) {
1737         t.fCurrentCluster  = cl;
1738         return 1;
1739       }
1740     }
1741   }
1742
1743   if (krow) {    
1744     //cl = krow.FindNearest2(y+10,z,roady,roadz,index);      
1745     cl = krow.FindNearest2(y,z,roady,roadz,index);      
1746   }
1747
1748   if (cl) t.fCurrentClusterIndex1 = krow.GetIndex(index);   
1749   t.fCurrentCluster  = cl;
1750
1751   return 1;
1752 }
1753
1754
1755 Int_t AliTPCtrackerMI::FollowToNextCluster(AliTPCseed & t, Int_t nr) {
1756   //-----------------------------------------------------------------
1757   // This function tries to find a track prolongation to next pad row
1758   //-----------------------------------------------------------------
1759
1760   //update error according neighborhoud
1761
1762   if (t.fCurrentCluster) {
1763     t.fRow = nr; 
1764     Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
1765     
1766     if (t.fCurrentCluster->IsUsed(10)){
1767       //
1768       //
1769       //  t.fErrorZ2*=2;
1770       //  t.fErrorY2*=2;
1771       t.fNShared++;
1772       if (t.fNShared>0.7*t.GetNumberOfClusters()) {
1773         t.fRemoval =10;
1774         return 0;
1775       }
1776     }   
1777     if (fIteration>0) accept = 0;
1778     if (accept<3)  UpdateTrack(&t,accept);  
1779  
1780   } else {
1781     if (fIteration==0){
1782       if ( ( (t.GetSigmaY2()+t.GetSigmaZ2())>0.16)&& t.GetNumberOfClusters()>18) t.fRemoval=10;      
1783       if (  t.GetChi2()/t.GetNumberOfClusters()>6 &&t.GetNumberOfClusters()>18) t.fRemoval=10;      
1784
1785       if (( (t.fNFoundable*0.5 > t.GetNumberOfClusters()) || t.fNoCluster>15)) t.fRemoval=10;
1786     }
1787   }
1788   return 1;
1789 }
1790
1791
1792
1793 //_____________________________________________________________________________
1794 Int_t AliTPCtrackerMI::FollowProlongation(AliTPCseed& t, Int_t rf, Int_t step) {
1795   //-----------------------------------------------------------------
1796   // This function tries to find a track prolongation.
1797   //-----------------------------------------------------------------
1798   Double_t xt=t.GetX();
1799   //
1800   Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
1801   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
1802   if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
1803   //
1804   t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
1805     
1806   Int_t first = GetRowNumber(xt)-1;
1807   for (Int_t nr= first; nr>=rf; nr-=step) {    
1808     if (nr<fInnerSec->GetNRows()) 
1809       fSectors = fInnerSec;
1810     else
1811       fSectors = fOuterSec;
1812     if (FollowToNext(t,nr)==0) 
1813       if (!t.IsActive()) 
1814         return 0;
1815     
1816   }   
1817   return 1;
1818 }
1819
1820
1821 //_____________________________________________________________________________
1822 Int_t AliTPCtrackerMI::FollowProlongationFast(AliTPCseed& t, Int_t rf, Int_t step) {
1823   //-----------------------------------------------------------------
1824   // This function tries to find a track prolongation.
1825   //-----------------------------------------------------------------
1826   Double_t xt=t.GetX();
1827   //
1828   Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
1829   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
1830   if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
1831   t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
1832     
1833   for (Int_t nr=GetRowNumber(xt)-1; nr>=rf; nr-=step) {
1834     
1835     if (FollowToNextFast(t,nr)==0) 
1836       if (!t.IsActive()) return 0;
1837     
1838   }   
1839   return 1;
1840 }
1841
1842
1843
1844
1845
1846 Int_t AliTPCtrackerMI::FollowBackProlongation(AliTPCseed& t, Int_t rf) {
1847   //-----------------------------------------------------------------
1848   // This function tries to find a track prolongation.
1849   //-----------------------------------------------------------------
1850   //  Double_t xt=t.GetX();  
1851   //
1852   Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
1853   if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
1854   if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
1855   t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
1856     
1857   Int_t first = t.fFirstPoint;
1858   //
1859   if (first<0) first=0;
1860   for (Int_t nr=first; nr<=rf; nr++) {
1861     //if ( (t.GetSnp()<0.9))
1862     if (nr<fInnerSec->GetNRows()) 
1863       fSectors = fInnerSec;
1864     else
1865       fSectors = fOuterSec;
1866     FollowToNext(t,nr);                                                             
1867   }   
1868   return 1;
1869 }
1870
1871
1872
1873
1874    
1875 Float_t AliTPCtrackerMI::OverlapFactor(AliTPCseed * s1, AliTPCseed * s2, Int_t &sum1, Int_t & sum2)
1876 {
1877   //
1878   //
1879   sum1=0;
1880   sum2=0;
1881   Int_t sum=0;
1882   //
1883   Float_t dz2 =(s1->GetZ() - s2->GetZ());
1884   dz2*=dz2;  
1885
1886   Float_t dy2 =TMath::Abs((s1->GetY() - s2->GetY()));
1887   dy2*=dy2;
1888   Float_t distance = TMath::Sqrt(dz2+dy2);
1889   if (distance>4.) return 0; // if there are far away  - not overlap - to reduce combinatorics
1890  
1891   //  Int_t offset =0;
1892   Int_t firstpoint = TMath::Min(s1->fFirstPoint,s2->fFirstPoint);
1893   Int_t lastpoint = TMath::Max(s1->fLastPoint,s2->fLastPoint);
1894   if (lastpoint>160) 
1895     lastpoint =160;
1896   if (firstpoint<0) 
1897     firstpoint = 0;
1898   if (firstpoint>lastpoint) {
1899     firstpoint =lastpoint;
1900     //    lastpoint  =160;
1901   }
1902     
1903   
1904   for (Int_t i=firstpoint-1;i<lastpoint+1;i++){
1905     if (s1->GetClusterIndex2(i)>0) sum1++;
1906     if (s2->GetClusterIndex2(i)>0) sum2++;
1907     if (s1->GetClusterIndex2(i)==s2->GetClusterIndex2(i) && s1->GetClusterIndex2(i)>0) {
1908       sum++;
1909     }
1910   }
1911   if (sum<5) return 0;
1912
1913   Float_t summin = TMath::Min(sum1+1,sum2+1);
1914   Float_t ratio = (sum+1)/Float_t(summin);
1915   return ratio;
1916 }
1917
1918 void  AliTPCtrackerMI::SignShared(AliTPCseed * s1, AliTPCseed * s2)
1919 {
1920   //
1921   //
1922   if (TMath::Abs(s1->GetC()-s2->GetC())>0.004) return;
1923   if (TMath::Abs(s1->GetTgl()-s2->GetTgl())>0.6) return;
1924
1925   Float_t dz2 =(s1->GetZ() - s2->GetZ());
1926   dz2*=dz2;
1927   Float_t dy2 =(s1->GetY() - s2->GetY());
1928   dy2*=dy2;
1929   Float_t distance = dz2+dy2;
1930   if (distance>325.) return ; // if there are far away  - not overlap - to reduce combinatorics
1931   
1932   //
1933   Int_t sumshared=0;
1934   //
1935   Int_t firstpoint = TMath::Max(s1->fFirstPoint,s2->fFirstPoint);
1936   Int_t lastpoint = TMath::Min(s1->fLastPoint,s2->fLastPoint);
1937   //
1938   if (firstpoint>=lastpoint-5) return;;
1939
1940   for (Int_t i=firstpoint;i<lastpoint;i++){
1941     //    if ( (s1->GetClusterIndex2(i)&0xFFFF8FFF)==(s2->GetClusterIndex2(i)&0xFFFF8FFF) && s1->GetClusterIndex2(i)>0) {
1942     if ( (s1->GetClusterIndex2(i))==(s2->GetClusterIndex2(i)) && s1->GetClusterIndex2(i)>0) {
1943       sumshared++;
1944     }
1945   }
1946   if (sumshared>4){
1947     // sign clusters
1948     //
1949     for (Int_t i=firstpoint;i<lastpoint;i++){
1950       //      if ( (s1->GetClusterIndex2(i)&0xFFFF8FFF)==(s2->GetClusterIndex2(i)&0xFFFF8FFF) && s1->GetClusterIndex2(i)>0) {
1951       if ( (s1->GetClusterIndex2(i))==(s2->GetClusterIndex2(i)) && s1->GetClusterIndex2(i)>0) {
1952         AliTPCTrackerPoint *p1  = s1->GetTrackPoint(i);
1953         AliTPCTrackerPoint *p2  = s2->GetTrackPoint(i);; 
1954         if (s1->IsActive()&&s2->IsActive()){
1955           p1->fIsShared = kTRUE;
1956           p2->fIsShared = kTRUE;
1957         }       
1958       }
1959     }
1960   }
1961   //  
1962   if (sumshared>10){
1963     for (Int_t i=0;i<4;i++){
1964       if (s1->fOverlapLabels[3*i]==0){
1965         s1->fOverlapLabels[3*i] = s2->GetLabel();
1966         s1->fOverlapLabels[3*i+1] = sumshared;
1967         s1->fOverlapLabels[3*i+2] = s2->GetUniqueID();
1968         break;
1969       } 
1970     }
1971     for (Int_t i=0;i<4;i++){
1972       if (s2->fOverlapLabels[3*i]==0){
1973         s2->fOverlapLabels[3*i] = s1->GetLabel();
1974         s2->fOverlapLabels[3*i+1] = sumshared;
1975         s2->fOverlapLabels[3*i+2] = s1->GetUniqueID();
1976         break;
1977       } 
1978     }    
1979   }
1980   
1981 }
1982
1983 void  AliTPCtrackerMI::SignShared(TObjArray * arr)
1984 {
1985   //
1986   //sort trackss according sectors
1987   //  
1988   for (Int_t i=0; i<arr->GetEntriesFast(); i++) {
1989     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
1990     if (!pt) continue;
1991     //if (pt) RotateToLocal(pt);
1992     pt->fSort = 0;
1993   }
1994   arr->UnSort();
1995   arr->Sort();  // sorting according z
1996   arr->Expand(arr->GetEntries());
1997   //
1998   //
1999   Int_t nseed=arr->GetEntriesFast();
2000   for (Int_t i=0; i<nseed; i++) {
2001     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2002     if (!pt) continue;
2003     for (Int_t j=0;j<=12;j++){
2004       pt->fOverlapLabels[j] =0;
2005     }
2006   }
2007   for (Int_t i=0; i<nseed; i++) {
2008     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2009     if (!pt) continue;
2010     if (pt->fRemoval>10) continue;
2011     for (Int_t j=i+1; j<nseed; j++){
2012       AliTPCseed *pt2=(AliTPCseed*)arr->UncheckedAt(j);
2013       //      if (pt2){
2014       if (pt2->fRemoval<=10) {
2015         if ( TMath::Abs(pt->fRelativeSector-pt2->fRelativeSector)>0) break;
2016         SignShared(pt,pt2);
2017       }
2018     }  
2019   }
2020 }
2021
2022 void  AliTPCtrackerMI::RemoveDouble(TObjArray * arr, Float_t factor1, Float_t factor2,  Int_t removalindex)
2023 {
2024   //
2025   //sort trackss according sectors
2026   //
2027   if (fDebug&1) {
2028     Info("RemoveDouble","Number of tracks before double removal- %d\n",arr->GetEntries());
2029   }
2030   //
2031   for (Int_t i=0; i<arr->GetEntriesFast(); i++) {
2032     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2033     if (!pt) continue;
2034     pt->fSort = 0;
2035   }
2036   arr->UnSort();
2037   arr->Sort();  // sorting according z
2038   arr->Expand(arr->GetEntries());
2039   //
2040   //reset overlap labels
2041   //
2042   Int_t nseed=arr->GetEntriesFast();
2043   for (Int_t i=0; i<nseed; i++) {
2044     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2045     if (!pt) continue;
2046     pt->SetUniqueID(i);
2047     for (Int_t j=0;j<=12;j++){
2048       pt->fOverlapLabels[j] =0;
2049     }
2050   }
2051   //
2052   //sign shared tracks
2053   for (Int_t i=0; i<nseed; i++) {
2054     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2055     if (!pt) continue;
2056     if (pt->fRemoval>10) continue;
2057     Float_t deltac = pt->GetC()*0.1;
2058     for (Int_t j=i+1; j<nseed; j++){
2059       AliTPCseed *pt2=(AliTPCseed*)arr->UncheckedAt(j);
2060       //      if (pt2){
2061       if (pt2->fRemoval<=10) {
2062         if ( TMath::Abs(pt->fRelativeSector-pt2->fRelativeSector)>0) break;
2063         if (TMath::Abs(pt->GetC()  -pt2->GetC())>deltac) continue;
2064         if (TMath::Abs(pt->GetTgl()-pt2->GetTgl())>0.05) continue;
2065         //
2066         SignShared(pt,pt2);
2067       }
2068     }
2069   }
2070   //
2071   // remove highly shared tracks
2072   for (Int_t i=0; i<nseed; i++) {
2073     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2074     if (!pt) continue;
2075     if (pt->fRemoval>10) continue;
2076     //
2077     Int_t sumshared =0;
2078     for (Int_t j=0;j<4;j++){
2079       sumshared = pt->fOverlapLabels[j*3+1];      
2080     }
2081     Float_t factor = factor1;
2082     if (pt->fRemoval>0) factor = factor2;
2083     if (sumshared/pt->GetNumberOfClusters()>factor){
2084       for (Int_t j=0;j<4;j++){
2085         if (pt->fOverlapLabels[3*j]==0) continue;
2086         if (pt->fOverlapLabels[3*j+1]<5) continue; 
2087         if (pt->fRemoval==removalindex) continue;      
2088         AliTPCseed * pt2 = (AliTPCseed*)arr->UncheckedAt(pt->fOverlapLabels[3*j+2]);
2089         if (!pt2) continue;
2090         if (pt2->GetSigma2C()<pt->GetSigma2C()){
2091           //      pt->fRemoval = removalindex;
2092           delete arr->RemoveAt(i);        
2093           break;
2094         }
2095       }      
2096     }
2097   }
2098   arr->Compress();
2099   if (fDebug&1) {
2100     Info("RemoveDouble","Number of tracks after double removal- %d\n",arr->GetEntries());
2101   }
2102 }
2103
2104
2105
2106
2107
2108
2109 void AliTPCtrackerMI::SortTracks(TObjArray * arr, Int_t mode) const
2110 {
2111   //
2112   //sort tracks in array according mode criteria
2113   Int_t nseed = arr->GetEntriesFast();    
2114   for (Int_t i=0; i<nseed; i++) {
2115     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2116     if (!pt) {
2117       continue;
2118     }
2119     pt->fSort = mode;
2120   }
2121   arr->UnSort();
2122   arr->Sort();
2123 }
2124
2125 void AliTPCtrackerMI::RemoveUsed(TObjArray * arr, Float_t factor1,  Float_t factor2, Int_t removalindex)
2126 {
2127
2128   //Loop over all tracks and remove "overlaps"
2129   //
2130   //
2131   Int_t nseed = arr->GetEntriesFast();  
2132   Int_t good =0;
2133
2134   for (Int_t i=0; i<nseed; i++) {
2135     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2136     if (!pt) {
2137       delete arr->RemoveAt(i);
2138     }
2139     else{
2140       pt->fSort =1;
2141       pt->fBSigned = kFALSE;
2142     }
2143   }
2144   arr->Compress();
2145   nseed = arr->GetEntriesFast();
2146   arr->UnSort();
2147   arr->Sort();
2148   //
2149   //unsign used
2150   UnsignClusters();
2151   //
2152   for (Int_t i=0; i<nseed; i++) {
2153     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2154     if (!pt) {
2155       continue;
2156     }    
2157     Int_t found,foundable,shared;
2158     if (pt->IsActive()) 
2159       pt->GetClusterStatistic(0,160,found, foundable,shared,kFALSE);
2160     else
2161       pt->GetClusterStatistic(0,160,found, foundable,shared,kTRUE); 
2162     //
2163     Double_t factor = factor2;
2164     if (pt->fBConstrain) factor = factor1;
2165
2166     if ((Float_t(shared)/Float_t(found))>factor){
2167       pt->Desactivate(removalindex);
2168       continue;
2169     }
2170
2171     good++;
2172     for (Int_t i=0; i<160; i++) {
2173       Int_t index=pt->GetClusterIndex2(i);
2174       if (index<0 || index&0x8000 ) continue;
2175       AliTPCclusterMI *c= pt->fClusterPointer[i];        
2176       if (!c) continue;
2177       //      if (!c->IsUsed(10)) c->Use(10);
2178       //if (pt->IsActive()) 
2179       c->Use(10);  
2180       //else
2181       //        c->Use(5);
2182     }
2183     
2184   }
2185   fNtracks = good;
2186   if (fDebug>0){
2187     Info("RemoveUsed","\n*****\nNumber of good tracks after shared removal\t%d\n",fNtracks);
2188   }
2189 }
2190
2191 void AliTPCtrackerMI::UnsignClusters() 
2192 {
2193   //
2194   // loop over all clusters and unsign them
2195   //
2196   
2197   for (Int_t sec=0;sec<fkNIS;sec++){
2198     for (Int_t row=0;row<fInnerSec->GetNRows();row++){
2199       AliTPCclusterMI *cl = fInnerSec[sec][row].fClusters1;
2200       for (Int_t icl =0;icl< fInnerSec[sec][row].fN1;icl++)
2201         //      if (cl[icl].IsUsed(10))         
2202         cl[icl].Use(-1);
2203       cl = fInnerSec[sec][row].fClusters2;
2204       for (Int_t icl =0;icl< fInnerSec[sec][row].fN2;icl++)
2205         //if (cl[icl].IsUsed(10))       
2206           cl[icl].Use(-1);      
2207     }
2208   }
2209   
2210   for (Int_t sec=0;sec<fkNOS;sec++){
2211     for (Int_t row=0;row<fOuterSec->GetNRows();row++){
2212       AliTPCclusterMI *cl = fOuterSec[sec][row].fClusters1;
2213       for (Int_t icl =0;icl< fOuterSec[sec][row].fN1;icl++)
2214         //if (cl[icl].IsUsed(10))       
2215           cl[icl].Use(-1);
2216       cl = fOuterSec[sec][row].fClusters2;
2217       for (Int_t icl =0;icl< fOuterSec[sec][row].fN2;icl++)
2218         //if (cl[icl].IsUsed(10))       
2219         cl[icl].Use(-1);      
2220     }
2221   }
2222   
2223 }
2224
2225
2226
2227 void AliTPCtrackerMI::SignClusters(TObjArray * arr, Float_t fnumber, Float_t fdensity)
2228 {
2229   //
2230   //sign clusters to be "used"
2231   //
2232   // snumber and sdensity sign number of sigmas - bellow mean value to be accepted
2233   // loop over "primaries"
2234   
2235   Float_t sumdens=0;
2236   Float_t sumdens2=0;
2237   Float_t sumn   =0;
2238   Float_t sumn2  =0;
2239   Float_t sumchi =0;
2240   Float_t sumchi2 =0;
2241
2242   Float_t sum    =0;
2243
2244   TStopwatch timer;
2245   timer.Start();
2246
2247   Int_t nseed = arr->GetEntriesFast();
2248   for (Int_t i=0; i<nseed; i++) {
2249     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2250     if (!pt) {
2251       continue;
2252     }    
2253     if (!(pt->IsActive())) continue;
2254     Float_t dens = pt->GetNumberOfClusters()/Float_t(pt->fNFoundable);
2255     if ( (dens>0.7) && (pt->GetNumberOfClusters()>70)){
2256       sumdens += dens;
2257       sumdens2+= dens*dens;
2258       sumn    += pt->GetNumberOfClusters();
2259       sumn2   += pt->GetNumberOfClusters()*pt->GetNumberOfClusters();
2260       Float_t chi2 = pt->GetChi2()/pt->GetNumberOfClusters();
2261       if (chi2>5) chi2=5;
2262       sumchi  +=chi2;
2263       sumchi2 +=chi2*chi2;
2264       sum++;
2265     }
2266   }
2267
2268   Float_t mdensity = 0.9;
2269   Float_t meann    = 130;
2270   Float_t meanchi  = 1;
2271   Float_t sdensity = 0.1;
2272   Float_t smeann    = 10;
2273   Float_t smeanchi  =0.4;
2274   
2275
2276   if (sum>20){
2277     mdensity = sumdens/sum;
2278     meann    = sumn/sum;
2279     meanchi  = sumchi/sum;
2280     //
2281     sdensity = sumdens2/sum-mdensity*mdensity;
2282     sdensity = TMath::Sqrt(sdensity);
2283     //
2284     smeann   = sumn2/sum-meann*meann;
2285     smeann   = TMath::Sqrt(smeann);
2286     //
2287     smeanchi = sumchi2/sum - meanchi*meanchi;
2288     smeanchi = TMath::Sqrt(smeanchi);
2289   }
2290
2291
2292   //REMOVE  SHORT DELTAS or tracks going out of sensitive volume of TPC
2293   //
2294   for (Int_t i=0; i<nseed; i++) {
2295     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2296     if (!pt) {
2297       continue;
2298     }
2299     if (pt->fBSigned) continue;
2300     if (pt->fBConstrain) continue;    
2301     //if (!(pt->IsActive())) continue;
2302     /*
2303     Int_t found,foundable,shared;    
2304     pt->GetClusterStatistic(0,160,found, foundable,shared);
2305     if (shared/float(found)>0.3) {
2306       if (shared/float(found)>0.9 ){
2307         //delete arr->RemoveAt(i);
2308       }
2309       continue;
2310     }
2311     */
2312     Bool_t isok =kFALSE;
2313     if ( (pt->fNShared/pt->GetNumberOfClusters()<0.5) &&pt->GetNumberOfClusters()>60)
2314       isok = kTRUE;
2315     if ((TMath::Abs(1/pt->GetC())<100.) && (pt->fNShared/pt->GetNumberOfClusters()<0.7))
2316       isok =kTRUE;
2317     if  (TMath::Abs(pt->GetZ()/pt->GetX())>1.1)
2318       isok =kTRUE;
2319     if ( (TMath::Abs(pt->GetSnp()>0.7) && pt->GetD(0,0)>60.))
2320       isok =kTRUE;
2321     
2322     if (isok)     
2323       for (Int_t i=0; i<160; i++) {     
2324         Int_t index=pt->GetClusterIndex2(i);
2325         if (index<0) continue;
2326         AliTPCclusterMI *c= pt->fClusterPointer[i];
2327         if (!c) continue;
2328         //if (!(c->IsUsed(10))) c->Use();  
2329         c->Use(10);  
2330       }
2331   }
2332   
2333   
2334   //
2335   Double_t maxchi  = meanchi+2.*smeanchi;
2336
2337   for (Int_t i=0; i<nseed; i++) {
2338     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2339     if (!pt) {
2340       continue;
2341     }    
2342     //if (!(pt->IsActive())) continue;
2343     if (pt->fBSigned) continue;
2344     Double_t chi     = pt->GetChi2()/pt->GetNumberOfClusters();
2345     if (chi>maxchi) continue;
2346
2347     Float_t bfactor=1;
2348     Float_t dens = pt->GetNumberOfClusters()/Float_t(pt->fNFoundable);
2349    
2350     //sign only tracks with enoug big density at the beginning
2351     
2352     if ((pt->GetDensityFirst(40)<0.75) && pt->GetNumberOfClusters()<meann) continue; 
2353     
2354     
2355     Double_t mindens = TMath::Max(double(mdensity-sdensity*fdensity*bfactor),0.65);
2356     Double_t minn    = TMath::Max(Int_t(meann-fnumber*smeann*bfactor),50);
2357    
2358     //    if (pt->fBConstrain) mindens = TMath::Max(mdensity-sdensity*fdensity*bfactor,0.65);
2359     if ( (pt->fRemoval==10) && (pt->GetSnp()>0.8)&&(dens>mindens))
2360       minn=0;
2361
2362     if ((dens>mindens && pt->GetNumberOfClusters()>minn) && chi<maxchi ){
2363       //Int_t noc=pt->GetNumberOfClusters();
2364       pt->fBSigned = kTRUE;
2365       for (Int_t i=0; i<160; i++) {
2366
2367         Int_t index=pt->GetClusterIndex2(i);
2368         if (index<0) continue;
2369         AliTPCclusterMI *c= pt->fClusterPointer[i];
2370         if (!c) continue;
2371         //      if (!(c->IsUsed(10))) c->Use();  
2372         c->Use(10);  
2373       }
2374     }
2375   }
2376   //  gLastCheck = nseed;
2377   //  arr->Compress();
2378   if (fDebug>0){
2379     timer.Print();
2380   }
2381 }
2382
2383
2384 void  AliTPCtrackerMI::StopNotActive(TObjArray * arr, Int_t row0, Float_t th0, Float_t th1, Float_t th2) const
2385 {
2386   // stop not active tracks
2387   // take th1 as threshold for number of founded to number of foundable on last 10 active rows
2388   // take th2 as threshold for number of founded to number of foundable on last 20 active rows 
2389   Int_t nseed = arr->GetEntriesFast();  
2390   //
2391   for (Int_t i=0; i<nseed; i++) {
2392     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
2393     if (!pt) {
2394       continue;
2395     }
2396     if (!(pt->IsActive())) continue;
2397     StopNotActive(pt,row0,th0, th1,th2);
2398   }
2399 }
2400
2401
2402
2403 void  AliTPCtrackerMI::StopNotActive(AliTPCseed * seed, Int_t row0, Float_t th0, Float_t th1,
2404  Float_t th2) const
2405 {
2406   // stop not active tracks
2407   // take th1 as threshold for number of founded to number of foundable on last 10 active rows
2408   // take th2 as threshold for number of founded to number of foundable on last 20 active rows 
2409   Int_t sumgood1  = 0;
2410   Int_t sumgood2  = 0;
2411   Int_t foundable = 0;
2412   Int_t maxindex = seed->fLastPoint;  //last foundable row
2413   if (seed->fNFoundable*th0 > seed->GetNumberOfClusters()) {
2414     seed->Desactivate(10) ;
2415     return;
2416   }
2417
2418   for (Int_t i=row0; i<maxindex; i++){
2419     Int_t index = seed->GetClusterIndex2(i);
2420     if (index!=-1) foundable++;
2421     //if (!c) continue;
2422     if (foundable<=30) sumgood1++;
2423     if (foundable<=50) {
2424       sumgood2++;
2425     }
2426     else{ 
2427       break;
2428     }        
2429   }
2430   if (foundable>=30.){ 
2431      if (sumgood1<(th1*30.)) seed->Desactivate(10);
2432   }
2433   if (foundable>=50)
2434     if (sumgood2<(th2*50.)) seed->Desactivate(10);
2435 }
2436
2437
2438 Int_t AliTPCtrackerMI::RefitInward(AliESD *event)
2439 {
2440   //
2441   // back propagation of ESD tracks
2442   //
2443   //return 0;
2444   fEvent = event;
2445   ReadSeeds(event,2);
2446   fIteration=2;
2447   //PrepareForProlongation(fSeeds,1);
2448   PropagateForward2(fSeeds);
2449   Int_t ntracks=0;
2450   Int_t nseed = fSeeds->GetEntriesFast();
2451   for (Int_t i=0;i<nseed;i++){
2452     AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
2453     if (!seed) continue;
2454     seed->PropagateTo(fParam->GetInnerRadiusLow());
2455     AliESDtrack *esd=event->GetTrack(i);
2456     seed->CookdEdx(0.02,0.6);
2457     CookLabel(seed,0.1); //For comparison only
2458     if (seed->GetNumberOfClusters()>60){
2459       esd->UpdateTrackParams(seed,AliESDtrack::kTPCrefit); 
2460       ntracks++;
2461     }
2462     else{
2463       //printf("problem\n");
2464     }
2465   }
2466   Info("RefitInward","Number of refitted tracks %d",ntracks);
2467   fEvent =0;
2468   //WriteTracks();
2469   return 0;
2470 }
2471
2472
2473 Int_t AliTPCtrackerMI::PropagateBack(AliESD *event)
2474 {
2475   //
2476   // back propagation of ESD tracks
2477   //
2478
2479   fEvent = event;
2480   fIteration = 1;
2481   ReadSeeds(event,0);
2482   PropagateBack(fSeeds);
2483   Int_t nseed = fSeeds->GetEntriesFast();
2484   Int_t ntracks=0;
2485   for (Int_t i=0;i<nseed;i++){
2486     AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
2487     if (!seed) continue;
2488     AliESDtrack *esd=event->GetTrack(i);
2489     seed->CookdEdx(0.02,0.6);
2490     CookLabel(seed,0.1); //For comparison only
2491     if (seed->GetNumberOfClusters()>60){
2492       esd->UpdateTrackParams(seed,AliESDtrack::kTPCout);
2493       ntracks++;
2494     }
2495   }
2496   Info("PropagateBack","Number of back propagated tracks %d",ntracks);
2497   fEvent =0;
2498   //WriteTracks();
2499   return 0;
2500 }
2501
2502
2503 void AliTPCtrackerMI::DeleteSeeds()
2504 {
2505   //
2506   //delete Seeds
2507   Int_t nseed = fSeeds->GetEntriesFast();
2508   for (Int_t i=0;i<nseed;i++){
2509     AliTPCseed * seed = (AliTPCseed*)fSeeds->At(i);
2510     if (seed) delete fSeeds->RemoveAt(i);
2511   }
2512   delete fSeeds;
2513   fSeeds =0;
2514 }
2515
2516 void AliTPCtrackerMI::ReadSeeds(AliESD *event, Int_t direction)
2517 {
2518   //
2519   //read seeds from the event
2520   
2521   Int_t nentr=event->GetNumberOfTracks();
2522   if (fDebug>0){
2523     Info("ReadSeeds", "Number of ESD tracks: %d\n", nentr);
2524   }
2525   if (fSeeds) 
2526     DeleteSeeds();
2527   if (!fSeeds){   
2528     fSeeds = new TObjArray(nentr);
2529   }
2530   UnsignClusters();
2531   //  Int_t ntrk=0;  
2532   for (Int_t i=0; i<nentr; i++) {
2533     AliESDtrack *esd=event->GetTrack(i);
2534     ULong_t status=esd->GetStatus();    
2535     AliTPCtrack t(*esd);
2536     AliTPCseed *seed = new AliTPCseed(t,t.GetAlpha());
2537     if ((status==AliESDtrack::kTPCin)&&(direction==1)) seed->ResetCovariance(); 
2538     if ( direction ==2 &&(status & AliESDtrack::kTRDrefit) == 0 ) seed->ResetCovariance();
2539     if ( direction ==2 && ((status & AliESDtrack::kTPCout) == 0) ) {
2540       fSeeds->AddAt(0,i);
2541       delete seed;
2542       continue;    
2543     }
2544     if ( direction ==2 &&(status & AliESDtrack::kTRDrefit) > 0 )  {
2545       Double_t par0[5],par1[5],x;
2546       esd->GetInnerExternalParameters(x,par0);
2547       esd->GetExternalParameters(x,par1);
2548       Double_t delta1 = TMath::Abs(par0[4]-par1[4])/(0.000000001+TMath::Abs(par0[4]+par1[4]));
2549       Double_t delta2 = TMath::Abs(par0[3]-par1[3]);
2550       //reset covariance if suspicious 
2551       if ( (delta1>0.1) || (delta2>0.006))
2552         seed->ResetCovariance();
2553     }
2554
2555     //
2556     //
2557     // rotate to the local coordinate system
2558    
2559     fSectors=fInnerSec; fN=fkNIS;
2560     
2561     Double_t alpha=seed->GetAlpha() - fSectors->GetAlphaShift();
2562     if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
2563     if (alpha < 0.            ) alpha += 2.*TMath::Pi();
2564     Int_t ns=Int_t(alpha/fSectors->GetAlpha())%fN;
2565     alpha =ns*fSectors->GetAlpha() + fSectors->GetAlphaShift();
2566     if (alpha<-TMath::Pi()) alpha += 2*TMath::Pi();
2567     if (alpha>=TMath::Pi()) alpha -= 2*TMath::Pi();
2568     alpha-=seed->GetAlpha();  
2569     if (!seed->Rotate(alpha)) {
2570       delete seed;
2571       continue;
2572     }
2573     seed->fEsd = esd;
2574     //
2575     //seed->PropagateTo(fSectors->GetX(0));
2576     //
2577     //    Int_t index = esd->GetTPCindex();
2578     //AliTPCseed * seed2= (AliTPCseed*)fSeeds->At(index);
2579     //if (direction==2){
2580     //  AliTPCseed * seed2  = ReSeed(seed,0.,0.5,1.);
2581     //  if (seed2) {
2582     //  delete seed;
2583     //  seed = seed2;
2584     //  }
2585     //}
2586     //
2587     // sign clusters
2588     for (Int_t irow=0;irow<160;irow++){
2589       Int_t index = seed->GetClusterIndex2(irow);    
2590       if (index>0){ 
2591         //
2592         AliTPCclusterMI * cl = GetClusterMI(index);
2593         seed->fClusterPointer[irow] = cl;
2594         if (cl){
2595           if ((index & 0x8000)==0){
2596             cl->Use(10);  // accepted cluster     
2597           }else{
2598             cl->Use(6);   // close cluster not accepted
2599           }     
2600         }else{
2601            Info("ReadSeeds","Not found cluster");
2602         }
2603       }
2604     }
2605     fSeeds->AddAt(seed,i);
2606   }
2607 }
2608
2609
2610
2611 //_____________________________________________________________________________
2612 void AliTPCtrackerMI::MakeSeeds3(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,  Float_t cuts[4],
2613                                  Float_t deltay, Int_t ddsec) {
2614   //-----------------------------------------------------------------
2615   // This function creates track seeds.
2616   // SEEDING WITH VERTEX CONSTRAIN 
2617   //-----------------------------------------------------------------
2618   // cuts[0]   - fP4 cut
2619   // cuts[1]   - tan(phi)  cut
2620   // cuts[2]   - zvertex cut
2621   // cuts[3]   - fP3 cut
2622   Int_t nin0  = 0;
2623   Int_t nin1  = 0;
2624   Int_t nin2  = 0;
2625   Int_t nin   = 0;
2626   Int_t nout1 = 0;
2627   Int_t nout2 = 0;
2628
2629   Double_t x[5], c[15];
2630   //  Int_t di = i1-i2;
2631   //
2632   AliTPCseed * seed = new AliTPCseed;
2633   Double_t alpha=fSectors->GetAlpha(), shift=fSectors->GetAlphaShift();
2634   Double_t cs=cos(alpha), sn=sin(alpha);
2635   //
2636   //  Double_t x1 =fOuterSec->GetX(i1);
2637   //Double_t xx2=fOuterSec->GetX(i2);
2638   
2639   Double_t x1 =GetXrow(i1);
2640   Double_t xx2=GetXrow(i2);
2641
2642   Double_t x3=GetX(), y3=GetY(), z3=GetZ();
2643
2644   Int_t imiddle = (i2+i1)/2;    //middle pad row index
2645   Double_t xm = GetXrow(imiddle); // radius of middle pad-row
2646   const AliTPCRow& krm=GetRow(sec,imiddle); //middle pad -row
2647   //
2648   Int_t ns =sec;   
2649
2650   const AliTPCRow& kr1=GetRow(ns,i1);
2651   Double_t ymax  = GetMaxY(i1)-kr1.fDeadZone-1.5;  
2652   Double_t ymaxm = GetMaxY(imiddle)-kr1.fDeadZone-1.5;  
2653
2654   //
2655   // change cut on curvature if it can't reach this layer
2656   // maximal curvature set to reach it
2657   Double_t dvertexmax  = TMath::Sqrt((x1-x3)*(x1-x3)+(ymax+5-y3)*(ymax+5-y3));
2658   if (dvertexmax*0.5*cuts[0]>0.85){
2659     cuts[0] = 0.85/(dvertexmax*0.5+1.);
2660   }
2661   Double_t r2min = 1/(cuts[0]*cuts[0]);  //minimal square of radius given by cut
2662
2663   //  Int_t ddsec = 1;
2664   if (deltay>0) ddsec = 0; 
2665   // loop over clusters  
2666   for (Int_t is=0; is < kr1; is++) {
2667     //
2668     if (kr1[is]->IsUsed(10)) continue;
2669     Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();    
2670     //if (TMath::Abs(y1)>ymax) continue;
2671
2672     if (deltay>0 && TMath::Abs(ymax-TMath::Abs(y1))> deltay ) continue;  // seed only at the edge
2673
2674     // find possible directions    
2675     Float_t anglez = (z1-z3)/(x1-x3); 
2676     Float_t extraz = z1 - anglez*(x1-xx2);  // extrapolated z      
2677     //
2678     //
2679     //find   rotation angles relative to line given by vertex and point 1
2680     Double_t dvertex2 = (x1-x3)*(x1-x3)+(y1-y3)*(y1-y3);
2681     Double_t dvertex  = TMath::Sqrt(dvertex2);
2682     Double_t angle13  = TMath::ATan((y1-y3)/(x1-x3));
2683     Double_t cs13     = cos(-angle13), sn13 = sin(-angle13);            
2684     
2685     //
2686     // loop over 2 sectors
2687     Int_t dsec1=-ddsec;
2688     Int_t dsec2= ddsec;
2689     if (y1<0)  dsec2= 0;
2690     if (y1>0)  dsec1= 0;
2691     
2692     Double_t dddz1=0;  // direction of delta inclination in z axis
2693     Double_t dddz2=0;
2694     if ( (z1-z3)>0)
2695       dddz1 =1;    
2696     else
2697       dddz2 =1;
2698     //
2699     for (Int_t dsec = dsec1; dsec<=dsec2;dsec++){
2700       Int_t sec2 = sec + dsec;
2701       // 
2702       //      AliTPCRow&  kr2  = fOuterSec[(sec2+fkNOS)%fkNOS][i2];
2703       //AliTPCRow&  kr2m = fOuterSec[(sec2+fkNOS)%fkNOS][imiddle];
2704       AliTPCRow&  kr2  = GetRow((sec2+fkNOS)%fkNOS,i2);
2705       AliTPCRow&  kr2m = GetRow((sec2+fkNOS)%fkNOS,imiddle);
2706       Int_t  index1 = TMath::Max(kr2.Find(extraz-0.6-dddz1*TMath::Abs(z1)*0.05)-1,0);
2707       Int_t  index2 = TMath::Min(kr2.Find(extraz+0.6+dddz2*TMath::Abs(z1)*0.05)+1,kr2);
2708
2709       // rotation angles to p1-p3
2710       Double_t cs13r     = cos(-angle13+dsec*alpha)/dvertex, sn13r = sin(-angle13+dsec*alpha)/dvertex;            
2711       Double_t x2,   y2,   z2; 
2712       //
2713       //      Double_t dymax = maxangle*TMath::Abs(x1-xx2);
2714
2715       //
2716       Double_t dxx0 =  (xx2-x3)*cs13r;
2717       Double_t dyy0 =  (xx2-x3)*sn13r;
2718       for (Int_t js=index1; js < index2; js++) {
2719         const AliTPCclusterMI *kcl = kr2[js];
2720         if (kcl->IsUsed(10)) continue;  
2721         //
2722         //calcutate parameters
2723         //      
2724         Double_t yy0 =  dyy0 +(kcl->GetY()-y3)*cs13r;
2725         // stright track
2726         if (TMath::Abs(yy0)<0.000001) continue;
2727         Double_t xx0 =  dxx0 -(kcl->GetY()-y3)*sn13r;
2728         Double_t y0  =  0.5*(xx0*xx0+yy0*yy0-xx0)/yy0;
2729         Double_t r02 = (0.25+y0*y0)*dvertex2;   
2730         //curvature (radius) cut
2731         if (r02<r2min) continue;                
2732        
2733         nin0++; 
2734         //
2735         Double_t c0  = 1/TMath::Sqrt(r02);
2736         if (yy0>0) c0*=-1.;     
2737                
2738        
2739         //Double_t dfi0   = 2.*TMath::ASin(dvertex*c0*0.5);
2740         //Double_t dfi1   = 2.*TMath::ASin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);
2741         Double_t dfi0   = 2.*AliTPCFastMath::FastAsin(dvertex*c0*0.5);
2742         Double_t dfi1   = 2.*AliTPCFastMath::FastAsin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);  
2743         //
2744         //
2745         Double_t z0  =  kcl->GetZ();  
2746         Double_t zzzz2    = z1-(z1-z3)*dfi1/dfi0;
2747         if (TMath::Abs(zzzz2-z0)>0.5) continue;       
2748         nin1++;              
2749         //      
2750         Double_t dip    = (z1-z0)*c0/dfi1;        
2751         Double_t x0 = (0.5*cs13+y0*sn13)*dvertex*c0;
2752         //
2753         y2 = kcl->GetY(); 
2754         if (dsec==0){
2755           x2 = xx2; 
2756           z2 = kcl->GetZ();       
2757         }
2758         else
2759           {
2760             // rotation 
2761             z2 = kcl->GetZ();  
2762             x2= xx2*cs-y2*sn*dsec;
2763             y2=+xx2*sn*dsec+y2*cs;
2764           }
2765         
2766         x[0] = y1;
2767         x[1] = z1;
2768         x[2] = x0;
2769         x[3] = dip;
2770         x[4] = c0;
2771         //
2772         //
2773         // do we have cluster at the middle ?
2774         Double_t ym,zm;
2775         GetProlongation(x1,xm,x,ym,zm);
2776         UInt_t dummy; 
2777         AliTPCclusterMI * cm=0;
2778         if (TMath::Abs(ym)-ymaxm<0){      
2779           cm = krm.FindNearest2(ym,zm,1.0,0.6,dummy);
2780           if ((!cm) || (cm->IsUsed(10))) {        
2781             continue;
2782           }
2783         }
2784         else{     
2785           // rotate y1 to system 0
2786           // get state vector in rotated system 
2787           Double_t yr1  = (-0.5*sn13+y0*cs13)*dvertex*c0;
2788           Double_t xr2  =  x0*cs+yr1*sn*dsec;
2789           Double_t xr[5]={kcl->GetY(),kcl->GetZ(), xr2, dip, c0};
2790           //
2791           GetProlongation(xx2,xm,xr,ym,zm);
2792           if (TMath::Abs(ym)-ymaxm<0){
2793             cm = kr2m.FindNearest2(ym,zm,1.0,0.6,dummy);
2794             if ((!cm) || (cm->IsUsed(10))) {      
2795               continue;
2796             }
2797           }
2798         }
2799        
2800
2801         Double_t dym = 0;
2802         Double_t dzm = 0;
2803         if (cm){
2804           dym = ym - cm->GetY();
2805           dzm = zm - cm->GetZ();
2806         }
2807         nin2++;
2808
2809
2810         //
2811         //
2812         Double_t sy1=kr1[is]->GetSigmaY2()*2., sz1=kr1[is]->GetSigmaZ2()*2.;
2813         Double_t sy2=kcl->GetSigmaY2()*2.,     sz2=kcl->GetSigmaZ2()*2.;
2814         //Double_t sy3=400*3./12., sy=0.1, sz=0.1;
2815         Double_t sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
2816         //Double_t sy3=25000*x[4]*x[4]*60+0.5, sy=0.1, sz=0.1;
2817
2818         Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
2819         Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
2820         Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
2821         Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
2822         Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
2823         Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
2824         
2825         Double_t f30=(F3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
2826         Double_t f31=(F3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
2827         Double_t f32=(F3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
2828         Double_t f34=(F3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
2829         
2830         c[0]=sy1;
2831         c[1]=0.;       c[2]=sz1;
2832         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
2833         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
2834                        c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
2835         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
2836         c[13]=f30*sy1*f40+f32*sy2*f42;
2837         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
2838         
2839         //      if (!BuildSeed(kr1[is],kcl,0,x1,x2,x3,x,c)) continue;
2840         
2841         UInt_t index=kr1.GetIndex(is);
2842         AliTPCseed *track=new(seed) AliTPCseed(index, x, c, x1, ns*alpha+shift);
2843         
2844         track->fIsSeeding = kTRUE;
2845         track->fSeed1 = i1;
2846         track->fSeed2 = i2;
2847         track->fSeedType=3;
2848
2849        
2850         //if (dsec==0) {
2851           FollowProlongation(*track, (i1+i2)/2,1);
2852           Int_t foundable,found,shared;
2853           track->GetClusterStatistic((i1+i2)/2,i1, found, foundable, shared, kTRUE);
2854           if ((found<0.55*foundable)  || shared>0.5*found || (track->GetSigmaY2()+track->GetSigmaZ2())>0.5){
2855             seed->Reset();
2856             seed->~AliTPCseed();
2857             continue;
2858           }
2859           //}
2860         
2861         nin++;
2862         FollowProlongation(*track, i2,1);
2863         
2864         
2865         //Int_t rc = 1;
2866         track->fBConstrain =1;
2867         //      track->fLastPoint = i1+fInnerSec->GetNRows();  // first cluster in track position
2868         track->fLastPoint = i1;  // first cluster in track position
2869         track->fFirstPoint = track->fLastPoint;
2870         
2871         if (track->GetNumberOfClusters()<(i1-i2)*0.5 || 
2872             track->GetNumberOfClusters() < track->fNFoundable*0.6 || 
2873             track->fNShared>0.4*track->GetNumberOfClusters() ) {
2874           seed->Reset();
2875           seed->~AliTPCseed();
2876           continue;
2877         }
2878         nout1++;
2879         // Z VERTEX CONDITION
2880         Double_t zv;
2881         zv = track->GetZ()+track->GetTgl()/track->GetC()*
2882           ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2883         if (TMath::Abs(zv-z3)>cuts[2]) {
2884           FollowProlongation(*track, TMath::Max(i2-20,0));
2885           zv = track->GetZ()+track->GetTgl()/track->GetC()*
2886             ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2887           if (TMath::Abs(zv-z3)>cuts[2]){
2888             FollowProlongation(*track, TMath::Max(i2-40,0));
2889             zv = track->GetZ()+track->GetTgl()/track->GetC()*
2890               ( asin(-track->GetEta()) - asin(track->GetX()*track->GetC()-track->GetEta()));
2891             if (TMath::Abs(zv-z3)>cuts[2] &&(track->GetNumberOfClusters() > track->fNFoundable*0.7)){
2892               // make seed without constrain
2893               AliTPCseed * track2 = MakeSeed(track,0.2,0.5,1.);
2894               FollowProlongation(*track2, i2,1);
2895               track2->fBConstrain = kFALSE;
2896               track2->fSeedType = 1;
2897               arr->AddLast(track2); 
2898               seed->Reset();
2899               seed->~AliTPCseed();
2900               continue;         
2901             }
2902             else{
2903               seed->Reset();
2904               seed->~AliTPCseed();
2905               continue;
2906             
2907             }
2908           }
2909         }
2910         
2911         track->fSeedType =0;
2912         arr->AddLast(track); 
2913         seed = new AliTPCseed;  
2914         nout2++;
2915         // don't consider other combinations
2916         if (track->GetNumberOfClusters() > track->fNFoundable*0.8)
2917           break;
2918       }
2919     }
2920   }
2921   if (fDebug>3){
2922     Info("MakeSeeds3","\nSeeding statistic:\t%d\t%d\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin,nout1,nout2);
2923   }
2924   delete seed;
2925 }
2926
2927
2928 void AliTPCtrackerMI::MakeSeeds5(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,  Float_t cuts[4],
2929                                  Float_t deltay) {
2930   
2931
2932
2933   //-----------------------------------------------------------------
2934   // This function creates track seeds.
2935   //-----------------------------------------------------------------
2936   // cuts[0]   - fP4 cut
2937   // cuts[1]   - tan(phi)  cut
2938   // cuts[2]   - zvertex cut
2939   // cuts[3]   - fP3 cut
2940
2941
2942   Int_t nin0  = 0;
2943   Int_t nin1  = 0;
2944   Int_t nin2  = 0;
2945   Int_t nin   = 0;
2946   Int_t nout1 = 0;
2947   Int_t nout2 = 0;
2948   Int_t nout3 =0;
2949   Double_t x[5], c[15];
2950   //
2951   // make temporary seed
2952   AliTPCseed * seed = new AliTPCseed;
2953   Double_t alpha=fOuterSec->GetAlpha(), shift=fOuterSec->GetAlphaShift();
2954   //  Double_t cs=cos(alpha), sn=sin(alpha);
2955   //
2956   //
2957
2958   // first 3 padrows
2959   Double_t x1 = GetXrow(i1-1);
2960   const    AliTPCRow& kr1=GetRow(sec,i1-1);
2961   Double_t y1max  = GetMaxY(i1-1)-kr1.fDeadZone-1.5;  
2962   //
2963   Double_t x1p = GetXrow(i1);
2964   const    AliTPCRow& kr1p=GetRow(sec,i1);
2965   //
2966   Double_t x1m = GetXrow(i1-2);
2967   const    AliTPCRow& kr1m=GetRow(sec,i1-2);
2968
2969   //
2970   //last 3 padrow for seeding
2971   AliTPCRow&  kr3  = GetRow((sec+fkNOS)%fkNOS,i1-7);
2972   Double_t    x3   =  GetXrow(i1-7);
2973   //  Double_t    y3max= GetMaxY(i1-7)-kr3.fDeadZone-1.5;  
2974   //
2975   AliTPCRow&  kr3p  = GetRow((sec+fkNOS)%fkNOS,i1-6);
2976   Double_t    x3p   = GetXrow(i1-6);
2977   //
2978   AliTPCRow&  kr3m  = GetRow((sec+fkNOS)%fkNOS,i1-8);
2979   Double_t    x3m   = GetXrow(i1-8);
2980
2981   //
2982   //
2983   // middle padrow
2984   Int_t im = i1-4;                           //middle pad row index
2985   Double_t xm         = GetXrow(im);         // radius of middle pad-row
2986   const AliTPCRow& krm=GetRow(sec,im);   //middle pad -row
2987   //  Double_t ymmax = GetMaxY(im)-kr1.fDeadZone-1.5;  
2988   //
2989   //
2990   Double_t deltax  = x1-x3;
2991   Double_t dymax   = deltax*cuts[1];
2992   Double_t dzmax   = deltax*cuts[3];
2993   //
2994   // loop over clusters  
2995   for (Int_t is=0; is < kr1; is++) {
2996     //
2997     if (kr1[is]->IsUsed(10)) continue;
2998     Double_t y1=kr1[is]->GetY(), z1=kr1[is]->GetZ();    
2999     //
3000     if (deltay>0 && TMath::Abs(y1max-TMath::Abs(y1))> deltay ) continue;  // seed only at the edge    
3001     // 
3002     Int_t  index1 = TMath::Max(kr3.Find(z1-dzmax)-1,0);
3003     Int_t  index2 = TMath::Min(kr3.Find(z1+dzmax)+1,kr3);
3004     //    
3005     Double_t y3,   z3;
3006     //
3007     //
3008     UInt_t index;
3009     for (Int_t js=index1; js < index2; js++) {
3010       const AliTPCclusterMI *kcl = kr3[js];
3011       if (kcl->IsUsed(10)) continue;
3012       y3 = kcl->GetY(); 
3013       // apply angular cuts
3014       if (TMath::Abs(y1-y3)>dymax) continue;
3015       x3 = x3; 
3016       z3 = kcl->GetZ(); 
3017       if (TMath::Abs(z1-z3)>dzmax) continue;
3018       //
3019       Double_t angley = (y1-y3)/(x1-x3);
3020       Double_t anglez = (z1-z3)/(x1-x3);
3021       //
3022       Double_t erry = TMath::Abs(angley)*(x1-x1m)*0.5+0.5;
3023       Double_t errz = TMath::Abs(anglez)*(x1-x1m)*0.5+0.5;
3024       //
3025       Double_t yyym = angley*(xm-x1)+y1;
3026       Double_t zzzm = anglez*(xm-x1)+z1;
3027
3028       const AliTPCclusterMI *kcm = krm.FindNearest2(yyym,zzzm,erry,errz,index);
3029       if (!kcm) continue;
3030       if (kcm->IsUsed(10)) continue;
3031       
3032       erry = TMath::Abs(angley)*(x1-x1m)*0.4+0.5;
3033       errz = TMath::Abs(anglez)*(x1-x1m)*0.4+0.5;
3034       //
3035       //
3036       //
3037       Int_t used  =0;
3038       Int_t found =0;
3039       //
3040       // look around first
3041       const AliTPCclusterMI *kc1m = kr1m.FindNearest2(angley*(x1m-x1)+y1,
3042                                                       anglez*(x1m-x1)+z1,
3043                                                       erry,errz,index);
3044       //
3045       if (kc1m){
3046         found++;
3047         if (kc1m->IsUsed(10)) used++;
3048       }
3049       const AliTPCclusterMI *kc1p = kr1p.FindNearest2(angley*(x1p-x1)+y1,
3050                                                       anglez*(x1p-x1)+z1,
3051                                                       erry,errz,index);
3052       //
3053       if (kc1p){
3054         found++;
3055         if (kc1p->IsUsed(10)) used++;
3056       }
3057       if (used>1)  continue;
3058       if (found<1) continue; 
3059
3060       //
3061       // look around last
3062       const AliTPCclusterMI *kc3m = kr3m.FindNearest2(angley*(x3m-x3)+y3,
3063                                                       anglez*(x3m-x3)+z3,
3064                                                       erry,errz,index);
3065       //
3066       if (kc3m){
3067         found++;
3068         if (kc3m->IsUsed(10)) used++;
3069       }
3070       else 
3071         continue;
3072       const AliTPCclusterMI *kc3p = kr3p.FindNearest2(angley*(x3p-x3)+y3,
3073                                                       anglez*(x3p-x3)+z3,
3074                                                       erry,errz,index);
3075       //
3076       if (kc3p){
3077         found++;
3078         if (kc3p->IsUsed(10)) used++;
3079       }
3080       else 
3081         continue;
3082       if (used>1)  continue;
3083       if (found<3) continue;       
3084       //
3085       Double_t x2,y2,z2;
3086       x2 = xm;
3087       y2 = kcm->GetY();
3088       z2 = kcm->GetZ();
3089       //
3090                         
3091       x[0]=y1;
3092       x[1]=z1;
3093       x[4]=F1(x1,y1,x2,y2,x3,y3);
3094       //if (TMath::Abs(x[4]) >= cuts[0]) continue;
3095       nin0++;
3096       //
3097       x[2]=F2(x1,y1,x2,y2,x3,y3);
3098       nin1++;
3099       //
3100       x[3]=F3n(x1,y1,x2,y2,z1,z2,x[4]);
3101       //if (TMath::Abs(x[3]) > cuts[3]) continue;
3102       nin2++;
3103       //
3104       //
3105       Double_t sy1=0.1,  sz1=0.1;
3106       Double_t sy2=0.1,  sz2=0.1;
3107       Double_t sy3=0.1,  sy=0.1, sz=0.1;
3108       
3109       Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
3110       Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
3111       Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
3112       Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
3113       Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
3114       Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
3115       
3116       Double_t f30=(F3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
3117       Double_t f31=(F3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
3118       Double_t f32=(F3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
3119       Double_t f34=(F3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
3120       
3121       c[0]=sy1;
3122       c[1]=0.;       c[2]=sz1; 
3123       c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3124       c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3125       c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3126       c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3127       c[13]=f30*sy1*f40+f32*sy2*f42;
3128       c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3129       
3130       //        if (!BuildSeed(kr1[is],kcl,0,x1,x2,x3,x,c)) continue;
3131       
3132       UInt_t index=kr1.GetIndex(is);
3133       AliTPCseed *track=new(seed) AliTPCseed(index, x, c, x1, sec*alpha+shift);
3134       
3135       track->fIsSeeding = kTRUE;
3136
3137       nin++;      
3138       FollowProlongation(*track, i1-7,1);
3139       if (track->GetNumberOfClusters() < track->fNFoundable*0.75 || 
3140           track->fNShared>0.6*track->GetNumberOfClusters() || ( track->GetSigmaY2()+ track->GetSigmaZ2())>0.6){
3141         seed->Reset();
3142         seed->~AliTPCseed();
3143         continue;
3144       }
3145       nout1++;
3146       nout2++;  
3147       //Int_t rc = 1;
3148       FollowProlongation(*track, i2,1);
3149       track->fBConstrain =0;
3150       track->fLastPoint = i1+fInnerSec->GetNRows();  // first cluster in track position
3151       track->fFirstPoint = track->fLastPoint;
3152       
3153       if (track->GetNumberOfClusters()<(i1-i2)*0.5 || 
3154           track->GetNumberOfClusters()<track->fNFoundable*0.7 || 
3155           track->fNShared>2. || track->GetChi2()/track->GetNumberOfClusters()>6 || ( track->GetSigmaY2()+ track->GetSigmaZ2())>0.5 ) {
3156         seed->Reset();
3157         seed->~AliTPCseed();
3158         continue;
3159       }
3160    
3161       {
3162         FollowProlongation(*track, TMath::Max(i2-10,0),1);
3163         AliTPCseed * track2 = MakeSeed(track,0.2,0.5,0.9);
3164         FollowProlongation(*track2, i2,1);
3165         track2->fBConstrain = kFALSE;
3166         track2->fSeedType = 4;
3167         arr->AddLast(track2); 
3168         seed->Reset();
3169         seed->~AliTPCseed();
3170       }
3171       
3172    
3173       //arr->AddLast(track); 
3174       //seed = new AliTPCseed;  
3175       nout3++;
3176     }
3177   }
3178   
3179   if (fDebug>3){
3180     Info("MakeSeeds5","\nSeeding statiistic:\t%d\t%d\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin,nout1,nout2,nout3);
3181   }
3182   delete seed;
3183 }
3184
3185
3186 //_____________________________________________________________________________
3187 void AliTPCtrackerMI::MakeSeeds2(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2, Float_t */*cuts[4]*/,
3188                                  Float_t deltay, Bool_t /*bconstrain*/) {
3189   //-----------------------------------------------------------------
3190   // This function creates track seeds - without vertex constraint
3191   //-----------------------------------------------------------------
3192   // cuts[0]   - fP4 cut        - not applied
3193   // cuts[1]   - tan(phi)  cut
3194   // cuts[2]   - zvertex cut    - not applied 
3195   // cuts[3]   - fP3 cut
3196   Int_t nin0=0;
3197   Int_t nin1=0;
3198   Int_t nin2=0;
3199   Int_t nin3=0;
3200   //  Int_t nin4=0;
3201   //Int_t nin5=0;
3202
3203   
3204
3205   Double_t alpha=fOuterSec->GetAlpha(), shift=fOuterSec->GetAlphaShift();
3206   //  Double_t cs=cos(alpha), sn=sin(alpha);
3207   Int_t row0 = (i1+i2)/2;
3208   Int_t drow = (i1-i2)/2;
3209   const AliTPCRow& kr0=fSectors[sec][row0];
3210   AliTPCRow * kr=0;
3211
3212   AliTPCpolyTrack polytrack;
3213   Int_t nclusters=fSectors[sec][row0];
3214   AliTPCseed * seed = new AliTPCseed;
3215
3216   Int_t sumused=0;
3217   Int_t cused=0;
3218   Int_t cnused=0;
3219   for (Int_t is=0; is < nclusters; is++) {  //LOOP over clusters
3220     Int_t nfound =0;
3221     Int_t nfoundable =0;
3222     for (Int_t iter =1; iter<2; iter++){   //iterations
3223       const AliTPCRow& krm=fSectors[sec][row0-iter];
3224       const AliTPCRow& krp=fSectors[sec][row0+iter];      
3225       const AliTPCclusterMI * cl= kr0[is];
3226       
3227       if (cl->IsUsed(10)) {
3228         cused++;
3229       }
3230       else{
3231         cnused++;
3232       }
3233       Double_t x = kr0.GetX();
3234       // Initialization of the polytrack
3235       nfound =0;
3236       nfoundable =0;
3237       polytrack.Reset();
3238       //
3239       Double_t y0= cl->GetY();
3240       Double_t z0= cl->GetZ();
3241       Float_t erry = 0;
3242       Float_t errz = 0;
3243       
3244       Double_t ymax = fSectors->GetMaxY(row0)-kr0.fDeadZone-1.5;
3245       if (deltay>0 && TMath::Abs(ymax-TMath::Abs(y0))> deltay ) continue;  // seed only at the edge
3246       
3247       erry = (0.5)*cl->GetSigmaY2()/TMath::Sqrt(cl->GetQ())*6;      
3248       errz = (0.5)*cl->GetSigmaZ2()/TMath::Sqrt(cl->GetQ())*6;      
3249       polytrack.AddPoint(x,y0,z0,erry, errz);
3250
3251       sumused=0;
3252       if (cl->IsUsed(10)) sumused++;
3253
3254
3255       Float_t roady = (5*TMath::Sqrt(cl->GetSigmaY2()+0.2)+1.)*iter;
3256       Float_t roadz = (5*TMath::Sqrt(cl->GetSigmaZ2()+0.2)+1.)*iter;
3257       //
3258       x = krm.GetX();
3259       AliTPCclusterMI * cl1 = krm.FindNearest(y0,z0,roady,roadz);
3260       if (cl1 && TMath::Abs(ymax-TMath::Abs(y0))) {
3261         erry = (0.5)*cl1->GetSigmaY2()/TMath::Sqrt(cl1->GetQ())*3;          
3262         errz = (0.5)*cl1->GetSigmaZ2()/TMath::Sqrt(cl1->GetQ())*3;
3263         if (cl1->IsUsed(10))  sumused++;
3264         polytrack.AddPoint(x,cl1->GetY(),cl1->GetZ(),erry,errz);
3265       }
3266       //
3267       x = krp.GetX();
3268       AliTPCclusterMI * cl2 = krp.FindNearest(y0,z0,roady,roadz);
3269       if (cl2) {
3270         erry = (0.5)*cl2->GetSigmaY2()/TMath::Sqrt(cl2->GetQ())*3;          
3271         errz = (0.5)*cl2->GetSigmaZ2()/TMath::Sqrt(cl2->GetQ())*3;
3272         if (cl2->IsUsed(10)) sumused++;  
3273         polytrack.AddPoint(x,cl2->GetY(),cl2->GetZ(),erry,errz);
3274       }
3275       //
3276       if (sumused>0) continue;
3277       nin0++;
3278       polytrack.UpdateParameters();
3279       // follow polytrack
3280       roadz = 1.2;
3281       roady = 1.2;
3282       //
3283       Double_t yn,zn;
3284       nfoundable = polytrack.GetN();
3285       nfound     = nfoundable; 
3286       //
3287       for (Int_t ddrow = iter+1; ddrow<drow;ddrow++){
3288         Float_t maxdist = 0.8*(1.+3./(ddrow));
3289         for (Int_t delta = -1;delta<=1;delta+=2){
3290           Int_t row = row0+ddrow*delta;
3291           kr = &(fSectors[sec][row]);
3292           Double_t xn = kr->GetX();
3293           Double_t ymax = fSectors->GetMaxY(row)-kr->fDeadZone-1.5;
3294           polytrack.GetFitPoint(xn,yn,zn);
3295           if (TMath::Abs(yn)>ymax) continue;
3296           nfoundable++;
3297           AliTPCclusterMI * cln = kr->FindNearest(yn,zn,roady,roadz);
3298           if (cln) {
3299             Float_t dist =  TMath::Sqrt(  (yn-cln->GetY())*(yn-cln->GetY())+(zn-cln->GetZ())*(zn-cln->GetZ()));
3300             if (dist<maxdist){
3301               /*
3302               erry = (dist+0.3)*cln->GetSigmaY2()/TMath::Sqrt(cln->GetQ())*(1.+1./(ddrow));         
3303               errz = (dist+0.3)*cln->GetSigmaZ2()/TMath::Sqrt(cln->GetQ())*(1.+1./(ddrow));
3304               if (cln->IsUsed(10)) {
3305                 //      printf("used\n");
3306                 sumused++;
3307                 erry*=2;
3308                 errz*=2;
3309               }
3310               */
3311               erry=0.1;
3312               errz=0.1;
3313               polytrack.AddPoint(xn,cln->GetY(),cln->GetZ(),erry, errz);
3314               nfound++;
3315             }
3316           }
3317         }
3318         if ( (sumused>3) || (sumused>0.5*nfound) || (nfound<0.6*nfoundable))  break;     
3319         polytrack.UpdateParameters();
3320       }           
3321     }
3322     if ( (sumused>3) || (sumused>0.5*nfound))  {
3323       //printf("sumused   %d\n",sumused);
3324       continue;
3325     }
3326     nin1++;
3327     Double_t dy,dz;
3328     polytrack.GetFitDerivation(kr0.GetX(),dy,dz);
3329     AliTPCpolyTrack track2;
3330     
3331     polytrack.Refit(track2,0.5+TMath::Abs(dy)*0.3,0.4+TMath::Abs(dz)*0.3);
3332     if (track2.GetN()<0.5*nfoundable) continue;
3333     nin2++;
3334
3335     if ((nfound>0.6*nfoundable) &&( nfoundable>0.4*(i1-i2))) {
3336       //
3337       // test seed with and without constrain
3338       for (Int_t constrain=0; constrain<=0;constrain++){
3339         // add polytrack candidate
3340
3341         Double_t x[5], c[15];
3342         Double_t x1,x2,x3,y1,y2,y3,z1,z2,z3;
3343         track2.GetBoundaries(x3,x1);    
3344         x2 = (x1+x3)/2.;
3345         track2.GetFitPoint(x1,y1,z1);
3346         track2.GetFitPoint(x2,y2,z2);
3347         track2.GetFitPoint(x3,y3,z3);
3348         //
3349         //is track pointing to the vertex ?
3350         Double_t x0,y0,z0;
3351         x0=0;
3352         polytrack.GetFitPoint(x0,y0,z0);
3353
3354         if (constrain) {
3355           x2 = x3;
3356           y2 = y3;
3357           z2 = z3;
3358           
3359           x3 = 0;
3360           y3 = 0;
3361           z3 = 0;
3362         }
3363         x[0]=y1;
3364         x[1]=z1;
3365         x[4]=F1(x1,y1,x2,y2,x3,y3);
3366                 
3367         //      if (TMath::Abs(x[4]) >= cuts[0]) continue;  //
3368         x[2]=F2(x1,y1,x2,y2,x3,y3);
3369         
3370         //if (TMath::Abs(x[4]*x1-x[2]) >= cuts[1]) continue;
3371         //x[3]=F3(x1,y1,x2,y2,z1,z2);
3372         x[3]=F3n(x1,y1,x3,y3,z1,z3,x[4]);
3373         //if (TMath::Abs(x[3]) > cuts[3]) continue;
3374
3375         
3376         Double_t sy =0.1, sz =0.1;
3377         Double_t sy1=0.02, sz1=0.02;
3378         Double_t sy2=0.02, sz2=0.02;
3379         Double_t sy3=0.02;
3380
3381         if (constrain){
3382           sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
3383         }
3384         
3385         Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
3386         Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
3387         Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
3388         Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
3389         Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
3390         Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
3391
3392         Double_t f30=(F3(x1,y1+sy,x3,y3,z1,z3)-x[3])/sy;
3393         Double_t f31=(F3(x1,y1,x3,y3,z1+sz,z3)-x[3])/sz;
3394         Double_t f32=(F3(x1,y1,x3,y3+sy,z1,z3)-x[3])/sy;
3395         Double_t f34=(F3(x1,y1,x3,y3,z1,z3+sz)-x[3])/sz;
3396
3397         
3398         c[0]=sy1;
3399         c[1]=0.;       c[2]=sz1;
3400         c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3401         c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3402         c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3403         c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3404         c[13]=f30*sy1*f40+f32*sy2*f42;
3405         c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3406         
3407         //Int_t row1 = fSectors->GetRowNumber(x1);
3408         Int_t row1 = GetRowNumber(x1);
3409
3410         UInt_t index=0;
3411         //kr0.GetIndex(is);
3412         AliTPCseed *track=new (seed) AliTPCseed(index, x, c, x1, sec*alpha+shift);
3413         track->fIsSeeding = kTRUE;
3414         Int_t rc=FollowProlongation(*track, i2);        
3415         if (constrain) track->fBConstrain =1;
3416         else
3417           track->fBConstrain =0;
3418         track->fLastPoint = row1+fInnerSec->GetNRows();  // first cluster in track position
3419         track->fFirstPoint = track->fLastPoint;
3420
3421         if (rc==0 || track->GetNumberOfClusters()<(i1-i2)*0.5 || 
3422             track->GetNumberOfClusters() < track->fNFoundable*0.6 || 
3423             track->fNShared>0.4*track->GetNumberOfClusters()) {
3424           //delete track;
3425           seed->Reset();
3426           seed->~AliTPCseed();
3427         }
3428         else {
3429           arr->AddLast(track);
3430           seed = new AliTPCseed;
3431         }
3432         nin3++;
3433       }
3434     }  // if accepted seed
3435   }
3436   if (fDebug>3){
3437     Info("MakeSeeds2","\nSeeding statiistic:\t%d\t%d\t%d\t%d",nin0,nin1,nin2,nin3);
3438   }
3439   delete seed;
3440 }
3441
3442
3443 AliTPCseed *AliTPCtrackerMI::MakeSeed(AliTPCseed *track, Float_t r0, Float_t r1, Float_t r2)
3444 {
3445   //
3446   //
3447   //reseed using track points
3448   Int_t p0 = int(r0*track->GetNumberOfClusters());     // point 0 
3449   Int_t p1 = int(r1*track->GetNumberOfClusters());
3450   Int_t p2 = int(r2*track->GetNumberOfClusters());   // last point
3451   Int_t pp2=0;
3452   Double_t  x0[3],x1[3],x2[3];
3453   x0[0]=-1;
3454   x0[0]=-1;
3455   x0[0]=-1;
3456
3457   // find track position at given ratio of the length
3458   Int_t  sec0, sec1, sec2;
3459   sec0=0;
3460   sec1=0;
3461   sec2=0;
3462   Int_t index=-1;
3463   Int_t clindex;
3464   for (Int_t i=0;i<160;i++){
3465     if (track->fClusterPointer[i]){
3466       index++;
3467       AliTPCTrackerPoint   *trpoint =track->GetTrackPoint(i);
3468       if ( (index<p0) || x0[0]<0 ){
3469         if (trpoint->GetX()>1){
3470           clindex = track->GetClusterIndex2(i);
3471           if (clindex>0){       
3472             x0[0] = trpoint->GetX();
3473             x0[1] = trpoint->GetY();
3474             x0[2] = trpoint->GetZ();
3475             sec0  = ((clindex&0xff000000)>>24)%18;
3476           }
3477         }
3478       }
3479
3480       if ( (index<p1) &&(trpoint->GetX()>1)){
3481         clindex = track->GetClusterIndex2(i);
3482         if (clindex>0){
3483           x1[0] = trpoint->GetX();
3484           x1[1] = trpoint->GetY();
3485           x1[2] = trpoint->GetZ();
3486           sec1  = ((clindex&0xff000000)>>24)%18;
3487         }
3488       }
3489       if ( (index<p2) &&(trpoint->GetX()>1)){
3490         clindex = track->GetClusterIndex2(i);
3491         if (clindex>0){
3492           x2[0] = trpoint->GetX();
3493           x2[1] = trpoint->GetY();
3494           x2[2] = trpoint->GetZ(); 
3495           sec2  = ((clindex&0xff000000)>>24)%18;
3496           pp2 = i;
3497         }
3498       }
3499     }
3500   }
3501   
3502   Double_t alpha, cs,sn, xx2,yy2;
3503   //
3504   alpha = (sec1-sec2)*fSectors->GetAlpha();
3505   cs = TMath::Cos(alpha);
3506   sn = TMath::Sin(alpha); 
3507   xx2= x1[0]*cs-x1[1]*sn;
3508   yy2= x1[0]*sn+x1[1]*cs;
3509   x1[0] = xx2;
3510   x1[1] = yy2;
3511   //
3512   alpha = (sec0-sec2)*fSectors->GetAlpha();
3513   cs = TMath::Cos(alpha);
3514   sn = TMath::Sin(alpha); 
3515   xx2= x0[0]*cs-x0[1]*sn;
3516   yy2= x0[0]*sn+x0[1]*cs;
3517   x0[0] = xx2;
3518   x0[1] = yy2;
3519   //
3520   //
3521   //
3522   Double_t x[5],c[15];
3523   //
3524   x[0]=x2[1];
3525   x[1]=x2[2];
3526   x[4]=F1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
3527   //  if (x[4]>1) return 0;
3528   x[2]=F2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
3529   x[3]=F3n(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2],x[4]);
3530   //if (TMath::Abs(x[3]) > 2.2)  return 0;
3531   //if (TMath::Abs(x[2]) > 1.99) return 0;
3532   //  
3533   Double_t sy =0.1,  sz =0.1;
3534   //
3535   Double_t sy1=0.02+track->GetSigmaY2(), sz1=0.02+track->GetSigmaZ2();
3536   Double_t sy2=0.01+track->GetSigmaY2(), sz2=0.01+track->GetSigmaZ2();
3537   Double_t sy3=0.01+track->GetSigmaY2();
3538   //
3539   Double_t f40=(F1(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[4])/sy;
3540   Double_t f42=(F1(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[4])/sy;
3541   Double_t f43=(F1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[4])/sy;
3542   Double_t f20=(F2(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[2])/sy;
3543   Double_t f22=(F2(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[2])/sy;
3544   Double_t f23=(F2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[2])/sy;
3545   //
3546   Double_t f30=(F3(x2[0],x2[1]+sy,x0[0],x0[1],x2[2],x0[2])-x[3])/sy;
3547   Double_t f31=(F3(x2[0],x2[1],x0[0],x0[1],x2[2]+sz,x0[2])-x[3])/sz;
3548   Double_t f32=(F3(x2[0],x2[1],x0[0],x0[1]+sy,x2[2],x0[2])-x[3])/sy;
3549   Double_t f34=(F3(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2]+sz)-x[3])/sz;
3550   
3551   
3552   c[0]=sy1;
3553   c[1]=0.;       c[2]=sz1;
3554   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3555   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3556   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3557   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3558   c[13]=f30*sy1*f40+f32*sy2*f42;
3559   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3560   
3561   //  Int_t row1 = fSectors->GetRowNumber(x2[0]);
3562   AliTPCseed *seed=new  AliTPCseed(0, x, c, x2[0], sec2*fSectors->GetAlpha()+fSectors->GetAlphaShift());
3563   //  Double_t y0,z0,y1,z1, y2,z2;
3564   //seed->GetProlongation(x0[0],y0,z0);
3565   // seed->GetProlongation(x1[0],y1,z1);
3566   //seed->GetProlongation(x2[0],y2,z2);
3567   //  seed =0;
3568   seed->fLastPoint  = pp2;
3569   seed->fFirstPoint = pp2;
3570   
3571
3572   return seed;
3573 }
3574
3575
3576 AliTPCseed *AliTPCtrackerMI::ReSeed(AliTPCseed *track, Float_t r0, Float_t r1, Float_t r2)
3577 {
3578   //
3579   //
3580   //reseed using founded clusters 
3581   //
3582   // Find the number of clusters
3583   Int_t nclusters = 0;
3584   for (Int_t irow=0;irow<160;irow++){
3585     if (track->GetClusterIndex(irow)>0) nclusters++;
3586   }
3587   //
3588   Int_t ipos[3];
3589   ipos[0] = TMath::Max(int(r0*nclusters),0);             // point 0 cluster
3590   ipos[1] = TMath::Min(int(r1*nclusters),nclusters-1);   // 
3591   ipos[2] = TMath::Min(int(r2*nclusters),nclusters-1);   // last point
3592   //
3593   //
3594   Double_t  xyz[3][3];
3595   Int_t     row[3],sec[3]={0,0,0};
3596   //
3597   // find track row position at given ratio of the length
3598   Int_t index=-1;
3599   for (Int_t irow=0;irow<160;irow++){    
3600     if (track->GetClusterIndex2(irow)<0) continue;
3601     index++;
3602     for (Int_t ipoint=0;ipoint<3;ipoint++){
3603       if (index<=ipos[ipoint]) row[ipoint] = irow;
3604     }        
3605   }
3606   //
3607   //Get cluster and sector position
3608   for (Int_t ipoint=0;ipoint<3;ipoint++){
3609     Int_t clindex = track->GetClusterIndex2(row[ipoint]);
3610     AliTPCclusterMI * cl = GetClusterMI(clindex);
3611     if (cl==0) {
3612       //Error("Bug\n");
3613       //      AliTPCclusterMI * cl = GetClusterMI(clindex);
3614       return 0;
3615     }
3616     sec[ipoint]     = ((clindex&0xff000000)>>24)%18;
3617     xyz[ipoint][0]  = GetXrow(row[ipoint]);
3618     xyz[ipoint][1]  = cl->GetY();
3619     xyz[ipoint][2]  = cl->GetZ();
3620   }
3621   //
3622   //
3623   // Calculate seed state vector and covariance matrix
3624
3625   Double_t alpha, cs,sn, xx2,yy2;
3626   //
3627   alpha = (sec[1]-sec[2])*fSectors->GetAlpha();
3628   cs = TMath::Cos(alpha);
3629   sn = TMath::Sin(alpha); 
3630   xx2= xyz[1][0]*cs-xyz[1][1]*sn;
3631   yy2= xyz[1][0]*sn+xyz[1][1]*cs;
3632   xyz[1][0] = xx2;
3633   xyz[1][1] = yy2;
3634   //
3635   alpha = (sec[0]-sec[2])*fSectors->GetAlpha();
3636   cs = TMath::Cos(alpha);
3637   sn = TMath::Sin(alpha); 
3638   xx2= xyz[0][0]*cs-xyz[0][1]*sn;
3639   yy2= xyz[0][0]*sn+xyz[0][1]*cs;
3640   xyz[0][0] = xx2;
3641   xyz[0][1] = yy2;
3642   //
3643   //
3644   //
3645   Double_t x[5],c[15];
3646   //
3647   x[0]=xyz[2][1];
3648   x[1]=xyz[2][2];
3649   x[4]=F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3650   x[2]=F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
3651   x[3]=F3n(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2],x[4]);
3652   //  
3653   Double_t sy =0.1,  sz =0.1;
3654   //
3655   Double_t sy1=0.2, sz1=0.2;
3656   Double_t sy2=0.2, sz2=0.2;
3657   Double_t sy3=0.2;
3658   //
3659   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;
3660   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;
3661   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;
3662   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;
3663   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;
3664   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;
3665   //
3666   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;
3667   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;
3668   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;
3669   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;
3670   
3671   
3672   c[0]=sy1;
3673   c[1]=0.;       c[2]=sz1;
3674   c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
3675   c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
3676   c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
3677   c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
3678   c[13]=f30*sy1*f40+f32*sy2*f42;
3679   c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
3680   
3681   //  Int_t row1 = fSectors->GetRowNumber(xyz[2][0]);
3682   AliTPCseed *seed=new  AliTPCseed(0, x, c, xyz[2][0], sec[2]*fSectors->GetAlpha()+fSectors->GetAlphaShift());
3683   seed->fLastPoint  = row[2];
3684   seed->fFirstPoint = row[2];  
3685   return seed;
3686 }
3687
3688 Int_t  AliTPCtrackerMI::CheckKinkPoint(AliTPCseed*seed, Float_t th)
3689 {
3690   //
3691   //
3692   // 
3693   for (Int_t i=0;i<12;i++) seed->fKinkPoint[i]=0;
3694   //
3695   if (TMath::Abs(seed->GetC())>0.01) return 0;
3696   //
3697
3698   Float_t x[160], y[160], erry[160], z[160], errz[160];
3699   Int_t sec[160];
3700   Float_t xt[160], yt[160], zt[160];
3701   Int_t i1 = 200;
3702   Int_t i2 = 0;
3703   Int_t secm   = -1;
3704   Int_t padm   = -1;
3705   Int_t middle = seed->GetNumberOfClusters()/2;
3706   //
3707   //
3708   // find central sector, get local cooordinates
3709   Int_t count = 0;
3710   for (Int_t i=seed->fFirstPoint;i<=seed->fLastPoint;i++) {
3711     sec[i]= seed->GetClusterSector(i)%18;
3712     x[i]  = GetXrow(i);  
3713     if (sec[i]>=0) {
3714       AliTPCclusterMI * cl = seed->fClusterPointer[i];
3715       //      if (cl==0)        cl = GetClusterMI(seed->GetClusterIndex2(i));
3716       if (cl==0) {
3717         sec[i] = -1;
3718         continue;
3719       }
3720       //
3721       //
3722       if (i>i2)  i2 = i;  //last  point with cluster
3723       if (i2<i1) i1 = i;  //first point with cluster
3724       y[i] = cl->GetY();
3725       z[i] = cl->GetZ();
3726       AliTPCTrackerPoint * point = seed->GetTrackPoint(i);
3727       xt[i] = x[i];
3728       yt[i] = point->GetY();
3729       zt[i] = point->GetZ();
3730   
3731       if (point->GetX()>0){
3732         erry[i] = point->GetErrY();
3733         errz[i] = point->GetErrZ();     
3734       }
3735
3736       count++;
3737       if (count<middle) {
3738         secm = sec[i];  //central sector
3739         padm = i;       //middle point with cluster
3740       }
3741     }
3742   }
3743   //
3744   // rotate position to global coordinate system connected to  sector at last the point
3745   //
3746   for (Int_t i=i1;i<=i2;i++){
3747     //    
3748     if (sec[i]<0) continue;
3749     Double_t alpha = (sec[i2]-sec[i])*fSectors->GetAlpha();
3750     Double_t cs = TMath::Cos(alpha);
3751     Double_t sn = TMath::Sin(alpha);    
3752     Float_t xx2= x[i]*cs+y[i]*sn;
3753     Float_t yy2= -x[i]*sn+y[i]*cs;
3754     x[i] = xx2;
3755     y[i] = yy2;    
3756     //
3757     xx2= xt[i]*cs+yt[i]*sn;
3758     yy2= -xt[i]*sn+yt[i]*cs;
3759     xt[i] = xx2;
3760     yt[i] = yy2;    
3761
3762   }
3763   //get "state" vector
3764   Double_t xh[5],xm = x[padm];  
3765   xh[0]=yt[i2];
3766   xh[1]=zt[i2];
3767   xh[4]=F1(xt[i2],yt[i2],xt[padm],yt[padm],xt[i1],yt[i1]);  
3768   xh[2]=F2(xt[i2],yt[i2],xt[padm],yt[padm],xt[i1],yt[i1]);
3769   xh[3]=F3n(xt[i2],yt[i2],xt[i1],yt[i1],zt[i2],zt[i1],xh[4]);
3770   //
3771   //
3772   for (Int_t i=i1;i<=i2;i++){
3773     Double_t yy,zz;
3774     if (sec[i]<0) continue;    
3775     GetProlongation(x[i2], x[i],xh,yy,zz);
3776     if (TMath::Abs(y[i]-yy)>4||TMath::Abs(z[i]-zz)>4){
3777       //Double_t xxh[5];
3778       //xxh[4]=F1old(x[i2],y[i2],x[padm],y[padm],x[i1],y[i1]);  
3779       //xxh[2]=F2old(x[i2],y[i2],x[padm],y[padm],x[i1],y[i1]);
3780       Error("AliTPCtrackerMI::CheckKinkPoint","problem\n");
3781     }
3782     y[i] = y[i] - yy;
3783     z[i] = z[i] - zz;
3784   }
3785   Float_t dyup[160],dydown[160], dzup[160], dzdown[160];
3786   Float_t yup[160], ydown[160],  zup[160],  zdown[160];
3787  
3788   AliTPCpolyTrack ptrack1,ptrack2;
3789   //
3790   // derivation up
3791   for (Int_t i=i1;i<=i2;i++){
3792     AliTPCclusterMI * cl = seed->fClusterPointer[i];
3793     if (!cl) continue;
3794     if (cl->GetType()<0) continue;
3795     if (cl->GetType()>10) continue;
3796
3797     if (sec[i]>=0){
3798       ptrack1.AddPoint(x[i]-xm,y[i],z[i],0.1,0.1);
3799     }
3800     if (ptrack1.GetN()>4.){
3801       ptrack1.UpdateParameters();
3802       Double_t ddy,ddz;
3803       ptrack1.GetFitDerivation(x[i]-xm,ddy,ddz);
3804       Double_t yy,zz;
3805       ptrack1.GetFitPoint(x[i]-xm,yy,zz);
3806
3807       dyup[i] = ddy;
3808       dzup[i] = ddz;
3809       yup[i]  = yy;
3810       zup[i]  = zz;
3811
3812     }
3813     else{
3814       dyup[i]=0.;  //not enough points
3815     }
3816   }
3817   //
3818   // derivation down
3819   for (Int_t i=i2;i>=i1;i--){
3820     AliTPCclusterMI * cl = seed->fClusterPointer[i];
3821     if (!cl) continue;
3822     if (cl->GetType()<0) continue;
3823     if (cl->GetType()>10) continue;
3824     if (sec[i]>=0){
3825       ptrack2.AddPoint(x[i]-xm,y[i],z[i],0.1,0.1);
3826     }
3827     if (ptrack2.GetN()>4){
3828       ptrack2.UpdateParameters();
3829       Double_t ddy,ddz;
3830       ptrack2.GetFitDerivation(x[i]-xm,ddy,ddz);
3831       Double_t yy,zz;
3832       ptrack2.GetFitPoint(x[i]-xm,yy,zz);
3833
3834       dydown[i] = ddy;
3835       dzdown[i] = ddz;
3836       ydown[i]  = yy;
3837       zdown[i]  = zz;
3838     }
3839     else{
3840       dydown[i]=0.;  //not enough points
3841     }
3842   }
3843   //
3844   //
3845   // find maximal difference of the derivation
3846   for (Int_t i=0;i<12;i++) seed->fKinkPoint[i]=0;
3847
3848
3849   for (Int_t i=i1+10;i<i2-10;i++){
3850     if ( (TMath::Abs(dydown[i])<0.00000001)  ||  (TMath::Abs(dyup[i])<0.00000001) ||i<30)continue;
3851     //    printf("%f\t%f\t%f\t%f\t%f\n",x[i],dydown[i],dyup[i],dzdown[i],dzup[i]);
3852     //
3853     Float_t ddy = TMath::Abs(dydown[i]-dyup[i]);
3854     Float_t ddz = TMath::Abs(dzdown[i]-dzup[i]);    
3855     if ( (ddy+ddz)> th){
3856       seed->fKinkPoint[0] = i;
3857       seed->fKinkPoint[1] = ddy;
3858       seed->fKinkPoint[2] = ddz;
3859       th = ddy+ddz;      
3860     }
3861   }
3862
3863   if (fTreeDebug){
3864     //
3865     //write information to the debug tree
3866     TBranch * br = fTreeDebug->GetBranch("debug");
3867     TClonesArray * arr = new TClonesArray("AliTPCTrackPoint2");
3868     arr->ExpandCreateFast(i2-i1);
3869     br->SetAddress(&arr);
3870     //
3871     AliTPCclusterMI cldummy;
3872     cldummy.SetQ(0);
3873     AliTPCTrackPoint2 pdummy;
3874     pdummy.GetTPoint().fIsShared = 10;
3875     //
3876     Double_t alpha = sec[i2]*fSectors->GetAlpha();
3877     Double_t cs    = TMath::Cos(alpha);
3878     Double_t sn    = TMath::Sin(alpha);    
3879
3880     for (Int_t i=i1;i<i2;i++){
3881       AliTPCTrackPoint2 *trpoint = (AliTPCTrackPoint2*)arr->UncheckedAt(i-i1);
3882       //cluster info
3883       AliTPCclusterMI * cl0 = seed->fClusterPointer[i];
3884       //      
3885       AliTPCTrackerPoint * point = seed->GetTrackPoint(i);
3886       
3887       if (cl0){
3888         Double_t x = GetXrow(i);
3889         trpoint->GetTPoint() = *point;
3890         trpoint->GetCPoint() = *cl0;
3891         trpoint->GetCPoint().SetQ(TMath::Abs(cl0->GetQ()));
3892         trpoint->fID    = seed->GetUniqueID();
3893         trpoint->fLab   = seed->GetLabel();
3894         //
3895         trpoint->fGX =  cs *x + sn*point->GetY();
3896         trpoint->fGY = -sn *x + cs*point->GetY() ;
3897         trpoint->fGZ = point->GetZ();
3898         //
3899         trpoint->fDY = y[i];
3900         trpoint->fDZ = z[i];
3901         //
3902         trpoint->fDYU = dyup[i];
3903         trpoint->fDZU = dzup[i];
3904         //
3905         trpoint->fDYD = dydown[i];
3906         trpoint->fDZD = dzdown[i];
3907         //
3908         if (TMath::Abs(dyup[i])>0.00000000001 &&TMath::Abs(dydown[i])>0.00000000001){
3909           trpoint->fDDY = dydown[i]-dyup[i];
3910           trpoint->fDDZ = dzdown[i]-dzup[i];
3911         }else{
3912           trpoint->fDDY = 0.;
3913           trpoint->fDDZ = 0.;
3914         }       
3915       }
3916       else{
3917         *trpoint = pdummy;
3918         trpoint->GetCPoint()= cldummy;
3919         trpoint->fID = -1;
3920       }
3921       //     
3922     }
3923     fTreeDebug->Fill();
3924   }
3925   
3926   
3927   return 0;
3928   
3929 }
3930
3931
3932
3933
3934
3935 AliTPCseed*  AliTPCtrackerMI::ReSeed(AliTPCseed *t)
3936 {
3937   //
3938   // reseed - refit -  track
3939   //
3940   Int_t first = 0;
3941   //  Int_t last  = fSectors->GetNRows()-1;
3942   //
3943   if (fSectors == fOuterSec){
3944     first = TMath::Max(first, t->fFirstPoint-fInnerSec->GetNRows());
3945     //last  = 
3946   }
3947   else
3948     first = t->fFirstPoint;
3949   //
3950   AliTPCseed * seed = MakeSeed(t,0.1,0.5,0.9);
3951   FollowBackProlongation(*t,fSectors->GetNRows()-1);
3952   t->Reset(kFALSE);
3953   FollowProlongation(*t,first);
3954   return seed;
3955 }
3956
3957
3958
3959
3960
3961
3962
3963 //_____________________________________________________________________________
3964 Int_t AliTPCtrackerMI::ReadSeeds(const TFile *inp) {
3965   //-----------------------------------------------------------------
3966   // This function reades track seeds.
3967   //-----------------------------------------------------------------
3968   TDirectory *savedir=gDirectory; 
3969
3970   TFile *in=(TFile*)inp;
3971   if (!in->IsOpen()) {
3972      cerr<<"AliTPCtrackerMI::ReadSeeds(): input file is not open !\n";
3973      return 1;
3974   }
3975
3976   in->cd();
3977   TTree *seedTree=(TTree*)in->Get("Seeds");
3978   if (!seedTree) {
3979      cerr<<"AliTPCtrackerMI::ReadSeeds(): ";
3980      cerr<<"can't get a tree with track seeds !\n";
3981      return 2;
3982   }
3983   AliTPCtrack *seed=new AliTPCtrack; 
3984   seedTree->SetBranchAddress("tracks",&seed);
3985   
3986   if (fSeeds==0) fSeeds=new TObjArray(15000);
3987
3988   Int_t n=(Int_t)seedTree->GetEntries();
3989   for (Int_t i=0; i<n; i++) {
3990      seedTree->GetEvent(i);
3991      fSeeds->AddLast(new AliTPCseed(*seed,seed->GetAlpha()));
3992   }
3993   
3994   delete seed;
3995   delete seedTree; 
3996   savedir->cd();
3997   return 0;
3998 }
3999
4000 Int_t AliTPCtrackerMI::Clusters2Tracks (AliESD *esd)
4001 {
4002   //
4003   if (fSeeds) DeleteSeeds();
4004   fEvent = esd;
4005   Clusters2Tracks();
4006   if (!fSeeds) return 1;
4007   FillESD(fSeeds);
4008   return 0;
4009   //
4010 }
4011
4012
4013 //_____________________________________________________________________________
4014 Int_t AliTPCtrackerMI::Clusters2Tracks() {
4015   //-----------------------------------------------------------------
4016   // This is a track finder.
4017   //-----------------------------------------------------------------
4018   TDirectory *savedir=gDirectory; 
4019   TStopwatch timer;
4020
4021   fIteration = 0;
4022   fSeeds = Tracking();
4023
4024   if (fDebug>0){
4025     Info("Clusters2Tracks","Time for tracking: \t");timer.Print();timer.Start();
4026   }
4027   //activate again some tracks
4028   for (Int_t i=0; i<fSeeds->GetEntriesFast(); i++) {
4029     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
4030     if (!pt) continue;    
4031     Int_t nc=t.GetNumberOfClusters();
4032     if (nc<20) {
4033       delete fSeeds->RemoveAt(i);
4034       continue;
4035     }
4036     if (pt->fRemoval==10) {
4037       if (pt->GetDensityFirst(20)>0.8 || pt->GetDensityFirst(30)>0.8 || pt->GetDensityFirst(40)>0.7)
4038         pt->Desactivate(10);  // make track again active
4039       else{
4040         pt->Desactivate(20);    
4041         delete fSeeds->RemoveAt(i);
4042       }
4043     } 
4044   }
4045   RemoveDouble(fSeeds,0.2,0.6,11);
4046   RemoveUsed(fSeeds,0.5,0.5,6);
4047
4048   //
4049   Int_t nseed=fSeeds->GetEntriesFast();
4050   Int_t found = 0;
4051   for (Int_t i=0; i<nseed; i++) {
4052     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
4053     if (!pt) continue;    
4054     Int_t nc=t.GetNumberOfClusters();
4055     if (nc<15) {
4056       delete fSeeds->RemoveAt(i);
4057       continue;
4058     }
4059     CookLabel(pt,0.1); //For comparison only
4060     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
4061     if ((pt->IsActive() || (pt->fRemoval==10) )){
4062       found++;      
4063       if (fDebug>0) cerr<<found<<'\r';      
4064       pt->fLab2 = i;
4065     }
4066     else
4067       delete fSeeds->RemoveAt(i);
4068   }
4069
4070   
4071   //RemoveOverlap(fSeeds,0.99,7,kTRUE);  
4072   SignShared(fSeeds);  
4073   //RemoveUsed(fSeeds,0.9,0.9,6);
4074   // 
4075   nseed=fSeeds->GetEntriesFast();
4076   found = 0;
4077   for (Int_t i=0; i<nseed; i++) {
4078     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
4079     if (!pt) continue;    
4080     Int_t nc=t.GetNumberOfClusters();
4081     if (nc<15) {
4082       delete fSeeds->RemoveAt(i);
4083       continue;
4084     }
4085     t.SetUniqueID(i);
4086     t.CookdEdx(0.02,0.6);
4087     //    CheckKinkPoint(&t,0.05);
4088     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
4089     if ((pt->IsActive() || (pt->fRemoval==10) )){
4090       found++;
4091       if (fDebug>0){
4092         cerr<<found<<'\r';      
4093       }
4094       pt->fLab2 = i;
4095     }
4096     else
4097       delete fSeeds->RemoveAt(i);
4098     //AliTPCseed * seed1 = ReSeed(pt,0.05,0.5,1);
4099     //if (seed1){
4100     //  FollowProlongation(*seed1,0);
4101     //  Int_t n = seed1->GetNumberOfClusters();
4102     //  printf("fP4\t%f\t%f\n",seed1->GetC(),pt->GetC());
4103     //  printf("fN\t%d\t%d\n", seed1->GetNumberOfClusters(),pt->GetNumberOfClusters());
4104     //
4105     //}
4106     //AliTPCseed * seed2 = ReSeed(pt,0.95,0.5,0.05);
4107     
4108   }
4109
4110   SortTracks(fSeeds, 1);
4111   
4112   /*    
4113   fIteration = 1;
4114   PrepareForBackProlongation(fSeeds,5.);
4115   PropagateBack(fSeeds);
4116   printf("Time for back propagation: \t");timer.Print();timer.Start();
4117   
4118   fIteration = 2;
4119   
4120   PrepareForProlongation(fSeeds,5.);
4121   PropagateForward2(fSeeds);
4122    
4123   printf("Time for FORWARD propagation: \t");timer.Print();timer.Start();
4124   // RemoveUsed(fSeeds,0.7,0.7,6);
4125   //RemoveOverlap(fSeeds,0.9,7,kTRUE);
4126    
4127   nseed=fSeeds->GetEntriesFast();
4128   found = 0;
4129   for (Int_t i=0; i<nseed; i++) {
4130     AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i), &t=*pt;    
4131     if (!pt) continue;    
4132     Int_t nc=t.GetNumberOfClusters();
4133     if (nc<15) {
4134       delete fSeeds->RemoveAt(i);
4135       continue;
4136     }
4137     t.CookdEdx(0.02,0.6);
4138     //    CookLabel(pt,0.1); //For comparison only
4139     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
4140     if ((pt->IsActive() || (pt->fRemoval==10) )){
4141       cerr<<found++<<'\r';      
4142     }
4143     else
4144       delete fSeeds->RemoveAt(i);
4145     pt->fLab2 = i;
4146   }
4147   */
4148  
4149   //  fNTracks = found;
4150   if (fDebug>0){
4151     Info("Clusters2Tracks","Time for overlap removal, track writing and dedx cooking: \t"); timer.Print();timer.Start();
4152   }
4153   //
4154   //  cerr<<"Number of found tracks : "<<"\t"<<found<<endl;  
4155   Info("Clusters2Tracks","Number of found tracks %d",found);  
4156   savedir->cd();
4157   //  UnloadClusters();
4158   //  
4159   return 0;
4160 }
4161
4162 void AliTPCtrackerMI::Tracking(TObjArray * arr)
4163 {
4164   //
4165   // tracking of the seeds
4166   //
4167
4168   fSectors = fOuterSec;
4169   ParallelTracking(arr,150,63);
4170   fSectors = fOuterSec;
4171   ParallelTracking(arr,63,0);
4172 }
4173
4174 TObjArray * AliTPCtrackerMI::Tracking(Int_t seedtype, Int_t i1, Int_t i2, Float_t cuts[4], Float_t dy, Int_t dsec)
4175 {
4176   //
4177   //
4178   //tracking routine
4179   TObjArray * arr = new TObjArray;
4180   // 
4181   fSectors = fOuterSec;
4182   TStopwatch timer;
4183   timer.Start();
4184   for (Int_t sec=0;sec<fkNOS;sec++){
4185     if (seedtype==3) MakeSeeds3(arr,sec,i1,i2,cuts,dy, dsec);
4186     if (seedtype==4) MakeSeeds5(arr,sec,i1,i2,cuts,dy);    
4187     if (seedtype==2) MakeSeeds2(arr,sec,i1,i2,cuts,dy);
4188   }
4189   if (fDebug>0){
4190     Info("Tracking","\nSeeding - %d\t%d\t%d\t%d\n",seedtype,i1,i2,arr->GetEntriesFast());
4191     timer.Print();
4192     timer.Start();
4193   }
4194   Tracking(arr);  
4195   if (fDebug>0){
4196     timer.Print();
4197   }
4198
4199   return arr;
4200 }
4201
4202 TObjArray * AliTPCtrackerMI::Tracking()
4203 {
4204   //
4205   //
4206   TStopwatch timer;
4207   timer.Start();
4208   Int_t nup=fOuterSec->GetNRows()+fInnerSec->GetNRows();
4209
4210   TObjArray * seeds = new TObjArray;
4211   TObjArray * arr=0;
4212   
4213   Int_t gap =20;
4214   Float_t cuts[4];
4215   cuts[0] = 0.002;
4216   cuts[1] = 1.5;
4217   cuts[2] = 3.;
4218   cuts[3] = 3.;
4219   Float_t fnumber  = 3.0;
4220   Float_t fdensity = 3.0;
4221   
4222   //  
4223   //find primaries  
4224   cuts[0]=0.0066;
4225   for (Int_t delta = 0; delta<18; delta+=6){
4226     //
4227     cuts[0]=0.0070;
4228     cuts[1] = 1.5;
4229     arr = Tracking(3,nup-1-delta,nup-1-delta-gap,cuts,-1,1);
4230     SumTracks(seeds,arr);   
4231     SignClusters(seeds,fnumber,fdensity); 
4232     //
4233     for (Int_t i=2;i<6;i+=2){
4234       // seed high pt tracks
4235       cuts[0]=0.0022;
4236       cuts[1]=0.3;
4237       arr = Tracking(3,nup-i-delta,nup-i-delta-gap,cuts,-1,0);
4238       SumTracks(seeds,arr);   
4239       SignClusters(seeds,fnumber,fdensity);        
4240     }
4241   }
4242   fnumber  = 4;
4243   fdensity = 4.;
4244   //  RemoveUsed(seeds,0.9,0.9,1);
4245   //  UnsignClusters();
4246   //  SignClusters(seeds,fnumber,fdensity);    
4247
4248   //find primaries  
4249   cuts[0]=0.0077;
4250   for (Int_t delta = 20; delta<120; delta+=10){
4251     //
4252     // seed high pt tracks
4253     cuts[0]=0.0060;
4254     cuts[1]=0.3;
4255     cuts[2]=6.;
4256     arr = Tracking(3,nup-delta,nup-delta-gap,cuts,-1);
4257     SumTracks(seeds,arr);   
4258     SignClusters(seeds,fnumber,fdensity);            
4259
4260     cuts[0]=0.003;
4261     cuts[1]=0.3;
4262     cuts[2]=6.;
4263     arr = Tracking(3,nup-delta-5,nup-delta-5-gap,cuts,-1);
4264     SumTracks(seeds,arr);   
4265     SignClusters(seeds,fnumber,fdensity);            
4266   }
4267
4268   cuts[0] = 0.01;
4269   cuts[1] = 2.0;
4270   cuts[2] = 3.;
4271   cuts[3] = 2.0;
4272   fnumber  = 2.;
4273   fdensity = 2.;
4274   
4275   if (fDebug>0){
4276     Info("Tracking()","\n\nPrimary seeding\t%d\n\n",seeds->GetEntriesFast());
4277     timer.Print();
4278     timer.Start();
4279   }
4280   //  RemoveUsed(seeds,0.75,0.75,1);
4281   //UnsignClusters();
4282   //SignClusters(seeds,fnumber,fdensity);
4283   
4284   // find secondaries
4285
4286   cuts[0] = 0.3;
4287   cuts[1] = 1.5;
4288   cuts[2] = 3.;
4289   cuts[3] = 1.5;
4290
4291   arr = Tracking(4,nup-1,nup-1-gap,cuts,-1);
4292   SumTracks(seeds,arr);   
4293   SignClusters(seeds,fnumber,fdensity);   
4294   //
4295   arr = Tracking(4,nup-2,nup-2-gap,cuts,-1);
4296   SumTracks(seeds,arr);   
4297   SignClusters(seeds,fnumber,fdensity);   
4298   //
4299   arr = Tracking(4,nup-3,nup-3-gap,cuts,-1);
4300   SumTracks(seeds,arr);   
4301   SignClusters(seeds,fnumber,fdensity);   
4302   //
4303
4304
4305   for (Int_t delta = 3; delta<30; delta+=5){
4306     //
4307     cuts[0] = 0.3;
4308     cuts[1] = 1.5;
4309     cuts[2] = 3.;
4310     cuts[3] = 1.5;
4311     arr = Tracking(4,nup-1-delta,nup-1-delta-gap,cuts,-1);
4312     SumTracks(seeds,arr);   
4313     SignClusters(seeds,fnumber,fdensity);   
4314     //
4315     arr = Tracking(4,nup-3-delta,nup-5-delta-gap,cuts,4);
4316     SumTracks(seeds,arr);   
4317     SignClusters(seeds,fnumber,fdensity); 
4318     //
4319   } 
4320   fnumber  = 1;
4321   fdensity = 1;
4322   //
4323   // change cuts
4324   fnumber  = 2.;
4325   fdensity = 2.;
4326   cuts[0]=0.0080;
4327
4328   // find secondaries
4329   for (Int_t delta = 30; delta<70; delta+=10){
4330     //
4331     cuts[0] = 0.3;
4332     cuts[1] = 1.5;
4333     cuts[2] = 3.;
4334     cuts[3] = 1.5;
4335     arr = Tracking(4,nup-1-delta,nup-1-delta-gap,cuts,-1);
4336     SumTracks(seeds,arr);   
4337     SignClusters(seeds,fnumber,fdensity);   
4338     //
4339     arr = Tracking(4,nup-5-delta,nup-5-delta-gap,cuts,5 );
4340     SumTracks(seeds,arr);   
4341     SignClusters(seeds,fnumber,fdensity);   
4342   }
4343  
4344   if (fDebug>0){
4345     Info("Tracking()","\n\nSecondary seeding\t%d\n\n",seeds->GetEntriesFast());
4346     timer.Print();
4347     timer.Start();
4348   }
4349
4350   return seeds;
4351   //
4352       
4353 }
4354
4355
4356 void AliTPCtrackerMI::SumTracks(TObjArray *arr1,TObjArray *arr2) const
4357 {
4358   //
4359   //sum tracks to common container
4360   //remove suspicious tracks
4361   Int_t nseed = arr2->GetEntriesFast();
4362   for (Int_t i=0;i<nseed;i++){
4363     AliTPCseed *pt=(AliTPCseed*)arr2->UncheckedAt(i);    
4364     if (pt){
4365       
4366       // NORMAL ACTIVE TRACK
4367       if (pt->IsActive()){
4368         arr1->AddLast(arr2->RemoveAt(i));
4369         continue;
4370       }
4371       //remove not usable tracks
4372       if (pt->fRemoval!=10){
4373         delete arr2->RemoveAt(i);
4374         continue;
4375       }
4376       // REMOVE VERY SHORT  TRACKS
4377       if (pt->GetNumberOfClusters()<20){ 
4378         delete arr2->RemoveAt(i);
4379         continue;
4380       }
4381       // ENABLE ONLY ENOUGH GOOD STOPPED TRACKS
4382       if (pt->GetDensityFirst(20)>0.8 || pt->GetDensityFirst(30)>0.8 || pt->GetDensityFirst(40)>0.7)
4383         arr1->AddLast(arr2->RemoveAt(i));
4384       else{      
4385         delete arr2->RemoveAt(i);
4386       }
4387     }
4388   }
4389   delete arr2;  
4390 }
4391
4392
4393
4394 void  AliTPCtrackerMI::ParallelTracking(TObjArray * arr, Int_t rfirst, Int_t rlast)
4395 {
4396   //
4397   // try to track in parralel
4398
4399   Int_t nseed=arr->GetEntriesFast();
4400   //prepare seeds for tracking
4401   for (Int_t i=0; i<nseed; i++) {
4402     AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i), &t=*pt; 
4403     if (!pt) continue;
4404     if (!t.IsActive()) continue;
4405     // follow prolongation to the first layer
4406     if ( (fSectors ==fInnerSec) || (t.fFirstPoint-fParam->GetNRowLow()>rfirst+1) )  
4407       FollowProlongation(t, rfirst+1);
4408   }
4409
4410
4411   //
4412   for (Int_t nr=rfirst; nr>=rlast; nr--){ 
4413     if (nr<fInnerSec->GetNRows()) 
4414       fSectors = fInnerSec;
4415     else
4416       fSectors = fOuterSec;
4417     // make indexes with the cluster tracks for given       
4418
4419     // find nearest cluster
4420     for (Int_t i=0; i<nseed; i++) {
4421       AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i), &t=*pt;       
4422       if (!pt) continue;
4423       if (!pt->IsActive()) continue;
4424       //      if ( (fSectors ==fOuterSec) && (pt->fFirstPoint-fParam->GetNRowLow())<nr) continue;
4425       if (pt->fRelativeSector>17) {
4426         continue;
4427       }
4428       UpdateClusters(t,nr);
4429     }
4430     // prolonagate to the nearest cluster - if founded
4431     for (Int_t i=0; i<nseed; i++) {
4432       AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i); 
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       FollowToNextCluster(*pt,nr);
4440     }
4441   }    
4442 }
4443
4444 void AliTPCtrackerMI::PrepareForBackProlongation(TObjArray * arr,Float_t fac) const
4445 {
4446   //
4447   //
4448   // if we use TPC track itself we have to "update" covariance
4449   //
4450   Int_t nseed= arr->GetEntriesFast();
4451   for (Int_t i=0;i<nseed;i++){
4452     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
4453     if (pt) {
4454       pt->Modify(fac);
4455       //
4456       //rotate to current local system at first accepted  point    
4457       Int_t index  = pt->GetClusterIndex2(pt->fFirstPoint); 
4458       Int_t sec    = (index&0xff000000)>>24;
4459       sec = sec%18;
4460       Float_t angle1 = fInnerSec->GetAlpha()*sec+fInnerSec->GetAlphaShift();
4461       if (angle1>TMath::Pi()) 
4462         angle1-=2.*TMath::Pi();
4463       Float_t angle2 = pt->GetAlpha();
4464       
4465       if (TMath::Abs(angle1-angle2)>0.001){
4466         pt->Rotate(angle1-angle2);
4467         //angle2 = pt->GetAlpha();
4468         //pt->fRelativeSector = pt->GetAlpha()/fInnerSec->GetAlpha();
4469         //if (pt->GetAlpha()<0) 
4470         //  pt->fRelativeSector+=18;
4471         //sec = pt->fRelativeSector;
4472       }
4473         
4474     }
4475     
4476   }
4477
4478
4479 }
4480 void AliTPCtrackerMI::PrepareForProlongation(TObjArray * arr, Float_t fac) const
4481 {
4482   //
4483   //
4484   // if we use TPC track itself we have to "update" covariance
4485   //
4486   Int_t nseed= arr->GetEntriesFast();
4487   for (Int_t i=0;i<nseed;i++){
4488     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
4489     if (pt) {
4490       pt->Modify(fac);
4491       pt->fFirstPoint = pt->fLastPoint; 
4492     }
4493     
4494   }
4495
4496
4497 }
4498
4499 Int_t AliTPCtrackerMI::PropagateBack(TObjArray * arr)
4500 {
4501   //
4502   // make back propagation
4503   //
4504   Int_t nseed= arr->GetEntriesFast();
4505   for (Int_t i=0;i<nseed;i++){
4506     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
4507     if (pt) { 
4508       //AliTPCseed *pt2 = new AliTPCseed(*pt);
4509       fSectors = fInnerSec;
4510       //FollowBackProlongation(*pt,fInnerSec->GetNRows()-1);
4511       //fSectors = fOuterSec;
4512       FollowBackProlongation(*pt,fInnerSec->GetNRows()+fOuterSec->GetNRows()-1);     
4513       //if (pt->GetNumberOfClusters()<(pt->fEsd->GetTPCclusters(0)) ){
4514       //        Error("PropagateBack","Not prolonged track %d",pt->GetLabel());
4515       //        FollowBackProlongation(*pt2,fInnerSec->GetNRows()+fOuterSec->GetNRows()-1);
4516       //}
4517     }      
4518   }
4519   return 0;
4520 }
4521
4522
4523 Int_t AliTPCtrackerMI::PropagateForward2(TObjArray * arr)
4524 {
4525   //
4526   // make forward propagation
4527   //
4528   Int_t nseed= arr->GetEntriesFast();
4529   //
4530   for (Int_t i=0;i<nseed;i++){
4531     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
4532     if (pt) { 
4533       FollowProlongation(*pt,0);
4534     }
4535   }
4536   return 0;
4537 }
4538
4539
4540 Int_t AliTPCtrackerMI::PropagateForward()
4541 {
4542   //
4543   // propagate track forward
4544   //UnsignClusters();
4545   Int_t nseed = fSeeds->GetEntriesFast();
4546   for (Int_t i=0;i<nseed;i++){
4547     AliTPCseed *pt = (AliTPCseed*)fSeeds->UncheckedAt(i);
4548     if (pt){
4549       AliTPCseed &t = *pt;
4550       Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
4551       if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
4552       if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
4553       t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
4554     }
4555   }
4556   
4557   fSectors = fOuterSec;
4558   ParallelTracking(fSeeds,fOuterSec->GetNRows()+fInnerSec->GetNRows()-1,fInnerSec->GetNRows());
4559   fSectors = fInnerSec;
4560   ParallelTracking(fSeeds,fInnerSec->GetNRows()-1,0);
4561   //WriteTracks();
4562   return 1;
4563 }
4564
4565
4566
4567
4568
4569
4570 Int_t AliTPCtrackerMI::PropagateBack(AliTPCseed * pt, Int_t row0, Int_t row1)
4571 {
4572   //
4573   // make back propagation, in between row0 and row1
4574   //
4575   
4576   if (pt) { 
4577     fSectors = fInnerSec;
4578     Int_t  r1;
4579     //
4580     if (row1<fSectors->GetNRows()) 
4581       r1 = row1;
4582     else 
4583       r1 = fSectors->GetNRows()-1;
4584
4585     if (row0<fSectors->GetNRows()&& r1>0 )
4586       FollowBackProlongation(*pt,r1);
4587     if (row1<=fSectors->GetNRows())
4588       return 0;
4589     //
4590     r1 = row1 - fSectors->GetNRows();
4591     if (r1<=0) return 0;
4592     if (r1>=fOuterSec->GetNRows()) return 0;
4593     fSectors = fOuterSec;
4594     return FollowBackProlongation(*pt,r1);
4595   }        
4596   return 0;
4597 }
4598
4599
4600
4601
4602 void  AliTPCtrackerMI::GetShape(AliTPCseed * seed, Int_t row)
4603 {
4604   //
4605   //
4606   Float_t sd2 = TMath::Abs((fParam->GetZLength()-TMath::Abs(seed->GetZ())))*fParam->GetDiffL()*fParam->GetDiffL();
4607   //  Float_t padlength =  fParam->GetPadPitchLength(seed->fSector);
4608   Float_t padlength =  GetPadPitchLength(row);
4609   //
4610   Float_t sresy = (seed->fSector < fParam->GetNSector()/2) ? 0.2 :0.3;
4611   Float_t angulary  = seed->GetSnp();
4612   angulary = angulary*angulary/(1-angulary*angulary);
4613   seed->fCurrentSigmaY2 = sd2+padlength*padlength*angulary/12.+sresy*sresy;  
4614   //
4615   Float_t sresz = fParam->GetZSigma();
4616   Float_t angularz  = seed->GetTgl();
4617   seed->fCurrentSigmaZ2 = sd2+padlength*padlength*angularz*angularz*(1+angulary)/12.+sresz*sresz;
4618   /*
4619   Float_t wy = GetSigmaY(seed);
4620   Float_t wz = GetSigmaZ(seed);
4621   wy*=wy;
4622   wz*=wz;
4623   if (TMath::Abs(wy/seed->fCurrentSigmaY2-1)>0.0001 || TMath::Abs(wz/seed->fCurrentSigmaZ2-1)>0.0001 ){
4624     printf("problem\n");
4625   }
4626   */
4627 }
4628
4629
4630 Float_t  AliTPCtrackerMI::GetSigmaY(AliTPCseed * seed)
4631 {
4632   //
4633   //  
4634   Float_t sd2 = TMath::Abs((fParam->GetZLength()-TMath::Abs(seed->GetZ())))*fParam->GetDiffL()*fParam->GetDiffL();
4635   Float_t padlength =  fParam->GetPadPitchLength(seed->fSector);
4636   Float_t sres = (seed->fSector < fParam->GetNSector()/2) ? 0.2 :0.3;
4637   Float_t angular  = seed->GetSnp();
4638   angular = angular*angular/(1-angular*angular);
4639   //  angular*=angular;
4640   //angular  = TMath::Sqrt(angular/(1-angular));
4641   Float_t res = TMath::Sqrt(sd2+padlength*padlength*angular/12.+sres*sres);
4642   return res;
4643 }
4644 Float_t  AliTPCtrackerMI::GetSigmaZ(AliTPCseed * seed)
4645 {
4646   //
4647   //
4648   Float_t sd2 = TMath::Abs((fParam->GetZLength()-TMath::Abs(seed->GetZ())))*fParam->GetDiffL()*fParam->GetDiffL();
4649   Float_t padlength =  fParam->GetPadPitchLength(seed->fSector);
4650   Float_t sres = fParam->GetZSigma();
4651   Float_t angular  = seed->GetTgl();
4652   Float_t res = TMath::Sqrt(sd2+padlength*padlength*angular*angular/12.+sres*sres);
4653   return res;
4654 }
4655
4656
4657
4658
4659 //__________________________________________________________________________
4660 void AliTPCtrackerMI::CookLabel(AliTPCseed *t, Float_t wrong) const {
4661   //--------------------------------------------------------------------
4662   //This function "cooks" a track label. If label<0, this track is fake.
4663   //--------------------------------------------------------------------
4664   Int_t noc=t->GetNumberOfClusters();
4665   if (noc<10){
4666     //printf("\nnot founded prolongation\n\n\n");
4667     //t->Dump();
4668     return ;
4669   }
4670   Int_t lb[160];
4671   Int_t mx[160];
4672   AliTPCclusterMI *clusters[160];
4673   //
4674   for (Int_t i=0;i<160;i++) {
4675     clusters[i]=0;
4676     lb[i]=mx[i]=0;
4677   }
4678
4679   Int_t i;
4680   Int_t current=0;
4681   for (i=0; i<160 && current<noc; i++) {
4682      
4683      Int_t index=t->GetClusterIndex2(i);
4684      if (index<=0) continue; 
4685      if (index&0x8000) continue;
4686      //     
4687      //clusters[current]=GetClusterMI(index);
4688      if (t->fClusterPointer[i]){
4689        clusters[current]=t->fClusterPointer[i];     
4690        current++;
4691      }
4692   }
4693   noc = current;
4694
4695   Int_t lab=123456789;
4696   for (i=0; i<noc; i++) {
4697     AliTPCclusterMI *c=clusters[i];
4698     if (!c) continue;
4699     lab=TMath::Abs(c->GetLabel(0));
4700     Int_t j;
4701     for (j=0; j<noc; j++) if (lb[j]==lab || mx[j]==0) break;
4702     lb[j]=lab;
4703     (mx[j])++;
4704   }
4705
4706   Int_t max=0;
4707   for (i=0; i<noc; i++) if (mx[i]>max) {max=mx[i]; lab=lb[i];}
4708     
4709   for (i=0; i<noc; i++) {
4710     AliTPCclusterMI *c=clusters[i]; 
4711     if (!c) continue;
4712     if (TMath::Abs(c->GetLabel(1)) == lab ||
4713         TMath::Abs(c->GetLabel(2)) == lab ) max++;
4714   }
4715
4716   if ((1.- Float_t(max)/noc) > wrong) lab=-lab;
4717
4718   else {
4719      Int_t tail=Int_t(0.10*noc);
4720      max=0;
4721      Int_t ind=0;
4722      for (i=1; i<=160&&ind<tail; i++) {
4723        //       AliTPCclusterMI *c=clusters[noc-i];
4724        AliTPCclusterMI *c=clusters[i];
4725        if (!c) continue;
4726        if (lab == TMath::Abs(c->GetLabel(0)) ||
4727            lab == TMath::Abs(c->GetLabel(1)) ||
4728            lab == TMath::Abs(c->GetLabel(2))) max++;
4729        ind++;
4730      }
4731      if (max < Int_t(0.5*tail)) lab=-lab;
4732   }
4733
4734   t->SetLabel(lab);
4735
4736   //  delete[] lb;
4737   //delete[] mx;
4738   //delete[] clusters;
4739 }
4740
4741
4742 Int_t  AliTPCtrackerMI::AliTPCSector::GetRowNumber(Double_t x) const 
4743 {
4744   //return pad row number for this x
4745   Double_t r;
4746   if (fN < 64){
4747     r=fRow[fN-1].GetX();
4748     if (x > r) return fN;
4749     r=fRow[0].GetX();
4750     if (x < r) return -1;
4751     return Int_t((x-r)/fPadPitchLength + 0.5);}
4752   else{    
4753     r=fRow[fN-1].GetX();
4754     if (x > r) return fN;
4755     r=fRow[0].GetX();
4756     if (x < r) return -1;
4757     Double_t r1=fRow[64].GetX();
4758     if(x<r1){       
4759       return Int_t((x-r)/f1PadPitchLength + 0.5);}
4760     else{
4761       return (Int_t((x-r1)/f2PadPitchLength + 0.5)+64);} 
4762   }
4763 }
4764
4765 //_________________________________________________________________________
4766 void AliTPCtrackerMI::AliTPCSector::Setup(const AliTPCParam *par, Int_t f) {
4767   //-----------------------------------------------------------------------
4768   // Setup inner sector
4769   //-----------------------------------------------------------------------
4770   if (f==0) {
4771      fAlpha=par->GetInnerAngle();
4772      fAlphaShift=par->GetInnerAngleShift();
4773      fPadPitchWidth=par->GetInnerPadPitchWidth();
4774      fPadPitchLength=par->GetInnerPadPitchLength();
4775      fN=par->GetNRowLow();
4776      fRow=new AliTPCRow[fN];
4777      for (Int_t i=0; i<fN; i++) {
4778        fRow[i].SetX(par->GetPadRowRadiiLow(i));
4779        fRow[i].fDeadZone =1.5;  //1.5 cm of dead zone
4780      }
4781   } else {
4782      fAlpha=par->GetOuterAngle();
4783      fAlphaShift=par->GetOuterAngleShift();
4784      fPadPitchWidth  = par->GetOuterPadPitchWidth();
4785      fPadPitchLength = par->GetOuter1PadPitchLength();
4786      f1PadPitchLength = par->GetOuter1PadPitchLength();
4787      f2PadPitchLength = par->GetOuter2PadPitchLength();
4788
4789      fN=par->GetNRowUp();
4790      fRow=new AliTPCRow[fN];
4791      for (Int_t i=0; i<fN; i++) {
4792        fRow[i].SetX(par->GetPadRowRadiiUp(i)); 
4793        fRow[i].fDeadZone =1.5;  // 1.5 cm of dead zone
4794      }
4795   } 
4796 }
4797
4798 AliTPCtrackerMI::AliTPCRow::AliTPCRow() {
4799   //
4800   // default constructor
4801   fN=0;
4802   fN1=0;
4803   fN2=0;
4804   fClusters1=0;
4805   fClusters2=0;
4806 }
4807
4808 AliTPCtrackerMI::AliTPCRow::~AliTPCRow(){
4809   //
4810
4811 }
4812
4813
4814
4815 //_________________________________________________________________________
4816 void 
4817 AliTPCtrackerMI::AliTPCRow::InsertCluster(const AliTPCclusterMI* c, UInt_t index) {
4818   //-----------------------------------------------------------------------
4819   // Insert a cluster into this pad row in accordence with its y-coordinate
4820   //-----------------------------------------------------------------------
4821   if (fN==kMaxClusterPerRow) {
4822     cerr<<"AliTPCRow::InsertCluster(): Too many clusters !\n"; return;
4823   }
4824   if (fN==0) {fIndex[0]=index; fClusters[fN++]=c; return;}
4825   Int_t i=Find(c->GetZ());
4826   memmove(fClusters+i+1 ,fClusters+i,(fN-i)*sizeof(AliTPCclusterMI*));
4827   memmove(fIndex   +i+1 ,fIndex   +i,(fN-i)*sizeof(UInt_t));
4828   fIndex[i]=index; fClusters[i]=c; fN++;
4829 }
4830
4831 void AliTPCtrackerMI::AliTPCRow::ResetClusters() {
4832    //
4833    // reset clusters
4834    fN  = 0; 
4835    fN1 = 0;
4836    fN2 = 0;
4837    //delete[] fClusterArray; 
4838    if (fClusters1) delete []fClusters1; 
4839    if (fClusters2) delete []fClusters2; 
4840    //fClusterArray=0;
4841    fClusters1 = 0;
4842    fClusters2 = 0;
4843 }
4844
4845
4846 //___________________________________________________________________
4847 Int_t AliTPCtrackerMI::AliTPCRow::Find(Double_t z) const {
4848   //-----------------------------------------------------------------------
4849   // Return the index of the nearest cluster 
4850   //-----------------------------------------------------------------------
4851   if (fN==0) return 0;
4852   if (z <= fClusters[0]->GetZ()) return 0;
4853   if (z > fClusters[fN-1]->GetZ()) return fN;
4854   Int_t b=0, e=fN-1, m=(b+e)/2;
4855   for (; b<e; m=(b+e)/2) {
4856     if (z > fClusters[m]->GetZ()) b=m+1;
4857     else e=m; 
4858   }
4859   return m;
4860 }
4861
4862
4863
4864 //___________________________________________________________________
4865 AliTPCclusterMI * AliTPCtrackerMI::AliTPCRow::FindNearest(Double_t y, Double_t z, Double_t roady, Double_t roadz) const {
4866   //-----------------------------------------------------------------------
4867   // Return the index of the nearest cluster in z y 
4868   //-----------------------------------------------------------------------
4869   Float_t maxdistance = roady*roady + roadz*roadz;
4870
4871   AliTPCclusterMI *cl =0;
4872   for (Int_t i=Find(z-roadz); i<fN; i++) {
4873       AliTPCclusterMI *c=(AliTPCclusterMI*)(fClusters[i]);
4874       if (c->GetZ() > z+roadz) break;
4875       if ( (c->GetY()-y) >  roady ) continue;
4876       Float_t distance = (c->GetZ()-z)*(c->GetZ()-z)+(c->GetY()-y)*(c->GetY()-y);
4877       if (maxdistance>distance) {
4878         maxdistance = distance;
4879         cl=c;       
4880       }
4881   }
4882   return cl;      
4883 }
4884
4885 AliTPCclusterMI * AliTPCtrackerMI::AliTPCRow::FindNearest2(Double_t y, Double_t z, Double_t roady, Double_t roadz,UInt_t & index) const 
4886 {
4887   //-----------------------------------------------------------------------
4888   // Return the index of the nearest cluster in z y 
4889   //-----------------------------------------------------------------------
4890   Float_t maxdistance = roady*roady + roadz*roadz;
4891   Int_t iz1 = TMath::Max(fFastCluster[Int_t(z-roadz+254.5)]-1,0);
4892   Int_t iz2 = TMath::Min(fFastCluster[Int_t(z+roadz+255.5)]+1,fN);
4893
4894   AliTPCclusterMI *cl =0;
4895   //FindNearest3(y,z,roady,roadz,index);
4896   //  for (Int_t i=Find(z-roadz); i<fN; i++) {
4897   for (Int_t i=iz1; i<iz2; i++) {
4898       AliTPCclusterMI *c=(AliTPCclusterMI*)(fClusters[i]);
4899       if (c->GetZ() > z+roadz) break;
4900       if ( c->GetY()-y >  roady ) continue;
4901       if ( y-c->GetY() >  roady ) continue;
4902       Float_t distance = (c->GetZ()-z)*(c->GetZ()-z)+(c->GetY()-y)*(c->GetY()-y);
4903       if (maxdistance>distance) {
4904         maxdistance = distance;
4905         cl=c;       
4906         index =i;
4907         //roady = TMath::Sqrt(maxdistance);
4908       }
4909   }
4910   return cl;      
4911 }
4912
4913
4914
4915 AliTPCclusterMI * AliTPCtrackerMI::AliTPCRow::FindNearest3(Double_t y, Double_t z, Double_t roady, Double_t roadz,UInt_t & index) const 
4916 {
4917   //-----------------------------------------------------------------------
4918   // Return the index of the nearest cluster in z y 
4919   //-----------------------------------------------------------------------
4920   Float_t maxdistance = roady*roady + roadz*roadz;
4921   //  Int_t iz = Int_t(z+255.);
4922   AliTPCclusterMI *cl =0;
4923   for (Int_t i=Find(z-roadz); i<fN; i++) {
4924     //for (Int_t i=fFastCluster[iz-2]; i<fFastCluster[iz+2]; i++) {
4925       AliTPCclusterMI *c=(AliTPCclusterMI*)(fClusters[i]);
4926       if (c->GetZ() > z+roadz) break;
4927       if ( c->GetY()-y >  roady ) continue;
4928       if ( y-c->GetY() >  roady ) continue;
4929       Float_t distance = (c->GetZ()-z)*(c->GetZ()-z)+(c->GetY()-y)*(c->GetY()-y);
4930       if (maxdistance>distance) {
4931         maxdistance = distance;
4932         cl=c;       
4933         index =i;
4934         //roady = TMath::Sqrt(maxdistance);
4935       }
4936   }
4937   return cl;      
4938 }
4939
4940
4941
4942
4943 AliTPCseed::AliTPCseed():AliTPCtrack(){
4944   //
4945   fRow=0; 
4946   fRemoval =0; 
4947   for (Int_t i=0;i<200;i++) SetClusterIndex2(i,-3);
4948   for (Int_t i=0;i<160;i++) fClusterPointer[i]=0;
4949
4950   fPoints = 0;
4951   fEPoints = 0;
4952   fNFoundable =0;
4953   fNShared  =0;
4954   fRemoval = 0;
4955   fSort =0;
4956   fFirstPoint =0;
4957   fNoCluster =0;
4958   fBSigned = kFALSE;
4959   fSeed1 =-1;
4960   fSeed2 =-1;
4961   fCurrentCluster =0;
4962   fCurrentSigmaY2=0;
4963   fCurrentSigmaZ2=0;
4964 }
4965 AliTPCseed::AliTPCseed(const AliTPCseed &s):AliTPCtrack(s){
4966   //---------------------
4967   // dummy copy constructor
4968   //-------------------------
4969 }
4970 AliTPCseed::AliTPCseed(const AliTPCtrack &t):AliTPCtrack(t){
4971   //
4972   //copy constructor
4973   fPoints = 0;
4974   fEPoints = 0;
4975   fNShared  =0; 
4976   //  fTrackPoints =0;
4977   fRemoval =0;
4978   fSort =0;
4979   for (Int_t i=0;i<160;i++) {
4980     fClusterPointer[i] = 0;
4981     Int_t index = t.GetClusterIndex(i);
4982     if (index>=-1){ 
4983       SetClusterIndex2(i,index);
4984     }
4985     else{
4986       SetClusterIndex2(i,-3); 
4987     }    
4988   }
4989   fFirstPoint =0;
4990   fNoCluster =0;
4991   fBSigned = kFALSE;
4992   fSeed1 =-1;
4993   fSeed2 =-1;
4994   fCurrentCluster =0;
4995   fCurrentSigmaY2=0;
4996   fCurrentSigmaZ2=0;
4997 }
4998
4999 AliTPCseed::AliTPCseed(const AliKalmanTrack &t, Double_t a):AliTPCtrack(t,a){
5000   //
5001   //copy constructor
5002   fRow=0;
5003   for (Int_t i=0;i<160;i++) {
5004     fClusterPointer[i] = 0;
5005     Int_t index = t.GetClusterIndex(i);
5006     SetClusterIndex2(i,index);
5007   }
5008   
5009   fPoints = 0;
5010   fEPoints = 0;
5011   fNFoundable =0; 
5012   fNShared  =0; 
5013   //  fTrackPoints =0;
5014   fRemoval =0;
5015   fSort = 0;
5016   fFirstPoint =0;
5017   fNoCluster =0;
5018   fBSigned = kFALSE;
5019   fSeed1 =-1;
5020   fSeed2 =-1;
5021   fCurrentCluster =0;
5022   fCurrentSigmaY2=0;
5023   fCurrentSigmaZ2=0;
5024
5025 }
5026
5027 AliTPCseed::AliTPCseed(UInt_t index, const Double_t xx[5], const Double_t cc[15], 
5028                                         Double_t xr, Double_t alpha):      
5029   AliTPCtrack(index, xx, cc, xr, alpha) {
5030   //
5031   //
5032   //constructor
5033   fRow =0;
5034   for (Int_t i=0;i<200;i++) SetClusterIndex2(i,-3);
5035   for (Int_t i=0;i<160;i++) fClusterPointer[i]=0;
5036   fPoints = 0;
5037   fEPoints = 0;
5038   fNFoundable =0;
5039   fNShared  = 0;
5040   //  fTrackPoints =0;
5041   fRemoval =0;
5042   fSort =0;
5043   fFirstPoint =0;
5044   //  fHelixIn = new TClonesArray("AliHelix",0);
5045   //fHelixOut = new TClonesArray("AliHelix",0);
5046   fNoCluster =0;
5047   fBSigned = kFALSE;
5048   fSeed1 =-1;
5049   fSeed2 =-1;
5050   fCurrentCluster =0;
5051   fCurrentSigmaY2=0;
5052   fCurrentSigmaZ2=0;
5053 }
5054
5055 AliTPCseed::~AliTPCseed(){
5056   //
5057   // destructor
5058   if (fPoints) delete fPoints;
5059   fPoints =0;
5060   if (fEPoints) delete fEPoints;
5061   fEPoints = 0;
5062   fNoCluster =0;
5063 }
5064
5065 AliTPCTrackerPoint * AliTPCseed::GetTrackPoint(Int_t i)
5066 {
5067   //
5068   // 
5069   return &fTrackPoints[i];
5070 }
5071
5072 void AliTPCseed::RebuildSeed()
5073 {
5074   //
5075   // rebuild seed to be ready for storing
5076   AliTPCclusterMI cldummy;
5077   cldummy.SetQ(0);
5078   AliTPCTrackPoint pdummy;
5079   pdummy.GetTPoint().fIsShared = 10;
5080   for (Int_t i=0;i<160;i++){
5081     AliTPCclusterMI * cl0 = fClusterPointer[i];
5082     AliTPCTrackPoint *trpoint = (AliTPCTrackPoint*)fPoints->UncheckedAt(i);     
5083     if (cl0){
5084       trpoint->GetTPoint() = *(GetTrackPoint(i));
5085       trpoint->GetCPoint() = *cl0;
5086       trpoint->GetCPoint().SetQ(TMath::Abs(cl0->GetQ()));
5087     }
5088     else{
5089       *trpoint = pdummy;
5090       trpoint->GetCPoint()= cldummy;
5091     }
5092     
5093   }
5094
5095 }
5096
5097
5098 Double_t AliTPCseed::GetDensityFirst(Int_t n)
5099 {
5100   //
5101   //
5102   // return cluster for n rows bellow first point
5103   Int_t nfoundable = 1;
5104   Int_t nfound      = 1;
5105   for (Int_t i=fLastPoint-1;i>0&&nfoundable<n; i--){
5106     Int_t index = GetClusterIndex2(i);
5107     if (index!=-1) nfoundable++;
5108     if (index>0) nfound++;
5109   }
5110   if (nfoundable<n) return 0;
5111   return Double_t(nfound)/Double_t(nfoundable);
5112
5113 }
5114
5115
5116 void AliTPCseed::GetClusterStatistic(Int_t first, Int_t last, Int_t &found, Int_t &foundable, Int_t &shared, Bool_t plus2)
5117 {
5118   // get cluster stat.  on given region
5119   //
5120   found       = 0;
5121   foundable   = 0;
5122   shared      =0;
5123   for (Int_t i=first;i<last; i++){
5124     Int_t index = GetClusterIndex2(i);
5125     if (index!=-1) foundable++;
5126     if (fClusterPointer[i]) {
5127       found++;
5128     }
5129     else 
5130       continue;
5131
5132     if (fClusterPointer[i]->IsUsed(10)) {
5133       shared++;
5134       continue;
5135     }
5136     if (!plus2) continue; //take also neighborhoud
5137     //
5138     if ( (i>0) && fClusterPointer[i-1]){
5139       if (fClusterPointer[i-1]->IsUsed(10)) {
5140         shared++;
5141         continue;
5142       }
5143     }
5144     if ( fClusterPointer[i+1]){
5145       if (fClusterPointer[i+1]->IsUsed(10)) {
5146         shared++;
5147         continue;
5148       }
5149     }
5150     
5151   }
5152   //if (shared>found){
5153     //Error("AliTPCseed::GetClusterStatistic","problem\n");
5154   //}
5155 }
5156
5157 //_____________________________________________________________________________
5158 void AliTPCseed::CookdEdx(Double_t low, Double_t up,Int_t i1, Int_t i2, Bool_t onlyused) {
5159   //-----------------------------------------------------------------
5160   // This funtion calculates dE/dX within the "low" and "up" cuts.
5161   //-----------------------------------------------------------------
5162
5163   Float_t amp[200];
5164   Float_t angular[200];
5165   Float_t weight[200];
5166   Int_t index[200];
5167   //Int_t nc = 0;
5168   //  TClonesArray & arr = *fPoints; 
5169   Float_t meanlog = 100.;
5170   
5171   Float_t mean[4]  = {0,0,0,0};
5172   Float_t sigma[4] = {1000,1000,1000,1000};
5173   Int_t nc[4]      = {0,0,0,0};
5174   Float_t norm[4]    = {1000,1000,1000,1000};
5175   //
5176   //
5177   fNShared =0;
5178
5179   for (Int_t of =0; of<4; of++){    
5180     for (Int_t i=of+i1;i<i2;i+=4)
5181       {
5182         Int_t index = fIndex[i];
5183         if (index<0||index&0x8000) continue;
5184
5185         //AliTPCTrackPoint * point = (AliTPCTrackPoint *) arr.At(i);
5186         AliTPCTrackerPoint * point = GetTrackPoint(i);
5187         //AliTPCTrackerPoint * pointm = GetTrackPoint(i-1);
5188         //AliTPCTrackerPoint * pointp = 0;
5189         //if (i<159) pointp = GetTrackPoint(i+1);
5190
5191         if (point==0) continue;
5192         AliTPCclusterMI * cl = fClusterPointer[i];
5193         if (cl==0) continue;    
5194         if (onlyused && (!cl->IsUsed(10))) continue;
5195         if (cl->IsUsed(11)) {
5196           fNShared++;
5197           continue;
5198         }
5199         Int_t   type   = cl->GetType();
5200         //if (point->fIsShared){
5201         //  fNShared++;
5202         //  continue;
5203         //}
5204         //if (pointm) 
5205         //  if (pointm->fIsShared) continue;
5206         //if (pointp) 
5207         //  if (pointp->fIsShared) continue;
5208
5209         if (type<0) continue;
5210         //if (type>10) continue;       
5211         //if (point->GetErrY()==0) continue;
5212         //if (point->GetErrZ()==0) continue;
5213
5214         //Float_t ddy = (point->GetY()-cl->GetY())/point->GetErrY();
5215         //Float_t ddz = (point->GetZ()-cl->GetZ())/point->GetErrZ();
5216         //if ((ddy*ddy+ddz*ddz)>10) continue; 
5217
5218
5219         //      if (point->GetCPoint().GetMax()<5) continue;
5220         if (cl->GetMax()<5) continue;
5221         Float_t angley = point->GetAngleY();
5222         Float_t anglez = point->GetAngleZ();
5223
5224         Float_t rsigmay2 =  point->GetSigmaY();
5225         Float_t rsigmaz2 =  point->GetSigmaZ();
5226         /*
5227         Float_t ns = 1.;
5228         if (pointm){
5229           rsigmay +=  pointm->GetTPoint().GetSigmaY();
5230           rsigmaz +=  pointm->GetTPoint().GetSigmaZ();
5231           ns+=1.;
5232         }
5233         if (pointp){
5234           rsigmay +=  pointp->GetTPoint().GetSigmaY();
5235           rsigmaz +=  pointp->GetTPoint().GetSigmaZ();
5236           ns+=1.;
5237         }
5238         rsigmay/=ns;
5239         rsigmaz/=ns;
5240         */
5241
5242         Float_t rsigma = TMath::Sqrt(rsigmay2*rsigmaz2);
5243
5244         Float_t ampc   = 0;     // normalization to the number of electrons
5245         if (i>64){
5246           //      ampc = 1.*point->GetCPoint().GetMax();
5247           ampc = 1.*cl->GetMax();
5248           //ampc = 1.*point->GetCPoint().GetQ();          
5249           //      AliTPCClusterPoint & p = point->GetCPoint();
5250           //      Float_t dy = TMath::Abs(Int_t( TMath::Abs(p.GetY()/0.6)) - TMath::Abs(p.GetY()/0.6)+0.5);
5251           // Float_t iz =  (250.0-TMath::Abs(p.GetZ())+0.11)/0.566;
5252           //Float_t dz = 
5253           //  TMath::Abs( Int_t(iz) - iz + 0.5);
5254           //ampc *= 1.15*(1-0.3*dy);
5255           //ampc *= 1.15*(1-0.3*dz);
5256           //      Float_t zfactor = (1.05-0.0004*TMath::Abs(point->GetCPoint().GetZ()));
5257           //ampc               *=zfactor; 
5258         }
5259         else{ 
5260           //ampc = 1.0*point->GetCPoint().GetMax(); 
5261           ampc = 1.0*cl->GetMax(); 
5262           //ampc = 1.0*point->GetCPoint().GetQ(); 
5263           //AliTPCClusterPoint & p = point->GetCPoint();
5264           // Float_t dy = TMath::Abs(Int_t( TMath::Abs(p.GetY()/0.4)) - TMath::Abs(p.GetY()/0.4)+0.5);
5265           //Float_t iz =  (250.0-TMath::Abs(p.GetZ())+0.11)/0.566;
5266           //Float_t dz = 
5267           //  TMath::Abs( Int_t(iz) - iz + 0.5);
5268
5269           //ampc *= 1.15*(1-0.3*dy);
5270           //ampc *= 1.15*(1-0.3*dz);
5271           //    Float_t zfactor = (1.02-0.000*TMath::Abs(point->GetCPoint().GetZ()));
5272           //ampc               *=zfactor; 
5273
5274         }
5275         ampc *= 2.0;     // put mean value to channel 50
5276         //ampc *= 0.58;     // put mean value to channel 50
5277         Float_t w      =  1.;
5278         //      if (type>0)  w =  1./(type/2.-0.5); 
5279         //      Float_t z = TMath::Abs(cl->GetZ());
5280         if (i<64) {
5281           ampc /= 0.6;
5282           //ampc /= (1+0.0008*z);
5283         } else
5284           if (i>128){
5285             ampc /=1.5;
5286             //ampc /= (1+0.0008*z);
5287           }else{
5288             //ampc /= (1+0.0008*z);
5289           }
5290         
5291         if (type<0) {  //amp at the border - lower weight
5292           // w*= 2.;
5293           
5294           continue;
5295         }
5296         if (rsigma>1.5) ampc/=1.3;  // if big backround
5297         amp[nc[of]]        = ampc;
5298         angular[nc[of]]    = TMath::Sqrt(1.+angley*angley+anglez*anglez);
5299         weight[nc[of]]     = w;
5300         nc[of]++;
5301       }
5302     
5303     TMath::Sort(nc[of],amp,index,kFALSE);
5304     Float_t sumamp=0;
5305     Float_t sumamp2=0;
5306     Float_t sumw=0;
5307     //meanlog = amp[index[Int_t(nc[of]*0.33)]];
5308     meanlog = 50;
5309     for (Int_t i=int(nc[of]*low+0.5);i<int(nc[of]*up+0.5);i++){
5310       Float_t ampl      = amp[index[i]]/angular[index[i]];
5311       ampl              = meanlog*TMath::Log(1.+ampl/meanlog);
5312       //
5313       sumw    += weight[index[i]]; 
5314       sumamp  += weight[index[i]]*ampl;
5315       sumamp2 += weight[index[i]]*ampl*ampl;
5316       norm[of]    += angular[index[i]]*weight[index[i]];
5317     }
5318     if (sumw<1){ 
5319       SetdEdx(0);  
5320     }
5321     else {
5322       norm[of] /= sumw;
5323       mean[of]  = sumamp/sumw;
5324       sigma[of] = sumamp2/sumw-mean[of]*mean[of];
5325       if (sigma[of]>0.1) 
5326         sigma[of] = TMath::Sqrt(sigma[of]);
5327       else
5328         sigma[of] = 1000;
5329       
5330     mean[of] = (TMath::Exp(mean[of]/meanlog)-1)*meanlog;
5331     //mean  *=(1-0.02*(sigma/(mean*0.17)-1.));
5332     //mean *=(1-0.1*(norm-1.));
5333     }
5334   }
5335
5336   Float_t dedx =0;
5337   fSdEdx =0;
5338   fMAngular =0;
5339   //  mean[0]*= (1-0.05*(sigma[0]/(0.01+mean[1]*0.18)-1));
5340   //  mean[1]*= (1-0.05*(sigma[1]/(0.01+mean[0]*0.18)-1));
5341
5342   
5343   //  dedx = (mean[0]* TMath::Sqrt((1.+nc[0]))+ mean[1]* TMath::Sqrt((1.+nc[1])) )/ 
5344   //  (  TMath::Sqrt((1.+nc[0]))+TMath::Sqrt((1.+nc[1])));
5345
5346   Int_t norm2 = 0;
5347   Int_t norm3 = 0;
5348   for (Int_t i =0;i<4;i++){
5349     if (nc[i]>2&&nc[i]<1000){
5350       dedx      += mean[i] *nc[i];
5351       fSdEdx    += sigma[i]*(nc[i]-2);
5352       fMAngular += norm[i] *nc[i];    
5353       norm2     += nc[i];
5354       norm3     += nc[i]-2;
5355     }
5356     fDEDX[i]  = mean[i];             
5357     fSDEDX[i] = sigma[i];            
5358     fNCDEDX[i]= nc[i]; 
5359   }
5360
5361   if (norm3>0){
5362     dedx   /=norm2;
5363     fSdEdx /=norm3;
5364     fMAngular/=norm2;
5365   }
5366   else{
5367     SetdEdx(0);
5368     return;
5369   }
5370   //  Float_t dedx1 =dedx;
5371   /*
5372   dedx =0;
5373   for (Int_t i =0;i<4;i++){
5374     if (nc[i]>2&&nc[i]<1000){
5375       mean[i]   = mean[i]*(1-0.12*(sigma[i]/(fSdEdx)-1.));
5376       dedx      += mean[i] *nc[i];
5377     }
5378     fDEDX[i]  = mean[i];                
5379   }
5380   dedx /= norm2;
5381   */
5382
5383   
5384   SetdEdx(dedx);
5385     
5386   //mi deDX
5387
5388
5389
5390   //Very rough PID
5391   Double_t p=TMath::Sqrt((1.+ GetTgl()*GetTgl())/(Get1Pt()*Get1Pt()));
5392
5393   if (p<0.6) {
5394     if (dedx < 39.+ 12./(p+0.25)/(p+0.25)) { SetMass(0.13957); return;}
5395     if (dedx < 39.+ 12./p/p) { SetMass(0.49368); return;}
5396     SetMass(0.93827); return;
5397   }
5398
5399   if (p<1.2) {
5400     if (dedx < 39.+ 12./(p+0.25)/(p+0.25)) { SetMass(0.13957); return;}
5401     SetMass(0.93827); return;
5402   }
5403
5404   SetMass(0.13957); return;
5405
5406 }
5407
5408
5409
5410 /*
5411
5412
5413
5414 void AliTPCseed::CookdEdx2(Double_t low, Double_t up) {
5415   //-----------------------------------------------------------------
5416   // This funtion calculates dE/dX within the "low" and "up" cuts.
5417   //-----------------------------------------------------------------
5418
5419   Float_t amp[200];
5420   Float_t angular[200];
5421   Float_t weight[200];
5422   Int_t index[200];
5423   Bool_t inlimit[200];
5424   for (Int_t i=0;i<200;i++) inlimit[i]=kFALSE;
5425   for (Int_t i=0;i<200;i++) amp[i]=10000;
5426   for (Int_t i=0;i<200;i++) angular[i]= 1;;
5427   
5428
5429   //
5430   Float_t meanlog = 100.;
5431   Int_t indexde[4]={0,64,128,160};
5432
5433   Float_t amean     =0;
5434   Float_t asigma    =0;
5435   Float_t anc       =0;
5436   Float_t anorm     =0;
5437
5438   Float_t mean[4]  = {0,0,0,0};
5439   Float_t sigma[4] = {1000,1000,1000,1000};
5440   Int_t nc[4]      = {0,0,0,0};
5441   Float_t norm[4]    = {1000,1000,1000,1000};
5442   //
5443   //
5444   fNShared =0;
5445
5446   //  for (Int_t of =0; of<3; of++){    
5447   //  for (Int_t i=indexde[of];i<indexde[of+1];i++)
5448   for (Int_t i =0; i<160;i++)
5449     {
5450         AliTPCTrackPoint * point = GetTrackPoint(i);
5451         if (point==0) continue;
5452         if (point->fIsShared){
5453           fNShared++;     
5454           continue;
5455         }
5456         Int_t   type   = point->GetCPoint().GetType();
5457         if (type<0) continue;
5458         if (point->GetCPoint().GetMax()<5) continue;
5459         Float_t angley = point->GetTPoint().GetAngleY();
5460         Float_t anglez = point->GetTPoint().GetAngleZ();
5461         Float_t rsigmay =  point->GetCPoint().GetSigmaY();
5462         Float_t rsigmaz =  point->GetCPoint().GetSigmaZ();
5463         Float_t rsigma = TMath::Sqrt(rsigmay*rsigmaz);
5464
5465         Float_t ampc   = 0;     // normalization to the number of electrons
5466         if (i>64){
5467           ampc =  point->GetCPoint().GetMax();
5468         }
5469         else{ 
5470           ampc = point->GetCPoint().GetMax(); 
5471         }
5472         ampc *= 2.0;     // put mean value to channel 50
5473         //      ampc *= 0.565;     // put mean value to channel 50
5474
5475         Float_t w      =  1.;
5476         Float_t z = TMath::Abs(point->GetCPoint().GetZ());
5477         if (i<64) {
5478           ampc /= 0.63;
5479         } else
5480           if (i>128){
5481             ampc /=1.51;
5482           }             
5483         if (type<0) {  //amp at the border - lower weight                 
5484           continue;
5485         }
5486         if (rsigma>1.5) ampc/=1.3;  // if big backround
5487         angular[i]    = TMath::Sqrt(1.+angley*angley+anglez*anglez);
5488         amp[i]        = ampc/angular[i];
5489         weight[i]     = w;
5490         anc++;
5491     }
5492
5493   TMath::Sort(159,amp,index,kFALSE);
5494   for (Int_t i=int(anc*low+0.5);i<int(anc*up+0.5);i++){      
5495     inlimit[index[i]] = kTRUE;  // take all clusters
5496   }
5497   
5498   //  meanlog = amp[index[Int_t(anc*0.3)]];
5499   meanlog =10000.;
5500   for (Int_t of =0; of<3; of++){    
5501     Float_t sumamp=0;
5502     Float_t sumamp2=0;
5503     Float_t sumw=0;    
5504    for (Int_t i=indexde[of];i<indexde[of+1];i++)
5505       {
5506         if (inlimit[i]==kFALSE) continue;
5507         Float_t ampl      = amp[i];
5508         ///angular[i];
5509         ampl              = meanlog*TMath::Log(1.+ampl/meanlog);
5510         //
5511         sumw    += weight[i]; 
5512         sumamp  += weight[i]*ampl;
5513         sumamp2 += weight[i]*ampl*ampl;
5514         norm[of]    += angular[i]*weight[i];
5515         nc[of]++;
5516       }
5517    if (sumw<1){ 
5518      SetdEdx(0);  
5519    }
5520    else {
5521      norm[of] /= sumw;
5522      mean[of]  = sumamp/sumw;
5523      sigma[of] = sumamp2/sumw-mean[of]*mean[of];
5524      if (sigma[of]>0.1) 
5525        sigma[of] = TMath::Sqrt(sigma[of]);
5526      else
5527        sigma[of] = 1000;      
5528      mean[of] = (TMath::Exp(mean[of]/meanlog)-1)*meanlog;
5529    }
5530   }
5531     
5532   Float_t dedx =0;
5533   fSdEdx =0;
5534   fMAngular =0;
5535   //
5536   Int_t norm2 = 0;
5537   Int_t norm3 = 0;
5538   Float_t www[3] = {12.,14.,17.};
5539   //Float_t www[3] = {1.,1.,1.};
5540
5541   for (Int_t i =0;i<3;i++){
5542     if (nc[i]>2&&nc[i]<1000){
5543       dedx      += mean[i] *nc[i]*www[i]/sigma[i];
5544       fSdEdx    += sigma[i]*(nc[i]-2)*www[i]/sigma[i];
5545       fMAngular += norm[i] *nc[i];    
5546       norm2     += nc[i]*www[i]/sigma[i];
5547       norm3     += (nc[i]-2)*www[i]/sigma[i];
5548     }
5549     fDEDX[i]  = mean[i];             
5550     fSDEDX[i] = sigma[i];            
5551     fNCDEDX[i]= nc[i]; 
5552   }
5553
5554   if (norm3>0){
5555     dedx   /=norm2;
5556     fSdEdx /=norm3;
5557     fMAngular/=norm2;
5558   }
5559   else{
5560     SetdEdx(0);
5561     return;
5562   }
5563   //  Float_t dedx1 =dedx;
5564   
5565   dedx =0;
5566   Float_t norm4 = 0;
5567   for (Int_t i =0;i<3;i++){
5568     if (nc[i]>2&&nc[i]<1000&&sigma[i]>3){
5569       //mean[i]   = mean[i]*(1+0.08*(sigma[i]/(fSdEdx)-1.));
5570       dedx      += mean[i] *(nc[i])/(sigma[i]);
5571       norm4     += (nc[i])/(sigma[i]);
5572     }
5573     fDEDX[i]  = mean[i];                
5574   }
5575   if (norm4>0) dedx /= norm4;
5576   
5577
5578   
5579   SetdEdx(dedx);
5580     
5581   //mi deDX
5582
5583 }
5584
5585 */