Implemented new class to read raw data and published TOF cluster class
[u/mrichter/AliRoot.git] / TOF / AliTOFtracker.cxx
CommitLineData
596a855f 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 **************************************************************************/
74ea065c 15// AliTOFtracker Class
16// Task: Perform association of the ESD tracks to TOF Clusters
17// and Update ESD track with associated TOF Cluster parameters
18//
19// -- Authors : S. Arcelli, C. Zampolli (Bologna University and INFN)
20// -- Contacts: Annalisa.De.Caro@cern.ch
21// -- : Chiara.Zampolli@bo.infn.it
22// -- : Silvia.Arcelli@bo.infn.it
23//--------------------------------------------------------------------
596a855f 24
2871e58d 25#include <Rtypes.h>
571dda3d 26
74ea065c 27#include "TClonesArray.h"
571dda3d 28
d076c8d5 29#include "AliLog.h"
74ea065c 30#include "AliRun.h"
31#include "AliModule.h"
596a855f 32
571dda3d 33#include "AliTOFdigit.h"
34#include "AliTOFcluster.h"
35#include "AliTOFtrack.h"
36#include "AliTOFGeometry.h"
37#include "AliTOFtracker.h"
38
596a855f 39ClassImp(AliTOFtracker)
40
74ea065c 41//_____________________________________________________________________________
42AliTOFtracker::AliTOFtracker(AliTOFGeometry * geom, Double_t parPID[2]) {
43 //AliTOFtracker main Ctor
44
45 fHoles=true;
46 fNseeds=0;
47 fNseedsTOF=0;
48 fngoodmatch=0;
49 fnbadmatch=0;
50 fnunmatch=0;
51 fnmatch=0;
52 fGeom = geom;
53 fTOFpid = new AliTOFpidESD(parPID);
54 fR=378.;
55 fTOFHeigth=15.3;
56 fdCut=3.;
57 fDy=AliTOFGeometry::XPad();
58 fDz=AliTOFGeometry::ZPad();
59 fDx=1.5;
60 fSeeds=0x0;
61 fTracks=0x0;
62 fN=0;
63 Init(); // temporary solution to know about Holes/no Holes
64}
65//_____________________________________________________________________________
66AliTOFtracker::AliTOFtracker(const AliTOFtracker &t):AliTracker() {
67 //AliTOFtracker copy Ctor
68
69 fHoles=t.fHoles;
70 fNseeds=t.fNseeds;
71 fNseedsTOF=t.fNseedsTOF;
72 fngoodmatch=t.fngoodmatch;
73 fnbadmatch=t.fnbadmatch;
74 fnunmatch=t.fnunmatch;
75 fnmatch=t.fnmatch;
76 fGeom = t.fGeom;
77 fTOFpid = t.fTOFpid;
78 fR=t.fR;
79 fTOFHeigth=t.fTOFHeigth;
80 fdCut=t.fdCut;
81 fDy=t.fDy;
82 fDz=t.fDz;
83 fDx=1.5;
84 fSeeds=t.fSeeds;
85 fTracks=t.fTracks;
86 fN=t.fN;
87}
88//_____________________________________________________________________________
89void AliTOFtracker::Init() {
90
91// temporary solution to know about Holes/no Holes, will be implemented as
92// an AliTOFGeometry getter
93
94 AliModule* frame=gAlice->GetModule("FRAME");
95
96 if(!frame) {
d076c8d5 97 AliError("Could Not load FRAME! Assume Frame with Holes");
74ea065c 98 fHoles=true;
99 } else{
100 if(frame->IsVersion()==1) {fHoles=false;}
101 else {fHoles=true;}
102 }
103}
104//_____________________________________________________________________________
105Int_t AliTOFtracker::PropagateBack(AliESD* event) {
106 //
107 // Gets seeds from ESD event and Match with TOF Clusters
108 //
109
110
111 //Initialise some counters
112
113 fNseeds=0;
114 fNseedsTOF=0;
115 fngoodmatch=0;
116 fnbadmatch=0;
117 fnunmatch=0;
118 fnmatch=0;
119
120 Int_t ntrk=event->GetNumberOfTracks();
121 fNseeds = ntrk;
122 fSeeds= new TClonesArray("AliESDtrack");
123 TClonesArray &aESDTrack = *fSeeds;
124
125
126 //Load ESD tracks into a local Array of ESD Seeds
127
128 for (Int_t i=0; i<fNseeds; i++) {
129 AliESDtrack *t=event->GetTrack(i);
130 new(aESDTrack[i]) AliESDtrack(*t);
131 }
132
133 //Prepare ESD tracks candidates for TOF Matching
134 CollectESD();
135
136 //First Step with Strict Matching Criterion
137 MatchTracks(kFALSE);
138
139 //Second Step with Looser Matching Criterion
140 MatchTracks(kTRUE);
141
d076c8d5 142 AliInfo(Form("Number of matched tracks: %d",fnmatch));
143 AliInfo(Form("Number of good matched tracks: %d",fngoodmatch));
144 AliInfo(Form("Number of bad matched tracks: %d",fnbadmatch));
74ea065c 145
146 //Update the matched ESD tracks
147
148 for (Int_t i=0; i<ntrk; i++) {
149 AliESDtrack *t=event->GetTrack(i);
150 AliESDtrack *seed =(AliESDtrack*)fSeeds->UncheckedAt(i);
151 if(seed->GetTOFsignal()>0){
152 t->SetTOFsignal(seed->GetTOFsignal());
153 t->SetTOFcluster(seed->GetTOFcluster());
154 AliTOFtrack *track = new AliTOFtrack(*seed);
155 t->UpdateTrackParams(track,AliESDtrack::kTOFout);
156 delete track;
157 }
158 }
159
160
161 //Make TOF PID
162 fTOFpid->MakePID(event);
163
7bf28302 164 if (fSeeds) {
165 fSeeds->Delete();
166 delete fSeeds;
167 fSeeds = 0x0;
168 }
169 if (fTracks) {
170 fTracks->Delete();
171 delete fTracks;
172 fTracks = 0x0;
173 }
74ea065c 174 return 0;
175
176}
177//_________________________________________________________________________
178void AliTOFtracker::CollectESD() {
179 //prepare the set of ESD tracks to be matched to clusters in TOF
180
181 fTracks= new TClonesArray("AliTOFtrack");
182 TClonesArray &aTOFTrack = *fTracks;
183 for (Int_t i=0; i<fNseeds; i++) {
184
185 AliESDtrack *t =(AliESDtrack*)fSeeds->UncheckedAt(i);
186 if ((t->GetStatus()&AliESDtrack::kTPCout)==0)continue;
187
188 // TRD good tracks, already propagated at 371 cm
189
56da420e 190 AliTOFtrack *track = new AliTOFtrack(*t); // New
191 Double_t x = track->GetX(); //New
192
193 if (((t->GetStatus()&AliESDtrack::kTRDout)!=0 ) &&
194 ( x >= AliTOFGeometry::RinTOF()) ){
74ea065c 195 track->SetSeedIndex(i);
196 t->UpdateTrackParams(track,AliESDtrack::kTOFout);
197 new(aTOFTrack[fNseedsTOF]) AliTOFtrack(*track);
198 fNseedsTOF++;
199 delete track;
200 }
201
202 // Propagate the rest of TPCbp
203
204 else {
74ea065c 205 if(track->PropagateToInnerTOF(fHoles)){ // temporary solution
206 // if(track->PropagateToInnerTOF(fGeom->GetHoles())){
207 track->SetSeedIndex(i);
208 t->UpdateTrackParams(track,AliESDtrack::kTOFout);
209 new(aTOFTrack[fNseedsTOF]) AliTOFtrack(*track);
210 fNseedsTOF++;
211 }
212 delete track;
213 }
214 }
215
216 // Sort according uncertainties on track position
217 fTracks->Sort();
218
219}
220//_________________________________________________________________________
221void AliTOFtracker::MatchTracks( Bool_t mLastStep){
222
223 //Match ESD tracks to clusters in TOF
224
74ea065c 225 Int_t nSteps=(Int_t)(fTOFHeigth/0.1);
7bf28302 226
227 //PH Arrays (moved outside of the loop)
228 Float_t * trackPos[4];
229 for (Int_t ii=0; ii<4; ii++) trackPos[ii] = new Float_t[nSteps];
230 Int_t * clind[6];
231 for (Int_t ii=0;ii<6;ii++) clind[ii] = new Int_t[fN];
74ea065c 232
233 for (Int_t i=0; i<fNseedsTOF; i++) {
234
235 AliTOFtrack *track =(AliTOFtrack*)fTracks->UncheckedAt(i);
236 AliESDtrack *t =(AliESDtrack*)fSeeds->UncheckedAt(track->GetSeedIndex());
7bf28302 237 if(t->GetTOFsignal()>0. ) continue;
74ea065c 238 AliTOFtrack *trackTOFin =new AliTOFtrack(*track);
74ea065c 239
240 // Some init
241
242 Int_t index[10000];
243 Float_t dist[10000];
244 Float_t cxpos[10000];
245 Float_t crecL[10000];
74ea065c 246
247 // Determine a window around the track
248
249 Double_t x,par[5];
250 trackTOFin->GetExternalParameters(x,par);
251 Double_t cov[15];
252 trackTOFin->GetExternalCovariance(cov);
253 Float_t scalefact=3.;
254 Double_t dphi=
255 scalefact*
256 ((5*TMath::Sqrt(cov[0]) + 0.5*fDy + 2.5*TMath::Abs(par[2]))/fR);
257 Double_t dz=
258 scalefact*
259 (5*TMath::Sqrt(cov[2]) + 0.5*fDz + 2.5*TMath::Abs(par[3]));
260
261 Double_t phi=TMath::ATan2(par[0],x) + trackTOFin->GetAlpha();
262 if (phi<-TMath::Pi())phi+=2*TMath::Pi();
263 if (phi>=TMath::Pi())phi-=2*TMath::Pi();
264 Double_t z=par[1];
265
74ea065c 266 Int_t nc=0;
267
268 // find the clusters in the window of the track
269
270 for (Int_t k=FindClusterIndex(z-dz); k<fN; k++) {
271 AliTOFcluster *c=fClusters[k];
272 if (c->GetZ() > z+dz) break;
273 if (c->IsUsed()) continue;
274
275 Double_t dph=TMath::Abs(c->GetPhi()-phi);
276 if (dph>TMath::Pi()) dph-=2.*TMath::Pi();
9b49e4c9 277 if (TMath::Abs(dph)>dphi) continue;
74ea065c 278
279 clind[0][nc] = c->GetDetInd(0);
280 clind[1][nc] = c->GetDetInd(1);
281 clind[2][nc] = c->GetDetInd(2);
282 clind[3][nc] = c->GetDetInd(3);
283 clind[4][nc] = c->GetDetInd(4);
284 clind[5][nc] = k;
285 nc++;
286 }
287
288 //start fine propagation
289
290 Int_t nStepsDone = 0;
291 for( Int_t istep=0; istep<nSteps; istep++){
292
293 Float_t xs=AliTOFGeometry::RinTOF()+istep*0.1;
294 Double_t ymax=xs*TMath::Tan(0.5*AliTOFGeometry::GetAlpha());
295
296 Bool_t skip=kFALSE;
297 Double_t ysect=trackTOFin->GetYat(xs,skip);
298 if(skip)break;
299 if (ysect > ymax) {
300 if (!trackTOFin->Rotate(AliTOFGeometry::GetAlpha())) {
301 break;
302 }
303 } else if (ysect <-ymax) {
304 if (!trackTOFin->Rotate(-AliTOFGeometry::GetAlpha())) {
305 break;
306 }
307 }
308
309 if(!trackTOFin->PropagateTo(xs)) {
310 break;
311 }
312
313 nStepsDone++;
314
315 // store the running point (Globalrf) - fine propagation
316
317 Double_t x,y,z;
318 trackTOFin->GetGlobalXYZ(x,y,z);
319 trackPos[0][istep]= (Float_t) x;
320 trackPos[1][istep]= (Float_t) y;
321 trackPos[2][istep]= (Float_t) z;
322 trackPos[3][istep]= trackTOFin->GetIntegratedLength();
323 }
324
325
326 Int_t nfound = 0;
327 for (Int_t istep=0; istep<nStepsDone; istep++) {
328
329 Bool_t isInside =kFALSE;
330 Float_t ctrackPos[3];
331
332 ctrackPos[0]= trackPos[0][istep];
333 ctrackPos[1]= trackPos[1][istep];
334 ctrackPos[2]= trackPos[2][istep];
335
336 //now see whether the track matches any of the TOF clusters
337
338 for (Int_t i=0; i<nc; i++){
339 Int_t cind[5];
340 cind[0]= clind[0][i];
341 cind[1]= clind[1][i];
342 cind[2]= clind[2][i];
343 cind[3]= clind[3][i];
344 cind[4]= clind[4][i];
345 Bool_t accept = kFALSE;
346 if( mLastStep)accept = (fGeom->DistanceToPad(cind,ctrackPos)<fdCut);
347 if(!mLastStep)accept = (fGeom->IsInsideThePad(cind,ctrackPos));
348 if(accept){
349 if(!mLastStep)isInside=kTRUE;
350 dist[nfound]=fGeom->DistanceToPad(cind,ctrackPos);
351 crecL[nfound]=trackPos[3][istep];
352 index[nfound]=clind[5][i]; // store cluster id
353 cxpos[nfound]=AliTOFGeometry::RinTOF()+istep*0.1; //store prop.radius
354 nfound++;
355 if(isInside)break;
356 }//end if accept
357 } //end for on the clusters
9b49e4c9 358
359
74ea065c 360 if(isInside)break;
361 } //end for on the steps
362
9b49e4c9 363
364
74ea065c 365 if (nfound == 0 ) {
366 fnunmatch++;
7bf28302 367 delete trackTOFin;
74ea065c 368 continue;
369 }
370
371 fnmatch++;
372
373 // now choose the cluster to be matched with the track.
374
375 Int_t idclus=0;
376 Float_t recL = 0.;
377 Float_t xpos=0.;
378 Float_t mindist=1000.;
379 for (Int_t iclus= 0; iclus<nfound;iclus++){
380 if (dist[iclus]< mindist){
381 mindist = dist[iclus];
382 xpos = cxpos[iclus];
383 idclus =index[iclus];
384 recL=crecL[iclus]+fDx*0.5;
385 }
386 }
387
388 AliTOFcluster *c=fClusters[idclus];
389 c->Use();
390
391 // Track length correction for matching Step 2
392
393 if(mLastStep){
394 Float_t rc=TMath::Sqrt(c->GetR()*c->GetR() + c->GetZ()*c->GetZ());
395 Float_t rt=TMath::Sqrt(trackPos[0][70]*trackPos[0][70]
396 +trackPos[1][70]*trackPos[1][70]
397 +trackPos[2][70]*trackPos[2][70]);
398 Float_t dlt=rc-rt;
399 recL=trackPos[3][70]+dlt;
400 }
401
402 if (
403 (c->GetLabel(0)==TMath::Abs(trackTOFin->GetLabel()))
404 ||
405 (c->GetLabel(1)==TMath::Abs(trackTOFin->GetLabel()))
406 ||
407 (c->GetLabel(2)==TMath::Abs(trackTOFin->GetLabel()))
408 ) {
409 fngoodmatch++;
410 }
411 else{
412 fnbadmatch++;
413 }
414
415 delete trackTOFin;
416
43f77f2d 417 //Double_t tof=50*c->GetTDC()+32; // in ps
418 Double_t tof=AliTOFGeometry::TdcBinWidth()*c->GetTDC()+32; // in ps
74ea065c 419 t->SetTOFsignal(tof);
420 t->SetTOFcluster(c->GetIndex());
421 Double_t time[10]; t->GetIntegratedTimes(time);
422 Double_t mom=t->GetP();
304864ab 423 for(Int_t j=0;j<=AliPID::kSPECIES;j++){
424 Double_t mass=AliPID::ParticleMass(j);
74ea065c 425 time[j]+=(recL-trackPos[3][0])/3e-2*TMath::Sqrt(mom*mom+mass*mass)/mom;
426 }
427
428 AliTOFtrack *trackTOFout = new AliTOFtrack(*t);
429 trackTOFout->PropagateTo(xpos);
430 t->UpdateTrackParams(trackTOFout,AliESDtrack::kTOFout);
431 t->SetIntegratedLength(recL);
432 t->SetIntegratedTimes(time);
433
434 delete trackTOFout;
435 }
7bf28302 436 for (Int_t ii=0; ii<4; ii++) delete [] trackPos[ii];
437 for (Int_t ii=0;ii<6;ii++) delete [] clind[ii];
74ea065c 438}
439//_________________________________________________________________________
440Int_t AliTOFtracker::LoadClusters(TTree *dTree) {
441 //--------------------------------------------------------------------
442 //This function loads the TOF clusters
443 //--------------------------------------------------------------------
444
445 TBranch *branch=dTree->GetBranch("TOF");
446 if (!branch) {
d076c8d5 447 AliError("can't get the branch with the TOF digits !");
74ea065c 448 return 1;
449 }
450
451 TClonesArray dummy("AliTOFdigit",10000), *digits=&dummy;
452 branch->SetAddress(&digits);
453
454 dTree->GetEvent(0);
455 Int_t nd=digits->GetEntriesFast();
d076c8d5 456 AliInfo(Form("number of digits: %d",nd));
74ea065c 457
458 for (Int_t i=0; i<nd; i++) {
459 AliTOFdigit *d=(AliTOFdigit*)digits->UncheckedAt(i);
460 Int_t dig[5]; Float_t g[3];
461 dig[0]=d->GetSector();
462 dig[1]=d->GetPlate();
463 dig[2]=d->GetStrip();
464 dig[3]=d->GetPadz();
465 dig[4]=d->GetPadx();
466
467 fGeom->GetPos(dig,g);
468
469 Double_t h[5];
470 h[0]=TMath::Sqrt(g[0]*g[0]+g[1]*g[1]);
471 h[1]=TMath::ATan2(g[1],g[0]); h[2]=g[2];
472 h[3]=d->GetTdc(); h[4]=d->GetAdc();
473
474 AliTOFcluster *cl=new AliTOFcluster(h,d->GetTracks(),dig,i);
475 InsertCluster(cl);
476 }
477
478 return 0;
479}
480//_________________________________________________________________________
481void AliTOFtracker::UnloadClusters() {
482 //--------------------------------------------------------------------
483 //This function unloads TOF clusters
484 //--------------------------------------------------------------------
485 for (Int_t i=0; i<fN; i++) delete fClusters[i];
486 fN=0;
487}
488
489//_________________________________________________________________________
490Int_t AliTOFtracker::InsertCluster(AliTOFcluster *c) {
491 //--------------------------------------------------------------------
492 //This function adds a cluster to the array of clusters sorted in Z
493 //--------------------------------------------------------------------
494 if (fN==kMaxCluster) {
d076c8d5 495 AliError("Too many clusters !");
74ea065c 496 return 1;
497 }
498
499 if (fN==0) {fClusters[fN++]=c; return 0;}
500 Int_t i=FindClusterIndex(c->GetZ());
501 memmove(fClusters+i+1 ,fClusters+i,(fN-i)*sizeof(AliTOFcluster*));
502 fClusters[i]=c; fN++;
503
504 return 0;
505}
506
507//_________________________________________________________________________
508Int_t AliTOFtracker::FindClusterIndex(Double_t z) const {
509 //--------------------------------------------------------------------
510 // This function returns the index of the nearest cluster
511 //--------------------------------------------------------------------
512 if (fN==0) return 0;
513 if (z <= fClusters[0]->GetZ()) return 0;
514 if (z > fClusters[fN-1]->GetZ()) return fN;
515 Int_t b=0, e=fN-1, m=(b+e)/2;
516 for (; b<e; m=(b+e)/2) {
517 if (z > fClusters[m]->GetZ()) b=m+1;
518 else e=m;
519 }
520 return m;
521}
522