+ 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();
+ }
+ 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());
+ }
+ }
+ }
+ }
+ 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];
+ char ch_url[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));
+ gROOT->ProcessLine(Form("sprintf((char*)0x%lx, \"%%s\", gProof->GetUrl();)", ch_url));
+ TString clientUrl(ch_url);
+ TString full_path_str(full_path);
+ if (clientUrl.Contains("localhost")){
+ TObjArray* array = full_path_str.Tokenize ( "//" );
+ TObjString *strobj = ( TObjString *)array->At(1);
+ full_path_str.ReplaceAll(strobj->GetString().Data(),"localhost:11094");
+ if (fDebug > 1) Info("ImportWrappers","Using tunnel from %s to %s",full_path_str.Data(),filename);
+ }
+ if (fDebug > 1)
+ printf(" Copying file %s from PROOF scratch space\n", full_path_str.Data());
+ Bool_t gotit = TFile::Cp(full_path_str.Data(), filename);
+ if (!gotit) {
+ Error("ImportWrappers", "Could not get file %s from proof scratch space", cont->GetFileName());
+ continue;
+ }
+ // 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 = TFile::Open(filename, "READ");
+ if (!f) {
+ Error("ImportWrappers", "Cannot open file %s in read-only mode", filename);
+ continue;
+ }
+ TObject *obj = 0;
+ // Try to fetch first a list object having the container name.
+ obj = f->Get(cont->GetName());
+ if (!obj) {
+ // Fetch first object from file having the container type.
+ TIter nextkey(f->GetListOfKeys());
+ TKey *key;
+ while ((key=(TKey*)nextkey())) {
+ obj = f->Get(key->GetName());
+ if (obj && obj->IsA()->InheritsFrom(cont->GetType())) break;
+ }
+ }
+ if (!obj) {
+ Error("ImportWrappers", "Could not find object for container %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", filename);
+ 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);
+