]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITStrackerMI.cxx
Additional protection
[u/mrichter/AliRoot.git] / ITS / AliITStrackerMI.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 /* $Id$ */
17
18 //-------------------------------------------------------------------------
19 //               Implementation of the ITS tracker class
20 //    It reads AliITSclusterV2 clusters and creates AliITStrackMI tracks
21 //                   and fills with them the ESD
22 //          Origin: Marian Ivanov, CERN, Marian.Ivanov@cern.ch 
23 //     dEdx analysis by: Boris Batyunya, JINR, Boris.Batiounia@cern.ch
24 //     
25 //-------------------------------------------------------------------------
26
27 #include <TMatrixD.h>
28 #include <TTree.h>
29 #include <TTreeStream.h>
30 #include <TTree.h>
31
32 #include "AliESD.h"
33 #include "AliESDV0MI.h"
34 #include "AliHelix.h"
35 #include "AliITSclusterV2.h"
36 #include "AliITSgeom.h"
37 #include "AliITStrackerMI.h"
38
39 ClassImp(AliITStrackerMI)
40
41
42
43 AliITStrackerMI::AliITSlayer AliITStrackerMI::fgLayers[kMaxLayer]; // ITS layers
44
45 AliITStrackerMI::AliITStrackerMI(const AliITSgeom *geom) : AliTracker() {
46   //--------------------------------------------------------------------
47   //This is the AliITStrackerMI constructor
48   //--------------------------------------------------------------------
49   fCoeficients = 0;
50   fAfterV0     = kFALSE;
51   AliITSgeom *g=(AliITSgeom*)geom;
52   Float_t x,y,z;
53   Int_t i;
54   for (i=1; i<kMaxLayer+1; i++) {
55     Int_t nlad=g->GetNladders(i);
56     Int_t ndet=g->GetNdetectors(i);
57
58     g->GetTrans(i,1,1,x,y,z); 
59     Double_t r=TMath::Sqrt(x*x + y*y);
60     Double_t poff=TMath::ATan2(y,x);
61     Double_t zoff=z;
62
63     g->GetTrans(i,1,2,x,y,z);
64     r += TMath::Sqrt(x*x + y*y);
65     g->GetTrans(i,2,1,x,y,z);
66     r += TMath::Sqrt(x*x + y*y);
67     g->GetTrans(i,2,2,x,y,z);
68     r += TMath::Sqrt(x*x + y*y);
69     r*=0.25;
70
71     new (fgLayers+i-1) AliITSlayer(r,poff,zoff,nlad,ndet);
72
73     for (Int_t j=1; j<nlad+1; j++) {
74       for (Int_t k=1; k<ndet+1; k++) { //Fill this layer with detectors
75         Float_t x,y,zshift; g->GetTrans(i,j,k,x,y,zshift); 
76         Double_t rot[9]; g->GetRotMatrix(i,j,k,rot);
77
78         Double_t phi=TMath::ATan2(rot[1],rot[0])+TMath::Pi();
79         phi+=TMath::Pi()/2;
80         if (i==1) phi+=TMath::Pi();
81         Double_t cp=TMath::Cos(phi), sp=TMath::Sin(phi);
82         Double_t r=x*cp+y*sp;
83
84         AliITSdetector &det=fgLayers[i-1].GetDetector((j-1)*ndet + k-1); 
85         new(&det) AliITSdetector(r,phi); 
86       } 
87     }  
88
89   }
90
91   fI=kMaxLayer;
92
93   fPass=0;
94   fConstraint[0]=1; fConstraint[1]=0;
95
96   Double_t xyz[]={kXV,kYV,kZV}, ers[]={kSigmaXV,kSigmaYV,kSigmaZV}; 
97   SetVertex(xyz,ers);
98
99   for (Int_t i=0; i<kMaxLayer; i++) fLayersNotToSkip[i]=kLayersNotToSkip[i];
100   fLastLayerToTrackTo=kLastLayerToTrackTo;
101   for (Int_t i=0;i<100000;i++){
102     fBestTrackIndex[i]=0;
103   }
104   //
105   fDebugStreamer = new TTreeSRedirector("ITSdebug.root");
106
107 }
108
109 AliITStrackerMI::~AliITStrackerMI()
110 {
111   //
112   //destructor
113   //
114   if (fCoeficients) delete []fCoeficients;
115   if (fDebugStreamer) {
116     //fDebugStreamer->Close();
117     delete fDebugStreamer;
118   }
119 }
120
121 void AliITStrackerMI::SetLayersNotToSkip(Int_t *l) {
122   //--------------------------------------------------------------------
123   //This function set masks of the layers which must be not skipped
124   //--------------------------------------------------------------------
125   for (Int_t i=0; i<kMaxLayer; i++) fLayersNotToSkip[i]=l[i];
126 }
127
128 Int_t AliITStrackerMI::LoadClusters(TTree *cTree) {
129   //--------------------------------------------------------------------
130   //This function loads ITS clusters
131   //--------------------------------------------------------------------
132   TBranch *branch=cTree->GetBranch("Clusters");
133   if (!branch) { 
134     Error("LoadClusters"," can't get the branch !\n");
135     return 1;
136   }
137
138   TClonesArray dummy("AliITSclusterV2",10000), *clusters=&dummy;
139   branch->SetAddress(&clusters);
140
141   Int_t j=0;
142   Int_t detector=0;
143   for (Int_t i=0; i<kMaxLayer; i++) {
144     Int_t ndet=fgLayers[i].GetNdetectors();
145     Int_t jmax = j + fgLayers[i].GetNladders()*ndet;
146     for (; j<jmax; j++) {           
147       if (!cTree->GetEvent(j)) continue;
148       Int_t ncl=clusters->GetEntriesFast();
149       SignDeltas(clusters,GetZ());
150       while (ncl--) {
151         AliITSclusterV2 *c=(AliITSclusterV2*)clusters->UncheckedAt(ncl);
152         detector = c->GetDetectorIndex();
153         fgLayers[i].InsertCluster(new AliITSclusterV2(*c));
154       }
155       clusters->Delete();
156       //add dead zone virtual "cluster"      
157       if (i<2){
158         for (Float_t ydead = 0; ydead < 1.31 ; ydead+=(i+1.)*0.018){     
159           Int_t lab[4] = {0,0,0,detector};
160           Int_t info[3] = {0,0,0};
161           Float_t hit[5]={0,0,0.004/12.,0.001/12.,0};
162           if (i==0) hit[0] =ydead-0.4;
163           if (i==1) hit[0]=ydead-3.75; 
164           hit[1] =-0.04;
165           if (TMath::Abs(fgLayers[i].GetDetector(detector).GetZmax()-hit[1])<2.) 
166             fgLayers[i].InsertCluster(new AliITSclusterV2(lab, hit, info));
167           hit[1]=-7.05;
168           if (TMath::Abs(fgLayers[i].GetDetector(detector).GetZmax()-hit[1])<2.) 
169             fgLayers[i].InsertCluster(new AliITSclusterV2(lab, hit, info));
170           hit[1]=-7.15;
171           if (TMath::Abs(fgLayers[i].GetDetector(detector).GetZmax()-hit[1])<2.) 
172             fgLayers[i].InsertCluster(new AliITSclusterV2(lab, hit, info));
173           hit[1] =0.06;
174           if (TMath::Abs(fgLayers[i].GetDetector(detector).GetZmax()-hit[1])<2.) 
175             fgLayers[i].InsertCluster(new AliITSclusterV2(lab, hit, info));
176           hit[1]=7.05;
177           if (TMath::Abs(fgLayers[i].GetDetector(detector).GetZmax()-hit[1])<2.) 
178             fgLayers[i].InsertCluster(new AliITSclusterV2(lab, hit, info));
179           hit[1]=7.25;
180           if (TMath::Abs(fgLayers[i].GetDetector(detector).GetZmax()-hit[1])<2.) 
181             fgLayers[i].InsertCluster(new AliITSclusterV2(lab, hit, info));       
182         }
183       }
184       
185     }
186     //
187     fgLayers[i].ResetRoad(); //road defined by the cluster density
188     fgLayers[i].SortClusters();
189   }
190
191   return 0;
192 }
193
194 void AliITStrackerMI::UnloadClusters() {
195   //--------------------------------------------------------------------
196   //This function unloads ITS clusters
197   //--------------------------------------------------------------------
198   for (Int_t i=0; i<kMaxLayer; i++) fgLayers[i].ResetClusters();
199 }
200
201 static Int_t CorrectForDeadZoneMaterial(AliITStrackMI *t) {
202   //--------------------------------------------------------------------
203   // Correction for the material between the TPC and the ITS
204   // (should it belong to the TPC code ?)
205   //--------------------------------------------------------------------
206   Double_t riw=80., diw=0.0053, x0iw=30; // TPC inner wall ? 
207   Double_t rcd=61., dcd=0.0053, x0cd=30; // TPC "central drum" ?
208   Double_t yr=12.8, dr=0.03; // rods ?
209   Double_t zm=0.2, dm=0.40;  // membrane
210   //Double_t rr=52., dr=0.19, x0r=24., yyr=7.77; //rails
211   Double_t rs=50., ds=0.001; // something belonging to the ITS (screen ?)
212
213   if (t->GetX() > riw) {
214      if (!t->PropagateTo(riw,diw,x0iw)) return 1;
215      if (TMath::Abs(t->GetY())>yr) t->CorrectForMaterial(dr); 
216      if (TMath::Abs(t->GetZ())<zm) t->CorrectForMaterial(dm); 
217      if (!t->PropagateTo(rcd,dcd,x0cd)) return 1;
218      //Double_t x,y,z; t->GetGlobalXYZat(rr,x,y,z);
219      //if (TMath::Abs(y)<yyr) t->PropagateTo(rr,dr,x0r); 
220      if (!t->PropagateTo(rs,ds)) return 1;
221   } else if (t->GetX() < rs) {
222      if (!t->PropagateTo(rs,-ds)) return 1;
223      //Double_t x,y,z; t->GetGlobalXYZat(rr,x,y,z);
224      //if (TMath::Abs(y)<yyr) t->PropagateTo(rr,-dr,x0r); 
225      if (!t->PropagateTo(rcd,-dcd,x0cd)) return 1;
226      if (!t->PropagateTo(riw+0.001,-diw,x0iw)) return 1;
227   } else {
228   ::Error("CorrectForDeadZoneMaterial","track is already in the dead zone !");
229     return 1;
230   }
231   
232   return 0;
233 }
234
235 Int_t AliITStrackerMI::Clusters2Tracks(AliESD *event) {
236   //--------------------------------------------------------------------
237   // This functions reconstructs ITS tracks
238   // The clusters must be already loaded !
239   //--------------------------------------------------------------------
240   TObjArray itsTracks(15000);
241   fOriginal.Clear();
242   fEsd = event;         // store pointer to the esd 
243   {/* Read ESD tracks */
244     Int_t nentr=event->GetNumberOfTracks();
245     Info("Clusters2Tracks", "Number of ESD tracks: %d\n", nentr);
246     while (nentr--) {
247       AliESDtrack *esd=event->GetTrack(nentr);
248
249       if ((esd->GetStatus()&AliESDtrack::kTPCin)==0) continue;
250       if (esd->GetStatus()&AliESDtrack::kTPCout) continue;
251       if (esd->GetStatus()&AliESDtrack::kITSin) continue;
252       if (esd->GetKinkIndex(0)>0) continue;   //kink daughter
253       AliITStrackMI *t=0;
254       try {
255         t=new AliITStrackMI(*esd);
256       } catch (const Char_t *msg) {
257         //Warning("Clusters2Tracks",msg);
258         delete t;
259         continue;
260       }
261       t->fD[0] = t->GetD(GetX(),GetY());
262       t->fD[1] = t->GetZat(GetX())-GetZ(); 
263       Double_t vdist = TMath::Sqrt(t->fD[0]*t->fD[0]+t->fD[1]*t->fD[1]);
264       if (t->GetMass()<0.13) t->SetMass(0.13957); // MI look to the esd - mass hypothesys  !!!!!!!!!!!
265       // write expected q
266       t->fExpQ = TMath::Max(0.8*t->fESDtrack->GetTPCsignal(),30.);
267
268       if (esd->GetV0Index(0)>0 && t->fD[0]<30){
269         //track - can be  V0 according to TPC
270       }
271       else{     
272         if (TMath::Abs(t->fD[0])>10) {
273           delete t;
274           continue;
275         }
276         
277         if (TMath::Abs(vdist)>20) {
278           delete t;
279           continue;
280         }
281         if (TMath::Abs(1/t->Get1Pt())<0.120) {
282           delete t;
283           continue;
284         }
285         
286         if (CorrectForDeadZoneMaterial(t)!=0) {
287           //Warning("Clusters2Tracks",
288           //        "failed to correct for the material in the dead zone !\n");
289           delete t;
290           continue;
291         }
292       }
293       t->fReconstructed = kFALSE;
294       itsTracks.AddLast(t);
295       fOriginal.AddLast(t);
296     }
297   } /* End Read ESD tracks */
298
299   itsTracks.Sort();
300   fOriginal.Sort();
301   Int_t nentr=itsTracks.GetEntriesFast();
302   fTrackHypothesys.Expand(nentr);
303   fBestHypothesys.Expand(nentr);
304   MakeCoeficients(nentr);
305   Int_t ntrk=0;
306   for (fPass=0; fPass<2; fPass++) {
307      Int_t &constraint=fConstraint[fPass]; if (constraint<0) continue;
308      for (Int_t i=0; i<nentr; i++) {
309 //       cerr<<fPass<<"    "<<i<<'\r';
310        fCurrentEsdTrack = i;
311        AliITStrackMI *t=(AliITStrackMI*)itsTracks.UncheckedAt(i);
312        if (t==0) continue;              //this track has been already tracked
313        if (t->fReconstructed&&(t->fNUsed<1.5)) continue;  //this track was  already  "succesfully" reconstructed
314        if ( (TMath::Abs(t->GetD(GetX(),GetY()))  >3.) && fConstraint[fPass]) continue;
315        if ( (TMath::Abs(t->GetZat(GetX())-GetZ())>3.) && fConstraint[fPass]) continue;
316
317        Int_t tpcLabel=t->GetLabel(); //save the TPC track label       
318        fI = 6;
319        ResetTrackToFollow(*t);
320        ResetBestTrack();
321        FollowProlongationTree(t,i,fConstraint[fPass]);
322
323        SortTrackHypothesys(fCurrentEsdTrack,20,0);  //MI change
324        //
325        AliITStrackMI * besttrack = GetBestHypothesys(fCurrentEsdTrack,t,15);
326        if (!besttrack) continue;
327        besttrack->SetLabel(tpcLabel);
328        //       besttrack->CookdEdx();
329        CookdEdx(besttrack);
330        besttrack->fFakeRatio=1.;
331        CookLabel(besttrack,0.); //For comparison only
332        UpdateESDtrack(besttrack,AliESDtrack::kITSin);
333
334        /*       
335        if ( besttrack->GetNumberOfClusters()<6 && fConstraint[fPass]) {  
336          continue;
337        }
338        if (besttrack->fChi2MIP[0]+besttrack->fNUsed>3.5) continue;
339        if ( (TMath::Abs(besttrack->fD[0]*besttrack->fD[0]+besttrack->fD[1]*besttrack->fD[1])>0.1) && fConstraint[fPass])  continue;      
340        //delete itsTracks.RemoveAt(i);
341        */
342        if (fConstraint[fPass]&&(!besttrack->IsGoldPrimary())) continue;  //to be tracked also without vertex constrain 
343
344
345        t->fReconstructed = kTRUE;
346        ntrk++;                     
347      }
348      GetBestHypothesysMIP(itsTracks); 
349   }
350
351   //GetBestHypothesysMIP(itsTracks);
352   UpdateTPCV0(event);
353   FindV02(event);
354   fAfterV0 = kTRUE;
355   //GetBestHypothesysMIP(itsTracks);
356   //
357   itsTracks.Delete();
358   //
359   Int_t entries = fTrackHypothesys.GetEntriesFast();
360   for (Int_t ientry=0;ientry<entries;ientry++){
361     TObjArray * array =(TObjArray*)fTrackHypothesys.UncheckedAt(ientry);
362     if (array) array->Delete();
363     delete fTrackHypothesys.RemoveAt(ientry); 
364   }
365
366   fTrackHypothesys.Delete();
367   fBestHypothesys.Delete();
368   fOriginal.Clear();
369   delete []fCoeficients;
370   fCoeficients=0;
371   Info("Clusters2Tracks","Number of prolonged tracks: %d\n",ntrk);
372   
373   return 0;
374 }
375
376
377 Int_t AliITStrackerMI::PropagateBack(AliESD *event) {
378   //--------------------------------------------------------------------
379   // This functions propagates reconstructed ITS tracks back
380   // The clusters must be loaded !
381   //--------------------------------------------------------------------
382   Int_t nentr=event->GetNumberOfTracks();
383   Info("PropagateBack", "Number of ESD tracks: %d\n", nentr);
384
385   Int_t ntrk=0;
386   for (Int_t i=0; i<nentr; i++) {
387      AliESDtrack *esd=event->GetTrack(i);
388
389      if ((esd->GetStatus()&AliESDtrack::kITSin)==0) continue;
390      if (esd->GetStatus()&AliESDtrack::kITSout) continue;
391
392      AliITStrackMI *t=0;
393      try {
394         t=new AliITStrackMI(*esd);
395      } catch (const Char_t *msg) {
396        //Warning("PropagateBack",msg);
397         delete t;
398         continue;
399      }
400      t->fExpQ = TMath::Max(0.8*t->fESDtrack->GetTPCsignal(),30.);
401
402      ResetTrackToFollow(*t);
403
404      // propagete to vertex [SR, GSI 17.02.2003]
405      // Start Time measurement [SR, GSI 17.02.2003], corrected by I.Belikov
406      if (fTrackToFollow.PropagateTo(3.,0.0028,65.19)) {
407        if (fTrackToFollow.PropagateToVertex()) {
408           fTrackToFollow.StartTimeIntegral();
409        }
410        fTrackToFollow.PropagateTo(3.,-0.0028,65.19);
411      }
412
413      fTrackToFollow.ResetCovariance(); fTrackToFollow.ResetClusters();
414      if (RefitAt(49.,&fTrackToFollow,t)) {
415         if (CorrectForDeadZoneMaterial(&fTrackToFollow)!=0) {
416           //Warning("PropagateBack",
417           //        "failed to correct for the material in the dead zone !\n");
418           delete t;
419           continue;
420         }
421         fTrackToFollow.SetLabel(t->GetLabel());
422         //fTrackToFollow.CookdEdx();
423         CookLabel(&fTrackToFollow,0.); //For comparison only
424         fTrackToFollow.UpdateESDtrack(AliESDtrack::kITSout);
425         //UseClusters(&fTrackToFollow);
426         ntrk++;
427      }
428      delete t;
429   }
430
431   Info("PropagateBack","Number of back propagated ITS tracks: %d\n",ntrk);
432
433   return 0;
434 }
435
436 Int_t AliITStrackerMI::RefitInward(AliESD *event) {
437   //--------------------------------------------------------------------
438   // This functions refits ITS tracks using the 
439   // "inward propagated" TPC tracks
440   // The clusters must be loaded !
441   //--------------------------------------------------------------------
442   RefitV02(event);
443   Int_t nentr=event->GetNumberOfTracks();
444   Info("RefitInward", "Number of ESD tracks: %d\n", nentr);
445
446   Int_t ntrk=0;
447   for (Int_t i=0; i<nentr; i++) {
448     AliESDtrack *esd=event->GetTrack(i);
449
450     if ((esd->GetStatus()&AliESDtrack::kITSout) == 0) continue;
451     if (esd->GetStatus()&AliESDtrack::kITSrefit) continue;
452     if (esd->GetStatus()&AliESDtrack::kTPCout)
453       if ((esd->GetStatus()&AliESDtrack::kTPCrefit)==0) continue;
454
455     AliITStrackMI *t=0;
456     try {
457         t=new AliITStrackMI(*esd);
458     } catch (const Char_t *msg) {
459       //Warning("RefitInward",msg);
460         delete t;
461         continue;
462     }
463     t->fExpQ = TMath::Max(0.8*t->fESDtrack->GetTPCsignal(),30.);
464     if (CorrectForDeadZoneMaterial(t)!=0) {
465       //Warning("RefitInward",
466       //         "failed to correct for the material in the dead zone !\n");
467        delete t;
468        continue;
469     }
470
471     ResetTrackToFollow(*t);
472     fTrackToFollow.ResetClusters();
473
474     if ((esd->GetStatus()&AliESDtrack::kTPCin)==0)
475       fTrackToFollow.ResetCovariance();
476
477     //Refitting...
478     if (RefitAt(3.7, &fTrackToFollow, t)) {
479        fTrackToFollow.SetLabel(t->GetLabel());
480        //       fTrackToFollow.CookdEdx();
481        CookdEdx(&fTrackToFollow);
482
483        CookLabel(&fTrackToFollow,0.0); //For comparison only
484
485        if (fTrackToFollow.PropagateTo(3.,0.0028,65.19)) {//The beam pipe    
486          Double_t a=fTrackToFollow.GetAlpha();
487          Double_t cs=TMath::Cos(a),sn=TMath::Sin(a);
488          Double_t xv= GetX()*cs + GetY()*sn;
489          Double_t yv=-GetX()*sn + GetY()*cs;
490          
491          Double_t c=fTrackToFollow.GetC(), snp=fTrackToFollow.GetSnp();
492          Double_t x=fTrackToFollow.GetX(), y=fTrackToFollow.GetY();
493          Double_t tgfv=-(c*(x-xv)-snp)/(c*(y-yv) + TMath::Sqrt(1.-snp*snp));
494          Double_t fv=TMath::ATan(tgfv);
495
496          cs=TMath::Cos(fv); sn=TMath::Sin(fv);
497          x = xv*cs + yv*sn;
498          yv=-xv*sn + yv*cs; xv=x;
499
500          if (fTrackToFollow.Propagate(fv+a,xv)) {
501             fTrackToFollow.UpdateESDtrack(AliESDtrack::kITSrefit);
502             Float_t d=fTrackToFollow.GetD(GetX(),GetY());
503             Float_t z=fTrackToFollow.GetZ()-GetZ();
504             fTrackToFollow.GetESDtrack()->SetImpactParameters(d,z);
505             //UseClusters(&fTrackToFollow);
506             {
507             AliITSclusterV2 c; c.SetY(yv); c.SetZ(GetZ());
508             c.SetSigmaY2(GetSigmaY()*GetSigmaY());
509             c.SetSigmaZ2(GetSigmaZ()*GetSigmaZ());
510             Double_t chi2=fTrackToFollow.GetPredictedChi2(&c);
511             //Double_t chi2=GetPredictedChi2MI(&fTrackToFollow,&c,fI);
512             if (chi2<kMaxChi2)
513               if (fTrackToFollow.Update(&c,-chi2,0))
514                 //if (UpdateMI(&fTrackToFollow,&c,-chi2,0))
515                 fTrackToFollow.SetConstrainedESDtrack(chi2);            
516             }
517             ntrk++;
518          }
519        }
520     }
521     delete t;
522   }
523
524   Info("RefitInward","Number of refitted tracks: %d\n",ntrk);
525
526   return 0;
527 }
528
529 AliCluster *AliITStrackerMI::GetCluster(Int_t index) const {
530   //--------------------------------------------------------------------
531   //       Return pointer to a given cluster
532   //--------------------------------------------------------------------
533   Int_t l=(index & 0xf0000000) >> 28;
534   Int_t c=(index & 0x0fffffff) >> 00;
535   return fgLayers[l].GetCluster(c);
536 }
537
538
539 void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdindex, Bool_t constrain) 
540 {
541   //--------------------------------------------------------------------
542   // Follow prolongation tree
543   //--------------------------------------------------------------------
544   //
545   AliESDtrack * esd = otrack->fESDtrack;
546   if (esd->GetV0Index(0)>0){
547     //
548     // TEMPORARY SOLLUTION: map V0 indexes to point to proper track
549     //                      mapping of esd track is different as its track in Containers
550     //                      Need something more stable
551     //                      Indexes are set back againg to the ESD track indexes in UpdateTPCV0
552     for (Int_t i=0;i<3;i++){
553       Int_t  index = esd->GetV0Index(i);
554       if (index==0) break;
555       AliESDV0MI * vertex = fEsd->GetV0MI(index);
556       if (vertex->GetStatus()<0) continue;     // rejected V0
557       //
558       if (esd->GetSign()>0) {
559         vertex->SetIndex(0,esdindex);
560       }
561       else{
562         vertex->SetIndex(1,esdindex);
563       }
564     }
565   }
566   TObjArray *bestarray = (TObjArray*)fBestHypothesys.At(esdindex);
567   if (!bestarray){
568     bestarray = new TObjArray(5);
569     fBestHypothesys.AddAt(bestarray,esdindex);
570   }
571
572   //
573   //setup tree of the prolongations
574   //
575   static AliITStrackMI tracks[7][100];
576   AliITStrackMI *currenttrack;
577   static AliITStrackMI currenttrack1;
578   static AliITStrackMI currenttrack2;  
579   static AliITStrackMI backuptrack;
580   Int_t ntracks[7];
581   Int_t nindexes[7][100];
582   Float_t normalizedchi2[100];
583   for (Int_t ilayer=0;ilayer<6;ilayer++) ntracks[ilayer]=0;
584   otrack->fNSkipped=0;
585   new (&(tracks[6][0])) AliITStrackMI(*otrack);
586   ntracks[6]=1;
587   for (Int_t i=0;i<7;i++) nindexes[i][0]=0;
588   // 
589   //
590   // follow prolongations
591   for (Int_t ilayer=5;ilayer>=0;ilayer--){
592     //
593     AliITSlayer &layer=fgLayers[ilayer]; 
594     Double_t r=layer.GetR();
595     ntracks[ilayer]=0;
596     //
597     //
598    Int_t nskipped=0;
599     Float_t nused =0;
600     for (Int_t itrack =0;itrack<ntracks[ilayer+1];itrack++){
601       //set current track
602       if (ntracks[ilayer]>=100) break;  
603       if (tracks[ilayer+1][nindexes[ilayer+1][itrack]].fNSkipped>0) nskipped++;
604       if (tracks[ilayer+1][nindexes[ilayer+1][itrack]].fNUsed>2.) nused++;
605       if (ntracks[ilayer]>15+ilayer){
606         if (itrack>1&&tracks[ilayer+1][nindexes[ilayer+1][itrack]].fNSkipped>0 && nskipped>4+ilayer) continue;
607         if (itrack>1&&tracks[ilayer+1][nindexes[ilayer+1][itrack]].fNUsed>2. && nused>3) continue;
608       }
609
610       new(&currenttrack1)  AliITStrackMI(tracks[ilayer+1][nindexes[ilayer+1][itrack]]);
611       if (ilayer==3 || ilayer==1) {
612         Double_t rs=0.5*(fgLayers[ilayer+1].GetR() + r);
613         Double_t d=0.0034, x0=38.6;
614         if (ilayer==1) {rs=9.; d=0.0097; x0=42;}
615         if (!currenttrack1.PropagateTo(rs,d,x0)) {
616           continue;
617         }
618       }
619       //
620       //find intersection with layer
621       Double_t x,y,z;  
622       if (!currenttrack1.GetGlobalXYZat(r,x,y,z)) {
623         continue;
624       }
625       Double_t phi=TMath::ATan2(y,x);
626       Int_t idet=layer.FindDetectorIndex(phi,z);
627       if (idet<0) {
628         continue;
629       }
630       //propagate to the intersection
631       const AliITSdetector &det=layer.GetDetector(idet);
632       phi=det.GetPhi();
633       new(&currenttrack2)  AliITStrackMI(currenttrack1);
634       if (!currenttrack1.Propagate(phi,det.GetR())) {   
635         continue;
636       }
637       currenttrack2.Propagate(phi,det.GetR());  //
638       currenttrack1.SetDetectorIndex(idet);
639       currenttrack2.SetDetectorIndex(idet);
640       
641       //
642       //
643       Double_t dz=7.5*TMath::Sqrt(currenttrack1.GetSigmaZ2() + 16.*kSigmaZ2[ilayer]);
644       Double_t dy=7.5*TMath::Sqrt(currenttrack1.GetSigmaY2() + 16.*kSigmaY2[ilayer]);
645       //
646       Bool_t isBoundary=kFALSE;
647       if (currenttrack1.GetY()-dy< det.GetYmin()+0.2) isBoundary = kTRUE;  
648       if (currenttrack1.GetY()+dy> det.GetYmax()-0.2) isBoundary = kTRUE;
649       if (currenttrack1.GetZ()-dz< det.GetZmin()+0.2) isBoundary = kTRUE;
650       if (currenttrack1.GetZ()+dz> det.GetZmax()-0.2) isBoundary = kTRUE;
651       
652       if (isBoundary){ // track at boundary between detectors
653         Float_t maxtgl = TMath::Abs(currenttrack1.GetTgl());
654         if (maxtgl>1) maxtgl=1;
655         dz = TMath::Sqrt(dz*dz+0.25*maxtgl*maxtgl);
656         //
657         Float_t maxsnp = TMath::Abs(currenttrack1.GetSnp());
658         if (maxsnp>0.95) continue;
659         //if (maxsnp>0.5) maxsnp=0.5;
660         dy=TMath::Sqrt(dy*dy+0.25*maxsnp*maxsnp);
661       }
662       
663       Double_t zmin=currenttrack1.GetZ() - dz; 
664       Double_t zmax=currenttrack1.GetZ() + dz;
665       Double_t ymin=currenttrack1.GetY() + r*phi - dy;
666       Double_t ymax=currenttrack1.GetY() + r*phi + dy;
667       layer.SelectClusters(zmin,zmax,ymin,ymax); 
668       //
669       // loop over all possible prolongations
670       //
671       Double_t msz=1./((currenttrack1.GetSigmaZ2() + 16.*kSigmaZ2[ilayer]));
672       Double_t msy=1./((currenttrack1.GetSigmaY2() + 16.*kSigmaY2[ilayer]));
673       if (constrain){
674         msy/=60; msz/=60.;
675       }
676       else{
677         msy/=50; msz/=50.;
678       }
679       //
680       const AliITSclusterV2 *c=0; Int_t ci=-1;
681       Double_t chi2=12345.;
682       Int_t deadzone=0;
683       currenttrack = &currenttrack1;
684       while ((c=layer.GetNextCluster(ci))!=0) { 
685         if (ntracks[ilayer]>95) break; //space for skipped clusters  
686         Bool_t change =kFALSE;  
687         if (c->GetQ()==0 && (deadzone==1)) continue;
688         Int_t idet=c->GetDetectorIndex();
689         if (currenttrack->GetDetectorIndex()!=idet) {
690           const AliITSdetector &det=layer.GetDetector(idet);
691           Double_t y,z;
692           if (!currenttrack2.GetProlongationFast(det.GetPhi(),det.GetR(),y,z)) continue;
693           Float_t pz = (z - c->GetZ()) , py=(y - c->GetY());
694           if (pz*pz*msz+py*py*msy>1.) continue;
695           //
696           new (&backuptrack) AliITStrackMI(currenttrack2);
697           change = kTRUE;
698           currenttrack =&currenttrack2;
699           if (!currenttrack->Propagate(det.GetPhi(),det.GetR())) {
700             new (currenttrack) AliITStrackMI(backuptrack);
701             change = kFALSE;
702             continue;
703           }
704           currenttrack->SetDetectorIndex(idet);
705         }
706         else{
707           Float_t pz = (currenttrack->GetZ() - c->GetZ()) , py=(currenttrack->GetY() - c->GetY());
708           if (pz*pz*msz+py*py*msy>1.) continue;
709         }
710
711         chi2=GetPredictedChi2MI(currenttrack,c,ilayer); 
712         if (chi2<kMaxChi2s[ilayer]){
713           if (c->GetQ()==0) deadzone=1;     // take dead zone only once   
714           if (ntracks[ilayer]>=100) continue;
715           AliITStrackMI * updatetrack = new (&tracks[ilayer][ntracks[ilayer]]) AliITStrackMI(*currenttrack);
716           updatetrack->fClIndex[ilayer]=0;
717           if (change){
718             new (&currenttrack2) AliITStrackMI(backuptrack);
719           }
720           if (c->GetQ()!=0){
721             if (!UpdateMI(updatetrack,c,chi2,(ilayer<<28)+ci)) continue; 
722             updatetrack->SetSampledEdx(c->GetQ(),updatetrack->GetNumberOfClusters()-1); //b.b.
723           }
724           else {
725             updatetrack->fNDeadZone++;
726             updatetrack->fDeadZoneProbability=GetDeadZoneProbability(updatetrack->GetZ(),TMath::Sqrt(updatetrack->GetSigmaZ2()));
727           }
728           if (c->IsUsed()){
729             updatetrack->fNUsed++;
730           }
731           Double_t x0;
732           Double_t d=layer.GetThickness(updatetrack->GetY(),updatetrack->GetZ(),x0);
733           updatetrack->CorrectForMaterial(d,x0);          
734           if (constrain) {
735             updatetrack->fConstrain = constrain;
736             fI = ilayer;
737             Double_t d=GetEffectiveThickness(0,0); //Think of this !!!!
738             Double_t xyz[]={GetX(),GetY(),GetZ()};
739             Double_t ptfactor = 1;
740             Double_t ers[]={GetSigmaX()*ptfactor,GetSigmaY()*ptfactor,GetSigmaZ()};
741             Bool_t isPrim = kTRUE;
742             if (ilayer<4){
743               updatetrack->fD[0] = updatetrack->GetD(GetX(),GetY());
744               updatetrack->fD[1] = updatetrack->GetZat(GetX())-GetZ();
745               if ( TMath::Abs(updatetrack->fD[0]/(1.+ilayer))>0.4 ||  TMath::Abs(updatetrack->fD[1]/(1.+ilayer))>0.4) isPrim=kFALSE;
746             }
747             if (isPrim) updatetrack->Improve(d,xyz,ers);
748           } //apply vertex constrain              
749           ntracks[ilayer]++;
750         }  // create new hypothesy 
751       } // loop over possible cluster prolongation      
752       //      if (constrain&&itrack<2&&currenttrack1.fNSkipped==0 && deadzone==0){      
753       if (constrain&&itrack<2&&currenttrack1.fNSkipped==0 && deadzone==0&&ntracks[ilayer]<100){ 
754         AliITStrackMI* vtrack = new (&tracks[ilayer][ntracks[ilayer]]) AliITStrackMI(currenttrack1);
755         vtrack->fClIndex[ilayer]=0;
756         fI = ilayer;
757         Double_t d=GetEffectiveThickness(0,0); //Think of this !!!!
758         Double_t xyz[]={GetX(),GetY(),GetZ()};
759         Double_t ers[]={GetSigmaX(),GetSigmaY(),GetSigmaZ()};
760         vtrack->Improve(d,xyz,ers);
761         vtrack->fNSkipped++;
762         ntracks[ilayer]++;
763       }
764
765       if (constrain&&itrack<1&&TMath::Abs(currenttrack1.fP3)>1.1){  //big theta -- for low mult. runs
766         AliITStrackMI* vtrack = new (&tracks[ilayer][ntracks[ilayer]]) AliITStrackMI(currenttrack1);
767         vtrack->fClIndex[ilayer]=0;
768         fI = ilayer;
769         Double_t d=GetEffectiveThickness(0,0); //Think of this !!!!
770         Double_t xyz[]={GetX(),GetY(),GetZ()};
771         Double_t ers[]={GetSigmaX(),GetSigmaY(),GetSigmaZ()};
772         vtrack->Improve(d,xyz,ers);
773         vtrack->fNDeadZone++;
774         ntracks[ilayer]++;
775       }
776      
777       
778     } //loop over track candidates
779     //
780     //
781     Int_t accepted=0;
782     
783     Int_t golds=0;
784     for (Int_t itrack=0;itrack<ntracks[ilayer];itrack++){
785       normalizedchi2[itrack] = NormalizedChi2(&tracks[ilayer][itrack],ilayer); 
786       if ( normalizedchi2[itrack]<3+0.5*ilayer) golds++;
787       if (ilayer>4) accepted++;
788       else{
789         if ( constrain && normalizedchi2[itrack]<kMaxNormChi2C[ilayer]+1) accepted++;
790         if (!constrain && normalizedchi2[itrack]<kMaxNormChi2NonC[ilayer]+1) accepted++;
791       }
792     }
793     TMath::Sort(ntracks[ilayer],normalizedchi2,nindexes[ilayer],kFALSE);
794     ntracks[ilayer] = TMath::Min(accepted,7+2*ilayer);
795     if (ntracks[ilayer]<golds+2+ilayer) ntracks[ilayer]=TMath::Min(golds+2+ilayer,accepted);
796     if (ntracks[ilayer]>90) ntracks[ilayer]=90; 
797   } //loop over layers
798   //printf("%d\t%d\t%d\t%d\t%d\t%d\n",ntracks[0],ntracks[1],ntracks[2],ntracks[3],ntracks[4],ntracks[5]);
799   Int_t max = constrain? 20: 5;
800
801   for (Int_t i=0;i<TMath::Min(max,ntracks[0]);i++) {
802     AliITStrackMI & track= tracks[0][nindexes[0][i]];
803     if (track.GetNumberOfClusters()<2) continue;
804     if (!constrain&&track.fNormChi2[0]>7.)continue;
805     AddTrackHypothesys(new AliITStrackMI(track), esdindex);
806   }
807   for (Int_t i=0;i<TMath::Min(2,ntracks[1]);i++) {
808     AliITStrackMI & track= tracks[1][nindexes[1][i]];
809     if (track.GetNumberOfClusters()<4) continue;
810     if (!constrain&&track.fNormChi2[1]>7.)continue;
811     if (constrain) track.fNSkipped+=1;
812     if (!constrain) {
813       track.fD[0] = track.GetD(GetX(),GetY());   
814       track.fNSkipped+=4./(4.+8.*TMath::Abs(track.fD[0]));
815       if (track.fN+track.fNDeadZone+track.fNSkipped>6) {
816         track.fNSkipped = 6-track.fN+track.fNDeadZone;
817       }
818     }
819     AddTrackHypothesys(new AliITStrackMI(track), esdindex);
820   }
821   //}
822   
823   if (!constrain){  
824     for (Int_t i=0;i<TMath::Min(2,ntracks[2]);i++) {
825       AliITStrackMI & track= tracks[2][nindexes[2][i]];
826       if (track.GetNumberOfClusters()<3) continue;
827       if (!constrain&&track.fNormChi2[2]>7.)continue;
828       if (constrain) track.fNSkipped+=2;      
829       if (!constrain){
830         track.fD[0] = track.GetD(GetX(),GetY());
831         track.fNSkipped+= 7./(7.+8.*TMath::Abs(track.fD[0]));
832         if (track.fN+track.fNDeadZone+track.fNSkipped>6) {
833           track.fNSkipped = 6-track.fN+track.fNDeadZone;
834         }
835       }
836       AddTrackHypothesys(new AliITStrackMI(track), esdindex);
837     }
838   }
839   
840   if (!constrain){
841     //
842     // register best tracks - important for V0 finder
843     //
844     for (Int_t ilayer=0;ilayer<5;ilayer++){
845       if (ntracks[ilayer]==0) continue;
846       AliITStrackMI & track= tracks[ilayer][nindexes[ilayer][0]];
847       if (track.GetNumberOfClusters()<1) continue;
848       CookLabel(&track,0);
849       bestarray->AddAt(new AliITStrackMI(track),ilayer);
850     }
851   }
852   //
853   // update TPC V0 information
854   //
855   if (otrack->fESDtrack->GetV0Index(0)>0){    
856     Float_t fprimvertex[3]={GetX(),GetY(),GetZ()};
857     for (Int_t i=0;i<3;i++){
858       Int_t  index = otrack->fESDtrack->GetV0Index(i); 
859       if (index==0) break;
860       AliESDV0MI * vertex = fEsd->GetV0MI(index);
861       if (vertex->GetStatus()<0) continue;     // rejected V0
862       //
863       if (otrack->fP4>0) {
864         vertex->SetIndex(0,esdindex);
865       }
866       else{
867         vertex->SetIndex(1,esdindex);
868       }
869       //find nearest layer with track info
870       Int_t nearestold  = GetNearestLayer(vertex->GetXrp());
871       Int_t nearest     = nearestold; 
872       for (Int_t ilayer =nearest;ilayer<8;ilayer++){
873         if (ntracks[nearest]==0){
874           nearest = ilayer;
875         }
876       }
877       //
878       AliITStrackMI & track= tracks[nearest][nindexes[nearest][0]];
879       if (nearestold<5&&nearest<5){
880         Bool_t accept = track.fNormChi2[nearest]<10; 
881         if (accept){
882           if (track.fP4>0) {
883             vertex->SetP(track);
884             vertex->Update(fprimvertex);
885             //      vertex->SetIndex(0,track.fESDtrack->GetID()); 
886             if (track.GetNumberOfClusters()>2) AddTrackHypothesys(new AliITStrackMI(track), esdindex);
887           }else{
888             vertex->SetM(track);
889             vertex->Update(fprimvertex);
890             //vertex->SetIndex(1,track.fESDtrack->GetID());
891             if (track.GetNumberOfClusters()>2) AddTrackHypothesys(new AliITStrackMI(track), esdindex);
892           }
893           vertex->SetStatus(vertex->GetStatus()+1);
894         }else{
895           //  vertex->SetStatus(-2);  // reject V0  - not enough clusters
896         }
897       }
898       // if (nearestold>3){
899 //      Int_t indexlayer = (ntracks[0]>0)? 0:1;
900 //      if (ntracks[indexlayer]>0){
901 //        AliITStrackMI & track= tracks[indexlayer][nindexes[indexlayer][0]];
902 //        if (track.GetNumberOfClusters()>4&&track.fNormChi2[indexlayer]<4){
903 //          vertex->SetStatus(-1);  // reject V0 - clusters before
904 //        }
905 //      }
906 //      }
907     }
908   }  
909 }
910
911
912 AliITStrackerMI::AliITSlayer & AliITStrackerMI::GetLayer(Int_t layer) const
913 {
914   //--------------------------------------------------------------------
915   //
916   //
917   return fgLayers[layer];
918 }
919 AliITStrackerMI::AliITSlayer::AliITSlayer() {
920   //--------------------------------------------------------------------
921   //default AliITSlayer constructor
922   //--------------------------------------------------------------------
923   fN=0;
924   fDetectors=0;
925   fSkip = 0;
926   fCurrentSlice=-1;
927   for (Int_t i=0; i<kMaxClusterPerLayer;i++) {
928     fClusterWeight[i]=0;
929     fClusterTracks[0][i]=-1;
930     fClusterTracks[1][i]=-1;
931     fClusterTracks[2][i]=-1;    
932     fClusterTracks[3][i]=-1;    
933   }
934 }
935
936 AliITStrackerMI::AliITSlayer::
937 AliITSlayer(Double_t r,Double_t p,Double_t z,Int_t nl,Int_t nd) {
938   //--------------------------------------------------------------------
939   //main AliITSlayer constructor
940   //--------------------------------------------------------------------
941   fR=r; fPhiOffset=p; fZOffset=z;
942   fNladders=nl; fNdetectors=nd;
943   fDetectors=new AliITSdetector[fNladders*fNdetectors];
944
945   fN=0;
946   fI=0;
947   fSkip = 0;
948   fRoad=2*fR*TMath::Sqrt(3.14/1.);//assuming that there's only one cluster
949 }
950
951 AliITStrackerMI::AliITSlayer::~AliITSlayer() {
952   //--------------------------------------------------------------------
953   // AliITSlayer destructor
954   //--------------------------------------------------------------------
955   delete[] fDetectors;
956   for (Int_t i=0; i<fN; i++) delete fClusters[i];
957   for (Int_t i=0; i<kMaxClusterPerLayer;i++) {
958     fClusterWeight[i]=0;
959     fClusterTracks[0][i]=-1;
960     fClusterTracks[1][i]=-1;
961     fClusterTracks[2][i]=-1;    
962     fClusterTracks[3][i]=-1;    
963   }
964 }
965
966 void AliITStrackerMI::AliITSlayer::ResetClusters() {
967   //--------------------------------------------------------------------
968   // This function removes loaded clusters
969   //--------------------------------------------------------------------
970   for (Int_t i=0; i<fN; i++) delete fClusters[i];
971   for (Int_t i=0; i<kMaxClusterPerLayer;i++){
972     fClusterWeight[i]=0;
973     fClusterTracks[0][i]=-1;
974     fClusterTracks[1][i]=-1;
975     fClusterTracks[2][i]=-1;    
976     fClusterTracks[3][i]=-1;  
977   }
978   
979   fN=0;
980   fI=0;
981 }
982
983 void AliITStrackerMI::AliITSlayer::ResetWeights() {
984   //--------------------------------------------------------------------
985   // This function reset weights of the clusters
986   //--------------------------------------------------------------------
987   for (Int_t i=0; i<kMaxClusterPerLayer;i++) {
988     fClusterWeight[i]=0;
989     fClusterTracks[0][i]=-1;
990     fClusterTracks[1][i]=-1;
991     fClusterTracks[2][i]=-1;    
992     fClusterTracks[3][i]=-1;  
993   }
994   for (Int_t i=0; i<fN;i++) {
995     AliITSclusterV2 * cl = (AliITSclusterV2*)GetCluster(i);
996     if (cl&&cl->IsUsed()) cl->Use();
997   }
998
999 }
1000
1001 void AliITStrackerMI::AliITSlayer::ResetRoad() {
1002   //--------------------------------------------------------------------
1003   // This function calculates the road defined by the cluster density
1004   //--------------------------------------------------------------------
1005   Int_t n=0;
1006   for (Int_t i=0; i<fN; i++) {
1007      if (TMath::Abs(fClusters[i]->GetZ())<fR) n++;
1008   }
1009   //if (n>1) fRoad=2*fR*TMath::Sqrt(3.14/n);
1010   if (n>1) fRoad=2*fR*TMath::Sqrt(3.14/n);
1011 }
1012
1013
1014 Int_t AliITStrackerMI::AliITSlayer::InsertCluster(AliITSclusterV2 *c) {
1015   //--------------------------------------------------------------------
1016   //This function adds a cluster to this layer
1017   //--------------------------------------------------------------------
1018   if (fN==kMaxClusterPerLayer) {
1019     ::Error("InsertCluster","Too many clusters !\n");
1020     return 1;
1021   }
1022   fCurrentSlice=-1;
1023   fClusters[fN]=c;
1024   fN++;
1025   AliITSdetector &det=GetDetector(c->GetDetectorIndex());    
1026   if (c->GetY()<det.GetYmin()) det.SetYmin(c->GetY());
1027   if (c->GetY()>det.GetYmax()) det.SetYmax(c->GetY());
1028   if (c->GetZ()<det.GetZmin()) det.SetZmin(c->GetZ());
1029   if (c->GetZ()>det.GetZmax()) det.SetZmax(c->GetZ());
1030                              
1031   return 0;
1032 }
1033
1034 void  AliITStrackerMI::AliITSlayer::SortClusters()
1035 {
1036   //
1037   //sort clusters
1038   //
1039   AliITSclusterV2 **clusters = new AliITSclusterV2*[fN];
1040   Float_t *z                = new Float_t[fN];
1041   Int_t   * index           = new Int_t[fN];
1042   //
1043   for (Int_t i=0;i<fN;i++){
1044     z[i] = fClusters[i]->GetZ();
1045   }
1046   TMath::Sort(fN,z,index,kFALSE);
1047   for (Int_t i=0;i<fN;i++){
1048     clusters[i] = fClusters[index[i]];
1049   }
1050   //
1051   for (Int_t i=0;i<fN;i++){
1052     fClusters[i] = clusters[i];
1053     fZ[i]        = fClusters[i]->GetZ();
1054     AliITSdetector &det=GetDetector(fClusters[i]->GetDetectorIndex());    
1055     Double_t y=fR*det.GetPhi() + fClusters[i]->GetY();
1056     if (y>2.*fR*TMath::Pi()) y -= 2.*fR*TMath::Pi();
1057     fY[i] = y;
1058   }
1059   delete[] index;
1060   delete[] z;
1061   delete[] clusters;
1062   //
1063
1064   fYB[0]=10000000;
1065   fYB[1]=-10000000;
1066   for (Int_t i=0;i<fN;i++){
1067     if (fY[i]<fYB[0]) fYB[0]=fY[i];
1068     if (fY[i]>fYB[1]) fYB[1]=fY[i];
1069     fClusterIndex[i] = i;
1070   }
1071   //
1072   // fill slices
1073   fDy5 = (fYB[1]-fYB[0])/5.;
1074   fDy10 = (fYB[1]-fYB[0])/10.;
1075   fDy20 = (fYB[1]-fYB[0])/20.;
1076   for (Int_t i=0;i<6;i++)  fN5[i] =0;  
1077   for (Int_t i=0;i<11;i++) fN10[i]=0;  
1078   for (Int_t i=0;i<21;i++) fN20[i]=0;
1079   //  
1080   for (Int_t i=0;i<6;i++) {fBy5[i][0] =  fYB[0]+(i-0.75)*fDy5; fBy5[i][1] =  fYB[0]+(i+0.75)*fDy5;}
1081   for (Int_t i=0;i<11;i++) {fBy10[i][0] =  fYB[0]+(i-0.75)*fDy10; fBy10[i][1] =  fYB[0]+(i+0.75)*fDy10;} 
1082   for (Int_t i=0;i<21;i++) {fBy20[i][0] =  fYB[0]+(i-0.75)*fDy20; fBy20[i][1] =  fYB[0]+(i+0.75)*fDy20;}
1083   //
1084   //
1085   for (Int_t i=0;i<fN;i++)
1086     for (Int_t irot=-1;irot<=1;irot++){
1087       Float_t curY = fY[i]+irot*TMath::TwoPi()*fR; 
1088       // slice 5
1089       for (Int_t slice=0; slice<6;slice++){
1090         if (fBy5[slice][0]<curY && curY<fBy5[slice][1]&&fN5[slice]<kMaxClusterPerLayer5){
1091           fClusters5[slice][fN5[slice]] = fClusters[i];
1092           fY5[slice][fN5[slice]] = curY;
1093           fZ5[slice][fN5[slice]] = fZ[i];
1094           fClusterIndex5[slice][fN5[slice]]=i;
1095           fN5[slice]++;
1096         }
1097       }
1098       // slice 10
1099       for (Int_t slice=0; slice<11;slice++){
1100         if (fBy10[slice][0]<curY && curY<fBy10[slice][1]&&fN10[slice]<kMaxClusterPerLayer10){
1101           fClusters10[slice][fN10[slice]] = fClusters[i];
1102           fY10[slice][fN10[slice]] = curY;
1103           fZ10[slice][fN10[slice]] = fZ[i];
1104           fClusterIndex10[slice][fN10[slice]]=i;
1105           fN10[slice]++;
1106         }
1107       }
1108       // slice 20
1109       for (Int_t slice=0; slice<21;slice++){
1110         if (fBy20[slice][0]<curY && curY<fBy20[slice][1]&&fN20[slice]<kMaxClusterPerLayer20){
1111           fClusters20[slice][fN20[slice]] = fClusters[i];
1112           fY20[slice][fN20[slice]] = curY;
1113           fZ20[slice][fN20[slice]] = fZ[i];
1114           fClusterIndex20[slice][fN20[slice]]=i;
1115           fN20[slice]++;
1116         }
1117       }      
1118     }
1119
1120   //
1121   // consistency check
1122   //
1123   for (Int_t i=0;i<fN-1;i++){
1124     if (fZ[i]>fZ[i+1]){
1125       printf("Bugg\n");
1126     }
1127   }
1128   //
1129   for (Int_t slice=0;slice<21;slice++)
1130   for (Int_t i=0;i<fN20[slice]-1;i++){
1131     if (fZ20[slice][i]>fZ20[slice][i+1]){
1132       printf("Bugg\n");
1133     }
1134   }
1135
1136
1137 }
1138
1139
1140 Int_t AliITStrackerMI::AliITSlayer::FindClusterIndex(Float_t z) const {
1141   //--------------------------------------------------------------------
1142   // This function returns the index of the nearest cluster 
1143   //--------------------------------------------------------------------
1144   Int_t ncl=0;
1145   const Float_t *zcl;  
1146   if (fCurrentSlice<0) {
1147     ncl = fN;
1148     zcl   = fZ;
1149   }
1150   else{
1151     ncl   = fNcs;
1152     zcl   = fZcs;;
1153   }
1154   
1155   if (ncl==0) return 0;
1156   Int_t b=0, e=ncl-1, m=(b+e)/2;
1157   for (; b<e; m=(b+e)/2) {
1158     //    if (z > fClusters[m]->GetZ()) b=m+1;
1159     if (z > zcl[m]) b=m+1;
1160     else e=m; 
1161   }
1162   return m;
1163 }
1164
1165
1166 void AliITStrackerMI::AliITSlayer::
1167 SelectClusters(Double_t zmin,Double_t zmax,Double_t ymin, Double_t ymax) {
1168   //--------------------------------------------------------------------
1169   // This function sets the "window"
1170   //--------------------------------------------------------------------
1171  
1172   Double_t circle=2*TMath::Pi()*fR;
1173   fYmin = ymin; fYmax =ymax;
1174   Float_t ymiddle = (fYmin+fYmax)*0.5;
1175   if (ymiddle<fYB[0]) {fYmin+=circle; fYmax+=circle;ymiddle+=circle;}
1176   else{
1177     if (ymiddle>fYB[1]) {fYmin-=circle; fYmax-=circle;ymiddle-=circle;}
1178   }
1179   //
1180   fCurrentSlice =-1;
1181   // defualt take all
1182   fClustersCs = fClusters;
1183   fClusterIndexCs = fClusterIndex;
1184   fYcs  = fY;
1185   fZcs  = fZ;
1186   fNcs  = fN;
1187   //
1188   //is in 20 slice?
1189   if (fCurrentSlice<0&&TMath::Abs(fYmax-fYmin)<1.49*fDy20){
1190     Int_t slice = int(0.5+(ymiddle-fYB[0])/fDy20);
1191     if (slice<0) slice=0;
1192     if (slice>20) slice=20;
1193     Bool_t isOK = (fYmin>fBy20[slice][0]&&fYmax<fBy20[slice][1]);
1194     if (isOK) {
1195       fCurrentSlice=slice;
1196       fClustersCs = fClusters20[fCurrentSlice];
1197       fClusterIndexCs = fClusterIndex20[fCurrentSlice];
1198       fYcs  = fY20[fCurrentSlice];
1199       fZcs  = fZ20[fCurrentSlice];
1200       fNcs  = fN20[fCurrentSlice];
1201     }
1202   }  
1203   //
1204   //is in 10 slice?
1205   if (fCurrentSlice<0&&TMath::Abs(fYmax-fYmin)<1.49*fDy10){
1206     Int_t slice = int(0.5+(ymiddle-fYB[0])/fDy10);
1207     if (slice<0) slice=0;
1208     if (slice>10) slice=10;
1209     Bool_t isOK = (fYmin>fBy10[slice][0]&&fYmax<fBy10[slice][1]);
1210     if (isOK) {
1211       fCurrentSlice=slice;
1212       fClustersCs = fClusters10[fCurrentSlice];
1213       fClusterIndexCs = fClusterIndex10[fCurrentSlice];
1214       fYcs  = fY10[fCurrentSlice];
1215       fZcs  = fZ10[fCurrentSlice];
1216       fNcs  = fN10[fCurrentSlice];
1217     }
1218   }  
1219   //
1220   //is in 5 slice?
1221   if (fCurrentSlice<0&&TMath::Abs(fYmax-fYmin)<1.49*fDy5){
1222     Int_t slice = int(0.5+(ymiddle-fYB[0])/fDy5);
1223     if (slice<0) slice=0;
1224     if (slice>5) slice=5;
1225     Bool_t isOK = (fYmin>fBy5[slice][0]&&fYmax<fBy5[slice][1]);
1226     if ( isOK){
1227       fCurrentSlice=slice;
1228       fClustersCs = fClusters5[fCurrentSlice];
1229       fClusterIndexCs = fClusterIndex5[fCurrentSlice];
1230       fYcs  = fY5[fCurrentSlice];
1231       fZcs  = fZ5[fCurrentSlice];
1232       fNcs  = fN5[fCurrentSlice];
1233     }
1234   }  
1235   //  
1236   fI=FindClusterIndex(zmin); fZmax=zmax;
1237   fImax = TMath::Min(FindClusterIndex(zmax)+1,fNcs);
1238   fSkip = 0;
1239   fAccepted =0;
1240 }
1241
1242
1243
1244
1245 Int_t AliITStrackerMI::AliITSlayer::
1246 FindDetectorIndex(Double_t phi, Double_t z) const {
1247   //--------------------------------------------------------------------
1248   //This function finds the detector crossed by the track
1249   //--------------------------------------------------------------------
1250   Double_t dphi=-(phi-fPhiOffset);
1251   if      (dphi <  0) dphi += 2*TMath::Pi();
1252   else if (dphi >= 2*TMath::Pi()) dphi -= 2*TMath::Pi();
1253   Int_t np=Int_t(dphi*fNladders*0.5/TMath::Pi()+0.5);
1254   if (np>=fNladders) np-=fNladders;
1255   if (np<0)          np+=fNladders;
1256
1257   Double_t dz=fZOffset-z;
1258   Int_t nz=Int_t(dz*(fNdetectors-1)*0.5/fZOffset+0.5);
1259   if (nz>=fNdetectors) return -1;
1260   if (nz<0)            return -1;
1261
1262   return np*fNdetectors + nz;
1263 }
1264
1265
1266 const AliITSclusterV2 *AliITStrackerMI::AliITSlayer::GetNextCluster(Int_t &ci){
1267   //--------------------------------------------------------------------
1268   // This function returns clusters within the "window" 
1269   //--------------------------------------------------------------------
1270
1271   if (fCurrentSlice<0){
1272     Double_t rpi2 = 2.*fR*TMath::Pi();
1273     for (Int_t i=fI; i<fImax; i++) {
1274       Double_t y = fY[i];
1275       if (fYmax<y) y -= rpi2;
1276       if (fYmin>y) y += rpi2;
1277       if (y<fYmin) continue;
1278       if (y>fYmax) continue;
1279       if (fClusters[i]->GetQ()==0&&fSkip==2) continue;
1280       ci=i;
1281       fI=i+1;
1282       return fClusters[i];
1283     }
1284   }
1285   else{
1286     for (Int_t i=fI; i<fImax; i++) {
1287       if (fYcs[i]<fYmin) continue;
1288       if (fYcs[i]>fYmax) continue;
1289       if (fClustersCs[i]->GetQ()==0&&fSkip==2) continue;
1290       ci=fClusterIndexCs[i];
1291       fI=i+1;
1292       return fClustersCs[i];
1293     }
1294   }
1295   return 0;
1296 }
1297
1298
1299
1300 Double_t AliITStrackerMI::AliITSlayer::GetThickness(Double_t y,Double_t z,Double_t &x0)
1301 const {
1302   //--------------------------------------------------------------------
1303   //This function returns the layer thickness at this point (units X0)
1304   //--------------------------------------------------------------------
1305   Double_t d=0.0085;
1306   x0=21.82;
1307   if (43<fR&&fR<45) { //SSD2
1308      Double_t dd=0.0034;
1309      d=dd;
1310      if (TMath::Abs(y-0.00)>3.40) d+=dd;
1311      if (TMath::Abs(y-1.90)<0.45) {d+=(0.013-0.0034);}
1312      if (TMath::Abs(y+1.90)<0.45) {d+=(0.013-0.0034);}
1313      for (Int_t i=0; i<12; i++) {
1314        if (TMath::Abs(z-3.9*(i+0.5))<0.15) {
1315           if (TMath::Abs(y-0.00)>3.40) d+=dd;
1316           d+=0.0034; 
1317           break;
1318        }
1319        if (TMath::Abs(z+3.9*(i+0.5))<0.15) {
1320           if (TMath::Abs(y-0.00)>3.40) d+=dd;
1321           d+=0.0034; 
1322           break;
1323        }         
1324        if (TMath::Abs(z-3.4-3.9*i)<0.50) {d+=(0.016-0.0034); break;}
1325        if (TMath::Abs(z+0.5+3.9*i)<0.50) {d+=(0.016-0.0034); break;}
1326      }
1327   } else 
1328   if (37<fR&&fR<41) { //SSD1
1329      Double_t dd=0.0034;
1330      d=dd;
1331      if (TMath::Abs(y-0.00)>3.40) d+=dd;
1332      if (TMath::Abs(y-1.90)<0.45) {d+=(0.013-0.0034);}
1333      if (TMath::Abs(y+1.90)<0.45) {d+=(0.013-0.0034);}
1334      for (Int_t i=0; i<11; i++) {
1335        if (TMath::Abs(z-3.9*i)<0.15) {
1336           if (TMath::Abs(y-0.00)>3.40) d+=dd;
1337           d+=dd; 
1338           break;
1339        }
1340        if (TMath::Abs(z+3.9*i)<0.15) {
1341           if (TMath::Abs(y-0.00)>3.40) d+=dd;
1342           d+=dd; 
1343           break;
1344        }         
1345        if (TMath::Abs(z-1.85-3.9*i)<0.50) {d+=(0.016-0.0034); break;}
1346        if (TMath::Abs(z+2.05+3.9*i)<0.50) {d+=(0.016-0.0034); break;}         
1347      }
1348   } else
1349   if (13<fR&&fR<26) { //SDD
1350      Double_t dd=0.0033;
1351      d=dd;
1352      if (TMath::Abs(y-0.00)>3.30) d+=dd;
1353
1354      if (TMath::Abs(y-1.80)<0.55) {
1355         d+=0.016;
1356         for (Int_t j=0; j<20; j++) {
1357           if (TMath::Abs(z+0.7+1.47*j)<0.12) {d+=0.08; x0=9.; break;}
1358           if (TMath::Abs(z-0.7-1.47*j)<0.12) {d+=0.08; x0=9.; break;}
1359         } 
1360      }
1361      if (TMath::Abs(y+1.80)<0.55) {
1362         d+=0.016;
1363         for (Int_t j=0; j<20; j++) {
1364           if (TMath::Abs(z-0.7-1.47*j)<0.12) {d+=0.08; x0=9.; break;}
1365           if (TMath::Abs(z+0.7+1.47*j)<0.12) {d+=0.08; x0=9.; break;}
1366         } 
1367      }
1368
1369      for (Int_t i=0; i<4; i++) {
1370        if (TMath::Abs(z-7.3*i)<0.60) {
1371           d+=dd;
1372           if (TMath::Abs(y-0.00)>3.30) d+=dd; 
1373           break;
1374        }
1375        if (TMath::Abs(z+7.3*i)<0.60) {
1376           d+=dd; 
1377           if (TMath::Abs(y-0.00)>3.30) d+=dd; 
1378           break;
1379        }
1380      }
1381   } else
1382   if (6<fR&&fR<8) {   //SPD2
1383      Double_t dd=0.0063; x0=21.5;
1384      d=dd;
1385      if (TMath::Abs(y-3.08)>0.5) d+=dd;
1386      //if (TMath::Abs(y-3.08)>0.45) d+=dd;
1387      if (TMath::Abs(y-3.03)<0.10) {d+=0.014;}
1388   } else
1389   if (3<fR&&fR<5) {   //SPD1
1390      Double_t dd=0.0063; x0=21.5;
1391      d=dd;
1392      if (TMath::Abs(y+0.21)>0.6) d+=dd;
1393      //if (TMath::Abs(y+0.21)>0.45) d+=dd;
1394      if (TMath::Abs(y+0.10)<0.10) {d+=0.014;}
1395   }
1396
1397   return d;
1398 }
1399
1400 Double_t AliITStrackerMI::GetEffectiveThickness(Double_t y,Double_t z) const
1401 {
1402   //--------------------------------------------------------------------
1403   //Returns the thickness between the current layer and the vertex (units X0)
1404   //--------------------------------------------------------------------
1405   Double_t d=0.0028*3*3; //beam pipe
1406   Double_t x0=0;
1407
1408   Double_t xn=fgLayers[fI].GetR();
1409   for (Int_t i=0; i<fI; i++) {
1410     Double_t xi=fgLayers[i].GetR();
1411     d+=fgLayers[i].GetThickness(y,z,x0)*xi*xi;
1412   }
1413
1414   if (fI>1) {
1415     Double_t xi=9.;
1416     d+=0.0097*xi*xi;
1417   }
1418
1419   if (fI>3) {
1420     Double_t xi=0.5*(fgLayers[3].GetR()+fgLayers[4].GetR());
1421     d+=0.0034*xi*xi;
1422   }
1423
1424   return d/(xn*xn);
1425 }
1426
1427 Int_t AliITStrackerMI::AliITSlayer::InRoad() const {
1428   //--------------------------------------------------------------------
1429   // This function returns number of clusters within the "window" 
1430   //--------------------------------------------------------------------
1431   Int_t ncl=0;
1432   for (Int_t i=fI; i<fN; i++) {
1433     const AliITSclusterV2 *c=fClusters[i];
1434     if (c->GetZ() > fZmax) break;
1435     if (c->IsUsed()) continue;
1436     const AliITSdetector &det=GetDetector(c->GetDetectorIndex());    
1437     Double_t y=fR*det.GetPhi() + c->GetY();
1438
1439     if (y>2.*fR*TMath::Pi()) y -= 2*fR*TMath::Pi();
1440     if (y>1.*fR*TMath::Pi() && fYmax<y) y -= 2*fR*TMath::Pi();
1441
1442     if (y<fYmin) continue;
1443     if (y>fYmax) continue;
1444     ncl++;
1445   }
1446   return ncl;
1447 }
1448
1449 Bool_t 
1450 AliITStrackerMI::RefitAt(Double_t xx,AliITStrackMI *t,const AliITStrackMI *c) {
1451   //--------------------------------------------------------------------
1452   // This function refits the track "t" at the position "x" using
1453   // the clusters from "c"
1454   //--------------------------------------------------------------------
1455   Int_t index[kMaxLayer];
1456   Int_t k;
1457   for (k=0; k<kMaxLayer; k++) index[k]=-1;
1458   Int_t nc=c->GetNumberOfClusters();
1459   for (k=0; k<nc; k++) { 
1460     Int_t idx=c->GetClusterIndex(k),nl=(idx&0xf0000000)>>28;
1461     index[nl]=idx; 
1462   }
1463
1464   Int_t from, to, step;
1465   if (xx > t->GetX()) {
1466       from=0; to=kMaxLayer;
1467       step=+1;
1468   } else {
1469       from=kMaxLayer-1; to=-1;
1470       step=-1;
1471   }
1472
1473   for (Int_t i=from; i != to; i += step) {
1474      AliITSlayer &layer=fgLayers[i];
1475      Double_t r=layer.GetR();
1476  
1477      {
1478      Double_t hI=i-0.5*step; 
1479      if (TMath::Abs(hI-1.5)<0.01 || TMath::Abs(hI-3.5)<0.01) {             
1480         Double_t rs=0.5*(fgLayers[i-step].GetR() + r);
1481         Double_t d=0.0034, x0=38.6; 
1482         if (TMath::Abs(hI-1.5)<0.01) {rs=9.; d=0.0097; x0=42;}
1483         if (!t->PropagateTo(rs,-step*d,x0)) {
1484           return kFALSE;
1485         }
1486      }
1487      }
1488
1489      // remember old position [SR, GSI 18.02.2003]
1490      Double_t oldX=0., oldY=0., oldZ=0.;
1491      if (t->IsStartedTimeIntegral() && step==1) {
1492         t->GetGlobalXYZat(t->GetX(),oldX,oldY,oldZ);
1493      }
1494      //
1495
1496      Double_t x,y,z;
1497      if (!t->GetGlobalXYZat(r,x,y,z)) { 
1498        return kFALSE;
1499      }
1500      Double_t phi=TMath::ATan2(y,x);
1501      Int_t idet=layer.FindDetectorIndex(phi,z);
1502      if (idet<0) { 
1503        return kFALSE;
1504      }
1505      const AliITSdetector &det=layer.GetDetector(idet);
1506      phi=det.GetPhi();
1507      if (!t->Propagate(phi,det.GetR())) {
1508        return kFALSE;
1509      }
1510      t->SetDetectorIndex(idet);
1511
1512      const AliITSclusterV2 *cl=0;
1513      Double_t maxchi2=1000.*kMaxChi2;
1514
1515      Int_t idx=index[i];
1516      if (idx>0) {
1517         const AliITSclusterV2 *c=(AliITSclusterV2 *)GetCluster(idx); 
1518         if (c){
1519           if (idet != c->GetDetectorIndex()) {
1520             idet=c->GetDetectorIndex();
1521             const AliITSdetector &det=layer.GetDetector(idet);
1522             if (!t->Propagate(det.GetPhi(),det.GetR())) {
1523               return kFALSE;
1524             }
1525             t->SetDetectorIndex(idet);
1526           }
1527           //Double_t chi2=t->GetPredictedChi2(c);
1528           Int_t layer = (idx & 0xf0000000) >> 28;;
1529           Double_t chi2=GetPredictedChi2MI(t,c,layer);
1530           if (chi2<maxchi2) { 
1531             cl=c; 
1532             maxchi2=chi2; 
1533           } else {
1534             return kFALSE;
1535           }
1536         }
1537      }
1538      /*
1539      if (cl==0)
1540      if (t->GetNumberOfClusters()>2) {
1541         Double_t dz=4*TMath::Sqrt(t->GetSigmaZ2()+kSigmaZ2[i]);
1542         Double_t dy=4*TMath::Sqrt(t->GetSigmaY2()+kSigmaY2[i]);
1543         Double_t zmin=t->GetZ() - dz;
1544         Double_t zmax=t->GetZ() + dz;
1545         Double_t ymin=t->GetY() + phi*r - dy;
1546         Double_t ymax=t->GetY() + phi*r + dy;
1547         layer.SelectClusters(zmin,zmax,ymin,ymax);
1548
1549         const AliITSclusterV2 *c=0; Int_t ci=-1;
1550         while ((c=layer.GetNextCluster(ci))!=0) {
1551            if (idet != c->GetDetectorIndex()) continue;
1552            Double_t chi2=t->GetPredictedChi2(c);
1553            if (chi2<maxchi2) { cl=c; maxchi2=chi2; idx=ci; }
1554         }
1555      }
1556      */
1557      if (cl) {
1558        //if (!t->Update(cl,maxchi2,idx)) {
1559        if (!UpdateMI(t,cl,maxchi2,idx)) {
1560           return kFALSE;
1561        }
1562        t->SetSampledEdx(cl->GetQ(),t->GetNumberOfClusters()-1);
1563      }
1564
1565      {
1566      Double_t x0;
1567      Double_t d=layer.GetThickness(t->GetY(),t->GetZ(),x0);
1568      t->CorrectForMaterial(-step*d,x0);
1569      }
1570                  
1571      // track time update [SR, GSI 17.02.2003]
1572      if (t->IsStartedTimeIntegral() && step==1) {
1573         Double_t newX, newY, newZ;
1574         t->GetGlobalXYZat(t->GetX(),newX,newY,newZ);
1575         Double_t dL2 = (oldX-newX)*(oldX-newX) + (oldY-newY)*(oldY-newY) + 
1576                        (oldZ-newZ)*(oldZ-newZ);
1577         t->AddTimeStep(TMath::Sqrt(dL2));
1578      }
1579      //
1580
1581   }
1582
1583   if (!t->PropagateTo(xx,0.,0.)) return kFALSE;
1584   return kTRUE;
1585 }
1586
1587
1588 Bool_t 
1589 AliITStrackerMI::RefitAt(Double_t xx,AliITStrackMI *t,const Int_t *clindex) {
1590   //--------------------------------------------------------------------
1591   // This function refits the track "t" at the position "x" using
1592   // the clusters from array
1593   //--------------------------------------------------------------------
1594   Int_t index[kMaxLayer];
1595   Int_t k;
1596   for (k=0; k<kMaxLayer; k++) index[k]=-1;
1597   //
1598   for (k=0; k<kMaxLayer; k++) { 
1599     index[k]=clindex[k]; 
1600   }
1601
1602   Int_t from, to, step;
1603   if (xx > t->GetX()) {
1604       from=0; to=kMaxLayer;
1605       step=+1;
1606   } else {
1607       from=kMaxLayer-1; to=-1;
1608       step=-1;
1609   }
1610
1611   for (Int_t i=from; i != to; i += step) {
1612      AliITSlayer &layer=fgLayers[i];
1613      Double_t r=layer.GetR();
1614      if (step<0 && xx>r) break;  //
1615      {
1616      Double_t hI=i-0.5*step; 
1617      if (TMath::Abs(hI-1.5)<0.01 || TMath::Abs(hI-3.5)<0.01) {             
1618         Double_t rs=0.5*(fgLayers[i-step].GetR() + r);
1619         Double_t d=0.0034, x0=38.6; 
1620         if (TMath::Abs(hI-1.5)<0.01) {rs=9.; d=0.0097; x0=42;}
1621         if (!t->PropagateTo(rs,-step*d,x0)) {
1622           return kFALSE;
1623         }
1624      }
1625      }
1626
1627      // remember old position [SR, GSI 18.02.2003]
1628      Double_t oldX=0., oldY=0., oldZ=0.;
1629      if (t->IsStartedTimeIntegral() && step==1) {
1630         t->GetGlobalXYZat(t->GetX(),oldX,oldY,oldZ);
1631      }
1632      //
1633
1634      Double_t x,y,z;
1635      if (!t->GetGlobalXYZat(r,x,y,z)) { 
1636        return kFALSE;
1637      }
1638      Double_t phi=TMath::ATan2(y,x);
1639      Int_t idet=layer.FindDetectorIndex(phi,z);
1640      if (idet<0) { 
1641        return kFALSE;
1642      }
1643      const AliITSdetector &det=layer.GetDetector(idet);
1644      phi=det.GetPhi();
1645      if (!t->Propagate(phi,det.GetR())) {
1646        return kFALSE;
1647      }
1648      t->SetDetectorIndex(idet);
1649
1650      const AliITSclusterV2 *cl=0;
1651      Double_t maxchi2=1000.*kMaxChi2;
1652
1653      Int_t idx=index[i];
1654      if (idx>0) {
1655         const AliITSclusterV2 *c=(AliITSclusterV2 *)GetCluster(idx); 
1656         if (c){
1657           if (idet != c->GetDetectorIndex()) {
1658             idet=c->GetDetectorIndex();
1659             const AliITSdetector &det=layer.GetDetector(idet);
1660             if (!t->Propagate(det.GetPhi(),det.GetR())) {
1661               return kFALSE;
1662             }
1663             t->SetDetectorIndex(idet);
1664           }
1665           //Double_t chi2=t->GetPredictedChi2(c);
1666           Int_t layer = (idx & 0xf0000000) >> 28;;
1667           Double_t chi2=GetPredictedChi2MI(t,c,layer);
1668           if (chi2<maxchi2) { 
1669             cl=c; 
1670             maxchi2=chi2; 
1671           } else {
1672             return kFALSE;
1673           }
1674         }
1675      }
1676      /*
1677      if (cl==0)
1678      if (t->GetNumberOfClusters()>2) {
1679         Double_t dz=4*TMath::Sqrt(t->GetSigmaZ2()+kSigmaZ2[i]);
1680         Double_t dy=4*TMath::Sqrt(t->GetSigmaY2()+kSigmaY2[i]);
1681         Double_t zmin=t->GetZ() - dz;
1682         Double_t zmax=t->GetZ() + dz;
1683         Double_t ymin=t->GetY() + phi*r - dy;
1684         Double_t ymax=t->GetY() + phi*r + dy;
1685         layer.SelectClusters(zmin,zmax,ymin,ymax);
1686
1687         const AliITSclusterV2 *c=0; Int_t ci=-1;
1688         while ((c=layer.GetNextCluster(ci))!=0) {
1689            if (idet != c->GetDetectorIndex()) continue;
1690            Double_t chi2=t->GetPredictedChi2(c);
1691            if (chi2<maxchi2) { cl=c; maxchi2=chi2; idx=ci; }
1692         }
1693      }
1694      */
1695      if (cl) {
1696        //if (!t->Update(cl,maxchi2,idx)) {
1697        if (!UpdateMI(t,cl,maxchi2,idx)) {
1698           return kFALSE;
1699        }
1700        t->SetSampledEdx(cl->GetQ(),t->GetNumberOfClusters()-1);
1701      }
1702
1703      {
1704      Double_t x0;
1705      Double_t d=layer.GetThickness(t->GetY(),t->GetZ(),x0);
1706      t->CorrectForMaterial(-step*d,x0);
1707      }
1708                  
1709      // track time update [SR, GSI 17.02.2003]
1710      if (t->IsStartedTimeIntegral() && step==1) {
1711         Double_t newX, newY, newZ;
1712         t->GetGlobalXYZat(t->GetX(),newX,newY,newZ);
1713         Double_t dL2 = (oldX-newX)*(oldX-newX) + (oldY-newY)*(oldY-newY) + 
1714                        (oldZ-newZ)*(oldZ-newZ);
1715         t->AddTimeStep(TMath::Sqrt(dL2));
1716      }
1717      //
1718
1719   }
1720
1721   if (!t->PropagateTo(xx,0.,0.)) return kFALSE;
1722   return kTRUE;
1723 }
1724
1725
1726
1727 Double_t AliITStrackerMI::GetNormalizedChi2(AliITStrackMI * track, Int_t mode)
1728 {
1729   //
1730   // calculate normalized chi2
1731   //  return NormalizedChi2(track,0);
1732   Float_t chi2 = 0;
1733   Float_t sum=0;
1734   Float_t *erry = GetErrY(fCurrentEsdTrack), *errz = GetErrZ(fCurrentEsdTrack);
1735   //  track->fdEdxMismatch=0;
1736   Float_t dedxmismatch =0;
1737   Float_t *ny = GetNy(fCurrentEsdTrack), *nz = GetNz(fCurrentEsdTrack); 
1738   if (mode<100){
1739     for (Int_t i = 0;i<6;i++){
1740       if (track->fClIndex[i]>0){
1741         Float_t cerry, cerrz;
1742         if (ny[i]>0) {cerry = erry[i]; cerrz=errz[i];}
1743         else 
1744           { cerry= track->fSigmaY[i]; cerrz = track->fSigmaZ[i];}
1745         cerry*=cerry;
1746         cerrz*=cerrz;   
1747         Float_t cchi2 = (track->fDy[i]*track->fDy[i]/cerry)+(track->fDz[i]*track->fDz[i]/cerrz);
1748         if (i>1){
1749           Float_t ratio = track->fNormQ[i]/track->fExpQ;
1750           if (ratio<0.5) {
1751             cchi2+=(0.5-ratio)*10.;
1752             //track->fdEdxMismatch+=(0.5-ratio)*10.;
1753             dedxmismatch+=(0.5-ratio)*10.;          
1754           }
1755         }
1756         if (i<2 ||i>3){
1757           AliITSclusterV2 * cl = (AliITSclusterV2*)GetCluster( track->fClIndex[i]);  
1758           Double_t delta = cl->GetNy()+cl->GetNz()-ny[i]-nz[i];
1759           if (delta>1) chi2 +=0.5*TMath::Min(delta/2,2.); 
1760           if (i<2) chi2+=2*cl->GetDeltaProbability();
1761         }
1762         chi2+=cchi2;
1763         sum++;
1764       }
1765     }
1766     if (TMath::Abs(track->fdEdxMismatch-dedxmismatch)>0.0001){
1767       track->fdEdxMismatch = dedxmismatch;
1768     }
1769   }
1770   else{
1771     for (Int_t i = 0;i<4;i++){
1772       if (track->fClIndex[i]>0){
1773         Float_t cerry, cerrz;
1774         if (ny[i]>0) {cerry = erry[i]; cerrz=errz[i];}
1775         else { cerry= track->fSigmaY[i]; cerrz = track->fSigmaZ[i];}
1776         cerry*=cerry;
1777         cerrz*=cerrz;
1778         chi2+= (track->fDy[i]*track->fDy[i]/cerry);
1779         chi2+= (track->fDz[i]*track->fDz[i]/cerrz);      
1780         sum++;
1781       }
1782     }
1783     for (Int_t i = 4;i<6;i++){
1784       if (track->fClIndex[i]>0){        
1785         Float_t cerry, cerrz;
1786         if (ny[i]>0) {cerry = erry[i]; cerrz=errz[i];}
1787         else { cerry= track->fSigmaY[i]; cerrz = track->fSigmaZ[i];}
1788         cerry*=cerry;
1789         cerrz*=cerrz;   
1790         Float_t cerryb, cerrzb;
1791         if (ny[i+6]>0) {cerryb = erry[i+6]; cerrzb=errz[i+6];}
1792         else { cerryb= track->fSigmaY[i+6]; cerrzb = track->fSigmaZ[i+6];}
1793         cerryb*=cerryb;
1794         cerrzb*=cerrzb;
1795         chi2+= TMath::Min((track->fDy[i+6]*track->fDy[i+6]/cerryb),track->fDy[i]*track->fDy[i]/cerry);
1796         chi2+= TMath::Min((track->fDz[i+6]*track->fDz[i+6]/cerrzb),track->fDz[i]*track->fDz[i]/cerrz);      
1797         sum++;
1798       }
1799     }
1800   }
1801   if (track->fESDtrack->GetTPCsignal()>85){
1802     Float_t ratio = track->fdEdx/track->fESDtrack->GetTPCsignal();
1803     if (ratio<0.5) {
1804       chi2+=(0.5-ratio)*5.;      
1805     }
1806     if (ratio>2){
1807       chi2+=(ratio-2.0)*3; 
1808     }
1809   }
1810   //
1811   Double_t match = TMath::Sqrt(track->fChi22);
1812   if (track->fConstrain)  match/=track->GetNumberOfClusters();
1813   if (!track->fConstrain) match/=track->GetNumberOfClusters()-2.;
1814   if (match<0) match=0;
1815   Float_t deadzonefactor = (track->fNDeadZone>0) ? 3*(1.1-track->fDeadZoneProbability):0.;
1816   Double_t normchi2 = 2*track->fNSkipped+match+deadzonefactor+(1+(2*track->fNSkipped+deadzonefactor)/track->GetNumberOfClusters())*
1817     (chi2)/TMath::Max(double(sum-track->fNSkipped),
1818                                 1./(1.+track->fNSkipped));     
1819  
1820  return normchi2;
1821 }
1822
1823
1824 Double_t AliITStrackerMI::GetMatchingChi2(AliITStrackMI * track1, AliITStrackMI * track2)
1825 {
1826   //
1827   // return matching chi2 between two tracks
1828   AliITStrackMI track3(*track2);
1829   track3.Propagate(track1->GetAlpha(),track1->GetX());
1830   TMatrixD vec(5,1);
1831   vec(0,0)=track1->fP0-track3.fP0;
1832   vec(1,0)=track1->fP1-track3.fP1;
1833   vec(2,0)=track1->fP2-track3.fP2;
1834   vec(3,0)=track1->fP3-track3.fP3;
1835   vec(4,0)=track1->fP4-track3.fP4;
1836   //
1837   TMatrixD cov(5,5);
1838   cov(0,0) = track1->fC00+track3.fC00;
1839   cov(1,1) = track1->fC11+track3.fC11;
1840   cov(2,2) = track1->fC22+track3.fC22;
1841   cov(3,3) = track1->fC33+track3.fC33;
1842   cov(4,4) = track1->fC44+track3.fC44;
1843   
1844   cov(0,1)=cov(1,0) = track1->fC10+track3.fC10;
1845   cov(0,2)=cov(2,0) = track1->fC20+track3.fC20;
1846   cov(0,3)=cov(3,0) = track1->fC30+track3.fC30;
1847   cov(0,4)=cov(4,0) = track1->fC40+track3.fC40;
1848   //
1849   cov(1,2)=cov(2,1) = track1->fC21+track3.fC21;
1850   cov(1,3)=cov(3,1) = track1->fC31+track3.fC31;
1851   cov(1,4)=cov(4,1) = track1->fC41+track3.fC41;
1852   //
1853   cov(2,3)=cov(3,2) = track1->fC32+track3.fC32;
1854   cov(2,4)=cov(4,2) = track1->fC42+track3.fC42;
1855   //
1856   cov(3,4)=cov(4,3) = track1->fC43+track3.fC43;
1857   
1858   cov.Invert();
1859   TMatrixD vec2(cov,TMatrixD::kMult,vec);
1860   TMatrixD chi2(vec2,TMatrixD::kTransposeMult,vec);
1861   return chi2(0,0);
1862 }
1863
1864 Double_t  AliITStrackerMI::GetDeadZoneProbability(Double_t zpos, Double_t zerr)
1865 {
1866   //
1867   //  return probability that given point - characterized by z position and error  is in dead zone
1868   //
1869   Double_t probability =0;
1870   Double_t absz = TMath::Abs(zpos);
1871   Double_t nearestz = (absz<2)? 0.:7.1;
1872   if (TMath::Abs(absz-nearestz)>0.25+3*zerr) return 0;
1873   Double_t zmin=0, zmax=0;   
1874   if (zpos<-6.){
1875     zmin = -7.25; zmax = -6.95; 
1876   }
1877   if (zpos>6){
1878     zmin = 7.0; zmax =7.3;
1879   }
1880   if (absz<2){
1881     zmin = -0.75; zmax = 1.5;
1882   }
1883   probability = (TMath::Erf((zpos-zmin)/zerr) - TMath::Erf((zpos-zmax)/zerr))*0.5;
1884   return probability;
1885 }
1886
1887
1888 Double_t AliITStrackerMI::GetTruncatedChi2(AliITStrackMI * track, Float_t fac)
1889 {
1890   //
1891   // calculate normalized chi2
1892   Float_t chi2[6];
1893   Float_t *erry = GetErrY(fCurrentEsdTrack), *errz = GetErrZ(fCurrentEsdTrack);
1894   Float_t ncl = 0;
1895   for (Int_t i = 0;i<6;i++){
1896     if (TMath::Abs(track->fDy[i])>0){      
1897       chi2[i]= (track->fDy[i]/erry[i])*(track->fDy[i]/erry[i]);
1898       chi2[i]+= (track->fDz[i]/errz[i])*(track->fDz[i]/errz[i]);
1899       ncl++;
1900     }
1901     else{chi2[i]=10000;}
1902   }
1903   Int_t index[6];
1904   TMath::Sort(6,chi2,index,kFALSE);
1905   Float_t max = float(ncl)*fac-1.;
1906   Float_t sumchi=0, sumweight=0; 
1907   for (Int_t i=0;i<max+1;i++){
1908     Float_t weight = (i<max)?1.:(max+1.-i);
1909     sumchi+=weight*chi2[index[i]];
1910     sumweight+=weight;
1911   }
1912   Double_t normchi2 = sumchi/sumweight;
1913   return normchi2;
1914 }
1915
1916
1917 Double_t AliITStrackerMI::GetInterpolatedChi2(AliITStrackMI * forwardtrack, AliITStrackMI * backtrack)
1918 {
1919   //
1920   // calculate normalized chi2
1921   //  if (forwardtrack->fNUsed>0.3*float(forwardtrack->GetNumberOfClusters())) return 10000;
1922   Int_t npoints = 0;
1923   Double_t res =0;
1924   for (Int_t i=0;i<6;i++){
1925     if ( (backtrack->fSigmaY[i]<0.000000001) || (forwardtrack->fSigmaY[i]<0.000000001)) continue;
1926     Double_t sy1 = forwardtrack->fSigmaY[i];
1927     Double_t sz1 = forwardtrack->fSigmaZ[i];
1928     Double_t sy2 = backtrack->fSigmaY[i];
1929     Double_t sz2 = backtrack->fSigmaZ[i];
1930     if (i<2){ sy2=1000.;sz2=1000;}
1931     //    
1932     Double_t dy0 = (forwardtrack->fDy[i]/(sy1*sy1) +backtrack->fDy[i]/(sy2*sy2))/(1./(sy1*sy1)+1./(sy2*sy2));
1933     Double_t dz0 = (forwardtrack->fDz[i]/(sz1*sz1) +backtrack->fDz[i]/(sz2*sz2))/(1./(sz1*sz1)+1./(sz2*sz2));
1934     // 
1935     Double_t nz0 = dz0*TMath::Sqrt((1./(sz1*sz1)+1./(sz2*sz2)));
1936     Double_t ny0 = dy0*TMath::Sqrt((1./(sy1*sy1)+1./(sy2*sy2)));
1937     //
1938     res+= nz0*nz0+ny0*ny0;
1939     npoints++;
1940   }
1941   if (npoints>1) return 
1942                    TMath::Max(TMath::Abs(0.3*forwardtrack->Get1Pt())-0.5,0.)+
1943                    //2*forwardtrack->fNUsed+
1944                    res/TMath::Max(double(npoints-forwardtrack->fNSkipped),
1945                                   1./(1.+forwardtrack->fNSkipped));
1946   return 1000;
1947 }
1948    
1949
1950
1951
1952
1953 Float_t  *AliITStrackerMI::GetWeight(Int_t index) {
1954   //--------------------------------------------------------------------
1955   //       Return pointer to a given cluster
1956   //--------------------------------------------------------------------
1957   Int_t l=(index & 0xf0000000) >> 28;
1958   Int_t c=(index & 0x0fffffff) >> 00;
1959   return fgLayers[l].GetWeight(c);
1960 }
1961
1962 void AliITStrackerMI::RegisterClusterTracks(AliITStrackMI* track,Int_t id)
1963 {
1964   //---------------------------------------------
1965   // register track to the list
1966   //
1967   if (track->fESDtrack->GetKinkIndex(0)!=0) return;  //don't register kink tracks
1968   //
1969   //
1970   for (Int_t icluster=0;icluster<track->GetNumberOfClusters();icluster++){
1971     Int_t index = track->GetClusterIndex(icluster);
1972     Int_t l=(index & 0xf0000000) >> 28;
1973     Int_t c=(index & 0x0fffffff) >> 00;
1974     if (c>fgLayers[l].fN) continue;
1975     for (Int_t itrack=0;itrack<4;itrack++){
1976       if (fgLayers[l].fClusterTracks[itrack][c]<0){
1977         fgLayers[l].fClusterTracks[itrack][c]=id;
1978         break;
1979       }
1980     }
1981   }
1982 }
1983 void AliITStrackerMI::UnRegisterClusterTracks(AliITStrackMI* track, Int_t id)
1984 {
1985   //---------------------------------------------
1986   // unregister track from the list
1987   for (Int_t icluster=0;icluster<track->GetNumberOfClusters();icluster++){
1988     Int_t index = track->GetClusterIndex(icluster);
1989     Int_t l=(index & 0xf0000000) >> 28;
1990     Int_t c=(index & 0x0fffffff) >> 00;
1991     if (c>fgLayers[l].fN) continue;
1992     for (Int_t itrack=0;itrack<4;itrack++){
1993       if (fgLayers[l].fClusterTracks[itrack][c]==id){
1994         fgLayers[l].fClusterTracks[itrack][c]=-1;
1995       }
1996     }
1997   }
1998 }
1999 Float_t AliITStrackerMI::GetNumberOfSharedClusters(AliITStrackMI* track,Int_t id, Int_t list[6], AliITSclusterV2 *clist[6])
2000 {
2001   //-------------------------------------------------------------
2002   //get number of shared clusters
2003   //-------------------------------------------------------------
2004   Float_t shared=0;
2005   for (Int_t i=0;i<6;i++) { list[i]=-1, clist[i]=0;}
2006   // mean  number of clusters
2007   Float_t *ny = GetNy(id), *nz = GetNz(id); 
2008
2009  
2010   for (Int_t icluster=0;icluster<track->GetNumberOfClusters();icluster++){
2011     Int_t index = track->GetClusterIndex(icluster);
2012     Int_t l=(index & 0xf0000000) >> 28;
2013     Int_t c=(index & 0x0fffffff) >> 00;
2014     if (c>fgLayers[l].fN) continue;
2015     if (ny[l]==0){
2016       printf("problem\n");
2017     }
2018     AliITSclusterV2 *cl = (AliITSclusterV2*)GetCluster(index);
2019     Float_t weight=1;
2020     //
2021     Float_t deltan = 0;
2022     if (l>3&&cl->GetNy()+cl->GetNz()>6) continue;
2023     if (l>2&&track->fNormQ[l]/track->fExpQ>3.5) continue;
2024     if (l<2 || l>3){      
2025       deltan = (cl->GetNy()+cl->GetNz()-ny[l]-nz[l]);
2026     }
2027     else{
2028       deltan = (cl->GetNz()-nz[l]);
2029     }
2030     if (deltan>2.0) continue;  // extended - highly probable shared cluster
2031     weight = 2./TMath::Max(3.+deltan,2.);
2032     //
2033     for (Int_t itrack=0;itrack<4;itrack++){
2034       if (fgLayers[l].fClusterTracks[itrack][c]>=0 && fgLayers[l].fClusterTracks[itrack][c]!=id){
2035         list[l]=index;
2036         clist[l] = (AliITSclusterV2*)GetCluster(index);
2037         shared+=weight; 
2038         break;
2039       }
2040     }
2041   }
2042   track->fNUsed=shared;
2043   return shared;
2044 }
2045
2046 Int_t AliITStrackerMI::GetOverlapTrack(AliITStrackMI *track, Int_t trackID, Int_t &shared, Int_t clusterlist[6],Int_t overlist[6])
2047 {
2048   //
2049   // find first shared track 
2050   //
2051   // mean  number of clusters
2052   Float_t *ny = GetNy(trackID), *nz = GetNz(trackID); 
2053   //
2054   for (Int_t i=0;i<6;i++) overlist[i]=-1;
2055   Int_t sharedtrack=100000;
2056   Int_t tracks[24],trackindex=0;
2057   for (Int_t i=0;i<24;i++) {tracks[i]=-1;}
2058   //
2059   for (Int_t icluster=0;icluster<6;icluster++){
2060     if (clusterlist[icluster]<0) continue;
2061     Int_t index = clusterlist[icluster];
2062     Int_t l=(index & 0xf0000000) >> 28;
2063     Int_t c=(index & 0x0fffffff) >> 00;
2064     if (ny[l]==0){
2065       printf("problem\n");
2066     }
2067     if (c>fgLayers[l].fN) continue;
2068     //if (l>3) continue;
2069     AliITSclusterV2 *cl = (AliITSclusterV2*)GetCluster(index);
2070     //
2071     Float_t deltan = 0;
2072     if (l>3&&cl->GetNy()+cl->GetNz()>6) continue;
2073     if (l>2&&track->fNormQ[l]/track->fExpQ>3.5) continue;
2074     if (l<2 || l>3){      
2075       deltan = (cl->GetNy()+cl->GetNz()-ny[l]-nz[l]);
2076     }
2077     else{
2078       deltan = (cl->GetNz()-nz[l]);
2079     }
2080     if (deltan>2.0) continue;  // extended - highly probable shared cluster
2081     //
2082     for (Int_t itrack=3;itrack>=0;itrack--){
2083       if (fgLayers[l].fClusterTracks[itrack][c]<0) continue;
2084       if (fgLayers[l].fClusterTracks[itrack][c]!=trackID){
2085        tracks[trackindex]  = fgLayers[l].fClusterTracks[itrack][c];
2086        trackindex++;
2087       }
2088     }
2089   }
2090   if (trackindex==0) return -1;
2091   if (trackindex==1){    
2092     sharedtrack = tracks[0];
2093   }else{
2094     if (trackindex==2) sharedtrack =TMath::Min(tracks[0],tracks[1]);
2095     else{
2096       //
2097       Int_t track[24], cluster[24];
2098       for (Int_t i=0;i<trackindex;i++){ track[i]=-1; cluster[i]=0;}
2099       Int_t index =0;
2100       //
2101       for (Int_t i=0;i<trackindex;i++){
2102         if (tracks[i]<0) continue;
2103         track[index] = tracks[i];
2104         cluster[index]++;       
2105         for (Int_t j=i+1;j<trackindex;j++){
2106           if (tracks[j]<0) continue;
2107           if (tracks[j]==tracks[i]){
2108             cluster[index]++;
2109             tracks[j]=-1;
2110           }
2111         }
2112         index++;
2113       }
2114       Int_t max=0;
2115       for (Int_t i=0;i<index;i++){
2116         if (cluster[index]>max) {
2117           sharedtrack=track[index];
2118           max=cluster[index];
2119         }
2120       }
2121     }
2122   }
2123   
2124   if (sharedtrack>=100000) return -1;
2125   //
2126   // make list of overlaps
2127   shared =0;
2128   for (Int_t icluster=0;icluster<6;icluster++){
2129     if (clusterlist[icluster]<0) continue;
2130     Int_t index = clusterlist[icluster];
2131     Int_t l=(index & 0xf0000000) >> 28;
2132     Int_t c=(index & 0x0fffffff) >> 00;
2133     if (c>fgLayers[l].fN) continue;
2134     AliITSclusterV2 *cl = (AliITSclusterV2*)GetCluster(index);
2135     if (l==0 || l==1){
2136       if (cl->GetNy()>2) continue;
2137       if (cl->GetNz()>2) continue;
2138     }
2139     if (l==4 || l==5){
2140       if (cl->GetNy()>3) continue;
2141       if (cl->GetNz()>3) continue;
2142     }
2143     //
2144     for (Int_t itrack=3;itrack>=0;itrack--){
2145       if (fgLayers[l].fClusterTracks[itrack][c]<0) continue;
2146       if (fgLayers[l].fClusterTracks[itrack][c]==sharedtrack){
2147         overlist[l]=index;
2148         shared++;      
2149       }
2150     }
2151   }
2152   return sharedtrack;
2153 }
2154
2155
2156 AliITStrackMI *  AliITStrackerMI::GetBest2Tracks(Int_t trackID1, Int_t trackID2, Float_t th0, Float_t th1){
2157   //
2158   // try to find track hypothesys without conflicts
2159   // with minimal chi2;
2160   TClonesArray *arr1 = (TClonesArray*)fTrackHypothesys.At(trackID1);
2161   Int_t entries1 = arr1->GetEntriesFast();
2162   TClonesArray *arr2 = (TClonesArray*)fTrackHypothesys.At(trackID2);
2163   if (!arr2) return (AliITStrackMI*) arr1->UncheckedAt(0);
2164   Int_t entries2 = arr2->GetEntriesFast();
2165   if (entries2<=0) return (AliITStrackMI*) arr1->UncheckedAt(0);
2166   //
2167   AliITStrackMI * track10=(AliITStrackMI*) arr1->UncheckedAt(0);
2168   AliITStrackMI * track20=(AliITStrackMI*) arr2->UncheckedAt(0);
2169   if (TMath::Abs(1./track10->Get1Pt())>0.5+TMath::Abs(1/track20->Get1Pt())) return track10;
2170
2171   for (Int_t itrack=0;itrack<entries1;itrack++){
2172     AliITStrackMI * track=(AliITStrackMI*) arr1->UncheckedAt(itrack);
2173     UnRegisterClusterTracks(track,trackID1);
2174   }
2175   //
2176   for (Int_t itrack=0;itrack<entries2;itrack++){
2177     AliITStrackMI * track=(AliITStrackMI*) arr2->UncheckedAt(itrack);
2178     UnRegisterClusterTracks(track,trackID2);
2179   }
2180   Int_t index1=0;
2181   Int_t index2=0;
2182   Float_t maxconflicts=6;
2183   Double_t maxchi2 =1000.;
2184   //
2185   // get weight of hypothesys - using best hypothesy
2186   Double_t w1,w2;
2187  
2188   Int_t list1[6],list2[6];
2189   AliITSclusterV2 *clist1[6], *clist2[6] ;
2190   RegisterClusterTracks(track10,trackID1);
2191   RegisterClusterTracks(track20,trackID2);
2192   Float_t conflict1 = GetNumberOfSharedClusters(track10,trackID1,list1,clist1);
2193   Float_t conflict2 = GetNumberOfSharedClusters(track20,trackID2,list2,clist2);
2194   UnRegisterClusterTracks(track10,trackID1);
2195   UnRegisterClusterTracks(track20,trackID2);
2196   //
2197   // normalized chi2
2198   Float_t chi21 =0,chi22=0,ncl1=0,ncl2=0;
2199   Float_t nerry[6],nerrz[6];
2200   Float_t *erry1=GetErrY(trackID1),*errz1 = GetErrZ(trackID1);
2201   Float_t *erry2=GetErrY(trackID2),*errz2 = GetErrZ(trackID2);
2202   for (Int_t i=0;i<6;i++){
2203      if ( (erry1[i]>0) && (erry2[i]>0)) {
2204        nerry[i] = TMath::Min(erry1[i],erry2[i]);
2205        nerrz[i] = TMath::Min(errz1[i],errz2[i]);
2206      }else{
2207        nerry[i] = TMath::Max(erry1[i],erry2[i]);
2208        nerrz[i] = TMath::Max(errz1[i],errz2[i]);
2209      }
2210      if (TMath::Abs(track10->fDy[i])>0.000000000000001){
2211        chi21 += track10->fDy[i]*track10->fDy[i]/(nerry[i]*nerry[i]);
2212        chi21 += track10->fDz[i]*track10->fDz[i]/(nerrz[i]*nerrz[i]);
2213        ncl1++;
2214      }
2215      if (TMath::Abs(track20->fDy[i])>0.000000000000001){
2216        chi22 += track20->fDy[i]*track20->fDy[i]/(nerry[i]*nerry[i]);
2217        chi22 += track20->fDz[i]*track20->fDz[i]/(nerrz[i]*nerrz[i]);
2218        ncl2++;
2219      }
2220   }
2221   chi21/=ncl1;
2222   chi22/=ncl2;
2223   //
2224   // 
2225   Float_t d1 = TMath::Sqrt(track10->fD[0]*track10->fD[0]+track10->fD[1]*track10->fD[1])+0.1;
2226   Float_t d2 = TMath::Sqrt(track20->fD[0]*track20->fD[0]+track20->fD[1]*track20->fD[1])+0.1;
2227   Float_t s1 = TMath::Sqrt(track10->GetSigmaY2()*track10->GetSigmaZ2());
2228   Float_t s2 = TMath::Sqrt(track20->GetSigmaY2()*track20->GetSigmaZ2());
2229   //
2230   w1 = (d2/(d1+d2)+ 2*s2/(s1+s2)+
2231         +s2/(s1+s2)*0.5*(chi22+2.)/(chi21+chi22+4.)
2232         +1.*TMath::Abs(1./track10->Get1Pt())/(TMath::Abs(1./track10->Get1Pt())+TMath::Abs(1./track20->Get1Pt()))
2233         );
2234   w2 = (d1/(d1+d2)+ 2*s1/(s1+s2)+
2235         s1/(s1+s2)*0.5*(chi21+2.)/(chi21+chi22+4.)
2236         +1.*TMath::Abs(1./track20->Get1Pt())/(TMath::Abs(1./track10->Get1Pt())+TMath::Abs(1./track20->Get1Pt()))
2237         );
2238
2239   Double_t sumw = w1+w2;
2240   w1/=sumw;
2241   w2/=sumw;
2242   if (w1<w2*0.5) {
2243     w1 = (d2+0.5)/(d1+d2+1.);
2244     w2 = (d1+0.5)/(d1+d2+1.);
2245   }
2246   //  Float_t maxmax       = w1*track10->fChi2MIP[0]+w2*track20->fChi2MIP[0]+w1*conflict1+w2*conflict2+1.;
2247   //Float_t maxconflicts0 = w1*conflict1+w2*conflict2;
2248   //
2249   // get pair of "best" hypothesys
2250   //  
2251   Float_t * ny1 = GetNy(trackID1), * nz1 = GetNz(trackID1); 
2252   Float_t * ny2 = GetNy(trackID2), * nz2 = GetNz(trackID2); 
2253
2254   for (Int_t itrack1=0;itrack1<entries1;itrack1++){
2255     AliITStrackMI * track1=(AliITStrackMI*) arr1->UncheckedAt(itrack1);
2256     //if (track1->fFakeRatio>0) continue;
2257     RegisterClusterTracks(track1,trackID1);
2258     for (Int_t itrack2=0;itrack2<entries2;itrack2++){
2259       AliITStrackMI * track2=(AliITStrackMI*) arr2->UncheckedAt(itrack2);
2260
2261       //      Float_t current = w1*track1->fChi2MIP[0]+w2*track2->fChi2MIP[0];
2262       //if (track2->fFakeRatio>0) continue;
2263       Float_t nskipped=0;            
2264       RegisterClusterTracks(track2,trackID2);
2265       Int_t list1[6],list2[6];
2266       AliITSclusterV2 *clist1[6], *clist2[6] ;
2267       Float_t cconflict1 = GetNumberOfSharedClusters(track1,trackID1,list1,clist1);
2268       Float_t cconflict2 = GetNumberOfSharedClusters(track2,trackID2,list2,clist2);
2269       UnRegisterClusterTracks(track2,trackID2);
2270       //
2271       if (track1->fConstrain) nskipped+=w1*track1->fNSkipped;
2272       if (track2->fConstrain) nskipped+=w2*track2->fNSkipped;
2273       if (nskipped>0.5) continue;
2274       //
2275       //if ( w1*conflict1+w2*conflict2>maxconflicts0) continue;
2276       if (conflict1+1<cconflict1) continue;
2277       if (conflict2+1<cconflict2) continue;
2278       Float_t conflict=0;
2279       Float_t sumchi2=0;
2280       Float_t sum=0;
2281       for (Int_t i=0;i<6;i++){
2282         //
2283         Float_t c1 =0.; // conflict coeficients
2284         Float_t c2 =0.; 
2285         if (clist1[i]&&clist2[i]){
2286           Float_t deltan = 0;
2287           if (i<2 || i>3){      
2288             deltan = (clist1[i]->GetNy()+clist1[i]->GetNz()-TMath::Max(ny1[i],ny2[i])-TMath::Max(nz1[i],nz2[i]));
2289           }
2290           else{
2291             deltan = (clist1[i]->GetNz()-TMath::Max(nz1[i],nz2[i]));
2292           }
2293           c1  = 2./TMath::Max(3.+deltan,2.);      
2294           c2  = 2./TMath::Max(3.+deltan,2.);      
2295         }
2296         else{
2297           if (clist1[i]){
2298             Float_t deltan = 0;
2299             if (i<2 || i>3){      
2300               deltan = (clist1[i]->GetNy()+clist1[i]->GetNz()-ny1[i]-nz1[i]);
2301             }
2302             else{
2303               deltan = (clist1[i]->GetNz()-nz1[i]);
2304             }
2305             c1  = 2./TMath::Max(3.+deltan,2.);    
2306             c2  = 0;
2307           }
2308
2309           if (clist2[i]){
2310             Float_t deltan = 0;
2311             if (i<2 || i>3){      
2312               deltan = (clist2[i]->GetNy()+clist2[i]->GetNz()-ny2[i]-nz2[i]);
2313             }
2314             else{
2315               deltan = (clist2[i]->GetNz()-nz2[i]);
2316             }
2317             c2  = 2./TMath::Max(3.+deltan,2.);    
2318             c1  = 0;
2319           }       
2320         }
2321         //
2322         Double_t chi21=0,chi22=0;
2323         if (TMath::Abs(track1->fDy[i])>0.) {
2324           chi21 = (track1->fDy[i]/track1->fSigmaY[i])*(track1->fDy[i]/track1->fSigmaY[i])+
2325             (track1->fDz[i]/track1->fSigmaZ[i])*(track1->fDz[i]/track1->fSigmaZ[i]);
2326           //chi21 = (track1->fDy[i]*track1->fDy[i])/(nerry[i]*nerry[i])+
2327           //  (track1->fDz[i]*track1->fDz[i])/(nerrz[i]*nerrz[i]);
2328         }else{
2329           if (TMath::Abs(track1->fSigmaY[i]>0.)) c1=1;
2330         }
2331         //
2332         if (TMath::Abs(track2->fDy[i])>0.) {
2333           chi22 = (track2->fDy[i]/track2->fSigmaY[i])*(track2->fDy[i]/track2->fSigmaY[i])+
2334             (track2->fDz[i]/track2->fSigmaZ[i])*(track2->fDz[i]/track2->fSigmaZ[i]);
2335           //chi22 = (track2->fDy[i]*track2->fDy[i])/(nerry[i]*nerry[i])+
2336           //  (track2->fDz[i]*track2->fDz[i])/(nerrz[i]*nerrz[i]);
2337         }
2338         else{
2339           if (TMath::Abs(track2->fSigmaY[i]>0.)) c2=1;
2340         }
2341         sumchi2+=w1*(1.+c1)*(1+c1)*(chi21+c1)+w2*(1.+c2)*(1+c2)*(chi22+c2);
2342         if (chi21>0) sum+=w1;
2343         if (chi22>0) sum+=w2;
2344         conflict+=(c1+c2);
2345       }
2346       Double_t norm = sum-w1*track1->fNSkipped-w2*track2->fNSkipped;
2347       if (norm<0) norm =1/(w1*track1->fNSkipped+w2*track2->fNSkipped);
2348       Double_t normchi2 = 2*conflict+sumchi2/sum;
2349       if ( normchi2 <maxchi2 ){   
2350         index1 = itrack1;
2351         index2 = itrack2;
2352         maxconflicts = conflict;
2353         maxchi2 = normchi2;
2354       }      
2355     }
2356     UnRegisterClusterTracks(track1,trackID1);
2357   }
2358   //
2359   //  if (maxconflicts<4 && maxchi2<th0){   
2360   if (maxchi2<th0*2.){   
2361     Float_t orig = track10->fFakeRatio*track10->GetNumberOfClusters();
2362     AliITStrackMI* track1=(AliITStrackMI*) arr1->UncheckedAt(index1);
2363     track1->fChi2MIP[5] = maxconflicts;
2364     track1->fChi2MIP[6] = maxchi2;
2365     track1->fChi2MIP[7] = 0.01+orig-(track1->fFakeRatio*track1->GetNumberOfClusters());
2366     //    track1->UpdateESDtrack(AliESDtrack::kITSin);
2367     track1->fChi2MIP[8] = index1;
2368     fBestTrackIndex[trackID1] =index1;
2369     UpdateESDtrack(track1, AliESDtrack::kITSin);
2370   }  
2371   else if (track10->fChi2MIP[0]<th1){
2372     track10->fChi2MIP[5] = maxconflicts;
2373     track10->fChi2MIP[6] = maxchi2;    
2374     //    track10->UpdateESDtrack(AliESDtrack::kITSin);
2375     UpdateESDtrack(track10,AliESDtrack::kITSin);
2376   }   
2377   
2378   for (Int_t itrack=0;itrack<entries1;itrack++){
2379     AliITStrackMI * track=(AliITStrackMI*) arr1->UncheckedAt(itrack);
2380     UnRegisterClusterTracks(track,trackID1);
2381   }
2382   //
2383   for (Int_t itrack=0;itrack<entries2;itrack++){
2384     AliITStrackMI * track=(AliITStrackMI*) arr2->UncheckedAt(itrack);
2385     UnRegisterClusterTracks(track,trackID2);
2386   }
2387
2388   if (track10->fConstrain&&track10->fChi2MIP[0]<kMaxChi2PerCluster[0]&&track10->fChi2MIP[1]<kMaxChi2PerCluster[1]
2389       &&track10->fChi2MIP[2]<kMaxChi2PerCluster[2]&&track10->fChi2MIP[3]<kMaxChi2PerCluster[3]){ 
2390     //  if (track10->fChi2MIP[0]<kMaxChi2PerCluster[0]&&track10->fChi2MIP[1]<kMaxChi2PerCluster[1]
2391   //    &&track10->fChi2MIP[2]<kMaxChi2PerCluster[2]&&track10->fChi2MIP[3]<kMaxChi2PerCluster[3]){ 
2392     RegisterClusterTracks(track10,trackID1);
2393   }
2394   if (track20->fConstrain&&track20->fChi2MIP[0]<kMaxChi2PerCluster[0]&&track20->fChi2MIP[1]<kMaxChi2PerCluster[1]
2395       &&track20->fChi2MIP[2]<kMaxChi2PerCluster[2]&&track20->fChi2MIP[3]<kMaxChi2PerCluster[3]){ 
2396     //if (track20->fChi2MIP[0]<kMaxChi2PerCluster[0]&&track20->fChi2MIP[1]<kMaxChi2PerCluster[1]
2397     //  &&track20->fChi2MIP[2]<kMaxChi2PerCluster[2]&&track20->fChi2MIP[3]<kMaxChi2PerCluster[3]){ 
2398     RegisterClusterTracks(track20,trackID2);  
2399   }
2400   return track10; 
2401  
2402 }
2403
2404 void AliITStrackerMI::UseClusters(const AliKalmanTrack *t, Int_t from) const {
2405   //--------------------------------------------------------------------
2406   // This function marks clusters assigned to the track
2407   //--------------------------------------------------------------------
2408   AliTracker::UseClusters(t,from);
2409
2410   AliITSclusterV2 *c=(AliITSclusterV2 *)GetCluster(t->GetClusterIndex(0));
2411   //if (c->GetQ()>2) c->Use();
2412   if (c->GetSigmaZ2()>0.1) c->Use();
2413   c=(AliITSclusterV2 *)GetCluster(t->GetClusterIndex(1));
2414   //if (c->GetQ()>2) c->Use();
2415   if (c->GetSigmaZ2()>0.1) c->Use();
2416
2417 }
2418
2419
2420 void AliITStrackerMI::AddTrackHypothesys(AliITStrackMI * track, Int_t esdindex)
2421 {
2422   //------------------------------------------------------------------
2423   // add track to the list of hypothesys
2424   //------------------------------------------------------------------
2425
2426   if (esdindex>=fTrackHypothesys.GetEntriesFast()) fTrackHypothesys.Expand(esdindex*2+10);
2427   //
2428   TObjArray * array = (TObjArray*) fTrackHypothesys.At(esdindex);
2429   if (!array) {
2430     array = new TObjArray(10);
2431     fTrackHypothesys.AddAt(array,esdindex);
2432   }
2433   array->AddLast(track);
2434 }
2435
2436 void AliITStrackerMI::SortTrackHypothesys(Int_t esdindex, Int_t maxcut, Int_t mode)
2437 {
2438   //-------------------------------------------------------------------
2439   // compress array of track hypothesys
2440   // keep only maxsize best hypothesys
2441   //-------------------------------------------------------------------
2442   if (esdindex>fTrackHypothesys.GetEntriesFast()) return;
2443   if (! (fTrackHypothesys.At(esdindex)) ) return;
2444   TObjArray * array = (TObjArray*) fTrackHypothesys.At(esdindex);
2445   Int_t entries = array->GetEntriesFast();
2446   //
2447   //- find preliminary besttrack as a reference
2448   Float_t minchi2=10000;
2449   Int_t maxn=0;
2450   AliITStrackMI * besttrack=0;
2451   for (Int_t itrack=0;itrack<array->GetEntriesFast();itrack++){
2452     AliITStrackMI * track = (AliITStrackMI*)array->At(itrack);
2453     if (!track) continue;
2454     Float_t chi2 = NormalizedChi2(track,0);
2455     //
2456     Int_t tpcLabel=track->fESDtrack->GetTPCLabel();
2457     track->SetLabel(tpcLabel);
2458     CookdEdx(track);
2459     track->fFakeRatio=1.;
2460     CookLabel(track,0.); //For comparison only
2461     //
2462     //if (chi2<kMaxChi2PerCluster[0]&&track->fFakeRatio==0){
2463     if (chi2<kMaxChi2PerCluster[0]){
2464       if (track->GetNumberOfClusters()<maxn) continue;
2465       maxn = track->GetNumberOfClusters();
2466       if (chi2<minchi2){
2467         minchi2=chi2;
2468         besttrack=track;
2469       }
2470     }
2471     else{
2472       if (track->fConstrain || track->fN>5){  //keep best short tracks - without vertex constrain
2473         delete array->RemoveAt(itrack);
2474       }  
2475     }
2476   }
2477   if (!besttrack) return;
2478   //
2479   //
2480   //take errors of best track as a reference
2481   Float_t *erry = GetErrY(esdindex), *errz = GetErrZ(esdindex);
2482   Float_t *ny = GetNy(esdindex), *nz = GetNz(esdindex);
2483   for (Int_t i=0;i<6;i++) {
2484     if (besttrack->fClIndex[i]>0){
2485       erry[i] = besttrack->fSigmaY[i]; erry[i+6] = besttrack->fSigmaY[i+6];
2486       errz[i] = besttrack->fSigmaZ[i]; errz[i+6] = besttrack->fSigmaZ[i+6];
2487       ny[i]   = besttrack->fNy[i];
2488       nz[i]   = besttrack->fNz[i];
2489     }
2490   }
2491   //
2492   // calculate normalized chi2
2493   //
2494   Float_t * chi2        = new Float_t[entries];
2495   Int_t * index         = new Int_t[entries];  
2496   for (Int_t i=0;i<entries;i++) chi2[i] =10000;
2497   for (Int_t itrack=0;itrack<entries;itrack++){
2498     AliITStrackMI * track = (AliITStrackMI*)array->At(itrack);
2499     if (track){
2500       track->fChi2MIP[0] = GetNormalizedChi2(track, mode);            
2501       if (track->fChi2MIP[0]<kMaxChi2PerCluster[0]) 
2502         chi2[itrack] = track->fChi2MIP[0];
2503       else{
2504         if (track->fConstrain || track->fN>5){  //keep best short tracks - without vertex constrain
2505           delete array->RemoveAt(itrack);            
2506         }
2507       }
2508     }
2509   }
2510   //
2511   TMath::Sort(entries,chi2,index,kFALSE);
2512   besttrack = (AliITStrackMI*)array->At(index[0]);
2513   if (besttrack&&besttrack->fChi2MIP[0]<kMaxChi2PerCluster[0]){
2514     for (Int_t i=0;i<6;i++){
2515       if (besttrack->fClIndex[i]>0){
2516         erry[i] = besttrack->fSigmaY[i]; erry[i+6] = besttrack->fSigmaY[i+6];
2517         errz[i] = besttrack->fSigmaZ[i]; erry[i+6] = besttrack->fSigmaY[i+6];
2518         ny[i]   = besttrack->fNy[i];
2519         nz[i]   = besttrack->fNz[i];
2520       }
2521     }
2522   }
2523   //
2524   // calculate one more time with updated normalized errors
2525   for (Int_t i=0;i<entries;i++) chi2[i] =10000;  
2526   for (Int_t itrack=0;itrack<entries;itrack++){
2527     AliITStrackMI * track = (AliITStrackMI*)array->At(itrack);
2528     if (track){      
2529       track->fChi2MIP[0] = GetNormalizedChi2(track,mode);            
2530       if (track->fChi2MIP[0]<kMaxChi2PerCluster[0]) 
2531         chi2[itrack] = track->fChi2MIP[0]-0*(track->GetNumberOfClusters()+track->fNDeadZone); 
2532       else
2533         {
2534           if (track->fConstrain || track->fN>5){  //keep best short tracks - without vertex constrain
2535             delete array->RemoveAt(itrack);     
2536           }
2537         }
2538     }   
2539   }
2540   entries = array->GetEntriesFast();  
2541   //
2542   //
2543   if (entries>0){
2544     TObjArray * newarray = new TObjArray();  
2545     TMath::Sort(entries,chi2,index,kFALSE);
2546     besttrack = (AliITStrackMI*)array->At(index[0]);
2547     if (besttrack){
2548       //
2549       for (Int_t i=0;i<6;i++){
2550         if (besttrack->fNz[i]>0&&besttrack->fNy[i]>0){
2551           erry[i] = besttrack->fSigmaY[i]; erry[i+6] = besttrack->fSigmaY[i+6];
2552           errz[i] = besttrack->fSigmaZ[i]; errz[i+6] = besttrack->fSigmaZ[i+6];
2553           ny[i]   = besttrack->fNy[i];
2554           nz[i]   = besttrack->fNz[i];
2555         }
2556       }
2557       besttrack->fChi2MIP[0] = GetNormalizedChi2(besttrack,mode);
2558       Float_t minchi2 = TMath::Min(besttrack->fChi2MIP[0]+5.+besttrack->fNUsed, double(kMaxChi2PerCluster[0]));
2559       Float_t minn = besttrack->GetNumberOfClusters()-3;
2560       Int_t accepted=0;
2561       for (Int_t i=0;i<entries;i++){
2562         AliITStrackMI * track = (AliITStrackMI*)array->At(index[i]);    
2563         if (!track) continue;
2564         if (accepted>maxcut) break;
2565         track->fChi2MIP[0] = GetNormalizedChi2(track,mode);
2566         if (track->fConstrain || track->fN>5){  //keep best short tracks - without vertex constrain
2567           if (track->GetNumberOfClusters()<6 && (track->fChi2MIP[0]+track->fNUsed>minchi2)){
2568             delete array->RemoveAt(index[i]);
2569             continue;
2570           }
2571         }
2572         Bool_t shortbest = !track->fConstrain && track->fN<6;
2573         if ((track->fChi2MIP[0]+track->fNUsed<minchi2 && track->GetNumberOfClusters()>=minn) ||shortbest){
2574           if (!shortbest) accepted++;
2575           //
2576           newarray->AddLast(array->RemoveAt(index[i]));      
2577           for (Int_t i=0;i<6;i++){
2578             if (nz[i]==0){
2579               erry[i] = track->fSigmaY[i]; erry[i+6] = track->fSigmaY[i+6];
2580               errz[i] = track->fSigmaZ[i]; errz[i]   = track->fSigmaZ[i+6];
2581               ny[i]   = track->fNy[i];
2582               nz[i]   = track->fNz[i];
2583             }
2584           }
2585         }
2586         else{
2587           delete array->RemoveAt(index[i]);
2588         }
2589       }
2590       array->Delete();
2591       delete fTrackHypothesys.RemoveAt(esdindex);
2592       fTrackHypothesys.AddAt(newarray,esdindex);
2593     }
2594     else{
2595       array->Delete();
2596       delete fTrackHypothesys.RemoveAt(esdindex);
2597     }
2598   }
2599   delete [] chi2;
2600   delete [] index;
2601 }
2602
2603
2604
2605 AliITStrackMI * AliITStrackerMI::GetBestHypothesys(Int_t esdindex, AliITStrackMI * original, Int_t checkmax)
2606 {
2607   //-------------------------------------------------------------
2608   // try to find best hypothesy
2609   // currently - minimal chi2 of track+backpropagated track+matching to the tpc track
2610   //-------------------------------------------------------------
2611   if (fTrackHypothesys.GetEntriesFast()<=esdindex) return 0;
2612   TObjArray * array = (TObjArray*) fTrackHypothesys.At(esdindex);
2613   if (!array) return 0;
2614   Int_t entries = array->GetEntriesFast();
2615   if (!entries) return 0;  
2616   Float_t minchi2 = 100000;
2617   AliITStrackMI * besttrack=0;
2618   //
2619   AliITStrackMI * backtrack    = new AliITStrackMI(*original);
2620   AliITStrackMI * forwardtrack = new AliITStrackMI(*original);
2621   Double_t xyzv[]={GetX(),GetY(),GetZ()};       
2622   Double_t ersv[]={GetSigmaX()/3.,GetSigmaY()/3.,GetSigmaZ()/3.};
2623   //
2624   for (Int_t i=0;i<entries;i++){    
2625     AliITStrackMI * track = (AliITStrackMI*)array->At(i);    
2626     if (!track) continue;
2627     Float_t sigmarfi,sigmaz;
2628     GetDCASigma(track,sigmarfi,sigmaz);
2629     track->fDnorm[0] = sigmarfi;
2630     track->fDnorm[1] = sigmaz;
2631     //
2632     track->fChi2MIP[1] = 1000000;
2633     track->fChi2MIP[2] = 1000000;
2634     track->fChi2MIP[3] = 1000000;
2635     //
2636     // backtrack
2637     backtrack = new(backtrack) AliITStrackMI(*track); 
2638     if (track->fConstrain){
2639       if (!backtrack->PropagateTo(3.,0.0028,65.19)) continue;
2640       if (!backtrack->Improve(0,xyzv,ersv))         continue;      
2641       if (!backtrack->PropagateTo(2.,0.0028,0))     continue;
2642       if (!backtrack->Improve(0,xyzv,ersv))         continue;
2643       if (!backtrack->PropagateTo(1.,0.0028,0))     continue;
2644       if (!backtrack->Improve(0,xyzv,ersv))         continue;                     
2645       if (!backtrack->PropagateToVertex())          continue;
2646       backtrack->ResetCovariance();      
2647       if (!backtrack->Improve(0,xyzv,ersv))         continue;                           
2648     }else{
2649       backtrack->ResetCovariance();
2650     }
2651     backtrack->ResetClusters();
2652
2653     Double_t x = original->GetX();
2654     if (!RefitAt(x,backtrack,track)) continue;
2655     //
2656     track->fChi2MIP[1] = NormalizedChi2(backtrack,0);
2657     //for (Int_t i=2;i<6;i++){track->fDy[i]+=backtrack->fDy[i]; track->fDz[i]+=backtrack->fDz[i];}
2658     if (track->fChi2MIP[1]>kMaxChi2PerCluster[1]*6.)  continue;
2659     track->fChi22 = GetMatchingChi2(backtrack,original);
2660
2661     if ((track->fConstrain) && track->fChi22>90.)  continue;
2662     if ((!track->fConstrain) && track->fChi22>30.)  continue;
2663     if ( track->fChi22/track->GetNumberOfClusters()>11.)  continue;
2664
2665
2666     if  (!(track->fConstrain)&&track->fChi2MIP[1]>kMaxChi2PerCluster[1])  continue;
2667     Bool_t isOK=kTRUE;
2668     if(!isOK) continue;
2669     //
2670     //forward track - without constraint
2671     forwardtrack = new(forwardtrack) AliITStrackMI(*original);
2672     forwardtrack->ResetClusters();
2673     x = track->GetX();
2674     RefitAt(x,forwardtrack,track);
2675     track->fChi2MIP[2] = NormalizedChi2(forwardtrack,0);    
2676     if  (track->fChi2MIP[2]>kMaxChi2PerCluster[2]*6.0)  continue;
2677     if  (!(track->fConstrain)&&track->fChi2MIP[2]>kMaxChi2PerCluster[2])  continue;
2678     
2679     track->fD[0] = forwardtrack->GetD(GetX(),GetY());
2680     track->fD[1] = forwardtrack->GetZat(GetX())-GetZ();
2681     forwardtrack->fD[0] = track->fD[0];
2682     forwardtrack->fD[1] = track->fD[1];    
2683     {
2684       Int_t list[6];
2685       AliITSclusterV2* clist[6];
2686       track->fChi2MIP[4] = GetNumberOfSharedClusters(track,esdindex,list,clist);      
2687       if ( (!track->fConstrain) && track->fChi2MIP[4]>1.0) continue;
2688     }
2689     
2690     track->fChi2MIP[3] = GetInterpolatedChi2(forwardtrack,backtrack);
2691     if  ( (track->fChi2MIP[3]>6.*kMaxChi2PerCluster[3])) continue;    
2692     if  ( (!track->fConstrain) && (track->fChi2MIP[3]>2*kMaxChi2PerCluster[3])) {
2693       track->fChi2MIP[3]=1000;
2694       continue; 
2695     }
2696     Double_t chi2 = track->fChi2MIP[0]+track->fNUsed;    
2697     //
2698     for (Int_t ichi=0;ichi<5;ichi++){
2699       forwardtrack->fChi2MIP[ichi] = track->fChi2MIP[ichi];
2700     }
2701     if (chi2 < minchi2){
2702       //besttrack = new AliITStrackMI(*forwardtrack);
2703       besttrack = track;
2704       besttrack->SetLabel(track->GetLabel());
2705       besttrack->fFakeRatio = track->fFakeRatio;
2706       minchi2   = chi2;
2707       original->fD[0] = forwardtrack->GetD(GetX(),GetY());
2708       original->fD[1] = forwardtrack->GetZat(GetX())-GetZ();
2709     }    
2710   }
2711   delete backtrack;
2712   delete forwardtrack;
2713   Int_t accepted=0;
2714   for (Int_t i=0;i<entries;i++){    
2715     AliITStrackMI * track = (AliITStrackMI*)array->At(i);   
2716     if (!track) continue;
2717     
2718     if (accepted>checkmax || track->fChi2MIP[3]>kMaxChi2PerCluster[3]*6. || 
2719         (track->GetNumberOfClusters()<besttrack->GetNumberOfClusters()-1.)||
2720         track->fChi2MIP[0]>besttrack->fChi2MIP[0]+2.*besttrack->fNUsed+3.){
2721       if (track->fConstrain || track->fN>5){  //keep best short tracks - without vertex constrain
2722         delete array->RemoveAt(i);    
2723         continue;
2724       }
2725     }
2726     else{
2727       accepted++;
2728     }
2729   }
2730   //
2731   array->Compress();
2732   SortTrackHypothesys(esdindex,checkmax,1);
2733   array = (TObjArray*) fTrackHypothesys.At(esdindex);
2734   if (!array) return 0; // PH What can be the reason? Check SortTrackHypothesys
2735   besttrack = (AliITStrackMI*)array->At(0);  
2736   if (!besttrack)  return 0;
2737   besttrack->fChi2MIP[8]=0;
2738   fBestTrackIndex[esdindex]=0;
2739   entries = array->GetEntriesFast();
2740   AliITStrackMI *longtrack =0;
2741   minchi2 =1000;
2742   Float_t minn=besttrack->GetNumberOfClusters()+besttrack->fNDeadZone;
2743   for (Int_t itrack=entries-1;itrack>0;itrack--){
2744     AliITStrackMI * track = (AliITStrackMI*)array->At(itrack);
2745     if (!track->fConstrain) continue;
2746     if (track->GetNumberOfClusters()+track->fNDeadZone<minn) continue;
2747     if (track->fChi2MIP[0]-besttrack->fChi2MIP[0]>0.0) continue;
2748     if (track->fChi2MIP[0]>4.) continue;
2749     minn = track->GetNumberOfClusters()+track->fNDeadZone;
2750     longtrack =track;
2751   }
2752   //if (longtrack) besttrack=longtrack;
2753
2754   Int_t list[6];
2755   AliITSclusterV2 * clist[6];
2756   Float_t shared = GetNumberOfSharedClusters(besttrack,esdindex,list,clist);
2757   if (besttrack->fConstrain&&besttrack->fChi2MIP[0]<kMaxChi2PerCluster[0]&&besttrack->fChi2MIP[1]<kMaxChi2PerCluster[1]
2758       &&besttrack->fChi2MIP[2]<kMaxChi2PerCluster[2]&&besttrack->fChi2MIP[3]<kMaxChi2PerCluster[3]){ 
2759     RegisterClusterTracks(besttrack,esdindex);
2760   }
2761   //
2762   //
2763   if (shared>0.0){
2764     Int_t nshared;
2765     Int_t overlist[6];
2766     Int_t sharedtrack = GetOverlapTrack(besttrack, esdindex, nshared, list, overlist);
2767     if (sharedtrack>=0){
2768       //
2769       besttrack = GetBest2Tracks(esdindex,sharedtrack,10,5.5);     
2770       if (besttrack){
2771         shared = GetNumberOfSharedClusters(besttrack,esdindex,list,clist);
2772       }
2773       else return 0;
2774     }
2775   }  
2776   
2777   if (shared>2.5) return 0;
2778   if (shared>1.0) return besttrack;
2779   //
2780   // Don't sign clusters if not gold track
2781   //
2782   if (!besttrack->IsGoldPrimary()) return besttrack;
2783   if (besttrack->fESDtrack->GetKinkIndex(0)!=0) return besttrack;   //track belong to kink
2784   //
2785   if (fConstraint[fPass]){
2786     //
2787     // sign clusters
2788     //
2789     Float_t *ny = GetNy(esdindex), *nz = GetNz(esdindex);
2790     for (Int_t i=0;i<6;i++){
2791       Int_t index = besttrack->fClIndex[i];
2792       if (index<=0) continue; 
2793       Int_t ilayer =  (index & 0xf0000000) >> 28;
2794       if (besttrack->fSigmaY[ilayer]<0.00000000001) continue;
2795       AliITSclusterV2 *c = (AliITSclusterV2*)GetCluster(index);     
2796       if (!c) continue;
2797       if (ilayer>3&&c->GetNy()+c->GetNz()>6) continue;
2798       if ( (c->GetNy()+c->GetNz() )> ny[i]+nz[i]+0.7) continue; //shared track
2799       if (  c->GetNz()> nz[i]+0.7) continue; //shared track
2800       if ( ilayer>2&& besttrack->fNormQ[ilayer]/besttrack->fExpQ>1.5) continue;
2801       //if (  c->GetNy()> ny[i]+0.7) continue; //shared track
2802
2803       Bool_t cansign = kTRUE;
2804       for (Int_t itrack=0;itrack<entries; itrack++){
2805         AliITStrackMI * track = (AliITStrackMI*)array->At(i);   
2806         if (!track) continue;
2807         if (track->fChi2MIP[0]>besttrack->fChi2MIP[0]+2.*shared+1.) break;
2808         if ( (track->fClIndex[ilayer]>0) && (track->fClIndex[ilayer]!=besttrack->fClIndex[ilayer])){
2809           cansign = kFALSE;
2810           break;
2811         }
2812       }
2813       if (cansign){
2814         if (TMath::Abs(besttrack->fDy[ilayer]/besttrack->fSigmaY[ilayer])>3.) continue;
2815         if (TMath::Abs(besttrack->fDz[ilayer]/besttrack->fSigmaZ[ilayer])>3.) continue;    
2816         if (!c->IsUsed()) c->Use();
2817       }
2818     }
2819   }
2820   return besttrack;
2821
2822
2823
2824
2825 void  AliITStrackerMI::GetBestHypothesysMIP(TObjArray &itsTracks)
2826 {
2827   //
2828   // get "best" hypothesys
2829   //
2830
2831   Int_t nentries = itsTracks.GetEntriesFast();
2832   for (Int_t i=0;i<nentries;i++){
2833     AliITStrackMI* track = (AliITStrackMI*)itsTracks.At(i);
2834     if (!track) continue;
2835     TObjArray * array = (TObjArray*) fTrackHypothesys.At(i);
2836     if (!array) continue;
2837     if (array->GetEntriesFast()<=0) continue;
2838     //
2839     AliITStrackMI* longtrack=0;
2840     Float_t minn=0;
2841     Float_t maxchi2=1000;
2842     for (Int_t j=0;j<array->GetEntriesFast();j++){
2843       AliITStrackMI* track = (AliITStrackMI*)array->At(j);
2844       if (!track) continue;
2845       if (track->fGoldV0) {
2846         longtrack = track;   //gold V0 track taken
2847         break;
2848       }
2849       if (track->GetNumberOfClusters()+track->fNDeadZone<minn) continue;
2850       Float_t chi2 = track->fChi2MIP[0];
2851       if (fAfterV0){
2852         if (!track->fGoldV0&&track->fConstrain==kFALSE) chi2+=5;
2853       }
2854       if (track->GetNumberOfClusters()+track->fNDeadZone>minn) maxchi2 = track->fChi2MIP[0];       
2855       //
2856       if (chi2 > maxchi2) continue;
2857       minn= track->GetNumberOfClusters()+track->fNDeadZone;
2858       maxchi2 = chi2;
2859       longtrack=track;
2860     }    
2861     //
2862     //
2863     //
2864     AliITStrackMI * besttrack = (AliITStrackMI*)array->At(0);
2865     if (!longtrack) {longtrack = besttrack;}
2866     else besttrack= longtrack;
2867     //
2868     if (besttrack){
2869       Int_t list[6];
2870       AliITSclusterV2 * clist[6];
2871       Float_t shared = GetNumberOfSharedClusters(longtrack,i,list,clist);
2872       //
2873       track->fNUsed = shared;      
2874       track->fNSkipped = besttrack->fNSkipped;
2875       track->fChi2MIP[0] = besttrack->fChi2MIP[0];
2876       if (shared>0){
2877         Int_t nshared;
2878         Int_t overlist[6]; 
2879         //
2880         Int_t sharedtrack = GetOverlapTrack(longtrack, i, nshared, list, overlist);
2881         //if (sharedtrack==-1) sharedtrack=0;
2882         if (sharedtrack>=0){       
2883           besttrack = GetBest2Tracks(i,sharedtrack,10,5.5);                       
2884         }
2885       }   
2886       if (besttrack&&fAfterV0){
2887         UpdateESDtrack(besttrack,AliESDtrack::kITSin);
2888       }
2889       if (besttrack&&fConstraint[fPass]) 
2890         UpdateESDtrack(besttrack,AliESDtrack::kITSin);
2891       //if (besttrack&&besttrack->fConstrain) 
2892       //        UpdateESDtrack(besttrack,AliESDtrack::kITSin);
2893       if (besttrack->fChi2MIP[0]+besttrack->fNUsed>1.5){
2894         if ( (TMath::Abs(besttrack->fD[0])>0.1) && fConstraint[fPass]) {
2895           track->fReconstructed= kFALSE;
2896         }
2897         if ( (TMath::Abs(besttrack->fD[1])>0.1) && fConstraint[fPass]){
2898           track->fReconstructed= kFALSE;
2899         }
2900       }       
2901
2902     }    
2903   }
2904
2905
2906
2907 void AliITStrackerMI::CookLabel(AliITStrackMI *track,Float_t wrong) const {
2908   //--------------------------------------------------------------------
2909   //This function "cooks" a track label. If label<0, this track is fake.
2910   //--------------------------------------------------------------------
2911   Int_t tpcLabel=-1; 
2912      
2913   if ( track->fESDtrack)   tpcLabel =  TMath::Abs(track->fESDtrack->GetTPCLabel());
2914
2915    track->fChi2MIP[9]=0;
2916    Int_t nwrong=0;
2917    for (Int_t i=0;i<track->GetNumberOfClusters();i++){
2918      Int_t cindex = track->GetClusterIndex(i);
2919      Int_t l=(cindex & 0xf0000000) >> 28;
2920      AliITSclusterV2 *cl = (AliITSclusterV2*)GetCluster(cindex);
2921      Int_t isWrong=1;
2922      for (Int_t ind=0;ind<3;ind++){
2923        if (tpcLabel>0)
2924          if (cl->GetLabel(ind)==tpcLabel) isWrong=0;
2925      }
2926      track->fChi2MIP[9]+=isWrong*(2<<l);
2927      nwrong+=isWrong;
2928    }
2929    track->fFakeRatio = double(nwrong)/double(track->GetNumberOfClusters());
2930    if (tpcLabel>0){
2931      if (track->fFakeRatio>wrong) track->fLab = -tpcLabel;
2932      else
2933        track->fLab = tpcLabel;
2934    }
2935    
2936 }
2937
2938
2939
2940 void AliITStrackerMI::CookdEdx(AliITStrackMI* track)
2941 {
2942   //
2943   //
2944   //  Int_t list[6];
2945   //AliITSclusterV2 * clist[6];
2946   //  Int_t shared = GetNumberOfSharedClusters(track,index,list,clist);
2947   Float_t dedx[4];
2948   Int_t accepted=0;
2949   track->fChi2MIP[9]=0;
2950   for (Int_t i=0;i<track->GetNumberOfClusters();i++){
2951     Int_t cindex = track->GetClusterIndex(i);
2952     Int_t l=(cindex & 0xf0000000) >> 28;
2953     AliITSclusterV2 *cl = (AliITSclusterV2*)GetCluster(cindex);
2954     Int_t lab = TMath::Abs(track->fESDtrack->GetTPCLabel());
2955     Int_t isWrong=1;
2956     for (Int_t ind=0;ind<3;ind++){
2957       if (cl->GetLabel(ind)==lab) isWrong=0;
2958     }
2959     track->fChi2MIP[9]+=isWrong*(2<<l);
2960     if (l<2) continue;
2961     //if (l>3 && (cl->GetNy()>4) || (cl->GetNz()>4)) continue;  //shared track
2962     //if (l>3&& !(cl->GetType()==1||cl->GetType()==10)) continue;
2963     //if (l<4&& !(cl->GetType()==1)) continue;   
2964     dedx[accepted]= track->fdEdxSample[i];
2965     //dedx[accepted]= track->fNormQ[l];
2966     accepted++;
2967   }
2968   if (accepted<1) {
2969     track->SetdEdx(0);
2970     return;
2971   }
2972   Int_t indexes[4];
2973   TMath::Sort(accepted,dedx,indexes,kFALSE);
2974   Double_t low=0.;
2975   Double_t up=0.51;    
2976   Double_t nl=low*accepted, nu =up*accepted;  
2977   Float_t sumamp = 0;
2978   Float_t sumweight =0;
2979   for (Int_t i=0; i<accepted; i++) {
2980     Float_t weight =1;
2981     if (i<nl+0.1)     weight = TMath::Max(1.-(nl-i),0.);
2982     if (i>nu-1)     weight = TMath::Max(nu-i,0.);
2983     sumamp+= dedx[indexes[i]]*weight;
2984     sumweight+=weight;
2985   }
2986   track->SetdEdx(sumamp/sumweight);
2987 }
2988
2989
2990 void  AliITStrackerMI::MakeCoeficients(Int_t ntracks){
2991   //
2992   //
2993   if (fCoeficients) delete []fCoeficients;
2994   fCoeficients = new Float_t[ntracks*48];
2995   for (Int_t i=0;i<ntracks*48;i++) fCoeficients[i]=-1.;
2996 }
2997
2998
2999 Double_t AliITStrackerMI::GetPredictedChi2MI(AliITStrackMI* track, const AliITSclusterV2 *cluster,Int_t layer) 
3000 {
3001   //
3002   //
3003   //
3004   Float_t erry,errz;
3005   Float_t theta = track->GetTgl();
3006   Float_t phi   = track->GetSnp();
3007   phi = TMath::Sqrt(phi*phi/(1.-phi*phi));
3008   GetError(layer,cluster,theta,phi,track->fExpQ,erry,errz);
3009   Double_t chi2 = track->GetPredictedChi2MI(cluster->GetY(),cluster->GetZ(),erry,errz);
3010   Float_t ny,nz;
3011   GetNTeor(layer,cluster, theta,phi,ny,nz);  
3012   Double_t delta = cluster->GetNy()+cluster->GetNz()-nz-ny;
3013   if (delta>1){
3014     chi2+=0.5*TMath::Min(delta/2,2.);
3015     chi2+=2.*cluster->GetDeltaProbability();
3016   }
3017   //
3018   track->fNy[layer] =ny;
3019   track->fNz[layer] =nz;
3020   track->fSigmaY[layer] = erry;
3021   track->fSigmaZ[layer] = errz;
3022   //track->fNormQ[layer] = cluster->GetQ()/TMath::Sqrt(1+theta*theta+phi*phi);
3023   track->fNormQ[layer] = cluster->GetQ()/TMath::Sqrt((1.+ track->fP3*track->fP3)/(1.- track->fP2*track->fP2));
3024   return chi2;
3025
3026 }
3027
3028 Int_t    AliITStrackerMI::UpdateMI(AliITStrackMI* track, const AliITSclusterV2* cl,Double_t chi2,Int_t index) const 
3029 {
3030   //
3031   //
3032   //
3033   Int_t layer = (index & 0xf0000000) >> 28;
3034   track->fClIndex[layer] = index;
3035   if ( (layer>1) &&track->fNormQ[layer]/track->fExpQ<0.5 ) {
3036     chi2+= (0.5-track->fNormQ[layer]/track->fExpQ)*10.;
3037     track->fdEdxMismatch+=(0.5-track->fNormQ[layer]/track->fExpQ)*10.;
3038   }
3039   return track->UpdateMI(cl->GetY(),cl->GetZ(),track->fSigmaY[layer],track->fSigmaZ[layer],chi2,index);
3040 }
3041
3042 void AliITStrackerMI::GetNTeor(Int_t layer, const AliITSclusterV2* /*cl*/, Float_t theta, Float_t phi, Float_t &ny, Float_t &nz)
3043 {
3044   //
3045   //get "mean shape"
3046   //
3047   if (layer==0){
3048     ny = 1.+TMath::Abs(phi)*3.2;
3049     nz = 1.+TMath::Abs(theta)*0.34;
3050     return;
3051   }
3052   if (layer==1){
3053     ny = 1.+TMath::Abs(phi)*3.2;
3054     nz = 1.+TMath::Abs(theta)*0.28;
3055     return;
3056   }
3057   
3058   if (layer>3){
3059     ny = 2.02+TMath::Abs(phi)*1.95;
3060     nz = 2.02+TMath::Abs(phi)*2.35;
3061     return;
3062   }
3063   ny  = 6.6-2.7*TMath::Abs(phi);
3064   nz  = 2.8-3.11*TMath::Abs(phi)+0.45*TMath::Abs(theta);
3065 }
3066
3067
3068
3069 Int_t AliITStrackerMI::GetError(Int_t layer, const AliITSclusterV2*cl, Float_t theta, Float_t phi,Float_t expQ, Float_t &erry, Float_t &errz)
3070 {
3071   //calculate cluster position error
3072   //
3073   Float_t nz,ny;
3074   GetNTeor(layer, cl,theta,phi,ny,nz);  
3075   erry   = TMath::Sqrt(cl->GetSigmaY2()); 
3076   errz   = TMath::Sqrt(cl->GetSigmaZ2()); 
3077   //
3078   // PIXELS
3079   if (layer<2){
3080     
3081     if (TMath::Abs(ny-cl->GetNy())>0.6)  {
3082       if (ny<cl->GetNy()){
3083         erry*=0.4+TMath::Abs(ny-cl->GetNy());
3084         errz*=0.4+TMath::Abs(ny-cl->GetNy());
3085       }else{
3086         erry*=0.7+0.5*TMath::Abs(ny-cl->GetNy());
3087         errz*=0.7+0.5*TMath::Abs(ny-cl->GetNy());
3088       }
3089     }
3090     if (TMath::Abs(nz-cl->GetNz())>1.)  {
3091       erry*=TMath::Abs(nz-cl->GetNz());
3092       errz*=TMath::Abs(nz-cl->GetNz());       
3093     }
3094     erry*=0.85;
3095     errz*=0.85;
3096     erry= TMath::Min(erry,float(0.005));
3097     errz= TMath::Min(errz,float(0.03));
3098     return 10;
3099   }
3100
3101 //STRIPS
3102   if (layer>3){ 
3103     //factor 1.8 appears in new simulation
3104     //
3105     Float_t scale=1.8;
3106     if (cl->GetNy()==100||cl->GetNz()==100){
3107       erry = 0.004*scale;
3108       errz = 0.2*scale;
3109       return 100;
3110     }
3111     if (cl->GetNy()+cl->GetNz()>12){
3112       erry = 0.06*scale;
3113       errz = 0.57*scale;
3114       return 100;
3115     }
3116     Float_t normq = cl->GetQ()/(TMath::Sqrt(1+theta*theta+phi*phi));
3117     Float_t chargematch = TMath::Max(double(normq/expQ),2.);
3118     //
3119     if (cl->GetType()==1 || cl->GetType()==10 ){                                                               
3120       if (chargematch<1.0 || (cl->GetNy()+cl->GetNz()<nz+ny+0.5)){
3121         errz = 0.043*scale;
3122         erry = 0.00094*scale;
3123         return 101;
3124       }
3125       if (cl->GetNy()+cl->GetNz()<nz+ny+1.2){
3126         errz = 0.06*scale;
3127         erry =0.0013*scale;
3128         return 102;
3129       }
3130       erry = 0.0027*scale;
3131       errz = TMath::Min(0.028*(chargematch+cl->GetNy()+cl->GetNz()-nz+ny),0.15)*scale;
3132       return 103;
3133     }
3134     if (cl->GetType()==2 || cl->GetType()==11 ){ 
3135       erry = TMath::Min(0.0010*(1+chargematch+cl->GetNy()+cl->GetNz()-nz+ny),0.05)*scale;
3136       errz = TMath::Min(0.025*(1+chargematch+cl->GetNy()+cl->GetNz()-nz+ny),0.5)*scale;
3137       return 104;
3138     }
3139     
3140     if (cl->GetType()>100 ){                                                                   
3141       if ((chargematch+cl->GetNy()+cl->GetNz()-nz-ny<1.5)){
3142         errz = 0.05*scale;
3143         erry = 0.00096*scale;
3144         return 105;
3145       }
3146       if (cl->GetNy()+cl->GetNz()-nz-ny<1){
3147         errz = 0.10*scale;
3148         erry = 0.0025*scale;
3149         return 106;
3150       }
3151
3152       errz = TMath::Min(0.05*(chargematch+cl->GetNy()+cl->GetNz()-nz-ny),0.4)*scale;
3153       erry = TMath::Min(0.003*(chargematch+cl->GetNy()+cl->GetNz()-nz-ny),0.05)*scale;
3154       return 107;
3155     }    
3156     Float_t diff = cl->GetNy()+cl->GetNz()-ny-nz;
3157     if (diff<1) diff=1;
3158     if (diff>4) diff=4;
3159         
3160     if (cl->GetType()==5||cl->GetType()==6||cl->GetType()==7||cl->GetType()==8){
3161       errz = 0.14*diff;
3162       erry = 0.003*diff;
3163       return 108;
3164     }  
3165     erry = 0.04*diff;
3166     errz = 0.06*diff;
3167     return 109;
3168   }
3169   //DRIFTS
3170   Float_t normq = cl->GetQ()/(TMath::Sqrt(1+theta*theta+phi*phi));
3171   Float_t chargematch = normq/expQ;
3172   Float_t factorz=1;
3173   Int_t   cnz = cl->GetNz()%10;
3174   //charge match
3175   if (cl->GetType()==1){
3176     if (chargematch<1.25){
3177       erry =  0.0028*(1.+6./cl->GetQ());  // gold clusters
3178     }
3179     else{
3180       erry = 0.003*chargematch;
3181       if (cl->GetNz()==3) erry*=1.5;
3182     }
3183     if (chargematch<1.0){
3184       errz =  0.0011*(1.+6./cl->GetQ());
3185     }
3186     else{
3187       errz = 0.002*(1+2*(chargematch-1.));
3188     }
3189     if (cnz>nz+0.6) {
3190       erry*=(cnz-nz+0.5);
3191       errz*=1.4*(cnz-nz+0.5);
3192     }
3193   }
3194   if (cl->GetType()>1){
3195     if (chargematch<1){
3196       erry =  0.00385*(1.+6./cl->GetQ());  // gold clusters
3197       errz =  0.0016*(1.+6./cl->GetQ());
3198     }
3199     else{
3200       errz = 0.0014*(1+3*(chargematch-1.));
3201       erry = 0.003*(1+3*(chargematch-1.));
3202     } 
3203     if (cnz>nz+0.6) {
3204       erry*=(cnz-nz+0.5);
3205       errz*=1.4*(cnz-nz+0.5);
3206     }
3207   }
3208
3209   if (TMath::Abs(cl->GetY())>2.5){
3210     factorz*=1+2*(TMath::Abs(cl->GetY())-2.5);
3211   }
3212   if (TMath::Abs(cl->GetY())<1){
3213     factorz*=1.+0.5*TMath::Abs(TMath::Abs(cl->GetY())-1.);
3214   }
3215   factorz= TMath::Min(factorz,float(4.));  
3216   errz*=factorz;
3217
3218   erry= TMath::Min(erry,float(0.05));
3219   errz= TMath::Min(errz,float(0.05));  
3220   return 200;
3221 }
3222
3223
3224
3225 void   AliITStrackerMI::GetDCASigma(AliITStrackMI* track, Float_t & sigmarfi, Float_t &sigmaz)
3226 {
3227   //
3228   //DCA sigmas parameterization
3229   //to be paramterized using external parameters in future 
3230   //
3231   // 
3232   sigmarfi = 0.004+1.4 *TMath::Abs(track->fP4)+332.*track->fP4*track->fP4;
3233   sigmaz   = 0.011+4.37*TMath::Abs(track->fP4);
3234 }
3235
3236
3237 void AliITStrackerMI::SignDeltas( TObjArray *ClusterArray, Float_t vz)
3238 {
3239   //
3240   //  
3241   Int_t entries = ClusterArray->GetEntriesFast();
3242   if (entries<4) return;
3243   AliITSclusterV2* cluster = (AliITSclusterV2*)ClusterArray->At(0);
3244   Int_t layer = cluster->GetLayer();
3245   if (layer>1) return;
3246   Int_t index[10000];
3247   Int_t ncandidates=0;
3248   Float_t r = (layer>0)? 7:4;
3249   // 
3250   for (Int_t i=0;i<entries;i++){
3251     AliITSclusterV2* cl0 = (AliITSclusterV2*)ClusterArray->At(i);
3252     Float_t nz = 1+TMath::Abs((cl0->GetZ()-vz)/r);
3253     if (cl0->GetNy()+cl0->GetNz()<=5+2*layer+nz) continue;
3254     index[ncandidates] = i;  //candidate to belong to delta electron track
3255     ncandidates++;
3256     if (cl0->GetNy()+cl0->GetNz()>9+2*layer+nz) {
3257       cl0->SetDeltaProbability(1);
3258     }
3259   }
3260   //
3261   //  
3262   //
3263   for (Int_t i=0;i<ncandidates;i++){
3264     AliITSclusterV2* cl0 = (AliITSclusterV2*)ClusterArray->At(index[i]);
3265     if (cl0->GetDeltaProbability()>0.8) continue;
3266     // 
3267     Int_t ncl = 0;
3268     Float_t y[100],z[100],sumy,sumz,sumy2, sumyz, sumw;
3269     sumy=sumz=sumy2=sumyz=sumw=0.0;
3270     for (Int_t j=0;j<ncandidates;j++){
3271       if (i==j) continue;
3272       AliITSclusterV2* cl1 = (AliITSclusterV2*)ClusterArray->At(index[j]);
3273       //
3274       Float_t dz = cl0->GetZ()-cl1->GetZ();
3275       Float_t dy = cl0->GetY()-cl1->GetY();
3276       if (TMath::Sqrt(dz*dz+dy*dy)<0.2){
3277         Float_t weight = cl1->GetNy()+cl1->GetNz()-2;
3278         y[ncl] = cl1->GetY();
3279         z[ncl] = cl1->GetZ();
3280         sumy+= y[ncl]*weight;
3281         sumz+= z[ncl]*weight;
3282         sumy2+=y[ncl]*y[ncl]*weight;
3283         sumyz+=y[ncl]*z[ncl]*weight;
3284         sumw+=weight;
3285         ncl++;
3286       }
3287     }
3288     if (ncl<4) continue;
3289     Float_t det = sumw*sumy2  - sumy*sumy;
3290     Float_t delta=1000;
3291     if (TMath::Abs(det)>0.01){
3292       Float_t z0  = (sumy2*sumz - sumy*sumyz)/det;
3293       Float_t k   = (sumyz*sumw - sumy*sumz)/det;
3294       delta = TMath::Abs(cl0->GetZ()-(z0+k*cl0->GetY()));
3295     }
3296     else{
3297       Float_t z0  = sumyz/sumy;
3298       delta = TMath::Abs(cl0->GetZ()-z0);
3299     }
3300     if ( delta<0.05) {
3301       cl0->SetDeltaProbability(1-20.*delta);
3302     }   
3303   }
3304 }
3305
3306
3307 void AliITStrackerMI::UpdateESDtrack(AliITStrackMI* track, ULong_t flags) const
3308 {
3309   //
3310   //
3311   track->UpdateESDtrack(flags);
3312   AliITStrackMI * oldtrack = (AliITStrackMI*)(track->fESDtrack->GetITStrack());
3313   if (oldtrack) delete oldtrack; 
3314   track->fESDtrack->SetITStrack(new AliITStrackMI(*track));
3315   if (TMath::Abs(track->fDnorm[1])<0.000000001){
3316     printf("Problem\n");
3317   }
3318 }
3319
3320
3321
3322 Int_t AliITStrackerMI::GetNearestLayer(const Double_t *xr) const{
3323   //
3324   //Get nearest upper layer close to the point xr.
3325   // rough approximation 
3326   //
3327   const Float_t kRadiuses[6]={4,6.5,15.03,24.,38.5,43.7};
3328   Float_t radius = TMath::Sqrt(xr[0]*xr[0]+xr[1]*xr[1]);
3329   Int_t res =6;
3330   for (Int_t i=0;i<6;i++){
3331     if (radius<kRadiuses[i]){
3332       res =i;
3333       break;
3334     }
3335   }
3336   return res;
3337 }
3338
3339
3340 void AliITStrackerMI::UpdateTPCV0(AliESD *event){
3341   //
3342   //try to update, or reject TPC  V0s
3343   //
3344   Int_t nv0s = event->GetNumberOfV0MIs();
3345   Int_t nitstracks = fTrackHypothesys.GetEntriesFast();
3346
3347   for (Int_t i=0;i<nv0s;i++){
3348     AliESDV0MI * vertex = event->GetV0MI(i);
3349     Int_t ip = vertex->GetIndex(0);
3350     Int_t im = vertex->GetIndex(1);
3351     //
3352     TObjArray * arrayp = (ip<nitstracks) ? (TObjArray*)fTrackHypothesys.At(ip):0;
3353     TObjArray * arraym = (im<nitstracks) ? (TObjArray*)fTrackHypothesys.At(im):0;
3354     AliITStrackMI * trackp = (arrayp!=0) ? (AliITStrackMI*)arrayp->At(0):0;
3355     AliITStrackMI * trackm = (arraym!=0) ? (AliITStrackMI*)arraym->At(0):0;
3356     //
3357     //
3358     if (trackp){
3359       if (trackp->fN+trackp->fNDeadZone>5.5){
3360         if (trackp->fConstrain&&trackp->fChi2MIP[0]<3) vertex->SetStatus(-100);
3361         if (!trackp->fConstrain&&trackp->fChi2MIP[0]<2) vertex->SetStatus(-100); 
3362       }
3363     }
3364
3365     if (trackm){
3366       if (trackm->fN+trackm->fNDeadZone>5.5){
3367         if (trackm->fConstrain&&trackm->fChi2MIP[0]<3) vertex->SetStatus(-100);
3368         if (!trackm->fConstrain&&trackm->fChi2MIP[0]<2) vertex->SetStatus(-100); 
3369       }
3370     }
3371     if (vertex->GetStatus()==-100) continue;
3372     //
3373     Int_t clayer = GetNearestLayer(vertex->GetXrp());
3374     vertex->SetNBefore(clayer);        //
3375     vertex->SetChi2Before(9*clayer);   //
3376     vertex->SetNAfter(6-clayer);       //
3377     vertex->SetChi2After(0);           //
3378     //
3379     if (clayer >1 ){ // calculate chi2 before vertex
3380       Float_t chi2p = 0, chi2m=0;  
3381       //
3382       if (trackp){
3383         for (Int_t ilayer=0;ilayer<clayer;ilayer++){
3384           if (trackp->fClIndex[ilayer]>0){
3385             chi2p+=trackp->fDy[ilayer]*trackp->fDy[ilayer]/(trackp->fSigmaY[ilayer]*trackp->fSigmaY[ilayer])+
3386               trackp->fDz[ilayer]*trackp->fDz[ilayer]/(trackp->fSigmaZ[ilayer]*trackp->fSigmaZ[ilayer]);
3387           }
3388           else{
3389             chi2p+=9;
3390           }
3391         }
3392       }else{
3393         chi2p = 9*clayer;
3394       }
3395       //
3396       if (trackm){
3397         for (Int_t ilayer=0;ilayer<clayer;ilayer++){
3398           if (trackm->fClIndex[ilayer]>0){
3399             chi2m+=trackm->fDy[ilayer]*trackm->fDy[ilayer]/(trackm->fSigmaY[ilayer]*trackm->fSigmaY[ilayer])+
3400               trackm->fDz[ilayer]*trackm->fDz[ilayer]/(trackm->fSigmaZ[ilayer]*trackm->fSigmaZ[ilayer]);
3401           }
3402           else{
3403             chi2m+=9;
3404           }
3405         }
3406       }else{
3407         chi2m = 9*clayer;
3408       }
3409       vertex->SetChi2Before(TMath::Min(chi2p,chi2m));
3410       if (TMath::Min(chi2p,chi2m)/Float_t(clayer)<4) vertex->SetStatus(-10);  // track exist before vertex
3411     }
3412     
3413     if (clayer < 5 ){ // calculate chi2 after vertex
3414       Float_t chi2p = 0, chi2m=0;  
3415       //
3416       if (trackp&&TMath::Abs(trackp->fP3)<1.){
3417         for (Int_t ilayer=clayer;ilayer<6;ilayer++){
3418           if (trackp->fClIndex[ilayer]>0){
3419             chi2p+=trackp->fDy[ilayer]*trackp->fDy[ilayer]/(trackp->fSigmaY[ilayer]*trackp->fSigmaY[ilayer])+
3420               trackp->fDz[ilayer]*trackp->fDz[ilayer]/(trackp->fSigmaZ[ilayer]*trackp->fSigmaZ[ilayer]);
3421           }
3422           else{
3423             chi2p+=9;
3424           }
3425         }
3426       }else{
3427         chi2p = 0;
3428       }
3429       //
3430       if (trackm&&TMath::Abs(trackm->fP3)<1.){
3431         for (Int_t ilayer=clayer;ilayer<6;ilayer++){
3432           if (trackm->fClIndex[ilayer]>0){
3433             chi2m+=trackm->fDy[ilayer]*trackm->fDy[ilayer]/(trackm->fSigmaY[ilayer]*trackm->fSigmaY[ilayer])+
3434               trackm->fDz[ilayer]*trackm->fDz[ilayer]/(trackm->fSigmaZ[ilayer]*trackm->fSigmaZ[ilayer]);
3435           }
3436           else{
3437             chi2m+=9;
3438           }
3439         }
3440       }else{
3441         chi2m = 0;
3442       }
3443       vertex->SetChi2After(TMath::Max(chi2p,chi2m));
3444       if (TMath::Max(chi2m,chi2p)/Float_t(6-clayer)>9) vertex->SetStatus(-20);  // track not found in ITS
3445     }
3446   }
3447   //
3448 }
3449
3450
3451
3452 void  AliITStrackerMI::FindV02(AliESD *event)
3453 {
3454   //
3455   // V0 finder
3456   //
3457   //  Cuts on DCA -  R dependent
3458   //          max distance DCA between 2 tracks cut 
3459   //          maxDist = TMath::Min(kMaxDist,kMaxDist0+pvertex->GetRr()*kMaxDist);
3460   //
3461   const Float_t kMaxDist0      = 0.1;    
3462   const Float_t kMaxDist1      = 0.1;     
3463   const Float_t kMaxDist       = 1;
3464   const Float_t kMinPointAngle  = 0.85;
3465   const Float_t kMinPointAngle2 = 0.99;
3466   const Float_t kMinR           = 0.5;
3467   const Float_t kMaxR           = 220;
3468   //const Float_t kCausality0Cut   = 0.19;
3469   //const Float_t kLikelihood01Cut = 0.25;
3470   //const Float_t kPointAngleCut   = 0.9996;
3471   const Float_t kCausality0Cut   = 0.19;
3472   const Float_t kLikelihood01Cut = 0.45;
3473   const Float_t kLikelihood1Cut  = 0.5;
3474   const Float_t kCombinedCut     = 0.55;
3475
3476   //
3477   //
3478   TTreeSRedirector &cstream = *fDebugStreamer;
3479   Int_t ntracks    = event->GetNumberOfTracks(); 
3480   Int_t nitstracks = fTrackHypothesys.GetEntriesFast();
3481   fOriginal.Expand(ntracks);
3482   fTrackHypothesys.Expand(ntracks);
3483   fBestHypothesys.Expand(ntracks);
3484   //
3485   AliHelix * helixes   = new AliHelix[ntracks+2];
3486   TObjArray trackarray(ntracks+2);     //array with tracks - with vertex constrain
3487   TObjArray trackarrayc(ntracks+2);    //array of "best    tracks" - without vertex constrain
3488   TObjArray trackarrayl(ntracks+2);    //array of "longest tracks" - without vertex constrain
3489   Bool_t * forbidden   = new Bool_t [ntracks+2];
3490   Int_t   *itsmap      = new Int_t  [ntracks+2];
3491   Float_t *dist        = new Float_t[ntracks+2];
3492   Float_t *normdist0   = new Float_t[ntracks+2];
3493   Float_t *normdist1   = new Float_t[ntracks+2];
3494   Float_t *normdist    = new Float_t[ntracks+2];
3495   Float_t *norm        = new Float_t[ntracks+2];
3496   Float_t *maxr        = new Float_t[ntracks+2];
3497   Float_t *minr        = new Float_t[ntracks+2];
3498   Float_t *minPointAngle= new Float_t[ntracks+2];
3499   //
3500   AliESDV0MI *pvertex      = new AliESDV0MI;
3501   AliITStrackMI * dummy= new AliITStrackMI;
3502   dummy->SetLabel(0);
3503   AliITStrackMI  trackat0;    //temporary track for DCA calculation
3504   //
3505   Float_t primvertex[3]={GetX(),GetY(),GetZ()};
3506   //
3507   // make its -  esd map
3508   //
3509   for (Int_t itrack=0;itrack<ntracks+2;itrack++) {
3510     itsmap[itrack]        = -1;
3511     forbidden[itrack]     = kFALSE;
3512     maxr[itrack]          = kMaxR;
3513     minr[itrack]          = kMinR;
3514     minPointAngle[itrack] = kMinPointAngle;
3515   }
3516   for (Int_t itrack=0;itrack<nitstracks;itrack++){
3517     AliITStrackMI * original =   (AliITStrackMI*)(fOriginal.At(itrack));
3518     Int_t           esdindex =   original->fESDtrack->GetID();
3519     itsmap[esdindex]         =   itrack;
3520   }
3521   //
3522   // create its tracks from esd tracks if not done before
3523   //
3524   for (Int_t itrack=0;itrack<ntracks;itrack++){
3525     if (itsmap[itrack]>=0) continue;
3526     AliITStrackMI * tpctrack = new AliITStrackMI(*(event->GetTrack(itrack)));
3527     tpctrack->fD[0] = tpctrack->GetD(GetX(),GetY());
3528     tpctrack->fD[1] = tpctrack->GetZat(GetX())-GetZ(); 
3529     if (tpctrack->fD[0]<20 && tpctrack->fD[1]<20){
3530       // tracks which can reach inner part of ITS
3531       // propagate track to outer its volume - with correction for material
3532       CorrectForDeadZoneMaterial(tpctrack);  
3533     }
3534     itsmap[itrack] = nitstracks;
3535     fOriginal.AddAt(tpctrack,nitstracks);
3536     nitstracks++;
3537   }
3538   //
3539   // fill temporary arrays
3540   //
3541   for (Int_t itrack=0;itrack<ntracks;itrack++){
3542     AliESDtrack *  esdtrack = event->GetTrack(itrack);
3543     Int_t          itsindex = itsmap[itrack];
3544     AliITStrackMI *original = (AliITStrackMI*)fOriginal.At(itsmap[itrack]);
3545     if (!original) continue;
3546     AliITStrackMI *bestConst  = 0;
3547     AliITStrackMI *bestLong   = 0;
3548     AliITStrackMI *best       = 0;    
3549     //
3550     //
3551     TObjArray * array    = (TObjArray*)  fTrackHypothesys.At(itsindex);
3552     Int_t       hentries = (array==0) ?  0 : array->GetEntriesFast();
3553     // Get best track with vertex constrain
3554     for (Int_t ih=0;ih<hentries;ih++){
3555       AliITStrackMI * trackh = (AliITStrackMI*)array->At(ih);
3556       if (!trackh->fConstrain) continue;
3557       if (!bestConst) bestConst = trackh;
3558       if (trackh->fN>5.0){
3559         bestConst  = trackh;                         // full track -  with minimal chi2
3560         break;
3561       }
3562       if (trackh->fN+trackh->fNDeadZone<=bestConst->fN+bestConst->fNDeadZone)  continue;      
3563       bestConst = trackh;
3564       break;
3565     }
3566     // Get best long track without vertex constrain and best track without vertex constrain
3567     for (Int_t ih=0;ih<hentries;ih++){
3568       AliITStrackMI * trackh = (AliITStrackMI*)array->At(ih);
3569       if (trackh->fConstrain) continue;
3570       if (!best)     best     = trackh;
3571       if (!bestLong) bestLong = trackh;
3572       if (trackh->fN>5.0){
3573         bestLong  = trackh;                         // full track -  with minimal chi2
3574         break;
3575       }
3576       if (trackh->fN+trackh->fNDeadZone<=bestLong->fN+bestLong->fNDeadZone)  continue;      
3577       bestLong = trackh;        
3578     }
3579     if (!best) {
3580       best     = original;
3581       bestLong = original;
3582     }
3583     trackat0 = *bestLong;
3584     Double_t xx,yy,zz,alpha; 
3585     bestLong->GetGlobalXYZat(bestLong->GetX(),xx,yy,zz);
3586     alpha = TMath::ATan2(yy,xx);    
3587     trackat0.Propagate(alpha,0);      
3588     // calculate normalized distances to the vertex 
3589     //
3590     Float_t ptfac  = (1.+100.*TMath::Abs(trackat0.fP4));
3591     if ( bestLong->fN>3 ){      
3592       dist[itsindex]      = trackat0.fP0;
3593       norm[itsindex]      = ptfac*TMath::Sqrt(trackat0.fC00);
3594       normdist0[itsindex] = TMath::Abs(trackat0.fP0/norm[itsindex]);
3595       normdist1[itsindex] = TMath::Abs((trackat0.fP1-primvertex[2])/(ptfac*TMath::Sqrt(trackat0.fC11)));
3596       normdist[itsindex]  = TMath::Sqrt(normdist0[itsindex]*normdist0[itsindex]+normdist1[itsindex]*normdist1[itsindex]);
3597       if (!bestConst){
3598         if (bestLong->fN+bestLong->fNDeadZone<6) normdist[itsindex]*=2.;
3599         if (bestLong->fN+bestLong->fNDeadZone<5) normdist[itsindex]*=2.;
3600         if (bestLong->fN+bestLong->fNDeadZone<4) normdist[itsindex]*=2.;
3601       }else{
3602         if (bestConst->fN+bestConst->fNDeadZone<6) normdist[itsindex]*=1.5;
3603         if (bestConst->fN+bestConst->fNDeadZone<5) normdist[itsindex]*=1.5;
3604       }
3605     }
3606     else{      
3607       if (bestConst&&bestConst->fN+bestConst->fNDeadZone>4.5){
3608         dist[itsindex] = bestConst->fD[0];
3609         norm[itsindex] = bestConst->fDnorm[0];
3610         normdist0[itsindex] = TMath::Abs(bestConst->fD[0]/norm[itsindex]);
3611         normdist1[itsindex] = TMath::Abs(bestConst->fD[0]/norm[itsindex]);
3612         normdist[itsindex]  = TMath::Sqrt(normdist0[itsindex]*normdist0[itsindex]+normdist1[itsindex]*normdist1[itsindex]);
3613       }else{
3614         dist[itsindex]      = trackat0.fP0;
3615         norm[itsindex]      = ptfac*TMath::Sqrt(trackat0.fC00);
3616         normdist0[itsindex] = TMath::Abs(trackat0.fP0/norm[itsindex]);
3617         normdist1[itsindex] = TMath::Abs((trackat0.fP1-primvertex[2])/(ptfac*TMath::Sqrt(trackat0.fC11)));
3618         normdist[itsindex]  = TMath::Sqrt(normdist0[itsindex]*normdist0[itsindex]+normdist1[itsindex]*normdist1[itsindex]);
3619         if (TMath::Abs(trackat0.fP3)>1.05){
3620           if (normdist[itsindex]<3) forbidden[itsindex]=kTRUE;
3621           if (normdist[itsindex]>3) {
3622             minr[itsindex] = TMath::Max(Float_t(40.),minr[itsindex]);
3623           }
3624         }
3625       }
3626     }
3627     //
3628     //-----------------------------------------------------------
3629     //Forbid primary track candidates - 
3630     //
3631     //treetr->SetAlias("forbidden0","Tr0.fN<4&&Tr1.fN+Tr1.fNDeadZone>4.5");
3632     //treetr->SetAlias("forbidden1","ND<3&&Tr1.fN+Tr1.fNDeadZone>5.5");
3633     //treetr->SetAlias("forbidden2","ND<2&&Tr1.fClIndex[0]>0&&Tr1.fClIndex[0]>0");
3634     //treetr->SetAlias("forbidden3","ND<1&&Tr1.fClIndex[0]>0");
3635     //treetr->SetAlias("forbidden4","ND<4&&Tr1.fNormChi2[0]<2");
3636     //treetr->SetAlias("forbidden5","ND<5&&Tr1.fNormChi2[0]<1");
3637     //-----------------------------------------------------------
3638     if (bestConst){
3639       if (bestLong->fN<4       && bestConst->fN+bestConst->fNDeadZone>4.5)               forbidden[itsindex]=kTRUE;
3640       if (normdist[itsindex]<3 && bestConst->fN+bestConst->fNDeadZone>5.5)               forbidden[itsindex]=kTRUE;
3641       if (normdist[itsindex]<2 && bestConst->fClIndex[0]>0 && bestConst->fClIndex[1]>0 ) forbidden[itsindex]=kTRUE;
3642       if (normdist[itsindex]<1 && bestConst->fClIndex[0]>0)                              forbidden[itsindex]=kTRUE;
3643       if (normdist[itsindex]<4 && bestConst->fNormChi2[0]<2)                             forbidden[itsindex]=kTRUE;
3644       if (normdist[itsindex]<5 && bestConst->fNormChi2[0]<1)                             forbidden[itsindex]=kTRUE;      
3645       if (bestConst->fNormChi2[0]<2.5) {
3646         minPointAngle[itsindex]= 0.9999;
3647         maxr[itsindex]         = 10;
3648       }
3649     }
3650     //
3651     //forbid daughter kink candidates
3652     //
3653     if (esdtrack->GetKinkIndex(0)>0) forbidden[itsindex] = kTRUE;
3654     Bool_t isElectron = kTRUE;
3655     Bool_t isProton   = kTRUE;
3656     Double_t pid[5];
3657     esdtrack->GetESDpid(pid);
3658     for (Int_t i=1;i<5;i++){
3659       if (pid[0]<pid[i]) isElectron= kFALSE;
3660       if (pid[4]<pid[i]) isProton= kFALSE;
3661     }
3662     if (isElectron){
3663       forbidden[itsindex]=kFALSE;       
3664       normdist[itsindex]*=-1;
3665     }
3666     if (isProton){
3667       if (normdist[itsindex]>2) forbidden[itsindex]=kFALSE;     
3668       normdist[itsindex]*=-1;
3669     }
3670
3671     //
3672     // Causality cuts in TPC volume
3673     //
3674     if (esdtrack->GetTPCdensity(0,10) >0.6)  maxr[itsindex] = TMath::Min(Float_t(110),maxr[itsindex]);
3675     if (esdtrack->GetTPCdensity(10,30)>0.6)  maxr[itsindex] = TMath::Min(Float_t(120),maxr[itsindex]);
3676     if (esdtrack->GetTPCdensity(20,40)>0.6)  maxr[itsindex] = TMath::Min(Float_t(130),maxr[itsindex]);
3677     if (esdtrack->GetTPCdensity(30,50)>0.6)  maxr[itsindex] = TMath::Min(Float_t(140),maxr[itsindex]);
3678     //
3679     if (esdtrack->GetTPCdensity(0,60)<0.4&&bestLong->fN<3) minr[itsindex]=100;    
3680     //
3681     //
3682     if (kFALSE){
3683       cstream<<"Track"<<
3684         "Tr0.="<<best<<
3685         "Tr1.="<<((bestConst)? bestConst:dummy)<<
3686         "Tr2.="<<bestLong<<
3687         "Tr3.="<<&trackat0<<
3688         "Esd.="<<esdtrack<<
3689         "Dist="<<dist[itsindex]<<
3690         "ND0="<<normdist0[itsindex]<<
3691         "ND1="<<normdist1[itsindex]<<
3692         "ND="<<normdist[itsindex]<<
3693         "Pz="<<primvertex[2]<<
3694         "Forbid="<<forbidden[itsindex]<<
3695         "\n";
3696       //
3697     }
3698     trackarray.AddAt(best,itsindex);
3699     trackarrayc.AddAt(bestConst,itsindex);
3700     trackarrayl.AddAt(bestLong,itsindex);
3701     new (&helixes[itsindex]) AliHelix(*best);
3702   }
3703   //
3704   //
3705   //
3706   // first iterration of V0 finder
3707   //
3708   for (Int_t iesd0=0;iesd0<ntracks;iesd0++){
3709     Int_t itrack0 = itsmap[iesd0];
3710     if (forbidden[itrack0]) continue;
3711     AliITStrackMI * btrack0 = (AliITStrackMI*)trackarray.At(itrack0);
3712     if (!btrack0) continue;    
3713     if (btrack0->fP4>0) continue;
3714     AliITStrackMI *trackc0 = (AliITStrackMI*)trackarrayc.At(itrack0);
3715     //
3716     for (Int_t iesd1=0;iesd1<ntracks;iesd1++){
3717       Int_t itrack1 = itsmap[iesd1];
3718       if (forbidden[itrack1]) continue;
3719
3720       AliITStrackMI * btrack1 = (AliITStrackMI*)trackarray.At(itrack1); 
3721       if (!btrack1) continue;
3722       if (btrack1->fP4<0) continue;
3723       Bool_t isGold = kFALSE;
3724       if (TMath::Abs(TMath::Abs(btrack0->GetLabel())-TMath::Abs(btrack1->GetLabel()))==1){
3725         isGold = kTRUE;
3726       }
3727       AliITStrackMI *trackc1 = (AliITStrackMI*)trackarrayc.At(itrack1);
3728       AliHelix &h1 = helixes[itrack0];
3729       AliHelix &h2 = helixes[itrack1];
3730       //
3731       // find linear distance
3732       Double_t rmin =0;
3733       //
3734       //
3735       //
3736       Double_t phase[2][2],radius[2];
3737       Int_t  points = h1.GetRPHIintersections(h2, phase, radius);
3738       if    (points==0)  continue;
3739       Double_t delta[2]={1000000,1000000};        
3740       rmin = radius[0];
3741       h1.ParabolicDCA(h2,phase[0][0],phase[0][1],radius[0],delta[0]);
3742       if (points==2){    
3743         if (radius[1]<rmin) rmin = radius[1];
3744         h1.ParabolicDCA(h2,phase[1][0],phase[1][1],radius[1],delta[1]);
3745       }
3746       rmin = TMath::Sqrt(rmin);
3747       Double_t distance = 0;
3748       Double_t radiusC  = 0;
3749       Int_t    iphase   = 0;
3750       if (points==1 || delta[0]<delta[1]){
3751         distance = TMath::Sqrt(delta[0]);
3752         radiusC  = TMath::Sqrt(radius[0]);
3753       }else{
3754         distance = TMath::Sqrt(delta[1]);
3755         radiusC  = TMath::Sqrt(radius[1]);
3756         iphase=1;
3757       }
3758       if (radiusC<TMath::Max(minr[itrack0],minr[itrack1]))    continue;
3759       if (radiusC>TMath::Min(maxr[itrack0],maxr[itrack1]))     continue; 
3760       Float_t maxDist  = TMath::Min(kMaxDist,Float_t(kMaxDist0+radiusC*kMaxDist1));      
3761       if (distance>maxDist) continue;
3762       Float_t pointAngle = h1.GetPointAngle(h2,phase[iphase],primvertex);
3763       if (pointAngle<TMath::Max(minPointAngle[itrack0],minPointAngle[itrack1])) continue;
3764       //
3765       //
3766       //      Double_t distance = TestV0(h1,h2,pvertex,rmin);
3767       //
3768       //       if (distance>maxDist)           continue;
3769       //       if (pvertex->GetRr()<kMinR)     continue;
3770       //       if (pvertex->GetRr()>kMaxR)     continue;
3771       AliITStrackMI * track0=btrack0;
3772       AliITStrackMI * track1=btrack1;
3773       //      if (pvertex->GetRr()<3.5){  
3774       if (radiusC<3.5){  
3775         //use longest tracks inside the pipe
3776         track0 = (AliITStrackMI*)trackarrayl.At(itrack0);
3777         track1 = (AliITStrackMI*)trackarrayl.At(itrack1);
3778       }      
3779       //
3780       //
3781       pvertex->SetM(*track0);
3782       pvertex->SetP(*track1);
3783       pvertex->Update(primvertex);
3784       pvertex->SetClusters(track0->fClIndex,track1->fClIndex);  // register clusters
3785
3786       if (pvertex->GetRr()<kMinR) continue;
3787       if (pvertex->GetRr()>kMaxR) continue;
3788       if (pvertex->GetPointAngle()<kMinPointAngle) continue;
3789       if (pvertex->GetDist2()>maxDist) continue;
3790       pvertex->SetLab(0,track0->GetLabel());
3791       pvertex->SetLab(1,track1->GetLabel());
3792       pvertex->SetIndex(0,track0->fESDtrack->GetID());
3793       pvertex->SetIndex(1,track1->fESDtrack->GetID());
3794       
3795       //      
3796       AliITStrackMI * htrackc0 = trackc0 ? trackc0:dummy;      
3797       AliITStrackMI * htrackc1 = trackc1 ? trackc1:dummy;
3798
3799       //
3800       //
3801       TObjArray * array0b     = (TObjArray*)fBestHypothesys.At(itrack0);
3802       if (!array0b&&pvertex->GetRr()<40 && TMath::Abs(track0->fP3)<1.1) 
3803         FollowProlongationTree((AliITStrackMI*)fOriginal.At(itrack0),itrack0, kFALSE);
3804       TObjArray * array1b    = (TObjArray*)fBestHypothesys.At(itrack1);
3805       if (!array1b&&pvertex->GetRr()<40 && TMath::Abs(track1->fP3)<1.1) 
3806         FollowProlongationTree((AliITStrackMI*)fOriginal.At(itrack1),itrack1, kFALSE);
3807       //
3808       AliITStrackMI * track0b = (AliITStrackMI*)fOriginal.At(itrack0);       
3809       AliITStrackMI * track1b = (AliITStrackMI*)fOriginal.At(itrack1);
3810       AliITStrackMI * track0l = (AliITStrackMI*)fOriginal.At(itrack0);       
3811       AliITStrackMI * track1l = (AliITStrackMI*)fOriginal.At(itrack1);
3812       
3813       Float_t minchi2before0=16;
3814       Float_t minchi2before1=16;
3815       Float_t minchi2after0 =16;
3816       Float_t minchi2after1 =16;
3817       Int_t maxLayer = GetNearestLayer(pvertex->GetXrp());
3818       
3819       if (array0b) for (Int_t i=0;i<5;i++){
3820         // best track after vertex
3821         AliITStrackMI * btrack = (AliITStrackMI*)array0b->At(i);
3822         if (!btrack) continue;
3823         if (btrack->fN>track0l->fN) track0l = btrack;     
3824         //      if (btrack->fX<pvertex->GetRr()-2.-0.5/(0.1+pvertex->GetAnglep()[2])) {
3825         if (btrack->fX<pvertex->GetRr()-2.) {
3826           if ( (maxLayer>i+2|| (i==0)) && btrack->fN==(6-i)&&i<3){
3827             Float_t sumchi2= 0;
3828             Float_t sumn   = 0;
3829             if (maxLayer<3){   // take prim vertex as additional measurement
3830               if (normdist[itrack0]>0 && htrackc0){
3831                 sumchi2 += TMath::Min((3.-maxLayer)*normdist[itrack0]*normdist[itrack0],16.);
3832               }else{
3833                 sumchi2 +=  TMath::Min((3.-maxLayer)*(3*normdist[itrack0]*normdist[itrack0]+3.),16.);
3834               }
3835               sumn    +=  3-maxLayer;
3836             }
3837             for (Int_t ilayer=i;ilayer<maxLayer;ilayer++){
3838               sumn+=1.;       
3839               if (!btrack->fClIndex[ilayer]){
3840                 sumchi2+=25;
3841                 continue;
3842               }else{
3843                 Int_t c=( btrack->fClIndex[ilayer] & 0x0fffffff);
3844                 for (Int_t itrack=0;itrack<4;itrack++){
3845                   if (fgLayers[ilayer].fClusterTracks[itrack][c]>=0 && fgLayers[ilayer].fClusterTracks[itrack][c]!=itrack0){
3846                     sumchi2+=18.;  //shared cluster
3847                     break;
3848                   }
3849                 }
3850                 sumchi2+=btrack->fDy[ilayer]*btrack->fDy[ilayer]/(btrack->fSigmaY[ilayer]*btrack->fSigmaY[ilayer]);
3851                 sumchi2+=btrack->fDz[ilayer]*btrack->fDz[ilayer]/(btrack->fSigmaZ[ilayer]*btrack->fSigmaZ[ilayer]);            
3852               }
3853             }
3854             sumchi2/=sumn;
3855             if (sumchi2<minchi2before0) minchi2before0=sumchi2; 
3856           }
3857           continue;   //safety space - Geo manager will give exact layer
3858         }
3859         track0b       = btrack;
3860         minchi2after0 = btrack->fNormChi2[i];
3861         break;
3862       }
3863       if (array1b) for (Int_t i=0;i<5;i++){
3864         // best track after vertex
3865         AliITStrackMI * btrack = (AliITStrackMI*)array1b->At(i);
3866         if (!btrack) continue;
3867         if (btrack->fN>track1l->fN) track1l = btrack;     
3868         //      if (btrack->fX<pvertex->GetRr()-2-0.5/(0.1+pvertex->GetAnglep()[2])){
3869         if (btrack->fX<pvertex->GetRr()-2){
3870           if ((maxLayer>i+2 || (i==0))&&btrack->fN==(6-i)&&(i<3)){
3871             Float_t sumchi2= 0;
3872             Float_t sumn   = 0;
3873             if (maxLayer<3){   // take prim vertex as additional measurement
3874               if (normdist[itrack1]>0 && htrackc1){
3875                 sumchi2 +=  TMath::Min((3.-maxLayer)*normdist[itrack1]*normdist[itrack1],16.);
3876               }else{
3877                 sumchi2 += TMath::Min((3.-maxLayer)*(3*normdist[itrack1]*normdist[itrack1]+3.),16.);
3878               }
3879               sumn    +=  3-maxLayer;
3880             }
3881             for (Int_t ilayer=i;ilayer<maxLayer;ilayer++){
3882               sumn+=1.;
3883               if (!btrack->fClIndex[ilayer]){
3884                 sumchi2+=30;
3885                 continue;
3886               }else{
3887                 Int_t c=( btrack->fClIndex[ilayer] & 0x0fffffff);
3888                 for (Int_t itrack=0;itrack<4;itrack++){
3889                   if (fgLayers[ilayer].fClusterTracks[itrack][c]>=0 && fgLayers[ilayer].fClusterTracks[itrack][c]!=itrack1){
3890                     sumchi2+=18.;  //shared cluster
3891                     break;
3892                   }
3893                 }
3894                 sumchi2+=btrack->fDy[ilayer]*btrack->fDy[ilayer]/(btrack->fSigmaY[ilayer]*btrack->fSigmaY[ilayer]);
3895                 sumchi2+=btrack->fDz[ilayer]*btrack->fDz[ilayer]/(btrack->fSigmaZ[ilayer]*btrack->fSigmaZ[ilayer]);            
3896               }
3897             }
3898             sumchi2/=sumn;
3899             if (sumchi2<minchi2before1) minchi2before1=sumchi2; 
3900           }
3901           continue;   //safety space - Geo manager will give exact layer           
3902         }
3903         track1b = btrack;
3904         minchi2after1 = btrack->fNormChi2[i];
3905         break;
3906       }
3907       //
3908       // position resolution - used for DCA cut
3909       Float_t sigmad = track0b->fC00+track0b->fC11+track1b->fC00+track1b->fC11+
3910         (track0b->fX-pvertex->GetRr())*(track0b->fX-pvertex->GetRr())*(track0b->fC22+track0b->fC33)+
3911         (track1b->fX-pvertex->GetRr())*(track1b->fX-pvertex->GetRr())*(track1b->fC22+track1b->fC33);
3912       sigmad =TMath::Sqrt(sigmad)+0.04;
3913       if (pvertex->GetRr()>50){
3914         Double_t cov0[15],cov1[15];
3915         track0b->fESDtrack->GetInnerExternalCovariance(cov0);
3916         track1b->fESDtrack->GetInnerExternalCovariance(cov1);
3917         sigmad = cov0[0]+cov0[2]+cov1[0]+cov1[2]+
3918           (80.-pvertex->GetRr())*(80.-pvertex->GetRr())*(cov0[5]+cov0[9])+
3919           (80.-pvertex->GetRr())*(80.-pvertex->GetRr())*(cov1[5]+cov1[9]);
3920         sigmad =TMath::Sqrt(sigmad)+0.05;
3921       }
3922       //       
3923       AliESDV0MI vertex2;
3924       vertex2.SetM(*track0b);
3925       vertex2.SetP(*track1b);
3926       vertex2.Update(primvertex);
3927       if (vertex2.GetDist2()<=pvertex->GetDist2()&&(vertex2.GetPointAngle()>=pvertex->GetPointAngle())){
3928         pvertex->SetM(*track0b);
3929         pvertex->SetP(*track1b);
3930         pvertex->Update(primvertex);
3931         pvertex->SetClusters(track0b->fClIndex,track1b->fClIndex);  // register clusters
3932         pvertex->SetIndex(0,track0->fESDtrack->GetID());
3933         pvertex->SetIndex(1,track1->fESDtrack->GetID());
3934       }
3935       pvertex->SetDistSigma(sigmad);
3936       pvertex->SetDistNorm(pvertex->GetDist2()/sigmad);       
3937       pvertex->SetNormDCAPrim(normdist[itrack0],normdist[itrack1]);
3938       //
3939       // define likelihhod and causalities
3940       //
3941       Float_t pa0=1, pa1=1, pb0=0.26, pb1=0.26;      
3942       if (maxLayer<1){
3943         Float_t fnorm0 = normdist[itrack0];
3944         if (fnorm0<0) fnorm0*=-3;
3945         Float_t fnorm1 = normdist[itrack1];
3946         if (fnorm1<0) fnorm1*=-3;
3947         if (pvertex->GetAnglep()[2]>0.1 ||  (pvertex->GetRr()<10.5)&& pvertex->GetAnglep()[2]>0.05 || pvertex->GetRr()<3){
3948           pb0    =  TMath::Exp(-TMath::Min(fnorm0,Float_t(16.))/12.);
3949           pb1    =  TMath::Exp(-TMath::Min(fnorm1,Float_t(16.))/12.);
3950         }
3951         pvertex->SetChi2Before(normdist[itrack0]);
3952         pvertex->SetChi2After(normdist[itrack1]);       
3953         pvertex->SetNAfter(0);
3954         pvertex->SetNBefore(0);
3955       }else{
3956         pvertex->SetChi2Before(minchi2before0);
3957         pvertex->SetChi2After(minchi2before1);
3958          if (pvertex->GetAnglep()[2]>0.1 || ( pvertex->GetRr()<10.5 && pvertex->GetAnglep()[2]>0.05) || pvertex->GetRr()<3){
3959            pb0    =  TMath::Exp(-TMath::Min(minchi2before0,Float_t(16))/12.);
3960            pb1    =  TMath::Exp(-TMath::Min(minchi2before1,Float_t(16))/12.);
3961          }
3962          pvertex->SetNAfter(maxLayer);
3963          pvertex->SetNBefore(maxLayer);      
3964       }
3965       if (pvertex->GetRr()<90){
3966         pa0  *= TMath::Min(track0->fESDtrack->GetTPCdensity(0,60),Float_t(1.));
3967         pa1  *= TMath::Min(track1->fESDtrack->GetTPCdensity(0,60),Float_t(1.));
3968       }
3969       if (pvertex->GetRr()<20){
3970         pa0  *= (0.2+TMath::Exp(-TMath::Min(minchi2after0,Float_t(16))/8.))/1.2;
3971         pa1  *= (0.2+TMath::Exp(-TMath::Min(minchi2after1,Float_t(16))/8.))/1.2;
3972       }
3973       //
3974       pvertex->SetCausality(pb0,pb1,pa0,pa1);
3975       //
3976       //  Likelihood calculations  - apply cuts
3977       //         
3978       Bool_t v0OK = kTRUE;
3979       Float_t p12 = pvertex->GetParamP()->GetParameter()[4]*pvertex->GetParamP()->GetParameter()[4];
3980       p12        += pvertex->GetParamM()->GetParameter()[4]*pvertex->GetParamM()->GetParameter()[4];
3981       p12         = TMath::Sqrt(p12);                             // "mean" momenta
3982       Float_t    sigmap0   = 0.0001+0.001/(0.1+pvertex->GetRr()); 
3983       Float_t    sigmap    = 0.5*sigmap0*(0.6+0.4*p12);           // "resolution: of point angle - as a function of radius and momenta
3984
3985       Float_t causalityA  = (1.0-pvertex->GetCausalityP()[0])*(1.0-pvertex->GetCausalityP()[1]);
3986       Float_t causalityB  = TMath::Sqrt(TMath::Min(pvertex->GetCausalityP()[2],Float_t(0.7))*
3987                                         TMath::Min(pvertex->GetCausalityP()[3],Float_t(0.7)));
3988       //
3989       Float_t likelihood0 = (TMath::Exp(-pvertex->GetDistNorm())+0.1) *(pvertex->GetDist2()<0.5)*(pvertex->GetDistNorm()<5);
3990
3991       Float_t likelihood1 = TMath::Exp(-(1.0001-pvertex->GetPointAngle())/sigmap)+
3992         0.4*TMath::Exp(-(1.0001-pvertex->GetPointAngle())/(4.*sigmap))+
3993         0.4*TMath::Exp(-(1.0001-pvertex->GetPointAngle())/(8.*sigmap))+
3994         0.1*TMath::Exp(-(1.0001-pvertex->GetPointAngle())/0.01);
3995       //
3996       if (causalityA<kCausality0Cut)                                          v0OK = kFALSE;
3997       if (TMath::Sqrt(likelihood0*likelihood1)<kLikelihood01Cut)              v0OK = kFALSE;
3998       if (likelihood1<kLikelihood1Cut)                                        v0OK = kFALSE;
3999       if (TMath::Power(likelihood0*likelihood1*causalityB,0.33)<kCombinedCut) v0OK = kFALSE;
4000       
4001       //
4002       //
4003       if (kFALSE){      
4004         Bool_t gold = TMath::Abs(TMath::Abs(track0->GetLabel())-TMath::Abs(track1->GetLabel()))==1;
4005         cstream<<"It0"<<
4006           "Tr0.="<<track0<<                       //best without constrain
4007           "Tr1.="<<track1<<                       //best without constrain  
4008           "Tr0B.="<<track0b<<                     //best without constrain  after vertex
4009           "Tr1B.="<<track1b<<                     //best without constrain  after vertex 
4010           "Tr0C.="<<htrackc0<<                    //best with constrain     if exist
4011           "Tr1C.="<<htrackc1<<                    //best with constrain     if exist
4012           "Tr0L.="<<track0l<<                     //longest best           
4013           "Tr1L.="<<track1l<<                     //longest best
4014           "Esd0.="<<track0->fESDtrack<<           // esd track0 params
4015           "Esd1.="<<track1->fESDtrack<<           // esd track1 params
4016           "V0.="<<pvertex<<                       //vertex properties
4017           "V0b.="<<&vertex2<<                       //vertex properties at "best" track
4018           "ND0="<<normdist[itrack0]<<             //normalize distance for track0
4019           "ND1="<<normdist[itrack1]<<             //normalize distance for track1
4020           "Gold="<<gold<<                         //
4021           //      "RejectBase="<<rejectBase<<             //rejection in First itteration
4022           "OK="<<v0OK<<
4023           "rmin="<<rmin<<
4024           "sigmad="<<sigmad<<
4025           "\n";
4026       }      
4027       //if (rejectBase) continue;
4028       //
4029       pvertex->SetStatus(0);
4030       //      if (rejectBase) {
4031       //        pvertex->SetStatus(-100);
4032       //}
4033       if (pvertex->GetPointAngle()>kMinPointAngle2) {
4034           pvertex->SetESDindexes(track0->fESDtrack->GetID(),track1->fESDtrack->GetID());
4035         if (v0OK){
4036           //      AliV0vertex vertexjuri(*track0,*track1);
4037           //      vertexjuri.SetESDindexes(track0->fESDtrack->GetID(),track1->fESDtrack->GetID());
4038           //      event->AddV0(&vertexjuri);
4039           pvertex->SetStatus(100);
4040         }
4041         event->AddV0MI(pvertex);
4042       }
4043     }
4044   }
4045   //
4046   //
4047   // delete temporary arrays
4048   //  
4049   delete[] minPointAngle;
4050   delete[] maxr;
4051   delete[] minr;
4052   delete[] norm;
4053   delete[] normdist;
4054   delete[] normdist1;
4055   delete[] normdist0;
4056   delete[] dist;
4057   delete[] itsmap;
4058   delete[] helixes;
4059   delete   pvertex;
4060 }
4061
4062
4063 void AliITStrackerMI::RefitV02(AliESD *event)
4064 {
4065   //
4066   //try to refit  V0s in the third path of the reconstruction
4067   //
4068   TTreeSRedirector &cstream = *fDebugStreamer;
4069   //
4070   Int_t  nv0s = event->GetNumberOfV0MIs();
4071   Float_t primvertex[3]={GetX(),GetY(),GetZ()};
4072   AliESDV0MI v0temp;
4073   for (Int_t iv0 = 0; iv0<nv0s;iv0++){
4074     AliESDV0MI * v0mi = event->GetV0MI(iv0);
4075     if (!v0mi) continue;
4076     Int_t     itrack0   = v0mi->GetIndex(0);
4077     Int_t     itrack1   = v0mi->GetIndex(1);
4078     AliESDtrack *esd0   = event->GetTrack(itrack0);
4079     AliESDtrack *esd1   = event->GetTrack(itrack1);
4080     if (!esd0||!esd1) continue;
4081     AliITStrackMI tpc0(*esd0);  
4082     AliITStrackMI tpc1(*esd1);
4083     Double_t alpha =TMath::ATan2(v0mi->GetXr(1),v0mi->GetXr(0));
4084     if (v0mi->GetRr()>85){
4085       if (tpc0.Propagate(alpha,v0mi->GetRr())&&tpc1.Propagate(alpha,v0mi->GetRr())){
4086         v0temp.SetM(tpc0);
4087         v0temp.SetP(tpc1);
4088         v0temp.Update(primvertex);
4089         if (kFALSE) cstream<<"Refit"<<
4090           "V0.="<<v0mi<<
4091           "V0refit.="<<&v0temp<<
4092           "Tr0.="<<&tpc0<<
4093           "Tr1.="<<&tpc1<<
4094           "\n";
4095         if (v0temp.GetDist2()<v0mi->GetDist2() || v0temp.GetPointAngle()>v0mi->GetPointAngle()){
4096           v0mi->SetM(tpc0);
4097           v0mi->SetP(tpc1);
4098           v0mi->Update(primvertex);
4099         }
4100       }
4101       continue;
4102     }
4103     if (v0mi->GetRr()>35){
4104       CorrectForDeadZoneMaterial(&tpc0);
4105       CorrectForDeadZoneMaterial(&tpc1);
4106       if (tpc0.Propagate(alpha,v0mi->GetRr())&&tpc1.Propagate(alpha,v0mi->GetRr())){
4107         v0temp.SetM(tpc0);
4108         v0temp.SetP(tpc1);
4109         v0temp.Update(primvertex);
4110         if (kFALSE) cstream<<"Refit"<<
4111           "V0.="<<v0mi<<
4112           "V0refit.="<<&v0temp<<
4113           "Tr0.="<<&tpc0<<
4114           "Tr1.="<<&tpc1<<
4115           "\n";
4116         if (v0temp.GetDist2()<v0mi->GetDist2() || v0temp.GetPointAngle()>v0mi->GetPointAngle()){
4117           v0mi->SetM(tpc0);
4118           v0mi->SetP(tpc1);
4119           v0mi->Update(primvertex);
4120         }       
4121       }
4122       continue;
4123     }
4124     CorrectForDeadZoneMaterial(&tpc0);
4125     CorrectForDeadZoneMaterial(&tpc1);    
4126     //    if (tpc0.Propagate(alpha,v0mi->GetRr())&&tpc1.Propagate(alpha,v0mi->GetRr())){
4127     if (RefitAt(v0mi->GetRr(),&tpc0, v0mi->GetClusters(0)) && RefitAt(v0mi->GetRr(),&tpc1, v0mi->GetClusters(1))){
4128       v0temp.SetM(tpc0);
4129       v0temp.SetP(tpc1);
4130       v0temp.Update(primvertex);
4131       if (kFALSE) cstream<<"Refit"<<
4132         "V0.="<<v0mi<<
4133         "V0refit.="<<&v0temp<<
4134         "Tr0.="<<&tpc0<<
4135         "Tr1.="<<&tpc1<<
4136         "\n";
4137       if (v0temp.GetDist2()<v0mi->GetDist2() || v0temp.GetPointAngle()>v0mi->GetPointAngle()){
4138         v0mi->SetM(tpc0);
4139         v0mi->SetP(tpc1);
4140         v0mi->Update(primvertex);
4141       } 
4142     }    
4143   }
4144 }
4145
4146
4147
4148
4149
4150
4151