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), |
327eaf46 |
60 | fFile(NULL), |
37a26056 |
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), |
327eaf46 |
74 | fFile(NULL), |
37a26056 |
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), |
327eaf46 |
88 | fFile(cont.fFile), |
37a26056 |
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; |
327eaf46 |
108 | if (fFile) { |
109 | fFile->Close(); |
110 | delete fFile; |
111 | } |
d3106602 |
112 | } |
113 | |
37a26056 |
114 | //______________________________________________________________________________ |
115 | AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont) |
116 | { |
117 | // Assignment. |
118 | if (&cont != this) { |
119 | TNamed::operator=(cont); |
120 | fDataReady = cont.fDataReady; |
121 | fOwnedData = kFALSE; // !!! Data owned by cont. |
327eaf46 |
122 | fFile = cont.fFile; |
37a26056 |
123 | fData = cont.fData; |
124 | fType = cont.fType; |
125 | fProducer = cont.fProducer; |
126 | if (cont.fConsumers) { |
127 | fConsumers = new TObjArray(2); |
128 | Int_t ncons = cont.fConsumers->GetEntriesFast(); |
129 | for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i)); |
130 | } |
131 | } |
132 | return *this; |
133 | } |
134 | |
d3106602 |
135 | //______________________________________________________________________________ |
327eaf46 |
136 | Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *) |
d3106602 |
137 | { |
138 | // Set the data as READY only if it was published by the producer. |
d3106602 |
139 | // If there is no producer declared, this is a top level container. |
140 | AliAnalysisTask *task; |
327eaf46 |
141 | Bool_t init = kFALSE; |
d3106602 |
142 | Int_t i, nc; |
143 | if (!fProducer) { |
327eaf46 |
144 | if (data != fData) init = kTRUE; |
d3106602 |
145 | fData = data; |
146 | fDataReady = kTRUE; |
147 | if (fConsumers) { |
148 | nc = fConsumers->GetEntriesFast(); |
149 | for (i=0; i<nc; i++) { |
150 | task = (AliAnalysisTask*)fConsumers->At(i); |
327eaf46 |
151 | task->CheckNotify(init); |
d3106602 |
152 | } |
153 | } |
154 | return kTRUE; |
155 | } |
156 | // Check if it is the producer who published the data |
157 | if (fProducer->GetPublishedData()==data) { |
158 | fData = data; |
159 | fDataReady = kTRUE; |
d3106602 |
160 | if (fConsumers) { |
161 | nc = fConsumers->GetEntriesFast(); |
162 | for (i=0; i<nc; i++) { |
163 | task = (AliAnalysisTask*)fConsumers->At(i); |
164 | task->CheckNotify(); |
165 | } |
166 | } |
167 | return kTRUE; |
168 | } else { |
169 | AliWarning(Form("Data for container %s can be published only by producer task %s", |
170 | GetName(), fProducer->GetName())); |
171 | return kFALSE; |
172 | } |
173 | } |
174 | |
175 | //______________________________________________________________________________ |
327eaf46 |
176 | void AliAnalysisDataContainer::OpenFile(const char *name, Option_t *option) |
d3106602 |
177 | { |
327eaf46 |
178 | // Data will be written to this file at the end of processing. |
d3106602 |
179 | // Option represent the way the file is accessed: NEW, APPEND, ... |
327eaf46 |
180 | if (fFile) { |
181 | fFile->Close(); |
182 | delete fFile; |
183 | } |
184 | fFile = new TFile(name, option); |
185 | if (fFile->IsZombie()) { |
186 | AliError(Form("Cannot open file %s with option %s",name,option)); |
187 | fFile = 0; |
188 | } |
d3106602 |
189 | } |
190 | |
327eaf46 |
191 | //______________________________________________________________________________ |
192 | void AliAnalysisDataContainer::WriteData() |
193 | { |
194 | // Write data to the file. |
195 | if (fFile) { |
196 | TDirectory *cursav = gDirectory; |
197 | fFile->cd(); |
198 | fData->Write(); |
199 | // fFile->Write(); |
200 | if (cursav) cursav->cd(); |
201 | } |
202 | } |
203 | |
d3106602 |
204 | //______________________________________________________________________________ |
205 | void AliAnalysisDataContainer::GetEntry(Long64_t ientry) |
206 | { |
207 | // If data is ready and derives from TTree or from TBranch, this will get the |
208 | // requested entry in memory if not already loaded. |
209 | if (!fDataReady) return; |
327eaf46 |
210 | Bool_t istree = fType->InheritsFrom(TTree::Class()); |
211 | if (istree) { |
d3106602 |
212 | TTree *tree = (TTree*)fData; |
213 | if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry); |
214 | return; |
215 | } |
327eaf46 |
216 | Bool_t isbranch = fType->InheritsFrom(TBranch::Class()); |
217 | if (isbranch) { |
d3106602 |
218 | TBranch *branch = (TBranch*)fData; |
219 | if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry); |
220 | return; |
221 | } |
222 | } |
223 | |
224 | //______________________________________________________________________________ |
225 | void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot) |
226 | { |
227 | // Set the producer of data. The slot number is required for data type checking. |
228 | if (fProducer) { |
229 | AliWarning(Form("Data container %s already has a producer: %s", |
230 | GetName(),fProducer->GetName())); |
231 | } |
232 | if (fDataReady) { |
233 | AliError(Form("%s container contains data - cannot change producer!", GetName())); |
234 | return; |
235 | } |
236 | AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot); |
237 | if (!slot) { |
238 | AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot)); |
239 | return; |
240 | } |
241 | if (!slot->GetType()->InheritsFrom(fType)) { |
242 | AliError(Form("Data type %s for output slot %i of task %s does not match container type %s", |
243 | slot->GetType()->GetName(),islot,prod->GetName(),fType->GetName())); |
244 | return; |
245 | } |
246 | |
247 | fProducer = prod; |
248 | // Add all consumers as daughter tasks |
249 | TIter next(fConsumers); |
250 | AliAnalysisTask *cons; |
251 | while ((cons=(AliAnalysisTask*)next())) { |
252 | if (!prod->GetListOfTasks()->FindObject(cons)) prod->Add(cons); |
253 | } |
254 | } |
255 | |
256 | //______________________________________________________________________________ |
257 | void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot) |
258 | { |
259 | // Add a consumer for contained data; |
260 | AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot); |
261 | if (!slot) { |
262 | AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot)); |
263 | return; |
264 | } |
265 | if (!slot->GetType()->InheritsFrom(fType)) { |
266 | AliError(Form("Data type %s for input slot %i of task %s does not match container type %s", |
267 | slot->GetType()->GetName(),islot,consumer->GetName(),fType->GetName())); |
268 | return; |
269 | } |
270 | |
271 | if (!fConsumers) fConsumers = new TObjArray(2); |
272 | fConsumers->Add(consumer); |
273 | // Add the consumer task to the list of task of the producer |
274 | if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer)) |
275 | fProducer->Add(consumer); |
276 | } |
277 | |
278 | //______________________________________________________________________________ |
279 | Bool_t AliAnalysisDataContainer::ClientsExecuted() const |
280 | { |
281 | // Check if all client tasks have executed. |
282 | TIter next(fConsumers); |
283 | AliAnalysisTask *task; |
284 | while ((task=(AliAnalysisTask*)next())) { |
285 | if (!task->HasExecuted()) return kFALSE; |
286 | } |
287 | return kTRUE; |
288 | } |
289 | |
290 | //______________________________________________________________________________ |
291 | void AliAnalysisDataContainer::DeleteData() |
292 | { |
293 | // Delete data if not needed anymore. |
294 | if (!fDataReady || !ClientsExecuted()) { |
295 | AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName())); |
296 | return; |
297 | } |
298 | if (!fOwnedData) { |
299 | AliWarning(Form("Data not owned by container %s. Not deleted.", GetName())); |
300 | return; |
301 | } |
302 | delete fData; |
303 | fData = 0; |
304 | fDataReady = kFALSE; |
305 | } |
306 | |
307 | //______________________________________________________________________________ |
308 | void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const |
309 | { |
310 | // Print info about this container. |
311 | TString ind; |
312 | for (Int_t i=0; i<indent; i++) ind += " "; |
313 | TString opt(option); |
314 | opt.ToLower(); |
315 | Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE; |
316 | if (!dep) { |
317 | printf("%s\n", Form("%sContainer: %s type: %s", ind.Data(), GetName(), fType->GetName())); |
318 | if (fProducer) |
319 | printf("%s\n", Form("%s = Data producer: task %s",ind.Data(),fProducer->GetName())); |
320 | else |
321 | printf("%s\n", Form("%s= No data producer")); |
322 | printf("%s", Form("%s = Consumer tasks: ")); |
323 | if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n"); |
324 | else printf("\n"); |
325 | } |
326 | TIter next(fConsumers); |
327 | AliAnalysisTask *task; |
328 | while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3); |
329 | } |