]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/ITS/tracking/AliITStrackerHLT.cxx
dynamic adjustment of the output buffer size estimator
[u/mrichter/AliRoot.git] / HLT / ITS / tracking / AliITStrackerHLT.cxx
1 /**************************************************************************
2  * Copyright(c) 2007-2009, 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 /* $Id: AliITStrackerHLT.cxx 32466 2009-05-20 07:51:56Z hristov $ */
16
17 //-------------------------------------------------------------------------
18 //               Implementation of the ITS tracker class
19 //    It reads AliITSRecPoint clusters and creates AliHLTITSTrack tracks
20 //                   and fills with them the ESD
21 //          Origin: Marian Ivanov, CERN, Marian.Ivanov@cern.ch 
22 //          Current support and development: 
23 //                     Andrea Dainese, andrea.dainese@lnl.infn.it
24 //     dE/dx analysis by: Boris Batyunya, JINR, Boris.Batiounia@cern.ch
25 //     Params moved to AliITSRecoParam by: Andrea Dainese, INFN
26 //     Material budget from TGeo by: Ludovic Gaudichet & Andrea Dainese, INFN
27 //-------------------------------------------------------------------------
28
29 //#include <TMatrixD.h>
30 #include <TTree.h>
31 #include <TDatabasePDG.h>
32 #include <TString.h>
33 #include <TRandom.h>
34 #include <TTreeStream.h>
35
36
37 #include "AliLog.h"
38 #include "AliITSCalibrationSPD.h"
39 #include "AliITSCalibrationSDD.h"
40 #include "AliITSCalibrationSSD.h"
41 #include "AliCDBEntry.h"
42 #include "AliCDBManager.h"
43 #include "AliAlignObj.h"
44 #include "AliTrackPointArray.h"
45 #include "AliESDVertex.h"
46 #include "AliESDEvent.h"
47 #include "AliESDtrack.h"
48 #include "AliV0.h"
49 #include "AliHelix.h"
50 #include "AliITSChannelStatus.h"
51 #include "AliITSRecPoint.h"
52 #include "AliITSgeomTGeo.h"
53 #include "AliITSReconstructor.h"
54 #include "AliITSClusterParam.h"
55 #include "AliITSsegmentation.h"
56 #include "AliITSCalibration.h"
57 #include "AliITSV0Finder.h"
58 #include "AliITStrackerHLT.h"
59 #include "TStopwatch.h"
60 //#include "AliHLTTPCCATrackParam.h"
61 //#include "AliHLTVertexer.h"
62
63
64 ClassImp(AliITStrackerHLT)
65
66 Bool_t AliITStrackerHLT::TransportToX( AliExternalTrackParam *t, double x ) const
67 {
68   return t->PropagateTo( x, t->GetBz() );
69 }
70
71 Bool_t AliITStrackerHLT::TransportToPhiX( AliExternalTrackParam *t, double phi, double x ) const
72 {
73   return t->Propagate( phi, x, t->GetBz() );
74 }
75
76
77
78 AliITStrackerHLT::AliITStrackerHLT()
79   :AliTracker(),
80    fRecoParam(0),
81    fLayers(new AliHLTITSLayer[AliITSgeomTGeo::kNLayers]),
82    fEsd(0),
83    fUseTGeo(2),
84    fxOverX0Pipe(-1.),
85    fxTimesRhoPipe(-1.),
86    fDebugStreamer(0),
87    fITSChannelStatus(0),
88    fTracks(),
89    fITSOutTracks(),
90    fLoadTime(0),
91    fRecoTime(0),
92    fNEvents(0),
93    fClusters()
94 {
95   //Default constructor
96   Int_t i;
97   for(i=0;i<4;i++) fSPDdetzcentre[i]=0.;
98   for(i=0;i<2;i++) {fxOverX0Shield[i]=-1.;fxTimesRhoShield[i]=-1.;}
99   for(i=0;i<6;i++) {fxOverX0Layer[i]=-1.;fxTimesRhoLayer[i]=-1.;}
100 }
101 //------------------------------------------------------------------------
102 AliITStrackerHLT::AliITStrackerHLT(const Char_t *geom) 
103 : AliTracker(),
104   fRecoParam(0),
105   fLayers(new AliHLTITSLayer[AliITSgeomTGeo::kNLayers]),
106   fEsd(0),
107   fUseTGeo(2),
108   fxOverX0Pipe(-1.),
109   fxTimesRhoPipe(-1.),
110   fDebugStreamer(0),
111   fITSChannelStatus(0),
112   fTracks(),
113   fITSOutTracks(),
114   fLoadTime(0),
115    fRecoTime(0),
116   fNEvents(0),
117   fClusters()
118 {
119   //--------------------------------------------------------------------
120   //This is the AliITStrackerHLT constructor
121   //--------------------------------------------------------------------
122   if (geom) {
123     AliWarning("\"geom\" is actually a dummy argument !");
124   }
125
126   if(AliGeomManager::GetGeometry()==NULL){
127     AliGeomManager::LoadGeometry();
128   }
129
130   fRecoParam = AliITSReconstructor::GetRecoParam();
131   if( !fRecoParam ){
132     AliITSReconstructor *tmp = new AliITSReconstructor();
133     tmp->Init();
134     fRecoParam = AliITSRecoParam::GetLowFluxParam();
135     tmp->AliReconstructor::SetRecoParam(fRecoParam);
136   }
137   for (Int_t i=1; i<AliITSgeomTGeo::GetNLayers()+1; i++) {
138     Int_t nlad=AliITSgeomTGeo::GetNLadders(i);
139     Int_t ndet=AliITSgeomTGeo::GetNDetectors(i);
140
141     Double_t xyz[3], &x=xyz[0], &y=xyz[1], &z=xyz[2];
142     AliITSgeomTGeo::GetOrigTranslation(i,1,1,xyz); 
143     Double_t poff=TMath::ATan2(y,x);
144     Double_t zoff=z;
145     Double_t r=TMath::Sqrt(x*x + y*y);
146
147     AliITSgeomTGeo::GetOrigTranslation(i,1,2,xyz);
148     r += TMath::Sqrt(x*x + y*y);
149     AliITSgeomTGeo::GetOrigTranslation(i,2,1,xyz);
150     r += TMath::Sqrt(x*x + y*y);
151     AliITSgeomTGeo::GetOrigTranslation(i,2,2,xyz);
152     r += TMath::Sqrt(x*x + y*y);
153     r*=0.25;
154
155     new (fLayers+i-1) AliHLTITSLayer(r,poff,zoff,nlad,ndet);
156
157     for (Int_t j=1; j<nlad+1; j++) {
158       for (Int_t k=1; k<ndet+1; k++) { //Fill this layer with detectors
159         TGeoHMatrix m; AliITSgeomTGeo::GetOrigMatrix(i,j,k,m);
160         const TGeoHMatrix *tm=AliITSgeomTGeo::GetTracking2LocalMatrix(i,j,k);
161         m.Multiply(tm);
162         Double_t txyz[3]={0.};
163         xyz[0]=0.;xyz[1]=0.;xyz[2]=0.;
164         m.LocalToMaster(txyz,xyz);
165         r=TMath::Sqrt(xyz[0]*xyz[0] + xyz[1]*xyz[1]);
166         Double_t phi=TMath::ATan2(xyz[1],xyz[0]);
167
168         if (phi<0) phi+=TMath::TwoPi();
169         else if (phi>=TMath::TwoPi()) phi-=TMath::TwoPi();
170
171         AliHLTITSDetector &det=fLayers[i-1].GetDetector((j-1)*ndet + k-1); 
172         new(&det) AliHLTITSDetector(r,phi); 
173         // compute the real radius (with misalignment)
174         TGeoHMatrix mmisal(*(AliITSgeomTGeo::GetMatrix(i,j,k)));
175         mmisal.Multiply(tm);
176         xyz[0]=0.;xyz[1]=0.;xyz[2]=0.;
177         mmisal.LocalToMaster(txyz,xyz);
178         Double_t rmisal=TMath::Sqrt(xyz[0]*xyz[0] + xyz[1]*xyz[1]);
179         det.SetRmisal(rmisal);
180         
181       } // end loop on detectors
182     } // end loop on ladders
183   } // end loop on layers
184
185   
186   Double_t xyzVtx[]={ fRecoParam->GetXVdef(),
187                       fRecoParam->GetYVdef(),
188                       fRecoParam->GetZVdef()}; 
189   Double_t ersVtx[]={ fRecoParam->GetSigmaXVdef(),
190                       fRecoParam->GetSigmaYVdef(),
191                       fRecoParam->GetSigmaZVdef()}; 
192
193   SetVertex(xyzVtx,ersVtx);
194
195   // store positions of centre of SPD modules (in z)
196   Double_t tr[3];
197   AliITSgeomTGeo::GetTranslation(1,1,1,tr);
198   fSPDdetzcentre[0] = tr[2];
199   AliITSgeomTGeo::GetTranslation(1,1,2,tr);
200   fSPDdetzcentre[1] = tr[2];
201   AliITSgeomTGeo::GetTranslation(1,1,3,tr);
202   fSPDdetzcentre[2] = tr[2];
203   AliITSgeomTGeo::GetTranslation(1,1,4,tr);
204   fSPDdetzcentre[3] = tr[2];
205
206   //fUseTGeo = fRecoParam->GetUseTGeoInTracker();
207   //if(fRecoParam->GetExtendedEtaAcceptance() && fUseTGeo!=1 && fUseTGeo!=3) {
208   //AliWarning("fUseTGeo changed to 3 because fExtendedEtaAcceptance is kTRUE");
209   //fUseTGeo = 3;
210   //}
211
212   for(Int_t i=0;i<2;i++) {fxOverX0Shield[i]=-1.;fxTimesRhoShield[i]=-1.;}
213   for(Int_t i=0;i<6;i++) {fxOverX0Layer[i]=-1.;fxTimesRhoLayer[i]=-1.;}
214   
215   fDebugStreamer = new TTreeSRedirector("ITSdebug.root");
216   Init();
217 }
218 //------------------------------------------------------------------------
219 AliITStrackerHLT::AliITStrackerHLT(const AliITStrackerHLT &tracker)
220 :AliTracker(tracker),
221  fRecoParam( tracker.fRecoParam),
222  fLayers(new AliHLTITSLayer[AliITSgeomTGeo::kNLayers]),
223  fEsd(tracker.fEsd),
224  fUseTGeo(tracker.fUseTGeo),
225  fxOverX0Pipe(tracker.fxOverX0Pipe),
226  fxTimesRhoPipe(tracker.fxTimesRhoPipe),
227  fDebugStreamer(tracker.fDebugStreamer),
228  fITSChannelStatus(tracker.fITSChannelStatus),
229  fTracks(),
230  fITSOutTracks(),
231   fLoadTime(0),
232    fRecoTime(0),
233  fNEvents(0),
234  fClusters()
235 {
236   //Copy constructor
237   Int_t i;
238   for(i=0;i<4;i++) {
239     fSPDdetzcentre[i]=tracker.fSPDdetzcentre[i];
240   }
241   for(i=0;i<6;i++) {
242     fxOverX0Layer[i]=tracker.fxOverX0Layer[i];
243     fxTimesRhoLayer[i]=tracker.fxTimesRhoLayer[i];
244   }
245   for(i=0;i<2;i++) {
246     fxOverX0Shield[i]=tracker.fxOverX0Shield[i];
247     fxTimesRhoShield[i]=tracker.fxTimesRhoShield[i];
248   }
249   Init();
250 }
251 //------------------------------------------------------------------------
252 AliITStrackerHLT & AliITStrackerHLT::operator=(const AliITStrackerHLT &tracker){
253   //Assignment operator
254   this->~AliITStrackerHLT();
255   new(this) AliITStrackerHLT(tracker);
256   return *this;
257 }
258 //------------------------------------------------------------------------
259 AliITStrackerHLT::~AliITStrackerHLT()
260 {
261   //
262   //destructor
263   //
264   if (fDebugStreamer) {
265     //fDebugStreamer->Close();
266     delete fDebugStreamer;
267   }
268   if(fITSChannelStatus) delete fITSChannelStatus;
269   delete [] fLayers;
270 }
271
272 void AliITStrackerHLT::Init()
273 {
274   BuildMaterialLUT("Layers");  
275   BuildMaterialLUT("Pipe");  
276   BuildMaterialLUT("Shields");  
277 }
278
279
280 void AliITStrackerHLT::StartLoadClusters( Int_t guessForNClusters )
281 {
282   // !
283   fClusters.clear();
284   fClusters.reserve( guessForNClusters );
285 }
286
287 void AliITStrackerHLT::LoadCluster( const AliITSRecPoint &cluster) 
288 {
289   fClusters.push_back( cluster );
290 }
291
292
293
294 //------------------------------------------------------------------------
295 Int_t AliITStrackerHLT::LoadClusters(TTree *cTree) {
296   //--------------------------------------------------------------------
297   //This function loads ITS clusters
298   //--------------------------------------------------------------------
299
300   StartLoadClusters();
301
302   TBranch *branch=cTree->GetBranch("ITSRecPoints");
303   if (!branch) { 
304     Error("LoadClusters"," can't get the branch !\n");
305     return 1;
306   }
307
308   static TClonesArray dummy("AliITSRecPoint",10000), *clusters=&dummy;
309   branch->SetAddress(&clusters);
310
311   Int_t i=0,j=0,ndet=0;
312   for (i=0; i<AliITSgeomTGeo::GetNLayers(); i++) {
313     ndet=fLayers[i].GetNdetectors();
314     Int_t jmax = j + fLayers[i].GetNladders()*ndet;
315     for (; j<jmax; j++) {           
316       if (!cTree->GetEvent(j)) continue;
317       Int_t ncl=clusters->GetEntriesFast(); 
318       while (ncl--) {
319         LoadCluster( *( (AliITSRecPoint*)clusters->UncheckedAt(ncl)));
320       }
321       clusters->Delete();
322     }
323   }
324   
325   dummy.Clear();
326
327   return 0;
328 }
329
330 //------------------------------------------------------------------------
331 void AliITStrackerHLT::UnloadClusters() {
332   //--------------------------------------------------------------------
333   //This function unloads ITS clusters
334   //--------------------------------------------------------------------
335   for (Int_t i=0; i<AliITSgeomTGeo::GetNLayers(); i++) fLayers[i].ResetClusters();
336   fClusters.clear();
337 }
338
339
340
341
342 void AliITStrackerHLT::Reconstruct( std::vector<AliExternalTrackParam> tracksTPC )
343 {
344
345   //--------------------------------------------------------------------
346   // This functions reconstructs ITS tracks
347   //--------------------------------------------------------------------
348
349   fNEvents++;
350
351   // Init clusters
352  
353   TStopwatch timerInit;
354
355   for( int i=0; i<AliITSgeomTGeo::GetNLayers(); i++ ){ 
356     fLayers[i].ResetClusters();
357   }
358
359   for( unsigned int icl=0; icl<fClusters.size(); icl++ ){   
360     AliITSRecPoint &cl = fClusters[icl];
361     if (!cl.Misalign()) AliWarning("Can't misalign this cluster !"); 
362     fLayers[cl.GetLayer()].InsertCluster(&cl); 
363   }
364
365   for( int i=0; i<AliITSgeomTGeo::GetNLayers(); i++ ){ 
366     fLayers[i].ResetRoad(); //road defined by the cluster density
367     fLayers[i].SortClusters();
368   }  
369   timerInit.Stop();
370   fLoadTime+=timerInit.RealTime();
371
372
373   TStopwatch timer;
374
375   Double_t pimass = TDatabasePDG::Instance()->GetParticle(211)->Mass();
376   fTracks.clear();
377   fITSOutTracks.clear();
378
379   for( unsigned int itr=0; itr<tracksTPC.size(); itr++ ){
380
381     AliHLTITSTrack tMI( tracksTPC[itr] );
382     AliHLTITSTrack *t = &tMI;
383     t->SetTPCtrackId( itr );
384     t->SetMass(pimass); 
385     t->SetExpQ(0);
386
387     //if (!CorrectForTPCtoITSDeadZoneMaterial(t))  continue;
388       
389     //Int_t tpcLabel=t->GetLabel(); //save the TPC track label       
390       
391     FollowProlongationTree(t); 
392     int nclu=0;
393     for(Int_t i=0; i<6; i++) {
394       if( t->GetClusterIndex(i)>=0 ) nclu++; 
395     }
396     //cout<<"N assigned ITS clusters = "<<nclu<<std::endl;
397     t->SetLabel(-1);
398     if( nclu>0 ){
399       t->SetLabel(-1);//tpcLabel);
400       t->SetFakeRatio(1.);
401       CookLabel(t,.99); //For comparison only
402       //cout<<"label = "<<t->GetLabel()<<" / "<<tpcLabel<<endl;
403     }
404
405     //CorrectForPipeMaterial(t);
406    
407     TransportToX(t, 0 );
408     fTracks.push_back( *t );  
409     if(  nclu>0 ){ // construct ITSOut track
410       AliHLTITSTrack tOut(*t);
411       if( FitOutward( &tOut ) ){
412         fITSOutTracks.push_back( *t );          
413       }
414     }
415   }
416
417   timer.Stop();
418   fRecoTime+=timer.RealTime();
419 }
420
421
422
423 //------------------------------------------------------------------------
424 Int_t AliITStrackerHLT::Clusters2Tracks(AliESDEvent *event) {
425   //--------------------------------------------------------------------
426   // This functions reconstructs ITS tracks
427   // The clusters must be already loaded !
428   //--------------------------------------------------------------------
429   
430   
431   fEsd = event;         // store pointer to the esd 
432   std::vector<AliExternalTrackParam> tracksTPC;
433   tracksTPC.reserve(event->GetNumberOfTracks());
434   fTracks.reserve(event->GetNumberOfTracks());
435
436   for( int itr=0; itr<event->GetNumberOfTracks(); itr++ ){
437
438     AliESDtrack *esdTrack = event->GetTrack(itr);
439     //esdTrack->myITS = esdTrack->myTPC;
440     if ((esdTrack->GetStatus()&AliESDtrack::kTPCin)==0) continue;
441     //if (esdTrack->GetStatus()&AliESDtrack::kTPCout) continue;
442     if (esdTrack->GetStatus()&AliESDtrack::kITSin) continue;
443     if (esdTrack->GetKinkIndex(0)>0) continue;   //kink daughter
444     
445     AliHLTITSTrack t(*esdTrack);
446     t.SetTPCtrackId( itr );
447     tracksTPC.push_back( t );
448   }
449   //for( int iter=0; iter<100; iter++){
450   Reconstruct( tracksTPC );
451   //}
452
453   for( unsigned int itr=0; itr<fTracks.size(); itr++ ){
454     AliHLTITSTrack &t = fTracks[itr];    
455     UpdateESDtrack(event->GetTrack(t.TPCtrackId()), &t, AliESDtrack::kITSin);          
456     //event->GetTrack(t.TPCtrackId())->myITS = t;
457   }
458  
459
460   int hz = ( int ) ( (fRecoTime+fLoadTime) > 1.e-4 ? fNEvents / (fRecoTime+fLoadTime) : 0 );
461   int hz1 = ( int ) ( fRecoTime > 1.e-4 ? fNEvents / fRecoTime : 0 );
462   int hz2 = ( int ) ( fLoadTime > 1.e-4 ? fNEvents / fLoadTime : 0 );
463
464   std::cout<<"\n\n ITS tracker time = "<<hz2<<" Hz load / "<<hz1<<" Hz reco ="
465            <<hz<<
466     " Hz ("
467            <<fLoadTime/fNEvents*1000<<"+"<<fRecoTime/fNEvents*1000.
468            <<" = "<<(fLoadTime + fRecoTime)/fNEvents*1000.
469            <<" ms/ev), "<<fNEvents<<" events processed\n\n "<<std::endl;
470   return 0;
471 }
472
473
474 AliCluster *AliITStrackerHLT::GetCluster(Int_t index) const 
475 {
476   //       Return pointer to a given cluster
477   Int_t l=(index & 0xf0000000) >> 28;
478   Int_t c=(index & 0x0fffffff) >> 00;
479   return fLayers[l].GetCluster(c);
480 }
481
482
483
484
485 //------------------------------------------------------------------------
486 void AliITStrackerHLT::FollowProlongationTree(AliHLTITSTrack * track ) 
487 {
488   for (Int_t ilayer=5; ilayer>=0; ilayer--) {
489    
490     AliHLTITSLayer &layer=fLayers[ilayer];
491   
492     // material between SSD and SDD, SDD and SPD
493     //if (ilayer==3 && !CorrectForShieldMaterial(track,1)) continue;
494     //if (ilayer==1 && !CorrectForShieldMaterial(track,0)) continue;
495     
496     int idet;
497
498     {            
499       Double_t xloc, phi,z;
500       if( !track->GetLocalXPhiZat( layer.GetR(), xloc, phi, z ) ) return;
501       idet = layer.FindDetectorIndex(phi,z);
502     }
503
504     // track outside layer acceptance in z
505    
506     if( idet<0 ) continue;
507     
508     // propagate to the intersection with the detector plane     
509     {
510       const AliHLTITSDetector &det=layer.GetDetector( idet );
511       if (!TransportToPhiX( track, det.GetPhi(), det.GetR() ) ) return;
512       CorrectForLayerMaterial(track,ilayer);
513     }
514
515     // DEFINITION OF SEARCH ROAD AND CLUSTERS SELECTION
516     
517     // road in global (rphi,z) [i.e. in tracking ref. system]
518     
519     Double_t zmin,zmax,ymin,ymax;
520
521     if (!ComputeRoad(track,ilayer,idet,zmin,zmax,ymin,ymax)) continue;
522   
523     layer.SelectClusters(zmin,zmax,ymin,ymax);     
524     
525     // Define criteria for track-cluster association
526     
527     Double_t msz = track->GetSigmaZ2() + 
528       fRecoParam->GetNSigmaZLayerForRoadZ()*
529       fRecoParam->GetNSigmaZLayerForRoadZ()*
530       fRecoParam->GetSigmaZ2(ilayer);
531     
532     Double_t msy = track->GetSigmaY2() + 
533       fRecoParam->GetNSigmaYLayerForRoadY()*
534       fRecoParam->GetNSigmaYLayerForRoadY()*
535       fRecoParam->GetSigmaY2(ilayer);
536     
537     msz *= fRecoParam->GetNSigma2RoadZNonC();
538     msy *= fRecoParam->GetNSigma2RoadYNonC(); 
539     
540     msz = 1./msz; // 1/RoadZ^2
541     msy = 1./msy; // 1/RoadY^2    
542     
543     const AliITSRecPoint *cl=0; 
544     Int_t clidx=-1;     
545     
546     // loop over clusters in the road     
547     
548     const AliITSRecPoint *bestCluster=0; 
549     double bestChi2 = 1.e10;
550     AliHLTITSTrack bestTrack( *track );
551     int bestIdx = -1;
552      
553     while( (cl=layer.GetNextCluster(clidx)) ){
554       Int_t idetc=cl->GetDetectorIndex();
555       if ( idet !=idetc ) { // new cluster's detector
556         const AliHLTITSDetector &detc=layer.GetDetector(idetc);
557         if (!TransportToPhiX( track, detc.GetPhi(),detc.GetR()) ) continue;
558         idet = idetc;
559       }  
560       //double y,z;
561       //if (! track->GetLocalYZat( layer.GetDetector(idetc).GetR() + cl->GetX(),y,z ) ) continue;
562       double dz = track->GetZ() - cl->GetZ();
563       double dy = track->GetY() - cl->GetY();
564       double chi2 = dz*dz*msz + dy*dy*msy ;       
565       if ( chi2 < bestChi2 ){
566         bestChi2 = chi2;
567         bestCluster = cl;
568         bestTrack = *track;
569         bestIdx = clidx;
570         continue;
571       }
572     }
573     
574     if( !bestCluster || bestChi2 >2*10. ) continue;
575     
576     if (!TransportToX( &bestTrack, layer.GetDetector(bestCluster->GetDetectorIndex()).GetR() + bestCluster->GetX() ) ) continue;
577     
578     Double_t par[2]={ bestCluster->GetY(), bestCluster->GetZ()};
579     Double_t cov[3]={ bestCluster->GetSigmaY2(), 0., bestCluster->GetSigmaZ2()};
580     if( !bestTrack.AliExternalTrackParam::Update(par,cov) ) continue;
581     
582     *track = bestTrack;
583     track->SetClusterIndex(track->GetNumberOfClusters(), (ilayer<<28)+bestIdx);
584     track->SetNumberOfClusters(track->GetNumberOfClusters()+1);  
585   }
586 }
587
588
589
590 Int_t AliITStrackerHLT::FitOutward(AliHLTITSTrack * track ) 
591 {
592
593   track->ResetCovariance(100);
594
595   for (Int_t iTrCl=track->GetNumberOfClusters()-1; iTrCl>=0; iTrCl--) {
596     
597     Int_t index = track->GetClusterIndex(iTrCl);
598     Int_t ilayer=(index & 0xf0000000) >> 28;
599     Int_t ic=(index & 0x0fffffff) >> 00;
600     const AliHLTITSLayer &layer=fLayers[ilayer];
601     AliITSRecPoint *cl = layer.GetCluster(ic); 
602     int idet = cl->GetDetectorIndex();
603     const AliHLTITSDetector &det=layer.GetDetector( idet );
604  
605     // material between SSD and SDD, SDD and SPD
606     //if (ilayer==4 && !CorrectForShieldMaterial(track,1)) continue;
607     //if (ilayer==2 && !CorrectForShieldMaterial(track,0)) continue;
608     
609
610     // propagate to the intersection with the detector plane     
611     {
612       if (!TransportToPhiX( track, det.GetPhi(), det.GetR()+ cl->GetX() ) ) return 0;
613       CorrectForLayerMaterial(track,ilayer);
614     }
615
616     Double_t par[2]={ cl->GetY(), cl->GetZ()};
617     Double_t cov[3]={ cl->GetSigmaY2(), 0., cl->GetSigmaZ2()};
618     if( !track->AliExternalTrackParam::Update(par,cov) ) return 0;    
619   }
620   return 1;
621 }
622
623
624 //------------------------------------------------------------------------
625 AliHLTITSLayer & AliITStrackerHLT::GetLayer(Int_t layer) const
626 {
627   //--------------------------------------------------------------------
628   //
629   //
630   return fLayers[layer];
631 }
632
633
634
635 //------------------------------------------------------------------------
636 void AliITStrackerHLT::CookLabel(AliHLTITSTrack *track,Float_t wrong) const 
637 {
638   // get MC label for the track
639
640   Int_t mcLabel = -1;
641   
642   vector<int> labels;
643   Int_t nClusters = track->GetNumberOfClusters();
644
645   for (Int_t i=0; i<nClusters; i++){
646     Int_t cindex = track->GetClusterIndex(i);
647     //Int_t l=(cindex & 0xf0000000) >> 28;
648     AliITSRecPoint *cl = (AliITSRecPoint*)GetCluster(cindex);    
649     if ( cl->GetLabel(0) >= 0 ) labels.push_back(cl->GetLabel(0)) ;
650     if ( cl->GetLabel(1) >= 0 ) labels.push_back(cl->GetLabel(1)) ;
651     if ( cl->GetLabel(2) >= 0 ) labels.push_back(cl->GetLabel(2)) ;
652   }
653   std::sort( labels.begin(), labels.end() );      
654   labels.push_back( -1 ); // put -1 to the end    
655   int labelMax = -1, labelCur = -1, nLabelsMax = 0, nLabelsCurr = 0;
656   for ( unsigned int iLab = 0; iLab < labels.size(); iLab++ ) {
657     if ( labels[iLab] != labelCur ) {         
658       if ( labelCur >= 0 && nLabelsMax< nLabelsCurr ) {
659         nLabelsMax = nLabelsCurr;
660         labelMax = labelCur;
661       }
662       labelCur = labels[iLab];
663       nLabelsCurr = 0;
664     }
665     nLabelsCurr++;
666   }
667           
668   if( labelMax>=0 && nLabelsMax < wrong * nClusters ) labelMax = -labelMax;
669   
670   mcLabel = labelMax;
671                 
672   track->SetLabel( mcLabel );
673 }
674
675
676
677
678
679
680
681
682
683 //------------------------------------------------------------------------
684 void AliITStrackerHLT::UpdateESDtrack(AliESDtrack *tESD, AliHLTITSTrack* track, ULong_t flags) const
685 {
686   //
687   // Update ESD track
688   //
689   tESD->UpdateTrackParams(track,flags);
690   AliHLTITSTrack * oldtrack = (AliHLTITSTrack*)(tESD->GetITStrack());
691   if (oldtrack) delete oldtrack; 
692   tESD->SetITStrack(new AliHLTITSTrack(*track));
693 }
694
695
696
697
698 //------------------------------------------------------------------------
699 void AliITStrackerHLT::BuildMaterialLUT(TString material) {
700   //--------------------------------------------------------------------
701   // Fill a look-up table with mean material
702   //--------------------------------------------------------------------
703
704   Int_t n=1000;
705   Double_t mparam[7];
706   Double_t point1[3],point2[3];
707   Double_t phi,cosphi,sinphi,z;
708   // 0-5 layers, 6 pipe, 7-8 shields 
709   Double_t rmin[9]={ 3.5, 5.5,13.0,22.0,35.0,41.0, 2.0, 8.0,25.0};
710   Double_t rmax[9]={ 5.5, 8.0,17.0,26.0,41.0,47.0, 3.0,10.5,30.0};
711
712   Int_t ifirst=0,ilast=0;  
713   if(material.Contains("Pipe")) {
714     ifirst=6; ilast=6;
715   } else if(material.Contains("Shields")) {
716     ifirst=7; ilast=8;
717   } else if(material.Contains("Layers")) {
718     ifirst=0; ilast=5;
719   } else {
720     Error("BuildMaterialLUT","Wrong layer name\n");
721   }
722
723   for(Int_t imat=ifirst; imat<=ilast; imat++) {
724     Double_t param[5]={0.,0.,0.,0.,0.};
725     for (Int_t i=0; i<n; i++) {
726       phi = 2.*TMath::Pi()*gRandom->Rndm();
727       cosphi = TMath::Cos(phi); sinphi = TMath::Sin(phi); 
728       z = 14.*(-1.+2.*gRandom->Rndm()); // SPD barrel
729       point1[0] = rmin[imat]*cosphi;
730       point1[1] = rmin[imat]*sinphi;
731       point1[2] = z;
732       point2[0] = rmax[imat]*cosphi;
733       point2[1] = rmax[imat]*sinphi;
734       point2[2] = z;
735       AliTracker::MeanMaterialBudget(point1,point2,mparam);
736       for(Int_t j=0;j<5;j++) param[j]+=mparam[j];
737     }
738     for(Int_t j=0;j<5;j++) param[j]/=(Float_t)n;
739     if(imat<=5) {
740       fxOverX0Layer[imat] = param[1];
741       fxTimesRhoLayer[imat] = param[0]*param[4];
742     } else if(imat==6) {
743       fxOverX0Pipe = param[1];
744       fxTimesRhoPipe = param[0]*param[4];
745     } else if(imat==7) {
746       fxOverX0Shield[0] = param[1];
747       fxTimesRhoShield[0] = param[0]*param[4];
748     } else if(imat==8) {
749       fxOverX0Shield[1] = param[1];
750       fxTimesRhoShield[1] = param[0]*param[4];
751     }
752   }
753   /*
754   printf("%s\n",material.Data());
755   printf("%f  %f\n",fxOverX0Pipe,fxTimesRhoPipe);
756   printf("%f  %f\n",fxOverX0Shield[0],fxTimesRhoShield[0]);
757   printf("%f  %f\n",fxOverX0Shield[1],fxTimesRhoShield[1]);
758   printf("%f  %f\n",fxOverX0Layer[0],fxTimesRhoLayer[0]);
759   printf("%f  %f\n",fxOverX0Layer[1],fxTimesRhoLayer[1]);
760   printf("%f  %f\n",fxOverX0Layer[2],fxTimesRhoLayer[2]);
761   printf("%f  %f\n",fxOverX0Layer[3],fxTimesRhoLayer[3]);
762   printf("%f  %f\n",fxOverX0Layer[4],fxTimesRhoLayer[4]);
763   printf("%f  %f\n",fxOverX0Layer[5],fxTimesRhoLayer[5]);
764   */
765   return;
766 }
767
768
769
770
771 //------------------------------------------------------------------------
772 Int_t AliITStrackerHLT::CorrectForTPCtoITSDeadZoneMaterial(AliHLTITSTrack *t) {
773   //--------------------------------------------------------------------
774   // Correction for the material between the TPC and the ITS
775   //--------------------------------------------------------------------
776   if (t->GetX() > AliITSRecoParam::Getriw()) {   // inward direction 
777       if (!t->PropagateToTGeo(AliITSRecoParam::Getriw(),1)) return 0;// TPC inner wall
778       if (!t->PropagateToTGeo(AliITSRecoParam::Getrcd(),1)) return 0;// TPC central drum
779       if (!t->PropagateToTGeo(AliITSRecoParam::Getrs(),1))  return 0;// ITS screen
780   } else if (t->GetX() < AliITSRecoParam::Getrs()) {  // outward direction
781       if (!t->PropagateToTGeo(AliITSRecoParam::Getrs(),1))        return 0;// ITS screen
782       if (!t->PropagateToTGeo(AliITSRecoParam::Getrcd(),1))       return 0;// TPC central drum
783       if (!t->PropagateToTGeo(AliITSRecoParam::Getriw()+0.001,1)) return 0;// TPC inner wall
784   } else {
785     printf("CorrectForTPCtoITSDeadZoneMaterial: Track is already in the dead zone !\n");
786     return 0;
787   }
788   
789   return 1;
790 }
791
792
793 //------------------------------------------------------------------------
794 Int_t AliITStrackerHLT::CorrectForPipeMaterial(AliHLTITSTrack *t,
795                                                bool InwardDirection) {
796   //-------------------------------------------------------------------
797   // Propagate beyond beam pipe and correct for material
798   // (material budget in different ways according to fUseTGeo value)
799   // Add time if going outward (PropagateTo or PropagateToTGeo)
800   //-------------------------------------------------------------------
801
802   // Define budget mode:
803   // 0: material from AliITSRecoParam (hard coded)
804   // 1: material from TGeo in one step (on the fly)
805   // 2: material from lut
806   // 3: material from TGeo in one step (same for all hypotheses)
807   Int_t mode;
808   switch(fUseTGeo) {
809   case 0:
810     mode=0; 
811     break;    
812   case 1:
813     mode=1;
814     break;    
815   case 2:
816     mode=2;
817     break;
818   case 3:
819     mode=3; 
820     break;
821   case 4:
822     mode=3;
823     break;
824   default:
825     mode=0;
826     break;
827   }
828   
829   Float_t  dir = (InwardDirection ? 1. : -1.);
830   Double_t rToGo= ( InwardDirection ? AliITSRecoParam::GetrInsidePipe() : AliITSRecoParam::GetrOutsidePipe());
831   Double_t xToGo, phi,z;
832
833   if (!t->GetLocalXPhiZat(rToGo,xToGo,phi,z)) return 0;
834
835   Double_t xOverX0,x0,lengthTimesMeanDensity;
836
837   switch(mode) {
838   case 0:
839     xOverX0 = AliITSRecoParam::GetdPipe();
840     x0 = AliITSRecoParam::GetX0Be();
841     lengthTimesMeanDensity = xOverX0*x0;
842     lengthTimesMeanDensity *= dir;
843     if (!t->PropagateTo(xToGo,xOverX0,lengthTimesMeanDensity/xOverX0)) return 0;
844     break;
845   case 1:
846     if (!t->PropagateToTGeo(xToGo,1)) return 0;
847     break;
848   case 2:
849     if(fxOverX0Pipe<0) BuildMaterialLUT("Pipe");  
850     xOverX0 = fxOverX0Pipe;
851     lengthTimesMeanDensity = fxTimesRhoPipe;
852     lengthTimesMeanDensity *= dir;
853     if (!t->PropagateTo(xToGo,xOverX0,lengthTimesMeanDensity/xOverX0)) return 0;
854     break;
855   case 3:
856     double xOverX0PipeTrks, xTimesRhoPipeTrks;
857     if (!t->PropagateToTGeo(xToGo,1,xOverX0,lengthTimesMeanDensity)) return 0;
858     Double_t angle=TMath::Sqrt((1.+t->GetTgl()*t->GetTgl())/
859                                ((1.-t->GetSnp())*(1.+t->GetSnp())));
860     xOverX0PipeTrks = TMath::Abs(xOverX0)/angle;
861     xTimesRhoPipeTrks = TMath::Abs(lengthTimesMeanDensity)/angle;
862     xOverX0 = xOverX0PipeTrks;
863     lengthTimesMeanDensity = xTimesRhoPipeTrks;
864     lengthTimesMeanDensity *= dir;
865     if (!t->PropagateTo(xToGo,xOverX0,lengthTimesMeanDensity/xOverX0)) return 0;
866     break;
867   }
868   
869   return 1;
870 }
871 //------------------------------------------------------------------------
872 Int_t AliITStrackerHLT::CorrectForShieldMaterial(AliHLTITSTrack *t,
873                                                  Int_t    shieldindex,
874                                                 bool InwardDirection) {
875   //-------------------------------------------------------------------
876   // Propagate beyond SPD or SDD shield and correct for material
877   // (material budget in different ways according to fUseTGeo value)
878   // Add time if going outward (PropagateTo or PropagateToTGeo)
879   //-------------------------------------------------------------------
880
881   // Define budget mode:
882   // 0: material from AliITSRecoParam (hard coded)
883   // 1: material from TGeo in steps of X cm (on the fly)
884   //    X = AliITSRecoParam::GetStepSizeTGeo()
885   // 2: material from lut
886   // 3: material from TGeo in one step (same for all hypotheses)
887   Int_t mode;
888   switch(fUseTGeo) {
889   case 0:
890     mode=0; 
891     break;    
892   case 1:
893     mode=1;
894     break;    
895   case 2:
896     mode=2;
897     break;
898   case 3:
899     mode=3;
900     break;
901   case 4:
902     mode=3;
903     break;
904   default:
905     mode=0;
906     break;
907   }
908
909
910   Float_t  dir = (InwardDirection ? 1. : -1.);
911   Double_t rToGo;
912
913   if (shieldindex==1 ) { // SDDouter
914     rToGo=(InwardDirection ? AliITSRecoParam::GetrInsideShield(1) : AliITSRecoParam::GetrOutsideShield(1));
915   } else if (shieldindex==0 ) {        // SPDouter
916     rToGo=(InwardDirection ? AliITSRecoParam::GetrInsideShield(0) : AliITSRecoParam::GetrOutsideShield(0)); 
917   } else {
918     Error("CorrectForShieldMaterial"," Wrong shield name\n");
919     return 0;
920   }
921   Double_t xToGo, phi,z;
922
923   if (!t->GetLocalXPhiZat(rToGo,xToGo,phi,z)) return 0;
924
925   Double_t xOverX0,x0,lengthTimesMeanDensity;
926   Int_t nsteps=1;
927
928   switch(mode) {
929   case 0:
930     xOverX0 = AliITSRecoParam::Getdshield(shieldindex);
931     x0 = AliITSRecoParam::GetX0shield(shieldindex);
932     lengthTimesMeanDensity = xOverX0*x0;
933     lengthTimesMeanDensity *= dir;
934     if (!t->PropagateTo(xToGo,xOverX0,lengthTimesMeanDensity/xOverX0)) return 0;
935     break;
936   case 1:
937     nsteps= (Int_t)(TMath::Abs(t->GetX()-xToGo)/fRecoParam->GetStepSizeTGeo())+1;
938     if (!t->PropagateToTGeo(xToGo,nsteps)) return 0; // cross the material and apply correction
939     break;
940   case 2:
941     if(fxOverX0Shield[shieldindex]<0) BuildMaterialLUT("Shields");  
942     xOverX0 = fxOverX0Shield[shieldindex];
943     lengthTimesMeanDensity = fxTimesRhoShield[shieldindex];
944     lengthTimesMeanDensity *= dir;
945     if (!t->PropagateTo(xToGo,xOverX0,lengthTimesMeanDensity/xOverX0)) return 0;
946     break;
947   case 3:    
948     if (!t->PropagateToTGeo(xToGo,1,xOverX0,lengthTimesMeanDensity)) return 0;
949     Double_t angle=TMath::Sqrt((1.+t->GetTgl()*t->GetTgl())/
950                                ((1.-t->GetSnp())*(1.+t->GetSnp())));
951     double xOverX0ShieldTrks = TMath::Abs(xOverX0)/angle;
952     double xTimesRhoShieldTrks = TMath::Abs(lengthTimesMeanDensity)/angle;
953     xOverX0 = xOverX0ShieldTrks;
954     lengthTimesMeanDensity = xTimesRhoShieldTrks;
955     lengthTimesMeanDensity *= dir;
956     if (!t->PropagateTo(xToGo,xOverX0,lengthTimesMeanDensity/xOverX0)) return 0;
957     break;
958   }
959
960   return 1;
961 }
962 //------------------------------------------------------------------------
963 Int_t AliITStrackerHLT::CorrectForLayerMaterial(AliHLTITSTrack *t,
964                                                Int_t layerindex,
965                                                bool InwardDirection ){
966   //-------------------------------------------------------------------
967   // Propagate beyond layer and correct for material
968   // (material budget in different ways according to fUseTGeo value)
969   // Add time if going outward (PropagateTo or PropagateToTGeo)
970   //-------------------------------------------------------------------
971
972   /*
973     Double_t r=fLayers[layerindex].GetR();
974     Double_t deltar=(layerindex<2 ? 0.10*r : 0.05*r);
975     Double_t rToGo=TMath::Sqrt(t->GetX()*t->GetX()+t->GetY()*t->GetY());
976     rToGo+= InwardDirection ?-deltar :deltar;
977     Double_t xToGo;
978     if (!t->GetLocalXat(rToGo,xToGo)) return 0;  
979   */
980   
981   if(fxOverX0Layer[layerindex]<0) BuildMaterialLUT("Layers");
982
983   Double_t lengthTimesMeanDensity = fxTimesRhoLayer[layerindex];
984   if( !InwardDirection ) lengthTimesMeanDensity = -lengthTimesMeanDensity;
985
986   return t->CorrectForMeanMaterial(fxOverX0Layer[layerindex],lengthTimesMeanDensity,kTRUE);
987 }
988
989
990 //------------------------------------------------------------------------
991 Bool_t AliITStrackerHLT::LocalModuleCoord(Int_t ilayer,Int_t idet,
992                                        const AliHLTITSTrack *track,
993                                        Float_t &xloc,Float_t &zloc) const {
994   //-----------------------------------------------------------------
995   // Gives position of track in local module ref. frame
996   //-----------------------------------------------------------------
997
998   xloc=0.; 
999   zloc=0.;
1000
1001   if(idet<0) return kFALSE;
1002
1003   Int_t ndet=AliITSgeomTGeo::GetNDetectors(ilayer+1); // layers from 1 to 6 
1004
1005   Int_t lad = Int_t(idet/ndet) + 1;
1006
1007   Int_t det = idet - (lad-1)*ndet + 1;
1008
1009   Double_t xyzGlob[3],xyzLoc[3];
1010
1011   AliHLTITSDetector &detector = fLayers[ilayer].GetDetector(idet);
1012   // take into account the misalignment: xyz at real detector plane
1013   if(!track->GetXYZAt(detector.GetRmisal(),GetBz(),xyzGlob)) return kFALSE;
1014
1015   if(!AliITSgeomTGeo::GlobalToLocal(ilayer+1,lad,det,xyzGlob,xyzLoc)) return kFALSE;
1016
1017   xloc = (Float_t)xyzLoc[0];
1018   zloc = (Float_t)xyzLoc[2];
1019
1020   return kTRUE;
1021 }
1022
1023 //------------------------------------------------------------------------
1024 Bool_t AliITStrackerHLT::ComputeRoad(AliHLTITSTrack* track,Int_t ilayer,Int_t idet,Double_t &zmin,Double_t &zmax,Double_t &ymin,Double_t &ymax) const {
1025   //--------------------------------------------------------------------
1026   // This function computes the rectangular road for this track
1027   //--------------------------------------------------------------------
1028
1029
1030   AliHLTITSDetector &det = fLayers[ilayer].GetDetector(idet);
1031   // take into account the misalignment: propagate track to misaligned detector plane
1032
1033   double y,z,snp,cov[3];
1034   if( !track->GetYZAtPhiX( det.GetPhi(),det.GetRmisal(), y, z, snp, cov))return 0;
1035
1036
1037   Double_t dz=fRecoParam->GetNSigmaRoadZ()*
1038                     TMath::Sqrt(cov[2] + 
1039                                 fRecoParam->GetNSigmaZLayerForRoadZ()*
1040                                 fRecoParam->GetNSigmaZLayerForRoadZ()*
1041                                 fRecoParam->GetSigmaZ2(ilayer));
1042   Double_t dy=fRecoParam->GetNSigmaRoadY()*
1043                     TMath::Sqrt(cov[0] + 
1044                                 fRecoParam->GetNSigmaYLayerForRoadY()*
1045                                 fRecoParam->GetNSigmaYLayerForRoadY()*
1046                                 fRecoParam->GetSigmaY2(ilayer));
1047       
1048   // track at boundary between detectors, enlarge road
1049   Double_t boundaryWidth=AliITSRecoParam::GetBoundaryWidth();
1050   if ( (y-dy < det.GetYmin()+boundaryWidth) || 
1051        (y+dy > det.GetYmax()-boundaryWidth) || 
1052        (z-dz < det.GetZmin()+boundaryWidth) ||
1053        (z+dz > det.GetZmax()-boundaryWidth) ) {
1054     Float_t tgl = TMath::Abs(track->GetTgl());
1055     if (tgl > 1.) tgl=1.;
1056     Double_t deltaXNeighbDets=AliITSRecoParam::GetDeltaXNeighbDets();
1057     dz = TMath::Sqrt(dz*dz+deltaXNeighbDets*deltaXNeighbDets*tgl*tgl);
1058     if (snp > fRecoParam->GetMaxSnp()) return kFALSE;
1059     dy = TMath::Sqrt(dy*dy+deltaXNeighbDets*deltaXNeighbDets*snp*snp);
1060   } // boundary
1061   
1062   // add to the road a term (up to 2-3 mm) to deal with misalignments
1063   dy = TMath::Sqrt(dy*dy + fRecoParam->GetRoadMisal()*fRecoParam->GetRoadMisal());
1064   dz = TMath::Sqrt(dz*dz + fRecoParam->GetRoadMisal()*fRecoParam->GetRoadMisal());
1065
1066   Double_t r = fLayers[ilayer].GetR();
1067   zmin = z - dz; 
1068   zmax = z + dz;
1069   ymin = y + r*det.GetPhi() - dy;
1070   ymax = y + r*det.GetPhi() + dy;
1071
1072   return kTRUE;
1073 }
1074
1075
1076
1077 Int_t AliITStrackerHLT::PropagateBack(AliESDEvent * /*event*/) 
1078 {
1079   // dummy
1080   return 0;
1081 }
1082
1083 Int_t AliITStrackerHLT::RefitInward(AliESDEvent * /*event*/ ) 
1084 {
1085   // dummy
1086   return 0;
1087 }
1088
1089
1090 Bool_t AliITStrackerHLT::GetTrackPoint(Int_t /*index*/, AliTrackPoint& /*p*/) const 
1091 {
1092   // dummy
1093   return 0;
1094 }
1095
1096 Bool_t AliITStrackerHLT::GetTrackPointTrackingError(Int_t /*index*/, 
1097                                                     AliTrackPoint& /*p*/, const AliESDtrack */*t*/) 
1098 {
1099   // dummy
1100   return 0;
1101 }