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