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>
104 #include "AliAnalysisTask.h"
105 #include "AliAnalysisDataSlot.h"
106 #include "AliAnalysisDataContainer.h"
107 #include "AliAnalysisManager.h"
109 ClassImp(AliAnalysisTask)
111 //______________________________________________________________________________
112 AliAnalysisTask::AliAnalysisTask()
114 fInitialized(kFALSE),
118 fPublishedData(NULL),
122 // Default constructor.
123 TObject::SetBit(kTaskEvtByEvt, kTRUE);
126 //______________________________________________________________________________
127 AliAnalysisTask::AliAnalysisTask(const char *name, const char *title)
130 fInitialized(kFALSE),
134 fPublishedData(NULL),
139 TObject::SetBit(kTaskEvtByEvt, kTRUE);
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 for (Int_t islot=0; islot<fNinputs; islot++) {
237 if (!GetInputData(islot)) {
243 if (fInitialized) return;
244 TDirectory *cursav = gDirectory;
246 if (cursav) cursav->cd();
247 fInitialized = kTRUE;
250 //______________________________________________________________________________
251 Bool_t AliAnalysisTask::ConnectInput(Int_t islot, AliAnalysisDataContainer *cont)
253 // Connect an input slot to a data container.
254 AliAnalysisDataSlot *input = GetInputSlot(islot);
256 Error("ConnectInput","Input slot %i not defined for analysis task %s", islot, GetName());
259 // Check type matching
260 if (!input->GetType()->InheritsFrom(cont->GetType())) {
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());
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);
272 //______________________________________________________________________________
273 Bool_t AliAnalysisTask::ConnectOutput(Int_t islot, AliAnalysisDataContainer *cont)
275 // Connect an output slot to a data container.
276 AliAnalysisDataSlot *output = GetOutputSlot(islot);
278 Error("ConnectOutput","Output slot %i not defined for analysis task %s", islot, GetName());
281 // Check type matching
282 if (!output->GetType()->InheritsFrom(cont->GetType())) {
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());
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);
294 //______________________________________________________________________________
295 void AliAnalysisTask::DefineInput(Int_t islot, TClass *type)
297 // Define an input slot and its type.
298 AliAnalysisDataSlot *input = new AliAnalysisDataSlot(type, this);
299 if (fNinputs<islot+1) fNinputs = islot+1;
300 fInputs->AddAtAndExpand(input, islot);
303 //______________________________________________________________________________
304 void AliAnalysisTask::DefineOutput(Int_t islot, TClass *type)
306 // Define an output slot and its type.
307 AliAnalysisDataSlot *output = new AliAnalysisDataSlot(type, this);
308 if (fNoutputs<islot+1) {
310 if (fOutputReady) delete [] fOutputReady;
311 fOutputReady = new Bool_t[fNoutputs];
312 memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
314 fOutputs->AddAtAndExpand(output, islot);
317 //______________________________________________________________________________
318 TClass *AliAnalysisTask::GetInputType(Int_t islot) const
320 // Retreive type of a given input slot.
321 AliAnalysisDataSlot *input = GetInputSlot(islot);
323 Error("GetInputType","Input slot %d not defined for analysis task %s", islot, GetName());
326 return (input->GetType());
329 //______________________________________________________________________________
330 TClass *AliAnalysisTask::GetOutputType(Int_t islot) const
332 // Retreive type of a given output slot.
333 AliAnalysisDataSlot *output = GetOutputSlot(islot);
335 Error("GetOutputType","Output slot %d not defined for analysis task %s", islot, GetName());
338 return (output->GetType());
341 //______________________________________________________________________________
342 TObject *AliAnalysisTask::GetInputData(Int_t islot) const
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);
348 Error("GetInputData","Input slot %d not defined for analysis task %s", islot, GetName());
351 return (input->GetData());
354 //______________________________________________________________________________
355 TObject *AliAnalysisTask::GetOutputData(Int_t islot) const
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);
361 Error("GetOutputData","Input slot %d not defined for analysis task %s", islot, GetName());
364 return (output->GetData());
367 //______________________________________________________________________________
368 char *AliAnalysisTask::GetBranchAddress(Int_t islot, const char *branch) const
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);
376 //______________________________________________________________________________
377 Bool_t AliAnalysisTask::SetBranchAddress(Int_t islot, const char *branch, void *address) const
379 // Connect an object address to a branch of the specified input.
380 return GetInputSlot(islot)->SetBranchAddress(branch, address);
383 //______________________________________________________________________________
384 void AliAnalysisTask::ConnectInputData(Option_t *)
386 // Overload and connect your branches here.
389 //______________________________________________________________________________
390 void AliAnalysisTask::LocalInit()
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.
399 //______________________________________________________________________________
400 void AliAnalysisTask::CreateOutputObjects()
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.
406 //______________________________________________________________________________
407 void AliAnalysisTask::OpenFile(Int_t iout, Option_t *option) const
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 //=========================================================================
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");
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",...);
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);
436 //______________________________________________________________________________
437 Bool_t AliAnalysisTask::Notify()
439 // Overload this IF you need to treat input file change.
443 //______________________________________________________________________________
444 void AliAnalysisTask::Terminate(Option_t *)
446 // Method called by the framework at the end of data processing.
449 //______________________________________________________________________________
450 Bool_t AliAnalysisTask::PostData(Int_t iout, TObject *data, Option_t *option)
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.
457 AliAnalysisDataSlot *output = GetOutputSlot(iout);
459 Error("PostData","Output slot %i not defined for analysis task %s", iout, GetName());
462 if (!output->IsConnected()) {
463 Error("PostData","Output slot %i of analysis task %s not connected to any data container", iout, GetName());
467 fOutputReady = new Bool_t[fNoutputs];
468 memset(fOutputReady, 0, fNoutputs*sizeof(Bool_t));
470 fOutputReady[iout] = kTRUE;
471 fPublishedData = data;
472 return (output->GetContainer()->SetData(data, option));
475 //______________________________________________________________________________
476 void AliAnalysisTask::SetUsed(Bool_t flag)
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);
489 //______________________________________________________________________________
490 Bool_t AliAnalysisTask::CheckCircularDeps()
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;
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;
507 //______________________________________________________________________________
508 void AliAnalysisTask::PrintTask(Option_t *option, Int_t indent) const
511 AliAnalysisTask *thistask = (AliAnalysisTask*)this;
514 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
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);
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");
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");
537 PrintContainers(option, indent+3);
540 //______________________________________________________________________________
541 void AliAnalysisTask::PrintContainers(Option_t *option, Int_t indent) const
543 // Print containers info.
544 AliAnalysisDataContainer *cont;
546 for (Int_t i=0; i<indent; i++) ind += " ";
548 for (islot=0; islot<fNoutputs; islot++) {
549 cont = GetOutputSlot(islot)->GetContainer();
550 cont->PrintContainer(option, indent);