]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ANALYSIS/AliAnalysisDataSlot.cxx
Coverity defect corrected.
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisDataSlot.cxx
index 9804a952593cd56e6485b84e44fa9a1dd2650727..343853f994b36698d77257d7fb1c72b02d600637 100644 (file)
 // match.
 //==============================================================================
 
-#include "TClass.h"
-#include "AliLog.h"
+#include <Riostream.h>
+#include <TROOT.h>
+#include <TClass.h>
+#include <TTree.h>
+#include <TLeaf.h>
 
 #include "AliAnalysisDataSlot.h"
 #include "AliAnalysisTask.h"
 
 ClassImp(AliAnalysisDataSlot)
 
+//______________________________________________________________________________
+AliAnalysisDataSlot::AliAnalysisDataSlot(TClass *type, AliAnalysisTask *task)
+                    :TNamed(), 
+                     fType(type),
+                     fParent(task), 
+                     fContainer(NULL)
+{
+// Default constructor.
+   SetTitle(fType->GetName());
+}
+
+//______________________________________________________________________________
+AliAnalysisDataSlot::AliAnalysisDataSlot(const AliAnalysisDataSlot &slot)
+                    :TNamed(slot), 
+                     fType(NULL), 
+                     fParent(slot.fParent), 
+                     fContainer(slot.fContainer)
+{
+// Copy ctor.
+   GetType();
+}                        
+
+//______________________________________________________________________________
+AliAnalysisDataSlot& AliAnalysisDataSlot::operator=(const AliAnalysisDataSlot &slot)
+{
+// Assignment
+   if (&slot == this) return *this;
+   TNamed::operator=(slot);
+   GetType();
+   fParent = slot.fParent;
+   fContainer = slot.fContainer;   
+   return *this;
+}
+
 //______________________________________________________________________________
 Bool_t AliAnalysisDataSlot::ConnectContainer(AliAnalysisDataContainer *cont)
 {
 // Connect the data slot with a given container. The operation will succeed only
 // if the type defined by the slot inherits from the type enforced by the container.
 // The error message in case of failure is posted by the caller.
-   if (!cont || !fType) return kFALSE;
+   if (!cont || !GetType()) return kFALSE;
    if (!fType->InheritsFrom(cont->GetType())) {
-      AliError(Form("Data slot of type %s of task %s cannot be connected to data container %s of type %s", 
-                    fType->GetName(), fParent->GetName(), cont->GetName(), cont->GetType()->GetName()));
+     cout<<"Data slot of type "<<GetTitle()<<" of task "<<fParent->GetName()<<" cannot be connected to data container "<<cont->GetName()<<" of type "<<cont->GetTitle()<<endl;
+     //AliError(Form("Data slot of type %s of task %s cannot be connected to data container %s of type %s", fType->GetName(), fParent->GetName(), cont->GetName(), cont->GetType()->GetName()));
       return kFALSE;
    }   
    fContainer = cont;
    return kTRUE;
 }   
 
+//______________________________________________________________________________
+TClass *AliAnalysisDataSlot::GetType() const
+{
+// Get class type for this slot.
+   AliAnalysisDataSlot *slot = (AliAnalysisDataSlot*)this;
+   if (!fType) slot->SetType(gROOT->GetClass(fTitle.Data()));
+   if (!fType) printf("AliAnalysisDataSlot: Unknown class: %s\n", GetTitle());
+   return fType;
+}
+   
+//______________________________________________________________________________
+void *AliAnalysisDataSlot::GetBranchAddress(const char *branchname) const
+{
+// Retrieve the address for a given branch. One should always test this before
+// using SetBranchAddress because the address gets set by the first caller.
+// Call this in MyTask::Init()
+   if (!GetType()) return NULL;
+   if (!fType->InheritsFrom(TTree::Class())) {
+     cout<<"Cannot call GetBranchAddress() for data slot of task "<<fParent->GetName()<<" not pointing to tree-type data"<<endl;
+     //AliFatal(Form("Cannot call GetBranchAddress() for data slot of task %s not pointing to tree-type data", fParent->GetName()));
+      return NULL;
+   }
+   if (!IsDataReady()) {
+     cout<<"Cannot call GetBranchAddress() for data slot of task "<<fParent->GetName()<<" while data is not ready"<<endl;
+     //AliFatal(Form("Cannot call GetBranchAddress() for data slot of task %s while data is not ready", fParent->GetName()));
+      return NULL;
+   }
+   TTree *tree = (TTree*)GetData();
+   TBranch *br = tree->GetBranch(branchname);
+   if (!br) {   
+     cout<<"Branch "<<branchname<<" not found in tree "<<tree->GetName()<<" as input of task "<<fParent->GetName()<<"..."<<endl;
+     //AliFatal(Form("Branch %s not found in tree %s as input of task %s...", branchname, tree->GetName(), fParent->GetName()));
+      return NULL;
+   }
+   return br->GetAddress();
+}   
+
+//______________________________________________________________________________
+Int_t AliAnalysisDataSlot::EnableBranch(const char *bname, TTree *tree)
+{
+// Static method to enable recursively a branch in a tree (why this in not in ROOT?)
+   TBranch *branch = tree->GetBranch(bname);
+   Int_t count = 0;
+//   static Int_t indent = 0;
+   if (!branch) return count;
+//   TString s;
+//   for (Int_t i=0; i<indent; i++) s += " ";
+   count++;
+//   printf("%sbranch %s: kDoNotProcess=%d\n",s.Data(), branch->GetName(), branch->TestBit(kDoNotProcess));
+   branch->SetBit(kDoNotProcess, kFALSE);
+   TIter next(branch->GetListOfBranches());
+   TBranch *branch_sub;
+   // Activate all sub-branches
+//   indent++;
+   while ((branch_sub=(TBranch*)next())) {
+      count += AliAnalysisDataSlot::EnableBranch(branch_sub->GetName(), tree);
+   }
+//   indent--;
+   return count;   
+}   
+
+//______________________________________________________________________________
+Bool_t AliAnalysisDataSlot::SetBranchAddress(const char *branchname, void *address)
+{
+// Set a branch address for input tree. To be called during MyTask::Init()
+// only if GetBranchAddress() returns a NULL pointer for a tree-type slot.
+   if (GetBranchAddress(branchname)) {
+      Error("SetBranchAddress","Branch address for %s already set by other task. Call first GetBranchAddress() in %s::ConnectInputData()",branchname, fParent->GetName());
+      return kFALSE;
+   }
+   TTree *tree = (TTree*)GetData();
+   tree->SetBranchAddress(branchname, address);
+   return kTRUE;
+}   
+      
 //______________________________________________________________________________
 TObject *AliAnalysisDataSlot::GetData() const
 {
 // Retreives the data from the container if it is ready.
    if (!fContainer) {
-      AliError(Form("Data slot of type %s of task %s has no connected data container",
-               fType->GetName(), fParent->GetName()));    
+     //AliError(Form("Data slot of type %s of task %s has no connected data container",fType->GetName(), fParent->GetName()));    
       return NULL;
    }
    if (!fContainer->IsDataReady()) return NULL;
@@ -82,8 +193,8 @@ Bool_t  AliAnalysisDataSlot::IsDataReady() const
 {
 // Check if data for this slot is ready in its container.
    if (!fContainer) {
-      AliError(Form("Data slot of type %s of task %s has no connected data container",
-               fType->GetName(), fParent->GetName()));    
+     cout<<"Data slot of type "<<GetTitle()<<" of task "<<fParent->GetName()<<" has no connected data container"<<endl;
+     //AliError(Form("Data slot of type %s of task %s has no connected data container",fType->GetName(), fParent->GetName()));    
       return kFALSE;
    }
    return fContainer->IsDataReady();