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