]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ANALYSIS/AliAnalysisManager.cxx
Moving reconstruction settings to the reco params, new cosmics reco param OCDB entry...
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisManager.cxx
index 9be896cff3668e8e9db19878c52a0a40e41584b6..136f64ee76b0ee3b93b29347f699417494c79385 100644 (file)
@@ -70,8 +70,11 @@ AliAnalysisManager::AliAnalysisManager(const char *name, const char *title)
                     fContainers(NULL),
                     fInputs(NULL),
                     fOutputs(NULL),
+                    fCommonInput(NULL),
+                    fCommonOutput(NULL),
                     fSelector(NULL),
-                    fGridHandler(NULL)
+                    fGridHandler(NULL),
+                    fExtraFiles("")
 {
 // Default constructor.
    fgAnalysisManager = this;
@@ -104,8 +107,11 @@ AliAnalysisManager::AliAnalysisManager(const AliAnalysisManager& other)
                     fContainers(NULL),
                     fInputs(NULL),
                     fOutputs(NULL),
+                    fCommonInput(NULL),
+                    fCommonOutput(NULL),
                     fSelector(NULL),
-                    fGridHandler(NULL)
+                    fGridHandler(NULL),
+                    fExtraFiles()
 {
 // Copy constructor.
    fTasks      = new TObjArray(*other.fTasks);
@@ -139,8 +145,11 @@ AliAnalysisManager& AliAnalysisManager::operator=(const AliAnalysisManager& othe
       fContainers = new TObjArray(*other.fContainers);
       fInputs     = new TObjArray(*other.fInputs);
       fOutputs    = new TObjArray(*other.fOutputs);
+      fCommonInput = NULL;
+      fCommonOutput = NULL;
       fSelector   = NULL;
       fGridHandler = NULL;
+      fExtraFiles = other.fExtraFiles;
       fgAnalysisManager = this;
    }
    return *this;
@@ -232,7 +241,8 @@ Bool_t AliAnalysisManager::Init(TTree *tree)
    if (!fInitOK) InitAnalysis();
    if (!fInitOK) return kFALSE;
    fTree = tree;
-   AliAnalysisDataContainer *top = (AliAnalysisDataContainer*)fInputs->At(0);
+   AliAnalysisDataContainer *top = fCommonInput;
+   if (!top) top = (AliAnalysisDataContainer*)fInputs->At(0);
    if (!top) {
       Error("Init","No top input container !");
       return kFALSE;
@@ -261,24 +271,13 @@ void AliAnalysisManager::SlaveBegin(TTree *tree)
    // Call Init of EventHandler
    if (fOutputEventHandler) {
       if (fMode == kProofAnalysis) {
-         TIter nextout(fOutputs);
-         AliAnalysisDataContainer *c_aod;
-         while ((c_aod=(AliAnalysisDataContainer*)nextout())) if (!strcmp(c_aod->GetFileName(),"default")) break;
-         if (c_aod && c_aod->IsSpecialOutput()) {
-            // Merging via files
-            if (fDebug > 1) printf("   Initializing special output file %s...\n", fOutputEventHandler->GetOutputFileName());
-            OpenProofFile(fOutputEventHandler->GetOutputFileName(), "RECREATE");
-            c_aod->SetFile(gFile);
-            init = fOutputEventHandler->Init("proofspecial");
-            if (!init) msg = "Failed to initialize output handler on worker using special proof output";
-         } else {
-            // Merging in memory
-            init = fOutputEventHandler->Init("proof");
-            if (!init) msg = "Failed to initialize output handler on worker";
-         }   
+         // Merging AOD's in PROOF via TProofOutputFile
+         if (fDebug > 1) printf("   Initializing AOD output file %s...\n", fOutputEventHandler->GetOutputFileName());
+         init = fOutputEventHandler->Init("proof");
+         if (!init) msg = "Failed to initialize output handler on worker";
       } else {
          init = fOutputEventHandler->Init("local");
-         if (!init) msg = "Failed to initialize output handler on worker";
+         if (!init) msg = "Failed to initialize output handler";
       }
       initOK &= init;
       if (!fSelector) Error("SlaveBegin", "Selector not set");
@@ -433,6 +432,7 @@ void AliAnalysisManager::PackOutput(TList *target)
          const char *filename = output->GetFileName();
          if (!(strcmp(filename, "default")) && fOutputEventHandler) {
             isManagedByHandler = kTRUE;
+            printf("#### Handler output. Extra: %s\n", fExtraFiles.Data());
             filename = fOutputEventHandler->GetOutputFileName();
          }
          // Check if data was posted to this container. If not, issue an error.
@@ -467,7 +467,7 @@ void AliAnalysisManager::PackOutput(TList *target)
                } else {
                   if (output->GetData()->InheritsFrom(TTree::Class())) {
                      TTree *tree = (TTree*)output->GetData();
-                     tree->SetDirectory(file);
+                     // tree->SetDirectory(file);
                      tree->AutoSave();
                   } else {
                      output->GetData()->Write();
@@ -495,10 +495,18 @@ void AliAnalysisManager::PackOutput(TList *target)
             wrap->SetDeleteData(kFALSE);
             target->Add(wrap);
          } else {
-         // Special outputs
+         // Special outputs. The file must be opened and connected to the container.
             TDirectory *opwd = gDirectory;
             TFile *file = output->GetFile();
-            if (fDebug > 1 && file) printf("PackOutput %s: file merge, special output\n", output->GetName());
+            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;
+            }   
+            TString outFilename = file->GetName();
+            if (fDebug > 1) printf("PackOutput %s: special output\n", output->GetName());
             if (isManagedByHandler) {
                // Terminate IO for files managed by the output handler
                if (file) file->Write();
@@ -507,40 +515,31 @@ void AliAnalysisManager::PackOutput(TList *target)
                   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 {               
+               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 {
-                  output->GetData()->Write();
-               }   
-            }      
-            file->Clear();
-            if (fDebug > 2) {
-               printf("   file %s listing content:\n", output->GetFileName());
-               file->ls();
+                  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();
             }
-            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
@@ -548,13 +547,45 @@ void AliAnalysisManager::PackOutput(TList *target)
                TString remote = fSpecialOutputLocation;
                remote += "/";
                Int_t gid = gROOT->ProcessLine("gProofServ->GetGroupId();");
-               remote += Form("%s_%d_", gSystem->HostName(), gid);
-               remote += output->GetFileName();
+               if (remote.BeginsWith("alien://")) {
+                  gROOT->ProcessLine("TGrid::Connect(\"alien://pcapiserv01.cern.ch:10000\", gProofServ->GetUser());");
+                  remote += outFilename;
+                  remote.ReplaceAll(".root", Form("_%d.root", gid));
+               } else {   
+                  remote += Form("%s_%d_", gSystem->HostName(), gid);
+                  remote += outFilename;
+               }   
+               if (fDebug > 1) 
+                  Info("PackOutput", "Output file for container %s to be copied \n   at: %s. No merging.",
+                       output->GetName(), remote.Data());
                TFile::Cp ( outFilename.Data(), remote.Data() );
+               // Copy extra outputs
+               if (fExtraFiles.Length() && isManagedByHandler) {
+                  TObjArray *arr = fExtraFiles.Tokenize(" ");
+                  TObjString *os;
+                  TIter nextfilename(arr);
+                  while ((os=(TObjString*)nextfilename())) {
+                     outFilename = os->GetString();
+                     remote = fSpecialOutputLocation;
+                     remote += "/";
+                     if (remote.BeginsWith("alien://")) {
+                        remote += outFilename;
+                        remote.ReplaceAll(".root", Form("_%d.root", gid));
+                     } else {   
+                        remote += Form("%s_%d_", gSystem->HostName(), gid);
+                        remote += outFilename;
+                     }   
+                     if (fDebug > 1) 
+                        Info("PackOutput", "Extra AOD file %s to be copied \n   at: %s. No merging.",
+                             outFilename.Data(), remote.Data());
+                     TFile::Cp ( outFilename.Data(), remote.Data() );
+                  }   
+                  delete arr;
+               }   
             } 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 > 1) printf("   File for container %s to be merged via file merger...\n", output->GetName());
             }
          }      
       }
@@ -582,37 +613,19 @@ void AliAnalysisManager::ImportWrappers(TList *source)
          filename = fOutputEventHandler->GetOutputFileName();
       }
       if (cont->IsSpecialOutput() || inGrid) {
-         if (strlen(fSpecialOutputLocation.Data()) && !isManagedByHandler) continue;
+         if (strlen(fSpecialOutputLocation.Data())) continue;
          // Copy merged file from PROOF scratch space. 
          // In case of grid the files are already in the current directory.
          if (!inGrid) {
-            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);
-               TObjArray* arrayPort = strobj->GetString().Tokenize ( ":" );
-               TObjString *strobjPort = ( TObjString *) arrayPort->At(1);
-               full_path_str.ReplaceAll(strobj->GetString().Data(),"localhost:PORT");
-               full_path_str.ReplaceAll(":PORT",Form(":%s",strobjPort->GetString().Data()));
-               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;
+            if (isManagedByHandler && fExtraFiles.Length()) {
+               // Copy extra registered dAOD files.
+               TObjArray *arr = fExtraFiles.Tokenize(" ");
+               TObjString *os;
+               TIter nextfilename(arr);
+               while ((os=(TObjString*)nextfilename())) GetFileFromWrapper(os->GetString(), source);
+               delete arr;
             }
+            if (!GetFileFromWrapper(filename, source)) continue;
          }   
          // Normally we should connect data from the copied file to the
          // corresponding output container, but it is not obvious how to do this
@@ -753,7 +766,16 @@ void AliAnalysisManager::Terminate()
    while ((output=(AliAnalysisDataContainer*)next1())) {
       // Close all files at output
       TDirectory *opwd = gDirectory;
-      if (output->GetFile()) output->GetFile()->Close();
+      if (output->GetFile()) {
+         output->GetFile()->Close();
+         // Copy merged outputs in alien if requested
+         if (fSpecialOutputLocation.Length() && 
+             fSpecialOutputLocation.BeginsWith("alien://")) {
+            Info("Terminate", "Copy file %s to %s", output->GetFile()->GetName(),fSpecialOutputLocation.Data()); 
+            TFile::Cp(output->GetFile()->GetName(), 
+                      Form("%s/%s", fSpecialOutputLocation.Data(), output->GetFile()->GetName()));
+         }             
+      }   
       if (opwd) opwd->cd();
    }   
 
@@ -772,14 +794,9 @@ void AliAnalysisManager::Terminate()
          tree->SetMarkerSize(0.5);
          if (!gROOT->IsBatch()) {
             tree->SetAlias("event", "id0");
-            tree->SetAlias("memUSED", "pI.fMemVirtual");
-            tree->SetAlias("userCPU", "pI.fCpuUser");
-            TCanvas *c = new TCanvas("SysInfo","SysInfo",10,10,800,600);
-            c->Divide(2,1,0.01,0.01);
-            c->cd(1);
+            tree->SetAlias("memUSED", "mi.fMemUsed");
+            new TCanvas("SysInfo","SysInfo",10,10,800,600);
             tree->Draw("memUSED:event","","", 1234567890, 0);
-            c->cd(2);
-            tree->Draw("userCPU:event","","", 1234567890, 0);
          }   
          tree->Write();
          f.Close();
@@ -846,6 +863,10 @@ Bool_t AliAnalysisManager::ConnectInput(AliAnalysisTask *task, Int_t islot,
                                         AliAnalysisDataContainer *cont)
 {
 // Connect input of an existing task to a data container.
+   if (!task) {
+      Error("ConnectInput", "Task pointer is NULL");
+      return kFALSE;
+   }   
    if (!fTasks->FindObject(task)) {
       AddTask(task);
       Info("ConnectInput", "Task %s was not registered. Now owned by analysis manager", task->GetName());
@@ -859,6 +880,10 @@ Bool_t AliAnalysisManager::ConnectOutput(AliAnalysisTask *task, Int_t islot,
                                         AliAnalysisDataContainer *cont)
 {
 // Connect output of an existing task to a data container.
+   if (!task) {
+      Error("ConnectOutput", "Task pointer is NULL");
+      return kFALSE;
+   }   
    if (!fTasks->FindObject(task)) {
       AddTask(task);
       Warning("ConnectOutput", "Task %s not registered. Now owned by analysis manager", task->GetName());
@@ -1038,7 +1063,10 @@ void AliAnalysisManager::StartAnalysis(const char *type, TTree *tree, Long64_t n
       while ((task=(AliAnalysisTask*)nextTask())) {
          task->LocalInit();
       }
-      fGridHandler->StartAnalysis(nentries, firstentry);
+      if (!fGridHandler->StartAnalysis(nentries, firstentry)) {
+         Info("StartAnalysis", "Grid analysis was stopped and cannot be terminated");
+         return;
+      }   
 
       // Terminate grid analysis
       if (fSelector && fSelector->GetStatus() == -1) return;
@@ -1191,6 +1219,11 @@ TFile *AliAnalysisManager::OpenProofFile(const char *filename, const char *optio
    if (fMode!=kProofAnalysis || !fSelector) {
       Error("OpenProofFile","Cannot open PROOF file %s",filename);
       return NULL;
+   } 
+   if (fSpecialOutputLocation.Length()) {
+      TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
+      if (!f) f = new TFile(filename, option);
+      return f;
    }   
    sprintf(line, "TProofOutputFile *pf = new TProofOutputFile(\"%s\");", filename);
    if (fDebug > 1) printf("=== %s\n", line);
@@ -1225,7 +1258,8 @@ void AliAnalysisManager::ExecAnalysis(Option_t *option)
       TIter next(fTasks);
    // De-activate all tasks
       while ((task=(AliAnalysisTask*)next())) task->SetActive(kFALSE);
-      AliAnalysisDataContainer *cont = (AliAnalysisDataContainer*)fInputs->At(0);
+      AliAnalysisDataContainer *cont = fCommonInput;
+      if (!cont) cont = (AliAnalysisDataContainer*)fInputs->At(0);
       if (!cont) {
              Error("ExecAnalysis","Cannot execute analysis in TSelector mode without at least one top container");
          return;
@@ -1285,3 +1319,86 @@ void AliAnalysisManager::FinishAnalysis()
 {
 // Finish analysis.
 }
+
+//______________________________________________________________________________
+void AliAnalysisManager::SetInputEventHandler(AliVEventHandler*  handler)
+{
+// Set the input event handler and create a container for it.
+   fInputEventHandler   = handler;
+   fCommonInput = CreateContainer("cAUTO_INPUT", TChain::Class(), AliAnalysisManager::kInputContainer);
+   Warning("SetInputEventHandler", " An automatic input container for the input chain was created.\nPlease use: mgr->GetCommonInputContainer() to access it.");
+}
+
+//______________________________________________________________________________
+void AliAnalysisManager::SetOutputEventHandler(AliVEventHandler*  handler)
+{
+// Set the input event handler and create a container for it.
+   fOutputEventHandler   = handler;
+   fCommonOutput = CreateContainer("cAUTO_OUTPUT", TTree::Class(), AliAnalysisManager::kOutputContainer, "default");
+   fCommonOutput->SetSpecialOutput();
+   Warning("SetOutputEventHandler", " An automatic output container for the output tree was created.\nPlease use: mgr->GetCommonOutputContainer() to access it.");
+}
+
+//______________________________________________________________________________
+void AliAnalysisManager::RegisterExtraFile(const char *fname)
+{
+// This method is used externally to register output files which are not
+// connected to any output container, so that the manager can properly register,
+// retrieve or merge them when running in distributed mode. The file names are
+// separated by blancs. The method has to be called in MyAnalysisTask::LocalInit().
+   if (fExtraFiles.Length()) fExtraFiles += " ";
+   fExtraFiles += fname;
+}
+
+//______________________________________________________________________________
+Bool_t AliAnalysisManager::GetFileFromWrapper(const char *filename, TList *source)
+{
+// Copy a file from the location specified ina the wrapper with the same name from the source list.
+   char full_path[512];
+   char ch_url[512];
+   TObject *pof =  source->FindObject(filename);
+   if (!pof || !pof->InheritsFrom("TProofOutputFile")) {
+      Error("GetFileFromWrapper", "TProofOutputFile object not found in output list for file %s", filename);
+      return kFALSE;
+   }
+   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);
+      TObjArray* arrayPort = strobj->GetString().Tokenize ( ":" );
+      TObjString *strobjPort = ( TObjString *) arrayPort->At(1);
+      full_path_str.ReplaceAll(strobj->GetString().Data(),"localhost:PORT");
+      full_path_str.ReplaceAll(":PORT",Form(":%s",strobjPort->GetString().Data()));
+      if (fDebug > 1) Info("GetFileFromWrapper","Using tunnel from %s to %s",full_path_str.Data(),filename);
+      delete arrayPort;
+      delete array;
+   }
+   if (fDebug > 1) 
+      Info("GetFileFromWrapper","Copying file %s from PROOF scratch space", full_path_str.Data());
+   Bool_t gotit = TFile::Cp(full_path_str.Data(), filename); 
+   if (!gotit)
+      Error("GetFileFromWrapper", "Could not get file %s from proof scratch space", filename);
+   return gotit;
+}
+
+//______________________________________________________________________________
+void AliAnalysisManager::GetAnalysisTypeString(TString &type) const
+{
+// Fill analysis type in the provided string.
+   switch (fMode) {
+      case kLocalAnalysis:
+         type = "local";
+         return;
+      case kProofAnalysis:
+         type = "proof";
+         return;
+      case kGridAnalysis:
+         type = "grid";
+         return;
+      case kMixingAnalysis:
+         type = "mix";
+   }
+}