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 | |
54 | ClassImp(AliAnalysisDataContainer) |
55 | |
56 | //______________________________________________________________________________ |
37a26056 |
57 | AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(), |
58 | fDataReady(kFALSE), |
59 | fOwnedData(kFALSE), |
60 | fFileName(), |
61 | fData(NULL), |
62 | fType(NULL), |
63 | fProducer(NULL), |
64 | fConsumers(NULL) |
d3106602 |
65 | { |
66 | // Default ctor. |
d3106602 |
67 | } |
37a26056 |
68 | |
d3106602 |
69 | //______________________________________________________________________________ |
70 | AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type) |
37a26056 |
71 | :TNamed(name,""), |
72 | fDataReady(kFALSE), |
73 | fOwnedData(kTRUE), |
74 | fFileName(), |
75 | fData(NULL), |
76 | fType(type), |
77 | fProducer(NULL), |
78 | fConsumers(NULL) |
d3106602 |
79 | { |
80 | // Normal constructor. |
37a26056 |
81 | } |
82 | |
83 | //______________________________________________________________________________ |
84 | AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont) |
85 | :TNamed(cont), |
86 | fDataReady(cont.fDataReady), |
87 | fOwnedData(kFALSE), |
88 | fFileName(cont.fFileName), |
89 | fData(cont.fData), |
90 | fType(cont.fType), |
91 | fProducer(cont.fProducer), |
92 | fConsumers(NULL) |
93 | { |
94 | // Copy ctor. |
95 | if (cont.fConsumers) { |
96 | fConsumers = new TObjArray(2); |
97 | Int_t ncons = cont.fConsumers->GetEntriesFast(); |
98 | for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i)); |
99 | } |
d3106602 |
100 | } |
101 | |
102 | //______________________________________________________________________________ |
103 | AliAnalysisDataContainer::~AliAnalysisDataContainer() |
104 | { |
105 | // Destructor. Deletes data ! (What happens if data is a container ???) |
106 | if (fData && fOwnedData) delete fData; |
107 | if (fConsumers) delete fConsumers; |
108 | } |
109 | |
37a26056 |
110 | //______________________________________________________________________________ |
111 | AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont) |
112 | { |
113 | // Assignment. |
114 | if (&cont != this) { |
115 | TNamed::operator=(cont); |
116 | fDataReady = cont.fDataReady; |
117 | fOwnedData = kFALSE; // !!! Data owned by cont. |
118 | fFileName = cont.fFileName; |
119 | fData = cont.fData; |
120 | fType = cont.fType; |
121 | fProducer = cont.fProducer; |
122 | if (cont.fConsumers) { |
123 | fConsumers = new TObjArray(2); |
124 | Int_t ncons = cont.fConsumers->GetEntriesFast(); |
125 | for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i)); |
126 | } |
127 | } |
128 | return *this; |
129 | } |
130 | |
d3106602 |
131 | //______________________________________________________________________________ |
132 | Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *option) |
133 | { |
134 | // Set the data as READY only if it was published by the producer. |
135 | // If option is not empty the data will be saved in the file fFileName and option |
136 | // describes the method to opent the file: NEW/CREATE, RECREATE, UPDATE |
137 | // If there is no producer declared, this is a top level container. |
138 | AliAnalysisTask *task; |
139 | Int_t i, nc; |
140 | if (!fProducer) { |
141 | fData = data; |
142 | fDataReady = kTRUE; |
143 | if (fConsumers) { |
144 | nc = fConsumers->GetEntriesFast(); |
145 | for (i=0; i<nc; i++) { |
146 | task = (AliAnalysisTask*)fConsumers->At(i); |
147 | task->CheckNotify(); |
148 | } |
149 | } |
150 | return kTRUE; |
151 | } |
152 | // Check if it is the producer who published the data |
153 | if (fProducer->GetPublishedData()==data) { |
154 | fData = data; |
155 | fDataReady = kTRUE; |
156 | if (strlen(option)) { |
157 | if (!fFileName.Length()) { |
158 | AliWarning(Form("Cannot write data since file name for container %s was not set", GetName())); |
159 | return kFALSE; |
160 | } |
161 | TFile *f = new TFile(fFileName.Data(), option); |
162 | if (!f->IsZombie()) { |
163 | fData->Write(); |
164 | f->Write(); |
165 | } |
166 | } |
167 | if (fConsumers) { |
168 | nc = fConsumers->GetEntriesFast(); |
169 | for (i=0; i<nc; i++) { |
170 | task = (AliAnalysisTask*)fConsumers->At(i); |
171 | task->CheckNotify(); |
172 | } |
173 | } |
174 | return kTRUE; |
175 | } else { |
176 | AliWarning(Form("Data for container %s can be published only by producer task %s", |
177 | GetName(), fProducer->GetName())); |
178 | return kFALSE; |
179 | } |
180 | } |
181 | |
182 | //______________________________________________________________________________ |
183 | void AliAnalysisDataContainer::SetFileName(const char *name) |
184 | { |
185 | // Data will be written to this file if it is set using SetData(data, option) |
186 | // Option represent the way the file is accessed: NEW, APPEND, ... |
187 | fFileName = name; |
188 | } |
189 | |
190 | //______________________________________________________________________________ |
191 | void AliAnalysisDataContainer::GetEntry(Long64_t ientry) |
192 | { |
193 | // If data is ready and derives from TTree or from TBranch, this will get the |
194 | // requested entry in memory if not already loaded. |
195 | if (!fDataReady) return; |
196 | Bool_t is_tree = fType->InheritsFrom(TTree::Class()); |
197 | if (is_tree) { |
198 | TTree *tree = (TTree*)fData; |
199 | if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry); |
200 | return; |
201 | } |
202 | Bool_t is_branch = fType->InheritsFrom(TBranch::Class()); |
203 | if (is_branch) { |
204 | TBranch *branch = (TBranch*)fData; |
205 | if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry); |
206 | return; |
207 | } |
208 | } |
209 | |
210 | //______________________________________________________________________________ |
211 | void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot) |
212 | { |
213 | // Set the producer of data. The slot number is required for data type checking. |
214 | if (fProducer) { |
215 | AliWarning(Form("Data container %s already has a producer: %s", |
216 | GetName(),fProducer->GetName())); |
217 | } |
218 | if (fDataReady) { |
219 | AliError(Form("%s container contains data - cannot change producer!", GetName())); |
220 | return; |
221 | } |
222 | AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot); |
223 | if (!slot) { |
224 | AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot)); |
225 | return; |
226 | } |
227 | if (!slot->GetType()->InheritsFrom(fType)) { |
228 | AliError(Form("Data type %s for output slot %i of task %s does not match container type %s", |
229 | slot->GetType()->GetName(),islot,prod->GetName(),fType->GetName())); |
230 | return; |
231 | } |
232 | |
233 | fProducer = prod; |
234 | // Add all consumers as daughter tasks |
235 | TIter next(fConsumers); |
236 | AliAnalysisTask *cons; |
237 | while ((cons=(AliAnalysisTask*)next())) { |
238 | if (!prod->GetListOfTasks()->FindObject(cons)) prod->Add(cons); |
239 | } |
240 | } |
241 | |
242 | //______________________________________________________________________________ |
243 | void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot) |
244 | { |
245 | // Add a consumer for contained data; |
246 | AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot); |
247 | if (!slot) { |
248 | AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot)); |
249 | return; |
250 | } |
251 | if (!slot->GetType()->InheritsFrom(fType)) { |
252 | AliError(Form("Data type %s for input slot %i of task %s does not match container type %s", |
253 | slot->GetType()->GetName(),islot,consumer->GetName(),fType->GetName())); |
254 | return; |
255 | } |
256 | |
257 | if (!fConsumers) fConsumers = new TObjArray(2); |
258 | fConsumers->Add(consumer); |
259 | // Add the consumer task to the list of task of the producer |
260 | if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer)) |
261 | fProducer->Add(consumer); |
262 | } |
263 | |
264 | //______________________________________________________________________________ |
265 | Bool_t AliAnalysisDataContainer::ClientsExecuted() const |
266 | { |
267 | // Check if all client tasks have executed. |
268 | TIter next(fConsumers); |
269 | AliAnalysisTask *task; |
270 | while ((task=(AliAnalysisTask*)next())) { |
271 | if (!task->HasExecuted()) return kFALSE; |
272 | } |
273 | return kTRUE; |
274 | } |
275 | |
276 | //______________________________________________________________________________ |
277 | void AliAnalysisDataContainer::DeleteData() |
278 | { |
279 | // Delete data if not needed anymore. |
280 | if (!fDataReady || !ClientsExecuted()) { |
281 | AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName())); |
282 | return; |
283 | } |
284 | if (!fOwnedData) { |
285 | AliWarning(Form("Data not owned by container %s. Not deleted.", GetName())); |
286 | return; |
287 | } |
288 | delete fData; |
289 | fData = 0; |
290 | fDataReady = kFALSE; |
291 | } |
292 | |
293 | //______________________________________________________________________________ |
294 | void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const |
295 | { |
296 | // Print info about this container. |
297 | TString ind; |
298 | for (Int_t i=0; i<indent; i++) ind += " "; |
299 | TString opt(option); |
300 | opt.ToLower(); |
301 | Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE; |
302 | if (!dep) { |
303 | printf("%s\n", Form("%sContainer: %s type: %s", ind.Data(), GetName(), fType->GetName())); |
304 | if (fProducer) |
305 | printf("%s\n", Form("%s = Data producer: task %s",ind.Data(),fProducer->GetName())); |
306 | else |
307 | printf("%s\n", Form("%s= No data producer")); |
308 | printf("%s", Form("%s = Consumer tasks: ")); |
309 | if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n"); |
310 | else printf("\n"); |
311 | } |
312 | TIter next(fConsumers); |
313 | AliAnalysisTask *task; |
314 | while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3); |
315 | } |