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