]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ITS/AliITSVertexer3D.cxx
Bug fix - Jira ticket ALIROOT-5665
[u/mrichter/AliRoot.git] / ITS / AliITSVertexer3D.cxx
index 00d54724a91bcdfc95d9dc6af9f6d284763c5c59..1cdf52eb4ee75f8595140fd264eef6aec2b90359 100644 (file)
 // p-p collisions
 ////////////////////////////////////////////////////////////////
 
-const Int_t    AliITSVertexer3D::fgkMaxNumOfClDefault = 500;
+const Int_t    AliITSVertexer3D::fgkMaxNumOfClDefault = 300;
+const Int_t    AliITSVertexer3D::fgkMaxNumOfClRebinDefault = 500;
+const Int_t    AliITSVertexer3D::fgkMaxNumOfClDownscaleDefault = 1000;
+const Float_t  AliITSVertexer3D::fgk3DBinSizeDefault = 0.1;
 
 ClassImp(AliITSVertexer3D)
 
 /* $Id$ */
 
 //______________________________________________________________________
-AliITSVertexer3D::AliITSVertexer3D():
+AliITSVertexer3D::AliITSVertexer3D(Double_t zcut):
   AliITSVertexer(),
   fLines("AliStrLine",1000),
   fVert3D(),
@@ -63,7 +66,17 @@ AliITSVertexer3D::AliITSVertexer3D():
   fBinSizeR(0.),
   fBinSizeZ(0.),
   fPileupAlgo(0),
-  fMaxNumOfCl(fgkMaxNumOfClDefault)
+  fMaxNumOfCl(fgkMaxNumOfClDefault),
+  fMaxNumOfClForRebin(fgkMaxNumOfClRebinDefault),
+  fMaxNumOfClForDownScale(fgkMaxNumOfClDownscaleDefault),
+  fNRecPLay1(0),
+  fNRecPLay2(0),
+  f3DBinSize(fgk3DBinSizeDefault),
+  fDoDownScale(kFALSE),
+  fGenerForDownScale(0),
+  f3DPeak(),
+  fHighMultAlgo(1),
+  fSwitchAlgorithm(kFALSE)
 {
   // Default constructor
   SetCoarseDiffPhiCut();
@@ -72,7 +85,12 @@ AliITSVertexer3D::AliITSVertexer3D():
   SetCoarseMaxRCut();
   SetMaxRCut();
   SetMaxRCutAlgo2();
-  SetZCutDiamond();
+  if(zcut>0.){
+    SetZCutDiamond(zcut);
+  }
+  else {
+    SetZCutDiamond();
+  }
   SetMaxZCut();
   SetDCACut();
   SetDiffPhiMax();
@@ -83,9 +101,48 @@ AliITSVertexer3D::AliITSVertexer3D():
   SetPileupAlgo();
   SetBinSizeR();
   SetBinSizeZ();
-  Double_t binsize=0.02; // default 200 micron
-  Int_t nbins=static_cast<Int_t>(1+2*fZCutDiamond/binsize);
-  fZHisto=new TH1F("hz","",nbins,-fZCutDiamond,-fZCutDiamond+binsize*nbins);
+  fGenerForDownScale=new TRandom3(987654321);
+}
+
+//______________________________________________________________________
+AliITSVertexer3D::AliITSVertexer3D(TRootIOCtor*):
+  AliITSVertexer(),
+  fLines("AliStrLine",1000),
+  fVert3D(),
+  fCoarseDiffPhiCut(0.),
+  fFineDiffPhiCut(0.),
+  fCutOnPairs(0.),
+  fCoarseMaxRCut(0.),
+  fMaxRCut(0.),
+  fMaxRCut2(0.),
+  fZCutDiamond(0.),
+  fMaxZCut(0.),
+  fDCAcut(0.),
+  fDiffPhiMax(0.),
+  fMeanPSelTrk(0.),
+  fMeanPtSelTrk(0.),
+  fUsedCluster(kMaxCluPerMod*kNSPDMod),
+  fZHisto(0),
+  fDCAforPileup(0.),
+  fDiffPhiforPileup(0.),
+  fBinSizeR(0.),
+  fBinSizeZ(0.),
+  fPileupAlgo(0),
+  fMaxNumOfCl(fgkMaxNumOfClDefault),
+  fMaxNumOfClForRebin(fgkMaxNumOfClRebinDefault),
+  fMaxNumOfClForDownScale(fgkMaxNumOfClDownscaleDefault),
+  fNRecPLay1(0),
+  fNRecPLay2(0),
+  f3DBinSize(fgk3DBinSizeDefault),
+  fDoDownScale(kFALSE),
+  fGenerForDownScale(0),
+  f3DPeak(),
+  fHighMultAlgo(1),
+  fSwitchAlgorithm(kFALSE)
+{
+  // I/O constructor
+
+
 }
 
 //______________________________________________________________________
@@ -93,11 +150,12 @@ AliITSVertexer3D::~AliITSVertexer3D() {
   // Destructor
   fLines.Clear("C");
   if(fZHisto) delete fZHisto;
+  if(fGenerForDownScale) delete fGenerForDownScale;
 }
 
 //______________________________________________________________________
 void AliITSVertexer3D::ResetVert3D(){
-  //
+  // Reset the fVert3D object and reset the used clusters
   ResetVertex();
   fVert3D.SetXv(0.);
   fVert3D.SetYv(0.);
@@ -117,15 +175,20 @@ AliESDVertex* AliITSVertexer3D::FindVertexForCurrentEvent(TTree *itsClusterTree)
   Int_t nolines = FindTracklets(itsClusterTree,0);
   Int_t rc;
   if(nolines>=2){
-    rc=Prepare3DVertex(0);
-    if(fVert3D.GetNContributors()>0){
-      fLines.Clear("C");
-      nolines = FindTracklets(itsClusterTree,1);
-      if(nolines>=2){
-       rc=Prepare3DVertex(1);
-       if(fPileupAlgo == 2 && rc == 0) FindVertex3DIterative();
-       else if(fPileupAlgo<2 && rc == 0) FindVertex3D(itsClusterTree);
-       if(rc!=0) fVert3D.SetNContributors(0); // exclude this vertex      
+    if(fSwitchAlgorithm) {
+      rc = Prepare3DVertexPbPb();
+      FindVertex3D(itsClusterTree);
+    } else {
+      rc=Prepare3DVertex(0);
+      if(fVert3D.GetNContributors()>0){
+       fLines.Clear("C");
+       nolines = FindTracklets(itsClusterTree,1);
+       if(nolines>=2){
+         rc=Prepare3DVertex(1);
+         if(fPileupAlgo == 2 && rc == 0) FindVertex3DIterative();
+         else if(fPileupAlgo!=2 && rc == 0) FindVertex3D(itsClusterTree);
+         if(rc!=0) fVert3D.SetNContributors(0); // exclude this vertex      
+       }
       }
     }
   }
@@ -138,28 +201,29 @@ AliESDVertex* AliITSVertexer3D::FindVertexForCurrentEvent(TTree *itsClusterTree)
     vertz.SetHighLimit(fZCutDiamond);
     AliESDVertex* vtxz = vertz.FindVertexForCurrentEvent(itsClusterTree);
     if(vtxz){
-      Double_t position[3]={GetNominalPos()[0],GetNominalPos()[1],vtxz->GetZv()};
+      Double_t position[3]={GetNominalPos()[0],GetNominalPos()[1],vtxz->GetZ()};
       Double_t covmatrix[6];
       vtxz->GetCovMatrix(covmatrix);
       Double_t chi2=99999.;
       Int_t    nContr=vtxz->GetNContributors();
       fCurrentVertex = new AliESDVertex(position,covmatrix,chi2,nContr);    
+      fCurrentVertex->SetDispersion(vtxz->GetDispersion());
       fCurrentVertex->SetTitle("vertexer: Z");
       fCurrentVertex->SetName("SPDVertexZ");
       delete vtxz;
     }
 
-  }
-  FindMultiplicity(itsClusterTree);
+  }  
+  if(fComputeMultiplicity) FindMultiplicity(itsClusterTree);
   return fCurrentVertex;
 }  
 
 //______________________________________________________________________
 void AliITSVertexer3D::FindVertex3D(TTree *itsClusterTree){
-  Double_t vRadius=TMath::Sqrt(fVert3D.GetXv()*fVert3D.GetXv()+fVert3D.GetYv()*fVert3D.GetYv());
+  // Instantiates the fCurrentVertex object. calle by FindVertexForCurrenEvent
+  Double_t vRadius=TMath::Sqrt(fVert3D.GetX()*fVert3D.GetX()+fVert3D.GetY()*fVert3D.GetY());
   if(vRadius<GetPipeRadius() && fVert3D.GetNContributors()>0){
-    Double_t position[3]={fVert3D.GetXv(),fVert3D.GetYv(),fVert3D.GetZv()};
+    Double_t position[3]={fVert3D.GetX(),fVert3D.GetY(),fVert3D.GetZ()};
     Double_t covmatrix[6];
     fVert3D.GetCovMatrix(covmatrix);
     Double_t chi2=99999.;
@@ -173,9 +237,11 @@ void AliITSVertexer3D::FindVertex3D(TTree *itsClusterTree){
     switch(fPileupAlgo){
     case 0: PileupFromZ(); break;
     case 1: FindOther3DVertices(itsClusterTree); break;
+    case 3: break; // no pileup algo  
     default: AliError("Wrong pileup algorithm"); break;
     }
     if(fNoVertices==1){
+      delete[] fVertArray;
       fVertArray = new AliESDVertex[1];
       fVertArray[0]=(*fCurrentVertex);   
     }
@@ -184,7 +250,7 @@ void AliITSVertexer3D::FindVertex3D(TTree *itsClusterTree){
 
 //______________________________________________________________________
 void AliITSVertexer3D::FindVertex3DIterative(){
-  //
+  // find vertex if fPileupAlgo == 2
 
   Int_t nLines=fLines.GetEntriesFast();
   Int_t maxPoints=nLines*(nLines-1)/2;
@@ -193,8 +259,8 @@ void AliITSVertexer3D::FindVertex3DIterative(){
   Double_t* zP=new Double_t[maxPoints];
   Int_t* index1=new Int_t[maxPoints];
   Int_t* index2=new Int_t[maxPoints];
-  Double_t xbeam=fVert3D.GetXv();
-  Double_t ybeam=fVert3D.GetYv();
+  Double_t xbeam=fVert3D.GetX();
+  Double_t ybeam=fVert3D.GetY();
 
   Int_t iPoint=0;
   for(Int_t ilin1=0; ilin1<nLines; ilin1++){
@@ -364,12 +430,12 @@ void AliITSVertexer3D::FindVertex3DIterative(){
     if(nGoodVert > 1){
       fIsPileup = kTRUE;
       fNTrpuv = fVertArray[1].GetNContributors();
-      fZpuv = fVertArray[1].GetZv();
+      fZpuv = fVertArray[1].GetZ();
     }
     
-    Double_t vRadius=TMath::Sqrt(fVertArray[0].GetXv()*fVertArray[0].GetXv()+fVertArray[0].GetYv()*fVertArray[0].GetYv());
+    Double_t vRadius=TMath::Sqrt(fVertArray[0].GetX()*fVertArray[0].GetX()+fVertArray[0].GetY()*fVertArray[0].GetY());
     if(vRadius<GetPipeRadius() && fVertArray[0].GetNContributors()>0){
-      Double_t position[3]={fVertArray[0].GetXv(),fVertArray[0].GetYv(),fVertArray[0].GetZv()};
+      Double_t position[3]={fVertArray[0].GetX(),fVertArray[0].GetY(),fVertArray[0].GetZ()};
       Double_t covmatrix[6];
       fVertArray[0].GetCovMatrix(covmatrix);
       Double_t chi2=99999.;
@@ -434,12 +500,12 @@ void AliITSVertexer3D::FindVertex3DIterativeMM(){
        // at least one second vertex is present
        fIsPileup = kTRUE;
        fNTrpuv = fVertArray[kk].GetNContributors();
-       fZpuv = fVertArray[kk].GetZv();
+       fZpuv = fVertArray[kk].GetZ();
       }
     }
-    Double_t vRadius=TMath::Sqrt(fVertArray[0].GetXv()*fVertArray[0].GetXv()+fVertArray[0].GetYv()*fVertArray[0].GetYv());
+    Double_t vRadius=TMath::Sqrt(fVertArray[0].GetX()*fVertArray[0].GetX()+fVertArray[0].GetY()*fVertArray[0].GetY());
     if(vRadius<GetPipeRadius() && fVertArray[0].GetNContributors()>0){
-      Double_t position[3]={fVertArray[0].GetXv(),fVertArray[0].GetYv(),fVertArray[0].GetZv()};
+      Double_t position[3]={fVertArray[0].GetX(),fVertArray[0].GetY(),fVertArray[0].GetZ()};
       Double_t covmatrix[6];
       fVertArray[0].GetCovMatrix(covmatrix);
       Double_t chi2=99999.;
@@ -474,14 +540,14 @@ Int_t AliITSVertexer3D::FindTracklets(TTree *itsClusterTree, Int_t optCuts){
 
   TClonesArray *itsRec  = 0;
   if(optCuts==0) fZHisto->Reset();
- // gc1 are local and global coordinates for layer 1
 // gc1 are local and global coordinates for layer 1
   Float_t gc1f[3]={0.,0.,0.};
   Double_t gc1[3]={0.,0.,0.};
   // gc2 are local and global coordinates for layer 2
   Float_t gc2f[3]={0.,0.,0.};
   Double_t gc2[3]={0.,0.,0.};
   AliITSRecPointContainer* rpcont=AliITSRecPointContainer::Instance();
-  itsRec=rpcont->FetchClusters(0,itsClusterTree);
+  rpcont->FetchClusters(0,itsClusterTree);
   if(!rpcont->IsSPDActive()){
     AliWarning("No SPD rec points found, 3D vertex not calculated");
     return -1;
@@ -495,9 +561,9 @@ Int_t AliITSVertexer3D::FindTracklets(TTree *itsClusterTree, Int_t optCuts){
   Double_t deltaR=fCoarseMaxRCut;
   Double_t dZmax=fZCutDiamond;
   if(optCuts==1){
-    xbeam=fVert3D.GetXv();
-    ybeam=fVert3D.GetYv();
-    zvert=fVert3D.GetZv();
+    xbeam=fVert3D.GetX();
+    ybeam=fVert3D.GetY();
+    zvert=fVert3D.GetZ();
     deltaPhi = fDiffPhiMax; 
     deltaR=fMaxRCut;
     dZmax=fMaxZCut;
@@ -506,37 +572,55 @@ Int_t AliITSVertexer3D::FindTracklets(TTree *itsClusterTree, Int_t optCuts){
       deltaR=fMaxRCut2;
     }
   } else if(optCuts==2){
-    xbeam=fVert3D.GetXv();
-    ybeam=fVert3D.GetYv();
+    xbeam=fVert3D.GetX();
+    ybeam=fVert3D.GetY();
     deltaPhi = fDiffPhiforPileup;
     deltaR=fMaxRCut;
   }
 
-  Int_t nrpL1 = 0;    // number of rec points on layer 1
-  Int_t nrpL2 = 0;    // number of rec points on layer 2
-
-  // By default irstL1=0 and lastL1=79
-  Int_t firstL1 = AliITSgeomTGeo::GetModuleIndex(1,1,1);
-  Int_t lastL1 = AliITSgeomTGeo::GetModuleIndex(2,1,1)-1;
-  for(Int_t module= firstL1; module<=lastL1;module++){  // count number of recopints on layer 1
-    itsRec=rpcont->UncheckedGetClusters(module);
-    nrpL1+= itsRec->GetEntries();
-  }
-  //By default firstL2=80 and lastL2=239
-  Int_t firstL2 = AliITSgeomTGeo::GetModuleIndex(2,1,1);
-  Int_t lastL2 = AliITSgeomTGeo::GetModuleIndex(3,1,1)-1;
-  for(Int_t module= firstL2; module<=lastL2;module++){  // count number of recopints on layer 2
-    itsRec=rpcont->UncheckedGetClusters(module);
-    nrpL2+= itsRec->GetEntries();
-  }
-  if(nrpL1 == 0 || nrpL2 == 0){
-    AliDebug(1,Form("No RecPoints in at least one SPD layer (%d %d)",nrpL1,nrpL2));
+  fNRecPLay1=rpcont->GetNClustersInLayerFast(1);
+  fNRecPLay2=rpcont->GetNClustersInLayerFast(2);
+  if(fNRecPLay1 == 0 || fNRecPLay2 == 0){
+    AliDebug(1,Form("No RecPoints in at least one SPD layer (%d %d)",fNRecPLay1,fNRecPLay2));
     return -1;
   }
-  AliDebug(1,Form("RecPoints on Layer 1,2 = %d, %d\n",nrpL1,nrpL2));
-  if(nrpL1>fMaxNumOfCl || nrpL2>fMaxNumOfCl){
-    AliWarning(Form("Too many recpoints on SPD(%d %d ), call vertexerZ",nrpL1,nrpL2));
-    return -1;
+  AliDebug(1,Form("RecPoints on Layer 1,2 = %d, %d\n",fNRecPLay1,fNRecPLay2));
+  fDoDownScale=kFALSE;
+  fSwitchAlgorithm=kFALSE;
+
+  Float_t factDownScal=1.;
+  Int_t origLaddersOnLayer2=fLadOnLay2;
+
+  switch(fHighMultAlgo){
+  case 0: 
+    if(fNRecPLay1>fMaxNumOfClForDownScale || fNRecPLay2>fMaxNumOfClForDownScale){
+      if(optCuts==2) return -1; // do not try to search for pileup
+      SetLaddersOnLayer2(2);
+      fDoDownScale=kTRUE;
+      factDownScal=(Float_t)fMaxNumOfClForDownScale*(Float_t)fMaxNumOfClForDownScale/(Float_t)fNRecPLay1/(Float_t)fNRecPLay2;
+      if(optCuts==1){
+       factDownScal*=(fCoarseDiffPhiCut/fDiffPhiMax)*10;
+       if(factDownScal>1.){
+         fDoDownScale=kFALSE;
+         SetLaddersOnLayer2(origLaddersOnLayer2);
+       }
+      }
+      if(fDoDownScale)AliDebug(1,Form("Too many recpoints on SPD(%d %d ), downscale by %f",fNRecPLay1,fNRecPLay2,factDownScal));
+    }
+    break;
+  case 1: 
+    if(fNRecPLay1>fMaxNumOfCl || fNRecPLay2>fMaxNumOfCl) {
+      if(optCuts==2) return -1; // do not try to search for pileup
+      fSwitchAlgorithm=kTRUE;
+    }
+    break;
+  default: break; // no pileup algo  
+  }
+
+  if(!fDoDownScale && !fSwitchAlgorithm){
+    if(fNRecPLay1>fMaxNumOfClForRebin || fNRecPLay2>fMaxNumOfClForRebin){
+      SetLaddersOnLayer2(2);
+    }
   }
 
   Double_t a[3]={xbeam,ybeam,0.}; 
@@ -547,15 +631,21 @@ Int_t AliITSVertexer3D::FindTracklets(TTree *itsClusterTree, Int_t optCuts){
 
   Int_t nolines = 0;
   // Loop on modules of layer 1
+  Int_t firstL1 = TMath::Max(0,AliITSgeomTGeo::GetModuleIndex(1,1,1));
+  Int_t lastL1 = AliITSgeomTGeo::GetModuleIndex(2,1,1)-1;
   for(Int_t modul1= firstL1; modul1<=lastL1;modul1++){   // Loop on modules of layer 1
     if(!fUseModule[modul1]) continue;
-    UShort_t ladder=int(modul1/4)+1; // ladders are numbered starting from 1
+    
+    UShort_t ladder=modul1/4+1; // ladders are numbered starting from 1
     TClonesArray *prpl1=rpcont->UncheckedGetClusters(modul1);
     Int_t nrecp1 = prpl1->GetEntries();
     for(Int_t j=0;j<nrecp1;j++){
       if(j>kMaxCluPerMod) continue;
       UShort_t idClu1=modul1*kMaxCluPerMod+j;
       if(fUsedCluster.TestBitNumber(idClu1)) continue;
+      if(fDoDownScale && !fSwitchAlgorithm){
+       if(fGenerForDownScale->Rndm()>factDownScal) continue;
+      }
       AliITSRecPoint *recp1 = (AliITSRecPoint*)prpl1->At(j);
       recp1->GetGlobalXYZ(gc1f);
       for(Int_t ico=0;ico<3;ico++)gc1[ico]=gc1f[ico];
@@ -567,6 +657,7 @@ Int_t AliITSVertexer3D::FindTracklets(TTree *itsClusterTree, Int_t optCuts){
          Int_t ladmod=fLadders[ladder-1]+ladl2;
          if(ladmod>AliITSgeomTGeo::GetNLadders(2)) ladmod=ladmod-AliITSgeomTGeo::GetNLadders(2);
          Int_t modul2=AliITSgeomTGeo::GetModuleIndex(2,ladmod,k+1);
+         if(modul2<0)continue;
          if(!fUseModule[modul2]) continue;
          itsRec=rpcont->UncheckedGetClusters(modul2);
          Int_t nrecp2 = itsRec->GetEntries();
@@ -574,6 +665,7 @@ Int_t AliITSVertexer3D::FindTracklets(TTree *itsClusterTree, Int_t optCuts){
            if(j2>kMaxCluPerMod) continue;
            UShort_t idClu2=modul2*kMaxCluPerMod+j2;
            if(fUsedCluster.TestBitNumber(idClu2)) continue;
+
            AliITSRecPoint *recp2 = (AliITSRecPoint*)itsRec->At(j2);
            recp2->GetGlobalXYZ(gc2f);
            for(Int_t ico=0;ico<3;ico++)gc2[ico]=gc2f[ico];
@@ -600,6 +692,7 @@ Int_t AliITSVertexer3D::FindTracklets(TTree *itsClusterTree, Int_t optCuts){
            Double_t deltaZ=cp[2]-zvert;
            if(TMath::Abs(deltaZ)>dZmax)continue;
 
+
            if(nolines == 0){
              if(fLines.GetEntriesFast()>0)fLines.Clear("C");
            }
@@ -649,6 +742,9 @@ Int_t AliITSVertexer3D::FindTracklets(TTree *itsClusterTree, Int_t optCuts){
       }
     }
   }
+
+  SetLaddersOnLayer2(origLaddersOnLayer2);
+
   if(nolines == 0)return -2;
   return nolines;
 }
@@ -657,16 +753,15 @@ Int_t AliITSVertexer3D::FindTracklets(TTree *itsClusterTree, Int_t optCuts){
 Int_t  AliITSVertexer3D::Prepare3DVertex(Int_t optCuts){
   // Finds the 3D vertex information using tracklets
   Int_t retcode = -1;
-
   Double_t xbeam=GetNominalPos()[0];
   Double_t ybeam=GetNominalPos()[1];
   Double_t zvert=0.;
   Double_t deltaR=fCoarseMaxRCut;
   Double_t dZmax=fZCutDiamond;
   if(optCuts==1){
-    xbeam=fVert3D.GetXv();
-    ybeam=fVert3D.GetYv();
-    zvert=fVert3D.GetZv();
+    xbeam=fVert3D.GetX();
+    ybeam=fVert3D.GetY();
+    zvert=fVert3D.GetZ();
     deltaR=fMaxRCut;
     dZmax=fMaxZCut;
     if(fPileupAlgo == 2){ 
@@ -674,11 +769,25 @@ Int_t  AliITSVertexer3D::Prepare3DVertex(Int_t optCuts){
       deltaR=fMaxRCut2;
     }
   }else if(optCuts==2){
-    xbeam=fVert3D.GetXv();
-    ybeam=fVert3D.GetYv();
+    xbeam=fVert3D.GetX();
+    ybeam=fVert3D.GetY();
     deltaR=fMaxRCut;
   }
 
+  Double_t origBinSizeR=fBinSizeR;
+  Double_t origBinSizeZ=fBinSizeZ;
+  Bool_t rebinned=kFALSE;
+  if(fDoDownScale){
+    SetBinSizeR(0.05);
+    SetBinSizeZ(0.05);
+    rebinned=kTRUE;
+  }else{
+    if(optCuts==0 && (fNRecPLay1>fMaxNumOfClForRebin || fNRecPLay2>fMaxNumOfClForRebin)){
+      SetBinSizeR(0.1);
+      SetBinSizeZ(0.2);
+      rebinned=kTRUE;
+    }
+  }
   Double_t rl=-fCoarseMaxRCut;
   Double_t rh=fCoarseMaxRCut;
   Double_t zl=-fZCutDiamond;
@@ -692,9 +801,10 @@ Int_t  AliITSVertexer3D::Prepare3DVertex(Int_t optCuts){
   TH3F *h3dcs = new TH3F("h3dcs","xyz distribution",nbrcs,rl,rh,nbrcs,rl,rh,nbzcs,zl,zh);
 
   // cleanup of the TCLonesArray of tracklets (i.e. fakes are removed)
-  Int_t *validate = new Int_t [fLines.GetEntriesFast()];
-  for(Int_t i=0; i<fLines.GetEntriesFast();i++)validate[i]=0;
-  for(Int_t i=0; i<fLines.GetEntriesFast()-1;i++){
+  Int_t vsiz = fLines.GetEntriesFast();
+  Int_t *validate = new Int_t [vsiz];
+  for(Int_t i=0; i<vsiz;i++)validate[i]=0;
+  for(Int_t i=0; i<vsiz-1;i++){
     AliStrLine *l1 = (AliStrLine*)fLines.At(i);
     for(Int_t j=i+1;j<fLines.GetEntriesFast();j++){
       AliStrLine *l2 = (AliStrLine*)fLines.At(j);
@@ -704,7 +814,7 @@ Int_t  AliITSVertexer3D::Prepare3DVertex(Int_t optCuts){
       Int_t retc = l1->Cross(l2,point);
       if(retc<0)continue;
       Double_t deltaZ=point[2]-zvert;
-     if(TMath::Abs(deltaZ)>dZmax)continue;
+      if(TMath::Abs(deltaZ)>dZmax)continue;
       Double_t rad=TMath::Sqrt(point[0]*point[0]+point[1]*point[1]);
       if(rad>fCoarseMaxRCut)continue;
       Double_t deltaX=point[0]-xbeam;
@@ -718,14 +828,14 @@ Int_t  AliITSVertexer3D::Prepare3DVertex(Int_t optCuts){
     }
   }
 
-
-
   Int_t numbtracklets=0;
-  for(Int_t i=0; i<fLines.GetEntriesFast();i++)if(validate[i]>=1)numbtracklets++;
+  for(Int_t i=0; i<vsiz;i++)if(validate[i]>=1)numbtracklets++;
   if(numbtracklets<2){
     delete [] validate; 
     delete h3d; 
     delete h3dcs; 
+    SetBinSizeR(origBinSizeR);
+    SetBinSizeZ(origBinSizeZ);
     return retcode; 
   }
 
@@ -740,6 +850,8 @@ Int_t  AliITSVertexer3D::Prepare3DVertex(Int_t optCuts){
   if(fPileupAlgo == 2 && optCuts==1){
     delete h3d; 
     delete h3dcs;     
+    SetBinSizeR(origBinSizeR);
+    SetBinSizeZ(origBinSizeZ);
     return 0;
   }
 
@@ -757,12 +869,15 @@ Int_t  AliITSVertexer3D::Prepare3DVertex(Int_t optCuts){
     FindPeaks(h3dcs,peak,ntrkl,ntimes);  
     binsizer=(rh-rl)/nbrcs;
     binsizez=(zh-zl)/nbzcs;
-    if(ntrkl==1 || ntimes>1){delete h3dcs; return retcode;}
+    if(ntrkl==1 || ntimes>1){
+      delete h3dcs; 
+      SetBinSizeR(origBinSizeR);
+      SetBinSizeZ(origBinSizeZ);
+      return retcode;
+    }
   }
   delete h3dcs;
 
-  //         Second selection loop
-
   Double_t bs=(binsizer+binsizez)/2.;
   for(Int_t i=0; i<fLines.GetEntriesFast();i++){
     AliStrLine *l1 = (AliStrLine*)fLines.At(i);
@@ -771,6 +886,67 @@ Int_t  AliITSVertexer3D::Prepare3DVertex(Int_t optCuts){
   fLines.Compress();
   AliDebug(1,Form("Number of tracklets (after 2nd compression) %d",fLines.GetEntriesFast()));
 
+  // Finer Histo in limited range in case of high mult.
+  if(rebinned){
+    SetBinSizeR(0.01);
+    SetBinSizeZ(0.01);
+    Double_t xl=peak[0]-0.3;
+    Double_t xh=peak[0]+0.3;
+    Double_t yl=peak[1]-0.3;
+    Double_t yh=peak[1]+0.3;
+    zl=peak[2]-0.5;
+    zh=peak[2]+0.5;
+    Int_t nbxfs=(Int_t)((xh-xl)/fBinSizeR+0.0001);
+    Int_t nbyfs=(Int_t)((yh-yl)/fBinSizeR+0.0001);
+    Int_t nbzfs=(Int_t)((zh-zl)/fBinSizeZ+0.0001);
+
+    TH3F *h3dfs = new TH3F("h3dfs","xyz distribution",nbxfs,xl,xh,nbyfs,yl,yh,nbzfs,zl,zh);
+    for(Int_t i=0; i<fLines.GetEntriesFast()-1;i++){
+      AliStrLine *l1 = (AliStrLine*)fLines.At(i);
+      for(Int_t j=i+1;j<fLines.GetEntriesFast();j++){
+       AliStrLine *l2 = (AliStrLine*)fLines.At(j);
+       Double_t dca=l1->GetDCA(l2);
+       if(dca > fDCAcut || dca<0.00001) continue;
+       Double_t point[3];
+       Int_t retc = l1->Cross(l2,point);
+       if(retc<0)continue;
+       Double_t deltaZ=point[2]-zvert;
+       if(TMath::Abs(deltaZ)>dZmax)continue;
+       Double_t rad=TMath::Sqrt(point[0]*point[0]+point[1]*point[1]);
+       if(rad>fCoarseMaxRCut)continue;
+       Double_t deltaX=point[0]-xbeam;
+       Double_t deltaY=point[1]-ybeam;
+       Double_t raddist=TMath::Sqrt(deltaX*deltaX+deltaY*deltaY);
+       if(raddist>deltaR)continue;
+       h3dfs->Fill(point[0],point[1],point[2]);
+      }
+    }
+    ntrkl=0;
+    ntimes=0;
+
+    Double_t newpeak[3]={0.,0.,0.};
+    FindPeaks(h3dfs,newpeak,ntrkl,ntimes);  
+    if(ntimes==1){
+      for(Int_t iCoo=0; iCoo<3; iCoo++) peak[iCoo]=newpeak[iCoo];
+      binsizer=fBinSizeR;
+      binsizez=fBinSizeZ;
+    }
+    delete h3dfs;
+    bs=(binsizer+binsizez)/2.;
+    for(Int_t i=0; i<fLines.GetEntriesFast();i++){
+      AliStrLine *l1 = (AliStrLine*)fLines.At(i);
+      if(l1->GetDistFromPoint(peak)>2.5*bs)fLines.RemoveAt(i);
+    }
+    fLines.Compress();
+    AliDebug(1,Form("Number of tracklets (after 3rd compression) %d",fLines.GetEntriesFast()));
+  }
+  SetBinSizeR(origBinSizeR);
+  SetBinSizeZ(origBinSizeZ);
+
+
+  //         Second selection loop
+
+
   if(fLines.GetEntriesFast()>1){
     retcode=0;
     //  find a first candidate for the primary vertex
@@ -778,10 +954,33 @@ Int_t  AliITSVertexer3D::Prepare3DVertex(Int_t optCuts){
     // make a further selection on tracklets based on this first candidate
     fVert3D.GetXYZ(peak);
     AliDebug(1,Form("FIRST V candidate: %f ; %f ; %f",peak[0],peak[1],peak[2]));
+    Int_t *validate2 = new Int_t [fLines.GetEntriesFast()];
+    for(Int_t i=0; i<fLines.GetEntriesFast();i++) validate2[i]=1; 
     for(Int_t i=0; i<fLines.GetEntriesFast();i++){
+      if(validate2[i]==0) continue; 
       AliStrLine *l1 = (AliStrLine*)fLines.At(i);
       if(l1->GetDistFromPoint(peak)> fDCAcut)fLines.RemoveAt(i);
+      if(optCuts==2){ // temporarily only for pileup
+       for(Int_t j=i+1; j<fLines.GetEntriesFast();j++){
+         AliStrLine *l2 = (AliStrLine*)fLines.At(j);
+         if(l1->GetDCA(l2)<0.00001){ 
+           Int_t delta1=(Int_t)l1->GetIdPoint(0)-(Int_t)l2->GetIdPoint(0);
+           Int_t delta2=(Int_t)l1->GetIdPoint(1)-(Int_t)l2->GetIdPoint(1);
+           Int_t deltamod1=(Int_t)l1->GetIdPoint(0)/kMaxCluPerMod
+             -(Int_t)l2->GetIdPoint(0)/kMaxCluPerMod;
+           Int_t deltamod2=(Int_t)l1->GetIdPoint(1)/kMaxCluPerMod
+             -(Int_t)l2->GetIdPoint(1)/kMaxCluPerMod;
+           // remove tracklets sharing a point
+           if( (delta1==0 && deltamod2==0)  || 
+               (delta2==0 && deltamod1==0)  ) validate2[j]=0; 
+         }
+       }
+      }
+    }
+    for(Int_t i=0; i<fLines.GetEntriesFast();i++){
+      if(validate2[i]==0)  fLines.RemoveAt(i);
     }
+    delete [] validate2;
     fLines.Compress();
     AliDebug(1,Form("Number of tracklets (after 3rd compression) %d",fLines.GetEntriesFast()));
     if(fLines.GetEntriesFast()>1){// this new tracklet selection is used
@@ -791,6 +990,90 @@ Int_t  AliITSVertexer3D::Prepare3DVertex(Int_t optCuts){
   return retcode;  
 }
 
+//________________________________________________________
+Int_t  AliITSVertexer3D::Prepare3DVertexPbPb(){
+  // Finds the 3D vertex information in Pb-Pb events using tracklets
+  AliDebug(1,"High multiplicity event.\n");
+
+  Int_t nxy=(Int_t)(2.*fCoarseMaxRCut/f3DBinSize);
+  Double_t xymi= -nxy*f3DBinSize/2.;
+  Double_t xyma= nxy*f3DBinSize/2.;
+  Int_t nz=(Int_t)(2.*fZCutDiamond/f3DBinSize);
+  Double_t zmi=-nz*f3DBinSize/2.;
+  Double_t zma=nz*f3DBinSize/2.;
+  Int_t nolines=fLines.GetEntriesFast();
+  TH3F *h3dv = new TH3F("h3dv","3d tracklets",nxy,xymi,xyma,nxy,xymi,xyma,nz,zmi,zma);
+  
+  for(Int_t itra=0; itra<nolines; itra++){
+    Double_t wei = GetFraction(itra);
+    //printf("tracklet %d ) - weight %f \n",itra,wei);
+    if(wei>1.e-6){
+      AliStrLine *str=(AliStrLine*)fLines.At(itra);
+      Double_t t1,t2;
+      if(str->GetParamAtRadius(fCoarseMaxRCut,t1,t2)){
+       do{
+         Double_t punt[3];
+         str->ComputePointAtT(t1,punt);
+         h3dv->Fill(punt[0],punt[1],punt[2],wei);
+         t1+=f3DBinSize/3.;
+       } while(t1<t2);
+      }
+    }
+  }
+  Int_t noftrk,noftim;
+  FindPeaks(h3dv,f3DPeak,noftrk,noftim); // arg: histo3d, peak, # of contrib., # of other peak with same magnitude
+  
+  
+  // Remove all the tracklets which are not passing near peak
+  
+  while(nolines--){
+    AliStrLine *str=(AliStrLine*)fLines.At(nolines);
+    Double_t dist = str->GetDistFromPoint(f3DPeak);
+    if(dist>(2.*f3DBinSize)) fLines.RemoveAt(nolines);
+    }
+  fLines.Compress();
+  nolines=fLines.GetEntriesFast();
+
+  delete h3dv;
+
+  Int_t *validate2 = new Int_t [fLines.GetEntriesFast()];
+  for(Int_t i=0; i<fLines.GetEntriesFast();i++) validate2[i]=1; 
+  for(Int_t i=0; i<fLines.GetEntriesFast();i++){
+    if(validate2[i]==0) continue; 
+    AliStrLine *l1 = (AliStrLine*)fLines.At(i);
+    if(l1->GetDistFromPoint(f3DPeak)> fDCAcut)fLines.RemoveAt(i);
+    for(Int_t j=i+1; j<fLines.GetEntriesFast();j++){
+      AliStrLine *l2 = (AliStrLine*)fLines.At(j);
+      if(l1->GetDCA(l2)<0.00001){ 
+       Int_t delta1=(Int_t)l1->GetIdPoint(0)-(Int_t)l2->GetIdPoint(0);
+       Int_t delta2=(Int_t)l1->GetIdPoint(1)-(Int_t)l2->GetIdPoint(1);
+       Int_t deltamod1=(Int_t)l1->GetIdPoint(0)/kMaxCluPerMod
+         -(Int_t)l2->GetIdPoint(0)/kMaxCluPerMod;
+       Int_t deltamod2=(Int_t)l1->GetIdPoint(1)/kMaxCluPerMod
+         -(Int_t)l2->GetIdPoint(1)/kMaxCluPerMod;
+       // remove tracklets sharing a point
+       if( (delta1==0 && deltamod2==0)  || 
+           (delta2==0 && deltamod1==0)  ) validate2[j]=0; 
+       
+      }
+    }
+  }
+  for(Int_t i=0; i<fLines.GetEntriesFast();i++){
+    if(validate2[i]==0)  fLines.RemoveAt(i);
+  }
+  
+  delete [] validate2;
+  fLines.Compress();
+
+  
+  AliDebug(1,Form("Number of tracklets (after 3rd compression) %d",fLines.GetEntriesFast()));
+
+  fVert3D=AliVertexerTracks::TrackletVertexFinder(&fLines,0); 
+  fVert3D.GetXYZ(f3DPeak);
+  
+  return 0;  
+}
+
 //________________________________________________________
 void AliITSVertexer3D::SetMeanPPtSelTracks(Double_t fieldTesla){
   // Sets mean values of Pt based on the field
@@ -813,6 +1096,19 @@ void AliITSVertexer3D::SetMeanPPtSelTracks(Double_t fieldTesla){
   }
 }
 
+//________________________________________________________
+void AliITSVertexer3D::SetZCutDiamond(Double_t zcut){
+  // The fiducial region along Z is set. The TH1 object pointed by 
+  // fZHisto is created 
+  if(TMath::AreEqualAbs(zcut,fZCutDiamond,1.e-10))return;
+  fZCutDiamond=zcut;
+  if(fZHisto) delete fZHisto;
+  Double_t binsize=0.02; // default 200 micron
+  Int_t nbins=static_cast<Int_t>(1+2*fZCutDiamond/binsize);
+  fZHisto=new TH1F("hz","",nbins,-fZCutDiamond,-fZCutDiamond+binsize*nbins);
+  fZHisto->SetDirectory(0);
+}
+
 //________________________________________________________
 void AliITSVertexer3D::FindPeaks(TH3F* histo, Double_t *peak, Int_t &nOfTracklets, Int_t &nOfTimes){
   // Finds bin with max contents in 3D histo of tracket intersections
@@ -884,7 +1180,7 @@ void AliITSVertexer3D::MarkUsedClusters(){
 //________________________________________________________
 Int_t AliITSVertexer3D::RemoveTracklets(){
   // Remove trackelts close to first found vertex
-  Double_t vert[3]={fVert3D.GetXv(),fVert3D.GetYv(),fVert3D.GetZv()};
+  Double_t vert[3]={fVert3D.GetX(),fVert3D.GetY(),fVert3D.GetZ()};
   Int_t nRemoved=0;
   for(Int_t i=0; i<fLines.GetEntriesFast();i++){
     AliStrLine *lin = (AliStrLine*)fLines.At(i);
@@ -903,28 +1199,35 @@ Int_t AliITSVertexer3D::RemoveTracklets(){
 //________________________________________________________
 void AliITSVertexer3D::FindOther3DVertices(TTree *itsClusterTree){
   // pileup identification based on 3D vertexing with not used clusters
-  MarkUsedClusters();
-  fLines.Clear("C");
-  Int_t nolines = FindTracklets(itsClusterTree,2);
-  if(nolines>=2){
-    Int_t nr=RemoveTracklets();
-    nolines-=nr;
+
+  fVertArray = new AliESDVertex[kMaxPileupVertices+1];
+  fVertArray[0]=(*fCurrentVertex);
+  Int_t nFoundVert=1;
+  for(Int_t iPilV=1; iPilV<=kMaxPileupVertices; iPilV++){
+    MarkUsedClusters();
+    fLines.Clear("C");
+    Int_t nolines = FindTracklets(itsClusterTree,2);
     if(nolines>=2){
-      Int_t rc=Prepare3DVertex(2);
-      if(rc==0){ 
-       fVert3D=AliVertexerTracks::TrackletVertexFinder(&fLines,0);
-       if(fVert3D.GetNContributors()>=fMinTrackletsForPilup){
-         fIsPileup=kTRUE;
-         fNoVertices=2;
-         fVertArray = new AliESDVertex[2];
-         fVertArray[0]=(*fCurrentVertex);
-         fVertArray[1]=fVert3D;
-         fZpuv=fVert3D.GetZv();
-         fNTrpuv=fVert3D.GetNContributors();
+      Int_t nr=RemoveTracklets();
+      nolines-=nr;
+      if(nolines>=2){
+       Int_t rc=Prepare3DVertex(2);
+       if(rc==0){ 
+         fVert3D=AliVertexerTracks::TrackletVertexFinder(&fLines,0);
+         if(fVert3D.GetNContributors()>=fMinTrackletsForPilup){
+           fIsPileup=kTRUE;
+           fVertArray[nFoundVert]=fVert3D;
+           nFoundVert++;
+           if(nFoundVert==2){
+             fZpuv=fVert3D.GetZ();
+             fNTrpuv=fVert3D.GetNContributors();
+           }
+         }
        }
       }
     }
   }
+  fNoVertices=nFoundVert;
 }
 //______________________________________________________________________
 void AliITSVertexer3D::PileupFromZ(){
@@ -957,6 +1260,54 @@ void AliITSVertexer3D::PileupFromZ(){
     }
   }
 }
+
+//________________________________________________________
+Double_t AliITSVertexer3D::GetFraction(Int_t itr) const {
+  // this method is used to fill a 3D histogram representing
+  // the trajectories of the candidate tracklets
+  // The computed fraction is used as a weight at filling time
+  AliStrLine *str = (AliStrLine*)fLines.At(itr);
+  Double_t spigolo=10.;
+  Double_t cd[3];
+  str->GetCd(cd);
+  Double_t par=0.;
+  Double_t maxl=TMath::Sqrt(3.)*spigolo;
+ // intersection with a plane normal to the X axis 
+  if(TMath::AreEqualAbs(cd[0],0.,1.e-9)){
+    par=1000000.;
+  }
+  else {
+    par=spigolo/cd[0];
+  }
+  Double_t zc=cd[2]*par;
+  Double_t yc=cd[1]*par;
+  if((-spigolo<=yc && yc<=spigolo) && (-spigolo<=zc && zc<=spigolo))return TMath::Abs(par/maxl);
+ // intersection with a plane normal to the Y axis
+  if(TMath::AreEqualAbs(cd[1],0.,1.e-9)){
+    par=1000000.;
+  }
+  else {
+    par=spigolo/cd[1];
+  }
+  zc=cd[2]*par;
+  Double_t xc=cd[0]*par;
+  if((-spigolo<=xc && xc<=spigolo) && (-spigolo<=zc && zc<=spigolo))return TMath::Abs(par/maxl);
+ // intersection with a plane normal to the Z axis
+  if(TMath::AreEqualAbs(cd[2],0.,1.e-9)){
+    par=1000000.;
+  }
+  else {
+    par=spigolo/cd[2];
+  }
+  yc=cd[1]*par;
+  xc=cd[0]*par;
+  if((-spigolo<=xc && xc<=spigolo) && (-spigolo<=yc && yc<=spigolo))return TMath::Abs(par/maxl);
+  // control should never reach the following lines
+  AliError(Form("anomalous tracklet direction for tracklet %d in fLines\n",itr));
+  str->PrintStatus();
+  return 0.;
+}
+
 //________________________________________________________
 void AliITSVertexer3D::PrintStatus() const {
   // Print current status
@@ -974,6 +1325,8 @@ void AliITSVertexer3D::PrintStatus() const {
   printf("Pileup algo: %d\n",fPileupAlgo);
   printf("Min DCA to 1st vertex for pileup (algo 0 and 1): %f\n",fDCAforPileup);
   printf("Cut on distance between pair-vertices  (algo 2): %f\n",fCutOnPairs);
-  printf("Maximum number of clusters allowed on L1 or L2: %d\n",fMaxNumOfCl);
+  printf("Maximum number of clusters on L1 or L2 for downscale: %d\n",fMaxNumOfClForDownScale);
+  printf("Maximum number of clusters on L1 or L2 for histo rebin: %d\n",fMaxNumOfClForRebin);
   printf("=======================================================\n");
 }
+