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