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