First prototype of the new analysis framework (A.Gheata)
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisDataContainer.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// AliAnalysysDataContainer - Container of data of arbitrary type deriving
21// from TObject used for analysis. A container must be connected to the
22// output data slot of a single analysis task (producer) , but also as
23// input slot for possibly several other tasks (consumers). The connected
24// slots must enforce the same data type as the container (or a derived type).
25// A container becomes the owner of the contained data once this was produced.
26//
27// Containers should be defined by the analysis module using:
28//
29// AliAnalysisModule::AddContainer(const char *name, TClass *type);
30//
31// A container should be connected to a producer:
32
33// AliAnalysisModule::ConnectOutput(AliAnalysisTask *task,
34// AliAnalysisDataContainer *cont)
35// and to its consumers:
36//
37// AliAnalysisModule::ConnectInput(AliAnalysisTask *task, Int_t islot,
38// AliAnalysisDataContainer *cont)
39//
40// The container will create an implicit connection between the producer task
41// and all consumers, which will become sub-tasks of the producer.
42//
43//==============================================================================
44
45#include "TClass.h"
46#include "TTree.h"
47#include "TFile.h"
48#include "AliLog.h"
49
50#include "AliAnalysisDataContainer.h"
51#include "AliAnalysisDataSlot.h"
52#include "AliAnalysisTask.h"
53
54ClassImp(AliAnalysisDataContainer)
55
56//______________________________________________________________________________
57AliAnalysisDataContainer::AliAnalysisDataContainer()
58{
59// Default ctor.
60 fDataReady = kFALSE;
61 fOwnedData = kFALSE;
62 fFileName = "";
63 fData = 0;
64 fType = 0;
65 fProducer = 0;
66 fConsumers = 0;
67}
68//______________________________________________________________________________
69AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
70 :TNamed(name,"")
71{
72// Normal constructor.
73 fDataReady = kFALSE;
74 fOwnedData = kTRUE;
75 fFileName = "";
76 fData = 0;
77 fType = type;
78 fProducer = 0;
79 fConsumers = 0;
80}
81
82//______________________________________________________________________________
83AliAnalysisDataContainer::~AliAnalysisDataContainer()
84{
85// Destructor. Deletes data ! (What happens if data is a container ???)
86 if (fData && fOwnedData) delete fData;
87 if (fConsumers) delete fConsumers;
88}
89
90//______________________________________________________________________________
91Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *option)
92{
93// Set the data as READY only if it was published by the producer.
94// If option is not empty the data will be saved in the file fFileName and option
95// describes the method to opent the file: NEW/CREATE, RECREATE, UPDATE
96 // If there is no producer declared, this is a top level container.
97 AliAnalysisTask *task;
98 Int_t i, nc;
99 if (!fProducer) {
100 fData = data;
101 fDataReady = kTRUE;
102 if (fConsumers) {
103 nc = fConsumers->GetEntriesFast();
104 for (i=0; i<nc; i++) {
105 task = (AliAnalysisTask*)fConsumers->At(i);
106 task->CheckNotify();
107 }
108 }
109 return kTRUE;
110 }
111 // Check if it is the producer who published the data
112 if (fProducer->GetPublishedData()==data) {
113 fData = data;
114 fDataReady = kTRUE;
115 if (strlen(option)) {
116 if (!fFileName.Length()) {
117 AliWarning(Form("Cannot write data since file name for container %s was not set", GetName()));
118 return kFALSE;
119 }
120 TFile *f = new TFile(fFileName.Data(), option);
121 if (!f->IsZombie()) {
122 fData->Write();
123 f->Write();
124 }
125 }
126 if (fConsumers) {
127 nc = fConsumers->GetEntriesFast();
128 for (i=0; i<nc; i++) {
129 task = (AliAnalysisTask*)fConsumers->At(i);
130 task->CheckNotify();
131 }
132 }
133 return kTRUE;
134 } else {
135 AliWarning(Form("Data for container %s can be published only by producer task %s",
136 GetName(), fProducer->GetName()));
137 return kFALSE;
138 }
139}
140
141//______________________________________________________________________________
142void AliAnalysisDataContainer::SetFileName(const char *name)
143{
144// Data will be written to this file if it is set using SetData(data, option)
145// Option represent the way the file is accessed: NEW, APPEND, ...
146 fFileName = name;
147}
148
149//______________________________________________________________________________
150void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
151{
152// If data is ready and derives from TTree or from TBranch, this will get the
153// requested entry in memory if not already loaded.
154 if (!fDataReady) return;
155 Bool_t is_tree = fType->InheritsFrom(TTree::Class());
156 if (is_tree) {
157 TTree *tree = (TTree*)fData;
158 if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
159 return;
160 }
161 Bool_t is_branch = fType->InheritsFrom(TBranch::Class());
162 if (is_branch) {
163 TBranch *branch = (TBranch*)fData;
164 if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
165 return;
166 }
167}
168
169//______________________________________________________________________________
170void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
171{
172// Set the producer of data. The slot number is required for data type checking.
173 if (fProducer) {
174 AliWarning(Form("Data container %s already has a producer: %s",
175 GetName(),fProducer->GetName()));
176 }
177 if (fDataReady) {
178 AliError(Form("%s container contains data - cannot change producer!", GetName()));
179 return;
180 }
181 AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
182 if (!slot) {
183 AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot));
184 return;
185 }
186 if (!slot->GetType()->InheritsFrom(fType)) {
187 AliError(Form("Data type %s for output slot %i of task %s does not match container type %s",
188 slot->GetType()->GetName(),islot,prod->GetName(),fType->GetName()));
189 return;
190 }
191
192 fProducer = prod;
193 // Add all consumers as daughter tasks
194 TIter next(fConsumers);
195 AliAnalysisTask *cons;
196 while ((cons=(AliAnalysisTask*)next())) {
197 if (!prod->GetListOfTasks()->FindObject(cons)) prod->Add(cons);
198 }
199}
200
201//______________________________________________________________________________
202void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
203{
204// Add a consumer for contained data;
205 AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot);
206 if (!slot) {
207 AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot));
208 return;
209 }
210 if (!slot->GetType()->InheritsFrom(fType)) {
211 AliError(Form("Data type %s for input slot %i of task %s does not match container type %s",
212 slot->GetType()->GetName(),islot,consumer->GetName(),fType->GetName()));
213 return;
214 }
215
216 if (!fConsumers) fConsumers = new TObjArray(2);
217 fConsumers->Add(consumer);
218 // Add the consumer task to the list of task of the producer
219 if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer))
220 fProducer->Add(consumer);
221}
222
223//______________________________________________________________________________
224Bool_t AliAnalysisDataContainer::ClientsExecuted() const
225{
226// Check if all client tasks have executed.
227 TIter next(fConsumers);
228 AliAnalysisTask *task;
229 while ((task=(AliAnalysisTask*)next())) {
230 if (!task->HasExecuted()) return kFALSE;
231 }
232 return kTRUE;
233}
234
235//______________________________________________________________________________
236void AliAnalysisDataContainer::DeleteData()
237{
238// Delete data if not needed anymore.
239 if (!fDataReady || !ClientsExecuted()) {
240 AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName()));
241 return;
242 }
243 if (!fOwnedData) {
244 AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
245 return;
246 }
247 delete fData;
248 fData = 0;
249 fDataReady = kFALSE;
250}
251
252//______________________________________________________________________________
253void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
254{
255// Print info about this container.
256 TString ind;
257 for (Int_t i=0; i<indent; i++) ind += " ";
258 TString opt(option);
259 opt.ToLower();
260 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
261 if (!dep) {
262 printf("%s\n", Form("%sContainer: %s type: %s", ind.Data(), GetName(), fType->GetName()));
263 if (fProducer)
264 printf("%s\n", Form("%s = Data producer: task %s",ind.Data(),fProducer->GetName()));
265 else
266 printf("%s\n", Form("%s= No data producer"));
267 printf("%s", Form("%s = Consumer tasks: "));
268 if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
269 else printf("\n");
270 }
271 TIter next(fConsumers);
272 AliAnalysisTask *task;
273 while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);
274}