]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ANALYSIS/AliAnalysisTask.cxx
Modifying the GetChainFromCollection function based on the additions of the TEntryList
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTask.cxx
index 68cd15b927afd758ca97609210035473a830ab38..2efecdfb76055aea4669df7973e9efd0d780eff3 100644 (file)
 //                                    AliAnalysisDataContainer *cont)
 // To connect a slot to a data container, the data types declared by both must
 // match.
+//
+// The method ConnectInputData() has to be overloaded by the derived class in order to
+// set the branch address or connect to a branch address in case the input
+// slots are connected to trees.
+// Example:
+// MyAnalysisTask::ConnectInputData(Option_t *)
+// {
+//  // One should first check if the branch address was taken by some other task
+//    char ** address = (char **)GetBranchAddress(0, "ESD");
+//    if (address) {
+//      fESD = (AliESD*)(*address);
+//    } else {
+//      fESD = new AliESD();
+//      SetBranchAddress(0, "ESD", &fESD);
+//    }
+// }
+// 
+// The method CreateOutputObjects() has to be overloaded an will contain the
+// objects that should be created only once per session (e.g. output
+// histograms)
+//
+// void MyAnalysisTask::CreateOutputObjects()
+//{
+  // create histograms 
+//  fhPt = new TH1F("fhPt","This is the Pt distribution",15,0.1,3.1);
+//  fhPt->SetStats(kTRUE);
+//  fhPt->GetXaxis()->SetTitle("P_{T} [GeV]");
+//  fhPt->GetYaxis()->SetTitle("#frac{dN}{dP_{T}}");
+//  fhPt->GetXaxis()->SetTitleColor(1);
+//  fhPt->SetMarkerStyle(kFullCircle);
+// }
+//
+// The method Terminate() will be called by the framework once at the end of
+// data processing. Overload this if needed. DO NOT ASSUME that the pointers
+// to histograms defined in  CreateOutputObjects() are valid, since this is
+// not true in case of PROOF. Restore the pointer values like:
+//
+//void MyAnalysisTask::Terminate(Option_t *) 
+//{
+//  fhPt = (TH1F*)GetOutputData(0);
+// ...
+//}
+
+//
 //==============================================================================
 
-#include "TClass.h"
+#include <Riostream.h>
+#include <TDirectory.h>
+#include <TClass.h>
 
-#include "AliLog.h"
 #include "AliAnalysisTask.h"
 #include "AliAnalysisDataSlot.h"
 #include "AliAnalysisDataContainer.h"
@@ -58,39 +103,48 @@ ClassImp(AliAnalysisTask)
 
 //______________________________________________________________________________
 AliAnalysisTask::AliAnalysisTask()
+                :fReady(kFALSE),
+                 fInitialized(kFALSE),
+                 fNinputs(0),
+                 fNoutputs(0),
+                 fOutputReady(NULL),
+                 fPublishedData(NULL),
+                 fInputs(NULL),
+                 fOutputs(NULL)
 {
 // Default constructor.
-   fReady       = kFALSE;
-   fNinputs     = 0;
-   fNoutputs    = 0;
-   fOutputReady = 0;
-   fPublishedData = 0;
-   fInputs      = 0;
-   fOutputs     = 0;
 }
 
 //______________________________________________________________________________
 AliAnalysisTask::AliAnalysisTask(const char *name, const char *title)
-                :TTask(name,title)
+                :TTask(name,title),
+                 fReady(kFALSE),
+                 fInitialized(kFALSE),
+                 fNinputs(0),
+                 fNoutputs(0),
+                 fOutputReady(NULL),
+                 fPublishedData(NULL),
+                 fInputs(NULL),
+                 fOutputs(NULL)                 
 {
-// Named constructor.
-   fReady       = kFALSE;
-   fNinputs     = 0;
-   fNoutputs    = 0;
-   fOutputReady = 0;
-   fPublishedData = 0;
+// Constructor.
    fInputs      = new TObjArray(2);
    fOutputs     = new TObjArray(2);
 }
 
 //______________________________________________________________________________
 AliAnalysisTask::AliAnalysisTask(const AliAnalysisTask &task)
-                :TTask(task)
+                :TTask(task),
+                 fReady(task.fReady),
+                 fInitialized(task.fInitialized),
+                 fNinputs(task.fNinputs),
+                 fNoutputs(task.fNoutputs),                 
+                 fOutputReady(NULL),
+                 fPublishedData(NULL),
+                 fInputs(NULL),
+                 fOutputs(NULL)                 
 {
 // Copy ctor.
-   fReady       = task.IsReady();
-   fNinputs     = task.GetNinputs();
-   fNoutputs    = task.GetNoutputs();
    fInputs      = new TObjArray((fNinputs)?fNinputs:2);
    fOutputs     = new TObjArray((fNoutputs)?fNoutputs:2);
    fPublishedData = 0;
@@ -107,6 +161,7 @@ AliAnalysisTask::AliAnalysisTask(const AliAnalysisTask &task)
 AliAnalysisTask::~AliAnalysisTask()
 {
 // Dtor.
+   if (fTasks) fTasks->Clear();
    if (fInputs)  {fInputs->Delete(); delete fInputs;}
    if (fOutputs) {fOutputs->Delete(); delete fOutputs;}
 }   
@@ -115,22 +170,22 @@ AliAnalysisTask::~AliAnalysisTask()
 AliAnalysisTask& AliAnalysisTask::operator=(const AliAnalysisTask& task)
 {
 // Assignment
-   if (&task != this) {
-      TTask::operator=(task);
-      fReady       = task.IsReady();
-      fNinputs     = task.GetNinputs();
-      fNoutputs    = task.GetNoutputs();
-      fInputs      = new TObjArray((fNinputs)?fNinputs:2);
-      fOutputs     = new TObjArray((fNoutputs)?fNoutputs:2);
-      fPublishedData = 0;
-      Int_t i;
-      for (i=0; i<fNinputs; i++) fInputs->AddAt(task.GetInputSlot(i),i);
-      fOutputReady = new Bool_t[(fNoutputs)?fNoutputs:2];
-      for (i=0; i<fNoutputs; i++) {
-         fOutputReady[i] = IsOutputReady(i);
-         fOutputs->AddAt(task.GetOutputSlot(i),i);
-      }         
-   }
+   if (&task == this) return *this;
+   TTask::operator=(task);
+   fReady       = task.IsReady();
+   fInitialized = task.IsInitialized();
+   fNinputs     = task.GetNinputs();
+   fNoutputs    = task.GetNoutputs();
+   fInputs      = new TObjArray((fNinputs)?fNinputs:2);
+   fOutputs     = new TObjArray((fNoutputs)?fNoutputs:2);
+   fPublishedData = 0;
+   Int_t i;
+   for (i=0; i<fNinputs; i++) fInputs->AddAt(new AliAnalysisDataSlot(*task.GetInputSlot(i)),i);
+   fOutputReady = new Bool_t[(fNoutputs)?fNoutputs:2];
+   for (i=0; i<fNoutputs; i++) {
+      fOutputReady[i] = IsOutputReady(i);
+      fOutputs->AddAt(new AliAnalysisDataSlot(*task.GetOutputSlot(i)),i);
+   }         
    return *this;
 }
 
@@ -145,7 +200,7 @@ Bool_t AliAnalysisTask::AreSlotsConnected()
    for (i=0; i<fNinputs; i++) {
       slot = (AliAnalysisDataSlot*)fInputs->At(i);
       if (!slot) {
-         AliError(Form("Input slot %i of task %s not defined !",i,GetName()));
+             Error("AreSlotsConnected", "Input slot %d of task %s not defined !",i,GetName());
          return kFALSE;
       }   
       if (!slot->IsConnected()) return kFALSE;
@@ -153,7 +208,7 @@ Bool_t AliAnalysisTask::AreSlotsConnected()
    for (i=0; i<fNoutputs; i++) {
       slot = (AliAnalysisDataSlot*)fOutputs->At(i);
       if (!slot) {
-         AliError(Form("Output slot %i of task %s not defined !",i,GetName()));
+         Error("AreSlotsConnected", "Output slot %d of task %s not defined !",i,GetName());
          return kFALSE;
       }   
       if (!slot->IsConnected()) return kFALSE;
@@ -163,11 +218,12 @@ Bool_t AliAnalysisTask::AreSlotsConnected()
 }
 
 //______________________________________________________________________________
-void AliAnalysisTask::CheckNotify()
+void AliAnalysisTask::CheckNotify(Bool_t init)
 {
 // Check if data is available from all inputs. Change the status of the task
 // accordingly. This method is called automatically for all tasks connected
 // to a container where the data was published.
+   if (init) fInitialized = kFALSE;
    for (Int_t islot=0; islot<fNinputs; islot++) {
       if (!GetInputData(islot)) {
          SetActive(kFALSE);
@@ -175,6 +231,11 @@ void AliAnalysisTask::CheckNotify()
       }   
    }   
    SetActive(kTRUE);
+   if (fInitialized) return;
+   TDirectory *cursav = gDirectory;
+   ConnectInputData();
+   if (cursav) cursav->cd();
+   fInitialized = kTRUE;
 }
 
 //______________________________________________________________________________
@@ -183,13 +244,12 @@ Bool_t AliAnalysisTask::ConnectInput(Int_t islot, AliAnalysisDataContainer *cont
 // Connect an input slot to a data container.
    AliAnalysisDataSlot *input = GetInputSlot(islot);
    if (!input) {
-      AliError(Form("Input slot %i not defined for analysis task %s", islot, GetName()));
+      Error("ConnectInput","Input slot %i not defined for analysis task %s", islot, GetName());
       return kFALSE;
    }
    // Check type matching          
    if (!input->GetType()->InheritsFrom(cont->GetType())) {
-      AliError(Form("Data type %s for input %i of task %s not matching container %s of type %s",
-               input->GetType()->GetName(), islot, GetName(), cont->GetName(), cont->GetType()->GetName()));
+      Error("ConnectInput","Data type %s for input %i of task %s not matching container %s of type %s",input->GetType()->GetName(), islot, GetName(), cont->GetName(), cont->GetType()->GetName());
       return kFALSE;
    }  
    // Connect the slot to the container as input          
@@ -206,13 +266,12 @@ Bool_t AliAnalysisTask::ConnectOutput(Int_t islot, AliAnalysisDataContainer *con
 // Connect an output slot to a data container.
    AliAnalysisDataSlot *output = GetOutputSlot(islot);
    if (!output) {
-      AliError(Form("Output slot %i not defined for analysis task %s", islot, GetName()));
+      Error("ConnectOutput","Output slot %i not defined for analysis task %s", islot, GetName());
       return kFALSE;
    }
    // Check type matching          
    if (!output->GetType()->InheritsFrom(cont->GetType())) {
-      AliError(Form("Data type %s for output %i of task %s not matching container %s of type %s",
-               output->GetType()->GetName(), islot, GetName(), cont->GetName(), cont->GetType()->GetName()));
+      Error("ConnectOutput","Data type %s for output %i of task %s not matching container %s of type %s",output->GetType()->GetName(), islot, GetName(), cont->GetName(), cont->GetType()->GetName());
       return kFALSE;
    }            
    // Connect the slot to the container as output         
@@ -229,17 +288,13 @@ void AliAnalysisTask::DefineInput(Int_t islot, TClass *type)
 // Define an input slot and its type.
    AliAnalysisDataSlot *input = new AliAnalysisDataSlot(type, this);
    if (fNinputs<islot+1) fNinputs = islot+1;
-   fInputs->AddAt(input, islot);
+   fInputs->AddAtAndExpand(input, islot);
 }
 
 //______________________________________________________________________________
 void AliAnalysisTask::DefineOutput(Int_t islot, TClass *type)
 {
 // Define an output slot and its type.
-   if (islot<0) {
-      AliError(Form("Cannot define negative output slot number for task %s", GetName()));
-      return;
-   }   
    AliAnalysisDataSlot *output = new AliAnalysisDataSlot(type, this);
    if (fNoutputs<islot+1) {
       fNoutputs = islot+1;
@@ -247,7 +302,7 @@ void AliAnalysisTask::DefineOutput(Int_t islot, TClass *type)
       fOutputReady = new Bool_t[fNoutputs];
       memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
    } 
-   fOutputs->AddAt(output, islot);
+   fOutputs->AddAtAndExpand(output, islot);
 }
 
 //______________________________________________________________________________
@@ -256,7 +311,7 @@ TClass *AliAnalysisTask::GetInputType(Int_t islot) const
 // Retreive type of a given input slot.
    AliAnalysisDataSlot *input = GetInputSlot(islot);
    if (!input) {
-      AliError(Form("Input slot %i not defined for analysis task %s", islot, GetName()));
+      Error("GetInputType","Input slot %d not defined for analysis task %s", islot, GetName());
       return NULL;
    }
    return (input->GetType());
@@ -268,7 +323,7 @@ TClass *AliAnalysisTask::GetOutputType(Int_t islot) const
 // Retreive type of a given output slot.
    AliAnalysisDataSlot *output = GetOutputSlot(islot);
    if (!output) {
-      AliError(Form("Output slot %i not defined for analysis task %s", islot, GetName()));
+      Error("GetOutputType","Output slot %d not defined for analysis task %s", islot, GetName());
       return NULL;
    }
    return (output->GetType());
@@ -281,12 +336,59 @@ TObject *AliAnalysisTask::GetInputData(Int_t islot) const
 // the object has to be statically cast to the appropriate type.
    AliAnalysisDataSlot *input = GetInputSlot(islot);
    if (!input) {
-      AliError(Form("Input slot %i not defined for analysis task %s", islot, GetName()));
+      Error("GetInputData","Input slot %d not defined for analysis task %s", islot, GetName());
       return NULL;
    }
    return (input->GetData()); 
 }
 
+//______________________________________________________________________________
+TObject *AliAnalysisTask::GetOutputData(Int_t islot) const
+{
+// Retreive output data for a slot. Normally called in UserTask::Terminate to
+// get a valid pointer to data even in case of Proof.
+   AliAnalysisDataSlot *output = GetOutputSlot(islot);
+   if (!output) {
+      Error("GetOutputData","Input slot %d not defined for analysis task %s", islot, GetName());
+      return NULL;
+   }
+   return (output->GetData()); 
+}
+
+//______________________________________________________________________________
+char *AliAnalysisTask::GetBranchAddress(Int_t islot, const char *branch) const
+{
+// Check if a branch with a given name from the specified input is connected
+// to some address. Call this in Init() before trying to call SetBranchAddress()
+// since the adress may be set by other task.
+   return (char *)GetInputSlot(islot)->GetBranchAddress(branch);
+}
+
+//______________________________________________________________________________
+Bool_t AliAnalysisTask::SetBranchAddress(Int_t islot, const char *branch, void *address) const
+{
+// Connect an object address to a branch of the specified input.
+   return GetInputSlot(islot)->SetBranchAddress(branch, address);
+}   
+
+//______________________________________________________________________________
+void AliAnalysisTask::ConnectInputData(Option_t *)
+{
+// Overload and connect your branches here.
+}
+
+//______________________________________________________________________________
+void AliAnalysisTask::CreateOutputObjects()
+{
+// Overload and create your output objects here.
+}
+
+//______________________________________________________________________________
+void AliAnalysisTask::Terminate(Option_t *)
+{
+// Method called by the framework at the end of data processing.
+}
+
 //______________________________________________________________________________
 Bool_t AliAnalysisTask::PostData(Int_t iout, TObject *data, Option_t *option)
 {
@@ -297,11 +399,11 @@ Bool_t AliAnalysisTask::PostData(Int_t iout, TObject *data, Option_t *option)
    fPublishedData = 0;
    AliAnalysisDataSlot *output = GetOutputSlot(iout);
    if (!output) {
-      AliError(Form("Output slot %i not defined for analysis task %s", iout, GetName()));
+      Error("PostData","Output slot %i not defined for analysis task %s", iout, GetName());
       return kFALSE;
    }
    if (!output->IsConnected()) {
-      AliError(Form("Output slot %i of analysis task %s not connected to any data container", iout, GetName()));
+      Error("PostData","Output slot %i of analysis task %s not connected to any data container", iout, GetName());
       return kFALSE;
    }
    if (!fOutputReady) {