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 **************************************************************************/
16 #include "AliAnalysisTaskCfg.h"
17 #include "AliAnalysisManager.h"
19 #include "Riostream.h"
23 #include "TObjArray.h"
24 #include "TObjString.h"
27 // Author: Andrei Gheata, 12/08/2011
29 //==============================================================================
30 // AliAnalysysTaskCfg - Class embedding the configuration needed to run
31 // a given analysis task: libraries to be loaded, location and name of the macro
32 // used to add the task to the analysis manager, dependencies.
33 //==============================================================================
35 // This class is used to fully describe how to run a given analysis task. It
36 // requires that the user creates an AddTask macro for his task and defines:
37 // - The needed libs separated by commas,
38 // - The full path to the AddTask macro (starting with $ALICE_ROOT if needed)
39 // - The list of arguments to be provided to the AddTask macro. One can use
40 // here only constants that can be interpreted.
41 // - The list of dependencies (other modules required to run this task). These
42 // must be names of other AliAnalysisTaskCfg objects, separated by commas.
43 // - Data types supported by the task (e.g. ESD, AOD, MC)
44 // The class has normal ROOT IO, but it can also read from and write to text files.
46 // Content of file: QAsym.cfg
48 # Lines that do not start with #Module are ignored, except those in embedded
52 #Module.Deps PhysicsSelection
53 #Module.DataTypes ESD, AOD, MC
54 #Module.MacroName $ALICE_ROOT/PWG1/PilotTrain/AddTaskQAsym.C
55 #Module.MacroArgs 0, AliVEvent::kAnyINT, AliVEvent::kHighMult, AliVEvent::kEMC7, AliVEvent::kMUU7
57 __R_ADDTASK__->SelectCollisionCandidates();
60 // The following special variable names can be used:
61 // __R_ADDTASK__ = the return value of the AddTask macro included
62 // __R_ESDH__ = pointer to ESD handler
63 // __R_AODH__ = pointer to AOD handler
64 // __R_MCH__ = pointer to MC handler
65 // The static method ExtractModulesFrom(const char *filename) allows reading
66 // several task configuration modules from the same text file and returning
67 // them in a TObjArray.
69 // A list of configuration modules representing a train should be injected in
70 // the right order in the grid handler to generate train macros.
73 ClassImp(AliAnalysisTaskCfg)
75 //______________________________________________________________________________
76 AliAnalysisTaskCfg::AliAnalysisTaskCfg()
90 //______________________________________________________________________________
91 AliAnalysisTaskCfg::AliAnalysisTaskCfg(const char *name)
102 // Constructor. All configuration objects need to be named since they are looked
106 //______________________________________________________________________________
107 AliAnalysisTaskCfg::AliAnalysisTaskCfg(const AliAnalysisTaskCfg &other)
109 fMacroName(other.fMacroName),
110 fMacroArgs(other.fMacroArgs),
113 fDataTypes(other.fDataTypes),
119 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
120 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
123 //______________________________________________________________________________
124 AliAnalysisTaskCfg::~AliAnalysisTaskCfg()
131 //______________________________________________________________________________
132 AliAnalysisTaskCfg& AliAnalysisTaskCfg::operator=(const AliAnalysisTaskCfg &other)
134 // Assignment operator.
135 if (&other == this) return *this;
136 TNamed::operator=(other);
137 fMacroName = other.fMacroName;
138 fMacroArgs = other.fMacroArgs;
141 fDataTypes = other.fDataTypes;
142 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
143 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
144 fRAddTask = other.fRAddTask;
148 //______________________________________________________________________________
149 TMacro *AliAnalysisTaskCfg::OpenMacro(const char *name)
151 // Opens the specified macro if name is not empty. In case of success updates
152 // fMacroName, creates the maco object and returns its pointer.
153 // Clean-up previous macro if any
159 if (strlen(name)) expname = gSystem->ExpandPathName(name);
160 else expname = gSystem->ExpandPathName(fMacroName.Data());
161 if (expname.IsNull()) {
162 Error("OpenMacro", "Macro name not provided and not previously set");
165 if (gSystem->AccessPathName(expname)) {
166 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
169 if (strlen(name)) fMacroName = name;
170 fMacro = new TMacro(expname);
174 //______________________________________________________________________________
175 void AliAnalysisTaskCfg::SetMacro(TMacro *macro)
177 // Set the AddTask macro from outside. This will discard the existing macro if
178 // any. The provided macro will be owned by this object.
179 if (fMacro) delete fMacro;
183 //______________________________________________________________________________
184 Long64_t AliAnalysisTaskCfg::ExecuteMacro(const char *newargs)
186 // Execute AddTask macro. Opens first the macro pointed by fMacroName if not yet
187 // done. Checks if the requested libraries are loaded, else loads them. Executes
188 // with stored fMacroArgs unless new arguments are provided. The flag IsLoaded
189 // is set once the macro was successfully executed.
190 if (IsLoaded()) return kTRUE;
191 if (!fMacro && !OpenMacro()) {
192 Error("ExecuteMacro", "Cannot execute this macro");
195 if (!CheckLoadLibraries()) {
196 Error("ExecuteMacro", "Cannot load requested libraries: %s", fLibs.Data());
200 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
202 Error("ExecuteMacro", "Analysis manager not defined yet");
205 Int_t ntasks0 = mgr->GetTasks()->GetEntriesFast();
206 TString args = newargs;
207 if (args.IsNull()) args = fMacroArgs;
208 Long64_t retval = fMacro->Exec(args);
209 Int_t ntasks = mgr->GetTasks()->GetEntriesFast();
210 Long64_t ptrTask = 1;
212 ptrTask = (Long64_t)mgr->GetTasks()->At(ntasks0);
213 if (retval >= ptrTask) {
214 TObject::SetBit(AliAnalysisTaskCfg::kLoaded, kTRUE);
215 fRAddTask = reinterpret_cast<TObject*>(retval);
216 if (fConfigDeps && dynamic_cast<TObject*>(fRAddTask)) {
217 TString classname = fRAddTask->ClassName();
218 classname += Form("* __R_ADDTASK__ = (%s*)0x%lx;", classname.Data(),(ULong_t)retval);
219 classname.Prepend(" ");
220 TObjString *line = fConfigDeps->GetLineWith("__R_ADDTASK__");
222 TList *lines = fConfigDeps->GetListOfLines();
223 lines->AddBefore(line, new TObjString(classname));
227 Info("ExecuteMacro", "Macro %s added %d tasks to the manager", fMacro->GetName(), ntasks-ntasks0);
231 //______________________________________________________________________________
232 Int_t AliAnalysisTaskCfg::GetNlibs() const
234 // Returns number of requested libraries.
235 if (fLibs.IsNull()) return 0;
236 Int_t nlibs = fLibs.CountChar(',')+1;
240 //______________________________________________________________________________
241 const char *AliAnalysisTaskCfg::GetLibrary(Int_t i) const
243 // Returns library name for the i-th library.
244 Int_t nlibs = GetNlibs();
245 if (i>=nlibs) return 0;
247 TObjArray *list = fLibs.Tokenize(",");
248 libname = list->At(i)->GetName();
249 libname.ReplaceAll(".so","");
250 libname.ReplaceAll(" ","");
251 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
253 return libname.Data();
256 //______________________________________________________________________________
257 Bool_t AliAnalysisTaskCfg::CheckLoadLibraries() const
259 // Check if all requested libraries were loaded, otherwise load them. If some
260 // library cannot be loaded return false.
262 Int_t nlibs = GetNlibs();
263 for (Int_t i=0; i<nlibs; i++) {
264 library = GetLibrary(i);
265 library.Prepend("lib");
266 Int_t loaded = strlen(gSystem->GetLibraries(library,"",kFALSE));
267 if (!loaded) loaded = gSystem->Load(library);
269 Error("CheckLoadLibraries", "Cannot load library %s", library.Data());
276 //______________________________________________________________________________
277 Bool_t AliAnalysisTaskCfg::NeedsLibrary(const char *lib) const
279 // Check if a given library is needed by the module.
280 TString libname = lib;
281 libname.ReplaceAll(".so","");
282 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
283 return fLibs.Contains(libname);
286 //______________________________________________________________________________
287 Int_t AliAnalysisTaskCfg::GetNdeps() const
289 // Returns number of requested libraries.
290 if (fDeps.IsNull()) return 0;
291 Int_t ndeps = fDeps.CountChar(',')+1;
295 //______________________________________________________________________________
296 const char *AliAnalysisTaskCfg::GetDependency(Int_t i) const
298 // Returns library name for the i-th library.
299 Int_t ndeps = GetNdeps();
300 if (i>=ndeps) return 0;
302 TObjArray *list = fDeps.Tokenize(",");
303 depname = list->At(i)->GetName();
304 depname.ReplaceAll(" ","");
306 return depname.Data();
309 //______________________________________________________________________________
310 Bool_t AliAnalysisTaskCfg::NeedsDependency(const char *dep) const
312 // Check if a given library is needed by the module.
313 return fDeps.Contains(dep);
316 //______________________________________________________________________________
317 TMacro *AliAnalysisTaskCfg::OpenConfigMacro(const char *name)
319 // Opens the specified macro if name is not empty.
325 TString expname = gSystem->ExpandPathName(name);
326 if (expname.IsNull()) {
327 Error("OpenConfigMacro", "Macro name not provided");
330 if (gSystem->AccessPathName(expname)) {
331 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
334 fConfigDeps = new TMacro(expname);
338 //______________________________________________________________________________
339 void AliAnalysisTaskCfg::SetConfigMacro(TMacro *macro)
341 // Set the macro for configuring deps from outside. This will discard the
342 // existing macro if any. The provided macro will be owned by this object.
343 if (fConfigDeps) delete fConfigDeps;
347 //______________________________________________________________________________
348 Long64_t AliAnalysisTaskCfg::ExecuteConfigMacro()
350 // Execute macro to configure dependencies. No arguments are supported.
352 Error("ExecuteConfigMacro", "Call OpenConfigMacro() first");
355 if (!CheckLoadLibraries()) {
356 Error("ExecuteConfigMacro", "Cannot load requested libraries: %s", fLibs.Data());
359 return fConfigDeps->Exec();
362 //______________________________________________________________________________
363 void AliAnalysisTaskCfg::SetDataTypes(const char *types)
365 // Sets the data types supported by the module. Stored in upper case.
367 fDataTypes.ToUpper();
370 //______________________________________________________________________________
371 Bool_t AliAnalysisTaskCfg::SupportsData(const char *type) const
373 // Checks if the given data type is supported.
374 TString stype = type;
376 return fDataTypes.Contains(stype);
379 //______________________________________________________________________________
380 void AliAnalysisTaskCfg::Print(Option_t * option) const
382 // Print content of the module.
384 Bool_t full = (opt.Length())?kTRUE:kFALSE;
385 printf("====================================================================\n");
386 printf("# Analysis task: %s\n", GetName());
387 printf("# Supported data types: %s\n", fDataTypes.Data());
388 printf("# Extra libraries: %s\n", fLibs.Data());
389 printf("# Extra dependencies: %s\n", fDeps.Data());
391 printf("# Macro to configure deps: %s\n", fConfigDeps->GetTitle());
392 if (full) fConfigDeps->Print();
394 printf("# Macro connecting this task: %s\n", fMacroName.Data());
395 printf("# Arguments to run the macro: %s\n", fMacroArgs.Data());
397 if (fMacro) fMacro->Print();
399 TMacro macro(gSystem->ExpandPathName(fMacroName.Data()));
405 //______________________________________________________________________________
406 void AliAnalysisTaskCfg::SaveAs(const char *filename, Option_t *option) const
408 // Save the configuration module as text file in the form key:value. The
409 // option can be APPEND, otherwise the file gets overwritten.
412 ios::openmode mode = ios::out;
413 if (opt == "APPEND") mode = ios::app;
415 out.open(filename, mode);
417 Error("SaveAs", "Bad file name: %s", filename);
420 out << "#Module.Begin " << GetName() << endl;
421 out << "#Module.Libs " << fLibs << endl;
422 out << "#Module.Deps " << fDeps << endl;
423 out << "#Module.DataTypes " << fDataTypes << endl;
424 out << "#Module.MacroName " << fMacroName << endl;
425 out << "#Module.MacroArgs " << fMacroArgs << endl;
427 out << "#Config.Deps " << fConfigDeps->GetTitle() << endl;
432 //______________________________________________________________________________
433 const char *AliAnalysisTaskCfg::DecodeValue(TString &line)
435 // Decode the value string from the line
436 TString value = line(line.Index(' '),line.Length());
437 value = value.Strip(TString::kLeading,' ');
438 value = value.Strip(TString::kTrailing,' ');
442 //______________________________________________________________________________
443 TObjArray *AliAnalysisTaskCfg::ExtractModulesFrom(const char *filename)
445 // Read all modules from a text file and add them to an object array. The
446 // caller must delete the array at the end. Any module must start with a line
447 // containing: #Module.Begin
448 TString expname = gSystem->ExpandPathName(filename);
449 if (gSystem->AccessPathName(expname)) {
450 ::Error("ExtractModulesFrom", "Cannot open file %s", filename);
453 AliAnalysisTaskCfg *cfg = 0;
454 TObjArray *array = 0;
460 TMacro *addMacro = 0;
461 TMacro *addConfig = 0;
463 in.getline(cline,1024);
465 if (line.BeginsWith("#Module.Begin")) {
466 // New module found, save previous if any
468 if (addMacro) cfg->SetMacro(addMacro);
469 if (addConfig) cfg->SetConfigMacro(addConfig);
470 if (!array) array = new TObjArray();
473 // Decode module name from the line
474 decode = AliAnalysisTaskCfg::DecodeValue(line);
475 cfg = new AliAnalysisTaskCfg(decode);
478 } else if (cfg && line.BeginsWith("#Module.Libs")) {
480 decode = AliAnalysisTaskCfg::DecodeValue(line);
481 cfg->SetLibraries(decode);
482 } else if (cfg && line.BeginsWith("#Module.Deps")) {
483 // Dependencies section
484 decode = AliAnalysisTaskCfg::DecodeValue(line);
485 cfg->SetDependencies(decode);
486 } else if (cfg && line.BeginsWith("#Module.DataTypes")) {
488 decode = AliAnalysisTaskCfg::DecodeValue(line);
489 cfg->SetDataTypes(decode);
490 } else if (cfg && line.BeginsWith("#Module.MacroName")) {
491 // Name of the add task macro (including path)
492 decode = AliAnalysisTaskCfg::DecodeValue(line);
493 cfg->SetMacroName(decode);
494 } else if (cfg && line.BeginsWith("#Module.MacroArgs")) {
495 // Arguments for the AddTask macro
496 decode = AliAnalysisTaskCfg::DecodeValue(line);
497 cfg->SetMacroArgs(decode);
498 } else if (cfg && line.BeginsWith("#Module.StartMacro")) {
499 // Marker for start of the AddTask macro
500 addMacro = new TMacro();
501 TString shortName = gSystem->BaseName(cfg->GetMacroName());
502 shortName = shortName(0,shortName.Index("."));
503 addMacro->SetName(shortName);
504 addMacro->SetTitle(cfg->GetMacroName());
505 } else if (cfg && line.BeginsWith("#Module.StartConfig")) {
506 // Marker for start of the configuration macro
507 addConfig = new TMacro();
508 TString shortName = gSystem->BaseName(cfg->GetMacroName());
509 shortName = shortName(0,shortName.Index("."));
510 shortName += "Config";
511 addConfig->SetName(shortName);
512 shortName.Prepend("/");
513 shortName.Prepend(gSystem->DirName(cfg->GetMacroName()));
515 addConfig->SetTitle(shortName);
516 } else if (cfg && line.BeginsWith("#Module.EndMacro")) {
517 // Marker for the end of the embedded macro. EndMacro block mandatory.
518 if (cfg && addMacro) {
519 cfg->SetMacro(addMacro);
522 ::Error("ExtractModulesFrom", "Canot end un-started macro block");
524 } else if (cfg && line.BeginsWith("#Module.EndConfig")) {
525 // Marker for the end of the config macro. EndConfig block is mandatory
526 if (cfg && addConfig) {
527 addConfig->GetListOfLines()->AddFirst(new TObjString(Form("%s() {",gSystem->BaseName(addConfig->GetName()))));
528 addConfig->GetListOfLines()->AddLast(new TObjString("}"));
529 cfg->SetConfigMacro(addConfig);
532 ::Error("ExtractModulesFrom", "Canot end un-started config macro block");
535 // Reading a block line
536 if (addMacro) addMacro->AddLine(line);
537 else if (addConfig) addConfig->AddLine(line);
540 // Add last found object to the list
542 if (addMacro) ::Error("ExtractModulesFrom", "#Module.EndMacro block not found");
543 if (addConfig) ::Error("ExtractModulesFrom", "#Module.EndConfig block not found");
544 if (!array) array = new TObjArray();