]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ANALYSIS/AliAnalysisTaskSE.cxx
ANALYSIS and ANALYSIScalib
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskSE.cxx
index 8c9b898a3a6c4c1027b61446142fa14523c811ad..34b2610d606ba91e64034ccedfb2858ee3ef2685 100644 (file)
 #include "AliESD.h"
 #include "AliAODEvent.h"
 #include "AliAODHeader.h"
+#include "AliAODVZERO.h"
+#include "AliTOFHeader.h"
 #include "AliAODTracklets.h"
 #include "AliAODCaloCells.h"
+#include "AliAODCaloTrigger.h"
 #include "AliAODMCParticle.h"
 #include "AliVEvent.h"
 #include "AliAODHandler.h"
@@ -52,7 +55,9 @@
 ClassImp(AliAnalysisTaskSE)
 
 ////////////////////////////////////////////////////////////////////////
-AliAODHeader*    AliAnalysisTaskSE::fgAODHeader         = NULL;
+AliVHeader*      AliAnalysisTaskSE::fgAODHeader         = NULL;
+AliTOFHeader*    AliAnalysisTaskSE::fgTOFHeader         = NULL;
+AliAODVZERO*     AliAnalysisTaskSE::fgAODVZERO          = NULL;
 TClonesArray*    AliAnalysisTaskSE::fgAODTracks         = NULL;
 TClonesArray*    AliAnalysisTaskSE::fgAODVertices       = NULL;
 TClonesArray*    AliAnalysisTaskSE::fgAODV0s            = NULL;
@@ -64,7 +69,10 @@ TClonesArray*    AliAnalysisTaskSE::fgAODMCParticles    = NULL;
 AliAODTracklets* AliAnalysisTaskSE::fgAODTracklets      = NULL;
 AliAODCaloCells* AliAnalysisTaskSE::fgAODEmcalCells     = NULL;
 AliAODCaloCells* AliAnalysisTaskSE::fgAODPhosCells      = NULL;
+AliAODCaloTrigger* AliAnalysisTaskSE::fgAODEMCALTrigger = NULL;
+AliAODCaloTrigger* AliAnalysisTaskSE::fgAODPHOSTrigger  = NULL;
 TClonesArray*    AliAnalysisTaskSE::fgAODDimuons        = NULL;
+TClonesArray*    AliAnalysisTaskSE::fgAODHmpidRings     = NULL;
 
 AliAnalysisTaskSE::AliAnalysisTaskSE():
     AliAnalysisTask(),
@@ -140,6 +148,9 @@ AliAnalysisTaskSE::AliAnalysisTaskSE(const AliAnalysisTaskSE& obj):
 AliAnalysisTaskSE& AliAnalysisTaskSE::operator=(const AliAnalysisTaskSE& other)
 {
 // Assignment
+  if(&other == this) return *this;
+  AliAnalysisTask::operator=(other);
+
     AliAnalysisTask::operator=(other);
     fDebug            = other.fDebug;
     fEntry            = other.fEntry;
@@ -158,31 +169,16 @@ AliAnalysisTaskSE& AliAnalysisTaskSE::operator=(const AliAnalysisTaskSE& other)
 }
 
 //______________________________________________________________________________
-Bool_t AliAnalysisTaskSE::CheckPostData() const
-{
-// Checks if data was posted to all outputs defined by the task. If task does
-// not have output slots this returns always kTRUE.
-   AliAnalysisDataContainer *coutput;
-   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
-   for (Int_t islot=1; islot<fNoutputs; islot++) {
-      coutput = GetOutputSlot(islot)->GetContainer();
-      if (!mgr->GetOutputs()->FindObject(coutput)) continue;
-      if (!coutput->GetData()) return kFALSE;
-   }
-   CheckOwnership();
-   return kTRUE;
-}
-
 void AliAnalysisTaskSE::ConnectInputData(Option_t* /*option*/)
 {
 // Connect the input data
     if (fDebug > 1) printf("AnalysisTaskSE::ConnectInputData() \n");
 
    // Connect input handlers (multi input handler is handled)
-   ConnectMultiHandler();
-
-    if (fInputHandler) {
-       if ((fInputHandler->GetTree())->GetBranch("ESDfriend."))
+    ConnectMultiHandler();
+    
+    if (fInputHandler && fInputHandler->GetTree()) {
+       if (fInputHandler->GetTree()->GetBranch("ESDfriend."))
            fESDfriend = ((AliESDInputHandler*)fInputHandler)->GetESDfriend();
 
        fInputEvent = fInputHandler->GetEvent();
@@ -211,20 +207,31 @@ void AliAnalysisTaskSE::CreateOutputObjects()
     if (aodIH) {
        if (aodIH->GetMergeEvents()) merging = kTRUE;
     }
-    
 
     // Check if AOD replication has been required
-    
     if (handler) {
        fOutputAOD   = handler->GetAOD();
        fTreeA = handler->GetTree();
        if (fOutputAOD && !(handler->IsStandard())) {
-           if ((handler->NeedsHeaderReplication()) && !(fgAODHeader)) 
+           if ((handler->NeedsHeaderReplication() || merging) && !(fgAODHeader)) 
                {
                 if (fDebug > 1) AliInfo("Replicating header");
                 fgAODHeader = new AliAODHeader;
                 handler->AddBranch("AliAODHeader", &fgAODHeader);
                }
+            if ((handler->NeedsTOFHeaderReplication() || merging) && !(fgTOFHeader))
+                {
+                 if (fDebug > 1) AliInfo("Replicating TOFheader");
+                 fgTOFHeader = new AliTOFHeader;
+                 handler->AddBranch("AliTOFHeader", &fgTOFHeader);
+                }
+            if ((handler->NeedsVZEROReplication() || merging) && !(fgAODVZERO))
+                {
+                 if (fDebug > 1) AliInfo("Replicating VZERO");
+                 fgAODVZERO = new AliAODVZERO;
+                 handler->AddBranch("AliAODVZERO", &fgAODVZERO);
+                }
+
            if ((handler->NeedsTracksBranchReplication() || merging) && !(fgAODTracks))      
            {   
                if (fDebug > 1) AliInfo("Replicating track branch\n");
@@ -280,13 +287,23 @@ void AliAnalysisTaskSE::CreateOutputObjects()
                fgAODCaloClusters->SetName("caloClusters");
                handler->AddBranch("TClonesArray", &fgAODCaloClusters);
 
-               fgAODEmcalCells = new AliAODCaloCells();
-               fgAODEmcalCells->SetName("emcalCells");
+               fgAODEmcalCells = new AliAODCaloCells("emcalCells","emcalCells",AliVCaloCells::kEMCALCell);
                handler->AddBranch("AliAODCaloCells", &fgAODEmcalCells);
 
-               fgAODPhosCells = new AliAODCaloCells();
-               fgAODPhosCells->SetName("phosCells");
+               fgAODPhosCells = new AliAODCaloCells("phosCells","phosCells",AliVCaloCells::kPHOSCell);
                handler->AddBranch("AliAODCaloCells", &fgAODPhosCells);
+               }
+           if ((handler->NeedsCaloTriggerBranchReplication() || merging) && !(fgAODEMCALTrigger))        
+           {   
+               if (fDebug > 1) AliInfo("Replicating EMCAL Calo Trigger branches\n");
+               fgAODEMCALTrigger = new AliAODCaloTrigger("emcalTrigger","emcalTrigger");
+               handler->AddBranch("AliAODCaloTrigger", &fgAODEMCALTrigger);
+               }
+               if ((handler->NeedsCaloTriggerBranchReplication() || merging) && !(fgAODPHOSTrigger))     
+               {   
+               if (fDebug > 1) AliInfo("Replicating PHOS Calo Trigger branches\n");
+               fgAODPHOSTrigger = new AliAODCaloTrigger("phosTrigger","phosTrigger");
+               handler->AddBranch("AliAODCaloTrigger", &fgAODPHOSTrigger);
            }
            if ((handler->NeedsMCParticlesBranchReplication() || merging) && !(fgAODMCParticles))         
            {   
@@ -295,13 +312,21 @@ void AliAnalysisTaskSE::CreateOutputObjects()
                fgAODMCParticles->SetName("mcparticles");
                handler->AddBranch("TClonesArray", &fgAODMCParticles);
            }
-           if ((handler->NeedsDimuonsBranchReplication() || merging) && !(fgAODDimuons))      
+            if ((handler->NeedsDimuonsBranchReplication() || merging) && !(fgAODDimuons))      
            {   
                if (fDebug > 1) AliInfo("Replicating dimuon branch\n");
                fgAODDimuons = new TClonesArray("AliAODDimuon",0);
                fgAODDimuons->SetName("dimuons");
                handler->AddBranch("TClonesArray", &fgAODDimuons);
            }    
+           if ((handler->NeedsHMPIDBranchReplication() || merging) && !(fgAODHmpidRings))      
+           {   
+               if (fDebug > 1) AliInfo("Replicating HMPID branch\n");
+               fgAODHmpidRings = new TClonesArray("AliAODHMPIDrings",0);
+               fgAODHmpidRings->SetName("hmpidRings");
+               handler->AddBranch("TClonesArray", &fgAODHmpidRings);
+           }
+                
 
            // cache the pointerd in the AODEvent
            fOutputAOD->GetStdContent();
@@ -316,62 +341,84 @@ void AliAnalysisTaskSE::Exec(Option_t* option)
 {
 //
 // Exec analysis of one event
-
+    
     ConnectMultiHandler();
-
+    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
+    if (mgr->GetDebugLevel() > 1) {
+       if (!mgr->GetTopTasks()->FindObject(this))
+          printf("    -> Executing sub-task %s\n", GetName());
+    }   
     if ( fDebug >= 10)
       printf("Task is active %5d\n", IsActive());
     
     if (fDebug > 1) AliInfo("AliAnalysisTaskSE::Exec() \n");
 //
-    AliAODHandler* handler = dynamic_cast<AliAODHandler*> 
-       ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
+    AliAODHandler* handler = dynamic_cast<AliAODHandler*>(mgr->GetOutputEventHandler());
 
     AliAODInputHandler* aodH = dynamic_cast<AliAODInputHandler*>(fInputHandler);
 //
 // Was event selected ? If no event selection mechanism, the event SHOULD be selected (AG)
     UInt_t isSelected = AliVEvent::kAny;
-    if( fInputHandler && fInputHandler->GetEventSelection()) {
+    if( fInputHandler && (fInputHandler->GetEventSelection() || aodH)) {
       // Get the actual offline trigger mask for the event and AND it with the
       // requested mask. If no mask requested select by default the event.
       if (fOfflineTriggerMask)
-         isSelected = fOfflineTriggerMask & fInputHandler->IsEventSelected();
+       isSelected = fOfflineTriggerMask & fInputHandler->IsEventSelected();
     }
 //  Functionality below moved in the filter tasks (AG)
 //    if (handler) handler->SetFillAOD(isSelected);
 
     if( fInputHandler ) {
-       fEntry = fInputHandler->GetReadEntry();
-       fESDfriend = ((AliESDInputHandler*)fInputHandler)->GetESDfriend();
+      fEntry = fInputHandler->GetReadEntry();
+      fESDfriend = ((AliESDInputHandler*)fInputHandler)->GetESDfriend();
     }
     
 
-// Notify the change of run number
+    // Notify the change of run number
     if (InputEvent() && (InputEvent()->GetRunNumber() != fCurrentRunNumber)) {
        fCurrentRunNumber = InputEvent()->GetRunNumber();
        NotifyRun();
-    }    
-          
-    else if( fMCEvent )
-       fEntry = fMCEvent->Header()->GetEvent(); 
-    if ( !((Entry()-1)%100) && fDebug > 0) 
-         AliInfo(Form("%s ----> Processing event # %lld", CurrentFileName(), Entry()));
+    } else if( fMCEvent )
+      fEntry = fMCEvent->Header()->GetEvent(); 
 
+    if ( !((Entry()-1)%100) && fDebug > 0) 
+      AliInfo(Form("%s ----> Processing event # %lld", CurrentFileName(), Entry()));
     
-    
+    if (aodH) fMCEvent = aodH->MCEvent();
 
     if (handler && aodH) {
-       fMCEvent = aodH->MCEvent();
        Bool_t merging = aodH->GetMergeEvents();
+       // Do not analyze merged events if last embedded file has less events than normal event, 
+       // skip analysis after last embeded event 
+       if(merging){
+         if(aodH->GetReadEntry() + aodH->GetMergeOffset() >= aodH->GetTreeToMerge()->GetEntriesFast()){
+           // Do I need to add the lines before the return?
+           // Added protection in case the derived task is not an AOD producer.
+           AliAnalysisDataSlot *out0 = GetOutputSlot(0);
+           if (out0 && out0->IsConnected()) PostData(0, fTreeA);    
+           DisconnectMultiHandler();
+           return;
+         }
+       }
+      
        AliAODEvent* aod = dynamic_cast<AliAODEvent*>(InputEvent());
 
        if (aod && !(handler->IsStandard()) && !(handler->AODIsReplicated())) {
-           if ((handler->NeedsHeaderReplication()) && (fgAODHeader))
+           if ((handler->NeedsHeaderReplication() || merging) && (fgAODHeader))
            {
              // copy the contents by assigment
              *fgAODHeader =  *(aod->GetHeader());
            }
-           if ((handler->NeedsTracksBranchReplication() || merging) && (fgAODTracks))
+            if ((handler->NeedsTOFHeaderReplication() || merging) && (fgTOFHeader))
+            {
+              if (aod->GetTOFHeader()) *fgTOFHeader =  *(aod->GetTOFHeader());
+            }
+            if ((handler->NeedsVZEROReplication() || merging) && (fgAODVZERO) && aod->GetVZEROData())
+            {
+              *fgAODVZERO = *(aod->GetVZEROData());
+            }
+
+           if ((handler->NeedsTracksBranchReplication() || (merging &&  aodH->GetMergeTracks())) && (fgAODTracks))
            {
                TClonesArray* tracks = aod->GetTracks();
                new (fgAODTracks) TClonesArray(*tracks);
@@ -395,7 +442,7 @@ void AliAnalysisTaskSE::Exec(Option_t* option)
                TClonesArray* pmdClusters = aod->GetPmdClusters();
                new (fgAODPMDClusters) TClonesArray(*pmdClusters);
            }
-           if ((handler->NeedsJetsBranchReplication() || merging) && (fgAODJets))
+           if ((handler->NeedsJetsBranchReplication() || (merging &&aodH->GetMergeTracks())) && (fgAODJets))
            {
                TClonesArray* jets = aod->GetJets();
                new (fgAODJets) TClonesArray(*jets);
@@ -405,7 +452,9 @@ void AliAnalysisTaskSE::Exec(Option_t* option)
                TClonesArray* fmdClusters = aod->GetFmdClusters();
                new (fgAODFMDClusters) TClonesArray(*fmdClusters);
            }
-           if ((handler->NeedsCaloClustersBranchReplication() || merging) && (fgAODCaloClusters))
+           if ((handler->NeedsCaloClustersBranchReplication() || 
+                (merging && (aodH->GetMergeEMCALClusters() || aodH->GetMergePHOSClusters()))) 
+               && (fgAODCaloClusters))
            {
                TClonesArray* caloClusters = aod->GetCaloClusters();
                new (fgAODCaloClusters) TClonesArray(*caloClusters);
@@ -414,10 +463,11 @@ void AliAnalysisTaskSE::Exec(Option_t* option)
            if ((handler->NeedsMCParticlesBranchReplication() || merging) && (fgAODMCParticles))
            {
                TClonesArray* mcParticles = (TClonesArray*) (aod->FindListObject("mcparticles"));
-               new (fgAODMCParticles) TClonesArray(*mcParticles);
+               if( mcParticles )
+                 new (fgAODMCParticles) TClonesArray(*mcParticles);
            }
            
-           if ((handler->NeedsDimuonsBranchReplication() || merging) && (fgAODDimuons))
+           if ((handler->NeedsDimuonsBranchReplication() || (merging && aodH->GetMergeTracks())) && (fgAODDimuons))
            {
                fgAODDimuons->Clear();
                TClonesArray& dimuons = *fgAODDimuons;
@@ -448,84 +498,277 @@ void AliAnalysisTaskSE::Exec(Option_t* option)
                    }    
                }
            }
-           
+            if ((handler->NeedsHMPIDBranchReplication()) && (fgAODHmpidRings))
+           {
+               TClonesArray* hmpidRings = aod->GetHMPIDrings();
+               new (fgAODHmpidRings) TClonesArray(*hmpidRings);
+           }
+            
+            
+            
            // Additional merging if needed
            if (merging) {
+             Int_t nc;
+
                // mcParticles
                TClonesArray* mcparticles = (TClonesArray*) ((aodH->GetEventToMerge())->FindListObject("mcparticles"));
-               Int_t npart = mcparticles->GetEntries();
-               Int_t nc = fgAODMCParticles->GetEntries();
-               Int_t nc0 = nc;
-               
-               for (Int_t i = 0; i < npart; i++) {
+               if( mcparticles ){
+                 Int_t npart = mcparticles->GetEntries();
+                 nc = fgAODMCParticles->GetEntries();
+                 
+                 for (Int_t i = 0; i < npart; i++) {
                    AliAODMCParticle* particle = (AliAODMCParticle*) mcparticles->At(i);
                    new((*fgAODMCParticles)[nc++]) AliAODMCParticle(*particle);
+                 }
                }
-               
+
                // tracks
                TClonesArray* tracks = aodH->GetEventToMerge()->GetTracks();
-               Int_t ntr = tracks->GetEntries();
-               nc  = fgAODTracks->GetEntries();        
-               for (Int_t i = 0; i < ntr; i++) {
+               if(tracks && aodH->GetMergeTracks()){
+                 Int_t ntr = tracks->GetEntries();
+                 nc  = fgAODTracks->GetEntries();      
+                 for (Int_t i = 0; i < ntr; i++) {
                    AliAODTrack*    track = (AliAODTrack*) tracks->At(i);
                    AliAODTrack* newtrack = new((*fgAODTracks)[nc++]) AliAODTrack(*track);
-                   newtrack->SetLabel(newtrack->GetLabel() + nc0);
-               }
-
-               for (Int_t i = 0; i < nc; i++) 
-               {
-                   AliAODTrack* track = (AliAODTrack*) fgAODTracks->At(i);
-                   track->ResetBit(kIsReferenced);
-                   track->SetUniqueID(0);
+                   newtrack->SetLabel(newtrack->GetLabel() + fgAODMCParticles->GetEntries());
+                 }
+                 
+                 for (Int_t i = 0; i < nc; i++) 
+                   {
+                     AliAODTrack* track = (AliAODTrack*) fgAODTracks->At(i);
+                     track->ResetBit(kIsReferenced);
+                     track->SetUniqueID(0);
+                   }
                }
                
-               
                // clusters
                TClonesArray* clusters = aodH->GetEventToMerge()->GetCaloClusters();
-               Int_t ncl  = clusters->GetEntries();
-               nc         =  fgAODCaloClusters->GetEntries();
-               for (Int_t i = 0; i < ncl; i++) {
+               if( clusters  && (aodH->GetMergeEMCALClusters() || aodH->GetMergePHOSClusters())) {
+                 Int_t ncl  = clusters->GetEntries();
+                 nc         =  fgAODCaloClusters->GetEntries();
+                 for (Int_t i = 0; i < ncl; i++) {
                    AliAODCaloCluster*    cluster = (AliAODCaloCluster*) clusters->At(i);
+                   if(cluster->IsEMCAL() && !aodH->GetMergeEMCALClusters() ) continue;
+                   if(cluster->IsPHOS()  && !aodH->GetMergePHOSClusters()  ) continue;   
                    new((*fgAODCaloClusters)[nc++]) AliAODCaloCluster(*cluster);
+                 }
                }
-               // cells
-               AliAODCaloCells* cellsA = aodH->GetEventToMerge()->GetEMCALCells();
-               Int_t ncells  = cellsA->GetNumberOfCells();
-               nc = fgAODEmcalCells->GetNumberOfCells();
-               
-               for (Int_t i  = 0; i < ncells; i++) {
-                   Int_t cn  = cellsA->GetCellNumber(i);
-                   Int_t pos = fgAODEmcalCells->GetCellPosition(cn);
-                   if (pos >= 0) {
-                       Double_t amp = cellsA->GetAmplitude(i) + fgAODEmcalCells->GetAmplitude(pos);
-                       fgAODEmcalCells->SetCell(pos, cn, amp);
-                   } else {
-                       Double_t amp = cellsA->GetAmplitude(i);
-                       fgAODEmcalCells->SetCell(nc++, cn, amp);
+
+               // EMCAL cells
+               //*fgAODEmcalCells =  *(aod->GetEMCALCells()); // This will be valid after 10.Mar.2011.
+               if(aodH->GetMergeEMCALCells()) 
+                 {
+                   AliAODCaloCells* copycells = aod->GetEMCALCells();
+                   fgAODEmcalCells->CreateContainer(copycells->GetNumberOfCells());
+                   nc  = copycells->GetNumberOfCells();
+                   
+                   while( nc-- ){ fgAODEmcalCells->SetCell(nc,copycells->GetCellNumber(nc),copycells->GetAmplitude(nc),
+                                                           copycells->GetTime(nc),copycells->GetMCLabel(nc),copycells->GetEFraction(nc)); }
+                   
+                   AliAODCaloCells* cellsA = aodH->GetEventToMerge()->GetEMCALCells();
+                   if( cellsA )
+                     {
+                       Int_t ncells  = cellsA->GetNumberOfCells();
+                       nc = fgAODEmcalCells->GetNumberOfCells();
+                       
+                       for (Int_t i  = 0; i < ncells; i++) 
+                         {
+                           Int_t cn  = cellsA->GetCellNumber(i);
+                           Int_t pos = fgAODEmcalCells->GetCellPosition(cn);
+                           
+                           if (pos >= 0) 
+                             {
+                               Double_t amp = cellsA->GetAmplitude(i) + fgAODEmcalCells->GetAmplitude(pos);
+                               
+                               //Check if it is MC, depending on that assing the mc lable, time and e fraction
+//                             Double_t time    = 0;
+                               Int_t    mclabel =-1;
+                               Double_t efrac   = 0;
+                               if(cellsA->GetMCLabel(i) >= 0 && fgAODEmcalCells->GetMCLabel(pos) < 0)
+                                 {
+                                   mclabel = cellsA->GetMCLabel(i) ;
+//                                 time    = fgAODEmcalCells->GetTime(pos) ; // Time from data
+                                   if(amp > 0) efrac = cellsA->GetAmplitude(i) / amp;
+                                 }
+                               else if(fgAODEmcalCells->GetMCLabel(pos) >= 0 &&  cellsA->GetMCLabel(i) < 0)
+                                 {
+                                   mclabel = fgAODEmcalCells->GetMCLabel(pos) ;
+//                                 time    = cellsA->GetTime(i) ; // Time from data
+                                   if(amp > 0) efrac = fgAODEmcalCells->GetAmplitude(pos) / amp;
+                                 }
+                               else 
+                                 { // take all from input
+                                   mclabel = cellsA->GetMCLabel(i) ;
+//                                 time    = cellsA->GetTime(i) ; 
+                                   if(amp > 0) efrac = cellsA->GetAmplitude(i) / amp;  
+                                 }
+                               
+                               fgAODEmcalCells->SetCell(pos, cn, amp,cellsA->GetTime(i),mclabel,efrac);
+                               
+                             } else 
+                             {
+                               AliAODCaloCells* copycells1 = new AliAODCaloCells(*fgAODEmcalCells);
+                               fgAODEmcalCells->CreateContainer(nc+1);
+                               Int_t nn = copycells1->GetNumberOfCells();
+                               
+                               while( nn-- ){ fgAODEmcalCells->SetCell(nn,copycells1->GetCellNumber(nn),copycells1->GetAmplitude(nn),
+                                                                       copycells1->GetTime(nn),copycells1->GetMCLabel(nn),copycells1->GetEFraction(nn)); }
+                               
+                               fgAODEmcalCells->SetCell(nc++,cn,cellsA->GetAmplitude(i),cellsA->GetTime(i), cellsA->GetMCLabel(i),1.);
+                               
+                               delete copycells1;
+                             }
+                         }
                        fgAODEmcalCells->Sort();
-                   }
-               }
+                     }
+                 } // merge emcal cells
+               
                
+               // PHOS cells
+               //*fgAODPhosCells =  *(aod->GetPHOSCells()); // This will be valid after 10.Mar.2011.
+               if(aodH->GetMergePHOSCells()) 
+                 {
+                   AliAODCaloCells* copycells = aod->GetPHOSCells();
+                   fgAODPhosCells->CreateContainer(copycells->GetNumberOfCells());
+                   nc  = copycells->GetNumberOfCells();
+                   
+                   while( nc-- ){ fgAODPhosCells->SetCell(nc,copycells->GetCellNumber(nc),copycells->GetAmplitude(nc),
+                                                          copycells->GetTime(nc),copycells->GetMCLabel(nc),copycells->GetEFraction(nc)); }
+                   
+                   AliAODCaloCells* cellsP = aodH->GetEventToMerge()->GetPHOSCells();
+                   if( cellsP )
+                     {
+                       Int_t ncellsP  = cellsP->GetNumberOfCells();
+                       nc = fgAODPhosCells->GetNumberOfCells();
+                       
+                       for (Int_t i  = 0; i < ncellsP; i++) 
+                         {
+                           Int_t cn  = cellsP->GetCellNumber(i);
+                           Int_t pos = fgAODPhosCells->GetCellPosition(cn);
+                           
+                           if (pos >= 0) 
+                             {
+                               Double_t amp = cellsP->GetAmplitude(i) + fgAODPhosCells->GetAmplitude(pos);
+                               
+                               //Check if it is MC, depending on that assing the mc lable, time and e fraction
+//                             Double_t time    = 0;
+                               Int_t    mclabel =-1;
+                               Double_t    efrac   = 0;
+                               if(cellsP->GetMCLabel(i) >= 0 && fgAODPhosCells->GetMCLabel(pos) < 0)
+                                 {
+                                   mclabel = cellsP->GetMCLabel(i) ;
+//                                 time    = fgAODPhosCells->GetTime(pos) ; // Time from data
+                                   if(amp > 0) efrac = cellsP->GetAmplitude(i) / amp;
+                                 }
+                               else if(fgAODPhosCells->GetMCLabel(pos) >= 0 &&  cellsP->GetMCLabel(i) < 0)
+                                 {
+                                   mclabel = fgAODPhosCells->GetMCLabel(pos) ;
+//                                 time    = cellsP->GetTime(i) ; // Time from data
+                                   if(amp > 0) efrac = fgAODPhosCells->GetAmplitude(pos) / amp;
+                                 }
+                               else 
+                                 { // take all from input
+                                   mclabel = cellsP->GetMCLabel(i) ;
+//                                 time    = cellsP->GetTime(i) ; 
+                                   if(amp > 0) efrac = cellsP->GetAmplitude(i) / amp;  
+                                 }
+                               
+                               fgAODPhosCells->SetCell(pos, cn, amp,cellsP->GetTime(i),mclabel,efrac);                
+                               
+                             } else 
+                             {
+                               AliAODCaloCells* copycells1 = new AliAODCaloCells(*fgAODPhosCells);
+                               fgAODPhosCells->CreateContainer(nc+1);
+                               Int_t nn = copycells1->GetNumberOfCells();
+                               
+                               while( nn-- ){ fgAODPhosCells->SetCell(nn,copycells1->GetCellNumber(nn),copycells1->GetAmplitude(nn), 
+                                                                      copycells1->GetTime(nn),copycells1->GetMCLabel(nn),copycells1->GetEFraction(nn)); }
+                               
+                               fgAODPhosCells->SetCell(nc++,cn,cellsP->GetAmplitude(i),cellsP->GetTime(i), cellsP->GetMCLabel(i),1.);
+                               
+                               delete copycells1;
+                             }
+                         }
+                       fgAODPhosCells->Sort();
+                     }
+                 } // Merge PHOS Cells
                
+               if (aodH->GetMergeEMCALTrigger() && aod->GetCaloTrigger("EMCAL")) 
+               {
+                       Int_t   tsEMCAL[48][64], px, py, ts;
+                       Float_t foEMCAL[48][64], am;
+                       
+                       for (Int_t i = 0; i < 48; i++) for (Int_t j = 0; j < 64; j++) 
+                       {
+                               tsEMCAL[i][j] = 0;
+                               foEMCAL[i][j] = 0.;
+                       }
+      
+      AliAODCaloTrigger& trg0 = *(aod->GetCaloTrigger("EMCAL"));
+      trg0.Reset();
+      while (trg0.Next())
+      {
+        trg0.GetPosition(px, py);
+        
+        if (px > -1 && py > -1) 
+        {
+          trg0.GetL1TimeSum(ts);
+          if (ts > -1) tsEMCAL[px][py] += ts;
+          
+          trg0.GetAmplitude(am);
+          if (am > -1) foEMCAL[px][py] += am;
+        }
+      }
+      
+      AliAODCaloTrigger& trg1 = *((aodH->GetEventToMerge())->GetCaloTrigger("EMCAL"));
+      
+      trg1.Reset();
+      while (trg1.Next())
+      {
+        trg1.GetPosition(px, py);
+        
+        if (px > -1 && py > -1) 
+        {
+          trg1.GetL1TimeSum(ts);
+          if (ts > -1) tsEMCAL[px][py] += ts;
+          
+          trg1.GetAmplitude(am);
+          if (am > -1) foEMCAL[px][py] += am;
+        }
+      }
+      
+      int nEntries = 0;
+      for (Int_t i = 0; i < 48; i++) 
+        for (Int_t j = 0; j < 64; j++) 
+          if (tsEMCAL[i][j] || foEMCAL[i][j]) nEntries++;
+      
+      fgAODEMCALTrigger->Allocate(nEntries);
+      Int_t timesL0[10]; for (int i = 0; i < 10; i++) timesL0[i] = -1;
+      
+      for (Int_t i = 0; i < 48; i++) 
+        for (Int_t j = 0; j < 64; j++) 
+          if (tsEMCAL[i][j] || foEMCAL[i][j]) 
+            fgAODEMCALTrigger->Add(i, j, foEMCAL[i][j], -1., timesL0, 0, tsEMCAL[i][j], 0);
+    }
+               
+               if (aodH->GetMergePHOSTrigger()) 
+               {
+                       // To be implemented by PHOS
+               }
            } // merging
            
            handler->SetAODIsReplicated();
        }
     }
 
+
 // Call the user analysis    
-    if (!fMCEventHandler) {
-       if (isSelected) 
-           UserExec(option);
-    } else {
-       if (isSelected && (fMCEventHandler->InitOk())) 
-           UserExec(option);
-    }
+       if (isSelected) UserExec(option);
     
 // Added protection in case the derived task is not an AOD producer.
     AliAnalysisDataSlot *out0 = GetOutputSlot(0);
     if (out0 && out0->IsConnected()) PostData(0, fTreeA);    
-    
+
     DisconnectMultiHandler();
 }
 
@@ -594,6 +837,7 @@ void AliAnalysisTaskSE::LoadBranches() const
   TIter next(arr);
   TObject *obj;
   while ((obj=next())) mgr->LoadBranch(obj->GetName());
+  delete arr;
 }
 
 
@@ -607,9 +851,9 @@ void AliAnalysisTaskSE::ConnectMultiHandler()
    fMultiInputHandler = dynamic_cast<AliMultiInputEventHandler *>(fInputHandler);
    if (fMultiInputHandler) {
       fInputHandler = dynamic_cast<AliInputEventHandler *>(fMultiInputHandler->GetFirstInputEventHandler());
-      fMCEventHandler = dynamic_cast<AliMCEventHandler *>(fMultiInputHandler->GetFirstMCEventHandler());
+      fMCEventHandler = dynamic_cast<AliInputEventHandler *>(fMultiInputHandler->GetFirstMCEventHandler());
    } else { 
-      fMCEventHandler = dynamic_cast<AliMCEventHandler *>((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler());
+      fMCEventHandler = dynamic_cast<AliInputEventHandler *>((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler());
    }
    if (fMCEventHandler) fMCEvent = fMCEventHandler->MCEvent();
 }