]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliESDEvent.cxx
change in the dtor
[u/mrichter/AliRoot.git] / STEER / AliESDEvent.cxx
index 10a3878104e77e0cba4215089036200634acafca..a2c17ecd45779bce412fc6f6e63ff03929ab98de 100644 (file)
@@ -27,7 +27,7 @@
       esdTree->GetEntry(i);
       if(ev->GetAliESDOld())ev->CopyFromOldESD();
 */
-//   The AliESDInputHAndler does this automatically for you
+//   The AliESDInputHandler does this automatically for you
 //
 // Origin: Christian Klein-Boesing, CERN, Christian.Klein-Boesing@cern.ch
 //-----------------------------------------------------------------
@@ -41,7 +41,6 @@
 #include "AliESDVZERO.h"
 #include "AliESDFMD.h"
 #include "AliESD.h"
-#include "AliESDfriend.h"
 #include "AliESDMuonTrack.h"
 #include "AliESDPmdTrack.h"
 #include "AliESDTrdTrack.h"
 #include "AliESDtrack.h"
 #include "AliESDHLTtrack.h"
 #include "AliESDCaloCluster.h"
+#include "AliESDCaloCells.h"
 #include "AliESDv0.h"
 #include "AliESDFMD.h"
 #include "AliESDVZERO.h"
 #include "AliMultiplicity.h"
 #include "AliRawDataErrorLog.h"
-
-
+#include "AliLog.h"
+#include "AliESDACORDE.h"
 ClassImp(AliESDEvent)
 
 
@@ -74,6 +74,7 @@ ClassImp(AliESDEvent)
                                                       "AliESDFMD",
                                                       "AliESDVZERO",
                                                       "AliESDTZERO",
+                                                      "TPCVertex",
                                                       "SPDVertex",
                                                       "PrimaryVertex",
                                                       "AliMultiplicity",
@@ -87,7 +88,11 @@ ClassImp(AliESDEvent)
                                                       "Cascades",
                                                       "Kinks",
                                                       "CaloClusters",
-                                                      "AliRawDataErrorLogs"};
+                                                     "EMCALCells",
+                                                     "PHOSCells",
+                                                      "AliRawDataErrorLogs",
+                                                      "AliESDACORDE"};
+
 //______________________________________________________________________________
 AliESDEvent::AliESDEvent():
   AliVEvent(),
@@ -98,11 +103,13 @@ AliESDEvent::AliESDEvent():
   fESDFMD(0),
   fESDVZERO(0),
   fESDTZERO(0),
+  fTPCVertex(0),
   fSPDVertex(0),
   fPrimaryVertex(0),
   fSPDMult(0),
   fPHOSTrigger(0),
   fEMCALTrigger(0),
+  fESDACORDE(0),
   fTracks(0),
   fMuonTracks(0),
   fPmdTracks(0),
@@ -111,10 +118,12 @@ AliESDEvent::AliESDEvent():
   fCascades(0),
   fKinks(0),
   fCaloClusters(0),
+  fEMCALCells(0), fPHOSCells(0),
   fErrorLogs(0),
   fESDOld(0),
   fESDFriendOld(0),
   fConnected(kFALSE),
+  fUseOwnList(kFALSE),
   fEMCALClusters(0), 
   fFirstEMCALCluster(-1),
   fPHOSClusters(0), 
@@ -131,11 +140,13 @@ AliESDEvent::AliESDEvent(const AliESDEvent& esd):
   fESDFMD(new AliESDFMD(*esd.fESDFMD)),
   fESDVZERO(new AliESDVZERO(*esd.fESDVZERO)),
   fESDTZERO(new AliESDTZERO(*esd.fESDTZERO)),
+  fTPCVertex(new AliESDVertex(*esd.fTPCVertex)),
   fSPDVertex(new AliESDVertex(*esd.fSPDVertex)),
   fPrimaryVertex(new AliESDVertex(*esd.fPrimaryVertex)),
   fSPDMult(new AliMultiplicity(*esd.fSPDMult)),
   fPHOSTrigger(new AliESDCaloTrigger(*esd.fPHOSTrigger)),
   fEMCALTrigger(new AliESDCaloTrigger(*esd.fEMCALTrigger)),
+  fESDACORDE(new AliESDACORDE(*esd.fESDACORDE)),
   fTracks(new TClonesArray(*esd.fTracks)),
   fMuonTracks(new TClonesArray(*esd.fMuonTracks)),
   fPmdTracks(new TClonesArray(*esd.fPmdTracks)),
@@ -144,10 +155,13 @@ AliESDEvent::AliESDEvent(const AliESDEvent& esd):
   fCascades(new TClonesArray(*esd.fCascades)),
   fKinks(new TClonesArray(*esd.fKinks)),
   fCaloClusters(new TClonesArray(*esd.fCaloClusters)),
+  fEMCALCells(new AliESDCaloCells(*esd.fEMCALCells)),
+  fPHOSCells(new AliESDCaloCells(*esd.fPHOSCells)),
   fErrorLogs(new TClonesArray(*esd.fErrorLogs)),
   fESDOld(new AliESD(*esd.fESDOld)),
   fESDFriendOld(new AliESDfriend(*esd.fESDFriendOld)),
   fConnected(esd.fConnected),
+  fUseOwnList(esd.fUseOwnList),
   fEMCALClusters(esd.fEMCALClusters), 
   fFirstEMCALCluster(esd.fFirstEMCALCluster),
   fPHOSClusters(esd.fPHOSClusters), 
@@ -161,6 +175,7 @@ AliESDEvent::AliESDEvent(const AliESDEvent& esd):
   AddObject(fESDFMD);
   AddObject(fESDVZERO);
   AddObject(fESDTZERO);
+  AddObject(fTPCVertex);
   AddObject(fSPDVertex);
   AddObject(fPrimaryVertex);
   AddObject(fSPDMult);
@@ -174,7 +189,10 @@ AliESDEvent::AliESDEvent(const AliESDEvent& esd):
   AddObject(fCascades);
   AddObject(fKinks);
   AddObject(fCaloClusters);
+  AddObject(fEMCALCells);
+  AddObject(fPHOSCells);
   AddObject(fErrorLogs);
+  AddObject(fESDACORDE);
 
   GetStdContent();
 
@@ -188,61 +206,69 @@ AliESDEvent & AliESDEvent::operator=(const AliESDEvent& source) {
   if(&source == this) return *this;
   AliVEvent::operator=(source);
 
-  fESDRun = new AliESDRun(*source.fESDRun);
-  fHeader = new AliESDHeader(*source.fHeader);
-  fESDZDC = new AliESDZDC(*source.fESDZDC);
-  fESDFMD = new AliESDFMD(*source.fESDFMD);
-  fESDVZERO = new AliESDVZERO(*source.fESDVZERO);
-  fESDTZERO = new AliESDTZERO(*source.fESDTZERO);
-  fSPDVertex = new AliESDVertex(*source.fSPDVertex);
-  fPrimaryVertex = new AliESDVertex(*source.fPrimaryVertex);
-  fSPDMult = new AliMultiplicity(*source.fSPDMult);
-  fPHOSTrigger = new AliESDCaloTrigger(*source.fPHOSTrigger);
-  fEMCALTrigger = new AliESDCaloTrigger(*source.fEMCALTrigger);
-  fTracks = new TClonesArray(*source.fTracks);
-  fMuonTracks = new TClonesArray(*source.fMuonTracks);
-  fPmdTracks = new TClonesArray(*source.fPmdTracks);
-  fTrdTracks = new TClonesArray(*source.fTrdTracks);
-  fV0s = new TClonesArray(*source.fV0s);
-  fCascades = new TClonesArray(*source.fCascades);
-  fKinks = new TClonesArray(*source.fKinks);
-  fCaloClusters = new TClonesArray(*source.fCaloClusters);
-  fErrorLogs = new TClonesArray(*source.fErrorLogs);
-  fESDOld       = new AliESD(*source.fESDOld);
-  fESDFriendOld = new AliESDfriend(*source.fESDFriendOld);
-  // CKB this way?? or 
-  // or AddObject(  fESDZDC = new AliESDZDC(*source.fESDZDC));
-
-  fESDObjects = new TList();
-  AddObject(fESDRun);
-  AddObject(fHeader);
-  AddObject(fESDZDC);
-  AddObject(fESDFMD);
-  AddObject(fESDVZERO);
-  AddObject(fESDTZERO);
-  AddObject(fSPDVertex);
-  AddObject(fPrimaryVertex);
-  AddObject(fSPDMult);
-  AddObject(fPHOSTrigger);
-  AddObject(fEMCALTrigger);
-  AddObject(fTracks);
-  AddObject(fMuonTracks);
-  AddObject(fPmdTracks);
-  AddObject(fTrdTracks);
-  AddObject(fV0s);
-  AddObject(fCascades);
-  AddObject(fKinks);
-  AddObject(fCaloClusters);
-  AddObject(fErrorLogs);
+  // This assumes that the list is already created
+  // and that the virtual void Copy(Tobject&) function
+  // is correctly implemented in the derived class
+  // otherwise only TObject::Copy() will be used
+
+
+  if((fESDObjects->GetSize()==0)&&(source.fESDObjects->GetSize()>=kESDListN)){
+    // We cover the case that we do not yet have the 
+    // standard content but the source has it
+    CreateStdContent();
+  }
+
+  TIter next(source.GetList());
+  TObject *its = 0;
+  TString name;
+  while ((its = next())) {
+    name.Form("%s", its->GetName());
+    TObject *mine = fESDObjects->FindObject(name.Data());
+    if(!mine){
+      // not in this: can be added to list (to be implemented)
+      AliWarning(Form("%s:%d Could not find %s for copying \n",
+                     (char*)__FILE__,__LINE__,name.Data()));
+      continue;
+    }
+
+    if(!its->InheritsFrom("TCollection")){
+      // simple objects
+      its->Copy(*mine);
+    }
+    else if(its->InheritsFrom("TClonesArray")){
+      // Create or expand the tclonesarray pointers
+      // so we can directly copy to the object
+      TClonesArray *its_tca = (TClonesArray*)its;
+      TClonesArray *mine_tca = (TClonesArray*)mine;
+
+      // this leaves the capacity of the TClonesArray the same
+      // except for a factor of 2 increase when size > capacity
+      // does not release any memory occupied by the tca
+      mine_tca->ExpandCreate(its_tca->GetEntriesFast());
+      for(int i = 0;i < its_tca->GetEntriesFast();++i){
+       // copy 
+       TObject *mine_tca_obj = mine_tca->At(i);
+       TObject *its_tca_obj = its_tca->At(i);
+       // no need to delete first
+       // pointers within the class should be handled by Copy()...
+       // Can there be Empty slots?
+       its_tca_obj->Copy(*mine_tca_obj);
+      }
+    }
+    else{
+      AliWarning(Form("%s:%d cannot copy TCollection \n",
+                     (char*)__FILE__,__LINE__));
+    }
+  }
 
   fConnected = source.fConnected;
+  fUseOwnList = source.fUseOwnList;
   fEMCALClusters = source.fEMCALClusters;
   fFirstEMCALCluster = source.fFirstEMCALCluster;
   fPHOSClusters = source.fPHOSClusters;
   fFirstPHOSCluster = source.fFirstPHOSCluster;
 
 
-
   return *this;
 
 }
@@ -267,6 +293,19 @@ AliESDEvent::~AliESDEvent()
   
 }
 
+void AliESDEvent::Copy(TObject &obj) const {
+
+  // interface to TOBject::Copy
+  // Copies the content of this into obj!
+  // bascially obj = *this
+
+  if(this==&obj)return;
+  AliESDEvent *robj = dynamic_cast<AliESDEvent*>(&obj);
+  if(!robj)return; // not an AliESEvent
+  *robj = *this;
+  return;
+}
+
 //______________________________________________________________________________
 void AliESDEvent::Reset()
 {
@@ -275,6 +314,20 @@ void AliESDEvent::Reset()
   // Reset the standard contents
   ResetStdContent(); 
   if(fESDOld)fESDOld->Reset();
+  //  reset for the friends...
+  if(fESDFriendOld){
+    fESDFriendOld->~AliESDfriend();
+    new (fESDFriendOld) AliESDfriend();
+  }
+  // for new data we have to fetch the Pointer from the list 
+  AliESDfriend *fr = (AliESDfriend*)FindListObject("AliESDfriend");
+  if(fr){
+    // delete the content
+    fr->~AliESDfriend();
+    // make a new valid ESDfriend at the same place
+    new (fr) AliESDfriend();
+  }
+
   // call reset for user supplied data?
 }
 
@@ -284,14 +337,25 @@ void AliESDEvent::ResetStdContent()
   if(fESDRun) fESDRun->Reset();
   if(fHeader) fHeader->Reset();
   if(fESDZDC) fESDZDC->Reset();
-  if(fESDFMD) fESDFMD->Clear(); // why clear.... need consistend names
+  if(fESDFMD) {
+    fESDFMD->Clear();
+  }
   if(fESDVZERO){
     // reset by callin d'to /c'tor keep the pointer
     fESDVZERO->~AliESDVZERO();
     new (fESDVZERO) AliESDVZERO();
   }  
+  if(fESDACORDE){
+    fESDACORDE->~AliESDACORDE();
+    new (fESDACORDE) AliESDACORDE();   
+  } 
   if(fESDTZERO) fESDTZERO->Reset(); 
   // CKB no clear/reset implemented
+  if(fTPCVertex){
+    fTPCVertex->~AliESDVertex();
+    new (fTPCVertex) AliESDVertex();
+    fTPCVertex->SetName(fgkESDListName[kTPCVertex]);
+  }
   if(fSPDVertex){
     fSPDVertex->~AliESDVertex();
     new (fSPDVertex) AliESDVertex();
@@ -316,9 +380,11 @@ void AliESDEvent::ResetStdContent()
   if(fCascades)fCascades->Delete();
   if(fKinks)fKinks->Delete();
   if(fCaloClusters)fCaloClusters->Delete();
+  if(fPHOSCells)fPHOSCells->DeleteContainer();
+  if(fEMCALCells)fEMCALCells->DeleteContainer();
   if(fErrorLogs) fErrorLogs->Delete();
 
-  // don't reset fconnected fConnected ;
+  // don't reset fconnected fConnected and the list
 
   fEMCALClusters=0; 
   fFirstEMCALCluster=-1; 
@@ -368,6 +434,10 @@ void AliESDEvent::Print(Option_t *) const
   printf("                 v0        %d\n", GetNumberOfV0s());
   printf("                 cascades  %d\n", GetNumberOfCascades());
   printf("                 kinks     %d\n", GetNumberOfKinks());
+  if(fPHOSCells)printf("                 PHOSCells %d\n", fPHOSCells->GetNumberOfCells());
+  else printf("                 PHOSCells not in the Event\n");
+  if(fEMCALCells)printf("                 EMCALCells %d\n", fEMCALCells->GetNumberOfCells());
+  else printf("                 EMCALCells not in the Event\n");
   printf("                 CaloClusters %d\n", GetNumberOfCaloClusters());
   printf("                 phos      %d\n", GetNumberOfPHOSClusters());
   printf("                 emcal     %d\n", GetNumberOfEMCALClusters());
@@ -377,7 +447,7 @@ void AliESDEvent::Print(Option_t *) const
   return;
 }
 
-void AliESDEvent::SetESDfriend(const AliESDfriend *ev) {
+void AliESDEvent::SetESDfriend(const AliESDfriend *ev) const {
   //
   // Attaches the complementary info to the ESD
   //
@@ -395,7 +465,7 @@ void AliESDEvent::SetESDfriend(const AliESDfriend *ev) {
   }
 }
 
-Bool_t  AliESDEvent::RemoveKink(Int_t rm) {
+Bool_t  AliESDEvent::RemoveKink(Int_t rm) const {
   // ---------------------------------------------------------
   // Remove a kink candidate and references to it from ESD,
   // if this candidate does not come from a reconstructed decay
@@ -407,7 +477,7 @@ Bool_t  AliESDEvent::RemoveKink(Int_t rm) {
   return kTRUE;
 }
 
-Bool_t  AliESDEvent::RemoveV0(Int_t rm) {
+Bool_t  AliESDEvent::RemoveV0(Int_t rm) const {
   // ---------------------------------------------------------
   // Remove a V0 candidate and references to it from ESD,
   // if this candidate does not come from a reconstructed decay
@@ -471,7 +541,7 @@ Bool_t  AliESDEvent::RemoveV0(Int_t rm) {
   return kTRUE;
 }
 
-Bool_t  AliESDEvent::RemoveTrack(Int_t rm) {
+Bool_t  AliESDEvent::RemoveTrack(Int_t rm) const {
   // ---------------------------------------------------------
   // Remove a track and references to it from ESD,
   // if this track does not come from a reconstructed decay
@@ -481,6 +551,26 @@ Bool_t  AliESDEvent::RemoveTrack(Int_t rm) {
 
   Int_t used=0;
 
+  // Check if this track comes from the reconstructed primary vertices
+  if (fTPCVertex && fTPCVertex->GetStatus()) {
+     UShort_t *primIdx=fTPCVertex->GetIndices();
+     Int_t n=fTPCVertex->GetNIndices();
+     while (n--) {
+       Int_t idx=Int_t(primIdx[n]);
+       if (rm==idx) return kFALSE;
+       if (idx==last) used++; 
+     }
+  }
+  if (fPrimaryVertex && fPrimaryVertex->GetStatus()) {
+     UShort_t *primIdx=fPrimaryVertex->GetIndices();
+     Int_t n=fPrimaryVertex->GetNIndices();
+     while (n--) {
+       Int_t idx=Int_t(primIdx[n]);
+       if (rm==idx) return kFALSE;
+       if (idx==last) used++; 
+     }
+  }
+  
   // Check if this track comes from a reconstructed decay
   Int_t nv0=GetNumberOfV0s();
   for (Int_t n=0; n<nv0; n++) {
@@ -529,9 +619,36 @@ Bool_t  AliESDEvent::RemoveTrack(Int_t rm) {
   new (a[rm]) AliESDtrack(*t);
   delete a.RemoveAt(last);
 
+
   if (!used) return kTRUE;
   
 
+  // Remap the indices of the tracks used for the primary vertex reconstruction
+  if (fTPCVertex && fTPCVertex->GetStatus()) {
+     UShort_t *primIdx=fTPCVertex->GetIndices();
+     Int_t n=fTPCVertex->GetNIndices();
+     while (n--) {
+       Int_t idx=Int_t(primIdx[n]);
+       if (idx==last) {
+          primIdx[n]=Short_t(rm); 
+          used--;
+          if (!used) return kTRUE;
+       }
+     }
+  }  
+  if (fPrimaryVertex && fPrimaryVertex->GetStatus()) {
+     UShort_t *primIdx=fPrimaryVertex->GetIndices();
+     Int_t n=fPrimaryVertex->GetNIndices();
+     while (n--) {
+       Int_t idx=Int_t(primIdx[n]);
+       if (idx==last) {
+          primIdx[n]=Short_t(rm); 
+          used--;
+          if (!used) return kTRUE;
+       }
+     }
+  }  
+
   // Remap the indices of the daughters of reconstructed decays
   for (Int_t n=0; n<nv0; n++) {
     AliESDv0 *v0=GetV0(n);
@@ -616,14 +733,13 @@ Bool_t AliESDEvent::Clean(Float_t *cleanPars) {
     Float_t csp=v0->GetV0CosineOfPointingAngle();
     Float_t cspcut=cspMin + dca/dcaMax*(1.-cspMin);
     if (csp > cspcut) continue;
-
     if (RemoveV0(i)) rc=kTRUE;
   }
 
 
   Float_t dmax=cleanPars[2], zmax=cleanPars[3];
 
-  const AliESDVertex *vertex=GetVertex();
+  const AliESDVertex *vertex=GetPrimaryVertexSPD();
   Bool_t vtxOK=vertex->GetStatus();
   
   Int_t nTracks=GetNumberOfTracks();
@@ -695,12 +811,22 @@ Int_t AliESDEvent::AddCaloCluster(const AliESDCaloCluster *c)
   }
 
 
-void  AliESDEvent::AddRawDataErrorLog(const AliRawDataErrorLog *log) {
+void  AliESDEvent::AddRawDataErrorLog(const AliRawDataErrorLog *log) const {
   TClonesArray &errlogs = *fErrorLogs;
   new(errlogs[errlogs.GetEntriesFast()])  AliRawDataErrorLog(*log);
 }
 
-void  AliESDEvent::SetVertex(const AliESDVertex *vertex) 
+void  AliESDEvent::SetPrimaryVertexTPC(const AliESDVertex *vertex) 
+{
+  // Set the TPC vertex
+  // use already allocated space
+  if(fTPCVertex){
+    *fTPCVertex = *vertex;
+    fTPCVertex->SetName(fgkESDListName[kTPCVertex]);
+  }
+}
+
+void  AliESDEvent::SetPrimaryVertexSPD(const AliESDVertex *vertex) 
 {
   // Set the SPD vertex
   // use already allocated space
@@ -744,6 +870,13 @@ void AliESDEvent::SetVZEROData(AliESDVZERO * obj)
     *fESDVZERO = *obj;
 }
 
+void AliESDEvent::SetACORDEData(AliESDACORDE * obj)
+{
+  if(fESDACORDE)
+    *fESDACORDE = *obj;
+}
+
+
 void AliESDEvent::GetESDfriend(AliESDfriend *ev) const 
 {
   //
@@ -785,6 +918,7 @@ void AliESDEvent::GetStdContent()
   fESDFMD = (AliESDFMD*)fESDObjects->FindObject(fgkESDListName[kESDFMD]);
   fESDVZERO = (AliESDVZERO*)fESDObjects->FindObject(fgkESDListName[kESDVZERO]);
   fESDTZERO = (AliESDTZERO*)fESDObjects->FindObject(fgkESDListName[kESDTZERO]);
+  fTPCVertex = (AliESDVertex*)fESDObjects->FindObject(fgkESDListName[kTPCVertex]);
   fSPDVertex = (AliESDVertex*)fESDObjects->FindObject(fgkESDListName[kSPDVertex]);
   fPrimaryVertex = (AliESDVertex*)fESDObjects->FindObject(fgkESDListName[kPrimaryVertex]);
   fSPDMult =       (AliMultiplicity*)fESDObjects->FindObject(fgkESDListName[kSPDMult]);
@@ -798,7 +932,10 @@ void AliESDEvent::GetStdContent()
   fCascades = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kCascades]);
   fKinks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kKinks]);
   fCaloClusters = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kCaloClusters]);
+  fEMCALCells = (AliESDCaloCells*)fESDObjects->FindObject(fgkESDListName[kEMCALCells]);
+  fPHOSCells = (AliESDCaloCells*)fESDObjects->FindObject(fgkESDListName[kPHOSCells]);
   fErrorLogs = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kErrorLogs]);
+  fESDACORDE = (AliESDACORDE*)fESDObjects->FindObject(fgkESDListName[kESDACORDE]);
 
 }
 
@@ -821,6 +958,12 @@ void AliESDEvent::SetStdNames(){
   }
 } 
 
+
+void AliESDEvent::CreateStdContent(Bool_t bUseThisList){
+  fUseOwnList = bUseThisList;
+  CreateStdContent();
+}
+
 void AliESDEvent::CreateStdContent() 
 {
   // create the standard AOD content and set pointers
@@ -834,6 +977,7 @@ void AliESDEvent::CreateStdContent()
   AddObject(new AliESDTZERO());
   AddObject(new AliESDVertex());
   AddObject(new AliESDVertex());
+  AddObject(new AliESDVertex());
   AddObject(new AliMultiplicity());
   AddObject(new AliESDCaloTrigger());
   AddObject(new AliESDCaloTrigger());
@@ -845,7 +989,10 @@ void AliESDEvent::CreateStdContent()
   AddObject(new TClonesArray("AliESDcascade",0));
   AddObject(new TClonesArray("AliESDkink",0));
   AddObject(new TClonesArray("AliESDCaloCluster",0));
+  AddObject(new AliESDCaloCells());
+  AddObject(new AliESDCaloCells());
   AddObject(new TClonesArray("AliRawDataErrorLog",0));
+  AddObject(new AliESDACORDE()); 
 
   // check the order of the indices against enum...
 
@@ -856,23 +1003,28 @@ void AliESDEvent::CreateStdContent()
 }
 
 TObject* AliESDEvent::FindListObject(const char *name){
-  if(fESDObjects)return fESDObjects->FindObject(name);
+//
+// Find object with name "name" in the list of branches
+//
+  if(fESDObjects){
+    return fESDObjects->FindObject(name);
+  }
   return 0;
 } 
 
 Int_t AliESDEvent::GetPHOSClusters(TRefArray *clusters) const
 {
   // fills the provided TRefArray with all found phos clusters
-
+  
   clusters->Clear();
-
+  
   AliESDCaloCluster *cl = 0;
   for (Int_t i = 0; i < GetNumberOfCaloClusters(); i++) {
-
-    if ( (cl = GetCaloCluster(i))) {
+    
+    if ( (cl = GetCaloCluster(i)) ) {
       if (cl->IsPHOS()){
        clusters->Add(cl);
-       printf("IsPHOS %d Size: %d \n",i,clusters->GetEntriesFast());
+       AliDebug(1,Form("IsPHOS cluster %d Size: %d \n",i,clusters->GetEntriesFast()));
       }
     }
   }
@@ -881,65 +1033,117 @@ Int_t AliESDEvent::GetPHOSClusters(TRefArray *clusters) const
 
 Int_t AliESDEvent::GetEMCALClusters(TRefArray *clusters) const
 {
-  // fills the provided TRefArray with all found phos clusters
+  // fills the provided TRefArray with all found emcal clusters
 
   clusters->Clear();
 
   AliESDCaloCluster *cl = 0;
   for (Int_t i = 0; i < GetNumberOfCaloClusters(); i++) {
 
-    if ( (cl = GetCaloCluster(i))) {
+    if ( (cl = GetCaloCluster(i)) ) {
       if (cl->IsEMCAL()){
        clusters->Add(cl);
-       printf("IsEMCAL %d Size: %d \n",i,clusters->GetEntriesFast());
+       AliDebug(1,Form("IsEMCAL cluster %d Size: %d \n",i,clusters->GetEntriesFast()));
       }
     }
   }
   return clusters->GetEntriesFast();
 }
 
+const void AliESDEvent::WriteToTree(TTree* tree) const {
+  // Book the branches as in TTree::Branch(TCollection*)
+  // but add a "." at the end of top level branches which are
+  // not a TClonesArray
+
+
+  TString branchname;
+  TIter next(fESDObjects);
+  const Int_t kSplitlevel = 99; // default value in TTree::Branch()
+  const Int_t kBufsize = 32000; // default value in TTree::Branch()
+  TObject *obj = 0;
 
-void AliESDEvent::ReadFromTree(TTree *tree){
+  while ((obj = next())) {
+    branchname.Form("%s", obj->GetName());
+    if ((kSplitlevel > 1) &&  !obj->InheritsFrom(TClonesArray::Class())) {
+      if(!branchname.EndsWith("."))branchname += ".";
+    }
+    tree->Bronch(branchname, obj->ClassName(), fESDObjects->GetObjectRef(obj),
+                kBufsize, kSplitlevel - 1);
+  }
+
+}
 
+
+void AliESDEvent::ReadFromTree(TTree *tree, Option_t* /*opt*/){
+//
+// Connect the ESDEvent to a tree
+//
   if(!tree){
     Printf("%s %d AliESDEvent::ReadFromTree() Zero Pointer to Tree \n",(char*)__FILE__,__LINE__);
     return;
   }
-
   // load the TTree
   if(!tree->GetTree())tree->LoadTree(0);
 
   // if we find the "ESD" branch on the tree we do have the old structure
   if(tree->GetBranch("ESD")) {
     char ** address  = (char **)(tree->GetBranch("ESD")->GetAddress());
-    char ** addressF = (char **)(tree->GetBranch("ESDfriend.")->GetAddress());
+    // do we have the friend branch
+    TBranch * esdFB = tree->GetBranch("ESDfriend.");
+    char ** addressF = 0;
+    if(esdFB)addressF = (char **)(esdFB->GetAddress());
     if (!address) {
       printf("%s %d AliESDEvent::ReadFromTree() Reading old Tree \n",(char*)__FILE__,__LINE__);
       tree->SetBranchAddress("ESD",       &fESDOld);
-      tree->SetBranchAddress("ESDfriend.",&fESDFriendOld);
+      if(esdFB){
+       tree->SetBranchAddress("ESDfriend.",&fESDFriendOld);
+      }
     } else {
       printf("%s %d AliESDEvent::ReadFromTree() Reading old Tree \n",(char*)__FILE__,__LINE__);
       printf("%s %d Branch already connected. Using existing branch address. \n",(char*)__FILE__,__LINE__);
       fESDOld       = (AliESD*)       (*address);
-      fESDFriendOld = (AliESDfriend*) (*addressF);
+      // addressF can still be 0, since branch needs to switched on
+      if(addressF)fESDFriendOld = (AliESDfriend*) (*addressF);
     }
-       
+                                      
     //  have already connected the old ESD structure... ?
     // reuse also the pointer of the AlliESDEvent
     // otherwise create new ones
-
     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("ESDObjectsConnectedToTree"));
   
     if(connectedList){
       // If connected use the connected list of objects
-      fESDObjects->Delete();
-      fESDObjects = connectedList;
+      if(fESDObjects!= connectedList){
+       // protect when called twice 
+       fESDObjects->Delete();
+       fESDObjects = connectedList;
+      }
       GetStdContent(); 
+
+      
+      // The pointer to the friend changes when called twice via InitIO
+      // since AliESDEvent is deleted
+      TObject* oldf = FindListObject("AliESDfriend");
+      TObject* newf = 0;
+      if(addressF){
+       newf = (TObject*)*addressF;
+      }
+      if(newf!=0&&oldf!=newf){
+       // remove the old reference
+       // Should we also delete it? Or is this handled in TTree I/O
+       // since it is created by the first SetBranchAddress
+       fESDObjects->Remove(oldf);
+       // add the new one 
+       fESDObjects->Add(newf);
+      }
+      
       fConnected = true;
       return;
     }
     // else...    
     CreateStdContent(); // create for copy
+    // if we have the esdfriend add it, so we always can access it via the userinfo
+    if(fESDFriendOld)AddObject(fESDFriendOld);
     // we are not owner of the list objects 
     // must not delete it
     fESDObjects->SetOwner(kFALSE);
@@ -948,9 +1152,10 @@ void AliESDEvent::ReadFromTree(TTree *tree){
     fConnected = true;
     return;
   }
+  
 
-  delete fESDOld;
-  fESDOld = 0;
+    delete fESDOld;
+    fESDOld = 0;
   // Try to find AliESDEvent
   AliESDEvent *esdEvent = 0;
   esdEvent = (AliESDEvent*)tree->GetTree()->GetUserInfo()->FindObject("AliESDEvent");
@@ -965,14 +1170,27 @@ void AliESDEvent::ReadFromTree(TTree *tree){
       fConnected = true;
       return;
     }
+
     // Connect to tree
     // prevent a memory leak when reading back the TList
-    delete fESDObjects;
-    fESDObjects = 0;
-    // create a new TList from the UserInfo TList... 
-    // copy constructor does not work...
-    fESDObjects = (TList*)(esdEvent->GetList()->Clone());
-    fESDObjects->SetOwner(kFALSE);
+
+    if(!fUseOwnList){
+      delete fESDObjects;
+      fESDObjects = 0;
+      // create a new TList from the UserInfo TList... 
+      // copy constructor does not work...
+      fESDObjects = (TList*)(esdEvent->GetList()->Clone());
+      fESDObjects->SetOwner(kFALSE);
+    }
+    else if ( fESDObjects->GetEntries()==0){
+      // at least create the std content if we want to read to our list
+      CreateStdContent(); 
+    }
+
+    // in principle
+    // we only need new things in the list if we do no already have it..
+    // TODO just add new entries
+
     if(fESDObjects->GetEntries()<kESDListN){
       printf("%s %d AliESDEvent::ReadFromTree() TList contains less than the standard contents %d < %d \n",
             (char*)__FILE__,__LINE__,fESDObjects->GetEntries(),kESDListN);
@@ -988,7 +1206,22 @@ void AliESDEvent::ReadFromTree(TTree *tree){
          tree->SetBranchAddress("ESDfriend.",fESDObjects->GetObjectRef(el));
        }
       else{
-       tree->SetBranchAddress(bname.Data(),fESDObjects->GetObjectRef(el));
+       // check if branch exists under this Name
+        TBranch *br = tree->GetBranch(bname.Data());
+        if(br){
+          tree->SetBranchAddress(bname.Data(),fESDObjects->GetObjectRef(el));
+        }
+        else{
+          br = tree->GetBranch(Form("%s.",bname.Data()));
+          if(br){
+            tree->SetBranchAddress(Form("%s.",bname.Data()),fESDObjects->GetObjectRef(el));
+          }
+          else{
+            printf("%s %d AliESDEvent::ReadFromTree() No Branch found with Name %s or %s. \n",
+                  (char*)__FILE__,__LINE__,bname.Data(),bname.Data());
+          }
+
+       }
       }
     }
     GetStdContent();
@@ -1009,7 +1242,16 @@ void AliESDEvent::ReadFromTree(TTree *tree){
     TNamed *el;
     while((el=(TNamed*)next())){
       TString bname(el->GetName());    
-      tree->SetBranchAddress(bname.Data(),fESDObjects->GetObjectRef(el));
+      TBranch *br = tree->GetBranch(bname.Data());
+      if(br){
+       tree->SetBranchAddress(bname.Data(),fESDObjects->GetObjectRef(el));
+      }
+      else{
+       br = tree->GetBranch(Form("%s.",bname.Data()));
+       if(br){
+         tree->SetBranchAddress(Form("%s.",bname.Data()),fESDObjects->GetObjectRef(el));
+       }
+      }
     }
     GetStdContent();
     // when reading back we are not owner of the list 
@@ -1023,8 +1265,6 @@ void AliESDEvent::CopyFromOldESD()
 {
   // Method which copies over everthing from the old esd structure to the 
   // new  
-    printf("CopyFromOldESD \n");
-    
   if(fESDOld){
     ResetStdContent();
      // Run
@@ -1047,11 +1287,12 @@ void AliESDEvent::CopyFromOldESD()
     // ZDC
 
     SetZDC(fESDOld->GetZDCN1Energy(),
-          fESDOld->GetZDCP1Energy(),
-          fESDOld->GetZDCEMEnergy(),
-          fESDOld->GetZDCN2Energy(),
-          fESDOld->GetZDCP2Energy(),
-          fESDOld->GetZDCParticipants());
+           fESDOld->GetZDCP1Energy(),
+           fESDOld->GetZDCEMEnergy(),
+           0,
+           fESDOld->GetZDCN2Energy(),
+           fESDOld->GetZDCP2Energy(),
+           fESDOld->GetZDCParticipants());
 
     // FMD
     
@@ -1066,13 +1307,12 @@ void AliESDEvent::CopyFromOldESD()
     // VZERO
     if (fESDOld->GetVZEROData()) SetVZEROData(fESDOld->GetVZEROData());
 
-    if(fESDOld->GetVertex())SetVertex(fESDOld->GetVertex());
+    if(fESDOld->GetVertex())SetPrimaryVertexSPD(fESDOld->GetVertex());
 
     if(fESDOld->GetPrimaryVertex())SetPrimaryVertex(fESDOld->GetPrimaryVertex());
 
     if(fESDOld->GetMultiplicity())SetMultiplicity(fESDOld->GetMultiplicity());
 
-    printf("CopyFromOldESD %d \n", fESDOld->GetNumberOfTracks());
     for(int i = 0;i<fESDOld->GetNumberOfTracks();i++){
       AddTrack(fESDOld->GetTrack(i));
     }