]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ITS/AliITSVertexerZ.cxx
remove props
[u/mrichter/AliRoot.git] / ITS / AliITSVertexerZ.cxx
index 8404c026639aa8ec1e4f92de7f50f6c05f56f5e5..ab333bd0cf00933a4ea52e3f88d95dda1fee1e05 100644 (file)
  * about the suitability of this software for any purpose. It is          *
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
-#include <AliITSVertexerZ.h>
-#include <TString.h>
-#include <AliITSLoader.h>
+#include "AliITSVertexerZ.h"
 #include<TBranch.h>
+#include<TClonesArray.h>
 #include<TH1.h>
+#include <TString.h>
 #include<TTree.h>
-#include <AliITSgeom.h>
-#include "AliITSDetTypeRec.h"
-#include <AliITSRecPoint.h>
+#include "AliESDVertex.h"
+#include "AliITSgeomTGeo.h"
+#include "AliITSRecPoint.h"
+#include "AliITSRecPointContainer.h"
+#include "AliITSZPoint.h"
 
 /////////////////////////////////////////////////////////////////
 // this class implements a fast method to determine
@@ -35,78 +37,76 @@ ClassImp(AliITSVertexerZ)
 
 
 //______________________________________________________________________
-AliITSVertexerZ::AliITSVertexerZ():AliITSVertexer() {
+AliITSVertexerZ::AliITSVertexerZ():AliITSVertexer(),
+fFirstL1(0),
+fLastL1(0),
+fFirstL2(0),
+fLastL2(0),
+fDiffPhiMax(0),
+fZFound(0),
+fZsig(0.),
+fZCombc(0),
+fLowLim(0.),
+fHighLim(0.),
+fStepCoarse(0),
+fTolerance(0.),
+fMaxIter(0),
+fWindowWidth(0) {
   // Default constructor
-  SetDiffPhiMax(0);
-  fX0 = 0.;
-  fY0 = 0.;
-  SetFirstLayerModules(0);
-  SetSecondLayerModules(0);
-  fZFound = 0;
-  fZsig = 0.;
-  fZCombc = 0;
-  fZCombf = 0;
-  fZCombv = 0;
-  SetLowLimit(0.);
-  SetHighLimit(0.);
-  SetBinWidthCoarse(0.);
-  SetBinWidthFine(0.);
-  SetTolerance(0.);
-  SetPPsetting(0.,0.);
+  SetDiffPhiMax();
+  SetFirstLayerModules();
+  SetSecondLayerModules();
+  SetLowLimit();
+  SetHighLimit();
+  SetBinWidthCoarse();
+  SetTolerance();
+  SetPPsetting();
   ConfigIterations();
+  SetWindowWidth();
 }
 
 //______________________________________________________________________
-AliITSVertexerZ::AliITSVertexerZ(TString fn, Float_t x0, Float_t y0):AliITSVertexer(fn) {
+AliITSVertexerZ::AliITSVertexerZ(Float_t x0, Float_t y0):AliITSVertexer(),
+fFirstL1(0),
+fLastL1(0),
+fFirstL2(0),
+fLastL2(0),
+fDiffPhiMax(0),
+fZFound(0),
+fZsig(0.),
+fZCombc(0),
+fLowLim(0.),
+fHighLim(0.),
+fStepCoarse(0),
+fTolerance(0.),
+fMaxIter(0),
+fWindowWidth(0) {
   // Standard Constructor
   SetDiffPhiMax();
-  fX0 = x0;
-  fY0 = y0;
   SetFirstLayerModules();
   SetSecondLayerModules();
-  fZFound = 0;
-  fZsig = 0.;
-  fZCombc = 0;
-  fZCombf = 0;
-  fZCombv = 0;
   SetLowLimit();
   SetHighLimit();
   SetBinWidthCoarse();
-  SetBinWidthFine();
   SetTolerance();
   SetPPsetting();
   ConfigIterations();
+  SetWindowWidth();
+  SetVtxStart((Double_t)x0,(Double_t)y0,0.);
 
 }
 
-//______________________________________________________________________
-AliITSVertexerZ::AliITSVertexerZ(const AliITSVertexerZ &vtxr) : AliITSVertexer(vtxr) {
-  // Copy constructor
-  // Copies are not allowed. The method is protected to avoid misuse.
-  Error("AliITSVertexerZ","Copy constructor not allowed\n");
-}
-
-//______________________________________________________________________
-AliITSVertexerZ& AliITSVertexerZ::operator=(const AliITSVertexerZ& /* vtxr */){
-  // Assignment operator
-  // Assignment is not allowed. The method is protected to avoid misuse.
-  Error("= operator","Assignment operator not allowed\n");
-  return *this;
-}
-
 //______________________________________________________________________
 AliITSVertexerZ::~AliITSVertexerZ() {
   // Destructor
   delete fZCombc;
-  delete fZCombf;
-  delete fZCombv;
 }
 
 //______________________________________________________________________
 void AliITSVertexerZ::ConfigIterations(Int_t noiter,Float_t *ptr){
   // configure the iterative procedure to gain efficiency for
   // pp events with very low multiplicity
-  Float_t defaults[5]={0.05,0.1,0.2,0.3,0.5};
+  Float_t defaults[5]={0.02,0.05,0.1,0.2,0.3};
   fMaxIter=noiter;
   if(noiter>5){
     Error("ConfigIterations","Maximum number of iterations is 5\n");
@@ -117,359 +117,302 @@ void AliITSVertexerZ::ConfigIterations(Int_t noiter,Float_t *ptr){
 }
 
 //______________________________________________________________________
-AliESDVertex* AliITSVertexerZ::FindVertexForCurrentEvent(Int_t evnumber){
+Int_t AliITSVertexerZ::GetPeakRegion(TH1F*h, Int_t &binmin, Int_t &binmax){
+  // Finds a region around a peak in the Z histogram
+  // Case of 2 peaks is treated 
+  Int_t imax=h->GetNbinsX();
+  Float_t maxval=0;
+  Int_t bi1=h->GetMaximumBin();
+  Int_t bi2=0;
+  for(Int_t i=imax;i>=1;i--){
+    if(h->GetBinContent(i)>maxval){
+      maxval=h->GetBinContent(i);
+      bi2=i;
+    }
+  }
+  Int_t npeaks=0;
+
+  if(bi1==bi2){
+    binmin=bi1-3;
+    binmax=bi1+3;
+    npeaks=1;
+  }else{
+    TH1F *copy = new TH1F(*h);
+    copy->SetBinContent(bi1,0.);
+    copy->SetBinContent(bi2,0.);
+    Int_t l1=TMath::Max(bi1-3,1);
+    Int_t l2=TMath::Min(bi1+3,h->GetNbinsX());
+    Float_t cont1=copy->Integral(l1,l2);
+    Int_t ll1=TMath::Max(bi2-3,1);
+    Int_t ll2=TMath::Min(bi2+3,h->GetNbinsX());
+    Float_t cont2=copy->Integral(ll1,ll2);
+    if(cont1>cont2){
+      binmin=l1;
+      binmax=l2;
+      npeaks=1;
+    }
+    if(cont2>cont1){
+      binmin=ll1;
+      binmax=ll2;
+      npeaks=1;
+    }
+    if(cont1==cont2){
+      binmin=l1;
+      binmax=ll2;
+      if(bi2-bi1==1) npeaks=1;
+      else npeaks=2;
+    }  
+    delete copy;
+  }    
+  return npeaks;
+}
+//______________________________________________________________________
+AliESDVertex* AliITSVertexerZ::FindVertexForCurrentEvent(TTree *itsClusterTree){
   // Defines the AliESDVertex for the current event
-  VertexZFinder(evnumber);
+  VertexZFinder(itsClusterTree);
   Int_t ntrackl=0;
   for(Int_t iteraz=0;iteraz<fMaxIter;iteraz++){
     if(fCurrentVertex) ntrackl=fCurrentVertex->GetNContributors();
     if(!fCurrentVertex || ntrackl==0 || ntrackl==-1){
       Float_t diffPhiMaxOrig=fDiffPhiMax;
       fDiffPhiMax=GetPhiMaxIter(iteraz);
-      VertexZFinder(evnumber);
+      VertexZFinder(itsClusterTree);
       fDiffPhiMax=diffPhiMaxOrig;
     }
   }
-  FindMultiplicity(evnumber);
+  if(fComputeMultiplicity) FindMultiplicity(itsClusterTree);
   return fCurrentVertex;
 }  
 
-
-
-
 //______________________________________________________________________
-void AliITSVertexerZ::VertexZFinder(Int_t evnumber){
+void AliITSVertexerZ::VertexZFinder(TTree *itsClusterTree){
   // Defines the AliESDVertex for the current event
   fCurrentVertex = 0;
-  AliRunLoader *rl =AliRunLoader::GetRunLoader();
-  AliITSLoader* itsLoader = (AliITSLoader*)rl->GetLoader("ITSLoader");
-  AliITSgeom* geom = itsLoader->GetITSgeom();
-  itsLoader->LoadRecPoints();
-  rl->GetEvent(evnumber);
-
-  AliITSDetTypeRec detTypeRec;
-
-  TTree *tR = itsLoader->TreeR();
-  detTypeRec.SetTreeAddressR(tR);
+  Double_t startPos[3]={GetNominalPos()[0],GetNominalPos()[1],GetNominalPos()[2]};
+  Double_t startCov[6]={GetNominalCov()[0],GetNominalCov()[1],GetNominalCov()[2],
+                       GetNominalCov()[3],GetNominalCov()[4],GetNominalCov()[5]};
+  ResetVertex();
   TClonesArray *itsRec  = 0;
-  // lc and gc are local and global coordinates for layer 1
-  Float_t lc[3]; for(Int_t ii=0; ii<3; ii++) lc[ii]=0.;
-  Float_t gc[3]; for(Int_t ii=0; ii<3; ii++) gc[ii]=0.;
+  // lc1 and gc1 are local and global coordinates for layer 1
+  Float_t gc1[3]={0.,0.,0.}; // ; for(Int_t ii=0; ii<3; ii++) gc1[ii]=0.;
   // lc2 and gc2 are local and global coordinates for layer 2
-  Float_t lc2[3]; for(Int_t ii=0; ii<3; ii++) lc2[ii]=0.;
-  Float_t gc2[3]; for(Int_t ii=0; ii<3; ii++) gc2[ii]=0.;
-
-  itsRec = detTypeRec.RecPoints();
-  TBranch *branch;
-  branch = tR->GetBranch("ITSRecPoints");
+  Float_t gc2[3]={0.,0.,0.}; //; for(Int_t ii=0; ii<3; ii++) gc2[ii]=0.;
+  AliITSRecPointContainer* rpcont=AliITSRecPointContainer::Instance();
+  rpcont->FetchClusters(0,itsClusterTree);
+  if(!rpcont->IsSPDActive()){
+    AliWarning("Null pointer for RecPoints branch, vertex not calculated");
+    ResetHistograms();
+    fCurrentVertex = new AliESDVertex(startPos,startCov,99999.,-2);
+    return;
+  }
 
   Int_t nrpL1 = 0;
   Int_t nrpL2 = 0;
-  // By default fFirstL1=0 and fLastL1=79
-  // This loop counts the number of recpoints on layer1 (central modules)
-  for(Int_t module= fFirstL1; module<=fLastL1;module++){
-    // Keep only central modules
-    if(module%4==0 || module%4==3)continue;
-    //   cout<<"Procesing module "<<module<<" ";
-    branch->GetEvent(module);
-    //    cout<<"Number of clusters "<<clusters->GetEntries()<<endl;
-    nrpL1+= itsRec->GetEntries();
-    detTypeRec.ResetRecPoints();
-  }
-  //By default fFirstL2=80 and fLastL2=239
-  //This loop counts the number of RP on layer 2
-  for(Int_t module= fFirstL2; module<=fLastL2;module++){
-    branch->GetEvent(module);
-    nrpL2+= itsRec->GetEntries();
-    detTypeRec.ResetRecPoints();
-  }
+  nrpL1=rpcont->GetNClustersInLayerFast(1);
+  nrpL2=rpcont->GetNClustersInLayerFast(2);
+
   if(nrpL1 == 0 || nrpL2 == 0){
+    AliDebug(1,Form("No RecPoints in at least one SPD layer (%d %d)",nrpL1,nrpL2));
     ResetHistograms();
-    itsLoader->UnloadRecPoints();
-    fCurrentVertex = new AliESDVertex(0.,5.3,-2);
+    fCurrentVertex = new AliESDVertex(startPos,startCov,99999.,-2);
     return;
   }
-  // The vertex finding is attempted only if the number of RP is !=0 on
-  // both layers
-  Float_t *xc1 = new Float_t [nrpL1]; // coordinates of the L1 Recpoints
-  Float_t *yc1 = new Float_t [nrpL1];
-  Float_t *zc1 = new Float_t [nrpL1];
-  Float_t *phi1 = new Float_t [nrpL1];
-  Float_t *xc2 = new Float_t [nrpL2]; // coordinates of the L1 Recpoints
-  Float_t *yc2 = new Float_t [nrpL2];
-  Float_t *zc2 = new Float_t [nrpL2];
-  Float_t *phi2 = new Float_t [nrpL2];
-  Int_t ind = 0;// running index for RP
   // Force a coarse bin size of 200 microns if the number of clusters on layer 2
   // is low
   if(nrpL2<fPPsetting[0])SetBinWidthCoarse(fPPsetting[1]);
-  // By default nbinfine = (10+10)/0.0005=40000
-  Int_t nbinfine = static_cast<Int_t>((fHighLim-fLowLim)/fStepFine);
   // By default nbincoarse=(10+10)/0.01=2000
   Int_t nbincoarse = static_cast<Int_t>((fHighLim-fLowLim)/fStepCoarse);
-  // Set stepverycoarse = 3*fStepCoarse
-  Int_t nbinvcoarse = static_cast<Int_t>((fHighLim-fLowLim)/(3.*fStepCoarse));
   if(fZCombc)delete fZCombc;
   fZCombc = new TH1F("fZCombc","Z",nbincoarse,fLowLim,fLowLim+nbincoarse*fStepCoarse);
-  if(fZCombv)delete fZCombv;
-  fZCombv = new TH1F("fZCombv","Z",nbinvcoarse,fLowLim,fLowLim+nbinvcoarse*3.*fStepCoarse);
-  if(fZCombf)delete fZCombf;
-  fZCombf = new TH1F("fZCombf","Z",nbinfine,fLowLim,fLowLim+nbinfine*fStepFine);
-  // Loop on modules of layer 1 (restricted to central modules)
-  for(Int_t module= fFirstL1; module<=fLastL1;module++){
-    if(module%4==0 || module%4==3)continue;
-    branch->GetEvent(module);
-    Int_t nrecp1 = itsRec->GetEntries();
-    for(Int_t j=0;j<nrecp1;j++){
-      AliITSRecPoint *recp = (AliITSRecPoint*)itsRec->At(j);
-      // Local coordinates of this recpoint
-      lc[0]=recp->GetDetLocalX();
-      lc[2]=recp->GetDetLocalZ();
-      geom->LtoG(module,lc,gc);
-      // Global coordinates of this recpoints
-      gc[0]-=fX0; // Possible beam offset in the bending plane
-      gc[1]-=fY0; //   "               "
-      xc1[ind]=gc[0];
-      yc1[ind]=gc[1];
-      zc1[ind]=gc[2];
-      // azimuthal angle is computed in the interval 0 --> 2*pi
-      phi1[ind] = TMath::ATan2(gc[1],gc[0]);
-      if(phi1[ind]<0)phi1[ind]=2*TMath::Pi()+phi1[ind];
-      ind++;
-    }
-    detTypeRec.ResetRecPoints();
-  }
-  ind = 0; // the running index is reset for Layer 2
-  for(Int_t module= fFirstL2; module<=fLastL2;module++){
-    branch->GetEvent(module);
-    Int_t nrecp2 = itsRec->GetEntries();
-    for(Int_t j=0;j<nrecp2;j++){
-      AliITSRecPoint *recp = (AliITSRecPoint*)itsRec->At(j);
-      lc[0]=recp->GetDetLocalX();
-      lc[2]=recp->GetDetLocalZ();
-      geom->LtoG(module,lc,gc);
-      gc[0]-=fX0;
-      gc[1]-=fY0;
-      xc2[ind]=gc[0];
-      yc2[ind]=gc[1];
-      zc2[ind]=gc[2];
-      phi2[ind] = TMath::ATan2(gc[1],gc[0]);
-      if(phi2[ind]<0)phi2[ind]=2*TMath::Pi()+phi2[ind];
-      ind++;
+
+ /* Test the ffect of mutiple scatternig on error. Negligible
+  // Multiple scattering
+  Float_t beta=1.,pmed=0.875; //pmed=875 MeV (for tracks with dphi<0.01 rad)
+  Float_t beta2=beta*beta;
+  Float_t p2=pmed*pmed;
+  Float_t rBP=3; //Beam Pipe radius = 3cm
+  Float_t dBP=0.08/35.3; // 800 um of Be
+  Float_t dL1=0.01; //approx. 1% of radiation length  
+  Float_t theta2BP=14.1*14.1/(beta2*p2*1e6)*TMath::Abs(dBP);
+  Float_t theta2L1=14.1*14.1/(beta2*p2*1e6)*TMath::Abs(dL1);
+*/
+  Int_t nEntriesMod[kNSPDMod];
+  TClonesArray* recpArr[kNSPDMod];
+  for(Int_t modul=0; modul<kNSPDMod; ++modul) {
+    if(!fUseModule[modul]) {
+      nEntriesMod[modul]=0;
+      recpArr[modul]=0;
+    } else {
+      recpArr[modul]=rpcont->UncheckedGetClusters(modul);
+      nEntriesMod[modul]=recpArr[modul]->GetEntriesFast();
     }
-    detTypeRec.ResetRecPoints();
   }
-  Int_t nolines=0;
-  for(Int_t i=0;i<nrpL1;i++){ // loop on L1 RP
-    Float_t r1=TMath::Sqrt(xc1[i]*xc1[i]+yc1[i]*yc1[i]); // radius L1 RP
-    for(Int_t j=0;j<nrpL2;j++){ // loop on L2 RP
-      Float_t diff = TMath::Abs(phi2[j]-phi1[i]); // diff in azimuth
-      if(diff>TMath::Pi())diff=2.*TMath::Pi()-diff; //diff<pi
-      if(diff<fDiffPhiMax){ // cut on 10 milliradians by def.
-       Float_t r2=TMath::Sqrt(xc2[j]*xc2[j]+yc2[j]*yc2[j]); // radius L2 RP
-       Float_t zr0=(r2*zc1[i]-r1*zc2[j])/(r2-r1); //Z @ null radius
-       fZCombf->Fill(zr0);
-       fZCombc->Fill(zr0);
-       fZCombv->Fill(zr0);
-       Double_t pA[3];
-       Double_t pB[3];
-       pA[0]=xc1[i];
-       pA[1]=yc1[i];
-       pA[2]=zc1[i];
-       pB[0]=xc2[j];
-       pB[1]=yc2[j];
-       pB[2]=zc2[j];
-       MakeTracklet(pA,pB,nolines);
+      
+  Int_t maxdim=TMath::Min(nrpL1*nrpL2,50000);  // temporary; to limit the size in PbPb
+  static TClonesArray points("AliITSZPoint",maxdim);
+  Int_t nopoints =0;
+  for(Int_t modul1= fFirstL1; modul1<=fLastL1;modul1++){   // Loop on modules of layer 1
+    if(!fUseModule[modul1]) continue;
+    UShort_t ladder=int(modul1/4)+1;  // ladders are numbered starting from 1
+    TClonesArray *prpl1=recpArr[modul1]; //rpcont->UncheckedGetClusters(modul1);
+    Int_t nrecp1 = nEntriesMod[modul1]; //prpl1->GetEntries();
+    for(Int_t j1=0;j1<nrecp1;j1++){
+      AliITSRecPoint *recp1 = (AliITSRecPoint*)prpl1->At(j1);
+      recp1->GetGlobalXYZ(gc1);
+      gc1[0]-=GetNominalPos()[0]; // Possible beam offset in the bending plane
+      gc1[1]-=GetNominalPos()[1]; //   "               "
+      Float_t phi1 = TMath::ATan2(gc1[1],gc1[0]);
+      if(phi1<0)phi1+=TMath::TwoPi();
+      for(Int_t ladl2=0 ; ladl2<fLadOnLay2*2+1;ladl2++){
+       for(Int_t k=0;k<4;k++){
+         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=recpArr[modul2]; // rpcont->UncheckedGetClusters(modul2);
+         Int_t nrecp2 = nEntriesMod[modul2]; // itsRec->GetEntries();
+         for(Int_t j2=0;j2<nrecp2;j2++){
+           AliITSRecPoint *recp2 = (AliITSRecPoint*)itsRec->At(j2);
+           recp2->GetGlobalXYZ(gc2);
+           gc2[0]-=GetNominalPos()[0];
+           gc2[1]-=GetNominalPos()[1];
+           Float_t phi2 = TMath::ATan2(gc2[1],gc2[0]);
+           if(phi2<0)phi2+=TMath::TwoPi();
+
+           Float_t diff = TMath::Abs(phi2-phi1); 
+           if(diff>TMath::Pi())diff=TMath::TwoPi()-diff;
+           if(diff<fDiffPhiMax){
+             Float_t r1=TMath::Sqrt(gc1[0]*gc1[0]+gc1[1]*gc1[1]);
+             Float_t zc1=gc1[2];
+             Float_t erz1=recp1->GetSigmaZ2();
+             Float_t r2=TMath::Sqrt(gc2[0]*gc2[0]+gc2[1]*gc2[1]);
+             Float_t zc2=gc2[2];
+             Float_t erz2=recp2->GetSigmaZ2();
+             //        Float_t tgth=(zc2[j]-zc1[i])/(r2-r1); // slope (used for multiple scattering)
+             Float_t zr0=(r2*zc1-r1*zc2)/(r2-r1); //Z @ null radius
+             Float_t ezr0q=(r2*r2*erz1+r1*r1*erz2)/((r2-r1)*(r2-r1)); //error on Z @ null radius
+             /*
+             // Multiple scattering
+             ezr0q+=r1*r1*(1+tgth*tgth)*theta2L1/2; // multiple scattering in layer 1
+             ezr0q+=rBP*rBP*(1+tgth*tgth)*theta2BP/2; // multiple scattering in beam pipe
+             */
+             if(nopoints<maxdim) new(points[nopoints++])AliITSZPoint(zr0,ezr0q);             
+             fZCombc->Fill(zr0);
+           }
+         }
+       }
       }
     }
   }
-  delete [] xc1;
-  delete [] yc1;
-  delete [] zc1;
-  delete [] phi1;
-  delete [] xc2;
-  delete [] yc2;
-  delete [] zc2;
-  delete [] phi2;
+
+  points.Sort();
+
   Double_t contents = fZCombc->GetEntries()- fZCombc->GetBinContent(0)-fZCombc->GetBinContent(nbincoarse+1);
   if(contents<1.){
     //    Warning("FindVertexForCurrentEvent","Insufficient number of rec. points\n");
     ResetHistograms();
-    itsLoader->UnloadRecPoints();
-    fCurrentVertex = new AliESDVertex(0.,5.3,-1);
+    fCurrentVertex = new AliESDVertex(startPos,startCov,99999.,-1);
+    points.Clear();
     return;
   }
-  /*
-  else {
-    if(fDebug>0)cout<<"Number of entries in hist. "<<fZCombc->GetEntries()<<endl;
-  }
-  */
 
   TH1F *hc = fZCombc;
-  Bool_t goon = kFALSE;
-  Int_t cnt = 0;
-  Int_t bi;
 
-  do {
-    goon = kFALSE;
-    cnt++;
-    bi = hc->GetMaximumBin();   // bin with maximal content on coarse histogram
-    if(hc->GetBinContent(bi)<3){
-      if(cnt==1)goon = kTRUE;
-      hc = fZCombv;
-    }
-  } while(goon);
-
-
-  Float_t centre = hc->GetBinCenter(bi);  // z value of the bin with maximal content
   
-  // evaluation of the centroid
-  Int_t ii1=TMath::Max(bi-3,1);
-  Int_t ii2=TMath::Min(bi+3,hc->GetNbinsX());
-  centre = 0.;
-  Int_t nn=0;
-  for(Int_t ii=ii1;ii<ii2;ii++){
-    centre+= hc->GetBinCenter(ii)*hc->GetBinContent(ii);
-    nn+=static_cast<Int_t>(hc->GetBinContent(ii));
-  }
-  if (nn) centre/=nn;
-  /*
-  if(fDebug>0){
-    cout<<"Value of center "<<centre<<endl;
-    cout<<"Population of 3 central bins: "<<hc->GetBinContent(bi-1)<<", ";
-    cout<<hc->GetBinContent(bi)<<", ";
-    cout<<hc->GetBinContent(bi+1)<<endl;
-  }
-  */
-  // n1 is the bin number of fine histogram containing the point located 1 coarse bin less than "centre"
-  Int_t n1 = static_cast<Int_t>((centre-hc->GetBinWidth(bi)-fZCombf->GetBinLowEdge(0))/fStepFine);
-  // n2 is the bin number of fine histogram containing the point located 1 coarse bin more than "centre"
-  Int_t n2 = static_cast<Int_t>((centre+hc->GetBinWidth(bi)-fZCombf->GetBinLowEdge(0))/fStepFine);
-  if(n1<1)n1=1;
-  if(n2>nbinfine)n2=nbinfine;
-  Int_t niter = 0;
-  goon = kTRUE;
-  Int_t num=0;
-  Bool_t last = kFALSE;
+  if(hc->GetBinContent(hc->GetMaximumBin())<3)hc->Rebin(4);
+  Int_t binmin,binmax;
+  Int_t nPeaks=GetPeakRegion(hc,binmin,binmax);   
+  if(nPeaks==2)AliDebug(2,"2 peaks found");
+  Float_t zm =0.;
+  Float_t ezm =0.;
+  Float_t lim1 = hc->GetBinLowEdge(binmin);
+  Float_t lim2 = hc->GetBinLowEdge(binmax)+hc->GetBinWidth(binmax);
+  Float_t widthSR=lim2-lim1;
 
-  while(goon || last){
-    fZFound = 0.;
-    fZsig = 0.;
-    num=0;
-    // at the end of the loop:
-    // fZFound = N*(Average Z) - where N is the number of tracklets
-    // num=N
-    // fZsig = N*Q - where Q is the average of Z*Z
-    for(Int_t n=n1;n<=n2;n++){
-      fZFound+=fZCombf->GetBinCenter(n)*fZCombf->GetBinContent(n);
-      num+=static_cast<Int_t>(fZCombf->GetBinContent(n));
-      fZsig+=fZCombf->GetBinCenter(n)*fZCombf->GetBinCenter(n)*fZCombf->GetBinContent(n);
-    }
-    if(num<2){
-      fZsig = 5.3;  // Default error from the beam sigmoid
-    }
-    else{
-      Float_t radi =  fZsig/(num-1)-fZFound*fZFound/num/(num-1);
-      // radi = square root of sample variance of Z
-      if(radi>0.)fZsig=TMath::Sqrt(radi);
-      else fZsig=5.3;  // Default error from the beam sigmoid
-      // fZfound - Average Z
-      fZFound/=num;
+  if(nPeaks ==1 && (lim2-lim1)<fWindowWidth){
+    Float_t c=(lim1+lim2)/2.;
+    lim1=c-fWindowWidth/2.;
+    lim2=c+fWindowWidth/2.;
+  }
+  Int_t niter = 0, ncontr=0;
+  do {
+    // symmetrization
+    if(zm  !=0.){
+      Float_t semilarg=TMath::Min((lim2-zm),(zm-lim1));
+      lim1=zm - semilarg;
+      lim2=zm + semilarg;
     }
-    if(!last){
-      // goon is true if the distance between the found Z and the lower bin differs from the distance between the found Z and
-      // the upper bin by more than tolerance (0.002)
-      goon = TMath::Abs(TMath::Abs(fZFound-fZCombf->GetBinCenter(n1))-TMath::Abs(fZFound-fZCombf->GetBinCenter(n2)))>fTolerance;
-      // a window in the fine grained histogram is centered aroung the found Z. The width is 2 bins of
-      // the coarse grained histogram
-      if(num>0){
-       n1 = static_cast<Int_t>((fZFound-hc->GetBinWidth(bi)-fZCombf->GetBinLowEdge(0))/fStepFine);
-       if(n1<1)n1=1;
-       n2 = static_cast<Int_t>((fZFound+hc->GetBinWidth(bi)-fZCombf->GetBinLowEdge(0))/fStepFine);
-       if(n2>nbinfine)n2=nbinfine;
-       /*
-         if(fDebug>0){
-         cout<<"Search restricted to n1 = "<<n1<<", n2= "<<n2<<endl;
-         cout<<"z1= "<<fZCombf->GetBinCenter(n1)<<", n2 = "<<fZCombf->GetBinCenter(n2)<<endl;
-         }
-       */
-      }
-      else {
-       n1 = static_cast<Int_t>((centre-(niter+2)*hc->GetBinWidth(bi)-fZCombf->GetBinLowEdge(0))/fStepFine);
-       n2 = static_cast<Int_t>((centre+(niter+2)*hc->GetBinWidth(bi)-fZCombf->GetBinLowEdge(0))/fStepFine);
-       if(n1<1)n1=1;
-       if(n2>nbinfine)n2=nbinfine;
-      }
-      niter++;
-      // no more than 10 adjusting iterations
-      if(niter>=10){
-       goon = kFALSE;
-       //      Warning("FindVertexForCurrentEvent","The procedure does not converge\n");
-      }
 
-      if((fZsig> 0.0001) && !goon && num>5){
-       last = kTRUE;
-       n1 = static_cast<Int_t>((fZFound-fZsig-fZCombf->GetBinLowEdge(0))/fZCombf->GetBinWidth(0));
-       if(n1<1)n1=1;
-       n2 = static_cast<Int_t>((fZFound+fZsig-fZCombf->GetBinLowEdge(0))/fZCombf->GetBinWidth(0));
-       if(n2>nbinfine)n2=nbinfine;
-       /*
-       if(fDebug>0){
-         cout<<"FINAL Search restricted to n1 = "<<n1<<", n2= "<<n2<<endl;
-         cout<<"z1= "<<fZCombf->GetBinCenter(n1)<<", n2 = "<<fZCombf->GetBinCenter(n2)<<endl;
-       }
-       */
+    zm=0.;
+    ezm=0.;
+    ncontr=0;
+    for(Int_t i =0; i<points.GetEntries(); i++){
+      AliITSZPoint* p=(AliITSZPoint*)points.UncheckedAt(i);
+      if(p->GetZ()>lim1 && p->GetZ()<lim2){
+        Float_t deno = p->GetErrZ();
+        zm+=p->GetZ()/deno;
+        ezm+=1./deno;
+        ncontr++;
       }
     }
-    else {
-      last = kFALSE;
+    if(ezm>0) {
+      zm/=ezm;
+      ezm=TMath::Sqrt(1./ezm);
     }
-
+    niter++;
+  } while(niter<10 && TMath::Abs((zm-lim1)-(lim2-zm))>fTolerance);
+  if(nPeaks==2) ezm=widthSR;
+  Double_t position[3]={GetNominalPos()[0],GetNominalPos()[1],zm};
+  Double_t covmatrix[6]={GetNominalCov()[0],0.,GetNominalCov()[2],0.,0.,ezm};
+  fCurrentVertex = new AliESDVertex(position,covmatrix,99999.,ncontr);
+  fCurrentVertex->SetTitle("vertexer: Z");
+  fCurrentVertex->SetDispersion(fDiffPhiMax);
+  fNoVertices=1;
+  points.Clear();
+  if(ncontr>fMinTrackletsForPilup){ 
+    Float_t secPeakPos;
+    Int_t ncontr2=FindSecondPeak(fZCombc,binmin,binmax,secPeakPos);
+    if(ncontr2>=fMinTrackletsForPilup){ 
+      fIsPileup=kTRUE;
+      fNoVertices=2;
+      fZpuv=secPeakPos;
+      fNTrpuv=ncontr2;
+      AliESDVertex secondVert(secPeakPos,0.1,ncontr2);
+      fVertArray = new AliESDVertex[2];
+      fVertArray[0]=(*fCurrentVertex);
+      fVertArray[1]=secondVert;
+    }
+  }
+  if(fNoVertices==1){
+    fVertArray = new AliESDVertex[1];
+    fVertArray[0]=(*fCurrentVertex);     
   }
-  //  if(fDebug>0)cout<<"Numer of Iterations "<<niter<<endl<<endl;
-  //  if(num!=0)fZsig/=TMath::Sqrt(num);
-  if (fZsig<=0) fZsig=5.3; // Default error from the beam sigmoid
-  fCurrentVertex = new AliESDVertex(fZFound,fZsig,num);
-  fCurrentVertex->SetTitle("vertexer: B");
+  
   ResetHistograms();
-  itsLoader->UnloadRecPoints();
   return;
 }
 
+//_____________________________________________________________________
+Int_t AliITSVertexerZ::FindSecondPeak(TH1F* h, Int_t binmin,Int_t binmax, Float_t& secPeakPos){  
+  for(Int_t i=binmin-1;i<=binmax+1;i++){
+    h->SetBinContent(i,0.);
+  }
+  Int_t secPeakBin=h->GetMaximumBin();
+  secPeakPos=h->GetBinCenter(secPeakBin);
+  Int_t secPeakCont=(Int_t)h->GetBinContent(secPeakBin);
+  secPeakCont+=(Int_t)h->GetBinContent(secPeakBin-1);
+  secPeakCont+=(Int_t)h->GetBinContent(secPeakBin+1);  
+  secPeakCont+=(Int_t)h->GetBinContent(secPeakBin-2);
+  secPeakCont+=(Int_t)h->GetBinContent(secPeakBin+2);  
+  return secPeakCont;
+}
+
 //_____________________________________________________________________
 void AliITSVertexerZ::ResetHistograms(){
   // delete TH1 data members
   if(fZCombc)delete fZCombc;
-  if(fZCombf)delete fZCombf;
-  if(fZCombv)delete fZCombv;
   fZCombc = 0;
-  fZCombf = 0;
-  fZCombv = 0;
-}
-
-//______________________________________________________________________
-void AliITSVertexerZ::FindVertices(){
-  // computes the vertices of the events in the range FirstEvent - LastEvent
-  AliRunLoader *rl = AliRunLoader::GetRunLoader();
-  AliITSLoader* itsLoader =  (AliITSLoader*) rl->GetLoader("ITSLoader");
-  itsLoader->ReloadRecPoints();
-  for(Int_t i=fFirstEvent;i<=fLastEvent;i++){
-    //  cout<<"Processing event "<<i<<endl;
-    rl->GetEvent(i);
-    FindVertexForCurrentEvent(i);
-    if(fCurrentVertex){
-      WriteCurrentVertex();
-    }
-    /*
-    else {
-      if(fDebug>0){
-       cout<<"Vertex not found for event "<<i<<endl;
-       cout<<"fZFound = "<<fZFound<<", fZsig= "<<fZsig<<endl;
-      }
-    }
-    */
-  }
 }
 
 //________________________________________________________
@@ -482,29 +425,14 @@ void AliITSVertexerZ::PrintStatus() const {
   cout <<fLastL2<<endl;
   cout <<" Max Phi difference: "<<fDiffPhiMax<<endl;
   cout <<"Limits for Z histograms: "<<fLowLim<<"; "<<fHighLim<<endl;
-  cout <<"Bin sizes for coarse and fine z histos "<<fStepCoarse<<"; "<<fStepFine<<endl;
+  cout <<"Bin sizes for coarse z histos "<<fStepCoarse<<endl;
   cout <<" Current Z "<<fZFound<<"; Z sig "<<fZsig<<endl;
-  cout <<" Debug flag: "<<fDebug<<endl;
-  cout <<"First event to be processed "<<fFirstEvent;
-  cout <<"\n Last event to be processed "<<fLastEvent<<endl;
   if(fZCombc){
     cout<<"fZCombc exists - entries="<<fZCombc->GetEntries()<<endl;
   }
   else{
     cout<<"fZCombc does not exist\n";
   }
-  if(fZCombv){
-    cout<<"fZCombv exists - entries="<<fZCombv->GetEntries()<<endl;
-  }
-  else{
-    cout<<"fZCombv does not exist\n";
-  }
-   if(fZCombf){
-    cout<<"fZCombf exists - entries="<<fZCombv->GetEntries()<<endl;
-  }
-  else{
-    cout<<"fZCombf does not exist\n";
-  }
  
   cout <<"=======================================================\n";
 }