updated ChangeLog, incremented version id after tagging v0-6
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisDataSlot.cxx
CommitLineData
d3106602 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/* $Id$ */
17// Author: Andrei Gheata, 31/05/2006
18
19//==============================================================================
20// AliAnalysysDataSlot - Class representing a data slot of an analysis task.
21// An analysis slot enforces a certain data type required by the Exec()
22// method of the analysis task. The slot must be connected to a data
23// container with data of the same type.
24//
25// The class should not be directly created by users - it is created by
26// each AliAnalysisTask when defining its input/output slots using:
27//
28// AliAnalysisTask::SetInput(Int_t index, TClass *type);
29// AliAnalysisTask::SetOutput(TClass *type);
30//
31// An existing data contaner (AliAnalysisDataContainer) can be connected to the
32// input/output slots of an analysis task using:
33//
34// AliAnalysisModule::ConnectInput(AliAnalysisTask *task, Int_t islot,
35// AliAnalysisDataContainer *cont)
36// AliAnalysisModule::ConnectOutput(AliAnalysisTask *task,
37// AliAnalysisDataContainer *cont)
38// To connect a slot to a data container, the data types declared by both must
39// match.
40//==============================================================================
41
c52c2132 42#include <Riostream.h>
43#include <TROOT.h>
44#include <TClass.h>
45#include <TTree.h>
521adffd 46#include <TLeaf.h>
d3106602 47
48#include "AliAnalysisDataSlot.h"
49#include "AliAnalysisTask.h"
50#include "AliAnalysisDataContainer.h"
51
52ClassImp(AliAnalysisDataSlot)
53
54//______________________________________________________________________________
c52c2132 55AliAnalysisDataSlot::AliAnalysisDataSlot(TClass *type, AliAnalysisTask *task)
56 :TNamed(),
57 fType(type),
58 fParent(task),
59 fContainer(NULL)
60{
61// Default constructor.
62 SetTitle(fType->GetName());
63}
64
65//______________________________________________________________________________
66AliAnalysisDataSlot::AliAnalysisDataSlot(const AliAnalysisDataSlot &slot)
67 :TNamed(slot),
68 fType(NULL),
69 fParent(slot.fParent),
70 fContainer(slot.fContainer)
71{
72// Copy ctor.
73 GetType();
74}
75
76//______________________________________________________________________________
37a26056 77AliAnalysisDataSlot& AliAnalysisDataSlot::operator=(const AliAnalysisDataSlot &slot)
78{
79// Assignment
80 if (&slot == this) return *this;
c52c2132 81 TNamed::operator=(slot);
82 GetType();
37a26056 83 fParent = slot.fParent;
84 fContainer = slot.fContainer;
85 return *this;
86}
87
88//______________________________________________________________________________
d3106602 89Bool_t AliAnalysisDataSlot::ConnectContainer(AliAnalysisDataContainer *cont)
90{
91// Connect the data slot with a given container. The operation will succeed only
92// if the type defined by the slot inherits from the type enforced by the container.
93// The error message in case of failure is posted by the caller.
c52c2132 94 if (!cont || !GetType()) return kFALSE;
d3106602 95 if (!fType->InheritsFrom(cont->GetType())) {
c52c2132 96 cout<<"Data slot of type "<<GetTitle()<<" of task "<<fParent->GetName()<<" cannot be connected to data container "<<cont->GetName()<<" of type "<<cont->GetTitle()<<endl;
11026a80 97 //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()));
d3106602 98 return kFALSE;
99 }
100 fContainer = cont;
101 return kTRUE;
102}
103
104//______________________________________________________________________________
c52c2132 105TClass *AliAnalysisDataSlot::GetType() const
106{
107// Get class type for this slot.
108 AliAnalysisDataSlot *slot = (AliAnalysisDataSlot*)this;
109 if (!fType) slot->SetType(gROOT->GetClass(fTitle.Data()));
110 if (!fType) printf("AliAnalysisDataSlot: Unknown class: %s\n", GetTitle());
111 return fType;
112}
113
114//______________________________________________________________________________
327eaf46 115void *AliAnalysisDataSlot::GetBranchAddress(const char *branchname) const
116{
117// Retrieve the address for a given branch. One should always test this before
118// using SetBranchAddress because the address gets set by the first caller.
119// Call this in MyTask::Init()
c52c2132 120 if (!GetType()) return NULL;
327eaf46 121 if (!fType->InheritsFrom(TTree::Class())) {
11026a80 122 cout<<"Cannot call GetBranchAddress() for data slot of task "<<fParent->GetName()<<" not pointing to tree-type data"<<endl;
123 //AliFatal(Form("Cannot call GetBranchAddress() for data slot of task %s not pointing to tree-type data", fParent->GetName()));
327eaf46 124 return NULL;
125 }
126 if (!IsDataReady()) {
11026a80 127 cout<<"Cannot call GetBranchAddress() for data slot of task "<<fParent->GetName()<<" while data is not ready"<<endl;
128 //AliFatal(Form("Cannot call GetBranchAddress() for data slot of task %s while data is not ready", fParent->GetName()));
327eaf46 129 return NULL;
130 }
131 TTree *tree = (TTree*)GetData();
132 TBranch *br = tree->GetBranch(branchname);
133 if (!br) {
11026a80 134 cout<<"Branch "<<branchname<<" not found in tree "<<tree->GetName()<<" as input of task "<<fParent->GetName()<<"..."<<endl;
135 //AliFatal(Form("Branch %s not found in tree %s as input of task %s...", branchname, tree->GetName(), fParent->GetName()));
327eaf46 136 return NULL;
137 }
138 return br->GetAddress();
139}
140
141//______________________________________________________________________________
142Bool_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)) {
981f2614 147 Error("SetBranchAddress","Branch address for %s already set by other task. Call first GetBranchAddress() in %s::ConnectInputData()",branchname, fParent->GetName());
327eaf46 148 return kFALSE;
149 }
150 TTree *tree = (TTree*)GetData();
521adffd 151 // Activate the branch itself
981f2614 152 tree->SetBranchStatus(branchname,1);
521adffd 153 TBranch *branch = tree->GetBranch(branchname);
154 if (!branch) return kFALSE;
155 TIter next(branch->GetListOfLeaves());
156 TLeaf *leaf;
157 // Activate all sub-branches
158 while ((leaf=(TLeaf*)next())) {
159 branch = (TBranch*)leaf->GetBranch();
160 tree->SetBranchStatus(branch->GetName(),1);
161 }
327eaf46 162 tree->SetBranchAddress(branchname, address);
163 return kTRUE;
164}
165
166//______________________________________________________________________________
d3106602 167TObject *AliAnalysisDataSlot::GetData() const
168{
169// Retreives the data from the container if it is ready.
170 if (!fContainer) {
c52c2132 171 cout<<"Data slot of type "<<GetTitle()<<" of task "<<fParent->GetName()<<" has no connected data container"<<endl;
11026a80 172 //AliError(Form("Data slot of type %s of task %s has no connected data container",fType->GetName(), fParent->GetName()));
d3106602 173 return NULL;
174 }
175 if (!fContainer->IsDataReady()) return NULL;
176 return fContainer->GetData();
177}
178
179//______________________________________________________________________________
180Bool_t AliAnalysisDataSlot::IsDataReady() const
181{
182// Check if data for this slot is ready in its container.
183 if (!fContainer) {
c52c2132 184 cout<<"Data slot of type "<<GetTitle()<<" of task "<<fParent->GetName()<<" has no connected data container"<<endl;
11026a80 185 //AliError(Form("Data slot of type %s of task %s has no connected data container",fType->GetName(), fParent->GetName()));
d3106602 186 return kFALSE;
187 }
188 return fContainer->IsDataReady();
189}
190