]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PHOS/AliPHOSEmcRecPoint.cxx
Updates for the ROOT trunk
[u/mrichter/AliRoot.git] / PHOS / AliPHOSEmcRecPoint.cxx
index d89039d288691ec57b55bc92d77407e747dbbbb5..de10f63a2d0c786cb935a7665f680267f2329068 100644 (file)
 
 /* $Id$ */
 
+/* History of cvs commits:
+ *
+ * $Log$
+ * Revision 1.59  2007/10/18 15:12:22  kharlov
+ * Moved MakePrimary to EMCRecPoint to rpduce correct order of primaries
+ *
+ * Revision 1.58  2007/04/16 09:03:37  kharlov
+ * Incedent angle correction fixed
+ *
+ * Revision 1.57  2007/04/05 10:18:58  policheh
+ * Introduced distance to nearest bad crystal.
+ *
+ * Revision 1.56  2007/03/06 06:47:28  kharlov
+ * DP:Possibility to use actual vertex position added
+ *
+ * Revision 1.55  2007/01/19 20:31:19  kharlov
+ * Improved formatting for Print()
+ */
+
 //_________________________________________________________________________
 //  RecPoint implementation for PHOS-EMC 
 //  An EmcRecPoint is a cluster of digits   
-//*--
-//*-- Author: Dmitri Peressounko (RRC KI & SUBATECH)
+//--
+//-- Author: Dmitri Peressounko (RRC KI & SUBATECH)
 
 
 // --- ROOT system ---
 // --- Standard library ---
 
 // --- AliRoot header files ---
+#include "AliLog.h"
 #include "AliPHOSLoader.h"
 #include "AliGenerator.h"
 #include "AliPHOSGeometry.h"
 #include "AliPHOSDigit.h"
 #include "AliPHOSEmcRecPoint.h"
+#include "AliPHOSReconstructor.h"
  
 ClassImp(AliPHOSEmcRecPoint)
 
 //____________________________________________________________________________
-AliPHOSEmcRecPoint::AliPHOSEmcRecPoint() : AliPHOSRecPoint()
+AliPHOSEmcRecPoint::AliPHOSEmcRecPoint() : 
+  AliPHOSRecPoint(),
+  fCoreEnergy(0.), fDispersion(0.),
+  fEnergyList(0), fTime(-1.), fNExMax(0),
+  fM2x(0.), fM2z(0.), fM3x(0.), fM4z(0.),
+  fPhixe(0.), fDistToBadCrystal(-1),fDebug(0)
 {
   // ctor
-
   fMulDigit   = 0 ;  
   fAmp   = 0. ;   
-  fCoreEnergy = 0 ; 
-  fEnergyList = 0 ;
-  fNExMax     = 0 ;   //Not unfolded yet
-  fTime = -1. ;
   fLocPos.SetX(1000000.)  ;      //Local position should be evaluated
-  fDebug=0;
-   
+
+  fLambda[0] = 0.;
+  fLambda[1] = 0.;
+
 }
 
 //____________________________________________________________________________
-AliPHOSEmcRecPoint::AliPHOSEmcRecPoint(const char * opt) : AliPHOSRecPoint(opt)
+AliPHOSEmcRecPoint::AliPHOSEmcRecPoint(const char * opt) : 
+  AliPHOSRecPoint(opt),
+  fCoreEnergy(0.), fDispersion(0.),
+  fEnergyList(0), fTime(-1.), fNExMax(0),
+  fM2x(0.), fM2z(0.), fM3x(0.), fM4z(0.),
+  fPhixe(0.), fDistToBadCrystal(-1), fDebug(0)
 {
   // ctor
-  
   fMulDigit   = 0 ;  
   fAmp   = 0. ;   
-  fNExMax     = 0 ;   //Not unfolded yet
-  fCoreEnergy = 0 ; 
-  fEnergyList = 0 ;
-  fTime = -1. ;
   fLocPos.SetX(1000000.)  ;      //Local position should be evaluated
-  fDebug=0;
   
+  fLambda[0] = 0.;
+  fLambda[1] = 0.;
 }
 
 //____________________________________________________________________________
-AliPHOSEmcRecPoint::AliPHOSEmcRecPoint(const AliPHOSEmcRecPoint & rp) : AliPHOSRecPoint(rp)
+AliPHOSEmcRecPoint::AliPHOSEmcRecPoint(const AliPHOSEmcRecPoint & rp) : 
+  AliPHOSRecPoint(rp),
+  fCoreEnergy(rp.fCoreEnergy), fDispersion(rp.fDispersion),
+  fEnergyList(0), fTime(rp.fTime), fNExMax(rp.fNExMax),
+  fM2x(rp.fM2x), fM2z(rp.fM2z), fM3x(rp.fM3x), fM4z(rp.fM4z),
+  fPhixe(rp.fPhixe), fDistToBadCrystal(rp.fDistToBadCrystal), fDebug(rp.fDebug)
 {
   // cpy ctor
-
   fMulDigit   = rp.fMulDigit ;  
   fAmp        = rp.fAmp ;   
-  fCoreEnergy = rp.fCoreEnergy ; 
-  fEnergyList = new Float_t[rp.fMulDigit] ;
-  Int_t index ; 
-  for(index = 0 ; index < fMulDigit ; index++) 
-    fEnergyList[index] = rp.fEnergyList[index] ; 
-  fNExMax     = rp.fNExMax ;  
-  fTime       = rp.fTime ;   
+  if (rp.fMulDigit>0) fEnergyList = new Float_t[rp.fMulDigit] ;
+  for(Int_t index = 0 ; index < fMulDigit ; index++) 
+    fEnergyList[index] = rp.fEnergyList[index] ;
+
+  for(Int_t i=0; i<2; i++) {
+    fLambda[i] = rp.fLambda[i];
+  }
 }
 
 //____________________________________________________________________________
 AliPHOSEmcRecPoint::~AliPHOSEmcRecPoint()
 {
   // dtor
-
   if ( fEnergyList )
     delete[] fEnergyList ; 
 }
 
 //____________________________________________________________________________
-void AliPHOSEmcRecPoint::AddDigit(AliPHOSDigit & digit, Float_t Energy)
+void AliPHOSEmcRecPoint::AddDigit(AliPHOSDigit & digit, Float_t Energy, Float_t time)
 {
   // Adds a digit to the RecPoint
   // and accumulates the total amplitude and the multiplicity 
@@ -131,11 +158,24 @@ void AliPHOSEmcRecPoint::AddDigit(AliPHOSDigit & digit, Float_t Energy)
     delete [] tempoE ; 
   } // if
   
+  //time
+  Bool_t isMax=kTRUE ;
+  for(Int_t index = 0 ; index < fMulDigit ; index++ ){
+    if(fEnergyList[index]>Energy){
+      isMax=kFALSE ;
+      break ;
+    }
+  }
+  if(isMax){
+    fTime=time ;
+  }
+  //Alternative time calculation - still to be validated
+  // fTime = (fTime*fAmp + time*Energy)/(fAmp+Energy) ;
+
   fDigitsList[fMulDigit]   = digit.GetIndexInList()  ; 
   fEnergyList[fMulDigit]   = Energy ;
   fMulDigit++ ; 
   fAmp += Energy ; 
-
   EvalPHOSMod(&digit) ;
 }
 
@@ -206,7 +246,7 @@ Int_t AliPHOSEmcRecPoint::Compare(const TObject * obj) const
   return rv ; 
 }
 //______________________________________________________________________________
-void AliPHOSEmcRecPoint::ExecuteEvent(Int_t event, Int_t, Int_t) const
+void AliPHOSEmcRecPoint::ExecuteEvent(Int_t event, Int_t, Int_t) /*const*/
 {
   
   // Execute action corresponding to one event
@@ -230,18 +270,18 @@ void AliPHOSEmcRecPoint::ExecuteEvent(Int_t event, Int_t, Int_t) const
   AliRunLoader* rn = AliRunLoader::GetRunLoader(AliConfig::GetDefaultEventFolderName());
   if (rn == 0x0) 
     {
-      Error("ExecuteEvent","Can not find Run Loader in Default Event Folder");
+      AliError(Form("Cannot find Run Loader in Default Event Folder"));
       return;
     }
-  AliPHOSLoader* gime = dynamic_cast<AliPHOSLoader*>(rn->GetLoader("PHOSLoader"));
-  if (gime == 0x0) 
+  AliPHOSLoader* phosLoader = dynamic_cast<AliPHOSLoader*>(rn->GetLoader("PHOSLoader"));
+  if (phosLoader == 0x0) 
     {
-      Error("ExecuteEvent","Can not find PHOS Loader from Run Loader");
+      AliError(Form("Cannot find PHOS Loader from Run Loader"));
       return;
     }
   
   
-  const TClonesArray * digits = gime->Digits() ;
+  const TClonesArray * digits = phosLoader->Digits() ;
   
   switch (event) {
     
@@ -284,7 +324,7 @@ void AliPHOSEmcRecPoint::ExecuteEvent(Int_t event, Int_t, Int_t) const
     // 2. gets the histogram title
     
     Text_t title[100] ; 
-    sprintf(title,"Energy=%1.2f GeV ; Digits ; %d ", GetEnergy(), GetDigitsMultiplicity()) ;
+    snprintf(title,100,"Energy=%1.2f GeV ; Digits ; %d ", GetEnergy(), GetDigitsMultiplicity()) ;
     
     if (!histo) {
       delete histo ; 
@@ -330,10 +370,11 @@ void AliPHOSEmcRecPoint::ExecuteEvent(Int_t event, Int_t, Int_t) const
 }
 
 //____________________________________________________________________________
-void  AliPHOSEmcRecPoint::EvalDispersion(Float_t logWeight,TClonesArray * digits)
+void  AliPHOSEmcRecPoint::EvalDispersion(Float_t logWeight,TClonesArray * digits, TVector3 & /* vInc */)
 {
   // Calculates the dispersion of the shower at the origine of the RecPoint
-
+  //DP: should we correct dispersion for non-perpendicular hit????????
   Float_t d    = 0. ;
   Float_t wtot = 0. ;
 
@@ -355,13 +396,21 @@ void  AliPHOSEmcRecPoint::EvalDispersion(Float_t logWeight,TClonesArray * digits
     Float_t zi ;
     phosgeom->AbsToRelNumbering(digit->GetId(), relid) ;
     phosgeom->RelPosInModule(relid, xi, zi);
-    Float_t w = TMath::Max( 0., logWeight + TMath::Log( fEnergyList[iDigit] / fAmp ) ) ;
-    x    += xi * w ;
-    z    += zi * w ;
-    wtot += w ;
+    if (fAmp>0 && fEnergyList[iDigit]>0) {
+      Float_t w = TMath::Max( 0., logWeight + TMath::Log( fEnergyList[iDigit] / fAmp ) ) ;
+      x    += xi * w ;
+      z    += zi * w ;
+      wtot += w ;
+    }
+    else
+      AliError(Form("Wrong energy %f and/or amplitude %f\n", fEnergyList[iDigit], fAmp));
+  }
+  if (wtot>0) {
+    x /= wtot ;
+    z /= wtot ;
   }
-  x /= wtot ;
-  z /= wtot ;
+  else
+    AliError(Form("Wrong weight %f\n", wtot));
 
 
 // Calculates the dispersion in coordinates 
@@ -373,28 +422,36 @@ void  AliPHOSEmcRecPoint::EvalDispersion(Float_t logWeight,TClonesArray * digits
     Float_t zi ;
     phosgeom->AbsToRelNumbering(digit->GetId(), relid) ;
     phosgeom->RelPosInModule(relid, xi, zi);
-    Float_t w = TMath::Max(0.,logWeight+TMath::Log(fEnergyList[iDigit]/fAmp ) ) ;
-    d += w*((xi-x)*(xi-x) + (zi-z)*(zi-z) ) ; 
-    wtot+=w ;
-
-  
+    if (fAmp>0 && fEnergyList[iDigit]>0) {
+      Float_t w = TMath::Max(0.,logWeight+TMath::Log(fEnergyList[iDigit]/fAmp ) ) ;
+      d += w*((xi-x)*(xi-x) + (zi-z)*(zi-z) ) ; 
+      wtot+=w ;
+    }
+    else
+      AliError(Form("Wrong energy %f and/or amplitude %f\n", fEnergyList[iDigit], fAmp));
   }
   
 
-  d /= wtot ;
+  if (wtot>0) {
+    d /= wtot ;
+  }
+  else
+    AliError(Form("Wrong weight %f\n", wtot));
+
+  fDispersion = 0;
+  if (d>=0)
+    fDispersion = TMath::Sqrt(d) ;
 
-  fDispersion = TMath::Sqrt(d) ;
  
 }
 //______________________________________________________________________________
-void AliPHOSEmcRecPoint::EvalCoreEnergy(Float_t logWeight, TClonesArray * digits)
+void AliPHOSEmcRecPoint::EvalCoreEnergy(Float_t logWeight, Float_t coreRadius, TClonesArray * digits)
 {
   // This function calculates energy in the core, 
   // i.e. within a radius rad = 3cm around the center. Beyond this radius
   // in accordance with shower profile the energy deposition 
   // should be less than 2%
-
-  Float_t coreRadius = 3 ;
+//DP: non-perpendicular incidence??????????????
 
   Float_t x = 0 ;
   Float_t z = 0 ;
@@ -414,13 +471,21 @@ void AliPHOSEmcRecPoint::EvalCoreEnergy(Float_t logWeight, TClonesArray * digits
     Float_t zi ;
     phosgeom->AbsToRelNumbering(digit->GetId(), relid) ;
     phosgeom->RelPosInModule(relid, xi, zi);
-    Float_t w = TMath::Max( 0., logWeight + TMath::Log( fEnergyList[iDigit] / fAmp ) ) ;
-    x    += xi * w ;
-    z    += zi * w ;
-    wtot += w ;
+    if (fAmp>0 && fEnergyList[iDigit]>0) {
+      Float_t w = TMath::Max( 0., logWeight + TMath::Log( fEnergyList[iDigit] / fAmp ) ) ;
+      x    += xi * w ;
+      z    += zi * w ;
+      wtot += w ;
+    }
+    else
+      AliError(Form("Wrong energy %f and/or amplitude %f\n", fEnergyList[iDigit], fAmp));
+  }
+  if (wtot>0) {
+    x /= wtot ;
+    z /= wtot ;
   }
-  x /= wtot ;
-  z /= wtot ;
+  else
+    AliError(Form("Wrong weight %f\n", wtot));
 
 
   for(iDigit=0; iDigit < fMulDigit; iDigit++) {
@@ -435,10 +500,11 @@ void AliPHOSEmcRecPoint::EvalCoreEnergy(Float_t logWeight, TClonesArray * digits
       fCoreEnergy += fEnergyList[iDigit] ;
   }
 
+
 }
 
 //____________________________________________________________________________
-void  AliPHOSEmcRecPoint::EvalElipsAxis(Float_t logWeight,TClonesArray * digits)
+void  AliPHOSEmcRecPoint::EvalElipsAxis(Float_t logWeight,TClonesArray * digits, TVector3 & /* vInc */)
 {
   // Calculates the axis of the shower ellipsoid
 
@@ -463,29 +529,33 @@ void  AliPHOSEmcRecPoint::EvalElipsAxis(Float_t logWeight,TClonesArray * digits)
     Float_t zi ;
     phosgeom->AbsToRelNumbering(digit->GetId(), relid) ;
     phosgeom->RelPosInModule(relid, xi, zi);
-    Double_t w = TMath::Max(0.,logWeight+TMath::Log(fEnergyList[iDigit]/fAmp ) ) ;
-    dxx  += w * xi * xi ;
-    x    += w * xi ;
-    dzz  += w * zi * zi ;
-    z    += w * zi ; 
-    dxz  += w * xi * zi ; 
-    wtot += w ;
-  }
-  dxx /= wtot ;
-  x   /= wtot ;
-  dxx -= x * x ;
-  dzz /= wtot ;
-  z   /= wtot ;
-  dzz -= z * z ;
-  dxz /= wtot ;
-  dxz -= x * z ;
+    if (fAmp>0 && fEnergyList[iDigit]>0) {
+      Double_t w = TMath::Max(0.,logWeight+TMath::Log(fEnergyList[iDigit]/fAmp ) ) ;
+      dxx  += w * xi * xi ;
+      x    += w * xi ;
+      dzz  += w * zi * zi ;
+      z    += w * zi ; 
+      dxz  += w * xi * zi ; 
+      wtot += w ;
+    }
+    else
+      AliError(Form("Wrong energy %f and/or amplitude %f\n", fEnergyList[iDigit], fAmp));
+  }
+  if (wtot>0) {
+    dxx /= wtot ;
+    x   /= wtot ;
+    dxx -= x * x ;
+    dzz /= wtot ;
+    z   /= wtot ;
+    dzz -= z * z ;
+    dxz /= wtot ;
+    dxz -= x * z ;
 
 //   //Apply correction due to non-perpendicular incidence
 //   Double_t CosX ;
 //   Double_t CosZ ;
-//   AliPHOSGetter * gime = AliPHOSGetter::Instance() ; 
-//   AliPHOSGeometry * phosgeom =  (AliPHOSGeometry*)gime->PHOSGeometry();
-  //   Double_t DistanceToIP= (Double_t ) phosgeom->GetIPtoCrystalSurface() ;
+//   AliPHOSGeometry * phosgeom = AliPHOSGeometry::GetInstance() ;
+//   Double_t DistanceToIP= (Double_t ) phosgeom->GetIPtoCrystalSurface() ;
   
 //   CosX = DistanceToIP/TMath::Sqrt(DistanceToIP*DistanceToIP+x*x) ;
 //   CosZ = DistanceToIP/TMath::Sqrt(DistanceToIP*DistanceToIP+z*z) ;
@@ -495,19 +565,24 @@ void  AliPHOSEmcRecPoint::EvalElipsAxis(Float_t logWeight,TClonesArray * digits)
 //   dxz = dxz/(CosX*CosZ) ;
 
 
-  fLambda[0] =  0.5 * (dxx + dzz) + TMath::Sqrt( 0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz )  ;
-  if(fLambda[0] > 0)
-    fLambda[0] = TMath::Sqrt(fLambda[0]) ;
+    fLambda[0] =  0.5 * (dxx + dzz) + TMath::Sqrt( 0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz )  ;
+    if(fLambda[0] > 0)
+      fLambda[0] = TMath::Sqrt(fLambda[0]) ;
 
-  fLambda[1] =  0.5 * (dxx + dzz) - TMath::Sqrt( 0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz )  ;
-  if(fLambda[1] > 0) //To avoid exception if numerical errors lead to negative lambda.
-    fLambda[1] = TMath::Sqrt(fLambda[1]) ;
-  else
-    fLambda[1]= 0. ;
+    fLambda[1] =  0.5 * (dxx + dzz) - TMath::Sqrt( 0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz )  ;
+    if(fLambda[1] > 0) //To avoid exception if numerical errors lead to negative lambda.
+      fLambda[1] = TMath::Sqrt(fLambda[1]) ;
+    else
+      fLambda[1]= 0. ;
+  }
+  else {
+    AliError(Form("Wrong weight %f\n", wtot));
+    fLambda[0]=fLambda[1]=0.;
+  }
 }
 
 //____________________________________________________________________________
-void  AliPHOSEmcRecPoint::EvalMoments(Float_t logWeight,TClonesArray * digits)
+void  AliPHOSEmcRecPoint::EvalMoments(Float_t logWeight,TClonesArray * digits, TVector3 & /* vInc */)
 {
   // Calculate the shower moments in the eigen reference system
   // M2x, M2z, M3x, M4z
@@ -539,27 +614,38 @@ void  AliPHOSEmcRecPoint::EvalMoments(Float_t logWeight,TClonesArray * digits)
     digit = (AliPHOSDigit *) digits->At(fDigitsList[iDigit])  ;
     phosgeom->AbsToRelNumbering(digit->GetId(), relid) ;
     phosgeom->RelPosInModule(relid, xi, zi);
-    w     = TMath::Max(0.,logWeight+TMath::Log(fEnergyList[iDigit]/fAmp ) ) ;
-    x    += w * xi ;
-    z    += w * zi ; 
-    dxx  += w * xi * xi ;
-    dzz  += w * zi * zi ;
-    dxz  += w * xi * zi ; 
-    wtot += w ;
-  }
-  x   /= wtot ;
-  z   /= wtot ;
-  dxx /= wtot ;
-  dzz /= wtot ;
-  dxz /= wtot ;
-  dxx -= x * x ;
-  dzz -= z * z ;
-  dxz -= x * z ;
+    if (fAmp>0 && fEnergyList[iDigit]>0) {
+      w     = TMath::Max(0.,logWeight+TMath::Log(fEnergyList[iDigit]/fAmp ) ) ;
+      x    += w * xi ;
+      z    += w * zi ; 
+      dxx  += w * xi * xi ;
+      dzz  += w * zi * zi ;
+      dxz  += w * xi * zi ; 
+      wtot += w ;
+    }
+    else
+      AliError(Form("Wrong energy %f and/or amplitude %f\n", fEnergyList[iDigit], fAmp));
+    
+  }
+  if (wtot>0) {
+    x   /= wtot ;
+    z   /= wtot ;
+    dxx /= wtot ;
+    dzz /= wtot ;
+    dxz /= wtot ;
+    dxx -= x * x ;
+    dzz -= z * z ;
+    dxz -= x * z ;
 
   // 2) Find covariance matrix eigen values lambda0 and lambda1
 
-  lambda0 =  0.5 * (dxx + dzz) + TMath::Sqrt( 0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz )  ;
-  lambda1 =  0.5 * (dxx + dzz) - TMath::Sqrt( 0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz )  ;
+    lambda0 =  0.5 * (dxx + dzz) + TMath::Sqrt( 0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz )  ;
+    lambda1 =  0.5 * (dxx + dzz) - TMath::Sqrt( 0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz )  ;
+  }
+  else {
+    AliError(Form("Wrong weight %f\n", wtot));
+    lambda0=lambda1=0.;
+  }
 
   // 3) Find covariance matrix eigen vectors e0 and e1
 
@@ -596,34 +682,44 @@ void  AliPHOSEmcRecPoint::EvalMoments(Float_t logWeight,TClonesArray * digits)
     phosgeom->RelPosInModule(relid, xiPHOS, ziPHOS);
     xi    = xiPHOS*cosPhi + ziPHOS*sinPhi;
     zi    = ziPHOS*cosPhi - xiPHOS*sinPhi;
-    w     = TMath::Max(0.,logWeight+TMath::Log(fEnergyList[iDigit]/fAmp ) ) ;
-    x    += w * xi ;
-    z    += w * zi ; 
-    dxx  += w * xi * xi ;
-    dzz  += w * zi * zi ;
-    dxz  += w * xi * zi ; 
-    dx3  += w * xi * xi * xi;
-    dz3  += w * zi * zi * zi ;
-    dz4  += w * zi * zi * zi * zi ;
-    wtot += w ;
-  }
-  x   /= wtot ;
-  z   /= wtot ;
-  dxx /= wtot ;
-  dzz /= wtot ;
-  dxz /= wtot ;
-  dx3 /= wtot ;
-  dz3 /= wtot ;
-  dz4 /= wtot ;
-  dx3 += -3*dxx*x + 2*x*x*x;
-  dz4 += -4*dz3*z + 6*dzz*z*z -3*z*z*z*z;
-  dxx -= x * x ;
-  dzz -= z * z ;
-  dxz -= x * z ;
+    if (fAmp>0 && fEnergyList[iDigit]>0) {
+      w     = TMath::Max(0.,logWeight+TMath::Log(fEnergyList[iDigit]/fAmp ) ) ;
+      x    += w * xi ;
+      z    += w * zi ; 
+      dxx  += w * xi * xi ;
+      dzz  += w * zi * zi ;
+      dxz  += w * xi * zi ; 
+      dx3  += w * xi * xi * xi;
+      dz3  += w * zi * zi * zi ;
+      dz4  += w * zi * zi * zi * zi ;
+      wtot += w ;
+    }
+    else
+      AliError(Form("Wrong energy %f and/or amplitude %f\n", fEnergyList[iDigit], fAmp));
+  }
+  if (wtot>0) {
+    x   /= wtot ;
+    z   /= wtot ;
+    dxx /= wtot ;
+    dzz /= wtot ;
+    dxz /= wtot ;
+    dx3 /= wtot ;
+    dz3 /= wtot ;
+    dz4 /= wtot ;
+    dx3 += -3*dxx*x + 2*x*x*x;
+    dz4 += -4*dz3*z + 6*dzz*z*z -3*z*z*z*z;
+    dxx -= x * x ;
+    dzz -= z * z ;
+    dxz -= x * z ;
+  }
+  else
+    AliError(Form("Wrong weight %f\n", wtot));
 
   // 5) Find an angle between cluster center vector and eigen vector e0
 
-  Float_t phi = TMath::ACos ((x*e0.X() + z*e0.Y()) / sqrt(x*x + z*z));
+  Float_t phi = 0;
+  if ( (x*x+z*z) > 0 ) 
+    phi = TMath::ACos ((x*e0.X() + z*e0.Y()) / sqrt(x*x + z*z));
 
   fM2x   = lambda0;
   fM2z   = lambda1;
@@ -632,20 +728,89 @@ void  AliPHOSEmcRecPoint::EvalMoments(Float_t logWeight,TClonesArray * digits)
   fPhixe = phi;
 
 }
+//______________________________________________________________________________
+void  AliPHOSEmcRecPoint::EvalPrimaries(TClonesArray * digits)
+{
+  // Constructs the list of primary particles (tracks) which have contributed to this RecPoint
+  
+  AliPHOSDigit * digit ;
+  Int_t * tempo    = new Int_t[fMaxTrack] ;
+  
+  //First find digit with maximal energy deposition and copy its primaries
+  Float_t emax=0.;
+  Int_t imaxDigit=0;
+  for(Int_t id=0; id<GetDigitsMultiplicity(); id++){
+    if(emax<fEnergyList[id])
+      imaxDigit=id ;
+  }
+  digit = static_cast<AliPHOSDigit *>(digits->At( fDigitsList[imaxDigit] )) ; 
+  Int_t nprimaries = digit->GetNprimary() ;
+  if ( nprimaries > fMaxTrack ) {
+    fMulTrack = - 1 ;
+    Error("EvalPrimaries", "GetNprimaries ERROR > increase fMaxTrack" ) ;
+    nprimaries = fMaxTrack; //skip the rest
+  }
+  for(fMulTrack=0; fMulTrack<nprimaries ; fMulTrack++){
+    tempo[fMulTrack] = digit->GetPrimary(fMulTrack+1) ;
+  }
+
+  //Now add other digits contributions
+  for (Int_t index = 0 ; index < GetDigitsMultiplicity() ; index++ ) { // all digits
+    if(index==imaxDigit) //already in
+      continue ; 
+    digit = static_cast<AliPHOSDigit *>(digits->At( fDigitsList[index] )) ; 
+    nprimaries = digit->GetNprimary() ;
+    for(Int_t ipr=0; ipr<nprimaries; ipr++){
+      Int_t iprimary = digit->GetPrimary(ipr+1) ;
+      Bool_t notIn=1 ;
+      for(Int_t kndex = 0 ; (kndex < fMulTrack)&& notIn ; kndex++ ) { //check if not already stored
+       if(iprimary == tempo[kndex]){
+         notIn = kFALSE ;
+       }
+      }
+      if(notIn){
+       if(fMulTrack<fMaxTrack){
+         tempo[fMulTrack]=iprimary ;
+         fMulTrack++ ;
+       }
+       else{
+         Error("EvalPrimaries", "GetNprimaries ERROR > increase fMaxTrack!!!" ) ;
+         break ;
+       }
+      }
+    }
+  } // all digits
+  if(fMulTrack > 0){
+    if(fTracksList)delete [] fTracksList;
+    fTracksList = new Int_t[fMulTrack] ;
+  }
+  for(Int_t index = 0; index < fMulTrack; index++)
+    fTracksList[index] = tempo[index] ;
+  
+  delete [] tempo ;
+  
+}
+
 //____________________________________________________________________________
-void AliPHOSEmcRecPoint::EvalAll(Float_t logWeight, TClonesArray * digits )
+void AliPHOSEmcRecPoint::EvalAll(TClonesArray * digits )
 {
-  // Evaluates all shower parameters
-  EvalLocalPosition(logWeight, digits) ;
-  EvalElipsAxis(logWeight, digits) ;
-  EvalMoments(logWeight, digits) ;
-  EvalDispersion(logWeight, digits) ;
-  EvalCoreEnergy(logWeight, digits);
+//   EvalCoreEnergy(logWeight, digits);
   EvalTime(digits) ;
+  EvalPrimaries(digits) ;
   AliPHOSRecPoint::EvalAll(digits) ;
 }
 //____________________________________________________________________________
-void AliPHOSEmcRecPoint::EvalLocalPosition(Float_t logWeight, TClonesArray * digits)
+void AliPHOSEmcRecPoint::EvalAll(Float_t logWeight, TVector3 &vtx, TClonesArray * digits )
+{
+  // Evaluates all shower parameters
+  TVector3 vInc ;
+  EvalLocalPosition(logWeight, vtx, digits,vInc) ;
+  EvalElipsAxis(logWeight, digits, vInc) ; //they are evaluated with momenta
+  EvalMoments(logWeight, digits, vInc) ;
+  EvalDispersion(logWeight, digits, vInc) ;
+}
+//____________________________________________________________________________
+void AliPHOSEmcRecPoint::EvalLocalPosition(Float_t logWeight, TVector3 &vtx, TClonesArray * digits, TVector3 &vInc)
 {
   // Calculates the center of gravity in the local PHOS-module coordinates 
   Float_t wtot = 0. ;
@@ -668,46 +833,41 @@ void AliPHOSEmcRecPoint::EvalLocalPosition(Float_t logWeight, TClonesArray * dig
     Float_t zi ;
     phosgeom->AbsToRelNumbering(digit->GetId(), relid) ;
     phosgeom->RelPosInModule(relid, xi, zi);
-    Float_t w = TMath::Max( 0., logWeight + TMath::Log( fEnergyList[iDigit] / fAmp ) ) ;
-    x    += xi * w ;
-    z    += zi * w ;
-    wtot += w ;
-
+    if (fAmp>0 && fEnergyList[iDigit]>0) {
+      Float_t w = TMath::Max( 0., logWeight + TMath::Log( fEnergyList[iDigit] / fAmp ) ) ;
+      x    += xi * w ;
+      z    += zi * w ;
+      wtot += w ;
+    }
+    else
+      AliError(Form("Wrong energy %f and/or amplitude %f\n", fEnergyList[iDigit], fAmp));
   }
-  x /= wtot ;
-  z /= wtot ;
+  if (wtot>0) {
+    x /= wtot ;
+    z /= wtot ;
+  }
+  else
+    AliError(Form("Wrong weight %f\n", wtot));
 
   // Correction for the depth of the shower starting point (TDR p 127)  
   Float_t para = 0.925 ; 
   Float_t parb = 6.52 ; 
 
-  Float_t xo,yo,zo ; //Coordinates of the origin
-  //We should check all 3 possibilities to avoid seg.v.
-  if(gAlice && gAlice->GetMCApp() && gAlice->Generator())
-    gAlice->Generator()->GetOrigin(xo,yo,zo) ;
-  else{
-    xo=yo=zo=0.;
-  }
-  Float_t phi = phosgeom->GetPHOSAngle(relid[0]) ;
+  phosgeom->GetIncidentVector(vtx,GetPHOSMod(),x,z,vInc) ;
 
-  //Transform to the local ref.frame
-  Float_t xoL,yoL ;
-  xoL = xo*TMath::Cos(phi)-yo*TMath::Sin(phi) ;
-  yoL = xo*TMath::Sin(phi)+yo*TMath::Cos(phi) ;
-  
-  Float_t radius = phosgeom->GetIPtoCrystalSurface()-yoL;
-  Float_t incidencephi = TMath::ATan((x-xoL ) / radius) ; 
-  Float_t incidencetheta = TMath::ATan((z-zo) / radius) ;
-  Float_t depthx =  ( para * TMath::Log(fAmp) + parb ) * TMath::Sin(incidencephi) ; 
-  Float_t depthz =  ( para * TMath::Log(fAmp) + parb ) * TMath::Sin(incidencetheta) ; 
+  Float_t depthx = 0.; 
+  Float_t depthz = 0.;
+  if (fAmp>0&&vInc.Y()!=0.) {
+    depthx = ( para * TMath::Log(fAmp) + parb ) * vInc.X()/TMath::Abs(vInc.Y()) ;
+    depthz = ( para * TMath::Log(fAmp) + parb ) * vInc.Z()/TMath::Abs(vInc.Y()) ;
+  }
+  else 
+    AliError(Form("Wrong amplitude %f\n", fAmp));
 
   fLocPos.SetX(x - depthx)  ;
   fLocPos.SetY(0.) ;
   fLocPos.SetZ(z - depthz)  ;
 
-  fLocPosM = 0 ;
 }
 
 //____________________________________________________________________________
@@ -799,10 +959,12 @@ Int_t  AliPHOSEmcRecPoint::GetNumberOfLocalMax( AliPHOSDigit **  maxAt, Float_t
   return iDigitN ;
 }
 //____________________________________________________________________________
-void AliPHOSEmcRecPoint::EvalTime(TClonesArray * digits)
+void AliPHOSEmcRecPoint::EvalTime(TClonesArray * /*digits*/)
 {
   // Define a rec.point time as a time in the cell with the maximum energy
+  //Time already evaluated during AddDigit()
 
+/*
   Float_t maxE = 0;
   Int_t maxAt = 0;
   for(Int_t idig=0; idig < fMulDigit; idig++){
@@ -812,14 +974,14 @@ void AliPHOSEmcRecPoint::EvalTime(TClonesArray * digits)
     }
   }
   fTime = ((AliPHOSDigit*) digits->At(fDigitsList[maxAt]))->GetTime() ;
-  
+*/  
 }
 //____________________________________________________________________________
-void AliPHOSEmcRecPoint::Purify(Float_t threshold){
+void AliPHOSEmcRecPoint::Purify(Float_t threshold, const TClonesArray * digits){
   //Removes digits below threshold
 
-  Int_t * tempo = new Int_t[fMaxDigit]; 
-  Float_t * tempoE =  new Float_t[fMaxDigit];
+  Int_t tempo[fMaxDigit]; 
+  Float_t tempoE[fMaxDigit];
 
   Int_t mult = 0 ;
   for(Int_t iDigit=0;iDigit< fMulDigit ;iDigit++){
@@ -829,8 +991,42 @@ void AliPHOSEmcRecPoint::Purify(Float_t threshold){
       mult++ ;
     }
   }
-  
-  fMulDigit = mult ;
+
+  if(mult==0){ //too soft cluster
+    fMulDigit =0 ;
+    fAmp = 0.; //Recalculate total energy
+  }
+    
+  //Remove non-connected cells
+  Int_t index[mult] ;
+  Bool_t used[mult] ;
+  for(Int_t i=0; i<mult; i++) used[i]=0 ;
+  Int_t inClu=0 ;
+  Double_t eMax=0. ;
+  //find maximum
+  for(Int_t iDigit=0; iDigit<mult; iDigit++) {
+    if(eMax<tempoE[iDigit]){
+      eMax=tempoE[iDigit];
+      index[0]=iDigit ;
+      inClu=1 ;
+    }
+  }
+  used[index[0]]=kTRUE ; //mark as used
+  for(Int_t i=0; i<inClu; i++){
+    AliPHOSDigit * digit = (AliPHOSDigit *) digits->At(tempo[index[i]]) ; 
+    for(Int_t iDigit=0 ;iDigit<mult; iDigit++){
+      if(used[iDigit]) //already used
+        continue ;
+      AliPHOSDigit * digitN = (AliPHOSDigit *) digits->At(tempo[iDigit]) ;     
+      if(AreNeighbours(digit,digitN)){
+       index[inClu]= iDigit ;
+       inClu++ ;
+       used[iDigit]=kTRUE ;
+      }
+    }
+  }
+     
+  fMulDigit = inClu ;
   delete [] fDigitsList ;
   delete [] fEnergyList ;
   fDigitsList = new Int_t[fMulDigit];
@@ -838,14 +1034,10 @@ void AliPHOSEmcRecPoint::Purify(Float_t threshold){
 
   fAmp = 0.; //Recalculate total energy
   for(Int_t iDigit=0;iDigit< fMulDigit ;iDigit++){
-     fDigitsList[iDigit] = tempo[iDigit];
-     fEnergyList[iDigit] = tempoE[iDigit] ;
-     fAmp+=tempoE[iDigit];
-  }
-      
-  delete [] tempo ;
-  delete [] tempoE ;
-
+     fDigitsList[iDigit] = tempo[index[iDigit]];
+     fEnergyList[iDigit] = tempoE[index[iDigit]] ;
+     fAmp+=tempoE[index[iDigit]];
+  }      
 }
 //____________________________________________________________________________
 void AliPHOSEmcRecPoint::Print(Option_t *) const
@@ -854,27 +1046,27 @@ void AliPHOSEmcRecPoint::Print(Option_t *) const
   
   TString message ; 
   message  = "AliPHOSEmcRecPoint:\n" ;
-  message +=  " digits # = " ; 
-  Info("Print", message.Data()) ; 
+  message += "Digit multiplicity = %d" ;
+  message += ", cluster Energy = %f" ; 
+  message += ", number of primaries = %d" ; 
+  message += ", rec.point index = %d \n" ; 
+  printf(message.Data(), fMulDigit, fAmp, fMulTrack,GetIndexInList() ) ;  
 
   Int_t iDigit;
+  printf(" digits ids = ") ; 
   for(iDigit=0; iDigit<fMulDigit; iDigit++)
     printf(" %d ", fDigitsList[iDigit] ) ;  
   
-  Info("Print", " Energies = ") ;
+  printf("\n digit energies = ") ;
   for(iDigit=0; iDigit<fMulDigit; iDigit++) 
     printf(" %f ", fEnergyList[iDigit] ) ;
-  printf("\n") ; 
-   Info("Print", " Primaries  ") ;
+
+  printf("\n digit primaries  ") ;
+  if (fMulTrack<1) printf("... no primaries");
   for(iDigit = 0;iDigit < fMulTrack; iDigit++)
     printf(" %d ", fTracksList[iDigit]) ;
   printf("\n") ;       
-  message  = "       Multiplicity    = %d" ;
-  message += "       Cluster Energy  = %f" ; 
-  message += "       Number of primaries %d" ; 
-  message += "       Stored at position %d" ; 
-  Info("Print", message.Data(), fMulDigit, fAmp, fMulTrack,GetIndexInList() ) ;  
+
 }