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 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.
71 // The method CreateOutputObjects() has to be implemented an will contain the
72 // objects that should be created only once per session (e.g. output
75 // void MyAnalysisTask::CreateOutputObjects()
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);
86 // The method Terminate() will be called by the framework once at the end of
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:
91 //void MyAnalysisTask::Terminate(Option_t *)
93 // fhPt = (TH1F*)GetOutputData(0);
98 //==============================================================================
100 #include <Riostream.h>
105 #include "AliAnalysisTask.h"
106 #include "AliAnalysisDataSlot.h"
107 #include "AliAnalysisDataContainer.h"
108 #include "AliAnalysisManager.h"
110 ClassImp(AliAnalysisTask)
112 //______________________________________________________________________________
113 AliAnalysisTask::AliAnalysisTask()
115 fInitialized(kFALSE),
119 fPublishedData(NULL),
123 // Default constructor.
126 //______________________________________________________________________________
127 AliAnalysisTask::AliAnalysisTask(const char *name, const char *title)
130 fInitialized(kFALSE),
134 fPublishedData(NULL),
139 fInputs = new TObjArray(2);
140 fOutputs = new TObjArray(2);
143 //______________________________________________________________________________
144 AliAnalysisTask::AliAnalysisTask(const AliAnalysisTask &task)
147 fInitialized(task.fInitialized),
148 fNinputs(task.fNinputs),
149 fNoutputs(task.fNoutputs),
151 fPublishedData(NULL),
156 fInputs = new TObjArray((fNinputs)?fNinputs:2);
157 fOutputs = new TObjArray((fNoutputs)?fNoutputs:2);
160 for (i=0; i<fNinputs; i++) fInputs->AddAt(task.GetInputSlot(i),i);
161 fOutputReady = new Bool_t[(fNoutputs)?fNoutputs:2];
162 for (i=0; i<fNoutputs; i++) {
163 fOutputReady[i] = IsOutputReady(i);
164 fOutputs->AddAt(task.GetOutputSlot(i),i);
168 //______________________________________________________________________________
169 AliAnalysisTask::~AliAnalysisTask()
172 if (fTasks) fTasks->Clear();
173 if (fInputs) {fInputs->Delete(); delete fInputs;}
174 if (fOutputs) {fOutputs->Delete(); delete fOutputs;}
177 //______________________________________________________________________________
178 AliAnalysisTask& AliAnalysisTask::operator=(const AliAnalysisTask& task)
181 if (&task == this) return *this;
182 TTask::operator=(task);
183 fReady = task.IsReady();
184 fInitialized = task.IsInitialized();
185 fNinputs = task.GetNinputs();
186 fNoutputs = task.GetNoutputs();
187 fInputs = new TObjArray((fNinputs)?fNinputs:2);
188 fOutputs = new TObjArray((fNoutputs)?fNoutputs:2);
191 for (i=0; i<fNinputs; i++) fInputs->AddAt(new AliAnalysisDataSlot(*task.GetInputSlot(i)),i);
192 fOutputReady = new Bool_t[(fNoutputs)?fNoutputs:2];
193 for (i=0; i<fNoutputs; i++) {
194 fOutputReady[i] = IsOutputReady(i);
195 fOutputs->AddAt(new AliAnalysisDataSlot(*task.GetOutputSlot(i)),i);
200 //______________________________________________________________________________
201 Bool_t AliAnalysisTask::AreSlotsConnected()
203 // Check if all input/output slots are connected. If this is the case fReady=true
205 if (!fNinputs || !fNoutputs) return kFALSE;
207 AliAnalysisDataSlot *slot;
208 for (i=0; i<fNinputs; i++) {
209 slot = (AliAnalysisDataSlot*)fInputs->At(i);
211 Error("AreSlotsConnected", "Input slot %d of task %s not defined !",i,GetName());
214 if (!slot->IsConnected()) return kFALSE;
216 for (i=0; i<fNoutputs; i++) {
217 slot = (AliAnalysisDataSlot*)fOutputs->At(i);
219 Error("AreSlotsConnected", "Output slot %d of task %s not defined !",i,GetName());
222 if (!slot->IsConnected()) return kFALSE;
228 //______________________________________________________________________________
229 void AliAnalysisTask::CheckNotify(Bool_t init)
231 // Check if data is available from all inputs. Change the status of the task
232 // accordingly. This method is called automatically for all tasks connected
233 // to a container where the data was published.
234 if (init) fInitialized = kFALSE;
235 Bool_t single_shot = IsPostEventLoop();
236 AliAnalysisDataContainer *cinput;
237 for (Int_t islot=0; islot<fNinputs; islot++) {
238 cinput = GetInputSlot(islot)->GetContainer();
239 if (!cinput->GetData() || (single_shot && !cinput->IsPostEventLoop())) {
245 if (fInitialized) return;
246 TDirectory *cursav = gDirectory;
248 if (cursav) cursav->cd();
249 fInitialized = kTRUE;
252 //______________________________________________________________________________
253 Bool_t AliAnalysisTask::ConnectInput(Int_t islot, AliAnalysisDataContainer *cont)
255 // Connect an input slot to a data container.
256 AliAnalysisDataSlot *input = GetInputSlot(islot);
258 Error("ConnectInput","Input slot %i not defined for analysis task %s", islot, GetName());
261 // Check type matching
262 if (!input->GetType()->InheritsFrom(cont->GetType())) {
263 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());
266 // Connect the slot to the container as input
267 if (!input->ConnectContainer(cont)) return kFALSE;
268 // Add this to the list of container consumers
269 cont->AddConsumer(this, islot);
274 //______________________________________________________________________________
275 Bool_t AliAnalysisTask::ConnectOutput(Int_t islot, AliAnalysisDataContainer *cont)
277 // Connect an output slot to a data container.
278 AliAnalysisDataSlot *output = GetOutputSlot(islot);
280 Error("ConnectOutput","Output slot %i not defined for analysis task %s", islot, GetName());
283 // Check type matching
284 if (!output->GetType()->InheritsFrom(cont->GetType())) {
285 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());
288 // Connect the slot to the container as output
289 if (!output->ConnectContainer(cont)) return kFALSE;
290 // Set event loop type the same as for the task
291 cont->SetPostEventLoop(IsPostEventLoop());
292 // Declare this as the data producer
293 cont->SetProducer(this, islot);
298 //______________________________________________________________________________
299 void AliAnalysisTask::DefineInput(Int_t islot, TClass *type)
301 // Define an input slot and its type.
302 AliAnalysisDataSlot *input = new AliAnalysisDataSlot(type, this);
303 if (fNinputs<islot+1) fNinputs = islot+1;
304 fInputs->AddAtAndExpand(input, islot);
307 //______________________________________________________________________________
308 void AliAnalysisTask::DefineOutput(Int_t islot, TClass *type)
310 // Define an output slot and its type.
311 AliAnalysisDataSlot *output = new AliAnalysisDataSlot(type, this);
312 if (fNoutputs<islot+1) {
314 if (fOutputReady) delete [] fOutputReady;
315 fOutputReady = new Bool_t[fNoutputs];
316 memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
318 fOutputs->AddAtAndExpand(output, islot);
321 //______________________________________________________________________________
322 TClass *AliAnalysisTask::GetInputType(Int_t islot) const
324 // Retreive type of a given input slot.
325 AliAnalysisDataSlot *input = GetInputSlot(islot);
327 Error("GetInputType","Input slot %d not defined for analysis task %s", islot, GetName());
330 return (input->GetType());
333 //______________________________________________________________________________
334 TClass *AliAnalysisTask::GetOutputType(Int_t islot) const
336 // Retreive type of a given output slot.
337 AliAnalysisDataSlot *output = GetOutputSlot(islot);
339 Error("GetOutputType","Output slot %d not defined for analysis task %s", islot, GetName());
342 return (output->GetType());
345 //______________________________________________________________________________
346 TObject *AliAnalysisTask::GetInputData(Int_t islot) const
348 // Retreive input data for a slot if ready. Normally called by Exec() and
349 // the object has to be statically cast to the appropriate type.
350 AliAnalysisDataSlot *input = GetInputSlot(islot);
352 Error("GetInputData","Input slot %d not defined for analysis task %s", islot, GetName());
355 return (input->GetData());
358 //______________________________________________________________________________
359 TObject *AliAnalysisTask::GetOutputData(Int_t islot) const
361 // Retreive output data for a slot. Normally called in UserTask::Terminate to
362 // get a valid pointer to data even in case of Proof.
363 AliAnalysisDataSlot *output = GetOutputSlot(islot);
365 Error("GetOutputData","Input slot %d not defined for analysis task %s", islot, GetName());
368 return (output->GetData());
371 //______________________________________________________________________________
372 char *AliAnalysisTask::GetBranchAddress(Int_t islot, const char *branch) const
374 // Check if a branch with a given name from the specified input is connected
375 // to some address. Call this in Init() before trying to call SetBranchAddress()
376 // since the adress may be set by other task.
377 return (char *)GetInputSlot(islot)->GetBranchAddress(branch);
380 //______________________________________________________________________________
381 Bool_t AliAnalysisTask::SetBranchAddress(Int_t islot, const char *branch, void *address) const
383 // Connect an object address to a branch of the specified input.
384 return GetInputSlot(islot)->SetBranchAddress(branch, address);
387 //______________________________________________________________________________
388 void AliAnalysisTask::EnableBranch(Int_t islot, const char *bname) const
390 // Call this in ConnectInputData() to enable only the branches needed by this
391 // task. "*" will enable everything.
392 AliAnalysisDataSlot *input = GetInputSlot(islot);
393 if (!input || !input->GetType()->InheritsFrom(TTree::Class())) {
394 Error("EnableBranch", "Wrong slot type #%d for task %s: not TTree-derived type", islot, GetName());
397 TTree *tree = (TTree*)input->GetData();
398 if (!strcmp(bname, "*")) {
399 tree->SetBranchStatus("*",1);
402 AliAnalysisDataSlot::EnableBranch(bname, tree);
405 //______________________________________________________________________________
406 void AliAnalysisTask::ConnectInputData(Option_t *)
408 // Overload and connect your branches here.
411 //______________________________________________________________________________
412 void AliAnalysisTask::LocalInit()
414 // The method LocalInit() may be implemented to call locally (on the client)
415 // all initialization methods of the class. It is not mandatory and was created
416 // in order to minimize the complexity and readability of the analysis macro.
417 // DO NOT create in this method the histigrams or task output objects that will
418 // go in the task output containers. Use CreateOutputObjects for that.
421 //______________________________________________________________________________
422 void AliAnalysisTask::CreateOutputObjects()
424 // Called once per task either in PROOF or local mode. Overload to put some
425 // task initialization and/or create your output objects here.
428 //______________________________________________________________________________
429 void AliAnalysisTask::OpenFile(Int_t iout, Option_t *option) const
431 // This method has to be called INSIDE the user redefined CreateOutputObjects
432 // method, before creating each object corresponding to the output containers
433 // that are to be written to a file. This need to be done in general for the big output
434 // objects that may not fit memory during processing.
435 // - 'option' is the file opening option.
436 //=========================================================================
437 // NOTE !: The method call will be ignored in PROOF mode, in which case the
438 // results have to be streamed back to the client and written just before Terminate()
439 //=========================================================================
442 // void MyAnaTask::CreateOutputObjects() {
443 // OpenFile(0); // Will open the file for the object to be written at output #0
444 // fAOD = new TTree("AOD for D0toKPi");
446 // now some histos that should go in the file of the second output container
447 // fHist1 = new TH1F("my quality check hist1",...);
448 // fHist2 = new TH2F("my quality check hist2",...);
451 if (iout<0 || iout>=fNoutputs) return;
452 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
453 if (!mgr || mgr->GetAnalysisType()==AliAnalysisManager::kProofAnalysis) return;
454 AliAnalysisDataContainer *cont = GetOutputSlot(iout)->GetContainer();
455 if (strlen(cont->GetFileName())) new TFile(cont->GetFileName(), option);
458 //______________________________________________________________________________
459 Bool_t AliAnalysisTask::Notify()
461 // Overload this IF you need to treat input file change.
465 //______________________________________________________________________________
466 void AliAnalysisTask::Terminate(Option_t *)
468 // Method called by the framework at the end of data processing.
471 //______________________________________________________________________________
472 Bool_t AliAnalysisTask::PostData(Int_t iout, TObject *data, Option_t *option)
474 // Post output data for a given ouput slot in the corresponding data container.
475 // Published data becomes owned by the data container.
476 // If option is specified, the container connected to the output slot must have
477 // an associated file name defined. The option represents the method to open the file.
479 AliAnalysisDataSlot *output = GetOutputSlot(iout);
481 Error("PostData","Output slot %i not defined for analysis task %s", iout, GetName());
484 if (!output->IsConnected()) {
485 Error("PostData","Output slot %i of analysis task %s not connected to any data container", iout, GetName());
489 fOutputReady = new Bool_t[fNoutputs];
490 memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
492 fOutputReady[iout] = kTRUE;
493 fPublishedData = data;
494 return (output->GetContainer()->SetData(data, option));
497 //______________________________________________________________________________
498 void AliAnalysisTask::SetUsed(Bool_t flag)
500 // Set 'used' flag recursively to task and all daughter tasks.
501 if (TestBit(kTaskUsed)==flag) return;
502 TObject::SetBit(kTaskUsed,flag);
503 Int_t nd = fTasks->GetSize();
504 AliAnalysisTask *task;
505 for (Int_t i=0; i<nd; i++) {
506 task = (AliAnalysisTask*)fTasks->At(i);
511 //______________________________________________________________________________
512 Bool_t AliAnalysisTask::CheckCircularDeps()
514 // Check for illegal circular dependencies, e.g. a daughter task should not have
515 // a hierarchical parent as subtask.
516 if (IsChecked()) return kTRUE;
518 TList *tasks = GetListOfTasks();
519 Int_t ntasks = tasks->GetSize();
520 AliAnalysisTask *task;
521 for (Int_t i=0; i<ntasks; i++) {
522 task = (AliAnalysisTask*)tasks->At(i);
523 if (task->CheckCircularDeps()) return kTRUE;
529 //______________________________________________________________________________
530 void AliAnalysisTask::PrintTask(Option_t *option, Int_t indent) const
533 AliAnalysisTask *thistask = (AliAnalysisTask*)this;
536 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
539 AliAnalysisDataContainer *cont;
540 for (Int_t i=0; i<indent; i++) ind += " ";
541 if (!dep || (dep && IsChecked())) {
542 printf("%s\n", Form("%stask: %s ACTIVE=%i POST_LOOP=%i", ind.Data(), GetName(),IsActive(),IsPostEventLoop()));
543 if (dep) thistask->SetChecked(kFALSE);
545 for (islot=0; islot<fNinputs; islot++) {
546 printf("%s", Form("%s INPUT #%i: %s <- ",ind.Data(),islot, GetInputType(islot)->GetName()));
547 cont = GetInputSlot(islot)->GetContainer();
548 if (cont) printf(" [%s]\n", cont->GetName());
549 else printf(" [NO CONTAINER]\n");
551 for (islot=0; islot<fNoutputs; islot++) {
552 printf("%s", Form("%s OUTPUT #%i: %s -> ",ind.Data(),islot, GetOutputType(islot)->GetName()));
553 cont = GetOutputSlot(islot)->GetContainer();
554 if (cont) printf(" [%s]\n", cont->GetName());
555 else printf(" [NO CONTAINER]\n");
559 PrintContainers(option, indent+3);
562 //______________________________________________________________________________
563 void AliAnalysisTask::PrintContainers(Option_t *option, Int_t indent) const
565 // Print containers info.
566 AliAnalysisDataContainer *cont;
568 for (Int_t i=0; i<indent; i++) ind += " ";
570 for (islot=0; islot<fNoutputs; islot++) {
571 cont = GetOutputSlot(islot)->GetContainer();
572 if (cont) cont->PrintContainer(option, indent);
576 //______________________________________________________________________________
577 void AliAnalysisTask::SetPostEventLoop(Bool_t flag)
579 // Set the task execution mode - run after event loop or not. All output
580 // containers of this task will get the same type.
581 TObject::SetBit(kTaskPostEventLoop,flag);
582 AliAnalysisDataContainer *cont;
584 for (islot=0; islot<fNoutputs; islot++) {
585 cont = GetOutputSlot(islot)->GetContainer();
586 if (cont) cont->SetPostEventLoop(flag);