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