]>
Commit | Line | Data |
---|---|---|
4579e070 | 1 | /************************************************************************** |
2 | * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. * | |
3 | * * | |
4 | * Author: The ALICE Off-line Project. * | |
5 | * Contributors are mentioned in the code where appropriate. * | |
6 | * * | |
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 | **************************************************************************/ | |
15 | ||
16 | #include "AliAnalysisTaskCfg.h" | |
17 | ||
18 | #include "Riostream.h" | |
19 | #include "TError.h" | |
20 | #include "TMacro.h" | |
21 | #include "TSystem.h" | |
22 | #include "TObjArray.h" | |
23 | ||
24 | // Author: Andrei Gheata, 12/08/2011 | |
25 | ||
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 | //============================================================================== | |
31 | ||
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. | |
42 | // An example: | |
43 | // Content of file: QAsym.cfg | |
44 | /* | |
45 | # Lines that do not start with #Module are ignored, except those in embedded | |
46 | macro blocks | |
47 | #Module.Begin QAsym | |
48 | #Module.Libs PWG1 | |
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 | |
53 | #Module.StartConfig | |
54 | __R_ADDTASK__->SelectCollisionCandidates(); | |
55 | #Module.EndConfig | |
56 | */ | |
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. | |
65 | // | |
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. | |
68 | ||
69 | ||
70 | ClassImp(AliAnalysisTaskCfg) | |
71 | ||
72 | //______________________________________________________________________________ | |
73 | AliAnalysisTaskCfg::AliAnalysisTaskCfg() | |
74 | :TNamed(), | |
75 | fMacroName(), | |
76 | fMacroArgs(), | |
77 | fLibs(), | |
78 | fDeps(), | |
79 | fDataTypes(), | |
80 | fMacro(0), | |
81 | fConfigDeps(0) | |
82 | { | |
83 | // I/O constructor. | |
84 | } | |
85 | ||
86 | //______________________________________________________________________________ | |
87 | AliAnalysisTaskCfg::AliAnalysisTaskCfg(const char *name) | |
88 | :TNamed(name,""), | |
89 | fMacroName(), | |
90 | fMacroArgs(), | |
91 | fLibs(), | |
92 | fDeps(), | |
93 | fDataTypes(), | |
94 | fMacro(0), | |
95 | fConfigDeps(0) | |
96 | { | |
97 | // Constructor. All configuration objects need to be named since they are looked | |
98 | // for by name. | |
99 | } | |
100 | ||
101 | //______________________________________________________________________________ | |
102 | AliAnalysisTaskCfg::AliAnalysisTaskCfg(const AliAnalysisTaskCfg &other) | |
103 | :TNamed(other), | |
104 | fMacroName(other.fMacroName), | |
105 | fMacroArgs(other.fMacroArgs), | |
106 | fLibs(other.fLibs), | |
107 | fDeps(other.fDeps), | |
108 | fDataTypes(other.fDataTypes), | |
109 | fMacro(0), | |
110 | fConfigDeps(0) | |
111 | { | |
112 | // Copy constructor. | |
113 | if (other.fMacro) fMacro = new TMacro(*other.fMacro); | |
114 | if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps); | |
115 | } | |
116 | ||
117 | //______________________________________________________________________________ | |
118 | AliAnalysisTaskCfg::~AliAnalysisTaskCfg() | |
119 | { | |
120 | // Destructor. | |
121 | delete fMacro; | |
122 | delete fConfigDeps; | |
123 | } | |
124 | ||
125 | //______________________________________________________________________________ | |
126 | AliAnalysisTaskCfg& AliAnalysisTaskCfg::operator=(const AliAnalysisTaskCfg &other) | |
127 | { | |
128 | // Assignment operator. | |
129 | if (&other == this) return *this; | |
130 | TNamed::operator=(other); | |
131 | fMacroName = other.fMacroName; | |
132 | fMacroArgs = other.fMacroArgs; | |
133 | fLibs = other.fLibs; | |
134 | fDeps = other.fDeps; | |
135 | fDataTypes = other.fDataTypes; | |
136 | if (other.fMacro) fMacro = new TMacro(*other.fMacro); | |
137 | if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps); | |
138 | return *this; | |
139 | } | |
140 | ||
141 | //______________________________________________________________________________ | |
142 | TMacro *AliAnalysisTaskCfg::OpenMacro(const char *name) | |
143 | { | |
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 | |
147 | if (fMacro) { | |
148 | delete fMacro; | |
149 | fMacro = 0; | |
150 | } | |
151 | TString expname; | |
152 | if (strlen(name)) expname = gSystem->ExpandPathName(name); | |
3e40fd4c | 153 | else expname = gSystem->ExpandPathName(fMacroName.Data()); |
4579e070 | 154 | if (expname.IsNull()) { |
155 | Error("OpenMacro", "Macro name not provided and not previously set"); | |
156 | return 0; | |
157 | } | |
158 | if (gSystem->AccessPathName(expname)) { | |
159 | Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data()); | |
160 | return 0; | |
161 | } | |
162 | if (strlen(name)) fMacroName = name; | |
163 | fMacro = new TMacro(expname); | |
164 | return fMacro; | |
165 | } | |
166 | ||
167 | //______________________________________________________________________________ | |
168 | void AliAnalysisTaskCfg::SetMacro(TMacro *macro) | |
169 | { | |
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; | |
173 | fMacro = macro; | |
174 | } | |
175 | ||
176 | //______________________________________________________________________________ | |
177 | Long64_t AliAnalysisTaskCfg::ExecuteMacro(const char *newargs) | |
178 | { | |
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 | |
77f34eae | 181 | // with stored fMacroArgs unless new arguments are provided. The flag IsLoaded |
182 | // is set once the macro was successfully executed. | |
4579e070 | 183 | if (!fMacro && !OpenMacro()) { |
184 | Error("ExecuteMacro", "Cannot execute this macro"); | |
185 | return -1; | |
186 | } | |
187 | if (!CheckLoadLibraries()) { | |
188 | Error("ExecuteMacro", "Cannot load requested libraries: %s", fLibs.Data()); | |
189 | return -1; | |
190 | } | |
191 | ||
192 | TString args = newargs; | |
193 | if (args.IsNull()) args = fMacroArgs; | |
77f34eae | 194 | Long64_t retval = fMacro->Exec(args); |
195 | if (retval >=0) SetBit(AliAnalysisTaskCfg::kLoaded, kTRUE); | |
196 | return retval; | |
4579e070 | 197 | } |
198 | ||
199 | //______________________________________________________________________________ | |
200 | Int_t AliAnalysisTaskCfg::GetNlibs() const | |
201 | { | |
202 | // Returns number of requested libraries. | |
203 | if (fLibs.IsNull()) return 0; | |
204 | Int_t nlibs = fLibs.CountChar(',')+1; | |
205 | return nlibs; | |
206 | } | |
207 | ||
208 | //______________________________________________________________________________ | |
209 | const char *AliAnalysisTaskCfg::GetLibrary(Int_t i) const | |
210 | { | |
211 | // Returns library name for the i-th library. | |
212 | Int_t nlibs = GetNlibs(); | |
213 | if (i>=nlibs) return 0; | |
214 | TString libname; | |
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); | |
220 | delete list; | |
221 | return libname.Data(); | |
222 | } | |
223 | ||
224 | //______________________________________________________________________________ | |
225 | Bool_t AliAnalysisTaskCfg::CheckLoadLibraries() const | |
226 | { | |
227 | // Check if all requested libraries were loaded, otherwise load them. If some | |
228 | // library cannot be loaded return false. | |
229 | TString library; | |
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); | |
236 | if (loaded < 0) { | |
237 | Error("CheckLoadLibraries", "Cannot load library %s", library.Data()); | |
238 | return kFALSE; | |
239 | } | |
240 | } | |
241 | return kTRUE; | |
242 | } | |
243 | ||
244 | //______________________________________________________________________________ | |
245 | Bool_t AliAnalysisTaskCfg::NeedsLibrary(const char *lib) const | |
246 | { | |
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); | |
252 | } | |
253 | ||
254 | //______________________________________________________________________________ | |
255 | Int_t AliAnalysisTaskCfg::GetNdeps() const | |
256 | { | |
257 | // Returns number of requested libraries. | |
258 | if (fDeps.IsNull()) return 0; | |
259 | Int_t ndeps = fDeps.CountChar(',')+1; | |
260 | return ndeps; | |
261 | } | |
262 | ||
263 | //______________________________________________________________________________ | |
264 | const char *AliAnalysisTaskCfg::GetDependency(Int_t i) const | |
265 | { | |
266 | // Returns library name for the i-th library. | |
267 | Int_t ndeps = GetNdeps(); | |
268 | if (i>=ndeps) return 0; | |
269 | TString depname; | |
270 | TObjArray *list = fDeps.Tokenize(","); | |
271 | depname = list->At(i)->GetName(); | |
272 | depname.ReplaceAll(" ",""); | |
273 | delete list; | |
274 | return depname.Data(); | |
275 | } | |
276 | ||
277 | //______________________________________________________________________________ | |
278 | Bool_t AliAnalysisTaskCfg::NeedsDependency(const char *dep) const | |
279 | { | |
280 | // Check if a given library is needed by the module. | |
281 | return fDeps.Contains(dep); | |
282 | } | |
283 | ||
284 | //______________________________________________________________________________ | |
285 | TMacro *AliAnalysisTaskCfg::OpenConfigMacro(const char *name) | |
286 | { | |
287 | // Opens the specified macro if name is not empty. | |
288 | if (fConfigDeps) { | |
289 | delete fConfigDeps; | |
290 | fConfigDeps = 0; | |
291 | } | |
292 | ||
293 | TString expname = gSystem->ExpandPathName(name); | |
294 | if (expname.IsNull()) { | |
295 | Error("OpenConfigMacro", "Macro name not provided"); | |
296 | return 0; | |
297 | } | |
298 | if (gSystem->AccessPathName(expname)) { | |
299 | Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data()); | |
300 | return 0; | |
301 | } | |
302 | fConfigDeps = new TMacro(expname); | |
303 | return fConfigDeps; | |
304 | } | |
305 | ||
306 | //______________________________________________________________________________ | |
307 | void AliAnalysisTaskCfg::SetConfigMacro(TMacro *macro) | |
308 | { | |
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; | |
312 | fConfigDeps = macro; | |
313 | } | |
314 | ||
315 | //______________________________________________________________________________ | |
316 | Long64_t AliAnalysisTaskCfg::ExecuteConfigMacro() | |
317 | { | |
318 | // Execute macro to configure dependencies. No arguments are supported. | |
319 | if (!fConfigDeps) { | |
320 | Error("ExecuteConfigMacro", "Call OpenConfigMacro() first"); | |
321 | return -1; | |
322 | } | |
323 | if (!CheckLoadLibraries()) { | |
324 | Error("ExecuteConfigMacro", "Cannot load requested libraries: %s", fLibs.Data()); | |
325 | return -1; | |
326 | } | |
327 | return fConfigDeps->Exec(); | |
328 | } | |
329 | ||
330 | //______________________________________________________________________________ | |
331 | void AliAnalysisTaskCfg::SetDataTypes(const char *types) | |
332 | { | |
333 | // Sets the data types supported by the module. Stored in upper case. | |
334 | fDataTypes = types; | |
335 | fDataTypes.ToUpper(); | |
336 | } | |
337 | ||
338 | //______________________________________________________________________________ | |
339 | Bool_t AliAnalysisTaskCfg::SupportsData(const char *type) const | |
340 | { | |
341 | // Checks if the given data type is supported. | |
342 | TString stype = type; | |
343 | stype.ToUpper(); | |
344 | return fDataTypes.Contains(stype); | |
345 | } | |
346 | ||
347 | //______________________________________________________________________________ | |
348 | void AliAnalysisTaskCfg::Print(Option_t * option) const | |
349 | { | |
350 | // Print content of the module. | |
351 | TString opt(option); | |
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()); | |
358 | if (fConfigDeps) { | |
359 | printf("# Macro to configure deps: %s\n", fConfigDeps->GetTitle()); | |
360 | if (full) fConfigDeps->Print(); | |
361 | } | |
362 | printf("# Macro connecting this task: %s\n", fMacroName.Data()); | |
363 | printf("# Arguments to run the macro: %s\n", fMacroArgs.Data()); | |
364 | if (full) { | |
365 | if (fMacro) fMacro->Print(); | |
366 | else { | |
3e40fd4c | 367 | TMacro macro(gSystem->ExpandPathName(fMacroName.Data())); |
4579e070 | 368 | macro.Print(); |
369 | } | |
370 | } | |
371 | } | |
372 | ||
373 | //______________________________________________________________________________ | |
374 | void AliAnalysisTaskCfg::SaveAs(const char *filename, Option_t *option) const | |
375 | { | |
376 | // Save the configuration module as text file in the form key:value. The | |
377 | // option can be APPEND, otherwise the file gets overwritten. | |
378 | TString opt(option); | |
379 | opt.ToUpper(); | |
380 | ios::openmode mode = ios::out; | |
381 | if (opt == "APPEND") mode = ios::app; | |
382 | ofstream out; | |
383 | out.open(filename, mode); | |
384 | if (out.bad()) { | |
385 | Error("SaveAs", "Bad file name: %s", filename); | |
386 | return; | |
387 | } | |
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; | |
394 | if (fConfigDeps) { | |
395 | out << "#Config.Deps " << fConfigDeps->GetTitle() << endl; | |
396 | } | |
397 | } | |
398 | ||
399 | ||
400 | //______________________________________________________________________________ | |
401 | const char *AliAnalysisTaskCfg::DecodeValue(TString &line) | |
402 | { | |
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,' '); | |
407 | return value.Data(); | |
408 | } | |
409 | ||
410 | //______________________________________________________________________________ | |
411 | TObjArray *AliAnalysisTaskCfg::ExtractModulesFrom(const char *filename) | |
412 | { | |
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); | |
419 | return 0; | |
420 | } | |
421 | AliAnalysisTaskCfg *cfg = 0; | |
422 | TObjArray *array = 0; | |
423 | ifstream in; | |
424 | in.open(expname); | |
425 | char cline[1024]; | |
426 | TString line; | |
427 | TString decode; | |
428 | TMacro *addMacro = 0; | |
429 | TMacro *addConfig = 0; | |
430 | while (in.good()) { | |
431 | in.getline(cline,1024); | |
432 | line = cline; | |
433 | if (line.BeginsWith("#Module.Begin")) { | |
434 | // New module found, save previous if any | |
435 | if (cfg) { | |
436 | if (addMacro) cfg->SetMacro(addMacro); | |
437 | if (addConfig) cfg->SetConfigMacro(addConfig); | |
438 | if (!array) array = new TObjArray(); | |
439 | array->Add(cfg); | |
440 | } | |
441 | // Decode module name from the line | |
442 | decode = AliAnalysisTaskCfg::DecodeValue(line); | |
443 | cfg = new AliAnalysisTaskCfg(decode); | |
444 | addMacro = 0; | |
445 | addConfig = 0; | |
446 | } else if (cfg && line.BeginsWith("#Module.Libs")) { | |
447 | // Libraries section | |
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")) { | |
455 | // Data types | |
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())); | |
482 | shortName += ".C"; | |
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); | |
488 | addMacro = 0; | |
489 | } else { | |
490 | ::Error("ExtractModulesFrom", "Canot end un-started macro block"); | |
491 | } | |
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); | |
496 | addConfig = 0; | |
497 | } else { | |
498 | ::Error("ExtractModulesFrom", "Canot end un-started config macro block"); | |
499 | } | |
500 | } else { | |
501 | // Reading a block line | |
502 | if (addMacro) addMacro->AddLine(line); | |
503 | else if (addConfig) addConfig->AddLine(line); | |
504 | } | |
505 | } | |
506 | // Add last found object to the list | |
507 | if (cfg) { | |
508 | if (addMacro) cfg->SetMacro(addMacro); | |
509 | if (addConfig) cfg->SetConfigMacro(addConfig); | |
510 | if (!array) array = new TObjArray(); | |
511 | array->Add(cfg); | |
512 | } | |
513 | return array; | |
514 | } |