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 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.
53 // MyAnalysisTask::ConnectInputData(Option_t *)
55 // // One should first check if the branch address was taken by some other task
56 // char ** address = (char **)GetBranchAddress(0, "ESD");
58 // fESD = (AliESD*)(*address);
60 // fESD = new AliESD();
61 // SetBranchAddress(0, "ESD", &fESD);
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
69 // void MyAnalysisTask::CreateOutputObjects()
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);
80 // The method Terminate() will be called by the framework once at the end of
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:
85 //void MyAnalysisTask::Terminate(Option_t *)
87 // fhPt = (TH1F*)GetOutputData(0);
92 //==============================================================================
94 #include <Riostream.h>
95 #include <TDirectory.h>
98 #include "AliAnalysisTask.h"
99 #include "AliAnalysisDataSlot.h"
100 #include "AliAnalysisDataContainer.h"
102 ClassImp(AliAnalysisTask)
104 //______________________________________________________________________________
105 AliAnalysisTask::AliAnalysisTask()
107 fInitialized(kFALSE),
111 fPublishedData(NULL),
115 // Default constructor.
118 //______________________________________________________________________________
119 AliAnalysisTask::AliAnalysisTask(const char *name, const char *title)
122 fInitialized(kFALSE),
126 fPublishedData(NULL),
131 fInputs = new TObjArray(2);
132 fOutputs = new TObjArray(2);
135 //______________________________________________________________________________
136 AliAnalysisTask::AliAnalysisTask(const AliAnalysisTask &task)
139 fInitialized(task.fInitialized),
140 fNinputs(task.fNinputs),
141 fNoutputs(task.fNoutputs),
143 fPublishedData(NULL),
148 fInputs = new TObjArray((fNinputs)?fNinputs:2);
149 fOutputs = new TObjArray((fNoutputs)?fNoutputs:2);
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);
160 //______________________________________________________________________________
161 AliAnalysisTask::~AliAnalysisTask()
164 if (fTasks) fTasks->Clear();
165 if (fInputs) {fInputs->Delete(); delete fInputs;}
166 if (fOutputs) {fOutputs->Delete(); delete fOutputs;}
169 //______________________________________________________________________________
170 AliAnalysisTask& AliAnalysisTask::operator=(const AliAnalysisTask& task)
173 if (&task == this) return *this;
174 TTask::operator=(task);
175 fReady = task.IsReady();
176 fInitialized = task.IsInitialized();
177 fNinputs = task.GetNinputs();
178 fNoutputs = task.GetNoutputs();
179 fInputs = new TObjArray((fNinputs)?fNinputs:2);
180 fOutputs = new TObjArray((fNoutputs)?fNoutputs:2);
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);
192 //______________________________________________________________________________
193 Bool_t AliAnalysisTask::AreSlotsConnected()
195 // Check if all input/output slots are connected. If this is the case fReady=true
197 if (!fNinputs || !fNoutputs) return kFALSE;
199 AliAnalysisDataSlot *slot;
200 for (i=0; i<fNinputs; i++) {
201 slot = (AliAnalysisDataSlot*)fInputs->At(i);
203 Error("AreSlotsConnected", "Input slot %d of task %s not defined !",i,GetName());
206 if (!slot->IsConnected()) return kFALSE;
208 for (i=0; i<fNoutputs; i++) {
209 slot = (AliAnalysisDataSlot*)fOutputs->At(i);
211 Error("AreSlotsConnected", "Output slot %d of task %s not defined !",i,GetName());
214 if (!slot->IsConnected()) return kFALSE;
220 //______________________________________________________________________________
221 void AliAnalysisTask::CheckNotify(Bool_t init)
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.
226 if (init) fInitialized = kFALSE;
227 for (Int_t islot=0; islot<fNinputs; islot++) {
228 if (!GetInputData(islot)) {
234 if (fInitialized) return;
235 TDirectory *cursav = gDirectory;
237 if (cursav) cursav->cd();
238 fInitialized = kTRUE;
241 //______________________________________________________________________________
242 Bool_t AliAnalysisTask::ConnectInput(Int_t islot, AliAnalysisDataContainer *cont)
244 // Connect an input slot to a data container.
245 AliAnalysisDataSlot *input = GetInputSlot(islot);
247 Error("ConnectInput","Input slot %i not defined for analysis task %s", islot, GetName());
250 // Check type matching
251 if (!input->GetType()->InheritsFrom(cont->GetType())) {
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());
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);
263 //______________________________________________________________________________
264 Bool_t AliAnalysisTask::ConnectOutput(Int_t islot, AliAnalysisDataContainer *cont)
266 // Connect an output slot to a data container.
267 AliAnalysisDataSlot *output = GetOutputSlot(islot);
269 Error("ConnectOutput","Output slot %i not defined for analysis task %s", islot, GetName());
272 // Check type matching
273 if (!output->GetType()->InheritsFrom(cont->GetType())) {
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());
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);
285 //______________________________________________________________________________
286 void AliAnalysisTask::DefineInput(Int_t islot, TClass *type)
288 // Define an input slot and its type.
289 AliAnalysisDataSlot *input = new AliAnalysisDataSlot(type, this);
290 if (fNinputs<islot+1) fNinputs = islot+1;
291 fInputs->AddAtAndExpand(input, islot);
294 //______________________________________________________________________________
295 void AliAnalysisTask::DefineOutput(Int_t islot, TClass *type)
297 // Define an output slot and its type.
298 AliAnalysisDataSlot *output = new AliAnalysisDataSlot(type, this);
299 if (fNoutputs<islot+1) {
301 if (fOutputReady) delete [] fOutputReady;
302 fOutputReady = new Bool_t[fNoutputs];
303 memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
305 fOutputs->AddAtAndExpand(output, islot);
308 //______________________________________________________________________________
309 TClass *AliAnalysisTask::GetInputType(Int_t islot) const
311 // Retreive type of a given input slot.
312 AliAnalysisDataSlot *input = GetInputSlot(islot);
314 Error("GetInputType","Input slot %d not defined for analysis task %s", islot, GetName());
317 return (input->GetType());
320 //______________________________________________________________________________
321 TClass *AliAnalysisTask::GetOutputType(Int_t islot) const
323 // Retreive type of a given output slot.
324 AliAnalysisDataSlot *output = GetOutputSlot(islot);
326 Error("GetOutputType","Output slot %d not defined for analysis task %s", islot, GetName());
329 return (output->GetType());
332 //______________________________________________________________________________
333 TObject *AliAnalysisTask::GetInputData(Int_t islot) const
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);
339 Error("GetInputData","Input slot %d not defined for analysis task %s", islot, GetName());
342 return (input->GetData());
345 //______________________________________________________________________________
346 TObject *AliAnalysisTask::GetOutputData(Int_t islot) const
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);
352 Error("GetOutputData","Input slot %d not defined for analysis task %s", islot, GetName());
355 return (output->GetData());
358 //______________________________________________________________________________
359 char *AliAnalysisTask::GetBranchAddress(Int_t islot, const char *branch) const
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);
367 //______________________________________________________________________________
368 Bool_t AliAnalysisTask::SetBranchAddress(Int_t islot, const char *branch, void *address) const
370 // Connect an object address to a branch of the specified input.
371 return GetInputSlot(islot)->SetBranchAddress(branch, address);
374 //______________________________________________________________________________
375 void AliAnalysisTask::ConnectInputData(Option_t *)
377 // Overload and connect your branches here.
380 //______________________________________________________________________________
381 void AliAnalysisTask::CreateOutputObjects()
383 // Overload and create your output objects here.
386 //______________________________________________________________________________
387 void AliAnalysisTask::Terminate(Option_t *)
389 // Method called by the framework at the end of data processing.
392 //______________________________________________________________________________
393 Bool_t AliAnalysisTask::PostData(Int_t iout, TObject *data, Option_t *option)
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.
400 AliAnalysisDataSlot *output = GetOutputSlot(iout);
402 Error("PostData","Output slot %i not defined for analysis task %s", iout, GetName());
405 if (!output->IsConnected()) {
406 Error("PostData","Output slot %i of analysis task %s not connected to any data container", iout, GetName());
410 fOutputReady = new Bool_t[fNoutputs];
411 memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
413 fOutputReady[iout] = kTRUE;
414 fPublishedData = data;
415 return (output->GetContainer()->SetData(data, option));
418 //______________________________________________________________________________
419 void AliAnalysisTask::SetUsed(Bool_t flag)
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);
432 //______________________________________________________________________________
433 Bool_t AliAnalysisTask::CheckCircularDeps()
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;
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;
450 //______________________________________________________________________________
451 void AliAnalysisTask::PrintTask(Option_t *option, Int_t indent) const
454 AliAnalysisTask *thistask = (AliAnalysisTask*)this;
457 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
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);
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");
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");
480 PrintContainers(option, indent+3);
483 //______________________________________________________________________________
484 void AliAnalysisTask::PrintContainers(Option_t *option, Int_t indent) const
486 // Print containers info.
487 AliAnalysisDataContainer *cont;
489 for (Int_t i=0; i<indent; i++) ind += " ";
491 for (islot=0; islot<fNoutputs; islot++) {
492 cont = GetOutputSlot(islot)->GetContainer();
493 cont->PrintContainer(option, indent);