]> git.uio.no Git - u/mrichter/AliRoot.git/commitdiff
(A.G.) Revision of analysis classes containing following changes:
authoragheata <agheata@f7af4fe6-9843-0410-8265-dc069ae4e863>
Tue, 20 May 2008 12:02:14 +0000 (12:02 +0000)
committeragheata <agheata@f7af4fe6-9843-0410-8265-dc069ae4e863>
Tue, 20 May 2008 12:02:14 +0000 (12:02 +0000)
   - Removed misleading virtual method AliAnalysisTask::Cleanup()
   - Introduced optional method AliAnalysisTask::FinishTaskOutput() that will be executed on worker nodes in SlaveTerminate() phase, after processing of last event but before the outputs are wrote to files/sent for merging. Output objects should NOT be cleaned-up in this method, but in the task destructor.
   - Output histograms in memory will not be deleted anymore when closing the output file - user have their ownership.
   - AliAnalysisTask::Terminate() called now before the outputs are written to files
   - Experimental feature for CAF: AliAnalysisDataContainer::SetSpecialOutput() can be called to force merging in PROOF via files. In this case calling OpenFile(out_slot) is mandatory in MyTask::CreateOutputObjects()
   - Refined debug level in the framework: level 0 - no verbosity, level 1 - calling sequence, level 2 - more info

ANALYSIS/AliAnalysisDataContainer.cxx
ANALYSIS/AliAnalysisDataContainer.h
ANALYSIS/AliAnalysisManager.cxx
ANALYSIS/AliAnalysisManager.h
ANALYSIS/AliAnalysisSelector.cxx
ANALYSIS/AliAnalysisTask.cxx
ANALYSIS/AliAnalysisTask.h

index b1afadcaba7174243966a537e281856a68ed8524..044514e5e45a0eab52883797b46ce88d6c3f1cf3 100644 (file)
 #include <TMethodCall.h>
 
 #include <TClass.h>
+#include <TFile.h>
 #include <TTree.h>
 #include <TH1.h>
 #include <TROOT.h>
 
+#include "AliAnalysisManager.h"
 #include "AliAnalysisDataContainer.h"
 #include "AliAnalysisDataSlot.h"
 #include "AliAnalysisTask.h"
@@ -61,6 +63,7 @@ AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
                           fDataReady(kFALSE),
                           fOwnedData(kFALSE),
                           fFileName(),
+                          fFile(NULL),
                           fData(NULL),
                           fType(NULL),
                           fProducer(NULL),
@@ -75,6 +78,7 @@ AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *typ
                           fDataReady(kFALSE),
                           fOwnedData(kTRUE),
                           fFileName(),
+                          fFile(NULL),
                           fData(NULL),
                           fType(type),
                           fProducer(NULL),
@@ -90,6 +94,7 @@ AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContaine
                           fDataReady(cont.fDataReady),
                           fOwnedData(kFALSE),
                           fFileName(cont.fFileName),
+                          fFile(NULL),
                           fData(cont.fData),
                           fType(NULL),
                           fProducer(cont.fProducer),
@@ -121,6 +126,7 @@ AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisD
       fDataReady = cont.fDataReady;
       fOwnedData = kFALSE;  // !!! Data owned by cont.
       fFileName = cont.fFileName;
+      fFile = NULL;
       fData = cont.fData;
       GetType();
       fProducer = cont.fProducer;
@@ -361,7 +367,12 @@ AliAnalysisDataWrapper *AliAnalysisDataContainer::ExportData() const
 {
 // Wraps data for sending it through the net.
    AliAnalysisDataWrapper *pack = 0;
-   if (!fData) return pack;
+   if (!fData) {
+      Error("ExportData", "Container %s - No data to be wrapped !", GetName());
+      return pack;
+   } 
+   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
+   if (mgr->GetDebugLevel() > 1) printf("   ExportData: Wrapping data %s for container %s\n", fData->GetName(),GetName());
    pack = new AliAnalysisDataWrapper(fData);
    pack->SetName(fName.Data());
    return pack;
@@ -373,6 +384,12 @@ void AliAnalysisDataContainer::ImportData(AliAnalysisDataWrapper *pack)
 // Unwraps data from a data wrapper.
    if (pack) {
       fData = pack->Data();
+      if (!fData) {
+         Error("ImportData", "No data was wrapped for container %s", GetName());
+         return;
+      }   
+      AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
+      if (mgr->GetDebugLevel() > 1) printf("   ImportData: Unwrapping data %s for container %s\n", fData->GetName(),GetName());
       fDataReady = kTRUE;
       // Imported wrappers do not own data anymore (AG 13-11-07)
       pack->SetDeleteData(kFALSE);
index 84d7d163d20c642682a3f7f95aec05c518c68726..997b82d8ba01c3ae928c20590dfa4db252f5155e 100644 (file)
 #endif
 
 class TClass;
+class TFile;
 class TObjArray;
 class TCollection;
 class AliAnalysisTask;
 class AliAnalysisDataWrapper;
-class AliESD;
 
 class AliAnalysisDataContainer : public TNamed {
 
@@ -52,6 +52,7 @@ enum EAnalysisContainerFlags {
    // Getters
    TObject                  *GetData() const      {return fData;}
    const char               *GetFileName() const  {return fFileName.Data();}
+   TFile                    *GetFile() const      {return fFile;}
    TClass                   *GetType() const;
    AliAnalysisTask          *GetProducer() const  {return fProducer;}
    TObjArray                *GetConsumers() const {return fConsumers;}
@@ -63,6 +64,7 @@ enum EAnalysisContainerFlags {
    void                      SetPostEventLoop(Bool_t flag=kTRUE) {TObject::SetBit(kPostEventLoop,flag);}
    void                      SetSpecialOutput(Bool_t flag=kTRUE) {TObject::SetBit(kSpecialOutput,flag);}
    void                      SetFileName(const char *filename) {fFileName = filename;}
+   void                      SetFile(TFile *f) {fFile = f;}
    void                      SetProducer(AliAnalysisTask *prod, Int_t islot);
    void                      AddConsumer(AliAnalysisTask *cons, Int_t islot);
    void                      DeleteData();
@@ -91,6 +93,7 @@ protected:
    Bool_t                    fDataReady;  // Flag that data is ready
    Bool_t                    fOwnedData;  // Flag data ownership
    TString                   fFileName;   // File storing the data
+   TFile                    *fFile;       //! Opened file
    TObject                  *fData;       // Contained data
    TClass                   *fType;       //! Type of contained data
    AliAnalysisTask          *fProducer;   // Analysis task to which the slot belongs
index f7608f12c901fbd601be35493f9e1ea009578a5d..764eae8e355b00c06487899039c166ca91564cd8 100644 (file)
@@ -35,6 +35,7 @@
 #include <TROOT.h>
 #include <TCanvas.h>
 
+#include "AliAnalysisSelector.h"
 #include "AliAnalysisTask.h"
 #include "AliAnalysisDataContainer.h"
 #include "AliAnalysisDataSlot.h"
@@ -64,7 +65,8 @@ AliAnalysisManager::AliAnalysisManager(const char *name, const char *title)
                     fZombies(NULL),
                     fContainers(NULL),
                     fInputs(NULL),
-                    fOutputs(NULL)
+                    fOutputs(NULL),
+                    fSelector(NULL)
 {
 // Default constructor.
    fgAnalysisManager = this;
@@ -95,7 +97,8 @@ AliAnalysisManager::AliAnalysisManager(const AliAnalysisManager& other)
                     fZombies(NULL),
                     fContainers(NULL),
                     fInputs(NULL),
-                    fOutputs(NULL)
+                    fOutputs(NULL),
+                    fSelector(NULL)
 {
 // Copy constructor.
    fTasks      = new TObjArray(*other.fTasks);
@@ -128,6 +131,7 @@ AliAnalysisManager& AliAnalysisManager::operator=(const AliAnalysisManager& othe
       fContainers = new TObjArray(*other.fContainers);
       fInputs     = new TObjArray(*other.fInputs);
       fOutputs    = new TObjArray(*other.fOutputs);
+      fSelector   = NULL;
       fgAnalysisManager = this;
    }
    return *this;
@@ -150,9 +154,7 @@ AliAnalysisManager::~AliAnalysisManager()
 Int_t AliAnalysisManager::GetEntry(Long64_t entry, Int_t getall)
 {
 // Read one entry of the tree or a whole branch.
-   if (fDebug > 1) {
-      cout << "== AliAnalysisManager::GetEntry()" << endl;
-   }   
+   if (fDebug > 0) printf("== AliAnalysisManager::GetEntry(%lld)\n", entry);
    fCurrentEntry = entry;
    return fTree ? fTree->GetTree()->GetEntry(entry, getall) : 0;
 }
@@ -166,8 +168,8 @@ void AliAnalysisManager::Init(TTree *tree)
   // generated code, but the routine can be extended by the user if needed.
   // Init() will be called many times when running with PROOF.
    if (!tree) return;
-   if (fDebug > 1) {
-      printf("->AliAnalysisManager::InitTree(%s)\n", tree->GetName());
+   if (fDebug > 0) {
+      printf("->AliAnalysisManager::Init(%s)\n", tree->GetName());
    }
 
    // Call InitTree of EventHandler
@@ -205,11 +207,11 @@ void AliAnalysisManager::Init(TTree *tree)
    fTree = tree;
    AliAnalysisDataContainer *top = (AliAnalysisDataContainer*)fInputs->At(0);
    if (!top) {
-      cout<<"Error: No top input container !" <<endl;
+      Error("Init","No top input container !");
       return;
    }
    top->SetData(tree);
-   if (fDebug > 1) {
+   if (fDebug > 0) {
       printf("<-AliAnalysisManager::Init(%s)\n", tree->GetName());
    }
 }
@@ -220,9 +222,7 @@ void AliAnalysisManager::SlaveBegin(TTree *tree)
   // The SlaveBegin() function is called after the Begin() function.
   // When running with PROOF SlaveBegin() is called on each slave server.
   // The tree argument is deprecated (on PROOF 0 is passed).
-   if (fDebug > 1) {
-      cout << "->AliAnalysisManager::SlaveBegin()" << endl;
-   }
+   if (fDebug > 0) printf("->AliAnalysisManager::SlaveBegin()\n");
 
    // Call Init of EventHandler
    if (fOutputEventHandler) {
@@ -259,9 +259,7 @@ void AliAnalysisManager::SlaveBegin(TTree *tree)
       if (curdir) curdir->cd();
    }
 
-   if (fDebug > 1) {
-      cout << "<-AliAnalysisManager::SlaveBegin()" << endl;
-   }
+   if (fDebug > 0) printf("<-AliAnalysisManager::SlaveBegin()\n");
 }
 
 //______________________________________________________________________________
@@ -272,30 +270,37 @@ Bool_t AliAnalysisManager::Notify()
    // is started when using PROOF. It is normaly not necessary to make changes
    // to the generated code, but the routine can be extended by the
    // user if needed. The return value is currently not used.
-    if (fTree) {
-       TFile *curfile = fTree->GetCurrentFile();
-       if (curfile && fDebug>1) printf("AliAnalysisManager::Notify() file: %s\n", curfile->GetName());
-       TIter next(fTasks);
-       AliAnalysisTask *task;
-       // Call Notify for all tasks
-       while ((task=(AliAnalysisTask*)next())) 
-           task->Notify();
+   if (!fTree) {
+      Error("Notify","No current tree.");
+      return kFALSE;
+   }   
+   TFile *curfile = fTree->GetCurrentFile();
+   if (!curfile) {
+      Error("Notify","No current file");
+      return kFALSE;
+   }   
+   
+   if (fDebug > 0) printf("->AliAnalysisManager::Notify() file: %s\n", curfile->GetName());
+   TIter next(fTasks);
+   AliAnalysisTask *task;
+   // Call Notify for all tasks
+   while ((task=(AliAnalysisTask*)next())) 
+      task->Notify();
        
-       // Call Notify of the event handlers
-       if (fInputEventHandler) {
-           fInputEventHandler->Notify(curfile->GetName());
-       }
-
-       if (fOutputEventHandler) {
-           fOutputEventHandler->Notify(curfile->GetName());
-       }
+   // Call Notify of the event handlers
+   if (fInputEventHandler) {
+       fInputEventHandler->Notify(curfile->GetName());
+   }
 
-       if (fMCtruthEventHandler) {
-           fMCtruthEventHandler->Notify(curfile->GetName());
-       }
+   if (fOutputEventHandler) {
+       fOutputEventHandler->Notify(curfile->GetName());
+   }
 
-    }
-    return kTRUE;
+   if (fMCtruthEventHandler) {
+       fMCtruthEventHandler->Notify(curfile->GetName());
+   }
+   if (fDebug > 0) printf("<-AliAnalysisManager::Notify()\n");
+   return kTRUE;
 }    
 
 //______________________________________________________________________________
@@ -318,18 +323,15 @@ Bool_t AliAnalysisManager::Process(Long64_t entry)
   //  The entry is always the local entry number in the current tree.
   //  Assuming that fChain is the pointer to the TChain being processed,
   //  use fChain->GetTree()->GetEntry(entry).
-   if (fDebug > 1) {
-      cout << "->AliAnalysisManager::Process(" << entry << ")" << endl;
-   }
+   if (fDebug > 0) printf("->AliAnalysisManager::Process(%lld)\n", entry);
+
    if (fInputEventHandler)   fInputEventHandler  ->BeginEvent(entry);
    if (fOutputEventHandler)  fOutputEventHandler ->BeginEvent(entry);
    if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(entry);
    
    GetEntry(entry);
    ExecAnalysis();
-   if (fDebug > 1) {
-      cout << "<-AliAnalysisManager::Process()" << endl;
-   }
+   if (fDebug > 0) printf("<-AliAnalysisManager::Process()\n");
    return kTRUE;
 }
 
@@ -338,9 +340,7 @@ void AliAnalysisManager::PackOutput(TList *target)
 {
   // Pack all output data containers in the output list. Called at SlaveTerminate
   // stage in PROOF case for each slave.
-   if (fDebug > 1) {
-      cout << "->AliAnalysisManager::PackOutput()" << endl;
-   }   
+   if (fDebug > 0) printf("->AliAnalysisManager::PackOutput()\n");
    if (!target) {
       Error("PackOutput", "No target. Aborting.");
       return;
@@ -348,55 +348,93 @@ void AliAnalysisManager::PackOutput(TList *target)
    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;
       while ((output=(AliAnalysisDataContainer*)next())) {
-         if (output->GetData() && !output->IsSpecialOutput()) {
-            if (output->GetProducer()->IsPostEventLoop()) continue;
-
+         // Do not consider outputs of post event loop tasks
+         if (output->GetProducer()->IsPostEventLoop()) continue;
+         // Check if data was posted to this container. If not, issue an error.
+         if (!output->GetData() ) {
+            Error("PackOutput", "No data for output container %s. Forgot to PostData ?\n", output->GetName());
+            continue;
+         }   
+         if (!output->IsSpecialOutput()) {
+            // Normal outputs
             const char *filename = output->GetFileName();
             if (!(strcmp(filename, "default"))) {
                if (fOutputEventHandler) filename = fOutputEventHandler->GetOutputFileName();
             }      
             if (strlen(filename)) {
-               TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
+            // 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()) continue;
+               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.
-               // Save data to file, then close.
                file->Clear();
+               // Save data to file, then close.
                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();
-               // Set null directory to histograms and trees.
-               TMethodCall callEnv;
-               if (output->GetData()->IsA())
-                  callEnv.InitWithPrototype(output->GetData()->IsA(), "SetDirectory", "TDirectory*");
-               if (callEnv.IsValid()) {
-                  callEnv.SetParam(Long_t(0));
-                  callEnv.Execute(output->GetData());
-               }
                // Restore current directory
                if (opwd) opwd->cd();
+            } else {
+               // Memory-resident outputs   
+               if (fDebug > 1) printf("PackOutput %s: memory merge memory resident output\n", output->GetName());
             }   
             AliAnalysisDataWrapper *wrap = output->ExportData();
             // Output wrappers must delete data after merging (AG 13/11/07)
             wrap->SetDeleteData(kTRUE);
-            if (fDebug > 1) printf("   Packing container %s...\n", output->GetName());
             target->Add(wrap);
          }   
-         // Special outputs files are closed and copied on the remote location
-         if (output->IsSpecialOutput() && strlen(output->GetFileName())) {
+         // Special outputs
+         if (output->IsSpecialOutput()) {
             TDirectory *opwd = gDirectory;
-            TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(output->GetFileName());
-            if (!file) continue;
+            TFile *file = output->GetFile();
+            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()) 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 += "/";
@@ -405,67 +443,68 @@ void AliAnalysisManager::PackOutput(TList *target)
                remote += output->GetFileName();
                TFile::Cp(output->GetFileName(), remote.Data());
             } else {
-            // No special location specified-> use TProofFile as merging utility
-               char line[256];
-               sprintf(line, "((TList*)0x%lx)->Add(new TProofFile(\"%s\"));",
-                       (ULong_t)target, output->GetFileName());
-               gROOT->ProcessLine(line);
+            // 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());
             }
          }      
       }
-      // Cleanup tasks on each slave
-      TIter nexttask(fTasks);
-      AliAnalysisTask *task;
-      while ((task=(AliAnalysisTask*)nexttask())) task->Cleanup();
    } 
-   if (fDebug > 1) {
-      printf("<-AliAnalysisManager::PackOutput: output list contains %d containers\n", target->GetSize());
-   }
+   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 > 1) {
-      cout << "->AliAnalysisManager::ImportWrappers()" << endl;
-   }   
+   if (fDebug > 0) printf("->AliAnalysisManager::ImportWrappers()\n");
    TIter next(fOutputs);
    AliAnalysisDataContainer *cont;
    AliAnalysisDataWrapper   *wrap;
    Int_t icont = 0;
    while ((cont=(AliAnalysisDataContainer*)next())) {
-      if (cont->GetProducer()->IsPostEventLoop() ||
-          cont->IsSpecialOutput()) continue;
+      if (cont->GetProducer()->IsPostEventLoop()) continue;
+      if (cont->IsSpecialOutput()) {
+         if (strlen(fSpecialOutputLocation.Data())) continue;
+         // Copy merged file from PROOF scratch space
+         if (fDebug > 1) 
+            printf("   Copying file %s from PROOF scratch space\n", cont->GetFileName());
+         Bool_t gotit = TFile::Cp(Form("root://lxb6045.cern.ch:11094//pool/scratch/%s",cont->GetFileName()),
+                   cont->GetFileName()); 
+         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...
+         continue;
+      }   
       wrap = (AliAnalysisDataWrapper*)source->FindObject(cont->GetName());
-      if (!wrap && fDebug>1) {
-         printf("(WW) ImportWrappers: container %s not found in analysis output !\n", 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\n", wrap->GetName());
-      if (cont->GetFileName()) printf("    -> %s\n", cont->GetFileName());
+      if (fDebug > 1) {
+         printf("   Importing data for container %s", cont->GetName());
+         if (strlen(cont->GetFileName())) printf("    -> file %s\n", cont->GetFileName());
+         else printf("\n");
+      }   
       cont->ImportData(wrap);
    }         
-   if (fDebug > 1) {
-      cout << "<-AliAnalysisManager::ImportWrappers(): "<< icont << " containers imported" << endl;
-   }   
+   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 > 1) {
-      cout << "->AliAnalysisManager::UnpackOutput()" << endl;
-   }   
+   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 (fDebug > 1) printf("   Source list contains %d containers\n", source->GetSize());
 
    if (fMode == kProofAnalysis) ImportWrappers(source);
 
@@ -484,39 +523,13 @@ void AliAnalysisManager::UnpackOutput(TList *source)
             task->CheckNotify(kTRUE);
             // If task is active, execute it
             if (task->IsPostEventLoop() && task->IsActive()) {
-               if (fDebug > 1) {
-                  cout << "== Executing post event loop task " << task->GetName() << endl;
-               }                  
+               if (fDebug > 0) printf("== Executing post event loop task %s\n", task->GetName());
                task->ExecuteTask();
             }   
          }
       }   
-      // Check if the output need to be written to a file.
-      const char *filename = output->GetFileName();
-      if (!(strcmp(filename, "default"))) {
-         if (fOutputEventHandler) filename = fOutputEventHandler->GetOutputFileName();
-      }
-      
-      if (!filename || !strlen(filename)) continue;
-      TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
-      TDirectory *opwd = gDirectory;
-      if (file) file->cd();
-      else      file = new TFile(filename, "RECREATE");
-      if (file->IsZombie()) continue;
-      // Reparent data to this file
-      TMethodCall callEnv;
-      if (output->GetData()->IsA())
-         callEnv.InitWithPrototype(output->GetData()->IsA(), "SetDirectory", "TDirectory*");
-      if (callEnv.IsValid()) {
-         callEnv.SetParam((Long_t) file);
-         callEnv.Execute(output->GetData());
-      }
-      output->GetData()->Write();
-      if (opwd) opwd->cd();
    }
-   if (fDebug > 1) {
-      cout << "<-AliAnalysisManager::UnpackOutput()" << endl;
-   }   
+   if (fDebug > 0) printf("<-AliAnalysisManager::UnpackOutput()\n");
 }
 
 //______________________________________________________________________________
@@ -525,16 +538,11 @@ void AliAnalysisManager::Terminate()
   // The Terminate() function is the last function to be called during
   // a query. It always runs on the client, it can be used to present
   // the results graphically.
-   if (fDebug > 1) {
-      cout << "->AliAnalysisManager::Terminate()" << endl;
-   }   
+   if (fDebug > 0) printf("->AliAnalysisManager::Terminate()\n");
    AliAnalysisTask *task;
    TIter next(fTasks);
    // Call Terminate() for tasks
    while ((task=(AliAnalysisTask*)next())) task->Terminate();
-   if (fDebug > 1) {
-      cout << "<-AliAnalysisManager::Terminate()" << endl;
-   }   
    //
    if (fInputEventHandler)   fInputEventHandler  ->TerminateIO();
    if (fOutputEventHandler)  fOutputEventHandler ->TerminateIO();
@@ -542,16 +550,26 @@ void AliAnalysisManager::Terminate()
    TIter next1(fOutputs);
    AliAnalysisDataContainer *output;
    while ((output=(AliAnalysisDataContainer*)next1())) {
+      if (!output->GetData()) continue;
       // Close all files at output
       const char *filename = output->GetFileName();
       if (!(strcmp(filename, "default"))) {
          if (fOutputEventHandler) filename = fOutputEventHandler->GetOutputFileName();
-      }
-      
-      if (!filename || !strlen(filename)) continue;
-      TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
-      if (!file || file->IsZombie()) continue;
+      }      
+      if (!strlen(filename)) continue;
+      TFile *file = output->GetFile();
+      TDirectory *opwd = gDirectory;
+      if (file) {
+         file->cd();
+      } else {
+         file = new TFile(filename, "RECREATE");
+         if (file->IsZombie()) continue;
+         output->SetFile(file);
+      }   
+      if (fDebug > 1) printf("   writing output data %s to file %s\n", output->GetData()->GetName(), file->GetName());
+      output->GetData()->Write();
       file->Close();
+      if (opwd) opwd->cd();
    }   
 
    Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
@@ -580,12 +598,17 @@ void AliAnalysisManager::Terminate()
       }
       if (cdir) cdir->cd();
    }      
+   if (fDebug > 0) printf("<-AliAnalysisManager::Terminate()\n");
 }
 
 //______________________________________________________________________________
 void AliAnalysisManager::AddTask(AliAnalysisTask *task)
 {
 // Adds a user task to the global list of tasks.
+   if (fTasks->FindObject(task)) {
+      Warning("AddTask", "Task %s: the same object already added to the analysis manager. Not adding.", task->GetName());
+      return;
+   }   
    task->SetActive(kFALSE);
    fTasks->Add(task);
 }  
@@ -636,7 +659,7 @@ Bool_t AliAnalysisManager::ConnectInput(AliAnalysisTask *task, Int_t islot,
 // Connect input of an existing task to a data container.
    if (!fTasks->FindObject(task)) {
       AddTask(task);
-      Warning("ConnectInput", "Task %s not registered. Now owned by analysis manager", task->GetName());
+      Info("ConnectInput", "Task %s was not registered. Now owned by analysis manager", task->GetName());
    } 
    Bool_t connected = task->ConnectInput(islot, cont);
    return connected;
@@ -757,6 +780,14 @@ Bool_t AliAnalysisManager::InitAnalysis()
          }
       }
    }   
+   // Check if all special output containers have a file name provided
+   TIter nextout(fOutputs);
+   while ((cont=(AliAnalysisDataContainer*)nextout())) {
+      if (cont->IsSpecialOutput() && !strlen(cont->GetFileName())) {
+         Error("InitAnalysis", "Wrong container %s : a file name MUST be provided for special outputs", cont->GetName());
+         return kFALSE;
+      }
+   }      
    fInitOK = kTRUE;
    return kTRUE;
 }   
@@ -794,9 +825,7 @@ void AliAnalysisManager::StartAnalysis(const char *type, TTree *tree, Long64_t n
       Error("StartAnalysis","Analysis manager was not initialized !");
       return;
    }
-   if (fDebug>1) {
-      cout << "StartAnalysis: " << GetName() << endl;   
-   }   
+   if (fDebug > 0) printf("StartAnalysis %s\n",GetName());
    TString anaType = type;
    anaType.ToLower();
    fMode = kLocalAnalysis;
@@ -810,14 +839,8 @@ void AliAnalysisManager::StartAnalysis(const char *type, TTree *tree, Long64_t n
    }
    char line[256];
    SetEventLoop(kFALSE);
-   // Disable all branches if requested and set event loop mode
-   if (tree) {
-      if (TestBit(kDisableBranches)) {
-         printf("Disabling all branches...\n");
-//         tree->SetBranchStatus("*",0); // not yet working
-      }   
-      SetEventLoop(kTRUE);
-   }   
+   // Enable event loop mode if a tree was provided
+   if (tree) SetEventLoop(kTRUE);
 
    TChain *chain = 0;
    TString ttype = "TTree";
@@ -889,9 +912,7 @@ void AliAnalysisManager::StartAnalysis(const char *type, const char *dataset, Lo
       Error("StartAnalysis","Analysis manager was not initialized !");
       return;
    }
-   if (fDebug>1) {
-      cout << "StartAnalysis: " << GetName() << endl;   
-   }   
+   if (fDebug > 0) printf("StartAnalysis %s\n",GetName());
    TString anaType = type;
    anaType.ToLower();
    if (!anaType.Contains("proof")) {
@@ -929,6 +950,30 @@ void AliAnalysisManager::StartAnalysis(const char *type, const char *dataset, Lo
    gROOT->ProcessLine(line);
 }   
 
+//______________________________________________________________________________
+TFile *AliAnalysisManager::OpenProofFile(const char *filename, const char *option)
+{
+// Opens a special output file used in PROOF.
+   char line[256];
+   if (fMode!=kProofAnalysis || !fSelector) {
+      Error("OpenProofFile","Cannot open PROOF file %s",filename);
+      return NULL;
+   }   
+   sprintf(line, "TProofOutputFile *pf = new TProofOutputFile(\"%s\");", filename);
+   if (fDebug > 1) printf("=== %s\n", line);
+   gROOT->ProcessLine(line);
+   sprintf(line, "pf->OpenFile(\"%s\");", option);
+   gROOT->ProcessLine(line);
+   if (fDebug > 1) {
+      gROOT->ProcessLine("pf->Print()");
+      printf(" == proof file name: %s\n", gFile->GetName());
+   }   
+   sprintf(line, "((TList*)0x%lx)->Add(pf);",(ULong_t)fSelector->GetOutputList());
+   if (fDebug > 1) printf("=== %s\n", line);
+   gROOT->ProcessLine(line);
+   return gFile;
+}   
+
 //______________________________________________________________________________
 void AliAnalysisManager::ExecAnalysis(Option_t *option)
 {
index 6970cb191ab5f1e73e3d598e1cfc486d19981d0b..5cb07f9d4d69a00babf882d8ce84916beaa917a0 100644 (file)
@@ -19,6 +19,8 @@
 
 class TClass;
 class TTree;
+class TFile;
+class AliAnalysisSelector;
 class AliAnalysisDataContainer;
 class AliAnalysisTask;
 class AliVEventHandler;
@@ -62,6 +64,7 @@ enum EAliAnalysisFlags {
    virtual Bool_t      ProcessCut(Long64_t entry) {return Process(entry);}
    virtual Bool_t      Process(Long64_t entry);
    virtual Int_t       GetEntry(Long64_t entry, Int_t getall = 0);
+   TFile              *OpenProofFile(const char *name, const char *option);
    void                PackOutput(TList *target);
    void                UnpackOutput(TList *source);
    virtual void        Terminate();
@@ -91,6 +94,7 @@ enum EAliAnalysisFlags {
    void                SetOutputEventHandler(AliVEventHandler*  handler) {fOutputEventHandler  = handler;}
    void                SetMCtruthEventHandler(AliVEventHandler* handler) {fMCtruthEventHandler = handler;}
    void                SetNSysInfo(Long64_t nevents) {fNSysInfo = nevents;}
+   void                SetSelector(AliAnalysisSelector *sel) {fSelector = sel;}
    AliVEventHandler*   GetInputEventHandler()   {return fInputEventHandler;}
    AliVEventHandler*   GetOutputEventHandler()  {return fOutputEventHandler;}
    AliVEventHandler*   GetMCtruthEventHandler() {return fMCtruthEventHandler;}
@@ -142,6 +146,7 @@ private:
    TObjArray              *fContainers;          // List of all containers
    TObjArray              *fInputs;              // List of containers with input data
    TObjArray              *fOutputs;             // List of containers with results
+   AliAnalysisSelector    *fSelector;            //! Current selector
 
    static AliAnalysisManager *fgAnalysisManager; //! static pointer to object instance
    ClassDef(AliAnalysisManager,3)  // Analysis manager class
index f7da20dd04560485f9405eab8d33e6218180b185..7568c1c4afc676edf321e31fc7aa6789daa81b6c 100644 (file)
@@ -58,7 +58,7 @@ void AliAnalysisSelector::Init(TTree *tree)
       Error("Init", "Analysis manager NULL !");
       return;
    }
-   if (fAnalysis->GetDebugLevel()>1) {
+   if (fAnalysis->GetDebugLevel()>0) {
       cout << "->AliAnalysisSelector->Init()" << endl;
    }   
    if (!tree) {
@@ -67,7 +67,7 @@ void AliAnalysisSelector::Init(TTree *tree)
    }
    fAnalysis->Init(tree);
    fInitialized = kTRUE;
-   if (fAnalysis->GetDebugLevel()>1) {
+   if (fAnalysis->GetDebugLevel()>0) {
       cout << "<-AliAnalysisSelector->Init()" << endl;
    }   
 }
@@ -77,7 +77,7 @@ void AliAnalysisSelector::Begin(TTree *)
 {
 // Assembly the input list.
    RestoreAnalysisManager();
-   if (fAnalysis && fAnalysis->GetDebugLevel()>1) {
+   if (fAnalysis && fAnalysis->GetDebugLevel()>0) {
       cout << "->AliAnalysisSelector->Begin: Analysis manager restored" << endl;
    }
 }
@@ -88,11 +88,11 @@ void AliAnalysisSelector::SlaveBegin(TTree *tree)
 // Called on each worker. We "unpack" analysis manager here and call InitAnalysis.
    RestoreAnalysisManager();
    if (fAnalysis) {
-      if (fAnalysis->GetDebugLevel()>1) {
+      if (fAnalysis->GetDebugLevel()>0) {
          cout << "->AliAnalysisSelector->SlaveBegin() after Restore" << endl;
       }   
       fAnalysis->SlaveBegin(tree);   
-      if (fAnalysis->GetDebugLevel()>1) {
+      if (fAnalysis->GetDebugLevel()>0) {
          cout << "<-AliAnalysisSelector->SlaveBegin()" << endl;
       }   
    }   
@@ -114,14 +114,14 @@ Bool_t AliAnalysisSelector::Notify()
 Bool_t AliAnalysisSelector::Process(Long64_t entry)
 {
 // Event loop.
-   if (fAnalysis->GetDebugLevel() >) {
+   if (fAnalysis->GetDebugLevel() > 0) {
       cout << "->AliAnalysisSelector::Process()" << endl;
    }
    Int_t nobjCount = TProcessID::GetObjectCount();
    fAnalysis->GetEntry(entry);
    fAnalysis->ExecAnalysis();
    TProcessID::SetObjectCount(nobjCount);
-   if (fAnalysis->GetDebugLevel() >) {
+   if (fAnalysis->GetDebugLevel() > 0) {
       cout << "<-AliAnalysisSelector::Process()" << endl;
    }   
    return kTRUE;
@@ -137,7 +137,8 @@ void AliAnalysisSelector::RestoreAnalysisManager()
       while ((obj=next())) {
          if (obj->IsA() == AliAnalysisManager::Class()) {
             fAnalysis = (AliAnalysisManager*)obj;
-            if (fAnalysis->GetDebugLevel()>1) {
+            fAnalysis->SetSelector(this);
+            if (fAnalysis->GetDebugLevel()>0) {
                cout << "->AliAnalysisSelector->RestoreAnalysisManager: Analysis manager restored" << endl;
             }   
             break;
@@ -156,11 +157,11 @@ void AliAnalysisSelector::SlaveTerminate()
   // The SlaveTerminate() function is called after all entries or objects
   // have been processed. When running with PROOF SlaveTerminate() is called
   // on each slave server.
-   if (fAnalysis->GetDebugLevel() >) {
+   if (fAnalysis->GetDebugLevel() > 0) {
       cout << "->AliAnalysisSelector::SlaveTerminate()" << endl;
    }   
    fAnalysis->PackOutput(fOutput);
-   if (fAnalysis->GetDebugLevel() >) {
+   if (fAnalysis->GetDebugLevel() > 0) {
       cout << "<-AliAnalysisSelector::SlaveTerminate()" << endl;
    }   
 }  
@@ -175,12 +176,12 @@ void AliAnalysisSelector::Terminate()
       Error("Terminate","AliAnalysisSelector::Terminate: No analysis manager!!!");
       return;
    }   
-   if (fAnalysis->GetDebugLevel() >) {
+   if (fAnalysis->GetDebugLevel() > 0) {
       cout << "->AliAnalysisSelector::Terminate()" << endl;
    }   
    fAnalysis->UnpackOutput(fOutput);
    fAnalysis->Terminate();   
-   if (fAnalysis->GetDebugLevel() >) {
+   if (fAnalysis->GetDebugLevel() > 0) {
       cout << "<-AliAnalysisSelector::Terminate()" << endl;
    }   
 }
index b754d4cdec34b23367ded56011d7418b6a7625b9..362c4551c50aa362a8eada192f4746c65fe3b455 100644 (file)
@@ -403,9 +403,13 @@ void AliAnalysisTask::EnableBranch(Int_t islot, const char *bname) const
 }
 
 //______________________________________________________________________________
-void AliAnalysisTask::Cleanup()
+void AliAnalysisTask::FinishTaskOutput()
 {
-// Cleanup method that is called only in proof mode in SlaveTerminate phase.
+// Optional method that is called in SlaveTerminate phase. 
+// Used for calling aditional methods just after the last event was processed ON
+// THE WORKING NODE. The call is made also in local case.
+// Do NOT delete output objects here since they will have to be sent for 
+// merging in PROOF mode - use class destructor for cleanup.
 }
       
 //______________________________________________________________________________
@@ -459,12 +463,22 @@ TFile *AliAnalysisTask::OpenFile(Int_t iout, Option_t *option) const
       return NULL;
    }   
    // We allow file opening also on the slaves (AG)
-//   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
-//   if (!mgr || mgr->GetAnalysisType()==AliAnalysisManager::kProofAnalysis) return;
+   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
    AliAnalysisDataContainer *cont = GetOutputSlot(iout)->GetContainer();
    TFile *f = NULL;
-   if (strlen(cont->GetFileName())) f = new TFile(cont->GetFileName(), option);
-   if (f && !f->IsZombie()) return f;
+   if (!strlen(cont->GetFileName())) {
+      Error("OpenFile", "No file name specified for container %s", cont->GetName());
+      return f;
+   }   
+   if (mgr->GetAnalysisType()==AliAnalysisManager::kProofAnalysis && cont->IsSpecialOutput())
+      f = mgr->OpenProofFile(cont->GetFileName(),option);
+   else
+      f = new TFile(cont->GetFileName(), option);
+   if (f && !f->IsZombie()) {
+      cont->SetFile(f);
+      return f;
+   }
+   cont->SetFile(NULL);
    return NULL;
 }
 
index db94cbcc184de5321f097cc8380468b1692ac474..93223511740c39b00303cdeb114884b3425502e1 100644 (file)
@@ -89,8 +89,10 @@ public:
   // === OVERLOAD THIS IF YOU NEED TO TREAT INPUT FILE/TREE CHANGE
   virtual Bool_t            Notify();
   //=====================================================================
-  // Optional cleanup method to be called only in PROOF in SlaveTerminate phase
-  virtual void              Cleanup();
+  // Optional method that will be called in SlaveTerminate phase for each task
+  // Warning: in PROOF mode this is called before merging so their cleanup is
+  //          not allowed - do output cleanup in class destructor.
+  virtual void              FinishTaskOutput();
   // Conect inputs/outputs to data containers (by AliAnalysisModule)
   Bool_t                    ConnectInput(Int_t islot, AliAnalysisDataContainer *cont);
   Bool_t                    ConnectOutput(Int_t islot, AliAnalysisDataContainer *cont);