Include of TMath moved from .cxx to .h
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTracker.cxx
CommitLineData
fe17d4cb 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// Class AliEMCALTracker
17// -----------------------
18// Implementation of the track matching method between barrel tracks and
19// EMCAL clusters.
20// Besides algorithm implementation, some cuts are required to be set
21// in order to define, for each track, an acceptance window where clusters
22// are searched to find best match (if any).
23// The class accepts as input an ESD container, and works directly on it,
24// simply setting, for each of its tracks, the fEMCALindex flag, for each
25// track which is matched to a cluster.
26// In order to use method, one must launch PropagateBack().
27//
28// ------------------------------------------------------------------------
29// author: A. Pulvirenti (alberto.pulvirenti@ct.infn.it)
30//=========================================================================
31
32#include <Riostream.h>
33#include <iomanip>
34
35#include <TFile.h>
36#include <TTree.h>
fe17d4cb 37#include <TList.h>
38#include <TString.h>
39#include <TVector3.h>
40#include <TClonesArray.h>
c61f0e70 41#include <TGeoMatrix.h>
fe17d4cb 42
43#include "AliLog.h"
44#include "AliESD.h"
45#include "AliESDtrack.h"
46#include "AliKalmanTrack.h"
47#include "AliEMCALRecPoint.h"
48#include "AliRunLoader.h"
49#include "AliEMCALTrack.h"
50#include "AliEMCALLoader.h"
c61f0e70 51#include "AliEMCALGeometry.h"
52
fe17d4cb 53#include "AliEMCALTracker.h"
54
55ClassImp(AliEMCALTracker)
56//
57//------------------------------------------------------------------------------
58//
59AliEMCALTracker::AliEMCALTracker()
60 : AliTracker(),
61 fNPropSteps(0),
62 fTrackCorrMode(kTrackCorrNone),
63 fCutX(50.0),
64 fCutY(50.0),
65 fCutZ(50.0),
66 fCutAlphaMin(-200.0),
67 fCutAlphaMax(200.0),
68 fCutAngle(100.0),
69 fMaxDist(100.0),
70 fRho(1.0),
71 fX0(1.0),
72 fTracks(0),
73 fClusters(0),
c61f0e70 74 fMatches(0),
75 fGeom(0)
fe17d4cb 76{
77 //
78 // Default constructor.
79 // Initializes al simple data members to default values,
80 // and all collections to NULL.
81 // Output file name is set to a default value.
82 //
83}
84//
85//------------------------------------------------------------------------------
86//
87AliEMCALTracker::AliEMCALTracker(const AliEMCALTracker& copy)
88 : AliTracker(),
89 fNPropSteps(copy.fNPropSteps),
90 fTrackCorrMode(copy.fTrackCorrMode),
91 fCutX(copy.fCutX),
92 fCutY(copy.fCutY),
93 fCutZ(copy.fCutZ),
94 fCutAlphaMin(copy.fCutAlphaMin),
95 fCutAlphaMax(copy.fCutAlphaMax),
96 fCutAngle(copy.fCutAngle),
97 fMaxDist(copy.fMaxDist),
98 fRho(copy.fRho),
99 fX0(copy.fX0),
100 fTracks((TObjArray*)copy.fTracks->Clone()),
101 fClusters((TObjArray*)copy.fClusters->Clone()),
c61f0e70 102 fMatches((TList*)copy.fMatches->Clone()),
103 fGeom(copy.fGeom)
fe17d4cb 104{
105 //
106 // Copy constructor
107 // Besides copying all parameters, duplicates all collections.
108 //
109}
110//
111//------------------------------------------------------------------------------
112//
113AliEMCALTracker& AliEMCALTracker::operator=(const AliEMCALTracker& copy)
114{
115 //
116 // Assignment operator.
117 // Besides copying all parameters, duplicates all collections.
118 //
119
120 fCutX = copy.fCutX;
121 fCutY = copy.fCutY;
122 fCutZ = copy.fCutZ;
123 fCutAlphaMin = copy.fCutAlphaMin;
124 fCutAlphaMax = copy.fCutAlphaMax;
125 fCutAngle = copy.fCutAngle;
126 fMaxDist = copy.fMaxDist;
127
128 fTracks = (TObjArray*)copy.fTracks->Clone();
129 fClusters = (TObjArray*)copy.fClusters->Clone();
130 fMatches = (TList*)copy.fMatches->Clone();
131
c61f0e70 132 fGeom = copy.fGeom;
133
fe17d4cb 134 return (*this);
135}
136//
137//------------------------------------------------------------------------------
138//
c61f0e70 139TTree* AliEMCALTracker::SearchTrueMatches()
140{
141 if (!fClusters) return 0;
142 if (fClusters->IsEmpty()) return 0;
143 if (!fTracks) return 0;
144 if (fTracks->IsEmpty()) return 0;
145
146 TTree *outTree = new TTree("tree", "True matches from event");
147 Int_t indexT, indexC, label;
148 outTree->Branch("indexC", &indexC, "indexC/I");
149 outTree->Branch("indexT", &indexT, "indexT/I");
150 outTree->Branch("label", &label , "label/I");
151
152 Double_t dist;
153 Int_t ic, nClusters = (Int_t)fClusters->GetEntries();
154 Int_t it, nTracks = fTracks->GetEntries();
155
156 for (ic = 0; ic < nClusters; ic++) {
157 AliEMCALMatchCluster *cluster = (AliEMCALMatchCluster*)fClusters->At(ic);
158 label = cluster->Label();
159 indexC = cluster->Index();
160 for (it = 0; it < nTracks; it++) {
161 AliEMCALTrack *track = (AliEMCALTrack*)fTracks->At(it);
162 if (TMath::Abs(track->GetSeedLabel()) != label) continue;
163 dist = CheckPair(track, cluster);
164 if (dist <= fMaxDist) {
165 indexT = track->GetSeedIndex();
166 outTree->Fill();
167 }
168 }
169 }
170
171 return outTree;
172}
173//
174//------------------------------------------------------------------------------
175//
fe17d4cb 176void AliEMCALTracker::Clear(Option_t* option)
177{
178 //
179 // Clearing method
180 // Clears all specified arrays and the containers themselves.
181 //
182
183 TString opt(option);
c61f0e70 184 Bool_t doDelete = opt.Contains("DELETE");
fe17d4cb 185 Bool_t clearTracks = opt.Contains("TRACKS");
186 Bool_t clearClusters = opt.Contains("CLUSTERS");
187 Bool_t clearMatches = opt.Contains("MATCHES");
188 if (opt.Contains("ALL")) {
189 clearTracks = kTRUE;
190 clearClusters = kTRUE;
191 clearMatches = kTRUE;
192 }
193
194 if (fTracks != 0x0 && clearTracks) {
c61f0e70 195 if (!doDelete) {
196 fTracks->Clear();
197 }
198 else {
199 if (!fTracks->IsEmpty()) fTracks->Delete();
200 delete fTracks;
201 fTracks = 0;
202 }
fe17d4cb 203 }
204 if (fClusters != 0x0 && clearClusters) {
c61f0e70 205 if (!doDelete) {
206 fClusters->Clear();
207 }
208 else {
209 if (!fClusters->IsEmpty()) fClusters->Delete();
210 delete fClusters;
211 fClusters = 0;
212 }
fe17d4cb 213 }
214 if (fMatches != 0x0 && clearMatches) {
c61f0e70 215 if (!doDelete) {
216 fMatches->Clear();
217 }
218 else {
219 if (!fMatches->IsEmpty()) fMatches->Delete();
220 delete fMatches;
221 fMatches = 0;
222 }
fe17d4cb 223 }
224}
225//
226//------------------------------------------------------------------------------
227//
228Int_t AliEMCALTracker::LoadClusters(TTree *cTree)
229{
230 //
231 // Load EMCAL clusters in the form of AliEMCALRecPoint,
232 // from simulation temporary files.
233 // (When included in reconstruction chain, this method is used automatically)
234 //
235
236 Clear("CLUSTERS");
237
238 TBranch *branch = cTree->GetBranch("EMCALECARP");
239 if (!branch) {
c61f0e70 240 AliError("Can't get the branch with the EMCAL clusters");
fe17d4cb 241 return 1;
242 }
243
244 TClonesArray dummy("AliEMCALRecPoint", 10000);
245 TClonesArray *clusters = &dummy;
246 branch->SetAddress(&clusters);
247 Int_t nClusters = (Int_t)clusters->GetEntries();
248
249 cTree->GetEvent(0);
250 fClusters = new TObjArray(0);
251 for (Int_t i = 0; i < nClusters; i++) {
252 AliEMCALRecPoint *cluster = (AliEMCALRecPoint*)clusters->At(i);
253 if (!cluster) continue;
254 if (cluster->GetClusterType() != AliESDCaloCluster::kClusterv1) continue;
255 AliEMCALMatchCluster *matchCluster = new AliEMCALMatchCluster(i, cluster);
256 fClusters->AddLast(matchCluster);
257 }
258 if (fClusters->IsEmpty()) {
259 AliError("No clusters collected");
260 return 1;
261 }
262
263 AliInfo(Form("Collected %d clusters", fClusters->GetEntries()));
264
265 return 0;
266}
267//
268//------------------------------------------------------------------------------
269//
270Int_t AliEMCALTracker::LoadClusters(AliESD *esd)
271{
272 //
273 // Load EMCAL clusters in the form of AliESDCaloClusters,
274 // from an AliESD object.
275 //
276
277 // make sure that tracks/clusters collections are empty
278 Clear("CLUSTERS");
279
280 Int_t start = esd->GetFirstEMCALCluster();
281 Int_t nClustersEMC = esd->GetNumberOfEMCALClusters();
282 Int_t end = start + nClustersEMC;
283
284 fClusters = new TObjArray(0);
285
286 Int_t i;
287 for (i = start; i < end; i++) {
288 AliESDCaloCluster *cluster = esd->GetCaloCluster(i);
289 if (!cluster) continue;
290 if (cluster->GetClusterType() != AliESDCaloCluster::kClusterv1) continue;
291 AliEMCALMatchCluster *matchCluster = new AliEMCALMatchCluster(i, cluster);
292 fClusters->AddLast(matchCluster);
293 }
294 if (fClusters->IsEmpty()) {
295 AliError("No clusters collected");
296 return 1;
297 }
298
299 AliInfo(Form("Collected %d clusters", fClusters->GetEntries()));
300
301 return 0;
302}
303//
304//------------------------------------------------------------------------------
305//
306Int_t AliEMCALTracker::LoadTracks(AliESD *esd)
307{
308 //
309 // Load ESD tracks.
310 //
311
312 Clear("TRACKS");
313
314 Int_t nTracks = esd->GetNumberOfTracks();
315 fTracks = new TObjArray(0);
316
317 Int_t i, j;
318 Bool_t isKink;
319 Double_t alpha;
320 for (i = 0; i < nTracks; i++) {
321 AliESDtrack *esdTrack = esd->GetTrack(i);
322 // set by default the value corresponding to "no match"
c61f0e70 323 esdTrack->SetEMCALcluster(kUnmatched);
fe17d4cb 324// if (esdTrack->GetLabel() < 0) continue;
325// if (!(esdTrack->GetStatus() & AliESDtrack::kTOFout)) continue;
326 isKink = kFALSE;
327 for (j = 0; j < 3; j++) {
c61f0e70 328 if (esdTrack->GetKinkIndex(j) != 0) isKink = kTRUE;
fe17d4cb 329 }
330 if (isKink) continue;
331 AliEMCALTrack *track = new AliEMCALTrack(*esdTrack);
c61f0e70 332 track->SetMass(0.13957018);
fe17d4cb 333 // check alpha and reject the tracks which fall outside EMCAL acceptance
334 alpha = track->GetAlpha() * TMath::RadToDeg();
335 if (alpha > -155.0 && alpha < 67.0) {
336 delete track;
337 continue;
338 }
c61f0e70 339// if (!PropagateToEMCAL(track)) {
340// delete track;
341// continue;
342// }
fe17d4cb 343 track->SetSeedIndex(i);
344 track->SetSeedLabel(esdTrack->GetLabel());
345 fTracks->AddLast(track);
346 }
347 if (fTracks->IsEmpty()) {
348 AliError("No tracks collected");
349 return 1;
350 }
351
352 AliInfo(Form("Collected %d tracks", fTracks->GetEntries()));
353
354 return 0;
355}
356//
357//------------------------------------------------------------------------------
358//
359Int_t AliEMCALTracker::PropagateBack(AliESD* esd)
360{
361 //
362 // Main operation method.
363 // Gets external AliESD containing tracks to be matched.
364 // After executing match finding, stores in the same ESD object all infos
365 // and releases the object for further reconstruction steps.
366 //
367
368 if (!esd) {
369 AliError("NULL ESD passed");
370 return 1;
371 }
372
373 // step 1:
374 // if cluster array is empty, cluster are collected
375 // from the passed ESD, and work is done with ESDCaloClusters
c61f0e70 376 Int_t okLoadClusters, nClusters;
fe17d4cb 377 if (!fClusters || (fClusters && fClusters->IsEmpty())) {
fe17d4cb 378 okLoadClusters = LoadClusters(esd);
379 if (okLoadClusters) return 2;
380 }
c61f0e70 381 nClusters = fClusters->GetEntries();
fe17d4cb 382
383 // step 2:
384 // collect ESD tracks
c61f0e70 385 Int_t okLoadTracks = LoadTracks(esd), nTracks;
fe17d4cb 386 if (okLoadTracks) return 3;
c61f0e70 387 nTracks = fTracks->GetEntries();
fe17d4cb 388
389 // step 3:
390 // each track is propagated to the "R" position of each cluster.
391 // The closest cluster is assigned as match.
392 // IF no clusters lie within the maximum allowed distance, no matches are assigned.
393 Int_t nMatches = CreateMatches();
394 if (!nMatches) {
c61f0e70 395 AliInfo(Form("#clusters = %d -- #tracks = %d --> No good matches found.", nClusters, nTracks));
fe17d4cb 396 return 4;
397 }
398 else {
c61f0e70 399 AliInfo(Form("#clusters = %d -- #tracks = %d --> Found %d matches.", nClusters, nTracks, nMatches));
fe17d4cb 400 }
401
402 // step 4:
403 // when more than 1 track share the same matched cluster, only the closest one is kept.
404 Int_t nRemoved = SolveCompetitions();
405 AliInfo(Form("Removed %d duplicate matches", nRemoved));
406 if (nRemoved >= nMatches) {
407 AliError("Removed ALL matches! Check the algorithm or data. Nothing to save");
408 return 5;
409 }
410
411 // step 5:
412 // save obtained information setting the 'fEMCALindex' field of AliESDtrack object
c61f0e70 413 Int_t nSaved = 0, trackID, nGood = 0, nFake = 0;
fe17d4cb 414 TListIter iter(fMatches);
415 AliEMCALMatch *match = 0;
416 while ( (match = (AliEMCALMatch*)iter.Next()) ) {
417 if (!match->CanBeSaved()) continue;
418 AliEMCALTrack *track = (AliEMCALTrack*)fTracks->At(match->GetIndexT());
419 AliEMCALMatchCluster *cluster = (AliEMCALMatchCluster*)fClusters->At(match->GetIndexC());
420 trackID = track->GetSeedIndex();
421 AliESDtrack *esdTrack = esd->GetTrack(trackID);
422 if (!esdTrack) continue;
c61f0e70 423 if (TMath::Abs(esdTrack->GetLabel()) == cluster->Label()) {
fe17d4cb 424 esdTrack->SetEMCALcluster(cluster->Index());
c61f0e70 425 nGood++;
fe17d4cb 426 }
427 else {
428 esdTrack->SetEMCALcluster(-cluster->Index());
c61f0e70 429 nFake++;
fe17d4cb 430 }
431 nSaved++;
432 }
433 /*
434 AliEMCALTrack *track = 0;
435 TObjArrayIter tracks(fTracks);
436 while ( (track = (AliEMCALTrack*)tracks.Next()) ) {
437 trackID = track->GetSeedIndex();
438 clusterID = track->GetMatchedClusterIndex();
439 AliESDtrack *esdTrack = esd->GetTrack(trackID);
440 if (!esdTrack) continue;
441 if (clusterID < 0) {
c61f0e70 442 esdTrack->SetEMCALcluster(kUnmatched);
fe17d4cb 443 }
444 else {
445 AliEMCALMatchCluster *cluster = (AliEMCALMatchCluster*)fClusters->At(clusterID);
446 if (!cluster) continue;
447 if (esdTrack->GetLabel() == cluster->Label()) {
448 nGood++;
449 esdTrack->SetEMCALcluster(cluster->Index());
450 }
451 else {
452 esdTrack->SetEMCALcluster(-cluster->Index());
453 }
454 nSaved++;
455 }
456 }
457 */
c61f0e70 458 AliInfo(Form("Saved %d matches [%d good + %d fake]", nSaved, nGood, nFake));
fe17d4cb 459
460 return 0;
461}
462//
463//------------------------------------------------------------------------------
464//
465void AliEMCALTracker::SetTrackCorrectionMode(Option_t *option)
466{
467 //
468 // Set track correction mode
469 // gest the choice in string format and converts into
470 // internal enum
471 //
472
473 TString opt(option);
474 opt.ToUpper();
475
476 if (!opt.CompareTo("NONE")) {
477 fTrackCorrMode = kTrackCorrNone;
478 }
479 else if (!opt.CompareTo("MMB")) {
480 fTrackCorrMode = kTrackCorrMMB;
481 }
482 else if (!opt.CompareTo("FIXED")) {
483 fTrackCorrMode = kTrackCorrFixed;
484 }
485 else {
486 cerr << "E-AliEMCALTracker::SetTrackCorrectionMode '" << option << "': Unrecognized option" << endl;
487 }
488}
489
490//
491//------------------------------------------------------------------------------
492//
493Double_t AliEMCALTracker::AngleDiff(Double_t angle1, Double_t angle2)
494{
495 //
496 // [PRIVATE]
497 // Given two angles in radiants, it converts them in the range 0-2pi
498 // then computes their true difference, i.e. if the difference a1-a2
499 // results to be larger than 180 degrees, it returns 360 - diff.
500 //
501
502 if (angle1 < 0.0) angle1 += TMath::TwoPi();
503 if (angle1 > TMath::TwoPi()) angle1 -= TMath::TwoPi();
504 if (angle2 < 0.0) angle2 += TMath::TwoPi();
505 if (angle2 > TMath::TwoPi()) angle2 -= TMath::TwoPi();
506
507 Double_t diff = TMath::Abs(angle1 - angle2);
508 if (diff > TMath::Pi()) diff = TMath::TwoPi() - diff;
509
510 if (angle2 > angle1) diff = -diff;
511
512 return diff;
513}
514//
515//------------------------------------------------------------------------------
516//
517Double_t AliEMCALTracker::CheckPair
518(AliEMCALTrack *track, AliEMCALMatchCluster *cl)
519{
520 //
521 // Given a track and a cluster,
522 // propagates the first to the radius of the second.
523 // Then, checks the propagation point against all cuts.
524 // If at least a cut is not passed, a valuer equal to
525 // twice the maximum allowed distance is passed (so the value returned
526 // will not be taken into account when creating matches)
527 //
528
529 // TEMP
530 Bool_t isTrue = kFALSE;
531// if (tr->GetSeedLabel() == cl->Label()) {
532// isTrue = kTRUE;
fe17d4cb 533// }
534
535 // copy track into temporary variable
536 AliEMCALTrack *tr = new AliEMCALTrack(*track);
537
538 Double_t distance = 2.0 * fMaxDist;
539
540 // check against cut on difference 'alpha - phi'
541 Double_t phi = TMath::ATan2(cl->Y(), cl->X());
542 phi = AngleDiff(phi, tr->GetAlpha());
543 if (phi < fCutAlphaMin || phi > fCutAlphaMax) return distance;
544
545 // try to propagate to cluster radius
546 // (return the 'distance' value if it fails)
547 Double_t pos[3], &x = pos[0], &y = pos[1], &z = pos[2];
548 Double_t x0, rho;
549 tr->GetXYZ(pos);
550 Double_t rt = TMath::Sqrt(x*x + y*y);
551 Double_t rc = TMath::Sqrt(cl->X()*cl->X() + cl->Y()*cl->Y());
552
553 if (fTrackCorrMode == kTrackCorrMMB) {
554 Double_t pos1[3], pos2[3], param[6];
555 pos1[0] = x;
556 pos1[1] = y;
557 pos1[2] = z;
558 pos2[0] = cl->X();
559 pos2[1] = cl->Y();
560 pos2[2] = cl->Z();
561 AliKalmanTrack::MeanMaterialBudget(pos1, pos2, param);
562 rho = param[0];
563 x0 = param[1];
564 }
565 else if (fTrackCorrMode == kTrackCorrFixed) {
566 rho = fRho;
567 x0 = fX0;
568 }
569 else {
570 rho = 0.0;
571 x0 = 0.0;
572 }
573 if (fNPropSteps) {
574 Int_t i;
575 Double_t r;
576 cout.setf(ios::fixed);
577 cout.precision(5);
578 if (isTrue) cout << "Init : " << rt << ' ' << x << ' ' << y << ' ' << z << endl;
579 for (i = 0; i < fNPropSteps; i++) {
580 r = rt + (rc - rt) * ((Double_t)(i+1)/(Double_t)fNPropSteps);
581 if (!tr->PropagateTo(r, x0, rho)) return distance;
582 tr->GetXYZ(pos);
583 if (isTrue) cout << "Step : " << r << ' ' << x << ' ' << y << ' ' << z << endl;
584 }
585 if (isTrue) cout << "Clstr: " << rc << ' ' << cl->X() << ' ' << cl->Y() << ' ' << cl->Z() << endl;
586 }
587 else {
588 // when no steps are used, no correction makes sense
c61f0e70 589 //if (!tr->PropagateTo(rc, 0.0, 0.0)) return distance;
590 if (!tr->PropagateToGlobal(cl->X(), cl->Y(), cl->Z(), 0.0, 0.0)) return distance;
591 /*
592 Bool_t propOK = kFALSE;
593 cout << "START" << endl;
594 Double_t dist, rCHK, bestDist = 10000000.0;
595 for (Double_t rTMP = rc; rTMP> rc*0.95; rTMP -= 0.1) {
596 if (!tr->PropagateTo(rTMP)) continue;
597 propOK = kTRUE;
598 tr->GetXYZ(pos);
599 rCHK = TMath::Sqrt(x*x + y*y);
600 dist = TMath::Abs(rCHK - rc);
601 cout << rCHK << " vs. " << rc << endl;
602
603 if (TMath::Abs(rCHK - rc) < 0.01) break;
604 }
605 cout << "STOP" << endl;
606 if (!propOK) return distance;
607 */
fe17d4cb 608 }
609
610 // get global propagation of track at end of propagation
611 tr->GetXYZ(pos);
612
613 // check angle cut
614 TVector3 vc(cl->X(), cl->Y(), cl->Z());
615 TVector3 vt(x, y, z);
616 Double_t angle = TMath::Abs(vc.Angle(vt)) * TMath::RadToDeg();
c61f0e70 617 // check: where is the track?
618 Double_t r, phiT, phiC;
619 r = TMath::Sqrt(pos[0]*pos[0] + pos[1]*pos[1]);
620 phiT = TMath::ATan2(pos[1], pos[0]) * TMath::RadToDeg();
621 phiC = vc.Phi() * TMath::RadToDeg();
622 //cout << "Propagated R, phiT, phiC = " << r << ' ' << phiT << ' ' << phiC << endl;
623
fe17d4cb 624 if (angle > fCutAngle) {
625 //cout << "angle" << endl;
626 return distance;
627 }
628
629 // compute differences wr to each coordinate
630 x -= cl->X();
c61f0e70 631 if (TMath::Abs(x) > fCutX) {
fe17d4cb 632 //cout << "cut X" << endl;
633 return distance;
634 }
635 y -= cl->Y();
c61f0e70 636 if (TMath::Abs(y) > fCutY) {
fe17d4cb 637 //cout << "cut Y" << endl;
638 return distance;
639 }
640 z -= cl->Z();
c61f0e70 641 if (TMath::Abs(z) > fCutZ) {
fe17d4cb 642 //cout << "cut Z" << endl;
643 return distance;
644 }
645
646 // compute true distance
647 distance = TMath::Sqrt(x*x + y*y + z*z);
648 //Double_t temp = CheckPairV2(tr, cl);
649 //if (temp < distance) return temp; else
650
651 // delete temporary object
652 delete tr;
653
654 return distance;
655}
656//
657//------------------------------------------------------------------------------
658//
659Double_t AliEMCALTracker::CheckPairV2
660(AliEMCALTrack *tr, AliEMCALMatchCluster *cl)
661{
662 //
663 // Given a track and a cluster,
664 // propagates the first to the radius of the second.
665 // Then, checks the propagation point against all cuts.
666 // If at least a cut is not passed, a valuer equal to
667 // twice the maximum allowed distance is passed (so the value returned
668 // will not be taken into account when creating matches)
669 //
670
671 // TEMP
672// Bool_t isTrue = kFALSE;
673// if (tr->GetSeedLabel() == cl->Label()) {
674// isTrue = kTRUE;
675// cout << "TRUE MATCH!!!" << endl;
676// }
677
678 Double_t distance = 2.0 * fMaxDist;
679
680 Double_t x0, rho;
681 if (fTrackCorrMode == kTrackCorrMMB) {
682 Double_t pos1[3], pos2[3], param[6];
683 tr->GetXYZ(pos1);
684// pos1[0] = x;
685// pos1[1] = y;
686// pos1[2] = z;
687 pos2[0] = cl->X();
688 pos2[1] = cl->Y();
689 pos2[2] = cl->Z();
690 AliKalmanTrack::MeanMaterialBudget(pos1, pos2, param);
691 rho = param[0];
692 x0 = param[1];
693 }
694 else if (fTrackCorrMode == kTrackCorrFixed) {
695 rho = fRho;
696 x0 = fX0;
697 }
698 else {
699 rho = 0.0;
700 x0 = 0.0;
701 }
702
703 // check against cut on difference 'alpha - phi'
704 Double_t phi = TMath::ATan2(cl->Y(), cl->X());
705 phi = AngleDiff(phi, tr->GetAlpha());
706 if (phi < fCutAlphaMin || phi > fCutAlphaMax) return distance;
707
708 // get cluster position and put them into a vector
709 TVector3 vc(cl->X(), cl->Y(), cl->Z());
710 // rotate the vector in order to put all clusters on a plane intersecting
711 // vertically the X axis; the angle depends on the sector
712 Double_t clusterRot, clusterPhi = vc.Phi() * TMath::RadToDeg();
713 if (clusterPhi < 0.0) clusterPhi += 360.0;
714 if (clusterPhi < 100.0) {
715 clusterRot = -90.0;
716 }
717 else if (clusterPhi < 120.0) {
718 clusterRot = -110.0;
719 }
720 else if (clusterPhi < 140.0) {
721 clusterRot = -130.0;
722 }
723 else if (clusterPhi < 160.0) {
724 clusterRot = -150.0;
725 }
726 else if (clusterPhi < 180.0) {
727 clusterRot = -170.0;
728 }
729 else {
730 clusterRot = -190.0;
731 }
732 vc.RotateZ(clusterRot * TMath::DegToRad());
733 // generate a track from the ESD track selected
734 AliEMCALTrack *track = new AliEMCALTrack(*tr);
735 // compute the 'phi' coordinate of the intersection point to
736 // the EMCAL surface
737 Double_t x = vc.X();
738 Double_t y;
739 track->GetYAt(vc.X(), track->GetBz(), y);
740 Double_t tmp = x*TMath::Cos(track->GetAlpha()) - y*TMath::Sin(track->GetAlpha());
741 y = x*TMath::Sin(track->GetAlpha()) + y*TMath::Cos(track->GetAlpha());
742 x = tmp;
743 Double_t trackPhi = TMath::ATan2(y, x) * TMath::RadToDeg();
744 // compute phi difference
745 Double_t dphi = trackPhi - clusterPhi;
746 if (TMath::Abs(dphi) > 180.0) {
747 dphi = 360.0 - TMath::Abs(dphi);
748 if (clusterPhi > trackPhi) dphi = -dphi;
749 }
750 // propagate track to the X position of rotated cluster
751 // and get the vector of X, Y, Z in the local ref. frame of the track
752 track->PropagateTo(vc.X(), x0, rho);
753 TVector3 vt(track->GetX(), track->GetY(), track->GetZ());
754 vt.RotateZ((clusterPhi - trackPhi) * TMath::DegToRad());
755 TVector3 vdiff = vt-vc;
756
757 // compute differences wr to each coordinate
758 if (vdiff.X() > fCutX) return distance;
759 if (vdiff.Y() > fCutY) return distance;
760 if (vdiff.Z() > fCutZ) return distance;
761
762 // compute true distance
763 distance = vdiff.Mag();
764 return distance;
765}
766//
767//------------------------------------------------------------------------------
768//
c61f0e70 769Double_t AliEMCALTracker::CheckPairV3
770(AliEMCALTrack *track, AliEMCALMatchCluster *cl)
771{
772 //
773 // Given a track and a cluster,
774 // propagates the first to the radius of the second.
775 // Then, checks the propagation point against all cuts.
776 // If at least a cut is not passed, a valuer equal to
777 // twice the maximum allowed distance is passed (so the value returned
778 // will not be taken into account when creating matches)
779 //
780
781 AliEMCALTrack tr(*track);
782
783 Int_t sector;
784 Double_t distance = 2.0 * fMaxDist;
785 Double_t dx, dy, dz;
786 Double_t phi, alpha, slope, tgtXnum, tgtXden, sectorWidth = 20.0 * TMath::DegToRad();
787 Double_t xcurr, xprop, param[6] = {0., 0., 0., 0., 0., 0.}, d, x0, rho, bz;
788 Double_t x[3], x1[3], x2[3];
789
790 // get initial track position
791 xcurr = tr.GetX();
792
793 // evaluate the EMCAL sector number
794 phi = cl->Phi();
795 if (phi < 0.0) phi += TMath::TwoPi();
796 sector = (Int_t)(phi / sectorWidth);
797 alpha = ((Double_t)sector + 0.5) * sectorWidth;
798 // evaluate the corresponding X for track propagation
799 slope = TMath::Tan(alpha - 0.5*TMath::Pi());
800 tgtXnum = cl->Y() - slope * cl->X();
801 tgtXden = TMath::Sqrt(1.0 + slope*slope);
802 xprop = TMath::Abs(tgtXnum / tgtXden);
803
804 // propagate by small steps
805 tr.GetXYZ(x1);
806 bz = tr.GetBz();
807 if (!tr.GetXYZAt(xprop, bz, x2)) return distance;
808 //AliKalmanTrack::MeanMaterialBudget(x1, x2, param);
809 d = param[4];
810 rho = param[0];
811 x0 = param[1];
812 if (!tr.PropagateTo(xprop, d*rho/x0, x0)) return distance;
813 //if (!tr.PropagateTo(xprop, 0.0, 0.0)) return distance;
814
815 // get propagated position at the end
816 tr.GetXYZ(x);
817 dx = TMath::Abs(x[0] - cl->X());
818 dy = TMath::Abs(x[1] - cl->Y());
819 dz = TMath::Abs(x[2] - cl->Z());
820 if (dx > fCutX || dy > fCutY || dz > fCutZ) return distance;
821
822 distance = TMath::Sqrt(dx*dx + dy*dy + dz*dz);
823
824 return distance;
825}
826//
827//------------------------------------------------------------------------------
828//
829Bool_t AliEMCALTracker::PropagateToEMCAL(AliEMCALTrack *tr)
830{
831 //
832 // Propagates the track to the proximity of the EMCAL surface
833 //
834
835 Double_t xcurr, xtemp, xprop = 438.0, step = 10.0, param[6], d, x0, rho, bz;
836 Double_t x1[3], x2[3];
837
838 // get initial track position
839 xcurr = tr->GetX();
840
841 // propagate by small steps
842 for (xtemp = xcurr + step; xtemp < xprop; xtemp += step) {
843 // to compute material budget, take current position and
844 // propagated hypothesis without energy loss
845 tr->GetXYZ(x1);
846 bz = tr->GetBz();
847 if (!tr->GetXYZAt(xtemp, bz, x2)) return kFALSE;
848 AliKalmanTrack::MeanMaterialBudget(x1, x2, param);
849 d = param[4];
850 rho = param[0];
851 x0 = param[1];
852 if (!tr->PropagateTo(xtemp, d*rho/x0, x0)) return kFALSE;
853 }
854
855 return kTRUE;
856}
857//
858//------------------------------------------------------------------------------
859//
fe17d4cb 860Int_t AliEMCALTracker::CreateMatches()
861{
862 //
863 // Creation of matches between tracks and clusters.
864 // For each ESD track collected by ReadESD(), an AliEMCALTrack is made.
865 // If it finds a cluster close enough to its propagation to EMCAL,
866 // which passes all cuts, its index is stored.
867 // If many clusters are found which satisfy the criteria described above,
868 // only the closest one is stored.
869 // At this level, it is possible that two tracks share the same cluster.
870 //
871
872 // if matches collection is already present, it is deleted
873 if (fMatches) {
874 fMatches->Delete();
875 delete fMatches;
876 }
877 fMatches = new TList;
878
879 // initialize counters and indexes
880 Int_t count = 0;
881 Int_t ic, nClusters = (Int_t)fClusters->GetEntries();
882 Int_t it, nTracks = fTracks->GetEntries();
883
884 // external loop on clusters, internal loop on tracks
885 Double_t dist;
886 for (ic = 0; ic < nClusters; ic++) {
fe17d4cb 887 AliEMCALMatchCluster *cluster = (AliEMCALMatchCluster*)fClusters->At(ic);
888 for (it = 0; it < nTracks; it++) {
889 AliEMCALTrack *track = (AliEMCALTrack*)fTracks->At(it);
890 dist = CheckPair(track, cluster);
c61f0e70 891 //cout << dist << endl;
fe17d4cb 892 if (dist <= fMaxDist) {
893 AliEMCALMatch *candidate = new AliEMCALMatch;
894 candidate->SetIndexT(it);
895 candidate->SetIndexC(ic);
896 candidate->SetDistance(dist);
c61f0e70 897 candidate->SetPt(track->GetPt());
fe17d4cb 898 fMatches->Add(candidate);
899 count++;
900 }
901 }
902 }
c61f0e70 903
fe17d4cb 904 /*
905 // loop on clusters and tracks
906 Int_t icBest;
907 Double_t dist, distBest;
908 for (it = 0; it < nTracks; it++) {
909 AliEMCALTrack *track = (AliEMCALTrack*)fTracks->At(it);
910 if (!track) continue;
911 icBest = -1;
912 distBest = fMaxDist;
913 for (ic = 0; ic < nClusters; ic++) {
914 AliEMCALMatchCluster *cluster = (AliEMCALMatchCluster*)fClusters->At(ic);
915 if (!cluster) continue;
916 dist = CheckPair(track, cluster);
917 if (dist < distBest) {
918 distBest = dist;
919 icBest = ic;
920 }
921 }
922 if (icBest >= 0) {
923 track->SetMatchedClusterIndex(icBest);
924 track->SetMatchedClusterDist(distBest);
925 count++;
926 }
927 else {
928 track->SetMatchedClusterIndex(-1);
929 }
930 }
931 */
932
933 return count;
934}
935//
936//------------------------------------------------------------------------------
937//
938Int_t AliEMCALTracker::SolveCompetitions()
939{
940 //
941 // Match selector.
942 // The match list is sorted from the best to the worst match, w.r. to the
943 // distance between track prolongation and cluster position.
944 // Based on this criterion, starting from the first (best) match, a flag
945 // is set to both the involved track and cluster, and all matches containing
946 // an already used track or cluster are removed, leaving only the best match
947 // for each cluster.
948 //
949
950 // sort matches with respect to track-cluster distance
951 fMatches->Sort(kSortAscending);
952
953 // keep track of eliminated matches
954 Int_t count = 0;
955
956 // initialize flags to check repetitions
957 Int_t ic, nClusters = (Int_t)fClusters->GetEntries();
958 Int_t it, nTracks = fTracks->GetEntries();
959 Bool_t *usedC = new Bool_t[nClusters];
960 Bool_t *usedT = new Bool_t[nTracks];
961 for (ic = 0; ic < nClusters; ic++) usedC[ic] = kFALSE;
962 for (it = 0; it < nTracks; it++) usedT[it] = kFALSE;
963
964 // loop on matches
965 TListIter iter(fMatches);
966 AliEMCALMatch *match = 0;
967 while ( (match = (AliEMCALMatch*)iter.Next()) ) {
968 ic = match->GetIndexC();
969 it = match->GetIndexT();
970 if (!usedT[it] && !usedC[ic]) {
971 usedT[it] = kTRUE;
972 usedC[ic] = kTRUE;
973 match->CanBeSaved() = kTRUE;
974 }
975 else {
976 count++;
977 }
978 }
979
980 /*
981 Int_t it1, it2, nTracks = (Int_t)fTracks->GetEntries();
982 AliEMCALTrack *track1 = 0, *track2 = 0;
983 for (it1 = 0; it1 < nTracks; it1++) {
984 track1 = (AliEMCALTrack*)fTracks->At(it1);
985 if (!track1) continue;
986 if (track1->GetMatchedClusterIndex() < 0) continue;
987 for (it2 = it1+1; it2 < nTracks; it2++) {
988 track2 = (AliEMCALTrack*)fTracks->At(it2);
989 if (!track2) continue;
990 if (track2->GetMatchedClusterIndex() < 0) continue;
991 if (track1->GetMatchedClusterIndex() != track2->GetMatchedClusterIndex()) continue;
992 count++;
993 if (track1->GetMatchedClusterDist() < track2->GetMatchedClusterDist()) {
994 track2->SetMatchedClusterIndex(-1);
995 }
996 else if (track2->GetMatchedClusterDist() < track1->GetMatchedClusterDist()) {
997 track1->SetMatchedClusterIndex(-1);
998 }
999 }
1000 }
1001 */
1002
1003 return count;
1004}
1005//
1006//------------------------------------------------------------------------------
1007//
1008void AliEMCALTracker::UnloadClusters()
1009{
1010 //
1011 // Free memory from clusters
1012 //
1013
1014 Clear("CLUSTERS");
1015}
1016//
1017//------------------------------------------------------------------------------
1018//
1019AliEMCALTracker::AliEMCALMatchCluster::AliEMCALMatchCluster(Int_t index, AliEMCALRecPoint *recPoint)
1020 : fIndex(index),
1021 fLabel(recPoint->GetPrimaryIndex()),
1022 fX(0.),
1023 fY(0.),
1024 fZ(0.)
1025{
1026 //
1027 // Translates an AliEMCALRecPoint object into the internal format.
1028 // Index of passed cluster in its native array must be specified.
1029 //
1030 TVector3 clpos;
1031 recPoint->GetGlobalPosition(clpos);
1032
1033 fX = clpos.X();
1034 fY = clpos.Y();
1035 fZ = clpos.Z();
1036}
1037//
1038//------------------------------------------------------------------------------
1039//
1040AliEMCALTracker::AliEMCALMatchCluster::AliEMCALMatchCluster(Int_t index, AliESDCaloCluster *caloCluster)
1041 : fIndex(index),
1042 fLabel(caloCluster->GetPrimaryIndex()),
1043 fX(0.),
1044 fY(0.),
1045 fZ(0.)
1046{
1047 //
1048 // Translates an AliESDCaloCluster object into the internal format.
1049 // Index of passed cluster in its native array must be specified.
1050 //
1051 Float_t clpos[3];
1052 caloCluster->GetGlobalPosition(clpos);
1053
1054 fX = (Double_t)clpos[0];
1055 fY = (Double_t)clpos[1];
1056 fZ = (Double_t)clpos[2];
1057}
1058//
1059//------------------------------------------------------------------------------
1060//
1061Int_t AliEMCALTracker::AliEMCALMatch::Compare(const TObject *obj) const
1062{
1063 //
1064 // Tracks compared wrt their distance from matched point
1065 //
1066
1067 AliEMCALTracker::AliEMCALMatch *that = (AliEMCALTracker::AliEMCALMatch*)obj;
1068
c61f0e70 1069 Double_t thisDist = fPt;//fDistance;
1070 Double_t thatDist = that->fPt;//that->GetDistance();
fe17d4cb 1071
1072 if (thisDist > thatDist) return 1;
1073 else if (thisDist < thatDist) return -1;
1074 return 0;
1075}
1076
1077AliEMCALTracker::AliEMCALMatch::AliEMCALMatch()
1078 : TObject(),
1079 fCanBeSaved(kFALSE),
1080 fIndexC(0),
1081 fIndexT(0),
c61f0e70 1082 fDistance(0.),
1083 fPt(0.)
fe17d4cb 1084{
1085 //default constructor
1086
1087}
1088
1089AliEMCALTracker::AliEMCALMatch::AliEMCALMatch(const AliEMCALMatch& copy)
1090 : TObject(),
1091 fCanBeSaved(copy.fCanBeSaved),
1092 fIndexC(copy.fIndexC),
1093 fIndexT(copy.fIndexT),
c61f0e70 1094 fDistance(copy.fDistance),
1095 fPt(copy.fPt)
fe17d4cb 1096{
1097 //copy ctor
1098}