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