+ // 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();
+ }
+ TString outFilename = file->GetName();
+ 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 ( outFilename.Data(), 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());
+ }
+ }