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