]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ITS/AliITSMultReconstructor.cxx
RC12, RC17 violation: suppression
[u/mrichter/AliRoot.git] / ITS / AliITSMultReconstructor.cxx
index 398a25ef9ec17782668a48e94e3d73f89ddf8db3..2b19d680f12b459c80aa10a5b9c33f7cb882d091 100644 (file)
 
 /* $Id$ */
 
-//____________________________________________________________________
-// 
-// AliITSMultReconstructor - find clusters in the pixels (theta and
-// phi) and tracklets.
+//_________________________________________________________________________
 // 
-// These can be used to extract charged particles multiplcicity from the ITS.
+//        Implementation of the ITS-SPD trackleter class
 //
-// A tracklet consist of two ITS clusters, one in the first pixel
-// layer and one in the second. The clusters are associates if the 
-// differencies in Phi (azimuth) and Zeta (longitudinal) are inside 
-// a fiducial volume. In case of multiple candidates it is selected the
-// candidate with minimum distance in Phi. 
-// The parameter AssociationChoice allows to control if two clusters
-// in layer 2 can be associated to the same cluster in layer 1 or not.
-// (TRUE means double associations exluded; default = TRUE)
+// It retrieves clusters in the pixels (theta and phi) and finds tracklets.
+// These can be used to extract charged particle multiplicity from the ITS.
 //
-// Two methods return the number of traklets and the number of clusters 
-// in the first SPD layer (GetNTracklets GetNSingleClusters)
+// A tracklet consists of two ITS clusters, one in the first pixel layer and 
+// one in the second. The clusters are associated if the differences in 
+// Phi (azimuth) and Theta (polar angle) are within fiducial windows.
+// In case of multiple candidates the candidate with minimum
+// distance is selected. 
 //
-// -----------------------------------------------------------------
-// 
-// NOTE: The cuts on phi and zeta depend on the interacting system (p-p  
-//  or Pb-Pb). Please, check the file AliITSMultReconstructor.h and be 
-//  sure that SetPhiWindow and SetZetaWindow are defined accordingly.
+// Two methods return the number of tracklets and the number of unassociated 
+// clusters (i.e. not used in any tracklet) in the first SPD layer
+// (GetNTracklets and GetNSingleClusters)
+//
+// The cuts on phi and theta depend on the interacting system (p-p or Pb-Pb)
+// and can be set via AliITSRecoParam class
+// (SetPhiWindow and SetThetaWindow)  
 // 
-//  Author :  Tiziano Virgili 
-//  
-//  Recent updates (D. Elia, INFN Bari):
-//     - multiple association forbidden (fOnlyOneTrackletPerC2 = kTRUE) 
+// Origin: Tiziano Virgili 
+//
+// Current support and development: 
+//         Domenico Elia, Maria Nicassio (INFN Bari) 
+//         Domenico.Elia@ba.infn.it, Maria.Nicassio@ba.infn.it
+//
+// Most recent updates:
+//     - multiple association forbidden (fOnlyOneTrackletPerC2 = kTRUE)    
 //     - phi definition changed to ALICE convention (0,2*TMath::pi()) 
 //     - cluster coordinates taken with GetGlobalXYZ()
 //     - fGeometry removed
 //     - number of fired chips on the two layers
-//     - option to avoid duplicates in the overlaps (fRemoveClustersFromOverlaps)
+//     - option to cut duplicates in the overlaps
 //     - options and fiducial cuts via AliITSRecoParam
-//
-//____________________________________________________________________
+//     - move from DeltaZeta to DeltaTheta cut
+//     - update to the new algorithm by Mariella and Jan Fiete
+//     - store also DeltaTheta in the ESD 
+//     - less new and delete calls when creating the needed arrays
+//_________________________________________________________________________
 
 #include <TClonesArray.h>
 #include <TH1F.h>
 #include <TH2F.h>
 #include <TTree.h>
+#include "TArrayI.h"
 
 #include "AliITSMultReconstructor.h"
 #include "AliITSReconstructor.h"
 #include "AliITSsegmentationSPD.h"
 #include "AliITSRecPoint.h"
+#include "AliITSRecPointContainer.h"
 #include "AliITSgeom.h"
+#include "AliITSgeomTGeo.h"
 #include "AliLog.h"
+#include "TGeoGlobalMagField.h"
+#include "AliMagF.h"
 
 //____________________________________________________________________
 ClassImp(AliITSMultReconstructor)
@@ -71,6 +79,7 @@ ClassImp(AliITSMultReconstructor)
 
 //____________________________________________________________________
 AliITSMultReconstructor::AliITSMultReconstructor():
+TObject(),
 fClustersLay1(0),
 fClustersLay2(0),
 fDetectorIndexClustersLay1(0),
@@ -79,28 +88,23 @@ fOverlapFlagClustersLay1(0),
 fOverlapFlagClustersLay2(0),
 fTracklets(0),
 fSClusters(0),
-fAssociationFlag(0),
 fNClustersLay1(0),
 fNClustersLay2(0),
 fNTracklets(0),
 fNSingleCluster(0),
-fOnlyOneTrackletPerC2(0),
 fPhiWindow(0),
-fZetaWindow(0),
+fThetaWindow(0),
+fPhiShift(0),
 fRemoveClustersFromOverlaps(0),
 fPhiOverlapCut(0),
 fZetaOverlapCut(0),
 fHistOn(0),
 fhClustersDPhiAcc(0),
 fhClustersDThetaAcc(0),
-fhClustersDZetaAcc(0),
 fhClustersDPhiAll(0),
 fhClustersDThetaAll(0),
-fhClustersDZetaAll(0),
 fhDPhiVsDThetaAll(0),
 fhDPhiVsDThetaAcc(0),
-fhDPhiVsDZetaAll(0),
-fhDPhiVsDZetaAcc(0),
 fhetaTracklets(0),
 fhphiTracklets(0),
 fhetaClustersLay1(0),
@@ -116,75 +120,51 @@ fhphiClustersLay1(0){
   SetHistOn();
 
   if(AliITSReconstructor::GetRecoParam()) { 
-    SetOnlyOneTrackletPerC2(AliITSReconstructor::GetRecoParam()->GetTrackleterOnlyOneTrackletPerC2());
     SetPhiWindow(AliITSReconstructor::GetRecoParam()->GetTrackleterPhiWindow());
-    SetZetaWindow(AliITSReconstructor::GetRecoParam()->GetTrackleterZetaWindow());
+    SetThetaWindow(AliITSReconstructor::GetRecoParam()->GetTrackleterThetaWindow());
+    SetPhiShift(AliITSReconstructor::GetRecoParam()->GetTrackleterPhiShift());
     SetRemoveClustersFromOverlaps(AliITSReconstructor::GetRecoParam()->GetTrackleterRemoveClustersFromOverlaps());
     SetPhiOverlapCut(AliITSReconstructor::GetRecoParam()->GetTrackleterPhiOverlapCut());
     SetZetaOverlapCut(AliITSReconstructor::GetRecoParam()->GetTrackleterZetaOverlapCut());
   } else {
-    SetOnlyOneTrackletPerC2();
     SetPhiWindow();
-    SetZetaWindow();
+    SetThetaWindow();
+    SetPhiShift();
     SetRemoveClustersFromOverlaps();
     SetPhiOverlapCut();
     SetZetaOverlapCut();
   } 
   
 
-  fClustersLay1              = new Float_t*[300000];
-  fClustersLay2              = new Float_t*[300000];
-  fDetectorIndexClustersLay1 = new Int_t[300000];
-  fDetectorIndexClustersLay2 = new Int_t[300000];
-  fOverlapFlagClustersLay1   = new Bool_t[300000];
-  fOverlapFlagClustersLay2   = new Bool_t[300000];
-  fTracklets                 = new Float_t*[300000];
-  fSClusters                 = new Float_t*[300000];
-  fAssociationFlag           = new Bool_t[300000];
-
-  for(Int_t i=0; i<300000; i++) {
-    fClustersLay1[i]       = new Float_t[6];
-    fClustersLay2[i]       = new Float_t[6];
-    fTracklets[i]          = new Float_t[5];
-    fSClusters[i]           = new Float_t[2];
-    fOverlapFlagClustersLay1[i]   = kFALSE;
-    fOverlapFlagClustersLay2[i]   = kFALSE;
-    fAssociationFlag[i]    = kFALSE;
-  }
+  fClustersLay1              = 0;
+  fClustersLay2              = 0;
+  fDetectorIndexClustersLay1 = 0;
+  fDetectorIndexClustersLay2 = 0;
+  fOverlapFlagClustersLay1   = 0;
+  fOverlapFlagClustersLay2   = 0;
+  fTracklets                 = 0;
+  fSClusters                 = 0;
 
   // definition of histograms
-  fhClustersDPhiAcc   = new TH1F("dphiacc",  "dphi",  100,0.,0.1);
-  fhClustersDPhiAcc->SetDirectory(0);
+  Bool_t oldStatus = TH1::AddDirectoryStatus();
+  TH1::AddDirectory(kFALSE);
+  
+  fhClustersDPhiAcc   = new TH1F("dphiacc",  "dphi",  100,-0.1,0.1);
   fhClustersDThetaAcc = new TH1F("dthetaacc","dtheta",100,-0.1,0.1);
-  fhClustersDThetaAcc->SetDirectory(0);
-  fhClustersDZetaAcc = new TH1F("dzetaacc","dzeta",100,-1.,1.);
-  fhClustersDZetaAcc->SetDirectory(0);
 
-  fhDPhiVsDZetaAcc = new TH2F("dphiVsDzetaacc","",100,-1.,1.,100,0.,0.1);
-  fhDPhiVsDZetaAcc->SetDirectory(0);
-  fhDPhiVsDThetaAcc = new TH2F("dphiVsDthetaAcc","",100,-0.1,0.1,100,0.,0.1);
-  fhDPhiVsDThetaAcc->SetDirectory(0);
+  fhDPhiVsDThetaAcc = new TH2F("dphiVsDthetaAcc","",100,-0.1,0.1,100,-0.1,0.1);
 
   fhClustersDPhiAll   = new TH1F("dphiall",  "dphi",  100,0.0,0.5);
-  fhClustersDPhiAll->SetDirectory(0);
-  fhClustersDThetaAll = new TH1F("dthetaall","dtheta",100,-0.5,0.5);
-  fhClustersDThetaAll->SetDirectory(0);
-  fhClustersDZetaAll = new TH1F("dzetaall","dzeta",100,-5.,5.);
-  fhClustersDZetaAll->SetDirectory(0);
+  fhClustersDThetaAll = new TH1F("dthetaall","dtheta",100,0.0,0.5);
 
-  fhDPhiVsDZetaAll = new TH2F("dphiVsDzetaall","",100,-5.,5.,100,0.,0.5);
-  fhDPhiVsDZetaAll->SetDirectory(0);
-  fhDPhiVsDThetaAll = new TH2F("dphiVsDthetaAll","",100,-0.5,0.5,100,0.,0.5);
-  fhDPhiVsDThetaAll->SetDirectory(0);
+  fhDPhiVsDThetaAll = new TH2F("dphiVsDthetaAll","",100,0.,0.5,100,0.,0.5);
 
   fhetaTracklets  = new TH1F("etaTracklets",  "eta",  100,-2.,2.);
-  fhetaTracklets->SetDirectory(0);
   fhphiTracklets  = new TH1F("phiTracklets",  "phi",  100, 0., 2*TMath::Pi());
-  fhphiTracklets->SetDirectory(0);
   fhetaClustersLay1  = new TH1F("etaClustersLay1",  "etaCl1",  100,-2.,2.);
-  fhetaClustersLay1->SetDirectory(0);
   fhphiClustersLay1  = new TH1F("phiClustersLay1", "phiCl1", 100, 0., 2*TMath::Pi());
-  fhphiClustersLay1->SetDirectory(0);
+  
+  TH1::AddDirectory(oldStatus);
 }
 
 //______________________________________________________________________
@@ -197,28 +177,23 @@ fOverlapFlagClustersLay1(mr.fOverlapFlagClustersLay1),
 fOverlapFlagClustersLay2(mr.fOverlapFlagClustersLay2),
 fTracklets(mr.fTracklets),
 fSClusters(mr.fSClusters),
-fAssociationFlag(mr.fAssociationFlag),
 fNClustersLay1(mr.fNClustersLay1),
 fNClustersLay2(mr.fNClustersLay2),
 fNTracklets(mr.fNTracklets),
 fNSingleCluster(mr.fNSingleCluster),
-fOnlyOneTrackletPerC2(mr.fOnlyOneTrackletPerC2),
 fPhiWindow(mr.fPhiWindow),
-fZetaWindow(mr.fZetaWindow),
+fThetaWindow(mr.fThetaWindow),
+fPhiShift(mr.fPhiShift),
 fRemoveClustersFromOverlaps(mr.fRemoveClustersFromOverlaps),
 fPhiOverlapCut(mr.fPhiOverlapCut),
 fZetaOverlapCut(mr.fZetaOverlapCut),
 fHistOn(mr.fHistOn),
 fhClustersDPhiAcc(mr.fhClustersDPhiAcc),
 fhClustersDThetaAcc(mr.fhClustersDThetaAcc),
-fhClustersDZetaAcc(mr.fhClustersDZetaAcc),
 fhClustersDPhiAll(mr.fhClustersDPhiAll),
 fhClustersDThetaAll(mr.fhClustersDThetaAll),
-fhClustersDZetaAll(mr.fhClustersDZetaAll),
 fhDPhiVsDThetaAll(mr.fhDPhiVsDThetaAll),
 fhDPhiVsDThetaAcc(mr.fhDPhiVsDThetaAcc),
-fhDPhiVsDZetaAll(mr.fhDPhiVsDZetaAll),
-fhDPhiVsDZetaAcc(mr.fhDPhiVsDZetaAcc),
 fhetaTracklets(mr.fhetaTracklets),
 fhphiTracklets(mr.fhphiTracklets),
 fhetaClustersLay1(mr.fhetaClustersLay1),
@@ -242,26 +217,28 @@ AliITSMultReconstructor::~AliITSMultReconstructor(){
   // delete histograms
   delete fhClustersDPhiAcc;
   delete fhClustersDThetaAcc;
-  delete fhClustersDZetaAcc;
   delete fhClustersDPhiAll;
   delete fhClustersDThetaAll;
-  delete fhClustersDZetaAll;
   delete fhDPhiVsDThetaAll;
   delete fhDPhiVsDThetaAcc;
-  delete fhDPhiVsDZetaAll;
-  delete fhDPhiVsDZetaAcc;
   delete fhetaTracklets;
   delete fhphiTracklets;
   delete fhetaClustersLay1;
   delete fhphiClustersLay1;
 
   // delete arrays
-  for(Int_t i=0; i<300000; i++) {
+  for(Int_t i=0; i<fNClustersLay1; i++)
     delete [] fClustersLay1[i];
+    
+  for(Int_t i=0; i<fNClustersLay2; i++)
     delete [] fClustersLay2[i];
+    
+  for(Int_t i=0; i<fNTracklets; i++)
     delete [] fTracklets[i];
+    
+  for(Int_t i=0; i<fNSingleCluster; i++)
     delete [] fSClusters[i];
-  }
+    
   delete [] fClustersLay1;
   delete [] fClustersLay2;
   delete [] fDetectorIndexClustersLay1;
@@ -270,18 +247,15 @@ AliITSMultReconstructor::~AliITSMultReconstructor(){
   delete [] fOverlapFlagClustersLay2;
   delete [] fTracklets;
   delete [] fSClusters;
-
-  delete [] fAssociationFlag;
 }
 
 //____________________________________________________________________
-void
-AliITSMultReconstructor::Reconstruct(TTree* clusterTree, Float_t* vtx, Float_t* /* vtxRes*/) {
+void AliITSMultReconstructor::Reconstruct(TTree* clusterTree, Float_t* vtx, Float_t* /* vtxRes*/) {
   //
   // - calls LoadClusterArray that finds the position of the clusters
   //   (in global coord) 
   // - convert the cluster coordinates to theta, phi (seen from the
-  //   interaction vertex). The third coordinate is used for ....
+  //   interaction vertex). 
   // - makes an array of tracklets 
   //   
   // After this method has been called, the clusters of the two layers
@@ -291,13 +265,49 @@ AliITSMultReconstructor::Reconstruct(TTree* clusterTree, Float_t* vtx, Float_t*
   fNClustersLay1 = 0;
   fNClustersLay2 = 0;
   fNTracklets = 0; 
-  fNSingleCluster = 0; 
+  fNSingleCluster = 0;
+
   // loading the clusters 
   LoadClusterArrays(clusterTree);
 
+  const Double_t pi = TMath::Pi();
+  
+  // dPhi shift is field dependent
+  // get average magnetic field
+  Float_t bz = 0;
+  AliMagF* field = 0;
+  if (TGeoGlobalMagField::Instance())
+    field = dynamic_cast<AliMagF*>(TGeoGlobalMagField::Instance()->GetField());
+  if (!field)
+  {
+    AliError("Could not retrieve magnetic field. Assuming no field. Delta Phi shift will be deactivated in AliITSMultReconstructor.")
+  }
+  else
+    bz = TMath::Abs(field->SolenoidField());
+  
+  const Double_t dPhiShift = fPhiShift / 5 * bz; 
+  AliDebug(1, Form("Using phi shift of %f", dPhiShift));
+  
+  const Double_t dPhiWindow2 = fPhiWindow * fPhiWindow;
+  const Double_t dThetaWindow2 = fThetaWindow * fThetaWindow;
+  
+  Int_t* partners = new Int_t[fNClustersLay2];
+  Float_t* minDists = new Float_t[fNClustersLay2];
+  Int_t* associatedLay1 = new Int_t[fNClustersLay1];
+  TArrayI** blacklist = new TArrayI*[fNClustersLay1];
+
+  for (Int_t i=0; i<fNClustersLay2; i++) {
+    partners[i] = -1;
+    minDists[i] = 2;
+  }
+  for (Int_t i=0; i<fNClustersLay1; i++)
+    associatedLay1[i] = 0; 
+  for (Int_t i=0; i<fNClustersLay1; i++)
+    blacklist[i] = 0;
+
   // find the tracklets
   AliDebug(1,"Looking for tracklets... ");  
-
+  
   //###########################################################
   // Loop on layer 1 : finding theta, phi and z 
   for (Int_t iC1=0; iC1<fNClustersLay1; iC1++) {    
@@ -305,13 +315,11 @@ AliITSMultReconstructor::Reconstruct(TTree* clusterTree, Float_t* vtx, Float_t*
     Float_t y = fClustersLay1[iC1][1] - vtx[1];
     Float_t z = fClustersLay1[iC1][2] - vtx[2];
 
-    Float_t r    = TMath::Sqrt(TMath::Power(x,2) +
-                              TMath::Power(y,2) +
-                              TMath::Power(z,2));
+    Float_t r    = TMath::Sqrt(x*x + y*y + z*z);
     
     fClustersLay1[iC1][0] = TMath::ACos(z/r);                   // Store Theta
     fClustersLay1[iC1][1] = TMath::Pi() + TMath::ATan2(-y,-x);  // Store Phi
-    fClustersLay1[iC1][2] = z/r;                                // Store scaled z
+    
     if (fHistOn) {
       Float_t eta=fClustersLay1[iC1][0];
       eta= TMath::Tan(eta/2.);
@@ -327,160 +335,200 @@ AliITSMultReconstructor::Reconstruct(TTree* clusterTree, Float_t* vtx, Float_t*
     Float_t y = fClustersLay2[iC2][1] - vtx[1];
     Float_t z = fClustersLay2[iC2][2] - vtx[2];
    
-    Float_t r    = TMath::Sqrt(TMath::Power(x,2) +
-                              TMath::Power(y,2) +
-                              TMath::Power(z,2));
+    Float_t r    = TMath::Sqrt(x*x + y*y + z*z);
     
     fClustersLay2[iC2][0] = TMath::ACos(z/r);                   // Store Theta
     fClustersLay2[iC2][1] = TMath::Pi() + TMath::ATan2(-y,-x);  // Store Phi
-    fClustersLay2[iC2][2] = z;                                  // Store z
-
- // this only needs to be initialized for the fNClustersLay2 first associations
-    fAssociationFlag[iC2] = kFALSE;
   }  
   
   //###########################################################
-  // Loop on layer 1 
-  for (Int_t iC1=0; iC1<fNClustersLay1; iC1++) {    
+  Int_t found = 1;
+  while (found > 0) {
+    found = 0;
+
+    // Step1: find all tracklets allowing double assocation
+    // Loop on layer 1 
+    for (Int_t iC1=0; iC1<fNClustersLay1; iC1++) {    
+
+      // already used or in the overlap ?
+      if (associatedLay1[iC1] != 0 || fOverlapFlagClustersLay1[iC1]) continue;
 
-    // reset of variables for multiple candidates
-    Int_t  iC2WithBestDist = 0;     // reset 
-    Float_t distmin        = 100.;  // just to put a huge number! 
-    Float_t dPhimin        = 0.;  // Used for histograms only! 
-    Float_t dThetamin      = 0.;  // Used for histograms only! 
-    Float_t dZetamin       = 0.;  // Used for histograms only! 
+      found++;
+
+      // reset of variables for multiple candidates
+      Int_t  iC2WithBestDist = -1;   // reset
+      Double_t minDist       =  2;   // reset
      
-    if (fOverlapFlagClustersLay1[iC1]) continue;
-    // Loop on layer 2 
-    for (Int_t iC2=0; iC2<fNClustersLay2; iC2++) {      
-      if (fOverlapFlagClustersLay2[iC2]) continue;
-      // The following excludes double associations
-      if (!fAssociationFlag[iC2]) {
-       
+      // Loop on layer 2 
+      for (Int_t iC2=0; iC2<fNClustersLay2; iC2++) {      
+
+        // in the overlap ?
+        if (fOverlapFlagClustersLay2[iC2]) continue;
+
+        if (blacklist[iC1]) {
+          Bool_t blacklisted = kFALSE;
+          for (Int_t i=0; i<blacklist[iC1]->GetSize(); i++) {
+            if (blacklist[iC1]->At(i) == iC2) {
+              blacklisted = kTRUE;
+              break;
+            }
+          }
+          if (blacklisted) continue;
+        }
+
        // find the difference in angles
-       Float_t dTheta = fClustersLay2[iC2][0] - fClustersLay1[iC1][0];
-       Float_t dPhi   = TMath::Abs(fClustersLay2[iC2][1] - fClustersLay1[iC1][1]);
+       Double_t dTheta = TMath::Abs(fClustersLay2[iC2][0] - fClustersLay1[iC1][0]);
+       Double_t dPhi  = TMath::Abs(fClustersLay2[iC2][1] - fClustersLay1[iC1][1]);
         // take into account boundary condition
-        if (dPhi>TMath::Pi()) dPhi=2.*TMath::Pi()-dPhi;        
-
-       // find the difference in z (between linear projection from layer 1
-       // and the actual point: Dzeta= z1/r1*r2 -z2)   
-       Float_t r2   = fClustersLay2[iC2][2]/TMath::Cos(fClustersLay2[iC2][0]);
-        Float_t dZeta  = fClustersLay1[iC1][2]*r2 - fClustersLay2[iC2][2];
-
+        if (dPhi>pi) dPhi=2.*pi-dPhi;
+        
        if (fHistOn) {
-         fhClustersDPhiAll->Fill(dPhi);    
+         fhClustersDPhiAll->Fill(dPhi);
          fhClustersDThetaAll->Fill(dTheta);    
-         fhClustersDZetaAll->Fill(dZeta);    
          fhDPhiVsDThetaAll->Fill(dTheta, dPhi);
-         fhDPhiVsDZetaAll->Fill(dZeta, dPhi);
        }
-       // make "elliptical" cut in Phi and Zeta! 
-       Float_t d = TMath::Sqrt(TMath::Power(dPhi/fPhiWindow,2) + TMath::Power(dZeta/fZetaWindow,2));
-
-       if (d>1) continue;      
-       
-       //look for the minimum distance: the minimum is in iC2WithBestDist
-               if (TMath::Sqrt(dZeta*dZeta+(r2*dPhi*r2*dPhi)) < distmin ) {
-         distmin=TMath::Sqrt(dZeta*dZeta + (r2*dPhi*r2*dPhi));
-         dPhimin = dPhi;
-         dThetamin = dTheta;
-         dZetamin = dZeta; 
+        
+        dPhi -= dPhiShift;
+                
+       // make "elliptical" cut in Phi and Theta! 
+       Float_t d = dPhi*dPhi/dPhiWindow2 + dTheta*dTheta/dThetaWindow2;
+
+       // look for the minimum distance: the minimum is in iC2WithBestDist
+               if (d<1 && d<minDist) {
+         minDist=d;
          iC2WithBestDist = iC2;
        }
-      } 
-    } // end of loop over clusters in layer 2 
+      } // end of loop over clusters in layer 2 
     
-    if (distmin<100) { // This means that a cluster in layer 2 was found that mathes with iC1
-
-      if (fHistOn) {
-       fhClustersDPhiAcc->Fill(dPhimin);
-       fhClustersDThetaAcc->Fill(dThetamin);    
-       fhClustersDZetaAcc->Fill(dZetamin);    
-       fhDPhiVsDThetaAcc->Fill(dThetamin, dPhimin);
-       fhDPhiVsDZetaAcc->Fill(dZetamin, dPhimin);
-      }
-      
-      if (fOnlyOneTrackletPerC2) fAssociationFlag[iC2WithBestDist] = kTRUE; // flag the association
-      
-      // store the tracklet
-      
-      // use the theta from the clusters in the first layer
-      fTracklets[fNTracklets][0] = fClustersLay1[iC1][0];
-      // use the phi from the clusters in the first layer
-      fTracklets[fNTracklets][1] = fClustersLay1[iC1][1];
-      // store the difference between phi1 and phi2
-      fTracklets[fNTracklets][2] = fClustersLay1[iC1][1] - fClustersLay2[iC2WithBestDist][1];
-
-      // define dphi in the range [0,pi] with proper sign (track charge correlated)
-      if (fTracklets[fNTracklets][2] > TMath::Pi()) 
-          fTracklets[fNTracklets][2] = fTracklets[fNTracklets][2]-2.*TMath::Pi();
-      if (fTracklets[fNTracklets][2] < -TMath::Pi()) 
-          fTracklets[fNTracklets][2] = fTracklets[fNTracklets][2]+2.*TMath::Pi();
-
-      // find label
-      // if equal label in both clusters found this label is assigned
-      // if no equal label can be found the first labels of the L1 AND L2 cluster are assigned
-      Int_t label1 = 0;
-      Int_t label2 = 0;
-      while (label2 < 3)
-      {
-        if ((Int_t) fClustersLay1[iC1][3+label1] != -2 && (Int_t) fClustersLay1[iC1][3+label1] == (Int_t) fClustersLay2[iC2WithBestDist][3+label2])
-          break;
-        label1++;
-        if (label1 == 3)
-        {
-          label1 = 0;
-          label2++;
-        }
-      }
+      if (minDist<1) { // This means that a cluster in layer 2 was found that matches with iC1
+
+        if (minDists[iC2WithBestDist] > minDist) {
+          Int_t oldPartner = partners[iC2WithBestDist];
+          partners[iC2WithBestDist] = iC1;
+          minDists[iC2WithBestDist] = minDist;
+
+          // mark as assigned
+          associatedLay1[iC1] = 1;
+          
+          if (oldPartner != -1) {
+            // redo partner search for cluster in L0 (oldPartner), putting this one (iC2WithBestDist) on its blacklist
+            if (blacklist[oldPartner] == 0) {
+              blacklist[oldPartner] = new TArrayI(1);
+            } else blacklist[oldPartner]->Set(blacklist[oldPartner]->GetSize()+1);
+
+            blacklist[oldPartner]->AddAt(iC2WithBestDist, blacklist[oldPartner]->GetSize()-1);
+
+            // mark as free
+            associatedLay1[oldPartner] = 0;
+          }
+        } else {
+          // try again to find a cluster without considering iC2WithBestDist 
+          if (blacklist[iC1] == 0) {
+            blacklist[iC1] = new TArrayI(1);
+          }
+          else 
+            blacklist[iC1]->Set(blacklist[iC1]->GetSize()+1);
+   
+          blacklist[iC1]->AddAt(iC2WithBestDist, blacklist[iC1]->GetSize()-1);
+        } 
 
-      if (label2 < 3)
-      {
-        AliDebug(AliLog::kDebug, Form("Found label %d == %d for tracklet candidate %d\n", (Int_t) fClustersLay1[iC1][3+label1], (Int_t) fClustersLay2[iC2WithBestDist][3+label2], fNTracklets));
-        fTracklets[fNTracklets][3] = fClustersLay1[iC1][3+label1];
-        fTracklets[fNTracklets][4] = fClustersLay2[iC2WithBestDist][3+label2];
-      }
-      else
-      {
-        AliDebug(AliLog::kDebug, Form("Did not find label %d %d %d %d %d %d for tracklet candidate %d\n", (Int_t) fClustersLay1[iC1][3], (Int_t) fClustersLay1[iC1][4], (Int_t) fClustersLay1[iC1][5], (Int_t) fClustersLay2[iC2WithBestDist][3], (Int_t) fClustersLay2[iC2WithBestDist][4], (Int_t) fClustersLay2[iC2WithBestDist][5], fNTracklets));
-        fTracklets[fNTracklets][3] = fClustersLay1[iC1][3];
-        fTracklets[fNTracklets][4] = fClustersLay2[iC2WithBestDist][3];
-      }
+      } else // cluster has no partner; remove
+        associatedLay1[iC1] = 2;   
+    } // end of loop over clusters in layer 1
+  }  
+  // Step2: store tracklets; remove used clusters
+  for (Int_t iC2=0; iC2<fNClustersLay2; iC2++) {
 
-      if (fHistOn) {
-       Float_t eta=fTracklets[fNTracklets][0];
-       eta= TMath::Tan(eta/2.);
-       eta=-TMath::Log(eta);
-       fhetaTracklets->Fill(eta);    
-       fhphiTracklets->Fill(fTracklets[fNTracklets][1]);    
-      }
-      
-      AliDebug(1,Form(" Adding tracklet candidate %d ", fNTracklets));
-      AliDebug(1,Form(" Cl. %d of Layer 1 and %d of Layer 2", iC1, 
-                     iC2WithBestDist));
-      fNTracklets++;
+    if (partners[iC2] == -1) continue;
 
-      if (fRemoveClustersFromOverlaps) FlagClustersInOverlapRegions (iC1,iC2WithBestDist);
+    if (fOverlapFlagClustersLay1[partners[iC2]] || fOverlapFlagClustersLay2[iC2]) continue;
+    if (fRemoveClustersFromOverlaps) FlagClustersInOverlapRegions (partners[iC2],iC2);
 
+    fTracklets[fNTracklets] = new Float_t[6];
+  
+    // use the theta from the clusters in the first layer
+    fTracklets[fNTracklets][0] = fClustersLay1[partners[iC2]][0];
+    // use the phi from the clusters in the first layer
+    fTracklets[fNTracklets][1] = fClustersLay1[partners[iC2]][1];
+    // store the difference between phi1 and phi2
+    fTracklets[fNTracklets][2] = fClustersLay1[partners[iC2]][1] - fClustersLay2[iC2][1];
+
+    // define dphi in the range [0,pi] with proper sign (track charge correlated)
+    if (fTracklets[fNTracklets][2] > TMath::Pi())
+      fTracklets[fNTracklets][2] = fTracklets[fNTracklets][2]-2.*TMath::Pi();
+    if (fTracklets[fNTracklets][2] < -TMath::Pi())
+      fTracklets[fNTracklets][2] = fTracklets[fNTracklets][2]+2.*TMath::Pi();
+
+    // store the difference between theta1 and theta2
+    fTracklets[fNTracklets][3] = fClustersLay1[partners[iC2]][0] - fClustersLay2[iC2][0];
+
+    if (fHistOn) {
+      fhClustersDPhiAcc->Fill(fTracklets[fNTracklets][2]); 
+      fhClustersDThetaAcc->Fill(fTracklets[fNTracklets][3]);    
+      fhDPhiVsDThetaAcc->Fill(fTracklets[fNTracklets][3],fTracklets[fNTracklets][2]);
     }
 
-    // Delete the following else if you do not want to save Clusters! 
+    // find label
+    // if equal label in both clusters found this label is assigned
+    // if no equal label can be found the first labels of the L1 AND L2 cluster are assigned
+    Int_t label1 = 0;
+    Int_t label2 = 0;
+    while (label2 < 3) {
+      if ((Int_t) fClustersLay1[partners[iC2]][3+label1] != -2 && (Int_t) fClustersLay1[partners[iC2]][3+label1] == (Int_t) fClustersLay2[iC2][3+label2])
+        break;
+      label1++;
+      if (label1 == 3) {
+        label1 = 0;
+        label2++;
+      }
+    }
+    if (label2 < 3) {
+      AliDebug(AliLog::kDebug, Form("Found label %d == %d for tracklet candidate %d\n", (Int_t) fClustersLay1[partners[iC2]][3+label1], (Int_t) fClustersLay2[iC2][3+label2], fNTracklets));
+      fTracklets[fNTracklets][4] = fClustersLay1[partners[iC2]][3+label1];
+      fTracklets[fNTracklets][5] = fClustersLay2[iC2][3+label2];
+    } else {
+      AliDebug(AliLog::kDebug, Form("Did not find label %d %d %d %d %d %d for tracklet candidate %d\n", (Int_t) fClustersLay1[partners[iC2]][3], (Int_t) fClustersLay1[partners[iC2]][4], (Int_t) fClustersLay1[partners[iC2]][5], (Int_t) fClustersLay2[iC2][3], (Int_t) fClustersLay2[iC2][4], (Int_t) fClustersLay2[iC2][5], fNTracklets));
+      fTracklets[fNTracklets][4] = fClustersLay1[partners[iC2]][3];
+      fTracklets[fNTracklets][5] = fClustersLay2[iC2][3];
+    }
+
+    if (fHistOn) {
+      Float_t eta=fTracklets[fNTracklets][0];
+      eta= TMath::Tan(eta/2.);
+      eta=-TMath::Log(eta);
+      fhetaTracklets->Fill(eta);
+      fhphiTracklets->Fill(fTracklets[fNTracklets][1]);
+    }
 
-    else { // This means that the cluster has not been associated
+    AliDebug(1,Form(" Adding tracklet candidate %d ", fNTracklets));
+    AliDebug(1,Form(" Cl. %d of Layer 1 and %d of Layer 2", partners[iC2], iC2));
+    fNTracklets++;
 
-      // store the cluster
-       
+    associatedLay1[partners[iC2]] = 1;
+  }
+  
+  // Delete the following else if you do not want to save Clusters! 
+  // store the cluster
+  for (Int_t iC1=0; iC1<fNClustersLay1; iC1++) {
+    if (associatedLay1[iC1]==2||associatedLay1[iC1]==0) { 
+      fSClusters[fNSingleCluster] = new Float_t[2];
       fSClusters[fNSingleCluster][0] = fClustersLay1[iC1][0];
       fSClusters[fNSingleCluster][1] = fClustersLay1[iC1][1];
       AliDebug(1,Form(" Adding a single cluster %d (cluster %d  of layer 1)",
-                     fNSingleCluster, iC1));
+                fNSingleCluster, iC1));
       fNSingleCluster++;
     }
+  }
+
+  delete[] partners;
+  delete[] minDists;
+
+  for (Int_t i=0; i<fNClustersLay1; i++)
+    if (blacklist[i])
+      delete blacklist[i];
+  delete[] blacklist;
 
-  } // end of loop over clusters in layer 1
-  
   AliDebug(1,Form("%d tracklets found", fNTracklets));
 }
 
@@ -500,21 +548,64 @@ AliITSMultReconstructor::LoadClusterArrays(TTree* itsClusterTree) {
   fNFiredChips[0] = 0;
   fNFiredChips[1] = 0;
   
-  AliITSsegmentationSPD *seg = new AliITSsegmentationSPD();
+  AliITSsegmentationSPD seg;
 
-  TClonesArray* itsClusters = new TClonesArray("AliITSRecPoint");
-  TBranch* itsClusterBranch=itsClusterTree->GetBranch("ITSRecPoints");
-
-  itsClusterBranch->SetAddress(&itsClusters);
-
-  Int_t nItsSubs = (Int_t)itsClusterTree->GetEntries();  
+  AliITSRecPointContainer* rpcont=AliITSRecPointContainer::Instance();
+  TClonesArray* itsClusters=rpcont->FetchClusters(0,itsClusterTree);
+  if(!rpcont->IsSPDActive()){
+    AliWarning("No SPD rec points found, multiplicity not calculated");
+    return;
+  } 
   Float_t cluGlo[3]={0.,0.,0.};
  
+
+  // count clusters
+  // loop over the SPD subdetectors
+  Int_t nSPDL1 = AliITSgeomTGeo::GetModuleIndex(2,1,1);
+  for (Int_t iIts=0; iIts < nSPDL1; iIts++) {
+    itsClusters=rpcont->UncheckedGetClusters(iIts);
+    fNClustersLay1 += itsClusters->GetEntriesFast();
+  }
+  Int_t nSPDL2=AliITSgeomTGeo::GetModuleIndex(3,1,1);
+  for (Int_t iIts=nSPDL1; iIts < nSPDL2; iIts++) {
+    itsClusters=rpcont->UncheckedGetClusters(iIts);
+    fNClustersLay2 += itsClusters->GetEntriesFast();
+  }
+  
+  // create arrays
+  fClustersLay1              = new Float_t*[fNClustersLay1];
+  fDetectorIndexClustersLay1 = new Int_t[fNClustersLay1];
+  fOverlapFlagClustersLay1   = new Bool_t[fNClustersLay1];
+  
+  fClustersLay2              = new Float_t*[fNClustersLay2];
+  fDetectorIndexClustersLay2 = new Int_t[fNClustersLay2];
+  fOverlapFlagClustersLay2   = new Bool_t[fNClustersLay2];
+  
+  // no double association allowed
+  fTracklets                 = new Float_t*[TMath::Min(fNClustersLay1, fNClustersLay2)];
+  fSClusters                 = new Float_t*[fNClustersLay1];
+  
+  for (Int_t i=0; i<fNClustersLay1; i++) {
+    fClustersLay1[i]       = new Float_t[6];
+    fOverlapFlagClustersLay1[i]   = kFALSE;
+    fSClusters[i] = 0;
+  } 
+  for (Int_t i=0; i<fNClustersLay2; i++) {
+    fClustersLay2[i]       = new Float_t[6];
+    fOverlapFlagClustersLay2[i]   = kFALSE;
+  } 
+  for (Int_t i=0; i<TMath::Min(fNClustersLay1, fNClustersLay2); i++)
+    fTracklets[i] = 0;
+    
+  // fill clusters
   // loop over the its subdetectors
-  for (Int_t iIts=0; iIts < nItsSubs; iIts++) {
+  fNClustersLay1 = 0; // reset to 0
+  fNClustersLay2 = 0;
+  for (Int_t iIts=0; iIts < nSPDL2; iIts++) {
     
-    if (!itsClusterTree->GetEvent(iIts)) 
-      continue;
+    itsClusters=rpcont->UncheckedGetClusters(iIts);
     
     Int_t nClusters = itsClusters->GetEntriesFast();
 
@@ -536,7 +627,7 @@ AliITSMultReconstructor::LoadClusterArrays(TTree* itsClusterTree) {
 
       // find the chip for the current cluster
       Float_t locz = cluster->GetDetLocalZ();
-      Int_t iChip = seg->GetChipFromLocal(0,locz);
+      Int_t iChip = seg.GetChipFromLocal(0,locz);
       nClustersInChip[iChip]++; 
       
       if (layer==0) {
@@ -565,19 +656,13 @@ AliITSMultReconstructor::LoadClusterArrays(TTree* itsClusterTree) {
     }// end of cluster loop
 
     // get number of fired chips in the current module
-    if(layer<2)
+
     for(Int_t ifChip=0; ifChip<5; ifChip++) {
       if(nClustersInChip[ifChip] >= 1)  fNFiredChips[layer]++;
     }
 
   } // end of its "subdetector" loop  
 
-  if (itsClusters) {
-    itsClusters->Delete();
-    delete itsClusters;
-    delete seg;
-    itsClusters = 0;
-  }
   AliDebug(1,Form("(clusters in layer 1 : %d,  layer 2: %d)",fNClustersLay1,fNClustersLay2));
   AliDebug(1,Form("(cluster-fired chips in layer 1 : %d,  layer 2: %d)",fNFiredChips[0],fNFiredChips[1]));
 }
@@ -593,21 +678,18 @@ AliITSMultReconstructor::LoadClusterFiredChips(TTree* itsClusterTree) {
   fNFiredChips[0] = 0;
   fNFiredChips[1] = 0;
   
-  AliITSsegmentationSPD *seg = new AliITSsegmentationSPD();
-
-  TClonesArray* itsClusters = new TClonesArray("AliITSRecPoint");
-  TBranch* itsClusterBranch=itsClusterTree->GetBranch("ITSRecPoints");
-
-  itsClusterBranch->SetAddress(&itsClusters);
+  AliITSsegmentationSPD seg;
+  AliITSRecPointContainer* rpcont=AliITSRecPointContainer::Instance();
+  TClonesArray* itsClusters=rpcont->FetchClusters(0,itsClusterTree);
+  if(!rpcont->IsSPDActive()){
+    AliWarning("No SPD rec points found, multiplicity not calculated");
+    return;
+  } 
 
-  Int_t nItsSubs = (Int_t)itsClusterTree->GetEntries();  
   // loop over the its subdetectors
-  for (Int_t iIts=0; iIts < nItsSubs; iIts++) {
-    
-    if (!itsClusterTree->GetEvent(iIts)) 
-      continue;
-    
+  Int_t nSPDmodules=AliITSgeomTGeo::GetModuleIndex(3,1,1);
+  for (Int_t iIts=0; iIts < nSPDmodules; iIts++) {
+    itsClusters=rpcont->UncheckedGetClusters(iIts);
     Int_t nClusters = itsClusters->GetEntriesFast();
 
     // number of clusters in each chip of the current module
@@ -623,25 +705,19 @@ AliITSMultReconstructor::LoadClusterFiredChips(TTree* itsClusterTree) {
 
       // find the chip for the current cluster
       Float_t locz = cluster->GetDetLocalZ();
-      Int_t iChip = seg->GetChipFromLocal(0,locz);
+      Int_t iChip = seg.GetChipFromLocal(0,locz);
       nClustersInChip[iChip]++; 
       
     }// end of cluster loop
 
     // get number of fired chips in the current module
-    if(layer<2)
     for(Int_t ifChip=0; ifChip<5; ifChip++) {
       if(nClustersInChip[ifChip] >= 1)  fNFiredChips[layer]++;
     }
 
   } // end of its "subdetector" loop  
   
-  if (itsClusters) {
-    itsClusters->Delete();
-    delete itsClusters;
-    delete seg;
-    itsClusters = 0;
-  }
+
   AliDebug(1,Form("(cluster-fired chips in layer 1 : %d,  layer 2: %d)",fNFiredChips[0],fNFiredChips[1]));
 }
 //____________________________________________________________________
@@ -655,15 +731,11 @@ AliITSMultReconstructor::SaveHists() {
 
   fhClustersDPhiAll->Write();
   fhClustersDThetaAll->Write();
-  fhClustersDZetaAll->Write();
   fhDPhiVsDThetaAll->Write();
-  fhDPhiVsDZetaAll->Write();
 
   fhClustersDPhiAcc->Write();
   fhClustersDThetaAcc->Write();
-  fhClustersDZetaAcc->Write();
   fhDPhiVsDThetaAcc->Write();
-  fhDPhiVsDZetaAcc->Write();
 
   fhetaTracklets->Write();
   fhphiTracklets->Write();