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.
182 if (!fMacro && !OpenMacro()) {
183 Error("ExecuteMacro", "Cannot execute this macro");
186 if (!CheckLoadLibraries()) {
187 Error("ExecuteMacro", "Cannot load requested libraries: %s", fLibs.Data());
191 TString args = newargs;
192 if (args.IsNull()) args = fMacroArgs;
193 return fMacro->Exec(args);
196 //______________________________________________________________________________
197 Int_t AliAnalysisTaskCfg::GetNlibs() const
199 // Returns number of requested libraries.
200 if (fLibs.IsNull()) return 0;
201 Int_t nlibs = fLibs.CountChar(',')+1;
205 //______________________________________________________________________________
206 const char *AliAnalysisTaskCfg::GetLibrary(Int_t i) const
208 // Returns library name for the i-th library.
209 Int_t nlibs = GetNlibs();
210 if (i>=nlibs) return 0;
212 TObjArray *list = fLibs.Tokenize(",");
213 libname = list->At(i)->GetName();
214 libname.ReplaceAll(".so","");
215 libname.ReplaceAll(" ","");
216 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
218 return libname.Data();
221 //______________________________________________________________________________
222 Bool_t AliAnalysisTaskCfg::CheckLoadLibraries() const
224 // Check if all requested libraries were loaded, otherwise load them. If some
225 // library cannot be loaded return false.
227 Int_t nlibs = GetNlibs();
228 for (Int_t i=0; i<nlibs; i++) {
229 library = GetLibrary(i);
230 library.Prepend("lib");
231 Int_t loaded = strlen(gSystem->GetLibraries(library,"",kFALSE));
232 if (!loaded) loaded = gSystem->Load(library);
234 Error("CheckLoadLibraries", "Cannot load library %s", library.Data());
241 //______________________________________________________________________________
242 Bool_t AliAnalysisTaskCfg::NeedsLibrary(const char *lib) const
244 // Check if a given library is needed by the module.
245 TString libname = lib;
246 libname.ReplaceAll(".so","");
247 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
248 return fLibs.Contains(libname);
251 //______________________________________________________________________________
252 Int_t AliAnalysisTaskCfg::GetNdeps() const
254 // Returns number of requested libraries.
255 if (fDeps.IsNull()) return 0;
256 Int_t ndeps = fDeps.CountChar(',')+1;
260 //______________________________________________________________________________
261 const char *AliAnalysisTaskCfg::GetDependency(Int_t i) const
263 // Returns library name for the i-th library.
264 Int_t ndeps = GetNdeps();
265 if (i>=ndeps) return 0;
267 TObjArray *list = fDeps.Tokenize(",");
268 depname = list->At(i)->GetName();
269 depname.ReplaceAll(" ","");
271 return depname.Data();
274 //______________________________________________________________________________
275 Bool_t AliAnalysisTaskCfg::NeedsDependency(const char *dep) const
277 // Check if a given library is needed by the module.
278 return fDeps.Contains(dep);
281 //______________________________________________________________________________
282 TMacro *AliAnalysisTaskCfg::OpenConfigMacro(const char *name)
284 // Opens the specified macro if name is not empty.
290 TString expname = gSystem->ExpandPathName(name);
291 if (expname.IsNull()) {
292 Error("OpenConfigMacro", "Macro name not provided");
295 if (gSystem->AccessPathName(expname)) {
296 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
299 fConfigDeps = new TMacro(expname);
303 //______________________________________________________________________________
304 void AliAnalysisTaskCfg::SetConfigMacro(TMacro *macro)
306 // Set the macro for configuring deps from outside. This will discard the
307 // existing macro if any. The provided macro will be owned by this object.
308 if (fConfigDeps) delete fConfigDeps;
312 //______________________________________________________________________________
313 Long64_t AliAnalysisTaskCfg::ExecuteConfigMacro()
315 // Execute macro to configure dependencies. No arguments are supported.
317 Error("ExecuteConfigMacro", "Call OpenConfigMacro() first");
320 if (!CheckLoadLibraries()) {
321 Error("ExecuteConfigMacro", "Cannot load requested libraries: %s", fLibs.Data());
324 return fConfigDeps->Exec();
327 //______________________________________________________________________________
328 void AliAnalysisTaskCfg::SetDataTypes(const char *types)
330 // Sets the data types supported by the module. Stored in upper case.
332 fDataTypes.ToUpper();
335 //______________________________________________________________________________
336 Bool_t AliAnalysisTaskCfg::SupportsData(const char *type) const
338 // Checks if the given data type is supported.
339 TString stype = type;
341 return fDataTypes.Contains(stype);
344 //______________________________________________________________________________
345 void AliAnalysisTaskCfg::Print(Option_t * option) const
347 // Print content of the module.
349 Bool_t full = (opt.Length())?kTRUE:kFALSE;
350 printf("====================================================================\n");
351 printf("# Analysis task: %s\n", GetName());
352 printf("# Supported data types: %s\n", fDataTypes.Data());
353 printf("# Extra libraries: %s\n", fLibs.Data());
354 printf("# Extra dependencies: %s\n", fDeps.Data());
356 printf("# Macro to configure deps: %s\n", fConfigDeps->GetTitle());
357 if (full) fConfigDeps->Print();
359 printf("# Macro connecting this task: %s\n", fMacroName.Data());
360 printf("# Arguments to run the macro: %s\n", fMacroArgs.Data());
362 if (fMacro) fMacro->Print();
364 TMacro macro(gSystem->ExpandPathName(fMacroName));
370 //______________________________________________________________________________
371 void AliAnalysisTaskCfg::SaveAs(const char *filename, Option_t *option) const
373 // Save the configuration module as text file in the form key:value. The
374 // option can be APPEND, otherwise the file gets overwritten.
377 ios::openmode mode = ios::out;
378 if (opt == "APPEND") mode = ios::app;
380 out.open(filename, mode);
382 Error("SaveAs", "Bad file name: %s", filename);
385 out << "#Module.Begin " << GetName() << endl;
386 out << "#Module.Libs " << fLibs << endl;
387 out << "#Module.Deps " << fDeps << endl;
388 out << "#Module.DataTypes " << fDataTypes << endl;
389 out << "#Module.MacroName " << fMacroName << endl;
390 out << "#Module.MacroArgs " << fMacroArgs << endl;
392 out << "#Config.Deps " << fConfigDeps->GetTitle() << endl;
397 //______________________________________________________________________________
398 const char *AliAnalysisTaskCfg::DecodeValue(TString &line)
400 // Decode the value string from the line
401 TString value = line(line.Index(' '),line.Length());
402 value = value.Strip(TString::kLeading,' ');
403 value = value.Strip(TString::kTrailing,' ');
407 //______________________________________________________________________________
408 TObjArray *AliAnalysisTaskCfg::ExtractModulesFrom(const char *filename)
410 // Read all modules from a text file and add them to an object array. The
411 // caller must delete the array at the end. Any module must start with a line
412 // containing: #Module.Begin
413 TString expname = gSystem->ExpandPathName(filename);
414 if (gSystem->AccessPathName(expname)) {
415 ::Error("ExtractModulesFrom", "Cannot open file %s", filename);
418 AliAnalysisTaskCfg *cfg = 0;
419 TObjArray *array = 0;
425 TMacro *addMacro = 0;
426 TMacro *addConfig = 0;
428 in.getline(cline,1024);
430 if (line.BeginsWith("#Module.Begin")) {
431 // New module found, save previous if any
433 if (addMacro) cfg->SetMacro(addMacro);
434 if (addConfig) cfg->SetConfigMacro(addConfig);
435 if (!array) array = new TObjArray();
438 // Decode module name from the line
439 decode = AliAnalysisTaskCfg::DecodeValue(line);
440 cfg = new AliAnalysisTaskCfg(decode);
443 } else if (cfg && line.BeginsWith("#Module.Libs")) {
445 decode = AliAnalysisTaskCfg::DecodeValue(line);
446 cfg->SetLibraries(decode);
447 } else if (cfg && line.BeginsWith("#Module.Deps")) {
448 // Dependencies section
449 decode = AliAnalysisTaskCfg::DecodeValue(line);
450 cfg->SetDependencies(decode);
451 } else if (cfg && line.BeginsWith("#Module.DataTypes")) {
453 decode = AliAnalysisTaskCfg::DecodeValue(line);
454 cfg->SetDataTypes(decode);
455 } else if (cfg && line.BeginsWith("#Module.MacroName")) {
456 // Name of the add task macro (including path)
457 decode = AliAnalysisTaskCfg::DecodeValue(line);
458 cfg->SetMacroName(decode);
459 } else if (cfg && line.BeginsWith("#Module.MacroArgs")) {
460 // Arguments for the AddTask macro
461 decode = AliAnalysisTaskCfg::DecodeValue(line);
462 cfg->SetMacroArgs(decode);
463 } else if (cfg && line.BeginsWith("#Module.StartMacro")) {
464 // Marker for start of the AddTask macro
465 addMacro = new TMacro();
466 TString shortName = gSystem->BaseName(cfg->GetMacroName());
467 shortName = shortName(0,shortName.Index("."));
468 addMacro->SetName(shortName);
469 addMacro->SetTitle(cfg->GetMacroName());
470 } else if (cfg && line.BeginsWith("#Module.StartConfig")) {
471 // Marker for start of the configuration macro
472 addConfig = new TMacro();
473 TString shortName = gSystem->BaseName(cfg->GetMacroName());
474 shortName = shortName(0,shortName.Index("."));
475 shortName += "Config";
476 addConfig->SetName(shortName);
477 shortName.Prepend("/");
478 shortName.Prepend(gSystem->DirName(cfg->GetMacroName()));
480 addConfig->SetTitle(shortName);
481 } else if (cfg && line.BeginsWith("#Module.EndMacro")) {
482 // Marker for the end of the embedded macro. EndMacro block mandatory.
483 if (cfg && addMacro) {
484 cfg->SetMacro(addMacro);
487 ::Error("ExtractModulesFrom", "Canot end un-started macro block");
489 } else if (cfg && line.BeginsWith("#Module.EndConfig")) {
490 // Marker for the end of the config macro. EndConfig block is mandatory
491 if (cfg && addConfig) {
492 cfg->SetConfigMacro(addConfig);
495 ::Error("ExtractModulesFrom", "Canot end un-started config macro block");
498 // Reading a block line
499 if (addMacro) addMacro->AddLine(line);
500 else if (addConfig) addConfig->AddLine(line);
503 // Add last found object to the list
505 if (addMacro) cfg->SetMacro(addMacro);
506 if (addConfig) cfg->SetConfigMacro(addConfig);
507 if (!array) array = new TObjArray();