+//______________________________________________________________________________
+void AliAnalysisAlien::AddAdditionalLibrary(const char *name)
+{
+// Add a single additional library to be loaded. Extension must be present.
+ TString lib(name);
+ if (!lib.Contains(".")) {
+ Error("AddAdditionalLibrary", "Extension not defined for %s", name);
+ return;
+ }
+ if (fAdditionalLibs.Contains(name)) {
+ Warning("AddAdditionalLibrary", "Library %s already added.", name);
+ return;
+ }
+ if (!fAdditionalLibs.IsNull()) fAdditionalLibs += " ";
+ fAdditionalLibs += lib;
+}
+
+//______________________________________________________________________________
+void AliAnalysisAlien::AddModule(AliAnalysisTaskCfg *module)
+{
+// Adding a module. Checks if already existing. Becomes owned by this.
+ if (!module) return;
+ if (GetModule(module->GetName())) {
+ Error("AddModule", "A module having the same name %s already added", module->GetName());
+ return;
+ }
+ if (!fModules) {
+ fModules = new TObjArray();
+ fModules->SetOwner();
+ }
+ fModules->Add(module);
+}
+
+//______________________________________________________________________________
+void AliAnalysisAlien::AddModules(TObjArray *list)
+{
+// Adding a list of modules. Checks if already existing. Becomes owned by this.
+ TIter next(list);
+ AliAnalysisTaskCfg *module;
+ while ((module = (AliAnalysisTaskCfg*)next())) AddModule(module);
+}
+
+//______________________________________________________________________________
+Bool_t AliAnalysisAlien::CheckDependencies()
+{
+// Check if all dependencies are satisfied. Reorder modules if needed.
+ Int_t nmodules = GetNmodules();
+ if (!nmodules) {
+ Warning("CheckDependencies", "No modules added yet to check their dependencies");
+ return kTRUE;
+ }
+ AliAnalysisTaskCfg *mod = 0;
+ AliAnalysisTaskCfg *dep = 0;
+ TString depname;
+ Int_t i, j, k;
+ for (i=0; i<nmodules; i++) {
+ mod = (AliAnalysisTaskCfg*) fModules->At(i);
+ Int_t ndeps = mod->GetNdeps();
+ Int_t istart = i;
+ for (j=0; j<ndeps; j++) {
+ depname = mod->GetDependency(j);
+ dep = GetModule(depname);
+ if (!dep) {
+ Error("CheckDependencies","Dependency %s not added for module %s",
+ depname.Data(), mod->GetName());
+ return kFALSE;
+ }
+ if (dep->NeedsDependency(mod->GetName())) {
+ Error("CheckDependencies","Modules %s and %s circularly depend on each other",
+ mod->GetName(), dep->GetName());
+ return kFALSE;
+ }
+ Int_t idep = fModules->IndexOf(dep);
+ // The dependency task must come first
+ if (idep>i) {
+ // Remove at idep and move all objects below up one slot
+ // down to index i included.
+ fModules->RemoveAt(idep);
+ for (k=idep-1; k>=i; k--) fModules->AddAt(fModules->RemoveAt(k),k+1);
+ fModules->AddAt(dep, i++);
+ }
+ //Redo from istart if dependencies were inserted
+ if (i>istart) i=istart-1;
+ }
+ }
+ return kTRUE;
+}
+
+//______________________________________________________________________________
+AliAnalysisManager *AliAnalysisAlien::CreateAnalysisManager(const char *name, const char *filename)
+{
+// Create the analysis manager and optionally execute the macro in filename.
+ AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
+ if (mgr) return mgr;
+ mgr = new AliAnalysisManager(name);
+ mgr->SetGridHandler((AliAnalysisGrid*)this);
+ if (strlen(filename)) {
+ TString line = gSystem->ExpandPathName(filename);
+ line.Prepend(".x ");
+ gROOT->ProcessLine(line.Data());
+ }
+ return mgr;
+}
+
+//______________________________________________________________________________
+Int_t AliAnalysisAlien::GetNmodules() const
+{
+// Get number of modules.
+ if (!fModules) return 0;
+ return fModules->GetEntries();
+}
+
+//______________________________________________________________________________
+AliAnalysisTaskCfg *AliAnalysisAlien::GetModule(const char *name)
+{
+// Get a module by name.
+ if (!fModules) return 0;
+ return (AliAnalysisTaskCfg*)fModules->FindObject(name);
+}
+
+//______________________________________________________________________________
+Bool_t AliAnalysisAlien::LoadModule(AliAnalysisTaskCfg *mod)
+{
+// Load a given module.
+ if (mod->IsLoaded()) return kTRUE;
+ AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
+ if (!mgr) {
+ Error("LoadModule", "No analysis manager created yet. Use CreateAnalysisManager first.");
+ return kFALSE;
+ }
+ Int_t ndeps = mod->GetNdeps();
+ TString depname;
+ for (Int_t j=0; j<ndeps; j++) {
+ depname = mod->GetDependency(j);
+ AliAnalysisTaskCfg *dep = GetModule(depname);
+ if (!dep) {
+ Error("LoadModule","Dependency %s not existing for module %s",
+ depname.Data(), mod->GetName());
+ return kFALSE;
+ }
+ if (!LoadModule(dep)) {
+ Error("LoadModule","Dependency %s for module %s could not be loaded",
+ depname.Data(), mod->GetName());
+ return kFALSE;
+ }
+ }
+ // Load libraries for the module
+ if (!mod->CheckLoadLibraries()) {
+ Error("LoadModule", "Cannot load all libraries for module %s", mod->GetName());
+ return kFALSE;
+ }
+ // Check if a custom file name was requested
+ if (strlen(mod->GetOutputFileName())) mgr->SetCommonFileName(mod->GetOutputFileName());
+
+ // Check if a custom terminate file name was requested
+ if (strlen(mod->GetTerminateFileName())) {
+ if (!fTerminateFiles.IsNull()) fTerminateFiles += ",";
+ fTerminateFiles += mod->GetTerminateFileName();
+ }
+
+ // Execute the macro
+ if (mod->ExecuteMacro()<0) {
+ Error("LoadModule", "Executing the macro %s with arguments: %s for module %s returned a negative value",
+ mod->GetMacroName(), mod->GetMacroArgs(), mod->GetName());
+ return kFALSE;
+ }
+ // Configure dependencies
+ if (mod->GetConfigMacro() && mod->ExecuteConfigMacro()<0) {
+ Error("LoadModule", "There was an error executing the deps config macro %s for module %s",
+ mod->GetConfigMacro()->GetTitle(), mod->GetName());
+ return kFALSE;
+ }
+ // Adjust extra libraries
+ Int_t nlibs = mod->GetNlibs();
+ TString lib;
+ for (Int_t i=0; i<nlibs; i++) {
+ lib = mod->GetLibrary(i);
+ lib = Form("lib%s.so", lib.Data());
+ if (fAdditionalLibs.Contains(lib)) continue;
+ if (!fAdditionalLibs.IsNull()) fAdditionalLibs += " ";
+ fAdditionalLibs += lib;
+ }
+ return kTRUE;
+}
+
+//______________________________________________________________________________
+Bool_t AliAnalysisAlien::GenerateTrain(const char *name)
+{
+// Generate the full train.
+ fAdditionalLibs = "";
+ if (!LoadModules()) return kFALSE;
+ AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
+ if (!mgr->InitAnalysis()) return kFALSE;
+ mgr->RunLocalInit();
+ mgr->PrintStatus();
+ Int_t productionMode = fProductionMode;
+ SetProductionMode();
+ TString macro = fAnalysisMacro;
+ TString executable = fExecutable;
+ TString validation = fValidationScript;
+ TString execCommand = fExecutableCommand;
+ SetAnalysisMacro(Form("%s.C", name));
+ SetExecutable(Form("%s.sh", name));
+// SetExecutableCommand("aliroot -b -q ");
+ SetValidationScript(Form("%s_validation.sh", name));
+ StartAnalysis();
+ SetProductionMode(productionMode);
+ fAnalysisMacro = macro;
+ fExecutable = executable;
+ fExecutableCommand = execCommand;
+ fValidationScript = validation;
+ return kTRUE;
+}
+
+//______________________________________________________________________________
+Bool_t AliAnalysisAlien::GenerateTest(const char *name, const char *modname)
+{
+// Generate test macros for a single module or for the full train.
+ fAdditionalLibs = "";
+ if (strlen(modname)) {
+ if (!CheckDependencies()) return kFALSE;
+ AliAnalysisTaskCfg *mod = GetModule(modname);
+ if (!mod) {
+ Error("GenerateTest", "cannot generate test for inexistent module %s", modname);
+ return kFALSE;
+ }
+ if (!LoadModule(mod)) return kFALSE;
+ } else if (!LoadModules()) return kFALSE;
+ AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
+ if (!mgr->InitAnalysis()) return kFALSE;
+ mgr->RunLocalInit();
+ mgr->PrintStatus();
+ SetLocalTest(kTRUE);
+ Int_t productionMode = fProductionMode;
+ SetProductionMode();
+ TString macro = fAnalysisMacro;
+ TString executable = fExecutable;
+ TString validation = fValidationScript;
+ TString execCommand = fExecutableCommand;
+ SetAnalysisMacro(Form("%s.C", name));
+ SetExecutable(Form("%s.sh", name));
+// SetExecutableCommand("aliroot -b -q ");
+ SetValidationScript(Form("%s_validation.sh", name));
+ WriteAnalysisFile();
+ WriteAnalysisMacro();
+ WriteExecutable();
+ WriteValidationScript();
+ SetLocalTest(kFALSE);
+ SetProductionMode(productionMode);
+ fAnalysisMacro = macro;
+ fExecutable = executable;
+ fExecutableCommand = execCommand;
+ fValidationScript = validation;
+ return kTRUE;
+}
+
+//______________________________________________________________________________
+Bool_t AliAnalysisAlien::LoadModules()
+{
+// Load all modules by executing the AddTask macros. Checks first the dependencies.
+ fAdditionalLibs = "";
+ Int_t nmodules = GetNmodules();
+ if (!nmodules) {
+ Warning("LoadModules", "No module to be loaded");
+ return kTRUE;
+ }
+ AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
+ if (!mgr) {
+ Error("LoadModules", "No analysis manager created yet. Use CreateAnalysisManager first.");
+ return kFALSE;
+ }
+ if (!CheckDependencies()) return kFALSE;
+ nmodules = GetNmodules();
+ AliAnalysisTaskCfg *mod;
+ for (Int_t imod=0; imod<nmodules; imod++) {
+ mod = (AliAnalysisTaskCfg*)fModules->At(imod);
+ if (!LoadModule(mod)) return kFALSE;
+ }
+ return kTRUE;
+}
+