1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 // Author: Andrei Gheata, 31/05/2006
19 //==============================================================================
20 // AliAnalysysTask - Class representing a basic analysis task. Any
21 // user-defined task should derive from it and implement the Exec() virtual
23 //==============================================================================
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:
28 // UserTask::UserTask(name, title)
30 // DefineInput(0, TTree::Class());
31 // DefineInput(1, TH1::Class());
33 // DefineOutput(0, TTree::Class());
34 // DefineOutput(1, MyObject::Class());
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:
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
49 // The method Init will be called once per session at the moment when the data is
50 // available at all input slots.
51 // The method Init() has to be overloaded by the derived class in order to:
52 // 1. Define objects that should be created only once per session (e.g. output
54 // 2. Set the branch address or connect to a branch address in case the input
55 // slots are connected to trees.
58 // MyAnalysisTask::Init(Option_t *)
60 // if (!fHist1) fHist1 = new TH1F("h1", ....);
61 // if (!fHist2) fHist2 = new TH1F("h2", ....);
62 // fESD = GetBranchAddress(islot=0, "ESD");
63 // if (!fESD) SetBranchAddress(islot=0, "ESD", &fESD);
66 // The method Terminate() will be called by the framework once at the end of
67 // data processing. Overload this if needed.
69 //==============================================================================
74 #include "AliAnalysisTask.h"
75 #include "AliAnalysisDataSlot.h"
76 #include "AliAnalysisDataContainer.h"
78 ClassImp(AliAnalysisTask)
80 //______________________________________________________________________________
81 AliAnalysisTask::AliAnalysisTask()
91 // Default constructor.
94 //______________________________________________________________________________
95 AliAnalysisTask::AliAnalysisTask(const char *name, const char *title)
102 fPublishedData(NULL),
107 fInputs = new TObjArray(2);
108 fOutputs = new TObjArray(2);
111 //______________________________________________________________________________
112 AliAnalysisTask::AliAnalysisTask(const AliAnalysisTask &task)
115 fInitialized(task.fInitialized),
116 fNinputs(task.fNinputs),
117 fNoutputs(task.fNoutputs),
119 fPublishedData(NULL),
124 fInputs = new TObjArray((fNinputs)?fNinputs:2);
125 fOutputs = new TObjArray((fNoutputs)?fNoutputs:2);
128 for (i=0; i<fNinputs; i++) fInputs->AddAt(task.GetInputSlot(i),i);
129 fOutputReady = new Bool_t[(fNoutputs)?fNoutputs:2];
130 for (i=0; i<fNoutputs; i++) {
131 fOutputReady[i] = IsOutputReady(i);
132 fOutputs->AddAt(task.GetOutputSlot(i),i);
136 //______________________________________________________________________________
137 AliAnalysisTask::~AliAnalysisTask()
140 if (fInputs) {fInputs->Delete(); delete fInputs;}
141 if (fOutputs) {fOutputs->Delete(); delete fOutputs;}
144 //______________________________________________________________________________
145 AliAnalysisTask& AliAnalysisTask::operator=(const AliAnalysisTask& task)
148 if (&task == this) return *this;
149 TTask::operator=(task);
150 fReady = task.IsReady();
151 fInitialized = task.IsInitialized();
152 fNinputs = task.GetNinputs();
153 fNoutputs = task.GetNoutputs();
154 fInputs = new TObjArray((fNinputs)?fNinputs:2);
155 fOutputs = new TObjArray((fNoutputs)?fNoutputs:2);
158 for (i=0; i<fNinputs; i++) fInputs->AddAt(new AliAnalysisDataSlot(*task.GetInputSlot(i)),i);
159 fOutputReady = new Bool_t[(fNoutputs)?fNoutputs:2];
160 for (i=0; i<fNoutputs; i++) {
161 fOutputReady[i] = IsOutputReady(i);
162 fOutputs->AddAt(new AliAnalysisDataSlot(*task.GetOutputSlot(i)),i);
167 //______________________________________________________________________________
168 Bool_t AliAnalysisTask::AreSlotsConnected()
170 // Check if all input/output slots are connected. If this is the case fReady=true
172 if (!fNinputs || !fNoutputs) return kFALSE;
174 AliAnalysisDataSlot *slot;
175 for (i=0; i<fNinputs; i++) {
176 slot = (AliAnalysisDataSlot*)fInputs->At(i);
178 AliError(Form("Input slot %i of task %s not defined !",i,GetName()));
181 if (!slot->IsConnected()) return kFALSE;
183 for (i=0; i<fNoutputs; i++) {
184 slot = (AliAnalysisDataSlot*)fOutputs->At(i);
186 AliError(Form("Output slot %i of task %s not defined !",i,GetName()));
189 if (!slot->IsConnected()) return kFALSE;
195 //______________________________________________________________________________
196 void AliAnalysisTask::CheckNotify(Bool_t init)
198 // Check if data is available from all inputs. Change the status of the task
199 // accordingly. This method is called automatically for all tasks connected
200 // to a container where the data was published.
201 if (init) fInitialized = kFALSE;
202 for (Int_t islot=0; islot<fNinputs; islot++) {
203 if (!GetInputData(islot)) {
209 if (fInitialized) return;
210 TDirectory *cursav = gDirectory;
212 if (cursav) cursav->cd();
213 fInitialized = kTRUE;
216 //______________________________________________________________________________
217 Bool_t AliAnalysisTask::ConnectInput(Int_t islot, AliAnalysisDataContainer *cont)
219 // Connect an input slot to a data container.
220 AliAnalysisDataSlot *input = GetInputSlot(islot);
222 AliError(Form("Input slot %i not defined for analysis task %s", islot, GetName()));
225 // Check type matching
226 if (!input->GetType()->InheritsFrom(cont->GetType())) {
227 AliError(Form("Data type %s for input %i of task %s not matching container %s of type %s",
228 input->GetType()->GetName(), islot, GetName(), cont->GetName(), cont->GetType()->GetName()));
231 // Connect the slot to the container as input
232 if (!input->ConnectContainer(cont)) return kFALSE;
233 // Add this to the list of container consumers
234 cont->AddConsumer(this, islot);
239 //______________________________________________________________________________
240 Bool_t AliAnalysisTask::ConnectOutput(Int_t islot, AliAnalysisDataContainer *cont)
242 // Connect an output slot to a data container.
243 AliAnalysisDataSlot *output = GetOutputSlot(islot);
245 AliError(Form("Output slot %i not defined for analysis task %s", islot, GetName()));
248 // Check type matching
249 if (!output->GetType()->InheritsFrom(cont->GetType())) {
250 AliError(Form("Data type %s for output %i of task %s not matching container %s of type %s",
251 output->GetType()->GetName(), islot, GetName(), cont->GetName(), cont->GetType()->GetName()));
254 // Connect the slot to the container as output
255 if (!output->ConnectContainer(cont)) return kFALSE;
256 // Declare this as the data producer
257 cont->SetProducer(this, islot);
262 //______________________________________________________________________________
263 void AliAnalysisTask::DefineInput(Int_t islot, TClass *type)
265 // Define an input slot and its type.
266 AliAnalysisDataSlot *input = new AliAnalysisDataSlot(type, this);
267 if (fNinputs<islot+1) fNinputs = islot+1;
268 fInputs->AddAtAndExpand(input, islot);
271 //______________________________________________________________________________
272 void AliAnalysisTask::DefineOutput(Int_t islot, TClass *type)
274 // Define an output slot and its type.
276 AliError(Form("Cannot define negative output slot number for task %s", GetName()));
279 AliAnalysisDataSlot *output = new AliAnalysisDataSlot(type, this);
280 if (fNoutputs<islot+1) {
282 if (fOutputReady) delete [] fOutputReady;
283 fOutputReady = new Bool_t[fNoutputs];
284 memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
286 fOutputs->AddAtAndExpand(output, islot);
289 //______________________________________________________________________________
290 TClass *AliAnalysisTask::GetInputType(Int_t islot) const
292 // Retreive type of a given input slot.
293 AliAnalysisDataSlot *input = GetInputSlot(islot);
295 AliError(Form("Input slot %i not defined for analysis task %s", islot, GetName()));
298 return (input->GetType());
301 //______________________________________________________________________________
302 TClass *AliAnalysisTask::GetOutputType(Int_t islot) const
304 // Retreive type of a given output slot.
305 AliAnalysisDataSlot *output = GetOutputSlot(islot);
307 AliError(Form("Output slot %i not defined for analysis task %s", islot, GetName()));
310 return (output->GetType());
313 //______________________________________________________________________________
314 TObject *AliAnalysisTask::GetInputData(Int_t islot) const
316 // Retreive input data for a slot if ready. Normally called by Exec() and
317 // the object has to be statically cast to the appropriate type.
318 AliAnalysisDataSlot *input = GetInputSlot(islot);
320 AliError(Form("Input slot %i not defined for analysis task %s", islot, GetName()));
323 return (input->GetData());
326 //______________________________________________________________________________
327 char *AliAnalysisTask::GetBranchAddress(Int_t islot, const char *branch) const
329 // Check if a branch with a given name from the specified input is connected
330 // to some address. Call this in Init() before trying to call SetBranchAddress()
331 // since the adress may be set by other task.
332 return (char *)GetInputSlot(islot)->GetBranchAddress(branch);
335 //______________________________________________________________________________
336 Bool_t AliAnalysisTask::SetBranchAddress(Int_t islot, const char *branch, void *address) const
338 // Connect an object address to a branch of the specified input.
339 return GetInputSlot(islot)->SetBranchAddress(branch, address);
342 //______________________________________________________________________________
343 void AliAnalysisTask::Init(Option_t *)
345 // Branch address initialization.
348 //______________________________________________________________________________
349 void AliAnalysisTask::Terminate(Option_t *)
351 // Method called by the framework at the end of data processing.
354 //______________________________________________________________________________
355 void AliAnalysisTask::OpenFile(Int_t iout, const char *name, Option_t *option) const
357 // Set data at output iout to be written in the specified file.
358 GetOutputSlot(iout)->GetContainer()->OpenFile(name, option);
361 //______________________________________________________________________________
362 Bool_t AliAnalysisTask::PostData(Int_t iout, TObject *data, Option_t *option)
364 // Post output data for a given ouput slot in the corresponding data container.
365 // Published data becomes owned by the data container.
366 // If option is specified, the container connected to the output slot must have
367 // an associated file name defined. The option represents the method to open the file.
369 AliAnalysisDataSlot *output = GetOutputSlot(iout);
371 AliError(Form("Output slot %i not defined for analysis task %s", iout, GetName()));
374 if (!output->IsConnected()) {
375 AliError(Form("Output slot %i of analysis task %s not connected to any data container", iout, GetName()));
379 fOutputReady = new Bool_t[fNoutputs];
380 memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
382 fOutputReady[iout] = kTRUE;
383 fPublishedData = data;
384 return (output->GetContainer()->SetData(data, option));
387 //______________________________________________________________________________
388 void AliAnalysisTask::SetUsed(Bool_t flag)
390 // Set 'used' flag recursively to task and all daughter tasks.
391 if (TestBit(kTaskUsed)==flag) return;
392 TObject::SetBit(kTaskUsed,flag);
393 Int_t nd = fTasks->GetSize();
394 AliAnalysisTask *task;
395 for (Int_t i=0; i<nd; i++) {
396 task = (AliAnalysisTask*)fTasks->At(i);
401 //______________________________________________________________________________
402 Bool_t AliAnalysisTask::CheckCircularDeps()
404 // Check for illegal circular dependencies, e.g. a daughter task should not have
405 // a hierarchical parent as subtask.
406 if (IsChecked()) return kTRUE;
408 TList *tasks = GetListOfTasks();
409 Int_t ntasks = tasks->GetSize();
410 AliAnalysisTask *task;
411 for (Int_t i=0; i<ntasks; i++) {
412 task = (AliAnalysisTask*)tasks->At(i);
413 if (task->CheckCircularDeps()) return kTRUE;
419 //______________________________________________________________________________
420 void AliAnalysisTask::PrintTask(Option_t *option, Int_t indent) const
423 AliAnalysisTask *thistask = (AliAnalysisTask*)this;
426 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
429 AliAnalysisDataContainer *cont;
430 for (Int_t i=0; i<indent; i++) ind += " ";
431 if (!dep || (dep && IsChecked())) {
432 printf("%s\n", Form("%stask: %s ACTIVE=%i", ind.Data(), GetName(),IsActive()));
433 if (dep) thistask->SetChecked(kFALSE);
435 for (islot=0; islot<fNinputs; islot++) {
436 printf("%s", Form("%s INPUT #%i: %s <- ",ind.Data(),islot, GetInputType(islot)->GetName()));
437 cont = GetInputSlot(islot)->GetContainer();
438 if (cont) printf(" [%s]\n", cont->GetName());
439 else printf(" [NO CONTAINER]\n");
441 for (islot=0; islot<fNoutputs; islot++) {
442 printf("%s", Form("%s OUTPUT #%i: %s -> ",ind.Data(),islot, GetOutputType(islot)->GetName()));
443 cont = GetOutputSlot(islot)->GetContainer();
444 if (cont) printf(" [%s]\n", cont->GetName());
445 else printf(" [NO CONTAINER]\n");
449 PrintContainers(option, indent+3);
452 //______________________________________________________________________________
453 void AliAnalysisTask::PrintContainers(Option_t *option, Int_t indent) const
455 // Print containers info.
456 AliAnalysisDataContainer *cont;
458 for (Int_t i=0; i<indent; i++) ind += " ";
460 for (islot=0; islot<fNoutputs; islot++) {
461 cont = GetOutputSlot(islot)->GetContainer();
462 cont->PrintContainer(option, indent);