]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ANALYSIS/AliAnalysisTaskSE.cxx
Fix for savannah bug report 87728 (Laurent) + fix invalid read found with valgrind...
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskSE.cxx
index 8c9b898a3a6c4c1027b61446142fa14523c811ad..6142278d434d448352253fb4806f846b05288cdf 100644 (file)
@@ -179,8 +179,8 @@ void AliAnalysisTaskSE::ConnectInputData(Option_t* /*option*/)
     if (fDebug > 1) printf("AnalysisTaskSE::ConnectInputData() \n");
 
    // Connect input handlers (multi input handler is handled)
-   ConnectMultiHandler();
-
+    ConnectMultiHandler();
+    
     if (fInputHandler) {
        if ((fInputHandler->GetTree())->GetBranch("ESDfriend."))
            fESDfriend = ((AliESDInputHandler*)fInputHandler)->GetESDfriend();
@@ -211,15 +211,13 @@ 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;
@@ -280,13 +278,12 @@ 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->NeedsMCParticlesBranchReplication() || merging) && !(fgAODMCParticles))         
            {   
@@ -331,7 +328,7 @@ void AliAnalysisTaskSE::Exec(Option_t* option)
 //
 // 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)
@@ -363,15 +360,34 @@ void AliAnalysisTaskSE::Exec(Option_t* option)
     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()){
+      //printf("Skip Entry %lld, Offset %d, Tree Entries %d\n",aodH->GetReadEntry(),aodH->GetMergeOffset(), aodH->GetTreeToMerge()->GetEntries());
+          
+      // 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;
+    }
+    //else   printf("MERGE Entry %lld, Offset %d, Tree Entries %d\n",aodH->GetReadEntry(),aodH->GetMergeOffset(), aodH->GetTreeToMerge()->GetEntries());
+  }
+      
        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->NeedsTracksBranchReplication() || (merging &&  aodH->GetMergeTracks())) && (fgAODTracks))
            {
                TClonesArray* tracks = aod->GetTracks();
                new (fgAODTracks) TClonesArray(*tracks);
@@ -395,7 +411,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 +421,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 +432,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,71 +467,124 @@ void AliAnalysisTaskSE::Exec(Option_t* option)
                    }    
                }
            }
-           
            // 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)); }
+
+                   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);
+                               fgAODEmcalCells->SetCell(pos, cn, amp);
+                           } 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)); }
+                               fgAODEmcalCells->SetCell(nc++,cn,cellsA->GetAmplitude(i));
+                               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)); }
+                   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);
+                               fgAODPhosCells->SetCell(pos, cn, amp);
+                           } 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)); }
+                               fgAODPhosCells->SetCell(nc++,cn,cellsP->GetAmplitude(i));
+                               delete copycells1;
+                           }
+                       }
+                       fgAODPhosCells->Sort();
+                   }
+               } // Merge PHOS Cells
+                       
            } // merging
            
            handler->SetAODIsReplicated();
        }
     }
 
+
 // Call the user analysis    
     if (!fMCEventHandler) {
        if (isSelected) 
@@ -525,7 +597,7 @@ void AliAnalysisTaskSE::Exec(Option_t* 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();
 }