AliRoot Core  3dc7879 (3dc7879)
AliAnalysisDataSlot.cxx
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  * *
4  * Author: The ALICE Off-line Project. *
5  * Contributors are mentioned in the code where appropriate. *
6  * *
7  * Permission to use, copy, modify and distribute this software and its *
8  * documentation strictly for non-commercial purposes is hereby granted *
9  * without fee, provided that the above copyright notice appears in all *
10  * copies and that both the copyright notice and this permission notice *
11  * appear in the supporting documentation. The authors make no claims *
12  * about the suitability of this software for any purpose. It is *
13  * provided "as is" without express or implied warranty. *
14  **************************************************************************/
15 
16 #include <Riostream.h>
17 #include <TROOT.h>
18 #include <TClass.h>
19 #include <TTree.h>
20 #include <TLeaf.h>
21 
22 #include "AliAnalysisDataSlot.h"
23 #include "AliAnalysisTask.h"
25 
26 using std::endl;
27 using std::cout;
28 ClassImp(AliAnalysisDataSlot)
29 
30 //______________________________________________________________________________
32  :TNamed(),
33  fType(type),
34  fParent(task),
35  fContainer(NULL)
36 {
37 // Default constructor.
38  SetTitle(fType->GetName());
39 }
40 
41 //______________________________________________________________________________
43  :TNamed(slot),
44  fType(NULL),
45  fParent(slot.fParent),
46  fContainer(slot.fContainer)
47 {
48 // Copy ctor.
49  GetType();
50 }
51 
52 //______________________________________________________________________________
54 {
55 // Assignment
56  if (&slot == this) return *this;
57  TNamed::operator=(slot);
58  GetType();
59  fParent = slot.fParent;
60  fContainer = slot.fContainer;
61  return *this;
62 }
63 
64 //______________________________________________________________________________
66 {
67 // Connect the data slot with a given container. The operation will succeed only
68 // if the type defined by the slot inherits from the type enforced by the container.
69 // The error message in case of failure is posted by the caller.
70  if (!cont || !GetType()) return kFALSE;
71  if (!fType->InheritsFrom(cont->GetType())) {
72  cout<<"Data slot of type "<<GetTitle()<<" of task "<<fParent->GetName()<<" cannot be connected to data container "<<cont->GetName()<<" of type "<<cont->GetTitle()<<endl;
73  //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()));
74  return kFALSE;
75  }
76  fContainer = cont;
77  return kTRUE;
78 }
79 
80 //______________________________________________________________________________
82 {
83 // Get class type for this slot.
85  if (!fType) slot->SetType(gROOT->GetClass(fTitle.Data()));
86  if (!fType) printf("AliAnalysisDataSlot: Unknown class: %s\n", GetTitle());
87  return fType;
88 }
89 
90 //______________________________________________________________________________
91 void *AliAnalysisDataSlot::GetBranchAddress(const char *branchname) const
92 {
93 // Retrieve the address for a given branch. One should always test this before
94 // using SetBranchAddress because the address gets set by the first caller.
95 // Call this in MyTask::Init()
96  if (!GetType()) return NULL;
97  if (!fType->InheritsFrom(TTree::Class())) {
98  cout<<"Cannot call GetBranchAddress() for data slot of task "<<fParent->GetName()<<" not pointing to tree-type data"<<endl;
99  //AliFatal(Form("Cannot call GetBranchAddress() for data slot of task %s not pointing to tree-type data", fParent->GetName()));
100  return NULL;
101  }
102  if (!IsDataReady()) {
103  cout<<"Cannot call GetBranchAddress() for data slot of task "<<fParent->GetName()<<" while data is not ready"<<endl;
104  //AliFatal(Form("Cannot call GetBranchAddress() for data slot of task %s while data is not ready", fParent->GetName()));
105  return NULL;
106  }
107  TTree *tree = (TTree*)GetData();
108  TBranch *br = tree->GetBranch(branchname);
109  if (!br) {
110  cout<<"Branch "<<branchname<<" not found in tree "<<tree->GetName()<<" as input of task "<<fParent->GetName()<<"..."<<endl;
111  //AliFatal(Form("Branch %s not found in tree %s as input of task %s...", branchname, tree->GetName(), fParent->GetName()));
112  return NULL;
113  }
114  return br->GetAddress();
115 }
116 
117 //______________________________________________________________________________
118 Int_t AliAnalysisDataSlot::EnableBranch(const char *bname, TTree *tree)
119 {
120 // Static method to enable recursively a branch in a tree (why this in not in ROOT?)
121  TBranch *branch = tree->GetBranch(bname);
122  Int_t count = 0;
123 // static Int_t indent = 0;
124  if (!branch) return count;
125 // TString s;
126 // for (Int_t i=0; i<indent; i++) s += " ";
127  count++;
128 // printf("%sbranch %s: kDoNotProcess=%d\n",s.Data(), branch->GetName(), branch->TestBit(kDoNotProcess));
129  branch->SetBit(kDoNotProcess, kFALSE);
130  TIter next(branch->GetListOfBranches());
131  TBranch *branch_sub;
132  // Activate all sub-branches
133 // indent++;
134  while ((branch_sub=(TBranch*)next())) {
135  count += AliAnalysisDataSlot::EnableBranch(branch_sub->GetName(), tree);
136  }
137 // indent--;
138  return count;
139 }
140 
141 //______________________________________________________________________________
142 Bool_t AliAnalysisDataSlot::SetBranchAddress(const char *branchname, void *address)
143 {
144 // Set a branch address for input tree. To be called during MyTask::Init()
145 // only if GetBranchAddress() returns a NULL pointer for a tree-type slot.
146  if (GetBranchAddress(branchname)) {
147  Error("SetBranchAddress","Branch address for %s already set by other task. Call first GetBranchAddress() in %s::ConnectInputData()",branchname, fParent->GetName());
148  return kFALSE;
149  }
150  TTree *tree = (TTree*)GetData();
151  tree->SetBranchAddress(branchname, address);
152  return kTRUE;
153 }
154 
155 //______________________________________________________________________________
157 {
158 // Retreives the data from the container if it is ready.
159  if (!fContainer) {
160  //AliError(Form("Data slot of type %s of task %s has no connected data container",fType->GetName(), fParent->GetName()));
161  return NULL;
162  }
163  if (!fContainer->IsDataReady()) return NULL;
164  return fContainer->GetData();
165 }
166 
167 //______________________________________________________________________________
169 {
170 // Check if data for this slot is ready in its container.
171  if (!fContainer) {
172  cout<<"Data slot of type "<<GetTitle()<<" of task "<<fParent->GetName()<<" has no connected data container"<<endl;
173  //AliError(Form("Data slot of type %s of task %s has no connected data container",fType->GetName(), fParent->GetName()));
174  return kFALSE;
175  }
176  return fContainer->IsDataReady();
177 }
178 
AliAnalysysTask - Class representing a basic analysis task. Any user-defined task should derive from...
printf("Chi2/npoints = %f\n", TMath::Sqrt(chi2/npoints))
Bool_t ConnectContainer(AliAnalysisDataContainer *cont)
Bool_t SetBranchAddress(const char *branch, void *address)
TObject * GetData() const
Getters.
void * GetBranchAddress(const char *branch) const
TClass * fType
! Type of the slot
TROOT * gROOT
AliAnalysysDataContainer AliAnalysysDataContainer - Container of data of arbitrary type deriving from...
static Int_t EnableBranch(const char *bname, TTree *tree)
TObject * GetData() const
TTree * tree
Bool_t IsDataReady() const
Container status checking.
AliAnalysysDataSlot Class representing a data slot of an analysis task. An analysis slot enforces a c...
AliAnalysisDataContainer * fContainer
Container connected to the slot.
void SetType(TClass *type)
TClass * GetType() const
AliAnalysisDataSlot & operator=(const AliAnalysisDataSlot &slot)
AliAnalysisTask * fParent
Analysis task to which the slot belongs.