]>
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. | |
21ca8e59 | 183 | if (IsLoaded()) return kTRUE; |
4579e070 | 184 | if (!fMacro && !OpenMacro()) { |
185 | Error("ExecuteMacro", "Cannot execute this macro"); | |
186 | return -1; | |
187 | } | |
188 | if (!CheckLoadLibraries()) { | |
189 | Error("ExecuteMacro", "Cannot load requested libraries: %s", fLibs.Data()); | |
190 | return -1; | |
191 | } | |
192 | ||
193 | TString args = newargs; | |
194 | if (args.IsNull()) args = fMacroArgs; | |
77f34eae | 195 | Long64_t retval = fMacro->Exec(args); |
21ca8e59 | 196 | if (retval >=0) TObject::SetBit(AliAnalysisTaskCfg::kLoaded, kTRUE); |
77f34eae | 197 | return retval; |
4579e070 | 198 | } |
199 | ||
200 | //______________________________________________________________________________ | |
201 | Int_t AliAnalysisTaskCfg::GetNlibs() const | |
202 | { | |
203 | // Returns number of requested libraries. | |
204 | if (fLibs.IsNull()) return 0; | |
205 | Int_t nlibs = fLibs.CountChar(',')+1; | |
206 | return nlibs; | |
207 | } | |
208 | ||
209 | //______________________________________________________________________________ | |
210 | const char *AliAnalysisTaskCfg::GetLibrary(Int_t i) const | |
211 | { | |
212 | // Returns library name for the i-th library. | |
213 | Int_t nlibs = GetNlibs(); | |
214 | if (i>=nlibs) return 0; | |
215 | TString libname; | |
216 | TObjArray *list = fLibs.Tokenize(","); | |
217 | libname = list->At(i)->GetName(); | |
218 | libname.ReplaceAll(".so",""); | |
219 | libname.ReplaceAll(" ",""); | |
220 | if (libname.BeginsWith("lib")) libname.Remove(0, 3); | |
221 | delete list; | |
222 | return libname.Data(); | |
223 | } | |
224 | ||
225 | //______________________________________________________________________________ | |
226 | Bool_t AliAnalysisTaskCfg::CheckLoadLibraries() const | |
227 | { | |
228 | // Check if all requested libraries were loaded, otherwise load them. If some | |
229 | // library cannot be loaded return false. | |
230 | TString library; | |
231 | Int_t nlibs = GetNlibs(); | |
232 | for (Int_t i=0; i<nlibs; i++) { | |
233 | library = GetLibrary(i); | |
234 | library.Prepend("lib"); | |
235 | Int_t loaded = strlen(gSystem->GetLibraries(library,"",kFALSE)); | |
236 | if (!loaded) loaded = gSystem->Load(library); | |
237 | if (loaded < 0) { | |
238 | Error("CheckLoadLibraries", "Cannot load library %s", library.Data()); | |
239 | return kFALSE; | |
240 | } | |
241 | } | |
242 | return kTRUE; | |
243 | } | |
244 | ||
245 | //______________________________________________________________________________ | |
246 | Bool_t AliAnalysisTaskCfg::NeedsLibrary(const char *lib) const | |
247 | { | |
248 | // Check if a given library is needed by the module. | |
249 | TString libname = lib; | |
250 | libname.ReplaceAll(".so",""); | |
251 | if (libname.BeginsWith("lib")) libname.Remove(0, 3); | |
252 | return fLibs.Contains(libname); | |
253 | } | |
254 | ||
255 | //______________________________________________________________________________ | |
256 | Int_t AliAnalysisTaskCfg::GetNdeps() const | |
257 | { | |
258 | // Returns number of requested libraries. | |
259 | if (fDeps.IsNull()) return 0; | |
260 | Int_t ndeps = fDeps.CountChar(',')+1; | |
261 | return ndeps; | |
262 | } | |
263 | ||
264 | //______________________________________________________________________________ | |
265 | const char *AliAnalysisTaskCfg::GetDependency(Int_t i) const | |
266 | { | |
267 | // Returns library name for the i-th library. | |
268 | Int_t ndeps = GetNdeps(); | |
269 | if (i>=ndeps) return 0; | |
270 | TString depname; | |
271 | TObjArray *list = fDeps.Tokenize(","); | |
272 | depname = list->At(i)->GetName(); | |
273 | depname.ReplaceAll(" ",""); | |
274 | delete list; | |
275 | return depname.Data(); | |
276 | } | |
277 | ||
278 | //______________________________________________________________________________ | |
279 | Bool_t AliAnalysisTaskCfg::NeedsDependency(const char *dep) const | |
280 | { | |
281 | // Check if a given library is needed by the module. | |
282 | return fDeps.Contains(dep); | |
283 | } | |
284 | ||
285 | //______________________________________________________________________________ | |
286 | TMacro *AliAnalysisTaskCfg::OpenConfigMacro(const char *name) | |
287 | { | |
288 | // Opens the specified macro if name is not empty. | |
289 | if (fConfigDeps) { | |
290 | delete fConfigDeps; | |
291 | fConfigDeps = 0; | |
292 | } | |
293 | ||
294 | TString expname = gSystem->ExpandPathName(name); | |
295 | if (expname.IsNull()) { | |
296 | Error("OpenConfigMacro", "Macro name not provided"); | |
297 | return 0; | |
298 | } | |
299 | if (gSystem->AccessPathName(expname)) { | |
300 | Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data()); | |
301 | return 0; | |
302 | } | |
303 | fConfigDeps = new TMacro(expname); | |
304 | return fConfigDeps; | |
305 | } | |
306 | ||
307 | //______________________________________________________________________________ | |
308 | void AliAnalysisTaskCfg::SetConfigMacro(TMacro *macro) | |
309 | { | |
310 | // Set the macro for configuring deps from outside. This will discard the | |
311 | // existing macro if any. The provided macro will be owned by this object. | |
312 | if (fConfigDeps) delete fConfigDeps; | |
313 | fConfigDeps = macro; | |
314 | } | |
315 | ||
316 | //______________________________________________________________________________ | |
317 | Long64_t AliAnalysisTaskCfg::ExecuteConfigMacro() | |
318 | { | |
319 | // Execute macro to configure dependencies. No arguments are supported. | |
320 | if (!fConfigDeps) { | |
321 | Error("ExecuteConfigMacro", "Call OpenConfigMacro() first"); | |
322 | return -1; | |
323 | } | |
324 | if (!CheckLoadLibraries()) { | |
325 | Error("ExecuteConfigMacro", "Cannot load requested libraries: %s", fLibs.Data()); | |
326 | return -1; | |
327 | } | |
328 | return fConfigDeps->Exec(); | |
329 | } | |
330 | ||
331 | //______________________________________________________________________________ | |
332 | void AliAnalysisTaskCfg::SetDataTypes(const char *types) | |
333 | { | |
334 | // Sets the data types supported by the module. Stored in upper case. | |
335 | fDataTypes = types; | |
336 | fDataTypes.ToUpper(); | |
337 | } | |
338 | ||
339 | //______________________________________________________________________________ | |
340 | Bool_t AliAnalysisTaskCfg::SupportsData(const char *type) const | |
341 | { | |
342 | // Checks if the given data type is supported. | |
343 | TString stype = type; | |
344 | stype.ToUpper(); | |
345 | return fDataTypes.Contains(stype); | |
346 | } | |
347 | ||
348 | //______________________________________________________________________________ | |
349 | void AliAnalysisTaskCfg::Print(Option_t * option) const | |
350 | { | |
351 | // Print content of the module. | |
352 | TString opt(option); | |
353 | Bool_t full = (opt.Length())?kTRUE:kFALSE; | |
354 | printf("====================================================================\n"); | |
355 | printf("# Analysis task: %s\n", GetName()); | |
356 | printf("# Supported data types: %s\n", fDataTypes.Data()); | |
357 | printf("# Extra libraries: %s\n", fLibs.Data()); | |
358 | printf("# Extra dependencies: %s\n", fDeps.Data()); | |
359 | if (fConfigDeps) { | |
360 | printf("# Macro to configure deps: %s\n", fConfigDeps->GetTitle()); | |
361 | if (full) fConfigDeps->Print(); | |
362 | } | |
363 | printf("# Macro connecting this task: %s\n", fMacroName.Data()); | |
364 | printf("# Arguments to run the macro: %s\n", fMacroArgs.Data()); | |
365 | if (full) { | |
366 | if (fMacro) fMacro->Print(); | |
367 | else { | |
3e40fd4c | 368 | TMacro macro(gSystem->ExpandPathName(fMacroName.Data())); |
4579e070 | 369 | macro.Print(); |
370 | } | |
371 | } | |
372 | } | |
373 | ||
374 | //______________________________________________________________________________ | |
375 | void AliAnalysisTaskCfg::SaveAs(const char *filename, Option_t *option) const | |
376 | { | |
377 | // Save the configuration module as text file in the form key:value. The | |
378 | // option can be APPEND, otherwise the file gets overwritten. | |
379 | TString opt(option); | |
380 | opt.ToUpper(); | |
381 | ios::openmode mode = ios::out; | |
382 | if (opt == "APPEND") mode = ios::app; | |
383 | ofstream out; | |
384 | out.open(filename, mode); | |
385 | if (out.bad()) { | |
386 | Error("SaveAs", "Bad file name: %s", filename); | |
387 | return; | |
388 | } | |
389 | out << "#Module.Begin " << GetName() << endl; | |
390 | out << "#Module.Libs " << fLibs << endl; | |
391 | out << "#Module.Deps " << fDeps << endl; | |
392 | out << "#Module.DataTypes " << fDataTypes << endl; | |
393 | out << "#Module.MacroName " << fMacroName << endl; | |
394 | out << "#Module.MacroArgs " << fMacroArgs << endl; | |
395 | if (fConfigDeps) { | |
396 | out << "#Config.Deps " << fConfigDeps->GetTitle() << endl; | |
397 | } | |
398 | } | |
399 | ||
400 | ||
401 | //______________________________________________________________________________ | |
402 | const char *AliAnalysisTaskCfg::DecodeValue(TString &line) | |
403 | { | |
404 | // Decode the value string from the line | |
405 | TString value = line(line.Index(' '),line.Length()); | |
406 | value = value.Strip(TString::kLeading,' '); | |
407 | value = value.Strip(TString::kTrailing,' '); | |
408 | return value.Data(); | |
409 | } | |
410 | ||
411 | //______________________________________________________________________________ | |
412 | TObjArray *AliAnalysisTaskCfg::ExtractModulesFrom(const char *filename) | |
413 | { | |
414 | // Read all modules from a text file and add them to an object array. The | |
415 | // caller must delete the array at the end. Any module must start with a line | |
416 | // containing: #Module.Begin | |
417 | TString expname = gSystem->ExpandPathName(filename); | |
418 | if (gSystem->AccessPathName(expname)) { | |
419 | ::Error("ExtractModulesFrom", "Cannot open file %s", filename); | |
420 | return 0; | |
421 | } | |
422 | AliAnalysisTaskCfg *cfg = 0; | |
423 | TObjArray *array = 0; | |
424 | ifstream in; | |
425 | in.open(expname); | |
426 | char cline[1024]; | |
427 | TString line; | |
428 | TString decode; | |
429 | TMacro *addMacro = 0; | |
430 | TMacro *addConfig = 0; | |
431 | while (in.good()) { | |
432 | in.getline(cline,1024); | |
433 | line = cline; | |
434 | if (line.BeginsWith("#Module.Begin")) { | |
435 | // New module found, save previous if any | |
436 | if (cfg) { | |
437 | if (addMacro) cfg->SetMacro(addMacro); | |
438 | if (addConfig) cfg->SetConfigMacro(addConfig); | |
439 | if (!array) array = new TObjArray(); | |
440 | array->Add(cfg); | |
441 | } | |
442 | // Decode module name from the line | |
443 | decode = AliAnalysisTaskCfg::DecodeValue(line); | |
444 | cfg = new AliAnalysisTaskCfg(decode); | |
445 | addMacro = 0; | |
446 | addConfig = 0; | |
447 | } else if (cfg && line.BeginsWith("#Module.Libs")) { | |
448 | // Libraries section | |
449 | decode = AliAnalysisTaskCfg::DecodeValue(line); | |
450 | cfg->SetLibraries(decode); | |
451 | } else if (cfg && line.BeginsWith("#Module.Deps")) { | |
452 | // Dependencies section | |
453 | decode = AliAnalysisTaskCfg::DecodeValue(line); | |
454 | cfg->SetDependencies(decode); | |
455 | } else if (cfg && line.BeginsWith("#Module.DataTypes")) { | |
456 | // Data types | |
457 | decode = AliAnalysisTaskCfg::DecodeValue(line); | |
458 | cfg->SetDataTypes(decode); | |
459 | } else if (cfg && line.BeginsWith("#Module.MacroName")) { | |
460 | // Name of the add task macro (including path) | |
461 | decode = AliAnalysisTaskCfg::DecodeValue(line); | |
462 | cfg->SetMacroName(decode); | |
463 | } else if (cfg && line.BeginsWith("#Module.MacroArgs")) { | |
464 | // Arguments for the AddTask macro | |
465 | decode = AliAnalysisTaskCfg::DecodeValue(line); | |
466 | cfg->SetMacroArgs(decode); | |
467 | } else if (cfg && line.BeginsWith("#Module.StartMacro")) { | |
468 | // Marker for start of the AddTask macro | |
469 | addMacro = new TMacro(); | |
470 | TString shortName = gSystem->BaseName(cfg->GetMacroName()); | |
471 | shortName = shortName(0,shortName.Index(".")); | |
472 | addMacro->SetName(shortName); | |
473 | addMacro->SetTitle(cfg->GetMacroName()); | |
474 | } else if (cfg && line.BeginsWith("#Module.StartConfig")) { | |
475 | // Marker for start of the configuration macro | |
476 | addConfig = new TMacro(); | |
477 | TString shortName = gSystem->BaseName(cfg->GetMacroName()); | |
478 | shortName = shortName(0,shortName.Index(".")); | |
479 | shortName += "Config"; | |
480 | addConfig->SetName(shortName); | |
481 | shortName.Prepend("/"); | |
482 | shortName.Prepend(gSystem->DirName(cfg->GetMacroName())); | |
483 | shortName += ".C"; | |
484 | addConfig->SetTitle(shortName); | |
485 | } else if (cfg && line.BeginsWith("#Module.EndMacro")) { | |
486 | // Marker for the end of the embedded macro. EndMacro block mandatory. | |
487 | if (cfg && addMacro) { | |
488 | cfg->SetMacro(addMacro); | |
489 | addMacro = 0; | |
490 | } else { | |
491 | ::Error("ExtractModulesFrom", "Canot end un-started macro block"); | |
492 | } | |
493 | } else if (cfg && line.BeginsWith("#Module.EndConfig")) { | |
494 | // Marker for the end of the config macro. EndConfig block is mandatory | |
495 | if (cfg && addConfig) { | |
496 | cfg->SetConfigMacro(addConfig); | |
497 | addConfig = 0; | |
498 | } else { | |
499 | ::Error("ExtractModulesFrom", "Canot end un-started config macro block"); | |
500 | } | |
501 | } else { | |
502 | // Reading a block line | |
503 | if (addMacro) addMacro->AddLine(line); | |
504 | else if (addConfig) addConfig->AddLine(line); | |
505 | } | |
506 | } | |
507 | // Add last found object to the list | |
508 | if (cfg) { | |
509 | if (addMacro) cfg->SetMacro(addMacro); | |
510 | if (addConfig) cfg->SetConfigMacro(addConfig); | |
511 | if (!array) array = new TObjArray(); | |
512 | array->Add(cfg); | |
513 | } | |
514 | return array; | |
515 | } |