]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ANALYSIS/AliAnalysisTask.cxx
Fixed includes and scope of a string
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTask.cxx
index 2f8db1be3d143443bf47f4b9462f19b74363848c..09c922c0c79463fba43096efd569e1ec996c4276 100644 (file)
 //==============================================================================
 
 #include <Riostream.h>
-#include <TDirectory.h>
+#include <TFile.h>
 #include <TClass.h>
+#include <TCollection.h>
+#include <TTree.h>
+#include <TROOT.h>
 
 #include "AliAnalysisTask.h"
 #include "AliAnalysisDataSlot.h"
 #include "AliAnalysisDataContainer.h"
+#include "AliAnalysisManager.h"
 
 ClassImp(AliAnalysisTask)
 
@@ -116,7 +120,8 @@ AliAnalysisTask::AliAnalysisTask()
                  fOutputReady(NULL),
                  fPublishedData(NULL),
                  fInputs(NULL),
-                 fOutputs(NULL)
+                 fOutputs(NULL),
+                 fBranchNames()
 {
 // Default constructor.
 }
@@ -131,7 +136,8 @@ AliAnalysisTask::AliAnalysisTask(const char *name, const char *title)
                  fOutputReady(NULL),
                  fPublishedData(NULL),
                  fInputs(NULL),
-                 fOutputs(NULL)                 
+                 fOutputs(NULL),
+                 fBranchNames()                 
 {
 // Constructor.
    fInputs      = new TObjArray(2);
@@ -148,7 +154,8 @@ AliAnalysisTask::AliAnalysisTask(const AliAnalysisTask &task)
                  fOutputReady(NULL),
                  fPublishedData(NULL),
                  fInputs(NULL),
-                 fOutputs(NULL)                 
+                 fOutputs(NULL),
+                 fBranchNames(task.fBranchNames)
 {
 // Copy ctor.
    fInputs      = new TObjArray((fNinputs)?fNinputs:2);
@@ -192,6 +199,7 @@ AliAnalysisTask& AliAnalysisTask::operator=(const AliAnalysisTask& task)
       fOutputReady[i] = IsOutputReady(i);
       fOutputs->AddAt(new AliAnalysisDataSlot(*task.GetOutputSlot(i)),i);
    }         
+   fBranchNames = task.fBranchNames;
    return *this;
 }
 
@@ -230,8 +238,11 @@ void AliAnalysisTask::CheckNotify(Bool_t init)
 // accordingly. This method is called automatically for all tasks connected
 // to a container where the data was published.
    if (init) fInitialized = kFALSE;
+   Bool_t single_shot = IsPostEventLoop();
+   AliAnalysisDataContainer *cinput;
    for (Int_t islot=0; islot<fNinputs; islot++) {
-      if (!GetInputData(islot)) {
+      cinput = GetInputSlot(islot)->GetContainer();
+      if (!cinput->GetData() || (single_shot && !cinput->IsPostEventLoop())) {
          SetActive(kFALSE);
          return;
       }   
@@ -244,6 +255,47 @@ void AliAnalysisTask::CheckNotify(Bool_t init)
    fInitialized = kTRUE;
 }
 
+//______________________________________________________________________________
+Bool_t AliAnalysisTask::CheckPostData() const
+{
+// Checks if data was posted to all outputs defined by the task. If task does
+// not have output slots this returns always kTRUE.
+   Bool_t dataPosted = kTRUE;
+   AliAnalysisDataContainer *coutput;
+   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
+   for (Int_t islot=0; islot<fNoutputs; islot++) {
+      coutput = GetOutputSlot(islot)->GetContainer();
+      if (!mgr->GetOutputs()->FindObject(coutput) || coutput==mgr->GetCommonOutputContainer()) continue;
+      if (!coutput->GetData()) {
+         Error("CheckPostData", "Data not posted for slot #%d of task %s (%s)", 
+               islot, GetName(), ClassName());
+         dataPosted = kFALSE;
+      }   
+   }
+   CheckOwnership();
+   return dataPosted;
+}
+
+//______________________________________________________________________________
+Bool_t AliAnalysisTask::CheckOwnership() const
+{
+// Check ownership of containers posted on output slots (1 level only)
+   TObject *outdata;
+   for (Int_t islot=0; islot<fNoutputs; islot++) {
+      outdata = GetOutputData(islot);
+      if (outdata && outdata->InheritsFrom(TCollection::Class())) {
+         TCollection *coll = (TCollection*)outdata;
+         if (!coll->IsOwner()) {
+            Error("CheckOwnership","####### IMPORTANT! ####### \n\n\n\
+                Task %s (%s) posts a container that is not owner at output #%d. This may apply for other embedded containers. \n\n\
+                ####### FIX YOUR CODE, THIS WILL PRODUCE A FATAL ERROR IN FUTURE! ##########", GetName(), ClassName(), islot);
+             return kFALSE;   
+         }
+      }
+   }
+   return kTRUE;
+}
+      
 //______________________________________________________________________________
 Bool_t AliAnalysisTask::ConnectInput(Int_t islot, AliAnalysisDataContainer *cont)
 {
@@ -282,6 +334,8 @@ Bool_t AliAnalysisTask::ConnectOutput(Int_t islot, AliAnalysisDataContainer *con
    }            
    // Connect the slot to the container as output         
    if (!output->ConnectContainer(cont)) return kFALSE;
+   // Set event loop type the same as for the task
+   cont->SetPostEventLoop(IsPostEventLoop());
    // Declare this as the data producer
    cont->SetProducer(this, islot);
    AreSlotsConnected();
@@ -377,6 +431,34 @@ Bool_t AliAnalysisTask::SetBranchAddress(Int_t islot, const char *branch, void *
    return GetInputSlot(islot)->SetBranchAddress(branch, address);
 }   
 
+//______________________________________________________________________________
+void AliAnalysisTask::EnableBranch(Int_t islot, const char *bname) const
+{
+// Call this in ConnectInputData() to enable only the branches needed by this 
+// task. "*" will enable everything.
+   AliAnalysisDataSlot *input = GetInputSlot(islot);
+   if (!input || !input->GetType()->InheritsFrom(TTree::Class())) {
+      Error("EnableBranch", "Wrong slot type #%d for task %s: not TTree-derived type", islot, GetName());
+      return;
+   }   
+   TTree *tree = (TTree*)input->GetData();
+   if (!strcmp(bname, "*")) {
+      tree->SetBranchStatus("*",1);
+      return;
+   }
+   AliAnalysisDataSlot::EnableBranch(bname, tree);
+}
+
+//______________________________________________________________________________
+void AliAnalysisTask::FinishTaskOutput()
+{
+// 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.
+}
+      
 //______________________________________________________________________________
 void AliAnalysisTask::ConnectInputData(Option_t *)
 {
@@ -400,6 +482,38 @@ void AliAnalysisTask::CreateOutputObjects()
 // task initialization and/or create your output objects here.
 }
 
+//______________________________________________________________________________
+TFile *AliAnalysisTask::OpenFile(Int_t iout, Option_t *option) const
+{
+// This method has to be called INSIDE the user redefined CreateOutputObjects
+// method, before creating each object corresponding to the output containers
+// that are to be written to a file. This need to be done in general for the big output
+// objects that may not fit memory during processing. 
+// - 'option' is the file opening option.
+//=========================================================================
+// NOTE !: The method call will be ignored in PROOF mode, in which case the 
+// results have to be streamed back to the client and written just before Terminate()
+//=========================================================================
+//
+// Example:
+// void MyAnaTask::CreateOutputObjects() {
+//    OpenFile(0);   // Will open the file for the object to be written at output #0
+//    fAOD = new TTree("AOD for D0toKPi");
+//    OpenFile(1);
+// now some histos that should go in the file of the second output container
+//    fHist1 = new TH1F("my quality check hist1",...);
+//    fHist2 = new TH2F("my quality check hist2",...);
+// }
+   
+   if (iout<0 || iout>=fNoutputs) {
+      Error("OpenFile", "No output slot for task %s with index %d", GetName(), iout);
+      return NULL;
+   }   
+   // Method delegated to the analysis manager (A.G. 02/11/09)
+   AliAnalysisDataContainer *cont = GetOutputSlot(iout)->GetContainer();
+   return AliAnalysisManager::OpenFile(cont, option);
+}
+
 //______________________________________________________________________________
 Bool_t AliAnalysisTask::Notify()
 {
@@ -407,6 +521,13 @@ Bool_t AliAnalysisTask::Notify()
    return kTRUE;
 }
 
+//______________________________________________________________________________
+Bool_t AliAnalysisTask::NotifyBinChange()
+{
+// Overload this IF you need to treat bin change in event mixing.
+   return kTRUE;
+}
+
 //______________________________________________________________________________
 void AliAnalysisTask::Terminate(Option_t *)
 {
@@ -475,7 +596,6 @@ Bool_t AliAnalysisTask::CheckCircularDeps()
 void AliAnalysisTask::PrintTask(Option_t *option, Int_t indent) const
 {
 // Print task info.
-   AliAnalysisTask *thistask = (AliAnalysisTask*)this;
    TString opt(option);
    opt.ToLower();
    Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
@@ -484,8 +604,9 @@ void AliAnalysisTask::PrintTask(Option_t *option, Int_t indent) const
    AliAnalysisDataContainer *cont;
    for (Int_t i=0; i<indent; i++) ind += " ";
    if (!dep || (dep && IsChecked())) {
-      printf("%s\n", Form("%stask: %s  ACTIVE=%i", ind.Data(), GetName(),IsActive()));
-      if (dep) thistask->SetChecked(kFALSE);
+      printf("______________________________________________________________________________\n");
+      printf("%s\n", Form("%stask: %s  ACTIVE=%i POST_LOOP=%i", ind.Data(), GetName(),IsActive(),IsPostEventLoop()));
+      if (dep) const_cast<AliAnalysisTask*>(this)->SetChecked(kFALSE);
       else {
          for (islot=0; islot<fNinputs; islot++) {
             printf("%s", Form("%s   INPUT #%i: %s <- ",ind.Data(),islot, GetInputType(islot)->GetName()));
@@ -502,6 +623,7 @@ void AliAnalysisTask::PrintTask(Option_t *option, Int_t indent) const
       }
    }
    PrintContainers(option, indent+3);
+   if (!fBranchNames.IsNull()) printf("Requested branches:   %s\n", fBranchNames.Data());
 }      
 
 //______________________________________________________________________________
@@ -514,6 +636,36 @@ void AliAnalysisTask::PrintContainers(Option_t *option, Int_t indent) const
    Int_t islot;
    for (islot=0; islot<fNoutputs; islot++) {
       cont = GetOutputSlot(islot)->GetContainer();
-      cont->PrintContainer(option, indent);
+      if (cont) cont->PrintContainer(option, indent);
    }   
 }
+
+//______________________________________________________________________________
+void AliAnalysisTask::SetPostEventLoop(Bool_t flag)
+{
+// Set the task execution mode - run after event loop or not. All output
+// containers of this task will get the same type.
+   TObject::SetBit(kTaskPostEventLoop,flag);
+   AliAnalysisDataContainer *cont;
+   Int_t islot;
+   for (islot=0; islot<fNoutputs; islot++) {
+      cont = GetOutputSlot(islot)->GetContainer();
+      if (cont) cont->SetPostEventLoop(flag);
+   }   
+}
+   
+//______________________________________________________________________________
+void AliAnalysisTask::GetBranches(const char *type, TString &result) const
+{
+// Get the list of branches for a given type (ESD, AOD). The list of branches
+// requested by a task has to ve declared in the form:
+//   SetBranches("ESD:branch1,branch2,...,branchN AOD:branch1,branch2,...,branchM")
+   result = "";
+   if (fBranchNames.IsNull()) return;
+   Int_t index1 = fBranchNames.Index(type);
+   if (index1<0) return;
+   index1 += 1+strlen(type);
+   Int_t index2 = fBranchNames.Index(" ", index1);
+   if (index2<0) index2 = fBranchNames.Length();
+   result = fBranchNames(index1, index2-index1);
+}