]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ANALYSIS/AliAnalysisDataContainer.cxx
Adding a reminder for coders
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisDataContainer.cxx
index ed782a810e315bd5e8907f7bb55148f1a2a6186f..6e5a1d50d421d9a85155fed755090fd158726ec5 100644 (file)
 //
 //==============================================================================
 
-#include "TClass.h"
-#include "TTree.h"
-#include "TFile.h"
-#include "AliLog.h"
+#include <Riostream.h>
+#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"
 ClassImp(AliAnalysisDataContainer)
 
 //______________________________________________________________________________
-AliAnalysisDataContainer::AliAnalysisDataContainer()
+AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
+                          fDataReady(kFALSE),
+                          fOwnedData(kFALSE),
+                          fFileName(),
+                          fFolderName(),
+                          fFile(NULL),
+                          fData(NULL),
+                          fType(NULL),
+                          fProducer(NULL),
+                          fConsumers(NULL)
 {
-// Default ctor.
-   fDataReady = kFALSE;
-   fOwnedData  = kFALSE;
-   fFileName = "";
-   fData  = 0;
-   fType  = 0;
-   fProducer  = 0; 
-   fConsumers = 0;
+// Dummy ctor.
 }
+
 //______________________________________________________________________________
 AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
-                         :TNamed(name,"")
+                         :TNamed(name,""),
+                          fDataReady(kFALSE),
+                          fOwnedData(kFALSE),
+                          fFileName(),
+                          fFolderName(),
+                          fFile(NULL),
+                          fData(NULL),
+                          fType(type),
+                          fProducer(NULL),
+                          fConsumers(NULL)
 {
-// Normal constructor.
-   fDataReady = kFALSE;
-   fOwnedData = kTRUE;
-   fFileName = "";
-   fData  = 0;
-   fType  = type;
-   fProducer  = 0; 
-   fConsumers = 0;
+// Default constructor.
+   SetTitle(fType->GetName());
+}
+
+//______________________________________________________________________________
+AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont)
+                         :TNamed(cont),
+                          fDataReady(cont.fDataReady),
+                          fOwnedData(kFALSE),
+                          fFileName(cont.fFileName),
+                          fFolderName(cont.fFolderName),
+                          fFile(NULL),
+                          fData(cont.fData),
+                          fType(NULL),
+                          fProducer(cont.fProducer),
+                          fConsumers(NULL)
+{
+// Copy ctor.
+   GetType();
+   if (cont.fConsumers) {
+      fConsumers = new TObjArray(2);
+      Int_t ncons = cont.fConsumers->GetEntriesFast();
+      for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
+   }   
 }
 
 //______________________________________________________________________________
@@ -88,41 +121,205 @@ AliAnalysisDataContainer::~AliAnalysisDataContainer()
 }
 
 //______________________________________________________________________________
-Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *option)
+AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont)
+{
+// Assignment.
+   if (&cont != this) {
+      TNamed::operator=(cont);
+      fDataReady = cont.fDataReady;
+      fOwnedData = kFALSE;
+      fFileName = cont.fFileName;
+      fFolderName = cont.fFolderName;
+      fFile = NULL;
+      fData = cont.fData;
+      GetType();
+      fProducer = cont.fProducer;
+      if (cont.fConsumers) {
+         fConsumers = new TObjArray(2);
+         Int_t ncons = cont.fConsumers->GetEntriesFast();
+         for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
+      }   
+   }   
+   return *this;
+}      
+
+//______________________________________________________________________________
+void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
+{
+// Add a consumer for contained data;
+   AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot);
+   if (!slot || !slot->GetType()) {
+     cout<<"Consumer task "<< consumer->GetName()<<" does not have an input/type #"<<islot<<endl;
+     //AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot));
+      return;
+   }
+   if (!slot->GetType()->InheritsFrom(GetType())) {
+     cout<<"Data type "<<slot->GetTitle()<<" for input slot "<<islot<<" of task "<<consumer->GetName()<<" does not match container type "<<GetTitle()<<endl;  
+     //AliError(Form("Data type %s for input slot %i of task %s does not match container type %s", slot->GetType()->GetName(),islot,consumer->GetName(),fType->GetName()));
+      return;
+   }   
+
+   if (!fConsumers) fConsumers = new TObjArray(2);   
+   fConsumers->Add(consumer);
+   // Add the consumer task to the list of task of the producer
+   if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer)) 
+      fProducer->Add(consumer);
+}      
+
+//______________________________________________________________________________
+Bool_t AliAnalysisDataContainer::ClientsExecuted() const
+{
+// Check if all client tasks have executed.
+   TIter next(fConsumers);
+   AliAnalysisTask *task;
+   while ((task=(AliAnalysisTask*)next())) {
+      if (!task->HasExecuted()) return kFALSE;
+   }
+   return kTRUE;
+}   
+
+//______________________________________________________________________________
+void AliAnalysisDataContainer::DeleteData()
+{
+// Delete data if not needed anymore.
+   if (!fDataReady || !ClientsExecuted()) {
+     cout<<"Data not ready or not all clients of container "<<GetName()<<" executed. Data not deleted."<<endl;
+     //AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName()));
+      return;
+   }
+   if (!fOwnedData) {
+     cout<<"Data not owned by container "<<GetName()<<". Not deleted."<<endl;
+     //AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
+      return;
+   }
+   delete fData;
+   fData = 0;
+   fDataReady = kFALSE;
+}   
+
+//______________________________________________________________________________
+TClass *AliAnalysisDataContainer::GetType() const
+{
+// Get class type for this slot.
+   AliAnalysisDataContainer *cont = (AliAnalysisDataContainer*)this;
+   if (!fType) cont->SetType(gROOT->GetClass(fTitle.Data()));
+   if (!fType) printf("AliAnalysisDataContainer: Unknown class: %s\n", GetTitle());
+   return fType;
+}
+
+//______________________________________________________________________________
+void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
+{
+// If data is ready and derives from TTree or from TBranch, this will get the
+// requested entry in memory if not already loaded.
+   if (!fDataReady || !GetType()) return;
+   Bool_t istree = fType->InheritsFrom(TTree::Class());
+   if (istree) {
+      TTree *tree = (TTree*)fData;
+      if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
+      return;
+   }   
+   Bool_t isbranch = fType->InheritsFrom(TBranch::Class());
+   if (isbranch) {
+      TBranch *branch = (TBranch*)fData;
+      if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
+      return;
+   }   
+}   
+
+//______________________________________________________________________________
+Long64_t AliAnalysisDataContainer::Merge(TCollection *list)
+{
+// Merge a list of containers with this one. Containers in the list must have
+// data of the same type.
+   if (!list || !fData) return 0;
+   printf("Merging %d containers %s\n", list->GetSize()+1, GetName());
+   TMethodCall callEnv;
+   if (fData->IsA())
+      callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
+   if (!callEnv.IsValid() && !list->IsEmpty()) {
+      cout << "No merge interface for data stored by " << GetName() << ". Merging not possible !" << endl;
+      return 1;
+   }
+
+   if (list->IsEmpty()) return 1;
+
+   TIter next(list);
+   AliAnalysisDataContainer *cont;
+   // Make a list where to temporary store the data to be merged.
+   TList *collectionData = new TList();
+   Int_t count = 0; // object counter
+   while ((cont=(AliAnalysisDataContainer*)next())) {
+      TObject *data = cont->GetData();
+      if (!data) continue;
+      if (strcmp(cont->GetName(), GetName())) {
+         cout << "Not merging containers with different names !" << endl;
+         continue;
+      }
+      printf(" ... merging object %s\n", data->GetName());
+      collectionData->Add(data);
+      count++;
+   }
+   callEnv.SetParam((Long_t) collectionData);
+   callEnv.Execute(fData);
+   delete collectionData;
+
+   return count+1;
+}
+
+//______________________________________________________________________________
+void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
+{
+// Print info about this container.
+   TString ind;
+   for (Int_t i=0; i<indent; i++) ind += " ";
+   TString opt(option);
+   opt.ToLower();
+   Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
+   if (!dep) {
+      printf("%sContainer: %s  type: %s POST_LOOP=%i", ind.Data(), GetName(), GetTitle(), IsPostEventLoop());
+      if (fProducer) 
+         printf("%s = Data producer: task %s",ind.Data(),fProducer->GetName());
+      else
+         printf("%s= No data producer",ind.Data());
+      printf("%s = Consumer tasks: ", ind.Data());
+      if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
+      else printf("\n");
+   }
+   if (fFolderName.Length())
+     printf("Filename: %s  folder: %s\n", fFileName.Data(), fFolderName.Data());
+   else
+     printf("Filename: %s\n", fFileName.Data());
+   TIter next(fConsumers);
+   AliAnalysisTask *task;
+   while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);
+}   
+
+//______________________________________________________________________________
+Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
 {
 // Set the data as READY only if it was published by the producer.
-// If option is not empty the data will be saved in the file fFileName and option
-// describes the method to opent the file: NEW/CREATE, RECREATE, UPDATE
    // If there is no producer declared, this is a top level container.
    AliAnalysisTask *task;
+   Bool_t init = kFALSE;
    Int_t i, nc;
    if (!fProducer) {
+      if (data != fData) init = kTRUE;
       fData = data;
       fDataReady = kTRUE;
       if (fConsumers) {
          nc = fConsumers->GetEntriesFast();
          for (i=0; i<nc; i++) {
             task = (AliAnalysisTask*)fConsumers->At(i);
-            task->CheckNotify();
+            task->CheckNotify(init);
          }
       }      
       return kTRUE;
-   } 
+   }
    // Check if it is the producer who published the data     
    if (fProducer->GetPublishedData()==data) {
       fData = data;
       fDataReady = kTRUE;
-      if (strlen(option)) {
-         if (!fFileName.Length()) {
-            AliWarning(Form("Cannot write data since file name for container %s was not set", GetName()));
-            return kFALSE;
-         }
-         TFile *f = new TFile(fFileName.Data(), option);
-         if (!f->IsZombie()) {
-            fData->Write();
-            f->Write();
-         }   
-      }
       if (fConsumers) {
          nc = fConsumers->GetEntriesFast();
          for (i=0; i<nc; i++) {
@@ -132,60 +329,56 @@ Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *option)
       }      
       return kTRUE;   
    } else {
-      AliWarning(Form("Data for container %s can be published only by producer task %s", 
-                 GetName(), fProducer->GetName()));   
+     // Ignore data posting from other than the producer
+//      cout<<"Data for container "<<GetName()<<" can be published only by producer task "<<fProducer->GetName()<<endl;
+      //AliWarning(Form("Data for container %s can be published only by producer task %s", GetName(), fProducer->GetName()));   
       return kFALSE;           
    }              
 }
 
 //______________________________________________________________________________
-void AliAnalysisDataContainer::SetFileName(const char *name)
+void AliAnalysisDataContainer::SetFileName(const char *filename)
 {
-// Data will be written to this file if it is set using SetData(data, option)
-// Option represent the way the file is accessed: NEW, APPEND, ...
-   fFileName = name;
-}   
-
-//______________________________________________________________________________
-void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
-{
-// If data is ready and derives from TTree or from TBranch, this will get the
-// requested entry in memory if not already loaded.
-   if (!fDataReady) return;
-   Bool_t is_tree = fType->InheritsFrom(TTree::Class());
-   if (is_tree) {
-      TTree *tree = (TTree*)fData;
-      if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
-      return;
-   }   
-   Bool_t is_branch = fType->InheritsFrom(TBranch::Class());
-   if (is_branch) {
-      TBranch *branch = (TBranch*)fData;
-      if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
-      return;
-   }   
-}   
+// The filename field can be actually composed by the actual file name followed
+// by :dirname (optional):
+// filename = file_name[:dirname]
+// No slashes (/) allowed
+  fFileName = filename;
+  fFolderName = "";
+  Int_t index = fFileName.Index(":");
+  // Fill the folder name
+  if (index >= 0) {
+    fFolderName = fFileName(index+1, fFileName.Length()-index);
+    fFileName.Remove(index);
+  }  
+  if (!fFileName.Length())
+    Fatal("SetFileName", "Empty file name");   
+  if (fFileName.Index("/")>=0)
+    Fatal("SetFileName", "No slashes (/) allowed in the file name");   
+}
 
 //______________________________________________________________________________
 void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
 {
 // Set the producer of data. The slot number is required for data type checking.
    if (fProducer) {
-      AliWarning(Form("Data container %s already has a producer: %s",
-                 GetName(),fProducer->GetName()));
+     cout<<"Data container "<<GetName()<<" already has a producer: "<<fProducer->GetName()<<endl;
+     //AliWarning(Form("Data container %s already has a producer: %s",GetName(),fProducer->GetName()));
    } 
    if (fDataReady) {
-      AliError(Form("%s container contains data - cannot change producer!", GetName()));
+     cout<<GetName()<<" container contains data - cannot change producer!"<<endl;
+     //AliError(Form("%s container contains data - cannot change producer!", GetName()));
       return;
    }   
    AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
    if (!slot) {
-      AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot));
+     cout<<"Producer task "<<prod->GetName()<<" does not have an output #"<<islot<<endl;
+     //AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot));
       return;
    }   
-   if (!slot->GetType()->InheritsFrom(fType)) {
-      AliError(Form("Data type %s for output slot %i of task %s does not match container type %s", 
-                     slot->GetType()->GetName(),islot,prod->GetName(),fType->GetName()));
+   if (!slot->GetType()->InheritsFrom(GetType())) {
+     cout<<"Data type "<<slot->GetTitle()<<"for output slot "<<islot<<" of task "<<prod->GetName()<<" does not match container type "<<GetTitle()<<endl;
+     //AliError(Form("Data type %s for output slot %i of task %s does not match container type %s", slot->GetType()->GetName(),islot,prod->GetName(),fType->GetName()));
       return;
    }   
    
@@ -199,76 +392,104 @@ void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
 }   
 
 //______________________________________________________________________________
-void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
+AliAnalysisDataWrapper *AliAnalysisDataContainer::ExportData() const
 {
-// Add a consumer for contained data;
-   AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot);
-   if (!slot) {
-      AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot));
-      return;
-   }   
-   if (!slot->GetType()->InheritsFrom(fType)) {
-      AliError(Form("Data type %s for input slot %i of task %s does not match container type %s", 
-                     slot->GetType()->GetName(),islot,consumer->GetName(),fType->GetName()));
-      return;
-   }   
+// Wraps data for sending it through the net.
+   AliAnalysisDataWrapper *pack = 0;
+   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;
+}
 
-   if (!fConsumers) fConsumers = new TObjArray(2);   
-   fConsumers->Add(consumer);
-   // Add the consumer task to the list of task of the producer
-   if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer)) 
-      fProducer->Add(consumer);
+//______________________________________________________________________________
+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);
+   }   
 }      
+      
+ClassImp (AliAnalysisDataWrapper)
 
 //______________________________________________________________________________
-Bool_t AliAnalysisDataContainer::ClientsExecuted() const
+AliAnalysisDataWrapper::AliAnalysisDataWrapper(TObject *data)
+                       :TNamed(),
+                        fData(data)
 {
-// Check if all client tasks have executed.
-   TIter next(fConsumers);
-   AliAnalysisTask *task;
-   while ((task=(AliAnalysisTask*)next())) {
-      if (!task->HasExecuted()) return kFALSE;
-   }
-   return kTRUE;
-}   
+// Ctor.
+   if (data) SetName(data->GetName());
+}
 
 //______________________________________________________________________________
-void AliAnalysisDataContainer::DeleteData()
+AliAnalysisDataWrapper::~AliAnalysisDataWrapper()
 {
-// Delete data if not needed anymore.
-   if (!fDataReady || !ClientsExecuted()) {
-      AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName()));
-      return;
-   }
-   if (!fOwnedData) {
-      AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
-      return;
-   }
-   delete fData;
-   fData = 0;
-   fDataReady = kFALSE;
+// Dtor.
+   if (fData && TObject::TestBit(kDeleteData)) delete fData;
 }   
-      
+
 //______________________________________________________________________________
-void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
+AliAnalysisDataWrapper &AliAnalysisDataWrapper::operator=(const AliAnalysisDataWrapper &other)
 {
-// Print info about this container.
-   TString ind;
-   for (Int_t i=0; i<indent; i++) ind += " ";
-   TString opt(option);
-   opt.ToLower();
-   Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
-   if (!dep) {
-      printf("%s\n", Form("%sContainer: %s  type: %s", ind.Data(), GetName(), fType->GetName()));
-      if (fProducer) 
-         printf("%s\n", Form("%s = Data producer: task %s",ind.Data(),fProducer->GetName()));
-      else
-         printf("%s\n", Form("%s= No data producer"));
-      printf("%s", Form("%s = Consumer tasks: "));
-      if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
-      else printf("\n");
+// Assignment.
+   if (&other != this) {
+      TNamed::operator=(other);
+      fData = other.fData;
    }   
-   TIter next(fConsumers);
-   AliAnalysisTask *task;
-   while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);
-}   
+   return *this;
+}
+
+//______________________________________________________________________________
+Long64_t AliAnalysisDataWrapper::Merge(TCollection *list)
+{
+// Merge a list of containers with this one. Containers in the list must have
+// data of the same type.
+   if (TH1::AddDirectoryStatus()) TH1::AddDirectory(kFALSE);
+   if (!fData) return 0;
+   if (!list || list->IsEmpty()) return 1;
+
+   SetDeleteData();
+
+   TMethodCall callEnv;
+   if (fData->IsA())
+      callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
+   if (!callEnv.IsValid()) {
+      cout << "No merge interface for data stored by " << GetName() << ". Merging not possible !" << endl;
+      return 1;
+   }
+
+   TIter next1(list);
+   AliAnalysisDataWrapper *cont;
+   // Make a list where to temporary store the data to be merged.
+   TList *collectionData = new TList();
+   Int_t count = 0; // object counter
+   // printf("Wrapper %s 0x%lx (data=%s) merged with:\n", GetName(), (ULong_t)this, fData->ClassName());
+   while ((cont=(AliAnalysisDataWrapper*)next1())) {
+      cont->SetDeleteData();
+      TObject *data = cont->Data();
+      if (!data) continue;
+      // printf("   - %s 0x%lx (data=%s)\n", cont->GetName(), (ULong_t)cont, data->ClassName());
+      collectionData->Add(data);
+      count++;
+   }
+   callEnv.SetParam((Long_t) collectionData);
+   callEnv.Execute(fData);
+   delete collectionData;
+
+   return count+1;
+}