Adding AliTPCTracklet to the repository (M.Mager)
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisManager.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// AliAnalysysManager - Manager analysis class. Allows creation of several
37153431 21// analysis tasks and data containers storing their input/output. Allows
d3106602 22// connecting/chaining tasks via shared data containers. Serializes the current
23// event for all tasks depending only on initial input data.
24//==============================================================================
25//
26//==============================================================================
27
c52c2132 28#include <Riostream.h>
11026a80 29
c52c2132 30#include <TClass.h>
31#include <TFile.h>
32#include <TMethodCall.h>
33#include <TChain.h>
34#include <TSystem.h>
35#include <TROOT.h>
d3106602 36
d3106602 37#include "AliAnalysisTask.h"
38#include "AliAnalysisDataContainer.h"
39#include "AliAnalysisDataSlot.h"
d2f1d9ef 40#include "AliVEventHandler.h"
c52c2132 41#include "AliAnalysisManager.h"
d3106602 42
43ClassImp(AliAnalysisManager)
44
c52c2132 45AliAnalysisManager *AliAnalysisManager::fgAnalysisManager = NULL;
46
d3106602 47//______________________________________________________________________________
c52c2132 48AliAnalysisManager::AliAnalysisManager(const char *name, const char *title)
49 :TNamed(name,title),
50 fTree(NULL),
54cff064 51 fInputEventHandler(NULL),
6bb2b24f 52 fOutputEventHandler(NULL),
53 fMCtruthEventHandler(NULL),
c52c2132 54 fCurrentEntry(-1),
55 fMode(kLocalAnalysis),
56 fInitOK(kFALSE),
57 fDebug(0),
37a26056 58 fTasks(NULL),
59 fTopTasks(NULL),
c52c2132 60 fZombies(NULL),
61 fContainers(NULL),
62 fInputs(NULL),
63 fOutputs(NULL)
d3106602 64{
65// Default constructor.
c52c2132 66 fgAnalysisManager = this;
67 fTasks = new TObjArray();
68 fTopTasks = new TObjArray();
69 fZombies = new TObjArray();
70 fContainers = new TObjArray();
71 fInputs = new TObjArray();
37153431 72 fOutputs = new TObjArray();
b1310ef5 73 SetEventLoop(kTRUE);
d3106602 74}
75
76//______________________________________________________________________________
77AliAnalysisManager::AliAnalysisManager(const AliAnalysisManager& other)
c52c2132 78 :TNamed(other),
327eaf46 79 fTree(NULL),
54cff064 80 fInputEventHandler(NULL),
6bb2b24f 81 fOutputEventHandler(NULL),
82 fMCtruthEventHandler(NULL),
c52c2132 83 fCurrentEntry(-1),
84 fMode(other.fMode),
85 fInitOK(other.fInitOK),
86 fDebug(other.fDebug),
37a26056 87 fTasks(NULL),
88 fTopTasks(NULL),
c52c2132 89 fZombies(NULL),
90 fContainers(NULL),
91 fInputs(NULL),
92 fOutputs(NULL)
d3106602 93{
94// Copy constructor.
37a26056 95 fTasks = new TObjArray(*other.fTasks);
96 fTopTasks = new TObjArray(*other.fTopTasks);
97 fZombies = new TObjArray(*other.fZombies);
c52c2132 98 fContainers = new TObjArray(*other.fContainers);
99 fInputs = new TObjArray(*other.fInputs);
100 fOutputs = new TObjArray(*other.fOutputs);
101 fgAnalysisManager = this;
d3106602 102}
103
104//______________________________________________________________________________
105AliAnalysisManager& AliAnalysisManager::operator=(const AliAnalysisManager& other)
106{
107// Assignment
108 if (&other != this) {
c52c2132 109 TNamed::operator=(other);
54cff064 110 fInputEventHandler = other.fInputEventHandler;
6bb2b24f 111 fOutputEventHandler = other.fOutputEventHandler;
112 fMCtruthEventHandler = other.fMCtruthEventHandler;
c52c2132 113 fTree = NULL;
114 fCurrentEntry = -1;
115 fMode = other.fMode;
37a26056 116 fInitOK = other.fInitOK;
c52c2132 117 fDebug = other.fDebug;
37a26056 118 fTasks = new TObjArray(*other.fTasks);
119 fTopTasks = new TObjArray(*other.fTopTasks);
120 fZombies = new TObjArray(*other.fZombies);
c52c2132 121 fContainers = new TObjArray(*other.fContainers);
122 fInputs = new TObjArray(*other.fInputs);
123 fOutputs = new TObjArray(*other.fOutputs);
124 fgAnalysisManager = this;
d3106602 125 }
126 return *this;
127}
128
129//______________________________________________________________________________
130AliAnalysisManager::~AliAnalysisManager()
131{
132// Destructor.
d3106602 133 if (fTasks) {fTasks->Delete(); delete fTasks;}
134 if (fTopTasks) delete fTopTasks;
135 if (fZombies) delete fZombies;
c52c2132 136 if (fContainers) {fContainers->Delete(); delete fContainers;}
137 if (fInputs) delete fInputs;
138 if (fOutputs) delete fOutputs;
139 if (fgAnalysisManager==this) fgAnalysisManager = NULL;
d3106602 140}
c52c2132 141
d3106602 142//______________________________________________________________________________
327eaf46 143Int_t AliAnalysisManager::GetEntry(Long64_t entry, Int_t getall)
144{
145// Read one entry of the tree or a whole branch.
c52c2132 146 if (fDebug > 1) {
147 cout << "== AliAnalysisManager::GetEntry()" << endl;
148 }
149 fCurrentEntry = entry;
327eaf46 150 return fTree ? fTree->GetTree()->GetEntry(entry, getall) : 0;
151}
152
153//______________________________________________________________________________
154void AliAnalysisManager::Init(TTree *tree)
d3106602 155{
156 // The Init() function is called when the selector needs to initialize
157 // a new tree or chain. Typically here the branch addresses of the tree
158 // will be set. It is normaly not necessary to make changes to the
159 // generated code, but the routine can be extended by the user if needed.
160 // Init() will be called many times when running with PROOF.
327eaf46 161 if (!tree) return;
c52c2132 162 if (fDebug > 1) {
981f2614 163 printf("->AliAnalysisManager::Init(%s)\n", tree->GetName());
c52c2132 164 }
fdb458ec 165
166 if (fInputEventHandler) {
167 fInputEventHandler->SetInputTree(tree);
6073f8c9 168 fInputEventHandler->InitIO("proof");
fdb458ec 169 }
170
c52c2132 171 if (!fInitOK) InitAnalysis();
172 if (!fInitOK) return;
327eaf46 173 fTree = tree;
174 AliAnalysisDataContainer *top = (AliAnalysisDataContainer*)fInputs->At(0);
c52c2132 175 if (!top) {
176 cout<<"Error: No top input container !" <<endl;
177 return;
37153431 178 }
327eaf46 179 top->SetData(tree);
981f2614 180 if (fDebug > 1) {
181 printf("<-AliAnalysisManager::Init(%s)\n", tree->GetName());
182 }
d3106602 183}
184
185//______________________________________________________________________________
327eaf46 186void AliAnalysisManager::Begin(TTree *tree)
d3106602 187{
188 // The Begin() function is called at the start of the query.
189 // When running with PROOF Begin() is only called on the client.
190 // The tree argument is deprecated (on PROOF 0 is passed).
c52c2132 191 if (fDebug > 1) {
192 cout << "AliAnalysisManager::Begin()" << endl;
193 }
327eaf46 194 Init(tree);
d3106602 195}
196
197//______________________________________________________________________________
327eaf46 198void AliAnalysisManager::SlaveBegin(TTree *tree)
d3106602 199{
200 // The SlaveBegin() function is called after the Begin() function.
201 // When running with PROOF SlaveBegin() is called on each slave server.
202 // The tree argument is deprecated (on PROOF 0 is passed).
c52c2132 203 if (fDebug > 1) {
981f2614 204 cout << "->AliAnalysisManager::SlaveBegin()" << endl;
37153431 205 }
8c9485b2 206 // Call InitIO of EventHandler
6bb2b24f 207 if (fOutputEventHandler) {
8ca08916 208 if (fMode == kProofAnalysis) {
6bb2b24f 209 fOutputEventHandler->InitIO("proof");
8ca08916 210 } else {
6bb2b24f 211 fOutputEventHandler->InitIO("local");
8ca08916 212 }
bcce695f 213 }
6073f8c9 214 if (fInputEventHandler) {
215 if (fMode == kProofAnalysis) {
216 fInputEventHandler->SetInputTree(tree);
217 fInputEventHandler->InitIO("proof");
218 } else {
219 fInputEventHandler->SetInputTree(tree);
220 fInputEventHandler->InitIO("local");
221 }
222 }
223
224 if (fMCtruthEventHandler) {
225 if (fMode == kProofAnalysis) {
226 fMCtruthEventHandler->InitIO("proof");
227 } else {
228 fMCtruthEventHandler->InitIO("local");
229 }
54cff064 230 }
231
8c9485b2 232 //
c52c2132 233 TIter next(fTasks);
234 AliAnalysisTask *task;
235 // Call CreateOutputObjects for all tasks
c5a87c56 236 while ((task=(AliAnalysisTask*)next())) {
237 TDirectory *curdir = gDirectory;
c52c2132 238 task->CreateOutputObjects();
c5a87c56 239 if (curdir) curdir->cd();
240 }
fdb458ec 241 if (fMode == kLocalAnalysis)
242 Init(tree);
981f2614 243 if (fDebug > 1) {
244 cout << "<-AliAnalysisManager::SlaveBegin()" << endl;
245 }
d3106602 246}
247
248//______________________________________________________________________________
327eaf46 249Bool_t AliAnalysisManager::Notify()
250{
251 // The Notify() function is called when a new file is opened. This
252 // can be either for a new TTree in a TChain or when when a new TTree
253 // is started when using PROOF. It is normaly not necessary to make changes
254 // to the generated code, but the routine can be extended by the
255 // user if needed. The return value is currently not used.
53faeca4 256 if (fTree) {
fdb458ec 257 TFile *curfile = fTree->GetCurrentFile();
258 if (curfile && fDebug>1) printf("AliAnalysisManager::Notify() file: %s\n", curfile->GetName());
259 TIter next(fTasks);
260 AliAnalysisTask *task;
261 // Call Notify for all tasks
262 while ((task=(AliAnalysisTask*)next()))
263 task->Notify();
264
6073f8c9 265 // Call Notify of the event handlers
266 if (fInputEventHandler) {
267 fInputEventHandler->Notify(curfile->GetName());
268 }
269
270 if (fOutputEventHandler) {
271 fOutputEventHandler->Notify(curfile->GetName());
272 }
273
fdb458ec 274 if (fMCtruthEventHandler) {
275 fMCtruthEventHandler->Notify(curfile->GetName());
276 }
890126ab 277
fdb458ec 278 }
279 return kTRUE;
327eaf46 280}
281
282//______________________________________________________________________________
283Bool_t AliAnalysisManager::Process(Long64_t entry)
d3106602 284{
285 // The Process() function is called for each entry in the tree (or possibly
286 // keyed object in the case of PROOF) to be processed. The entry argument
287 // specifies which entry in the currently loaded tree is to be processed.
288 // It can be passed to either TTree::GetEntry() or TBranch::GetEntry()
289 // to read either all or the required parts of the data. When processing
290 // keyed objects with PROOF, the object is already loaded and is available
291 // via the fObject pointer.
292 //
293 // This function should contain the "body" of the analysis. It can contain
294 // simple or elaborate selection criteria, run algorithms on the data
295 // of the event and typically fill histograms.
296
297 // WARNING when a selector is used with a TChain, you must use
298 // the pointer to the current TTree to call GetEntry(entry).
299 // The entry is always the local entry number in the current tree.
300 // Assuming that fChain is the pointer to the TChain being processed,
301 // use fChain->GetTree()->GetEntry(entry).
c52c2132 302 if (fDebug > 1) {
981f2614 303 cout << "->AliAnalysisManager::Process()" << endl;
37153431 304 }
c3701689 305 if (fInputEventHandler) fInputEventHandler ->BeginEvent();
6bb2b24f 306 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent();
307 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent();
308
327eaf46 309 GetEntry(entry);
310 ExecAnalysis();
981f2614 311 if (fDebug > 1) {
312 cout << "<-AliAnalysisManager::Process()" << endl;
313 }
327eaf46 314 return kTRUE;
d3106602 315}
316
317//______________________________________________________________________________
c52c2132 318void AliAnalysisManager::PackOutput(TList *target)
d3106602 319{
981f2614 320 // Pack all output data containers in the output list. Called at SlaveTerminate
321 // stage in PROOF case for each slave.
c52c2132 322 if (fDebug > 1) {
981f2614 323 cout << "->AliAnalysisManager::PackOutput()" << endl;
c52c2132 324 }
325 if (!target) {
326 Error("PackOutput", "No target. Aborting.");
327 return;
37153431 328 }
6073f8c9 329 if (fInputEventHandler) fInputEventHandler ->Terminate();
6bb2b24f 330 if (fOutputEventHandler) fOutputEventHandler ->Terminate();
331 if (fMCtruthEventHandler) fMCtruthEventHandler->Terminate();
8c9485b2 332
c52c2132 333 if (fMode == kProofAnalysis) {
334 TIter next(fOutputs);
335 AliAnalysisDataContainer *output;
336 while ((output=(AliAnalysisDataContainer*)next())) {
8167b1d0 337 if (output->GetData()) {
338 if (output->GetProducer()->IsPostEventLoop()) continue;
339 AliAnalysisDataWrapper *wrap = output->ExportData();
340 // Output wrappers must delete data after merging (AG 13/11/07)
341 wrap->SetDeleteData(kTRUE);
342 if (fDebug > 1) printf(" Packing container %s...\n", output->GetName());
343 target->Add(wrap);
344 }
c52c2132 345 }
346 }
c52c2132 347 if (fDebug > 1) {
981f2614 348 printf("<-AliAnalysisManager::PackOutput: output list contains %d containers\n", target->GetSize());
37153431 349 }
c52c2132 350}
351
352//______________________________________________________________________________
981f2614 353void AliAnalysisManager::ImportWrappers(TList *source)
c52c2132 354{
981f2614 355// Import data in output containers from wrappers coming in source.
356 if (fDebug > 1) {
357 cout << "->AliAnalysisManager::ImportWrappers()" << endl;
358 }
327eaf46 359 TIter next(fOutputs);
981f2614 360 AliAnalysisDataContainer *cont;
361 AliAnalysisDataWrapper *wrap;
362 Int_t icont = 0;
c52c2132 363 while ((cont=(AliAnalysisDataContainer*)next())) {
8167b1d0 364 if (cont->GetProducer()->IsPostEventLoop()) continue;
981f2614 365 wrap = (AliAnalysisDataWrapper*)source->FindObject(cont->GetName());
366 if (!wrap && fDebug>1) {
367 printf("(WW) ImportWrappers: container %s not found in analysis output !\n", cont->GetName());
c52c2132 368 continue;
369 }
981f2614 370 icont++;
371 if (fDebug > 1) printf(" Importing data for container %s\n", wrap->GetName());
372 if (cont->GetFileName()) printf(" -> %s\n", cont->GetFileName());
373 cont->ImportData(wrap);
c52c2132 374 }
981f2614 375 if (fDebug > 1) {
376 cout << "<-AliAnalysisManager::ImportWrappers(): "<< icont << " containers imported" << endl;
377 }
c52c2132 378}
379
380//______________________________________________________________________________
381void AliAnalysisManager::UnpackOutput(TList *source)
382{
383 // Called by AliAnalysisSelector::Terminate. Output containers should
384 // be in source in the same order as in fOutputs.
981f2614 385 if (fDebug > 1) {
386 cout << "->AliAnalysisManager::UnpackOutput()" << endl;
387 }
c52c2132 388 if (!source) {
981f2614 389 Error("UnpackOutput", "No target. Aborting.");
c52c2132 390 return;
391 }
392 if (fDebug > 1) {
c52c2132 393 printf(" Source list contains %d containers\n", source->GetSize());
327eaf46 394 }
c52c2132 395
981f2614 396 if (fMode == kProofAnalysis) ImportWrappers(source);
37153431 397
981f2614 398 TIter next(fOutputs);
c52c2132 399 AliAnalysisDataContainer *output;
400 while ((output=(AliAnalysisDataContainer*)next())) {
c52c2132 401 if (!output->GetData()) continue;
b1310ef5 402 // Check if there are client tasks that run post event loop
403 if (output->HasConsumers()) {
404 // Disable event loop semaphore
405 output->SetPostEventLoop(kTRUE);
406 TObjArray *list = output->GetConsumers();
407 Int_t ncons = list->GetEntriesFast();
408 for (Int_t i=0; i<ncons; i++) {
409 AliAnalysisTask *task = (AliAnalysisTask*)list->At(i);
410 task->CheckNotify(kTRUE);
411 // If task is active, execute it
412 if (task->IsPostEventLoop() && task->IsActive()) {
413 if (fDebug > 1) {
414 cout << "== Executing post event loop task " << task->GetName() << endl;
415 }
416 task->ExecuteTask();
417 }
418 }
419 }
c52c2132 420 // Check if the output need to be written to a file.
421 const char *filename = output->GetFileName();
8ca08916 422 if (!(strcmp(filename, "default"))) {
6bb2b24f 423 if (fOutputEventHandler) filename = fOutputEventHandler->GetOutputFileName();
8ca08916 424 }
425
c52c2132 426 if (!filename || !strlen(filename)) continue;
427 TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
428 if (file) file->cd();
429 else file = new TFile(filename, "RECREATE");
430 if (file->IsZombie()) continue;
431 // Reparent data to this file
432 TMethodCall callEnv;
433 if (output->GetData()->IsA())
434 callEnv.InitWithPrototype(output->GetData()->IsA(), "SetDirectory", "TDirectory*");
435 if (callEnv.IsValid()) {
436 callEnv.SetParam((Long_t) file);
437 callEnv.Execute(output->GetData());
438 }
b1109411 439 output->GetData()->Write();
c52c2132 440 }
981f2614 441 if (fDebug > 1) {
442 cout << "<-AliAnalysisManager::UnpackOutput()" << endl;
443 }
d3106602 444}
445
446//______________________________________________________________________________
447void AliAnalysisManager::Terminate()
448{
449 // The Terminate() function is the last function to be called during
450 // a query. It always runs on the client, it can be used to present
c52c2132 451 // the results graphically.
452 if (fDebug > 1) {
981f2614 453 cout << "->AliAnalysisManager::Terminate()" << endl;
c52c2132 454 }
327eaf46 455 AliAnalysisTask *task;
c52c2132 456 TIter next(fTasks);
327eaf46 457 // Call Terminate() for tasks
c52c2132 458 while ((task=(AliAnalysisTask*)next())) task->Terminate();
981f2614 459 if (fDebug > 1) {
460 cout << "<-AliAnalysisManager::Terminate()" << endl;
461 }
8c9485b2 462 //
6073f8c9 463 if (fInputEventHandler) fInputEventHandler ->TerminateIO();
464 if (fOutputEventHandler) fOutputEventHandler ->TerminateIO();
465 if (fMCtruthEventHandler) fMCtruthEventHandler->TerminateIO();
d3106602 466}
467
468//______________________________________________________________________________
469void AliAnalysisManager::AddTask(AliAnalysisTask *task)
470{
471// Adds a user task to the global list of tasks.
472 task->SetActive(kFALSE);
473 fTasks->Add(task);
474}
475
476//______________________________________________________________________________
477AliAnalysisTask *AliAnalysisManager::GetTask(const char *name) const
478{
479// Retreive task by name.
480 if (!fTasks) return NULL;
481 return (AliAnalysisTask*)fTasks->FindObject(name);
482}
483
484//______________________________________________________________________________
485AliAnalysisDataContainer *AliAnalysisManager::CreateContainer(const char *name,
c52c2132 486 TClass *datatype, EAliAnalysisContType type, const char *filename)
d3106602 487{
488// Create a data container of a certain type. Types can be:
c52c2132 489// kExchangeContainer = 0, used to exchange date between tasks
d3106602 490// kInputContainer = 1, used to store input data
491// kOutputContainer = 2, used for posting results
b1310ef5 492 if (fContainers->FindObject(name)) {
493 Error("CreateContainer","A container named %s already defined !\n",name);
494 return NULL;
495 }
d3106602 496 AliAnalysisDataContainer *cont = new AliAnalysisDataContainer(name, datatype);
497 fContainers->Add(cont);
498 switch (type) {
499 case kInputContainer:
500 fInputs->Add(cont);
501 break;
502 case kOutputContainer:
503 fOutputs->Add(cont);
c52c2132 504 if (filename && strlen(filename)) cont->SetFileName(filename);
d3106602 505 break;
c52c2132 506 case kExchangeContainer:
d3106602 507 break;
508 }
509 return cont;
510}
511
512//______________________________________________________________________________
513Bool_t AliAnalysisManager::ConnectInput(AliAnalysisTask *task, Int_t islot,
514 AliAnalysisDataContainer *cont)
515{
516// Connect input of an existing task to a data container.
517 if (!fTasks->FindObject(task)) {
518 AddTask(task);
c52c2132 519 Warning("ConnectInput", "Task %s not registered. Now owned by analysis manager", task->GetName());
d3106602 520 }
521 Bool_t connected = task->ConnectInput(islot, cont);
522 return connected;
523}
524
525//______________________________________________________________________________
526Bool_t AliAnalysisManager::ConnectOutput(AliAnalysisTask *task, Int_t islot,
527 AliAnalysisDataContainer *cont)
528{
529// Connect output of an existing task to a data container.
530 if (!fTasks->FindObject(task)) {
531 AddTask(task);
c52c2132 532 Warning("ConnectOutput", "Task %s not registered. Now owned by analysis manager", task->GetName());
d3106602 533 }
534 Bool_t connected = task->ConnectOutput(islot, cont);
535 return connected;
536}
537
538//______________________________________________________________________________
539void AliAnalysisManager::CleanContainers()
540{
541// Clean data from all containers that have already finished all client tasks.
542 TIter next(fContainers);
543 AliAnalysisDataContainer *cont;
544 while ((cont=(AliAnalysisDataContainer *)next())) {
545 if (cont->IsOwnedData() &&
546 cont->IsDataReady() &&
547 cont->ClientsExecuted()) cont->DeleteData();
548 }
549}
550
551//______________________________________________________________________________
552Bool_t AliAnalysisManager::InitAnalysis()
553{
554// Initialization of analysis chain of tasks. Should be called after all tasks
555// and data containers are properly connected
556 // Check for input/output containers
557 fInitOK = kFALSE;
d3106602 558 // Check for top tasks (depending only on input data containers)
559 if (!fTasks->First()) {
c52c2132 560 Error("InitAnalysis", "Analysis has no tasks !");
d3106602 561 return kFALSE;
562 }
563 TIter next(fTasks);
564 AliAnalysisTask *task;
565 AliAnalysisDataContainer *cont;
566 Int_t ntop = 0;
567 Int_t nzombies = 0;
327eaf46 568 Bool_t iszombie = kFALSE;
569 Bool_t istop = kTRUE;
d3106602 570 Int_t i;
571 while ((task=(AliAnalysisTask*)next())) {
327eaf46 572 istop = kTRUE;
573 iszombie = kFALSE;
d3106602 574 Int_t ninputs = task->GetNinputs();
d3106602 575 for (i=0; i<ninputs; i++) {
576 cont = task->GetInputSlot(i)->GetContainer();
577 if (!cont) {
327eaf46 578 if (!iszombie) {
d3106602 579 task->SetZombie();
580 fZombies->Add(task);
581 nzombies++;
327eaf46 582 iszombie = kTRUE;
d3106602 583 }
c52c2132 584 Error("InitAnalysis", "Input slot %d of task %s has no container connected ! Declared zombie...",
585 i, task->GetName());
d3106602 586 }
327eaf46 587 if (iszombie) continue;
d3106602 588 // Check if cont is an input container
327eaf46 589 if (istop && !fInputs->FindObject(cont)) istop=kFALSE;
d3106602 590 // Connect to parent task
591 }
327eaf46 592 if (istop) {
d3106602 593 ntop++;
594 fTopTasks->Add(task);
595 }
596 }
597 if (!ntop) {
c52c2132 598 Error("InitAnalysis", "No top task defined. At least one task should be connected only to input containers");
d3106602 599 return kFALSE;
600 }
601 // Check now if there are orphan tasks
602 for (i=0; i<ntop; i++) {
603 task = (AliAnalysisTask*)fTopTasks->At(i);
604 task->SetUsed();
605 }
606 Int_t norphans = 0;
607 next.Reset();
608 while ((task=(AliAnalysisTask*)next())) {
609 if (!task->IsUsed()) {
610 norphans++;
c52c2132 611 Warning("InitAnalysis", "Task %s is orphan", task->GetName());
d3106602 612 }
613 }
614 // Check the task hierarchy (no parent task should depend on data provided
615 // by a daughter task)
616 for (i=0; i<ntop; i++) {
617 task = (AliAnalysisTask*)fTopTasks->At(i);
618 if (task->CheckCircularDeps()) {
c52c2132 619 Error("InitAnalysis", "Found illegal circular dependencies between following tasks:");
d3106602 620 PrintStatus("dep");
621 return kFALSE;
622 }
623 }
b1310ef5 624 // Check that all containers feeding post-event loop tasks are in the outputs list
625 TIter nextcont(fContainers); // loop over all containers
626 while ((cont=(AliAnalysisDataContainer*)nextcont())) {
627 if (!cont->IsPostEventLoop() && !fOutputs->FindObject(cont)) {
628 if (cont->HasConsumers()) {
629 // Check if one of the consumers is post event loop
630 TIter nextconsumer(cont->GetConsumers());
631 while ((task=(AliAnalysisTask*)nextconsumer())) {
632 if (task->IsPostEventLoop()) {
633 fOutputs->Add(cont);
634 break;
635 }
636 }
637 }
638 }
639 }
327eaf46 640 fInitOK = kTRUE;
d3106602 641 return kTRUE;
642}
643
644//______________________________________________________________________________
645void AliAnalysisManager::PrintStatus(Option_t *option) const
646{
647// Print task hierarchy.
648 TIter next(fTopTasks);
649 AliAnalysisTask *task;
650 while ((task=(AliAnalysisTask*)next()))
651 task->PrintTask(option);
652}
653
654//______________________________________________________________________________
655void AliAnalysisManager::ResetAnalysis()
656{
657// Reset all execution flags and clean containers.
658 CleanContainers();
659}
660
661//______________________________________________________________________________
c52c2132 662void AliAnalysisManager::StartAnalysis(const char *type, TTree *tree)
663{
664// Start analysis for this manager. Analysis task can be: LOCAL, PROOF or GRID.
665 if (!fInitOK) {
666 Error("StartAnalysis","Analysis manager was not initialized !");
667 return;
668 }
669 if (fDebug>1) {
670 cout << "StartAnalysis: " << GetName() << endl;
671 }
672 TString anaType = type;
673 anaType.ToLower();
674 fMode = kLocalAnalysis;
675 if (tree) {
676 if (anaType.Contains("proof")) fMode = kProofAnalysis;
677 else if (anaType.Contains("grid")) fMode = kGridAnalysis;
678 }
679 if (fMode == kGridAnalysis) {
680 Warning("StartAnalysis", "GRID analysis mode not implemented. Running local.");
981f2614 681 fMode = kLocalAnalysis;
682 }
efd53803 683 char line[128];
684 SetEventLoop(kFALSE);
b1310ef5 685 // Disable all branches if requested and set event loop mode
efd53803 686 if (tree) {
b1310ef5 687 if (TestBit(kDisableBranches)) {
688 printf("Disabling all branches...\n");
689// tree->SetBranchStatus("*",0); // not yet working
690 }
efd53803 691 SetEventLoop(kTRUE);
692 }
efd53803 693
c52c2132 694 TChain *chain = dynamic_cast<TChain*>(tree);
9b33830a 695
696 // Initialize locally all tasks
697 TIter next(fTasks);
698 AliAnalysisTask *task;
efd53803 699 while ((task=(AliAnalysisTask*)next())) {
efd53803 700 task->LocalInit();
701 }
702
c52c2132 703 switch (fMode) {
704 case kLocalAnalysis:
705 if (!tree) {
981f2614 706 TIter next(fTasks);
707 AliAnalysisTask *task;
708 // Call CreateOutputObjects for all tasks
c5a87c56 709 while ((task=(AliAnalysisTask*)next())) {
710 TDirectory *curdir = gDirectory;
711 task->CreateOutputObjects();
712 if (curdir) curdir->cd();
713 }
c52c2132 714 ExecAnalysis();
981f2614 715 Terminate();
c52c2132 716 return;
717 }
718 // Run tree-based analysis via AliAnalysisSelector
8eedd442 719// gROOT->ProcessLine(".L $ALICE_ROOT/ANALYSIS/AliAnalysisSelector.cxx+");
c52c2132 720 cout << "===== RUNNING LOCAL ANALYSIS " << GetName() << " ON TREE " << tree->GetName() << endl;
721 sprintf(line, "AliAnalysisSelector *selector = new AliAnalysisSelector((AliAnalysisManager*)0x%lx);",(ULong_t)this);
722 gROOT->ProcessLine(line);
723 sprintf(line, "((TTree*)0x%lx)->Process(selector);",(ULong_t)tree);
724 gROOT->ProcessLine(line);
725 break;
726 case kProofAnalysis:
727 if (!gROOT->GetListOfProofs() || !gROOT->GetListOfProofs()->GetEntries()) {
728 printf("StartAnalysis: no PROOF!!!\n");
729 return;
730 }
731 sprintf(line, "gProof->AddInput((TObject*)0x%lx);", (ULong_t)this);
732 gROOT->ProcessLine(line);
733 if (chain) {
734 chain->SetProof();
735 cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON CHAIN " << chain->GetName() << endl;
8eedd442 736 chain->Process("AliAnalysisSelector");
c52c2132 737 } else {
738 printf("StartAnalysis: no chain\n");
739 return;
740 }
741 break;
742 case kGridAnalysis:
743 Warning("StartAnalysis", "GRID analysis mode not implemented. Running local.");
744 }
745}
746
747//______________________________________________________________________________
d3106602 748void AliAnalysisManager::ExecAnalysis(Option_t *option)
749{
750// Execute analysis.
327eaf46 751 if (!fInitOK) {
c52c2132 752 Error("ExecAnalysis", "Analysis manager was not initialized !");
327eaf46 753 return;
754 }
d3106602 755 AliAnalysisTask *task;
327eaf46 756 // Check if the top tree is active.
757 if (fTree) {
758 TIter next(fTasks);
759 // De-activate all tasks
760 while ((task=(AliAnalysisTask*)next())) task->SetActive(kFALSE);
761 AliAnalysisDataContainer *cont = (AliAnalysisDataContainer*)fInputs->At(0);
762 if (!cont) {
c52c2132 763 Error("ExecAnalysis","Cannot execute analysis in TSelector mode without at least one top container");
327eaf46 764 return;
765 }
766 cont->SetData(fTree); // This will notify all consumers
6bb2b24f 767//
c3701689 768// Call BeginEvent() for optional input/output and MC services
769 if (fInputEventHandler) fInputEventHandler ->BeginEvent();
6bb2b24f 770 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent();
771 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent();
772//
773// Execute the tasks
327eaf46 774 TIter next1(cont->GetConsumers());
775 while ((task=(AliAnalysisTask*)next1())) {
c52c2132 776 if (fDebug >1) {
777 cout << " Executing task " << task->GetName() << endl;
778 }
6bb2b24f 779
327eaf46 780 task->ExecuteTask(option);
781 }
6bb2b24f 782//
783// Call FinishEvent() for optional output and MC services
6073f8c9 784 if (fInputEventHandler) fInputEventHandler ->FinishEvent();
6bb2b24f 785 if (fOutputEventHandler) fOutputEventHandler ->FinishEvent();
786 if (fMCtruthEventHandler) fMCtruthEventHandler->FinishEvent();
787//
327eaf46 788 return;
789 }
790 // The event loop is not controlled by TSelector
6bb2b24f 791//
c3701689 792// Call BeginEvent() for optional input/output and MC services
793 if (fInputEventHandler) fInputEventHandler ->BeginEvent();
6bb2b24f 794 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent();
795 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent();
327eaf46 796 TIter next2(fTopTasks);
797 while ((task=(AliAnalysisTask*)next2())) {
798 task->SetActive(kTRUE);
c52c2132 799 if (fDebug > 1) {
800 cout << " Executing task " << task->GetName() << endl;
801 }
d3106602 802 task->ExecuteTask(option);
327eaf46 803 }
6bb2b24f 804//
805// Call FinishEvent() for optional output and MC services
6073f8c9 806 if (fInputEventHandler) fInputEventHandler ->FinishEvent();
807 if (fOutputEventHandler) fOutputEventHandler ->FinishEvent();
6bb2b24f 808 if (fMCtruthEventHandler) fMCtruthEventHandler->FinishEvent();
d3106602 809}
810
811//______________________________________________________________________________
812void AliAnalysisManager::FinishAnalysis()
813{
814// Finish analysis.
815}