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),
72 fSpecialOutputLocation(""),
85 fAutoBranchHandling(kTRUE),
88 // Default constructor.
89 fgAnalysisManager = this;
90 fgCommonFileName = "AnalysisResults.root";
91 fTasks = new TObjArray();
92 fTopTasks = new TObjArray();
93 fZombies = new TObjArray();
94 fContainers = new TObjArray();
95 fInputs = new TObjArray();
96 fOutputs = new TObjArray();
97 fParamCont = new TObjArray();
99 TObject::SetObjectStat(kFALSE);
102 //______________________________________________________________________________
103 AliAnalysisManager::AliAnalysisManager(const AliAnalysisManager& other)
106 fInputEventHandler(NULL),
107 fOutputEventHandler(NULL),
108 fMCtruthEventHandler(NULL),
113 fInitOK(other.fInitOK),
114 fIsRemote(other.fIsRemote),
115 fDebug(other.fDebug),
116 fSpecialOutputLocation(""),
129 fAutoBranchHandling(other.fAutoBranchHandling),
133 fTasks = new TObjArray(*other.fTasks);
134 fTopTasks = new TObjArray(*other.fTopTasks);
135 fZombies = new TObjArray(*other.fZombies);
136 fContainers = new TObjArray(*other.fContainers);
137 fInputs = new TObjArray(*other.fInputs);
138 fOutputs = new TObjArray(*other.fOutputs);
139 fParamCont = new TObjArray(*other.fParamCont);
140 fgCommonFileName = "AnalysisResults.root";
141 fgAnalysisManager = this;
142 TObject::SetObjectStat(kFALSE);
145 //______________________________________________________________________________
146 AliAnalysisManager& AliAnalysisManager::operator=(const AliAnalysisManager& other)
149 if (&other != this) {
150 TNamed::operator=(other);
151 fInputEventHandler = other.fInputEventHandler;
152 fOutputEventHandler = other.fOutputEventHandler;
153 fMCtruthEventHandler = other.fMCtruthEventHandler;
154 fEventPool = other.fEventPool;
157 fNSysInfo = other.fNSysInfo;
159 fInitOK = other.fInitOK;
160 fIsRemote = other.fIsRemote;
161 fDebug = other.fDebug;
162 fTasks = new TObjArray(*other.fTasks);
163 fTopTasks = new TObjArray(*other.fTopTasks);
164 fZombies = new TObjArray(*other.fZombies);
165 fContainers = new TObjArray(*other.fContainers);
166 fInputs = new TObjArray(*other.fInputs);
167 fOutputs = new TObjArray(*other.fOutputs);
168 fParamCont = new TObjArray(*other.fParamCont);
170 fCommonOutput = NULL;
173 fExtraFiles = other.fExtraFiles;
174 fgCommonFileName = "AnalysisResults.root";
175 fgAnalysisManager = this;
176 fAutoBranchHandling = other.fAutoBranchHandling;
177 fTable.Clear("nodelete");
182 //______________________________________________________________________________
183 AliAnalysisManager::~AliAnalysisManager()
186 if (fTasks) {fTasks->Delete(); delete fTasks;}
187 if (fTopTasks) delete fTopTasks;
188 if (fZombies) delete fZombies;
189 if (fContainers) {fContainers->Delete(); delete fContainers;}
190 if (fInputs) delete fInputs;
191 if (fOutputs) delete fOutputs;
192 if (fParamCont) delete fParamCont;
193 if (fGridHandler) delete fGridHandler;
194 if (fInputEventHandler) delete fInputEventHandler;
195 if (fOutputEventHandler) delete fOutputEventHandler;
196 if (fMCtruthEventHandler) delete fMCtruthEventHandler;
197 if (fEventPool) delete fEventPool;
198 if (fgAnalysisManager==this) fgAnalysisManager = NULL;
199 TObject::SetObjectStat(kTRUE);
202 //______________________________________________________________________________
203 Int_t AliAnalysisManager::GetEntry(Long64_t entry, Int_t getall)
205 // Read one entry of the tree or a whole branch.
206 fCurrentEntry = entry;
207 if (!fAutoBranchHandling)
209 return fTree ? fTree->GetTree()->GetEntry(entry, getall) : 0;
212 //______________________________________________________________________________
213 Bool_t AliAnalysisManager::Init(TTree *tree)
215 // The Init() function is called when the selector needs to initialize
216 // a new tree or chain. Typically here the branch addresses of the tree
217 // will be set. It is normaly not necessary to make changes to the
218 // generated code, but the routine can be extended by the user if needed.
219 // Init() will be called many times when running with PROOF.
220 Bool_t init = kFALSE;
221 if (!tree) return kFALSE; // Should not happen - protected in selector caller
223 printf("->AliAnalysisManager::Init(%s)\n", tree->GetName());
225 // Call InitTree of EventHandler
226 if (fOutputEventHandler) {
227 if (fMode == kProofAnalysis) {
228 init = fOutputEventHandler->Init(0x0, "proof");
230 init = fOutputEventHandler->Init(0x0, "local");
233 Error("Init", "Output event handler failed to initialize");
238 if (fInputEventHandler) {
239 if (fMode == kProofAnalysis) {
240 init = fInputEventHandler->Init(tree, "proof");
242 init = fInputEventHandler->Init(tree, "local");
245 Error("Init", "Input event handler failed to initialize tree");
249 // If no input event handler we need to get the tree once
251 if(!tree->GetTree()) {
252 Long64_t readEntry = tree->LoadTree(0);
253 if (readEntry == -2) {
254 Error("Init", "Input tree has no entry. Exiting");
260 if (fMCtruthEventHandler) {
261 if (fMode == kProofAnalysis) {
262 init = fMCtruthEventHandler->Init(0x0, "proof");
264 init = fMCtruthEventHandler->Init(0x0, "local");
267 Error("Init", "MC event handler failed to initialize");
272 if (!fInitOK) InitAnalysis();
273 if (!fInitOK) return kFALSE;
276 AliAnalysisDataContainer *top = fCommonInput;
277 if (!top) top = (AliAnalysisDataContainer*)fInputs->At(0);
279 Error("Init","No top input container !");
284 printf("<-AliAnalysisManager::Init(%s)\n", tree->GetName());
289 //______________________________________________________________________________
290 void AliAnalysisManager::SlaveBegin(TTree *tree)
292 // The SlaveBegin() function is called after the Begin() function.
293 // When running with PROOF SlaveBegin() is called on each slave server.
294 // The tree argument is deprecated (on PROOF 0 is passed).
295 if (fDebug > 1) printf("->AliAnalysisManager::SlaveBegin()\n");
296 static Bool_t isCalled = kFALSE;
297 Bool_t init = kFALSE;
298 Bool_t initOK = kTRUE;
300 TDirectory *curdir = gDirectory;
301 // Call SlaveBegin only once in case of mixing
302 if (isCalled && fMode==kMixingAnalysis) return;
304 // Call Init of EventHandler
305 if (fOutputEventHandler) {
306 if (fMode == kProofAnalysis) {
307 // Merging AOD's in PROOF via TProofOutputFile
308 if (fDebug > 1) printf(" Initializing AOD output file %s...\n", fOutputEventHandler->GetOutputFileName());
309 init = fOutputEventHandler->Init("proof");
310 if (!init) msg = "Failed to initialize output handler on worker";
312 init = fOutputEventHandler->Init("local");
313 if (!init) msg = "Failed to initialize output handler";
316 if (!fSelector) Error("SlaveBegin", "Selector not set");
317 else if (!init) {fSelector->Abort(msg); fSelector->SetStatus(-1);}
320 if (fInputEventHandler) {
321 fInputEventHandler->SetInputTree(tree);
322 if (fMode == kProofAnalysis) {
323 init = fInputEventHandler->Init("proof");
324 if (!init) msg = "Failed to initialize input handler on worker";
326 init = fInputEventHandler->Init("local");
327 if (!init) msg = "Failed to initialize input handler";
330 if (!fSelector) Error("SlaveBegin", "Selector not set");
331 else if (!init) {fSelector->Abort(msg); fSelector->SetStatus(-1);}
334 if (fMCtruthEventHandler) {
335 if (fMode == kProofAnalysis) {
336 init = fMCtruthEventHandler->Init("proof");
337 if (!init) msg = "Failed to initialize MC handler on worker";
339 init = fMCtruthEventHandler->Init("local");
340 if (!init) msg = "Failed to initialize MC handler";
343 if (!fSelector) Error("SlaveBegin", "Selector not set");
344 else if (!init) {fSelector->Abort(msg); fSelector->SetStatus(-1);}
346 if (curdir) curdir->cd();
350 AliAnalysisTask *task;
351 // Call CreateOutputObjects for all tasks
352 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
353 Bool_t dirStatus = TH1::AddDirectoryStatus();
355 while ((task=(AliAnalysisTask*)next())) {
357 // Start with memory as current dir and make sure by default histograms do not get attached to files.
358 TH1::AddDirectory(kFALSE);
359 task->CreateOutputObjects();
360 if (getsysInfo) AliSysInfo::AddStamp(Form("%s_CREATEOUTOBJ",task->ClassName()), 0, itask, 0);
363 TH1::AddDirectory(dirStatus);
364 if (curdir) curdir->cd();
365 if (fDebug > 1) printf("<-AliAnalysisManager::SlaveBegin()\n");
368 //______________________________________________________________________________
369 Bool_t AliAnalysisManager::Notify()
371 // The Notify() function is called when a new file is opened. This
372 // can be either for a new TTree in a TChain or when when a new TTree
373 // is started when using PROOF. It is normaly not necessary to make changes
374 // to the generated code, but the routine can be extended by the
375 // user if needed. The return value is currently not used.
376 if (!fTree) return kFALSE;
378 fTable.Clear("nodelete"); // clearing the hash table may not be needed -> C.L.
379 if (fMode == kProofAnalysis) fIsRemote = kTRUE;
381 TFile *curfile = fTree->GetCurrentFile();
383 Error("Notify","No current file");
387 if (fDebug > 1) printf("->AliAnalysisManager::Notify() file: %s\n", curfile->GetName());
389 AliAnalysisTask *task;
390 // Call Notify for all tasks
391 while ((task=(AliAnalysisTask*)next()))
394 // Call Notify of the event handlers
395 if (fInputEventHandler) {
396 fInputEventHandler->Notify(curfile->GetName());
399 if (fOutputEventHandler) {
400 fOutputEventHandler->Notify(curfile->GetName());
403 if (fMCtruthEventHandler) {
404 fMCtruthEventHandler->Notify(curfile->GetName());
407 if (fDebug > 1) printf("<-AliAnalysisManager::Notify()\n");
411 //______________________________________________________________________________
412 Bool_t AliAnalysisManager::Process(Long64_t entry)
414 // The Process() function is called for each entry in the tree (or possibly
415 // keyed object in the case of PROOF) to be processed. The entry argument
416 // specifies which entry in the currently loaded tree is to be processed.
417 // It can be passed to either TTree::GetEntry() or TBranch::GetEntry()
418 // to read either all or the required parts of the data. When processing
419 // keyed objects with PROOF, the object is already loaded and is available
420 // via the fObject pointer.
422 // This function should contain the "body" of the analysis. It can contain
423 // simple or elaborate selection criteria, run algorithms on the data
424 // of the event and typically fill histograms.
426 // WARNING when a selector is used with a TChain, you must use
427 // the pointer to the current TTree to call GetEntry(entry).
428 // The entry is always the local entry number in the current tree.
429 // Assuming that fChain is the pointer to the TChain being processed,
430 // use fChain->GetTree()->GetEntry(entry).
431 if (fDebug > 1) printf("->AliAnalysisManager::Process(%lld)\n", entry);
433 if (fInputEventHandler) fInputEventHandler ->BeginEvent(entry);
434 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent(entry);
435 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(entry);
439 if (fDebug > 1) printf("<-AliAnalysisManager::Process()\n");
443 //______________________________________________________________________________
444 void AliAnalysisManager::PackOutput(TList *target)
446 // Pack all output data containers in the output list. Called at SlaveTerminate
447 // stage in PROOF case for each slave.
448 if (fDebug > 1) printf("->AliAnalysisManager::PackOutput()\n");
450 Error("PackOutput", "No target. Exiting.");
453 TDirectory *cdir = gDirectory;
455 if (fInputEventHandler) fInputEventHandler ->Terminate();
456 if (fOutputEventHandler) fOutputEventHandler ->Terminate();
457 if (fMCtruthEventHandler) fMCtruthEventHandler->Terminate();
460 // Call FinishTaskOutput() for each event loop task (not called for
461 // post-event loop tasks - use Terminate() fo those)
462 TIter nexttask(fTasks);
463 AliAnalysisTask *task;
464 while ((task=(AliAnalysisTask*)nexttask())) {
465 if (!task->IsPostEventLoop()) {
466 if (fDebug > 1) printf("->FinishTaskOutput: task %s\n", task->GetName());
467 task->FinishTaskOutput();
469 if (fDebug > 1) printf("<-FinishTaskOutput: task %s\n", task->GetName());
473 if (fMode == kProofAnalysis) {
474 TIter next(fOutputs);
475 AliAnalysisDataContainer *output;
476 Bool_t isManagedByHandler = kFALSE;
479 while ((output=(AliAnalysisDataContainer*)next())) {
480 // Do not consider outputs of post event loop tasks
481 isManagedByHandler = kFALSE;
482 if (output->GetProducer()->IsPostEventLoop()) continue;
483 const char *filename = output->GetFileName();
484 if (!(strcmp(filename, "default")) && fOutputEventHandler) {
485 isManagedByHandler = kTRUE;
486 printf("#### Handler output. Extra: %s\n", fExtraFiles.Data());
487 filename = fOutputEventHandler->GetOutputFileName();
489 // Check if data was posted to this container. If not, issue an error.
490 if (!output->GetData() && !isManagedByHandler) {
491 Error("PackOutput", "No data for output container %s. Forgot to PostData ?", output->GetName());
494 if (!output->IsSpecialOutput()) {
496 if (strlen(filename) && !isManagedByHandler) {
497 // Backup current folder
498 TDirectory *opwd = gDirectory;
499 // File resident outputs.
500 // Check first if the file exists.
501 TString openoption = "RECREATE";
502 Bool_t firsttime = kTRUE;
503 if (filestmp.FindObject(output->GetFileName())) {
506 filestmp.Add(new TNamed(output->GetFileName(),""));
508 if (!gSystem->AccessPathName(output->GetFileName()) && !firsttime) openoption = "UPDATE";
509 // TFile *file = AliAnalysisManager::OpenFile(output, openoption, kTRUE);
510 // Save data to file, then close.
511 if (output->GetData()->InheritsFrom(TCollection::Class())) {
512 // If data is a collection, we set the name of the collection
513 // as the one of the container and we save as a single key.
514 TCollection *coll = (TCollection*)output->GetData();
515 coll->SetName(output->GetName());
516 // coll->Write(output->GetName(), TObject::kSingleKey);
518 if (output->GetData()->InheritsFrom(TTree::Class())) {
519 TFile *file = AliAnalysisManager::OpenFile(output, openoption, kTRUE);
520 // Save data to file, then close.
521 TTree *tree = (TTree*)output->GetData();
522 // Check if tree is in memory
523 if (tree->GetDirectory()==gROOT) tree->SetDirectory(gDirectory);
527 // output->GetData()->Write();
530 if (fDebug > 1) printf("PackOutput %s: memory merge, file resident output\n", output->GetName());
532 // printf(" file %s listing content:\n", filename);
535 // Clear file list to release object ownership to user.
538 output->SetFile(NULL);
539 // Restore current directory
540 if (opwd) opwd->cd();
542 // Memory-resident outputs
543 if (fDebug > 1) printf("PackOutput %s: memory merge memory resident output\n", filename);
545 AliAnalysisDataWrapper *wrap = 0;
546 if (isManagedByHandler) {
547 wrap = new AliAnalysisDataWrapper(fOutputEventHandler->GetTree());
548 wrap->SetName(output->GetName());
550 else wrap =output->ExportData();
551 // Output wrappers must NOT delete data after merging - the user owns them
552 wrap->SetDeleteData(kFALSE);
555 // Special outputs. The file must be opened and connected to the container.
556 TDirectory *opwd = gDirectory;
557 TFile *file = output->GetFile();
559 AliAnalysisTask *producer = output->GetProducer();
561 "File %s for special container %s was NOT opened in %s::CreateOutputObjects !!!",
562 output->GetFileName(), output->GetName(), producer->ClassName());
565 TString outFilename = file->GetName();
566 if (fDebug > 1) printf("PackOutput %s: special output\n", output->GetName());
567 if (isManagedByHandler) {
568 // Terminate IO for files managed by the output handler
569 // file->Write() moved to AOD handler (A.G. 11.01.10)
570 // if (file) file->Write();
571 if (file && fDebug > 2) {
572 printf(" handled file %s listing content:\n", file->GetName());
575 fOutputEventHandler->TerminateIO();
578 // Release object ownership to users after writing data to file
579 if (output->GetData()->InheritsFrom(TCollection::Class())) {
580 // If data is a collection, we set the name of the collection
581 // as the one of the container and we save as a single key.
582 TCollection *coll = (TCollection*)output->GetData();
583 coll->SetName(output->GetName());
584 coll->Write(output->GetName(), TObject::kSingleKey);
586 if (output->GetData()->InheritsFrom(TTree::Class())) {
587 TTree *tree = (TTree*)output->GetData();
588 tree->SetDirectory(file);
591 output->GetData()->Write();
595 printf(" file %s listing content:\n", output->GetFileName());
598 // Clear file list to release object ownership to user.
601 output->SetFile(NULL);
603 // Restore current directory
604 if (opwd) opwd->cd();
605 // Check if a special output location was provided or the output files have to be merged
606 if (strlen(fSpecialOutputLocation.Data())) {
607 TString remote = fSpecialOutputLocation;
609 Int_t gid = gROOT->ProcessLine("gProofServ->GetGroupId();");
610 if (remote.BeginsWith("alien:")) {
611 gROOT->ProcessLine("TGrid::Connect(\"alien:\", gProofServ->GetUser());");
612 remote += outFilename;
613 remote.ReplaceAll(".root", Form("_%d.root", gid));
615 remote += Form("%s_%d_", gSystem->HostName(), gid);
616 remote += outFilename;
619 Info("PackOutput", "Output file for container %s to be copied \n at: %s. No merging.",
620 output->GetName(), remote.Data());
621 TFile::Cp ( outFilename.Data(), remote.Data() );
622 // Copy extra outputs
623 if (fExtraFiles.Length() && isManagedByHandler) {
624 TObjArray *arr = fExtraFiles.Tokenize(" ");
626 TIter nextfilename(arr);
627 while ((os=(TObjString*)nextfilename())) {
628 outFilename = os->GetString();
629 remote = fSpecialOutputLocation;
631 if (remote.BeginsWith("alien://")) {
632 remote += outFilename;
633 remote.ReplaceAll(".root", Form("_%d.root", gid));
635 remote += Form("%s_%d_", gSystem->HostName(), gid);
636 remote += outFilename;
639 Info("PackOutput", "Extra AOD file %s to be copied \n at: %s. No merging.",
640 outFilename.Data(), remote.Data());
641 TFile::Cp ( outFilename.Data(), remote.Data() );
646 // No special location specified-> use TProofOutputFile as merging utility
647 // The file at this output slot must be opened in CreateOutputObjects
648 if (fDebug > 1) printf(" File for container %s to be merged via file merger...\n", output->GetName());
654 if (fDebug > 1) printf("<-AliAnalysisManager::PackOutput: output list contains %d containers\n", target->GetSize());
657 //______________________________________________________________________________
658 void AliAnalysisManager::ImportWrappers(TList *source)
660 // Import data in output containers from wrappers coming in source.
661 if (fDebug > 1) printf("->AliAnalysisManager::ImportWrappers()\n");
662 TIter next(fOutputs);
663 AliAnalysisDataContainer *cont;
664 AliAnalysisDataWrapper *wrap;
666 Bool_t inGrid = (fMode == kGridAnalysis)?kTRUE:kFALSE;
667 TDirectory *cdir = gDirectory;
668 while ((cont=(AliAnalysisDataContainer*)next())) {
670 if (cont->GetProducer() && cont->GetProducer()->IsPostEventLoop() && !inGrid) continue;
671 if (cont->IsRegisterDataset()) continue;
672 const char *filename = cont->GetFileName();
673 Bool_t isManagedByHandler = kFALSE;
674 if (!(strcmp(filename, "default")) && fOutputEventHandler) {
675 isManagedByHandler = kTRUE;
676 filename = fOutputEventHandler->GetOutputFileName();
678 if (cont->IsSpecialOutput() || inGrid) {
679 if (strlen(fSpecialOutputLocation.Data())) continue;
680 // Copy merged file from PROOF scratch space.
681 // In case of grid the files are already in the current directory.
683 if (isManagedByHandler && fExtraFiles.Length()) {
684 // Copy extra registered dAOD files.
685 TObjArray *arr = fExtraFiles.Tokenize(" ");
687 TIter nextfilename(arr);
688 while ((os=(TObjString*)nextfilename())) GetFileFromWrapper(os->GetString(), source);
691 if (!GetFileFromWrapper(filename, source)) continue;
693 // Normally we should connect data from the copied file to the
694 // corresponding output container, but it is not obvious how to do this
695 // automatically if several objects in file...
696 TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
697 if (!f) f = TFile::Open(filename, "READ");
699 Error("ImportWrappers", "Cannot open file %s in read-only mode", filename);
703 // Cd to the directory pointed by the container
704 TString folder = cont->GetFolderName();
705 if (!folder.IsNull()) f->cd(folder);
706 // Try to fetch first an object having the container name.
707 obj = gDirectory->Get(cont->GetName());
709 Warning("ImportWrappers", "Could not import object for container %s in file %s:%s.\n Object will not be available in Terminate()",
710 cont->GetName(), filename, cont->GetFolderName());
713 wrap = new AliAnalysisDataWrapper(obj);
714 wrap->SetDeleteData(kFALSE);
716 if (!wrap) wrap = (AliAnalysisDataWrapper*)source->FindObject(cont->GetName());
718 Error("ImportWrappers","Container %s not found in analysis output !", cont->GetName());
723 printf(" Importing data for container %s\n", cont->GetName());
724 if (strlen(filename)) printf(" -> file %s\n", filename);
727 cont->ImportData(wrap);
729 if (cdir) cdir->cd();
730 if (fDebug > 1) printf("<-AliAnalysisManager::ImportWrappers(): %d containers imported\n", icont);
733 //______________________________________________________________________________
734 void AliAnalysisManager::UnpackOutput(TList *source)
736 // Called by AliAnalysisSelector::Terminate only on the client.
737 if (fDebug > 1) printf("->AliAnalysisManager::UnpackOutput()\n");
739 Error("UnpackOutput", "No target. Exiting.");
742 if (fDebug > 1) printf(" Source list contains %d containers\n", source->GetSize());
744 if (fMode == kProofAnalysis) ImportWrappers(source);
746 TIter next(fOutputs);
747 AliAnalysisDataContainer *output;
748 while ((output=(AliAnalysisDataContainer*)next())) {
749 if (!output->GetData()) continue;
750 // Check if there are client tasks that run post event loop
751 if (output->HasConsumers()) {
752 // Disable event loop semaphore
753 output->SetPostEventLoop(kTRUE);
754 TObjArray *list = output->GetConsumers();
755 Int_t ncons = list->GetEntriesFast();
756 for (Int_t i=0; i<ncons; i++) {
757 AliAnalysisTask *task = (AliAnalysisTask*)list->At(i);
758 task->CheckNotify(kTRUE);
759 // If task is active, execute it
760 if (task->IsPostEventLoop() && task->IsActive()) {
761 if (fDebug > 1) printf("== Executing post event loop task %s\n", task->GetName());
767 if (fDebug > 1) printf("<-AliAnalysisManager::UnpackOutput()\n");
770 //______________________________________________________________________________
771 void AliAnalysisManager::Terminate()
773 // The Terminate() function is the last function to be called during
774 // a query. It always runs on the client, it can be used to present
775 // the results graphically.
776 if (fDebug > 1) printf("->AliAnalysisManager::Terminate()\n");
777 TDirectory *cdir = gDirectory;
779 AliAnalysisTask *task;
780 AliAnalysisDataContainer *output;
783 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
784 // Call Terminate() for tasks
786 while (!IsSkipTerminate() && (task=(AliAnalysisTask*)next())) {
787 // Save all the canvases produced by the Terminate
788 TString pictname = Form("%s_%s", task->GetName(), task->ClassName());
792 AliSysInfo::AddStamp(Form("%s_TERMINATE",task->ClassName()),0, itask, 2);
794 if (TObject::TestBit(kSaveCanvases)) {
795 if (!gROOT->IsBatch()) {
796 if (fDebug>1) printf("Waiting 5 sec for %s::Terminate() to finish drawing ...\n", task->ClassName());
798 while (timer.CpuTime()<5) {
800 gSystem->ProcessEvents();
803 Int_t iend = gROOT->GetListOfCanvases()->GetEntries();
804 if (iend==0) continue;
806 for (Int_t ipict=0; ipict<iend; ipict++) {
807 canvas = (TCanvas*)gROOT->GetListOfCanvases()->At(ipict);
808 if (!canvas) continue;
809 canvas->SaveAs(Form("%s_%02d.gif", pictname.Data(),ipict));
811 gROOT->GetListOfCanvases()->Delete();
815 if (fInputEventHandler) fInputEventHandler ->TerminateIO();
816 if (fOutputEventHandler) fOutputEventHandler ->TerminateIO();
817 if (fMCtruthEventHandler) fMCtruthEventHandler->TerminateIO();
819 TObjArray *allOutputs = new TObjArray();
821 for (icont=0; icont<fOutputs->GetEntriesFast(); icont++) allOutputs->Add(fOutputs->At(icont));
822 if (!IsSkipTerminate())
823 for (icont=0; icont<fParamCont->GetEntriesFast(); icont++) allOutputs->Add(fParamCont->At(icont));
824 TIter next1(allOutputs);
825 TString handlerFile = "";
826 if (fOutputEventHandler) {
827 handlerFile = fOutputEventHandler->GetOutputFileName();
831 while ((output=(AliAnalysisDataContainer*)next1())) {
832 // Special outputs or grid files have the files already closed and written.
834 if (fMode == kGridAnalysis && icont<=fOutputs->GetEntriesFast()) continue;
835 if (fMode == kProofAnalysis) {
836 if (output->IsSpecialOutput() || output->IsRegisterDataset()) continue;
838 const char *filename = output->GetFileName();
839 TString openoption = "RECREATE";
840 if (!(strcmp(filename, "default"))) continue;
841 if (!strlen(filename)) continue;
842 if (!output->GetData()) continue;
843 TDirectory *opwd = gDirectory;
844 TFile *file = output->GetFile();
845 if (!file) file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
847 //if (handlerFile == filename && !gSystem->AccessPathName(filename)) openoption = "UPDATE";
848 Bool_t firsttime = kTRUE;
849 if (filestmp.FindObject(filename)) {
852 filestmp.Add(new TNamed(filename,""));
854 if (!gSystem->AccessPathName(filename) && !firsttime) openoption = "UPDATE";
855 if (fDebug>1) printf("Opening file: %s option=%s\n",filename, openoption.Data());
856 file = new TFile(filename, openoption);
858 if (fDebug>1) printf("File <%s> already opened with option: <%s> \n", filename, file->GetOption());
859 openoption = file->GetOption();
860 if (openoption == "READ") {
861 if (fDebug>1) printf("...reopening in UPDATE mode\n");
862 file->ReOpen("UPDATE");
865 if (file->IsZombie()) {
866 Error("Terminate", "Cannot open output file %s", filename);
869 output->SetFile(file);
871 // Check for a folder request
872 TString dir = output->GetFolderName();
874 if (!file->GetDirectory(dir)) file->mkdir(dir);
877 if (fDebug > 1) printf("...writing container %s to file %s:%s\n", output->GetName(), file->GetName(), output->GetFolderName());
878 if (output->GetData()->InheritsFrom(TCollection::Class())) {
879 // If data is a collection, we set the name of the collection
880 // as the one of the container and we save as a single key.
881 TCollection *coll = (TCollection*)output->GetData();
882 coll->SetName(output->GetName());
883 coll->Write(output->GetName(), TObject::kSingleKey);
885 if (output->GetData()->InheritsFrom(TTree::Class())) {
886 TTree *tree = (TTree*)output->GetData();
887 tree->SetDirectory(gDirectory);
890 output->GetData()->Write();
893 if (opwd) opwd->cd();
897 while ((output=(AliAnalysisDataContainer*)next1())) {
898 // Close all files at output
899 TDirectory *opwd = gDirectory;
900 if (output->GetFile()) {
901 // Clear file list to release object ownership to user.
902 // output->GetFile()->Clear();
903 output->GetFile()->Close();
904 output->SetFile(NULL);
905 // Copy merged outputs in alien if requested
906 if (fSpecialOutputLocation.Length() &&
907 fSpecialOutputLocation.BeginsWith("alien://")) {
908 Info("Terminate", "Copy file %s to %s", output->GetFile()->GetName(),fSpecialOutputLocation.Data());
909 TFile::Cp(output->GetFile()->GetName(),
910 Form("%s/%s", fSpecialOutputLocation.Data(), output->GetFile()->GetName()));
913 if (opwd) opwd->cd();
918 TDirectory *crtdir = gDirectory;
919 TFile f("syswatch.root", "RECREATE");
923 TTree *tree = AliSysInfo::MakeTree("syswatch.log");
924 tree->SetName("syswatch");
925 tree->SetMarkerStyle(kCircle);
926 tree->SetMarkerColor(kBlue);
927 tree->SetMarkerSize(0.5);
928 if (!gROOT->IsBatch()) {
929 tree->SetAlias("event", "id0");
930 tree->SetAlias("task", "id1");
931 tree->SetAlias("stage", "id2");
932 // Already defined aliases
933 // tree->SetAlias("deltaT","stampSec-stampOldSec");
934 // tree->SetAlias("T","stampSec-first");
935 // tree->SetAlias("deltaVM","(pI.fMemVirtual-pIOld.fMemVirtual)");
936 // tree->SetAlias("VM","pI.fMemVirtual");
937 TCanvas *canvas = new TCanvas("SysInfo","SysInfo",10,10,1200,800);
938 Int_t npads = 1 /*COO plot for all tasks*/ +
939 fTopTasks->GetEntries() /*Exec plot per task*/ +
940 1 /*Terminate plot for all tasks*/ +
943 Int_t iopt = (Int_t)TMath::Sqrt((Double_t)npads);
944 if (npads<iopt*(iopt+1))
945 canvas->Divide(iopt, iopt+1, 0.01, 0.01);
947 canvas->Divide(iopt+1, iopt+1, 0.01, 0.01);
949 // draw the plot of deltaVM for Exec for each task
950 for (itask=0; itask<fTopTasks->GetEntriesFast(); itask++) {
951 task = (AliAnalysisTask*)fTopTasks->At(itask);
953 cut = Form("task==%d && stage==1", itask);
954 tree->Draw("deltaVM:event",cut,"", 1234567890, 0);
955 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
957 hist->SetTitle(Form("%s: Exec dVM[kB]/event", task->GetName()));
958 hist->GetYaxis()->SetTitle("deltaVM [kB]");
961 // Draw the plot of deltaVM for CreateOutputObjects for all tasks
963 tree->SetMarkerStyle(kFullTriangleUp);
964 tree->SetMarkerColor(kRed);
965 tree->SetMarkerSize(0.8);
966 cut = "task>=0 && task<1000 && stage==0";
967 tree->Draw("deltaVM:sname",cut,"", 1234567890, 0);
968 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
970 hist->SetTitle("Memory in CreateOutputObjects()");
971 hist->GetYaxis()->SetTitle("deltaVM [kB]");
972 hist->GetXaxis()->SetTitle("task");
974 // draw the plot of deltaVM for Terminate for all tasks
976 tree->SetMarkerStyle(kOpenSquare);
977 tree->SetMarkerColor(kMagenta);
978 cut = "task>=0 && task<1000 && stage==2";
979 tree->Draw("deltaVM:sname",cut,"", 1234567890, 0);
980 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
982 hist->SetTitle("Memory in Terminate()");
983 hist->GetYaxis()->SetTitle("deltaVM [kB]");
984 hist->GetXaxis()->SetTitle("task");
988 tree->SetMarkerStyle(kFullCircle);
989 tree->SetMarkerColor(kGreen);
990 cut = Form("task==%d && stage==1",fTopTasks->GetEntriesFast()-1);
991 tree->Draw("VM:event",cut,"", 1234567890, 0);
992 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
994 hist->SetTitle("Virtual memory");
995 hist->GetYaxis()->SetTitle("VM [kB]");
999 tree->SetMarkerStyle(kCircle);
1000 tree->SetMarkerColor(kBlue);
1001 tree->SetMarkerSize(0.5);
1006 if (crtdir) crtdir->cd();
1008 // Validate the output files
1009 if (ValidateOutputFiles()) {
1011 out.open("outputs_valid", ios::out);
1015 if (fDebug > 1) printf("<-AliAnalysisManager::Terminate()\n");
1017 //______________________________________________________________________________
1018 void AliAnalysisManager::ProfileTask(Int_t itop, const char *option) const
1020 // Profiles the task having the itop index in the list of top (first level) tasks.
1021 AliAnalysisTask *task = (AliAnalysisTask*)fTopTasks->At(itop);
1023 Error("ProfileTask", "There are only %d top tasks in the manager", fTopTasks->GetEntries());
1026 ProfileTask(task->GetName(), option);
1029 //______________________________________________________________________________
1030 void AliAnalysisManager::ProfileTask(const char *name, const char */*option*/) const
1032 // Profile a managed task after the execution of the analysis in case NSysInfo
1034 if (gSystem->AccessPathName("syswatch.root")) {
1035 Error("ProfileTask", "No file syswatch.root found in the current directory");
1038 if (gROOT->IsBatch()) return;
1039 AliAnalysisTask *task = (AliAnalysisTask*)fTopTasks->FindObject(name);
1041 Error("ProfileTask", "No top task named %s known by the manager.", name);
1044 Int_t itop = fTopTasks->IndexOf(task);
1045 Int_t itask = fTasks->IndexOf(task);
1046 // Create canvas with 2 pads: first draw COO + Terminate, second Exec
1047 TDirectory *cdir = gDirectory;
1048 TFile f("syswatch.root");
1049 TTree *tree = (TTree*)f.Get("syswatch");
1051 Error("ProfileTask", "No tree named <syswatch> found in file syswatch.root");
1054 if (fDebug > 1) printf("=== Profiling task %s (class %s)\n", name, task->ClassName());
1055 TCanvas *canvas = new TCanvas(Form("profile_%d",itop),Form("Profile of task %s (class %s)",name,task->ClassName()),10,10,800,600);
1056 canvas->Divide(2, 2, 0.01, 0.01);
1060 // VM profile for COO and Terminate methods
1062 cut = Form("task==%d && (stage==0 || stage==2)",itask);
1063 tree->Draw("deltaVM:sname",cut,"", 1234567890, 0);
1064 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
1066 hist->SetTitle("Alocated VM[kB] for COO and Terminate");
1067 hist->GetYaxis()->SetTitle("deltaVM [kB]");
1068 hist->GetXaxis()->SetTitle("method");
1070 // CPU profile per event
1072 cut = Form("task==%d && stage==1",itop);
1073 tree->Draw("deltaT:event",cut,"", 1234567890, 0);
1074 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
1076 hist->SetTitle("Execution time per event");
1077 hist->GetYaxis()->SetTitle("CPU/event [s]");
1079 // VM profile for Exec
1081 cut = Form("task==%d && stage==1",itop);
1082 tree->Draw("deltaVM:event",cut,"", 1234567890, 0);
1083 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
1085 hist->SetTitle("Alocated VM[kB] per event");
1086 hist->GetYaxis()->SetTitle("deltaVM [kB]");
1091 if (cdir) cdir->cd();
1094 //______________________________________________________________________________
1095 void AliAnalysisManager::AddTask(AliAnalysisTask *task)
1097 // Adds a user task to the global list of tasks.
1098 if (fTasks->FindObject(task)) {
1099 Warning("AddTask", "Task %s: the same object already added to the analysis manager. Not adding.", task->GetName());
1102 task->SetActive(kFALSE);
1106 //______________________________________________________________________________
1107 AliAnalysisTask *AliAnalysisManager::GetTask(const char *name) const
1109 // Retreive task by name.
1110 if (!fTasks) return NULL;
1111 return (AliAnalysisTask*)fTasks->FindObject(name);
1114 //______________________________________________________________________________
1115 AliAnalysisDataContainer *AliAnalysisManager::CreateContainer(const char *name,
1116 TClass *datatype, EAliAnalysisContType type, const char *filename)
1118 // Create a data container of a certain type. Types can be:
1119 // kExchangeContainer = 0, used to exchange data between tasks
1120 // kInputContainer = 1, used to store input data
1121 // kOutputContainer = 2, used for writing result to a file
1122 // filename: composed by file#folder (e.g. results.root#INCLUSIVE) - will write
1123 // the output object to a folder inside the output file
1124 if (fContainers->FindObject(name)) {
1125 Error("CreateContainer","A container named %s already defined !",name);
1128 AliAnalysisDataContainer *cont = new AliAnalysisDataContainer(name, datatype);
1129 fContainers->Add(cont);
1131 case kInputContainer:
1134 case kOutputContainer:
1135 fOutputs->Add(cont);
1136 if (filename && strlen(filename)) {
1137 cont->SetFileName(filename);
1138 cont->SetDataOwned(kFALSE); // data owned by the file
1141 case kParamContainer:
1142 fParamCont->Add(cont);
1143 if (filename && strlen(filename)) {
1144 cont->SetFileName(filename);
1145 cont->SetDataOwned(kFALSE); // data owned by the file
1148 case kExchangeContainer:
1154 //______________________________________________________________________________
1155 Bool_t AliAnalysisManager::ConnectInput(AliAnalysisTask *task, Int_t islot,
1156 AliAnalysisDataContainer *cont)
1158 // Connect input of an existing task to a data container.
1160 Error("ConnectInput", "Task pointer is NULL");
1163 if (!fTasks->FindObject(task)) {
1165 Info("ConnectInput", "Task %s was not registered. Now owned by analysis manager", task->GetName());
1167 Bool_t connected = task->ConnectInput(islot, cont);
1171 //______________________________________________________________________________
1172 Bool_t AliAnalysisManager::ConnectOutput(AliAnalysisTask *task, Int_t islot,
1173 AliAnalysisDataContainer *cont)
1175 // Connect output of an existing task to a data container.
1177 Error("ConnectOutput", "Task pointer is NULL");
1180 if (!fTasks->FindObject(task)) {
1182 Warning("ConnectOutput", "Task %s not registered. Now owned by analysis manager", task->GetName());
1184 Bool_t connected = task->ConnectOutput(islot, cont);
1188 //______________________________________________________________________________
1189 void AliAnalysisManager::CleanContainers()
1191 // Clean data from all containers that have already finished all client tasks.
1192 TIter next(fContainers);
1193 AliAnalysisDataContainer *cont;
1194 while ((cont=(AliAnalysisDataContainer *)next())) {
1195 if (cont->IsOwnedData() &&
1196 cont->IsDataReady() &&
1197 cont->ClientsExecuted()) cont->DeleteData();
1201 //______________________________________________________________________________
1202 Bool_t AliAnalysisManager::InitAnalysis()
1204 // Initialization of analysis chain of tasks. Should be called after all tasks
1205 // and data containers are properly connected
1206 // Reset flag and remove valid_outputs file if exists
1208 if (!gSystem->AccessPathName("outputs_valid"))
1209 gSystem->Unlink("outputs_valid");
1210 // Check for top tasks (depending only on input data containers)
1211 if (!fTasks->First()) {
1212 Error("InitAnalysis", "Analysis has no tasks !");
1216 AliAnalysisTask *task;
1217 AliAnalysisDataContainer *cont;
1220 Bool_t iszombie = kFALSE;
1221 Bool_t istop = kTRUE;
1223 while ((task=(AliAnalysisTask*)next())) {
1226 Int_t ninputs = task->GetNinputs();
1227 for (i=0; i<ninputs; i++) {
1228 cont = task->GetInputSlot(i)->GetContainer();
1232 fZombies->Add(task);
1236 Error("InitAnalysis", "Input slot %d of task %s has no container connected ! Declared zombie...",
1237 i, task->GetName());
1239 if (iszombie) continue;
1240 // Check if cont is an input container
1241 if (istop && !fInputs->FindObject(cont)) istop=kFALSE;
1242 // Connect to parent task
1246 fTopTasks->Add(task);
1250 Error("InitAnalysis", "No top task defined. At least one task should be connected only to input containers");
1253 // Check now if there are orphan tasks
1254 for (i=0; i<ntop; i++) {
1255 task = (AliAnalysisTask*)fTopTasks->At(i);
1260 while ((task=(AliAnalysisTask*)next())) {
1261 if (!task->IsUsed()) {
1263 Warning("InitAnalysis", "Task %s is orphan", task->GetName());
1266 // Check the task hierarchy (no parent task should depend on data provided
1267 // by a daughter task)
1268 for (i=0; i<ntop; i++) {
1269 task = (AliAnalysisTask*)fTopTasks->At(i);
1270 if (task->CheckCircularDeps()) {
1271 Error("InitAnalysis", "Found illegal circular dependencies between following tasks:");
1276 // Check that all containers feeding post-event loop tasks are in the outputs list
1277 TIter nextcont(fContainers); // loop over all containers
1278 while ((cont=(AliAnalysisDataContainer*)nextcont())) {
1279 if (!cont->IsPostEventLoop() && !fOutputs->FindObject(cont)) {
1280 if (cont->HasConsumers()) {
1281 // Check if one of the consumers is post event loop
1282 TIter nextconsumer(cont->GetConsumers());
1283 while ((task=(AliAnalysisTask*)nextconsumer())) {
1284 if (task->IsPostEventLoop()) {
1285 fOutputs->Add(cont);
1292 // Check if all special output containers have a file name provided
1293 TIter nextout(fOutputs);
1294 while ((cont=(AliAnalysisDataContainer*)nextout())) {
1295 if (cont->IsSpecialOutput() && !strlen(cont->GetFileName())) {
1296 Error("InitAnalysis", "Wrong container %s : a file name MUST be provided for special outputs", cont->GetName());
1304 //______________________________________________________________________________
1305 void AliAnalysisManager::PrintStatus(Option_t *option) const
1307 // Print task hierarchy.
1309 Info("PrintStatus", "Analysis manager %s not initialized : call InitAnalysis() first", GetName());
1312 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
1314 Info("PrintStatus", "System information will be collected each %lld events", fNSysInfo);
1315 TIter next(fTopTasks);
1316 AliAnalysisTask *task;
1317 while ((task=(AliAnalysisTask*)next()))
1318 task->PrintTask(option);
1321 //______________________________________________________________________________
1322 void AliAnalysisManager::ResetAnalysis()
1324 // Reset all execution flags and clean containers.
1328 //______________________________________________________________________________
1329 Long64_t AliAnalysisManager::StartAnalysis(const char *type, TTree * const tree, Long64_t nentries, Long64_t firstentry)
1331 // Start analysis for this manager. Analysis task can be: LOCAL, PROOF, GRID or
1332 // MIX. Process nentries starting from firstentry
1334 // Backup current directory and make sure gDirectory points to gROOT
1335 TDirectory *cdir = gDirectory;
1338 Error("StartAnalysis","Analysis manager was not initialized !");
1342 if (fDebug > 1) printf("StartAnalysis %s\n",GetName());
1344 TString anaType = type;
1346 fMode = kLocalAnalysis;
1347 Bool_t runlocalinit = kTRUE;
1348 if (anaType.Contains("file")) {
1349 runlocalinit = kFALSE;
1352 if (anaType.Contains("proof")) fMode = kProofAnalysis;
1353 else if (anaType.Contains("grid")) fMode = kGridAnalysis;
1354 else if (anaType.Contains("mix")) fMode = kMixingAnalysis;
1356 if (fMode == kGridAnalysis) {
1358 if (!anaType.Contains("terminate")) {
1359 if (!fGridHandler) {
1360 Error("StartAnalysis", "Cannot start grid analysis without a grid handler.");
1361 Info("===", "Add an AliAnalysisAlien object as plugin for this manager and configure it.");
1365 // Write analysis manager in the analysis file
1366 cout << "===== RUNNING GRID ANALYSIS: " << GetName() << endl;
1367 // run local task configuration
1368 TIter nextTask(fTasks);
1369 AliAnalysisTask *task;
1370 while ((task=(AliAnalysisTask*)nextTask())) {
1374 if (!fGridHandler->StartAnalysis(nentries, firstentry)) {
1375 Info("StartAnalysis", "Grid analysis was stopped and cannot be terminated");
1380 // Terminate grid analysis
1381 if (fSelector && fSelector->GetStatus() == -1) {cdir->cd(); return -1;}
1382 if (fGridHandler->GetRunMode() == AliAnalysisGrid::kOffline) {cdir->cd(); return 0;}
1383 cout << "===== MERGING OUTPUTS REGISTERED BY YOUR ANALYSIS JOB: " << GetName() << endl;
1384 if (!fGridHandler->MergeOutputs()) {
1385 // Return if outputs could not be merged or if it alien handler
1386 // was configured for offline mode or local testing.
1391 cout << "===== TERMINATING GRID ANALYSIS JOB: " << GetName() << endl;
1392 ImportWrappers(NULL);
1398 SetEventLoop(kFALSE);
1399 // Enable event loop mode if a tree was provided
1400 if (tree || fMode==kMixingAnalysis) SetEventLoop(kTRUE);
1403 TString ttype = "TTree";
1404 if (tree && tree->IsA() == TChain::Class()) {
1405 chain = (TChain*)tree;
1406 if (!chain || !chain->GetListOfFiles()->First()) {
1407 Error("StartAnalysis", "Cannot process null or empty chain...");
1414 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
1415 if (getsysInfo) AliSysInfo::AddStamp("Start", 0);
1416 // Initialize locally all tasks (happens for all modes)
1418 AliAnalysisTask *task;
1420 while ((task=(AliAnalysisTask*)next())) {
1424 if (getsysInfo) AliSysInfo::AddStamp("LocalInit_all", 0);
1428 case kLocalAnalysis:
1430 TIter nextT(fTasks);
1431 // Call CreateOutputObjects for all tasks
1433 Bool_t dirStatus = TH1::AddDirectoryStatus();
1434 while ((task=(AliAnalysisTask*)nextT())) {
1435 TH1::AddDirectory(kFALSE);
1436 task->CreateOutputObjects();
1437 if (getsysInfo) AliSysInfo::AddStamp(Form("%s_CREATEOUTOBJ",task->ClassName()), 0, itask, 0);
1441 TH1::AddDirectory(dirStatus);
1442 if (IsExternalLoop()) {
1443 Info("StartAnalysis", "Initialization done. Event loop is controlled externally.\
1444 \nSetData for top container, call ExecAnalysis in a loop and then Terminate manually");
1451 // Run tree-based analysis via AliAnalysisSelector
1452 cout << "===== RUNNING LOCAL ANALYSIS " << GetName() << " ON TREE " << tree->GetName() << endl;
1453 fSelector = new AliAnalysisSelector(this);
1454 retv = tree->Process(fSelector, "", nentries, firstentry);
1456 case kProofAnalysis:
1458 // Check if the plugin is used
1460 return StartAnalysis(type, fGridHandler->GetProofDataSet(), nentries, firstentry);
1462 if (!gROOT->GetListOfProofs() || !gROOT->GetListOfProofs()->GetEntries()) {
1463 Error("StartAnalysis", "No PROOF!!! Exiting.");
1467 line = Form("gProof->AddInput((TObject*)0x%lx);", (ULong_t)this);
1468 gROOT->ProcessLine(line);
1471 cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON CHAIN " << chain->GetName() << endl;
1472 retv = chain->Process("AliAnalysisSelector", "", nentries, firstentry);
1474 Error("StartAnalysis", "No chain!!! Exiting.");
1480 Warning("StartAnalysis", "GRID analysis mode not implemented. Running local.");
1482 case kMixingAnalysis:
1483 // Run event mixing analysis
1485 Error("StartAnalysis", "Cannot run event mixing without event pool");
1489 cout << "===== RUNNING EVENT MIXING ANALYSIS " << GetName() << endl;
1490 fSelector = new AliAnalysisSelector(this);
1491 while ((chain=fEventPool->GetNextChain())) {
1493 // Call NotifyBinChange for all tasks
1494 while ((task=(AliAnalysisTask*)next()))
1495 if (!task->IsPostEventLoop()) task->NotifyBinChange();
1496 retv = chain->Process(fSelector);
1498 Error("StartAnalysis", "Mixing analysis failed");
1503 PackOutput(fSelector->GetOutputList());
1510 //______________________________________________________________________________
1511 Long64_t AliAnalysisManager::StartAnalysis(const char *type, const char *dataset, Long64_t nentries, Long64_t firstentry)
1513 // Start analysis for this manager on a given dataset. Analysis task can be:
1514 // LOCAL, PROOF or GRID. Process nentries starting from firstentry.
1516 Error("StartAnalysis","Analysis manager was not initialized !");
1520 if (fDebug > 1) printf("StartAnalysis %s\n",GetName());
1521 TString anaType = type;
1523 if (!anaType.Contains("proof")) {
1524 Error("StartAnalysis", "Cannot process datasets in %s mode. Try PROOF.", type);
1527 fMode = kProofAnalysis;
1529 SetEventLoop(kTRUE);
1530 // Set the dataset flag
1531 TObject::SetBit(kUseDataSet);
1535 // Start proof analysis using the grid handler
1536 if (!fGridHandler->StartAnalysis(nentries, firstentry)) {
1537 Error("StartAnalysis", "The grid plugin could not start PROOF analysis");
1540 // Check if the plugin is in test mode
1541 if (fGridHandler->GetRunMode() == AliAnalysisGrid::kTest) {
1542 // Get the chain to be used for test mode
1543 TString dataType = "esdTree";
1544 if (fInputEventHandler) {
1545 dataType = fInputEventHandler->GetDataType();
1549 chain = fGridHandler->GetChainForTestMode(dataType);
1551 Error("StartAnalysis", "No chain for test mode. Aborting.");
1556 dataset = fGridHandler->GetProofDataSet();
1560 if (!gROOT->GetListOfProofs() || !gROOT->GetListOfProofs()->GetEntries()) {
1561 Error("StartAnalysis", "No PROOF!!! Exiting.");
1565 // Initialize locally all tasks
1567 AliAnalysisTask *task;
1568 while ((task=(AliAnalysisTask*)next())) {
1572 line = Form("gProof->AddInput((TObject*)0x%lx);", (ULong_t)this);
1573 gROOT->ProcessLine(line);
1576 // chain->SetProof();
1577 cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON TEST CHAIN " << chain->GetName() << endl;
1578 retv = chain->Process("AliAnalysisSelector", "", nentries, firstentry);
1580 line = Form("gProof->Process(\"%s\", \"AliAnalysisSelector\", \"\", %lld, %lld);",
1581 dataset, nentries, firstentry);
1582 cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON DATASET " << dataset << endl;
1583 retv = (Long_t)gROOT->ProcessLine(line);
1588 //______________________________________________________________________________
1589 TFile *AliAnalysisManager::OpenFile(AliAnalysisDataContainer *cont, const char *option, Bool_t ignoreProof)
1591 // Opens according the option the file specified by cont->GetFileName() and changes
1592 // current directory to cont->GetFolderName(). If the file was already opened, it
1593 // checks if the option UPDATE was preserved. File open via TProofOutputFile can
1594 // be optionally ignored.
1595 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
1596 TString filename = cont->GetFileName();
1598 if (filename.IsNull()) {
1599 ::Error("AliAnalysisManager::OpenFile", "No file name specified for container %s", cont->GetName());
1602 if (mgr->GetAnalysisType()==AliAnalysisManager::kProofAnalysis && cont->IsSpecialOutput()
1604 f = mgr->OpenProofFile(cont,option);
1606 // Check first if the file is already opened
1607 f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
1609 // Check if option "UPDATE" was preserved
1610 TString opt(option);
1612 if ((opt=="UPDATE") && (opt!=f->GetOption()))
1613 ::Info("AliAnalysisManager::OpenFile", "File %s already opened in %s mode!", cont->GetFileName(), f->GetOption());
1615 f = TFile::Open(filename, option);
1618 if (f && !f->IsZombie() && !f->TestBit(TFile::kRecovered)) {
1622 // Check for a folder request
1623 TString dir = cont->GetFolderName();
1624 if (!dir.IsNull()) {
1625 if (!f->GetDirectory(dir)) f->mkdir(dir);
1630 ::Fatal("AliAnalysisManager::OpenFile", "File %s could not be opened", filename.Data());
1631 cont->SetFile(NULL);
1635 //______________________________________________________________________________
1636 TFile *AliAnalysisManager::OpenProofFile(AliAnalysisDataContainer *cont, const char *option)
1638 // Opens a special output file used in PROOF.
1640 TString filename = cont->GetFileName();
1641 if (cont == fCommonOutput) {
1642 if (fOutputEventHandler) filename = fOutputEventHandler->GetOutputFileName();
1643 else Fatal("OpenProofFile","No output container. Exiting.");
1646 if (fMode!=kProofAnalysis || !fSelector) {
1647 Fatal("OpenProofFile","Cannot open PROOF file %s: no PROOF or selector",filename.Data());
1650 if (fSpecialOutputLocation.Length()) {
1651 f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
1653 // Check if option "UPDATE" was preserved
1654 TString opt(option);
1656 if ((opt=="UPDATE") && (opt!=f->GetOption()))
1657 ::Info("OpenProofFile", "File %s already opened in %s mode!", cont->GetFileName(), f->GetOption());
1659 f = new TFile(filename, option);
1661 if (f && !f->IsZombie() && !f->TestBit(TFile::kRecovered)) {
1665 // Check for a folder request
1666 TString dir = cont->GetFolderName();
1668 if (!f->GetDirectory(dir)) f->mkdir(dir);
1673 Fatal("OpenProofFile", "File %s could not be opened", cont->GetFileName());
1674 cont->SetFile(NULL);
1677 // Check if there is already a proof output file in the output list
1678 TObject *pof = fSelector->GetOutputList()->FindObject(filename);
1680 // Get the actual file
1681 line = Form("((TProofOutputFile*)0x%lx)->GetFileName();", (ULong_t)pof);
1682 filename = (const char*)gROOT->ProcessLine(line);
1684 printf("File: %s already booked via TProofOutputFile\n", filename.Data());
1686 f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
1688 Fatal("OpenProofFile", "Proof output file found but no file opened for %s", filename.Data());
1691 // Check if option "UPDATE" was preserved
1692 TString opt(option);
1694 if ((opt=="UPDATE") && (opt!=f->GetOption()))
1695 Fatal("OpenProofFile", "File %s already opened, but not in UPDATE mode!", cont->GetFileName());
1697 if (cont->IsRegisterDataset()) {
1698 TString dsetName = filename;
1699 dsetName.ReplaceAll(".root", cont->GetTitle());
1700 dsetName.ReplaceAll(":","_");
1701 if (fDebug>1) printf("Booking dataset: %s\n", dsetName.Data());
1702 line = Form("TProofOutputFile *pf = new TProofOutputFile(\"%s\", \"DROV\", \"%s\");", filename.Data(), dsetName.Data());
1704 if (fDebug>1) printf("Booking TProofOutputFile: %s to be merged\n", filename.Data());
1705 line = Form("TProofOutputFile *pf = new TProofOutputFile(\"%s\");", filename.Data());
1707 if (fDebug > 1) printf("=== %s\n", line.Data());
1708 gROOT->ProcessLine(line);
1709 line = Form("pf->OpenFile(\"%s\");", option);
1710 gROOT->ProcessLine(line);
1713 gROOT->ProcessLine("pf->Print()");
1714 printf(" == proof file name: %s", f->GetName());
1716 // Add to proof output list
1717 line = Form("((TList*)0x%lx)->Add(pf);",(ULong_t)fSelector->GetOutputList());
1718 if (fDebug > 1) printf("=== %s\n", line.Data());
1719 gROOT->ProcessLine(line);
1721 if (f && !f->IsZombie() && !f->TestBit(TFile::kRecovered)) {
1725 // Check for a folder request
1726 TString dir = cont->GetFolderName();
1727 if (!dir.IsNull()) {
1728 if (!f->GetDirectory(dir)) f->mkdir(dir);
1733 Fatal("OpenProofFile", "File %s could not be opened", cont->GetFileName());
1734 cont->SetFile(NULL);
1738 //______________________________________________________________________________
1739 void AliAnalysisManager::ExecAnalysis(Option_t *option)
1741 // Execute analysis.
1742 static Long64_t ncalls = 0;
1743 static Long64_t nentries = 0;
1744 static TTree *lastTree = 0;
1745 static TStopwatch *timer = new TStopwatch();
1746 if (fDebug > 0) printf("MGR: Processing event #%lld\n", ncalls);
1748 if (fTree && (fTree != lastTree)) {
1749 nentries += fTree->GetEntries();
1752 if (!ncalls) timer->Start();
1753 if (!fIsRemote && TObject::TestBit(kUseProgressBar)) ProgressBar("Processing event", ncalls, nentries, timer, kFALSE);
1756 TDirectory *cdir = gDirectory;
1757 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
1758 if (getsysInfo && ((ncalls%fNSysInfo)==0)) AliSysInfo::AddStamp("Exec_start", (Int_t)ncalls);
1761 Error("ExecAnalysis", "Analysis manager was not initialized !");
1765 AliAnalysisTask *task;
1766 // Check if the top tree is active.
1768 if (getsysInfo && ((ncalls%fNSysInfo)==0))
1769 AliSysInfo::AddStamp("Handlers_BeginEventGroup",(Int_t)ncalls, 1002, 0);
1771 // De-activate all tasks
1772 while ((task=(AliAnalysisTask*)next())) task->SetActive(kFALSE);
1773 AliAnalysisDataContainer *cont = fCommonInput;
1774 if (!cont) cont = (AliAnalysisDataContainer*)fInputs->At(0);
1776 Error("ExecAnalysis","Cannot execute analysis in TSelector mode without at least one top container");
1780 cont->SetData(fTree); // This will notify all consumers
1781 Long64_t entry = fTree->GetTree()->GetReadEntry();
1783 // Call BeginEvent() for optional input/output and MC services
1784 if (fInputEventHandler) fInputEventHandler ->BeginEvent(entry);
1785 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent(entry);
1786 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(entry);
1788 if (getsysInfo && ((ncalls%fNSysInfo)==0))
1789 AliSysInfo::AddStamp("Handlers_BeginEvent",(Int_t)ncalls, 1000, 0);
1791 // Execute the tasks
1792 // TIter next1(cont->GetConsumers());
1793 TIter next1(fTopTasks);
1795 while ((task=(AliAnalysisTask*)next1())) {
1797 cout << " Executing task " << task->GetName() << endl;
1799 task->ExecuteTask(option);
1801 if (getsysInfo && ((ncalls%fNSysInfo)==0))
1802 AliSysInfo::AddStamp(task->ClassName(),(Int_t)ncalls, itask, 1);
1806 // Call FinishEvent() for optional output and MC services
1807 if (fInputEventHandler) fInputEventHandler ->FinishEvent();
1808 if (fOutputEventHandler) fOutputEventHandler ->FinishEvent();
1809 if (fMCtruthEventHandler) fMCtruthEventHandler->FinishEvent();
1810 // Gather system information if requested
1811 if (getsysInfo && ((ncalls%fNSysInfo)==0))
1812 AliSysInfo::AddStamp("Handlers_FinishEvent",(Int_t)ncalls, 1001, 1);
1816 // The event loop is not controlled by TSelector
1818 // Call BeginEvent() for optional input/output and MC services
1819 if (fInputEventHandler) fInputEventHandler ->BeginEvent(-1);
1820 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent(-1);
1821 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(-1);
1823 if (getsysInfo && ((ncalls%fNSysInfo)==0))
1824 AliSysInfo::AddStamp("Handlers_BeginEvent",(Int_t)ncalls, 1000, 0);
1825 TIter next2(fTopTasks);
1826 while ((task=(AliAnalysisTask*)next2())) {
1827 task->SetActive(kTRUE);
1829 cout << " Executing task " << task->GetName() << endl;
1831 task->ExecuteTask(option);
1835 // Call FinishEvent() for optional output and MC services
1836 if (fInputEventHandler) fInputEventHandler ->FinishEvent();
1837 if (fOutputEventHandler) fOutputEventHandler ->FinishEvent();
1838 if (fMCtruthEventHandler) fMCtruthEventHandler->FinishEvent();
1839 if (getsysInfo && ((ncalls%fNSysInfo)==0))
1840 AliSysInfo::AddStamp("Handlers_FinishEvent",(Int_t)ncalls, 1000, 1);
1844 //______________________________________________________________________________
1845 void AliAnalysisManager::SetInputEventHandler(AliVEventHandler* const handler)
1847 // Set the input event handler and create a container for it.
1848 fInputEventHandler = handler;
1849 fCommonInput = CreateContainer("cAUTO_INPUT", TChain::Class(), AliAnalysisManager::kInputContainer);
1850 // Warning("SetInputEventHandler", " An automatic input container for the input chain was created.\nPlease use: mgr->GetCommonInputContainer() to access it.");
1853 //______________________________________________________________________________
1854 void AliAnalysisManager::SetOutputEventHandler(AliVEventHandler* const handler)
1856 // Set the input event handler and create a container for it.
1857 fOutputEventHandler = handler;
1858 fCommonOutput = CreateContainer("cAUTO_OUTPUT", TTree::Class(), AliAnalysisManager::kOutputContainer, "default");
1859 fCommonOutput->SetSpecialOutput();
1860 // Warning("SetOutputEventHandler", " An automatic output container for the output tree was created.\nPlease use: mgr->GetCommonOutputContainer() to access it.");
1863 //______________________________________________________________________________
1864 void AliAnalysisManager::RegisterExtraFile(const char *fname)
1866 // This method is used externally to register output files which are not
1867 // connected to any output container, so that the manager can properly register,
1868 // retrieve or merge them when running in distributed mode. The file names are
1869 // separated by blancs. The method has to be called in MyAnalysisTask::LocalInit().
1870 if (fExtraFiles.Contains(fname)) return;
1871 if (fExtraFiles.Length()) fExtraFiles += " ";
1872 fExtraFiles += fname;
1875 //______________________________________________________________________________
1876 Bool_t AliAnalysisManager::GetFileFromWrapper(const char *filename, const TList *source)
1878 // Copy a file from the location specified ina the wrapper with the same name from the source list.
1881 TObject *pof = source->FindObject(filename);
1882 if (!pof || !pof->InheritsFrom("TProofOutputFile")) {
1883 Error("GetFileFromWrapper", "TProofOutputFile object not found in output list for file %s", filename);
1886 gROOT->ProcessLine(Form("sprintf((char*)0x%lx, \"%%s\", ((TProofOutputFile*)0x%lx)->GetOutputFileName();)", (ULong_t)fullPath, (ULong_t)pof));
1887 gROOT->ProcessLine(Form("sprintf((char*)0x%lx, \"%%s\", gProof->GetUrl();)", (ULong_t)chUrl));
1888 TString clientUrl(chUrl);
1889 TString fullPath_str(fullPath);
1890 if (clientUrl.Contains("localhost")){
1891 TObjArray* array = fullPath_str.Tokenize ( "//" );
1892 TObjString *strobj = ( TObjString *)array->At(1);
1893 TObjArray* arrayPort = strobj->GetString().Tokenize ( ":" );
1894 TObjString *strobjPort = ( TObjString *) arrayPort->At(1);
1895 fullPath_str.ReplaceAll(strobj->GetString().Data(),"localhost:PORT");
1896 fullPath_str.ReplaceAll(":PORT",Form(":%s",strobjPort->GetString().Data()));
1897 if (fDebug > 1) Info("GetFileFromWrapper","Using tunnel from %s to %s",fullPath_str.Data(),filename);
1902 Info("GetFileFromWrapper","Copying file %s from PROOF scratch space", fullPath_str.Data());
1903 Bool_t gotit = TFile::Cp(fullPath_str.Data(), filename);
1905 Error("GetFileFromWrapper", "Could not get file %s from proof scratch space", filename);
1909 //______________________________________________________________________________
1910 void AliAnalysisManager::GetAnalysisTypeString(TString &type) const
1912 // Fill analysis type in the provided string.
1914 case kLocalAnalysis:
1917 case kProofAnalysis:
1923 case kMixingAnalysis:
1928 //______________________________________________________________________________
1929 Bool_t AliAnalysisManager::ValidateOutputFiles() const
1931 // Validate all output files.
1932 TIter next(fOutputs);
1933 AliAnalysisDataContainer *output;
1934 TDirectory *cdir = gDirectory;
1935 TString openedFiles;
1936 while ((output=(AliAnalysisDataContainer*)next())) {
1937 if (output->IsRegisterDataset()) continue;
1938 TString filename = output->GetFileName();
1939 if (filename == "default") {
1940 if (!fOutputEventHandler) continue;
1941 filename = fOutputEventHandler->GetOutputFileName();
1942 // Main AOD may not be there
1943 if (gSystem->AccessPathName(filename)) continue;
1945 // Check if the file is closed
1946 if (openedFiles.Contains(filename)) continue;;
1947 TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
1949 Warning("ValidateOutputs", "File %s was not closed. Closing.", filename.Data());
1950 // Clear file list to release object ownership to user.
1954 file = TFile::Open(filename);
1955 if (!file || file->IsZombie() || file->TestBit(TFile::kRecovered)) {
1956 Error("ValidateOutputs", "Output file <%s> was not created or invalid", filename.Data());
1961 openedFiles += filename;
1968 //______________________________________________________________________________
1969 void AliAnalysisManager::ProgressBar(const char *opname, Long64_t current, Long64_t size, TStopwatch * const watch, Bool_t last, Bool_t refresh)
1971 // Implements a nice text mode progress bar.
1972 static Long64_t icount = 0;
1973 static TString oname;
1974 static TString nname;
1975 static Long64_t ocurrent = 0;
1976 static Long64_t osize = 0;
1977 static Int_t oseconds = 0;
1978 static TStopwatch *owatch = 0;
1979 static Bool_t oneoftwo = kFALSE;
1980 static Int_t nrefresh = 0;
1981 static Int_t nchecks = 0;
1982 const char symbol[4] = {'=','\\','|','/'};
1983 char progress[11] = " ";
1984 Int_t ichar = icount%4;
1991 ocurrent = TMath::Abs(current);
1992 osize = TMath::Abs(size);
1993 if (ocurrent > osize) ocurrent=osize;
2003 if (owatch && !last) {
2005 time = owatch->RealTime();
2006 hours = (Int_t)(time/3600.);
2008 minutes = (Int_t)(time/60.);
2010 seconds = (Int_t)time;
2012 if (oseconds==seconds) {
2016 oneoftwo = !oneoftwo;
2020 if (refresh && oneoftwo) {
2022 if (nchecks <= 0) nchecks = nrefresh+1;
2023 Int_t pctdone = (Int_t)(100.*nrefresh/nchecks);
2024 oname = Form(" == %d%% ==", pctdone);
2026 Double_t percent = 100.0*ocurrent/osize;
2027 Int_t nchar = Int_t(percent/10);
2028 if (nchar>10) nchar=10;
2030 for (i=0; i<nchar; i++) progress[i] = '=';
2031 progress[nchar] = symbol[ichar];
2032 for (i=nchar+1; i<10; i++) progress[i] = ' ';
2033 progress[10] = '\0';
2036 if(size<10000) fprintf(stderr, "%s [%10s] %4lld ", oname.Data(), progress, ocurrent);
2037 else if(size<100000) fprintf(stderr, "%s [%10s] %5lld ",oname.Data(), progress, ocurrent);
2038 else fprintf(stderr, "%s [%10s] %7lld ",oname.Data(), progress, ocurrent);
2039 if (time>0.) fprintf(stderr, "[%6.2f %%] TIME %.2d:%.2d:%.2d \r", percent, hours, minutes, seconds);
2040 else fprintf(stderr, "[%6.2f %%]\r", percent);
2041 if (refresh && oneoftwo) oname = nname;
2042 if (owatch) owatch->Continue();
2051 fprintf(stderr, "\n");
2055 //______________________________________________________________________________
2056 void AliAnalysisManager::DoLoadBranch(const char *name)
2058 // Get tree and load branch if needed.
2063 TBranch *br = dynamic_cast<TBranch*>(fTable.FindObject(name));
2065 br = fTree->GetBranch(name);
2067 Error("DoLoadBranch", "Could not find branch %s",name);
2072 if (br->GetReadEntry()==GetCurrentEntry())
2074 br->GetEntry(GetCurrentEntry());