Adding the AliAnalysisGUI class which is the main class that controls the GUI.
[u/mrichter/AliRoot.git] / ITS / AliITSTrackerV1.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 // The purpose of this class is to permorm the ITS tracking. The 
19 // constructor has the task to inizialize some private members. The method 
20 // DoTracking is written to be called by a macro. It gets the event number,
21 // the minimum and maximum order number of TPC tracks that are to be tracked
22 // trough the ITS, and the file where the recpoints are registered. The 
23 // method Recursivetracking is a recursive function that performs the 
24 // tracking trough the ITS The method Intersection found the layer, ladder 
25 // and detector whre the intersection take place and caluclate the 
26 // cohordinates of this intersection. It returns an integer that is 0 if the 
27 // intersection has been found successfully. The two mwthods Kalmanfilter 
28 // and kalmanfiltervert operate the kalmanfilter without and with the vertex
29 // imposition respectively. The authors thank Mariana Bondila to have help 
30 // them to resolve some problems.  July-2000
31
32 #include <Riostream.h>
33 #include <TMath.h>
34 #include <TBranch.h>
35 #include <TVector.h>
36 #include <TFile.h>
37 #include <TRandom.h>
38 #include <TTree.h>
39
40 #include "TParticle.h"
41 #include "AliRun.h"
42 #include "AliITSDetTypeRec.h"
43 #include "AliITSgeomSPD.h"
44 #include "AliITSgeomSDD.h"
45 #include "AliITSgeomSSD.h"
46 #include "AliITSgeom.h"
47 #include "AliITSRecPoint.h"
48 #include "AliKalmanTrack.h" 
49 #include "AliMagF.h"
50 #include "AliITSTrackV1.h"
51 #include "AliITSIOTrack.h"
52 #include "AliITSRad.h"   
53 #include "AliTPCtracker.h"
54 #include "AliITSTrackerV1.h"
55 #include "AliITSPid.h"
56 #include "AliMC.h"
57
58 ClassImp(AliITSTrackerV1)
59  //______________________________________________________________________
60 AliITSTrackerV1::AliITSTrackerV1() {
61   //Default constructor
62   //fITS = 0;
63   fresult = 0;
64   fPtref=0.;
65   fChi2max=0.;
66   //fepsphi=0.;
67   //fepsz=0.;
68   frecPoints = 0;
69   fvettid = 0;
70   fflagvert=0;
71   frl = 0;
72   Int_t ia;
73   for(ia=0; ia<6; ia++) {
74   fNlad[ia]=0;
75   fNdet[ia]=0;
76   fAvrad[ia]=0.;
77   fDetx[ia]=0.;
78   fDetz[ia]=0.; 
79   } // end for ia  
80   fzmin = 0;
81   fzmax = 0;
82   fphimin = 0;
83   fphimax = 0;
84   fphidet = 0;
85   fNRecPoints=0;
86   fRecCylR=0;
87   fRecCylPhi=0;
88   fRecCylZ=0;
89   fFieldFactor=0;
90   fDetTypeRec = 0;
91 }
92 //______________________________________________________________________
93 AliITSTrackerV1::AliITSTrackerV1(Int_t evnumber, Bool_t flag) {
94     //Origin   A. Badala' and G.S. Pappalardo:  
95     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
96     // Class constructor. It does some initializations.
97
98   //PH Initialisation taken from the default constructor
99   //fITS      = IITTSS;
100   fresult = 0;
101   fPtref    = 0.;
102   fChi2max  =0.; 
103   frecPoints = 0;                
104   fvettid = 0;
105   fflagvert = flag;      
106   frl = 0;
107   fzmin = 0;
108   fzmax = 0;
109   fphimin = 0;
110   fphimax = 0;
111   fphidet = 0;
112   
113   Int_t imax = 200,jmax = 450;
114   frl       = new AliITSRad(imax,jmax);
115   
116   //////////  gets information on geometry /////////////////////////////
117   AliRunLoader* rl = AliRunLoader::Open("galice.root");
118   AliITSLoader* loader = (AliITSLoader*)runLoader->GetLoader("ITSLoader");
119   if (!loader) {
120     Error("AliITSTrackerV1", "ITS loader not found");
121     return;
122   }
123   fDetTypeRec = new AliITSDetTypeRec(loader);
124   AliITSgeom* g1 = loader->GetITSgeom();
125
126   Int_t ll=1, dd=1;
127   TVector det(9);
128   
129   Int_t ia;
130   for(ia=0; ia<6; ia++) {
131     fNlad[ia]=g1->GetNladders(ia+1);
132     fNdet[ia]=g1->GetNdetectors(ia+1);
133     //cout<<fNlad[i]<<" "<<fNdet[i]<<"\n"; 
134   } // end for ia
135   
136     //cout<<" mean radius = ";
137   Int_t ib;
138   for(ib=0; ib<6; ib++) {  
139     g1->GetCenterThetaPhi(ib+1,ll,dd,det);
140     Double_t r1=TMath::Sqrt(det(0)*det(0)+det(1)*det(1));
141     g1->GetCenterThetaPhi(ib+1,ll,dd+1,det);
142     Double_t r2=TMath::Sqrt(det(0)*det(0)+det(1)*det(1));
143     fAvrad[ib]=(r1+r2)/2.;
144     //cout<<fAvrad[ib]<<" ";
145   } // end for ib
146     //cout<<"\n"; getchar();
147   
148   fDetx[0] = ((AliITSgeomSPD*)(g1->GetShape(1, ll, dd)))->GetDx();
149   fDetz[0] = ((AliITSgeomSPD*)(g1->GetShape(1, ll, dd)))->GetDz();
150   
151   fDetx[1] = ((AliITSgeomSPD*)(g1->GetShape(2, ll, dd)))->GetDx();
152   fDetz[1] = ((AliITSgeomSPD*)(g1->GetShape(2, ll, dd)))->GetDz();
153
154     fDetx[2] = ((AliITSgeomSDD*)(g1->GetShape(3, ll, dd)))->GetDx();
155     fDetz[2] = ((AliITSgeomSDD*)(g1->GetShape(3, ll, dd)))->GetDz();
156
157     fDetx[3] = ((AliITSgeomSDD*)(g1->GetShape(4, ll, dd)))->GetDx();
158     fDetz[3] = ((AliITSgeomSDD*)(g1->GetShape(4, ll, dd)))->GetDz();
159
160     fDetx[4] = ((AliITSgeomSSD*)(g1->GetShape(5, ll, dd)))->GetDx();
161     fDetz[4] = ((AliITSgeomSSD*)(g1->GetShape(5, ll, dd)))->GetDz();
162
163     fDetx[5] = ((AliITSgeomSSD*)(g1->GetShape(6, ll, dd)))->GetDx();
164     fDetz[5] = ((AliITSgeomSSD*)(g1->GetShape(6, ll, dd)))->GetDz();
165     //cout<<"    Detx     Detz\n";
166     //for(Int_t la=0; la<6; la++) cout<<"    "<<fDetx[la]<<"     "<<
167     //                                 fDetz[la]<<endl;
168     //getchar();
169          
170     // allocate memory and define matrices fzmin, fzmax, fphimin and fphimax //
171     Double_t epsz=1.2;
172     Double_t epszdrift=0.05;
173
174     fzmin = new Double_t*[6]; fzmax = new Double_t*[6];
175     Int_t im1, im2, im2max;
176     for(im1=0; im1<6; im1++) {
177         im2max=fNdet[im1];
178         fzmin[im1] = new Double_t[im2max]; fzmax[im1] = new Double_t[im2max];
179     } // end for im1
180
181     for(im1=0; im1<6; im1++) {
182         im2max=fNdet[im1];
183         for(im2=0; im2<im2max; im2++) {
184             g1->GetCenterThetaPhi(im1+1,1,im2+1,det);
185             if(im2!=0) fzmin[im1][im2]=det(2)-fDetz[im1];
186             else   
187                 fzmin[im1][im2]=det(2)-(fDetz[im1])*epsz;
188             if(im2!=(im2max-1)) fzmax[im1][im2]=det(2)+fDetz[im1];
189             else
190                 fzmax[im1][im2]=det(2)+fDetz[im1]*epsz;
191             if(im1==2 || im1==3) {
192                 fzmin[im1][im2]-=epszdrift;
193                 fzmax[im1][im2]+=epszdrift;
194             } // end if im1==2 || im1==3
195         } // end for im2
196     } // end for im1
197
198     fphimin = new Double_t*[6]; fphimax = new Double_t*[6];
199     for(im1=0;im1<6;im1++) {
200         im2max=fNlad[im1];
201         fphimin[im1] = new Double_t[im2max]; 
202         fphimax[im1] = new Double_t[im2max];
203     } // end for im1
204   
205     fphidet = new Double_t*[6];
206     for(im1=0; im1<6; im1++) {
207         im2max=fNlad[im1];
208         fphidet[im1] = new Double_t[im2max];
209     } // end for im1
210
211         Double_t global[3],local[3];
212     Double_t pigre=TMath::Pi();
213     Double_t xmin,ymin,xmax,ymax;
214
215     for(im1=0; im1<6; im1++) {
216         im2max=fNlad[im1];
217         for(im2=0; im2<im2max; im2++) {
218             Int_t idet=2;
219             g1->GetCenterThetaPhi(im1+1,im2+1,idet,det);
220             fphidet[im1][im2] = TMath::ATan2(Double_t(det(1)),
221                                              Double_t(det(0))); 
222             if(fphidet[im1][im2]<0.) fphidet[im1][im2]+=2.*pigre;  
223             local[1]=local[2]=0.;  
224             local[0]= -(fDetx[im1]);    
225             if(im1==0) local[0]= (fDetx[im1]); //to take into account 
226                                                // different reference system
227             g1->LtoG(im1+1,im2+1,idet,local,global);
228             xmax=global[0]; ymax=global[1];
229             local[0]= (fDetx[im1]);   
230             if(im1==0) local[0]= -(fDetx[im1]);//take into account different 
231                                                // reference system
232             g1->LtoG(im1+1,im2+1,idet,local,global);
233             xmin=global[0]; ymin=global[1];
234             fphimin[im1][im2]= TMath::ATan2(ymin,xmin);
235             if(fphimin[im1][im2]<0.) fphimin[im1][im2]+=2.*pigre; 
236             fphimax[im1][im2]= TMath::ATan2(ymax,xmax);
237             if(fphimax[im1][im2]<0.) fphimax[im1][im2]+=2.*pigre;
238         } // end for im2
239     } // end for im1
240 //////////////////////////////////////////////////////////////////////////////////////////////////////////
241 /////////////// allocate memory and define vector fNRecPoints and matrices fRecCylR, fRecCylPhi, fRecCylZ /////////////
242         gAlice->GetEvent(evnumber);
243   Int_t numOfModules = g1->GetIndexMax();
244   fRecCylR = new Double_t *[numOfModules];
245   fRecCylPhi = new Double_t *[numOfModules]; 
246   fRecCylZ = new Double_t *[numOfModules];
247   AliITSRecPoint *recp;
248   fNRecPoints = new Int_t[numOfModules];
249    
250                  for(Int_t module=0; module<numOfModules; module++) {                           
251                   fDetTypeRec->ResetRecPoints();                     
252                   gAlice->TreeR()->GetEvent(module);              
253                   frecPoints=fDetTypeRec->RecPoints();
254                   Int_t nRecPoints=fNRecPoints[module]=frecPoints->GetEntries();
255                   fRecCylR[module] = new Double_t[nRecPoints];
256                   fRecCylPhi[module] = new Double_t[nRecPoints];
257                   fRecCylZ[module] = new  Double_t[nRecPoints];           
258                   Int_t ind;
259                   for(ind=0; ind<fNRecPoints[module]; ind++) {    
260                     recp=(AliITSRecPoint*)frecPoints->UncheckedAt(ind);                                          
261                         // Float_t global[3], local[3];
262                          Double_t global[3], local[3];
263                local[0]=recp->GetX();
264                local[1]=0.;
265                local[2]= recp->GetZ();                                  
266                          g1->LtoG(module,local,global);
267                                                                                          
268                          Double_t r = TMath::Sqrt(global[0]*global[0]+global[1]*global[1]);                     // r hit
269                          Double_t phi = TMath::ATan2(global[1],global[0]); if(phi<0.) phi+=2.*TMath::Pi();      // phi hit                      
270                  Double_t z = global[2];                                                                // z hit
271                                                                                                                                                                                                                                      
272                          fRecCylR[module][ind]=r;
273                          fRecCylPhi[module][ind]=phi;
274                          fRecCylZ[module][ind]=z;                        
275                   }             
276                 }        
277          //}  
278   //}
279 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
280          
281
282     ////////// gets magnetic field factor //////////////////////////////
283
284     AliMagF * fieldPointer = gAlice->Field();
285    // fFieldFactor = (Double_t)fieldPointer->Factor();
286     fFieldFactor =(Double_t)fieldPointer-> SolenoidField()/10/.2;
287    // cout<< " field factor = "<<fFieldFactor<<"\n"; getchar();
288     delete rl;
289     
290 }
291 //______________________________________________________________________
292 AliITSTrackerV1::AliITSTrackerV1(const AliITSTrackerV1 &cobj) : TObject(cobj) {
293     // Origin  A. Badala' and G.S. Pappalardo:
294     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
295     // copy constructor
296          
297   //     *fITS = *cobj.fITS;
298     *fresult = *cobj.fresult;
299     fPtref = cobj.fPtref;
300     fChi2max = cobj.fChi2max;    
301     **fvettid = **cobj.fvettid;
302     fflagvert = cobj.fflagvert;
303     Int_t imax=200,jmax=450;
304     frl = new AliITSRad(imax,jmax);      
305     *frl = *cobj.frl;
306     fFieldFactor = cobj.fFieldFactor;
307     Int_t i,im1,im2,im2max;
308     for(i=0; i<6; i++) {
309         fNlad[i] = cobj.fNlad[i];
310         fNdet[i] = cobj.fNdet[i]; 
311         fAvrad[i] = cobj.fAvrad[i];
312         fDetx[i] = cobj.fDetx[i];
313         fDetz[i] = cobj.fDetz[i];
314     } // end or i
315     fzmin = new Double_t*[6]; fzmax = new Double_t*[6];
316     for(im1=0; im1<6; im1++) {
317         im2max=fNdet[im1];
318         fzmin[im1] = new Double_t[im2max];
319         fzmax[im1] = new Double_t[im2max];
320     } // end for im1
321     fphimin = new Double_t*[6]; fphimax = new Double_t*[6];
322     for(im1=0;im1<6;im1++) {
323         im2max=fNlad[im1];
324         fphimin[im1] = new Double_t[im2max];
325         fphimax[im1] = new Double_t[im2max];
326     } // end for im1
327   
328     fphidet = new Double_t*[6];
329     for(im1=0; im1<6; im1++) {
330         im2max=fNlad[im1];
331         fphidet[im1] = new Double_t[im2max];
332     } // end for im1
333     for(im1=0; im1<6; im1++) {
334         im2max=fNdet[im1];
335         for(im2=0; im2<im2max; im2++) {
336             fzmin[im1][im2]=cobj.fzmin[im1][im2];
337             fzmax[im1][im2]=cobj.fzmax[im1][im2];
338         } // end for im2
339     } // end for im1
340     for(im1=0; im1<6; im1++) {
341         im2max=fNlad[im1];
342         for(im2=0; im2<im2max; im2++) {
343             fphimin[im1][im2]=cobj.fphimin[im1][im2];
344             fphimax[im1][im2]=cobj.fphimax[im1][im2];
345             fphidet[im1][im2]=cobj.fphidet[im1][im2];  
346         } // end for im2
347     } // end for im2
348
349
350     AliRunLoader* rl = AliRunLoader::Open("galice.root");
351     rl->CdGAFile();
352     AliITSgeom* g1 = (AliITSgeom*)gDirectory->Get("AliITSgeom");
353
354    Int_t numOfModules = g1->GetIndexMax();
355         /*
356   fRecCylR = new Float_t *[numOfModules];
357   fRecCylPhi = new Float_t *[numOfModules]; 
358   fRecCylZ = new Float_t *[numOfModules];
359   */
360   fRecCylR = new Double_t *[numOfModules];
361   fRecCylPhi = new Double_t *[numOfModules]; 
362   fRecCylZ = new Double_t *[numOfModules];  
363   fNRecPoints = new Int_t[numOfModules];        
364                 for(Int_t module=0; module<numOfModules; module++) {            
365                   Int_t nRecPoints=fNRecPoints[module]=cobj.fNRecPoints[module];
366                   /*
367                   fRecCylR[module] = new Float_t[nRecPoints];
368                   fRecCylPhi[module] = new Float_t[nRecPoints];
369                   fRecCylZ[module] = new Float_t[nRecPoints];
370                   */
371                   fRecCylR[module] = new Double_t[nRecPoints];
372                   fRecCylPhi[module] = new Double_t[nRecPoints];
373                   fRecCylZ[module] = new Double_t[nRecPoints];            
374                   Int_t ind;    
375                   for(ind=0; ind<nRecPoints; ind++) {       
376                          fRecCylR[module][ind]=cobj.fRecCylR[module][ind];
377                          fRecCylPhi[module][ind]=cobj.fRecCylPhi[module][ind];
378                          fRecCylZ[module][ind]=cobj.fRecCylZ[module][ind];                       
379                   }             
380                 }        
381                 delete rl;
382  
383 }
384 /*
385 //______________________________________________________________________
386 void AliITSTrackerV1::DelMatrix(Int_t numOfModules) {
387   // cleanup of some data members
388   for(Int_t mod=0; mod<numOfModules; mod++) {
389     delete fRecCylR[mod];
390          delete fRecCylPhi[mod];
391          delete fRecCylZ[mod];
392   }
393     delete fRecCylR;
394          delete fRecCylPhi;
395          delete fRecCylZ;
396 }
397 */
398 //______________________________________________________________________
399 AliITSTrackerV1::~AliITSTrackerV1(){
400     // Origin  A. Badala' and G.S. Pappalardo:
401     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it  
402     // class destructor  
403   delete frl;
404   delete fNRecPoints;
405   for(Int_t i=0; i<6; i++) {
406     delete fzmin[i];
407          delete fzmax[i];
408          delete fphimin[i];
409          delete fphimax[i];
410          delete fphidet[i];
411   }
412
413   delete fzmin;
414   delete fzmax;
415   delete fphimin;
416   delete fphimax;
417   delete fphidet;
418          
419 }
420 //______________________________________________________________________
421 AliITSTrackerV1 &AliITSTrackerV1::operator=(const AliITSTrackerV1 &obj) {
422     // Origin  A. Badala' and G.S. Pappalardo:
423     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it  
424     // assignement operator
425
426   //     *fITS = *obj.fITS;
427     *fresult = *obj.fresult;
428     fPtref = obj.fPtref;
429     fChi2max = obj.fChi2max;      
430     **fvettid = **obj.fvettid;
431     fflagvert = obj.fflagvert;
432     Int_t imax=200,jmax=450;
433     frl = new AliITSRad(imax,jmax);      
434     *frl = *obj.frl;
435     fFieldFactor = obj.fFieldFactor;
436     Int_t i;
437     for(i=0; i<6; i++) {
438         fNlad[i] = obj.fNlad[i];
439         fNdet[i] = obj.fNdet[i]; 
440         fAvrad[i] = obj.fAvrad[i];
441         fDetx[i] = obj.fDetx[i];
442         fDetz[i] = obj.fDetz[i];
443     } // end for i
444     fzmin = new Double_t*[6];
445     fzmax = new Double_t*[6];
446     Int_t im1, im2, im2max;
447     for(im1=0; im1<6; im1++) {
448         im2max=fNdet[im1];
449         fzmin[im1] = new Double_t[im2max]; fzmax[im1] = new Double_t[im2max];
450     } // end for im1
451     fphimin = new Double_t*[6]; fphimax = new Double_t*[6];
452     for(im1=0;im1<6;im1++) {
453         im2max=fNlad[im1];
454         fphimin[im1] = new Double_t[im2max];
455         fphimax[im1] = new Double_t[im2max];
456     } // end for im1
457
458     fphidet = new Double_t*[6];
459     for(im1=0; im1<6; im1++) {
460         im2max=fNlad[im1];
461         fphidet[im1] = new Double_t[im2max];
462     } // end for im1
463     for(im1=0; im1<6; im1++) {
464         im2max=fNdet[im1];
465         for(im2=0; im2<im2max; im2++) {
466             fzmin[im1][im2]=obj.fzmin[im1][im2];
467             fzmax[im1][im2]=obj.fzmax[im1][im2];
468         } // end for im2
469     } // end for im1
470     for(im1=0; im1<6; im1++) {
471         im2max=fNlad[im1];
472         for(im2=0; im2<im2max; im2++) {
473             fphimin[im1][im2]=obj.fphimin[im1][im2];
474             fphimax[im1][im2]=obj.fphimax[im1][im2];
475             fphidet[im1][im2]=obj.fphidet[im1][im2];  
476         } // end for im2
477     } // end for im1
478
479     AliRunLoader* rl = AliRunLoader::Open("galice.root");
480     rl->CdGAFile();
481     AliITSgeom* g1 = (AliITSgeom*)gDirectory->Get("AliITSgeom");
482     //  AliITSgeom *g1 = fITS->GetITSgeom();  
483    Int_t numOfModules = g1->GetIndexMax();
484   fRecCylR = new Double_t *[numOfModules];
485   fRecCylPhi = new Double_t *[numOfModules]; 
486   fRecCylZ = new Double_t *[numOfModules];  
487   fNRecPoints = new Int_t[numOfModules];  
488           for(Int_t module=0; module<numOfModules; module++) {            
489                   Int_t nRecPoints=fNRecPoints[module]=obj.fNRecPoints[module];
490                   fRecCylR[module] = new Double_t[nRecPoints];
491                   fRecCylPhi[module] = new Double_t[nRecPoints];
492                   fRecCylZ[module] = new Double_t[nRecPoints];            
493                   Int_t ind;
494                   for(ind=0; ind<nRecPoints; ind++) {     
495                          fRecCylR[module][ind]=obj.fRecCylR[module][ind];
496                          fRecCylPhi[module][ind]=obj.fRecCylPhi[module][ind];
497                          fRecCylZ[module][ind]=obj.fRecCylZ[module][ind];                        
498                   }             
499                 }        
500          
501          
502           delete rl;
503     return *this;
504 }
505 //______________________________________________________________________
506 void AliITSTrackerV1::DoTracking(Int_t evNumber,Int_t minTr,Int_t maxTr,
507                                  TFile *file, Bool_t realmass) {
508     // Origin   A. Badala' and G.S. Pappalardo:
509     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
510     // The method needs the event number, the minimum and maximum order
511     // number of TPC tracks that 
512     // are to be tracked trough the ITS, and the file where the recpoints
513     // are registered.
514     // The method can be called by a macro. It preforms the tracking for
515     // all good TPC tracks
516
517     printf("begin DoTracking - file %p\n",(void*)file);
518
519     gAlice->GetEvent(evNumber);  //modificato per gestire hbt
520  
521     AliKalmanTrack::SetFieldMap(gAlice->Field());
522    // cout<<" field = "<<gAlice->Field()->SolenoidField()<<endl;
523
524
525     TFile *cf=TFile::Open("AliTPCclusters.root");  
526     AliTPCParam *digp= (AliTPCParam*)cf->Get("75x40_100x60_150x60");
527     if (!digp) { cerr<<"TPC parameters have not been found !\n"; getchar();}
528
529     cf->cd();
530     AliRunLoader* rl = AliRunLoader::Open("galice.root");
531     AliITSLoader* itsl = (AliITSLoader*)rl->GetLoader("ITSLoader");
532     TString foldname(itsl->GetEventFolder()->GetName());
533     //TString foldname(fITS->GetLoader()->GetEventFolder()->GetName());
534     delete rl;   
535     printf("This method is not converted to the NewIO !\n");  //I.B.
536     return; //I.B.
537     AliTPCtracker *tracker = new AliTPCtracker(digp);            //I.B.
538     //PH    tracker->SetEventNumber(evNumber);                           //I.B.
539
540     // Load clusters
541     printf("This method is not converted to the NewIO !\n");  //I.B.
542     return; //I.B.
543     tracker->LoadClusters(0);       //I.B.
544
545     // Load tracks
546     TFile *tf=TFile::Open("AliTPCtracksSorted.root");  
547     if (!tf->IsOpen()) {
548         cerr<<"Can't open AliTPCtracksSorted.root !\n";
549         return ;
550     } // end if
551     TObjArray tracks(200000);
552     char tname[100];                 
553     sprintf(tname,"TreeT_TPC_%d",evNumber);
554
555     TTree *tracktree=(TTree*)tf->Get(tname);   
556     if (!tracktree) {cerr<<"Can't get a tree with TPC tracks !\n";}   
557     TBranch *tbranch=tracktree->GetBranch("tracks");
558     Int_t nentr=(Int_t)tracktree->GetEntries();
559     Int_t kk;
560
561     AliITSRecPoint *recp;    // oggi
562         AliTPCtrack *ioTrackTPC=0;    
563     for (kk=0; kk<nentr; kk++) {
564         ioTrackTPC=new AliTPCtrack; 
565         tbranch->SetAddress(&ioTrackTPC);
566         tracktree->GetEvent(kk);    
567         tracker->CookLabel(ioTrackTPC,0.1);       
568         tracks.AddLast(ioTrackTPC);         
569     } // end for kk
570     delete tracker;      
571     tf->Close();
572
573     Int_t nt = tracks.GetEntriesFast();
574     cerr<<"Number of found tracks "<<nt<<endl;
575
576     TVector vec(5);
577     TTree *tr=gAlice->TreeR();
578     Int_t nent=(Int_t)tr->GetEntries();  
579     frecPoints = fDetTypeRec->RecPoints();
580
581     Int_t numbpoints;
582     Int_t totalpoints=0;
583     Int_t *np = new Int_t[nent];
584     fvettid = new Int_t* [nent];
585     Int_t mod;
586
587     for (mod=0; mod<nent; mod++) {
588         fvettid[mod]=0;
589         fDetTypeRec->ResetRecPoints();  
590         gAlice->TreeR()->GetEvent(mod); 
591         numbpoints = frecPoints->GetEntries();
592         totalpoints+=numbpoints;
593         np[mod] = numbpoints;
594         //cout<<" mod = "<<mod<<"   numbpoints = "<<numbpoints<<"\n";getchar();
595         fvettid[mod] = new Int_t[numbpoints];
596         Int_t ii;
597         for (ii=0;ii<numbpoints; ii++) *(fvettid[mod]+ii)=0;
598     } // end for mod
599
600     AliTPCtrack *track=0;
601
602     if(minTr < 0) {minTr = 0; maxTr = nt-1;}   
603
604     TVector vgeant(3);
605
606     TTree tracktree1("TreeT","Tree with ITS tracks");
607     AliITSIOTrack *ioTrack=0;
608         AliITSPid *pid=new AliITSPid(1000);  // oggi
609
610     tracktree1.Branch("ITStracks","AliITSIOTrack",&ioTrack,32000,0);
611   
612     TDatabasePDG * db = new TDatabasePDG;   
613   
614     Int_t j;       
615     for (j=minTr; j<=maxTr; j++) {     
616         track=(AliTPCtrack*)tracks.UncheckedAt(j);
617         if (!track) continue;
618         
619         ///   mass definition ////////////////////////
620     Double_t mass=0.13956995;
621          Int_t pcode=211;  // a pion by default
622          
623          if(realmass) {
624         if(TMath::Abs(pcode)<20443) mass=db->GetParticle(pcode)->Mass();
625          }
626          else {
627                  mass = track->GetMass();
628 //               cout << "Mass = " << mass << endl;
629          }
630          
631         
632         
633          //   new propagation to the end of TPC
634     Double_t xk=80.;
635    // track->PropagateTo(xk,0.,0.); //Ne if it's still there   //attenzione funziona solo se modifica in TPC
636         // Double_t xk=77.415;   
637          track->PropagateTo(xk, 28.94, 1.204e-3);
638     xk-=0.005;
639     track->PropagateTo(xk, 44.77,1.71); //Tedlar         
640     xk-=0.02;
641     track->PropagateTo(xk, 44.86, 1.45);   //Kevlar
642     xk-=2.0;
643     track->PropagateTo(xk, 41.28, 0.029);//Nomex
644     xk-=0.02;
645     track->PropagateTo(xk, 44.86, 1.45);   //Kevlar
646     xk-=0.005;
647     track->PropagateTo(xk, 44.77, 1.71); //Tedlar
648
649     xk=61.;
650    // track->PropagateTo(xk,0.,0.); //C02
651          track->PropagateTo(xk,36.2,1.98e-3); //C02        //attenzione funziona solo se modifica in TPC
652
653     xk -=0.005;
654     track->PropagateTo(xk, 24.01, 2.7);    //Al    
655     xk -=0.005;
656     track->PropagateTo(xk, 44.77, 1.71);  //Tedlar
657     xk -=0.02;
658     track->PropagateTo(xk, 44.86, 1.45);    //Kevlar
659     xk -=0.5;
660     track->PropagateTo(xk, 41.28, 0.029);  //Nomex    
661     xk -=0.02;
662     track->PropagateTo(xk, 44.86, 1.45);    //Kevlar
663     xk -=0.005;
664     track->PropagateTo(xk, 44.77, 1.71);  //Tedlar
665     xk -=0.005;
666     track->PropagateTo(xk, 24.01, 2.7);    //Al 
667     
668         ////////////////////////////////////////////////////////////////////////////////////////////////////////        
669         //AliITSTrackV1 trackITS(*track);
670         AliITSTrackV1 trackITS(*track, fFieldFactor);
671         //cout<<" fFieldFactor = "<<fFieldFactor<<"\n";      
672         trackITS.PutMass(mass);   //new to add mass to track
673         if(fresult){ delete fresult; fresult=0;}         
674         fresult = new AliITSTrackV1(trackITS);   
675
676         AliITSTrackV1 primaryTrack(trackITS);
677         vgeant=(*fresult).GetVertex();
678
679         // Definition of dv and zv for vertex constraint        
680         Double_t sigmaDv=0.0050;  Double_t sigmaZv=0.010;       
681         //Double_t sigmaDv=0.0015;  Double_t sigmaZv=0.0015;
682         Double_t uniform= gRandom->Uniform();
683         Double_t signdv;
684         if(uniform<=0.5) signdv=-1.;
685         else
686             signdv=1.;
687
688         Double_t vr=TMath::Sqrt(vgeant(0)*vgeant(0)+ vgeant(1)*vgeant(1));
689         Double_t dv=gRandom->Gaus(signdv*vr,(Float_t)sigmaDv); 
690         Double_t zv=gRandom->Gaus(vgeant(2),(Float_t)sigmaZv);          
691         //cout<<" Dv e Zv = "<<dv<<" "<<zv<<"\n";
692         trackITS.SetDv(dv);
693         trackITS.SetZv(zv);
694         trackITS.SetsigmaDv(sigmaDv);
695         trackITS.SetsigmaZv(sigmaZv); 
696         (*fresult).SetDv(dv);
697         (*fresult).SetZv(zv);
698         (*fresult).SetsigmaDv(sigmaDv);
699         (*fresult).SetsigmaZv(sigmaZv);
700         primaryTrack.SetDv(dv);
701         primaryTrack.SetZv(zv);
702         primaryTrack.SetsigmaDv(sigmaDv);
703         primaryTrack.SetsigmaZv(sigmaZv);
704         primaryTrack.PrimaryTrack();
705         TVector  d2=primaryTrack.Getd2();
706         TVector  tgl2=primaryTrack.Gettgl2();
707         TVector  dtgl=primaryTrack.Getdtgl();
708         trackITS.Setd2(d2); trackITS.Settgl2(tgl2);
709         trackITS.Setdtgl(dtgl); 
710         (*fresult).Setd2(d2); (*fresult).Settgl2(tgl2);
711         (*fresult).Setdtgl(dtgl);
712         /*
713           trackITS.SetVertex(vertex); trackITS.SetErrorVertex(ervertex);
714           (*result).SetVertex(vertex);   (*result).SetErrorVertex(ervertex);   
715         */
716         TList *list= new TList();
717
718         list->AddLast(&trackITS);
719   
720         fPtref=TMath::Abs( (trackITS).GetPt() );
721         //cout<<" fPtref = " <<fPtref<<"\n";
722         if(fPtref>1.0) fChi2max=40.;         
723         if(fPtref<=1.0) fChi2max=20.;
724         if(fPtref<0.4 ) fChi2max=100.;
725         if(fPtref<0.2 ) fChi2max=40.;             
726         // if(fPtref<0.4 ) fChi2max=30.;                                 
727         // if(fPtref<0.2 ) fChi2max=20.;
728         //if(fPtref<0.2 ) fChi2max=10.;
729         //if(fPtref<0.1 ) fChi2max=5.;
730         //cout << "\n Pt = " << fPtref <<"\n";  //stampa
731         RecursiveTracking(list);   
732         list->Delete();
733         delete list;
734
735         Int_t itot=-1;
736         TVector vecTotLabRef(18);
737         Int_t lay, k;
738         for(lay=5; lay>=0; lay--) {
739             TVector vecLabRef(3); 
740             vecLabRef=(*fresult).GetLabTrack(lay);
741             Float_t clustZ=(*fresult).GetZclusterTrack( lay);   
742             for(k=0; k<3; k++){  
743                 Int_t lpp=(Int_t)vecLabRef(k);
744                 if(lpp>=0) {
745                     TParticle *p=(TParticle*) gAlice->GetMCApp()->Particle(lpp);
746                     Int_t pcode=p->GetPdgCode();
747                     if(pcode==11) vecLabRef(k)=p->GetFirstMother();
748                 } // end if
749                 itot++; vecTotLabRef(itot)=vecLabRef(k);
750                 if(vecLabRef(k)==0. && clustZ == -1.) vecTotLabRef(itot) =-3.;
751             } // end for k
752         } // end for lay
753         Long_t labref;
754         Int_t freq;  
755         (*fresult).Search(vecTotLabRef, labref, freq);
756
757         //if(freq < 6) labref=-labref;        // cinque - sei
758         if(freq < 5) labref=-labref;        // cinque - sei     
759         (*fresult).SetLabel(labref);
760
761         // cout<<" progressive track number = "<<j<<"\r";
762         // cout<<j<<"\r";
763         Int_t numOfCluster=(*fresult).GetNumClust();  
764         //cout<<" progressive track number = "<<j<<"\n";    // stampa
765         Long_t labITS=(*fresult).GetLabel();
766         //cout << " ITS track label = " << labITS << "\n";      // stampa           
767         Int_t lab=track->GetLabel();                
768         //cout << " TPC track label = " << lab <<"\n";      // stampa
769         //propagation to vertex
770
771         Double_t rbeam=3.;
772         if((*fresult).DoNotCross(rbeam)) continue;  //no intersection with beampipe     
773         (*fresult).Propagation(rbeam);
774         Double_t c00,c10,c11,c20,c21,c22,c30,c31,c32,c33,c40,c41,c42,c43,c44;
775         (*fresult).GetCElements(c00,
776                                 c10,c11,
777                                 c20,c21,c22,
778                                 c30,c31,c32,c33,
779                                 c40,c41,c42,c43,c44);
780                  
781         Double_t pt=TMath::Abs((*fresult).GetPt());
782         Double_t dr=(*fresult).GetD();
783         Double_t z=(*fresult).GetZ();
784         Double_t tgl=(*fresult).GetTgl();
785         Double_t c=(*fresult).GetC();
786         Double_t cy=c/2.;
787         Double_t dz=z-(tgl/cy)*TMath::ASin((*fresult).Arga(rbeam));
788         dz-=vgeant(2);
789         // cout<<" dr e dz alla fine = "<<dr<<" "<<dz<<"\n"; getchar();
790         Double_t phi=(*fresult).Getphi();
791         Double_t phivertex = phi - TMath::ASin((*fresult).ArgA(rbeam));
792         Double_t duepi=2.*TMath::Pi();   
793         if(phivertex>duepi) phivertex-=duepi;
794         if(phivertex<0.) phivertex+=duepi;
795         /////////////////////////////////////////////////////////////
796         Int_t idmodule,idpoint;
797         if(numOfCluster >=5)  {            // cinque - sei
798             //if(numOfCluster ==6)  {            // cinque - sei
799             AliITSIOTrack outTrack;
800             ioTrack=&outTrack;
801             ioTrack->SetStatePhi(phi);
802             ioTrack->SetStateZ(z);
803             ioTrack->SetStateD(dr);
804             ioTrack->SetStateTgl(tgl);
805             ioTrack->SetStateC(c);
806             Double_t radius=(*fresult).Getrtrack();
807             ioTrack->SetRadius(radius);
808             Int_t charge;
809             if(c>0.) charge=-1;  else charge=1;
810             ioTrack->SetCharge(charge);
811                 Double_t trackmass=(*fresult).GetMass();         // oggi
812                 ioTrack->SetMass(trackmass);                     // oggi
813             ioTrack->SetCovMatrix(c00,
814                                   c10,c11,
815                                   c20,c21,c22,
816                                   c30,c31,c32,c33,
817                                   c40,c41,c42,c43,c44);
818             Double_t px=pt*TMath::Cos(phivertex);
819             Double_t py=pt*TMath::Sin(phivertex);
820             Double_t pz=pt*tgl;
821             Double_t xtrack=dr*TMath::Sin(phivertex);
822             Double_t ytrack=dr*TMath::Cos(phivertex);
823             Double_t ztrack=dz+vgeant(2);
824             ioTrack->SetPx(px);
825             ioTrack->SetPy(py);
826             ioTrack->SetPz(pz);
827             ioTrack->SetX(xtrack);
828             ioTrack->SetY(ytrack);
829             ioTrack->SetZ(ztrack);
830             ioTrack->SetLabel(labITS);
831             ioTrack->SetTPCLabel(lab);
832                  ioTrack->SetDz(dz);
833             Int_t il;           
834             /*
835                 for(il=0;il<6; il++){
836                 ioTrack->SetIdPoint(il,(*fresult).GetIdPoint(il));
837                 ioTrack->SetIdModule(il,(*fresult).GetIdModule(il));
838             } // end for il
839             */
840             //tracktree1.Fill();
841                 Float_t q[4]={-1.,-1.,-1.,-1.};
842             Float_t  globaldedx=0.;        
843             for (il=0;il<6;il++) {
844                 idpoint=(*fresult).GetIdPoint(il);
845                 idmodule=(*fresult).GetIdModule(il);
846                 if(idmodule>0.) *(fvettid[idmodule]+idpoint)=1;
847                                                                
848                 ioTrack->SetIdPoint(il,idpoint);
849                 ioTrack->SetIdModule(il,idmodule);
850                 ////  for q definition
851                 if(il>1){
852                   if(idmodule>0.){                      
853                     fDetTypeRec->ResetRecPoints();
854                     gAlice->TreeR()->GetEvent(idmodule);
855                     recp=(AliITSRecPoint*)frecPoints->UncheckedAt(idpoint);
856                     q[il-2]=recp->GetQ()*(*fresult).Getfcor(il-2);
857                   }
858                 }                               
859             } // end for il      
860                 q[0]/=280.; q[1]/=280.;
861             q[2]/=38.; q[3]/=38.;
862
863  // cout<<" q prima = "<<q[0]<<" "<<q[1]<<" "<<q[2]<<" "<<q[3]<<"\n"; getchar(); 
864      
865      Int_t swap;
866   do{
867     swap=0;   
868     for (il=0; il<3; il++) {
869       if (q[il]<=q[il+1]) continue;
870       Float_t tmp=q[il];
871       q[il]=q[il+1]; q[il+1]=tmp;
872       swap++;
873     }
874   } while(swap); 
875
876  
877   // cout<<" q dopo = "<<q[0]<<" "<<q[1]<<" "<<q[2]<<" "<<q[3]<<"\n"; getchar();
878       
879     if(q[0]<0.) {
880       q[0]=q[1];
881       q[1]=q[2];
882       q[2]=q[3];
883       q[3]=-1.;             
884     } 
885  
886   // cout<<" q  dopo if = "<<q[0]<<" "<<q[1]<<" "<<q[2]<<" "<<q[3]<<"\n"; getchar(); 
887      
888     globaldedx=(q[0]+q[1])/2.;
889     
890    // if(q[3]> 0.) globaldedx=(q[0]+q[1]+q[2]+q[3])/4.;
891    //      else    globaldedx=(q[0]+q[1]+q[2])/3.; 
892    
893     ioTrack->SetdEdx(globaldedx);
894     ioTrack->SetPid(pid->GetPcode(ioTrack));
895             
896             tracktree1.Fill();         
897         } // end if on numOfCluster
898         //gObjectTable->Print();    // stampa memoria     
899     }  //  end for (int j=minTr; j<=maxTr; j++)
900     delete db;           
901     static Bool_t first=kTRUE;
902     static TFile *tfile;
903     if(first) {
904         tfile=new TFile("itstracks.root","RECREATE");
905         //cout<<"I have opened itstracks.root file "<<endl;
906     } // end if     
907     first=kFALSE;
908     tfile->cd();
909     tfile->ls();
910     char hname[30];
911     sprintf(hname,"TreeT%d",evNumber);
912         cout << "Number of saved ITS tracks " << tracktree1.GetEntries() << endl;
913     tracktree1.Write(hname);
914   
915     TTree *fAli=gAlice->TreeK();
916     TFile *fileAli=0;
917     if (fAli) fileAli =fAli->GetCurrentFile();
918     fileAli->cd();
919     ////////////////////////////////////////////////////////////////////
920
921     printf("delete vectors\n");
922     if(np) delete [] np;
923     if(fvettid) delete [] fvettid;
924     if(fresult) {delete fresult; fresult=0;}
925 }
926 //______________________________________________________________________
927 void AliITSTrackerV1::RecursiveTracking(TList *trackITSlist) {
928     //   This function perform the recursive tracking in ITS detectors
929     // reference is a pointer to the final best track
930     // Origin  A. Badala' and G.S. Pappalardo:
931     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
932     // The authors thank Mariana Bondila to have help them to resolve some
933     // problems.  July-2000
934
935     //Rlayer[0]=4.; Rlayer[1]=7.;  Rlayer[2]=14.9;
936     // Rlayer[3]=23.8;  Rlayer[4]=39.1;  Rlayer[5]=43.6; //vecchio
937
938     ////////////////////// 
939     Float_t sigmaphil[6], sigmazl[6];
940     sigmaphil[0]=1.44e-6/(fAvrad[0]*fAvrad[0]);
941     sigmaphil[1]=1.44e-6/(fAvrad[1]*fAvrad[1]);
942     sigmaphil[2]=1.444e-5/(fAvrad[2]*fAvrad[2]);
943     sigmaphil[3]=1.444e-5/(fAvrad[3]*fAvrad[3]);
944     sigmaphil[4]=4e-6/(fAvrad[4]*fAvrad[4]);
945     sigmaphil[5]=4e-6/(fAvrad[5]*fAvrad[5]);
946     sigmazl[0]=1e-2;
947     sigmazl[1]=1e-2;
948     sigmazl[2]=7.84e-4;
949     sigmazl[3]=7.84e-4;
950     sigmazl[4]=0.6889;
951     sigmazl[5]=0.6889;  
952     ///////////////////////////////////////////////////////////
953     Int_t index; 
954     AliRunLoader* rl = AliRunLoader::Open("galice.root");
955     rl->CdGAFile();
956     AliITSgeom* g1 = (AliITSgeom*)gDirectory->Get("AliITSgeom");
957
958     AliITSRecPoint *recp;        
959     for(index =0; index<trackITSlist->GetSize(); index++) {
960         AliITSTrackV1 *trackITS = (AliITSTrackV1 *) trackITSlist->At(index);
961         if((*trackITS).GetLayer()==7) fresult->SetChi2(10.223e140);
962         // cout <<" Layer inizio = "<<(*trackITS).GetLayer()<<"\n";
963         //  cout<<"fvtrack =" <<"\n";
964         //  cout << (*trackITS)(0) << " "<<(*trackITS)(1)<<" "
965         //       <<(*trackITS)(2)<<" "<<(*trackITS)(3)<<" "
966         //       <<(*trackITS)(4)<<"\n";
967         //  cout<< " rtrack = "<<(*trackITS).Getrtrack()<<"\n";
968         //  cout<< " Pt = "<<(*trackITS).GetPt()<<"\n";
969         //  getchar();    
970         Double_t chi2Now, chi2Ref;
971         Float_t numClustRef = fresult->GetNumClust();            
972         if((*trackITS).GetLayer()==1 ) {
973             chi2Now = trackITS->GetChi2();
974             Float_t numClustNow = trackITS->GetNumClust();
975             if(trackITS->GetNumClust()) 
976                 chi2Now /= (Double_t)trackITS->GetNumClust();
977             chi2Ref = fresult->GetChi2();
978             if(fresult->GetNumClust()) 
979                 chi2Ref /= (Double_t)fresult->GetNumClust();
980             //cout<<" chi2Now and chi2Ref = "<<chi2Now<<" "<<chi2Ref<<"\n";
981             if( numClustNow > numClustRef ) {*fresult = *trackITS;} 
982             if((numClustNow == numClustRef )&& 
983                (chi2Now < chi2Ref))  {
984                 *fresult = *trackITS;
985             } // end if
986             continue;   
987         } // end if
988
989         if(trackITS->Getfnoclust()>=2)  continue;      
990         Float_t numClustNow = trackITS->GetNumClust();
991         if(numClustNow) { 
992             chi2Now = trackITS->GetChi2();
993
994             if(numClustNow<numClustRef && chi2Now>fresult->GetChi2()) continue;
995             //cout<<" chi2Now =  "<<chi2Now<<"\n";   
996               
997             chi2Now/=numClustNow;
998             if(fPtref > 1.0 && chi2Now > 30.) continue; 
999             if((fPtref >= 0.6 && fPtref<=1.0) && chi2Now > 40.) continue;
1000             // if((fPtref <= 0.6 && fPtref>0.2)&& chi2Now > 40.) continue;
1001             // if(fPtref <= 0.2 && chi2Now > 8.) continue;
1002             if((fPtref <= 0.6 && fPtref>0.2)&& chi2Now > 30.) continue;
1003             if(fPtref <= 0.2 && chi2Now > 7.) continue;     
1004             /////////////////////////////
1005         } // end if
1006
1007         Int_t layerInit = (*trackITS).GetLayer();
1008         Int_t layernew = layerInit - 2;// -1 for new layer, -1 for matrix index
1009         TList listoftrack;
1010         Int_t ladp, ladm, detp,detm,ladinters,detinters;        
1011         Int_t layerfin=layerInit-1;
1012         // cout<<"Prima di intersection \n";
1013         Int_t  outinters=Intersection(*trackITS,layerfin,ladinters,detinters);
1014         // cout<<" outinters = "<<outinters<<"\n";
1015         //  cout<<" Layer ladder detector intersection ="
1016         //      <<layerfin<<" "<<ladinters<<" "<<detinters<<"\n";
1017         //  cout << " phiinters zinters = "<<(*trackITS)(0) 
1018         //       << " "<<(*trackITS)(1)<<"\n"; getchar();
1019         if(outinters==-1) continue;
1020         Int_t flaghit=0;
1021         (*trackITS).SetLayer(layerfin);  // oggi
1022         (*trackITS).Setfcor();           // oggi
1023         if(outinters==0){
1024             TVector toucLad(9), toucDet(9);      
1025             Int_t lycur=layerfin;
1026             ladp=ladinters+1;
1027             ladm=ladinters-1;
1028             if(ladm <= 0) ladm=fNlad[layerfin-1];    
1029             if(ladp > fNlad[layerfin-1]) ladp=1;  
1030             detp=detinters+1;
1031             detm=detinters-1;
1032             Int_t idetot=1;
1033             /*
1034               toucLad(0)=ladinters; toucLad(1)=ladm; toucLad(2)=ladp;
1035               toucLad(3)=ladinters; toucLad(4)=ladm; toucLad(5)=ladp;
1036               toucLad(6)=ladinters; toucLad(7)=ladm; toucLad(8)=ladp;
1037               toucDet(0)=detinters; toucDet(1)=detinters; toucDet(2)=detinters;
1038               if(detm > 0 && detp <= fNdet[layerfin-1]) {     
1039                    idetot=9;
1040                    toucDet(3)=detm; toucDet(4)=detm; toucDet(5)=detm;      
1041                    toucDet(6)=detp; toucDet(7)=detp; toucDet(8)=detp;
1042               } // end if
1043               if(detm > 0 && detp > fNdet[layerfin-1]) {   
1044                    idetot=6;
1045                    toucDet(3)=detm; toucDet(4)=detm; toucDet(5)=detm;
1046              } // end if
1047              if(detm <= 0 && detp <= fNdet[layerfin-1]) {   
1048                   idetot=6;
1049                   toucDet(3)=detp; toucDet(4)=detp; toucDet(5)=detp;
1050              } // end if
1051             */
1052             Float_t epsphi=5.0, epsz=5.0;                  
1053             if(fPtref<0.2) {epsphi=3.; epsz=3.;}     
1054             // new definition of idetot e toucLad e toucDet to be
1055             // transformed in a method
1056             // these values could be modified
1057             Float_t pigre=TMath::Pi();
1058             Float_t rangephi=5., rangez=5.;
1059             if(layerfin==1 || layerfin ==2){
1060                 rangephi=40.*epsphi*TMath::Sqrt(sigmaphil[layerfin-1]+
1061                                                 (*trackITS).GetSigmaphi());
1062                 rangez = 40.*epsz*TMath::Sqrt(sigmazl[layerfin-1]+
1063                                               (*trackITS).GetSigmaZ());
1064             } // end if
1065             if(layerfin==3 || layerfin ==4){
1066                 //rangephi=30.*fepsphi*TMath::Sqrt(sigmaphil[layerfin-1]+
1067                 //                                 (*trackITS).GetSigmaphi());
1068                 //rangez = 40.*fepsz*TMath::Sqrt(sigmazl[layerfin-1]+
1069                 //                               (*trackITS).GetSigmaZ());
1070                 rangephi=40.*epsphi*TMath::Sqrt(sigmaphil[layerfin-1]+
1071                                                 (*trackITS).GetSigmaphi());
1072                 rangez = 50.*epsz*TMath::Sqrt(sigmazl[layerfin-1]+
1073                                               (*trackITS).GetSigmaZ());
1074             } // end if
1075             if(layerfin==5 || layerfin ==6){
1076                 rangephi=20.*epsphi*TMath::Sqrt(sigmaphil[layerfin-1]+
1077                                                 (*trackITS).GetSigmaphi());
1078                 rangez =5.*epsz*TMath::Sqrt(sigmazl[layerfin-1]+
1079                                             (*trackITS).GetSigmaZ());
1080             } // end if
1081             Float_t phinters, zinters;
1082             phinters=(*trackITS).Getphi();
1083             zinters=(*trackITS).GetZ();
1084             Float_t distz = 0.0;
1085             Float_t phicm, phicp, distphim, distphip;
1086             phicm=phinters;
1087                  if(phinters>fphimax[layerfin-1][ladm-1]) phicm=phinters-2*pigre;  //corretto il 20-11-2001
1088                  distphim=TMath::Abs(phicm-fphimax[layerfin-1][ladm-1]);  //corretto il 20-11-2001
1089             phicp=phinters;
1090                  //cout<<" fNlad[layerfin-1] e ladp = "<<fNlad[layerfin-1]<<" "<<ladp<<endl;
1091                  if(phinters>fphimin[layerfin-1][ladp-1]) phicp=phinters-2.*pigre;   //corretto il 20-11-2001
1092                  distphip=TMath::Abs(phicp-fphimin[layerfin-1][ladp-1]);      //corretto il 20-11-2001
1093             Int_t flagzmin=0;
1094             Int_t flagzmax=0;
1095             idetot=1;
1096             toucLad(0)=ladinters; toucDet(0)=detinters;
1097             if(detm>0) distz=TMath::Abs(zinters-fzmax[layerfin-1][detm-1]);
1098             if(detm>0 && rangez>=distz){
1099                 flagzmin=1; 
1100                 idetot++; toucLad(idetot-1)=ladinters; toucDet(idetot-1)=detm;
1101                 if(rangephi>=distphim){
1102                     idetot++; 
1103                     toucLad(idetot-1)=ladm;
1104                     toucDet(idetot-1)=detinters;
1105                     idetot++;
1106                     toucLad(idetot-1)=ladm;
1107                     toucDet(idetot-1)=detm;
1108                 } // end if
1109                 if(rangephi>=distphip){
1110                     idetot++;
1111                     toucLad(idetot-1)=ladp;
1112                     toucDet(idetot-1)=detinters;
1113                     idetot++;
1114                     toucLad(idetot-1)=ladp;
1115                     toucDet(idetot-1)=detm;
1116                 } // end if
1117             }  //end detm>0....          
1118             if(detp<=fNdet[layerfin-1]) 
1119                 distz=TMath::Abs(zinters-fzmin[layerfin-1][detp-1]);
1120             if(detp<=fNdet[layerfin-1] && rangez>=distz){
1121                 flagzmax=1;
1122                 idetot++; toucLad(idetot-1)=ladinters; toucDet(idetot-1)=detp;
1123                 if(rangephi>=distphim){
1124                     idetot++; toucLad(idetot-1)=ladm; toucDet(idetot-1)=detp;
1125                     if(flagzmin == 0) {
1126                         idetot++;
1127                         toucLad(idetot-1)=ladm;
1128                         toucDet(idetot-1)=detinters;
1129                     } // end if
1130                 } // end if
1131                 if(rangephi>=distphip){
1132                     idetot++;
1133                     toucLad(idetot-1)=ladp;
1134                     toucDet(idetot-1)=detp;
1135                     if(flagzmin == 0) {
1136                         idetot++;
1137                         toucLad(idetot-1)=ladp; 
1138                         toucDet(idetot-1)=detinters;
1139                     } // end if
1140                 } // end if
1141             }  //end detm<fNdet[.......
1142
1143             if(flagzmin == 0 && flagzmax==0){
1144                 if(rangephi>=distphim){
1145                     idetot++; 
1146                     toucLad(idetot-1)=ladm;
1147                     toucDet(idetot-1)=detinters;
1148                 } // end if     
1149                 if(rangephi>=distphip){
1150                     idetot++; 
1151                     toucLad(idetot-1)=ladp;
1152                     toucDet(idetot-1)=detinters;
1153                 } // end if       
1154             } // end if
1155             ////////////////////////////////////////////////////////////
1156             Int_t iriv;
1157             for (iriv=0; iriv<idetot; iriv++) {  //for on detectors
1158                 ///////////////////////////////////////////////////////
1159                 /*** Rec points sorted by module *****/
1160                 /**************************************/
1161                 Int_t indexmod;       
1162                 indexmod = g1->GetModuleIndex(lycur,(Int_t)toucLad(iriv),
1163                                               (Int_t)toucDet(iriv)); 
1164                 fDetTypeRec->ResetRecPoints();   
1165                 gAlice->TreeR()->GetEvent(indexmod); 
1166                 Int_t npoints=frecPoints->GetEntries();
1167        
1168                 Int_t indnew;
1169                 for(indnew=0; indnew<npoints; indnew++){
1170                     if (*(fvettid[indexmod]+indnew)==0)
1171                         recp =(AliITSRecPoint*)frecPoints->UncheckedAt(indnew);
1172                     else
1173                         continue;
1174                     TVector cluster(3),vecclust(9);
1175                     //vecclust(6)=vecclust(7)=vecclust(8)=-1.;
1176                     Double_t sigma[2];
1177   // now vecclust is with cylindrical cohordinates
1178                vecclust(0)=(Float_t)fRecCylR[indexmod][indnew];     
1179                vecclust(1)=(Float_t)fRecCylPhi[indexmod][indnew];
1180                vecclust(2)=(Float_t)fRecCylZ[indexmod][indnew];                          
1181                     vecclust(3) = (Float_t)recp->fTracks[0]; 
1182                     vecclust(4) = (Float_t)indnew;                       
1183                     vecclust(5) = (Float_t)indexmod;    
1184                     vecclust(6) = (Float_t)recp->fTracks[0];
1185                     vecclust(7) = (Float_t)recp->fTracks[1];
1186                     vecclust(8) = (Float_t)recp->fTracks[2];
1187                     sigma[0] = (Double_t)  recp->GetSigmaX2();     
1188                     sigma[1] = (Double_t) recp->GetSigmaZ2();
1189                          
1190                          cluster(0)=fRecCylR[indexmod][indnew];
1191           cluster(1)=fRecCylPhi[indexmod][indnew];
1192                          cluster(2)=fRecCylZ[indexmod][indnew];
1193                          
1194                     // cout<<" layer = "<<play<<"\n";
1195                     // cout<<" cluster prima = "<<vecclust(0)<<" "
1196                     //     <<vecclust(1)<<" "
1197                     //     <<vecclust(2)<<"\n"; getchar();    
1198
1199                     Float_t sigmatotphi, sigmatotz;  
1200                     // Float_t epsphi=5.0, epsz=5.0;                 
1201                     //if(fPtref<0.2) {epsphi=3.; epsz=3.;}
1202                     Double_t rTrack=(*trackITS).Getrtrack();
1203                     Double_t sigmaphi=sigma[0]/(rTrack*rTrack);
1204                     sigmatotphi=epsphi*TMath::Sqrt(sigmaphi + 
1205                                                    (*trackITS).GetSigmaphi());
1206                     sigmatotz=epsz*TMath::Sqrt(sigma[1] + 
1207                                                (*trackITS).GetSigmaZ());
1208                     //cout<<"cluster e sigmatotphi e track = "<<cluster(0)
1209                     //    <<" "<<cluster(1)<<" "<<sigmatotphi<<" "
1210                     //    <<vecclust(3)<<"\n";
1211                     //if(vecclust(3)==481) getchar();
1212                     if(cluster(1)<6. && (*trackITS).Getphi()>6.) 
1213                         cluster(1)=cluster(1)+(2.*TMath::Pi());
1214                     if(cluster(1)>6. && (*trackITS).Getphi()<6.) 
1215                         cluster(1)=cluster(1)-(2.*TMath::Pi());
1216                     if(TMath::Abs(cluster(1)-(*trackITS).Getphi())>sigmatotphi)
1217                         continue;
1218                     // cout<<" supero sigmaphi \n";      
1219                     AliITSTrackV1 *newTrack = new AliITSTrackV1((*trackITS));
1220                     //(*newTrack).SetLayer((*trackITS).GetLayer()-1);
1221                     if (TMath::Abs(rTrack-cluster(0))/rTrack>1e-6) 
1222                         (*newTrack).Correct(Double_t(cluster(0)));      
1223                     //cout<<" cluster(2) e(*newTrack).GetZ()="<<cluster(2)<<" "
1224                     //    << (*newTrack).GetZ()<<"\n";
1225                     if(TMath::Abs(cluster(2)-(*newTrack).GetZ()) > sigmatotz){ 
1226                         delete newTrack;
1227                         continue;
1228                     } // end if
1229                     Double_t sigmanew[2];
1230                     sigmanew[0]= sigmaphi;
1231                     sigmanew[1]=sigma[1];
1232                     Double_t m[2];
1233                     m[0]=cluster(1);
1234                     m[1]=cluster(2);
1235                     //  Double_t chi2pred=newTrack->GetPredChi2(m,sigmanew);   
1236                     // cout<<" chi2pred = "<<chi2pred<<"\n";
1237                     // if(chi2pred>fChi2max) continue; //aggiunto il 30-7-2001
1238                     if(iriv == 0) flaghit=1;
1239                     (*newTrack).AddMS();  // add the multiple scattering 
1240                                              //matrix to the covariance matrix 
1241                     (*newTrack).AddEL(1.,0);
1242
1243                     if(fflagvert){
1244                         KalmanFilterVert(newTrack,cluster,sigmanew);
1245                         //KalmanFilterVert(newTrack,cluster,sigmanew,chi2pred);
1246                     }else{
1247                         KalmanFilter(newTrack,cluster,sigmanew);
1248                     } // end if
1249                     (*newTrack).PutCluster(layernew, vecclust);
1250                     newTrack->AddClustInTrack();
1251                     listoftrack.AddLast(newTrack);
1252                 }   // end for indnew
1253             }  // end of for on detectors (iriv)
1254         }//end if(outinters==0)
1255
1256         if(flaghit==0 || outinters==-2) {
1257             AliITSTrackV1 *newTrack = new AliITSTrackV1(*trackITS);
1258             (*newTrack).Setfnoclust();           
1259             //(*newTrack).SetLayer((*trackITS).GetLayer()-1); 
1260             (*newTrack).AddMS();  // add the multiple scattering matrix
1261                                      // to the covariance matrix  
1262             (*newTrack).AddEL(1.,0);
1263             listoftrack.AddLast(newTrack);
1264         } // end if
1265
1266         //gObjectTable->Print();   // stampa memoria
1267          
1268         RecursiveTracking(&listoftrack);          
1269         listoftrack.Delete();
1270     } // end of for on tracks (index)
1271
1272     //gObjectTable->Print();   // stampa memoria
1273      delete rl;
1274     
1275 }
1276 //______________________________________________________________________
1277 Int_t AliITSTrackerV1::Intersection(AliITSTrackV1 &track,Int_t layer,
1278                                     Int_t &ladder,Int_t &detector) { 
1279     // Origin  A. Badala' and G.S. Pappalardo
1280     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
1281     // Found the intersection and the detector 
1282
1283     Double_t rk=fAvrad[layer-1];
1284     if(track.DoNotCross(rk)){ /*cout<< " Do not cross \n";*/ return -1;} 
1285     track.Propagation(rk);
1286     Double_t zinters=track.GetZ();
1287     Double_t phinters=track.Getphi();
1288     //cout<<"zinters = "<<zinters<<"  phinters = "<<phinters<<"\n";
1289
1290     TVector det(9);
1291     TVector listDet(2);
1292     TVector distZCenter(2);
1293
1294     Int_t iz=0; 
1295     Int_t iD;
1296     for(iD = 1; iD<= fNdet[layer-1]; iD++) {
1297         if(zinters > fzmin[layer-1][iD-1] && zinters <= fzmax[layer-1][iD-1]) {
1298             if(iz>1) {
1299                 cout<< " Errore su iz in Intersection \n";
1300                 getchar();
1301             }else {
1302                 listDet(iz)= iD; distZCenter(iz)=TMath::Abs(zinters-det(2));
1303                 iz++;
1304             } // end if
1305         } // end if
1306     } // end for iD
1307
1308     if(iz==0) {/* cout<< " No detector along Z \n";*/ return -2;}
1309     detector=Int_t (listDet(0));
1310     if(iz>1 && (distZCenter(0)>distZCenter(1)))   detector=Int_t (listDet(1));
1311
1312     TVector listLad(2);
1313     TVector distPhiCenter(2);
1314     Int_t ip=0;
1315     Double_t pigre=TMath::Pi();
1316     Int_t iLd;   
1317     for(iLd = 1; iLd<= fNlad[layer-1]; iLd++) {
1318         Double_t phimin=fphimin[layer-1][iLd-1];
1319         Double_t phimax=fphimax[layer-1][iLd-1];
1320         Double_t phidet=fphidet[layer-1][iLd-1];
1321         Double_t phiconfr=phinters;
1322         if(phimin>phimax) {  
1323             //if(phimin <5.5) {cout<<" Error in Intersection for phi \n";
1324             // getchar();}
1325             phimin-=(2.*pigre);
1326             if(phinters>(1.5*pigre)) phiconfr=phinters-(2.*pigre); 
1327             if(phidet>(1.5*pigre)) phidet-=(2.*pigre);
1328         } // end if
1329         if(phiconfr>phimin && phiconfr<= phimax) {
1330             if(ip>1) {
1331                 cout<< " Errore su ip in Intersection \n"; getchar();
1332             }else  {
1333                 listLad(ip)= iLd;
1334                 distPhiCenter(ip)=TMath::Abs(phiconfr-phidet); ip++;
1335             } // end if
1336         } // end if
1337     } // end for iLd
1338     if(ip==0) { cout<< " No detector along phi \n"; getchar();}
1339     ladder=Int_t (listLad(0));
1340     if(ip>1 && (distPhiCenter(0)>distPhiCenter(1)))  ladder=Int_t (listLad(1));
1341     return 0;
1342 }
1343 //______________________________________________________________________
1344 void AliITSTrackerV1::KalmanFilter(AliITSTrackV1 *newTrack,TVector &cluster,
1345                                    Double_t sigma[2]){ 
1346     //Origin  A. Badala' and G.S. Pappalardo:
1347     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
1348     // Kalman filter without vertex constraint
1349     ////// Evaluation of the measurement vector ////////////////////////
1350     Double_t m[2];
1351     Double_t rk,phik,zk;
1352     rk=cluster(0);   phik=cluster(1);  zk=cluster(2);
1353     m[0]=phik;    m[1]=zk;
1354     //////////////////////// Evaluation of the error matrix V  /////////
1355     Double_t v00=sigma[0];
1356     Double_t v11=sigma[1];
1357     ////////////////////////////////////////////////////////////////////  
1358     Double_t cin00,cin10,cin20,cin30,cin40,cin11,cin21,cin31,cin41,cin22,
1359              cin32,cin42,cin33,cin43,cin44;
1360
1361     newTrack->GetCElements(cin00,
1362                            cin10,cin11,
1363                            cin20,cin21,cin22,
1364                            cin30,cin31,cin32,cin33,
1365                            cin40,cin41,cin42,cin43,cin44); //get C matrix
1366     Double_t rold00=cin00+v00;
1367     Double_t rold10=cin10;
1368     Double_t rold11=cin11+v11;
1369     ////////////////////// R matrix inversion  /////////////////////////
1370     Double_t det=rold00*rold11-rold10*rold10;
1371     Double_t r00=rold11/det;
1372     Double_t r10=-rold10/det;
1373     Double_t r11=rold00/det;
1374     ////////////////////////////////////////////////////////////////////
1375     Double_t k00=cin00*r00+cin10*r10;
1376     Double_t k01=cin00*r10+cin10*r11;
1377     Double_t k10=cin10*r00+cin11*r10;  
1378     Double_t k11=cin10*r10+cin11*r11;
1379     Double_t k20=cin20*r00+cin21*r10;  
1380     Double_t k21=cin20*r10+cin21*r11;  
1381     Double_t k30=cin30*r00+cin31*r10;  
1382     Double_t k31=cin30*r10+cin31*r11;  
1383     Double_t k40=cin40*r00+cin41*r10;
1384     Double_t k41=cin40*r10+cin41*r11;
1385     Double_t x0,x1,x2,x3,x4;
1386     newTrack->GetXElements(x0,x1,x2,x3,x4);     // get the state vector
1387     Double_t savex0=x0, savex1=x1;
1388     x0+=k00*(m[0]-savex0)+k01*(m[1]-savex1);
1389     x1+=k10*(m[0]-savex0)+k11*(m[1]-savex1);
1390     x2+=k20*(m[0]-savex0)+k21*(m[1]-savex1);
1391     x3+=k30*(m[0]-savex0)+k31*(m[1]-savex1);
1392     x4+=k40*(m[0]-savex0)+k41*(m[1]-savex1);
1393     Double_t c00,c10,c20,c30,c40,c11,c21,c31,c41,c22,c32,c42,c33,c43,c44;
1394     c00=cin00-k00*cin00-k01*cin10;
1395     c10=cin10-k00*cin10-k01*cin11;
1396     c20=cin20-k00*cin20-k01*cin21;
1397     c30=cin30-k00*cin30-k01*cin31;
1398     c40=cin40-k00*cin40-k01*cin41;
1399     c11=cin11-k10*cin10-k11*cin11;
1400     c21=cin21-k10*cin20-k11*cin21;
1401     c31=cin31-k10*cin30-k11*cin31;
1402     c41=cin41-k10*cin40-k11*cin41;
1403     c22=cin22-k20*cin20-k21*cin21;
1404     c32=cin32-k20*cin30-k21*cin31;
1405     c42=cin42-k20*cin40-k21*cin41;
1406     c33=cin33-k30*cin30-k31*cin31;
1407     c43=cin43-k30*cin40-k31*cin41;
1408     c44=cin44-k40*cin40-k41*cin41;
1409     newTrack->PutXElements(x0,x1,x2,x3,x4);  // put the new state vector
1410     newTrack->PutCElements(c00,
1411                            c10,c11,
1412                            c20,c21,c22,
1413                            c30,c31,c32,c33,
1414                            c40,c41,c42,c43,c44); // put in track the
1415                                                  // new cov matrix 
1416     Double_t vmcold00=v00-c00;
1417     Double_t vmcold10=-c10;
1418     Double_t vmcold11=v11-c11;
1419     ////////////////////// Matrix vmc inversion  ///////////////////////
1420     det=vmcold00*vmcold11-vmcold10*vmcold10;
1421     Double_t vmc00=vmcold11/det;
1422     Double_t vmc10=-vmcold10/det;
1423     Double_t vmc11=vmcold00/det;
1424     ////////////////////////////////////////////////////////////////////
1425   Double_t chi2=(m[0]-x0)*( vmc00*(m[0]-x0) + 2.*vmc10*(m[1]-x1) ) +
1426                 (m[1]-x1)*vmc11*(m[1]-x1);
1427   newTrack->SetChi2(newTrack->GetChi2()+chi2);
1428 }
1429 //----------------------------------------------------------------------
1430 //void AliITSTrackerV1::KalmanFilterVert(AliITSTrackV1 *newTrack,
1431 //                                       TVector &cluster,Double_t sigma[2]){
1432 void AliITSTrackerV1::KalmanFilterVert(AliITSTrackV1 *newTrack,
1433                                        TVector &cluster,Double_t sigma[2]
1434                                        /*, Double_t chi2pred*/){
1435     //Origin  A. Badala' and G.S. Pappalardo:
1436     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it 
1437     // Kalman filter with vertex constraint
1438     ///////////////////// Evaluation of the measurement vector m ////////
1439     Double_t m[4];
1440     Double_t rk,phik,zk;
1441     rk=cluster(0);   phik=cluster(1);  zk=cluster(2);
1442     m[0]=phik;    m[1]=zk;
1443     Double_t cc=(*newTrack).GetC();
1444     Double_t zv=(*newTrack).GetZv(); 
1445     Double_t dv=(*newTrack).GetDv();
1446     Double_t cy=cc/2.;
1447     Double_t tgl= (zk-zv)*cy/TMath::ASin(cy*rk);
1448     m[2]=dv;    m[3]=tgl;
1449     /////////////////////// Evaluation of the error matrix V  //////////
1450     Int_t layer=newTrack->GetLayer();
1451     Double_t v00=sigma[0];
1452     Double_t v11=sigma[1];
1453     Double_t v31=sigma[1]/rk;
1454     Double_t sigmaDv=newTrack->GetsigmaDv();
1455     Double_t v22=sigmaDv*sigmaDv  + newTrack->Getd2(layer-1);
1456     Double_t v32=newTrack->Getdtgl(layer-1);
1457     Double_t sigmaZv=newTrack->GetsigmaZv();  
1458     Double_t v33=(sigma[1]+sigmaZv*sigmaZv)/(rk*rk)+newTrack->Gettgl2(layer-1);
1459     //////////////////////////////////////////////////////////////////
1460     Double_t cin00,cin10,cin11,cin20,cin21,cin22,
1461              cin30,cin31,cin32,cin33,cin40,cin41,cin42,cin43,cin44;
1462     newTrack->GetCElements(cin00,
1463                            cin10,cin11,
1464                            cin20,cin21,cin22,
1465                            cin30,cin31,cin32,cin33,
1466                            cin40,cin41,cin42,cin43,cin44); //get C matrix
1467     Double_t r[4][4];
1468     r[0][0]=cin00+v00;
1469     r[1][0]=cin10;
1470     r[2][0]=cin20;
1471     r[3][0]=cin30;
1472     r[1][1]=cin11+v11;
1473     r[2][1]=cin21;
1474     r[3][1]=cin31+sigma[1]/rk;
1475     r[2][2]=cin22+sigmaDv*sigmaDv+newTrack->Getd2(layer-1);
1476     r[3][2]=cin32+newTrack->Getdtgl(layer-1);
1477     r[3][3]=cin33+(sigma[1]+sigmaZv*sigmaZv)/(rk*rk)+
1478                                     newTrack->Gettgl2(layer-1);
1479     r[0][1]=r[1][0]; r[0][2]=r[2][0]; r[0][3]=r[3][0]; 
1480     r[1][2]=r[2][1]; r[1][3]=r[3][1]; r[2][3]=r[3][2];
1481     /////////////////////  Matrix R inversion //////////////////////////
1482     const Int_t kn=4;
1483     Double_t big, hold;
1484     Double_t d=1.;
1485     Int_t ll[kn],mm[kn];
1486     Int_t i,j,k;
1487
1488     for(k=0; k<kn; k++) {
1489         ll[k]=k;
1490         mm[k]=k;
1491         big=r[k][k];
1492         for(j=k; j<kn ; j++) {
1493             for (i=j; i<kn; i++) {
1494                 if(TMath::Abs(big) < TMath::Abs(r[i][j]) ) { 
1495                     big=r[i][j]; 
1496                     ll[k]=i; 
1497                     mm[k]=j; 
1498                 } // end if
1499             } // end for i
1500         } // end for j
1501         //
1502         j= ll[k];
1503         if(j > k) {
1504             for(i=0; i<kn; i++) {
1505                 hold=-r[k][i]; 
1506                 r[k][i]=r[j][i]; 
1507                 r[j][i]=hold;
1508             } // end for i
1509         }// end if
1510         //
1511         i=mm[k];
1512         if(i > k ) { 
1513             for(j=0; j<kn; j++) {
1514                 hold=-r[j][k];
1515                 r[j][k]=r[j][i]; 
1516                 r[j][i]=hold; 
1517             } // end for j
1518         } // end if
1519         //
1520         if(!big) {
1521             d=0.;
1522             cout << "Singular matrix\n"; 
1523         } // end if
1524         for(i=0; i<kn; i++) {
1525             if(i == k) { continue; }    
1526             r[i][k]=r[i][k]/(-big);
1527         } // end for i
1528         //
1529         for(i=0; i<kn; i++) {
1530             hold=r[i][k];
1531             for(j=0; j<kn; j++) {
1532                 if(i == k || j == k) continue;
1533                 r[i][j]=hold*r[k][j]+r[i][j];
1534             } // end for j
1535         } // end of ri
1536         // 
1537         for(j=0; j<kn; j++) {
1538             if(j == k) continue;
1539             r[k][j]=r[k][j]/big;
1540         } // end for j
1541         //
1542         d=d*big;
1543         //
1544         r[k][k]=1./big;        
1545     } // end for k
1546     //
1547     for(k=kn-1; k>=0; k--) {
1548         i=ll[k];
1549         if(i > k) {
1550             for (j=0; j<kn; j++) {
1551                 hold=r[j][k];
1552                 r[j][k]=-r[j][i]; 
1553                 r[j][i]=hold;
1554             } // end for j
1555         } // end if i
1556         j=mm[k];
1557         if(j > k) {
1558             for (i=0; i<kn; i++) {
1559                 hold=r[k][i]; 
1560                 r[k][i]=-r[j][i]; 
1561                 r[j][i]=hold;
1562             } // end for i
1563         } // end if
1564     } // end for k
1565     ////////////////////////////////////////////////////////////////////
1566     Double_t k00=cin00*r[0][0]+cin10*r[1][0]+cin20*r[2][0]+cin30*r[3][0];
1567     Double_t k01=cin00*r[1][0]+cin10*r[1][1]+cin20*r[2][1]+cin30*r[3][1];
1568     Double_t k02=cin00*r[2][0]+cin10*r[2][1]+cin20*r[2][2]+cin30*r[3][2];
1569     Double_t k03=cin00*r[3][0]+cin10*r[3][1]+cin20*r[3][2]+cin30*r[3][3];
1570     Double_t k10=cin10*r[0][0]+cin11*r[1][0]+cin21*r[2][0]+cin31*r[3][0];  
1571     Double_t k11=cin10*r[1][0]+cin11*r[1][1]+cin21*r[2][1]+cin31*r[3][1];
1572     Double_t k12=cin10*r[2][0]+cin11*r[2][1]+cin21*r[2][2]+cin31*r[3][2];
1573     Double_t k13=cin10*r[3][0]+cin11*r[3][1]+cin21*r[3][2]+cin31*r[3][3];
1574     Double_t k20=cin20*r[0][0]+cin21*r[1][0]+cin22*r[2][0]+cin32*r[3][0];  
1575     Double_t k21=cin20*r[1][0]+cin21*r[1][1]+cin22*r[2][1]+cin32*r[3][1];  
1576     Double_t k22=cin20*r[2][0]+cin21*r[2][1]+cin22*r[2][2]+cin32*r[3][2];
1577     Double_t k23=cin20*r[3][0]+cin21*r[3][1]+cin22*r[3][2]+cin32*r[3][3];
1578     Double_t k30=cin30*r[0][0]+cin31*r[1][0]+cin32*r[2][0]+cin33*r[3][0];  
1579     Double_t k31=cin30*r[1][0]+cin31*r[1][1]+cin32*r[2][1]+cin33*r[3][1];  
1580     Double_t k32=cin30*r[2][0]+cin31*r[2][1]+cin32*r[2][2]+cin33*r[3][2];  
1581     Double_t k33=cin30*r[3][0]+cin31*r[3][1]+cin32*r[3][2]+cin33*r[3][3];
1582     Double_t k40=cin40*r[0][0]+cin41*r[1][0]+cin42*r[2][0]+cin43*r[3][0];
1583     Double_t k41=cin40*r[1][0]+cin41*r[1][1]+cin42*r[2][1]+cin43*r[3][1];
1584     Double_t k42=cin40*r[2][0]+cin41*r[2][1]+cin42*r[2][2]+cin43*r[3][2];  
1585     Double_t k43=cin40*r[3][0]+cin41*r[3][1]+cin42*r[3][2]+cin43*r[3][3];
1586
1587     Double_t x0,x1,x2,x3,x4;
1588     newTrack->GetXElements(x0,x1,x2,x3,x4);     // get the state vector
1589     Double_t savex0=x0, savex1=x1, savex2=x2, savex3=x3;
1590     x0+=k00*(m[0]-savex0)+k01*(m[1]-savex1)+k02*(m[2]-savex2)+
1591         k03*(m[3]-savex3);
1592     x1+=k10*(m[0]-savex0)+k11*(m[1]-savex1)+k12*(m[2]-savex2)+
1593         k13*(m[3]-savex3);
1594     x2+=k20*(m[0]-savex0)+k21*(m[1]-savex1)+k22*(m[2]-savex2)+
1595         k23*(m[3]-savex3);
1596     x3+=k30*(m[0]-savex0)+k31*(m[1]-savex1)+k32*(m[2]-savex2)+
1597         k33*(m[3]-savex3);
1598     x4+=k40*(m[0]-savex0)+k41*(m[1]-savex1)+k42*(m[2]-savex2)+
1599         k43*(m[3]-savex3);
1600     Double_t c00,c10,c20,c30,c40,c11,c21,c31,c41,c22,c32,c42,c33,c43,c44;
1601     c00=cin00-k00*cin00-k01*cin10-k02*cin20-k03*cin30;
1602     c10=cin10-k00*cin10-k01*cin11-k02*cin21-k03*cin31;
1603     c20=cin20-k00*cin20-k01*cin21-k02*cin22-k03*cin32;
1604     c30=cin30-k00*cin30-k01*cin31-k02*cin32-k03*cin33;
1605     c40=cin40-k00*cin40-k01*cin41-k02*cin42-k03*cin43;
1606     c11=cin11-k10*cin10-k11*cin11-k12*cin21-k13*cin31;
1607     c21=cin21-k10*cin20-k11*cin21-k12*cin22-k13*cin32;
1608     c31=cin31-k10*cin30-k11*cin31-k12*cin32-k13*cin33;
1609     c41=cin41-k10*cin40-k11*cin41-k12*cin42-k13*cin43;
1610     c22=cin22-k20*cin20-k21*cin21-k22*cin22-k23*cin32;
1611     c32=cin32-k20*cin30-k21*cin31-k22*cin32-k23*cin33;
1612     c42=cin42-k20*cin40-k21*cin41-k22*cin42-k23*cin43;
1613     c33=cin33-k30*cin30-k31*cin31-k32*cin32-k33*cin33;
1614     c43=cin43-k30*cin40-k31*cin41-k32*cin42-k33*cin43;
1615     c44=cin44-k40*cin40-k41*cin41-k42*cin42-k43*cin43;
1616
1617     newTrack->PutXElements(x0,x1,x2,x3,x4); // put the new state vector
1618     newTrack->PutCElements(c00,
1619                            c10,c11,
1620                            c20,c21,c22,
1621                            c30,c31,c32,c33,
1622                            c40,c41,c42,c43,c44); // put in track the
1623                                                  // new cov matrix
1624     Double_t vmc[4][4];
1625     vmc[0][0]=v00-c00; vmc[1][0]=-c10; vmc[2][0]=-c20; vmc[3][0]=-c30;
1626     vmc[1][1]=v11-c11; vmc[2][1]=-c21; vmc[3][1]=v31-c31;
1627     vmc[2][2]=v22-c22; vmc[3][2]=v32-c32;
1628     vmc[3][3]=v33-c33;
1629     vmc[0][1]=vmc[1][0]; vmc[0][2]=vmc[2][0]; vmc[0][3]=vmc[3][0];
1630     vmc[1][2]=vmc[2][1]; vmc[1][3]=vmc[3][1];
1631     vmc[2][3]=vmc[3][2];
1632     /////////////////////// vmc matrix inversion /////////////////////// 
1633     d=1.;
1634     for(k=0; k<kn; k++) {
1635         ll[k]=k;
1636         mm[k]=k;
1637         big=vmc[k][k];
1638         for(j=k; j<kn ; j++) {
1639             for (i=j; i<kn; i++) {
1640                 if(TMath::Abs(big) < TMath::Abs(vmc[i][j]) ) {
1641                     big=vmc[i][j]; 
1642                     ll[k]=i;
1643                     mm[k]=j;
1644                 } // end if
1645             } // end for i
1646         } // end for j
1647         //
1648         j= ll[k];
1649         if(j > k) {
1650             for(i=0; i<kn; i++) {
1651                 hold=-vmc[k][i]; 
1652                 vmc[k][i]=vmc[j][i];
1653                 vmc[j][i]=hold;
1654             } // end for i
1655         } // end if
1656         //
1657         i=mm[k];
1658         if(i > k ) { 
1659             for(j=0; j<kn; j++) { 
1660                 hold=-vmc[j][k]; 
1661                 vmc[j][k]=vmc[j][i]; 
1662                 vmc[j][i]=hold; 
1663             } // end for j
1664         } // end if
1665         //
1666         if(!big) {
1667             d=0.;
1668             cout << "Singular matrix\n"; 
1669         } // end if
1670         for(i=0; i<kn; i++) {
1671             if(i == k) continue;    
1672             vmc[i][k]=vmc[i][k]/(-big);
1673         }    // end for i
1674         //
1675         for(i=0; i<kn; i++) {
1676             hold=vmc[i][k];
1677             for(j=0; j<kn; j++) {
1678                 if(i == k || j == k) continue;
1679                 vmc[i][j]=hold*vmc[k][j]+vmc[i][j];
1680             } // end for j
1681         } // end for i
1682         //  
1683         for(j=0; j<kn; j++) {
1684             if(j == k) continue;
1685             vmc[k][j]=vmc[k][j]/big;
1686         } // end for j
1687         //
1688         d=d*big;
1689         //
1690         vmc[k][k]=1./big;        
1691     }  // end for k
1692     //  
1693     for(k=kn-1; k>=0; k--) {
1694         i=ll[k];
1695         if(i > k) {
1696             for (j=0; j<kn; j++) {
1697                 hold=vmc[j][k]; 
1698                 vmc[j][k]=-vmc[j][i];
1699                 vmc[j][i]=hold;
1700             } // end for j
1701         } // end if i>k
1702         j=mm[k];
1703         if(j > k) {
1704             for (i=0; i<kn; i++) {
1705                 hold=vmc[k][i]; 
1706                 vmc[k][i]=-vmc[j][i]; 
1707                 vmc[j][i]=hold;
1708             } // end for i
1709         } // end if j>k
1710     } // end for k
1711     ////////////////////////////////////////////////////////////////////
1712     Double_t chi2=(m[0]-x0)*( vmc[0][0]*(m[0]-x0) + 2.*vmc[1][0]*(m[1]-x1) + 
1713                               2.*vmc[2][0]*(m[2]-x2)+ 2.*vmc[3][0]*(m[3]-x3) )+
1714         (m[1]-x1)* ( vmc[1][1]*(m[1]-x1) + 2.*vmc[2][1]*(m[2]-x2)+ 
1715                      2.*vmc[3][1]*(m[3]-x3) ) +
1716         (m[2]-x2)* ( vmc[2][2]*(m[2]-x2)+ 2.*vmc[3][2]*(m[3]-x3) ) +
1717         (m[3]-x3)*vmc[3][3]*(m[3]-x3);
1718     //cout<<" chi2 kalman = "<<chi2<<"\n";  getchar(); 
1719     newTrack->SetChi2(newTrack->GetChi2()+chi2);   
1720     //   newTrack->SetChi2(newTrack->GetChi2()+chi2pred);
1721 }
1722