- Introduced merging via TProofFile waiting for a full API to handle the output file.
[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>
8c0ab8e8 36#include <TCanvas.h>
d3106602 37
d3106602 38#include "AliAnalysisTask.h"
39#include "AliAnalysisDataContainer.h"
40#include "AliAnalysisDataSlot.h"
d2f1d9ef 41#include "AliVEventHandler.h"
8c0ab8e8 42#include "AliSysInfo.h"
c52c2132 43#include "AliAnalysisManager.h"
d3106602 44
45ClassImp(AliAnalysisManager)
46
c52c2132 47AliAnalysisManager *AliAnalysisManager::fgAnalysisManager = NULL;
48
c52c2132 49//______________________________________________________________________________
50AliAnalysisManager::AliAnalysisManager(const char *name, const char *title)
51 :TNamed(name,title),
52 fTree(NULL),
8c0ab8e8 53 fInputEventHandler(NULL),
54 fOutputEventHandler(NULL),
55 fMCtruthEventHandler(NULL),
c52c2132 56 fCurrentEntry(-1),
8c0ab8e8 57 fNSysInfo(0),
c52c2132 58 fMode(kLocalAnalysis),
59 fInitOK(kFALSE),
60 fDebug(0),
37a26056 61 fTasks(NULL),
62 fTopTasks(NULL),
c52c2132 63 fZombies(NULL),
64 fContainers(NULL),
65 fInputs(NULL),
66 fOutputs(NULL)
d3106602 67{
68// Default constructor.
c52c2132 69 fgAnalysisManager = this;
70 fTasks = new TObjArray();
71 fTopTasks = new TObjArray();
72 fZombies = new TObjArray();
73 fContainers = new TObjArray();
74 fInputs = new TObjArray();
37153431 75 fOutputs = new TObjArray();
b1310ef5 76 SetEventLoop(kTRUE);
d3106602 77}
78
79//______________________________________________________________________________
80AliAnalysisManager::AliAnalysisManager(const AliAnalysisManager& other)
c52c2132 81 :TNamed(other),
327eaf46 82 fTree(NULL),
8c0ab8e8 83 fInputEventHandler(NULL),
84 fOutputEventHandler(NULL),
85 fMCtruthEventHandler(NULL),
c52c2132 86 fCurrentEntry(-1),
8c0ab8e8 87 fNSysInfo(0),
c52c2132 88 fMode(other.fMode),
89 fInitOK(other.fInitOK),
90 fDebug(other.fDebug),
37a26056 91 fTasks(NULL),
92 fTopTasks(NULL),
c52c2132 93 fZombies(NULL),
94 fContainers(NULL),
95 fInputs(NULL),
96 fOutputs(NULL)
d3106602 97{
98// Copy constructor.
37a26056 99 fTasks = new TObjArray(*other.fTasks);
100 fTopTasks = new TObjArray(*other.fTopTasks);
101 fZombies = new TObjArray(*other.fZombies);
c52c2132 102 fContainers = new TObjArray(*other.fContainers);
103 fInputs = new TObjArray(*other.fInputs);
104 fOutputs = new TObjArray(*other.fOutputs);
105 fgAnalysisManager = this;
d3106602 106}
107
108//______________________________________________________________________________
109AliAnalysisManager& AliAnalysisManager::operator=(const AliAnalysisManager& other)
110{
111// Assignment
112 if (&other != this) {
c52c2132 113 TNamed::operator=(other);
54cff064 114 fInputEventHandler = other.fInputEventHandler;
6bb2b24f 115 fOutputEventHandler = other.fOutputEventHandler;
116 fMCtruthEventHandler = other.fMCtruthEventHandler;
c52c2132 117 fTree = NULL;
118 fCurrentEntry = -1;
8c0ab8e8 119 fNSysInfo = other.fNSysInfo;
c52c2132 120 fMode = other.fMode;
37a26056 121 fInitOK = other.fInitOK;
c52c2132 122 fDebug = other.fDebug;
37a26056 123 fTasks = new TObjArray(*other.fTasks);
124 fTopTasks = new TObjArray(*other.fTopTasks);
125 fZombies = new TObjArray(*other.fZombies);
c52c2132 126 fContainers = new TObjArray(*other.fContainers);
127 fInputs = new TObjArray(*other.fInputs);
128 fOutputs = new TObjArray(*other.fOutputs);
129 fgAnalysisManager = this;
d3106602 130 }
131 return *this;
132}
133
134//______________________________________________________________________________
135AliAnalysisManager::~AliAnalysisManager()
136{
137// Destructor.
d3106602 138 if (fTasks) {fTasks->Delete(); delete fTasks;}
139 if (fTopTasks) delete fTopTasks;
140 if (fZombies) delete fZombies;
c52c2132 141 if (fContainers) {fContainers->Delete(); delete fContainers;}
142 if (fInputs) delete fInputs;
143 if (fOutputs) delete fOutputs;
144 if (fgAnalysisManager==this) fgAnalysisManager = NULL;
d3106602 145}
c52c2132 146
d3106602 147//______________________________________________________________________________
327eaf46 148Int_t AliAnalysisManager::GetEntry(Long64_t entry, Int_t getall)
149{
150// Read one entry of the tree or a whole branch.
c52c2132 151 if (fDebug > 1) {
152 cout << "== AliAnalysisManager::GetEntry()" << endl;
153 }
154 fCurrentEntry = entry;
327eaf46 155 return fTree ? fTree->GetTree()->GetEntry(entry, getall) : 0;
156}
157
158//______________________________________________________________________________
159void AliAnalysisManager::Init(TTree *tree)
d3106602 160{
161 // The Init() function is called when the selector needs to initialize
162 // a new tree or chain. Typically here the branch addresses of the tree
163 // will be set. It is normaly not necessary to make changes to the
164 // generated code, but the routine can be extended by the user if needed.
165 // Init() will be called many times when running with PROOF.
327eaf46 166 if (!tree) return;
c52c2132 167 if (fDebug > 1) {
f3d59a0d 168 printf("->AliAnalysisManager::InitTree(%s)\n", tree->GetName());
c52c2132 169 }
fdb458ec 170
f3d59a0d 171 // Call InitTree of EventHandler
36e82a52 172 if (fOutputEventHandler) {
173 if (fMode == kProofAnalysis) {
f3d59a0d 174 fOutputEventHandler->Init(0x0, "proof");
36e82a52 175 } else {
f3d59a0d 176 fOutputEventHandler->Init(0x0, "local");
36e82a52 177 }
178 }
179
fdb458ec 180 if (fInputEventHandler) {
36e82a52 181 if (fMode == kProofAnalysis) {
f3d59a0d 182 fInputEventHandler->Init(tree, "proof");
36e82a52 183 } else {
f3d59a0d 184 fInputEventHandler->Init(tree, "local");
36e82a52 185 }
e7ae3836 186 } else {
187 // If no input event handler we need to get the tree once
188 // for the chain
189 if(!tree->GetTree()) tree->LoadTree(0);
36e82a52 190 }
e7ae3836 191
36e82a52 192
193 if (fMCtruthEventHandler) {
194 if (fMode == kProofAnalysis) {
f3d59a0d 195 fMCtruthEventHandler->Init(0x0, "proof");
36e82a52 196 } else {
f3d59a0d 197 fMCtruthEventHandler->Init(0x0, "local");
36e82a52 198 }
fdb458ec 199 }
200
c52c2132 201 if (!fInitOK) InitAnalysis();
202 if (!fInitOK) return;
327eaf46 203 fTree = tree;
204 AliAnalysisDataContainer *top = (AliAnalysisDataContainer*)fInputs->At(0);
c52c2132 205 if (!top) {
206 cout<<"Error: No top input container !" <<endl;
207 return;
37153431 208 }
327eaf46 209 top->SetData(tree);
981f2614 210 if (fDebug > 1) {
211 printf("<-AliAnalysisManager::Init(%s)\n", tree->GetName());
212 }
d3106602 213}
214
d3106602 215//______________________________________________________________________________
327eaf46 216void AliAnalysisManager::SlaveBegin(TTree *tree)
d3106602 217{
218 // The SlaveBegin() function is called after the Begin() function.
219 // When running with PROOF SlaveBegin() is called on each slave server.
220 // The tree argument is deprecated (on PROOF 0 is passed).
c52c2132 221 if (fDebug > 1) {
981f2614 222 cout << "->AliAnalysisManager::SlaveBegin()" << endl;
37153431 223 }
6073f8c9 224
f3d59a0d 225 // Call Init of EventHandler
226 if (fOutputEventHandler) {
227 if (fMode == kProofAnalysis) {
228 fOutputEventHandler->Init("proof");
229 } else {
230 fOutputEventHandler->Init("local");
231 }
232 }
233
234 if (fInputEventHandler) {
235 fInputEventHandler->SetInputTree(tree);
236 if (fMode == kProofAnalysis) {
237 fInputEventHandler->Init("proof");
238 } else {
239 fInputEventHandler->Init("local");
240 }
241 }
242
243 if (fMCtruthEventHandler) {
244 if (fMode == kProofAnalysis) {
245 fMCtruthEventHandler->Init("proof");
246 } else {
247 fMCtruthEventHandler->Init("local");
248 }
249 }
250
c52c2132 251 TIter next(fTasks);
252 AliAnalysisTask *task;
253 // Call CreateOutputObjects for all tasks
c5a87c56 254 while ((task=(AliAnalysisTask*)next())) {
255 TDirectory *curdir = gDirectory;
c52c2132 256 task->CreateOutputObjects();
c5a87c56 257 if (curdir) curdir->cd();
36e82a52 258 }
259
981f2614 260 if (fDebug > 1) {
261 cout << "<-AliAnalysisManager::SlaveBegin()" << endl;
262 }
d3106602 263}
264
265//______________________________________________________________________________
327eaf46 266Bool_t AliAnalysisManager::Notify()
267{
268 // The Notify() function is called when a new file is opened. This
269 // can be either for a new TTree in a TChain or when when a new TTree
270 // is started when using PROOF. It is normaly not necessary to make changes
271 // to the generated code, but the routine can be extended by the
272 // user if needed. The return value is currently not used.
53faeca4 273 if (fTree) {
fdb458ec 274 TFile *curfile = fTree->GetCurrentFile();
275 if (curfile && fDebug>1) printf("AliAnalysisManager::Notify() file: %s\n", curfile->GetName());
276 TIter next(fTasks);
277 AliAnalysisTask *task;
278 // Call Notify for all tasks
279 while ((task=(AliAnalysisTask*)next()))
280 task->Notify();
281
6073f8c9 282 // Call Notify of the event handlers
283 if (fInputEventHandler) {
284 fInputEventHandler->Notify(curfile->GetName());
285 }
286
287 if (fOutputEventHandler) {
288 fOutputEventHandler->Notify(curfile->GetName());
289 }
290
fdb458ec 291 if (fMCtruthEventHandler) {
292 fMCtruthEventHandler->Notify(curfile->GetName());
293 }
890126ab 294
fdb458ec 295 }
296 return kTRUE;
327eaf46 297}
298
299//______________________________________________________________________________
300Bool_t AliAnalysisManager::Process(Long64_t entry)
d3106602 301{
302 // The Process() function is called for each entry in the tree (or possibly
303 // keyed object in the case of PROOF) to be processed. The entry argument
304 // specifies which entry in the currently loaded tree is to be processed.
305 // It can be passed to either TTree::GetEntry() or TBranch::GetEntry()
306 // to read either all or the required parts of the data. When processing
307 // keyed objects with PROOF, the object is already loaded and is available
308 // via the fObject pointer.
309 //
310 // This function should contain the "body" of the analysis. It can contain
311 // simple or elaborate selection criteria, run algorithms on the data
312 // of the event and typically fill histograms.
313
314 // WARNING when a selector is used with a TChain, you must use
315 // the pointer to the current TTree to call GetEntry(entry).
316 // The entry is always the local entry number in the current tree.
317 // Assuming that fChain is the pointer to the TChain being processed,
318 // use fChain->GetTree()->GetEntry(entry).
c52c2132 319 if (fDebug > 1) {
36e82a52 320 cout << "->AliAnalysisManager::Process(" << entry << ")" << endl;
37153431 321 }
ed97dc98 322 if (fInputEventHandler) fInputEventHandler ->BeginEvent(entry);
323 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent(entry);
324 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(entry);
6bb2b24f 325
327eaf46 326 GetEntry(entry);
327 ExecAnalysis();
981f2614 328 if (fDebug > 1) {
329 cout << "<-AliAnalysisManager::Process()" << endl;
330 }
327eaf46 331 return kTRUE;
d3106602 332}
333
334//______________________________________________________________________________
c52c2132 335void AliAnalysisManager::PackOutput(TList *target)
d3106602 336{
981f2614 337 // Pack all output data containers in the output list. Called at SlaveTerminate
338 // stage in PROOF case for each slave.
c52c2132 339 if (fDebug > 1) {
981f2614 340 cout << "->AliAnalysisManager::PackOutput()" << endl;
c52c2132 341 }
342 if (!target) {
343 Error("PackOutput", "No target. Aborting.");
344 return;
37153431 345 }
6073f8c9 346 if (fInputEventHandler) fInputEventHandler ->Terminate();
6bb2b24f 347 if (fOutputEventHandler) fOutputEventHandler ->Terminate();
348 if (fMCtruthEventHandler) fMCtruthEventHandler->Terminate();
8c9485b2 349
c52c2132 350 if (fMode == kProofAnalysis) {
351 TIter next(fOutputs);
352 AliAnalysisDataContainer *output;
353 while ((output=(AliAnalysisDataContainer*)next())) {
ef788aee 354 if (output->GetData() && !output->IsSpecialOutput()) {
8167b1d0 355 if (output->GetProducer()->IsPostEventLoop()) continue;
ca78991b 356
357 const char *filename = output->GetFileName();
358 if (!(strcmp(filename, "default"))) {
359 if (fOutputEventHandler) filename = fOutputEventHandler->GetOutputFileName();
360 }
361 if (strlen(filename)) {
362 TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
363 TDirectory *opwd = gDirectory;
364 if (file) file->cd();
365 else file = new TFile(filename, "RECREATE");
366 if (file->IsZombie()) continue;
367 // Clear file list to release object ownership to user.
368 // Save data to file, then close.
369 file->Clear();
370 output->GetData()->Write();
371 file->Close();
372 // Set null directory to histograms and trees.
373 TMethodCall callEnv;
374 if (output->GetData()->IsA())
375 callEnv.InitWithPrototype(output->GetData()->IsA(), "SetDirectory", "TDirectory*");
376 if (callEnv.IsValid()) {
377 callEnv.SetParam(Long_t(0));
378 callEnv.Execute(output->GetData());
379 }
380 // Restore current directory
381 if (opwd) opwd->cd();
382 }
8167b1d0 383 AliAnalysisDataWrapper *wrap = output->ExportData();
384 // Output wrappers must delete data after merging (AG 13/11/07)
385 wrap->SetDeleteData(kTRUE);
386 if (fDebug > 1) printf(" Packing container %s...\n", output->GetName());
387 target->Add(wrap);
388 }
13ef3bb0 389 // Special outputs files are closed and copied on the remote location
390 if (output->IsSpecialOutput() && strlen(output->GetFileName())) {
d0864eb4 391 TDirectory *opwd = gDirectory;
13ef3bb0 392 TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(output->GetFileName());
393 if (!file) continue;
ef788aee 394 file->cd();
395 if (output->GetData()) output->GetData()->Write();
13ef3bb0 396 file->Close();
d0864eb4 397 if (opwd) opwd->cd();
13ef3bb0 398 if (strlen(fSpecialOutputLocation.Data())) {
399 TString remote = fSpecialOutputLocation;
400 remote += "/";
ef788aee 401 Int_t gid = gROOT->ProcessLine("gProofServ->GetGroupId();");
d0864eb4 402 remote += Form("%s_%d_", gSystem->HostName(), gid);
13ef3bb0 403 remote += output->GetFileName();
404 TFile::Cp(output->GetFileName(), remote.Data());
ca78991b 405 } else {
406 // No special location specified-> use TProofFile as merging utility
407 char line[256];
408 sprintf(line, "((TList*)0x%lx)->Add(new TProofFile(\"%s\"));",
409 (ULong_t)target, output->GetFileName());
410 gROOT->ProcessLine(line);
13ef3bb0 411 }
412 }
c52c2132 413 }
414 }
c52c2132 415 if (fDebug > 1) {
981f2614 416 printf("<-AliAnalysisManager::PackOutput: output list contains %d containers\n", target->GetSize());
37153431 417 }
c52c2132 418}
419
420//______________________________________________________________________________
981f2614 421void AliAnalysisManager::ImportWrappers(TList *source)
c52c2132 422{
981f2614 423// Import data in output containers from wrappers coming in source.
424 if (fDebug > 1) {
425 cout << "->AliAnalysisManager::ImportWrappers()" << endl;
426 }
327eaf46 427 TIter next(fOutputs);
981f2614 428 AliAnalysisDataContainer *cont;
429 AliAnalysisDataWrapper *wrap;
430 Int_t icont = 0;
c52c2132 431 while ((cont=(AliAnalysisDataContainer*)next())) {
ef788aee 432 if (cont->GetProducer()->IsPostEventLoop() ||
433 cont->IsSpecialOutput()) continue;
981f2614 434 wrap = (AliAnalysisDataWrapper*)source->FindObject(cont->GetName());
435 if (!wrap && fDebug>1) {
436 printf("(WW) ImportWrappers: container %s not found in analysis output !\n", cont->GetName());
c52c2132 437 continue;
438 }
981f2614 439 icont++;
440 if (fDebug > 1) printf(" Importing data for container %s\n", wrap->GetName());
441 if (cont->GetFileName()) printf(" -> %s\n", cont->GetFileName());
442 cont->ImportData(wrap);
c52c2132 443 }
981f2614 444 if (fDebug > 1) {
445 cout << "<-AliAnalysisManager::ImportWrappers(): "<< icont << " containers imported" << endl;
446 }
c52c2132 447}
448
449//______________________________________________________________________________
450void AliAnalysisManager::UnpackOutput(TList *source)
451{
ca78991b 452 // Called by AliAnalysisSelector::Terminate only on the client.
981f2614 453 if (fDebug > 1) {
454 cout << "->AliAnalysisManager::UnpackOutput()" << endl;
455 }
c52c2132 456 if (!source) {
981f2614 457 Error("UnpackOutput", "No target. Aborting.");
c52c2132 458 return;
459 }
460 if (fDebug > 1) {
c52c2132 461 printf(" Source list contains %d containers\n", source->GetSize());
327eaf46 462 }
c52c2132 463
981f2614 464 if (fMode == kProofAnalysis) ImportWrappers(source);
37153431 465
981f2614 466 TIter next(fOutputs);
c52c2132 467 AliAnalysisDataContainer *output;
468 while ((output=(AliAnalysisDataContainer*)next())) {
c52c2132 469 if (!output->GetData()) continue;
b1310ef5 470 // Check if there are client tasks that run post event loop
471 if (output->HasConsumers()) {
472 // Disable event loop semaphore
473 output->SetPostEventLoop(kTRUE);
474 TObjArray *list = output->GetConsumers();
475 Int_t ncons = list->GetEntriesFast();
476 for (Int_t i=0; i<ncons; i++) {
477 AliAnalysisTask *task = (AliAnalysisTask*)list->At(i);
478 task->CheckNotify(kTRUE);
479 // If task is active, execute it
480 if (task->IsPostEventLoop() && task->IsActive()) {
481 if (fDebug > 1) {
482 cout << "== Executing post event loop task " << task->GetName() << endl;
483 }
484 task->ExecuteTask();
485 }
486 }
487 }
c52c2132 488 // Check if the output need to be written to a file.
489 const char *filename = output->GetFileName();
8ca08916 490 if (!(strcmp(filename, "default"))) {
6bb2b24f 491 if (fOutputEventHandler) filename = fOutputEventHandler->GetOutputFileName();
8ca08916 492 }
493
c52c2132 494 if (!filename || !strlen(filename)) continue;
495 TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
e7ae3836 496 TDirectory *opwd = gDirectory;
c52c2132 497 if (file) file->cd();
498 else file = new TFile(filename, "RECREATE");
499 if (file->IsZombie()) continue;
500 // Reparent data to this file
501 TMethodCall callEnv;
502 if (output->GetData()->IsA())
503 callEnv.InitWithPrototype(output->GetData()->IsA(), "SetDirectory", "TDirectory*");
504 if (callEnv.IsValid()) {
505 callEnv.SetParam((Long_t) file);
506 callEnv.Execute(output->GetData());
507 }
b1109411 508 output->GetData()->Write();
e7ae3836 509 if (opwd) opwd->cd();
c52c2132 510 }
981f2614 511 if (fDebug > 1) {
512 cout << "<-AliAnalysisManager::UnpackOutput()" << endl;
513 }
d3106602 514}
515
516//______________________________________________________________________________
517void AliAnalysisManager::Terminate()
518{
519 // The Terminate() function is the last function to be called during
520 // a query. It always runs on the client, it can be used to present
c52c2132 521 // the results graphically.
522 if (fDebug > 1) {
981f2614 523 cout << "->AliAnalysisManager::Terminate()" << endl;
c52c2132 524 }
327eaf46 525 AliAnalysisTask *task;
c52c2132 526 TIter next(fTasks);
327eaf46 527 // Call Terminate() for tasks
c52c2132 528 while ((task=(AliAnalysisTask*)next())) task->Terminate();
981f2614 529 if (fDebug > 1) {
530 cout << "<-AliAnalysisManager::Terminate()" << endl;
531 }
8c9485b2 532 //
6073f8c9 533 if (fInputEventHandler) fInputEventHandler ->TerminateIO();
534 if (fOutputEventHandler) fOutputEventHandler ->TerminateIO();
535 if (fMCtruthEventHandler) fMCtruthEventHandler->TerminateIO();
8c0ab8e8 536 TIter next1(fOutputs);
537 AliAnalysisDataContainer *output;
538 while ((output=(AliAnalysisDataContainer*)next1())) {
539 // Close all files at output
540 const char *filename = output->GetFileName();
541 if (!(strcmp(filename, "default"))) {
542 if (fOutputEventHandler) filename = fOutputEventHandler->GetOutputFileName();
543 }
544
545 if (!filename || !strlen(filename)) continue;
546 TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
547 if (!file || file->IsZombie()) continue;
548 file->Close();
549 }
550
551 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
552 if (getsysInfo) {
553 TDirectory *cdir = gDirectory;
554 TFile f("syswatch.root", "RECREATE");
555 if (!f.IsZombie()) {
556 TTree *tree = AliSysInfo::MakeTree("syswatch.log");
557 tree->SetMarkerStyle(kCircle);
558 tree->SetMarkerColor(kBlue);
559 tree->SetMarkerSize(0.5);
560 if (!gROOT->IsBatch()) {
561 tree->SetAlias("event", "id0");
562 tree->SetAlias("memUSED", "mi.fMemUsed");
563 tree->SetAlias("userCPU", "pI.fCpuUser");
564 TCanvas *c = new TCanvas("SysInfo","SysInfo",10,10,800,600);
565 c->Divide(2,1,0.01,0.01);
566 c->cd(1);
567 tree->Draw("memUSED:event","","", 1234567890, 0);
568 c->cd(2);
569 tree->Draw("userCPU:event","","", 1234567890, 0);
570 }
571 tree->Write();
572 f.Close();
573 delete tree;
574 }
575 if (cdir) cdir->cd();
576 }
d3106602 577}
578
579//______________________________________________________________________________
580void AliAnalysisManager::AddTask(AliAnalysisTask *task)
581{
582// Adds a user task to the global list of tasks.
583 task->SetActive(kFALSE);
584 fTasks->Add(task);
585}
586
587//______________________________________________________________________________
588AliAnalysisTask *AliAnalysisManager::GetTask(const char *name) const
589{
590// Retreive task by name.
591 if (!fTasks) return NULL;
592 return (AliAnalysisTask*)fTasks->FindObject(name);
593}
594
595//______________________________________________________________________________
596AliAnalysisDataContainer *AliAnalysisManager::CreateContainer(const char *name,
c52c2132 597 TClass *datatype, EAliAnalysisContType type, const char *filename)
d3106602 598{
599// Create a data container of a certain type. Types can be:
c52c2132 600// kExchangeContainer = 0, used to exchange date between tasks
d3106602 601// kInputContainer = 1, used to store input data
602// kOutputContainer = 2, used for posting results
b1310ef5 603 if (fContainers->FindObject(name)) {
604 Error("CreateContainer","A container named %s already defined !\n",name);
605 return NULL;
606 }
d3106602 607 AliAnalysisDataContainer *cont = new AliAnalysisDataContainer(name, datatype);
608 fContainers->Add(cont);
609 switch (type) {
610 case kInputContainer:
611 fInputs->Add(cont);
612 break;
613 case kOutputContainer:
614 fOutputs->Add(cont);
8c0ab8e8 615 if (filename && strlen(filename)) {
616 cont->SetFileName(filename);
617 cont->SetDataOwned(kFALSE); // data owned by the file
618 }
d3106602 619 break;
c52c2132 620 case kExchangeContainer:
d3106602 621 break;
622 }
623 return cont;
624}
625
626//______________________________________________________________________________
627Bool_t AliAnalysisManager::ConnectInput(AliAnalysisTask *task, Int_t islot,
628 AliAnalysisDataContainer *cont)
629{
630// Connect input of an existing task to a data container.
631 if (!fTasks->FindObject(task)) {
632 AddTask(task);
c52c2132 633 Warning("ConnectInput", "Task %s not registered. Now owned by analysis manager", task->GetName());
d3106602 634 }
635 Bool_t connected = task->ConnectInput(islot, cont);
636 return connected;
637}
638
639//______________________________________________________________________________
640Bool_t AliAnalysisManager::ConnectOutput(AliAnalysisTask *task, Int_t islot,
641 AliAnalysisDataContainer *cont)
642{
643// Connect output of an existing task to a data container.
644 if (!fTasks->FindObject(task)) {
645 AddTask(task);
c52c2132 646 Warning("ConnectOutput", "Task %s not registered. Now owned by analysis manager", task->GetName());
d3106602 647 }
648 Bool_t connected = task->ConnectOutput(islot, cont);
649 return connected;
650}
651
652//______________________________________________________________________________
653void AliAnalysisManager::CleanContainers()
654{
655// Clean data from all containers that have already finished all client tasks.
656 TIter next(fContainers);
657 AliAnalysisDataContainer *cont;
658 while ((cont=(AliAnalysisDataContainer *)next())) {
659 if (cont->IsOwnedData() &&
660 cont->IsDataReady() &&
661 cont->ClientsExecuted()) cont->DeleteData();
662 }
663}
664
665//______________________________________________________________________________
666Bool_t AliAnalysisManager::InitAnalysis()
667{
668// Initialization of analysis chain of tasks. Should be called after all tasks
669// and data containers are properly connected
670 // Check for input/output containers
671 fInitOK = kFALSE;
d3106602 672 // Check for top tasks (depending only on input data containers)
673 if (!fTasks->First()) {
c52c2132 674 Error("InitAnalysis", "Analysis has no tasks !");
d3106602 675 return kFALSE;
676 }
677 TIter next(fTasks);
678 AliAnalysisTask *task;
679 AliAnalysisDataContainer *cont;
680 Int_t ntop = 0;
681 Int_t nzombies = 0;
327eaf46 682 Bool_t iszombie = kFALSE;
683 Bool_t istop = kTRUE;
d3106602 684 Int_t i;
685 while ((task=(AliAnalysisTask*)next())) {
327eaf46 686 istop = kTRUE;
687 iszombie = kFALSE;
d3106602 688 Int_t ninputs = task->GetNinputs();
d3106602 689 for (i=0; i<ninputs; i++) {
690 cont = task->GetInputSlot(i)->GetContainer();
691 if (!cont) {
327eaf46 692 if (!iszombie) {
d3106602 693 task->SetZombie();
694 fZombies->Add(task);
695 nzombies++;
327eaf46 696 iszombie = kTRUE;
d3106602 697 }
c52c2132 698 Error("InitAnalysis", "Input slot %d of task %s has no container connected ! Declared zombie...",
699 i, task->GetName());
d3106602 700 }
327eaf46 701 if (iszombie) continue;
d3106602 702 // Check if cont is an input container
327eaf46 703 if (istop && !fInputs->FindObject(cont)) istop=kFALSE;
d3106602 704 // Connect to parent task
705 }
327eaf46 706 if (istop) {
d3106602 707 ntop++;
708 fTopTasks->Add(task);
709 }
710 }
711 if (!ntop) {
c52c2132 712 Error("InitAnalysis", "No top task defined. At least one task should be connected only to input containers");
d3106602 713 return kFALSE;
714 }
715 // Check now if there are orphan tasks
716 for (i=0; i<ntop; i++) {
717 task = (AliAnalysisTask*)fTopTasks->At(i);
718 task->SetUsed();
719 }
720 Int_t norphans = 0;
721 next.Reset();
722 while ((task=(AliAnalysisTask*)next())) {
723 if (!task->IsUsed()) {
724 norphans++;
c52c2132 725 Warning("InitAnalysis", "Task %s is orphan", task->GetName());
d3106602 726 }
727 }
728 // Check the task hierarchy (no parent task should depend on data provided
729 // by a daughter task)
730 for (i=0; i<ntop; i++) {
731 task = (AliAnalysisTask*)fTopTasks->At(i);
732 if (task->CheckCircularDeps()) {
c52c2132 733 Error("InitAnalysis", "Found illegal circular dependencies between following tasks:");
d3106602 734 PrintStatus("dep");
735 return kFALSE;
736 }
737 }
b1310ef5 738 // Check that all containers feeding post-event loop tasks are in the outputs list
739 TIter nextcont(fContainers); // loop over all containers
740 while ((cont=(AliAnalysisDataContainer*)nextcont())) {
741 if (!cont->IsPostEventLoop() && !fOutputs->FindObject(cont)) {
742 if (cont->HasConsumers()) {
743 // Check if one of the consumers is post event loop
744 TIter nextconsumer(cont->GetConsumers());
745 while ((task=(AliAnalysisTask*)nextconsumer())) {
746 if (task->IsPostEventLoop()) {
747 fOutputs->Add(cont);
748 break;
749 }
750 }
751 }
752 }
753 }
327eaf46 754 fInitOK = kTRUE;
d3106602 755 return kTRUE;
756}
757
758//______________________________________________________________________________
759void AliAnalysisManager::PrintStatus(Option_t *option) const
760{
761// Print task hierarchy.
8c0ab8e8 762 if (!fInitOK) {
763 Info("PrintStatus", "Analysis manager %s not initialized : call InitAnalysis() first", GetName());
764 return;
765 }
766 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
767 if (getsysInfo)
768 Info("PrintStatus", "System information will be collected each %lld events", fNSysInfo);
d3106602 769 TIter next(fTopTasks);
770 AliAnalysisTask *task;
771 while ((task=(AliAnalysisTask*)next()))
772 task->PrintTask(option);
773}
774
775//______________________________________________________________________________
776void AliAnalysisManager::ResetAnalysis()
777{
778// Reset all execution flags and clean containers.
779 CleanContainers();
780}
781
c52c2132 782//______________________________________________________________________________
8c0ab8e8 783void AliAnalysisManager::StartAnalysis(const char *type, TTree *tree, Long64_t nentries, Long64_t firstentry)
c52c2132 784{
785// Start analysis for this manager. Analysis task can be: LOCAL, PROOF or GRID.
8c0ab8e8 786// Process nentries starting from firstentry
c52c2132 787 if (!fInitOK) {
788 Error("StartAnalysis","Analysis manager was not initialized !");
789 return;
790 }
791 if (fDebug>1) {
792 cout << "StartAnalysis: " << GetName() << endl;
793 }
794 TString anaType = type;
795 anaType.ToLower();
796 fMode = kLocalAnalysis;
797 if (tree) {
798 if (anaType.Contains("proof")) fMode = kProofAnalysis;
799 else if (anaType.Contains("grid")) fMode = kGridAnalysis;
800 }
801 if (fMode == kGridAnalysis) {
802 Warning("StartAnalysis", "GRID analysis mode not implemented. Running local.");
981f2614 803 fMode = kLocalAnalysis;
804 }
d86ed856 805 char line[256];
efd53803 806 SetEventLoop(kFALSE);
b1310ef5 807 // Disable all branches if requested and set event loop mode
efd53803 808 if (tree) {
b1310ef5 809 if (TestBit(kDisableBranches)) {
810 printf("Disabling all branches...\n");
811// tree->SetBranchStatus("*",0); // not yet working
812 }
efd53803 813 SetEventLoop(kTRUE);
814 }
efd53803 815
8c0ab8e8 816 TChain *chain = 0;
817 TString ttype = "TTree";
818 if (tree->IsA() == TChain::Class()) {
819 chain = (TChain*)tree;
6b742510 820 if (!chain || !chain->GetListOfFiles()->First()) {
821 Error("StartAnalysis", "Cannot process null or empty chain...");
822 return;
823 }
8c0ab8e8 824 ttype = "TChain";
825 }
9b33830a 826
827 // Initialize locally all tasks
828 TIter next(fTasks);
829 AliAnalysisTask *task;
efd53803 830 while ((task=(AliAnalysisTask*)next())) {
efd53803 831 task->LocalInit();
832 }
833
c52c2132 834 switch (fMode) {
835 case kLocalAnalysis:
836 if (!tree) {
981f2614 837 TIter next(fTasks);
838 AliAnalysisTask *task;
839 // Call CreateOutputObjects for all tasks
c5a87c56 840 while ((task=(AliAnalysisTask*)next())) {
841 TDirectory *curdir = gDirectory;
842 task->CreateOutputObjects();
843 if (curdir) curdir->cd();
844 }
c52c2132 845 ExecAnalysis();
981f2614 846 Terminate();
c52c2132 847 return;
848 }
849 // Run tree-based analysis via AliAnalysisSelector
c52c2132 850 cout << "===== RUNNING LOCAL ANALYSIS " << GetName() << " ON TREE " << tree->GetName() << endl;
851 sprintf(line, "AliAnalysisSelector *selector = new AliAnalysisSelector((AliAnalysisManager*)0x%lx);",(ULong_t)this);
852 gROOT->ProcessLine(line);
8c0ab8e8 853 sprintf(line, "((%s*)0x%lx)->Process(selector, \"\",%lld, %lld);",ttype.Data(),(ULong_t)tree, nentries, firstentry);
c52c2132 854 gROOT->ProcessLine(line);
855 break;
856 case kProofAnalysis:
857 if (!gROOT->GetListOfProofs() || !gROOT->GetListOfProofs()->GetEntries()) {
858 printf("StartAnalysis: no PROOF!!!\n");
859 return;
860 }
861 sprintf(line, "gProof->AddInput((TObject*)0x%lx);", (ULong_t)this);
862 gROOT->ProcessLine(line);
863 if (chain) {
864 chain->SetProof();
865 cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON CHAIN " << chain->GetName() << endl;
8c0ab8e8 866 chain->Process("AliAnalysisSelector", "", nentries, firstentry);
c52c2132 867 } else {
868 printf("StartAnalysis: no chain\n");
869 return;
870 }
871 break;
872 case kGridAnalysis:
873 Warning("StartAnalysis", "GRID analysis mode not implemented. Running local.");
874 }
875}
876
d86ed856 877//______________________________________________________________________________
878void AliAnalysisManager::StartAnalysis(const char *type, const char *dataset, Long64_t nentries, Long64_t firstentry)
879{
880// Start analysis for this manager on a given dataset. Analysis task can be:
881// LOCAL, PROOF or GRID. Process nentries starting from firstentry.
882 if (!fInitOK) {
883 Error("StartAnalysis","Analysis manager was not initialized !");
884 return;
885 }
886 if (fDebug>1) {
887 cout << "StartAnalysis: " << GetName() << endl;
888 }
889 TString anaType = type;
890 anaType.ToLower();
891 if (!anaType.Contains("proof")) {
892 Error("Cannot process datasets in %s mode. Try PROOF.", type);
893 return;
894 }
895 fMode = kProofAnalysis;
896 char line[256];
897 SetEventLoop(kTRUE);
898 // Set the dataset flag
899 TObject::SetBit(kUseDataSet);
900 fTree = 0;
901
902 // Initialize locally all tasks
903 TIter next(fTasks);
904 AliAnalysisTask *task;
905 while ((task=(AliAnalysisTask*)next())) {
906 task->LocalInit();
907 }
908
909 if (!gROOT->GetListOfProofs() || !gROOT->GetListOfProofs()->GetEntries()) {
910 printf("StartAnalysis: no PROOF!!!\n");
911 return;
912 }
913 sprintf(line, "gProof->AddInput((TObject*)0x%lx);", (ULong_t)this);
914 gROOT->ProcessLine(line);
915 sprintf(line, "gProof->GetDataSet(\"%s\");", dataset);
916 if (!gROOT->ProcessLine(line)) {
917 Error("StartAnalysis", "Dataset %s not found", dataset);
918 return;
919 }
920 sprintf(line, "gProof->Process(\"%s\", \"AliAnalysisSelector\", \"\", %lld, %lld);",
921 dataset, nentries, firstentry);
922 cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON DATASET " << dataset << endl;
923 gROOT->ProcessLine(line);
924}
925
d3106602 926//______________________________________________________________________________
927void AliAnalysisManager::ExecAnalysis(Option_t *option)
928{
929// Execute analysis.
8c0ab8e8 930 static Long64_t ncalls = 0;
931 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
932 if (getsysInfo && ncalls==0) AliSysInfo::AddStamp("Start", (Int_t)ncalls);
933 ncalls++;
327eaf46 934 if (!fInitOK) {
c52c2132 935 Error("ExecAnalysis", "Analysis manager was not initialized !");
327eaf46 936 return;
937 }
d3106602 938 AliAnalysisTask *task;
327eaf46 939 // Check if the top tree is active.
940 if (fTree) {
941 TIter next(fTasks);
942 // De-activate all tasks
943 while ((task=(AliAnalysisTask*)next())) task->SetActive(kFALSE);
944 AliAnalysisDataContainer *cont = (AliAnalysisDataContainer*)fInputs->At(0);
945 if (!cont) {
c52c2132 946 Error("ExecAnalysis","Cannot execute analysis in TSelector mode without at least one top container");
327eaf46 947 return;
948 }
949 cont->SetData(fTree); // This will notify all consumers
ed97dc98 950 Long64_t entry = fTree->GetTree()->GetReadEntry();
951
6bb2b24f 952//
c3701689 953// Call BeginEvent() for optional input/output and MC services
ed97dc98 954 if (fInputEventHandler) fInputEventHandler ->BeginEvent(entry);
955 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent(entry);
956 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(entry);
6bb2b24f 957//
958// Execute the tasks
276941c8 959// TIter next1(cont->GetConsumers());
960 TIter next1(fTopTasks);
327eaf46 961 while ((task=(AliAnalysisTask*)next1())) {
c52c2132 962 if (fDebug >1) {
963 cout << " Executing task " << task->GetName() << endl;
964 }
6bb2b24f 965
327eaf46 966 task->ExecuteTask(option);
967 }
6bb2b24f 968//
969// Call FinishEvent() for optional output and MC services
6073f8c9 970 if (fInputEventHandler) fInputEventHandler ->FinishEvent();
6bb2b24f 971 if (fOutputEventHandler) fOutputEventHandler ->FinishEvent();
972 if (fMCtruthEventHandler) fMCtruthEventHandler->FinishEvent();
8c0ab8e8 973 // Gather system information if requested
974 if (getsysInfo && ((ncalls%fNSysInfo)==0))
975 AliSysInfo::AddStamp(Form("Event#%lld",ncalls),(Int_t)ncalls);
327eaf46 976 return;
977 }
978 // The event loop is not controlled by TSelector
6bb2b24f 979//
c3701689 980// Call BeginEvent() for optional input/output and MC services
ed97dc98 981 if (fInputEventHandler) fInputEventHandler ->BeginEvent(-1);
982 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent(-1);
983 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(-1);
327eaf46 984 TIter next2(fTopTasks);
985 while ((task=(AliAnalysisTask*)next2())) {
986 task->SetActive(kTRUE);
c52c2132 987 if (fDebug > 1) {
988 cout << " Executing task " << task->GetName() << endl;
989 }
d3106602 990 task->ExecuteTask(option);
327eaf46 991 }
6bb2b24f 992//
993// Call FinishEvent() for optional output and MC services
6073f8c9 994 if (fInputEventHandler) fInputEventHandler ->FinishEvent();
995 if (fOutputEventHandler) fOutputEventHandler ->FinishEvent();
6bb2b24f 996 if (fMCtruthEventHandler) fMCtruthEventHandler->FinishEvent();
d3106602 997}
998
999//______________________________________________________________________________
1000void AliAnalysisManager::FinishAnalysis()
1001{
1002// Finish analysis.
1003}