]> git.uio.no Git - u/mrichter/AliRoot.git/blame - ANALYSIS/AliAnalysisTask.cxx
In SetNofManusPerModule(): return false if no action
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTask.cxx
CommitLineData
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//
9b33830a 65// The method LocalInit() may be implemented to call locally (on the client)
66// all initialization methods of the class. It is not mandatory and was created
67// in order to minimize the complexity and readability of the analysis macro.
68// DO NOT create in this method the histigrams or task output objects that will
69// go in the task output containers. Use CreateOutputObjects for that.
70//
71// The method CreateOutputObjects() has to be implemented an will contain the
c52c2132 72// objects that should be created only once per session (e.g. output
73// histograms)
74//
75// void MyAnalysisTask::CreateOutputObjects()
76//{
77 // create histograms
78// fhPt = new TH1F("fhPt","This is the Pt distribution",15,0.1,3.1);
79// fhPt->SetStats(kTRUE);
80// fhPt->GetXaxis()->SetTitle("P_{T} [GeV]");
81// fhPt->GetYaxis()->SetTitle("#frac{dN}{dP_{T}}");
82// fhPt->GetXaxis()->SetTitleColor(1);
83// fhPt->SetMarkerStyle(kFullCircle);
327eaf46 84// }
85//
86// The method Terminate() will be called by the framework once at the end of
c52c2132 87// data processing. Overload this if needed. DO NOT ASSUME that the pointers
88// to histograms defined in CreateOutputObjects() are valid, since this is
89// not true in case of PROOF. Restore the pointer values like:
90//
91//void MyAnalysisTask::Terminate(Option_t *)
92//{
93// fhPt = (TH1F*)GetOutputData(0);
94// ...
95//}
96
327eaf46 97//
d3106602 98//==============================================================================
99
0b28fd57 100#include <Riostream.h>
c5a87c56 101#include <TFile.h>
c52c2132 102#include <TClass.h>
d3106602 103
d3106602 104#include "AliAnalysisTask.h"
105#include "AliAnalysisDataSlot.h"
106#include "AliAnalysisDataContainer.h"
c5a87c56 107#include "AliAnalysisManager.h"
d3106602 108
109ClassImp(AliAnalysisTask)
110
111//______________________________________________________________________________
112AliAnalysisTask::AliAnalysisTask()
37a26056 113 :fReady(kFALSE),
327eaf46 114 fInitialized(kFALSE),
37a26056 115 fNinputs(0),
116 fNoutputs(0),
117 fOutputReady(NULL),
118 fPublishedData(NULL),
119 fInputs(NULL),
120 fOutputs(NULL)
d3106602 121{
122// Default constructor.
c5a87c56 123 TObject::SetBit(kTaskEvtByEvt, kTRUE);
d3106602 124}
125
126//______________________________________________________________________________
127AliAnalysisTask::AliAnalysisTask(const char *name, const char *title)
37a26056 128 :TTask(name,title),
129 fReady(kFALSE),
327eaf46 130 fInitialized(kFALSE),
37a26056 131 fNinputs(0),
132 fNoutputs(0),
133 fOutputReady(NULL),
134 fPublishedData(NULL),
135 fInputs(NULL),
136 fOutputs(NULL)
d3106602 137{
37a26056 138// Constructor.
c5a87c56 139 TObject::SetBit(kTaskEvtByEvt, kTRUE);
d3106602 140 fInputs = new TObjArray(2);
141 fOutputs = new TObjArray(2);
142}
143
144//______________________________________________________________________________
145AliAnalysisTask::AliAnalysisTask(const AliAnalysisTask &task)
37a26056 146 :TTask(task),
147 fReady(task.fReady),
327eaf46 148 fInitialized(task.fInitialized),
37a26056 149 fNinputs(task.fNinputs),
150 fNoutputs(task.fNoutputs),
151 fOutputReady(NULL),
152 fPublishedData(NULL),
153 fInputs(NULL),
154 fOutputs(NULL)
d3106602 155{
156// Copy ctor.
d3106602 157 fInputs = new TObjArray((fNinputs)?fNinputs:2);
158 fOutputs = new TObjArray((fNoutputs)?fNoutputs:2);
159 fPublishedData = 0;
160 Int_t i;
161 for (i=0; i<fNinputs; i++) fInputs->AddAt(task.GetInputSlot(i),i);
162 fOutputReady = new Bool_t[(fNoutputs)?fNoutputs:2];
163 for (i=0; i<fNoutputs; i++) {
164 fOutputReady[i] = IsOutputReady(i);
165 fOutputs->AddAt(task.GetOutputSlot(i),i);
166 }
167}
168
169//______________________________________________________________________________
170AliAnalysisTask::~AliAnalysisTask()
171{
172// Dtor.
c52c2132 173 if (fTasks) fTasks->Clear();
d3106602 174 if (fInputs) {fInputs->Delete(); delete fInputs;}
175 if (fOutputs) {fOutputs->Delete(); delete fOutputs;}
176}
177
178//______________________________________________________________________________
179AliAnalysisTask& AliAnalysisTask::operator=(const AliAnalysisTask& task)
180{
181// Assignment
37a26056 182 if (&task == this) return *this;
183 TTask::operator=(task);
184 fReady = task.IsReady();
327eaf46 185 fInitialized = task.IsInitialized();
37a26056 186 fNinputs = task.GetNinputs();
187 fNoutputs = task.GetNoutputs();
188 fInputs = new TObjArray((fNinputs)?fNinputs:2);
189 fOutputs = new TObjArray((fNoutputs)?fNoutputs:2);
190 fPublishedData = 0;
191 Int_t i;
192 for (i=0; i<fNinputs; i++) fInputs->AddAt(new AliAnalysisDataSlot(*task.GetInputSlot(i)),i);
193 fOutputReady = new Bool_t[(fNoutputs)?fNoutputs:2];
194 for (i=0; i<fNoutputs; i++) {
195 fOutputReady[i] = IsOutputReady(i);
196 fOutputs->AddAt(new AliAnalysisDataSlot(*task.GetOutputSlot(i)),i);
197 }
d3106602 198 return *this;
199}
200
201//______________________________________________________________________________
202Bool_t AliAnalysisTask::AreSlotsConnected()
203{
204// Check if all input/output slots are connected. If this is the case fReady=true
205 fReady = kFALSE;
206 if (!fNinputs || !fNoutputs) return kFALSE;
207 Int_t i;
208 AliAnalysisDataSlot *slot;
209 for (i=0; i<fNinputs; i++) {
210 slot = (AliAnalysisDataSlot*)fInputs->At(i);
211 if (!slot) {
c52c2132 212 Error("AreSlotsConnected", "Input slot %d of task %s not defined !",i,GetName());
d3106602 213 return kFALSE;
214 }
215 if (!slot->IsConnected()) return kFALSE;
216 }
217 for (i=0; i<fNoutputs; i++) {
218 slot = (AliAnalysisDataSlot*)fOutputs->At(i);
219 if (!slot) {
c52c2132 220 Error("AreSlotsConnected", "Output slot %d of task %s not defined !",i,GetName());
d3106602 221 return kFALSE;
222 }
223 if (!slot->IsConnected()) return kFALSE;
224 }
225 fReady = kTRUE;
226 return kTRUE;
227}
228
229//______________________________________________________________________________
327eaf46 230void AliAnalysisTask::CheckNotify(Bool_t init)
d3106602 231{
232// Check if data is available from all inputs. Change the status of the task
233// accordingly. This method is called automatically for all tasks connected
234// to a container where the data was published.
327eaf46 235 if (init) fInitialized = kFALSE;
d3106602 236 for (Int_t islot=0; islot<fNinputs; islot++) {
237 if (!GetInputData(islot)) {
238 SetActive(kFALSE);
239 return;
240 }
241 }
242 SetActive(kTRUE);
327eaf46 243 if (fInitialized) return;
244 TDirectory *cursav = gDirectory;
c52c2132 245 ConnectInputData();
327eaf46 246 if (cursav) cursav->cd();
247 fInitialized = kTRUE;
d3106602 248}
249
250//______________________________________________________________________________
251Bool_t AliAnalysisTask::ConnectInput(Int_t islot, AliAnalysisDataContainer *cont)
252{
253// Connect an input slot to a data container.
254 AliAnalysisDataSlot *input = GetInputSlot(islot);
255 if (!input) {
c52c2132 256 Error("ConnectInput","Input slot %i not defined for analysis task %s", islot, GetName());
d3106602 257 return kFALSE;
258 }
259 // Check type matching
260 if (!input->GetType()->InheritsFrom(cont->GetType())) {
c52c2132 261 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 262 return kFALSE;
263 }
264 // Connect the slot to the container as input
265 if (!input->ConnectContainer(cont)) return kFALSE;
266 // Add this to the list of container consumers
267 cont->AddConsumer(this, islot);
268 AreSlotsConnected();
269 return kTRUE;
270}
271
272//______________________________________________________________________________
273Bool_t AliAnalysisTask::ConnectOutput(Int_t islot, AliAnalysisDataContainer *cont)
274{
275// Connect an output slot to a data container.
276 AliAnalysisDataSlot *output = GetOutputSlot(islot);
277 if (!output) {
c52c2132 278 Error("ConnectOutput","Output slot %i not defined for analysis task %s", islot, GetName());
d3106602 279 return kFALSE;
280 }
281 // Check type matching
282 if (!output->GetType()->InheritsFrom(cont->GetType())) {
c52c2132 283 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 284 return kFALSE;
285 }
286 // Connect the slot to the container as output
287 if (!output->ConnectContainer(cont)) return kFALSE;
288 // Declare this as the data producer
289 cont->SetProducer(this, islot);
290 AreSlotsConnected();
291 return kTRUE;
292}
293
294//______________________________________________________________________________
295void AliAnalysisTask::DefineInput(Int_t islot, TClass *type)
296{
297// Define an input slot and its type.
298 AliAnalysisDataSlot *input = new AliAnalysisDataSlot(type, this);
299 if (fNinputs<islot+1) fNinputs = islot+1;
6ae18197 300 fInputs->AddAtAndExpand(input, islot);
d3106602 301}
302
303//______________________________________________________________________________
304void AliAnalysisTask::DefineOutput(Int_t islot, TClass *type)
305{
306// Define an output slot and its type.
d3106602 307 AliAnalysisDataSlot *output = new AliAnalysisDataSlot(type, this);
308 if (fNoutputs<islot+1) {
309 fNoutputs = islot+1;
310 if (fOutputReady) delete [] fOutputReady;
311 fOutputReady = new Bool_t[fNoutputs];
312 memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
313 }
6ae18197 314 fOutputs->AddAtAndExpand(output, islot);
d3106602 315}
316
317//______________________________________________________________________________
318TClass *AliAnalysisTask::GetInputType(Int_t islot) const
319{
320// Retreive type of a given input slot.
321 AliAnalysisDataSlot *input = GetInputSlot(islot);
322 if (!input) {
c52c2132 323 Error("GetInputType","Input slot %d not defined for analysis task %s", islot, GetName());
d3106602 324 return NULL;
325 }
326 return (input->GetType());
327}
328
329//______________________________________________________________________________
330TClass *AliAnalysisTask::GetOutputType(Int_t islot) const
331{
332// Retreive type of a given output slot.
333 AliAnalysisDataSlot *output = GetOutputSlot(islot);
334 if (!output) {
c52c2132 335 Error("GetOutputType","Output slot %d not defined for analysis task %s", islot, GetName());
d3106602 336 return NULL;
337 }
338 return (output->GetType());
339}
340
341//______________________________________________________________________________
342TObject *AliAnalysisTask::GetInputData(Int_t islot) const
343{
344// Retreive input data for a slot if ready. Normally called by Exec() and
345// the object has to be statically cast to the appropriate type.
346 AliAnalysisDataSlot *input = GetInputSlot(islot);
347 if (!input) {
c52c2132 348 Error("GetInputData","Input slot %d not defined for analysis task %s", islot, GetName());
d3106602 349 return NULL;
350 }
351 return (input->GetData());
352}
353
c52c2132 354//______________________________________________________________________________
355TObject *AliAnalysisTask::GetOutputData(Int_t islot) const
356{
357// Retreive output data for a slot. Normally called in UserTask::Terminate to
358// get a valid pointer to data even in case of Proof.
359 AliAnalysisDataSlot *output = GetOutputSlot(islot);
360 if (!output) {
361 Error("GetOutputData","Input slot %d not defined for analysis task %s", islot, GetName());
362 return NULL;
363 }
364 return (output->GetData());
365}
366
327eaf46 367//______________________________________________________________________________
368char *AliAnalysisTask::GetBranchAddress(Int_t islot, const char *branch) const
369{
370// Check if a branch with a given name from the specified input is connected
371// to some address. Call this in Init() before trying to call SetBranchAddress()
372// since the adress may be set by other task.
373 return (char *)GetInputSlot(islot)->GetBranchAddress(branch);
374}
375
376//______________________________________________________________________________
377Bool_t AliAnalysisTask::SetBranchAddress(Int_t islot, const char *branch, void *address) const
378{
379// Connect an object address to a branch of the specified input.
380 return GetInputSlot(islot)->SetBranchAddress(branch, address);
381}
382
383//______________________________________________________________________________
c52c2132 384void AliAnalysisTask::ConnectInputData(Option_t *)
327eaf46 385{
c52c2132 386// Overload and connect your branches here.
327eaf46 387}
388
9b33830a 389//______________________________________________________________________________
390void AliAnalysisTask::LocalInit()
391{
392// The method LocalInit() may be implemented to call locally (on the client)
393// all initialization methods of the class. It is not mandatory and was created
394// in order to minimize the complexity and readability of the analysis macro.
395// DO NOT create in this method the histigrams or task output objects that will
396// go in the task output containers. Use CreateOutputObjects for that.
397}
398
327eaf46 399//______________________________________________________________________________
c52c2132 400void AliAnalysisTask::CreateOutputObjects()
327eaf46 401{
981f2614 402// Called once per task either in PROOF or local mode. Overload to put some
403// task initialization and/or create your output objects here.
404}
405
c5a87c56 406//______________________________________________________________________________
407void AliAnalysisTask::OpenFile(Int_t iout, Option_t *option) const
408{
409// This method has to be called INSIDE the user redefined CreateOutputObjects
410// method, before creating each object corresponding to the output containers
411// that are to be written to a file. This need to be done in general for the big output
412// objects that may not fit memory during processing.
413// - 'option' is the file opening option.
414//=========================================================================
415// NOTE !: The method call will be ignored in PROOF mode, in which case the
416// results have to be streamed back to the client and written just before Terminate()
417//=========================================================================
418//
419// Example:
420// void MyAnaTask::CreateOutputObjects() {
421// OpenFile(0); // Will open the file for the object to be written at output #0
422// fAOD = new TTree("AOD for D0toKPi");
423// OpenFile(1);
424// now some histos that should go in the file of the second output container
425// fHist1 = new TH1F("my quality check hist1",...);
426// fHist2 = new TH2F("my quality check hist2",...);
427// }
428
429 if (iout<0 || iout>=fNoutputs) return;
430 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
431 if (!mgr || mgr->GetAnalysisType()==AliAnalysisManager::kProofAnalysis) return;
432 AliAnalysisDataContainer *cont = GetOutputSlot(iout)->GetContainer();
433 if (strlen(cont->GetFileName())) new TFile(cont->GetFileName(), option);
434}
435
981f2614 436//______________________________________________________________________________
437Bool_t AliAnalysisTask::Notify()
438{
439// Overload this IF you need to treat input file change.
440 return kTRUE;
327eaf46 441}
442
443//______________________________________________________________________________
c52c2132 444void AliAnalysisTask::Terminate(Option_t *)
327eaf46 445{
c52c2132 446// Method called by the framework at the end of data processing.
447}
327eaf46 448
d3106602 449//______________________________________________________________________________
450Bool_t AliAnalysisTask::PostData(Int_t iout, TObject *data, Option_t *option)
451{
452// Post output data for a given ouput slot in the corresponding data container.
453// Published data becomes owned by the data container.
454// If option is specified, the container connected to the output slot must have
455// an associated file name defined. The option represents the method to open the file.
456 fPublishedData = 0;
457 AliAnalysisDataSlot *output = GetOutputSlot(iout);
458 if (!output) {
c52c2132 459 Error("PostData","Output slot %i not defined for analysis task %s", iout, GetName());
d3106602 460 return kFALSE;
461 }
462 if (!output->IsConnected()) {
c52c2132 463 Error("PostData","Output slot %i of analysis task %s not connected to any data container", iout, GetName());
d3106602 464 return kFALSE;
465 }
466 if (!fOutputReady) {
467 fOutputReady = new Bool_t[fNoutputs];
468 memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
469 }
470 fOutputReady[iout] = kTRUE;
471 fPublishedData = data;
472 return (output->GetContainer()->SetData(data, option));
473}
474
475//______________________________________________________________________________
476void AliAnalysisTask::SetUsed(Bool_t flag)
477{
478// Set 'used' flag recursively to task and all daughter tasks.
479 if (TestBit(kTaskUsed)==flag) return;
480 TObject::SetBit(kTaskUsed,flag);
481 Int_t nd = fTasks->GetSize();
482 AliAnalysisTask *task;
483 for (Int_t i=0; i<nd; i++) {
484 task = (AliAnalysisTask*)fTasks->At(i);
485 task->SetUsed(flag);
486 }
487}
488
489//______________________________________________________________________________
490Bool_t AliAnalysisTask::CheckCircularDeps()
491{
492// Check for illegal circular dependencies, e.g. a daughter task should not have
493// a hierarchical parent as subtask.
494 if (IsChecked()) return kTRUE;
495 SetChecked();
496 TList *tasks = GetListOfTasks();
497 Int_t ntasks = tasks->GetSize();
498 AliAnalysisTask *task;
499 for (Int_t i=0; i<ntasks; i++) {
500 task = (AliAnalysisTask*)tasks->At(i);
501 if (task->CheckCircularDeps()) return kTRUE;
502 }
503 SetChecked(kFALSE);
504 return kFALSE;
505}
506
507//______________________________________________________________________________
508void AliAnalysisTask::PrintTask(Option_t *option, Int_t indent) const
509{
510// Print task info.
511 AliAnalysisTask *thistask = (AliAnalysisTask*)this;
512 TString opt(option);
513 opt.ToLower();
514 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
515 TString ind;
516 Int_t islot;
517 AliAnalysisDataContainer *cont;
518 for (Int_t i=0; i<indent; i++) ind += " ";
519 if (!dep || (dep && IsChecked())) {
520 printf("%s\n", Form("%stask: %s ACTIVE=%i", ind.Data(), GetName(),IsActive()));
521 if (dep) thistask->SetChecked(kFALSE);
522 else {
523 for (islot=0; islot<fNinputs; islot++) {
524 printf("%s", Form("%s INPUT #%i: %s <- ",ind.Data(),islot, GetInputType(islot)->GetName()));
525 cont = GetInputSlot(islot)->GetContainer();
526 if (cont) printf(" [%s]\n", cont->GetName());
527 else printf(" [NO CONTAINER]\n");
528 }
529 for (islot=0; islot<fNoutputs; islot++) {
530 printf("%s", Form("%s OUTPUT #%i: %s -> ",ind.Data(),islot, GetOutputType(islot)->GetName()));
531 cont = GetOutputSlot(islot)->GetContainer();
532 if (cont) printf(" [%s]\n", cont->GetName());
533 else printf(" [NO CONTAINER]\n");
534 }
535 }
536 }
537 PrintContainers(option, indent+3);
538}
539
540//______________________________________________________________________________
541void AliAnalysisTask::PrintContainers(Option_t *option, Int_t indent) const
542{
543// Print containers info.
544 AliAnalysisDataContainer *cont;
545 TString ind;
546 for (Int_t i=0; i<indent; i++) ind += " ";
547 Int_t islot;
548 for (islot=0; islot<fNoutputs; islot++) {
549 cont = GetOutputSlot(islot)->GetContainer();
550 cont->PrintContainer(option, indent);
551 }
552}