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