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