]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EMCAL/AliEMCALRecPoint.cxx
Updates in calibration objects handling
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALRecPoint.cxx
index 7ef32f40f9ebd8de5d49f8e04144c6051a1c55a3..d4d633e5344aa19f9c5f2d1945fa6b54ad962249 100644 (file)
 //*-- Author: Heather Gray (LBL) merged AliEMCALRecPoint and AliEMCALTowerRecPoint 02/04
 
 // --- ROOT system ---
-class Riostream;
-#include <TPad.h>
-class TGraph;
-class TPaveText;
-#include <TClonesArray.h>
-#include <TMath.h>
+#include "TPad.h"
+#include "TGraph.h"
+#include "TPaveText.h"
+#include "TClonesArray.h"
+#include "TMath.h"
+#include "TGeoMatrix.h"
+#include "TGeoManager.h"
+#include "TGeoPhysicalNode.h"
 
 // --- Standard library ---
+#include <Riostream.h>
 
 // --- AliRoot header files ---
 //#include "AliGenerator.h"
 class AliGenerator;
-#include "AliRunLoader.h"
-#include "AliRun.h"
 class AliEMCAL;
-#include "AliEMCALLoader.h"
+#include "AliLog.h"
+#include "AliGeomManager.h"
 #include "AliEMCALGeometry.h"
 #include "AliEMCALHit.h"
 #include "AliEMCALDigit.h"
@@ -46,112 +48,86 @@ ClassImp(AliEMCALRecPoint)
 
 //____________________________________________________________________________
 AliEMCALRecPoint::AliEMCALRecPoint()
-  : AliRecPoint(),
-    fGeomPtr(0),
-    fClusterType(-1),
-    fCoreEnergy(0),
-    fDispersion(0),
-    fEnergyList(0),
-    fTimeList(0),
-    fAbsIdList(0),
-    fTime(0.),
-    fCoreRadius(10),  //HG check this
-    fDETracksList(0),
-    fMulParent(0),
-    fMaxParent(0),
-    fParentsList(0),
-    fDEParentsList(0),
-    fSuperModuleNumber(0)
+  : AliCluster(), fGeomPtr(0),
+    fAmp(0), fIndexInList(-1), //to be set when the point is already stored
+    fLocPos(0,0,0), fLocPosM(0),
+    fMaxDigit(100), fMulDigit(0), fMaxTrack(200),
+    fMulTrack(0), fDigitsList(0), fTracksList(0),
+    fClusterType(-1), fCoreEnergy(0), fDispersion(0),
+    fEnergyList(0), fTimeList(0), fAbsIdList(0),
+    fTime(0.), fCoreRadius(10),  //HG check this
+    fDETracksList(0), fMulParent(0), fMaxParent(0),
+    fParentsList(0), fDEParentsList(0), fSuperModuleNumber(0),
+    fDigitIndMax(-1)
 {
   // ctor
-  fMaxTrack = 0 ;
-  fMulDigit = 0 ;
-  fAmp   = 0. ;   
+  fGeomPtr = AliEMCALGeometry::GetInstance();
+  
+  fLambda[0] = 0;
+  fLambda[1] = 0;
 
-  AliRunLoader *rl = AliRunLoader::GetRunLoader();
-  if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL"))
-    fGeomPtr = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
-  else
-    fGeomPtr = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaulGeometryName());
-  fGeomPtr->GetTransformationForSM(); // Global <-> Local
 }
 
 //____________________________________________________________________________
-AliEMCALRecPoint::AliEMCALRecPoint(const char * opt) 
-  : AliRecPoint(opt),
-    fGeomPtr(0),
-    fClusterType(-1),
-    fCoreEnergy(0),
-    fDispersion(0),
-    fEnergyList(0),
-    fTimeList(0),
-    fAbsIdList(0),
-    fTime(-1.),
-    fCoreRadius(10),  //HG check this
-    fDETracksList(0),
-    fMulParent(0),
-    fMaxParent(1000),
-    fParentsList(0),
-    fDEParentsList(0),
-    fSuperModuleNumber(0)
+AliEMCALRecPoint::AliEMCALRecPoint(const char *) 
+  : AliCluster(), fGeomPtr(0),
+    fAmp(0), fIndexInList(-1), //to be set when the point is already stored
+    fLocPos(0,0,0), fLocPosM(new TMatrixF(3,3)),
+    fMaxDigit(100), fMulDigit(0), fMaxTrack(1000), fMulTrack(0),
+    fDigitsList(new Int_t[fMaxDigit]), fTracksList(new Int_t[fMaxTrack]),
+    fClusterType(-1), fCoreEnergy(0), fDispersion(0),
+    fEnergyList(new Float_t[fMaxDigit]), fTimeList(new Float_t[fMaxDigit]), 
+    fAbsIdList(new Int_t[fMaxDigit]), fTime(-1.), fCoreRadius(10),
+    fDETracksList(new Float_t[fMaxTrack]), fMulParent(0), fMaxParent(1000),
+    fParentsList(new Int_t[fMaxParent]), fDEParentsList(new Float_t[fMaxParent]),
+    fSuperModuleNumber(0), fDigitIndMax(-1)
 {
   // ctor
-  fMaxTrack = 1000 ;
-  fMulDigit   = 0 ; 
-  fAmp   = 0. ;   
-  fDETracksList = new Float_t[fMaxTrack];
-  fParentsList = new Int_t[fMaxParent];
-  fDEParentsList = new Float_t[fMaxParent];
   for (Int_t i = 0; i < fMaxTrack; i++)
     fDETracksList[i] = 0;
-  for (Int_t i = 0; i < fMaxParent; i++)
+  for (Int_t i = 0; i < fMaxParent; i++) {
+    fParentsList[i] = -1;
     fDEParentsList[i] = 0;
+  }
 
-  AliRunLoader *rl = AliRunLoader::GetRunLoader();
-  if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL"))
-    fGeomPtr = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
-  else
-    fGeomPtr = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaulGeometryName());
-  fGeomPtr->GetTransformationForSM(); // Global <-> Local
+  fGeomPtr = AliEMCALGeometry::GetInstance();
+  fLambda[0] = 0;
+  fLambda[1] = 0;
 }
 
 //____________________________________________________________________________
 AliEMCALRecPoint::AliEMCALRecPoint(const AliEMCALRecPoint & rp) 
-  : AliRecPoint(rp),
-    fGeomPtr(rp.fGeomPtr),
-    fClusterType(rp.fClusterType),
-    fCoreEnergy(rp.fCoreEnergy),
+  : AliCluster(rp), fGeomPtr(rp.fGeomPtr),
+    fAmp(rp.fAmp), fIndexInList(rp.fIndexInList),
+    fLocPos(rp.fLocPos), fLocPosM(rp.fLocPosM),
+    fMaxDigit(rp.fMaxDigit), fMulDigit(rp.fMulDigit),
+    fMaxTrack(rp.fMaxTrack), fMulTrack(rp.fMaxTrack),
+    fDigitsList(new Int_t[rp.fMaxDigit]), fTracksList(new Int_t[rp.fMaxTrack]),
+    fClusterType(rp.fClusterType), fCoreEnergy(rp.fCoreEnergy), 
     fDispersion(rp.fDispersion),
-    fEnergyList(0),
-    fTimeList(0),
-    fAbsIdList(0),
-    fTime(rp.fTime),
-    fCoreRadius(rp.fCoreRadius),
-    fDETracksList(0),
-    fMulParent(rp.fMulParent),
-    fMaxParent(rp.fMaxParent),
-    fParentsList(0),
-    fDEParentsList(0),
-    fSuperModuleNumber(rp.fSuperModuleNumber)
+    fEnergyList(new Float_t[rp.fMaxDigit]), fTimeList(new Float_t[rp.fMaxDigit]), 
+    fAbsIdList(new Int_t[rp.fMaxDigit]), fTime(rp.fTime), fCoreRadius(rp.fCoreRadius),
+    fDETracksList(new Float_t[rp.fMaxTrack]), fMulParent(rp.fMulParent), 
+    fMaxParent(rp.fMaxParent), fParentsList(new Int_t[rp.fMaxParent]), 
+    fDEParentsList(new Float_t[rp.fMaxParent]),
+    fSuperModuleNumber(rp.fSuperModuleNumber), fDigitIndMax(rp.fDigitIndMax)
 {
   //copy ctor
   fLambda[0] = rp.fLambda[0];
   fLambda[1] = rp.fLambda[1];
 
-  fEnergyList = new Float_t[rp.fMaxDigit];
-  fTimeList = new Float_t[rp.fMaxDigit];
-  fAbsIdList = new Int_t[rp.fMaxDigit];
   for(Int_t i = 0; i < rp.fMulDigit; i++) {
     fEnergyList[i] = rp.fEnergyList[i];
     fTimeList[i] = rp.fTimeList[i];
     fAbsIdList[i] = rp.fAbsIdList[i];
   }
-  fDETracksList = new Float_t[rp.fMaxTrack];
+
   for(Int_t i = 0; i < rp.fMulTrack; i++) fDETracksList[i] = rp.fDETracksList[i];
-  fParentsList = new Int_t[rp.fMaxParent];
-  for(Int_t i = 0; i < rp.fMulParent; i++) fParentsList[i] = rp.fParentsList[i];
-  fDEParentsList = new Float_t[rp.fMaxParent];
-  for(Int_t i = 0; i < rp.fMulParent; i++) fDEParentsList[i] = rp.fDEParentsList[i];
+
+  for(Int_t i = 0; i < rp.fMulParent; i++) {
+    fParentsList[i] = rp.fParentsList[i];
+    fDEParentsList[i] = rp.fDEParentsList[i];
+  }
 
 }
 //____________________________________________________________________________
@@ -170,6 +146,55 @@ AliEMCALRecPoint::~AliEMCALRecPoint()
     delete[] fParentsList;
    if ( fDEParentsList)
     delete[] fDEParentsList;
+
+   delete fLocPosM ;
+   delete [] fDigitsList ;
+   delete [] fTracksList ;
+}
+
+//____________________________________________________________________________
+AliEMCALRecPoint& AliEMCALRecPoint::operator= (const AliEMCALRecPoint &rp)
+{
+  // assignment operator
+
+  if(&rp == this) return *this;
+
+  fGeomPtr = rp.fGeomPtr;
+  fAmp = rp.fAmp;
+  fIndexInList = rp.fIndexInList;
+  fLocPos = rp.fLocPos;
+  fLocPosM = rp.fLocPosM;
+  fMaxDigit = rp.fMaxDigit;
+  fMulDigit = rp.fMulDigit;
+  fMaxTrack = rp.fMaxTrack;
+  fMulTrack = rp.fMaxTrack;
+  for(Int_t i = 0; i<fMaxDigit; i++) fDigitsList[i] = rp.fDigitsList[i];
+  for(Int_t i = 0; i<fMaxTrack; i++) fTracksList[i] = rp.fTracksList[i];
+  fClusterType = rp.fClusterType;
+  fCoreEnergy = rp.fCoreEnergy; 
+  fDispersion = rp.fDispersion;
+  for(Int_t i = 0; i<fMaxDigit; i++) {
+    fEnergyList[i] = rp.fEnergyList[i];
+    fTimeList[i] = rp.fTimeList[i]; 
+    fAbsIdList[i] = rp.fAbsIdList[i];
+  }
+  fTime = rp.fTime;
+  fCoreRadius = rp.fCoreRadius;
+  for(Int_t i = 0; i < fMaxTrack; i++) fDETracksList[i] = rp.fDETracksList[i];
+  fMulParent = rp.fMulParent;
+  fMaxParent = rp.fMaxParent;
+  for(Int_t i = 0; i < fMaxParent; i++) {
+    fParentsList[i] = rp.fParentsList[i]; 
+    fDEParentsList[i] = rp.fDEParentsList[i];
+  }
+  fSuperModuleNumber = rp.fSuperModuleNumber;
+  fDigitIndMax = rp.fDigitIndMax;
+
+  fLambda[0] = rp.fLambda[0];
+  fLambda[1] = rp.fLambda[1];
+
+  return *this;
+
 }
 
 //____________________________________________________________________________
@@ -184,7 +209,6 @@ void AliEMCALRecPoint::AddDigit(AliEMCALDigit & digit, Float_t Energy)
     fTimeList =  new Float_t[fMaxDigit]; 
   if(fAbsIdList == 0) {
     fAbsIdList =  new Int_t[fMaxDigit];
-    fSuperModuleNumber = fGeomPtr->GetSuperModuleNumber(digit.GetId());
   }
 
   if ( fMulDigit >= fMaxDigit ) { // increase the size of the lists 
@@ -202,38 +226,31 @@ void AliEMCALRecPoint::AddDigit(AliEMCALDigit & digit, Float_t Energy)
       tempoId[index] = fAbsIdList[index] ; 
     }
     
-    delete [] fDigitsList ; 
-    fDigitsList =  new Int_t[fMaxDigit];
+    delete [] fDigitsList ;
     delete [] fEnergyList ;
-    fEnergyList =  new Float_t[fMaxDigit];
-
     delete [] fTimeList ;
-    fTimeList =  new Float_t[fMaxDigit];
-
     delete [] fAbsIdList ;
-    fAbsIdList =  new Int_t[fMaxDigit];
 
-    for ( index = 0 ; index < fMulDigit ; index++ ){
-      fDigitsList[index] = tempo[index] ;
-      fEnergyList[index] = tempoE[index] ; 
-      fTimeList[index] = tempoT[index] ; 
-      fAbsIdList[index]  = tempoId[index] ; 
-    }
-    delete [] tempo ;
-    delete [] tempoE ; 
-    delete [] tempoT ; 
-    delete [] tempoId ; 
+    fDigitsList = tempo;
+    fEnergyList = tempoE; 
+    fTimeList = tempoT;
+    fAbsIdList = tempoId;
   } // if
   
   fDigitsList[fMulDigit]   = digit.GetIndexInList()  ; 
   fEnergyList[fMulDigit]   = Energy ;
-  fTimeList[fMulDigit]     = digit.GetTime() ;
+  fTimeList[fMulDigit]     = digit.GetTimeR() ;
   fAbsIdList[fMulDigit]    = digit.GetId();
   fMulDigit++ ; 
   fAmp += Energy ; 
 
+  //JLK 10-Oct-2007 this hasn't been filled before because it was in
+  //the wrong place in previous versions.
+  //Now we evaluate it only if the supermodulenumber for this recpoint
+  //has not yet been set (or is the 0th one)
+  if(fSuperModuleNumber == 0)
+    fSuperModuleNumber = fGeomPtr->GetSuperModuleNumber(digit.GetId());
+
 }
 //____________________________________________________________________________
 Bool_t AliEMCALRecPoint::AreNeighbours(AliEMCALDigit * digit1, AliEMCALDigit * digit2 ) const
@@ -334,7 +351,7 @@ void AliEMCALRecPoint::ExecuteEvent(Int_t /*event*/, Int_t, Int_t)
 
   //  static Int_t pxold, pyold;
 
-  /* static TGraph *  digitgraph = 0 ;
+  /*  static TGraph *  digitgraph = 0 ;
   static TPaveText* clustertext = 0 ;
   
   if (!gPad->IsEditable()) return;
@@ -344,7 +361,6 @@ void AliEMCALRecPoint::ExecuteEvent(Int_t /*event*/, Int_t, Int_t)
     
   case kButton1Down:{
     AliEMCALDigit * digit ;
-    AliEMCALGeometry * emcalgeom =  (AliEMCALGetter::Instance())->EMCALGeometry() ;
 
     Int_t iDigit;
     Int_t relid[2] ;
@@ -356,8 +372,8 @@ void AliEMCALRecPoint::ExecuteEvent(Int_t /*event*/, Int_t, Int_t)
     for(iDigit = 0; iDigit < kMulDigit; iDigit++) {
       Fatal("AliEMCALRecPoint::ExecuteEvent", " -> Something wrong with the code"); 
       digit = 0 ; //dynamic_cast<AliEMCALDigit *>((fDigitsList)[iDigit]);
-      emcalgeom->AbsToRelNumbering(digit->GetId(), relid) ;
-      emcalgeom->PosInAlice(relid, xi[iDigit], zi[iDigit]) ;
+      fGeomPtr->AbsToRelNumbering(digit->GetId(), relid) ;
+      fGeomPtr->PosInAlice(relid, xi[iDigit], zi[iDigit]) ;
     }
     
     if (!digitgraph) {
@@ -406,22 +422,17 @@ break;
 void AliEMCALRecPoint::EvalAll(Float_t logWeight,TClonesArray * digits) 
 {
   // Evaluates all shower parameters
-
   EvalLocalPosition(logWeight, digits) ;
-  //  printf("eval position done\n");
   EvalElipsAxis(logWeight, digits) ;
-  //  printf("eval axis done\n");
   EvalDispersion(logWeight, digits) ;
-  //  printf("eval dispersion done\n");
   //EvalCoreEnergy(logWeight, digits);
- // printf("eval energy done\n");
   EvalTime(digits) ;
-  //  printf("eval time done\n");
-
   EvalPrimaries(digits) ;
-  //  printf("eval pri done\n");
   EvalParents(digits);
-  //  printf("eval parent done\n");
+
+  //Called last because it sets the global position of the cluster?
+  EvalLocal2TrackingCSTransform();
+
 }
 
 //____________________________________________________________________________
@@ -491,15 +502,25 @@ void AliEMCALRecPoint::EvalLocalPosition(Float_t logWeight, TClonesArray * digit
   // Calculates the center of gravity in the local EMCAL-module coordinates 
   //  Info("Print", " logWeight %f : cluster energy %f ", logWeight, fAmp); // for testing
   
+  static Double_t dist;
+
   AliEMCALDigit * digit;
-  Int_t i=0, nstat=0;
+  Int_t i=0, nstat=0, idMax=-1;
   Double_t clXYZ[3]={0.,0.,0.}, clRmsXYZ[3]={0.,0.,0.}, xyzi[3], wtot=0., w=0.;
 
+  //printf(" dist : %f  e : %f \n", dist, fAmp);
   for(Int_t iDigit=0; iDigit<fMulDigit; iDigit++) {
     digit = dynamic_cast<AliEMCALDigit *>(digits->At(fDigitsList[iDigit])) ;
+    if(iDigit==0) {
+      idMax = digit->GetId(); // is it correct
+      dist  = TmaxInCm(Double_t(fAmp));
+    }
+    fGeomPtr->RelPosCellInSModule(digit->GetId(), idMax, dist, xyzi[0], xyzi[1], xyzi[2]);
+    //printf(" Id %i : dist %f Local x,y,z %f %f %f \n", digit->GetId(), dist, xyzi[0], xyzi[1], xyzi[2]);
 
-    fGeomPtr->RelPosCellInSModule(digit->GetId(), xyzi[0], xyzi[1], xyzi[2]);
-    // printf(" Id %i : Local x,y,z %f %f %f \n", digit->GetId(), xyzi[0], xyzi[1], xyzi[2]);
+    //fGeomPtr->RelPosCellInSModule(digit->GetId(), xyzi[0], xyzi[1], xyzi[2]);
+    //printf(" Id %i : dist %f Local x,y,z %f %f %f \n", digit->GetId(), 0.0, xyzi[0], xyzi[1], xyzi[2]);
+    //    if(fAmp>102.) assert(0);
 
     if(logWeight > 0.0)  w = TMath::Max( 0., logWeight + TMath::Log( fEnergyList[iDigit] / fAmp ));
     else  w = fEnergyList[iDigit]; // just energy
@@ -541,9 +562,168 @@ void AliEMCALRecPoint::EvalLocalPosition(Float_t logWeight, TClonesArray * digit
   fLocPosM = 0 ; // covariance matrix
 }
 
-//void AliEMCALRecPoint::EvalLocalPositionSimple()
-//{ // Weight is proportional of cell energy 
-//}
+//____________________________________________________________________________
+void AliEMCALRecPoint::EvalLocalPositionFit(Double_t deff, Double_t logWeight, 
+Double_t phiSlope, TClonesArray * digits)
+{
+  // Aug 14-16, 2007 - for fit 
+  // Aug 31 - should be static ??
+  static Double_t dist, ycorr;
+  static AliEMCALDigit *digit;
+
+  Int_t i=0, nstat=0, idMax=-1;
+  Double_t clXYZ[3]={0.,0.,0.}, clRmsXYZ[3]={0.,0.,0.}, xyzi[3], wtot=0., w=0.; 
+
+  for(Int_t iDigit=0; iDigit<digits->GetEntries(); iDigit++) {
+    digit = dynamic_cast<AliEMCALDigit *>(digits->At(fDigitsList[iDigit])) ;
+    if(iDigit==0) {
+      idMax = digit->GetId(); // is it correct
+      dist  = TmaxInCm(Double_t(fAmp));
+    }
+
+    dist = deff;
+    fGeomPtr->RelPosCellInSModule(digit->GetId(), idMax, dist, xyzi[0], xyzi[1], xyzi[2]);
+
+    if(logWeight > 0.0)  w = TMath::Max( 0., logWeight + TMath::Log( fEnergyList[iDigit] / fAmp ));
+    else                 w = fEnergyList[iDigit]; // just energy
+
+    if(w>0.0) {
+      wtot += w ;
+      nstat++;
+      for(i=0; i<3; i++ ) {
+        clXYZ[i]    += (w*xyzi[i]);
+        clRmsXYZ[i] += (w*xyzi[i]*xyzi[i]);
+      }
+    }
+  }
+  //  cout << " wtot " << wtot << endl;
+  if ( wtot > 0 ) { 
+    //    xRMS   = TMath::Sqrt(x2m - xMean*xMean);
+    for(i=0; i<3; i++ ) {
+      clXYZ[i] /= wtot;
+      if(nstat>1) {
+        clRmsXYZ[i] /= (wtot*wtot);  
+        clRmsXYZ[i]  =  clRmsXYZ[i] - clXYZ[i]*clXYZ[i];
+       if(clRmsXYZ[i] > 0.0) {
+          clRmsXYZ[i] =  TMath::Sqrt(clRmsXYZ[i]);
+        } else      clRmsXYZ[i] = 0;
+      } else        clRmsXYZ[i] = 0;
+    }    
+  } else {
+    for(i=0; i<3; i++ ) {
+      clXYZ[i] = clRmsXYZ[i] = -1.;
+    }
+  }
+  // clRmsXYZ[i] ??
+  if(phiSlope != 0.0 && logWeight > 0.0 && wtot) { 
+    // Correction in phi direction (y - coords here); Aug 16;
+    // May be put to global level or seperate method
+    ycorr = clXYZ[1] * (1. + phiSlope);
+    //printf(" y %f : ycorr %f : slope %f \n", clXYZ[1], ycorr, phiSlope); 
+    clXYZ[1] = ycorr;
+  }
+  fLocPos.SetX(clXYZ[0]);
+  fLocPos.SetY(clXYZ[1]);
+  fLocPos.SetZ(clXYZ[2]);
+    
+//  if (gDebug==2)
+//    printf("EvalLocalPosition: eta,phi,r = %f,%f,%f", fLocPos.X(), fLocPos.Y(), fLocPos.Z()) ; 
+  fLocPosM = 0 ; // covariance matrix
+}
+
+//_____________________________________________________________________________
+Bool_t AliEMCALRecPoint::EvalLocalPosition2(TClonesArray * digits, TArrayD &ed)
+{
+  // Evaluated local position of rec.point using digits 
+  // and parametrisation of w0 and deff
+  //printf(" <I> AliEMCALRecPoint::EvalLocalPosition2() \n"); 
+  return AliEMCALRecPoint::EvalLocalPositionFromDigits(digits, ed, fLocPos);
+}
+
+//_____________________________________________________________________________
+Bool_t AliEMCALRecPoint::EvalLocalPositionFromDigits(TClonesArray *digits, TArrayD &ed, TVector3 &locPos)
+{
+  // Used when digits should be recalibrated
+  static Double_t deff, w0, esum;
+  static Int_t iDigit;
+  //  static AliEMCALDigit *digit;
+
+  if(ed.GetSize() && (digits->GetEntries()!=ed.GetSize())) return kFALSE;
+
+  // Calculate sum energy of digits
+  esum = 0.0;
+  for(iDigit=0; iDigit<ed.GetSize(); iDigit++) esum += ed[iDigit];
+
+  GetDeffW0(esum, deff, w0);
+  
+  return EvalLocalPositionFromDigits(esum, deff, w0, digits, ed, locPos); 
+}
+
+//_____________________________________________________________________________
+Bool_t AliEMCALRecPoint::EvalLocalPositionFromDigits(const Double_t esum, const Double_t deff, const Double_t w0, TClonesArray *digits, TArrayD &ed, TVector3 &locPos)
+{
+  static AliEMCALDigit *digit;
+
+  Int_t i=0, nstat=0, idMax=-1;
+  Double_t clXYZ[3]={0.,0.,0.}, xyzi[3], wtot=0., w=0.; 
+
+  // Get pointer to EMCAL geometry
+  // (can't use fGeomPtr in static method)
+  AliEMCALGeometry* geo = AliEMCALGeometry::GetInstance(); 
+
+  for(Int_t iDigit=0; iDigit<digits->GetEntries(); iDigit++) {
+    digit = dynamic_cast<AliEMCALDigit *>(digits->At(iDigit));
+
+    geo->RelPosCellInSModule(digit->GetId(), idMax, deff, xyzi[0], xyzi[1], xyzi[2]);
+
+    if(w0 > 0.0)  w = TMath::Max( 0., w0 + TMath::Log(ed[iDigit] / esum));
+    else          w = ed[iDigit]; // just energy
+
+    if(w>0.0) {
+      wtot += w ;
+      nstat++;
+      for(i=0; i<3; i++ ) {
+        clXYZ[i] += (w*xyzi[i]);
+      }
+    }
+  }
+  //  cout << " wtot " << wtot << endl;
+  if (wtot > 0) { 
+    for(i=0; i<3; i++ ) {
+      clXYZ[i] /= wtot;
+    }
+    locPos.SetX(clXYZ[0]);
+    locPos.SetY(clXYZ[1]);
+    locPos.SetZ(clXYZ[2]);
+    return kTRUE;
+  } else {
+    return kFALSE;
+  }
+
+}
+
+//_____________________________________________________________________________
+void AliEMCALRecPoint::GetDeffW0(const Double_t esum , Double_t &deff,  Double_t &w0)
+{
+  //
+  // Aug 31, 2001 
+  // Applied for simulation data with threshold 3 adc
+  // Calculate efective distance (deff) and weigh parameter (w0) 
+  // for coordinate calculation; 0.5 GeV < esum <100 GeV.
+  // Look to:  http://rhic.physics.wayne.edu/~pavlinov/ALICE/SHISHKEBAB/RES/CALIB/GEOMCORR/deffandW0VaEgamma_2.gif
+  //
+  static Double_t e=0.0;
+  const  Double_t kdp0=9.25147, kdp1=1.16700; // Hard coded now
+  const  Double_t kwp0=4.83713, kwp1=-2.77970e-01, kwp2 = 4.41116;
+
+  // No extrapolation here
+  e = esum<0.5?0.5:esum;
+  e = e>100.?100.:e;
+
+  deff = kdp0 + kdp1*TMath::Log(e);
+  w0   = kwp0 / (1. + TMath::Exp(kwp1*(e+kwp2)));
+  //printf("<I> AliEMCALRecPoint::GetDeffW0 esum %5.2f : deff %5.2f : w0 %5.2f \n", esum, deff, w0); 
+}
 
 //______________________________________________________________________________
 void AliEMCALRecPoint::EvalCoreEnergy(Float_t logWeight, TClonesArray * digits)
@@ -682,7 +862,7 @@ void  AliEMCALRecPoint::EvalPrimaries(TClonesArray * digits)
     for ( jndex = 0 ; jndex < nprimaries ; jndex++ ) { // all primaries in digit
       if ( fMulTrack > fMaxTrack ) {
        fMulTrack = fMaxTrack ;
-       Error("GetNprimaries", "increase fMaxTrack ")  ;
+       Error("EvalPrimaries", "increase fMaxTrack ")  ;
        break ;
       }
       Int_t newPrimary = digit->GetPrimary(jndex+1);
@@ -727,6 +907,8 @@ void  AliEMCALRecPoint::EvalParents(TClonesArray * digits)
 
   Int_t index ;  
   for ( index = 0 ; index < GetDigitsMultiplicity() ; index++ ) { // all digits
+    if (fDigitsList[index] >= digits->GetEntries() || fDigitsList[index] < 0)
+       AliError(Form("Trying to get invalid digit %d (idx in WriteRecPoint %d)",fDigitsList[index],index));
     digit = dynamic_cast<AliEMCALDigit *>(digits->At( fDigitsList[index] )) ; 
     Int_t nparents = digit->GetNiparent() ;
     if ( nparents == 0 ) continue ;
@@ -735,7 +917,7 @@ void  AliEMCALRecPoint::EvalParents(TClonesArray * digits)
     for ( jndex = 0 ; jndex < nparents ; jndex++ ) { // all primaries in digit
       if ( fMulParent > fMaxParent ) {
        fMulTrack = - 1 ;
-       Error("GetNiparent", "increase fMaxParent")  ;
+       Error("EvalParents", "increase fMaxParent")  ;
        break ;
       }
       Int_t newParent = digit->GetIparent(jndex+1) ;
@@ -749,7 +931,7 @@ void  AliEMCALRecPoint::EvalParents(TClonesArray * digits)
          break ;
        }
       } // end of check
-      if ( !already && (fMulTrack < fMaxTrack)) { // store it
+      if ( !already && (fMulParent < fMaxParent)) { // store it
        parentArray[fMulParent] = newParent ; 
        dEParentArray[fMulParent] = newdEParent ; 
        fMulParent++ ;
@@ -774,11 +956,10 @@ void  AliEMCALRecPoint::EvalParents(TClonesArray * digits)
 //____________________________________________________________________________
 void AliEMCALRecPoint::GetLocalPosition(TVector3 & lpos) const
 {
-  // returns the position of the cluster in the local reference system of ALICE
+  // returns the position of the cluster in the local reference system
+  // of the sub-detector
   
-  lpos.SetX(fLocPos.X()) ;
-  lpos.SetY(fLocPos.Y()) ;
-  lpos.SetZ(fLocPos.Z()) ;
+  lpos = fLocPos;
 }
 
 //____________________________________________________________________________
@@ -788,6 +969,54 @@ void AliEMCALRecPoint::GetGlobalPosition(TVector3 & gpos) const
   // These are now the Cartesian X, Y and Z
   //  cout<<" geom "<<geom<<endl;
   fGeomPtr->GetGlobal(fLocPos, gpos, fSuperModuleNumber);
+
+}
+
+//____________________________________________________________________________
+void AliEMCALRecPoint::GetGlobalPosition(TVector3 & gpos, TMatrixF & gmat) const
+{
+  // returns the position of the cluster in the global reference system of ALICE
+  // These are now the Cartesian X, Y and Z
+  //  cout<<" geom "<<geom<<endl;
+
+  //To be implemented
+  fGeomPtr->GetGlobalEMCAL(this, gpos, gmat);
+
+}
+
+//_____________________________________________________________________________
+void AliEMCALRecPoint::EvalLocal2TrackingCSTransform()
+{
+  //Evaluates local to "tracking" c.s. transformation (B.P.).
+  //All evaluations should be completed before calling for this
+  //function.                           
+  //See ALICE PPR Chapter 5 p.18 for "tracking" c.s. definition,
+  //or just ask Jouri Belikov. :) 
+
+  SetVolumeId(AliGeomManager::LayerToVolUID(AliGeomManager::kEMCAL,GetSuperModuleNumber()));
+
+  const TGeoHMatrix* tr2loc = GetTracking2LocalMatrix();
+  if(!tr2loc) AliFatal(Form("No Tracking2LocalMatrix found."));
+
+  Double_t lxyz[3] = {fLocPos.X(),fLocPos.Y(),fLocPos.Z()};
+  Double_t txyz[3] = {0,0,0};
+
+  tr2loc->MasterToLocal(lxyz,txyz);
+  SetX(txyz[0]); SetY(txyz[1]); SetZ(txyz[2]);
+
+  if(AliLog::GetGlobalDebugLevel()>0) {
+    TVector3 gpos; TMatrixF gmat;
+    GetGlobalPosition(gpos,gmat);
+    Float_t gxyz[3];
+    GetGlobalXYZ(gxyz);
+    AliInfo(Form("lCS-->(%.3f,%.3f,%.3f), tCS-->(%.3f,%.3f,%.3f), gCS-->(%.3f,%.3f,%.3f),  gCScalc-\
+->(%.3f,%.3f,%.3f), supermodule %d",
+                 fLocPos.X(),fLocPos.Y(),fLocPos.Z(),
+                 GetX(),GetY(),GetZ(),
+                 gpos.X(),gpos.Y(),gpos.Z(),
+                 gxyz[0],gxyz[1],gxyz[2],GetSuperModuleNumber()));
+  }
+
 }
 
 //____________________________________________________________________________
@@ -923,6 +1152,26 @@ void AliEMCALRecPoint::Paint(Option_t *)
   gPad->PaintPolyMarker(1,&x,&y,"") ;
 }
 
+//_____________________________________________________________________
+Double_t AliEMCALRecPoint::TmaxInCm(const Double_t e , const Int_t key)
+{ 
+  // e energy in GeV)
+  // key  =  0(gamma, default)
+  //     !=  0(electron)
+  static Double_t ca = 4.82;  // shower max parameter - first guess; ca=TMath::Log(1000./8.07)
+  static Double_t x0 = 1.23;  // radiation lenght (cm)
+  static Double_t tmax = 0.;   // position of electromagnetic shower max in cm
+
+  tmax = 0.0;
+  if(e>0.1) {
+    tmax = TMath::Log(e) + ca;
+    if      (key==0) tmax += 0.5; 
+    else             tmax -= 0.5;
+    tmax *= x0; // convert to cm
+  }
+  return tmax;
+}
+
 //______________________________________________________________________________
 Float_t AliEMCALRecPoint::EtaToTheta(Float_t arg) const
 {
@@ -938,10 +1187,10 @@ Float_t AliEMCALRecPoint::ThetaToEta(Float_t arg) const
 }
 
 //____________________________________________________________________________
-void AliEMCALRecPoint::Print(Option_t *) const
+void AliEMCALRecPoint::Print(Option_t *opt) const
 {
   // Print the list of digits belonging to the cluster
-  return;
+  if(strlen(opt)==0) return;
   TString message ; 
   message  = "AliEMCALRecPoint:\n" ;
   message +=  " digits # = " ; 
@@ -978,8 +1227,10 @@ void AliEMCALRecPoint::Print(Option_t *) const
   Info("Print", message.Data(), fClusterType, fMulDigit, fAmp, fCoreEnergy, fCoreRadius, fMulTrack, GetIndexInList() ) ;  
 }
 
+//___________________________________________________________
 Double_t  AliEMCALRecPoint::GetPointEnergy() const
 {
+  //Returns energy ....
   static double e;
   e=0.0;
   for(int ic=0; ic<GetMultiplicity(); ic++) e += double(fEnergyList[ic]);