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.
81 ClassImp(AliAnalysisTaskCfg)
83 //______________________________________________________________________________
84 AliAnalysisTaskCfg::AliAnalysisTaskCfg()
100 //______________________________________________________________________________
101 AliAnalysisTaskCfg::AliAnalysisTaskCfg(const char *name)
114 // Constructor. All configuration objects need to be named since they are looked
118 //______________________________________________________________________________
119 AliAnalysisTaskCfg::AliAnalysisTaskCfg(const AliAnalysisTaskCfg &other)
121 fMacroName(other.fMacroName),
122 fMacroArgs(other.fMacroArgs),
125 fDataTypes(other.fDataTypes),
126 fOutputFile(other.fOutputFile),
127 fTerminateFile(other.fTerminateFile),
133 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
134 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
137 //______________________________________________________________________________
138 AliAnalysisTaskCfg::~AliAnalysisTaskCfg()
145 //______________________________________________________________________________
146 AliAnalysisTaskCfg& AliAnalysisTaskCfg::operator=(const AliAnalysisTaskCfg &other)
148 // Assignment operator.
149 if (&other == this) return *this;
150 TNamed::operator=(other);
151 fMacroName = other.fMacroName;
152 fMacroArgs = other.fMacroArgs;
155 fDataTypes = other.fDataTypes;
156 fOutputFile = other.fOutputFile;
157 fTerminateFile = other.fTerminateFile;
158 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
159 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
160 fRAddTask = other.fRAddTask;
164 //______________________________________________________________________________
165 TMacro *AliAnalysisTaskCfg::OpenMacro(const char *name)
167 // Opens the specified macro if name is not empty. In case of success updates
168 // fMacroName, creates the maco object and returns its pointer.
169 // Clean-up previous macro if any
175 if (strlen(name)) expname = gSystem->ExpandPathName(name);
176 else expname = gSystem->ExpandPathName(fMacroName.Data());
177 if (expname.IsNull()) {
178 Error("OpenMacro", "Macro name not provided and not previously set");
181 if (gSystem->AccessPathName(expname)) {
182 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
185 if (strlen(name)) fMacroName = name;
186 fMacro = new TMacro(expname);
190 //______________________________________________________________________________
191 void AliAnalysisTaskCfg::SetMacro(TMacro *macro)
193 // Set the AddTask macro from outside. This will discard the existing macro if
194 // any. The provided macro will be owned by this object.
195 if (fMacro) delete fMacro;
199 //______________________________________________________________________________
200 Long64_t AliAnalysisTaskCfg::ExecuteMacro(const char *newargs)
202 // Execute AddTask macro. Opens first the macro pointed by fMacroName if not yet
203 // done. Checks if the requested libraries are loaded, else loads them. Executes
204 // with stored fMacroArgs unless new arguments are provided. The flag IsLoaded
205 // is set once the macro was successfully executed.
206 if (IsLoaded()) return kTRUE;
207 if (!fMacro && !OpenMacro()) {
208 Error("ExecuteMacro", "Cannot execute this macro");
211 if (!CheckLoadLibraries()) {
212 Error("ExecuteMacro", "Cannot load requested libraries: %s", fLibs.Data());
216 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
218 Error("ExecuteMacro", "Analysis manager not defined yet");
221 Int_t ntasks0 = mgr->GetTasks()->GetEntriesFast();
222 TString args = newargs;
223 if (args.IsNull()) args = fMacroArgs;
225 Long64_t retval = fMacro->Exec(args, &error);
226 if (error != TInterpreter::kNoError)
228 Error("ExecuteMacro", "Macro interpretation failed");
231 Int_t ntasks = mgr->GetTasks()->GetEntriesFast();
234 Error("ExecuteMacro", "The macro did not add any tasks to the manager");
237 Long64_t ptrTask = (Long64_t)mgr->GetTasks()->At(ntasks0);
238 if (retval >= ptrTask) {
239 TObject::SetBit(AliAnalysisTaskCfg::kLoaded, kTRUE);
240 fRAddTask = reinterpret_cast<TObject*>(retval);
241 if (fConfigDeps && dynamic_cast<TObject*>(fRAddTask)) {
242 TString classname = fRAddTask->ClassName();
243 classname += Form("* __R_ADDTASK__ = (%s*)0x%lx;", classname.Data(),(ULong_t)retval);
244 classname.Prepend(" ");
245 TObjString *line = fConfigDeps->GetLineWith("__R_ADDTASK__");
247 TList *lines = fConfigDeps->GetListOfLines();
248 lines->AddBefore(line, new TObjString(classname));
252 Info("ExecuteMacro", "Macro %s added %d tasks to the manager", fMacro->GetName(), ntasks-ntasks0);
256 //______________________________________________________________________________
257 Int_t AliAnalysisTaskCfg::GetNlibs() const
259 // Returns number of requested libraries.
260 if (fLibs.IsNull()) return 0;
261 TObjArray *list = fLibs.Tokenize(",");
262 Int_t nlibs = list->GetEntriesFast();
267 //______________________________________________________________________________
268 const char *AliAnalysisTaskCfg::GetLibrary(Int_t i) const
270 // Returns library name for the i-th library.
271 Int_t nlibs = GetNlibs();
272 if (i>=nlibs) return 0;
273 static TString libname;
274 TObjArray *list = fLibs.Tokenize(",");
275 libname = list->At(i)->GetName();
276 libname.ReplaceAll(".so","");
277 libname.ReplaceAll(" ","");
278 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
280 return libname.Data();
283 //______________________________________________________________________________
284 Bool_t AliAnalysisTaskCfg::CheckLoadLibraries() const
286 // Check if all requested libraries were loaded, otherwise load them. If some
287 // library cannot be loaded return false.
289 Int_t nlibs = GetNlibs();
290 for (Int_t i=0; i<nlibs; i++) {
291 library = GetLibrary(i);
292 library.Prepend("lib");
293 Int_t loaded = strlen(gSystem->GetLibraries(library,"",kFALSE));
294 if (!loaded) loaded = gSystem->Load(library);
296 Error("CheckLoadLibraries", "Cannot load library %s", library.Data());
303 //______________________________________________________________________________
304 Bool_t AliAnalysisTaskCfg::NeedsLibrary(const char *lib) const
306 // Check if a given library is needed by the module.
307 TString libname = lib;
308 libname.ReplaceAll(".so","");
309 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
310 return fLibs.Contains(libname);
313 //______________________________________________________________________________
314 Int_t AliAnalysisTaskCfg::GetNdeps() const
316 // Returns number of requested libraries.
317 if (fDeps.IsNull()) return 0;
318 Int_t ndeps = fDeps.CountChar(',')+1;
322 //______________________________________________________________________________
323 const char *AliAnalysisTaskCfg::GetDependency(Int_t i) const
325 // Returns library name for the i-th library.
326 Int_t ndeps = GetNdeps();
327 if (i>=ndeps) return 0;
328 static TString depname;
329 TObjArray *list = fDeps.Tokenize(",");
330 depname = list->At(i)->GetName();
331 depname.ReplaceAll(" ","");
333 return depname.Data();
336 //______________________________________________________________________________
337 Bool_t AliAnalysisTaskCfg::NeedsDependency(const char *dep) const
339 // Check if a given library is needed by the module.
342 Int_t len = fDeps.Length();
345 indmax = fDeps.Index(",",indmin);
346 if (indmax < 0) indmax = len;
347 // indmin points to the beginning of the string while indmax to the end+1
348 crt = fDeps(indmin, indmax-indmin);
349 if (crt==dep) return kTRUE;
355 //______________________________________________________________________________
356 TMacro *AliAnalysisTaskCfg::OpenConfigMacro(const char *name)
358 // Opens the specified macro if name is not empty.
364 TString expname = gSystem->ExpandPathName(name);
365 if (expname.IsNull()) {
366 Error("OpenConfigMacro", "Macro name not provided");
369 if (gSystem->AccessPathName(expname)) {
370 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
373 fConfigDeps = new TMacro(expname);
377 //______________________________________________________________________________
378 void AliAnalysisTaskCfg::SetConfigMacro(TMacro *macro)
380 // Set the macro for configuring deps from outside. This will discard the
381 // existing macro if any. The provided macro will be owned by this object.
382 if (fConfigDeps) delete fConfigDeps;
386 //______________________________________________________________________________
387 Long64_t AliAnalysisTaskCfg::ExecuteConfigMacro()
389 // Execute macro to configure dependencies. No arguments are supported.
391 Error("ExecuteConfigMacro", "Call OpenConfigMacro() first");
394 if (!CheckLoadLibraries()) {
395 Error("ExecuteConfigMacro", "Cannot load requested libraries: %s", fLibs.Data());
399 Long64_t retval = fConfigDeps->Exec("", &error);
400 if (error != TInterpreter::kNoError)
402 Error("ExecuteMacro", "Macro interpretation failed");
408 //______________________________________________________________________________
409 void AliAnalysisTaskCfg::SetDataTypes(const char *types)
411 // Sets the data types supported by the module. Stored in upper case.
413 fDataTypes.ToUpper();
416 //______________________________________________________________________________
417 Bool_t AliAnalysisTaskCfg::SupportsData(const char *type) const
419 // Checks if the given data type is supported.
420 TString stype = type;
422 return fDataTypes.Contains(stype);
425 //______________________________________________________________________________
426 void AliAnalysisTaskCfg::Print(Option_t * option) const
428 // Print content of the module.
430 Bool_t full = (opt.Length())?kTRUE:kFALSE;
431 printf("====================================================================\n");
432 printf("# Analysis task: %s\n", GetName());
433 printf("# Supported data types: %s\n", fDataTypes.Data());
434 printf("# Extra libraries: %s\n", fLibs.Data());
435 printf("# Extra dependencies: %s\n", fDeps.Data());
437 printf("# Macro to configure deps: %s\n", fConfigDeps->GetTitle());
438 if (full) fConfigDeps->Print();
440 printf("# Macro connecting this task: %s\n", fMacroName.Data());
441 printf("# Arguments to run the macro: %s\n", fMacroArgs.Data());
443 if (fMacro) fMacro->Print();
445 TMacro macro(gSystem->ExpandPathName(fMacroName.Data()));
451 //______________________________________________________________________________
452 void AliAnalysisTaskCfg::SaveAs(const char *filename, Option_t *option) const
454 // Save the configuration module as text file in the form key:value. The
455 // option can be APPEND, otherwise the file gets overwritten.
458 ios::openmode mode = ios::out;
459 if (opt == "APPEND") mode = ios::app;
461 out.open(filename, mode);
463 Error("SaveAs", "Bad file name: %s", filename);
466 out << "#Module.Begin " << GetName() << endl;
467 out << "#Module.Libs " << fLibs << endl;
468 out << "#Module.Deps " << fDeps << endl;
469 out << "#Module.DataTypes " << fDataTypes << endl;
470 out << "#Module.OutputFile " << fOutputFile << endl;
471 out << "#Module.TerminateFile " << fTerminateFile << endl;
472 out << "#Module.MacroName " << fMacroName << endl;
473 out << "#Module.MacroArgs " << fMacroArgs << endl;
475 out << "#Config.Deps " << fConfigDeps->GetTitle() << endl;
480 //______________________________________________________________________________
481 const char *AliAnalysisTaskCfg::DecodeValue(TString &line)
483 // Decode the value string from the line
484 static TString value;
485 value = line(line.Index(' '),line.Length());
486 value = value.Strip(TString::kLeading,' ');
487 value = value.Strip(TString::kTrailing,' ');
491 //______________________________________________________________________________
492 TObjArray *AliAnalysisTaskCfg::ExtractModulesFrom(const char *filename)
494 // Read all modules from a text file and add them to an object array. The
495 // caller must delete the array at the end. Any module must start with a line
496 // containing: #Module.Begin
497 TString expname = gSystem->ExpandPathName(filename);
498 if (gSystem->AccessPathName(expname)) {
499 ::Error("ExtractModulesFrom", "Cannot open file %s", filename);
502 AliAnalysisTaskCfg *cfg = 0;
503 TObjArray *array = 0;
509 TMacro *addMacro = 0;
510 TMacro *addConfig = 0;
512 in.getline(cline,1024);
514 if (line.BeginsWith("#Module.Begin")) {
515 // New module found, save previous if any
517 if (addMacro) cfg->SetMacro(addMacro);
518 if (addConfig) cfg->SetConfigMacro(addConfig);
519 if (!array) array = new TObjArray();
522 // Decode module name from the line
523 decode = AliAnalysisTaskCfg::DecodeValue(line);
524 cfg = new AliAnalysisTaskCfg(decode);
527 } else if (cfg && line.BeginsWith("#Module.Libs")) {
529 decode = AliAnalysisTaskCfg::DecodeValue(line);
530 cfg->SetLibraries(decode);
531 } else if (cfg && line.BeginsWith("#Module.Deps")) {
532 // Dependencies section
533 decode = AliAnalysisTaskCfg::DecodeValue(line);
534 cfg->SetDependencies(decode);
535 } else if (cfg && line.BeginsWith("#Module.DataTypes")) {
537 decode = AliAnalysisTaskCfg::DecodeValue(line);
538 cfg->SetDataTypes(decode);
539 } else if (cfg && line.BeginsWith("#Module.OutputFile")) {
540 // Desired output file name (via SetCommonOutput)
541 decode = AliAnalysisTaskCfg::DecodeValue(line);
542 cfg->SetOutputFileName(decode);
543 } else if (cfg && line.BeginsWith("#Module.TerminateFile")) {
544 // Custom file name produced in Terminate if any
545 decode = AliAnalysisTaskCfg::DecodeValue(line);
546 cfg->SetTerminateFileName(decode);
547 } else if (cfg && line.BeginsWith("#Module.MacroName")) {
548 // Name of the add task macro (including path)
549 decode = AliAnalysisTaskCfg::DecodeValue(line);
550 cfg->SetMacroName(decode);
551 } else if (cfg && line.BeginsWith("#Module.MacroArgs")) {
552 // Arguments for the AddTask macro
553 decode = AliAnalysisTaskCfg::DecodeValue(line);
554 cfg->SetMacroArgs(decode);
555 } else if (cfg && line.BeginsWith("#Module.StartMacro")) {
556 // Marker for start of the AddTask macro
557 addMacro = new TMacro();
558 TString shortName = gSystem->BaseName(cfg->GetMacroName());
559 shortName = shortName(0,shortName.Index("."));
560 addMacro->SetName(shortName);
561 addMacro->SetTitle(cfg->GetMacroName());
562 } else if (cfg && line.BeginsWith("#Module.StartConfig")) {
563 // Marker for start of the configuration macro
564 addConfig = new TMacro();
565 // TString shortName = gSystem->BaseName(cfg->GetMacroName());
566 // shortName = shortName(0,shortName.Index("."));
567 TString shortName = cfg->GetName();
568 shortName += "_Config";
569 addConfig->SetName(shortName);
570 // shortName.Prepend("/");
571 // shortName.Prepend(gSystem->DirName(cfg->GetMacroName()));
573 addConfig->SetTitle(shortName);
574 } else if (cfg && line.BeginsWith("#Module.EndMacro")) {
575 // Marker for the end of the embedded macro. EndMacro block mandatory.
576 if (cfg && addMacro) {
577 cfg->SetMacro(addMacro);
580 ::Error("ExtractModulesFrom", "Canot end un-started macro block");
582 } else if (cfg && line.BeginsWith("#Module.EndConfig")) {
583 // Marker for the end of the config macro. EndConfig block is mandatory
584 if (cfg && addConfig) {
585 addConfig->GetListOfLines()->AddFirst(new TObjString(Form("%s() {",gSystem->BaseName(addConfig->GetName()))));
586 addConfig->GetListOfLines()->AddLast(new TObjString("}"));
587 cfg->SetConfigMacro(addConfig);
590 ::Error("ExtractModulesFrom", "Canot end un-started config macro block");
593 // Reading a block line
594 if (addMacro) addMacro->AddLine(line);
595 else if (addConfig) addConfig->AddLine(line);
598 // Add last found object to the list
600 if (addMacro) ::Error("ExtractModulesFrom", "#Module.EndMacro block not found");
601 if (addConfig) ::Error("ExtractModulesFrom", "#Module.EndConfig block not found");
602 if (!array) array = new TObjArray();