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