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>
106 #include "AliAnalysisTask.h"
107 #include "AliAnalysisDataSlot.h"
108 #include "AliAnalysisDataContainer.h"
109 #include "AliAnalysisManager.h"
111 ClassImp(AliAnalysisTask)
113 //______________________________________________________________________________
114 AliAnalysisTask::AliAnalysisTask()
116 fInitialized(kFALSE),
120 fPublishedData(NULL),
124 // Default constructor.
127 //______________________________________________________________________________
128 AliAnalysisTask::AliAnalysisTask(const char *name, const char *title)
131 fInitialized(kFALSE),
135 fPublishedData(NULL),
140 fInputs = new TObjArray(2);
141 fOutputs = new TObjArray(2);
144 //______________________________________________________________________________
145 AliAnalysisTask::AliAnalysisTask(const AliAnalysisTask &task)
148 fInitialized(task.fInitialized),
149 fNinputs(task.fNinputs),
150 fNoutputs(task.fNoutputs),
152 fPublishedData(NULL),
157 fInputs = new TObjArray((fNinputs)?fNinputs:2);
158 fOutputs = new TObjArray((fNoutputs)?fNoutputs:2);
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);
169 //______________________________________________________________________________
170 AliAnalysisTask::~AliAnalysisTask()
173 if (fTasks) fTasks->Clear();
174 if (fInputs) {fInputs->Delete(); delete fInputs;}
175 if (fOutputs) {fOutputs->Delete(); delete fOutputs;}
178 //______________________________________________________________________________
179 AliAnalysisTask& AliAnalysisTask::operator=(const AliAnalysisTask& task)
182 if (&task == this) return *this;
183 TTask::operator=(task);
184 fReady = task.IsReady();
185 fInitialized = task.IsInitialized();
186 fNinputs = task.GetNinputs();
187 fNoutputs = task.GetNoutputs();
188 fInputs = new TObjArray((fNinputs)?fNinputs:2);
189 fOutputs = new TObjArray((fNoutputs)?fNoutputs:2);
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);
201 //______________________________________________________________________________
202 Bool_t AliAnalysisTask::AreSlotsConnected()
204 // Check if all input/output slots are connected. If this is the case fReady=true
206 if (!fNinputs || !fNoutputs) return kFALSE;
208 AliAnalysisDataSlot *slot;
209 for (i=0; i<fNinputs; i++) {
210 slot = (AliAnalysisDataSlot*)fInputs->At(i);
212 Error("AreSlotsConnected", "Input slot %d of task %s not defined !",i,GetName());
215 if (!slot->IsConnected()) return kFALSE;
217 for (i=0; i<fNoutputs; i++) {
218 slot = (AliAnalysisDataSlot*)fOutputs->At(i);
220 Error("AreSlotsConnected", "Output slot %d of task %s not defined !",i,GetName());
223 if (!slot->IsConnected()) return kFALSE;
229 //______________________________________________________________________________
230 void AliAnalysisTask::CheckNotify(Bool_t init)
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.
235 if (init) fInitialized = kFALSE;
236 Bool_t single_shot = IsPostEventLoop();
237 AliAnalysisDataContainer *cinput;
238 for (Int_t islot=0; islot<fNinputs; islot++) {
239 cinput = GetInputSlot(islot)->GetContainer();
240 if (!cinput->GetData() || (single_shot && !cinput->IsPostEventLoop())) {
246 if (fInitialized) return;
247 TDirectory *cursav = gDirectory;
249 if (cursav) cursav->cd();
250 fInitialized = kTRUE;
253 //______________________________________________________________________________
254 Bool_t AliAnalysisTask::ConnectInput(Int_t islot, AliAnalysisDataContainer *cont)
256 // Connect an input slot to a data container.
257 AliAnalysisDataSlot *input = GetInputSlot(islot);
259 Error("ConnectInput","Input slot %i not defined for analysis task %s", islot, GetName());
262 // Check type matching
263 if (!input->GetType()->InheritsFrom(cont->GetType())) {
264 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());
267 // Connect the slot to the container as input
268 if (!input->ConnectContainer(cont)) return kFALSE;
269 // Add this to the list of container consumers
270 cont->AddConsumer(this, islot);
275 //______________________________________________________________________________
276 Bool_t AliAnalysisTask::ConnectOutput(Int_t islot, AliAnalysisDataContainer *cont)
278 // Connect an output slot to a data container.
279 AliAnalysisDataSlot *output = GetOutputSlot(islot);
281 Error("ConnectOutput","Output slot %i not defined for analysis task %s", islot, GetName());
284 // Check type matching
285 if (!output->GetType()->InheritsFrom(cont->GetType())) {
286 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());
289 // Connect the slot to the container as output
290 if (!output->ConnectContainer(cont)) return kFALSE;
291 // Set event loop type the same as for the task
292 cont->SetPostEventLoop(IsPostEventLoop());
293 // Declare this as the data producer
294 cont->SetProducer(this, islot);
299 //______________________________________________________________________________
300 void AliAnalysisTask::DefineInput(Int_t islot, TClass *type)
302 // Define an input slot and its type.
303 AliAnalysisDataSlot *input = new AliAnalysisDataSlot(type, this);
304 if (fNinputs<islot+1) fNinputs = islot+1;
305 fInputs->AddAtAndExpand(input, islot);
308 //______________________________________________________________________________
309 void AliAnalysisTask::DefineOutput(Int_t islot, TClass *type)
311 // Define an output slot and its type.
312 AliAnalysisDataSlot *output = new AliAnalysisDataSlot(type, this);
313 if (fNoutputs<islot+1) {
315 if (fOutputReady) delete [] fOutputReady;
316 fOutputReady = new Bool_t[fNoutputs];
317 memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
319 fOutputs->AddAtAndExpand(output, islot);
322 //______________________________________________________________________________
323 TClass *AliAnalysisTask::GetInputType(Int_t islot) const
325 // Retreive type of a given input slot.
326 AliAnalysisDataSlot *input = GetInputSlot(islot);
328 Error("GetInputType","Input slot %d not defined for analysis task %s", islot, GetName());
331 return (input->GetType());
334 //______________________________________________________________________________
335 TClass *AliAnalysisTask::GetOutputType(Int_t islot) const
337 // Retreive type of a given output slot.
338 AliAnalysisDataSlot *output = GetOutputSlot(islot);
340 Error("GetOutputType","Output slot %d not defined for analysis task %s", islot, GetName());
343 return (output->GetType());
346 //______________________________________________________________________________
347 TObject *AliAnalysisTask::GetInputData(Int_t islot) const
349 // Retreive input data for a slot if ready. Normally called by Exec() and
350 // the object has to be statically cast to the appropriate type.
351 AliAnalysisDataSlot *input = GetInputSlot(islot);
353 Error("GetInputData","Input slot %d not defined for analysis task %s", islot, GetName());
356 return (input->GetData());
359 //______________________________________________________________________________
360 TObject *AliAnalysisTask::GetOutputData(Int_t islot) const
362 // Retreive output data for a slot. Normally called in UserTask::Terminate to
363 // get a valid pointer to data even in case of Proof.
364 AliAnalysisDataSlot *output = GetOutputSlot(islot);
366 Error("GetOutputData","Input slot %d not defined for analysis task %s", islot, GetName());
369 return (output->GetData());
372 //______________________________________________________________________________
373 char *AliAnalysisTask::GetBranchAddress(Int_t islot, const char *branch) const
375 // Check if a branch with a given name from the specified input is connected
376 // to some address. Call this in Init() before trying to call SetBranchAddress()
377 // since the adress may be set by other task.
378 return (char *)GetInputSlot(islot)->GetBranchAddress(branch);
381 //______________________________________________________________________________
382 Bool_t AliAnalysisTask::SetBranchAddress(Int_t islot, const char *branch, void *address) const
384 // Connect an object address to a branch of the specified input.
385 return GetInputSlot(islot)->SetBranchAddress(branch, address);
388 //______________________________________________________________________________
389 void AliAnalysisTask::EnableBranch(Int_t islot, const char *bname) const
391 // Call this in ConnectInputData() to enable only the branches needed by this
392 // task. "*" will enable everything.
393 AliAnalysisDataSlot *input = GetInputSlot(islot);
394 if (!input || !input->GetType()->InheritsFrom(TTree::Class())) {
395 Error("EnableBranch", "Wrong slot type #%d for task %s: not TTree-derived type", islot, GetName());
398 TTree *tree = (TTree*)input->GetData();
399 if (!strcmp(bname, "*")) {
400 tree->SetBranchStatus("*",1);
403 AliAnalysisDataSlot::EnableBranch(bname, tree);
406 //______________________________________________________________________________
407 void AliAnalysisTask::FinishTaskOutput()
409 // Optional method that is called in SlaveTerminate phase.
410 // Used for calling aditional methods just after the last event was processed ON
411 // THE WORKING NODE. The call is made also in local case.
412 // Do NOT delete output objects here since they will have to be sent for
413 // merging in PROOF mode - use class destructor for cleanup.
416 //______________________________________________________________________________
417 void AliAnalysisTask::ConnectInputData(Option_t *)
419 // Overload and connect your branches here.
422 //______________________________________________________________________________
423 void AliAnalysisTask::LocalInit()
425 // The method LocalInit() may be implemented to call locally (on the client)
426 // all initialization methods of the class. It is not mandatory and was created
427 // in order to minimize the complexity and readability of the analysis macro.
428 // DO NOT create in this method the histigrams or task output objects that will
429 // go in the task output containers. Use CreateOutputObjects for that.
432 //______________________________________________________________________________
433 void AliAnalysisTask::CreateOutputObjects()
435 // Called once per task either in PROOF or local mode. Overload to put some
436 // task initialization and/or create your output objects here.
439 //______________________________________________________________________________
440 TFile *AliAnalysisTask::OpenFile(Int_t iout, Option_t *option) const
442 // This method has to be called INSIDE the user redefined CreateOutputObjects
443 // method, before creating each object corresponding to the output containers
444 // that are to be written to a file. This need to be done in general for the big output
445 // objects that may not fit memory during processing.
446 // - 'option' is the file opening option.
447 //=========================================================================
448 // NOTE !: The method call will be ignored in PROOF mode, in which case the
449 // results have to be streamed back to the client and written just before Terminate()
450 //=========================================================================
453 // void MyAnaTask::CreateOutputObjects() {
454 // OpenFile(0); // Will open the file for the object to be written at output #0
455 // fAOD = new TTree("AOD for D0toKPi");
457 // now some histos that should go in the file of the second output container
458 // fHist1 = new TH1F("my quality check hist1",...);
459 // fHist2 = new TH2F("my quality check hist2",...);
462 if (iout<0 || iout>=fNoutputs) {
463 Error("OpenFile", "No output slot for task %s with index %d", GetName(), iout);
466 // We allow file opening also on the slaves (AG)
467 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
468 AliAnalysisDataContainer *cont = GetOutputSlot(iout)->GetContainer();
470 if (!strlen(cont->GetFileName())) {
471 Error("OpenFile", "No file name specified for container %s", cont->GetName());
474 if (mgr->GetAnalysisType()==AliAnalysisManager::kProofAnalysis && cont->IsSpecialOutput())
475 f = mgr->OpenProofFile(cont->GetFileName(),option);
477 // Check first if the file is already opened
478 f = (TFile*)gROOT->GetListOfFiles()->FindObject(cont->GetFileName());
479 if (!f) f = new TFile(cont->GetFileName(), option);
481 if (f && !f->IsZombie()) {
489 //______________________________________________________________________________
490 Bool_t AliAnalysisTask::Notify()
492 // Overload this IF you need to treat input file change.
496 //______________________________________________________________________________
497 Bool_t AliAnalysisTask::NotifyBinChange()
499 // Overload this IF you need to treat bin change in event mixing.
503 //______________________________________________________________________________
504 void AliAnalysisTask::Terminate(Option_t *)
506 // Method called by the framework at the end of data processing.
509 //______________________________________________________________________________
510 Bool_t AliAnalysisTask::PostData(Int_t iout, TObject *data, Option_t *option)
512 // Post output data for a given ouput slot in the corresponding data container.
513 // Published data becomes owned by the data container.
514 // If option is specified, the container connected to the output slot must have
515 // an associated file name defined. The option represents the method to open the file.
517 AliAnalysisDataSlot *output = GetOutputSlot(iout);
519 Error("PostData","Output slot %i not defined for analysis task %s", iout, GetName());
522 if (!output->IsConnected()) {
523 Error("PostData","Output slot %i of analysis task %s not connected to any data container", iout, GetName());
527 fOutputReady = new Bool_t[fNoutputs];
528 memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
530 fOutputReady[iout] = kTRUE;
531 fPublishedData = data;
532 return (output->GetContainer()->SetData(data, option));
535 //______________________________________________________________________________
536 void AliAnalysisTask::SetUsed(Bool_t flag)
538 // Set 'used' flag recursively to task and all daughter tasks.
539 if (TestBit(kTaskUsed)==flag) return;
540 TObject::SetBit(kTaskUsed,flag);
541 Int_t nd = fTasks->GetSize();
542 AliAnalysisTask *task;
543 for (Int_t i=0; i<nd; i++) {
544 task = (AliAnalysisTask*)fTasks->At(i);
549 //______________________________________________________________________________
550 Bool_t AliAnalysisTask::CheckCircularDeps()
552 // Check for illegal circular dependencies, e.g. a daughter task should not have
553 // a hierarchical parent as subtask.
554 if (IsChecked()) return kTRUE;
556 TList *tasks = GetListOfTasks();
557 Int_t ntasks = tasks->GetSize();
558 AliAnalysisTask *task;
559 for (Int_t i=0; i<ntasks; i++) {
560 task = (AliAnalysisTask*)tasks->At(i);
561 if (task->CheckCircularDeps()) return kTRUE;
567 //______________________________________________________________________________
568 void AliAnalysisTask::PrintTask(Option_t *option, Int_t indent) const
571 AliAnalysisTask *thistask = (AliAnalysisTask*)this;
574 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
577 AliAnalysisDataContainer *cont;
578 for (Int_t i=0; i<indent; i++) ind += " ";
579 if (!dep || (dep && IsChecked())) {
580 printf("%s\n", Form("%stask: %s ACTIVE=%i POST_LOOP=%i", ind.Data(), GetName(),IsActive(),IsPostEventLoop()));
581 if (dep) thistask->SetChecked(kFALSE);
583 for (islot=0; islot<fNinputs; islot++) {
584 printf("%s", Form("%s INPUT #%i: %s <- ",ind.Data(),islot, GetInputType(islot)->GetName()));
585 cont = GetInputSlot(islot)->GetContainer();
586 if (cont) printf(" [%s]\n", cont->GetName());
587 else printf(" [NO CONTAINER]\n");
589 for (islot=0; islot<fNoutputs; islot++) {
590 printf("%s", Form("%s OUTPUT #%i: %s -> ",ind.Data(),islot, GetOutputType(islot)->GetName()));
591 cont = GetOutputSlot(islot)->GetContainer();
592 if (cont) printf(" [%s]\n", cont->GetName());
593 else printf(" [NO CONTAINER]\n");
597 PrintContainers(option, indent+3);
600 //______________________________________________________________________________
601 void AliAnalysisTask::PrintContainers(Option_t *option, Int_t indent) const
603 // Print containers info.
604 AliAnalysisDataContainer *cont;
606 for (Int_t i=0; i<indent; i++) ind += " ";
608 for (islot=0; islot<fNoutputs; islot++) {
609 cont = GetOutputSlot(islot)->GetContainer();
610 if (cont) cont->PrintContainer(option, indent);
614 //______________________________________________________________________________
615 void AliAnalysisTask::SetPostEventLoop(Bool_t flag)
617 // Set the task execution mode - run after event loop or not. All output
618 // containers of this task will get the same type.
619 TObject::SetBit(kTaskPostEventLoop,flag);
620 AliAnalysisDataContainer *cont;
622 for (islot=0; islot<fNoutputs; islot++) {
623 cont = GetOutputSlot(islot)->GetContainer();
624 if (cont) cont->SetPostEventLoop(flag);