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