]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ANALYSIS/AliAnalysisTaskSE.cxx
Fix plus small changes
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskSE.cxx
index c8ca54a9cdcfd96e7d549c09863807c78c7b9827..651aed7417c2ae4d1d7fc635068383c92f2d268b 100644 (file)
 #include "AliAnalysisManager.h"
 #include "AliAnalysisCuts.h"
 #include "AliAnalysisDataSlot.h"
+#include "AliAnalysisDataContainer.h"
 
 #include "AliESDEvent.h"
 #include "AliESDfriend.h"
 #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"
 #include "AliAODInputHandler.h"
 #include "AliMCEventHandler.h"
 #include "AliInputEventHandler.h"
+#include "AliMultiInputEventHandler.h"
 #include "AliESDInputHandler.h"
 #include "AliMCEvent.h"
 #include "AliStack.h"
@@ -51,6 +56,8 @@ ClassImp(AliAnalysisTaskSE)
 
 ////////////////////////////////////////////////////////////////////////
 AliAODHeader*    AliAnalysisTaskSE::fgAODHeader         = NULL;
+AliTOFHeader*    AliAnalysisTaskSE::fgTOFHeader         = NULL;
+AliAODVZERO*     AliAnalysisTaskSE::fgAODVZERO          = NULL;
 TClonesArray*    AliAnalysisTaskSE::fgAODTracks         = NULL;
 TClonesArray*    AliAnalysisTaskSE::fgAODVertices       = NULL;
 TClonesArray*    AliAnalysisTaskSE::fgAODV0s            = NULL;
@@ -62,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(),
@@ -76,7 +86,9 @@ AliAnalysisTaskSE::AliAnalysisTaskSE():
     fTreeA(0x0),
     fCurrentRunNumber(-1),
     fHistosQA(0x0),
-    fOfflineTriggerMask(0)
+    fOfflineTriggerMask(0),
+    fMultiInputHandler(0),
+    fMCEventHandler(0)
 {
   // Default constructor
 }
@@ -93,7 +105,9 @@ AliAnalysisTaskSE::AliAnalysisTaskSE(const char* name):
     fTreeA(0x0),
     fCurrentRunNumber(-1),
     fHistosQA(0x0),
-    fOfflineTriggerMask(0)
+    fOfflineTriggerMask(0),
+    fMultiInputHandler(0),
+    fMCEventHandler(0)
 {
   // Default constructor
     DefineInput (0, TChain::Class());
@@ -112,7 +126,9 @@ AliAnalysisTaskSE::AliAnalysisTaskSE(const AliAnalysisTaskSE& obj):
     fTreeA(0x0),
     fCurrentRunNumber(-1),
     fHistosQA(0x0),
-    fOfflineTriggerMask(0)
+    fOfflineTriggerMask(0),
+    fMultiInputHandler(obj.fMultiInputHandler),
+    fMCEventHandler(obj.fMCEventHandler)
 {
 // Copy constructor
     fDebug            = obj.fDebug;
@@ -132,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;
@@ -144,27 +163,19 @@ AliAnalysisTaskSE& AliAnalysisTaskSE::operator=(const AliAnalysisTaskSE& other)
     fCurrentRunNumber = other.fCurrentRunNumber;
     fHistosQA         = other.fHistosQA;
     fOfflineTriggerMask = other.fOfflineTriggerMask;
+    fMultiInputHandler  = other.fMultiInputHandler;
+    fMCEventHandler     = other.fMCEventHandler;
     return *this;
 }
 
-
+//______________________________________________________________________________
 void AliAnalysisTaskSE::ConnectInputData(Option_t* /*option*/)
 {
 // Connect the input data
     if (fDebug > 1) printf("AnalysisTaskSE::ConnectInputData() \n");
-//
-//  ESD
-//
-    fInputHandler = (AliInputEventHandler*) 
-         ((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
-//
-//  Monte Carlo
-//
-    AliMCEventHandler*    mcH = 0;
-    mcH = (AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler());
-    if (mcH) {
-       fMCEvent = mcH->MCEvent();
-    } 
+
+   // Connect input handlers (multi input handler is handled)
+    ConnectMultiHandler();
     
     if (fInputHandler) {
        if ((fInputHandler->GetTree())->GetBranch("ESDfriend."))
@@ -177,6 +188,8 @@ void AliAnalysisTaskSE::ConnectInputData(Option_t* /*option*/)
          AliError("No Input Event Handler connected") ; 
          return ; 
     }
+    // Disconnect multi handler
+    DisconnectMultiHandler();
 }
 
 void AliAnalysisTaskSE::CreateOutputObjects()
@@ -194,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");
@@ -263,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))         
            {   
@@ -278,132 +312,166 @@ 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();
        }
-    } else {
-       AliWarning("No AOD Event Handler connected.") ; 
     }
+    ConnectMultiHandler();
     UserCreateOutputObjects();
+    DisconnectMultiHandler();
 }
 
 void AliAnalysisTaskSE::Exec(Option_t* option)
 {
 //
 // Exec analysis of one event
+
+    ConnectMultiHandler();
+
     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());
+      ((AliAnalysisManager::GetAnalysisManager())->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()) {
-      // Get the actual offline trigger mask for the event
-      isSelected = fInputHandler->IsEventSelected();
-      // If the task had a selection AND it with the event mask (i.e. event interesting for the task if at least one matching trigger)
-      if (fOfflineTriggerMask) isSelected &= fOfflineTriggerMask;
+    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();
     }
 //  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();
-       
-       if (!(handler->IsStandard()) && !(handler->AODIsReplicated())) {
-           if ((handler->NeedsHeaderReplication()) && (fgAODHeader))
+       // 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() || merging) && (fgAODHeader))
            {
              // copy the contents by assigment
-             *fgAODHeader =  *(dynamic_cast<AliAODHeader*>(InputEvent()->GetHeader()));
+             *fgAODHeader =  *(aod->GetHeader());
            }
-           if ((handler->NeedsTracksBranchReplication() || merging) && (fgAODTracks))
+            if ((handler->NeedsTOFHeaderReplication() || merging) && (fgTOFHeader))
+            {
+              *fgTOFHeader =  *(aod->GetTOFHeader());
+            }
+            if ((handler->NeedsVZEROReplication() || merging) && (fgAODVZERO))
+            {
+              *fgAODVZERO = *(aod->GetVZEROData());
+            }
+
+           if ((handler->NeedsTracksBranchReplication() || (merging &&  aodH->GetMergeTracks())) && (fgAODTracks))
            {
-               TClonesArray* tracks = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetTracks();
+               TClonesArray* tracks = aod->GetTracks();
                new (fgAODTracks) TClonesArray(*tracks);
            }
            if ((handler->NeedsVerticesBranchReplication() || merging) && (fgAODVertices))
            {
-               TClonesArray* vertices = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetVertices();
+               TClonesArray* vertices = aod->GetVertices();
                new (fgAODVertices) TClonesArray(*vertices);
            }
            if ((handler->NeedsV0sBranchReplication()) && (fgAODV0s))
            {
-               TClonesArray* v0s = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetV0s();
+               TClonesArray* v0s = aod->GetV0s();
                new (fgAODV0s) TClonesArray(*v0s);
            }
            if ((handler->NeedsTrackletsBranchReplication()) && (fgAODTracklets))
            {
-             *fgAODTracklets = *(dynamic_cast<AliAODEvent*>(InputEvent()))->GetTracklets();
+             *fgAODTracklets = *aod->GetTracklets();
            }
            if ((handler->NeedsPMDClustersBranchReplication()) && (fgAODPMDClusters))
            {
-               TClonesArray* pmdClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetPmdClusters();
+               TClonesArray* pmdClusters = aod->GetPmdClusters();
                new (fgAODPMDClusters) TClonesArray(*pmdClusters);
            }
-           if ((handler->NeedsJetsBranchReplication() || merging) && (fgAODJets))
+           if ((handler->NeedsJetsBranchReplication() || (merging &&aodH->GetMergeTracks())) && (fgAODJets))
            {
-               TClonesArray* jets = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetJets();
+               TClonesArray* jets = aod->GetJets();
                new (fgAODJets) TClonesArray(*jets);
            }
            if ((handler->NeedsFMDClustersBranchReplication()) && (fgAODFMDClusters))
            {
-               TClonesArray* fmdClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetFmdClusters();
+               TClonesArray* fmdClusters = aod->GetFmdClusters();
                new (fgAODFMDClusters) TClonesArray(*fmdClusters);
            }
-           if ((handler->NeedsCaloClustersBranchReplication() || merging) && (fgAODCaloClusters))
+           if ((handler->NeedsCaloClustersBranchReplication() || 
+                (merging && (aodH->GetMergeEMCALClusters() || aodH->GetMergePHOSClusters()))) 
+               && (fgAODCaloClusters))
            {
-               TClonesArray* caloClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetCaloClusters();
+               TClonesArray* caloClusters = aod->GetCaloClusters();
                new (fgAODCaloClusters) TClonesArray(*caloClusters);
            }
 
            if ((handler->NeedsMCParticlesBranchReplication() || merging) && (fgAODMCParticles))
            {
-               TClonesArray* mcParticles = (TClonesArray*) ((dynamic_cast<AliAODEvent*>(InputEvent()))->FindListObject("mcparticles"));
-               new (fgAODMCParticles) TClonesArray(*mcParticles);
+               TClonesArray* mcParticles = (TClonesArray*) (aod->FindListObject("mcparticles"));
+               if( mcParticles )
+                 new (fgAODMCParticles) TClonesArray(*mcParticles);
            }
            
-           if ((handler->NeedsDimuonsBranchReplication() || merging) && (fgAODDimuons))
+           if ((handler->NeedsDimuonsBranchReplication() || (merging && aodH->GetMergeTracks())) && (fgAODDimuons))
            {
                fgAODDimuons->Clear();
                TClonesArray& dimuons = *fgAODDimuons;
                TClonesArray& tracksnew = *fgAODTracks;
                
-                Int_t nMuonTrack[10]; 
-                for(Int_t imuon = 0; imuon < 10; imuon++) nMuonTrack[imuon] = 0;
+                Int_t nMuonTrack[100]; 
+                for(Int_t imuon = 0; imuon < 100; imuon++) nMuonTrack[imuon] = 0;
                 Int_t nMuons=0;
                for(Int_t ii=0; ii < fgAODTracks->GetEntries(); ii++){
                    AliAODTrack *track = (AliAODTrack*) fgAODTracks->At(ii);
@@ -427,86 +495,284 @@ 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(i) < 0)
+                                 {
+                                   mclabel = cellsA->GetMCLabel(i) ;
+                                   time    = fgAODEmcalCells->GetTime(i) ; // Time from data
+                                   if(amp > 0) efrac = cellsA->GetAmplitude(i) / amp;
+                                 }
+                               else if(fgAODEmcalCells->GetMCLabel(i) >= 0 &&  cellsA->GetMCLabel(i) < 0)
+                                 {
+                                   mclabel = fgAODEmcalCells->GetMCLabel(i) ;
+                                   time    = cellsA->GetTime(i) ; // Time from data
+                                   if(amp > 0) efrac = fgAODEmcalCells->GetAmplitude(i) / 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(i) < 0)
+                                 {
+                                   mclabel = cellsP->GetMCLabel(i) ;
+                                   time    = fgAODPhosCells->GetTime(i) ; // Time from data
+                                   if(amp > 0) efrac = cellsP->GetAmplitude(i) / amp;
+                                 }
+                               else if(fgAODPhosCells->GetMCLabel(i) >= 0 &&  cellsP->GetMCLabel(i) < 0)
+                                 {
+                                   mclabel = fgAODPhosCells->GetMCLabel(i) ;
+                                   time    = cellsP->GetTime(i) ; // Time from data
+                                   if(amp > 0) efrac = fgAODPhosCells->GetAmplitude(i) / 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    
-    AliMCEventHandler*    mcH = 0;
-    mcH = (AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler());
 
-    if (!mcH) {
+// Call the user analysis    
+    if (!fMCEventHandler) {
        if (isSelected) 
            UserExec(option);
     } else {
-       if (isSelected && (mcH->InitOk())) 
+       if (isSelected && (fMCEventHandler->InitOk())) 
            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();
 }
 
 const char* AliAnalysisTaskSE::CurrentFileName()
@@ -547,4 +813,59 @@ Bool_t AliAnalysisTaskSE::Notify()
     return (UserNotify());
 }
 
+const AliEventTag *AliAnalysisTaskSE::EventTag() const
+{
+// Returns tag for the current event, if any. The return value should always be checked by the user.
+   if (!fInputHandler) {
+      Error("EventTag", "Input handler not yet available. Call this in UserExec");
+      return NULL;
+   }
+   return fInputHandler->GetEventTag();
+}
 
+void AliAnalysisTaskSE::LoadBranches() const
+{
+// Load all branches declared in fBranchNames data member of the parent class.
+// Should be called in UserExec.
+  if (!fInputHandler) {
+     Error("LoadBranches", "Input handler not available yet. Call this in UserExec");
+     return;
+  }
+  AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
+  if (mgr->GetAutoBranchLoading()) return;
+  TString taskbranches;
+  GetBranches(fInputHandler->GetDataType(), taskbranches);
+  if (taskbranches.IsNull()) return;
+  TObjArray *arr = taskbranches.Tokenize(",");
+  TIter next(arr);
+  TObject *obj;
+  while ((obj=next())) mgr->LoadBranch(obj->GetName());
+  delete arr;
+}
+
+
+//_________________________________________________________________________________________________
+void AliAnalysisTaskSE::ConnectMultiHandler()
+{
+   //
+   // Connect MultiHandler
+   //
+   fInputHandler = (AliInputEventHandler *)((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
+   fMultiInputHandler = dynamic_cast<AliMultiInputEventHandler *>(fInputHandler);
+   if (fMultiInputHandler) {
+      fInputHandler = dynamic_cast<AliInputEventHandler *>(fMultiInputHandler->GetFirstInputEventHandler());
+      fMCEventHandler = dynamic_cast<AliMCEventHandler *>(fMultiInputHandler->GetFirstMCEventHandler());
+   } else { 
+      fMCEventHandler = dynamic_cast<AliMCEventHandler *>((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler());
+   }
+   if (fMCEventHandler) fMCEvent = fMCEventHandler->MCEvent();
+}
+
+//_________________________________________________________________________________________________
+void AliAnalysisTaskSE::DisconnectMultiHandler()
+{
+   //
+   // Disconnect MultiHandler
+   //
+   if (fMultiInputHandler) fInputHandler = fMultiInputHandler;
+}