+ // Pack all output data containers in the output list. Called at SlaveTerminate
+ // stage in PROOF case for each slave.
+ if (fDebug > 0) printf("->AliAnalysisManager::PackOutput()\n");
+ if (!target) {
+ Error("PackOutput", "No target. Aborting.");
+ return;
+ }
+ if (fInputEventHandler) fInputEventHandler ->Terminate();
+ if (fOutputEventHandler) fOutputEventHandler ->Terminate();
+ if (fMCtruthEventHandler) fMCtruthEventHandler->Terminate();
+
+ // Call FinishTaskOutput() for each event loop task (not called for
+ // post-event loop tasks - use Terminate() fo those)
+ TIter nexttask(fTasks);
+ AliAnalysisTask *task;
+ while ((task=(AliAnalysisTask*)nexttask())) {
+ if (!task->IsPostEventLoop()) {
+ if (fDebug > 0) printf("->FinishTaskOutput: task %s\n", task->GetName());
+ task->FinishTaskOutput();
+ if (fDebug > 0) printf("<-FinishTaskOutput: task %s\n", task->GetName());
+ }
+ }
+
+ if (fMode == kProofAnalysis) {
+ TIter next(fOutputs);
+ AliAnalysisDataContainer *output;
+ Bool_t isManagedByHandler = kFALSE;
+ while ((output=(AliAnalysisDataContainer*)next())) {
+ // Do not consider outputs of post event loop tasks
+ isManagedByHandler = kFALSE;
+ if (output->GetProducer()->IsPostEventLoop()) continue;
+ const char *filename = output->GetFileName();
+ if (!(strcmp(filename, "default")) && fOutputEventHandler) {
+ isManagedByHandler = kTRUE;
+ filename = fOutputEventHandler->GetOutputFileName();
+ }
+ // Check if data was posted to this container. If not, issue an error.
+ if (!output->GetData() && !isManagedByHandler) {
+ Error("PackOutput", "No data for output container %s. Forgot to PostData ?\n", output->GetName());
+ continue;
+ }
+ if (!output->IsSpecialOutput()) {
+ // Normal outputs
+ if (strlen(filename) && !isManagedByHandler) {
+ // File resident outputs
+ TFile *file = output->GetFile();
+ // Backup current folder
+ TDirectory *opwd = gDirectory;
+ // Create file if not existing and register to container.
+ if (file) file->cd();
+ else file = new TFile(filename, "RECREATE");
+ if (file->IsZombie()) {
+ Fatal("PackOutput", "Could not recreate file %s\n", filename);
+ return;
+ }
+ output->SetFile(file);
+ // Clear file list to release object ownership to user.
+ file->Clear();
+ // Save data to file, then close.
+ if (output->GetData()->InheritsFrom(TCollection::Class())) {
+ // If data is a collection, we set the name of the collection
+ // as the one of the container and we save as a single key.
+ TCollection *coll = (TCollection*)output->GetData();
+ coll->SetName(output->GetName());
+ coll->Write(output->GetName(), TObject::kSingleKey);
+ } else {
+ if (output->GetData()->InheritsFrom(TTree::Class())) {
+ TTree *tree = (TTree*)output->GetData();
+ tree->SetDirectory(file);
+ tree->AutoSave();
+ } else {
+ output->GetData()->Write();
+ }
+ }
+ if (fDebug > 1) printf("PackOutput %s: memory merge, file resident output\n", output->GetName());
+ if (fDebug > 2) {
+ printf(" file %s listing content:\n", filename);
+ file->ls();
+ }
+ file->Close();
+ // Restore current directory
+ if (opwd) opwd->cd();
+ } else {
+ // Memory-resident outputs
+ if (fDebug > 1) printf("PackOutput %s: memory merge memory resident output\n", filename);
+ }
+ AliAnalysisDataWrapper *wrap = 0;
+ if (isManagedByHandler) {
+ wrap = new AliAnalysisDataWrapper(fOutputEventHandler->GetTree());
+ wrap->SetName(output->GetName());
+ }
+ else wrap =output->ExportData();
+ // Output wrappers must NOT delete data after merging - the user owns them
+ wrap->SetDeleteData(kFALSE);
+ target->Add(wrap);
+ } else {
+ // Special outputs
+ TDirectory *opwd = gDirectory;
+ TFile *file = output->GetFile();
+ if (fDebug > 1 && file) printf("PackOutput %s: file merge, special output\n", output->GetName());
+ if (isManagedByHandler) {
+ // Terminate IO for files managed by the output handler
+ if (file) file->Write();
+ if (file && fDebug > 2) {
+ printf(" handled file %s listing content:\n", file->GetName());
+ file->ls();
+ }
+ fOutputEventHandler->TerminateIO();
+ continue;
+ }
+
+ if (!file) {
+ AliAnalysisTask *producer = output->GetProducer();
+ Error("PackOutput",
+ "File %s for special container %s was NOT opened in %s::CreateOutputObjects !!!",
+ output->GetFileName(), output->GetName(), producer->ClassName());
+ continue;
+ }
+ file->cd();
+ // Release object ownership to users after writing data to file
+ if (output->GetData()->InheritsFrom(TCollection::Class())) {
+ // If data is a collection, we set the name of the collection
+ // as the one of the container and we save as a single key.
+ TCollection *coll = (TCollection*)output->GetData();
+ coll->SetName(output->GetName());
+ coll->Write(output->GetName(), TObject::kSingleKey);
+ } else {
+ if (output->GetData()->InheritsFrom(TTree::Class())) {
+ TTree *tree = (TTree*)output->GetData();
+ tree->SetDirectory(file);
+ tree->AutoSave();
+ } else {
+ output->GetData()->Write();
+ }
+ }
+ file->Clear();
+ if (fDebug > 2) {
+ printf(" file %s listing content:\n", output->GetFileName());
+ file->ls();
+ }
+ file->Close();
+ // Restore current directory
+ if (opwd) opwd->cd();
+ // Check if a special output location was provided or the output files have to be merged
+ if (strlen(fSpecialOutputLocation.Data())) {
+ TString remote = fSpecialOutputLocation;
+ remote += "/";
+ Int_t gid = gROOT->ProcessLine("gProofServ->GetGroupId();");
+ remote += Form("%s_%d_", gSystem->HostName(), gid);
+ remote += output->GetFileName();
+ TFile::Cp(output->GetFileName(), remote.Data());
+ } else {
+ // No special location specified-> use TProofOutputFile as merging utility
+ // The file at this output slot must be opened in CreateOutputObjects
+ if (fDebug > 1) printf(" File %s to be merged...\n", output->GetFileName());
+ }
+ }
+ }
+ }
+ if (fDebug > 0) printf("<-AliAnalysisManager::PackOutput: output list contains %d containers\n", target->GetSize());
+}
+
+//______________________________________________________________________________
+void AliAnalysisManager::ImportWrappers(TList *source)
+{
+// Import data in output containers from wrappers coming in source.
+ if (fDebug > 0) printf("->AliAnalysisManager::ImportWrappers()\n");
+ TIter next(fOutputs);
+ AliAnalysisDataContainer *cont;
+ AliAnalysisDataWrapper *wrap;
+ Int_t icont = 0;
+ while ((cont=(AliAnalysisDataContainer*)next())) {
+ wrap = 0;
+ if (cont->GetProducer()->IsPostEventLoop()) continue;
+ const char *filename = cont->GetFileName();
+ Bool_t isManagedByHandler = kFALSE;
+ if (!(strcmp(filename, "default")) && fOutputEventHandler) {
+ isManagedByHandler = kTRUE;
+ filename = fOutputEventHandler->GetOutputFileName();
+ }
+ if (cont->IsSpecialOutput()) {
+ if (strlen(fSpecialOutputLocation.Data()) && !isManagedByHandler) continue;
+ // Copy merged file from PROOF scratch space
+ char full_path[512];
+ TObject *pof = source->FindObject(filename);
+ if (!pof || !pof->InheritsFrom("TProofOutputFile")) {
+ Error("ImportWrappers", "TProofOutputFile object not found in output list for container %s", cont->GetName());
+ continue;
+ }
+ gROOT->ProcessLine(Form("sprintf((char*)0x%lx, \"%%s\", ((TProofOutputFile*)0x%lx)->GetOutputFileName();)", full_path, pof));
+ if (fDebug > 1)
+ printf(" Copying file %s from PROOF scratch space\n", full_path);
+ Bool_t gotit = TFile::Cp(full_path, filename);
+ if (!gotit) {
+ Error("ImportWrappers", "Could not get file %s from proof scratch space", cont->GetFileName());
+ }
+ // Normally we should connect data from the copied file to the
+ // corresponding output container, but it is not obvious how to do this
+ // automatically if several objects in file...
+ TFile *f = new TFile(filename, "READ");
+ TObject *obj = 0;
+ if (!isManagedByHandler) obj = f->Get(cont->GetName());
+ if (!obj && !isManagedByHandler) {
+ Error("ImportWrappers", "Could not find object %s in file %s", cont->GetName(), filename);
+ continue;
+ }
+ wrap = new AliAnalysisDataWrapper(obj);
+ wrap->SetDeleteData(kFALSE);
+ }
+ if (!wrap) wrap = (AliAnalysisDataWrapper*)source->FindObject(cont->GetName());
+ if (!wrap) {
+ Error("ImportWrappers","Container %s not found in analysis output !", cont->GetName());
+ continue;
+ }
+ icont++;
+ if (fDebug > 1) {
+ printf(" Importing data for container %s", cont->GetName());
+ if (strlen(filename)) printf(" -> file %s\n", cont->GetFileName());
+ else printf("\n");
+ }
+ cont->ImportData(wrap);
+ }
+ if (fDebug > 0) printf("<-AliAnalysisManager::ImportWrappers(): %d containers imported\n", icont);
+}
+
+//______________________________________________________________________________
+void AliAnalysisManager::UnpackOutput(TList *source)
+{
+ // Called by AliAnalysisSelector::Terminate only on the client.
+ if (fDebug > 0) printf("->AliAnalysisManager::UnpackOutput()\n");
+ if (!source) {
+ Error("UnpackOutput", "No target. Aborting.");
+ return;
+ }
+ if (fDebug > 1) printf(" Source list contains %d containers\n", source->GetSize());
+
+ if (fMode == kProofAnalysis) ImportWrappers(source);
+
+ TIter next(fOutputs);
+ AliAnalysisDataContainer *output;
+ while ((output=(AliAnalysisDataContainer*)next())) {
+ if (!output->GetData()) continue;
+ // Check if there are client tasks that run post event loop
+ if (output->HasConsumers()) {
+ // Disable event loop semaphore
+ output->SetPostEventLoop(kTRUE);
+ TObjArray *list = output->GetConsumers();
+ Int_t ncons = list->GetEntriesFast();
+ for (Int_t i=0; i<ncons; i++) {
+ AliAnalysisTask *task = (AliAnalysisTask*)list->At(i);
+ task->CheckNotify(kTRUE);
+ // If task is active, execute it
+ if (task->IsPostEventLoop() && task->IsActive()) {
+ if (fDebug > 0) printf("== Executing post event loop task %s\n", task->GetName());
+ task->ExecuteTask();
+ }
+ }
+ }
+ }
+ if (fDebug > 0) printf("<-AliAnalysisManager::UnpackOutput()\n");