]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EMCAL/AliEMCALTracker.cxx
disable the runtype setting (done from GRP inside aliroot)
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTracker.cxx
index e12dd5dd13bb0a3c50629f42811375cb6371601f..9b8d594a3d052908990d3efdb2cd3f3d6804e60f 100644 (file)
 
 #include <TFile.h>
 #include <TTree.h>
-#include <TMath.h>
 #include <TList.h>
 #include <TString.h>
 #include <TVector3.h>
 #include <TClonesArray.h>
+#include <TGeoMatrix.h>
 
 #include "AliLog.h"
-#include "AliESD.h"
+#include "AliESDEvent.h"
 #include "AliESDtrack.h"
-#include "AliKalmanTrack.h"
+#include "AliESDCaloCluster.h"
 #include "AliEMCALRecPoint.h"
 #include "AliRunLoader.h"
 #include "AliEMCALTrack.h"
 #include "AliEMCALLoader.h"
+#include "AliEMCALGeometry.h"
+#include "AliEMCALReconstructor.h"
+#include "AliEMCALRecParam.h"
+#include "AliCDBEntry.h"
+#include "AliCDBManager.h"
+
 #include "AliEMCALTracker.h"
 
 ClassImp(AliEMCALTracker)
+
+AliEMCALRecParam* AliEMCALTracker::fgkRecParam = 0;  // EMCAL rec. parameters
+
 //
 //------------------------------------------------------------------------------
 //
@@ -64,12 +73,13 @@ AliEMCALTracker::AliEMCALTracker()
     fCutAlphaMin(-200.0),
     fCutAlphaMax(200.0),
     fCutAngle(100.0),
-    fMaxDist(100.0),
+    fMaxDist(10.0),
     fRho(1.0),
     fX0(1.0),
     fTracks(0),
     fClusters(0),
-    fMatches(0)
+    fMatches(0),
+    fGeom(0)
 {
        //
        // Default constructor.
@@ -77,6 +87,8 @@ AliEMCALTracker::AliEMCALTracker()
        // and all collections to NULL.
        // Output file name is set to a default value.
        //
+       
+       InitParameters();
 }
 //
 //------------------------------------------------------------------------------
@@ -96,7 +108,8 @@ AliEMCALTracker::AliEMCALTracker(const AliEMCALTracker& copy)
     fX0(copy.fX0),
     fTracks((TObjArray*)copy.fTracks->Clone()),
     fClusters((TObjArray*)copy.fClusters->Clone()),
-    fMatches((TList*)copy.fMatches->Clone())
+    fMatches((TList*)copy.fMatches->Clone()),
+    fGeom(copy.fGeom)
 {
        //
        // Copy constructor
@@ -125,16 +138,92 @@ AliEMCALTracker& AliEMCALTracker::operator=(const AliEMCALTracker& copy)
        fClusters = (TObjArray*)copy.fClusters->Clone();
        fMatches = (TList*)copy.fMatches->Clone();
        
+       fGeom = copy.fGeom;
+       
        return (*this);
 }
 //
 //------------------------------------------------------------------------------
 //
+void AliEMCALTracker::InitParameters()
+{
+       //
+       // Retrieve initialization parameters
+       //
+       
+  // Check if the instance of AliEMCALRecParam exists, 
+  // if not, get it from OCDB if available, otherwise create a default one
+  
+  if (!fgkRecParam  && (AliCDBManager::Instance()->IsDefaultStorageSet())) {
+    AliCDBEntry *entry = (AliCDBEntry*) 
+      AliCDBManager::Instance()->Get("EMCAL/Config/RecParam");
+    if (entry) fgkRecParam =  (AliEMCALRecParam*) entry->GetObject();
+  }
+  
+  if(!fgkRecParam){
+    AliWarning("The Track Matching parameters for EMCAL nonitialized - Used default one");
+    fgkRecParam = new AliEMCALRecParam;
+  }
+  
+  fCutX =  fgkRecParam->GetTrkCutX();
+  fCutY =  fgkRecParam->GetTrkCutY();
+  fCutZ =  fgkRecParam->GetTrkCutZ();
+  fMaxDist =  fgkRecParam->GetTrkCutR();
+  fCutAngle =  fgkRecParam->GetTrkCutAngle();
+  fCutAlphaMin =  fgkRecParam->GetTrkCutAlphaMin();
+  fCutAlphaMax =  fgkRecParam->GetTrkCutAlphaMax();
+  
+}
+//
+//------------------------------------------------------------------------------
+//
+TTree* AliEMCALTracker::SearchTrueMatches()
+{
+  //Search through the list of
+  //track match candidates and clusters
+  //and look for true matches
+  //
+  //
+       if (!fClusters) return 0;
+       if (fClusters->IsEmpty()) return 0;
+       if (!fTracks) return 0;
+       if (fTracks->IsEmpty()) return 0;
+       
+       TTree *outTree = new TTree("tree", "True matches from event");
+       Int_t indexT, indexC, label;
+       outTree->Branch("indexC", &indexC, "indexC/I");
+       outTree->Branch("indexT", &indexT, "indexT/I");
+       outTree->Branch("label",  &label , "label/I");
+       
+       Double_t dist;
+       Int_t ic, nClusters = (Int_t)fClusters->GetEntries();
+       Int_t it, nTracks = fTracks->GetEntries();
+       
+       for (ic = 0; ic < nClusters; ic++) {
+               AliEMCALMatchCluster *cluster = (AliEMCALMatchCluster*)fClusters->At(ic);
+               label = cluster->Label();
+               indexC = cluster->Index();
+               for (it = 0; it < nTracks; it++) {
+                       AliEMCALTrack *track = (AliEMCALTrack*)fTracks->At(it);
+                       if (TMath::Abs(track->GetSeedLabel()) != label) continue;
+                       dist = CheckPair(track, cluster);
+                       if (dist <= fMaxDist) {
+                               indexT = track->GetSeedIndex();
+                               outTree->Fill();
+                       }
+               }
+       }
+       
+       return outTree;
+}
+//
+//------------------------------------------------------------------------------
+//
 void AliEMCALTracker::Clear(Option_t* option)
 {
        //
        // Clearing method
-       // Clears all specified arrays and the containers themselves.
+        // Deletes all objects in arrays and the arrays themselves
        //
 
        TString opt(option);
@@ -148,19 +237,19 @@ void AliEMCALTracker::Clear(Option_t* option)
        }
        
        if (fTracks != 0x0 && clearTracks) {
-               if (!fTracks->IsEmpty()) fTracks->Delete();
-               delete fTracks;
-               fTracks = 0;
+          fTracks->Delete();
+          delete fTracks;
+           fTracks = 0;
        }
        if (fClusters != 0x0 && clearClusters) {
-               if (!fClusters->IsEmpty()) fClusters->Delete();
-               delete fClusters;
-               fClusters = 0;
+          fClusters->Delete();
+          delete fClusters;
+          fClusters = 0;
        }
        if (fMatches != 0x0 && clearMatches) {
-               if (!fMatches->IsEmpty()) fMatches->Delete();
-               delete fMatches;
-               fMatches = 0;
+          fMatches->Delete();
+          delete fMatches;
+           fMatches = 0;
        }
 }
 //
@@ -178,37 +267,38 @@ Int_t AliEMCALTracker::LoadClusters(TTree *cTree)
 
        TBranch *branch = cTree->GetBranch("EMCALECARP");
        if (!branch) {
-               AliError("can't get the branch with the EMCAL clusters");
+               AliError("Can't get the branch with the EMCAL clusters");
                return 1;
        }
        
-       TClonesArray dummy("AliEMCALRecPoint", 10000);
-       TClonesArray *clusters = &dummy;
+       TClonesArray *clusters = new TClonesArray("AliEMCALRecPoint", 1000);
        branch->SetAddress(&clusters);
-       Int_t nClusters = (Int_t)clusters->GetEntries();
        
        cTree->GetEvent(0);
+       Int_t nClusters = (Int_t)clusters->GetEntries();
        fClusters = new TObjArray(0);
        for (Int_t i = 0; i < nClusters; i++) {
                AliEMCALRecPoint *cluster = (AliEMCALRecPoint*)clusters->At(i);
                if (!cluster) continue;
-               if (cluster->GetClusterType() != AliESDCaloCluster::kClusterv1) continue;
+               if (cluster->GetClusterType() != AliESDCaloCluster::kEMCALClusterv1) continue;
                AliEMCALMatchCluster *matchCluster = new AliEMCALMatchCluster(i, cluster);
                fClusters->AddLast(matchCluster);
        }
-       if (fClusters->IsEmpty()) {
-               AliError("No clusters collected");
-               return 1;
-       }
-       
-       AliInfo(Form("Collected %d clusters", fClusters->GetEntries()));
+
+       branch->SetAddress(0);
+        clusters->Delete();
+        delete clusters;
+        if (fClusters->IsEmpty())
+           AliWarning("No clusters collected");
+
+       AliInfo(Form("Collected %d clusters (RecPoints)", fClusters->GetEntries()));
 
        return 0;
 }
 //
 //------------------------------------------------------------------------------
 //
-Int_t AliEMCALTracker::LoadClusters(AliESD *esd) 
+Int_t AliEMCALTracker::LoadClusters(AliESDEvent *esd) 
 {
        //
        // Load EMCAL clusters in the form of AliESDCaloClusters,
@@ -228,23 +318,21 @@ Int_t AliEMCALTracker::LoadClusters(AliESD *esd)
        for (i = start; i < end; i++) {
                AliESDCaloCluster *cluster = esd->GetCaloCluster(i);
                if (!cluster) continue;
-               if (cluster->GetClusterType() != AliESDCaloCluster::kClusterv1) continue;
+               if (cluster->GetClusterType() != AliESDCaloCluster::kEMCALClusterv1) continue;
                AliEMCALMatchCluster *matchCluster = new AliEMCALMatchCluster(i, cluster);
                fClusters->AddLast(matchCluster);
        }
-       if (fClusters->IsEmpty()) {
-               AliError("No clusters collected");
-               return 1;
-       }
+        if (fClusters->IsEmpty())
+           AliWarning("No clusters collected");
        
-       AliInfo(Form("Collected %d clusters", fClusters->GetEntries()));
+       AliInfo(Form("Collected %d clusters from ESD", fClusters->GetEntries()));
 
        return 0;
 }
 //
 //------------------------------------------------------------------------------
 //
-Int_t AliEMCALTracker::LoadTracks(AliESD *esd)
+Int_t AliEMCALTracker::LoadTracks(AliESDEvent *esd)
 {
        //
        // Load ESD tracks.
@@ -261,28 +349,32 @@ Int_t AliEMCALTracker::LoadTracks(AliESD *esd)
        for (i = 0; i < nTracks; i++) {
                AliESDtrack *esdTrack = esd->GetTrack(i);
                // set by default the value corresponding to "no match"
-               esdTrack->SetEMCALcluster(-99999);
+               esdTrack->SetEMCALcluster(kUnmatched);
 //             if (esdTrack->GetLabel() < 0) continue;
 //             if (!(esdTrack->GetStatus() & AliESDtrack::kTOFout)) continue;
                isKink = kFALSE;
                for (j = 0; j < 3; j++) {
-                       if (esdTrack->GetKinkIndex(j) > 0) isKink = kTRUE;
+                       if (esdTrack->GetKinkIndex(j) != 0) isKink = kTRUE;
                }
                if (isKink) continue;
                AliEMCALTrack *track = new AliEMCALTrack(*esdTrack);
+               track->SetMass(0.13957018);
                // check alpha and reject the tracks which fall outside EMCAL acceptance
                alpha = track->GetAlpha() * TMath::RadToDeg();
                if (alpha >  -155.0 && alpha < 67.0) {
                        delete track;
                        continue;
                }
+//             if (!PropagateToEMCAL(track)) {
+//                     delete track;
+//                     continue;
+//             }
                track->SetSeedIndex(i);
                track->SetSeedLabel(esdTrack->GetLabel());
                fTracks->AddLast(track);
        }
        if (fTracks->IsEmpty()) {
-               AliError("No tracks collected");
-               return 1;
+               AliWarning("No tracks collected");
        }
        
        AliInfo(Form("Collected %d tracks", fTracks->GetEntries()));
@@ -292,7 +384,7 @@ Int_t AliEMCALTracker::LoadTracks(AliESD *esd)
 //
 //------------------------------------------------------------------------------
 //
-Int_t AliEMCALTracker::PropagateBack(AliESD* esd)
+Int_t AliEMCALTracker::PropagateBack(AliESDEvent* esd)
 {
        //
        // Main operation method.
@@ -300,7 +392,10 @@ Int_t AliEMCALTracker::PropagateBack(AliESD* esd)
        // After executing match finding, stores in the same ESD object all infos
        // and releases the object for further reconstruction steps.
        //
-       
+        //
+        // Note: should always return 0=OK, because otherwise all tracking
+        // is aborted for this event
+
        if (!esd) {
                AliError("NULL ESD passed");
                return 1;
@@ -309,17 +404,17 @@ Int_t AliEMCALTracker::PropagateBack(AliESD* esd)
        // step 1: 
        // if cluster array is empty, cluster are collected
        // from the passed ESD, and work is done with ESDCaloClusters
-       Int_t okLoadClusters;
+       Int_t okLoadClusters, nClusters;
        if (!fClusters || (fClusters && fClusters->IsEmpty())) {
-               AliInfo("Cluster array is empty. Loading clusters...");
                okLoadClusters = LoadClusters(esd);
-               if (okLoadClusters) return 2;
        }
+       nClusters = fClusters->GetEntries();
        
        // step 2:
        // collect ESD tracks
-       Int_t okLoadTracks = LoadTracks(esd);
+       Int_t okLoadTracks = LoadTracks(esd), nTracks;
        if (okLoadTracks) return 3;
+       nTracks = fTracks->GetEntries();
        
        // step 3:
        // each track is propagated to the "R" position of each cluster.
@@ -327,11 +422,11 @@ Int_t AliEMCALTracker::PropagateBack(AliESD* esd)
        // IF no clusters lie within the maximum allowed distance, no matches are assigned.
        Int_t nMatches = CreateMatches();
        if (!nMatches) {
-               AliInfo("No good matches found.");
-               return 4;
+               AliInfo(Form("#clusters = %d -- #tracks = %d --> No good matches found.", nClusters, nTracks));
+               return 0;
        }
        else {
-               AliInfo(Form("Found %d matches", nMatches));
+               AliInfo(Form("#clusters = %d -- #tracks = %d --> Found %d matches.", nClusters, nTracks, nMatches));
        }
        
        // step 4:
@@ -345,7 +440,7 @@ Int_t AliEMCALTracker::PropagateBack(AliESD* esd)
        
        // step 5:
        // save obtained information setting the 'fEMCALindex' field of AliESDtrack object
-       Int_t nSaved = 0, trackID;
+       Int_t nSaved = 0, trackID, nGood = 0, nFake = 0;
        TListIter iter(fMatches);
        AliEMCALMatch *match = 0;
        while ( (match = (AliEMCALMatch*)iter.Next()) ) {
@@ -355,11 +450,13 @@ Int_t AliEMCALTracker::PropagateBack(AliESD* esd)
                trackID = track->GetSeedIndex();
                AliESDtrack *esdTrack = esd->GetTrack(trackID);
                if (!esdTrack) continue;
-               if (esdTrack->GetLabel() == cluster->Label()) {
+               if (TMath::Abs(esdTrack->GetLabel()) == cluster->Label()) {
                        esdTrack->SetEMCALcluster(cluster->Index());
+                       nGood++;
                }
                else {
                        esdTrack->SetEMCALcluster(-cluster->Index());
+                       nFake++;
                }
                nSaved++;
        }
@@ -372,7 +469,7 @@ Int_t AliEMCALTracker::PropagateBack(AliESD* esd)
                AliESDtrack *esdTrack = esd->GetTrack(trackID);
                if (!esdTrack) continue;
                if (clusterID < 0) {
-                       esdTrack->SetEMCALcluster(-99999);
+                       esdTrack->SetEMCALcluster(kUnmatched);
                }
                else {
                        AliEMCALMatchCluster *cluster = (AliEMCALMatchCluster*)fClusters->At(clusterID);
@@ -388,7 +485,7 @@ Int_t AliEMCALTracker::PropagateBack(AliESD* esd)
                }
        }
        */
-       AliInfo(Form("Saved %d matches", nSaved));
+       AliInfo(Form("Saved %d matches [%d good + %d fake]", nSaved, nGood, nFake));
 
        return 0;
 }
@@ -463,7 +560,6 @@ Double_t AliEMCALTracker::CheckPair
        Bool_t isTrue = kFALSE;
 //     if (tr->GetSeedLabel() == cl->Label()) {
 //             isTrue = kTRUE;
-//             cout << "TRUE MATCH!!!" << endl;
 //     }
        
        // copy track into temporary variable
@@ -474,7 +570,10 @@ Double_t AliEMCALTracker::CheckPair
        // check against cut on difference 'alpha - phi'
        Double_t phi = TMath::ATan2(cl->Y(), cl->X());
        phi = AngleDiff(phi, tr->GetAlpha());
-       if (phi < fCutAlphaMin || phi > fCutAlphaMax) return distance;
+       if (phi < fCutAlphaMin || phi > fCutAlphaMax){
+         delete tr;
+         return distance;
+       }
        
        // try to propagate to cluster radius
        // (return the 'distance' value if it fails)
@@ -492,8 +591,8 @@ Double_t AliEMCALTracker::CheckPair
                pos2[0] = cl->X();
                pos2[1] = cl->Y();
                pos2[2] = cl->Z();
-               AliKalmanTrack::MeanMaterialBudget(pos1, pos2, param);
-               rho = param[0];
+               MeanMaterialBudget(pos1, pos2, param);
+               rho = param[0]*param[4];
                x0 = param[1];
        }
        else if (fTrackCorrMode == kTrackCorrFixed) {
@@ -512,7 +611,10 @@ Double_t AliEMCALTracker::CheckPair
                if (isTrue) cout << "Init : " << rt << ' ' << x << ' ' << y << ' ' << z << endl;
                for (i = 0; i < fNPropSteps; i++) {
                        r = rt + (rc - rt) * ((Double_t)(i+1)/(Double_t)fNPropSteps);
-                       if (!tr->PropagateTo(r, x0, rho)) return distance;
+                       if (!tr->PropagateTo(r, x0, rho)){
+                         delete tr;
+                         return distance;
+                       }
                        tr->GetXYZ(pos);
                        if (isTrue) cout << "Step : " << r << ' ' << x << ' ' << y << ' ' << z << endl;
                }
@@ -520,7 +622,28 @@ Double_t AliEMCALTracker::CheckPair
        }
        else {
                // when no steps are used, no correction makes sense
-               if (!tr->PropagateTo(rc, 0.0, 0.0)) return distance;
+               //if (!tr->PropagateTo(rc, 0.0, 0.0)) return distance;
+               if (!tr->PropagateToGlobal(cl->X(), cl->Y(), cl->Z(), 0.0, 0.0)){
+                 delete tr;
+                 return distance;
+               }
+               /*
+               Bool_t propOK = kFALSE;
+               cout << "START" << endl;
+               Double_t dist, rCHK, bestDist = 10000000.0;
+               for (Double_t rTMP = rc; rTMP> rc*0.95; rTMP -= 0.1) {
+                       if (!tr->PropagateTo(rTMP)) continue;
+                       propOK = kTRUE;
+                       tr->GetXYZ(pos);
+                       rCHK = TMath::Sqrt(x*x + y*y);
+                       dist = TMath::Abs(rCHK - rc);
+                       cout << rCHK << " vs. " << rc << endl;
+                       
+                       if (TMath::Abs(rCHK - rc) < 0.01) break;
+               }
+               cout << "STOP" << endl;
+               if (!propOK) return distance;
+               */
        }
        
        // get global propagation of track at end of propagation
@@ -530,25 +653,36 @@ Double_t AliEMCALTracker::CheckPair
        TVector3 vc(cl->X(), cl->Y(), cl->Z());
        TVector3 vt(x, y, z);
        Double_t angle = TMath::Abs(vc.Angle(vt)) * TMath::RadToDeg();
+       // check: where is the track?
+       Double_t r, phiT, phiC;
+       r = TMath::Sqrt(pos[0]*pos[0] + pos[1]*pos[1]);
+       phiT = TMath::ATan2(pos[1], pos[0]) * TMath::RadToDeg();
+       phiC = vc.Phi() * TMath::RadToDeg();
+       //cout << "Propagated R, phiT, phiC = " << r << ' ' << phiT << ' ' << phiC << endl;
+       
        if (angle > fCutAngle) {
                //cout << "angle" << endl;
+         delete tr;
                return distance;
        }
                
        // compute differences wr to each coordinate
        x -= cl->X();
-       if (x > fCutX) {
+       if (TMath::Abs(x) > fCutX) {
                //cout << "cut X" << endl;
+         delete tr;
                return distance;
        }
        y -= cl->Y();
-       if (y > fCutY) {
+       if (TMath::Abs(y) > fCutY) {
                //cout << "cut Y" << endl;
+         delete tr;
                return distance;
        }
        z -= cl->Z();
-       if (z > fCutZ) {
+       if (TMath::Abs(z) > fCutZ) {
                //cout << "cut Z" << endl;
+         delete tr;
                return distance;
        }
        
@@ -596,8 +730,8 @@ Double_t AliEMCALTracker::CheckPairV2
                pos2[0] = cl->X();
                pos2[1] = cl->Y();
                pos2[2] = cl->Z();
-               AliKalmanTrack::MeanMaterialBudget(pos1, pos2, param);
-               rho = param[0];
+               MeanMaterialBudget(pos1, pos2, param);
+               rho = param[0]*param[4];
                x0 = param[1];
        }
        else if (fTrackCorrMode == kTrackCorrFixed) {
@@ -664,6 +798,7 @@ Double_t AliEMCALTracker::CheckPairV2
        TVector3 vdiff = vt-vc;
                
        // compute differences wr to each coordinate
+       delete track;
        if (vdiff.X() > fCutX) return distance;
        if (vdiff.Y() > fCutY) return distance;
        if (vdiff.Z() > fCutZ) return distance;
@@ -675,6 +810,95 @@ Double_t AliEMCALTracker::CheckPairV2
 //
 //------------------------------------------------------------------------------
 //
+Double_t AliEMCALTracker::CheckPairV3
+(AliEMCALTrack *track, AliEMCALMatchCluster *cl)
+{
+       //
+       // Given a track and a cluster,
+       // propagates the first to the radius of the second.
+       // Then, checks the propagation point against all cuts.
+       // If at least a cut is not passed, a valuer equal to 
+       // twice the maximum allowed distance is passed (so the value returned
+       // will not be taken into account when creating matches)
+       //
+       
+       AliEMCALTrack tr(*track);
+       
+       Int_t    sector;
+       Double_t distance = 2.0 * fMaxDist;
+       Double_t dx, dy, dz;
+       Double_t phi, alpha, slope, tgtXnum, tgtXden, sectorWidth = 20.0 * TMath::DegToRad();
+       Double_t xcurr, xprop, param[6] = {0., 0., 0., 0., 0., 0.}, x0, rho, bz;
+       Double_t x[3], x1[3], x2[3];
+       
+       // get initial track position
+       xcurr = tr.GetX();
+       
+       // evaluate the EMCAL sector number
+       phi = cl->Phi();
+       if (phi < 0.0) phi += TMath::TwoPi();
+       sector = (Int_t)(phi / sectorWidth);
+       alpha = ((Double_t)sector + 0.5) * sectorWidth;
+       // evaluate the corresponding X for track propagation
+       slope = TMath::Tan(alpha - 0.5*TMath::Pi());
+       tgtXnum = cl->Y() - slope * cl->X();
+       tgtXden = TMath::Sqrt(1.0 + slope*slope);
+       xprop = TMath::Abs(tgtXnum / tgtXden);
+       
+       // propagate by small steps
+       tr.GetXYZ(x1);
+       bz = tr.GetBz();
+       if (!tr.GetXYZAt(xprop, bz, x2)) return distance;
+       //AliKalmanTrack::MeanMaterialBudget(x1, x2, param);
+       rho = param[0]*param[4];
+       x0 = param[1];
+       if (!tr.PropagateTo(xprop, x0, rho)) return distance;
+       //if (!tr.PropagateTo(xprop, 0.0, 0.0)) return distance;
+       
+       // get propagated position at the end
+       tr.GetXYZ(x);
+       dx = TMath::Abs(x[0] - cl->X());
+       dy = TMath::Abs(x[1] - cl->Y());
+       dz = TMath::Abs(x[2] - cl->Z());
+       if (dx > fCutX || dy > fCutY || dz > fCutZ) return distance;
+       
+       distance = TMath::Sqrt(dx*dx + dy*dy + dz*dz);
+       
+       return distance;
+}
+//
+//------------------------------------------------------------------------------
+//
+Bool_t AliEMCALTracker::PropagateToEMCAL(AliEMCALTrack *tr)
+{
+       //
+       // Propagates the track to the proximity of the EMCAL surface
+       //
+       
+       Double_t xcurr, xtemp, xprop = 438.0, step = 10.0, param[6], x0, rho, bz;
+       Double_t x1[3], x2[3];
+       
+       // get initial track position
+       xcurr = tr->GetX();
+       
+       // propagate by small steps
+       for (xtemp = xcurr + step; xtemp < xprop; xtemp += step) {
+               // to compute material budget, take current position and 
+               // propagated hypothesis without energy loss
+               tr->GetXYZ(x1);
+               bz = tr->GetBz();
+               if (!tr->GetXYZAt(xtemp, bz, x2)) return kFALSE;
+               MeanMaterialBudget(x1, x2, param);
+               rho = param[0]*param[4];
+               x0 = param[1];
+               if (!tr->PropagateTo(xtemp, x0, rho)) return kFALSE;
+       }
+       
+       return kTRUE;
+}
+//
+//------------------------------------------------------------------------------
+//
 Int_t AliEMCALTracker::CreateMatches()
 {
        //
@@ -702,23 +926,23 @@ Int_t AliEMCALTracker::CreateMatches()
        // external loop on clusters, internal loop on tracks
        Double_t dist;
        for (ic = 0; ic < nClusters; ic++) {
-               cout << "\rMatching cluster " << ic+1 << " of " << nClusters << flush;
                AliEMCALMatchCluster *cluster = (AliEMCALMatchCluster*)fClusters->At(ic);
                for (it = 0; it < nTracks; it++) {
                        AliEMCALTrack *track = (AliEMCALTrack*)fTracks->At(it);
                        dist = CheckPair(track, cluster);
+                       //cout << dist << endl;
                        if (dist <= fMaxDist) {
                                AliEMCALMatch *candidate = new AliEMCALMatch;
                                candidate->SetIndexT(it);
                                candidate->SetIndexC(ic);
                                candidate->SetDistance(dist);
+                               candidate->SetPt(track->GetSignedPt());
                                fMatches->Add(candidate);
                                count++;
                        }
                }
        }
-       cout << endl;           
-       
+               
        /*
        // loop on clusters and tracks
        Int_t icBest;
@@ -818,6 +1042,9 @@ Int_t AliEMCALTracker::SolveCompetitions()
        }
        */
        
+       delete [] usedC;
+       delete [] usedT;
+
        return count;
 }
 //
@@ -826,10 +1053,12 @@ Int_t AliEMCALTracker::SolveCompetitions()
 void AliEMCALTracker::UnloadClusters() 
 {
        //
-       // Free memory from clusters
+       // Free memory from all arrays
+        // This method is called after the local tracking step
+        // so we can safely delete everything 
        //
        
-       Clear("CLUSTERS");
+       Clear();
 }
 //
 //------------------------------------------------------------------------------
@@ -857,7 +1086,7 @@ AliEMCALTracker::AliEMCALMatchCluster::AliEMCALMatchCluster(Int_t index, AliEMCA
 //
 AliEMCALTracker::AliEMCALMatchCluster::AliEMCALMatchCluster(Int_t index, AliESDCaloCluster *caloCluster)
   : fIndex(index),
-    fLabel(caloCluster->GetPrimaryIndex()),
+    fLabel(caloCluster->GetLabel()),
     fX(0.),
     fY(0.),
     fZ(0.)
@@ -867,7 +1096,7 @@ AliEMCALTracker::AliEMCALMatchCluster::AliEMCALMatchCluster(Int_t index, AliESDC
        // Index of passed cluster in its native array must be specified.
        //
        Float_t clpos[3];
-       caloCluster->GetGlobalPosition(clpos);
+       caloCluster->GetPosition(clpos);
        
        fX = (Double_t)clpos[0];
        fY = (Double_t)clpos[1];
@@ -884,8 +1113,8 @@ Int_t AliEMCALTracker::AliEMCALMatch::Compare(const TObject *obj) const
        
        AliEMCALTracker::AliEMCALMatch *that = (AliEMCALTracker::AliEMCALMatch*)obj;
        
-       Double_t thisDist = fDistance;
-       Double_t thatDist = that->GetDistance();
+       Double_t thisDist = fPt;//fDistance;
+       Double_t thatDist = that->fPt;//that->GetDistance();
        
        if (thisDist > thatDist) return 1;
        else if (thisDist < thatDist) return -1;
@@ -897,7 +1126,8 @@ AliEMCALTracker::AliEMCALMatch::AliEMCALMatch()
     fCanBeSaved(kFALSE), 
     fIndexC(0), 
     fIndexT(0), 
-    fDistance(0.)
+    fDistance(0.),
+    fPt(0.)
 {
   //default constructor
 
@@ -908,7 +1138,8 @@ AliEMCALTracker::AliEMCALMatch::AliEMCALMatch(const AliEMCALMatch& copy)
     fCanBeSaved(copy.fCanBeSaved),
     fIndexC(copy.fIndexC),
     fIndexT(copy.fIndexT),
-    fDistance(copy.fDistance)
+    fDistance(copy.fDistance),
+    fPt(copy.fPt)
 {
   //copy ctor
 }