Adding a reminder for coders
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisDataContainer.cxx
index 982915b..6e5a1d5 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"
@@ -60,6 +63,8 @@ AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
                           fDataReady(kFALSE),
                           fOwnedData(kFALSE),
                           fFileName(),
+                          fFolderName(),
+                          fFile(NULL),
                           fData(NULL),
                           fType(NULL),
                           fProducer(NULL),
@@ -72,8 +77,10 @@ AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
 AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
                          :TNamed(name,""),
                           fDataReady(kFALSE),
-                          fOwnedData(kTRUE),
+                          fOwnedData(kFALSE),
                           fFileName(),
+                          fFolderName(),
+                          fFile(NULL),
                           fData(NULL),
                           fType(type),
                           fProducer(NULL),
@@ -89,6 +96,8 @@ AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContaine
                           fDataReady(cont.fDataReady),
                           fOwnedData(kFALSE),
                           fFileName(cont.fFileName),
+                          fFolderName(cont.fFolderName),
+                          fFile(NULL),
                           fData(cont.fData),
                           fType(NULL),
                           fProducer(cont.fProducer),
@@ -118,8 +127,10 @@ AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisD
    if (&cont != this) {
       TNamed::operator=(cont);
       fDataReady = cont.fDataReady;
-      fOwnedData = kFALSE;  // !!! Data owned by cont.
+      fOwnedData = kFALSE;
       fFileName = cont.fFileName;
+      fFolderName = cont.fFolderName;
+      fFile = NULL;
       fData = cont.fData;
       GetType();
       fProducer = cont.fProducer;
@@ -275,7 +286,10 @@ void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) co
       if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
       else printf("\n");
    }
-   printf("Filename: %s\n", fFileName.Data());
+   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);
@@ -315,13 +329,35 @@ Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
       }      
       return kTRUE;   
    } else {
-     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;           
+     // 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 *filename)
+{
+// 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.
@@ -360,7 +396,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;
@@ -372,13 +413,37 @@ 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)
 
 //______________________________________________________________________________
+AliAnalysisDataWrapper::AliAnalysisDataWrapper(TObject *data)
+                       :TNamed(),
+                        fData(data)
+{
+// Ctor.
+   if (data) SetName(data->GetName());
+}
+
+//______________________________________________________________________________
+AliAnalysisDataWrapper::~AliAnalysisDataWrapper()
+{
+// Dtor.
+   if (fData && TObject::TestBit(kDeleteData)) delete fData;
+}   
+
+//______________________________________________________________________________
 AliAnalysisDataWrapper &AliAnalysisDataWrapper::operator=(const AliAnalysisDataWrapper &other)
 {
 // Assignment.
@@ -394,38 +459,13 @@ 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;
 
-   printf("Merging %d data wrappers %s\n", list->GetSize()+1, GetName());
+   SetDeleteData();
+
    TMethodCall callEnv;
-   if (fData->InheritsFrom(TSeqCollection::Class())) {
-      TSeqCollection *coll = (TSeqCollection*)fData;
-      if (coll->IsEmpty()) return 0;
-      Int_t nentries = coll->GetEntries();
-      AliAnalysisDataWrapper *top, *crt;
-      TIter next(list);
-      TSeqCollection *collcrt = 0;
-      TList *list1 = 0;
-      // Loop entries of the collection attached to this wrapper.
-      for (Int_t i=0; i<nentries; i++) {
-         list1 = new TList();
-         top = new AliAnalysisDataWrapper(coll->At(i));
-         next.Reset();
-         // Loop wrappers coming in the 'to merge with' list
-         while ((crt=(AliAnalysisDataWrapper*)next())) {
-            collcrt = (TSeqCollection*)crt->Data();
-            list1->Add(new AliAnalysisDataWrapper(collcrt->At(i)));
-         }
-         // Now merge 'top' wrapper with 'list1'. This may go recursively.
-         top->Merge(list1);
-         delete top;
-         list1->Delete();
-         delete list1;
-      }
-      return nentries;
-   }   
-   
    if (fData->IsA())
       callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
    if (!callEnv.IsValid()) {
@@ -438,9 +478,12 @@ Long64_t AliAnalysisDataWrapper::Merge(TCollection *list)
    // 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++;
    }