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