]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTracker.cxx
Adding the SSD absolute residual histos.
[u/mrichter/AliRoot.git] / STEER / AliTracker.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //-------------------------------------------------------------------------
19 //               Implementation of the AliTracker class
20 //  that is the base for AliTPCtracker, AliITStrackerV2 and AliTRDtracker    
21 //        Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
22 //-------------------------------------------------------------------------
23 #include <TClass.h>
24 #include <TMath.h>
25 #include <TH1F.h>
26 #include <TGeoManager.h>
27
28 #include "AliMagF.h"
29 #include "AliTracker.h"
30 #include "AliGeomManager.h"
31 #include "AliCluster.h"
32 #include "AliKalmanTrack.h"
33
34 extern TGeoManager *gGeoManager;
35
36 Bool_t AliTracker::fgUniformField=kTRUE;
37 Double_t AliTracker::fgBz=kAlmost0Field;
38 const AliMagF *AliTracker::fgkFieldMap=0;
39 Bool_t AliTracker::fFillResiduals=kFALSE;
40 TObjArray *AliTracker::fResiduals=0;
41
42 ClassImp(AliTracker)
43
44 AliTracker::AliTracker():
45   TObject(),
46   fX(0),
47   fY(0),
48   fZ(0),
49   fSigmaX(0.005),
50   fSigmaY(0.005),
51   fSigmaZ(0.010)
52 {
53   //--------------------------------------------------------------------
54   // The default constructor.
55   //--------------------------------------------------------------------
56   if (!fgkFieldMap) AliWarning("Field map is not set. Call AliTracker::SetFieldMap before creating a tracker!");
57 }
58
59 //__________________________________________________________________________
60 AliTracker::AliTracker(const AliTracker &atr):
61   TObject(atr),
62   fX(atr.fX),
63   fY(atr.fY),
64   fZ(atr.fZ),
65   fSigmaX(atr.fSigmaX),
66   fSigmaY(atr.fSigmaY),
67   fSigmaZ(atr.fSigmaZ)
68 {
69   //--------------------------------------------------------------------
70   // The default constructor.
71   //--------------------------------------------------------------------
72   if (!fgkFieldMap) AliWarning("Field map is not set. Call AliTracker::SetFieldMap before creating a tracker!");
73 }
74
75 //__________________________________________________________________________
76 void AliTracker::SetFieldMap(const AliMagF* map, Bool_t uni) {
77   //--------------------------------------------------------------------
78   //This passes the field map to the reconstruction.
79   //--------------------------------------------------------------------
80   if (map==0) AliFatalClass("Can't access the field map !");
81
82   if (fgkFieldMap) {
83      AliWarningClass("The magnetic field map has been already set !");
84      return;
85   }
86
87   fgUniformField=uni;
88   fgkFieldMap=map;
89
90   //Float_t r[3]={0.,0.,0.},b[3]; map->Field(r,b);
91   //Double_t bz=-b[2];
92  
93   Double_t bz=-map->SolenoidField();
94   fgBz=TMath::Sign(kAlmost0Field,bz) + bz;
95
96 }
97
98 //__________________________________________________________________________
99 void AliTracker::FillClusterArray(TObjArray* /*array*/) const
100 {
101   // Publishes all pointers to clusters known to the tracker into the
102   // passed object array.
103   // The ownership is not transfered - the caller is not expected to delete
104   // the clusters.
105
106   AliWarning("should be overriden by a sub-class.");
107 }
108
109 //__________________________________________________________________________
110 void AliTracker::CookLabel(AliKalmanTrack *t, Float_t wrong) const {
111   //--------------------------------------------------------------------
112   //This function "cooks" a track label. If label<0, this track is fake.
113   //--------------------------------------------------------------------
114   Int_t noc=t->GetNumberOfClusters();
115   if (noc<1) return;
116   Int_t *lb=new Int_t[noc];
117   Int_t *mx=new Int_t[noc];
118   AliCluster **clusters=new AliCluster*[noc];
119
120   Int_t i;
121   for (i=0; i<noc; i++) {
122      lb[i]=mx[i]=0;
123      Int_t index=t->GetClusterIndex(i);
124      clusters[i]=GetCluster(index);
125   }
126
127   Int_t lab=123456789;
128   for (i=0; i<noc; i++) {
129     AliCluster *c=clusters[i];
130     lab=TMath::Abs(c->GetLabel(0));
131     Int_t j;
132     for (j=0; j<noc; j++) if (lb[j]==lab || mx[j]==0) break;
133     lb[j]=lab;
134     (mx[j])++;
135   }
136
137   Int_t max=0;
138   for (i=0; i<noc; i++) if (mx[i]>max) {max=mx[i]; lab=lb[i];}
139     
140   for (i=0; i<noc; i++) {
141     AliCluster *c=clusters[i];
142     //if (TMath::Abs(c->GetLabel(1)) == lab ||
143     //    TMath::Abs(c->GetLabel(2)) == lab ) max++;
144     if (TMath::Abs(c->GetLabel(0)!=lab))
145         if (TMath::Abs(c->GetLabel(1)) == lab ||
146             TMath::Abs(c->GetLabel(2)) == lab ) max++;
147   }
148
149   if ((1.- Float_t(max)/noc) > wrong) lab=-lab;
150   t->SetFakeRatio((1.- Float_t(max)/noc));
151   t->SetLabel(lab);
152
153   delete[] lb;
154   delete[] mx;
155   delete[] clusters;
156 }
157
158 //____________________________________________________________________________
159 void AliTracker::UseClusters(const AliKalmanTrack *t, Int_t from) const {
160   //------------------------------------------------------------------
161   //This function marks clusters associated with the track.
162   //------------------------------------------------------------------
163   Int_t noc=t->GetNumberOfClusters();
164   for (Int_t i=from; i<noc; i++) {
165      Int_t index=t->GetClusterIndex(i);
166      AliCluster *c=GetCluster(index); 
167      c->Use();   
168   }
169 }
170
171 Double_t AliTracker::GetBz(Float_t *r) {
172   //------------------------------------------------------------------
173   // Returns Bz (kG) at the point "r" .
174   //------------------------------------------------------------------
175     Float_t b[3]; fgkFieldMap->Field(r,b);
176     Double_t bz=-Double_t(b[2]);
177     return  (TMath::Sign(kAlmost0Field,bz) + bz);
178 }
179
180 Double_t 
181 AliTracker::MeanMaterialBudget(Double_t *start,Double_t *end,Double_t *mparam)
182 {
183   // 
184   // Calculate mean material budget and material properties between 
185   //    the points "start" and "end".
186   //
187   // "mparam" - parameters used for the energy and multiple scattering
188   //  corrections: 
189   //
190   // mparam[0] - mean density: sum(x_i*rho_i)/sum(x_i) [g/cm3]
191   // mparam[1] - equivalent rad length fraction: sum(x_i/X0_i) [adimensional]
192   // mparam[2] - mean A: sum(x_i*A_i)/sum(x_i) [adimensional]
193   // mparam[3] - mean Z: sum(x_i*Z_i)/sum(x_i) [adimensional]
194   // mparam[4] - length: sum(x_i) [cm]
195   // mparam[5] - Z/A mean: sum(x_i*Z_i/A_i)/sum(x_i) [adimensional]
196   // mparam[6] - number of boundary crosses
197   //
198   //  Origin:  Marian Ivanov, Marian.Ivanov@cern.ch
199   //
200   //  Corrections and improvements by
201   //        Andrea Dainese, Andrea.Dainese@lnl.infn.it,
202   //        Andrei Gheata,  Andrei.Gheata@cern.ch
203   //
204
205   mparam[0]=0; mparam[1]=1; mparam[2] =0; mparam[3] =0;
206   mparam[4]=0; mparam[5]=0; mparam[6]=0;
207   //
208   Double_t bparam[6]; // total parameters
209   Double_t lparam[6]; // local parameters
210
211   for (Int_t i=0;i<6;i++) bparam[i]=0;
212
213   if (!gGeoManager) {
214     printf("ERROR: no TGeo\n");
215     return 0.;
216   }
217   //
218   Double_t length;
219   Double_t dir[3];
220   length = TMath::Sqrt((end[0]-start[0])*(end[0]-start[0])+
221                        (end[1]-start[1])*(end[1]-start[1])+
222                        (end[2]-start[2])*(end[2]-start[2]));
223   mparam[4]=length;
224   if (length<TGeoShape::Tolerance()) return 0.0;
225   Double_t invlen = 1./length;
226   dir[0] = (end[0]-start[0])*invlen;
227   dir[1] = (end[1]-start[1])*invlen;
228   dir[2] = (end[2]-start[2])*invlen;
229
230   // Initialize start point and direction
231   TGeoNode *currentnode = 0;
232   TGeoNode *startnode = gGeoManager->InitTrack(start, dir);
233   //printf("%s length=%f\n",gGeoManager->GetPath(),length);
234   if (!startnode) {
235     AliErrorClass(Form("start point out of geometry: x %f, y %f, z %f",
236                   start[0],start[1],start[2]));
237     return 0.0;
238   }
239   TGeoMaterial *material = startnode->GetVolume()->GetMedium()->GetMaterial();
240   lparam[0]   = material->GetDensity();
241   lparam[1]   = material->GetRadLen();
242   lparam[2]   = material->GetA();
243   lparam[3]   = material->GetZ();
244   lparam[4]   = length;
245   lparam[5]   = lparam[3]/lparam[2];
246   if (material->IsMixture()) {
247     TGeoMixture * mixture = (TGeoMixture*)material;
248     lparam[5] =0;
249     Double_t sum =0;
250     for (Int_t iel=0;iel<mixture->GetNelements();iel++){
251       sum  += mixture->GetWmixt()[iel];
252       lparam[5]+= mixture->GetZmixt()[iel]*mixture->GetWmixt()[iel]/mixture->GetAmixt()[iel];
253     }
254     lparam[5]/=sum;
255   }
256
257   // Locate next boundary within length without computing safety.
258   // Propagate either with length (if no boundary found) or just cross boundary
259   gGeoManager->FindNextBoundaryAndStep(length, kFALSE);
260   Double_t step = 0.0; // Step made
261   Double_t snext = gGeoManager->GetStep();
262   // If no boundary within proposed length, return current density
263   if (!gGeoManager->IsOnBoundary()) {
264     mparam[0] = lparam[0];
265     mparam[1] = lparam[4]/lparam[1];
266     mparam[2] = lparam[2];
267     mparam[3] = lparam[3];
268     mparam[4] = lparam[4];
269     return lparam[0];
270   }
271   // Try to cross the boundary and see what is next
272   Int_t nzero = 0;
273   while (length>TGeoShape::Tolerance()) {
274     currentnode = gGeoManager->GetCurrentNode();
275     if (snext<2.*TGeoShape::Tolerance()) nzero++;
276     else nzero = 0;
277     if (nzero>3) {
278       // This means navigation has problems on one boundary
279       // Try to cross by making a small step
280       printf("ERROR: cannot cross boundary\n");
281       mparam[0] = bparam[0]/step;
282       mparam[1] = bparam[1];
283       mparam[2] = bparam[2]/step;
284       mparam[3] = bparam[3]/step;
285       mparam[5] = bparam[5]/step;
286       mparam[4] = step;
287       mparam[0] = 0.;             // if crash of navigation take mean density 0
288       mparam[1] = 1000000;        // and infinite rad length
289       return bparam[0]/step;
290     }
291     mparam[6]+=1.;
292     step += snext;
293     bparam[1]    += snext/lparam[1];
294     bparam[2]    += snext*lparam[2];
295     bparam[3]    += snext*lparam[3];
296     bparam[5]    += snext*lparam[5];
297     bparam[0]    += snext*lparam[0];
298
299     if (snext>=length) break;
300     if (!currentnode) break;
301     length -= snext;
302     //printf("%s snext=%f length=%f\n", currentnode->GetName(),snext,length);
303     material = currentnode->GetVolume()->GetMedium()->GetMaterial();
304     lparam[0] = material->GetDensity();
305     lparam[1]  = material->GetRadLen();
306     lparam[2]  = material->GetA();
307     lparam[3]  = material->GetZ();
308     //printf("       %f %f %f %f\n",lparam[0],lparam[1],lparam[2],lparam[3]); 
309     lparam[5]   = lparam[3]/lparam[2];
310     if (material->IsMixture()) {
311       TGeoMixture * mixture = (TGeoMixture*)material;
312       lparam[5]=0;
313       Double_t sum =0;
314       for (Int_t iel=0;iel<mixture->GetNelements();iel++){
315         sum+= mixture->GetWmixt()[iel];
316         lparam[5]+= mixture->GetZmixt()[iel]*mixture->GetWmixt()[iel]/mixture->GetAmixt()[iel];
317       }
318       lparam[5]/=sum;
319     }
320     gGeoManager->FindNextBoundaryAndStep(length, kFALSE);
321     snext = gGeoManager->GetStep();
322     //printf("snext %f\n",snext);
323   }
324   mparam[0] = bparam[0]/step;
325   mparam[1] = bparam[1];
326   mparam[2] = bparam[2]/step;
327   mparam[3] = bparam[3]/step;
328   mparam[5] = bparam[5]/step;
329   return bparam[0]/step;
330 }
331
332
333 Bool_t 
334 AliTracker::PropagateTrackTo(AliExternalTrackParam *track, Double_t xToGo, 
335 Double_t mass, Double_t maxStep, Bool_t rotateTo, Double_t maxSnp){
336   //----------------------------------------------------------------
337   //
338   // Propagates the track to the plane X=xk (cm) using the magnetic field map 
339   // and correcting for the crossed material.
340   //
341   // mass     - mass used in propagation - used for energy loss correction
342   // maxStep  - maximal step for propagation
343   //
344   //  Origin: Marian Ivanov,  Marian.Ivanov@cern.ch
345   //
346   //----------------------------------------------------------------
347   const Double_t kEpsilon = 0.00001;
348   Double_t xpos     = track->GetX();
349   Double_t dir      = (xpos<xToGo) ? 1.:-1.;
350   //
351   while ( (xToGo-xpos)*dir > kEpsilon){
352     Double_t step = dir*TMath::Min(TMath::Abs(xToGo-xpos), maxStep);
353     Double_t x    = xpos+step;
354     Double_t xyz0[3],xyz1[3],param[7];
355     track->GetXYZ(xyz0);   //starting global position
356
357     Double_t bz=GetBz(xyz0); // getting the local Bz
358
359     if (!track->GetXYZAt(x,bz,xyz1)) return kFALSE;   // no prolongation
360     xyz1[2]+=kEpsilon; // waiting for bug correction in geo
361
362     if (TMath::Abs(track->GetSnpAt(x,bz)) >= maxSnp) return kFALSE;
363     if (!track->PropagateTo(x,bz))  return kFALSE;
364
365     MeanMaterialBudget(xyz0,xyz1,param);        
366     Double_t xrho=param[0]*param[4], xx0=param[1];
367
368     if (!track->CorrectForMeanMaterial(xx0,xrho,mass)) return kFALSE;
369     if (rotateTo){
370       if (TMath::Abs(track->GetSnp()) >= maxSnp) return kFALSE;
371       track->GetXYZ(xyz0);   // global position
372       Double_t alphan = TMath::ATan2(xyz0[1], xyz0[0]); 
373       //
374       Double_t ca=TMath::Cos(alphan-track->GetAlpha()), 
375                sa=TMath::Sin(alphan-track->GetAlpha());
376       Double_t sf=track->GetSnp(), cf=TMath::Sqrt(1.- sf*sf);
377       Double_t sinNew =  sf*ca - cf*sa;
378       if (TMath::Abs(sinNew) >= maxSnp) return kFALSE;
379       if (!track->Rotate(alphan)) return kFALSE;
380     }
381     xpos = track->GetX();
382   }
383   return kTRUE;
384 }
385
386 void AliTracker::FillResiduals(const AliExternalTrackParam *t,
387                               Double_t *p, Double_t *cov, 
388                               UShort_t id, Bool_t updated) {
389   //
390   // This function fills the histograms of residuals 
391   // The array of these histos is external for this AliTracker class.
392   // Normally, this array belong to AliGlobalQADataMaker class.  
393   // 
394   if (!fFillResiduals) return; 
395   if (!fResiduals) return; 
396
397   const Double_t *residuals=t->GetResiduals(p,cov,updated);
398   if (!residuals) return;
399
400   TH1F *h=0;
401   AliGeomManager::ELayerID layer=AliGeomManager::VolUIDToLayer(id);
402   h=(TH1F*)fResiduals->At(2*layer-2);
403   h->Fill(residuals[0]);
404   h=(TH1F*)fResiduals->At(2*layer-1);
405   h->Fill(residuals[1]);
406
407   if (layer==5) {
408     if (p[1]<0)                      // Absolute residual
409        h=(TH1F*)fResiduals->At(40);  // SSD1, C side
410     else                             // 
411        h=(TH1F*)fResiduals->At(41);  // SSD1, A side
412     h->Fill(residuals[1]);           
413   }
414   if (layer==6) {
415     if (p[1]<0)                      // Absolute residual
416        h=(TH1F*)fResiduals->At(42);  // SSD2, C side
417     else                             // 
418        h=(TH1F*)fResiduals->At(43);  // SSD2, A side
419     h->Fill(residuals[1]);       
420   }
421
422 }
423