Automatic treatment of the magnetic field value
[u/mrichter/AliRoot.git] / ITS / AliITSTrackerV1.cxx
1 //The purpose of this class is to permorm the ITS tracking.
2 //The constructor has the task to inizialize some private members.
3 //The method DoTracking is written to be called by a macro. It gets the event number, the minimum and maximum
4 //order number of TPC tracks that are to be tracked trough the ITS, and the file where the recpoints are
5 //registered.
6 //The method Recursivetracking is a recursive function that performs the tracking trough the ITS
7 //The method Intersection found the layer, ladder and detector whre the intersection take place and caluclate
8 //the cohordinates of this intersection. It returns an integer that is 0 if the intersection has been found
9 //successfully.
10 //The two mwthods Kalmanfilter and kalmanfiltervert operate the kalmanfilter without and with the vertex
11 //imposition respectively.
12 //The authors  thank Mariana Bondila to have help them to resolve some problems.  July-2000                                                      
13
14
15 #include <fstream.h>
16 #include <TMath.h>
17 #include <TBranch.h>
18 #include <TVector.h>
19 #include <TFile.h>
20 #include <TTree.h>
21 #include "TParticle.h"
22 #include "AliRun.h"
23 #include "AliITS.h"
24 #include "AliITSsegmentationSSD.h"
25 #include "AliITSgeomSPD.h"
26 #include "AliITSgeomSDD.h"
27 #include "AliITSgeomSSD.h"
28 #include "AliITSgeom.h"
29 #include "AliITSRecPoint.h"
30 #include "stdlib.h"
31 #include "AliKalmanTrack.h" 
32 #include "AliMagF.h"
33 #include "AliITSTrackV1.h"
34 #include "AliITSIOTrack.h"
35 #include "AliITSRad.h"   
36 #include "../TPC/AliTPCtracker.h"
37 #include "AliITSTrackerV1.h"
38
39
40 ClassImp(AliITSTrackerV1)
41
42
43 //________________________________________________________________
44
45 AliITSTrackerV1::AliITSTrackerV1(AliITS* IITTSS, Bool_t flag) {
46 //Origin   A. Badala' and G.S. Pappalardo:  e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
47 // Class constructor. It does some initializations.
48
49   fITS = IITTSS;
50   fPtref=0.;
51   fflagvert=flag;
52   Int_t imax=200,jmax=450;
53   frl = new AliITSRad(imax,jmax);
54
55 ///////////////////////////////////////  gets information on geometry ///////////////////////////////////  
56
57   AliITSgeom *g1 = ((AliITS*)gAlice->GetDetector("ITS"))->GetITSgeom(); 
58   
59   Int_t ll=1, dd=1;
60   TVector det(9);
61   
62   //cout<<" nlad ed ndet \n";
63   Int_t ia;                              // fuori
64   for(ia=0; ia<6; ia++) {
65     fNlad[ia]=g1->GetNladders(ia+1);
66     fNdet[ia]=g1->GetNdetectors(ia+1);
67          //cout<<fNlad[i]<<" "<<fNdet[i]<<"\n"; 
68   }
69   //getchar();
70
71   //cout<<" raggio medio = ";
72   Int_t ib;                                  // fuori
73   for(ib=0; ib<6; ib++) {  
74     g1->GetCenterThetaPhi(ib+1,ll,dd,det);
75     fAvrad[ib]=TMath::Sqrt(det(0)*det(0)+det(1)*det(1));
76          //cout<<fAvrad[ib]<<" ";
77   }
78   //cout<<"\n"; getchar();
79   
80   fDetx[0] = ((AliITSgeomSPD*)(g1->GetShape(1, ll, dd)))->GetDx();
81   fDetz[0] = ((AliITSgeomSPD*)(g1->GetShape(1, ll, dd)))->GetDz();
82   
83   fDetx[1] = ((AliITSgeomSPD*)(g1->GetShape(2, ll, dd)))->GetDx();
84   fDetz[1] = ((AliITSgeomSPD*)(g1->GetShape(2, ll, dd)))->GetDz();
85   
86   fDetx[2] = ((AliITSgeomSDD*)(g1->GetShape(3, ll, dd)))->GetDx();
87   fDetz[2] = ((AliITSgeomSDD*)(g1->GetShape(3, ll, dd)))->GetDz();
88   
89   fDetx[3] = ((AliITSgeomSDD*)(g1->GetShape(4, ll, dd)))->GetDx();
90   fDetz[3] = ((AliITSgeomSDD*)(g1->GetShape(4, ll, dd)))->GetDz();
91   
92   fDetx[4] = ((AliITSgeomSSD*)(g1->GetShape(5, ll, dd)))->GetDx();
93   fDetz[4] = ((AliITSgeomSSD*)(g1->GetShape(5, ll, dd)))->GetDz();
94   
95   fDetx[5] = ((AliITSgeomSSD*)(g1->GetShape(6, ll, dd)))->GetDx();
96   fDetz[5] = ((AliITSgeomSSD*)(g1->GetShape(6, ll, dd)))->GetDz();
97   
98   //cout<<"    Detx     Detz\n";
99   //for(Int_t la=0; la<6; la++) cout<<"    "<<fDetx[la]<<"     "<<fDetz[la]<<"\n";
100   //getchar();
101 //////////////////////////////////////////////////////////////////////////////////////////////////////////
102
103 //////////////////////////////////////// gets magnetic field factor ////////////////////////////////
104
105   AliMagF * fieldPointer = gAlice->Field();
106   fFieldFactor = (Double_t)fieldPointer->Factor();
107   //cout<< " field factor = "<<fFieldFactor<<"\n"; getchar();
108
109 /////////////////////////////////////////////////////////////////////////////////////////////////////////
110   
111 }
112
113 AliITSTrackerV1::AliITSTrackerV1(const AliITSTrackerV1 &cobj) {
114 //Origin  A. Badala' and G.S. Pappalardo:  e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
115 // copy constructor
116     
117     *fITS = *cobj.fITS;
118          *fresult = *cobj.fresult;
119     fPtref = cobj.fPtref;
120          //frecPoints = fITS->RecPoints();
121          //*frecPoints = *cobj.frecPoints;
122          **fvettid = **cobj.fvettid;
123          fflagvert = cobj.fflagvert;
124          Int_t imax=200,jmax=450;
125          frl = new AliITSRad(imax,jmax);         
126          *frl = *cobj.frl;
127          fFieldFactor = cobj.fFieldFactor;
128          Int_t i;
129          for(i=0; i<6; i++) {
130            fNlad[i] = cobj.fNlad[i];
131       fNdet[i] = cobj.fNdet[i]; 
132            fAvrad[i] = cobj.fAvrad[i];
133            fDetx[i] = cobj.fDetx[i];
134            fDetz[i] = cobj.fDetz[i];
135         }
136
137 }
138
139 AliITSTrackerV1 &AliITSTrackerV1::operator=(AliITSTrackerV1 obj) {
140 //Origin  A. Badala' and G.S. Pappalardo:  e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it  
141 // assignement operator
142
143     *fITS = *obj.fITS;
144          *fresult = *obj.fresult;
145     fPtref = obj.fPtref;
146          **fvettid = **obj.fvettid;
147          fflagvert = obj.fflagvert;
148          Int_t imax=200,jmax=450;
149          frl = new AliITSRad(imax,jmax);         
150          *frl = *obj.frl;
151          fFieldFactor = obj.fFieldFactor;
152          Int_t i;
153          for(i=0; i<6; i++) {
154            fNlad[i] = obj.fNlad[i];
155       fNdet[i] = obj.fNdet[i]; 
156            fAvrad[i] = obj.fAvrad[i];
157            fDetx[i] = obj.fDetx[i];
158            fDetz[i] = obj.fDetz[i];
159         }
160
161   return *this;
162   
163 }
164
165
166 //________________________________________________________________
167
168
169 void AliITSTrackerV1::DoTracking(Int_t evNumber, Int_t minTr, Int_t maxTr, TFile *file) {
170 //Origin   A. Badala' and G.S. Pappalardo:  e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
171 //The method needs the event number, the minimum and maximum order number of TPC tracks that 
172 //are to be tracked trough the ITS, and the file where the recpoints are registered.
173 //The method can be called by a macro. It preforms the tracking for all good TPC tracks
174
175
176   printf("begin DoTracking - file %p\n",file);
177       
178   struct GoodTrack {
179     Int_t lab,code;
180     Float_t px,py,pz,x,y,z,pxg,pyg,pzg,ptg;
181     Bool_t flag;
182   };
183   
184
185   gAlice->GetEvent(0);
186  
187   AliKalmanTrack *kkprov;
188   kkprov->SetConvConst(100/0.299792458/0.2/fFieldFactor);  
189
190
191   TFile *cf=TFile::Open("AliTPCclusters.root");  
192   AliTPCParam *digp= (AliTPCParam*)cf->Get("75x40_100x60");
193   if (!digp) { cerr<<"TPC parameters have not been found !\n"; getchar();}
194   
195    AliTPCtracker *tracker = new AliTPCtracker(digp);  
196
197  // Load clusters
198    tracker->LoadInnerSectors();
199    tracker->LoadOuterSectors();
200        
201   
202   GoodTrack gt[15000];
203   Int_t ngood=0;
204   ifstream in("itsgood_tracks");
205
206   cerr<<"Reading itsgood tracks...\n";
207   while (in>>gt[ngood].lab>>gt[ngood].code
208           >>gt[ngood].px >>gt[ngood].py>>gt[ngood].pz
209           >>gt[ngood].x  >>gt[ngood].y >>gt[ngood].z
210           >>gt[ngood].pxg  >>gt[ngood].pyg >>gt[ngood].pzg
211           >>gt[ngood].ptg >>gt[ngood].flag) {
212     ngood++;
213     cerr<<ngood<<'\r';
214     if (ngood==15000) {
215       cerr<<"Too many good tracks !\n";
216       break;
217     }
218   }
219   if (!in.eof()) cerr<<"Read error (itsgood_tracks) !\n";
220   
221   
222 // Load tracks
223   TFile *tf=TFile::Open("AliTPCtracks.root"); 
224   if (!tf->IsOpen()) {cerr<<"Can't open AliTPCtracks.root !\n"; return ;}
225   TObjArray tracks(200000);
226    TTree *tracktree=(TTree*)tf->Get("TPCf"); 
227    if (!tracktree) {cerr<<"Can't get a tree with TPC tracks !\n";}   
228   TBranch *tbranch=tracktree->GetBranch("tracks");
229   Int_t nentr=(Int_t)tracktree->GetEntries();
230   Int_t kk;
231
232    AliTPCtrack *ioTrackTPC=0;    
233   for (kk=0; kk<nentr; kk++) {
234     ioTrackTPC=new AliTPCtrack; 
235     tbranch->SetAddress(&ioTrackTPC);
236     tracktree->GetEvent(kk);    
237     tracker->CookLabel(ioTrackTPC,0.1);       
238     tracks.AddLast(ioTrackTPC);         
239   }  
240    delete tracker;      
241   tf->Close();
242
243
244   Int_t nt = tracks.GetEntriesFast();
245   cerr<<"Number of found tracks "<<nt<<endl;
246   
247   TVector dataOut(9);
248   Int_t kkk=0;
249   
250   Double_t ptg=0.,pxg=0.,pyg=0.,pzg=0.;
251
252   //////////////////////////////  good tracks definition in TPC  ////////////////////////////////
253       
254   ofstream out1 ("AliITSTrag.out");
255   Int_t i;
256   for (i=0; i<ngood; i++) out1 << gt[i].ptg << "\n";
257   out1.close();
258
259
260   TVector vec(5);
261   TTree *tr=gAlice->TreeR();
262   Int_t nent=(Int_t)tr->GetEntries();  
263   //TClonesArray  *recPoints = RecPoints();      // nuova eliminata
264   //TClonesArray  *recPoints = ITS->RecPoints();  // nuova
265   //TObjArray  *
266   frecPoints = fITS->RecPoints();  // nuovissima   tolta
267   
268   Int_t numbpoints;
269   Int_t totalpoints=0;
270   Int_t *np = new Int_t[nent];
271   //Int_t **
272   fvettid = new Int_t* [nent];
273   Int_t mod;
274   
275   for (mod=0; mod<nent; mod++) {
276     fvettid[mod]=0;
277     fITS->ResetRecPoints();  // nuova
278     //gAlice->TreeR()->GetEvent(mod+1); //first entry in TreeR is empty
279     gAlice->TreeR()->GetEvent(mod); //first entry in TreeR is empty
280     numbpoints = frecPoints->GetEntries();
281     totalpoints+=numbpoints;
282     np[mod] = numbpoints;
283   //cout<<" mod = "<<mod<<"   numbpoints = "<<numbpoints<<"\n"; getchar();
284     fvettid[mod] = new Int_t[numbpoints];
285     Int_t ii;
286     for (ii=0;ii<numbpoints; ii++) *(fvettid[mod]+ii)=0;
287   }
288
289   AliTPCtrack *track=0;  // sono qui
290
291      
292   if(minTr < 0) {minTr = 0; maxTr = nt-1;}   
293
294 /*
295   ///////////////////////////////// Definition of vertex end its error ////////////////////////////
296   ////////////////////////// In the future it will be given by a method ///////////////////////////
297   Double_t Vx=0.;
298   Double_t Vy=0.;
299   Double_t Vz=0.;
300   
301   Float_t sigmavx=0.0050;      // 50  microns
302   Float_t sigmavy=0.0050;      // 50  microns
303   Float_t sigmavz=0.010;       // 100 microns
304
305   //Vx+=gRandom->Gaus(0,sigmavx);  Vy+=gRandom->Gaus(0,sigmavy);  Vz+=gRandom->Gaus(0,sigmavz);
306   TVector vertex(3), ervertex(3)
307   vertex(0)=Vx; vertex(1)=Vy; vertex(2)=Vz;
308   ervertex(0)=sigmavx;  ervertex(1)=sigmavy;  ervertex(2)=sigmavz;
309   /////////////////////////////////////////////////////////////////////////////////////////////////
310 */      
311  
312
313   TTree tracktree1("TreeT","Tree with ITS tracks");
314   AliITSIOTrack *ioTrack=0;
315   tracktree1.Branch("ITStracks","AliITSIOTrack",&ioTrack,32000,0);
316
317   ofstream out ("AliITSTra.out");
318
319   
320   Int_t j;       
321   for (j=minTr; j<=maxTr; j++) {     
322     track=(AliTPCtrack*)tracks.UncheckedAt(j);
323     Int_t flaglab=0;
324     if (!track) continue;
325     ////// elimination of not good tracks ////////////   
326     Int_t ilab=TMath::Abs(track->GetLabel());
327     Int_t iii;
328     for (iii=0;iii<ngood;iii++) {
329          //cout<<" ilab, gt[iii].lab = "<<ilab<<" "<<gt[iii].lab<<"\n"; getchar();
330       if (ilab==gt[iii].lab) { 
331         flaglab=1;
332         ptg=gt[iii].ptg; 
333         pxg=gt[iii].pxg;
334         pyg=gt[iii].pyg;
335         pzg=gt[iii].pzg;        
336         break;
337       }
338     }
339          //cout<<" j flaglab =  " <<j<<" "<<flaglab<<"\n";  getchar();
340     if (!flaglab) continue;  
341          //cout<<" j =  " <<j<<"\n";  getchar();
342
343          //////   propagation to the end of TPC //////////////
344     Double_t xk=77.415;
345     track->PropagateTo(xk, 28.94, 1.204e-3);     //Ne    
346          xk -=0.01;
347     track->PropagateTo(xk, 44.77, 1.71);         //Tedlar
348          xk -=0.04;
349     track->PropagateTo(xk, 44.86, 1.45);         //kevlar
350          xk -=2.0;
351     track->PropagateTo(xk, 41.28, 0.029);        //Nomex         
352     xk-=16;
353     track->PropagateTo(xk,36.2,1.98e-3); //C02
354          xk -=0.01;
355     track->PropagateTo(xk, 24.01, 2.7);  //Al    
356          xk -=0.01;
357     track->PropagateTo(xk, 44.77, 1.71);         //Tedlar
358          xk -=0.04;
359     track->PropagateTo(xk, 44.86, 1.45);         //kevlar
360          xk -=0.5;
361     track->PropagateTo(xk, 41.28, 0.029);        //Nomex                                                 
362                      
363     ///////////////////////////////////////////////////////////////                      
364  
365
366          AliITSTrackV1 trackITS(*track);
367          
368          if(fresult) delete fresult;
369          fresult = new AliITSTrackV1(trackITS);  
370
371          AliITSTrackV1 primaryTrack(trackITS);
372
373          
374          TVector vgeant(3);
375          vgeant=(*fresult).GetVertex(); 
376                           
377   // Definition of dv and zv for vertex constraint      
378      Double_t sigmaDv=0.0050;  Double_t sigmaZv=0.010;  
379     //Double_t sigmaDv=0.0015;  Double_t sigmaZv=0.0015;                                  
380         Double_t uniform= gRandom->Uniform();
381         Double_t signdv;
382         if(uniform<=0.5) signdv=-1.;
383            else
384                  signdv=1.;
385          
386         Double_t vr=TMath::Sqrt(vgeant(0)*vgeant(0)+ vgeant(1)*vgeant(1));
387           Double_t dv=gRandom->Gaus(signdv*vr,(Float_t)sigmaDv); 
388     Double_t zv=gRandom->Gaus(vgeant(2),(Float_t)sigmaZv);
389                                 
390   //cout<<" Dv e Zv = "<<dv<<" "<<zv<<"\n";                             
391     trackITS.SetDv(dv);  trackITS.SetZv(zv);
392     trackITS.SetsigmaDv(sigmaDv); trackITS.SetsigmaZv(sigmaZv); 
393     (*fresult).SetDv(dv);  (*fresult).SetZv(zv);
394     (*fresult).SetsigmaDv(sigmaDv); (*fresult).SetsigmaZv(sigmaZv);
395     primaryTrack.SetDv(dv);  primaryTrack.SetZv(zv);
396     primaryTrack.SetsigmaDv(sigmaDv); primaryTrack.SetsigmaZv(sigmaZv);                                                                          
397                 
398     primaryTrack.PrimaryTrack(frl);
399     TVector  d2=primaryTrack.Getd2();
400     TVector  tgl2=primaryTrack.Gettgl2();
401     TVector  dtgl=primaryTrack.Getdtgl();
402     trackITS.Setd2(d2); trackITS.Settgl2(tgl2);  trackITS.Setdtgl(dtgl); 
403     (*fresult).Setd2(d2); (*fresult).Settgl2(tgl2);  (*fresult).Setdtgl(dtgl);     
404          /*                      
405     trackITS.SetVertex(vertex); trackITS.SetErrorVertex(ervertex);
406     (*result).SetVertex(vertex);   (*result).SetErrorVertex(ervertex);   
407     */
408                           
409                                                                                     
410   TList *list= new TList();   
411
412   list->AddLast(&trackITS);
413   
414   fPtref=TMath::Abs( (trackITS).GetPt() );
415   cout << "\n Pt = " << fPtref <<"\n";  //stampa
416
417   RecursiveTracking(list);  // nuova ITS 
418   list->Delete();
419   delete list;
420
421   Int_t itot=-1;
422   TVector vecTotLabRef(18);
423   Int_t lay, k;
424   for(lay=5; lay>=0; lay--) {
425     TVector vecLabRef(3); 
426     vecLabRef=(*fresult).GetLabTrack(lay);
427     Float_t clustZ=(*fresult).GetZclusterTrack( lay);   
428     for(k=0; k<3; k++){  
429                 Int_t lpp=(Int_t)vecLabRef(k);
430                 if(lpp>=0) {
431                   TParticle *p=(TParticle*) gAlice->Particle(lpp);
432                   Int_t pcode=p->GetPdgCode();
433                   if(pcode==11) vecLabRef(k)=p->GetFirstMother();
434                 }    
435     itot++; vecTotLabRef(itot)=vecLabRef(k);
436     if(vecLabRef(k)==0. && clustZ == 0.) vecTotLabRef(itot) =-3.; }  
437   }
438   Long_t labref;
439   Int_t freq;  
440   (*fresult).Search(vecTotLabRef, labref, freq);
441     
442
443   //if(freq < 6) labref=-labref;        // cinque - sei
444   if(freq < 5) labref=-labref;        // cinque - sei   
445   (*fresult).SetLabel(labref);
446
447     // cout<<" progressive track number = "<<j<<"\r";
448    // cout<<j<<"\r";
449     Int_t numOfCluster=(*fresult).GetNumClust();  
450     cout<<" progressive track number = "<<j<<"\n";    // stampa
451     Long_t labITS=(*fresult).GetLabel();
452     cout << " ITS track label = " << labITS << "\n";    // stampa           
453     int lab=track->GetLabel();              
454     cout << " TPC track label = " << lab <<"\n";      // stampa
455          
456              
457 //propagation to vertex
458         
459     Double_t rbeam=3.;
460      
461     (*fresult).Propagation(rbeam);
462          
463          Double_t c00,c10,c11,c20,c21,c22,c30,c31,c32,c33,c40,c41,c42,c43,c44;
464          (*fresult).GetCElements(c00,c10,c11,c20,c21,c22,c30,c31,c32,c33,c40,c41,c42,c43,c44);
465                  
466     Double_t pt=TMath::Abs((*fresult).GetPt());
467     Double_t dr=(*fresult).GetD();
468     Double_t z=(*fresult).GetZ();
469     Double_t tgl=(*fresult).GetTgl();
470     Double_t c=(*fresult).GetC();
471     Double_t cy=c/2.;
472     Double_t dz=z-(tgl/cy)*TMath::ASin((*fresult).Arga(rbeam));
473          dz-=vgeant(2);
474           
475          // cout<<" dr e dz alla fine = "<<dr<<" "<<dz<<"\n"; getchar();
476     Double_t phi=(*fresult).Getphi();
477     Double_t phivertex = phi - TMath::ASin((*fresult).ArgA(rbeam));
478     Double_t duepi=2.*TMath::Pi();       
479     if(phivertex>duepi) phivertex-=duepi;
480     if(phivertex<0.) phivertex+=duepi;
481     Double_t dtot=TMath::Sqrt(dr*dr+dz*dz);
482          
483 //////////////////////////////////////////////////////////////////////////////////////////      
484   
485     Int_t idmodule,idpoint;
486          if(numOfCluster >=5)  {            // cinque - sei
487          //if(numOfCluster ==6)  {            // cinque - sei 
488
489
490       AliITSIOTrack outTrack;
491
492       ioTrack=&outTrack;
493
494       ioTrack->SetStatePhi(phi);
495       ioTrack->SetStateZ(z);
496       ioTrack->SetStateD(dr);
497       ioTrack->SetStateTgl(tgl);
498       ioTrack->SetStateC(c);
499                 Double_t radius=(*fresult).Getrtrack();
500                 ioTrack->SetRadius(radius);
501                 Int_t charge;
502                 if(c>0.) charge=-1;  else charge=1;
503                 ioTrack->SetCharge(charge);
504                 
505
506
507       ioTrack->SetCovMatrix(c00,c10,c11,c20,c21,c22,c30,c31,c32,c33,c40,c41,c42,c43,c44);  
508
509       Double_t px=pt*TMath::Cos(phivertex);
510       Double_t py=pt*TMath::Sin(phivertex);
511       Double_t pz=pt*tgl;
512                 
513       Double_t xtrack=dr*TMath::Sin(phivertex);
514       Double_t ytrack=dr*TMath::Cos(phivertex);
515       Double_t ztrack=dz+vgeant(2);
516
517
518       ioTrack->SetPx(px);
519       ioTrack->SetPy(py);
520       ioTrack->SetPz(pz);
521       ioTrack->SetX(xtrack);
522       ioTrack->SetY(ytrack);
523       ioTrack->SetZ(ztrack);
524       ioTrack->SetLabel(labITS);
525                 
526       Int_t il;         
527                 for(il=0;il<6; il++){
528                   ioTrack->SetIdPoint(il,(*fresult).GetIdPoint(il));
529                   ioTrack->SetIdModule(il,(*fresult).GetIdModule(il));          
530                 }
531       tracktree1.Fill();
532
533    //cout<<" labITS = "<<labITS<<"\n";
534         //cout<<" phi z dr tgl c = "<<phi<<" "<<z<<" "<<dr<<" "<<tgl<<" "<<c<<"\n";  getchar();    
535
536      dataOut(kkk) = ptg; kkk++; dataOut(kkk)=labITS; kkk++; dataOut(kkk)=lab; kkk++;            
537
538       for (il=0;il<6;il++) {
539         idpoint=(*fresult).GetIdPoint(il);
540         idmodule=(*fresult).GetIdModule(il);
541         *(fvettid[idmodule]+idpoint)=1; 
542         ioTrack->SetIdPoint(il,idpoint);
543         ioTrack->SetIdModule(il,idmodule);
544       }
545       
546     //  cout<<"  +++++++++++++  pt e ptg = "<<pt<<" "<<ptg<<"  ++++++++++\n";
547
548        ///////////////////////////////
549       Double_t difpt= (pt-ptg)/ptg*100.;                                        
550       dataOut(kkk)=difpt; kkk++;                                             
551       Double_t lambdag=TMath::ATan(pzg/ptg);
552       Double_t   lam=TMath::ATan(tgl);      
553       Double_t diflam = (lam - lambdag)*1000.;
554       dataOut(kkk) = diflam; kkk++;                                         
555       Double_t phig=TMath::ATan2(pyg,pxg);  if(phig<0) phig=2.*TMath::Pi()+phig;       
556       Double_t phi=phivertex;
557         
558       Double_t difphi = (phi - phig)*1000.;
559       dataOut(kkk)=difphi; kkk++;
560       dataOut(kkk)=dtot*1.e4; kkk++;
561       dataOut(kkk)=dr*1.e4; kkk++;
562       dataOut(kkk)=dz*1.e4; kkk++; 
563       Int_t r;
564       for (r=0; r<9; r++) { out<<dataOut(r)<<" ";}
565       out<<"\n";
566       kkk=0;  
567                 
568             
569     } // end if on numOfCluster
570   //gObjectTable->Print();    // stampa memoria     
571   }  //  end for (int j=minTr; j<=maxTr; j++)
572   
573   out.close();  
574   
575  
576   static Bool_t first=kTRUE;
577   static TFile *tfile;
578
579         if(first) {
580             tfile=new TFile("itstracks.root","RECREATE");
581             //cout<<"I have opened itstracks.root file "<<endl;
582         }           
583         first=kFALSE;
584         tfile->cd();
585         tfile->ls();
586
587    char hname[30];
588    sprintf(hname,"TreeT%d",evNumber);
589
590   tracktree1.Write(hname);
591
592
593   
594             TTree *fAli=gAlice->TreeK();
595             TFile *fileAli=0;
596             
597             if (fAli) fileAli =fAli->GetCurrentFile();
598             fileAli->cd();
599      
600   ////////////////////////////////////////////////////////////////////////////////////////////////
601
602   printf("delete vectors\n");
603   if(np) delete [] np;
604   if(fvettid) delete [] fvettid;
605   if(fresult) delete fresult;
606   
607 }
608
609
610
611 void AliITSTrackerV1::RecursiveTracking(TList *trackITSlist) {
612                                                                                  
613 ///////////////////////   This function perform the recursive tracking in ITS detectors /////////////////////
614 ///////////////////////     reference is a pointer to the final best track    ///////////////////// 
615 //Origin  A. Badala' and G.S. Pappalardo:  e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
616 // The authors  thank   Mariana Bondila to have help them to resolve some problems.  July-2000                                                      
617
618   //Rlayer[0]=4.; Rlayer[1]=7.;  Rlayer[2]=14.9;  Rlayer[3]=23.8;  Rlayer[4]=39.1;  Rlayer[5]=43.6; //vecchio
619   
620   Int_t index;   
621   for(index =0; index<trackITSlist->GetSize(); index++) {
622     AliITSTrackV1 *trackITS = (AliITSTrackV1 *) trackITSlist->At(index);
623
624     if((*trackITS).GetLayer()==7) fresult->SetChi2(10.223e140);
625    // cout <<" Layer inizio = "<<(*trackITS).GetLayer()<<"\n";
626    //  cout<<"fvtrack =" <<"\n";
627    //  cout << (*trackITS)(0) << " "<<(*trackITS)(1)<<" "<<(*trackITS)(2)<<" "<<(*trackITS)(3)<<" "<<(*trackITS)(4)<<"\n";
628    //  cout<< " rtrack = "<<(*trackITS).Getrtrack()<<"\n";
629    //  cout<< " Pt = "<<(*trackITS).GetPt()<<"\n";
630    //  getchar();    
631     Double_t chi2Now, chi2Ref;
632     if((*trackITS).GetLayer()==1 ) {
633       chi2Now = trackITS->GetChi2();
634       Float_t numClustNow = trackITS->GetNumClust();
635       if(trackITS->GetNumClust()) chi2Now /= (Double_t )trackITS->GetNumClust();
636       chi2Ref = fresult->GetChi2();
637       Float_t numClustRef = fresult->GetNumClust();     
638       if(fresult->GetNumClust()) chi2Ref /= (Double_t )fresult->GetNumClust();
639       //cout<<" chi2Now and chi2Ref = "<<chi2Now<<" "<<chi2Ref<<"\n";
640                 if( numClustNow > numClustRef ) {*fresult = *trackITS;} 
641       if((numClustNow == numClustRef )&& (chi2Now < chi2Ref))  {*fresult = *trackITS;}
642       continue; 
643     }
644     Float_t numClustNow = trackITS->GetNumClust();
645     if(numClustNow) { 
646       chi2Now = trackITS->GetChi2();
647       chi2Now/=numClustNow;
648       //cout<<" chi2Now =  "<<chi2Now<<"\n"; 
649     /*
650     // if(Ptref > 0.6 && chi2Now > 20.) continue; 
651     if(Ptref > 0.6 && chi2Now > 30.) continue;    
652     if((Ptref <= 0.6 && Ptref>0.2)&& chi2Now > 15.) continue;        
653      // if(chi2Now>5.) continue; 
654       //if(chi2Now>15.) continue;     
655      // if(Ptref <= 0.2 && chi2Now > 10.) continue;  
656      if(Ptref <= 0.2 && chi2Now > 8.) continue; 
657      */
658      if(fPtref > 1.0 && chi2Now > 30.) continue; 
659      if((fPtref >= 0.6 && fPtref<=1.0) && chi2Now > 40.) continue;        
660      if((fPtref <= 0.6 && fPtref>0.2)&& chi2Now > 40.) continue;            
661      if(fPtref <= 0.2 && chi2Now > 8.) continue;                                                                         
662     }
663                  
664     Int_t layerInit = (*trackITS).GetLayer();
665     Int_t layernew = layerInit - 2;  // -1 for new layer, -1 for matrix index 
666                                           
667     //Int_t NLadder[]= {20, 40, 14, 22, 34, 38};   //vecchio
668     //Int_t NDetector[]= {4,  4,   6,  8, 23, 26}; //vecchio
669                                                 
670     TList listoftrack;           
671     Int_t ladp, ladm, detp,detm,ladinters,detinters;    
672     Int_t layerfin=layerInit-1;
673          Double_t rFin=fAvrad[layerfin-1];  
674     // cout<<"Prima di intersection \n";
675
676     Int_t  outinters=Intersection(*trackITS, rFin, layerfin, ladinters, detinters);
677                  
678    // cout<<" outinters = "<<outinters<<"\n";
679    //  cout<<" Layer ladder detector intersection ="<<layerfin<<" "<<ladinters<<" "<<detinters<<"\n";
680    //  cout << " phiinters zinters = "<<(*trackITS)(0) << " "<<(*trackITS)(1)<<"\n"; getchar();
681                          
682     if(outinters==-1) continue;
683          
684     Int_t flaghit=0;                    
685     if(outinters==0){   
686       TVector toucLad(9), toucDet(9);    
687       Int_t lycur=layerfin;                                            
688       ladp=ladinters+1;
689       ladm=ladinters-1;
690                 if(ladm <= 0) ladm=fNlad[layerfin-1];    
691                 if(ladp > fNlad[layerfin-1]) ladp=1;  
692       detp=detinters+1;
693       detm=detinters-1;
694       Int_t idetot=1;
695       toucLad(0)=ladinters; toucLad(1)=ladm; toucLad(2)=ladp;
696       toucLad(3)=ladinters; toucLad(4)=ladm; toucLad(5)=ladp;
697       toucLad(6)=ladinters; toucLad(7)=ladm; toucLad(8)=ladp;
698       toucDet(0)=detinters; toucDet(1)=detinters; toucDet(2)=detinters;
699                 if(detm > 0 && detp <= fNdet[layerfin-1]) {     
700         idetot=9;
701         toucDet(3)=detm; toucDet(4)=detm; toucDet(5)=detm;         
702         toucDet(6)=detp; toucDet(7)=detp; toucDet(8)=detp;
703       }
704          
705                 if(detm > 0 && detp > fNdet[layerfin-1]) {   
706         idetot=6;
707         toucDet(3)=detm; toucDet(4)=detm; toucDet(5)=detm;
708       }
709          
710                 if(detm <= 0 && detp <= fNdet[layerfin-1]) {   
711         idetot=6;
712         toucDet(3)=detp; toucDet(4)=detp; toucDet(5)=detp;
713       }
714       Int_t iriv;       
715       for (iriv=0; iriv<idetot; iriv++) {  //for on detectors
716         //AliITSgeom *g1 = aliITS->GetITSgeom();  //vvecchia
717                   AliITSgeom *g1 = fITS->GetITSgeom();  //nnuova
718         TVector ctf(9);
719         g1->GetCenterThetaPhi(layerInit-1,(Int_t)toucLad(iriv),(Int_t)toucDet(iriv),ctf);
720
721         // cout<<" layer, ladder, det, xo, yo, zo = "<<layerInit-1<<" "<<(Int_t)toucLad(iriv)<<
722         // " "<<(Int_t)toucDet(iriv)<<" "<<ctf(0)<<" "<<ctf(1)<<" "<<ctf(2)<< " "<<ctf(6)<<"\n"; getchar(); 
723
724         ////////////////////////////////////////////////////////////////////////////////////////////////
725
726         /*** Rec points sorted by module *****/
727         /**************************************/
728
729         Int_t index;
730         AliITSRecPoint *recp;
731         //AliITSgeom *geom = aliITS->GetITSgeom();  //vvecchia
732                   AliITSgeom *geom = fITS->GetITSgeom();  //nnuova
733         index = geom->GetModuleIndex(lycur,toucLad(iriv),toucDet(iriv));
734         Int_t lay,lad,det;
735         geom->GetModuleId(index,lay,lad,det);
736         //aliITS->ResetRecPoints();   //vvecchia
737                   fITS->ResetRecPoints();   //nnuova
738         //gAlice->TreeR()->GetEvent(index+1); //first entry in TreeR is empty
739         gAlice->TreeR()->GetEvent(index); //first entry in TreeR is empty
740
741         Int_t npoints=frecPoints->GetEntries();
742         Int_t *indlist=new Int_t[npoints+1];
743         Int_t counter=0;
744         Int_t ind;
745         for (ind=0; ind<=npoints; ind++) {
746           indlist[ind]=-1;
747                if (*(fvettid[index]+ind)==0) {
748               indlist[counter]=ind;
749                    counter++;
750               }
751         }
752
753         ind=-1;
754         
755         for(;;) { 
756           ind++;
757           if(indlist[ind] < 0) recp=0;
758                else recp = (AliITSRecPoint*)frecPoints->UncheckedAt(indlist[ind]);
759
760           if((!recp)  )  break; 
761           TVector cluster(3),vecclust(9);
762           vecclust(6)=vecclust(7)=vecclust(8)=-1.;
763           Double_t sigma[2];               
764           // set veclust in global
765           Float_t global[3], local[3];
766           local[0]=recp->GetX();
767           local[1]=0.;
768           local[2]= recp->GetZ();
769           //AliITSgeom *g1 = aliITS->GetITSgeom();  //vvecchia
770                          AliITSgeom *g1 = fITS->GetITSgeom();  //nnuova
771           Int_t play = lycur;
772           Int_t plad = TMath::Nint(toucLad(iriv));   
773           Int_t pdet = TMath::Nint(toucDet(iriv));              
774           g1->LtoG(play,plad,pdet,local,global); 
775           
776           vecclust(0)=global[0];
777           vecclust(1)=global[1];
778           vecclust(2)=global[2];
779
780                                                      
781           vecclust(3) = (float)recp->fTracks[0]; 
782           vecclust(4) = (float)indlist[ind];
783           vecclust(5) = (float)index;
784           vecclust(6) = (float)recp->fTracks[0];
785           vecclust(7) = (float)recp->fTracks[1];
786           vecclust(8) = (float)recp->fTracks[2];
787      
788           sigma[0] = (Double_t)  recp->GetSigmaX2();       
789           sigma[1] = (Double_t) recp->GetSigmaZ2();
790                  
791          //now we are in r,phi,z in global
792           cluster(0) = TMath::Sqrt(vecclust(0)*vecclust(0)+vecclust(1)*vecclust(1));//r hit
793          // cluster(1) = PhiDef(vecclust(0),vecclust(1));    // phi hit  //vecchio
794                          cluster(1) = TMath::ATan2(vecclust(1),vecclust(0)); if(cluster(1)<0.) cluster(1)+=2.*TMath::Pi();  //nuovo                     
795           cluster(2) = vecclust(2);                   // z hit
796         
797          // cout<<" layer = "<<play<<"\n";
798          // cout<<" cluster prima = "<<vecclust(0)<<" "<<vecclust(1)<<" "
799          // <<vecclust(2)<<"\n"; getchar();    
800           //cluster(1)= cluster(1)-trackITS->Getalphaprov();  //provvisorio;
801                          //if(cluster(1)<0.) cluster(1)+=2.*TMath::Pi(); //provvisorio
802                          //cout<<" cluster(1) dopo = "<<cluster(1)<< " alphaprov = "<<trackITS->Getalphaprov()<<"\n";                    
803           Float_t sigmatotphi, sigmatotz;
804                                   
805           //Float_t epsphi=3.2, epsz=3.; 
806                Float_t epsphi=5.0, epsz=5.0;              
807           if(fPtref<0.2) {epsphi=3.; epsz=3.;}
808                                   
809           Double_t rTrack=(*trackITS).Getrtrack();
810           Double_t sigmaphi=sigma[0]/(rTrack*rTrack);
811           sigmatotphi=epsphi*TMath::Sqrt(sigmaphi + (*trackITS).GetSigmaphi());
812                  
813           sigmatotz=epsz*TMath::Sqrt(sigma[1] + (*trackITS).GetSigmaZ());
814   //cout<<"cluster e sigmatotphi e track = "<<cluster(0)<<" "<<cluster(1)<<" "<<sigmatotphi<<" "<<vecclust(3)<<"\n";
815   //if(vecclust(3)==481) getchar();
816                if(cluster(1)<6. && (*trackITS).Getphi()>6.) cluster(1)=cluster(1)+(2.*TMath::Pi());
817                if(cluster(1)>6. && (*trackITS).Getphi()<6.) cluster(1)=cluster(1)-(2.*TMath::Pi());                               
818           if(TMath::Abs(cluster(1)-(*trackITS).Getphi()) > sigmatotphi) continue;
819                         // cout<<" supero sigmaphi \n";      
820           AliITSTrackV1 *newTrack = new AliITSTrackV1((*trackITS));
821           (*newTrack).SetLayer((*trackITS).GetLayer()-1); 
822  
823                if (TMath::Abs(rTrack-cluster(0))/rTrack>1e-6) 
824                                                (*newTrack).Correct(Double_t(cluster(0)));       
825                         //cout<<" cluster(2) e (*newTrack).GetZ() = "<<cluster(2)<<" "<<        (*newTrack).GetZ()<<"\n";                                                                               
826           if(TMath::Abs(cluster(2)-(*newTrack).GetZ()) > sigmatotz){ 
827              delete newTrack;
828              continue;}
829        
830           if(iriv == 0) flaghit=1;
831  
832           (*newTrack).AddMS(frl);  // add the multiple scattering matrix to the covariance matrix 
833           (*newTrack).AddEL(frl,1.,0);
834                  
835           Double_t sigmanew[2];
836           sigmanew[0]= sigmaphi;
837           sigmanew[1]=sigma[1];
838
839           if(fflagvert)          
840             KalmanFilterVert(newTrack,cluster,sigmanew);  
841           else                                                    
842             KalmanFilter(newTrack,cluster,sigmanew);
843                
844                   
845           (*newTrack).PutCluster(layernew, vecclust);
846            newTrack->AddClustInTrack();            
847                                                                         
848            listoftrack.AddLast(newTrack);
849
850         }   // end of for(;;) on rec points 
851
852         delete [] indlist;
853   
854       }  // end of for on detectors
855      
856     }//end if(outinters==0) 
857   
858     if(flaghit==0 || outinters==-2) {
859       AliITSTrackV1 *newTrack = new AliITSTrackV1(*trackITS);    
860       (*newTrack).SetLayer((*trackITS).GetLayer()-1); 
861       (*newTrack).AddMS(frl);  // add the multiple scattering matrix to the covariance matrix  
862       (*newTrack).AddEL(frl,1.,0);            
863                                                   
864       listoftrack.AddLast(newTrack);      
865     }   
866                 
867
868     //gObjectTable->Print();   // stampa memoria
869          
870     RecursiveTracking(&listoftrack);          
871     listoftrack.Delete();
872   } // end of for on tracks
873
874   //gObjectTable->Print();   // stampa memoria
875
876 }   
877
878 Int_t AliITSTrackerV1::Intersection(AliITSTrackV1 &track, Double_t rk,Int_t layer, Int_t &ladder, 
879 Int_t &detector) { 
880 //Origin  A. Badala' and G.S. Pappalardo:  e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
881 // Found the intersection and the detector 
882
883   if(track.DoNotCross(rk)){ /*cout<< " Do not cross \n";*/ return -1;} 
884   track.Propagation(rk);
885   Double_t zinters=track.GetZ();
886   Double_t phinters=track.Getphi();
887   //cout<<"zinters = "<<zinters<<"  phinters = "<<phinters<<"\n";
888
889   //////////////////////////////////      limits for Geometry 5      /////////////////////////////
890   
891   //Int_t NLadder[]= {20, 40, 14, 22, 34, 38};
892   //Int_t NDetector[]= {4,  4,   6,  8, 23, 26}; 
893
894   //Float_t Detx[]= {0.64, 0.64, 3.509, 3.509, 3.65, 3.65 };
895   //Float_t Detz[]= {4.19, 4.19, 3.75 , 3.75 , 2   , 2    };
896
897   ////////////////////////////////////////////////////////////////////////////////////////////////  
898   
899   TVector det(9);
900   TVector listDet(2);
901   TVector distZCenter(2);  
902   AliITSgeom *g1 = ((AliITS*)gAlice->GetDetector("ITS"))->GetITSgeom();
903   
904   Int_t iz=0; 
905   Double_t epsz=1.2;
906   Double_t epszpixel=0.05;
907
908   Int_t iD;
909   for(iD = 1; iD<= fNdet[layer-1]; iD++) {
910     g1->GetCenterThetaPhi(layer,1,iD,det);
911          Double_t zmin=det(2)-fDetz[layer-1];   
912          if(iD==1) zmin=det(2)-(fDetz[layer-1])*epsz;           
913          Double_t zmax=det(2)+fDetz[layer-1];    
914          if(iD==fNdet[layer-1]) zmax=det(2)+(fDetz[layer-1])*epsz;   
915     //added to take into account problem on drift
916     if(layer == 4 || layer==3) zmin=zmin-epszpixel; zmax=zmax+epszpixel;
917     //cout<<"zmin zinters zmax det(2)= "<<zmin<<" "<<zinters<<" "<<zmax<<" "<<det(2)<<"\n";     
918     if(zinters > zmin && zinters <= zmax) { 
919       if(iz>1) {cout<< " Errore su iz in Intersection \n"; getchar();}
920       else {
921         listDet(iz)= iD; distZCenter(iz)=TMath::Abs(zinters-det(2)); iz++;
922       }
923     }                         
924   }
925   
926   if(iz==0) {/* cout<< " No detector along Z \n";*/ return -2;}
927   detector=Int_t (listDet(0));
928   if(iz>1 && (distZCenter(0)>distZCenter(1)))   detector=Int_t (listDet(1));
929   
930   AliITSgeom *g2 = ((AliITS*)gAlice->GetDetector("ITS"))->GetITSgeom(); 
931   Float_t global[3];
932   Float_t local[3];
933   TVector listLad(2);
934   TVector distPhiCenter(2);
935   Int_t ip=0;
936   Double_t pigre=TMath::Pi();
937   
938   Int_t iLd;   
939   for(iLd = 1; iLd<= fNlad[layer-1]; iLd++) {  
940           g1->GetCenterThetaPhi(layer,iLd,detector,det);
941  // Double_t phidet=PhiDef(Double_t(det(0)),Double_t(det(1)));  //vecchio
942     Double_t phidet= TMath::ATan2(Double_t(det(1)),Double_t(det(0))); if(phidet<0.) phidet+=2.*TMath::Pi(); //nuovo 
943   // cout<<" layer phidet e det(6) = "<< layer<<" "<<phidet<<" "<<det(6)<<"\n"; getchar();
944   Double_t xmin,ymin,xmax,ymax; 
945  // Double_t phiconfr=0.0;
946   //cout<<" phiconfr inizio =  "<<phiconfr <<"\n"; getchar();  
947   local[1]=local[2]=0.;  
948   local[0]= -(fDetx[layer-1]);    
949   if(layer==1)    local[0]= (fDetx[layer-1]);  //take into account different reference system   
950   g2->LtoG(layer,iLd,detector,local,global);
951   xmax=global[0]; ymax=global[1];
952   local[0]= (fDetx[layer-1]);   
953   if(layer==1)    local[0]= -(fDetx[layer-1]);  //take into account different reference system 
954   g2->LtoG(layer,iLd,detector,local,global);
955   xmin=global[0]; ymin=global[1];
956  // Double_t phimin=PhiDef(xmin,ymin);  //vecchio
957      Double_t phimin= TMath::ATan2(ymin,xmin); if(phimin<0.) phimin+=2.*TMath::Pi();  //nuovo 
958  // Double_t phimax=PhiDef(xmax,ymax);
959     Double_t phimax= TMath::ATan2(ymax,xmax); if(phimax<0.) phimax+=2.*TMath::Pi();  //nuovo 
960   //cout<<" xmin ymin = "<<xmin<<" "<<ymin<<"\n";
961   // cout<<" xmax ymax = "<<xmax<<" "<<ymax<<"\n";  
962   // cout<<" iLd phimin phimax ="<<iLd<<" "<<phimin<<" "<<phimax<<"\n";
963
964   Double_t phiconfr=phinters;
965  if(phimin>phimax ){    
966      if(phimin <5.5) {cout<<" Error in Intersection for phi \n"; getchar();}
967     phimin=phimin-(2.*pigre);
968     if(phinters>(1.5*pigre)) phiconfr=phinters-(2.*pigre); 
969     if(phidet>(1.5*pigre)) phidet=phidet-(2.*pigre);
970   }              
971   //  cout<<" phiconfr finale = "<<phiconfr<<"\n"; getchar(); 
972   if(phiconfr>phimin && phiconfr<= phimax) {
973     if(ip>1) {
974       cout<< " Errore su ip in Intersection \n"; getchar();
975     }
976       else  {
977         listLad(ip)= iLd; distPhiCenter(ip)=TMath::Abs(phiconfr-phidet); ip++;
978       }  
979     }
980   }
981   if(ip==0) { cout<< " No detector along phi \n"; getchar();}
982   ladder=Int_t (listLad(0));
983   if(ip>1 && (distPhiCenter(0)>distPhiCenter(1)))   ladder=Int_t (listLad(1));       
984
985   return 0;
986 }
987
988
989 void AliITSTrackerV1::KalmanFilter(AliITSTrackV1 *newTrack,TVector &cluster,Double_t sigma[2]){ 
990 //Origin  A. Badala' and G.S. Pappalardo:  e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
991 // Kalman filter without vertex constraint
992
993
994   ////////////////////////////// Evaluation of the measurement vector /////////////////////////////////////  
995
996   Double_t m[2];
997   Double_t rk,phik,zk;
998   rk=cluster(0);   phik=cluster(1);  zk=cluster(2);
999   m[0]=phik;    m[1]=zk; 
1000        
1001   ///////////////////////////////////// Evaluation of the error matrix V  ///////////////////////////////          
1002
1003   Double_t v00=sigma[0];
1004   Double_t v11=sigma[1];
1005   
1006   ///////////////////////////////////////////////////////////////////////////////////////////
1007   
1008   
1009   Double_t cin00,cin10,cin20,cin30,cin40,cin11,cin21,cin31,cin41,cin22,cin32,cin42,cin33,cin43,cin44;
1010                             
1011   newTrack->GetCElements(cin00,cin10,cin11,cin20,cin21,cin22,cin30,cin31,cin32,cin33,cin40,
1012                          cin41,cin42,cin43,cin44); //get C matrix
1013                           
1014   Double_t rold00=cin00+v00;
1015   Double_t rold10=cin10;
1016   Double_t rold11=cin11+v11;
1017   
1018 //////////////////////////////////// R matrix inversion  ///////////////////////////////////////////////
1019   
1020   Double_t det=rold00*rold11-rold10*rold10;
1021   Double_t r00=rold11/det;
1022   Double_t r10=-rold10/det;
1023   Double_t r11=rold00/det;
1024
1025 ////////////////////////////////////////////////////////////////////////////////////////////////////////                            
1026
1027   Double_t k00=cin00*r00+cin10*r10;
1028   Double_t k01=cin00*r10+cin10*r11;
1029   Double_t k10=cin10*r00+cin11*r10;  
1030   Double_t k11=cin10*r10+cin11*r11;
1031   Double_t k20=cin20*r00+cin21*r10;  
1032   Double_t k21=cin20*r10+cin21*r11;  
1033   Double_t k30=cin30*r00+cin31*r10;  
1034   Double_t k31=cin30*r10+cin31*r11;  
1035   Double_t k40=cin40*r00+cin41*r10;
1036   Double_t k41=cin40*r10+cin41*r11;
1037   
1038   Double_t x0,x1,x2,x3,x4;
1039   newTrack->GetXElements(x0,x1,x2,x3,x4);     // get the state vector
1040   
1041   Double_t savex0=x0, savex1=x1;
1042   
1043   x0+=k00*(m[0]-savex0)+k01*(m[1]-savex1);
1044   x1+=k10*(m[0]-savex0)+k11*(m[1]-savex1);
1045   x2+=k20*(m[0]-savex0)+k21*(m[1]-savex1);
1046   x3+=k30*(m[0]-savex0)+k31*(m[1]-savex1);
1047   x4+=k40*(m[0]-savex0)+k41*(m[1]-savex1);
1048   
1049   Double_t c00,c10,c20,c30,c40,c11,c21,c31,c41,c22,c32,c42,c33,c43,c44;
1050   
1051   c00=cin00-k00*cin00-k01*cin10;
1052   c10=cin10-k00*cin10-k01*cin11;
1053   c20=cin20-k00*cin20-k01*cin21;
1054   c30=cin30-k00*cin30-k01*cin31;
1055   c40=cin40-k00*cin40-k01*cin41;
1056   
1057   c11=cin11-k10*cin10-k11*cin11;
1058   c21=cin21-k10*cin20-k11*cin21;
1059   c31=cin31-k10*cin30-k11*cin31;
1060   c41=cin41-k10*cin40-k11*cin41;
1061   
1062   c22=cin22-k20*cin20-k21*cin21;
1063   c32=cin32-k20*cin30-k21*cin31;
1064   c42=cin42-k20*cin40-k21*cin41;
1065
1066   c33=cin33-k30*cin30-k31*cin31;
1067   c43=cin43-k30*cin40-k31*cin41;
1068   
1069   c44=cin44-k40*cin40-k41*cin41;
1070   
1071   newTrack->PutXElements(x0,x1,x2,x3,x4);               // put the new state vector
1072    
1073   newTrack->PutCElements(c00,c10,c11,c20,c21,c22,c30,c31,c32,c33,c40,c41,c42,c43,c44); // put in track the
1074                                                                                        // new cov matrix  
1075   Double_t vmcold00=v00-c00;
1076   Double_t vmcold10=-c10;
1077   Double_t vmcold11=v11-c11;
1078   
1079 ///////////////////////////////////// Matrix vmc inversion  ////////////////////////////////////////////////
1080   
1081   det=vmcold00*vmcold11-vmcold10*vmcold10;
1082   Double_t vmc00=vmcold11/det;
1083   Double_t vmc10=-vmcold10/det;
1084   Double_t vmc11=vmcold00/det;
1085
1086 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
1087
1088   Double_t chi2=(m[0]-x0)*( vmc00*(m[0]-x0) + 2.*vmc10*(m[1]-x1) ) +                    
1089                 (m[1]-x1)*vmc11*(m[1]-x1);       
1090          
1091   newTrack->SetChi2(newTrack->GetChi2()+chi2);
1092    
1093
1094
1095
1096 void AliITSTrackerV1::KalmanFilterVert(AliITSTrackV1 *newTrack,TVector &cluster,Double_t sigma[2]){
1097 //Origin  A. Badala' and G.S. Pappalardo:  e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it 
1098 // Kalman filter with vertex constraint
1099
1100   ////////////////////////////// Evaluation of the measurement vector m ///////////////  
1101
1102   Double_t m[4];
1103   Double_t rk,phik,zk;
1104   rk=cluster(0);   phik=cluster(1);  zk=cluster(2);
1105   m[0]=phik;    m[1]=zk;
1106  
1107   Double_t cc=(*newTrack).GetC();
1108   Double_t zv=(*newTrack).GetZv(); 
1109   Double_t dv=(*newTrack).GetDv();
1110   Double_t cy=cc/2.;
1111   Double_t tgl= (zk-zv)*cy/TMath::ASin(cy*rk);
1112   m[2]=dv;    m[3]=tgl;
1113
1114   ///////////////////////////////////// Evaluation of the error matrix V  //////////////
1115   Int_t layer=newTrack->GetLayer();
1116   Double_t v00=sigma[0];
1117   Double_t v11=sigma[1];
1118   Double_t v31=sigma[1]/rk;
1119   Double_t sigmaDv=newTrack->GetsigmaDv();
1120   Double_t v22=sigmaDv*sigmaDv  + newTrack->Getd2(layer-1);
1121   Double_t v32=newTrack->Getdtgl(layer-1);
1122   Double_t sigmaZv=newTrack->GetsigmaZv();  
1123   Double_t v33=(sigma[1]+sigmaZv*sigmaZv)/(rk*rk) + newTrack->Gettgl2(layer-1);
1124   ///////////////////////////////////////////////////////////////////////////////////////
1125   
1126   Double_t cin00,cin10,cin11,cin20,cin21,cin22,cin30,cin31,cin32,cin33,cin40,cin41,cin42,cin43,cin44;
1127                             
1128   newTrack->GetCElements(cin00,cin10,cin11,cin20,cin21,cin22,cin30,cin31,cin32,cin33,cin40,
1129                          cin41,cin42,cin43,cin44); //get C matrix
1130                           
1131   Double_t r[4][4];
1132   r[0][0]=cin00+v00;
1133   r[1][0]=cin10;
1134   r[2][0]=cin20;
1135   r[3][0]=cin30;
1136   r[1][1]=cin11+v11;
1137   r[2][1]=cin21;
1138   r[3][1]=cin31+sigma[1]/rk;
1139   r[2][2]=cin22+sigmaDv*sigmaDv+newTrack->Getd2(layer-1);
1140   r[3][2]=cin32+newTrack->Getdtgl(layer-1);
1141   r[3][3]=cin33+(sigma[1]+sigmaZv*sigmaZv)/(rk*rk) + newTrack->Gettgl2(layer-1);
1142   
1143   r[0][1]=r[1][0]; r[0][2]=r[2][0]; r[0][3]=r[3][0]; r[1][2]=r[2][1]; r[1][3]=r[3][1]; 
1144   r[2][3]=r[3][2];
1145
1146 /////////////////////  Matrix R inversion ////////////////////////////////////////////
1147  
1148   const Int_t kn=4;
1149   Double_t big, hold;
1150   Double_t d=1.;
1151   Int_t ll[kn],mm[kn];
1152
1153   Int_t i,j,k;
1154   
1155   for(k=0; k<kn; k++) {
1156     ll[k]=k;
1157     mm[k]=k;
1158     big=r[k][k];
1159     for(j=k; j<kn ; j++) {
1160       for (i=j; i<kn; i++) {
1161         if(TMath::Abs(big) < TMath::Abs(r[i][j]) ) { big=r[i][j]; ll[k]=i; mm[k]=j; }
1162       }
1163     }    
1164 //
1165     j= ll[k];
1166     if(j > k) {
1167       for(i=0; i<kn; i++) { hold=-r[k][i]; r[k][i]=r[j][i]; r[j][i]=hold; }
1168       
1169     }
1170 //
1171     i=mm[k];
1172     if(i > k ) { 
1173       for(j=0; j<kn; j++) { hold=-r[j][k]; r[j][k]=r[j][i]; r[j][i]=hold; }
1174     }
1175 //
1176     if(!big) {
1177       d=0.;
1178       cout << "Singular matrix\n"; 
1179     }
1180     for(i=0; i<kn; i++) {
1181       if(i == k) { continue; }    
1182       r[i][k]=r[i][k]/(-big);
1183     }   
1184 //
1185     for(i=0; i<kn; i++) {
1186       hold=r[i][k];
1187       for(j=0; j<kn; j++) {
1188         if(i == k || j == k) { continue; }
1189         r[i][j]=hold*r[k][j]+r[i][j];
1190       }
1191     }
1192 //  
1193     for(j=0; j<kn; j++) {
1194       if(j == k) { continue; }
1195       r[k][j]=r[k][j]/big;
1196     }
1197 //
1198     d=d*big;
1199 //
1200     r[k][k]=1./big;        
1201   } 
1202 //  
1203   for(k=kn-1; k>=0; k--) {
1204     i=ll[k];
1205     if(i > k) {
1206       for (j=0; j<kn; j++) {hold=r[j][k]; r[j][k]=-r[j][i]; r[j][i]=hold;}
1207     }  
1208     j=mm[k];
1209     if(j > k) {
1210       for (i=0; i<kn; i++) {hold=r[k][i]; r[k][i]=-r[j][i]; r[j][i]=hold;}
1211       }
1212   }
1213 //////////////////////////////////////////////////////////////////////////////////
1214
1215
1216   Double_t k00=cin00*r[0][0]+cin10*r[1][0]+cin20*r[2][0]+cin30*r[3][0];
1217   Double_t k01=cin00*r[1][0]+cin10*r[1][1]+cin20*r[2][1]+cin30*r[3][1];
1218   Double_t k02=cin00*r[2][0]+cin10*r[2][1]+cin20*r[2][2]+cin30*r[3][2];
1219   Double_t k03=cin00*r[3][0]+cin10*r[3][1]+cin20*r[3][2]+cin30*r[3][3];
1220   Double_t k10=cin10*r[0][0]+cin11*r[1][0]+cin21*r[2][0]+cin31*r[3][0];  
1221   Double_t k11=cin10*r[1][0]+cin11*r[1][1]+cin21*r[2][1]+cin31*r[3][1];
1222   Double_t k12=cin10*r[2][0]+cin11*r[2][1]+cin21*r[2][2]+cin31*r[3][2];
1223   Double_t k13=cin10*r[3][0]+cin11*r[3][1]+cin21*r[3][2]+cin31*r[3][3];
1224   Double_t k20=cin20*r[0][0]+cin21*r[1][0]+cin22*r[2][0]+cin32*r[3][0];  
1225   Double_t k21=cin20*r[1][0]+cin21*r[1][1]+cin22*r[2][1]+cin32*r[3][1];  
1226   Double_t k22=cin20*r[2][0]+cin21*r[2][1]+cin22*r[2][2]+cin32*r[3][2];
1227   Double_t k23=cin20*r[3][0]+cin21*r[3][1]+cin22*r[3][2]+cin32*r[3][3];
1228   Double_t k30=cin30*r[0][0]+cin31*r[1][0]+cin32*r[2][0]+cin33*r[3][0];  
1229   Double_t k31=cin30*r[1][0]+cin31*r[1][1]+cin32*r[2][1]+cin33*r[3][1];  
1230   Double_t k32=cin30*r[2][0]+cin31*r[2][1]+cin32*r[2][2]+cin33*r[3][2];  
1231   Double_t k33=cin30*r[3][0]+cin31*r[3][1]+cin32*r[3][2]+cin33*r[3][3];
1232   Double_t k40=cin40*r[0][0]+cin41*r[1][0]+cin42*r[2][0]+cin43*r[3][0];
1233   Double_t k41=cin40*r[1][0]+cin41*r[1][1]+cin42*r[2][1]+cin43*r[3][1];
1234   Double_t k42=cin40*r[2][0]+cin41*r[2][1]+cin42*r[2][2]+cin43*r[3][2];  
1235   Double_t k43=cin40*r[3][0]+cin41*r[3][1]+cin42*r[3][2]+cin43*r[3][3];
1236   
1237   Double_t x0,x1,x2,x3,x4;
1238   newTrack->GetXElements(x0,x1,x2,x3,x4);     // get the state vector
1239   
1240   Double_t savex0=x0, savex1=x1, savex2=x2, savex3=x3;
1241   
1242   x0+=k00*(m[0]-savex0)+k01*(m[1]-savex1)+k02*(m[2]-savex2)+
1243       k03*(m[3]-savex3);
1244   x1+=k10*(m[0]-savex0)+k11*(m[1]-savex1)+k12*(m[2]-savex2)+
1245       k13*(m[3]-savex3);
1246   x2+=k20*(m[0]-savex0)+k21*(m[1]-savex1)+k22*(m[2]-savex2)+
1247       k23*(m[3]-savex3);
1248   x3+=k30*(m[0]-savex0)+k31*(m[1]-savex1)+k32*(m[2]-savex2)+
1249       k33*(m[3]-savex3);
1250   x4+=k40*(m[0]-savex0)+k41*(m[1]-savex1)+k42*(m[2]-savex2)+
1251       k43*(m[3]-savex3);       
1252
1253   Double_t c00,c10,c20,c30,c40,c11,c21,c31,c41,c22,c32,c42,c33,c43,c44;
1254   
1255   c00=cin00-k00*cin00-k01*cin10-k02*cin20-k03*cin30;
1256   c10=cin10-k00*cin10-k01*cin11-k02*cin21-k03*cin31;
1257   c20=cin20-k00*cin20-k01*cin21-k02*cin22-k03*cin32;
1258   c30=cin30-k00*cin30-k01*cin31-k02*cin32-k03*cin33;
1259   c40=cin40-k00*cin40-k01*cin41-k02*cin42-k03*cin43;
1260   
1261   c11=cin11-k10*cin10-k11*cin11-k12*cin21-k13*cin31;
1262   c21=cin21-k10*cin20-k11*cin21-k12*cin22-k13*cin32;
1263   c31=cin31-k10*cin30-k11*cin31-k12*cin32-k13*cin33;
1264   c41=cin41-k10*cin40-k11*cin41-k12*cin42-k13*cin43;
1265   
1266   c22=cin22-k20*cin20-k21*cin21-k22*cin22-k23*cin32;
1267   c32=cin32-k20*cin30-k21*cin31-k22*cin32-k23*cin33;
1268   c42=cin42-k20*cin40-k21*cin41-k22*cin42-k23*cin43;
1269
1270   c33=cin33-k30*cin30-k31*cin31-k32*cin32-k33*cin33;
1271   c43=cin43-k30*cin40-k31*cin41-k32*cin42-k33*cin43;
1272   
1273   c44=cin44-k40*cin40-k41*cin41-k42*cin42-k43*cin43;
1274   
1275   newTrack->PutXElements(x0,x1,x2,x3,x4);               // put the new state vector
1276   
1277   newTrack->PutCElements(c00,c10,c11,c20,c21,c22,c30,c31,c32,c33,c40,c41,c42,c43,c44); // put in track the
1278                                                                                        // new cov matrix
1279   
1280   Double_t vmc[4][4];
1281   
1282   vmc[0][0]=v00-c00; vmc[1][0]=-c10; vmc[2][0]=-c20; vmc[3][0]=-c30;
1283   vmc[1][1]=v11-c11; vmc[2][1]=-c21; vmc[3][1]=v31-c31;
1284   vmc[2][2]=v22-c22; vmc[3][2]=v32-c32;
1285   vmc[3][3]=v33-c33;
1286   
1287   vmc[0][1]=vmc[1][0]; vmc[0][2]=vmc[2][0]; vmc[0][3]=vmc[3][0];
1288   vmc[1][2]=vmc[2][1]; vmc[1][3]=vmc[3][1];
1289   vmc[2][3]=vmc[3][2];
1290   
1291
1292 /////////////////////// vmc matrix inversion ///////////////////////////////////  
1293  
1294   d=1.;
1295   
1296   for(k=0; k<kn; k++) {
1297     ll[k]=k;
1298     mm[k]=k;
1299     big=vmc[k][k];
1300     for(j=k; j<kn ; j++) {
1301       for (i=j; i<kn; i++) {
1302         if(TMath::Abs(big) < TMath::Abs(vmc[i][j]) ) { big=vmc[i][j]; ll[k]=i; mm[k]=j; }
1303       }
1304     }    
1305 //
1306     j= ll[k];
1307     if(j > k) {
1308       for(i=0; i<kn; i++) { hold=-vmc[k][i]; vmc[k][i]=vmc[j][i]; vmc[j][i]=hold; }
1309       
1310     }
1311 //
1312     i=mm[k];
1313     if(i > k ) { 
1314       for(j=0; j<kn; j++) { hold=-vmc[j][k]; vmc[j][k]=vmc[j][i]; vmc[j][i]=hold; }
1315     }
1316 //
1317     if(!big) {
1318       d=0.;
1319       cout << "Singular matrix\n"; 
1320     }
1321     for(i=0; i<kn; i++) {
1322       if(i == k) { continue; }    
1323       vmc[i][k]=vmc[i][k]/(-big);
1324     }   
1325 //
1326     for(i=0; i<kn; i++) {
1327       hold=vmc[i][k];
1328       for(j=0; j<kn; j++) {
1329         if(i == k || j == k) { continue; }
1330         vmc[i][j]=hold*vmc[k][j]+vmc[i][j];
1331       }
1332     }
1333 //  
1334     for(j=0; j<kn; j++) {
1335       if(j == k) { continue; }
1336       vmc[k][j]=vmc[k][j]/big;
1337     }
1338 //
1339     d=d*big;
1340 //
1341     vmc[k][k]=1./big;        
1342   } 
1343 //  
1344   for(k=kn-1; k>=0; k--) {
1345     i=ll[k];
1346     if(i > k) {
1347       for (j=0; j<kn; j++) {hold=vmc[j][k]; vmc[j][k]=-vmc[j][i]; vmc[j][i]=hold;}
1348     }  
1349     j=mm[k];
1350     if(j > k) {
1351       for (i=0; i<kn; i++) {hold=vmc[k][i]; vmc[k][i]=-vmc[j][i]; vmc[j][i]=hold;}
1352       }
1353   }
1354
1355
1356 ////////////////////////////////////////////////////////////////////////////////
1357
1358   Double_t chi2=(m[0]-x0)*( vmc[0][0]*(m[0]-x0) + 2.*vmc[1][0]*(m[1]-x1) + 
1359                    2.*vmc[2][0]*(m[2]-x2)+ 2.*vmc[3][0]*(m[3]-x3) ) +
1360                 (m[1]-x1)* ( vmc[1][1]*(m[1]-x1) + 2.*vmc[2][1]*(m[2]-x2)+ 
1361                    2.*vmc[3][1]*(m[3]-x3) ) +
1362                 (m[2]-x2)* ( vmc[2][2]*(m[2]-x2)+ 2.*vmc[3][2]*(m[3]-x3) ) +
1363                 (m[3]-x3)*vmc[3][3]*(m[3]-x3);  
1364          
1365   newTrack->SetChi2(newTrack->GetChi2()+chi2);
1366    
1367
1368