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"
18 #include "Riostream.h"
22 #include "TObjArray.h"
24 // Author: Andrei Gheata, 12/08/2011
26 //==============================================================================
27 // AliAnalysysTaskCfg - Class embedding the configuration needed to run
28 // a given analysis task: libraries to be loaded, location and name of the macro
29 // used to add the task to the analysis manager, dependencies.
30 //==============================================================================
32 // This class is used to fully describe how to run a given analysis task. It
33 // requires that the user creates an AddTask macro for his task and defines:
34 // - The needed libs separated by commas,
35 // - The full path to the AddTask macro (starting with $ALICE_ROOT if needed)
36 // - The list of arguments to be provided to the AddTask macro. One can use
37 // here only constants that can be interpreted.
38 // - The list of dependencies (other modules required to run this task). These
39 // must be names of other AliAnalysisTaskCfg objects, separated by commas.
40 // - Data types supported by the task (e.g. ESD, AOD, MC)
41 // The class has normal ROOT IO, but it can also read from and write to text files.
43 // Content of file: QAsym.cfg
45 # Lines that do not start with #Module are ignored, except those in embedded
49 #Module.Deps PhysicsSelection
50 #Module.DataTypes ESD, AOD, MC
51 #Module.MacroName $ALICE_ROOT/PWG1/PilotTrain/AddTaskQAsym.C
52 #Module.MacroArgs 0, AliVEvent::kAnyINT, AliVEvent::kHighMult, AliVEvent::kEMC7, AliVEvent::kMUU7
54 __R_ADDTASK__->SelectCollisionCandidates();
57 // The following special variable names can be used:
58 // __R_ADDTASK__ = the return value of the AddTask macro included
59 // __R_ESDH__ = pointer to ESD handler
60 // __R_AODH__ = pointer to AOD handler
61 // __R_MCH__ = pointer to MC handler
62 // The static method ExtractModulesFrom(const char *filename) allows reading
63 // several task configuration modules from the same text file and returning
64 // them in a TObjArray.
66 // A list of configuration modules representing a train should be injected in
67 // the right order in the grid handler to generate train macros.
70 ClassImp(AliAnalysisTaskCfg)
72 //______________________________________________________________________________
73 AliAnalysisTaskCfg::AliAnalysisTaskCfg()
86 //______________________________________________________________________________
87 AliAnalysisTaskCfg::AliAnalysisTaskCfg(const char *name)
97 // Constructor. All configuration objects need to be named since they are looked
101 //______________________________________________________________________________
102 AliAnalysisTaskCfg::AliAnalysisTaskCfg(const AliAnalysisTaskCfg &other)
104 fMacroName(other.fMacroName),
105 fMacroArgs(other.fMacroArgs),
108 fDataTypes(other.fDataTypes),
113 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
114 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
117 //______________________________________________________________________________
118 AliAnalysisTaskCfg::~AliAnalysisTaskCfg()
125 //______________________________________________________________________________
126 AliAnalysisTaskCfg& AliAnalysisTaskCfg::operator=(const AliAnalysisTaskCfg &other)
128 // Assignment operator.
129 if (&other == this) return *this;
130 TNamed::operator=(other);
131 fMacroName = other.fMacroName;
132 fMacroArgs = other.fMacroArgs;
135 fDataTypes = other.fDataTypes;
136 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
137 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
141 //______________________________________________________________________________
142 TMacro *AliAnalysisTaskCfg::OpenMacro(const char *name)
144 // Opens the specified macro if name is not empty. In case of success updates
145 // fMacroName, creates the maco object and returns its pointer.
146 // Clean-up previous macro if any
152 if (strlen(name)) expname = gSystem->ExpandPathName(name);
153 else expname = gSystem->ExpandPathName(fMacroName);
154 if (expname.IsNull()) {
155 Error("OpenMacro", "Macro name not provided and not previously set");
158 if (gSystem->AccessPathName(expname)) {
159 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
162 if (strlen(name)) fMacroName = name;
163 fMacro = new TMacro(expname);
167 //______________________________________________________________________________
168 void AliAnalysisTaskCfg::SetMacro(TMacro *macro)
170 // Set the AddTask macro from outside. This will discard the existing macro if
171 // any. The provided macro will be owned by this object.
172 if (fMacro) delete fMacro;
176 //______________________________________________________________________________
177 Long64_t AliAnalysisTaskCfg::ExecuteMacro(const char *newargs)
179 // Execute AddTask macro. Opens first the macro pointed by fMacroName if not yet
180 // done. Checks if the requested libraries are loaded, else loads them. Executes
181 // with stored fMacroArgs unless new arguments are provided. The flag IsLoaded
182 // is set once the macro was successfully executed.
183 if (!fMacro && !OpenMacro()) {
184 Error("ExecuteMacro", "Cannot execute this macro");
187 if (!CheckLoadLibraries()) {
188 Error("ExecuteMacro", "Cannot load requested libraries: %s", fLibs.Data());
192 TString args = newargs;
193 if (args.IsNull()) args = fMacroArgs;
194 Long64_t retval = fMacro->Exec(args);
195 if (retval >=0) SetBit(AliAnalysisTaskCfg::kLoaded, kTRUE);
199 //______________________________________________________________________________
200 Int_t AliAnalysisTaskCfg::GetNlibs() const
202 // Returns number of requested libraries.
203 if (fLibs.IsNull()) return 0;
204 Int_t nlibs = fLibs.CountChar(',')+1;
208 //______________________________________________________________________________
209 const char *AliAnalysisTaskCfg::GetLibrary(Int_t i) const
211 // Returns library name for the i-th library.
212 Int_t nlibs = GetNlibs();
213 if (i>=nlibs) return 0;
215 TObjArray *list = fLibs.Tokenize(",");
216 libname = list->At(i)->GetName();
217 libname.ReplaceAll(".so","");
218 libname.ReplaceAll(" ","");
219 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
221 return libname.Data();
224 //______________________________________________________________________________
225 Bool_t AliAnalysisTaskCfg::CheckLoadLibraries() const
227 // Check if all requested libraries were loaded, otherwise load them. If some
228 // library cannot be loaded return false.
230 Int_t nlibs = GetNlibs();
231 for (Int_t i=0; i<nlibs; i++) {
232 library = GetLibrary(i);
233 library.Prepend("lib");
234 Int_t loaded = strlen(gSystem->GetLibraries(library,"",kFALSE));
235 if (!loaded) loaded = gSystem->Load(library);
237 Error("CheckLoadLibraries", "Cannot load library %s", library.Data());
244 //______________________________________________________________________________
245 Bool_t AliAnalysisTaskCfg::NeedsLibrary(const char *lib) const
247 // Check if a given library is needed by the module.
248 TString libname = lib;
249 libname.ReplaceAll(".so","");
250 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
251 return fLibs.Contains(libname);
254 //______________________________________________________________________________
255 Int_t AliAnalysisTaskCfg::GetNdeps() const
257 // Returns number of requested libraries.
258 if (fDeps.IsNull()) return 0;
259 Int_t ndeps = fDeps.CountChar(',')+1;
263 //______________________________________________________________________________
264 const char *AliAnalysisTaskCfg::GetDependency(Int_t i) const
266 // Returns library name for the i-th library.
267 Int_t ndeps = GetNdeps();
268 if (i>=ndeps) return 0;
270 TObjArray *list = fDeps.Tokenize(",");
271 depname = list->At(i)->GetName();
272 depname.ReplaceAll(" ","");
274 return depname.Data();
277 //______________________________________________________________________________
278 Bool_t AliAnalysisTaskCfg::NeedsDependency(const char *dep) const
280 // Check if a given library is needed by the module.
281 return fDeps.Contains(dep);
284 //______________________________________________________________________________
285 TMacro *AliAnalysisTaskCfg::OpenConfigMacro(const char *name)
287 // Opens the specified macro if name is not empty.
293 TString expname = gSystem->ExpandPathName(name);
294 if (expname.IsNull()) {
295 Error("OpenConfigMacro", "Macro name not provided");
298 if (gSystem->AccessPathName(expname)) {
299 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
302 fConfigDeps = new TMacro(expname);
306 //______________________________________________________________________________
307 void AliAnalysisTaskCfg::SetConfigMacro(TMacro *macro)
309 // Set the macro for configuring deps from outside. This will discard the
310 // existing macro if any. The provided macro will be owned by this object.
311 if (fConfigDeps) delete fConfigDeps;
315 //______________________________________________________________________________
316 Long64_t AliAnalysisTaskCfg::ExecuteConfigMacro()
318 // Execute macro to configure dependencies. No arguments are supported.
320 Error("ExecuteConfigMacro", "Call OpenConfigMacro() first");
323 if (!CheckLoadLibraries()) {
324 Error("ExecuteConfigMacro", "Cannot load requested libraries: %s", fLibs.Data());
327 return fConfigDeps->Exec();
330 //______________________________________________________________________________
331 void AliAnalysisTaskCfg::SetDataTypes(const char *types)
333 // Sets the data types supported by the module. Stored in upper case.
335 fDataTypes.ToUpper();
338 //______________________________________________________________________________
339 Bool_t AliAnalysisTaskCfg::SupportsData(const char *type) const
341 // Checks if the given data type is supported.
342 TString stype = type;
344 return fDataTypes.Contains(stype);
347 //______________________________________________________________________________
348 void AliAnalysisTaskCfg::Print(Option_t * option) const
350 // Print content of the module.
352 Bool_t full = (opt.Length())?kTRUE:kFALSE;
353 printf("====================================================================\n");
354 printf("# Analysis task: %s\n", GetName());
355 printf("# Supported data types: %s\n", fDataTypes.Data());
356 printf("# Extra libraries: %s\n", fLibs.Data());
357 printf("# Extra dependencies: %s\n", fDeps.Data());
359 printf("# Macro to configure deps: %s\n", fConfigDeps->GetTitle());
360 if (full) fConfigDeps->Print();
362 printf("# Macro connecting this task: %s\n", fMacroName.Data());
363 printf("# Arguments to run the macro: %s\n", fMacroArgs.Data());
365 if (fMacro) fMacro->Print();
367 TMacro macro(gSystem->ExpandPathName(fMacroName));
373 //______________________________________________________________________________
374 void AliAnalysisTaskCfg::SaveAs(const char *filename, Option_t *option) const
376 // Save the configuration module as text file in the form key:value. The
377 // option can be APPEND, otherwise the file gets overwritten.
380 ios::openmode mode = ios::out;
381 if (opt == "APPEND") mode = ios::app;
383 out.open(filename, mode);
385 Error("SaveAs", "Bad file name: %s", filename);
388 out << "#Module.Begin " << GetName() << endl;
389 out << "#Module.Libs " << fLibs << endl;
390 out << "#Module.Deps " << fDeps << endl;
391 out << "#Module.DataTypes " << fDataTypes << endl;
392 out << "#Module.MacroName " << fMacroName << endl;
393 out << "#Module.MacroArgs " << fMacroArgs << endl;
395 out << "#Config.Deps " << fConfigDeps->GetTitle() << endl;
400 //______________________________________________________________________________
401 const char *AliAnalysisTaskCfg::DecodeValue(TString &line)
403 // Decode the value string from the line
404 TString value = line(line.Index(' '),line.Length());
405 value = value.Strip(TString::kLeading,' ');
406 value = value.Strip(TString::kTrailing,' ');
410 //______________________________________________________________________________
411 TObjArray *AliAnalysisTaskCfg::ExtractModulesFrom(const char *filename)
413 // Read all modules from a text file and add them to an object array. The
414 // caller must delete the array at the end. Any module must start with a line
415 // containing: #Module.Begin
416 TString expname = gSystem->ExpandPathName(filename);
417 if (gSystem->AccessPathName(expname)) {
418 ::Error("ExtractModulesFrom", "Cannot open file %s", filename);
421 AliAnalysisTaskCfg *cfg = 0;
422 TObjArray *array = 0;
428 TMacro *addMacro = 0;
429 TMacro *addConfig = 0;
431 in.getline(cline,1024);
433 if (line.BeginsWith("#Module.Begin")) {
434 // New module found, save previous if any
436 if (addMacro) cfg->SetMacro(addMacro);
437 if (addConfig) cfg->SetConfigMacro(addConfig);
438 if (!array) array = new TObjArray();
441 // Decode module name from the line
442 decode = AliAnalysisTaskCfg::DecodeValue(line);
443 cfg = new AliAnalysisTaskCfg(decode);
446 } else if (cfg && line.BeginsWith("#Module.Libs")) {
448 decode = AliAnalysisTaskCfg::DecodeValue(line);
449 cfg->SetLibraries(decode);
450 } else if (cfg && line.BeginsWith("#Module.Deps")) {
451 // Dependencies section
452 decode = AliAnalysisTaskCfg::DecodeValue(line);
453 cfg->SetDependencies(decode);
454 } else if (cfg && line.BeginsWith("#Module.DataTypes")) {
456 decode = AliAnalysisTaskCfg::DecodeValue(line);
457 cfg->SetDataTypes(decode);
458 } else if (cfg && line.BeginsWith("#Module.MacroName")) {
459 // Name of the add task macro (including path)
460 decode = AliAnalysisTaskCfg::DecodeValue(line);
461 cfg->SetMacroName(decode);
462 } else if (cfg && line.BeginsWith("#Module.MacroArgs")) {
463 // Arguments for the AddTask macro
464 decode = AliAnalysisTaskCfg::DecodeValue(line);
465 cfg->SetMacroArgs(decode);
466 } else if (cfg && line.BeginsWith("#Module.StartMacro")) {
467 // Marker for start of the AddTask macro
468 addMacro = new TMacro();
469 TString shortName = gSystem->BaseName(cfg->GetMacroName());
470 shortName = shortName(0,shortName.Index("."));
471 addMacro->SetName(shortName);
472 addMacro->SetTitle(cfg->GetMacroName());
473 } else if (cfg && line.BeginsWith("#Module.StartConfig")) {
474 // Marker for start of the configuration macro
475 addConfig = new TMacro();
476 TString shortName = gSystem->BaseName(cfg->GetMacroName());
477 shortName = shortName(0,shortName.Index("."));
478 shortName += "Config";
479 addConfig->SetName(shortName);
480 shortName.Prepend("/");
481 shortName.Prepend(gSystem->DirName(cfg->GetMacroName()));
483 addConfig->SetTitle(shortName);
484 } else if (cfg && line.BeginsWith("#Module.EndMacro")) {
485 // Marker for the end of the embedded macro. EndMacro block mandatory.
486 if (cfg && addMacro) {
487 cfg->SetMacro(addMacro);
490 ::Error("ExtractModulesFrom", "Canot end un-started macro block");
492 } else if (cfg && line.BeginsWith("#Module.EndConfig")) {
493 // Marker for the end of the config macro. EndConfig block is mandatory
494 if (cfg && addConfig) {
495 cfg->SetConfigMacro(addConfig);
498 ::Error("ExtractModulesFrom", "Canot end un-started config macro block");
501 // Reading a block line
502 if (addMacro) addMacro->AddLine(line);
503 else if (addConfig) addConfig->AddLine(line);
506 // Add last found object to the list
508 if (addMacro) cfg->SetMacro(addMacro);
509 if (addConfig) cfg->SetConfigMacro(addConfig);
510 if (!array) array = new TObjArray();