]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EMCAL/AliEMCALReconstructor.cxx
This is a backward incompatible change in AliRoot. The following methods have been...
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALReconstructor.cxx
index a20a6a16a4f6c47ffc0160d77fefdeda24bc8078..0f0493465a36f85bea9076bd4ef5d6844df7e6a4 100644 (file)
 /* $Id$ */
 
 //_________________________________________________________________________
-//*--
-//*-- Yves Schutz (SUBATECH) 
+//--
+//-- Yves Schutz (SUBATECH) 
 // Reconstruction class. Redesigned from the old AliReconstructionner class and 
 // derived from STEER/AliReconstructor. 
 // 
+//-- Aleksei Pavlinov : added staf for EMCAL jet trigger 9Apr 25, 2008)
+//                    : fgDigitsArr should read just once at event
+
 // --- ROOT system ---
+#include <TList.h>
+#include <TClonesArray.h>
+#include <TH2.h>
+#include "TGeoManager.h"
+#include "TGeoMatrix.h"
 
 // --- Standard library ---
 
 // --- AliRoot header files ---
 #include "AliEMCALReconstructor.h"
 
-#include "AliRun.h"
-#include "AliESD.h"
-#include "AliRunLoader.h"
+#include "AliCodeTimer.h"
+#include "AliESDEvent.h"
+#include "AliESDCaloCluster.h"
+#include "AliESDCaloCells.h"
+#include "AliESDtrack.h"
 #include "AliEMCALLoader.h"
 #include "AliEMCALRawUtils.h"
+#include "AliEMCALDigit.h"
 #include "AliEMCALClusterizerv1.h"
 #include "AliEMCALRecPoint.h"
 #include "AliEMCALPID.h"
 #include "AliEMCALTrigger.h"
 #include "AliRawReader.h"
+#include "AliCDBEntry.h"
+#include "AliCDBManager.h"
+#include "AliEMCALGeometry.h"
+#include "AliEMCAL.h"
+#include "AliEMCALHistoUtilities.h"
+#include "AliESDVZERO.h"
+#include "AliCDBManager.h"
+#include "AliRunLoader.h"
+#include "AliRun.h"
+#include "AliEMCALTriggerData.h"
+#include "AliEMCALTriggerElectronics.h"
+#include "AliVZEROLoader.h"
 
+ClassImp(AliEMCALReconstructor) 
 
-ClassImp(AliEMCALReconstructor)
-
+const AliEMCALRecParam* AliEMCALReconstructor::fgkRecParam = 0;  // EMCAL rec. parameters
+AliEMCALRawUtils* AliEMCALReconstructor::fgRawUtils = 0;   // EMCAL raw utilities class
+AliEMCALClusterizer* AliEMCALReconstructor::fgClusterizer = 0;   // EMCAL clusterizer class
+TClonesArray*     AliEMCALReconstructor::fgDigitsArr = 0;  // shoud read just once at event
+AliEMCALTriggerElectronics* AliEMCALReconstructor::fgTriggerProcessor = 0x0;
 //____________________________________________________________________________
 AliEMCALReconstructor::AliEMCALReconstructor() 
-  : fDebug(kFALSE) 
+  : fDebug(kFALSE), fList(0), fGeom(0),fCalibData(0),fPedestalData(0) 
 {
   // ctor
-} 
 
-//____________________________________________________________________________
-AliEMCALReconstructor::AliEMCALReconstructor(const AliEMCALReconstructor & rec)
-  : AliReconstructor(rec),
-    fDebug(rec.fDebug)
-{
-  //copy ctor
-}
+  fgRawUtils = new AliEMCALRawUtils;
+
+  //To make sure we match with the geometry in a simulation file,
+  //let's try to get it first.  If not, take the default geometry
+  AliRunLoader *rl = AliRunLoader::Instance();
+  if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL")) {
+    fGeom = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
+  } else {
+    AliInfo(Form("Using default geometry in reconstruction"));
+    fGeom =  AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
+  }
+
+  //Get calibration parameters 
+  if(!fCalibData)
+    {
+               AliCDBEntry *entry = (AliCDBEntry*) 
+               AliCDBManager::Instance()->Get("EMCAL/Calib/Data");
+               if (entry) fCalibData =  (AliEMCALCalibData*) entry->GetObject();
+    }
+       
+  if(!fCalibData)
+               AliFatal("Calibration parameters not found in CDB!");
+       
+  //Get calibration parameters 
+  if(!fPedestalData)
+    {
+               AliCDBEntry *entry = (AliCDBEntry*) 
+               AliCDBManager::Instance()->Get("EMCAL/Calib/Pedestals");
+               if (entry) fPedestalData =  (AliCaloCalibPedestal*) entry->GetObject();
+    }
+       
+       if(!fPedestalData)
+               AliFatal("Dead map not found in CDB!");
+       
+       
+  //Init the clusterizer with geometry and calibration pointers, avoid doing it twice.
+  fgClusterizer = new AliEMCALClusterizerv1(fGeom, fCalibData,fPedestalData); 
+       
+  if(!fGeom) AliFatal(Form("Could not get geometry!"));
+
+  fgTriggerProcessor = new AliEMCALTriggerElectronics();
+} 
 
 //____________________________________________________________________________
 AliEMCALReconstructor::~AliEMCALReconstructor()
 {
   // dtor
+  delete fGeom;
+  delete fgRawUtils;
+  delete fgClusterizer;
+  delete fgTriggerProcessor;
+
+  AliCodeTimer::Instance()->Print();
 } 
 
 //____________________________________________________________________________
-void AliEMCALReconstructor::Reconstruct(AliRunLoader* runLoader) const 
+void AliEMCALReconstructor::Init()
+{
+  // Trigger hists - Oct 24, 2007
+  fList = AliEMCALHistoUtilities::GetTriggersListOfHists(kTRUE);
+}
+
+//____________________________________________________________________________
+void AliEMCALReconstructor::Reconstruct(TTree* digitsTree, TTree* clustersTree) const
 {
   // method called by AliReconstruction; 
   // Only the clusterization is performed,; the rest of the reconstruction is done in FillESD because the track
   // segment maker needs access to the AliESD object to retrieve the tracks reconstructed by 
   // the global tracking.
-  TString headerFile(runLoader->GetFileName()) ; 
-  TString branchName(runLoader->GetEventFolder()->GetName() ) ;  
+  // Works on the current event.
+
+  AliCodeTimerAuto("",0)
+
+  ReadDigitsArrayFromTree(digitsTree);
+  fgClusterizer->InitParameters();
+  fgClusterizer->SetOutput(clustersTree);
+
+  AliEMCALTriggerData* trgData = new AliEMCALTriggerData();
+       
+  Int_t bufferSize = 32000;
+
+  if (TBranch* triggerBranch = clustersTree->GetBranch("EMTRG"))
+         triggerBranch->SetAddress(&trgData);
+  else
+         clustersTree->Branch("EMTRG","AliEMCALTriggerData",&trgData,bufferSize);
+
+  AliVZEROLoader* vzeroLoader = dynamic_cast<AliVZEROLoader*>(AliRunLoader::Instance()->GetDetectorLoader("VZERO"));
   
-  AliEMCALClusterizerv1 clu(headerFile, branchName);
-  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(runLoader->GetDetectorLoader("EMCAL"));    
-
-  Int_t nEvents   = runLoader->GetNumberOfEvents();
-  runLoader->LoadDigits("EMCAL");
-  for (Int_t ievent = 0; ievent < nEvents; ievent++) {
-    runLoader->GetEvent(ievent);
-    if ( Debug() ) 
-      clu.ExecuteTask("deb all") ; 
-    else 
-      clu.ExecuteTask("pseudo") ;  
-  }   
-  // Unload the Digits and RecPoints
-  emcalLoader->UnloadDigits() ; 
-  emcalLoader->UnloadRecPoints() ; 
+  TTree* treeV0 = 0x0;
+       
+  if (vzeroLoader) 
+  {
+         vzeroLoader->LoadDigits("READ");
+      treeV0 = vzeroLoader->TreeD();
+  }
+
+  TClonesArray *trgDigits = new TClonesArray("AliEMCALRawDigit",1000);
+  TBranch *branchdig = digitsTree->GetBranch("EMTRG");
+  if (!branchdig) 
+  { 
+         AliError("Can't get the branch with the EMCAL trigger digits !");
+         return;
+  }
+
+  branchdig->SetAddress(&trgDigits);
+  branchdig->GetEntry(0);
+
+  fgTriggerProcessor->Digits2Trigger(trgDigits, treeV0, trgData);
+       
+  trgDigits->Delete();
+       
+  if(fgDigitsArr && fgDigitsArr->GetEntries()) {
+
+    fgClusterizer->SetInput(digitsTree);
+    
+    if(Debug())
+      fgClusterizer->Digits2Clusters("deb all") ;
+    else
+      fgClusterizer->Digits2Clusters("");
+    
+    fgClusterizer->Clear();
+
+  }
+
+  if (vzeroLoader) 
+  {
+         vzeroLoader->UnloadDigits();
+  }
+
+  clustersTree->Fill();        
+
+  delete trgData;
 }
 
 //____________________________________________________________________________
-void AliEMCALReconstructor::Reconstruct(AliRunLoader* runLoader, AliRawReader* rawReader) const 
+void AliEMCALReconstructor::ConvertDigits(AliRawReader* rawReader, TTree* digitsTree) const
 
 {
-  // Reconstruction loop for Raw Data processing
-  //
-  // Only the clusterization is performed
-  // Track-cluster matching is done in FillESD because the track
-  // segment maker needs access to the AliESD object to retrieve 
-  // the tracks reconstructed by the global tracking.
-  
-  TString headerFile(runLoader->GetFileName()) ; 
-  TString branchName(runLoader->GetEventFolder()->GetName()) ;  
-
-  static AliEMCALRawUtils rawUtils;
-  AliEMCALClusterizerv1 clu(headerFile, branchName);
-  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(runLoader->GetDetectorLoader("EMCAL"));    
-  Int_t iEvent = 0;
+  // Conversion from raw data to
+  // EMCAL digits.
+  // Works on a single-event basis
+
   rawReader->Reset() ; 
-  while (rawReader->NextEvent()) {  
-    runLoader->GetEvent(iEvent++);
 
-    rawUtils.Raw2Digits(rawReader);
+  TClonesArray *digitsArr = new TClonesArray("AliEMCALDigit",200);
+  TClonesArray *digitsTrg = new TClonesArray("AliEMCALRawDigit", 200);
+
+  Int_t bufsize = 32000;
+  digitsTree->Branch("EMCAL", &digitsArr, bufsize);
+  digitsTree->Branch("EMTRG", &digitsTrg, bufsize);
+
+  //must be done here because, in constructor, option is not yet known
+  fgRawUtils->SetOption(GetOption());
+
+  fgRawUtils->SetRawFormatHighLowGainFactor(GetRecParam()->GetHighLowGainFactor());
+  fgRawUtils->SetRawFormatOrder(GetRecParam()->GetOrderParameter());
+  fgRawUtils->SetRawFormatTau(GetRecParam()->GetTau());
+  fgRawUtils->SetNoiseThreshold(GetRecParam()->GetNoiseThreshold());
+  fgRawUtils->SetNPedSamples(GetRecParam()->GetNPedSamples());
+  fgRawUtils->SetRemoveBadChannels(GetRecParam()->GetRemoveBadChannels());
+  fgRawUtils->SetFittingAlgorithm(GetRecParam()->GetFittingAlgorithm());
+  fgRawUtils->SetFALTROUsage(GetRecParam()->UseFALTRO());
+       
+  fgRawUtils->Raw2Digits(rawReader,digitsArr,fPedestalData,digitsTrg);
+
+  digitsTree->Fill();
+  digitsArr->Delete();
+  digitsTrg->Delete();
+  delete digitsArr;
+  delete digitsTrg;
 
-    if ( Debug() ) 
-      clu.ExecuteTask("deb pseudo all") ; 
-    else 
-      clu.ExecuteTask("pseudo") ;  
-    // Unload the RecPoints
-    emcalLoader->UnloadRecPoints() ; 
-  }
 }
 
+
 //____________________________________________________________________________
-void AliEMCALReconstructor::FillESD(AliRunLoader* runLoader, AliESD* esd) const
+void AliEMCALReconstructor::FillESD(TTree* digitsTree, TTree* clustersTree, 
+                                   AliESDEvent* esd) const
 {
   // Called by AliReconstruct after Reconstruct() and global tracking and vertexing 
-  const double timeScale = 1.e+11; // transition constant from sec to 0.01 ns 
-
-  Int_t eventNumber = runLoader->GetEventNumber() ;
-
-  // Creates AliESDCaloCluster from AliEMCALRecPoints 
-  AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(runLoader->GetDetectorLoader("EMCAL"));
-  runLoader->LoadRecPoints("EMCAL");
-  runLoader->GetEvent(eventNumber);
-  TObjArray *clusters = emcalLoader->RecPoints();
-  Int_t nClusters = clusters->GetEntries(), nClustersNew=0;
-  AliDebug(1,Form("Event %d: %d clusters",eventNumber,nClusters));
-  //  Int_t nRP=0, nPC=0; // in input
-  esd->SetFirstEMCALCluster(esd->GetNumberOfCaloClusters()); // Put after Phos clusters 
+  // and V0 
+  // Works on the current event
+  //  printf(" ## AliEMCALReconstructor::FillESD() is started ### \n ");
+  //return;
 
+  //######################################################
   //#########Calculate trigger and set trigger info###########
-  AliEMCALTrigger tr ;
-  //   tr.SetPatchSize(1);//create 4x4 patches
+  //######################################################
+  // Obsolete, to be changed with new trigger emulator when consensus is achieved about what is stored in ESDs.
+  AliEMCALTrigger tr;
+  //   tr.SetPatchSize(1);  // create 4x4 patches
+  tr.SetSimulation(kFALSE); // Reconstruction mode
+  tr.SetDigitsList(fgDigitsArr);
+  // Get VZERO total multiplicity for jet trigger simulation 
+  // The simulation of jey trigger will be incorrect if no VZERO data 
+  // at ESD
+  AliESDVZERO* vZero = esd->GetVZEROData();
+  if(vZero) {
+    tr.SetVZER0Multiplicity(vZero->GetMTotV0A() + vZero->GetMTotV0C());
+  }
+  //
   tr.Trigger();
-  
+
   Float_t maxAmp2x2  = tr.Get2x2MaxAmplitude();
   Float_t maxAmpnxn  = tr.GetnxnMaxAmplitude();
   Float_t ampOutOfPatch2x2  = tr.Get2x2AmpOutOfPatch() ;
   Float_t ampOutOfPatchnxn  = tr.GetnxnAmpOutOfPatch() ;
 
-  AliEMCALGeometry * geom = 0;
-  if (runLoader->GetAliRun() && runLoader->GetAliRun()->GetDetector("EMCAL"))
-    geom = dynamic_cast<AliEMCAL*>(runLoader->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
-  if (geom == 0) 
-    geom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaulGeometryName());
-
   Int_t iSM2x2      = tr.Get2x2SuperModule();
   Int_t iSMnxn      = tr.GetnxnSuperModule();
-  Int_t iCellPhi2x2 = tr.Get2x2CellPhi();
-  Int_t iCellPhinxn = tr.GetnxnCellPhi();
-  Int_t iCellEta2x2 = tr.Get2x2CellEta();
-  Int_t iCellEtanxn = tr.GetnxnCellEta();
+  Int_t iModulePhi2x2 = tr.Get2x2ModulePhi();
+  Int_t iModulePhinxn = tr.GetnxnModulePhi();
+  Int_t iModuleEta2x2 = tr.Get2x2ModuleEta();
+  Int_t iModuleEtanxn = tr.GetnxnModuleEta();
 
-  AliDebug(2, Form("Trigger 2x2 max amp %f, out amp %f, SM %d, iphi %d ieta %d",  maxAmp2x2, ampOutOfPatch2x2, iSM2x2,iCellPhi2x2, iCellEta2x2));
-  AliDebug(2, Form("Trigger 4x4 max amp %f , out amp %f, SM %d, iphi %d, ieta %d",  maxAmpnxn, ampOutOfPatchnxn, iSMnxn,iCellPhinxn, iCellEtanxn));
+  AliDebug(2, Form("Trigger 2x2 max amp %f, out amp %f, SM %d, iphi %d ieta %d",  maxAmp2x2, ampOutOfPatch2x2, iSM2x2,iModulePhi2x2, iModuleEta2x2));
+  AliDebug(2, Form("Trigger 4x4 max amp %f , out amp %f, SM %d, iphi %d, ieta %d",  maxAmpnxn, ampOutOfPatchnxn, iSMnxn,iModulePhinxn, iModuleEtanxn));
 
   TVector3    pos2x2(-1,-1,-1);
   TVector3    posnxn(-1,-1,-1);
 
-  Int_t iAbsId2x2 = geom->GetAbsCellIdFromCellIndexes( iSM2x2, iCellPhi2x2, iCellEta2x2) ;
-  Int_t iAbsIdnxn = geom->GetAbsCellIdFromCellIndexes( iSMnxn, iCellPhinxn, iCellEtanxn) ;
-  geom->GetGlobal(iAbsId2x2, pos2x2);
-  geom->GetGlobal(iAbsIdnxn, posnxn);
+  Int_t iAbsId2x2 = fGeom->GetAbsCellIdFromCellIndexes( iSM2x2, iModulePhi2x2, iModuleEta2x2) ; // should be changed to Module
+  Int_t iAbsIdnxn = fGeom->GetAbsCellIdFromCellIndexes( iSMnxn, iModulePhinxn, iModuleEtanxn) ;
+  fGeom->GetGlobal(iAbsId2x2, pos2x2);
+  fGeom->GetGlobal(iAbsIdnxn, posnxn);
+  //printf(" iAbsId2x2 %i iAbsIdnxn %i \n", iAbsId2x2, iAbsIdnxn);
   
   TArrayF triggerPosition(6);
   triggerPosition[0] = pos2x2(0) ;   
@@ -184,128 +307,261 @@ void AliEMCALReconstructor::FillESD(AliRunLoader* runLoader, AliESD* esd) const
   triggerPosition[2] = pos2x2(2) ;  
   triggerPosition[3] = posnxn(0) ;   
   triggerPosition[4] = posnxn(1) ;   
-  triggerPosition[5] = posnxn(2) ;  
+  triggerPosition[5] = posnxn(2) ;
+  //printf(" triggerPosition ");
+  //for(int i=0; i<6; i++) printf(" %i %f : ", i, triggerPosition[i]);
 
   TArrayF triggerAmplitudes(4);
   triggerAmplitudes[0] = maxAmp2x2 ;   
   triggerAmplitudes[1] = ampOutOfPatch2x2 ;    
   triggerAmplitudes[2] = maxAmpnxn ;   
   triggerAmplitudes[3] = ampOutOfPatchnxn ;   
-
+  //printf("\n triggerAmplitudes ");
+  //for(int i=0; i<4; i++) printf(" %i %f : ", i, triggerAmplitudes[i]);
+  //printf("\n");
+  //tr.Print("");
+  //
+  // Trigger jet staff
+  //
+  if(tr.GetNJetThreshold()>0) {
+    // Jet phi/eta
+    Int_t n0 = triggerPosition.GetSize();
+    const TH2F *hpatch = tr.GetJetMatrixE();
+    triggerPosition.Set(n0 + 2);
+    for(Int_t i=0; i<2; i++) triggerPosition[n0+i] = hpatch->GetMean(i+1);   
+    // Add jet ampitudes
+    n0 = triggerAmplitudes.GetSize();
+    triggerAmplitudes.Set(n0 + tr.GetNJetThreshold());
+    Double_t *ampJet = tr.GetL1JetThresholds();
+    for(Int_t i=0; i<tr.GetNJetThreshold(); i++){
+      triggerAmplitudes[n0 + i] = Float_t(ampJet[i]);
+    }
+  }
   esd->AddEMCALTriggerPosition(triggerPosition);
   esd->AddEMCALTriggerAmplitudes(triggerAmplitudes);
+  // Fill trigger hists
+  AliEMCALHistoUtilities::FillTriggersListOfHists(fList,&triggerPosition,&triggerAmplitudes);
+
+  //########################################
+  //##############Fill CaloCells###############
+  //########################################
+
+  TClonesArray *digits = new TClonesArray("AliEMCALDigit",1000);
+  TBranch *branchdig = digitsTree->GetBranch("EMCAL");
+  if (!branchdig) { 
+    AliError("can't get the branch with the EMCAL digits !");
+    return;
+  }
+  branchdig->SetAddress(&digits);
+  digitsTree->GetEvent(0);
+  Int_t nDigits = digits->GetEntries(), idignew = 0 ;
+  AliDebug(1,Form("%d digits",nDigits));
+
+  AliESDCaloCells &emcCells = *(esd->GetEMCALCells());
+  emcCells.CreateContainer(nDigits);
+  emcCells.SetType(AliESDCaloCells::kEMCALCell);
+  Float_t energy = 0;
+  for (Int_t idig = 0 ; idig < nDigits ; idig++) {
+    const AliEMCALDigit * dig = (const AliEMCALDigit*)digits->At(idig);
+    if(dig->GetAmp() > 0 ){
+         energy = (static_cast<AliEMCALClusterizerv1*> (fgClusterizer))->Calibrate(dig->GetAmp(),dig->GetId());
+         if(energy > 0){ //Digits tagged as bad (dead, hot, not alive) are set to 0 in calibrate, remove them  
+                 emcCells.SetCell(idignew,dig->GetId(),energy, dig->GetTime());   
+                 idignew++;
+         }
+    }
+  }
+  emcCells.SetNumberOfCells(idignew);
+  emcCells.Sort();
+
+  //------------------------------------------------------------
+  //-----------------CLUSTERS-----------------------------
+  //------------------------------------------------------------
+  TObjArray *clusters = new TObjArray(100);
+  TBranch *branch = clustersTree->GetBranch("EMCALECARP");
+  branch->SetAddress(&clusters);
+  clustersTree->GetEvent(0);
+
+  Int_t nClusters = clusters->GetEntries(),  nClustersNew=0;
+  AliDebug(1,Form("%d clusters",nClusters));
+
+  //######################################################
+  //#######################TRACK MATCHING###############
+  //######################################################
+  //Fill list of integers, each one is index of track to which the cluster belongs.
+
+  // step 1 - initialize array of matched track indexes
+  Int_t *matchedTrack = new Int_t[nClusters];
+  for (Int_t iclus = 0; iclus < nClusters; iclus++)
+    matchedTrack[iclus] = -1;  // neg. index --> no matched track
   
-  //######################################
-
-  //Fill CaloClusters 
-
+  // step 2, change the flag for all matched clusters found in tracks
+  Int_t iemcalMatch = -1;
+  Int_t endtpc = esd->GetNumberOfTracks();
+  for (Int_t itrack = 0; itrack < endtpc; itrack++) {
+    AliESDtrack * track = esd->GetTrack(itrack) ; // retrieve track
+    iemcalMatch = track->GetEMCALcluster();
+    if(iemcalMatch >= 0) matchedTrack[iemcalMatch] = itrack;
+  } 
+  
+  //########################################
+  //##############Fill CaloClusters#############
+  //########################################
   for (Int_t iClust = 0 ; iClust < nClusters ; iClust++) {
-    const AliEMCALRecPoint * clust = emcalLoader->RecPoint(iClust);
-    //if(clust->GetClusterType()== AliESDCaloCluster::kClusterv1) nRP++; else nPC++;
+    const AliEMCALRecPoint * clust = (const AliEMCALRecPoint*)clusters->At(iClust);
+    //if(clust->GetClusterType()== AliESDCaloCluster::kEMCALClusterv1) nRP++; else nPC++;
     if (Debug()) clust->Print();
     // Get information from EMCAL reconstruction points
     Float_t xyz[3];
     TVector3 gpos;
     clust->GetGlobalPosition(gpos);
-    for (Int_t ixyz=0; ixyz<3; ixyz++) 
+    for (Int_t ixyz=0; ixyz<3; ixyz++)
       xyz[ixyz] = gpos[ixyz];
-
-    Int_t digitMult = clust->GetMultiplicity();
-    UShort_t *amplList = new UShort_t[digitMult];
-    UShort_t *timeList = new UShort_t[digitMult];
-    UShort_t *digiList = new UShort_t[digitMult];
-    Float_t *amplFloat = clust->GetEnergiesList();
-    Float_t *timeFloat = clust->GetTimeList();
-    Int_t   *digitInts = clust->GetAbsId();
     Float_t elipAxis[2];
     clust->GetElipsAxis(elipAxis);
-
-    // Convert Float_t* and Int_t* to UShort_t* to save memory
-    // Problem : we should recalculate a cluster characteristics when discard digit(s)
-    Int_t newdigitMult = 0; 
-    for (Int_t iDigit=0; iDigit<digitMult; iDigit++) {
-      if (amplFloat[iDigit] > 0) {
-       amplList[newdigitMult] = (UShort_t)(amplFloat[iDigit]*500);
-        // Time in units of 0.01 ns = 10 ps
-        if(timeFloat[iDigit] < 65536./timeScale) 
-         timeList[newdigitMult] = (UShort_t)(timeFloat[iDigit]*timeScale);
-        else
-          timeList[newdigitMult] = 65535;
-       digiList[newdigitMult] = (UShort_t)(digitInts[iDigit]);
-        newdigitMult++;
+       //Create digits lists
+    Int_t cellMult = clust->GetMultiplicity();
+    //TArrayS digiList(digitMult);
+    Float_t *amplFloat = clust->GetEnergiesList();
+    Int_t   *digitInts = clust->GetAbsId();
+    TArrayS absIdList(cellMult);
+    TArrayD fracList(cellMult);
+
+    Int_t newCellMult = 0;
+    for (Int_t iCell=0; iCell<cellMult; iCell++) {
+      if (amplFloat[iCell] > 0) {
+      absIdList[newCellMult] = (UShort_t)(digitInts[iCell]);
+      //Uncomment when unfolding is done
+      //if(emcCells.GetCellAmplitude(digitInts[iCell])>0)
+      //fracList[newCellMult] = amplFloat[iCell]/(emcCells.GetCellAmplitude(digitInts[iCell])*calibration);//get cell calibration value 
+      //else
+      fracList[newCellMult] = 0; 
+      newCellMult++;
       }
-      else if (clust->GetClusterType() != AliESDCaloCluster::kPseudoCluster)
-        Warning("FillESD()","Negative or 0 digit amplitude in cluster");
     }
 
-    if(newdigitMult > 0) { // accept cluster if it has some digit
+    absIdList.Set(newCellMult);
+    fracList.Set(newCellMult);
+    
+    if(newCellMult > 0) { // accept cluster if it has some digit
       nClustersNew++;
-      if(newdigitMult != digitMult) { // some digits were deleted
-        UShort_t *amplListNew = new UShort_t[newdigitMult];
-        UShort_t *timeListNew = new UShort_t[newdigitMult];
-        UShort_t *digiListNew = new UShort_t[newdigitMult];
-        for (Int_t iDigit=0; iDigit<newdigitMult; iDigit++) {
-          amplListNew[iDigit] = amplList[iDigit];
-          timeListNew[iDigit] = timeList[iDigit];
-          digiListNew[iDigit] = digiList[iDigit];
-        }
-
-        delete [] amplList;
-        delete [] timeList;
-        delete [] digiList;
-
-        amplList = amplListNew;
-        timeList = timeListNew;
-        digiList = digiListNew;
-      }
-
       //Primaries
-      Int_t  primMult  = 0;
-      Int_t *primInts =  clust->GetPrimaries(primMult);
-      UShort_t *primList = new UShort_t[primMult];
-      for (Int_t ipr=0; ipr<primMult; ipr++) 
-       primList[ipr] = (UShort_t)(primInts[ipr]);       
-
-
+      Int_t  parentMult  = 0;
+      Int_t *parentList =  clust->GetParents(parentMult);
       // fills the ESDCaloCluster
-      AliESDCaloCluster * ec = new AliESDCaloCluster() ; 
-      ec->SetEMCAL(kTRUE);
-      ec->SetClusterType(clust->GetClusterType());
-      ec->SetGlobalPosition(xyz);
-      ec->SetClusterEnergy(clust->GetEnergy());
-      
-      ec->SetNumberOfDigits(newdigitMult);
-      ec->SetDigitAmplitude(amplList); //energies
-      ec->SetDigitTime(timeList);      //times
-      ec->SetDigitIndex(digiList);     //indices
-      if(clust->GetClusterType()== AliESDCaloCluster::kClusterv1){
-        ec->SetPrimaryIndex(clust->GetPrimaryIndex());
-        ec->SetNumberOfPrimaries(primMult);           //primary multiplicity
-        ec->SetListOfPrimaries(primList);                  //primary List for a cluster  
-        ec->SetClusterDisp(clust->GetDispersion());
-        ec->SetClusterChi2(-1); //not yet implemented
-        ec->SetM02(elipAxis[0]*elipAxis[0]) ;
-        ec->SetM20(elipAxis[1]*elipAxis[1]) ;
-        ec->SetM11(-1) ;        //not yet implemented
-      } 
-    // add the cluster to the esd object
+      AliESDCaloCluster * ec = new AliESDCaloCluster() ;
+      ec->SetClusterType(AliESDCaloCluster::kEMCALClusterv1);
+      ec->SetPosition(xyz);
+      ec->SetE(clust->GetEnergy());
+               
+         //Distance to the nearest bad crystal
+         ec->SetDistanceToBadChannel(clust->GetDistanceToBadTower()); 
+
+      ec->SetNCells(newCellMult);
+      //Change type of list from short to ushort
+      UShort_t *newAbsIdList  = new UShort_t[newCellMult];
+      Double_t *newFracList  = new Double_t[newCellMult];
+      for(Int_t i = 0; i < newCellMult ; i++) {
+        newAbsIdList[i]=absIdList[i];
+        newFracList[i]=fracList[i];
+      }
+      ec->SetCellsAbsId(newAbsIdList);
+      ec->SetCellsAmplitudeFraction(newFracList);
+      ec->SetClusterDisp(clust->GetDispersion());
+      ec->SetClusterChi2(-1); //not yet implemented
+      ec->SetM02(elipAxis[0]*elipAxis[0]) ;
+      ec->SetM20(elipAxis[1]*elipAxis[1]) ;
+      ec->SetTOF(clust->GetTime()) ; //time-of-fligh
+      ec->SetNExMax(clust->GetNExMax());          //number of local maxima
+      TArrayI arrayTrackMatched(1);// Only one track, temporal solution.
+      arrayTrackMatched[0]= matchedTrack[iClust];
+      ec->AddTracksMatched(arrayTrackMatched);
+
+      TArrayI arrayParents(parentMult,parentList);
+      ec->AddLabels(arrayParents);
+
+      // add the cluster to the esd object
       esd->AddCaloCluster(ec);
       delete ec;
-    } else { // no new ESD cluster
-        delete [] amplList;
-        delete [] timeList;
-        delete [] digiList;
-    }
-  } // cycle on clusters
-  esd->SetNumberOfEMCALClusters(nClustersNew);
-  //if(nClustersNew != nClusters) 
-  //printf(" ##### nClusters %i -> new %i ##### \n", nClusters, nClustersNew );
-
-  //Fill ESDCaloCluster with PID weights
-  AliEMCALPID *pid = new AliEMCALPID;
-  //pid->SetPrintInfo(kTRUE);
-  pid->SetReconstructor(kTRUE);
-  pid->RunPID(esd);
+      delete [] newAbsIdList ;
+      delete [] newFracList ;
+   }
+ } // cycle on clusters
+
+ delete [] matchedTrack;
+
+ //Fill ESDCaloCluster with PID weights
+ AliEMCALPID *pid = new AliEMCALPID;
+ //pid->SetPrintInfo(kTRUE);
+ pid->SetReconstructor(kTRUE);
+ pid->RunPID(esd);
+ delete pid;
+  
+ delete digits;
+ delete clusters;
+  
+ //Store EMCAL misalignment matrixes
+ FillMisalMatrixes(esd) ;
+
+}
+
+//==================================================================================
+void AliEMCALReconstructor::FillMisalMatrixes(AliESDEvent* esd)const{
+       //Store EMCAL matrixes in ESD Header
+       
+       //Check, if matrixes was already stored
+       for(Int_t sm = 0 ; sm < fGeom->GetNumberOfSuperModules(); sm++){
+               if(esd->GetEMCALMatrix(sm)!=0)
+                       return ;
+       }
+       
+       //Create and store matrixes
+       if(!gGeoManager){
+               AliError("Can not store misal. matrixes: no gGeoManager! \n") ;
+               return ;
+       }
+       //Note, that owner of copied marixes will be header
+       char path[255] ;
+       TGeoHMatrix * m = 0x0;
+       for(Int_t sm = 0; sm < fGeom->GetNumberOfSuperModules(); sm++){
+               sprintf(path,"/ALIC_1/XEN1_1/SMOD_%d",sm+1) ; //In Geometry modules numbered 1,2,.,5
+               if(sm >= 10) sprintf(path,"/ALIC_1/XEN1_1/SM10_%d",sm-10+1) ;
+               
+               if (gGeoManager->CheckPath(path)){
+                       gGeoManager->cd(path);
+                       m = gGeoManager->GetCurrentMatrix() ;
+//                     printf("================================================= \n");
+//                     printf("AliEMCALReconstructor::FixMisalMatrixes(), sm %d, \n",sm);
+//                     m->Print("");
+                       esd->SetEMCALMatrix(new TGeoHMatrix(*m),sm) ;
+//                     printf("================================================= \n");
+               }
+               else{
+                       esd->SetEMCALMatrix(NULL,sm) ;
+               }
+       }
+}
+
+
+
+//__________________________________________________________________________
+void AliEMCALReconstructor::ReadDigitsArrayFromTree(TTree *digitsTree) const
+{
+  // See AliEMCALClusterizer::SetInput(TTree *digitsTree);
+  if(fgDigitsArr) {
+    // Clear previous digits 
+    fgDigitsArr->Delete();
+    delete fgDigitsArr;
+  }
+  // Read the digits from the input tree
+  TBranch *branch = digitsTree->GetBranch("EMCAL");
+  if (!branch) { 
+    AliError("can't get the branch with the EMCAL digits !");
+    return;
+  }
+  fgDigitsArr = new TClonesArray("AliEMCALDigit",100);
+  branch->SetAddress(&fgDigitsArr);
+  branch->GetEntry(0);
 }