1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 // Author: Andrei Gheata, 31/05/2006
19 //==============================================================================
20 // AliAnalysysManager - Manager analysis class. Allows creation of several
21 // analysis tasks and data containers storing their input/output. Allows
22 // connecting/chaining tasks via shared data containers. Serializes the current
23 // event for all tasks depending only on initial input data.
24 //==============================================================================
26 //==============================================================================
28 #include <Riostream.h>
36 #include <TMethodCall.h>
41 #include <TStopwatch.h>
43 #include "AliAnalysisSelector.h"
44 #include "AliAnalysisGrid.h"
45 #include "AliAnalysisTask.h"
46 #include "AliAnalysisDataContainer.h"
47 #include "AliAnalysisDataSlot.h"
48 #include "AliVEventHandler.h"
49 #include "AliVEventPool.h"
50 #include "AliSysInfo.h"
51 #include "AliAnalysisManager.h"
53 ClassImp(AliAnalysisManager)
55 AliAnalysisManager *AliAnalysisManager::fgAnalysisManager = NULL;
56 TString AliAnalysisManager::fgCommonFileName = "";
58 //______________________________________________________________________________
59 AliAnalysisManager::AliAnalysisManager(const char *name, const char *title)
62 fInputEventHandler(NULL),
63 fOutputEventHandler(NULL),
64 fMCtruthEventHandler(NULL),
68 fMode(kLocalAnalysis),
71 fSpecialOutputLocation(""),
84 // Default constructor.
85 fgAnalysisManager = this;
86 fgCommonFileName = "AnalysisResults.root";
87 fTasks = new TObjArray();
88 fTopTasks = new TObjArray();
89 fZombies = new TObjArray();
90 fContainers = new TObjArray();
91 fInputs = new TObjArray();
92 fOutputs = new TObjArray();
94 TObject::SetObjectStat(kFALSE);
97 //______________________________________________________________________________
98 AliAnalysisManager::AliAnalysisManager(const AliAnalysisManager& other)
101 fInputEventHandler(NULL),
102 fOutputEventHandler(NULL),
103 fMCtruthEventHandler(NULL),
108 fInitOK(other.fInitOK),
109 fDebug(other.fDebug),
110 fSpecialOutputLocation(""),
124 fTasks = new TObjArray(*other.fTasks);
125 fTopTasks = new TObjArray(*other.fTopTasks);
126 fZombies = new TObjArray(*other.fZombies);
127 fContainers = new TObjArray(*other.fContainers);
128 fInputs = new TObjArray(*other.fInputs);
129 fOutputs = new TObjArray(*other.fOutputs);
130 fgCommonFileName = "AnalysisResults.root";
131 fgAnalysisManager = this;
132 TObject::SetObjectStat(kFALSE);
135 //______________________________________________________________________________
136 AliAnalysisManager& AliAnalysisManager::operator=(const AliAnalysisManager& other)
139 if (&other != this) {
140 TNamed::operator=(other);
141 fInputEventHandler = other.fInputEventHandler;
142 fOutputEventHandler = other.fOutputEventHandler;
143 fMCtruthEventHandler = other.fMCtruthEventHandler;
144 fEventPool = other.fEventPool;
147 fNSysInfo = other.fNSysInfo;
149 fInitOK = other.fInitOK;
150 fDebug = other.fDebug;
151 fTasks = new TObjArray(*other.fTasks);
152 fTopTasks = new TObjArray(*other.fTopTasks);
153 fZombies = new TObjArray(*other.fZombies);
154 fContainers = new TObjArray(*other.fContainers);
155 fInputs = new TObjArray(*other.fInputs);
156 fOutputs = new TObjArray(*other.fOutputs);
158 fCommonOutput = NULL;
161 fExtraFiles = other.fExtraFiles;
162 fgCommonFileName = "AnalysisResults.root";
163 fgAnalysisManager = this;
168 //______________________________________________________________________________
169 AliAnalysisManager::~AliAnalysisManager()
172 if (fTasks) {fTasks->Delete(); delete fTasks;}
173 if (fTopTasks) delete fTopTasks;
174 if (fZombies) delete fZombies;
175 if (fContainers) {fContainers->Delete(); delete fContainers;}
176 if (fInputs) delete fInputs;
177 if (fOutputs) delete fOutputs;
178 if (fGridHandler) delete fGridHandler;
179 if (fgAnalysisManager==this) fgAnalysisManager = NULL;
180 TObject::SetObjectStat(kTRUE);
183 //______________________________________________________________________________
184 Int_t AliAnalysisManager::GetEntry(Long64_t entry, Int_t getall)
186 // Read one entry of the tree or a whole branch.
187 fCurrentEntry = entry;
188 return fTree ? fTree->GetTree()->GetEntry(entry, getall) : 0;
191 //______________________________________________________________________________
192 Bool_t AliAnalysisManager::Init(TTree *tree)
194 // The Init() function is called when the selector needs to initialize
195 // a new tree or chain. Typically here the branch addresses of the tree
196 // will be set. It is normaly not necessary to make changes to the
197 // generated code, but the routine can be extended by the user if needed.
198 // Init() will be called many times when running with PROOF.
199 Bool_t init = kFALSE;
200 if (!tree) return kFALSE; // Should not happen - protected in selector caller
202 printf("->AliAnalysisManager::Init(%s)\n", tree->GetName());
204 // Call InitTree of EventHandler
205 if (fOutputEventHandler) {
206 if (fMode == kProofAnalysis) {
207 init = fOutputEventHandler->Init(0x0, "proof");
209 init = fOutputEventHandler->Init(0x0, "local");
212 Error("Init", "Output event handler failed to initialize");
217 if (fInputEventHandler) {
218 if (fMode == kProofAnalysis) {
219 init = fInputEventHandler->Init(tree, "proof");
221 init = fInputEventHandler->Init(tree, "local");
224 Error("Init", "Input event handler failed to initialize tree");
228 // If no input event handler we need to get the tree once
230 if(!tree->GetTree()) {
231 Long64_t readEntry = tree->LoadTree(0);
232 if (readEntry == -2) {
233 Error("Init", "Input tree has no entry. Aborting");
239 if (fMCtruthEventHandler) {
240 if (fMode == kProofAnalysis) {
241 init = fMCtruthEventHandler->Init(0x0, "proof");
243 init = fMCtruthEventHandler->Init(0x0, "local");
246 Error("Init", "MC event handler failed to initialize");
251 if (!fInitOK) InitAnalysis();
252 if (!fInitOK) return kFALSE;
254 AliAnalysisDataContainer *top = fCommonInput;
255 if (!top) top = (AliAnalysisDataContainer*)fInputs->At(0);
257 Error("Init","No top input container !");
262 printf("<-AliAnalysisManager::Init(%s)\n", tree->GetName());
267 //______________________________________________________________________________
268 void AliAnalysisManager::SlaveBegin(TTree *tree)
270 // The SlaveBegin() function is called after the Begin() function.
271 // When running with PROOF SlaveBegin() is called on each slave server.
272 // The tree argument is deprecated (on PROOF 0 is passed).
273 if (fDebug > 0) printf("->AliAnalysisManager::SlaveBegin()\n");
274 static Bool_t isCalled = kFALSE;
275 Bool_t init = kFALSE;
276 Bool_t initOK = kTRUE;
278 TDirectory *curdir = gDirectory;
279 // Call SlaveBegin only once in case of mixing
280 if (isCalled && fMode==kMixingAnalysis) return;
281 // Call Init of EventHandler
282 if (fOutputEventHandler) {
283 if (fMode == kProofAnalysis) {
284 // Merging AOD's in PROOF via TProofOutputFile
285 if (fDebug > 1) printf(" Initializing AOD output file %s...\n", fOutputEventHandler->GetOutputFileName());
286 init = fOutputEventHandler->Init("proof");
287 if (!init) msg = "Failed to initialize output handler on worker";
289 init = fOutputEventHandler->Init("local");
290 if (!init) msg = "Failed to initialize output handler";
293 if (!fSelector) Error("SlaveBegin", "Selector not set");
294 else if (!init) {fSelector->Abort(msg); fSelector->SetStatus(-1);}
297 if (fInputEventHandler) {
298 fInputEventHandler->SetInputTree(tree);
299 if (fMode == kProofAnalysis) {
300 init = fInputEventHandler->Init("proof");
301 if (!init) msg = "Failed to initialize input handler on worker";
303 init = fInputEventHandler->Init("local");
304 if (!init) msg = "Failed to initialize input handler";
307 if (!fSelector) Error("SlaveBegin", "Selector not set");
308 else if (!init) {fSelector->Abort(msg); fSelector->SetStatus(-1);}
311 if (fMCtruthEventHandler) {
312 if (fMode == kProofAnalysis) {
313 init = fMCtruthEventHandler->Init("proof");
314 if (!init) msg = "Failed to initialize MC handler on worker";
316 init = fMCtruthEventHandler->Init("local");
317 if (!init) msg = "Failed to initialize MC handler";
320 if (!fSelector) Error("SlaveBegin", "Selector not set");
321 else if (!init) {fSelector->Abort(msg); fSelector->SetStatus(-1);}
323 if (curdir) curdir->cd();
327 AliAnalysisTask *task;
328 // Call CreateOutputObjects for all tasks
329 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
331 while ((task=(AliAnalysisTask*)next())) {
333 task->CreateOutputObjects();
334 if (getsysInfo) AliSysInfo::AddStamp(Form("%s_CREATEOUTOBJ",task->ClassName()), 0, itask, 0);
336 if (curdir) curdir->cd();
338 if (fDebug > 0) printf("<-AliAnalysisManager::SlaveBegin()\n");
341 //______________________________________________________________________________
342 Bool_t AliAnalysisManager::Notify()
344 // The Notify() function is called when a new file is opened. This
345 // can be either for a new TTree in a TChain or when when a new TTree
346 // is started when using PROOF. It is normaly not necessary to make changes
347 // to the generated code, but the routine can be extended by the
348 // user if needed. The return value is currently not used.
349 if (!fTree) return kFALSE;
351 TFile *curfile = fTree->GetCurrentFile();
353 Error("Notify","No current file");
357 if (fDebug > 0) printf("->AliAnalysisManager::Notify() file: %s\n", curfile->GetName());
359 AliAnalysisTask *task;
360 // Call Notify for all tasks
361 while ((task=(AliAnalysisTask*)next()))
364 // Call Notify of the event handlers
365 if (fInputEventHandler) {
366 fInputEventHandler->Notify(curfile->GetName());
369 if (fOutputEventHandler) {
370 fOutputEventHandler->Notify(curfile->GetName());
373 if (fMCtruthEventHandler) {
374 fMCtruthEventHandler->Notify(curfile->GetName());
376 if (fDebug > 0) printf("<-AliAnalysisManager::Notify()\n");
380 //______________________________________________________________________________
381 Bool_t AliAnalysisManager::Process(Long64_t entry)
383 // The Process() function is called for each entry in the tree (or possibly
384 // keyed object in the case of PROOF) to be processed. The entry argument
385 // specifies which entry in the currently loaded tree is to be processed.
386 // It can be passed to either TTree::GetEntry() or TBranch::GetEntry()
387 // to read either all or the required parts of the data. When processing
388 // keyed objects with PROOF, the object is already loaded and is available
389 // via the fObject pointer.
391 // This function should contain the "body" of the analysis. It can contain
392 // simple or elaborate selection criteria, run algorithms on the data
393 // of the event and typically fill histograms.
395 // WARNING when a selector is used with a TChain, you must use
396 // the pointer to the current TTree to call GetEntry(entry).
397 // The entry is always the local entry number in the current tree.
398 // Assuming that fChain is the pointer to the TChain being processed,
399 // use fChain->GetTree()->GetEntry(entry).
400 if (fDebug > 0) printf("->AliAnalysisManager::Process(%lld)\n", entry);
402 if (fInputEventHandler) fInputEventHandler ->BeginEvent(entry);
403 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent(entry);
404 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(entry);
408 if (fDebug > 0) printf("<-AliAnalysisManager::Process()\n");
412 //______________________________________________________________________________
413 void AliAnalysisManager::PackOutput(TList *target)
415 // Pack all output data containers in the output list. Called at SlaveTerminate
416 // stage in PROOF case for each slave.
417 if (fDebug > 0) printf("->AliAnalysisManager::PackOutput()\n");
419 Error("PackOutput", "No target. Aborting.");
422 if (fInputEventHandler) fInputEventHandler ->Terminate();
423 if (fOutputEventHandler) fOutputEventHandler ->Terminate();
424 if (fMCtruthEventHandler) fMCtruthEventHandler->Terminate();
426 // Call FinishTaskOutput() for each event loop task (not called for
427 // post-event loop tasks - use Terminate() fo those)
428 TIter nexttask(fTasks);
429 AliAnalysisTask *task;
430 while ((task=(AliAnalysisTask*)nexttask())) {
431 if (!task->IsPostEventLoop()) {
432 if (fDebug > 0) printf("->FinishTaskOutput: task %s\n", task->GetName());
433 task->FinishTaskOutput();
434 if (fDebug > 0) printf("<-FinishTaskOutput: task %s\n", task->GetName());
438 if (fMode == kProofAnalysis) {
439 TIter next(fOutputs);
440 AliAnalysisDataContainer *output;
441 Bool_t isManagedByHandler = kFALSE;
442 while ((output=(AliAnalysisDataContainer*)next())) {
443 // Do not consider outputs of post event loop tasks
444 isManagedByHandler = kFALSE;
445 if (output->GetProducer()->IsPostEventLoop()) continue;
446 const char *filename = output->GetFileName();
447 if (!(strcmp(filename, "default")) && fOutputEventHandler) {
448 isManagedByHandler = kTRUE;
449 printf("#### Handler output. Extra: %s\n", fExtraFiles.Data());
450 filename = fOutputEventHandler->GetOutputFileName();
452 // Check if data was posted to this container. If not, issue an error.
453 if (!output->GetData() && !isManagedByHandler) {
454 Error("PackOutput", "No data for output container %s. Forgot to PostData ?", output->GetName());
457 if (!output->IsSpecialOutput()) {
459 if (strlen(filename) && !isManagedByHandler) {
460 // Backup current folder
461 TDirectory *opwd = gDirectory;
462 // File resident outputs.
463 // Check first if the file exists.
464 TString open_option = "RECREATE";
465 if (!gSystem->AccessPathName(output->GetFileName())) open_option = "UPDATE";
466 TFile *file = AliAnalysisManager::OpenFile(output, open_option, kTRUE);
467 // Clear file list to release object ownership to user.
469 // Save data to file, then close.
470 if (output->GetData()->InheritsFrom(TCollection::Class())) {
471 // If data is a collection, we set the name of the collection
472 // as the one of the container and we save as a single key.
473 TCollection *coll = (TCollection*)output->GetData();
474 coll->SetName(output->GetName());
475 coll->Write(output->GetName(), TObject::kSingleKey);
477 if (output->GetData()->InheritsFrom(TTree::Class())) {
478 TTree *tree = (TTree*)output->GetData();
479 // tree->SetDirectory(file);
482 output->GetData()->Write();
485 if (fDebug > 1) printf("PackOutput %s: memory merge, file resident output\n", output->GetName());
487 printf(" file %s listing content:\n", filename);
491 output->SetFile(NULL);
492 // Restore current directory
493 if (opwd) opwd->cd();
495 // Memory-resident outputs
496 if (fDebug > 1) printf("PackOutput %s: memory merge memory resident output\n", filename);
498 AliAnalysisDataWrapper *wrap = 0;
499 if (isManagedByHandler) {
500 wrap = new AliAnalysisDataWrapper(fOutputEventHandler->GetTree());
501 wrap->SetName(output->GetName());
503 else wrap =output->ExportData();
504 // Output wrappers must NOT delete data after merging - the user owns them
505 wrap->SetDeleteData(kFALSE);
508 // Special outputs. The file must be opened and connected to the container.
509 TDirectory *opwd = gDirectory;
510 TFile *file = output->GetFile();
512 AliAnalysisTask *producer = output->GetProducer();
514 "File %s for special container %s was NOT opened in %s::CreateOutputObjects !!!",
515 output->GetFileName(), output->GetName(), producer->ClassName());
518 TString outFilename = file->GetName();
519 if (fDebug > 1) printf("PackOutput %s: special output\n", output->GetName());
520 if (isManagedByHandler) {
521 // Terminate IO for files managed by the output handler
522 // file->Write() moved to AOD handler (A.G. 11.01.10)
523 // if (file) file->Write();
524 if (file && fDebug > 2) {
525 printf(" handled file %s listing content:\n", file->GetName());
528 fOutputEventHandler->TerminateIO();
531 // Release object ownership to users after writing data to file
532 if (output->GetData()->InheritsFrom(TCollection::Class())) {
533 // If data is a collection, we set the name of the collection
534 // as the one of the container and we save as a single key.
535 TCollection *coll = (TCollection*)output->GetData();
536 coll->SetName(output->GetName());
537 coll->Write(output->GetName(), TObject::kSingleKey);
539 if (output->GetData()->InheritsFrom(TTree::Class())) {
540 TTree *tree = (TTree*)output->GetData();
541 tree->SetDirectory(file);
544 output->GetData()->Write();
549 printf(" file %s listing content:\n", output->GetFileName());
553 output->SetFile(NULL);
555 // Restore current directory
556 if (opwd) opwd->cd();
557 // Check if a special output location was provided or the output files have to be merged
558 if (strlen(fSpecialOutputLocation.Data())) {
559 TString remote = fSpecialOutputLocation;
561 Int_t gid = gROOT->ProcessLine("gProofServ->GetGroupId();");
562 if (remote.BeginsWith("alien://")) {
563 gROOT->ProcessLine("TGrid::Connect(\"alien://pcapiserv01.cern.ch:10000\", gProofServ->GetUser());");
564 remote += outFilename;
565 remote.ReplaceAll(".root", Form("_%d.root", gid));
567 remote += Form("%s_%d_", gSystem->HostName(), gid);
568 remote += outFilename;
571 Info("PackOutput", "Output file for container %s to be copied \n at: %s. No merging.",
572 output->GetName(), remote.Data());
573 TFile::Cp ( outFilename.Data(), remote.Data() );
574 // Copy extra outputs
575 if (fExtraFiles.Length() && isManagedByHandler) {
576 TObjArray *arr = fExtraFiles.Tokenize(" ");
578 TIter nextfilename(arr);
579 while ((os=(TObjString*)nextfilename())) {
580 outFilename = os->GetString();
581 remote = fSpecialOutputLocation;
583 if (remote.BeginsWith("alien://")) {
584 remote += outFilename;
585 remote.ReplaceAll(".root", Form("_%d.root", gid));
587 remote += Form("%s_%d_", gSystem->HostName(), gid);
588 remote += outFilename;
591 Info("PackOutput", "Extra AOD file %s to be copied \n at: %s. No merging.",
592 outFilename.Data(), remote.Data());
593 TFile::Cp ( outFilename.Data(), remote.Data() );
598 // No special location specified-> use TProofOutputFile as merging utility
599 // The file at this output slot must be opened in CreateOutputObjects
600 if (fDebug > 1) printf(" File for container %s to be merged via file merger...\n", output->GetName());
605 if (fDebug > 0) printf("<-AliAnalysisManager::PackOutput: output list contains %d containers\n", target->GetSize());
608 //______________________________________________________________________________
609 void AliAnalysisManager::ImportWrappers(TList *source)
611 // Import data in output containers from wrappers coming in source.
612 if (fDebug > 0) printf("->AliAnalysisManager::ImportWrappers()\n");
613 TIter next(fOutputs);
614 AliAnalysisDataContainer *cont;
615 AliAnalysisDataWrapper *wrap;
617 Bool_t inGrid = (fMode == kGridAnalysis)?kTRUE:kFALSE;
618 TDirectory *cdir = gDirectory;
619 while ((cont=(AliAnalysisDataContainer*)next())) {
621 if (cont->GetProducer()->IsPostEventLoop() && !inGrid) continue;
622 if (cont->IsRegisterDataset()) continue;
623 const char *filename = cont->GetFileName();
624 Bool_t isManagedByHandler = kFALSE;
625 if (!(strcmp(filename, "default")) && fOutputEventHandler) {
626 isManagedByHandler = kTRUE;
627 filename = fOutputEventHandler->GetOutputFileName();
629 if (cont->IsSpecialOutput() || inGrid) {
630 if (strlen(fSpecialOutputLocation.Data())) continue;
631 // Copy merged file from PROOF scratch space.
632 // In case of grid the files are already in the current directory.
634 if (isManagedByHandler && fExtraFiles.Length()) {
635 // Copy extra registered dAOD files.
636 TObjArray *arr = fExtraFiles.Tokenize(" ");
638 TIter nextfilename(arr);
639 while ((os=(TObjString*)nextfilename())) GetFileFromWrapper(os->GetString(), source);
642 if (!GetFileFromWrapper(filename, source)) continue;
644 // Normally we should connect data from the copied file to the
645 // corresponding output container, but it is not obvious how to do this
646 // automatically if several objects in file...
647 TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
648 if (!f) f = TFile::Open(filename, "READ");
650 Error("ImportWrappers", "Cannot open file %s in read-only mode", filename);
654 // Cd to the directory pointed by the container
655 TString folder = cont->GetFolderName();
656 if (!folder.IsNull()) f->cd(folder);
657 // Try to fetch first an object having the container name.
658 obj = gDirectory->Get(cont->GetName());
660 Warning("ImportWrappers", "Could not import object for container %s in file %s:%s.\n Object will not be available in Terminate()",
661 cont->GetName(), filename, cont->GetFolderName());
664 wrap = new AliAnalysisDataWrapper(obj);
665 wrap->SetDeleteData(kFALSE);
667 if (!wrap) wrap = (AliAnalysisDataWrapper*)source->FindObject(cont->GetName());
669 Error("ImportWrappers","Container %s not found in analysis output !", cont->GetName());
674 printf(" Importing data for container %s\n", cont->GetName());
675 if (strlen(filename)) printf(" -> file %s\n", filename);
678 cont->ImportData(wrap);
680 if (cdir) cdir->cd();
681 if (fDebug > 0) printf("<-AliAnalysisManager::ImportWrappers(): %d containers imported\n", icont);
684 //______________________________________________________________________________
685 void AliAnalysisManager::UnpackOutput(TList *source)
687 // Called by AliAnalysisSelector::Terminate only on the client.
688 if (fDebug > 0) printf("->AliAnalysisManager::UnpackOutput()\n");
690 Error("UnpackOutput", "No target. Aborting.");
693 if (fDebug > 1) printf(" Source list contains %d containers\n", source->GetSize());
695 if (fMode == kProofAnalysis) ImportWrappers(source);
697 TIter next(fOutputs);
698 AliAnalysisDataContainer *output;
699 while ((output=(AliAnalysisDataContainer*)next())) {
700 if (!output->GetData()) continue;
701 // Check if there are client tasks that run post event loop
702 if (output->HasConsumers()) {
703 // Disable event loop semaphore
704 output->SetPostEventLoop(kTRUE);
705 TObjArray *list = output->GetConsumers();
706 Int_t ncons = list->GetEntriesFast();
707 for (Int_t i=0; i<ncons; i++) {
708 AliAnalysisTask *task = (AliAnalysisTask*)list->At(i);
709 task->CheckNotify(kTRUE);
710 // If task is active, execute it
711 if (task->IsPostEventLoop() && task->IsActive()) {
712 if (fDebug > 0) printf("== Executing post event loop task %s\n", task->GetName());
718 if (fDebug > 0) printf("<-AliAnalysisManager::UnpackOutput()\n");
721 //______________________________________________________________________________
722 void AliAnalysisManager::Terminate()
724 // The Terminate() function is the last function to be called during
725 // a query. It always runs on the client, it can be used to present
726 // the results graphically.
727 if (fDebug > 0) printf("->AliAnalysisManager::Terminate()\n");
728 AliAnalysisTask *task;
729 AliAnalysisDataContainer *output;
732 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
733 // Call Terminate() for tasks
735 while (!IsSkipTerminate() && (task=(AliAnalysisTask*)next())) {
736 // Save all the canvases produced by the Terminate
737 TString pictname = Form("%s_%s", task->GetName(), task->ClassName());
740 AliSysInfo::AddStamp(Form("%s_TERMINATE",task->ClassName()),0, itask, 2);
742 if (TObject::TestBit(kSaveCanvases)) {
743 if (!gROOT->IsBatch()) {
744 if (fDebug>0) printf("Waiting 5 sec for %s::Terminate() to finish drawing ...", task->ClassName());
746 while (timer.CpuTime()<5) {
748 gSystem->ProcessEvents();
751 Int_t iend = gROOT->GetListOfCanvases()->GetEntries();
752 if (iend==0) continue;
754 for (Int_t ipict=0; ipict<iend; ipict++) {
755 canvas = (TCanvas*)gROOT->GetListOfCanvases()->At(ipict);
756 if (!canvas) continue;
757 canvas->SaveAs(Form("%s_%02d.gif", pictname.Data(),ipict));
759 gROOT->GetListOfCanvases()->Delete();
763 if (fInputEventHandler) fInputEventHandler ->TerminateIO();
764 if (fOutputEventHandler) fOutputEventHandler ->TerminateIO();
765 if (fMCtruthEventHandler) fMCtruthEventHandler->TerminateIO();
766 TIter next1(fOutputs);
767 TString handlerFile = "";
768 if (fOutputEventHandler) {
769 handlerFile = fOutputEventHandler->GetOutputFileName();
771 while ((output=(AliAnalysisDataContainer*)next1())) {
772 // Special outputs or grid files have the files already closed and written.
773 if (fMode == kGridAnalysis) continue;
774 if (fMode == kProofAnalysis) {
775 if (output->IsSpecialOutput() || output->IsRegisterDataset()) continue;
777 const char *filename = output->GetFileName();
778 TString open_option = "RECREATE";
779 if (!(strcmp(filename, "default"))) continue;
780 if (!strlen(filename)) continue;
781 if (!output->GetData()) continue;
782 TDirectory *opwd = gDirectory;
783 TFile *file = output->GetFile();
784 if (!file) file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
786 printf("Terminate : handlerFile = %s, filename = %s\n",handlerFile.Data(),filename);
787 //if (handlerFile == filename && !gSystem->AccessPathName(filename)) open_option = "UPDATE";
788 if (!gSystem->AccessPathName(filename)) open_option = "UPDATE";
789 file = new TFile(filename, open_option);
791 if (file->IsZombie()) {
792 Error("Terminate", "Cannot open output file %s", filename);
795 output->SetFile(file);
797 // Check for a folder request
798 TString dir = output->GetFolderName();
800 if (!file->GetDirectory(dir)) file->mkdir(dir);
803 if (fDebug > 1) printf(" writing output data %s to file %s:%s\n", output->GetData()->GetName(), file->GetName(), output->GetFolderName());
804 if (output->GetData()->InheritsFrom(TCollection::Class())) {
805 // If data is a collection, we set the name of the collection
806 // as the one of the container and we save as a single key.
807 TCollection *coll = (TCollection*)output->GetData();
808 coll->SetName(output->GetName());
809 coll->Write(output->GetName(), TObject::kSingleKey);
811 if (output->GetData()->InheritsFrom(TTree::Class())) {
812 TTree *tree = (TTree*)output->GetData();
813 tree->SetDirectory(file);
816 output->GetData()->Write();
819 if (opwd) opwd->cd();
822 while ((output=(AliAnalysisDataContainer*)next1())) {
823 // Close all files at output
824 TDirectory *opwd = gDirectory;
825 if (output->GetFile()) {
826 output->GetFile()->Close();
827 output->SetFile(NULL);
828 // Copy merged outputs in alien if requested
829 if (fSpecialOutputLocation.Length() &&
830 fSpecialOutputLocation.BeginsWith("alien://")) {
831 Info("Terminate", "Copy file %s to %s", output->GetFile()->GetName(),fSpecialOutputLocation.Data());
832 TFile::Cp(output->GetFile()->GetName(),
833 Form("%s/%s", fSpecialOutputLocation.Data(), output->GetFile()->GetName()));
836 if (opwd) opwd->cd();
840 TDirectory *cdir = gDirectory;
841 TFile f("syswatch.root", "RECREATE");
845 TTree *tree = AliSysInfo::MakeTree("syswatch.log");
846 tree->SetName("syswatch");
847 tree->SetMarkerStyle(kCircle);
848 tree->SetMarkerColor(kBlue);
849 tree->SetMarkerSize(0.5);
850 if (!gROOT->IsBatch()) {
851 tree->SetAlias("event", "id0");
852 tree->SetAlias("task", "id1");
853 tree->SetAlias("stage", "id2");
854 // Already defined aliases
855 // tree->SetAlias("deltaT","stampSec-stampOldSec");
856 // tree->SetAlias("T","stampSec-first");
857 // tree->SetAlias("deltaVM","(pI.fMemVirtual-pIOld.fMemVirtual)");
858 // tree->SetAlias("VM","pI.fMemVirtual");
859 TCanvas *canvas = new TCanvas("SysInfo","SysInfo",10,10,1200,800);
860 Int_t npads = 1 /*COO plot for all tasks*/ +
861 fTopTasks->GetEntries() /*Exec plot per task*/ +
862 1 /*Terminate plot for all tasks*/ +
865 Int_t iopt = (Int_t)TMath::Sqrt((Double_t)npads);
866 if (npads<iopt*(iopt+1))
867 canvas->Divide(iopt, iopt+1, 0.01, 0.01);
869 canvas->Divide(iopt+1, iopt+1, 0.01, 0.01);
871 // draw the plot of deltaVM for Exec for each task
872 for (Int_t itask=0; itask<fTopTasks->GetEntriesFast(); itask++) {
873 task = (AliAnalysisTask*)fTopTasks->At(itask);
875 cut = Form("task==%d && stage==1", itask);
876 tree->Draw("deltaVM:event",cut,"", 1234567890, 0);
877 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
879 hist->SetTitle(Form("%s: Exec dVM[kB]/event", task->GetName()));
880 hist->GetYaxis()->SetTitle("deltaVM [kB]");
883 // Draw the plot of deltaVM for CreateOutputObjects for all tasks
885 tree->SetMarkerStyle(kFullTriangleUp);
886 tree->SetMarkerColor(kRed);
887 tree->SetMarkerSize(0.8);
888 cut = "task>=0 && task<1000 && stage==0";
889 tree->Draw("deltaVM:sname",cut,"", 1234567890, 0);
890 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
892 hist->SetTitle("Memory in CreateOutputObjects()");
893 hist->GetYaxis()->SetTitle("deltaVM [kB]");
894 hist->GetXaxis()->SetTitle("task");
896 // draw the plot of deltaVM for Terminate for all tasks
898 tree->SetMarkerStyle(kOpenSquare);
899 tree->SetMarkerColor(kMagenta);
900 cut = "task>=0 && task<1000 && stage==2";
901 tree->Draw("deltaVM:sname",cut,"", 1234567890, 0);
902 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
904 hist->SetTitle("Memory in Terminate()");
905 hist->GetYaxis()->SetTitle("deltaVM [kB]");
906 hist->GetXaxis()->SetTitle("task");
910 tree->SetMarkerStyle(kFullCircle);
911 tree->SetMarkerColor(kGreen);
912 cut = Form("task==%d && stage==1",fTopTasks->GetEntriesFast()-1);
913 tree->Draw("VM:event",cut,"", 1234567890, 0);
914 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
916 hist->SetTitle("Virtual memory");
917 hist->GetYaxis()->SetTitle("VM [kB]");
921 tree->SetMarkerStyle(kCircle);
922 tree->SetMarkerColor(kBlue);
923 tree->SetMarkerSize(0.5);
928 if (cdir) cdir->cd();
930 // Validate the output files
931 if (ValidateOutputFiles()) {
933 out.open("outputs_valid", ios::out);
936 if (fDebug > 0) printf("<-AliAnalysisManager::Terminate()\n");
938 //______________________________________________________________________________
939 void AliAnalysisManager::ProfileTask(Int_t itop, const char *option) const
941 // Profiles the task having the itop index in the list of top (first level) tasks.
942 AliAnalysisTask *task = (AliAnalysisTask*)fTopTasks->At(itop);
944 Error("ProfileTask", "There are only %d top tasks in the manager", fTopTasks->GetEntries());
947 ProfileTask(task->GetName(), option);
950 //______________________________________________________________________________
951 void AliAnalysisManager::ProfileTask(const char *name, const char */*option*/) const
953 // Profile a managed task after the execution of the analysis in case NSysInfo
955 if (gSystem->AccessPathName("syswatch.root")) {
956 Error("ProfileTask", "No file syswatch.root found in the current directory");
959 if (gROOT->IsBatch()) return;
960 AliAnalysisTask *task = (AliAnalysisTask*)fTopTasks->FindObject(name);
962 Error("ProfileTask", "No top task named %s known by the manager.", name);
965 Int_t itop = fTopTasks->IndexOf(task);
966 Int_t itask = fTasks->IndexOf(task);
967 // Create canvas with 2 pads: first draw COO + Terminate, second Exec
968 TDirectory *cdir = gDirectory;
969 TFile f("syswatch.root");
970 TTree *tree = (TTree*)f.Get("syswatch");
972 Error("ProfileTask", "No tree named <syswatch> found in file syswatch.root");
975 if (fDebug > 0) printf("=== Profiling task %s (class %s)\n", name, task->ClassName());
976 TCanvas *canvas = new TCanvas(Form("profile_%d",itop),Form("Profile of task %s (class %s)",name,task->ClassName()),10,10,800,600);
977 canvas->Divide(2, 2, 0.01, 0.01);
981 // VM profile for COO and Terminate methods
983 cut = Form("task==%d && (stage==0 || stage==2)",itask);
984 tree->Draw("deltaVM:sname",cut,"", 1234567890, 0);
985 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
987 hist->SetTitle("Alocated VM[kB] for COO and Terminate");
988 hist->GetYaxis()->SetTitle("deltaVM [kB]");
989 hist->GetXaxis()->SetTitle("method");
991 // CPU profile per event
993 cut = Form("task==%d && stage==1",itop);
994 tree->Draw("deltaT:event",cut,"", 1234567890, 0);
995 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
997 hist->SetTitle("Execution time per event");
998 hist->GetYaxis()->SetTitle("CPU/event [s]");
1000 // VM profile for Exec
1002 cut = Form("task==%d && stage==1",itop);
1003 tree->Draw("deltaVM:event",cut,"", 1234567890, 0);
1004 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
1006 hist->SetTitle("Alocated VM[kB] per event");
1007 hist->GetYaxis()->SetTitle("deltaVM [kB]");
1012 if (cdir) cdir->cd();
1015 //______________________________________________________________________________
1016 void AliAnalysisManager::AddTask(AliAnalysisTask *task)
1018 // Adds a user task to the global list of tasks.
1019 if (fTasks->FindObject(task)) {
1020 Warning("AddTask", "Task %s: the same object already added to the analysis manager. Not adding.", task->GetName());
1023 task->SetActive(kFALSE);
1027 //______________________________________________________________________________
1028 AliAnalysisTask *AliAnalysisManager::GetTask(const char *name) const
1030 // Retreive task by name.
1031 if (!fTasks) return NULL;
1032 return (AliAnalysisTask*)fTasks->FindObject(name);
1035 //______________________________________________________________________________
1036 AliAnalysisDataContainer *AliAnalysisManager::CreateContainer(const char *name,
1037 TClass *datatype, EAliAnalysisContType type, const char *filename)
1039 // Create a data container of a certain type. Types can be:
1040 // kExchangeContainer = 0, used to exchange data between tasks
1041 // kInputContainer = 1, used to store input data
1042 // kOutputContainer = 2, used for writing result to a file
1043 // filename: composed by file#folder (e.g. results.root#INCLUSIVE) - will write
1044 // the output object to a folder inside the output file
1045 if (fContainers->FindObject(name)) {
1046 Error("CreateContainer","A container named %s already defined !",name);
1049 AliAnalysisDataContainer *cont = new AliAnalysisDataContainer(name, datatype);
1050 fContainers->Add(cont);
1052 case kInputContainer:
1055 case kOutputContainer:
1056 fOutputs->Add(cont);
1057 if (filename && strlen(filename)) {
1058 cont->SetFileName(filename);
1059 cont->SetDataOwned(kFALSE); // data owned by the file
1062 case kExchangeContainer:
1068 //______________________________________________________________________________
1069 Bool_t AliAnalysisManager::ConnectInput(AliAnalysisTask *task, Int_t islot,
1070 AliAnalysisDataContainer *cont)
1072 // Connect input of an existing task to a data container.
1074 Error("ConnectInput", "Task pointer is NULL");
1077 if (!fTasks->FindObject(task)) {
1079 Info("ConnectInput", "Task %s was not registered. Now owned by analysis manager", task->GetName());
1081 Bool_t connected = task->ConnectInput(islot, cont);
1085 //______________________________________________________________________________
1086 Bool_t AliAnalysisManager::ConnectOutput(AliAnalysisTask *task, Int_t islot,
1087 AliAnalysisDataContainer *cont)
1089 // Connect output of an existing task to a data container.
1091 Error("ConnectOutput", "Task pointer is NULL");
1094 if (!fTasks->FindObject(task)) {
1096 Warning("ConnectOutput", "Task %s not registered. Now owned by analysis manager", task->GetName());
1098 Bool_t connected = task->ConnectOutput(islot, cont);
1102 //______________________________________________________________________________
1103 void AliAnalysisManager::CleanContainers()
1105 // Clean data from all containers that have already finished all client tasks.
1106 TIter next(fContainers);
1107 AliAnalysisDataContainer *cont;
1108 while ((cont=(AliAnalysisDataContainer *)next())) {
1109 if (cont->IsOwnedData() &&
1110 cont->IsDataReady() &&
1111 cont->ClientsExecuted()) cont->DeleteData();
1115 //______________________________________________________________________________
1116 Bool_t AliAnalysisManager::InitAnalysis()
1118 // Initialization of analysis chain of tasks. Should be called after all tasks
1119 // and data containers are properly connected
1120 // Reset flag and remove valid_outputs file if exists
1122 if (!gSystem->AccessPathName("outputs_valid"))
1123 gSystem->Unlink("outputs_valid");
1124 // Check for top tasks (depending only on input data containers)
1125 if (!fTasks->First()) {
1126 Error("InitAnalysis", "Analysis has no tasks !");
1130 AliAnalysisTask *task;
1131 AliAnalysisDataContainer *cont;
1134 Bool_t iszombie = kFALSE;
1135 Bool_t istop = kTRUE;
1137 while ((task=(AliAnalysisTask*)next())) {
1140 Int_t ninputs = task->GetNinputs();
1141 for (i=0; i<ninputs; i++) {
1142 cont = task->GetInputSlot(i)->GetContainer();
1146 fZombies->Add(task);
1150 Error("InitAnalysis", "Input slot %d of task %s has no container connected ! Declared zombie...",
1151 i, task->GetName());
1153 if (iszombie) continue;
1154 // Check if cont is an input container
1155 if (istop && !fInputs->FindObject(cont)) istop=kFALSE;
1156 // Connect to parent task
1160 fTopTasks->Add(task);
1164 Error("InitAnalysis", "No top task defined. At least one task should be connected only to input containers");
1167 // Check now if there are orphan tasks
1168 for (i=0; i<ntop; i++) {
1169 task = (AliAnalysisTask*)fTopTasks->At(i);
1174 while ((task=(AliAnalysisTask*)next())) {
1175 if (!task->IsUsed()) {
1177 Warning("InitAnalysis", "Task %s is orphan", task->GetName());
1180 // Check the task hierarchy (no parent task should depend on data provided
1181 // by a daughter task)
1182 for (i=0; i<ntop; i++) {
1183 task = (AliAnalysisTask*)fTopTasks->At(i);
1184 if (task->CheckCircularDeps()) {
1185 Error("InitAnalysis", "Found illegal circular dependencies between following tasks:");
1190 // Check that all containers feeding post-event loop tasks are in the outputs list
1191 TIter nextcont(fContainers); // loop over all containers
1192 while ((cont=(AliAnalysisDataContainer*)nextcont())) {
1193 if (!cont->IsPostEventLoop() && !fOutputs->FindObject(cont)) {
1194 if (cont->HasConsumers()) {
1195 // Check if one of the consumers is post event loop
1196 TIter nextconsumer(cont->GetConsumers());
1197 while ((task=(AliAnalysisTask*)nextconsumer())) {
1198 if (task->IsPostEventLoop()) {
1199 fOutputs->Add(cont);
1206 // Check if all special output containers have a file name provided
1207 TIter nextout(fOutputs);
1208 while ((cont=(AliAnalysisDataContainer*)nextout())) {
1209 if (cont->IsSpecialOutput() && !strlen(cont->GetFileName())) {
1210 Error("InitAnalysis", "Wrong container %s : a file name MUST be provided for special outputs", cont->GetName());
1218 //______________________________________________________________________________
1219 void AliAnalysisManager::PrintStatus(Option_t *option) const
1221 // Print task hierarchy.
1223 Info("PrintStatus", "Analysis manager %s not initialized : call InitAnalysis() first", GetName());
1226 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
1228 Info("PrintStatus", "System information will be collected each %lld events", fNSysInfo);
1229 TIter next(fTopTasks);
1230 AliAnalysisTask *task;
1231 while ((task=(AliAnalysisTask*)next()))
1232 task->PrintTask(option);
1235 //______________________________________________________________________________
1236 void AliAnalysisManager::ResetAnalysis()
1238 // Reset all execution flags and clean containers.
1242 //______________________________________________________________________________
1243 Long64_t AliAnalysisManager::StartAnalysis(const char *type, TTree *tree, Long64_t nentries, Long64_t firstentry)
1245 // Start analysis for this manager. Analysis task can be: LOCAL, PROOF, GRID or
1246 // MIX. Process nentries starting from firstentry
1249 Error("StartAnalysis","Analysis manager was not initialized !");
1252 if (fDebug > 0) printf("StartAnalysis %s\n",GetName());
1253 TString anaType = type;
1255 fMode = kLocalAnalysis;
1256 Bool_t runlocalinit = kTRUE;
1257 if (anaType.Contains("file")) {
1258 runlocalinit = kFALSE;
1260 if (anaType.Contains("proof")) fMode = kProofAnalysis;
1261 else if (anaType.Contains("grid")) fMode = kGridAnalysis;
1262 else if (anaType.Contains("mix")) fMode = kMixingAnalysis;
1264 if (fMode == kGridAnalysis) {
1265 if (!fGridHandler) {
1266 Error("StartAnalysis", "Cannot start grid analysis without a grid handler.");
1267 Info("===", "Add an AliAnalysisAlien object as plugin for this manager and configure it.");
1270 // Write analysis manager in the analysis file
1271 cout << "===== RUNNING GRID ANALYSIS: " << GetName() << endl;
1272 // run local task configuration
1273 TIter nextTask(fTasks);
1274 AliAnalysisTask *task;
1275 while ((task=(AliAnalysisTask*)nextTask())) {
1278 if (!fGridHandler->StartAnalysis(nentries, firstentry)) {
1279 Info("StartAnalysis", "Grid analysis was stopped and cannot be terminated");
1283 // Terminate grid analysis
1284 if (fSelector && fSelector->GetStatus() == -1) return -1;
1285 if (fGridHandler->GetRunMode() == AliAnalysisGrid::kOffline) return 0;
1286 cout << "===== MERGING OUTPUTS REGISTERED BY YOUR ANALYSIS JOB: " << GetName() << endl;
1287 if (!fGridHandler->MergeOutputs()) {
1288 // Return if outputs could not be merged or if it alien handler
1289 // was configured for offline mode or local testing.
1292 ImportWrappers(NULL);
1297 SetEventLoop(kFALSE);
1298 // Enable event loop mode if a tree was provided
1299 if (tree || fMode==kMixingAnalysis) SetEventLoop(kTRUE);
1302 TString ttype = "TTree";
1303 if (tree && tree->IsA() == TChain::Class()) {
1304 chain = (TChain*)tree;
1305 if (!chain || !chain->GetListOfFiles()->First()) {
1306 Error("StartAnalysis", "Cannot process null or empty chain...");
1312 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
1313 if (getsysInfo) AliSysInfo::AddStamp("Start", 0);
1314 // Initialize locally all tasks (happens for all modes)
1316 AliAnalysisTask *task;
1318 while ((task=(AliAnalysisTask*)next())) {
1321 if (getsysInfo) AliSysInfo::AddStamp("LocalInit_all", 0);
1325 case kLocalAnalysis:
1327 TIter nextT(fTasks);
1328 // Call CreateOutputObjects for all tasks
1330 while ((task=(AliAnalysisTask*)nextT())) {
1331 TDirectory *curdir = gDirectory;
1332 task->CreateOutputObjects();
1333 if (getsysInfo) AliSysInfo::AddStamp(Form("%s_CREATEOUTOBJ",task->ClassName()), 0, itask, 0);
1334 if (curdir) curdir->cd();
1337 if (IsExternalLoop()) {
1338 Info("StartAnalysis", "Initialization done. Event loop is controlled externally.\
1339 \nSetData for top container, call ExecAnalysis in a loop and then Terminate manually");
1346 // Run tree-based analysis via AliAnalysisSelector
1347 cout << "===== RUNNING LOCAL ANALYSIS " << GetName() << " ON TREE " << tree->GetName() << endl;
1348 fSelector = new AliAnalysisSelector(this);
1349 retv = tree->Process(fSelector, "", nentries, firstentry);
1351 case kProofAnalysis:
1352 if (!gROOT->GetListOfProofs() || !gROOT->GetListOfProofs()->GetEntries()) {
1353 Error("StartAnalysis", "No PROOF!!! Aborting.");
1356 sprintf(line, "gProof->AddInput((TObject*)0x%lx);", (ULong_t)this);
1357 gROOT->ProcessLine(line);
1360 cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON CHAIN " << chain->GetName() << endl;
1361 retv = chain->Process("AliAnalysisSelector", "", nentries, firstentry);
1363 Error("StartAnalysis", "No chain!!! Aborting.");
1368 Warning("StartAnalysis", "GRID analysis mode not implemented. Running local.");
1370 case kMixingAnalysis:
1371 // Run event mixing analysis
1373 Error("StartAnalysis", "Cannot run event mixing without event pool");
1376 cout << "===== RUNNING EVENT MIXING ANALYSIS " << GetName() << endl;
1377 fSelector = new AliAnalysisSelector(this);
1378 while ((chain=fEventPool->GetNextChain())) {
1380 // Call NotifyBinChange for all tasks
1381 while ((task=(AliAnalysisTask*)next()))
1382 if (!task->IsPostEventLoop()) task->NotifyBinChange();
1383 retv = chain->Process(fSelector);
1385 Error("StartAnalysis", "Mixing analysis failed");
1389 PackOutput(fSelector->GetOutputList());
1395 //______________________________________________________________________________
1396 Long64_t AliAnalysisManager::StartAnalysis(const char *type, const char *dataset, Long64_t nentries, Long64_t firstentry)
1398 // Start analysis for this manager on a given dataset. Analysis task can be:
1399 // LOCAL, PROOF or GRID. Process nentries starting from firstentry.
1401 Error("StartAnalysis","Analysis manager was not initialized !");
1404 if (fDebug > 0) printf("StartAnalysis %s\n",GetName());
1405 TString anaType = type;
1407 if (!anaType.Contains("proof")) {
1408 Error("StartAnalysis", "Cannot process datasets in %s mode. Try PROOF.", type);
1411 fMode = kProofAnalysis;
1413 SetEventLoop(kTRUE);
1414 // Set the dataset flag
1415 TObject::SetBit(kUseDataSet);
1418 // Initialize locally all tasks
1420 AliAnalysisTask *task;
1421 while ((task=(AliAnalysisTask*)next())) {
1425 if (!gROOT->GetListOfProofs() || !gROOT->GetListOfProofs()->GetEntries()) {
1426 Error("StartAnalysis", "No PROOF!!! Aborting.");
1429 sprintf(line, "gProof->AddInput((TObject*)0x%lx);", (ULong_t)this);
1430 gROOT->ProcessLine(line);
1431 sprintf(line, "gProof->GetDataSet(\"%s\");", dataset);
1432 if (!gROOT->ProcessLine(line)) {
1433 Error("StartAnalysis", "Dataset %s not found", dataset);
1436 sprintf(line, "gProof->Process(\"%s\", \"AliAnalysisSelector\", \"\", %lld, %lld);",
1437 dataset, nentries, firstentry);
1438 cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON DATASET " << dataset << endl;
1439 Long_t retv = (Long_t)gROOT->ProcessLine(line);
1443 //______________________________________________________________________________
1444 TFile *AliAnalysisManager::OpenFile(AliAnalysisDataContainer *cont, const char *option, Bool_t ignoreProof)
1446 // Opens according the option the file specified by cont->GetFileName() and changes
1447 // current directory to cont->GetFolderName(). If the file was already opened, it
1448 // checks if the option UPDATE was preserved. File open via TProofOutputFile can
1449 // be optionally ignored.
1450 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
1451 TString filename = cont->GetFileName();
1453 if (filename.IsNull()) {
1454 ::Error("AliAnalysisManager::OpenFile", "No file name specified for container %s", cont->GetName());
1457 if (mgr->GetAnalysisType()==AliAnalysisManager::kProofAnalysis && cont->IsSpecialOutput()
1459 f = mgr->OpenProofFile(cont,option);
1461 // Check first if the file is already opened
1462 f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
1464 // Check if option "UPDATE" was preserved
1465 TString opt(option);
1467 if ((opt=="UPDATE") && (opt!=f->GetOption()))
1468 ::Info("AliAnalysisManager::OpenFile", "File %s already opened in %s mode!", cont->GetFileName(), f->GetOption());
1470 f = TFile::Open(filename, option);
1473 if (f && !f->IsZombie() && !f->TestBit(TFile::kRecovered)) {
1477 // Check for a folder request
1478 TString dir = cont->GetFolderName();
1479 if (!dir.IsNull()) {
1480 if (!f->GetDirectory(dir)) f->mkdir(dir);
1485 ::Fatal("AliAnalysisManager::OpenFile", "File %s could not be opened", filename.Data());
1486 cont->SetFile(NULL);
1490 //______________________________________________________________________________
1491 TFile *AliAnalysisManager::OpenProofFile(AliAnalysisDataContainer *cont, const char *option)
1493 // Opens a special output file used in PROOF.
1495 TString filename = cont->GetFileName();
1496 if (cont == fCommonOutput) {
1497 if (fOutputEventHandler) filename = fOutputEventHandler->GetOutputFileName();
1498 else Fatal("OpenProofFile","No output container. Aborting.");
1501 if (fMode!=kProofAnalysis || !fSelector) {
1502 Fatal("OpenProofFile","Cannot open PROOF file %s: no PROOF or selector",filename.Data());
1505 if (fSpecialOutputLocation.Length()) {
1506 f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
1508 // Check if option "UPDATE" was preserved
1509 TString opt(option);
1511 if ((opt=="UPDATE") && (opt!=f->GetOption()))
1512 ::Info("OpenProofFile", "File %s already opened in %s mode!", cont->GetFileName(), f->GetOption());
1514 f = new TFile(filename, option);
1516 if (f && !f->IsZombie() && !f->TestBit(TFile::kRecovered)) {
1520 // Check for a folder request
1521 TString dir = cont->GetFolderName();
1523 if (!f->GetDirectory(dir)) f->mkdir(dir);
1528 Fatal("OpenProofFile", "File %s could not be opened", cont->GetFileName());
1529 cont->SetFile(NULL);
1532 // Check if there is already a proof output file in the output list
1533 TObject *pof = fSelector->GetOutputList()->FindObject(filename);
1535 // Get the actual file
1536 line = Form("((TProofOutputFile*)0x%lx)->GetFileName();", (ULong_t)pof);
1537 filename = (const char*)gROOT->ProcessLine(line);
1539 printf("File: %s already booked via TProofOutputFile\n", filename.Data());
1541 f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
1542 if (!f) Fatal("OpenProofFile", "Proof output file found but no file opened for %s", filename.Data());
1543 // Check if option "UPDATE" was preserved
1544 TString opt(option);
1546 if ((opt=="UPDATE") && (opt!=f->GetOption()))
1547 Fatal("OpenProofFile", "File %s already opened, but not in UPDATE mode!", cont->GetFileName());
1549 if (cont->IsRegisterDataset()) {
1550 TString dset_name = filename;
1551 dset_name.ReplaceAll(".root", cont->GetTitle());
1552 dset_name.ReplaceAll(":","_");
1553 if (fDebug>1) printf("Booking dataset: %s\n", dset_name.Data());
1554 line = Form("TProofOutputFile *pf = new TProofOutputFile(\"%s\", \"DROV\", \"%s\");", filename.Data(), dset_name.Data());
1556 if (fDebug>1) printf("Booking TProofOutputFile: %s to be merged\n", filename.Data());
1557 line = Form("TProofOutputFile *pf = new TProofOutputFile(\"%s\");", filename.Data());
1559 if (fDebug > 1) printf("=== %s\n", line.Data());
1560 gROOT->ProcessLine(line);
1561 line = Form("pf->OpenFile(\"%s\");", option);
1562 gROOT->ProcessLine(line);
1565 gROOT->ProcessLine("pf->Print()");
1566 printf(" == proof file name: %s", f->GetName());
1568 // Add to proof output list
1569 line = Form("((TList*)0x%lx)->Add(pf);",(ULong_t)fSelector->GetOutputList());
1570 if (fDebug > 1) printf("=== %s\n", line.Data());
1571 gROOT->ProcessLine(line);
1573 if (f && !f->IsZombie() && !f->TestBit(TFile::kRecovered)) {
1577 // Check for a folder request
1578 TString dir = cont->GetFolderName();
1579 if (!dir.IsNull()) {
1580 if (!f->GetDirectory(dir)) f->mkdir(dir);
1585 Fatal("OpenProofFile", "File %s could not be opened", cont->GetFileName());
1586 cont->SetFile(NULL);
1590 //______________________________________________________________________________
1591 void AliAnalysisManager::ExecAnalysis(Option_t *option)
1593 // Execute analysis.
1594 static Long64_t ncalls = 0;
1595 if (fDebug > 0) printf("MGR: Processing event #%lld\n", ncalls);
1596 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
1597 if (getsysInfo && ((ncalls%fNSysInfo)==0)) AliSysInfo::AddStamp("Exec_start", (Int_t)ncalls);
1600 Error("ExecAnalysis", "Analysis manager was not initialized !");
1603 AliAnalysisTask *task;
1604 // Check if the top tree is active.
1607 // De-activate all tasks
1608 while ((task=(AliAnalysisTask*)next())) task->SetActive(kFALSE);
1609 AliAnalysisDataContainer *cont = fCommonInput;
1610 if (!cont) cont = (AliAnalysisDataContainer*)fInputs->At(0);
1612 Error("ExecAnalysis","Cannot execute analysis in TSelector mode without at least one top container");
1615 cont->SetData(fTree); // This will notify all consumers
1616 Long64_t entry = fTree->GetTree()->GetReadEntry();
1619 // Call BeginEvent() for optional input/output and MC services
1620 if (fInputEventHandler) fInputEventHandler ->BeginEvent(entry);
1621 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent(entry);
1622 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(entry);
1623 if (getsysInfo && ((ncalls%fNSysInfo)==0))
1624 AliSysInfo::AddStamp("Handlers_BeginEvent",(Int_t)ncalls, 1000, 0);
1626 // Execute the tasks
1627 // TIter next1(cont->GetConsumers());
1628 TIter next1(fTopTasks);
1630 while ((task=(AliAnalysisTask*)next1())) {
1632 cout << " Executing task " << task->GetName() << endl;
1634 task->ExecuteTask(option);
1635 if (getsysInfo && ((ncalls%fNSysInfo)==0))
1636 AliSysInfo::AddStamp(task->ClassName(),(Int_t)ncalls, itask, 1);
1640 // Call FinishEvent() for optional output and MC services
1641 if (fInputEventHandler) fInputEventHandler ->FinishEvent();
1642 if (fOutputEventHandler) fOutputEventHandler ->FinishEvent();
1643 if (fMCtruthEventHandler) fMCtruthEventHandler->FinishEvent();
1644 // Gather system information if requested
1645 if (getsysInfo && ((ncalls%fNSysInfo)==0))
1646 AliSysInfo::AddStamp("Handlers_FinishEvent",(Int_t)ncalls, 1000, 1);
1649 // The event loop is not controlled by TSelector
1651 // Call BeginEvent() for optional input/output and MC services
1652 if (fInputEventHandler) fInputEventHandler ->BeginEvent(-1);
1653 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent(-1);
1654 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(-1);
1655 if (getsysInfo && ((ncalls%fNSysInfo)==0))
1656 AliSysInfo::AddStamp("Handlers_BeginEvent",(Int_t)ncalls, 1000, 0);
1657 TIter next2(fTopTasks);
1658 while ((task=(AliAnalysisTask*)next2())) {
1659 task->SetActive(kTRUE);
1661 cout << " Executing task " << task->GetName() << endl;
1663 task->ExecuteTask(option);
1666 // Call FinishEvent() for optional output and MC services
1667 if (fInputEventHandler) fInputEventHandler ->FinishEvent();
1668 if (fOutputEventHandler) fOutputEventHandler ->FinishEvent();
1669 if (fMCtruthEventHandler) fMCtruthEventHandler->FinishEvent();
1670 if (getsysInfo && ((ncalls%fNSysInfo)==0))
1671 AliSysInfo::AddStamp("Handlers_FinishEvent",(Int_t)ncalls, 1000, 1);
1674 //______________________________________________________________________________
1675 void AliAnalysisManager::FinishAnalysis()
1680 //______________________________________________________________________________
1681 void AliAnalysisManager::SetInputEventHandler(AliVEventHandler* handler)
1683 // Set the input event handler and create a container for it.
1684 fInputEventHandler = handler;
1685 fCommonInput = CreateContainer("cAUTO_INPUT", TChain::Class(), AliAnalysisManager::kInputContainer);
1686 // Warning("SetInputEventHandler", " An automatic input container for the input chain was created.\nPlease use: mgr->GetCommonInputContainer() to access it.");
1689 //______________________________________________________________________________
1690 void AliAnalysisManager::SetOutputEventHandler(AliVEventHandler* handler)
1692 // Set the input event handler and create a container for it.
1693 fOutputEventHandler = handler;
1694 fCommonOutput = CreateContainer("cAUTO_OUTPUT", TTree::Class(), AliAnalysisManager::kOutputContainer, "default");
1695 fCommonOutput->SetSpecialOutput();
1696 // Warning("SetOutputEventHandler", " An automatic output container for the output tree was created.\nPlease use: mgr->GetCommonOutputContainer() to access it.");
1699 //______________________________________________________________________________
1700 void AliAnalysisManager::RegisterExtraFile(const char *fname)
1702 // This method is used externally to register output files which are not
1703 // connected to any output container, so that the manager can properly register,
1704 // retrieve or merge them when running in distributed mode. The file names are
1705 // separated by blancs. The method has to be called in MyAnalysisTask::LocalInit().
1706 if (fExtraFiles.Length()) fExtraFiles += " ";
1707 fExtraFiles += fname;
1710 //______________________________________________________________________________
1711 Bool_t AliAnalysisManager::GetFileFromWrapper(const char *filename, TList *source)
1713 // Copy a file from the location specified ina the wrapper with the same name from the source list.
1714 char full_path[512];
1716 TObject *pof = source->FindObject(filename);
1717 if (!pof || !pof->InheritsFrom("TProofOutputFile")) {
1718 Error("GetFileFromWrapper", "TProofOutputFile object not found in output list for file %s", filename);
1721 gROOT->ProcessLine(Form("sprintf((char*)0x%lx, \"%%s\", ((TProofOutputFile*)0x%lx)->GetOutputFileName();)", full_path, pof));
1722 gROOT->ProcessLine(Form("sprintf((char*)0x%lx, \"%%s\", gProof->GetUrl();)", ch_url));
1723 TString clientUrl(ch_url);
1724 TString full_path_str(full_path);
1725 if (clientUrl.Contains("localhost")){
1726 TObjArray* array = full_path_str.Tokenize ( "//" );
1727 TObjString *strobj = ( TObjString *)array->At(1);
1728 TObjArray* arrayPort = strobj->GetString().Tokenize ( ":" );
1729 TObjString *strobjPort = ( TObjString *) arrayPort->At(1);
1730 full_path_str.ReplaceAll(strobj->GetString().Data(),"localhost:PORT");
1731 full_path_str.ReplaceAll(":PORT",Form(":%s",strobjPort->GetString().Data()));
1732 if (fDebug > 1) Info("GetFileFromWrapper","Using tunnel from %s to %s",full_path_str.Data(),filename);
1737 Info("GetFileFromWrapper","Copying file %s from PROOF scratch space", full_path_str.Data());
1738 Bool_t gotit = TFile::Cp(full_path_str.Data(), filename);
1740 Error("GetFileFromWrapper", "Could not get file %s from proof scratch space", filename);
1744 //______________________________________________________________________________
1745 void AliAnalysisManager::GetAnalysisTypeString(TString &type) const
1747 // Fill analysis type in the provided string.
1749 case kLocalAnalysis:
1752 case kProofAnalysis:
1758 case kMixingAnalysis:
1763 //______________________________________________________________________________
1764 Bool_t AliAnalysisManager::ValidateOutputFiles() const
1766 // Validate all output files.
1767 TIter next(fOutputs);
1768 AliAnalysisDataContainer *output;
1769 TDirectory *cdir = gDirectory;
1770 TString openedFiles;
1771 while ((output=(AliAnalysisDataContainer*)next())) {
1772 if (output->IsRegisterDataset()) continue;
1773 TString filename = output->GetFileName();
1774 if (filename == "default") {
1775 if (!fOutputEventHandler) continue;
1776 filename = fOutputEventHandler->GetOutputFileName();
1777 // Main AOD may not be there
1778 if (gSystem->AccessPathName(filename)) continue;
1780 // Check if the file is closed
1781 if (openedFiles.Contains(filename)) continue;;
1782 TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
1784 Warning("ValidateOutputs", "File %s was not closed. Closing.", filename.Data());
1787 file = TFile::Open(filename);
1788 if (!file || file->IsZombie() || file->TestBit(TFile::kRecovered)) {
1789 Error("ValidateOutputs", "Output file <%s> was not created or invalid", filename.Data());
1794 openedFiles += filename;
1801 //______________________________________________________________________________
1802 void AliAnalysisManager::ProgressBar(const char *opname, Long64_t current, Long64_t size, TStopwatch *watch, Bool_t last, Bool_t refresh)
1804 // Implements a nice text mode progress bar.
1805 static Long64_t icount = 0;
1806 static TString oname;
1807 static TString nname;
1808 static Long64_t ocurrent = 0;
1809 static Long64_t osize = 0;
1810 static Int_t oseconds = 0;
1811 static TStopwatch *owatch = 0;
1812 static Bool_t oneoftwo = kFALSE;
1813 static Int_t nrefresh = 0;
1814 static Int_t nchecks = 0;
1815 const char symbol[4] = {'=','\\','|','/'};
1816 char progress[11] = " ";
1817 Int_t ichar = icount%4;
1824 ocurrent = TMath::Abs(current);
1825 osize = TMath::Abs(size);
1826 if (ocurrent > osize) ocurrent=osize;
1836 if (owatch && !last) {
1838 time = owatch->RealTime();
1839 hours = (Int_t)(time/3600.);
1841 minutes = (Int_t)(time/60.);
1843 seconds = (Int_t)time;
1845 if (oseconds==seconds) {
1849 oneoftwo = !oneoftwo;
1853 if (refresh && oneoftwo) {
1855 if (nchecks <= 0) nchecks = nrefresh+1;
1856 Int_t pctdone = (Int_t)(100.*nrefresh/nchecks);
1857 oname = Form(" == %d%% ==", pctdone);
1859 Double_t percent = 100.0*ocurrent/osize;
1860 Int_t nchar = Int_t(percent/10);
1861 if (nchar>10) nchar=10;
1863 for (i=0; i<nchar; i++) progress[i] = '=';
1864 progress[nchar] = symbol[ichar];
1865 for (i=nchar+1; i<10; i++) progress[i] = ' ';
1866 progress[10] = '\0';
1869 if(size<10000) fprintf(stderr, "%s [%10s] %4lld ", oname.Data(), progress, ocurrent);
1870 else if(size<100000) fprintf(stderr, "%s [%10s] %5lld ",oname.Data(), progress, ocurrent);
1871 else fprintf(stderr, "%s [%10s] %7lld ",oname.Data(), progress, ocurrent);
1872 if (time>0.) fprintf(stderr, "[%6.2f %%] TIME %.2d:%.2d:%.2d \r", percent, hours, minutes, seconds);
1873 else fprintf(stderr, "[%6.2f %%]\r", percent);
1874 if (refresh && oneoftwo) oname = nname;
1875 if (owatch) owatch->Continue();
1884 fprintf(stderr, "\n");