free arrays before of return in PropagateBack
[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 **************************************************************************/
0e46b9ae 15
16//--------------------------------------------------------------------//
17// //
18// AliTOFtracker Class //
19// Task: Perform association of the ESD tracks to TOF Clusters //
20// and Update ESD track with associated TOF Cluster parameters //
21// //
22// -- Authors : S. Arcelli, C. Zampolli (Bologna University and INFN) //
23// -- Contacts: Annalisa.De.Caro@cern.ch //
24// -- : Chiara.Zampolli@bo.infn.it //
25// -- : Silvia.Arcelli@bo.infn.it //
26// //
27//--------------------------------------------------------------------//
28
08048cd3 29#include <Rtypes.h>
30#include <TROOT.h>
571dda3d 31
5c7c93fa 32#include <TSeqCollection.h>
08048cd3 33#include <TClonesArray.h>
0841bd02 34#include <TObjArray.h>
08048cd3 35#include <TGeoManager.h>
36#include <TTree.h>
37#include <TFile.h>
38#include <TH2F.h>
571dda3d 39
5c7c93fa 40#include "AliGeomManager.h"
0e46b9ae 41#include "AliESDtrack.h"
af885e0f 42#include "AliESDEvent.h"
10d100d4 43#include "AliESDpid.h"
d076c8d5 44#include "AliLog.h"
0e46b9ae 45#include "AliTrackPointArray.h"
5c7c93fa 46#include "AliCDBManager.h"
596a855f 47
10d100d4 48//#include "AliTOFpidESD.h"
e0ddb533 49#include "AliTOFRecoParam.h"
3a646035 50#include "AliTOFReconstructor.h"
571dda3d 51#include "AliTOFcluster.h"
ba66add8 52#include "AliTOFGeometry.h"
571dda3d 53#include "AliTOFtracker.h"
0e46b9ae 54#include "AliTOFtrack.h"
571dda3d 55
0e46b9ae 56extern TGeoManager *gGeoManager;
5480a4f2 57//extern TROOT *gROOT;
d4754572 58
de60fa8a 59
596a855f 60ClassImp(AliTOFtracker)
61
74ea065c 62//_____________________________________________________________________________
e0ddb533 63AliTOFtracker::AliTOFtracker():
f31a249f 64 fkRecoParam(0x0),
e0ddb533 65 fGeom(0x0),
58d8d9a3 66 fN(0),
67 fNseeds(0),
68 fNseedsTOF(0),
69 fngoodmatch(0),
70 fnbadmatch(0),
71 fnunmatch(0),
72 fnmatch(0),
9d802709 73 fTracks(new TClonesArray("AliTOFtrack")),
de60fa8a 74 fSeeds(new TObjArray(100)),
75 fTOFtrackPoints(new TObjArray(10)),
5664c6ed 76 fHDigClusMap(0x0),
77 fHDigNClus(0x0),
78 fHDigClusTime(0x0),
79 fHDigClusToT(0x0),
80 fHRecNClus(0x0),
81 fHRecDist(0x0),
82 fHRecSigYVsP(0x0),
83 fHRecSigZVsP(0x0),
84 fHRecSigYVsPWin(0x0),
85 fHRecSigZVsPWin(0x0),
86 fCalTree(0x0),
87 fIch(-1),
88 fToT(-1.),
89 fTime(-1.),
90 fExpTimePi(-1.),
91 fExpTimeKa(-1.),
115179c6 92 fExpTimePr(-1.),
93 fNTOFmatched(0)
78c25bbc 94{
74ea065c 95 //AliTOFtracker main Ctor
1d834a1e 96
78c25bbc 97 for (Int_t ii=0; ii<kMaxCluster; ii++) fClusters[ii]=0x0;
3a646035 98
78c25bbc 99 // Getting the geometry
100 fGeom = new AliTOFGeometry();
101
ea23cd7a 102 for(Int_t i=0; i< 20000;i++){
103 fClusterESD[i] = NULL;
104 }
105
78c25bbc 106 InitCheckHists();
3a646035 107
74ea065c 108}
109//_____________________________________________________________________________
5664c6ed 110AliTOFtracker::~AliTOFtracker() {
111 //
112 // Dtor
113 //
e0ddb533 114
5664c6ed 115 SaveCheckHists();
e0ddb533 116
5d456bcd 117 if(!(AliCDBManager::Instance()->GetCacheFlag())){
f31a249f 118 delete fkRecoParam;
5d456bcd 119 }
e0ddb533 120 delete fGeom;
5664c6ed 121 delete fHDigClusMap;
122 delete fHDigNClus;
123 delete fHDigClusTime;
124 delete fHDigClusToT;
125 delete fHRecNClus;
126 delete fHRecDist;
127 delete fHRecSigYVsP;
128 delete fHRecSigZVsP;
129 delete fHRecSigYVsPWin;
130 delete fHRecSigZVsPWin;
131 delete fCalTree;
9d802709 132 if (fTracks){
133 fTracks->Delete();
134 delete fTracks;
135 fTracks=0x0;
136 }
137 if (fSeeds){
138 fSeeds->Delete();
139 delete fSeeds;
140 fSeeds=0x0;
141 }
de60fa8a 142 if (fTOFtrackPoints){
143 fTOFtrackPoints->Delete();
144 delete fTOFtrackPoints;
145 fTOFtrackPoints=0x0;
146 }
9d802709 147
7e96dc55 148 for (Int_t ii=0; ii<kMaxCluster; ii++)
149 if (fClusters[ii]) fClusters[ii]->Delete();
1d834a1e 150
115179c6 151 for(Int_t i=0; i< 20000;i++){
152 if(fClusterESD[i]){
153 delete fClusterESD[i];
154 fClusterESD[i] = NULL;
155 }
156 }
157
158
5664c6ed 159}
74ea065c 160//_____________________________________________________________________________
10d100d4 161void AliTOFtracker::GetPidSettings(AliESDpid *esdPID) {
162 //
163 // Sets TOF resolution from RecoParams
164 //
f31a249f 165 if (fkRecoParam)
166 esdPID->GetTOFResponse().SetTimeResolution(fkRecoParam->GetTimeResolution());
10d100d4 167 else
f31a249f 168 AliWarning("fkRecoParam not yet set; cannot set PID settings");
10d100d4 169}
170//_____________________________________________________________________________
f31a249f 171Int_t AliTOFtracker::PropagateBack(AliESDEvent * const event) {
74ea065c 172 //
173 // Gets seeds from ESD event and Match with TOF Clusters
174 //
175
115179c6 176 if(fNTOFmatched==0)
177 event->SetTOFcluster(1,fClusterESD);
178
ba597140 179 if (fNTOFmatched==0) {
115179c6 180 AliInfo("No TOF recPoints to be matched with reconstructed tracks");
181 return 0;
182 }
183
3a646035 184 // initialize RecoParam for current event
bafe290d 185 AliDebug(1,"Initializing params for TOF");
3a646035 186
f31a249f 187 fkRecoParam = AliTOFReconstructor::GetRecoParam(); // instantiate reco param from STEER...
90b234fe 188
f31a249f 189 if (fkRecoParam == 0x0) {
3a646035 190 AliFatal("No Reco Param found for TOF!!!");
191 }
f31a249f 192 //fkRecoParam->Dump();
193 //if(fkRecoParam->GetApplyPbPbCuts())fkRecoParam=fkRecoParam->GetPbPbparam();
194 //fkRecoParam->PrintParameters();
195
74ea065c 196 //Initialise some counters
197
198 fNseeds=0;
199 fNseedsTOF=0;
200 fngoodmatch=0;
201 fnbadmatch=0;
202 fnunmatch=0;
203 fnmatch=0;
204
205 Int_t ntrk=event->GetNumberOfTracks();
206 fNseeds = ntrk;
74ea065c 207
208
209 //Load ESD tracks into a local Array of ESD Seeds
b08af685 210 for (Int_t i=0; i<fNseeds; i++){
0841bd02 211 fSeeds->AddLast(event->GetTrack(i));
b08af685 212 event->GetTrack(i)->SetESDEvent(event);
213 }
74ea065c 214
215 //Prepare ESD tracks candidates for TOF Matching
216 CollectESD();
217
115179c6 218 if (fNseeds==0 || fNseedsTOF==0) {
ba597140 219 event->SetTOFcluster(1,fClusterESD);
115179c6 220 AliInfo("No seeds to try TOF match");
42c3320f 221 fSeeds->Clear();
222 fTracks->Clear();
115179c6 223 return 0 ;
224 }
225
74ea065c 226 //First Step with Strict Matching Criterion
115179c6 227 MatchTracks(0);
74ea065c 228
229 //Second Step with Looser Matching Criterion
115179c6 230 MatchTracks(1);
231
232 //Third Step without kTOFout flag (just to update clusters)
233 MatchTracks(2);
234
235 event->SetTOFcluster(fNTOFmatched,fClusterESD);
236
237 if (fNTOFmatched==0) {
238 AliInfo("No TOF recPoints to be matched with reconstructed tracks");
42c3320f 239 fSeeds->Clear();
240 fTracks->Clear();
115179c6 241 return 0;
242 }
74ea065c 243
8a06b2a0 244 AliInfo(Form("Number of matched tracks = %d (good = %d, bad = %d)",fnmatch,fngoodmatch,fnbadmatch));
74ea065c 245
246 //Update the matched ESD tracks
247
248 for (Int_t i=0; i<ntrk; i++) {
249 AliESDtrack *t=event->GetTrack(i);
0841bd02 250 AliESDtrack *seed =(AliESDtrack*)fSeeds->At(i);
ff826920 251
af61c656 252 if ( (seed->GetStatus()&AliESDtrack::kTOFin)!=0 ) {
253 t->SetStatus(AliESDtrack::kTOFin);
254 //if(seed->GetTOFsignal()>0){
255 if ( (seed->GetStatus()&AliESDtrack::kTOFout)!=0 ) {
256 t->SetStatus(AliESDtrack::kTOFout);
257 t->SetTOFsignal(seed->GetTOFsignal());
258 t->SetTOFcluster(seed->GetTOFcluster());
259 t->SetTOFsignalToT(seed->GetTOFsignalToT());
260 t->SetTOFsignalRaw(seed->GetTOFsignalRaw());
261 t->SetTOFsignalDz(seed->GetTOFsignalDz());
46d7d82e 262 t->SetTOFsignalDx(seed->GetTOFsignalDx());
8b441e14 263 t->SetTOFDeltaBC(seed->GetTOFDeltaBC());
264 t->SetTOFL0L1(seed->GetTOFL0L1());
af61c656 265 t->SetTOFCalChannel(seed->GetTOFCalChannel());
266 Int_t tlab[3]; seed->GetTOFLabel(tlab);
267 t->SetTOFLabel(tlab);
268
0841bd02 269 Double_t alphaA = (Double_t)t->GetAlpha();
270 Double_t xA = (Double_t)t->GetX();
271 Double_t yA = (Double_t)t->GetY();
272 Double_t zA = (Double_t)t->GetZ();
273 Double_t p1A = (Double_t)t->GetSnp();
274 Double_t p2A = (Double_t)t->GetTgl();
275 Double_t p3A = (Double_t)t->GetSigned1Pt();
276 const Double_t *covA = (Double_t*)t->GetCovariance();
e81ea7b6 277
278 // Make attention, please:
279 // AliESDtrack::fTOFInfo array does not be stored in the AliESDs.root file
280 // it is there only for a check during the reconstruction step.
281 Float_t info[10]; seed->GetTOFInfo(info);
24322e5c 282 t->SetTOFInfo(info);
e81ea7b6 283 AliDebug(3,Form(" distance=%f; residual in the pad reference frame: dX=%f, dZ=%f", info[0],info[1],info[2]));
284
285 // Check done:
286 // by calling the AliESDtrack::UpdateTrackParams,
287 // the current track parameters are changed
288 // and it could cause refit problems.
289 // We need to update only the following track parameters:
290 // the track length and expected times.
291 // Removed AliESDtrack::UpdateTrackParams call
292 // Called AliESDtrack::SetIntegratedTimes(...) and
293 // AliESDtrack::SetIntegratedLength() routines.
294 /*
295 AliTOFtrack *track = new AliTOFtrack(*seed);
296 t->UpdateTrackParams(track,AliESDtrack::kTOFout); // to be checked - AdC
297 delete track;
115179c6 298 Double_t time[AliPID::kSPECIESC]; t->GetIntegratedTimes(time);
e81ea7b6 299 */
300
fc9b31a7 301 Double_t time[AliPID::kSPECIESC]; seed->GetIntegratedTimes(time,AliPID::kSPECIESC);
e81ea7b6 302 t->SetIntegratedTimes(time);
303
304 Double_t length = seed->GetIntegratedLength();
305 t->SetIntegratedLength(length);
306
0841bd02 307 Double_t alphaB = (Double_t)t->GetAlpha();
308 Double_t xB = (Double_t)t->GetX();
309 Double_t yB = (Double_t)t->GetY();
310 Double_t zB = (Double_t)t->GetZ();
311 Double_t p1B = (Double_t)t->GetSnp();
312 Double_t p2B = (Double_t)t->GetTgl();
313 Double_t p3B = (Double_t)t->GetSigned1Pt();
314 const Double_t *covB = (Double_t*)t->GetCovariance();
115179c6 315 AliDebug(3,"Track params -now(before)-:");
316 AliDebug(3,Form(" X: %f(%f), Y: %f(%f), Z: %f(%f) --- alpha: %f(%f)",
e81ea7b6 317 xB,xA,
318 yB,yA,
319 zB,zA,
320 alphaB,alphaA));
115179c6 321 AliDebug(3,Form(" p1: %f(%f), p2: %f(%f), p3: %f(%f)",
e81ea7b6 322 p1B,p1A,
323 p2B,p2A,
324 p3B,p3A));
115179c6 325 AliDebug(3,Form(" cov1: %f(%f), cov2: %f(%f), cov3: %f(%f)"
e81ea7b6 326 " cov4: %f(%f), cov5: %f(%f), cov6: %f(%f)"
327 " cov7: %f(%f), cov8: %f(%f), cov9: %f(%f)"
328 " cov10: %f(%f), cov11: %f(%f), cov12: %f(%f)"
329 " cov13: %f(%f), cov14: %f(%f), cov15: %f(%f)",
330 covB[0],covA[0],
331 covB[1],covA[1],
332 covB[2],covA[2],
333 covB[3],covA[3],
334 covB[4],covA[4],
335 covB[5],covA[5],
336 covB[6],covA[6],
337 covB[7],covA[7],
338 covB[8],covA[8],
339 covB[9],covA[9],
340 covB[10],covA[10],
341 covB[11],covA[11],
342 covB[12],covA[12],
343 covB[13],covA[13],
344 covB[14],covA[14]
345 ));
115179c6 346 AliDebug(2,Form(" TOF params: %6d %f %f %f %f %f %6d %3d %f",
af61c656 347 i,
348 t->GetTOFsignalRaw(),
349 t->GetTOFsignal(),
350 t->GetTOFsignalToT(),
351 t->GetTOFsignalDz(),
46d7d82e 352 t->GetTOFsignalDx(),
af61c656 353 t->GetTOFCalChannel(),
354 t->GetTOFcluster(),
115179c6 355 t->GetIntegratedLength()));
356 AliDebug(2,Form(" %f %f %f %f %f %f %f %f %f",
357 time[0], time[1], time[2], time[3], time[4], time[5], time[6], time[7], time[8]));
af61c656 358 }
74ea065c 359 }
360 }
361
115179c6 362 if(fNTOFmatched){
363 Int_t *matchmap = new Int_t[fNTOFmatched];
364 event->SetTOFcluster(fNTOFmatched,fClusterESD,matchmap);
365 for (Int_t i=0; i<ntrk; i++) { // remapping after TOF matching selection
366 AliESDtrack *t=event->GetTrack(i);
367 t->ReMapTOFcluster(fNTOFmatched,matchmap);
368 }
369
370 delete[] matchmap;
371 }
372
74ea065c 373 //Make TOF PID
10d100d4 374 // Now done in AliESDpid
375 // fPid->MakePID(event,timeZero);
74ea065c 376
de60fa8a 377 fSeeds->Clear();
1d834a1e 378 //fTracks->Delete();
379 fTracks->Clear();
74ea065c 380 return 0;
381
382}
383//_________________________________________________________________________
384void AliTOFtracker::CollectESD() {
385 //prepare the set of ESD tracks to be matched to clusters in TOF
5478df1f 386
387 Int_t seedsTOF1=0;
78c25bbc 388 Int_t seedsTOF3=0;
5478df1f 389 Int_t seedsTOF2=0;
74ea065c 390
74ea065c 391 TClonesArray &aTOFTrack = *fTracks;
392 for (Int_t i=0; i<fNseeds; i++) {
393
0841bd02 394 AliESDtrack *t =(AliESDtrack*)fSeeds->At(i);
74ea065c 395 if ((t->GetStatus()&AliESDtrack::kTPCout)==0)continue;
396
56da420e 397 AliTOFtrack *track = new AliTOFtrack(*t); // New
af61c656 398 Float_t x = (Float_t)track->GetX(); //New
399
78c25bbc 400 // TRD 'good' tracks
401 if ( ( (t->GetStatus()&AliESDtrack::kTRDout)!=0 ) ) {
af61c656 402
78c25bbc 403 AliDebug(1,Form(" Before propagation till inner TOF radius, ESDtrackLength=%f, TOFtrackLength=%f",t->GetIntegratedLength(),track->GetIntegratedLength()));
404
405 // TRD 'good' tracks, already propagated at 371 cm
406 if( x >= AliTOFGeometry::Rmin() ) {
407
408 if ( track->PropagateToInnerTOF() ) {
409
410 AliDebug(1,Form(" TRD propagated track till rho = %fcm."
411 " And then the track has been propagated till rho = %fcm.",
412 x, (Float_t)track->GetX()));
413
414 track->SetSeedIndex(i);
415 t->UpdateTrackParams(track,AliESDtrack::kTOFin);
416 new(aTOFTrack[fNseedsTOF]) AliTOFtrack(*track);
417 fNseedsTOF++;
418 seedsTOF1++;
419
420 AliDebug(1,Form(" After propagation till inner TOF radius, ESDtrackLength=%f, TOFtrackLength=%f",t->GetIntegratedLength(),track->GetIntegratedLength()));
421 }
422 delete track;
af61c656 423
af61c656 424 }
78c25bbc 425 else { // TRD 'good' tracks, propagated rho<371cm
426
427 if ( track->PropagateToInnerTOF() ) {
428
429 AliDebug(1,Form(" TRD propagated track till rho = %fcm."
430 " And then the track has been propagated till rho = %fcm.",
431 x, (Float_t)track->GetX()));
432
433 track->SetSeedIndex(i);
434 t->UpdateTrackParams(track,AliESDtrack::kTOFin);
435 new(aTOFTrack[fNseedsTOF]) AliTOFtrack(*track);
436 fNseedsTOF++;
437 seedsTOF3++;
438
439 AliDebug(1,Form(" After propagation till inner TOF radius, ESDtrackLength=%f, TOFtrackLength=%f",t->GetIntegratedLength(),track->GetIntegratedLength()));
440 }
441 delete track;
442
443 }
444 //delete track;
74ea065c 445 }
446
78c25bbc 447 else { // Propagate the rest of TPCbp
448
449 AliDebug(1,Form(" Before propagation till inner TOF radius, ESDtrackLength=%f, TOFtrackLength=%f",t->GetIntegratedLength(),track->GetIntegratedLength()));
450
af61c656 451 if ( track->PropagateToInnerTOF() ) {
452
453 AliDebug(1,Form(" TPC propagated track till rho = %fcm."
454 " And then the track has been propagated till rho = %fcm.",
455 x, (Float_t)track->GetX()));
456
74ea065c 457 track->SetSeedIndex(i);
af61c656 458 t->UpdateTrackParams(track,AliESDtrack::kTOFin);
74ea065c 459 new(aTOFTrack[fNseedsTOF]) AliTOFtrack(*track);
460 fNseedsTOF++;
5478df1f 461 seedsTOF2++;
78c25bbc 462
463 AliDebug(1,Form(" After propagation till inner TOF radius, ESDtrackLength=%f, TOFtrackLength=%f",t->GetIntegratedLength(),track->GetIntegratedLength()));
74ea065c 464 }
465 delete track;
466 }
467 }
468
78c25bbc 469 AliInfo(Form("Number of TOF seeds = %d (kTRDout371 = %d, kTRDoutLess371 = %d, !kTRDout = %d)",fNseedsTOF,seedsTOF1,seedsTOF3,seedsTOF2));
7b61cd9c 470
74ea065c 471 // Sort according uncertainties on track position
472 fTracks->Sort();
473
474}
78c25bbc 475
74ea065c 476//_________________________________________________________________________
115179c6 477void AliTOFtracker::MatchTracks( Int_t mLastStep){
74ea065c 478
e0ddb533 479 // Parameters used/regulating the reconstruction
480
78c25bbc 481 //static Float_t corrLen=0.;//0.75;
482 static Float_t detDepth=18.;
d01bf4f4 483 static Float_t padDepth=0.5;
e0ddb533 484
6819758a 485 const Float_t kSpeedOfLight= 2.99792458e-2; // speed of light [cm/ps]
f2524833 486 const Float_t kTimeOffset = 0.; // time offset for tracking algorithm [ps]
6819758a 487
ba66add8 488 Float_t dY=AliTOFGeometry::XPad();
489 Float_t dZ=AliTOFGeometry::ZPad();
e0ddb533 490
f31a249f 491 Float_t sensRadius = fkRecoParam->GetSensRadius();
492 Float_t stepSize = fkRecoParam->GetStepSize();
78c25bbc 493 Float_t scaleFact = fkRecoParam->GetWindowScaleFact();
f31a249f 494 Float_t dyMax=fkRecoParam->GetWindowSizeMaxY();
495 Float_t dzMax=fkRecoParam->GetWindowSizeMaxZ();
496 Float_t dCut=fkRecoParam->GetDistanceCut();
78c25bbc 497 if (dCut==3. && fNseedsTOF<=10) {
498 dCut=10.;
499 AliInfo(Form("Matching window=%f, since low multiplicity event (fNseedsTOF=%d)",
500 dCut, fNseedsTOF));
501 }
115179c6 502 if(mLastStep == 2)
503 dCut=10.;
504
505
f31a249f 506 Double_t maxChi2=fkRecoParam->GetMaxChi2TRD();
507 Bool_t timeWalkCorr = fkRecoParam->GetTimeWalkCorr();
a838421b 508 if(!mLastStep){
115179c6 509 AliDebug(1,"++++++++++++++TOF Reconstruction Parameters:++++++++++++");
a838421b 510 AliDebug(1,Form("TOF sens radius: %f",sensRadius));
511 AliDebug(1,Form("TOF step size: %f",stepSize));
512 AliDebug(1,Form("TOF Window scale factor: %f",scaleFact));
513 AliDebug(1,Form("TOF Window max dy: %f",dyMax));
514 AliDebug(1,Form("TOF Window max dz: %f",dzMax));
515 AliDebug(1,Form("TOF distance Cut: %f",dCut));
516 AliDebug(1,Form("TOF Max Chi2: %f",maxChi2));
517 AliDebug(1,Form("Time Walk Correction? : %d",timeWalkCorr));
518 }
74ea065c 519
53884c34 520 //Match ESD tracks to clusters in TOF
a533f541 521
e0ddb533 522 // Get the number of propagation steps
e0ddb533 523 Int_t nSteps=(Int_t)(detDepth/stepSize);
78c25bbc 524 AliDebug(1,Form(" Number of steps to be done %d",nSteps));
7bf28302 525
115179c6 526 AliDebug(1,"++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
527
7bf28302 528 //PH Arrays (moved outside of the loop)
529 Float_t * trackPos[4];
530 for (Int_t ii=0; ii<4; ii++) trackPos[ii] = new Float_t[nSteps];
128563f6 531 Int_t * clind = new Int_t[fN];
74ea065c 532
53884c34 533 // Some init
53884c34 534 const Int_t kNclusterMax = 1000; // related to fN value
de60fa8a 535 TGeoHMatrix global[kNclusterMax];
536
af61c656 537 //The matching loop
c0545837 538 for (Int_t iseed=0; iseed<fNseedsTOF; iseed++) {
74ea065c 539
48b634ff 540 fTOFtrackPoints->Delete();
de60fa8a 541
53884c34 542 for (Int_t ii=0; ii<kNclusterMax; ii++)
543 global[ii] = 0x0;
c0545837 544 AliTOFtrack *track =(AliTOFtrack*)fTracks->UncheckedAt(iseed);
0841bd02 545 AliESDtrack *t =(AliESDtrack*)fSeeds->At(track->GetSeedIndex());
af61c656 546 //if ( t->GetTOFsignal()>0. ) continue;
115179c6 547 if ( ((t->GetStatus()&AliESDtrack::kTOFout)!=0 ) && mLastStep < 2) continue;
de60fa8a 548 AliTOFtrack *trackTOFin = new AliTOFtrack(*track);
74ea065c 549
74ea065c 550 // Determine a window around the track
74ea065c 551 Double_t x,par[5];
552 trackTOFin->GetExternalParameters(x,par);
553 Double_t cov[15];
554 trackTOFin->GetExternalCovariance(cov);
6c94f330 555
53884c34 556 if (cov[0]<0. || cov[2]<0.) {
557 AliWarning(Form("Very strange track (%d)! At least one of its covariance matrix diagonal elements is negative!",iseed));
ecd795d8 558 //delete trackTOFin;
559 //continue;
53884c34 560 }
561
74ea065c 562 Double_t dphi=
e0ddb533 563 scaleFact*
ecd795d8 564 ((5*TMath::Sqrt(TMath::Abs(cov[0])) + 0.5*dY + 2.5*TMath::Abs(par[2]))/sensRadius);
74ea065c 565 Double_t dz=
e0ddb533 566 scaleFact*
ecd795d8 567 (5*TMath::Sqrt(TMath::Abs(cov[2])) + 0.5*dZ + 2.5*TMath::Abs(par[3]));
6c94f330 568
74ea065c 569 Double_t phi=TMath::ATan2(par[0],x) + trackTOFin->GetAlpha();
570 if (phi<-TMath::Pi())phi+=2*TMath::Pi();
571 if (phi>=TMath::Pi())phi-=2*TMath::Pi();
572 Double_t z=par[1];
573
c0545837 574 //upper limit on window's size.
de60fa8a 575 if (dz> dzMax) dz=dzMax;
576 if (dphi*sensRadius> dyMax) dphi=dyMax/sensRadius;
c0545837 577
578
74ea065c 579 // find the clusters in the window of the track
de60fa8a 580 Int_t nc=0;
74ea065c 581 for (Int_t k=FindClusterIndex(z-dz); k<fN; k++) {
7b61cd9c 582
53884c34 583 if (nc>=kNclusterMax) {
584 AliWarning("No more matchable clusters can be stored! Please, increase the corresponding vectors size.");
585 break;
586 }
587
74ea065c 588 AliTOFcluster *c=fClusters[k];
589 if (c->GetZ() > z+dz) break;
115179c6 590 if (c->IsUsed() && mLastStep < 2) continue;
17149e6b 591 if (!c->GetStatus()) {
53884c34 592 AliDebug(1,"Cluster in channel declared bad!");
593 continue; // skip bad channels as declared in OCDB
17149e6b 594 }
7b61cd9c 595
74ea065c 596 Double_t dph=TMath::Abs(c->GetPhi()-phi);
597 if (dph>TMath::Pi()) dph-=2.*TMath::Pi();
9b49e4c9 598 if (TMath::Abs(dph)>dphi) continue;
ba66add8 599
6c94f330 600 Double_t yc=(c->GetPhi() - trackTOFin->GetAlpha())*c->GetR();
601 Double_t p[2]={yc, c->GetZ()};
3c609b5c 602 Double_t cov2[3]= {dY*dY/12., 0., dZ*dZ/12.};
603 if (trackTOFin->AliExternalTrackParam::GetPredictedChi2(p,cov2) > maxChi2)continue;
6c94f330 604
128563f6 605 clind[nc] = k;
de40f9c1 606 Char_t path[200];
c0545837 607 Int_t ind[5];
128563f6 608 ind[0]=c->GetDetInd(0);
609 ind[1]=c->GetDetInd(1);
610 ind[2]=c->GetDetInd(2);
611 ind[3]=c->GetDetInd(3);
612 ind[4]=c->GetDetInd(4);
c0545837 613 fGeom->GetVolumePath(ind,path);
614 gGeoManager->cd(path);
615 global[nc] = *gGeoManager->GetCurrentMatrix();
74ea065c 616 nc++;
617 }
618
78c25bbc 619 if (nc == 0 ) {
115179c6 620 AliDebug(1,Form("No available clusters for the track number %d",iseed));
78c25bbc 621 fnunmatch++;
622 delete trackTOFin;
623 continue;
624 }
625
115179c6 626 AliDebug(1,Form(" Number of available TOF clusters for the track number %d: %d",iseed,nc));
627
74ea065c 628 //start fine propagation
629
630 Int_t nStepsDone = 0;
631 for( Int_t istep=0; istep<nSteps; istep++){
632
78c25bbc 633 // First of all, propagate the track...
634 Float_t xs = AliTOFGeometry::RinTOF()+istep*stepSize;
9a4d9088 635 if (!(trackTOFin->PropagateTo(xs))) break;
74ea065c 636
78c25bbc 637 // ...and then, if necessary, rotate the track
638 Double_t ymax = xs*TMath::Tan(0.5*AliTOFGeometry::GetAlpha());
639 Double_t ysect = trackTOFin->GetY();
74ea065c 640 if (ysect > ymax) {
9a4d9088 641 if (!(trackTOFin->Rotate(AliTOFGeometry::GetAlpha()))) break;
74ea065c 642 } else if (ysect <-ymax) {
9a4d9088 643 if (!(trackTOFin->Rotate(-AliTOFGeometry::GetAlpha()))) break;
74ea065c 644 }
645
74ea065c 646 nStepsDone++;
115179c6 647 AliDebug(3,Form(" current step %d (%d) - nStepsDone=%d",istep,nSteps,nStepsDone));
74ea065c 648
649 // store the running point (Globalrf) - fine propagation
650
6c94f330 651 Double_t r[3];
652 trackTOFin->GetXYZ(r);
78c25bbc 653 trackPos[0][nStepsDone-1]= (Float_t) r[0];
654 trackPos[1][nStepsDone-1]= (Float_t) r[1];
655 trackPos[2][nStepsDone-1]= (Float_t) r[2];
656 trackPos[3][nStepsDone-1]= trackTOFin->GetIntegratedLength();
74ea065c 657 }
658
659
78c25bbc 660#if 0
661 /*****************/
662 /**** OLD CODE ***/
663 /*****************/
664
74ea065c 665 Int_t nfound = 0;
d01bf4f4 666 Bool_t accept = kFALSE;
6819758a 667 Bool_t isInside = kFALSE;
74ea065c 668 for (Int_t istep=0; istep<nStepsDone; istep++) {
669
53884c34 670 Float_t ctrackPos[3];
ff826920 671 ctrackPos[0] = trackPos[0][istep];
672 ctrackPos[1] = trackPos[1][istep];
673 ctrackPos[2] = trackPos[2][istep];
74ea065c 674
675 //now see whether the track matches any of the TOF clusters
676
524da123 677 Float_t dist3d[3];
ff826920 678 accept = kFALSE;
679 for (Int_t i=0; i<nc; i++) {
8104fec9 680 isInside = fGeom->IsInsideThePad((TGeoHMatrix*)(&global[i]),ctrackPos,dist3d);
d01bf4f4 681
6819758a 682 if ( mLastStep ) {
683 Float_t yLoc = dist3d[1];
684 Float_t rLoc = TMath::Sqrt(dist3d[0]*dist3d[0]+dist3d[2]*dist3d[2]);
685 accept = (TMath::Abs(yLoc)<padDepth*0.5 && rLoc<dCut);
115179c6 686 AliDebug(3," I am in the case mLastStep==kTRUE ");
d01bf4f4 687 }
ff826920 688 else {
d01bf4f4 689 accept = isInside;
690 }
ff826920 691 if (accept) {
6819758a 692
de60fa8a 693 fTOFtrackPoints->AddLast(new AliTOFtrackPoint(clind[i],
694 TMath::Sqrt(dist3d[0]*dist3d[0] + dist3d[1]*dist3d[1] + dist3d[2]*dist3d[2]),
78c25bbc 695 dist3d[2],dist3d[0],dist3d[1],
696 AliTOFGeometry::RinTOF()+istep*stepSize,trackPos[3][istep]));
de60fa8a 697
115179c6 698 AliDebug(3,Form(" dist3dLoc[0] = %f, dist3dLoc[1] = %f, dist3dLoc[2] = %f ",dist3d[0],dist3d[1],dist3d[2]));
74ea065c 699 nfound++;
d01bf4f4 700 if(accept &&!mLastStep)break;
74ea065c 701 }//end if accept
524da123 702
74ea065c 703 } //end for on the clusters
d01bf4f4 704 if(accept &&!mLastStep)break;
74ea065c 705 } //end for on the steps
706
78c25bbc 707 /*****************/
708 /**** OLD CODE ***/
709 /*****************/
710#endif
711
78c25bbc 712 if ( nStepsDone == 0 ) {
115179c6 713 AliDebug(1,Form(" No track points for the track number %d",iseed));
78c25bbc 714 fnunmatch++;
715 delete trackTOFin;
716 continue;
717 }
718
115179c6 719 AliDebug(2,Form(" Number of steps done for the track number %d: %d",iseed,nStepsDone));
720
78c25bbc 721 /*****************/
722 /**** NEW CODE ***/
723 /*****************/
724
115179c6 725 Int_t *isClusterMatchable = NULL;
726 if(nc){
727 isClusterMatchable = new Int_t[nc];
728 for (Int_t i=0; i<nc; i++) isClusterMatchable[i] = kFALSE;
729 }
730
78c25bbc 731 Int_t nfound = 0;
732 Bool_t accept = kFALSE;
733 Bool_t isInside = kFALSE;
734 for (Int_t istep=0; istep<nStepsDone; istep++) {
735
736 Bool_t gotInsideCluster = kFALSE;
737 Int_t trackInsideCluster = -1;
738
739 Float_t ctrackPos[3];
740 ctrackPos[0] = trackPos[0][istep];
741 ctrackPos[1] = trackPos[1][istep];
742 ctrackPos[2] = trackPos[2][istep];
743
744 //now see whether the track matches any of the TOF clusters
745
746 Float_t dist3d[3]={0.,0.,0.};
747 accept = kFALSE;
748 for (Int_t i=0; i<nc; i++) {
749
750 // ***** NEW *****
751 /* check whether track was inside another cluster
752 * and in case inhibit this cluster.
753 * this will allow to only go on and add track points for
754 * that cluster where the track got inside first */
755 if (gotInsideCluster && trackInsideCluster != i) {
115179c6 756 AliDebug(3,Form(" A - istep=%d ~ %d %d ~ nfound=%d",istep,trackInsideCluster,i,nfound));
78c25bbc 757 continue;
758 }
115179c6 759 AliDebug(3,Form(" B - istep=%d ~ %d %d ~ nfound=%d",istep,trackInsideCluster,i,nfound));
78c25bbc 760
761 /* check whether track is inside this cluster */
762 for (Int_t hh=0; hh<3; hh++) dist3d[hh]=0.;
763 isInside = fGeom->IsInsideThePad((TGeoHMatrix*)(&global[i]),ctrackPos,dist3d);
764
765 // ***** NEW *****
766 /* if track is inside this cluster set flags which will then
767 * inhibit to add track points for the other clusters */
768 if (isInside) {
769 gotInsideCluster = kTRUE;
770 trackInsideCluster = i;
771 }
772
773 if ( mLastStep ) {
774 Float_t yLoc = dist3d[1];
775 Float_t rLoc = TMath::Sqrt(dist3d[0]*dist3d[0]+dist3d[2]*dist3d[2]);
776 accept = (TMath::Abs(yLoc)<padDepth*0.5 && rLoc<dCut);
115179c6 777 AliDebug(3," I am in the case mLastStep==kTRUE ");
78c25bbc 778 }
779
780 //***** NEW *****
781 /* add point everytime that:
782 * - the track is inside the cluster
783 * - the track got inside the cluster, even when it eventually exited the cluster
784 * - the tracks is within dCut from the cluster
785 */
786 if (accept || isInside || gotInsideCluster) {
787
788 fTOFtrackPoints->AddLast(new AliTOFtrackPoint(clind[i],
789 TMath::Sqrt(dist3d[0]*dist3d[0] + dist3d[1]*dist3d[1] + dist3d[2]*dist3d[2]),
790 dist3d[2],dist3d[0],dist3d[1],
791 AliTOFGeometry::RinTOF()+istep*stepSize,trackPos[3][istep]));
792
793 AliDebug(2,Form(" dist3dLoc[0] = %f, dist3dLoc[1] = %f, dist3dLoc[2] = %f ",dist3d[0],dist3d[1],dist3d[2]));
794 nfound++;
795
115179c6 796 AliDebug(3,Form(" C - istep=%d ~ %d %d ~ nfound=%d",istep,trackInsideCluster,i,nfound));
78c25bbc 797
115179c6 798
799 if(clind[i] < 20000 && mLastStep==2 && !isClusterMatchable[i]){ // add TOF clusters to the track
800 isClusterMatchable[i] = kTRUE;
801
802 //Tracking info
803 Double_t mom=t->GetP();
804 AliDebug(3,Form(" Momentum for track %d -> %f", iseed,mom));
805 Double_t time[AliPID::kSPECIESC];
806 // read from old structure (the one used by TPC in reco)
807 for(Int_t isp=0;isp<AliPID::kSPECIESC;isp++){
808 time[isp] = t->GetIntegratedTimesOld(isp); // in ps
809 Double_t mass=AliPID::ParticleMass(isp);
810 Double_t momz = mom*AliPID::ParticleCharge(isp);
811 time[isp]+=(trackPos[3][istep]-trackPos[3][0])/kSpeedOfLight*TMath::Sqrt(momz*momz+mass*mass)/momz;
812 //time[isp]+=(trackPos[3][istep]-trackPos[3][0])/kSpeedOfLight*TMath::Sqrt(mom*mom+mass*mass)/mom;
813 }
814
815 if(!fClusterESD[clind[i]]->Update(t->GetID(),dist3d[1],dist3d[0],dist3d[2],trackPos[3][istep],time))//x,y,z -> tracking RF
816 t->AddTOFcluster(clind[i]);
817 }
818
78c25bbc 819 // ***** NEW *****
820 /* do not break loop in any case
821 * if the track got inside a cluster all other clusters
822 * are inhibited */
823 // if(accept &&!mLastStep)break;
824
825 }//end if accept
826
827 } //end for on the clusters
828
829 // ***** NEW *****
830 /* do not break loop in any case
831 * if the track got inside a cluster all other clusters
832 * are inhibited but we want to go on adding track points */
833 // if(accept &&!mLastStep)break;
834
835 } //end for on the steps
115179c6 836 if(nc) delete[] isClusterMatchable;
78c25bbc 837
74ea065c 838 if (nfound == 0 ) {
115179c6 839 AliDebug(1,Form("No track points for the track number %d",iseed));
74ea065c 840 fnunmatch++;
7bf28302 841 delete trackTOFin;
74ea065c 842 continue;
843 }
844
115179c6 845 AliDebug(1,Form(" Number of track points for the track number %d: %d",iseed,nfound));
846
74ea065c 847 // now choose the cluster to be matched with the track.
848
90b234fe 849 Int_t idclus=-1;
74ea065c 850 Float_t recL = 0.;
851 Float_t xpos=0.;
852 Float_t mindist=1000.;
e0ddb533 853 Float_t mindistZ=0.;
24322e5c 854 Float_t mindistY=0.;
78c25bbc 855 Float_t mindistX=stepSize;
856 for (Int_t iclus= 0; iclus<nfound;iclus++) {
de60fa8a 857 AliTOFtrackPoint *matchableTOFcluster = (AliTOFtrackPoint*)fTOFtrackPoints->At(iclus);
78c25bbc 858 //if ( matchableTOFcluster->Distance()<mindist ) {
859 if ( TMath::Abs(matchableTOFcluster->DistanceX())<TMath::Abs(mindistX) &&
860 TMath::Abs(matchableTOFcluster->DistanceX())<=stepSize ) {
de60fa8a 861 mindist = matchableTOFcluster->Distance();
78c25bbc 862 mindistZ = matchableTOFcluster->DistanceZ(); // Z distance in the
863 // RF of the hit pad
864 // closest to the
865 // reconstructed
866 // track
de60fa8a 867 mindistY = matchableTOFcluster->DistanceY(); // Y distance in the
868 // RF of the hit pad
869 // closest to the
870 // reconstructed
871 // track
78c25bbc 872 mindistX = matchableTOFcluster->DistanceX(); // X distance in the
873 // RF of the hit pad
874 // closest to the
875 // reconstructed
876 // track
877 xpos = matchableTOFcluster->PropRadius();
de60fa8a 878 idclus = matchableTOFcluster->Index();
78c25bbc 879 recL = matchableTOFcluster->Length();// + corrLen*0.5;
880
115179c6 881 AliDebug(2,Form(" %d(%d) --- %f (%f, %f, %f), step=%f -- idclus=%d --- seed=%d, trackId=%d, trackLab=%d", iclus,nfound,
78c25bbc 882 mindist,mindistX,mindistY,mindistZ,stepSize,idclus,iseed,track->GetSeedIndex(),track->GetLabel()));
883
74ea065c 884 }
de60fa8a 885 } // loop on found TOF track points
74ea065c 886
78c25bbc 887 if (TMath::Abs(mindistX)>stepSize && idclus!=-1) {
115179c6 888 AliInfo(Form(" %d - not matched --- but idclus=%d, trackId=%d, trackLab=%d",iseed,
78c25bbc 889 idclus,track->GetSeedIndex(),track->GetLabel()));
890 idclus=-1;
891 }
892
1d834a1e 893 if (idclus==-1) {
894 AliDebug(1,Form("Reconstructed track %d doesn't match any TOF cluster", iseed));
78c25bbc 895 fnunmatch++;
1d834a1e 896 delete trackTOFin;
897 continue;
898 }
d01bf4f4 899
115179c6 900 AliDebug(1,Form(" %d - matched",iseed));
78c25bbc 901
902 fnmatch++;
903
74ea065c 904 AliTOFcluster *c=fClusters[idclus];
de60fa8a 905
115179c6 906 AliDebug(3, Form("%7d %7d %10d %10d %10d %10d %7d",
32ead898 907 iseed,
3a646035 908 fnmatch-1,
32ead898 909 TMath::Abs(trackTOFin->GetLabel()),
910 c->GetLabel(0), c->GetLabel(1), c->GetLabel(2),
911 idclus)); // AdC
912
e0ddb533 913 c->Use();
74ea065c 914
915 // Track length correction for matching Step 2
78c25bbc 916 /*
de60fa8a 917 if (mLastStep) {
918 Float_t rc = TMath::Sqrt(c->GetR()*c->GetR() + c->GetZ()*c->GetZ());
919 Float_t rt = TMath::Sqrt(trackPos[0][70]*trackPos[0][70]
920 +trackPos[1][70]*trackPos[1][70]
921 +trackPos[2][70]*trackPos[2][70]);
922 Float_t dlt=rc-rt;
74ea065c 923 recL=trackPos[3][70]+dlt;
de60fa8a 924 }
78c25bbc 925 */
74ea065c 926 if (
927 (c->GetLabel(0)==TMath::Abs(trackTOFin->GetLabel()))
928 ||
929 (c->GetLabel(1)==TMath::Abs(trackTOFin->GetLabel()))
930 ||
931 (c->GetLabel(2)==TMath::Abs(trackTOFin->GetLabel()))
932 ) {
933 fngoodmatch++;
d3c7bfac 934
6819758a 935 AliDebug(2,Form(" track label good %5d",trackTOFin->GetLabel()));
d3c7bfac 936
74ea065c 937 }
53884c34 938 else {
74ea065c 939 fnbadmatch++;
d3c7bfac 940
6819758a 941 AliDebug(2,Form(" track label bad %5d",trackTOFin->GetLabel()));
d3c7bfac 942
74ea065c 943 }
944
945 delete trackTOFin;
946
a533f541 947 // Store quantities to be used in the TOF Calibration
ba66add8 948 Float_t tToT=AliTOFGeometry::ToTBinWidth()*c->GetToT()*1E-3; // in ns
7aeeaf38 949 t->SetTOFsignalToT(tToT);
6819758a 950 Float_t rawTime=AliTOFGeometry::TdcBinWidth()*c->GetTDCRAW()+kTimeOffset; // RAW time,in ps
d321691a 951 t->SetTOFsignalRaw(rawTime);
952 t->SetTOFsignalDz(mindistZ);
46d7d82e 953 t->SetTOFsignalDx(mindistY);
8b441e14 954 t->SetTOFDeltaBC(c->GetDeltaBC());
955 t->SetTOFL0L1(c->GetL0L1Latency());
6819758a 956
24322e5c 957 Float_t info[10] = {mindist,mindistY,mindistZ,
958 0.,0.,0.,0.,0.,0.,0.};
959 t->SetTOFInfo(info);
115179c6 960 AliDebug(3,Form(" distance=%f; residual in the pad reference frame: dX=%f, dZ=%f", info[0],info[1],info[2]));
24322e5c 961
962
a533f541 963 Int_t ind[5];
964 ind[0]=c->GetDetInd(0);
965 ind[1]=c->GetDetInd(1);
966 ind[2]=c->GetDetInd(2);
967 ind[3]=c->GetDetInd(3);
968 ind[4]=c->GetDetInd(4);
ba66add8 969 Int_t calindex = AliTOFGeometry::GetIndex(ind);
a533f541 970 t->SetTOFCalChannel(calindex);
11c7ff68 971
972 // keep track of the track labels in the matched cluster
973 Int_t tlab[3];
974 tlab[0]=c->GetLabel(0);
975 tlab[1]=c->GetLabel(1);
976 tlab[2]=c->GetLabel(2);
115179c6 977 AliDebug(3,Form(" tdc time of the matched track %6d = ",c->GetTDC()));
6819758a 978 Double_t tof=AliTOFGeometry::TdcBinWidth()*c->GetTDC()+kTimeOffset; // in ps
115179c6 979 AliDebug(3,Form(" tof time of the matched track: %f = ",tof));
5478df1f 980 Double_t tofcorr=tof;
981 if(timeWalkCorr)tofcorr=CorrectTimeWalk(mindistZ,tof);
115179c6 982 AliDebug(3,Form(" tof time of the matched track, after TW corr: %f = ",tofcorr));
5478df1f 983 //Set TOF time signal and pointer to the matched cluster
984 t->SetTOFsignal(tofcorr);
ee77ad87 985 t->SetTOFcluster(idclus); // pointing to the recPoints tree
5478df1f 986
115179c6 987 AliDebug(3,Form(" Setting TOF raw time: %f, z distance: %f corrected time: %f ",rawTime,mindistZ,tofcorr));
6819758a 988
5478df1f 989 //Tracking info
115179c6 990 Double_t time[AliPID::kSPECIESC];
991 // read from old structure (the one used by TPC in reco)
992 for(Int_t isp=0;isp<AliPID::kSPECIESC;isp++){
993 time[isp] = t->GetIntegratedTimesOld(isp); // in ps
994 }
74ea065c 995 Double_t mom=t->GetP();
115179c6 996 AliDebug(3,Form(" Momentum for track %d -> %f", iseed,mom));
997 for (Int_t j=0;j<AliPID::kSPECIESC;j++) {
304864ab 998 Double_t mass=AliPID::ParticleMass(j);
115179c6 999 Double_t momz = mom*AliPID::ParticleCharge(j);
1000 time[j]+=(recL-trackPos[3][0])/kSpeedOfLight*TMath::Sqrt(momz*momz+mass*mass)/momz;
1001 //time[j]+=(recL-trackPos[3][0])/kSpeedOfLight*TMath::Sqrt(mom*mom+mass*mass)/mom;
74ea065c 1002 }
1003
1004 AliTOFtrack *trackTOFout = new AliTOFtrack(*t);
9a4d9088 1005 if (!(trackTOFout->PropagateTo(xpos))) {
1006 delete trackTOFout;
1007 continue;
1008 }
77a9ea9a 1009
78c25bbc 1010 // If necessary, rotate the track
1011 Double_t yATxposMax=xpos*TMath::Tan(0.5*AliTOFGeometry::GetAlpha());
1012 Double_t yATxpos=trackTOFout->GetY();
1013 if (yATxpos > yATxposMax) {
9a4d9088 1014 if (!(trackTOFout->Rotate(AliTOFGeometry::GetAlpha()))) {
1015 delete trackTOFout;
1016 continue;
1017 }
78c25bbc 1018 } else if (yATxpos < -yATxposMax) {
9a4d9088 1019 if (!(trackTOFout->Rotate(-AliTOFGeometry::GetAlpha()))) {
1020 delete trackTOFout;
1021 continue;
1022 }
78c25bbc 1023 }
1024
115179c6 1025 // Fill the track residual histograms and update track only if in the first two step (0 and 1)
1026 if(mLastStep < 2){
1027 FillResiduals(trackTOFout,c,kFALSE);
77a9ea9a 1028
115179c6 1029 t->UpdateTrackParams(trackTOFout,AliESDtrack::kTOFout);
5478df1f 1030
115179c6 1031// don't update old structure with TOF info
1032// t->SetIntegratedLength(recL);
1033// t->SetIntegratedTimes(time);
1034// t->SetTOFLabel(tlab);
5478df1f 1035
115179c6 1036 // add tof cluster to the track also for step 2
1037 if(idclus < 20000){
1038 fClusterESD[idclus]->Update(t->GetID(),mindistY,mindist,mindistZ,recL,time);//x,y,z -> tracking RF
1039
1040 t->AddTOFcluster(idclus);
1041 }
1042 else{
1043 AliInfo("Too many TOF clusters matched with tracks (> 20000)");
1044 }
1045
1046 }
5664c6ed 1047 // Fill Reco-QA histos for Reconstruction
1048 fHRecNClus->Fill(nc);
1049 fHRecDist->Fill(mindist);
38c767fa 1050 if (cov[0]>=0.)
1051 fHRecSigYVsP->Fill(mom,TMath::Sqrt(cov[0]));
1052 else
57bd70af 1053 fHRecSigYVsP->Fill(mom,-TMath::Sqrt(-cov[0]));
38c767fa 1054 if (cov[2]>=0.)
1055 fHRecSigZVsP->Fill(mom,TMath::Sqrt(cov[2]));
1056 else
57bd70af 1057 fHRecSigZVsP->Fill(mom,-TMath::Sqrt(-cov[2]));
e0ddb533 1058 fHRecSigYVsPWin->Fill(mom,dphi*sensRadius);
5664c6ed 1059 fHRecSigZVsPWin->Fill(mom,dz);
1060
1061 // Fill Tree for on-the-fly offline Calibration
1062
53884c34 1063 if ( !((t->GetStatus() & AliESDtrack::kTIME)==0 ) ) {
5664c6ed 1064 fIch=calindex;
1065 fToT=tToT;
5478df1f 1066 fTime=rawTime;
5664c6ed 1067 fExpTimePi=time[2];
1068 fExpTimeKa=time[3];
1069 fExpTimePr=time[4];
1070 fCalTree->Fill();
1071 }
74ea065c 1072 delete trackTOFout;
1073 }
de60fa8a 1074
115179c6 1075
7bf28302 1076 for (Int_t ii=0; ii<4; ii++) delete [] trackPos[ii];
128563f6 1077 delete [] clind;
ba66add8 1078
74ea065c 1079}
1080//_________________________________________________________________________
7b61cd9c 1081Int_t AliTOFtracker::LoadClusters(TTree *cTree) {
74ea065c 1082 //--------------------------------------------------------------------
1083 //This function loads the TOF clusters
1084 //--------------------------------------------------------------------
1085
ba66add8 1086 Int_t npadX = AliTOFGeometry::NpadX();
1087 Int_t npadZ = AliTOFGeometry::NpadZ();
1088 Int_t nStripA = AliTOFGeometry::NStripA();
1089 Int_t nStripB = AliTOFGeometry::NStripB();
1090 Int_t nStripC = AliTOFGeometry::NStripC();
5664c6ed 1091
7b61cd9c 1092 TBranch *branch=cTree->GetBranch("TOF");
74ea065c 1093 if (!branch) {
7b61cd9c 1094 AliError("can't get the branch with the TOF clusters !");
74ea065c 1095 return 1;
1096 }
1097
9d802709 1098 static TClonesArray dummy("AliTOFcluster",10000);
1099 dummy.Clear();
1100 TClonesArray *clusters=&dummy;
7b61cd9c 1101 branch->SetAddress(&clusters);
74ea065c 1102
7b61cd9c 1103 cTree->GetEvent(0);
1104 Int_t nc=clusters->GetEntriesFast();
5664c6ed 1105 fHDigNClus->Fill(nc);
1106
7b61cd9c 1107 AliInfo(Form("Number of clusters: %d",nc));
74ea065c 1108
ba597140 1109 fN = 0;
115179c6 1110 fNTOFmatched = 0;
1111 for(Int_t i=0; i< 20000;i++){
1112 if(fClusterESD[i]){
1113 delete fClusterESD[i];
1114 fClusterESD[i] = NULL;
1115 }
1116 }
1117
7b61cd9c 1118 for (Int_t i=0; i<nc; i++) {
1119 AliTOFcluster *c=(AliTOFcluster*)clusters->UncheckedAt(i);
16a2e36a 1120//PH fClusters[i]=new AliTOFcluster(*c); fN++;
1121 fClusters[i]=c; fN++;
5664c6ed 1122
1123 // Fill Digits QA histos
1124
1125 Int_t isector = c->GetDetInd(0);
1126 Int_t iplate = c->GetDetInd(1);
1127 Int_t istrip = c->GetDetInd(2);
1128 Int_t ipadX = c->GetDetInd(4);
1129 Int_t ipadZ = c->GetDetInd(3);
1130
ba66add8 1131 Float_t time =(AliTOFGeometry::TdcBinWidth()*c->GetTDC())*1E-3; // in ns
1132 Float_t tot = (AliTOFGeometry::TdcBinWidth()*c->GetToT())*1E-3;//in ns
115179c6 1133
1134 Int_t ind[5];
1135 ind[0]=isector;
1136 ind[1]=iplate;
1137 ind[2]=istrip;
fedc2fd0 1138 ind[3]=ipadZ;
1139 ind[4]=ipadX;
115179c6 1140
1141 Int_t calindex = AliTOFGeometry::GetIndex(ind);
1142 Int_t tofLabels[3]={c->GetLabel(0),c->GetLabel(1),c->GetLabel(2)};
5664c6ed 1143
1144 Int_t stripOffset = 0;
1145 switch (iplate) {
1146 case 0:
1147 stripOffset = 0;
1148 break;
1149 case 1:
1150 stripOffset = nStripC;
1151 break;
1152 case 2:
1153 stripOffset = nStripC+nStripB;
1154 break;
1155 case 3:
1156 stripOffset = nStripC+nStripB+nStripA;
1157 break;
1158 case 4:
1159 stripOffset = nStripC+nStripB+nStripA+nStripB;
1160 break;
1161 default:
1162 AliError(Form("Wrong plate number in TOF (%d) !",iplate));
1163 break;
1164 };
1165 Int_t zindex=npadZ*(istrip+stripOffset)+(ipadZ+1);
1166 Int_t phiindex=npadX*isector+ipadX+1;
1167 fHDigClusMap->Fill(zindex,phiindex);
1168 fHDigClusTime->Fill(time);
1169 fHDigClusToT->Fill(tot);
1170
115179c6 1171 if(fNTOFmatched < 20000){
1172 fClusterESD[fNTOFmatched] = new AliESDTOFcluster(i,calindex,
1173 AliTOFGeometry::TdcBinWidth()*c->GetTDC()/*ps*/,
1174 AliTOFGeometry::TdcBinWidth()*c->GetTDCRAW()/*ps*/,
1175 AliTOFGeometry::ToTBinWidth()*c->GetToT()*1E-3/*ns*/,
1176 tofLabels,
1177 c->GetDeltaBC(),c->GetL0L1Latency(),
1178 c->GetStatus(),c->GetZ(),c->GetPhi(),c->GetR());
1179 fNTOFmatched++;
1180 }
1181
7b61cd9c 1182 }
74ea065c 1183
74ea065c 1184
115179c6 1185 if(fNTOFmatched == 0)
1186 fClusterESD[0] = new AliESDTOFcluster();
1187
74ea065c 1188 return 0;
1189}
1190//_________________________________________________________________________
1191void AliTOFtracker::UnloadClusters() {
1192 //--------------------------------------------------------------------
1193 //This function unloads TOF clusters
1194 //--------------------------------------------------------------------
7b61cd9c 1195 for (Int_t i=0; i<fN; i++) {
16a2e36a 1196//PH delete fClusters[i];
7b61cd9c 1197 fClusters[i] = 0x0;
74ea065c 1198 }
7b61cd9c 1199 fN=0;
74ea065c 1200}
1201
1202//_________________________________________________________________________
1203Int_t AliTOFtracker::FindClusterIndex(Double_t z) const {
1204 //--------------------------------------------------------------------
1205 // This function returns the index of the nearest cluster
1206 //--------------------------------------------------------------------
1207 if (fN==0) return 0;
1208 if (z <= fClusters[0]->GetZ()) return 0;
1209 if (z > fClusters[fN-1]->GetZ()) return fN;
1210 Int_t b=0, e=fN-1, m=(b+e)/2;
1211 for (; b<e; m=(b+e)/2) {
1212 if (z > fClusters[m]->GetZ()) b=m+1;
1213 else e=m;
1214 }
1215 return m;
1216}
d4754572 1217
1218//_________________________________________________________________________
1219Bool_t AliTOFtracker::GetTrackPoint(Int_t index, AliTrackPoint& p) const
1220{
1221 // Get track space point with index i
1222 // Coordinates are in the global system
1223 AliTOFcluster *cl = fClusters[index];
1224 Float_t xyz[3];
1225 xyz[0] = cl->GetR()*TMath::Cos(cl->GetPhi());
1226 xyz[1] = cl->GetR()*TMath::Sin(cl->GetPhi());
1227 xyz[2] = cl->GetZ();
468f26c6 1228 Float_t phiangle = (Int_t(cl->GetPhi()*TMath::RadToDeg()/20.)+0.5)*20.*TMath::DegToRad();
1229 Float_t sinphi = TMath::Sin(phiangle), cosphi = TMath::Cos(phiangle);
ba66add8 1230 Float_t tiltangle = AliTOFGeometry::GetAngles(cl->GetDetInd(1),cl->GetDetInd(2))*TMath::DegToRad();
468f26c6 1231 Float_t sinth = TMath::Sin(tiltangle), costh = TMath::Cos(tiltangle);
ba66add8 1232 Float_t sigmay2 = AliTOFGeometry::XPad()*AliTOFGeometry::XPad()/12.;
1233 Float_t sigmaz2 = AliTOFGeometry::ZPad()*AliTOFGeometry::ZPad()/12.;
468f26c6 1234 Float_t cov[6];
1235 cov[0] = sinphi*sinphi*sigmay2 + cosphi*cosphi*sinth*sinth*sigmaz2;
1236 cov[1] = -sinphi*cosphi*sigmay2 + sinphi*cosphi*sinth*sinth*sigmaz2;
1237 cov[2] = -cosphi*sinth*costh*sigmaz2;
1238 cov[3] = cosphi*cosphi*sigmay2 + sinphi*sinphi*sinth*sinth*sigmaz2;
1239 cov[4] = -sinphi*sinth*costh*sigmaz2;
1240 cov[5] = costh*costh*sigmaz2;
1241 p.SetXYZ(xyz[0],xyz[1],xyz[2],cov);
d4754572 1242
1243 // Detector numbering scheme
ba66add8 1244 Int_t nSector = AliTOFGeometry::NSectors();
1245 Int_t nPlate = AliTOFGeometry::NPlates();
1246 Int_t nStripA = AliTOFGeometry::NStripA();
1247 Int_t nStripB = AliTOFGeometry::NStripB();
1248 Int_t nStripC = AliTOFGeometry::NStripC();
d4754572 1249
1250 Int_t isector = cl->GetDetInd(0);
1251 if (isector >= nSector)
1252 AliError(Form("Wrong sector number in TOF (%d) !",isector));
1253 Int_t iplate = cl->GetDetInd(1);
1254 if (iplate >= nPlate)
1255 AliError(Form("Wrong plate number in TOF (%d) !",iplate));
1256 Int_t istrip = cl->GetDetInd(2);
1257
1258 Int_t stripOffset = 0;
1259 switch (iplate) {
1260 case 0:
1261 stripOffset = 0;
1262 break;
1263 case 1:
1264 stripOffset = nStripC;
1265 break;
1266 case 2:
1267 stripOffset = nStripC+nStripB;
1268 break;
1269 case 3:
1270 stripOffset = nStripC+nStripB+nStripA;
1271 break;
1272 case 4:
1273 stripOffset = nStripC+nStripB+nStripA+nStripB;
1274 break;
1275 default:
1276 AliError(Form("Wrong plate number in TOF (%d) !",iplate));
1277 break;
1278 };
1279
1280 Int_t idet = (2*(nStripC+nStripB)+nStripA)*isector +
1281 stripOffset +
1282 istrip;
ae079791 1283 UShort_t volid = AliGeomManager::LayerToVolUID(AliGeomManager::kTOF,idet);
d4754572 1284 p.SetVolumeID((UShort_t)volid);
1285 return kTRUE;
1286}
5664c6ed 1287//_________________________________________________________________________
1288void AliTOFtracker::InitCheckHists() {
1289
1290 //Init histos for Digits/Reco QA and Calibration
1291
1292
0366f10f 1293 TDirectory *dir = gDirectory;
1294 TFile *logFileTOF = 0;
1295
1296 TSeqCollection *list = gROOT->GetListOfFiles();
1297 int n = list->GetEntries();
1298 Bool_t isThere=kFALSE;
1299 for(int i=0; i<n; i++) {
1300 logFileTOF = (TFile*)list->At(i);
1301 if (strstr(logFileTOF->GetName(), "TOFQA.root")){
1302 isThere=kTRUE;
1303 break;
1304 }
1305 }
1306
1307 if(!isThere)logFileTOF = new TFile( "TOFQA.root","RECREATE");
1308 logFileTOF->cd();
1309
5664c6ed 1310 fCalTree = new TTree("CalTree", "Tree for TOF calibration");
1311 fCalTree->Branch("TOFchannelindex",&fIch,"iTOFch/I");
1312 fCalTree->Branch("ToT",&fToT,"TOFToT/F");
1313 fCalTree->Branch("TOFtime",&fTime,"TOFtime/F");
1314 fCalTree->Branch("PionExpTime",&fExpTimePi,"PiExpTime/F");
1315 fCalTree->Branch("KaonExpTime",&fExpTimeKa,"KaExpTime/F");
1316 fCalTree->Branch("ProtonExpTime",&fExpTimePr,"PrExpTime/F");
1317
1318 //Digits "QA"
1319 fHDigClusMap = new TH2F("TOFDig_ClusMap", "",182,0.5,182.5,864, 0.5,864.5);
1320 fHDigNClus = new TH1F("TOFDig_NClus", "",200,0.5,200.5);
1321 fHDigClusTime = new TH1F("TOFDig_ClusTime", "",2000,0.,200.);
1322 fHDigClusToT = new TH1F("TOFDig_ClusToT", "",500,0.,100);
1323
1324 //Reco "QA"
1325 fHRecNClus =new TH1F("TOFRec_NClusW", "",50,0.5,50.5);
1326 fHRecDist=new TH1F("TOFRec_Dist", "",50,0.5,10.5);
1327 fHRecSigYVsP=new TH2F("TOFDig_SigYVsP", "",40,0.,4.,100, 0.,5.);
1328 fHRecSigZVsP=new TH2F("TOFDig_SigZVsP", "",40,0.,4.,100, 0.,5.);
1329 fHRecSigYVsPWin=new TH2F("TOFDig_SigYVsPWin", "",40,0.,4.,100, 0.,50.);
1330 fHRecSigZVsPWin=new TH2F("TOFDig_SigZVsPWin", "",40,0.,4.,100, 0.,50.);
0366f10f 1331
1332 dir->cd();
1333
5664c6ed 1334}
1335
1336//_________________________________________________________________________
1337void AliTOFtracker::SaveCheckHists() {
1338
1339 //write histos for Digits/Reco QA and Calibration
1340
1341 TDirectory *dir = gDirectory;
5664c6ed 1342 TFile *logFileTOF = 0;
1343
1344 TSeqCollection *list = gROOT->GetListOfFiles();
d200609f 1345 int n = list->GetEntries();
5664c6ed 1346 Bool_t isThere=kFALSE;
d200609f 1347 for(int i=0; i<n; i++) {
5664c6ed 1348 logFileTOF = (TFile*)list->At(i);
1349 if (strstr(logFileTOF->GetName(), "TOFQA.root")){
1350 isThere=kTRUE;
1351 break;
1352 }
1353 }
1354
0366f10f 1355 if(!isThere) {
1356 AliError(Form("File TOFQA.root not found!! not wring histograms...."));
1357 return;
1358 }
5664c6ed 1359 logFileTOF->cd();
1360 fHDigClusMap->Write(fHDigClusMap->GetName(), TObject::kOverwrite);
1361 fHDigNClus->Write(fHDigNClus->GetName(), TObject::kOverwrite);
1362 fHDigClusTime->Write(fHDigClusTime->GetName(), TObject::kOverwrite);
1363 fHDigClusToT->Write(fHDigClusToT->GetName(), TObject::kOverwrite);
1364 fHRecNClus->Write(fHRecNClus->GetName(), TObject::kOverwrite);
1365 fHRecDist->Write(fHRecDist->GetName(), TObject::kOverwrite);
1366 fHRecSigYVsP->Write(fHRecSigYVsP->GetName(), TObject::kOverwrite);
1367 fHRecSigZVsP->Write(fHRecSigZVsP->GetName(), TObject::kOverwrite);
1368 fHRecSigYVsPWin->Write(fHRecSigYVsPWin->GetName(), TObject::kOverwrite);
1369 fHRecSigZVsPWin->Write(fHRecSigZVsPWin->GetName(), TObject::kOverwrite);
1370 fCalTree->Write(fCalTree->GetName(),TObject::kOverwrite);
1371 logFileTOF->Flush();
1372
1373 dir->cd();
1374 }
e0ddb533 1375//_________________________________________________________________________
f31a249f 1376Float_t AliTOFtracker::CorrectTimeWalk( Float_t dist, Float_t tof) const {
e0ddb533 1377
1378 //dummy, for the moment
1379 Float_t tofcorr=0.;
ba66add8 1380 if(dist<AliTOFGeometry::ZPad()*0.5){
e0ddb533 1381 tofcorr=tof;
1382 //place here the actual correction
1383 }else{
1384 tofcorr=tof;
1385 }
1386 return tofcorr;
1387}
1388//_________________________________________________________________________
128563f6 1389
1390void AliTOFtracker::FillClusterArray(TObjArray* arr) const
1391{
1392 //
1393 // Returns the TOF cluster array
1394 //
1395
1396 if (fN==0)
1397 arr = 0x0;
1398 else
1399 for (Int_t i=0; i<fN; ++i) arr->Add(fClusters[i]);
1400
1401}
ff826920 1402//_________________________________________________________________________
1403