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"
22 #include "TInterpreter.h"
24 #include "TObjArray.h"
25 #include "TObjString.h"
28 // Author: Andrei Gheata, 12/08/2011
30 //==============================================================================
31 // AliAnalysysTaskCfg - Class embedding the configuration needed to run
32 // a given analysis task: libraries to be loaded, location and name of the macro
33 // used to add the task to the analysis manager, dependencies.
34 //==============================================================================
36 // This class is used to fully describe how to run a given analysis task. It
37 // requires that the user creates an AddTask macro for his task and defines:
38 // - The needed libs separated by commas,
39 // - The full path to the AddTask macro (starting with $ALICE_ROOT if needed)
40 // - The list of arguments to be provided to the AddTask macro. One can use
41 // here only constants that can be interpreted.
42 // - The list of dependencies (other modules required to run this task). These
43 // must be names of other AliAnalysisTaskCfg objects, separated by commas.
44 // - Data types supported by the task (e.g. ESD, AOD, MC)
45 // The class has normal ROOT IO, but it can also read from and write to text files.
47 // Content of file: QAsym.cfg
49 # Lines that do not start with #Module are ignored, except those in embedded
53 #Module.Deps PhysicsSelection
54 #Module.DataTypes ESD, AOD, MC
55 #Module.MacroName $ALICE_ROOT/PWGPP/PilotTrain/AddTaskQAsym.C
56 #Module.MacroArgs 0, AliVEvent::kAnyINT, AliVEvent::kHighMult, AliVEvent::kEMC7, AliVEvent::kMUU7
60 __R_ADDTASK__->SelectCollisionCandidates();
63 // The following special variable names can be used:
64 // __R_ADDTASK__ = the return value of the AddTask macro included
65 // __R_ESDH__ = pointer to ESD handler
66 // __R_AODH__ = pointer to AOD handler
67 // __R_MCH__ = pointer to MC handler
68 // The static method ExtractModulesFrom(const char *filename) allows reading
69 // several task configuration modules from the same text file and returning
70 // them in a TObjArray.
72 // A list of configuration modules representing a train should be injected in
73 // the right order in the grid handler to generate train macros.
76 ClassImp(AliAnalysisTaskCfg)
78 //______________________________________________________________________________
79 AliAnalysisTaskCfg::AliAnalysisTaskCfg()
95 //______________________________________________________________________________
96 AliAnalysisTaskCfg::AliAnalysisTaskCfg(const char *name)
109 // Constructor. All configuration objects need to be named since they are looked
113 //______________________________________________________________________________
114 AliAnalysisTaskCfg::AliAnalysisTaskCfg(const AliAnalysisTaskCfg &other)
116 fMacroName(other.fMacroName),
117 fMacroArgs(other.fMacroArgs),
120 fDataTypes(other.fDataTypes),
121 fOutputFile(other.fOutputFile),
122 fTerminateFile(other.fTerminateFile),
128 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
129 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
132 //______________________________________________________________________________
133 AliAnalysisTaskCfg::~AliAnalysisTaskCfg()
140 //______________________________________________________________________________
141 AliAnalysisTaskCfg& AliAnalysisTaskCfg::operator=(const AliAnalysisTaskCfg &other)
143 // Assignment operator.
144 if (&other == this) return *this;
145 TNamed::operator=(other);
146 fMacroName = other.fMacroName;
147 fMacroArgs = other.fMacroArgs;
150 fDataTypes = other.fDataTypes;
151 fOutputFile = other.fOutputFile;
152 fTerminateFile = other.fTerminateFile;
153 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
154 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
155 fRAddTask = other.fRAddTask;
159 //______________________________________________________________________________
160 TMacro *AliAnalysisTaskCfg::OpenMacro(const char *name)
162 // Opens the specified macro if name is not empty. In case of success updates
163 // fMacroName, creates the maco object and returns its pointer.
164 // Clean-up previous macro if any
170 if (strlen(name)) expname = gSystem->ExpandPathName(name);
171 else expname = gSystem->ExpandPathName(fMacroName.Data());
172 if (expname.IsNull()) {
173 Error("OpenMacro", "Macro name not provided and not previously set");
176 if (gSystem->AccessPathName(expname)) {
177 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
180 if (strlen(name)) fMacroName = name;
181 fMacro = new TMacro(expname);
185 //______________________________________________________________________________
186 void AliAnalysisTaskCfg::SetMacro(TMacro *macro)
188 // Set the AddTask macro from outside. This will discard the existing macro if
189 // any. The provided macro will be owned by this object.
190 if (fMacro) delete fMacro;
194 //______________________________________________________________________________
195 Long64_t AliAnalysisTaskCfg::ExecuteMacro(const char *newargs)
197 // Execute AddTask macro. Opens first the macro pointed by fMacroName if not yet
198 // done. Checks if the requested libraries are loaded, else loads them. Executes
199 // with stored fMacroArgs unless new arguments are provided. The flag IsLoaded
200 // is set once the macro was successfully executed.
201 if (IsLoaded()) return kTRUE;
202 if (!fMacro && !OpenMacro()) {
203 Error("ExecuteMacro", "Cannot execute this macro");
206 if (!CheckLoadLibraries()) {
207 Error("ExecuteMacro", "Cannot load requested libraries: %s", fLibs.Data());
211 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
213 Error("ExecuteMacro", "Analysis manager not defined yet");
216 Int_t ntasks0 = mgr->GetTasks()->GetEntriesFast();
217 TString args = newargs;
218 if (args.IsNull()) args = fMacroArgs;
220 Long64_t retval = fMacro->Exec(args, &error);
221 if (error != TInterpreter::kNoError)
223 Error("ExecuteMacro", "Macro interpretation failed");
226 Int_t ntasks = mgr->GetTasks()->GetEntriesFast();
229 Error("ExecuteMacro", "The macro did not add any tasks to the manager");
232 Long64_t ptrTask = (Long64_t)mgr->GetTasks()->At(ntasks0);
233 if (retval >= ptrTask) {
234 TObject::SetBit(AliAnalysisTaskCfg::kLoaded, kTRUE);
235 fRAddTask = reinterpret_cast<TObject*>(retval);
236 if (fConfigDeps && dynamic_cast<TObject*>(fRAddTask)) {
237 TString classname = fRAddTask->ClassName();
238 classname += Form("* __R_ADDTASK__ = (%s*)0x%lx;", classname.Data(),(ULong_t)retval);
239 classname.Prepend(" ");
240 TObjString *line = fConfigDeps->GetLineWith("__R_ADDTASK__");
242 TList *lines = fConfigDeps->GetListOfLines();
243 lines->AddBefore(line, new TObjString(classname));
247 Info("ExecuteMacro", "Macro %s added %d tasks to the manager", fMacro->GetName(), ntasks-ntasks0);
251 //______________________________________________________________________________
252 Int_t AliAnalysisTaskCfg::GetNlibs() const
254 // Returns number of requested libraries.
255 if (fLibs.IsNull()) return 0;
256 TObjArray *list = fLibs.Tokenize(",");
257 Int_t nlibs = list->GetEntriesFast();
262 //______________________________________________________________________________
263 const char *AliAnalysisTaskCfg::GetLibrary(Int_t i) const
265 // Returns library name for the i-th library.
266 Int_t nlibs = GetNlibs();
267 if (i>=nlibs) return 0;
268 static TString libname;
269 TObjArray *list = fLibs.Tokenize(",");
270 libname = list->At(i)->GetName();
271 libname.ReplaceAll(".so","");
272 libname.ReplaceAll(" ","");
273 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
275 return libname.Data();
278 //______________________________________________________________________________
279 Bool_t AliAnalysisTaskCfg::CheckLoadLibraries() const
281 // Check if all requested libraries were loaded, otherwise load them. If some
282 // library cannot be loaded return false.
284 Int_t nlibs = GetNlibs();
285 for (Int_t i=0; i<nlibs; i++) {
286 library = GetLibrary(i);
287 library.Prepend("lib");
288 Int_t loaded = strlen(gSystem->GetLibraries(library,"",kFALSE));
289 if (!loaded) loaded = gSystem->Load(library);
291 Error("CheckLoadLibraries", "Cannot load library %s", library.Data());
298 //______________________________________________________________________________
299 Bool_t AliAnalysisTaskCfg::NeedsLibrary(const char *lib) const
301 // Check if a given library is needed by the module.
302 TString libname = lib;
303 libname.ReplaceAll(".so","");
304 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
305 return fLibs.Contains(libname);
308 //______________________________________________________________________________
309 Int_t AliAnalysisTaskCfg::GetNdeps() const
311 // Returns number of requested libraries.
312 if (fDeps.IsNull()) return 0;
313 Int_t ndeps = fDeps.CountChar(',')+1;
317 //______________________________________________________________________________
318 const char *AliAnalysisTaskCfg::GetDependency(Int_t i) const
320 // Returns library name for the i-th library.
321 Int_t ndeps = GetNdeps();
322 if (i>=ndeps) return 0;
323 static TString depname;
324 TObjArray *list = fDeps.Tokenize(",");
325 depname = list->At(i)->GetName();
326 depname.ReplaceAll(" ","");
328 return depname.Data();
331 //______________________________________________________________________________
332 Bool_t AliAnalysisTaskCfg::NeedsDependency(const char *dep) const
334 // Check if a given library is needed by the module.
335 return fDeps.Contains(dep);
338 //______________________________________________________________________________
339 TMacro *AliAnalysisTaskCfg::OpenConfigMacro(const char *name)
341 // Opens the specified macro if name is not empty.
347 TString expname = gSystem->ExpandPathName(name);
348 if (expname.IsNull()) {
349 Error("OpenConfigMacro", "Macro name not provided");
352 if (gSystem->AccessPathName(expname)) {
353 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
356 fConfigDeps = new TMacro(expname);
360 //______________________________________________________________________________
361 void AliAnalysisTaskCfg::SetConfigMacro(TMacro *macro)
363 // Set the macro for configuring deps from outside. This will discard the
364 // existing macro if any. The provided macro will be owned by this object.
365 if (fConfigDeps) delete fConfigDeps;
369 //______________________________________________________________________________
370 Long64_t AliAnalysisTaskCfg::ExecuteConfigMacro()
372 // Execute macro to configure dependencies. No arguments are supported.
374 Error("ExecuteConfigMacro", "Call OpenConfigMacro() first");
377 if (!CheckLoadLibraries()) {
378 Error("ExecuteConfigMacro", "Cannot load requested libraries: %s", fLibs.Data());
381 return fConfigDeps->Exec();
384 //______________________________________________________________________________
385 void AliAnalysisTaskCfg::SetDataTypes(const char *types)
387 // Sets the data types supported by the module. Stored in upper case.
389 fDataTypes.ToUpper();
392 //______________________________________________________________________________
393 Bool_t AliAnalysisTaskCfg::SupportsData(const char *type) const
395 // Checks if the given data type is supported.
396 TString stype = type;
398 return fDataTypes.Contains(stype);
401 //______________________________________________________________________________
402 void AliAnalysisTaskCfg::Print(Option_t * option) const
404 // Print content of the module.
406 Bool_t full = (opt.Length())?kTRUE:kFALSE;
407 printf("====================================================================\n");
408 printf("# Analysis task: %s\n", GetName());
409 printf("# Supported data types: %s\n", fDataTypes.Data());
410 printf("# Extra libraries: %s\n", fLibs.Data());
411 printf("# Extra dependencies: %s\n", fDeps.Data());
413 printf("# Macro to configure deps: %s\n", fConfigDeps->GetTitle());
414 if (full) fConfigDeps->Print();
416 printf("# Macro connecting this task: %s\n", fMacroName.Data());
417 printf("# Arguments to run the macro: %s\n", fMacroArgs.Data());
419 if (fMacro) fMacro->Print();
421 TMacro macro(gSystem->ExpandPathName(fMacroName.Data()));
427 //______________________________________________________________________________
428 void AliAnalysisTaskCfg::SaveAs(const char *filename, Option_t *option) const
430 // Save the configuration module as text file in the form key:value. The
431 // option can be APPEND, otherwise the file gets overwritten.
434 ios::openmode mode = ios::out;
435 if (opt == "APPEND") mode = ios::app;
437 out.open(filename, mode);
439 Error("SaveAs", "Bad file name: %s", filename);
442 out << "#Module.Begin " << GetName() << endl;
443 out << "#Module.Libs " << fLibs << endl;
444 out << "#Module.Deps " << fDeps << endl;
445 out << "#Module.DataTypes " << fDataTypes << endl;
446 out << "#Module.OutputFile " << fOutputFile << endl;
447 out << "#Module.TerminateFile " << fTerminateFile << endl;
448 out << "#Module.MacroName " << fMacroName << endl;
449 out << "#Module.MacroArgs " << fMacroArgs << endl;
451 out << "#Config.Deps " << fConfigDeps->GetTitle() << endl;
456 //______________________________________________________________________________
457 const char *AliAnalysisTaskCfg::DecodeValue(TString &line)
459 // Decode the value string from the line
460 static TString value;
461 value = line(line.Index(' '),line.Length());
462 value = value.Strip(TString::kLeading,' ');
463 value = value.Strip(TString::kTrailing,' ');
467 //______________________________________________________________________________
468 TObjArray *AliAnalysisTaskCfg::ExtractModulesFrom(const char *filename)
470 // Read all modules from a text file and add them to an object array. The
471 // caller must delete the array at the end. Any module must start with a line
472 // containing: #Module.Begin
473 TString expname = gSystem->ExpandPathName(filename);
474 if (gSystem->AccessPathName(expname)) {
475 ::Error("ExtractModulesFrom", "Cannot open file %s", filename);
478 AliAnalysisTaskCfg *cfg = 0;
479 TObjArray *array = 0;
485 TMacro *addMacro = 0;
486 TMacro *addConfig = 0;
488 in.getline(cline,1024);
490 if (line.BeginsWith("#Module.Begin")) {
491 // New module found, save previous if any
493 if (addMacro) cfg->SetMacro(addMacro);
494 if (addConfig) cfg->SetConfigMacro(addConfig);
495 if (!array) array = new TObjArray();
498 // Decode module name from the line
499 decode = AliAnalysisTaskCfg::DecodeValue(line);
500 cfg = new AliAnalysisTaskCfg(decode);
503 } else if (cfg && line.BeginsWith("#Module.Libs")) {
505 decode = AliAnalysisTaskCfg::DecodeValue(line);
506 cfg->SetLibraries(decode);
507 } else if (cfg && line.BeginsWith("#Module.Deps")) {
508 // Dependencies section
509 decode = AliAnalysisTaskCfg::DecodeValue(line);
510 cfg->SetDependencies(decode);
511 } else if (cfg && line.BeginsWith("#Module.DataTypes")) {
513 decode = AliAnalysisTaskCfg::DecodeValue(line);
514 cfg->SetDataTypes(decode);
515 } else if (cfg && line.BeginsWith("#Module.OutputFile")) {
516 // Desired output file name (via SetCommonOutput)
517 decode = AliAnalysisTaskCfg::DecodeValue(line);
518 cfg->SetOutputFileName(decode);
519 } else if (cfg && line.BeginsWith("#Module.TerminateFile")) {
520 // Custom file name produced in Terminate if any
521 decode = AliAnalysisTaskCfg::DecodeValue(line);
522 cfg->SetTerminateFileName(decode);
523 } else if (cfg && line.BeginsWith("#Module.MacroName")) {
524 // Name of the add task macro (including path)
525 decode = AliAnalysisTaskCfg::DecodeValue(line);
526 cfg->SetMacroName(decode);
527 } else if (cfg && line.BeginsWith("#Module.MacroArgs")) {
528 // Arguments for the AddTask macro
529 decode = AliAnalysisTaskCfg::DecodeValue(line);
530 cfg->SetMacroArgs(decode);
531 } else if (cfg && line.BeginsWith("#Module.StartMacro")) {
532 // Marker for start of the AddTask macro
533 addMacro = new TMacro();
534 TString shortName = gSystem->BaseName(cfg->GetMacroName());
535 shortName = shortName(0,shortName.Index("."));
536 addMacro->SetName(shortName);
537 addMacro->SetTitle(cfg->GetMacroName());
538 } else if (cfg && line.BeginsWith("#Module.StartConfig")) {
539 // Marker for start of the configuration macro
540 addConfig = new TMacro();
541 TString shortName = gSystem->BaseName(cfg->GetMacroName());
542 shortName = shortName(0,shortName.Index("."));
543 shortName += "Config";
544 addConfig->SetName(shortName);
545 shortName.Prepend("/");
546 shortName.Prepend(gSystem->DirName(cfg->GetMacroName()));
548 addConfig->SetTitle(shortName);
549 } else if (cfg && line.BeginsWith("#Module.EndMacro")) {
550 // Marker for the end of the embedded macro. EndMacro block mandatory.
551 if (cfg && addMacro) {
552 cfg->SetMacro(addMacro);
555 ::Error("ExtractModulesFrom", "Canot end un-started macro block");
557 } else if (cfg && line.BeginsWith("#Module.EndConfig")) {
558 // Marker for the end of the config macro. EndConfig block is mandatory
559 if (cfg && addConfig) {
560 addConfig->GetListOfLines()->AddFirst(new TObjString(Form("%s() {",gSystem->BaseName(addConfig->GetName()))));
561 addConfig->GetListOfLines()->AddLast(new TObjString("}"));
562 cfg->SetConfigMacro(addConfig);
565 ::Error("ExtractModulesFrom", "Canot end un-started config macro block");
568 // Reading a block line
569 if (addMacro) addMacro->AddLine(line);
570 else if (addConfig) addConfig->AddLine(line);
573 // Add last found object to the list
575 if (addMacro) ::Error("ExtractModulesFrom", "#Module.EndMacro block not found");
576 if (addConfig) ::Error("ExtractModulesFrom", "#Module.EndConfig block not found");
577 if (!array) array = new TObjArray();