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/PWG1/PilotTrain/AddTaskQAsym.C
56 #Module.MacroArgs 0, AliVEvent::kAnyINT, AliVEvent::kHighMult, AliVEvent::kEMC7, AliVEvent::kMUU7
58 __R_ADDTASK__->SelectCollisionCandidates();
61 // The following special variable names can be used:
62 // __R_ADDTASK__ = the return value of the AddTask macro included
63 // __R_ESDH__ = pointer to ESD handler
64 // __R_AODH__ = pointer to AOD handler
65 // __R_MCH__ = pointer to MC handler
66 // The static method ExtractModulesFrom(const char *filename) allows reading
67 // several task configuration modules from the same text file and returning
68 // them in a TObjArray.
70 // A list of configuration modules representing a train should be injected in
71 // the right order in the grid handler to generate train macros.
74 ClassImp(AliAnalysisTaskCfg)
76 //______________________________________________________________________________
77 AliAnalysisTaskCfg::AliAnalysisTaskCfg()
91 //______________________________________________________________________________
92 AliAnalysisTaskCfg::AliAnalysisTaskCfg(const char *name)
103 // Constructor. All configuration objects need to be named since they are looked
107 //______________________________________________________________________________
108 AliAnalysisTaskCfg::AliAnalysisTaskCfg(const AliAnalysisTaskCfg &other)
110 fMacroName(other.fMacroName),
111 fMacroArgs(other.fMacroArgs),
114 fDataTypes(other.fDataTypes),
120 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
121 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
124 //______________________________________________________________________________
125 AliAnalysisTaskCfg::~AliAnalysisTaskCfg()
132 //______________________________________________________________________________
133 AliAnalysisTaskCfg& AliAnalysisTaskCfg::operator=(const AliAnalysisTaskCfg &other)
135 // Assignment operator.
136 if (&other == this) return *this;
137 TNamed::operator=(other);
138 fMacroName = other.fMacroName;
139 fMacroArgs = other.fMacroArgs;
142 fDataTypes = other.fDataTypes;
143 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
144 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
145 fRAddTask = other.fRAddTask;
149 //______________________________________________________________________________
150 TMacro *AliAnalysisTaskCfg::OpenMacro(const char *name)
152 // Opens the specified macro if name is not empty. In case of success updates
153 // fMacroName, creates the maco object and returns its pointer.
154 // Clean-up previous macro if any
160 if (strlen(name)) expname = gSystem->ExpandPathName(name);
161 else expname = gSystem->ExpandPathName(fMacroName.Data());
162 if (expname.IsNull()) {
163 Error("OpenMacro", "Macro name not provided and not previously set");
166 if (gSystem->AccessPathName(expname)) {
167 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
170 if (strlen(name)) fMacroName = name;
171 fMacro = new TMacro(expname);
175 //______________________________________________________________________________
176 void AliAnalysisTaskCfg::SetMacro(TMacro *macro)
178 // Set the AddTask macro from outside. This will discard the existing macro if
179 // any. The provided macro will be owned by this object.
180 if (fMacro) delete fMacro;
184 //______________________________________________________________________________
185 Long64_t AliAnalysisTaskCfg::ExecuteMacro(const char *newargs)
187 // Execute AddTask macro. Opens first the macro pointed by fMacroName if not yet
188 // done. Checks if the requested libraries are loaded, else loads them. Executes
189 // with stored fMacroArgs unless new arguments are provided. The flag IsLoaded
190 // is set once the macro was successfully executed.
191 if (IsLoaded()) return kTRUE;
192 if (!fMacro && !OpenMacro()) {
193 Error("ExecuteMacro", "Cannot execute this macro");
196 if (!CheckLoadLibraries()) {
197 Error("ExecuteMacro", "Cannot load requested libraries: %s", fLibs.Data());
201 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
203 Error("ExecuteMacro", "Analysis manager not defined yet");
206 Int_t ntasks0 = mgr->GetTasks()->GetEntriesFast();
207 TString args = newargs;
208 if (args.IsNull()) args = fMacroArgs;
210 Long64_t retval = fMacro->Exec(args, &error);
211 if (error != TInterpreter::kNoError)
213 Error("ExecuteMacro", "Macro interpretation failed");
216 Int_t ntasks = mgr->GetTasks()->GetEntriesFast();
219 Error("ExecuteMacro", "The macro did not add any tasks to the manager");
222 Long64_t ptrTask = (Long64_t)mgr->GetTasks()->At(ntasks0);
223 if (retval >= ptrTask) {
224 TObject::SetBit(AliAnalysisTaskCfg::kLoaded, kTRUE);
225 fRAddTask = reinterpret_cast<TObject*>(retval);
226 if (fConfigDeps && dynamic_cast<TObject*>(fRAddTask)) {
227 TString classname = fRAddTask->ClassName();
228 classname += Form("* __R_ADDTASK__ = (%s*)0x%lx;", classname.Data(),(ULong_t)retval);
229 classname.Prepend(" ");
230 TObjString *line = fConfigDeps->GetLineWith("__R_ADDTASK__");
232 TList *lines = fConfigDeps->GetListOfLines();
233 lines->AddBefore(line, new TObjString(classname));
237 Info("ExecuteMacro", "Macro %s added %d tasks to the manager", fMacro->GetName(), ntasks-ntasks0);
241 //______________________________________________________________________________
242 Int_t AliAnalysisTaskCfg::GetNlibs() const
244 // Returns number of requested libraries.
245 if (fLibs.IsNull()) return 0;
246 Int_t nlibs = fLibs.CountChar(',')+1;
250 //______________________________________________________________________________
251 const char *AliAnalysisTaskCfg::GetLibrary(Int_t i) const
253 // Returns library name for the i-th library.
254 Int_t nlibs = GetNlibs();
255 if (i>=nlibs) return 0;
256 static TString libname;
257 TObjArray *list = fLibs.Tokenize(",");
258 libname = list->At(i)->GetName();
259 libname.ReplaceAll(".so","");
260 libname.ReplaceAll(" ","");
261 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
263 return libname.Data();
266 //______________________________________________________________________________
267 Bool_t AliAnalysisTaskCfg::CheckLoadLibraries() const
269 // Check if all requested libraries were loaded, otherwise load them. If some
270 // library cannot be loaded return false.
272 Int_t nlibs = GetNlibs();
273 for (Int_t i=0; i<nlibs; i++) {
274 library = GetLibrary(i);
275 library.Prepend("lib");
276 Int_t loaded = strlen(gSystem->GetLibraries(library,"",kFALSE));
277 if (!loaded) loaded = gSystem->Load(library);
279 Error("CheckLoadLibraries", "Cannot load library %s", library.Data());
286 //______________________________________________________________________________
287 Bool_t AliAnalysisTaskCfg::NeedsLibrary(const char *lib) const
289 // Check if a given library is needed by the module.
290 TString libname = lib;
291 libname.ReplaceAll(".so","");
292 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
293 return fLibs.Contains(libname);
296 //______________________________________________________________________________
297 Int_t AliAnalysisTaskCfg::GetNdeps() const
299 // Returns number of requested libraries.
300 if (fDeps.IsNull()) return 0;
301 Int_t ndeps = fDeps.CountChar(',')+1;
305 //______________________________________________________________________________
306 const char *AliAnalysisTaskCfg::GetDependency(Int_t i) const
308 // Returns library name for the i-th library.
309 Int_t ndeps = GetNdeps();
310 if (i>=ndeps) return 0;
311 static TString depname;
312 TObjArray *list = fDeps.Tokenize(",");
313 depname = list->At(i)->GetName();
314 depname.ReplaceAll(" ","");
316 return depname.Data();
319 //______________________________________________________________________________
320 Bool_t AliAnalysisTaskCfg::NeedsDependency(const char *dep) const
322 // Check if a given library is needed by the module.
323 return fDeps.Contains(dep);
326 //______________________________________________________________________________
327 TMacro *AliAnalysisTaskCfg::OpenConfigMacro(const char *name)
329 // Opens the specified macro if name is not empty.
335 TString expname = gSystem->ExpandPathName(name);
336 if (expname.IsNull()) {
337 Error("OpenConfigMacro", "Macro name not provided");
340 if (gSystem->AccessPathName(expname)) {
341 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
344 fConfigDeps = new TMacro(expname);
348 //______________________________________________________________________________
349 void AliAnalysisTaskCfg::SetConfigMacro(TMacro *macro)
351 // Set the macro for configuring deps from outside. This will discard the
352 // existing macro if any. The provided macro will be owned by this object.
353 if (fConfigDeps) delete fConfigDeps;
357 //______________________________________________________________________________
358 Long64_t AliAnalysisTaskCfg::ExecuteConfigMacro()
360 // Execute macro to configure dependencies. No arguments are supported.
362 Error("ExecuteConfigMacro", "Call OpenConfigMacro() first");
365 if (!CheckLoadLibraries()) {
366 Error("ExecuteConfigMacro", "Cannot load requested libraries: %s", fLibs.Data());
369 return fConfigDeps->Exec();
372 //______________________________________________________________________________
373 void AliAnalysisTaskCfg::SetDataTypes(const char *types)
375 // Sets the data types supported by the module. Stored in upper case.
377 fDataTypes.ToUpper();
380 //______________________________________________________________________________
381 Bool_t AliAnalysisTaskCfg::SupportsData(const char *type) const
383 // Checks if the given data type is supported.
384 TString stype = type;
386 return fDataTypes.Contains(stype);
389 //______________________________________________________________________________
390 void AliAnalysisTaskCfg::Print(Option_t * option) const
392 // Print content of the module.
394 Bool_t full = (opt.Length())?kTRUE:kFALSE;
395 printf("====================================================================\n");
396 printf("# Analysis task: %s\n", GetName());
397 printf("# Supported data types: %s\n", fDataTypes.Data());
398 printf("# Extra libraries: %s\n", fLibs.Data());
399 printf("# Extra dependencies: %s\n", fDeps.Data());
401 printf("# Macro to configure deps: %s\n", fConfigDeps->GetTitle());
402 if (full) fConfigDeps->Print();
404 printf("# Macro connecting this task: %s\n", fMacroName.Data());
405 printf("# Arguments to run the macro: %s\n", fMacroArgs.Data());
407 if (fMacro) fMacro->Print();
409 TMacro macro(gSystem->ExpandPathName(fMacroName.Data()));
415 //______________________________________________________________________________
416 void AliAnalysisTaskCfg::SaveAs(const char *filename, Option_t *option) const
418 // Save the configuration module as text file in the form key:value. The
419 // option can be APPEND, otherwise the file gets overwritten.
422 ios::openmode mode = ios::out;
423 if (opt == "APPEND") mode = ios::app;
425 out.open(filename, mode);
427 Error("SaveAs", "Bad file name: %s", filename);
430 out << "#Module.Begin " << GetName() << endl;
431 out << "#Module.Libs " << fLibs << endl;
432 out << "#Module.Deps " << fDeps << endl;
433 out << "#Module.DataTypes " << fDataTypes << endl;
434 out << "#Module.MacroName " << fMacroName << endl;
435 out << "#Module.MacroArgs " << fMacroArgs << endl;
437 out << "#Config.Deps " << fConfigDeps->GetTitle() << endl;
442 //______________________________________________________________________________
443 const char *AliAnalysisTaskCfg::DecodeValue(TString &line)
445 // Decode the value string from the line
446 static TString value;
447 value = line(line.Index(' '),line.Length());
448 value = value.Strip(TString::kLeading,' ');
449 value = value.Strip(TString::kTrailing,' ');
453 //______________________________________________________________________________
454 TObjArray *AliAnalysisTaskCfg::ExtractModulesFrom(const char *filename)
456 // Read all modules from a text file and add them to an object array. The
457 // caller must delete the array at the end. Any module must start with a line
458 // containing: #Module.Begin
459 TString expname = gSystem->ExpandPathName(filename);
460 if (gSystem->AccessPathName(expname)) {
461 ::Error("ExtractModulesFrom", "Cannot open file %s", filename);
464 AliAnalysisTaskCfg *cfg = 0;
465 TObjArray *array = 0;
471 TMacro *addMacro = 0;
472 TMacro *addConfig = 0;
474 in.getline(cline,1024);
476 if (line.BeginsWith("#Module.Begin")) {
477 // New module found, save previous if any
479 if (addMacro) cfg->SetMacro(addMacro);
480 if (addConfig) cfg->SetConfigMacro(addConfig);
481 if (!array) array = new TObjArray();
484 // Decode module name from the line
485 decode = AliAnalysisTaskCfg::DecodeValue(line);
486 cfg = new AliAnalysisTaskCfg(decode);
489 } else if (cfg && line.BeginsWith("#Module.Libs")) {
491 decode = AliAnalysisTaskCfg::DecodeValue(line);
492 cfg->SetLibraries(decode);
493 } else if (cfg && line.BeginsWith("#Module.Deps")) {
494 // Dependencies section
495 decode = AliAnalysisTaskCfg::DecodeValue(line);
496 cfg->SetDependencies(decode);
497 } else if (cfg && line.BeginsWith("#Module.DataTypes")) {
499 decode = AliAnalysisTaskCfg::DecodeValue(line);
500 cfg->SetDataTypes(decode);
501 } else if (cfg && line.BeginsWith("#Module.MacroName")) {
502 // Name of the add task macro (including path)
503 decode = AliAnalysisTaskCfg::DecodeValue(line);
504 cfg->SetMacroName(decode);
505 } else if (cfg && line.BeginsWith("#Module.MacroArgs")) {
506 // Arguments for the AddTask macro
507 decode = AliAnalysisTaskCfg::DecodeValue(line);
508 cfg->SetMacroArgs(decode);
509 } else if (cfg && line.BeginsWith("#Module.StartMacro")) {
510 // Marker for start of the AddTask macro
511 addMacro = new TMacro();
512 TString shortName = gSystem->BaseName(cfg->GetMacroName());
513 shortName = shortName(0,shortName.Index("."));
514 addMacro->SetName(shortName);
515 addMacro->SetTitle(cfg->GetMacroName());
516 } else if (cfg && line.BeginsWith("#Module.StartConfig")) {
517 // Marker for start of the configuration macro
518 addConfig = new TMacro();
519 TString shortName = gSystem->BaseName(cfg->GetMacroName());
520 shortName = shortName(0,shortName.Index("."));
521 shortName += "Config";
522 addConfig->SetName(shortName);
523 shortName.Prepend("/");
524 shortName.Prepend(gSystem->DirName(cfg->GetMacroName()));
526 addConfig->SetTitle(shortName);
527 } else if (cfg && line.BeginsWith("#Module.EndMacro")) {
528 // Marker for the end of the embedded macro. EndMacro block mandatory.
529 if (cfg && addMacro) {
530 cfg->SetMacro(addMacro);
533 ::Error("ExtractModulesFrom", "Canot end un-started macro block");
535 } else if (cfg && line.BeginsWith("#Module.EndConfig")) {
536 // Marker for the end of the config macro. EndConfig block is mandatory
537 if (cfg && addConfig) {
538 addConfig->GetListOfLines()->AddFirst(new TObjString(Form("%s() {",gSystem->BaseName(addConfig->GetName()))));
539 addConfig->GetListOfLines()->AddLast(new TObjString("}"));
540 cfg->SetConfigMacro(addConfig);
543 ::Error("ExtractModulesFrom", "Canot end un-started config macro block");
546 // Reading a block line
547 if (addMacro) addMacro->AddLine(line);
548 else if (addConfig) addConfig->AddLine(line);
551 // Add last found object to the list
553 if (addMacro) ::Error("ExtractModulesFrom", "#Module.EndMacro block not found");
554 if (addConfig) ::Error("ExtractModulesFrom", "#Module.EndConfig block not found");
555 if (!array) array = new TObjArray();