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 | // AliAnalysysTask - Class representing a basic analysis task. Any |
21 | // user-defined task should derive from it and implement the Exec() virtual |
22 | // method. |
23 | //============================================================================== |
24 | // |
25 | // A specific user analysis task have to derive from this class. The list of |
26 | // specific input and output slots have to be defined in the derived class ctor: |
27 | // |
28 | // UserTask::UserTask(name, title) |
29 | // { |
30 | // DefineInput(0, TTree::Class()); |
31 | // DefineInput(1, TH1::Class()); |
32 | // ... |
33 | // DefineOutput(0, TTree::Class()); |
34 | // DefineOutput(1, MyObject::Class()); |
35 | // ... |
36 | // } |
37 | // |
38 | // An existing data contaner (AliAnalysisDataContainer) can be connected to the |
39 | // input/output slots of an analysis task. Containers should not be defined and |
40 | // connected by the derived analysis task, but from the level of AliAnalysisManager: |
41 | // |
42 | // AliAnalysisManager::ConnectInput(AliAnalysisTask *task, Int_t islot, |
43 | // AliAnalysisDataContainer *cont) |
44 | // AliAnalysisManager::ConnectOutput(AliAnalysisTask *task, Int_t islot, |
45 | // AliAnalysisDataContainer *cont) |
46 | // To connect a slot to a data container, the data types declared by both must |
47 | // match. |
327eaf46 |
48 | // |
c52c2132 |
49 | // The method ConnectInputData() has to be overloaded by the derived class in order to |
50 | // set the branch address or connect to a branch address in case the input |
51 | // slots are connected to trees. |
327eaf46 |
52 | // Example: |
c52c2132 |
53 | // MyAnalysisTask::ConnectInputData(Option_t *) |
327eaf46 |
54 | // { |
c52c2132 |
55 | // // One should first check if the branch address was taken by some other task |
56 | // char ** address = (char **)GetBranchAddress(0, "ESD"); |
57 | // if (address) { |
58 | // fESD = (AliESD*)(*address); |
59 | // } else { |
60 | // fESD = new AliESD(); |
61 | // SetBranchAddress(0, "ESD", &fESD); |
62 | // } |
63 | // } |
64 | // |
65 | // The method CreateOutputObjects() has to be overloaded an will contain the |
66 | // objects that should be created only once per session (e.g. output |
67 | // histograms) |
68 | // |
69 | // void MyAnalysisTask::CreateOutputObjects() |
70 | //{ |
71 | // create histograms |
72 | // fhPt = new TH1F("fhPt","This is the Pt distribution",15,0.1,3.1); |
73 | // fhPt->SetStats(kTRUE); |
74 | // fhPt->GetXaxis()->SetTitle("P_{T} [GeV]"); |
75 | // fhPt->GetYaxis()->SetTitle("#frac{dN}{dP_{T}}"); |
76 | // fhPt->GetXaxis()->SetTitleColor(1); |
77 | // fhPt->SetMarkerStyle(kFullCircle); |
327eaf46 |
78 | // } |
79 | // |
80 | // The method Terminate() will be called by the framework once at the end of |
c52c2132 |
81 | // data processing. Overload this if needed. DO NOT ASSUME that the pointers |
82 | // to histograms defined in CreateOutputObjects() are valid, since this is |
83 | // not true in case of PROOF. Restore the pointer values like: |
84 | // |
85 | //void MyAnalysisTask::Terminate(Option_t *) |
86 | //{ |
87 | // fhPt = (TH1F*)GetOutputData(0); |
88 | // ... |
89 | //} |
90 | |
327eaf46 |
91 | // |
d3106602 |
92 | //============================================================================== |
93 | |
0b28fd57 |
94 | #include <Riostream.h> |
0b28fd57 |
95 | #include <TDirectory.h> |
c52c2132 |
96 | #include <TClass.h> |
d3106602 |
97 | |
d3106602 |
98 | #include "AliAnalysisTask.h" |
99 | #include "AliAnalysisDataSlot.h" |
100 | #include "AliAnalysisDataContainer.h" |
101 | |
102 | ClassImp(AliAnalysisTask) |
103 | |
104 | //______________________________________________________________________________ |
105 | AliAnalysisTask::AliAnalysisTask() |
37a26056 |
106 | :fReady(kFALSE), |
327eaf46 |
107 | fInitialized(kFALSE), |
37a26056 |
108 | fNinputs(0), |
109 | fNoutputs(0), |
110 | fOutputReady(NULL), |
111 | fPublishedData(NULL), |
112 | fInputs(NULL), |
113 | fOutputs(NULL) |
d3106602 |
114 | { |
115 | // Default constructor. |
d3106602 |
116 | } |
117 | |
118 | //______________________________________________________________________________ |
119 | AliAnalysisTask::AliAnalysisTask(const char *name, const char *title) |
37a26056 |
120 | :TTask(name,title), |
121 | fReady(kFALSE), |
327eaf46 |
122 | fInitialized(kFALSE), |
37a26056 |
123 | fNinputs(0), |
124 | fNoutputs(0), |
125 | fOutputReady(NULL), |
126 | fPublishedData(NULL), |
127 | fInputs(NULL), |
128 | fOutputs(NULL) |
d3106602 |
129 | { |
37a26056 |
130 | // Constructor. |
d3106602 |
131 | fInputs = new TObjArray(2); |
132 | fOutputs = new TObjArray(2); |
133 | } |
134 | |
135 | //______________________________________________________________________________ |
136 | AliAnalysisTask::AliAnalysisTask(const AliAnalysisTask &task) |
37a26056 |
137 | :TTask(task), |
138 | fReady(task.fReady), |
327eaf46 |
139 | fInitialized(task.fInitialized), |
37a26056 |
140 | fNinputs(task.fNinputs), |
141 | fNoutputs(task.fNoutputs), |
142 | fOutputReady(NULL), |
143 | fPublishedData(NULL), |
144 | fInputs(NULL), |
145 | fOutputs(NULL) |
d3106602 |
146 | { |
147 | // Copy ctor. |
d3106602 |
148 | fInputs = new TObjArray((fNinputs)?fNinputs:2); |
149 | fOutputs = new TObjArray((fNoutputs)?fNoutputs:2); |
150 | fPublishedData = 0; |
151 | Int_t i; |
152 | for (i=0; i<fNinputs; i++) fInputs->AddAt(task.GetInputSlot(i),i); |
153 | fOutputReady = new Bool_t[(fNoutputs)?fNoutputs:2]; |
154 | for (i=0; i<fNoutputs; i++) { |
155 | fOutputReady[i] = IsOutputReady(i); |
156 | fOutputs->AddAt(task.GetOutputSlot(i),i); |
157 | } |
158 | } |
159 | |
160 | //______________________________________________________________________________ |
161 | AliAnalysisTask::~AliAnalysisTask() |
162 | { |
163 | // Dtor. |
c52c2132 |
164 | if (fTasks) fTasks->Clear(); |
d3106602 |
165 | if (fInputs) {fInputs->Delete(); delete fInputs;} |
166 | if (fOutputs) {fOutputs->Delete(); delete fOutputs;} |
167 | } |
168 | |
169 | //______________________________________________________________________________ |
170 | AliAnalysisTask& AliAnalysisTask::operator=(const AliAnalysisTask& task) |
171 | { |
172 | // Assignment |
37a26056 |
173 | if (&task == this) return *this; |
174 | TTask::operator=(task); |
175 | fReady = task.IsReady(); |
327eaf46 |
176 | fInitialized = task.IsInitialized(); |
37a26056 |
177 | fNinputs = task.GetNinputs(); |
178 | fNoutputs = task.GetNoutputs(); |
179 | fInputs = new TObjArray((fNinputs)?fNinputs:2); |
180 | fOutputs = new TObjArray((fNoutputs)?fNoutputs:2); |
181 | fPublishedData = 0; |
182 | Int_t i; |
183 | for (i=0; i<fNinputs; i++) fInputs->AddAt(new AliAnalysisDataSlot(*task.GetInputSlot(i)),i); |
184 | fOutputReady = new Bool_t[(fNoutputs)?fNoutputs:2]; |
185 | for (i=0; i<fNoutputs; i++) { |
186 | fOutputReady[i] = IsOutputReady(i); |
187 | fOutputs->AddAt(new AliAnalysisDataSlot(*task.GetOutputSlot(i)),i); |
188 | } |
d3106602 |
189 | return *this; |
190 | } |
191 | |
192 | //______________________________________________________________________________ |
193 | Bool_t AliAnalysisTask::AreSlotsConnected() |
194 | { |
195 | // Check if all input/output slots are connected. If this is the case fReady=true |
196 | fReady = kFALSE; |
197 | if (!fNinputs || !fNoutputs) return kFALSE; |
198 | Int_t i; |
199 | AliAnalysisDataSlot *slot; |
200 | for (i=0; i<fNinputs; i++) { |
201 | slot = (AliAnalysisDataSlot*)fInputs->At(i); |
202 | if (!slot) { |
c52c2132 |
203 | Error("AreSlotsConnected", "Input slot %d of task %s not defined !",i,GetName()); |
d3106602 |
204 | return kFALSE; |
205 | } |
206 | if (!slot->IsConnected()) return kFALSE; |
207 | } |
208 | for (i=0; i<fNoutputs; i++) { |
209 | slot = (AliAnalysisDataSlot*)fOutputs->At(i); |
210 | if (!slot) { |
c52c2132 |
211 | Error("AreSlotsConnected", "Output slot %d of task %s not defined !",i,GetName()); |
d3106602 |
212 | return kFALSE; |
213 | } |
214 | if (!slot->IsConnected()) return kFALSE; |
215 | } |
216 | fReady = kTRUE; |
217 | return kTRUE; |
218 | } |
219 | |
220 | //______________________________________________________________________________ |
327eaf46 |
221 | void AliAnalysisTask::CheckNotify(Bool_t init) |
d3106602 |
222 | { |
223 | // Check if data is available from all inputs. Change the status of the task |
224 | // accordingly. This method is called automatically for all tasks connected |
225 | // to a container where the data was published. |
327eaf46 |
226 | if (init) fInitialized = kFALSE; |
d3106602 |
227 | for (Int_t islot=0; islot<fNinputs; islot++) { |
228 | if (!GetInputData(islot)) { |
229 | SetActive(kFALSE); |
230 | return; |
231 | } |
232 | } |
233 | SetActive(kTRUE); |
327eaf46 |
234 | if (fInitialized) return; |
235 | TDirectory *cursav = gDirectory; |
c52c2132 |
236 | ConnectInputData(); |
327eaf46 |
237 | if (cursav) cursav->cd(); |
238 | fInitialized = kTRUE; |
d3106602 |
239 | } |
240 | |
241 | //______________________________________________________________________________ |
242 | Bool_t AliAnalysisTask::ConnectInput(Int_t islot, AliAnalysisDataContainer *cont) |
243 | { |
244 | // Connect an input slot to a data container. |
245 | AliAnalysisDataSlot *input = GetInputSlot(islot); |
246 | if (!input) { |
c52c2132 |
247 | Error("ConnectInput","Input slot %i not defined for analysis task %s", islot, GetName()); |
d3106602 |
248 | return kFALSE; |
249 | } |
250 | // Check type matching |
251 | if (!input->GetType()->InheritsFrom(cont->GetType())) { |
c52c2132 |
252 | Error("ConnectInput","Data type %s for input %i of task %s not matching container %s of type %s",input->GetType()->GetName(), islot, GetName(), cont->GetName(), cont->GetType()->GetName()); |
d3106602 |
253 | return kFALSE; |
254 | } |
255 | // Connect the slot to the container as input |
256 | if (!input->ConnectContainer(cont)) return kFALSE; |
257 | // Add this to the list of container consumers |
258 | cont->AddConsumer(this, islot); |
259 | AreSlotsConnected(); |
260 | return kTRUE; |
261 | } |
262 | |
263 | //______________________________________________________________________________ |
264 | Bool_t AliAnalysisTask::ConnectOutput(Int_t islot, AliAnalysisDataContainer *cont) |
265 | { |
266 | // Connect an output slot to a data container. |
267 | AliAnalysisDataSlot *output = GetOutputSlot(islot); |
268 | if (!output) { |
c52c2132 |
269 | Error("ConnectOutput","Output slot %i not defined for analysis task %s", islot, GetName()); |
d3106602 |
270 | return kFALSE; |
271 | } |
272 | // Check type matching |
273 | if (!output->GetType()->InheritsFrom(cont->GetType())) { |
c52c2132 |
274 | Error("ConnectOutput","Data type %s for output %i of task %s not matching container %s of type %s",output->GetType()->GetName(), islot, GetName(), cont->GetName(), cont->GetType()->GetName()); |
d3106602 |
275 | return kFALSE; |
276 | } |
277 | // Connect the slot to the container as output |
278 | if (!output->ConnectContainer(cont)) return kFALSE; |
279 | // Declare this as the data producer |
280 | cont->SetProducer(this, islot); |
281 | AreSlotsConnected(); |
282 | return kTRUE; |
283 | } |
284 | |
285 | //______________________________________________________________________________ |
286 | void AliAnalysisTask::DefineInput(Int_t islot, TClass *type) |
287 | { |
288 | // Define an input slot and its type. |
289 | AliAnalysisDataSlot *input = new AliAnalysisDataSlot(type, this); |
290 | if (fNinputs<islot+1) fNinputs = islot+1; |
6ae18197 |
291 | fInputs->AddAtAndExpand(input, islot); |
d3106602 |
292 | } |
293 | |
294 | //______________________________________________________________________________ |
295 | void AliAnalysisTask::DefineOutput(Int_t islot, TClass *type) |
296 | { |
297 | // Define an output slot and its type. |
d3106602 |
298 | AliAnalysisDataSlot *output = new AliAnalysisDataSlot(type, this); |
299 | if (fNoutputs<islot+1) { |
300 | fNoutputs = islot+1; |
301 | if (fOutputReady) delete [] fOutputReady; |
302 | fOutputReady = new Bool_t[fNoutputs]; |
303 | memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t)); |
304 | } |
6ae18197 |
305 | fOutputs->AddAtAndExpand(output, islot); |
d3106602 |
306 | } |
307 | |
308 | //______________________________________________________________________________ |
309 | TClass *AliAnalysisTask::GetInputType(Int_t islot) const |
310 | { |
311 | // Retreive type of a given input slot. |
312 | AliAnalysisDataSlot *input = GetInputSlot(islot); |
313 | if (!input) { |
c52c2132 |
314 | Error("GetInputType","Input slot %d not defined for analysis task %s", islot, GetName()); |
d3106602 |
315 | return NULL; |
316 | } |
317 | return (input->GetType()); |
318 | } |
319 | |
320 | //______________________________________________________________________________ |
321 | TClass *AliAnalysisTask::GetOutputType(Int_t islot) const |
322 | { |
323 | // Retreive type of a given output slot. |
324 | AliAnalysisDataSlot *output = GetOutputSlot(islot); |
325 | if (!output) { |
c52c2132 |
326 | Error("GetOutputType","Output slot %d not defined for analysis task %s", islot, GetName()); |
d3106602 |
327 | return NULL; |
328 | } |
329 | return (output->GetType()); |
330 | } |
331 | |
332 | //______________________________________________________________________________ |
333 | TObject *AliAnalysisTask::GetInputData(Int_t islot) const |
334 | { |
335 | // Retreive input data for a slot if ready. Normally called by Exec() and |
336 | // the object has to be statically cast to the appropriate type. |
337 | AliAnalysisDataSlot *input = GetInputSlot(islot); |
338 | if (!input) { |
c52c2132 |
339 | Error("GetInputData","Input slot %d not defined for analysis task %s", islot, GetName()); |
d3106602 |
340 | return NULL; |
341 | } |
342 | return (input->GetData()); |
343 | } |
344 | |
c52c2132 |
345 | //______________________________________________________________________________ |
346 | TObject *AliAnalysisTask::GetOutputData(Int_t islot) const |
347 | { |
348 | // Retreive output data for a slot. Normally called in UserTask::Terminate to |
349 | // get a valid pointer to data even in case of Proof. |
350 | AliAnalysisDataSlot *output = GetOutputSlot(islot); |
351 | if (!output) { |
352 | Error("GetOutputData","Input slot %d not defined for analysis task %s", islot, GetName()); |
353 | return NULL; |
354 | } |
355 | return (output->GetData()); |
356 | } |
357 | |
327eaf46 |
358 | //______________________________________________________________________________ |
359 | char *AliAnalysisTask::GetBranchAddress(Int_t islot, const char *branch) const |
360 | { |
361 | // Check if a branch with a given name from the specified input is connected |
362 | // to some address. Call this in Init() before trying to call SetBranchAddress() |
363 | // since the adress may be set by other task. |
364 | return (char *)GetInputSlot(islot)->GetBranchAddress(branch); |
365 | } |
366 | |
367 | //______________________________________________________________________________ |
368 | Bool_t AliAnalysisTask::SetBranchAddress(Int_t islot, const char *branch, void *address) const |
369 | { |
370 | // Connect an object address to a branch of the specified input. |
371 | return GetInputSlot(islot)->SetBranchAddress(branch, address); |
372 | } |
373 | |
374 | //______________________________________________________________________________ |
c52c2132 |
375 | void AliAnalysisTask::ConnectInputData(Option_t *) |
327eaf46 |
376 | { |
c52c2132 |
377 | // Overload and connect your branches here. |
327eaf46 |
378 | } |
379 | |
380 | //______________________________________________________________________________ |
c52c2132 |
381 | void AliAnalysisTask::CreateOutputObjects() |
327eaf46 |
382 | { |
c52c2132 |
383 | // Overload and create your output objects here. |
327eaf46 |
384 | } |
385 | |
386 | //______________________________________________________________________________ |
c52c2132 |
387 | void AliAnalysisTask::Terminate(Option_t *) |
327eaf46 |
388 | { |
c52c2132 |
389 | // Method called by the framework at the end of data processing. |
390 | } |
327eaf46 |
391 | |
d3106602 |
392 | //______________________________________________________________________________ |
393 | Bool_t AliAnalysisTask::PostData(Int_t iout, TObject *data, Option_t *option) |
394 | { |
395 | // Post output data for a given ouput slot in the corresponding data container. |
396 | // Published data becomes owned by the data container. |
397 | // If option is specified, the container connected to the output slot must have |
398 | // an associated file name defined. The option represents the method to open the file. |
399 | fPublishedData = 0; |
400 | AliAnalysisDataSlot *output = GetOutputSlot(iout); |
401 | if (!output) { |
c52c2132 |
402 | Error("PostData","Output slot %i not defined for analysis task %s", iout, GetName()); |
d3106602 |
403 | return kFALSE; |
404 | } |
405 | if (!output->IsConnected()) { |
c52c2132 |
406 | Error("PostData","Output slot %i of analysis task %s not connected to any data container", iout, GetName()); |
d3106602 |
407 | return kFALSE; |
408 | } |
409 | if (!fOutputReady) { |
410 | fOutputReady = new Bool_t[fNoutputs]; |
411 | memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t)); |
412 | } |
413 | fOutputReady[iout] = kTRUE; |
414 | fPublishedData = data; |
415 | return (output->GetContainer()->SetData(data, option)); |
416 | } |
417 | |
418 | //______________________________________________________________________________ |
419 | void AliAnalysisTask::SetUsed(Bool_t flag) |
420 | { |
421 | // Set 'used' flag recursively to task and all daughter tasks. |
422 | if (TestBit(kTaskUsed)==flag) return; |
423 | TObject::SetBit(kTaskUsed,flag); |
424 | Int_t nd = fTasks->GetSize(); |
425 | AliAnalysisTask *task; |
426 | for (Int_t i=0; i<nd; i++) { |
427 | task = (AliAnalysisTask*)fTasks->At(i); |
428 | task->SetUsed(flag); |
429 | } |
430 | } |
431 | |
432 | //______________________________________________________________________________ |
433 | Bool_t AliAnalysisTask::CheckCircularDeps() |
434 | { |
435 | // Check for illegal circular dependencies, e.g. a daughter task should not have |
436 | // a hierarchical parent as subtask. |
437 | if (IsChecked()) return kTRUE; |
438 | SetChecked(); |
439 | TList *tasks = GetListOfTasks(); |
440 | Int_t ntasks = tasks->GetSize(); |
441 | AliAnalysisTask *task; |
442 | for (Int_t i=0; i<ntasks; i++) { |
443 | task = (AliAnalysisTask*)tasks->At(i); |
444 | if (task->CheckCircularDeps()) return kTRUE; |
445 | } |
446 | SetChecked(kFALSE); |
447 | return kFALSE; |
448 | } |
449 | |
450 | //______________________________________________________________________________ |
451 | void AliAnalysisTask::PrintTask(Option_t *option, Int_t indent) const |
452 | { |
453 | // Print task info. |
454 | AliAnalysisTask *thistask = (AliAnalysisTask*)this; |
455 | TString opt(option); |
456 | opt.ToLower(); |
457 | Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE; |
458 | TString ind; |
459 | Int_t islot; |
460 | AliAnalysisDataContainer *cont; |
461 | for (Int_t i=0; i<indent; i++) ind += " "; |
462 | if (!dep || (dep && IsChecked())) { |
463 | printf("%s\n", Form("%stask: %s ACTIVE=%i", ind.Data(), GetName(),IsActive())); |
464 | if (dep) thistask->SetChecked(kFALSE); |
465 | else { |
466 | for (islot=0; islot<fNinputs; islot++) { |
467 | printf("%s", Form("%s INPUT #%i: %s <- ",ind.Data(),islot, GetInputType(islot)->GetName())); |
468 | cont = GetInputSlot(islot)->GetContainer(); |
469 | if (cont) printf(" [%s]\n", cont->GetName()); |
470 | else printf(" [NO CONTAINER]\n"); |
471 | } |
472 | for (islot=0; islot<fNoutputs; islot++) { |
473 | printf("%s", Form("%s OUTPUT #%i: %s -> ",ind.Data(),islot, GetOutputType(islot)->GetName())); |
474 | cont = GetOutputSlot(islot)->GetContainer(); |
475 | if (cont) printf(" [%s]\n", cont->GetName()); |
476 | else printf(" [NO CONTAINER]\n"); |
477 | } |
478 | } |
479 | } |
480 | PrintContainers(option, indent+3); |
481 | } |
482 | |
483 | //______________________________________________________________________________ |
484 | void AliAnalysisTask::PrintContainers(Option_t *option, Int_t indent) const |
485 | { |
486 | // Print containers info. |
487 | AliAnalysisDataContainer *cont; |
488 | TString ind; |
489 | for (Int_t i=0; i<indent; i++) ind += " "; |
490 | Int_t islot; |
491 | for (islot=0; islot<fNoutputs; islot++) { |
492 | cont = GetOutputSlot(islot)->GetContainer(); |
493 | cont->PrintContainer(option, indent); |
494 | } |
495 | } |