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 // AliAnalysisManager - 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),
92 // Default constructor.
93 fgAnalysisManager = this;
94 fgCommonFileName = "AnalysisResults.root";
95 fTasks = new TObjArray();
96 fTopTasks = new TObjArray();
97 fZombies = new TObjArray();
98 fContainers = new TObjArray();
99 fInputs = new TObjArray();
100 fOutputs = new TObjArray();
101 fParamCont = new TObjArray();
103 TObject::SetObjectStat(kFALSE);
106 //______________________________________________________________________________
107 AliAnalysisManager::AliAnalysisManager(const AliAnalysisManager& other)
110 fInputEventHandler(NULL),
111 fOutputEventHandler(NULL),
112 fMCtruthEventHandler(NULL),
117 fInitOK(other.fInitOK),
118 fIsRemote(other.fIsRemote),
119 fDebug(other.fDebug),
120 fSpecialOutputLocation(""),
133 fAutoBranchHandling(other.fAutoBranchHandling),
136 fNcalls(other.fNcalls),
137 fStatisticsMsg(other.fStatisticsMsg),
138 fRequestedBranches(other.fRequestedBranches)
141 fTasks = new TObjArray(*other.fTasks);
142 fTopTasks = new TObjArray(*other.fTopTasks);
143 fZombies = new TObjArray(*other.fZombies);
144 fContainers = new TObjArray(*other.fContainers);
145 fInputs = new TObjArray(*other.fInputs);
146 fOutputs = new TObjArray(*other.fOutputs);
147 fParamCont = new TObjArray(*other.fParamCont);
148 fgCommonFileName = "AnalysisResults.root";
149 fgAnalysisManager = this;
150 TObject::SetObjectStat(kFALSE);
153 //______________________________________________________________________________
154 AliAnalysisManager& AliAnalysisManager::operator=(const AliAnalysisManager& other)
157 if (&other != this) {
158 TNamed::operator=(other);
159 fInputEventHandler = other.fInputEventHandler;
160 fOutputEventHandler = other.fOutputEventHandler;
161 fMCtruthEventHandler = other.fMCtruthEventHandler;
162 fEventPool = other.fEventPool;
165 fNSysInfo = other.fNSysInfo;
167 fInitOK = other.fInitOK;
168 fIsRemote = other.fIsRemote;
169 fDebug = other.fDebug;
170 fTasks = new TObjArray(*other.fTasks);
171 fTopTasks = new TObjArray(*other.fTopTasks);
172 fZombies = new TObjArray(*other.fZombies);
173 fContainers = new TObjArray(*other.fContainers);
174 fInputs = new TObjArray(*other.fInputs);
175 fOutputs = new TObjArray(*other.fOutputs);
176 fParamCont = new TObjArray(*other.fParamCont);
178 fCommonOutput = NULL;
181 fExtraFiles = other.fExtraFiles;
182 fgCommonFileName = "AnalysisResults.root";
183 fgAnalysisManager = this;
184 fAutoBranchHandling = other.fAutoBranchHandling;
185 fTable.Clear("nodelete");
186 fRunFromPath = other.fRunFromPath;
187 fNcalls = other. fNcalls;
188 fStatisticsMsg = other.fStatisticsMsg;
189 fRequestedBranches = other.fRequestedBranches;
194 //______________________________________________________________________________
195 AliAnalysisManager::~AliAnalysisManager()
198 if (fTasks) {fTasks->Delete(); delete fTasks;}
199 if (fTopTasks) delete fTopTasks;
200 if (fZombies) delete fZombies;
201 if (fContainers) {fContainers->Delete(); delete fContainers;}
202 if (fInputs) delete fInputs;
203 if (fOutputs) delete fOutputs;
204 if (fParamCont) delete fParamCont;
205 if (fGridHandler) delete fGridHandler;
206 if (fInputEventHandler) delete fInputEventHandler;
207 if (fOutputEventHandler) delete fOutputEventHandler;
208 if (fMCtruthEventHandler) delete fMCtruthEventHandler;
209 if (fEventPool) delete fEventPool;
210 if (fgAnalysisManager==this) fgAnalysisManager = NULL;
211 TObject::SetObjectStat(kTRUE);
214 //______________________________________________________________________________
215 Int_t AliAnalysisManager::GetEntry(Long64_t entry, Int_t getall)
217 // Read one entry of the tree or a whole branch.
218 fCurrentEntry = entry;
219 if (!fAutoBranchHandling)
221 return fTree ? fTree->GetTree()->GetEntry(entry, getall) : 0;
224 //______________________________________________________________________________
225 Int_t AliAnalysisManager::GetRunFromAlienPath(const char *path)
227 // Attempt to extract run number from input data path. Works only for paths to
228 // alice data in alien.
229 // sim: /alice/sim/<production>/run_no/...
230 // data: /alice/data/year/period/000run_no/... (ESD or AOD)
234 Int_t index = s.Index("/alice/sim");
236 for (Int_t i=0; i<3; i++) {
237 index = s.Index("/", index+1);
238 if (index<0) return 0;
243 index = s.Index("/alice/data");
245 for (Int_t i=0; i<4; i++) {
246 index = s.Index("/", index+1);
247 if (index<0) return 0;
255 //______________________________________________________________________________
256 Bool_t AliAnalysisManager::Init(TTree *tree)
258 // The Init() function is called when the selector needs to initialize
259 // a new tree or chain. Typically here the branch addresses of the tree
260 // will be set. It is normaly not necessary to make changes to the
261 // generated code, but the routine can be extended by the user if needed.
262 // Init() will be called many times when running with PROOF.
263 Bool_t init = kFALSE;
264 if (!tree) return kFALSE; // Should not happen - protected in selector caller
266 printf("->AliAnalysisManager::Init(%s)\n", tree->GetName());
268 // Call InitTree of EventHandler
269 if (fOutputEventHandler) {
270 if (fMode == kProofAnalysis) {
271 init = fOutputEventHandler->Init(0x0, "proof");
273 init = fOutputEventHandler->Init(0x0, "local");
276 Error("Init", "Output event handler failed to initialize");
281 if (fInputEventHandler) {
282 if (fMode == kProofAnalysis) {
283 init = fInputEventHandler->Init(tree, "proof");
285 init = fInputEventHandler->Init(tree, "local");
288 Error("Init", "Input event handler failed to initialize tree");
292 // If no input event handler we need to get the tree once
294 if(!tree->GetTree()) {
295 Long64_t readEntry = tree->LoadTree(0);
296 if (readEntry == -2) {
297 Error("Init", "Input tree has no entry. Exiting");
303 if (fMCtruthEventHandler) {
304 if (fMode == kProofAnalysis) {
305 init = fMCtruthEventHandler->Init(0x0, "proof");
307 init = fMCtruthEventHandler->Init(0x0, "local");
310 Error("Init", "MC event handler failed to initialize");
315 if (!fInitOK) InitAnalysis();
316 if (!fInitOK) return kFALSE;
319 AliAnalysisDataContainer *top = fCommonInput;
320 if (!top) top = (AliAnalysisDataContainer*)fInputs->At(0);
322 Error("Init","No top input container !");
326 CheckBranches(kFALSE);
328 printf("<-AliAnalysisManager::Init(%s)\n", tree->GetName());
333 //______________________________________________________________________________
334 void AliAnalysisManager::SlaveBegin(TTree *tree)
336 // The SlaveBegin() function is called after the Begin() function.
337 // When running with PROOF SlaveBegin() is called on each slave server.
338 // The tree argument is deprecated (on PROOF 0 is passed).
339 if (fDebug > 1) printf("->AliAnalysisManager::SlaveBegin()\n");
340 static Bool_t isCalled = kFALSE;
341 Bool_t init = kFALSE;
342 Bool_t initOK = kTRUE;
344 TDirectory *curdir = gDirectory;
345 // Call SlaveBegin only once in case of mixing
346 if (isCalled && fMode==kMixingAnalysis) return;
348 // Call Init of EventHandler
349 if (fOutputEventHandler) {
350 if (fMode == kProofAnalysis) {
351 // Merging AOD's in PROOF via TProofOutputFile
352 if (fDebug > 1) printf(" Initializing AOD output file %s...\n", fOutputEventHandler->GetOutputFileName());
353 init = fOutputEventHandler->Init("proof");
354 if (!init) msg = "Failed to initialize output handler on worker";
356 init = fOutputEventHandler->Init("local");
357 if (!init) msg = "Failed to initialize output handler";
360 if (!fSelector) Error("SlaveBegin", "Selector not set");
361 else if (!init) {fSelector->Abort(msg); fSelector->SetStatus(-1);}
364 if (fInputEventHandler) {
365 fInputEventHandler->SetInputTree(tree);
366 if (fMode == kProofAnalysis) {
367 init = fInputEventHandler->Init("proof");
368 if (!init) msg = "Failed to initialize input handler on worker";
370 init = fInputEventHandler->Init("local");
371 if (!init) msg = "Failed to initialize input handler";
374 if (!fSelector) Error("SlaveBegin", "Selector not set");
375 else if (!init) {fSelector->Abort(msg); fSelector->SetStatus(-1);}
378 if (fMCtruthEventHandler) {
379 if (fMode == kProofAnalysis) {
380 init = fMCtruthEventHandler->Init("proof");
381 if (!init) msg = "Failed to initialize MC handler on worker";
383 init = fMCtruthEventHandler->Init("local");
384 if (!init) msg = "Failed to initialize MC handler";
387 if (!fSelector) Error("SlaveBegin", "Selector not set");
388 else if (!init) {fSelector->Abort(msg); fSelector->SetStatus(-1);}
390 if (curdir) curdir->cd();
394 AliAnalysisTask *task;
395 // Call CreateOutputObjects for all tasks
396 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
397 Bool_t dirStatus = TH1::AddDirectoryStatus();
399 while ((task=(AliAnalysisTask*)next())) {
401 // Start with memory as current dir and make sure by default histograms do not get attached to files.
402 TH1::AddDirectory(kFALSE);
403 task->CreateOutputObjects();
404 if (getsysInfo) AliSysInfo::AddStamp(Form("%s_CREATEOUTOBJ",task->ClassName()), 0, itask, 0);
407 TH1::AddDirectory(dirStatus);
408 if (curdir) curdir->cd();
409 if (fDebug > 1) printf("<-AliAnalysisManager::SlaveBegin()\n");
412 //______________________________________________________________________________
413 Bool_t AliAnalysisManager::Notify()
415 // The Notify() function is called when a new file is opened. This
416 // can be either for a new TTree in a TChain or when when a new TTree
417 // is started when using PROOF. It is normaly not necessary to make changes
418 // to the generated code, but the routine can be extended by the
419 // user if needed. The return value is currently not used.
420 if (!fTree) return kFALSE;
422 fTable.Clear("nodelete"); // clearing the hash table may not be needed -> C.L.
423 if (fMode == kProofAnalysis) fIsRemote = kTRUE;
425 TFile *curfile = fTree->GetCurrentFile();
427 Error("Notify","No current file");
431 if (fDebug > 1) printf("->AliAnalysisManager::Notify() file: %s\n", curfile->GetName());
432 Int_t run = AliAnalysisManager::GetRunFromAlienPath(curfile->GetName());
433 if (run) SetRunFromPath(run);
434 if (fDebug > 1) printf(" ### run found from path: %d\n", run);
436 AliAnalysisTask *task;
438 // Call Notify of the event handlers
439 if (fInputEventHandler) {
440 fInputEventHandler->Notify(curfile->GetName());
443 if (fOutputEventHandler) {
444 fOutputEventHandler->Notify(curfile->GetName());
447 if (fMCtruthEventHandler) {
448 fMCtruthEventHandler->Notify(curfile->GetName());
451 // Call Notify for all tasks
452 while ((task=(AliAnalysisTask*)next()))
455 if (fDebug > 1) printf("<-AliAnalysisManager::Notify()\n");
459 //______________________________________________________________________________
460 Bool_t AliAnalysisManager::Process(Long64_t entry)
462 // The Process() function is called for each entry in the tree (or possibly
463 // keyed object in the case of PROOF) to be processed. The entry argument
464 // specifies which entry in the currently loaded tree is to be processed.
465 // It can be passed to either TTree::GetEntry() or TBranch::GetEntry()
466 // to read either all or the required parts of the data. When processing
467 // keyed objects with PROOF, the object is already loaded and is available
468 // via the fObject pointer.
470 // This function should contain the "body" of the analysis. It can contain
471 // simple or elaborate selection criteria, run algorithms on the data
472 // of the event and typically fill histograms.
474 // WARNING when a selector is used with a TChain, you must use
475 // the pointer to the current TTree to call GetEntry(entry).
476 // The entry is always the local entry number in the current tree.
477 // Assuming that fChain is the pointer to the TChain being processed,
478 // use fChain->GetTree()->GetEntry(entry).
479 if (fDebug > 1) printf("->AliAnalysisManager::Process(%lld)\n", entry);
481 if (fInputEventHandler) fInputEventHandler ->BeginEvent(entry);
482 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent(entry);
483 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(entry);
487 if (fInputEventHandler) fInputEventHandler ->GetEntry();
490 if (fDebug > 1) printf("<-AliAnalysisManager::Process()\n");
494 //______________________________________________________________________________
495 void AliAnalysisManager::PackOutput(TList *target)
497 // Pack all output data containers in the output list. Called at SlaveTerminate
498 // stage in PROOF case for each slave.
499 if (fDebug > 1) printf("->AliAnalysisManager::PackOutput()\n");
501 Error("PackOutput", "No target. Exiting.");
504 TDirectory *cdir = gDirectory;
506 if (fInputEventHandler) fInputEventHandler ->Terminate();
507 if (fOutputEventHandler) fOutputEventHandler ->Terminate();
508 if (fMCtruthEventHandler) fMCtruthEventHandler->Terminate();
511 // Call FinishTaskOutput() for each event loop task (not called for
512 // post-event loop tasks - use Terminate() fo those)
513 TIter nexttask(fTasks);
514 AliAnalysisTask *task;
515 while ((task=(AliAnalysisTask*)nexttask())) {
516 if (!task->IsPostEventLoop()) {
517 if (fDebug > 1) printf("->FinishTaskOutput: task %s\n", task->GetName());
518 task->FinishTaskOutput();
520 if (fDebug > 1) printf("<-FinishTaskOutput: task %s\n", task->GetName());
523 // Write statistics message on the workers.
524 WriteStatisticsMsg(fNcalls);
526 if (fMode == kProofAnalysis) {
527 TIter next(fOutputs);
528 AliAnalysisDataContainer *output;
529 Bool_t isManagedByHandler = kFALSE;
532 while ((output=(AliAnalysisDataContainer*)next())) {
533 // Do not consider outputs of post event loop tasks
534 isManagedByHandler = kFALSE;
535 if (output->GetProducer() && output->GetProducer()->IsPostEventLoop()) continue;
536 const char *filename = output->GetFileName();
537 if (!(strcmp(filename, "default")) && fOutputEventHandler) {
538 isManagedByHandler = kTRUE;
539 printf("#### Handler output. Extra: %s\n", fExtraFiles.Data());
540 filename = fOutputEventHandler->GetOutputFileName();
542 // Check if data was posted to this container. If not, issue an error.
543 if (!output->GetData() && !isManagedByHandler) {
544 Error("PackOutput", "No data for output container %s. Forgot to PostData ?", output->GetName());
547 if (!output->IsSpecialOutput()) {
549 if (strlen(filename) && !isManagedByHandler) {
550 // Backup current folder
551 TDirectory *opwd = gDirectory;
552 // File resident outputs.
553 // Check first if the file exists.
554 TString openoption = "RECREATE";
555 Bool_t firsttime = kTRUE;
556 if (filestmp.FindObject(output->GetFileName())) {
559 filestmp.Add(new TNamed(output->GetFileName(),""));
561 if (!gSystem->AccessPathName(output->GetFileName()) && !firsttime) openoption = "UPDATE";
562 // TFile *file = AliAnalysisManager::OpenFile(output, openoption, kTRUE);
563 // Save data to file, then close.
564 if (output->GetData()->InheritsFrom(TCollection::Class())) {
565 // If data is a collection, we set the name of the collection
566 // as the one of the container and we save as a single key.
567 TCollection *coll = (TCollection*)output->GetData();
568 coll->SetName(output->GetName());
569 // coll->Write(output->GetName(), TObject::kSingleKey);
571 if (output->GetData()->InheritsFrom(TTree::Class())) {
572 TFile *file = AliAnalysisManager::OpenFile(output, openoption, kTRUE);
573 // Save data to file, then close.
574 TTree *tree = (TTree*)output->GetData();
575 // Check if tree is in memory
576 if (tree->GetDirectory()==gROOT) tree->SetDirectory(gDirectory);
580 // output->GetData()->Write();
583 if (fDebug > 1) printf("PackOutput %s: memory merge, file resident output\n", output->GetName());
585 // printf(" file %s listing content:\n", filename);
588 // Clear file list to release object ownership to user.
591 output->SetFile(NULL);
592 // Restore current directory
593 if (opwd) opwd->cd();
595 // Memory-resident outputs
596 if (fDebug > 1) printf("PackOutput %s: memory merge memory resident output\n", filename);
598 AliAnalysisDataWrapper *wrap = 0;
599 if (isManagedByHandler) {
600 wrap = new AliAnalysisDataWrapper(fOutputEventHandler->GetTree());
601 wrap->SetName(output->GetName());
603 else wrap =output->ExportData();
604 // Output wrappers must NOT delete data after merging - the user owns them
605 wrap->SetDeleteData(kFALSE);
608 // Special outputs. The file must be opened and connected to the container.
609 TDirectory *opwd = gDirectory;
610 TFile *file = output->GetFile();
612 AliAnalysisTask *producer = output->GetProducer();
614 "File %s for special container %s was NOT opened in %s::CreateOutputObjects !!!",
615 output->GetFileName(), output->GetName(), producer->ClassName());
618 TString outFilename = file->GetName();
619 if (fDebug > 1) printf("PackOutput %s: special output\n", output->GetName());
620 if (isManagedByHandler) {
621 // Terminate IO for files managed by the output handler
622 // file->Write() moved to AOD handler (A.G. 11.01.10)
623 // if (file) file->Write();
624 if (file && fDebug > 2) {
625 printf(" handled file %s listing content:\n", file->GetName());
628 fOutputEventHandler->TerminateIO();
631 // Release object ownership to users after writing data to file
632 if (output->GetData()->InheritsFrom(TCollection::Class())) {
633 // If data is a collection, we set the name of the collection
634 // as the one of the container and we save as a single key.
635 TCollection *coll = (TCollection*)output->GetData();
636 coll->SetName(output->GetName());
637 coll->Write(output->GetName(), TObject::kSingleKey);
639 if (output->GetData()->InheritsFrom(TTree::Class())) {
640 TTree *tree = (TTree*)output->GetData();
641 tree->SetDirectory(file);
644 output->GetData()->Write();
648 printf(" file %s listing content:\n", output->GetFileName());
651 // Clear file list to release object ownership to user.
654 output->SetFile(NULL);
656 // Restore current directory
657 if (opwd) opwd->cd();
658 // Check if a special output location was provided or the output files have to be merged
659 if (strlen(fSpecialOutputLocation.Data())) {
660 TString remote = fSpecialOutputLocation;
662 Int_t gid = gROOT->ProcessLine("gProofServ->GetGroupId();");
663 if (remote.BeginsWith("alien:")) {
664 gROOT->ProcessLine("TGrid::Connect(\"alien:\", gProofServ->GetUser());");
665 remote += outFilename;
666 remote.ReplaceAll(".root", Form("_%d.root", gid));
668 remote += Form("%s_%d_", gSystem->HostName(), gid);
669 remote += outFilename;
672 Info("PackOutput", "Output file for container %s to be copied \n at: %s. No merging.",
673 output->GetName(), remote.Data());
674 TFile::Cp ( outFilename.Data(), remote.Data() );
675 // Copy extra outputs
676 if (fExtraFiles.Length() && isManagedByHandler) {
677 TObjArray *arr = fExtraFiles.Tokenize(" ");
679 TIter nextfilename(arr);
680 while ((os=(TObjString*)nextfilename())) {
681 outFilename = os->GetString();
682 remote = fSpecialOutputLocation;
684 if (remote.BeginsWith("alien://")) {
685 remote += outFilename;
686 remote.ReplaceAll(".root", Form("_%d.root", gid));
688 remote += Form("%s_%d_", gSystem->HostName(), gid);
689 remote += outFilename;
692 Info("PackOutput", "Extra AOD file %s to be copied \n at: %s. No merging.",
693 outFilename.Data(), remote.Data());
694 TFile::Cp ( outFilename.Data(), remote.Data() );
699 // No special location specified-> use TProofOutputFile as merging utility
700 // The file at this output slot must be opened in CreateOutputObjects
701 if (fDebug > 1) printf(" File for container %s to be merged via file merger...\n", output->GetName());
707 if (fDebug > 1) printf("<-AliAnalysisManager::PackOutput: output list contains %d containers\n", target->GetSize());
710 //______________________________________________________________________________
711 void AliAnalysisManager::ImportWrappers(TList *source)
713 // Import data in output containers from wrappers coming in source.
714 if (fDebug > 1) printf("->AliAnalysisManager::ImportWrappers()\n");
715 TIter next(fOutputs);
716 AliAnalysisDataContainer *cont;
717 AliAnalysisDataWrapper *wrap;
719 Bool_t inGrid = (fMode == kGridAnalysis)?kTRUE:kFALSE;
720 TDirectory *cdir = gDirectory;
721 while ((cont=(AliAnalysisDataContainer*)next())) {
723 if (cont->GetProducer() && cont->GetProducer()->IsPostEventLoop() && !inGrid) continue;
724 if (cont->IsRegisterDataset()) continue;
725 const char *filename = cont->GetFileName();
726 Bool_t isManagedByHandler = kFALSE;
727 if (!(strcmp(filename, "default")) && fOutputEventHandler) {
728 isManagedByHandler = kTRUE;
729 filename = fOutputEventHandler->GetOutputFileName();
731 if (cont->IsSpecialOutput() || inGrid) {
732 if (strlen(fSpecialOutputLocation.Data())) continue;
733 // Copy merged file from PROOF scratch space.
734 // In case of grid the files are already in the current directory.
736 if (isManagedByHandler && fExtraFiles.Length()) {
737 // Copy extra registered dAOD files.
738 TObjArray *arr = fExtraFiles.Tokenize(" ");
740 TIter nextfilename(arr);
741 while ((os=(TObjString*)nextfilename())) GetFileFromWrapper(os->GetString(), source);
744 if (!GetFileFromWrapper(filename, source)) continue;
746 // Normally we should connect data from the copied file to the
747 // corresponding output container, but it is not obvious how to do this
748 // automatically if several objects in file...
749 TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
750 if (!f) f = TFile::Open(filename, "READ");
752 Error("ImportWrappers", "Cannot open file %s in read-only mode", filename);
756 // Cd to the directory pointed by the container
757 TString folder = cont->GetFolderName();
758 if (!folder.IsNull()) f->cd(folder);
759 // Try to fetch first an object having the container name.
760 obj = gDirectory->Get(cont->GetName());
762 Warning("ImportWrappers", "Could not import object of type:%s for container %s in file %s:%s.\n Object will not be available in Terminate(). Try if possible to name the output object as the container (%s) or to embed it in a TList",
763 cont->GetType()->GetName(), cont->GetName(), filename, cont->GetFolderName(), cont->GetName());
766 wrap = new AliAnalysisDataWrapper(obj);
767 wrap->SetDeleteData(kFALSE);
769 if (!wrap) wrap = (AliAnalysisDataWrapper*)source->FindObject(cont->GetName());
771 Error("ImportWrappers","Container %s not found in analysis output !", cont->GetName());
776 printf(" Importing data for container %s\n", cont->GetName());
777 if (strlen(filename)) printf(" -> file %s\n", filename);
780 cont->ImportData(wrap);
782 if (cdir) cdir->cd();
783 if (fDebug > 1) printf("<-AliAnalysisManager::ImportWrappers(): %d containers imported\n", icont);
786 //______________________________________________________________________________
787 void AliAnalysisManager::UnpackOutput(TList *source)
789 // Called by AliAnalysisSelector::Terminate only on the client.
790 if (fDebug > 1) printf("->AliAnalysisManager::UnpackOutput()\n");
792 Error("UnpackOutput", "No target. Exiting.");
795 if (fDebug > 1) printf(" Source list contains %d containers\n", source->GetSize());
797 if (fMode == kProofAnalysis) ImportWrappers(source);
799 TIter next(fOutputs);
800 AliAnalysisDataContainer *output;
801 while ((output=(AliAnalysisDataContainer*)next())) {
802 if (!output->GetData()) continue;
803 // Check if there are client tasks that run post event loop
804 if (output->HasConsumers()) {
805 // Disable event loop semaphore
806 output->SetPostEventLoop(kTRUE);
807 TObjArray *list = output->GetConsumers();
808 Int_t ncons = list->GetEntriesFast();
809 for (Int_t i=0; i<ncons; i++) {
810 AliAnalysisTask *task = (AliAnalysisTask*)list->At(i);
811 task->CheckNotify(kTRUE);
812 // If task is active, execute it
813 if (task->IsPostEventLoop() && task->IsActive()) {
814 if (fDebug > 1) printf("== Executing post event loop task %s\n", task->GetName());
820 if (fDebug > 1) printf("<-AliAnalysisManager::UnpackOutput()\n");
823 //______________________________________________________________________________
824 void AliAnalysisManager::Terminate()
826 // The Terminate() function is the last function to be called during
827 // a query. It always runs on the client, it can be used to present
828 // the results graphically.
829 if (fDebug > 1) printf("->AliAnalysisManager::Terminate()\n");
830 TDirectory *cdir = gDirectory;
832 AliAnalysisTask *task;
833 AliAnalysisDataContainer *output;
836 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
837 // Call Terminate() for tasks
839 while (!IsSkipTerminate() && (task=(AliAnalysisTask*)next())) {
840 // Save all the canvases produced by the Terminate
841 TString pictname = Form("%s_%s", task->GetName(), task->ClassName());
845 AliSysInfo::AddStamp(Form("%s_TERMINATE",task->ClassName()),0, itask, 2);
847 if (TObject::TestBit(kSaveCanvases)) {
848 if (!gROOT->IsBatch()) {
849 if (fDebug>1) printf("Waiting 5 sec for %s::Terminate() to finish drawing ...\n", task->ClassName());
851 while (timer.CpuTime()<5) {
853 gSystem->ProcessEvents();
856 Int_t iend = gROOT->GetListOfCanvases()->GetEntries();
857 if (iend==0) continue;
859 for (Int_t ipict=0; ipict<iend; ipict++) {
860 canvas = (TCanvas*)gROOT->GetListOfCanvases()->At(ipict);
861 if (!canvas) continue;
862 canvas->SaveAs(Form("%s_%02d.gif", pictname.Data(),ipict));
864 gROOT->GetListOfCanvases()->Delete();
868 if (fInputEventHandler) fInputEventHandler ->TerminateIO();
869 if (fOutputEventHandler) fOutputEventHandler ->TerminateIO();
870 if (fMCtruthEventHandler) fMCtruthEventHandler->TerminateIO();
872 TObjArray *allOutputs = new TObjArray();
874 for (icont=0; icont<fOutputs->GetEntriesFast(); icont++) allOutputs->Add(fOutputs->At(icont));
875 if (!IsSkipTerminate())
876 for (icont=0; icont<fParamCont->GetEntriesFast(); icont++) allOutputs->Add(fParamCont->At(icont));
877 TIter next1(allOutputs);
878 TString handlerFile = "";
879 TString extraOutputs = "";
880 if (fOutputEventHandler) {
881 handlerFile = fOutputEventHandler->GetOutputFileName();
882 extraOutputs = fOutputEventHandler->GetExtraOutputs();
886 while ((output=(AliAnalysisDataContainer*)next1())) {
887 // Special outputs or grid files have the files already closed and written.
889 if (fMode == kGridAnalysis && icont<=fOutputs->GetEntriesFast()) continue;
890 if (fMode == kProofAnalysis) {
891 if (output->IsSpecialOutput() || output->IsRegisterDataset()) continue;
893 const char *filename = output->GetFileName();
894 TString openoption = "RECREATE";
895 if (!(strcmp(filename, "default"))) continue;
896 if (!strlen(filename)) continue;
897 if (!output->GetData()) continue;
898 TDirectory *opwd = gDirectory;
899 TFile *file = output->GetFile();
900 if (!file) file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
902 //if (handlerFile == filename && !gSystem->AccessPathName(filename)) openoption = "UPDATE";
903 Bool_t firsttime = kTRUE;
904 if (filestmp.FindObject(filename) || extraOutputs.Contains(filename)) {
907 filestmp.Add(new TNamed(filename,""));
909 if (!gSystem->AccessPathName(filename) && !firsttime) openoption = "UPDATE";
910 if (fDebug>1) printf("Opening file: %s option=%s\n",filename, openoption.Data());
911 file = new TFile(filename, openoption);
913 if (fDebug>1) printf("File <%s> already opened with option: <%s> \n", filename, file->GetOption());
914 openoption = file->GetOption();
915 if (openoption == "READ") {
916 if (fDebug>1) printf("...reopening in UPDATE mode\n");
917 file->ReOpen("UPDATE");
920 if (file->IsZombie()) {
921 Error("Terminate", "Cannot open output file %s", filename);
924 output->SetFile(file);
926 // Check for a folder request
927 TString dir = output->GetFolderName();
929 if (!file->GetDirectory(dir)) file->mkdir(dir);
932 if (fDebug > 1) printf("...writing container %s to file %s:%s\n", output->GetName(), file->GetName(), output->GetFolderName());
933 if (output->GetData()->InheritsFrom(TCollection::Class())) {
934 // If data is a collection, we set the name of the collection
935 // as the one of the container and we save as a single key.
936 TCollection *coll = (TCollection*)output->GetData();
937 coll->SetName(output->GetName());
938 coll->Write(output->GetName(), TObject::kSingleKey);
940 if (output->GetData()->InheritsFrom(TTree::Class())) {
941 TTree *tree = (TTree*)output->GetData();
942 tree->SetDirectory(gDirectory);
945 output->GetData()->Write();
948 if (opwd) opwd->cd();
952 while ((output=(AliAnalysisDataContainer*)next1())) {
953 // Close all files at output
954 TDirectory *opwd = gDirectory;
955 if (output->GetFile()) {
956 // Clear file list to release object ownership to user.
957 // output->GetFile()->Clear();
958 output->GetFile()->Close();
959 output->SetFile(NULL);
960 // Copy merged outputs in alien if requested
961 if (fSpecialOutputLocation.Length() &&
962 fSpecialOutputLocation.BeginsWith("alien://")) {
963 Info("Terminate", "Copy file %s to %s", output->GetFile()->GetName(),fSpecialOutputLocation.Data());
964 TFile::Cp(output->GetFile()->GetName(),
965 Form("%s/%s", fSpecialOutputLocation.Data(), output->GetFile()->GetName()));
968 if (opwd) opwd->cd();
971 //Write statistics information on the client
972 WriteStatisticsMsg(fNcalls);
974 TDirectory *crtdir = gDirectory;
975 TFile f("syswatch.root", "RECREATE");
979 TTree *tree = AliSysInfo::MakeTree("syswatch.log");
980 tree->SetName("syswatch");
981 tree->SetMarkerStyle(kCircle);
982 tree->SetMarkerColor(kBlue);
983 tree->SetMarkerSize(0.5);
984 if (!gROOT->IsBatch()) {
985 tree->SetAlias("event", "id0");
986 tree->SetAlias("task", "id1");
987 tree->SetAlias("stage", "id2");
988 // Already defined aliases
989 // tree->SetAlias("deltaT","stampSec-stampOldSec");
990 // tree->SetAlias("T","stampSec-first");
991 // tree->SetAlias("deltaVM","(pI.fMemVirtual-pIOld.fMemVirtual)");
992 // tree->SetAlias("VM","pI.fMemVirtual");
993 TCanvas *canvas = new TCanvas("SysInfo","SysInfo",10,10,1200,800);
994 Int_t npads = 1 /*COO plot for all tasks*/ +
995 fTopTasks->GetEntries() /*Exec plot per task*/ +
996 1 /*Terminate plot for all tasks*/ +
999 Int_t iopt = (Int_t)TMath::Sqrt((Double_t)npads);
1000 if (npads<iopt*(iopt+1))
1001 canvas->Divide(iopt, iopt+1, 0.01, 0.01);
1003 canvas->Divide(iopt+1, iopt+1, 0.01, 0.01);
1005 // draw the plot of deltaVM for Exec for each task
1006 for (itask=0; itask<fTopTasks->GetEntriesFast(); itask++) {
1007 task = (AliAnalysisTask*)fTopTasks->At(itask);
1009 cut = Form("task==%d && stage==1", itask);
1010 tree->Draw("deltaVM:event",cut,"", 1234567890, 0);
1011 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
1013 hist->SetTitle(Form("%s: Exec dVM[kB]/event", task->GetName()));
1014 hist->GetYaxis()->SetTitle("deltaVM [kB]");
1017 // Draw the plot of deltaVM for CreateOutputObjects for all tasks
1019 tree->SetMarkerStyle(kFullTriangleUp);
1020 tree->SetMarkerColor(kRed);
1021 tree->SetMarkerSize(0.8);
1022 cut = "task>=0 && task<1000 && stage==0";
1023 tree->Draw("deltaVM:sname",cut,"", 1234567890, 0);
1024 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
1026 hist->SetTitle("Memory in CreateOutputObjects()");
1027 hist->GetYaxis()->SetTitle("deltaVM [kB]");
1028 hist->GetXaxis()->SetTitle("task");
1030 // draw the plot of deltaVM for Terminate for all tasks
1032 tree->SetMarkerStyle(kOpenSquare);
1033 tree->SetMarkerColor(kMagenta);
1034 cut = "task>=0 && task<1000 && stage==2";
1035 tree->Draw("deltaVM:sname",cut,"", 1234567890, 0);
1036 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
1038 hist->SetTitle("Memory in Terminate()");
1039 hist->GetYaxis()->SetTitle("deltaVM [kB]");
1040 hist->GetXaxis()->SetTitle("task");
1044 tree->SetMarkerStyle(kFullCircle);
1045 tree->SetMarkerColor(kGreen);
1046 cut = Form("task==%d && stage==1",fTopTasks->GetEntriesFast()-1);
1047 tree->Draw("VM:event",cut,"", 1234567890, 0);
1048 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
1050 hist->SetTitle("Virtual memory");
1051 hist->GetYaxis()->SetTitle("VM [kB]");
1055 tree->SetMarkerStyle(kCircle);
1056 tree->SetMarkerColor(kBlue);
1057 tree->SetMarkerSize(0.5);
1062 if (crtdir) crtdir->cd();
1064 // Validate the output files
1065 if (ValidateOutputFiles()) {
1067 out.open("outputs_valid", ios::out);
1071 if (fDebug > 1) printf("<-AliAnalysisManager::Terminate()\n");
1073 //______________________________________________________________________________
1074 void AliAnalysisManager::ProfileTask(Int_t itop, const char *option) const
1076 // Profiles the task having the itop index in the list of top (first level) tasks.
1077 AliAnalysisTask *task = (AliAnalysisTask*)fTopTasks->At(itop);
1079 Error("ProfileTask", "There are only %d top tasks in the manager", fTopTasks->GetEntries());
1082 ProfileTask(task->GetName(), option);
1085 //______________________________________________________________________________
1086 void AliAnalysisManager::ProfileTask(const char *name, const char */*option*/) const
1088 // Profile a managed task after the execution of the analysis in case NSysInfo
1090 if (gSystem->AccessPathName("syswatch.root")) {
1091 Error("ProfileTask", "No file syswatch.root found in the current directory");
1094 if (gROOT->IsBatch()) return;
1095 AliAnalysisTask *task = (AliAnalysisTask*)fTopTasks->FindObject(name);
1097 Error("ProfileTask", "No top task named %s known by the manager.", name);
1100 Int_t itop = fTopTasks->IndexOf(task);
1101 Int_t itask = fTasks->IndexOf(task);
1102 // Create canvas with 2 pads: first draw COO + Terminate, second Exec
1103 TDirectory *cdir = gDirectory;
1104 TFile f("syswatch.root");
1105 TTree *tree = (TTree*)f.Get("syswatch");
1107 Error("ProfileTask", "No tree named <syswatch> found in file syswatch.root");
1110 if (fDebug > 1) printf("=== Profiling task %s (class %s)\n", name, task->ClassName());
1111 TCanvas *canvas = new TCanvas(Form("profile_%d",itop),Form("Profile of task %s (class %s)",name,task->ClassName()),10,10,800,600);
1112 canvas->Divide(2, 2, 0.01, 0.01);
1116 // VM profile for COO and Terminate methods
1118 cut = Form("task==%d && (stage==0 || stage==2)",itask);
1119 tree->Draw("deltaVM:sname",cut,"", 1234567890, 0);
1120 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
1122 hist->SetTitle("Alocated VM[kB] for COO and Terminate");
1123 hist->GetYaxis()->SetTitle("deltaVM [kB]");
1124 hist->GetXaxis()->SetTitle("method");
1126 // CPU profile per event
1128 cut = Form("task==%d && stage==1",itop);
1129 tree->Draw("deltaT:event",cut,"", 1234567890, 0);
1130 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
1132 hist->SetTitle("Execution time per event");
1133 hist->GetYaxis()->SetTitle("CPU/event [s]");
1135 // VM profile for Exec
1137 cut = Form("task==%d && stage==1",itop);
1138 tree->Draw("deltaVM:event",cut,"", 1234567890, 0);
1139 hist = (TH1*)gPad->GetListOfPrimitives()->FindObject("htemp");
1141 hist->SetTitle("Alocated VM[kB] per event");
1142 hist->GetYaxis()->SetTitle("deltaVM [kB]");
1147 if (cdir) cdir->cd();
1150 //______________________________________________________________________________
1151 void AliAnalysisManager::AddTask(AliAnalysisTask *task)
1153 // Adds a user task to the global list of tasks.
1154 if (fTasks->FindObject(task)) {
1155 Warning("AddTask", "Task %s: the same object already added to the analysis manager. Not adding.", task->GetName());
1158 task->SetActive(kFALSE);
1162 //______________________________________________________________________________
1163 AliAnalysisTask *AliAnalysisManager::GetTask(const char *name) const
1165 // Retreive task by name.
1166 if (!fTasks) return NULL;
1167 return (AliAnalysisTask*)fTasks->FindObject(name);
1170 //______________________________________________________________________________
1171 AliAnalysisDataContainer *AliAnalysisManager::CreateContainer(const char *name,
1172 TClass *datatype, EAliAnalysisContType type, const char *filename)
1174 // Create a data container of a certain type. Types can be:
1175 // kExchangeContainer = 0, used to exchange data between tasks
1176 // kInputContainer = 1, used to store input data
1177 // kOutputContainer = 2, used for writing result to a file
1178 // filename: composed by file#folder (e.g. results.root#INCLUSIVE) - will write
1179 // the output object to a folder inside the output file
1180 if (fContainers->FindObject(name)) {
1181 Error("CreateContainer","A container named %s already defined !",name);
1184 AliAnalysisDataContainer *cont = new AliAnalysisDataContainer(name, datatype);
1185 fContainers->Add(cont);
1187 case kInputContainer:
1190 case kOutputContainer:
1191 fOutputs->Add(cont);
1192 if (filename && strlen(filename)) {
1193 cont->SetFileName(filename);
1194 cont->SetDataOwned(kFALSE); // data owned by the file
1197 case kParamContainer:
1198 fParamCont->Add(cont);
1199 if (filename && strlen(filename)) {
1200 cont->SetFileName(filename);
1201 cont->SetDataOwned(kFALSE); // data owned by the file
1204 case kExchangeContainer:
1210 //______________________________________________________________________________
1211 Bool_t AliAnalysisManager::ConnectInput(AliAnalysisTask *task, Int_t islot,
1212 AliAnalysisDataContainer *cont)
1214 // Connect input of an existing task to a data container.
1216 Error("ConnectInput", "Task pointer is NULL");
1219 if (!fTasks->FindObject(task)) {
1221 Info("ConnectInput", "Task %s was not registered. Now owned by analysis manager", task->GetName());
1223 Bool_t connected = task->ConnectInput(islot, cont);
1227 //______________________________________________________________________________
1228 Bool_t AliAnalysisManager::ConnectOutput(AliAnalysisTask *task, Int_t islot,
1229 AliAnalysisDataContainer *cont)
1231 // Connect output of an existing task to a data container.
1233 Error("ConnectOutput", "Task pointer is NULL");
1236 if (!fTasks->FindObject(task)) {
1238 Warning("ConnectOutput", "Task %s not registered. Now owned by analysis manager", task->GetName());
1240 Bool_t connected = task->ConnectOutput(islot, cont);
1244 //______________________________________________________________________________
1245 void AliAnalysisManager::CleanContainers()
1247 // Clean data from all containers that have already finished all client tasks.
1248 TIter next(fContainers);
1249 AliAnalysisDataContainer *cont;
1250 while ((cont=(AliAnalysisDataContainer *)next())) {
1251 if (cont->IsOwnedData() &&
1252 cont->IsDataReady() &&
1253 cont->ClientsExecuted()) cont->DeleteData();
1257 //______________________________________________________________________________
1258 Bool_t AliAnalysisManager::InitAnalysis()
1260 // Initialization of analysis chain of tasks. Should be called after all tasks
1261 // and data containers are properly connected
1262 // Reset flag and remove valid_outputs file if exists
1264 if (!gSystem->AccessPathName("outputs_valid"))
1265 gSystem->Unlink("outputs_valid");
1266 // Check for top tasks (depending only on input data containers)
1267 if (!fTasks->First()) {
1268 Error("InitAnalysis", "Analysis has no tasks !");
1272 AliAnalysisTask *task;
1273 AliAnalysisDataContainer *cont;
1276 Bool_t iszombie = kFALSE;
1277 Bool_t istop = kTRUE;
1279 while ((task=(AliAnalysisTask*)next())) {
1282 Int_t ninputs = task->GetNinputs();
1283 for (i=0; i<ninputs; i++) {
1284 cont = task->GetInputSlot(i)->GetContainer();
1288 fZombies->Add(task);
1292 Error("InitAnalysis", "Input slot %d of task %s has no container connected ! Declared zombie...",
1293 i, task->GetName());
1295 if (iszombie) continue;
1296 // Check if cont is an input container
1297 if (istop && !fInputs->FindObject(cont)) istop=kFALSE;
1298 // Connect to parent task
1302 fTopTasks->Add(task);
1306 Error("InitAnalysis", "No top task defined. At least one task should be connected only to input containers");
1309 // Check now if there are orphan tasks
1310 for (i=0; i<ntop; i++) {
1311 task = (AliAnalysisTask*)fTopTasks->At(i);
1316 while ((task=(AliAnalysisTask*)next())) {
1317 if (!task->IsUsed()) {
1319 Warning("InitAnalysis", "Task %s is orphan", task->GetName());
1322 // Check the task hierarchy (no parent task should depend on data provided
1323 // by a daughter task)
1324 for (i=0; i<ntop; i++) {
1325 task = (AliAnalysisTask*)fTopTasks->At(i);
1326 if (task->CheckCircularDeps()) {
1327 Error("InitAnalysis", "Found illegal circular dependencies between following tasks:");
1332 // Check that all containers feeding post-event loop tasks are in the outputs list
1333 TIter nextcont(fContainers); // loop over all containers
1334 while ((cont=(AliAnalysisDataContainer*)nextcont())) {
1335 if (!cont->IsPostEventLoop() && !fOutputs->FindObject(cont)) {
1336 if (cont->HasConsumers()) {
1337 // Check if one of the consumers is post event loop
1338 TIter nextconsumer(cont->GetConsumers());
1339 while ((task=(AliAnalysisTask*)nextconsumer())) {
1340 if (task->IsPostEventLoop()) {
1341 fOutputs->Add(cont);
1348 // Check if all special output containers have a file name provided
1349 TIter nextout(fOutputs);
1350 while ((cont=(AliAnalysisDataContainer*)nextout())) {
1351 if (cont->IsSpecialOutput() && !strlen(cont->GetFileName())) {
1352 Error("InitAnalysis", "Wrong container %s : a file name MUST be provided for special outputs", cont->GetName());
1356 // Initialize requested branch list if needed
1357 if (!fAutoBranchHandling) {
1359 while ((task=(AliAnalysisTask*)next())) {
1360 if (!task->HasBranches()) {
1361 Error("InitAnalysis", "Manual branch loading requested but task %s of type %s does not define branches.\nUse: fBranchNames = \"ESD:br1,br2,...,brN AOD:bra1,bra2,...,braM\"",
1362 task->GetName(), task->ClassName());
1365 if (!fInputEventHandler || !strlen(fInputEventHandler->GetDataType())) {
1366 Error("InitAnalysis", "Manual branch loading requested but no input handler defined or handler does not define data type.");
1369 TString taskbranches;
1370 task->GetBranches(fInputEventHandler->GetDataType(), taskbranches);
1371 if (taskbranches.IsNull()) {
1372 Error("InitAnalysis", "Manual branch loading requested but task %s of type %s does not define branches of type %s:",
1373 task->GetName(), task->ClassName(), fInputEventHandler->GetDataType());
1376 AddBranches(taskbranches);
1383 //______________________________________________________________________________
1384 void AliAnalysisManager::AddBranches(const char *branches)
1386 // Add branches to the existing fRequestedBranches.
1387 TString br(branches);
1388 TObjArray *arr = br.Tokenize(",");
1391 while ((obj=next())) {
1392 if (!fRequestedBranches.Contains(obj->GetName())) {
1393 if (!fRequestedBranches.IsNull()) fRequestedBranches += ",";
1394 fRequestedBranches += obj->GetName();
1397 if (arr) delete arr;
1400 //______________________________________________________________________________
1401 void AliAnalysisManager::CheckBranches(Bool_t load)
1403 // The method checks the input branches to be loaded during the analysis.
1404 if (fAutoBranchHandling || fRequestedBranches.IsNull() || !fTree) return;
1405 TObjArray *arr = fRequestedBranches.Tokenize(",");
1408 while ((obj=next())) {
1409 TBranch *br = dynamic_cast<TBranch*>(fTable.FindObject(obj->GetName()));
1411 br = fTree->GetBranch(obj->GetName());
1413 Error("CheckBranches", "Could not find branch %s",obj->GetName());
1418 if (load && br->GetReadEntry()!=GetCurrentEntry()) br->GetEntry(GetCurrentEntry());
1422 //______________________________________________________________________________
1423 void AliAnalysisManager::PrintStatus(Option_t *option) const
1425 // Print task hierarchy.
1427 Info("PrintStatus", "Analysis manager %s not initialized : call InitAnalysis() first", GetName());
1430 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
1432 Info("PrintStatus", "System information will be collected each %lld events", fNSysInfo);
1433 TIter next(fTopTasks);
1434 AliAnalysisTask *task;
1435 while ((task=(AliAnalysisTask*)next()))
1436 task->PrintTask(option);
1437 if (!fAutoBranchHandling && !fRequestedBranches.IsNull())
1438 printf("Requested input branches:\n%s\n", fRequestedBranches.Data());
1441 //______________________________________________________________________________
1442 void AliAnalysisManager::ResetAnalysis()
1444 // Reset all execution flags and clean containers.
1448 //______________________________________________________________________________
1449 Long64_t AliAnalysisManager::StartAnalysis(const char *type, Long64_t nentries, Long64_t firstentry)
1451 // Start analysis having a grid handler.
1452 if (!fGridHandler) {
1453 Error("StartAnalysis", "Cannot start analysis providing just the analysis type without a grid handler.");
1454 Info("===", "Add an AliAnalysisAlien object as plugin for this manager and configure it.");
1458 return StartAnalysis(type, tree, nentries, firstentry);
1461 //______________________________________________________________________________
1462 Long64_t AliAnalysisManager::StartAnalysis(const char *type, TTree * const tree, Long64_t nentries, Long64_t firstentry)
1464 // Start analysis for this manager. Analysis task can be: LOCAL, PROOF, GRID or
1465 // MIX. Process nentries starting from firstentry
1467 // Backup current directory and make sure gDirectory points to gROOT
1468 TDirectory *cdir = gDirectory;
1471 Error("StartAnalysis","Analysis manager was not initialized !");
1475 if (fDebug > 1) printf("StartAnalysis %s\n",GetName());
1477 TString anaType = type;
1479 fMode = kLocalAnalysis;
1480 Bool_t runlocalinit = kTRUE;
1481 if (anaType.Contains("file")) {
1482 runlocalinit = kFALSE;
1485 if (anaType.Contains("proof")) fMode = kProofAnalysis;
1486 else if (anaType.Contains("grid")) fMode = kGridAnalysis;
1487 else if (anaType.Contains("mix")) fMode = kMixingAnalysis;
1489 if (fMode == kGridAnalysis) {
1491 if (!anaType.Contains("terminate")) {
1492 if (!fGridHandler) {
1493 Error("StartAnalysis", "Cannot start grid analysis without a grid handler.");
1494 Info("===", "Add an AliAnalysisAlien object as plugin for this manager and configure it.");
1498 // Write analysis manager in the analysis file
1499 cout << "===== RUNNING GRID ANALYSIS: " << GetName() << endl;
1500 // run local task configuration
1501 TIter nextTask(fTasks);
1502 AliAnalysisTask *task;
1503 while ((task=(AliAnalysisTask*)nextTask())) {
1507 if (!fGridHandler->StartAnalysis(nentries, firstentry)) {
1508 Info("StartAnalysis", "Grid analysis was stopped and cannot be terminated");
1513 // Terminate grid analysis
1514 if (fSelector && fSelector->GetStatus() == -1) {cdir->cd(); return -1;}
1515 if (fGridHandler->GetRunMode() == AliAnalysisGrid::kOffline) {cdir->cd(); return 0;}
1516 cout << "===== MERGING OUTPUTS REGISTERED BY YOUR ANALYSIS JOB: " << GetName() << endl;
1517 if (!fGridHandler->MergeOutputs()) {
1518 // Return if outputs could not be merged or if it alien handler
1519 // was configured for offline mode or local testing.
1524 cout << "===== TERMINATING GRID ANALYSIS JOB: " << GetName() << endl;
1525 ImportWrappers(NULL);
1531 SetEventLoop(kFALSE);
1532 // Enable event loop mode if a tree was provided
1533 if (tree || fGridHandler || fMode==kMixingAnalysis) SetEventLoop(kTRUE);
1536 TString ttype = "TTree";
1537 if (tree && tree->IsA() == TChain::Class()) {
1538 chain = (TChain*)tree;
1539 if (!chain || !chain->GetListOfFiles()->First()) {
1540 Error("StartAnalysis", "Cannot process null or empty chain...");
1547 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
1548 if (getsysInfo) AliSysInfo::AddStamp("Start", 0);
1549 // Initialize locally all tasks (happens for all modes)
1551 AliAnalysisTask *task;
1553 while ((task=(AliAnalysisTask*)next())) {
1557 if (getsysInfo) AliSysInfo::AddStamp("LocalInit_all", 0);
1561 case kLocalAnalysis:
1562 if (!tree && !fGridHandler) {
1563 TIter nextT(fTasks);
1564 // Call CreateOutputObjects for all tasks
1566 Bool_t dirStatus = TH1::AddDirectoryStatus();
1567 while ((task=(AliAnalysisTask*)nextT())) {
1568 TH1::AddDirectory(kFALSE);
1569 task->CreateOutputObjects();
1570 if (getsysInfo) AliSysInfo::AddStamp(Form("%s_CREATEOUTOBJ",task->ClassName()), 0, itask, 0);
1574 TH1::AddDirectory(dirStatus);
1575 if (IsExternalLoop()) {
1576 Info("StartAnalysis", "Initialization done. Event loop is controlled externally.\
1577 \nSetData for top container, call ExecAnalysis in a loop and then Terminate manually");
1584 fSelector = new AliAnalysisSelector(this);
1585 // Check if a plugin handler is used
1587 // Get the chain from the plugin
1588 TString dataType = "esdTree";
1589 if (fInputEventHandler) {
1590 dataType = fInputEventHandler->GetDataType();
1594 chain = fGridHandler->GetChainForTestMode(dataType);
1596 Error("StartAnalysis", "No chain for test mode. Aborting.");
1599 cout << "===== RUNNING LOCAL ANALYSIS" << GetName() << " ON CHAIN " << chain->GetName() << endl;
1600 retv = chain->Process(fSelector, "", nentries, firstentry);
1603 // Run tree-based analysis via AliAnalysisSelector
1604 cout << "===== RUNNING LOCAL ANALYSIS " << GetName() << " ON TREE " << tree->GetName() << endl;
1605 retv = tree->Process(fSelector, "", nentries, firstentry);
1607 case kProofAnalysis:
1609 // Check if the plugin is used
1611 return StartAnalysis(type, fGridHandler->GetProofDataSet(), nentries, firstentry);
1613 if (!gROOT->GetListOfProofs() || !gROOT->GetListOfProofs()->GetEntries()) {
1614 Error("StartAnalysis", "No PROOF!!! Exiting.");
1618 line = Form("gProof->AddInput((TObject*)0x%lx);", (ULong_t)this);
1619 gROOT->ProcessLine(line);
1622 cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON CHAIN " << chain->GetName() << endl;
1623 retv = chain->Process("AliAnalysisSelector", "", nentries, firstentry);
1625 Error("StartAnalysis", "No chain!!! Exiting.");
1632 if (!anaType.Contains("terminate")) {
1633 if (!fGridHandler) {
1634 Error("StartAnalysis", "Cannot start grid analysis without a grid handler.");
1635 Info("===", "Add an AliAnalysisAlien object as plugin for this manager and configure it.");
1639 // Write analysis manager in the analysis file
1640 cout << "===== RUNNING GRID ANALYSIS: " << GetName() << endl;
1641 // Start the analysis via the handler
1642 if (!fGridHandler->StartAnalysis(nentries, firstentry)) {
1643 Info("StartAnalysis", "Grid analysis was stopped and cannot be terminated");
1648 // Terminate grid analysis
1649 if (fSelector && fSelector->GetStatus() == -1) {cdir->cd(); return -1;}
1650 if (fGridHandler->GetRunMode() == AliAnalysisGrid::kOffline) {cdir->cd(); return 0;}
1651 cout << "===== MERGING OUTPUTS REGISTERED BY YOUR ANALYSIS JOB: " << GetName() << endl;
1652 if (!fGridHandler->MergeOutputs()) {
1653 // Return if outputs could not be merged or if it alien handler
1654 // was configured for offline mode or local testing.
1659 cout << "===== TERMINATING GRID ANALYSIS JOB: " << GetName() << endl;
1660 ImportWrappers(NULL);
1664 case kMixingAnalysis:
1665 // Run event mixing analysis
1667 Error("StartAnalysis", "Cannot run event mixing without event pool");
1671 cout << "===== RUNNING EVENT MIXING ANALYSIS " << GetName() << endl;
1672 fSelector = new AliAnalysisSelector(this);
1673 while ((chain=fEventPool->GetNextChain())) {
1675 // Call NotifyBinChange for all tasks
1676 while ((task=(AliAnalysisTask*)next()))
1677 if (!task->IsPostEventLoop()) task->NotifyBinChange();
1678 retv = chain->Process(fSelector);
1680 Error("StartAnalysis", "Mixing analysis failed");
1685 PackOutput(fSelector->GetOutputList());
1692 //______________________________________________________________________________
1693 Long64_t AliAnalysisManager::StartAnalysis(const char *type, const char *dataset, Long64_t nentries, Long64_t firstentry)
1695 // Start analysis for this manager on a given dataset. Analysis task can be:
1696 // LOCAL, PROOF or GRID. Process nentries starting from firstentry.
1698 Error("StartAnalysis","Analysis manager was not initialized !");
1702 if (fDebug > 1) printf("StartAnalysis %s\n",GetName());
1703 TString anaType = type;
1705 if (!anaType.Contains("proof")) {
1706 Error("StartAnalysis", "Cannot process datasets in %s mode. Try PROOF.", type);
1709 fMode = kProofAnalysis;
1711 SetEventLoop(kTRUE);
1712 // Set the dataset flag
1713 TObject::SetBit(kUseDataSet);
1717 // Start proof analysis using the grid handler
1718 if (!fGridHandler->StartAnalysis(nentries, firstentry)) {
1719 Error("StartAnalysis", "The grid plugin could not start PROOF analysis");
1722 // Check if the plugin is in test mode
1723 if (fGridHandler->GetRunMode() == AliAnalysisGrid::kTest) {
1724 dataset = "test_collection";
1726 dataset = fGridHandler->GetProofDataSet();
1730 if (!gROOT->GetListOfProofs() || !gROOT->GetListOfProofs()->GetEntries()) {
1731 Error("StartAnalysis", "No PROOF!!! Exiting.");
1735 // Initialize locally all tasks
1737 AliAnalysisTask *task;
1738 while ((task=(AliAnalysisTask*)next())) {
1742 line = Form("gProof->AddInput((TObject*)0x%lx);", (ULong_t)this);
1743 gROOT->ProcessLine(line);
1746 // chain->SetProof();
1747 cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON TEST CHAIN " << chain->GetName() << endl;
1748 retv = chain->Process("AliAnalysisSelector", "", nentries, firstentry);
1750 line = Form("gProof->Process(\"%s\", \"AliAnalysisSelector\", \"\", %lld, %lld);",
1751 dataset, nentries, firstentry);
1752 cout << "===== RUNNING PROOF ANALYSIS " << GetName() << " ON DATASET " << dataset << endl;
1753 retv = (Long_t)gROOT->ProcessLine(line);
1758 //______________________________________________________________________________
1759 TFile *AliAnalysisManager::OpenFile(AliAnalysisDataContainer *cont, const char *option, Bool_t ignoreProof)
1761 // Opens according the option the file specified by cont->GetFileName() and changes
1762 // current directory to cont->GetFolderName(). If the file was already opened, it
1763 // checks if the option UPDATE was preserved. File open via TProofOutputFile can
1764 // be optionally ignored.
1765 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
1766 TString filename = cont->GetFileName();
1768 if (filename.IsNull()) {
1769 ::Error("AliAnalysisManager::OpenFile", "No file name specified for container %s", cont->GetName());
1772 if (mgr->GetAnalysisType()==AliAnalysisManager::kProofAnalysis && cont->IsSpecialOutput()
1774 f = mgr->OpenProofFile(cont,option);
1776 // Check first if the file is already opened
1777 f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
1779 // Check if option "UPDATE" was preserved
1780 TString opt(option);
1782 if ((opt=="UPDATE") && (opt!=f->GetOption()))
1783 ::Info("AliAnalysisManager::OpenFile", "File %s already opened in %s mode!", cont->GetFileName(), f->GetOption());
1785 f = TFile::Open(filename, option);
1788 if (f && !f->IsZombie() && !f->TestBit(TFile::kRecovered)) {
1792 // Check for a folder request
1793 TString dir = cont->GetFolderName();
1794 if (!dir.IsNull()) {
1795 if (!f->GetDirectory(dir)) f->mkdir(dir);
1800 ::Fatal("AliAnalysisManager::OpenFile", "File %s could not be opened", filename.Data());
1801 cont->SetFile(NULL);
1805 //______________________________________________________________________________
1806 TFile *AliAnalysisManager::OpenProofFile(AliAnalysisDataContainer *cont, const char *option, const char *extaod)
1808 // Opens a special output file used in PROOF.
1810 TString filename = cont->GetFileName();
1811 if (cont == fCommonOutput) {
1812 if (fOutputEventHandler) {
1813 if (strlen(extaod)) filename = extaod;
1814 filename = fOutputEventHandler->GetOutputFileName();
1816 else Fatal("OpenProofFile","No output container. Exiting.");
1819 if (fMode!=kProofAnalysis || !fSelector) {
1820 Fatal("OpenProofFile","Cannot open PROOF file %s: no PROOF or selector",filename.Data());
1823 if (fSpecialOutputLocation.Length()) {
1824 f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
1826 // Check if option "UPDATE" was preserved
1827 TString opt(option);
1829 if ((opt=="UPDATE") && (opt!=f->GetOption()))
1830 ::Info("OpenProofFile", "File %s already opened in %s mode!", cont->GetFileName(), f->GetOption());
1832 f = new TFile(filename, option);
1834 if (f && !f->IsZombie() && !f->TestBit(TFile::kRecovered)) {
1838 // Check for a folder request
1839 TString dir = cont->GetFolderName();
1841 if (!f->GetDirectory(dir)) f->mkdir(dir);
1846 Fatal("OpenProofFile", "File %s could not be opened", cont->GetFileName());
1847 cont->SetFile(NULL);
1850 // Check if there is already a proof output file in the output list
1851 TObject *pof = fSelector->GetOutputList()->FindObject(filename);
1853 // Get the actual file
1854 line = Form("((TProofOutputFile*)0x%lx)->GetFileName();", (ULong_t)pof);
1855 filename = (const char*)gROOT->ProcessLine(line);
1857 printf("File: %s already booked via TProofOutputFile\n", filename.Data());
1859 f = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
1861 Fatal("OpenProofFile", "Proof output file found but no file opened for %s", filename.Data());
1864 // Check if option "UPDATE" was preserved
1865 TString opt(option);
1867 if ((opt=="UPDATE") && (opt!=f->GetOption()))
1868 Fatal("OpenProofFile", "File %s already opened, but not in UPDATE mode!", cont->GetFileName());
1870 if (cont->IsRegisterDataset()) {
1871 TString dsetName = filename;
1872 dsetName.ReplaceAll(".root", cont->GetTitle());
1873 dsetName.ReplaceAll(":","_");
1874 if (fDebug>1) printf("Booking dataset: %s\n", dsetName.Data());
1875 line = Form("TProofOutputFile *pf = new TProofOutputFile(\"%s\", \"DROV\", \"%s\");", filename.Data(), dsetName.Data());
1877 if (fDebug>1) printf("Booking TProofOutputFile: %s to be merged\n", filename.Data());
1878 line = Form("TProofOutputFile *pf = new TProofOutputFile(\"%s\");", filename.Data());
1880 if (fDebug > 1) printf("=== %s\n", line.Data());
1881 gROOT->ProcessLine(line);
1882 line = Form("pf->OpenFile(\"%s\");", option);
1883 gROOT->ProcessLine(line);
1886 gROOT->ProcessLine("pf->Print()");
1887 printf(" == proof file name: %s", f->GetName());
1889 // Add to proof output list
1890 line = Form("((TList*)0x%lx)->Add(pf);",(ULong_t)fSelector->GetOutputList());
1891 if (fDebug > 1) printf("=== %s\n", line.Data());
1892 gROOT->ProcessLine(line);
1894 if (f && !f->IsZombie() && !f->TestBit(TFile::kRecovered)) {
1898 // Check for a folder request
1899 TString dir = cont->GetFolderName();
1900 if (!dir.IsNull()) {
1901 if (!f->GetDirectory(dir)) f->mkdir(dir);
1906 Fatal("OpenProofFile", "File %s could not be opened", cont->GetFileName());
1907 cont->SetFile(NULL);
1911 //______________________________________________________________________________
1912 void AliAnalysisManager::ExecAnalysis(Option_t *option)
1914 // Execute analysis.
1915 static Long64_t nentries = 0;
1916 static TTree *lastTree = 0;
1917 static TStopwatch *timer = new TStopwatch();
1918 if (fDebug > 0) printf("MGR: Processing event #%d\n", fNcalls);
1920 if (fTree && (fTree != lastTree)) {
1921 nentries += fTree->GetEntries();
1924 if (!fNcalls) timer->Start();
1925 if (!fIsRemote && TObject::TestBit(kUseProgressBar)) ProgressBar("Processing event", fNcalls, nentries, timer, kFALSE);
1928 TDirectory *cdir = gDirectory;
1929 Bool_t getsysInfo = ((fNSysInfo>0) && (fMode==kLocalAnalysis))?kTRUE:kFALSE;
1930 if (getsysInfo && ((fNcalls%fNSysInfo)==0)) AliSysInfo::AddStamp("Exec_start", (Int_t)fNcalls);
1932 Error("ExecAnalysis", "Analysis manager was not initialized !");
1937 AliAnalysisTask *task;
1938 // Check if the top tree is active.
1940 if (getsysInfo && ((fNcalls%fNSysInfo)==0))
1941 AliSysInfo::AddStamp("Handlers_BeginEventGroup",fNcalls, 1002, 0);
1943 // De-activate all tasks
1944 while ((task=(AliAnalysisTask*)next())) task->SetActive(kFALSE);
1945 AliAnalysisDataContainer *cont = fCommonInput;
1946 if (!cont) cont = (AliAnalysisDataContainer*)fInputs->At(0);
1948 Error("ExecAnalysis","Cannot execute analysis in TSelector mode without at least one top container");
1952 cont->SetData(fTree); // This will notify all consumers
1953 Long64_t entry = fTree->GetTree()->GetReadEntry();
1955 // Call BeginEvent() for optional input/output and MC services
1956 if (fInputEventHandler) fInputEventHandler ->BeginEvent(entry);
1957 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent(entry);
1958 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(entry);
1960 if (getsysInfo && ((fNcalls%fNSysInfo)==0))
1961 AliSysInfo::AddStamp("Handlers_BeginEvent",fNcalls, 1000, 0);
1963 // Execute the tasks
1964 // TIter next1(cont->GetConsumers());
1965 TIter next1(fTopTasks);
1967 while ((task=(AliAnalysisTask*)next1())) {
1969 cout << " Executing task " << task->GetName() << endl;
1971 task->ExecuteTask(option);
1973 if (getsysInfo && ((fNcalls%fNSysInfo)==0))
1974 AliSysInfo::AddStamp(task->ClassName(), fNcalls, itask, 1);
1978 // Call FinishEvent() for optional output and MC services
1979 if (fInputEventHandler) fInputEventHandler ->FinishEvent();
1980 if (fOutputEventHandler) fOutputEventHandler ->FinishEvent();
1981 if (fMCtruthEventHandler) fMCtruthEventHandler->FinishEvent();
1982 // Gather system information if requested
1983 if (getsysInfo && ((fNcalls%fNSysInfo)==0))
1984 AliSysInfo::AddStamp("Handlers_FinishEvent",fNcalls, 1001, 1);
1988 // The event loop is not controlled by TSelector
1990 // Call BeginEvent() for optional input/output and MC services
1991 if (fInputEventHandler) fInputEventHandler ->BeginEvent(-1);
1992 if (fOutputEventHandler) fOutputEventHandler ->BeginEvent(-1);
1993 if (fMCtruthEventHandler) fMCtruthEventHandler->BeginEvent(-1);
1995 if (getsysInfo && ((fNcalls%fNSysInfo)==0))
1996 AliSysInfo::AddStamp("Handlers_BeginEvent",fNcalls, 1000, 0);
1997 TIter next2(fTopTasks);
1998 while ((task=(AliAnalysisTask*)next2())) {
1999 task->SetActive(kTRUE);
2001 cout << " Executing task " << task->GetName() << endl;
2003 task->ExecuteTask(option);
2007 // Call FinishEvent() for optional output and MC services
2008 if (fInputEventHandler) fInputEventHandler ->FinishEvent();
2009 if (fOutputEventHandler) fOutputEventHandler ->FinishEvent();
2010 if (fMCtruthEventHandler) fMCtruthEventHandler->FinishEvent();
2011 if (getsysInfo && ((fNcalls%fNSysInfo)==0))
2012 AliSysInfo::AddStamp("Handlers_FinishEvent",fNcalls, 1000, 1);
2016 //______________________________________________________________________________
2017 void AliAnalysisManager::SetInputEventHandler(AliVEventHandler* const handler)
2019 // Set the input event handler and create a container for it.
2020 fInputEventHandler = handler;
2021 fCommonInput = CreateContainer("cAUTO_INPUT", TChain::Class(), AliAnalysisManager::kInputContainer);
2022 // Warning("SetInputEventHandler", " An automatic input container for the input chain was created.\nPlease use: mgr->GetCommonInputContainer() to access it.");
2025 //______________________________________________________________________________
2026 void AliAnalysisManager::SetOutputEventHandler(AliVEventHandler* const handler)
2028 // Set the input event handler and create a container for it.
2029 fOutputEventHandler = handler;
2030 fCommonOutput = CreateContainer("cAUTO_OUTPUT", TTree::Class(), AliAnalysisManager::kOutputContainer, "default");
2031 fCommonOutput->SetSpecialOutput();
2032 // Warning("SetOutputEventHandler", " An automatic output container for the output tree was created.\nPlease use: mgr->GetCommonOutputContainer() to access it.");
2035 //______________________________________________________________________________
2036 void AliAnalysisManager::RegisterExtraFile(const char *fname)
2038 // This method is used externally to register output files which are not
2039 // connected to any output container, so that the manager can properly register,
2040 // retrieve or merge them when running in distributed mode. The file names are
2041 // separated by blancs. The method has to be called in MyAnalysisTask::LocalInit().
2042 if (fExtraFiles.Contains(fname)) return;
2043 if (fExtraFiles.Length()) fExtraFiles += " ";
2044 fExtraFiles += fname;
2047 //______________________________________________________________________________
2048 Bool_t AliAnalysisManager::GetFileFromWrapper(const char *filename, const TList *source)
2050 // Copy a file from the location specified ina the wrapper with the same name from the source list.
2053 TObject *pof = source->FindObject(filename);
2054 if (!pof || !pof->InheritsFrom("TProofOutputFile")) {
2055 Error("GetFileFromWrapper", "TProofOutputFile object not found in output list for file %s", filename);
2058 gROOT->ProcessLine(Form("sprintf((char*)0x%lx, \"%%s\", ((TProofOutputFile*)0x%lx)->GetOutputFileName();)", (ULong_t)fullPath, (ULong_t)pof));
2059 gROOT->ProcessLine(Form("sprintf((char*)0x%lx, \"%%s\", gProof->GetUrl();)", (ULong_t)chUrl));
2060 TString clientUrl(chUrl);
2061 TString fullPath_str(fullPath);
2062 if (clientUrl.Contains("localhost")){
2063 TObjArray* array = fullPath_str.Tokenize ( "//" );
2064 TObjString *strobj = ( TObjString *)array->At(1);
2065 TObjArray* arrayPort = strobj->GetString().Tokenize ( ":" );
2066 TObjString *strobjPort = ( TObjString *) arrayPort->At(1);
2067 fullPath_str.ReplaceAll(strobj->GetString().Data(),"localhost:PORT");
2068 fullPath_str.ReplaceAll(":PORT",Form(":%s",strobjPort->GetString().Data()));
2069 if (fDebug > 1) Info("GetFileFromWrapper","Using tunnel from %s to %s",fullPath_str.Data(),filename);
2074 Info("GetFileFromWrapper","Copying file %s from PROOF scratch space", fullPath_str.Data());
2075 Bool_t gotit = TFile::Cp(fullPath_str.Data(), filename);
2077 Error("GetFileFromWrapper", "Could not get file %s from proof scratch space", filename);
2081 //______________________________________________________________________________
2082 void AliAnalysisManager::GetAnalysisTypeString(TString &type) const
2084 // Fill analysis type in the provided string.
2086 case kLocalAnalysis:
2089 case kProofAnalysis:
2095 case kMixingAnalysis:
2100 //______________________________________________________________________________
2101 Bool_t AliAnalysisManager::ValidateOutputFiles() const
2103 // Validate all output files.
2104 TIter next(fOutputs);
2105 AliAnalysisDataContainer *output;
2106 TDirectory *cdir = gDirectory;
2107 TString openedFiles;
2108 while ((output=(AliAnalysisDataContainer*)next())) {
2109 if (output->IsRegisterDataset()) continue;
2110 TString filename = output->GetFileName();
2111 if (filename == "default") {
2112 if (!fOutputEventHandler) continue;
2113 filename = fOutputEventHandler->GetOutputFileName();
2114 // Main AOD may not be there
2115 if (gSystem->AccessPathName(filename)) continue;
2117 // Check if the file is closed
2118 if (openedFiles.Contains(filename)) continue;;
2119 TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(filename);
2121 Warning("ValidateOutputs", "File %s was not closed. Closing.", filename.Data());
2122 // Clear file list to release object ownership to user.
2126 file = TFile::Open(filename);
2127 if (!file || file->IsZombie() || file->TestBit(TFile::kRecovered)) {
2128 Error("ValidateOutputs", "Output file <%s> was not created or invalid", filename.Data());
2133 openedFiles += filename;
2140 //______________________________________________________________________________
2141 void AliAnalysisManager::ProgressBar(const char *opname, Long64_t current, Long64_t size, TStopwatch * const watch, Bool_t last, Bool_t refresh)
2143 // Implements a nice text mode progress bar.
2144 static Long64_t icount = 0;
2145 static TString oname;
2146 static TString nname;
2147 static Long64_t ocurrent = 0;
2148 static Long64_t osize = 0;
2149 static Int_t oseconds = 0;
2150 static TStopwatch *owatch = 0;
2151 static Bool_t oneoftwo = kFALSE;
2152 static Int_t nrefresh = 0;
2153 static Int_t nchecks = 0;
2154 const char symbol[4] = {'=','\\','|','/'};
2155 char progress[11] = " ";
2156 Int_t ichar = icount%4;
2163 ocurrent = TMath::Abs(current);
2164 osize = TMath::Abs(size);
2165 if (ocurrent > osize) ocurrent=osize;
2175 if (owatch && !last) {
2177 time = owatch->RealTime();
2178 hours = (Int_t)(time/3600.);
2180 minutes = (Int_t)(time/60.);
2182 seconds = (Int_t)time;
2184 if (oseconds==seconds) {
2188 oneoftwo = !oneoftwo;
2192 if (refresh && oneoftwo) {
2194 if (nchecks <= 0) nchecks = nrefresh+1;
2195 Int_t pctdone = (Int_t)(100.*nrefresh/nchecks);
2196 oname = Form(" == %d%% ==", pctdone);
2198 Double_t percent = 100.0*ocurrent/osize;
2199 Int_t nchar = Int_t(percent/10);
2200 if (nchar>10) nchar=10;
2202 for (i=0; i<nchar; i++) progress[i] = '=';
2203 progress[nchar] = symbol[ichar];
2204 for (i=nchar+1; i<10; i++) progress[i] = ' ';
2205 progress[10] = '\0';
2208 if(size<10000) fprintf(stderr, "%s [%10s] %4lld ", oname.Data(), progress, ocurrent);
2209 else if(size<100000) fprintf(stderr, "%s [%10s] %5lld ",oname.Data(), progress, ocurrent);
2210 else fprintf(stderr, "%s [%10s] %7lld ",oname.Data(), progress, ocurrent);
2211 if (time>0.) fprintf(stderr, "[%6.2f %%] TIME %.2d:%.2d:%.2d \r", percent, hours, minutes, seconds);
2212 else fprintf(stderr, "[%6.2f %%]\r", percent);
2213 if (refresh && oneoftwo) oname = nname;
2214 if (owatch) owatch->Continue();
2223 fprintf(stderr, "\n");
2227 //______________________________________________________________________________
2228 void AliAnalysisManager::DoLoadBranch(const char *name)
2230 // Get tree and load branch if needed.
2232 if (fAutoBranchHandling || !fTree)
2235 TBranch *br = dynamic_cast<TBranch*>(fTable.FindObject(name));
2237 br = fTree->GetBranch(name);
2239 Error("DoLoadBranch", "Could not find branch %s",name);
2244 if (br->GetReadEntry()==GetCurrentEntry()) return;
2245 br->GetEntry(GetCurrentEntry());
2248 //______________________________________________________________________________
2249 void AliAnalysisManager::AddStatisticsMsg(const char *line)
2251 // Add a line in the statistics message. If available, the statistics message is written
2252 // at the end of the SlaveTerminate phase on workers AND at the end of Terminate
2254 if (!strlen(line)) return;
2255 if (!fStatisticsMsg.IsNull()) fStatisticsMsg += "\n";
2256 fStatisticsMsg += line;
2259 //______________________________________________________________________________
2260 void AliAnalysisManager::WriteStatisticsMsg(Int_t nevents)
2262 // Write the statistics message in a file named <nevents.stat>
2263 if (!nevents) return;
2265 out.open(Form("%09d.stat", nevents), ios::out);
2266 if (!fStatisticsMsg.IsNull()) out << fStatisticsMsg << endl;