Adding a reminder for coders
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskCfg.cxx
CommitLineData
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"
15f0154e 17#include "AliAnalysisManager.h"
4579e070 18
19#include "Riostream.h"
20#include "TError.h"
21#include "TMacro.h"
22#include "TSystem.h"
23#include "TObjArray.h"
15f0154e 24#include "TObjString.h"
25#include "TList.h"
4579e070 26
27// Author: Andrei Gheata, 12/08/2011
28
29//==============================================================================
30// AliAnalysysTaskCfg - Class embedding the configuration needed to run
31// a given analysis task: libraries to be loaded, location and name of the macro
32// used to add the task to the analysis manager, dependencies.
33//==============================================================================
34
35// This class is used to fully describe how to run a given analysis task. It
36// requires that the user creates an AddTask macro for his task and defines:
37// - The needed libs separated by commas,
38// - The full path to the AddTask macro (starting with $ALICE_ROOT if needed)
39// - The list of arguments to be provided to the AddTask macro. One can use
40// here only constants that can be interpreted.
41// - The list of dependencies (other modules required to run this task). These
42// must be names of other AliAnalysisTaskCfg objects, separated by commas.
43// - Data types supported by the task (e.g. ESD, AOD, MC)
44// The class has normal ROOT IO, but it can also read from and write to text files.
45// An example:
46// Content of file: QAsym.cfg
47/*
48# Lines that do not start with #Module are ignored, except those in embedded
49 macro blocks
50#Module.Begin QAsym
51#Module.Libs PWG1
52#Module.Deps PhysicsSelection
53#Module.DataTypes ESD, AOD, MC
54#Module.MacroName $ALICE_ROOT/PWG1/PilotTrain/AddTaskQAsym.C
55#Module.MacroArgs 0, AliVEvent::kAnyINT, AliVEvent::kHighMult, AliVEvent::kEMC7, AliVEvent::kMUU7
56#Module.StartConfig
57__R_ADDTASK__->SelectCollisionCandidates();
58#Module.EndConfig
59*/
60// The following special variable names can be used:
61// __R_ADDTASK__ = the return value of the AddTask macro included
62// __R_ESDH__ = pointer to ESD handler
63// __R_AODH__ = pointer to AOD handler
64// __R_MCH__ = pointer to MC handler
65// The static method ExtractModulesFrom(const char *filename) allows reading
66// several task configuration modules from the same text file and returning
67// them in a TObjArray.
68//
69// A list of configuration modules representing a train should be injected in
70// the right order in the grid handler to generate train macros.
71
72
73ClassImp(AliAnalysisTaskCfg)
74
75//______________________________________________________________________________
76AliAnalysisTaskCfg::AliAnalysisTaskCfg()
77 :TNamed(),
78 fMacroName(),
79 fMacroArgs(),
80 fLibs(),
81 fDeps(),
82 fDataTypes(),
83 fMacro(0),
15f0154e 84 fConfigDeps(0),
85 fRAddTask(0)
4579e070 86{
87// I/O constructor.
88}
89
90//______________________________________________________________________________
91AliAnalysisTaskCfg::AliAnalysisTaskCfg(const char *name)
92 :TNamed(name,""),
93 fMacroName(),
94 fMacroArgs(),
95 fLibs(),
96 fDeps(),
97 fDataTypes(),
98 fMacro(0),
15f0154e 99 fConfigDeps(0),
100 fRAddTask(0)
4579e070 101{
102// Constructor. All configuration objects need to be named since they are looked
103// for by name.
104}
105
106//______________________________________________________________________________
107AliAnalysisTaskCfg::AliAnalysisTaskCfg(const AliAnalysisTaskCfg &other)
108 :TNamed(other),
109 fMacroName(other.fMacroName),
110 fMacroArgs(other.fMacroArgs),
111 fLibs(other.fLibs),
112 fDeps(other.fDeps),
113 fDataTypes(other.fDataTypes),
114 fMacro(0),
15f0154e 115 fConfigDeps(0),
116 fRAddTask(0)
4579e070 117{
118// Copy constructor.
119 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
120 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
121}
122
123//______________________________________________________________________________
124AliAnalysisTaskCfg::~AliAnalysisTaskCfg()
125{
126// Destructor.
127 delete fMacro;
128 delete fConfigDeps;
129}
130
131//______________________________________________________________________________
132AliAnalysisTaskCfg& AliAnalysisTaskCfg::operator=(const AliAnalysisTaskCfg &other)
133{
134// Assignment operator.
135 if (&other == this) return *this;
136 TNamed::operator=(other);
137 fMacroName = other.fMacroName;
138 fMacroArgs = other.fMacroArgs;
139 fLibs = other.fLibs;
140 fDeps = other.fDeps;
141 fDataTypes = other.fDataTypes;
142 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
143 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
15f0154e 144 fRAddTask = other.fRAddTask;
4579e070 145 return *this;
146}
147
148//______________________________________________________________________________
149TMacro *AliAnalysisTaskCfg::OpenMacro(const char *name)
150{
151// Opens the specified macro if name is not empty. In case of success updates
152// fMacroName, creates the maco object and returns its pointer.
153 // Clean-up previous macro if any
154 if (fMacro) {
155 delete fMacro;
156 fMacro = 0;
157 }
158 TString expname;
159 if (strlen(name)) expname = gSystem->ExpandPathName(name);
3e40fd4c 160 else expname = gSystem->ExpandPathName(fMacroName.Data());
4579e070 161 if (expname.IsNull()) {
162 Error("OpenMacro", "Macro name not provided and not previously set");
163 return 0;
164 }
165 if (gSystem->AccessPathName(expname)) {
166 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
167 return 0;
168 }
169 if (strlen(name)) fMacroName = name;
170 fMacro = new TMacro(expname);
171 return fMacro;
172}
173
174//______________________________________________________________________________
175void AliAnalysisTaskCfg::SetMacro(TMacro *macro)
176{
177// Set the AddTask macro from outside. This will discard the existing macro if
178// any. The provided macro will be owned by this object.
179 if (fMacro) delete fMacro;
180 fMacro = macro;
181}
182
183//______________________________________________________________________________
184Long64_t AliAnalysisTaskCfg::ExecuteMacro(const char *newargs)
185{
186// Execute AddTask macro. Opens first the macro pointed by fMacroName if not yet
187// done. Checks if the requested libraries are loaded, else loads them. Executes
77f34eae 188// with stored fMacroArgs unless new arguments are provided. The flag IsLoaded
189// is set once the macro was successfully executed.
21ca8e59 190 if (IsLoaded()) return kTRUE;
4579e070 191 if (!fMacro && !OpenMacro()) {
192 Error("ExecuteMacro", "Cannot execute this macro");
193 return -1;
194 }
195 if (!CheckLoadLibraries()) {
196 Error("ExecuteMacro", "Cannot load requested libraries: %s", fLibs.Data());
197 return -1;
198 }
199
15f0154e 200 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
201 if (!mgr) {
202 Error("ExecuteMacro", "Analysis manager not defined yet");
203 return -1;
204 }
205 Int_t ntasks0 = mgr->GetTasks()->GetEntriesFast();
4579e070 206 TString args = newargs;
207 if (args.IsNull()) args = fMacroArgs;
77f34eae 208 Long64_t retval = fMacro->Exec(args);
5e8c7c22 209 Int_t ntasks = mgr->GetTasks()->GetEntriesFast();
210 Long64_t ptrTask = 1;
211 if (ntasks>ntasks0)
212 ptrTask = (Long64_t)mgr->GetTasks()->At(ntasks0);
213 if (retval >= ptrTask) {
15f0154e 214 TObject::SetBit(AliAnalysisTaskCfg::kLoaded, kTRUE);
215 fRAddTask = reinterpret_cast<TObject*>(retval);
5e8c7c22 216 if (fConfigDeps && dynamic_cast<TObject*>(fRAddTask)) {
15f0154e 217 TString classname = fRAddTask->ClassName();
218 classname += Form("* __R_ADDTASK__ = (%s*)0x%lx;", classname.Data(),(ULong_t)retval);
219 classname.Prepend(" ");
220 TObjString *line = fConfigDeps->GetLineWith("__R_ADDTASK__");
221 if (line) {
222 TList *lines = fConfigDeps->GetListOfLines();
223 lines->AddBefore(line, new TObjString(classname));
224 }
225 }
226 }
15f0154e 227 Info("ExecuteMacro", "Macro %s added %d tasks to the manager", fMacro->GetName(), ntasks-ntasks0);
77f34eae 228 return retval;
4579e070 229}
230
231//______________________________________________________________________________
232Int_t AliAnalysisTaskCfg::GetNlibs() const
233{
234// Returns number of requested libraries.
235 if (fLibs.IsNull()) return 0;
236 Int_t nlibs = fLibs.CountChar(',')+1;
237 return nlibs;
238}
239
240//______________________________________________________________________________
241const char *AliAnalysisTaskCfg::GetLibrary(Int_t i) const
242{
243// Returns library name for the i-th library.
244 Int_t nlibs = GetNlibs();
245 if (i>=nlibs) return 0;
246 TString libname;
247 TObjArray *list = fLibs.Tokenize(",");
248 libname = list->At(i)->GetName();
249 libname.ReplaceAll(".so","");
250 libname.ReplaceAll(" ","");
251 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
252 delete list;
253 return libname.Data();
254}
255
256//______________________________________________________________________________
257Bool_t AliAnalysisTaskCfg::CheckLoadLibraries() const
258{
259// Check if all requested libraries were loaded, otherwise load them. If some
260// library cannot be loaded return false.
261 TString library;
262 Int_t nlibs = GetNlibs();
263 for (Int_t i=0; i<nlibs; i++) {
264 library = GetLibrary(i);
265 library.Prepend("lib");
266 Int_t loaded = strlen(gSystem->GetLibraries(library,"",kFALSE));
267 if (!loaded) loaded = gSystem->Load(library);
268 if (loaded < 0) {
269 Error("CheckLoadLibraries", "Cannot load library %s", library.Data());
270 return kFALSE;
271 }
272 }
273 return kTRUE;
274}
275
276//______________________________________________________________________________
277Bool_t AliAnalysisTaskCfg::NeedsLibrary(const char *lib) const
278{
279// Check if a given library is needed by the module.
280 TString libname = lib;
281 libname.ReplaceAll(".so","");
282 if (libname.BeginsWith("lib")) libname.Remove(0, 3);
283 return fLibs.Contains(libname);
284}
285
286//______________________________________________________________________________
287Int_t AliAnalysisTaskCfg::GetNdeps() const
288{
289// Returns number of requested libraries.
290 if (fDeps.IsNull()) return 0;
291 Int_t ndeps = fDeps.CountChar(',')+1;
292 return ndeps;
293}
294
295//______________________________________________________________________________
296const char *AliAnalysisTaskCfg::GetDependency(Int_t i) const
297{
298// Returns library name for the i-th library.
299 Int_t ndeps = GetNdeps();
300 if (i>=ndeps) return 0;
301 TString depname;
302 TObjArray *list = fDeps.Tokenize(",");
303 depname = list->At(i)->GetName();
304 depname.ReplaceAll(" ","");
305 delete list;
306 return depname.Data();
307}
308
309//______________________________________________________________________________
310Bool_t AliAnalysisTaskCfg::NeedsDependency(const char *dep) const
311{
312// Check if a given library is needed by the module.
313 return fDeps.Contains(dep);
314}
315
316//______________________________________________________________________________
317TMacro *AliAnalysisTaskCfg::OpenConfigMacro(const char *name)
318{
319// Opens the specified macro if name is not empty.
320 if (fConfigDeps) {
321 delete fConfigDeps;
322 fConfigDeps = 0;
323 }
324
325 TString expname = gSystem->ExpandPathName(name);
326 if (expname.IsNull()) {
327 Error("OpenConfigMacro", "Macro name not provided");
328 return 0;
329 }
330 if (gSystem->AccessPathName(expname)) {
331 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
332 return 0;
333 }
334 fConfigDeps = new TMacro(expname);
335 return fConfigDeps;
336}
337
338//______________________________________________________________________________
339void AliAnalysisTaskCfg::SetConfigMacro(TMacro *macro)
340{
341// Set the macro for configuring deps from outside. This will discard the
342// existing macro if any. The provided macro will be owned by this object.
343 if (fConfigDeps) delete fConfigDeps;
344 fConfigDeps = macro;
345}
346
347//______________________________________________________________________________
348Long64_t AliAnalysisTaskCfg::ExecuteConfigMacro()
349{
350// Execute macro to configure dependencies. No arguments are supported.
351 if (!fConfigDeps) {
352 Error("ExecuteConfigMacro", "Call OpenConfigMacro() first");
353 return -1;
354 }
355 if (!CheckLoadLibraries()) {
356 Error("ExecuteConfigMacro", "Cannot load requested libraries: %s", fLibs.Data());
357 return -1;
358 }
359 return fConfigDeps->Exec();
360}
361
362//______________________________________________________________________________
363void AliAnalysisTaskCfg::SetDataTypes(const char *types)
364{
365// Sets the data types supported by the module. Stored in upper case.
366 fDataTypes = types;
367 fDataTypes.ToUpper();
368}
369
370//______________________________________________________________________________
371Bool_t AliAnalysisTaskCfg::SupportsData(const char *type) const
372{
373// Checks if the given data type is supported.
374 TString stype = type;
375 stype.ToUpper();
376 return fDataTypes.Contains(stype);
377}
378
379//______________________________________________________________________________
380void AliAnalysisTaskCfg::Print(Option_t * option) const
381{
382// Print content of the module.
383 TString opt(option);
384 Bool_t full = (opt.Length())?kTRUE:kFALSE;
385 printf("====================================================================\n");
386 printf("# Analysis task: %s\n", GetName());
387 printf("# Supported data types: %s\n", fDataTypes.Data());
388 printf("# Extra libraries: %s\n", fLibs.Data());
389 printf("# Extra dependencies: %s\n", fDeps.Data());
390 if (fConfigDeps) {
391 printf("# Macro to configure deps: %s\n", fConfigDeps->GetTitle());
392 if (full) fConfigDeps->Print();
393 }
394 printf("# Macro connecting this task: %s\n", fMacroName.Data());
395 printf("# Arguments to run the macro: %s\n", fMacroArgs.Data());
396 if (full) {
397 if (fMacro) fMacro->Print();
398 else {
3e40fd4c 399 TMacro macro(gSystem->ExpandPathName(fMacroName.Data()));
4579e070 400 macro.Print();
401 }
402 }
403}
404
405//______________________________________________________________________________
406void AliAnalysisTaskCfg::SaveAs(const char *filename, Option_t *option) const
407{
408// Save the configuration module as text file in the form key:value. The
409// option can be APPEND, otherwise the file gets overwritten.
410 TString opt(option);
411 opt.ToUpper();
412 ios::openmode mode = ios::out;
413 if (opt == "APPEND") mode = ios::app;
414 ofstream out;
415 out.open(filename, mode);
416 if (out.bad()) {
417 Error("SaveAs", "Bad file name: %s", filename);
418 return;
419 }
420 out << "#Module.Begin " << GetName() << endl;
421 out << "#Module.Libs " << fLibs << endl;
422 out << "#Module.Deps " << fDeps << endl;
423 out << "#Module.DataTypes " << fDataTypes << endl;
424 out << "#Module.MacroName " << fMacroName << endl;
425 out << "#Module.MacroArgs " << fMacroArgs << endl;
426 if (fConfigDeps) {
427 out << "#Config.Deps " << fConfigDeps->GetTitle() << endl;
428 }
429}
430
431
432//______________________________________________________________________________
433const char *AliAnalysisTaskCfg::DecodeValue(TString &line)
434{
435// Decode the value string from the line
436 TString value = line(line.Index(' '),line.Length());
437 value = value.Strip(TString::kLeading,' ');
438 value = value.Strip(TString::kTrailing,' ');
439 return value.Data();
440}
441
442//______________________________________________________________________________
443TObjArray *AliAnalysisTaskCfg::ExtractModulesFrom(const char *filename)
444{
445// Read all modules from a text file and add them to an object array. The
446// caller must delete the array at the end. Any module must start with a line
447// containing: #Module.Begin
448 TString expname = gSystem->ExpandPathName(filename);
449 if (gSystem->AccessPathName(expname)) {
450 ::Error("ExtractModulesFrom", "Cannot open file %s", filename);
451 return 0;
452 }
453 AliAnalysisTaskCfg *cfg = 0;
454 TObjArray *array = 0;
455 ifstream in;
456 in.open(expname);
457 char cline[1024];
458 TString line;
459 TString decode;
460 TMacro *addMacro = 0;
461 TMacro *addConfig = 0;
462 while (in.good()) {
463 in.getline(cline,1024);
464 line = cline;
465 if (line.BeginsWith("#Module.Begin")) {
466 // New module found, save previous if any
467 if (cfg) {
468 if (addMacro) cfg->SetMacro(addMacro);
469 if (addConfig) cfg->SetConfigMacro(addConfig);
470 if (!array) array = new TObjArray();
471 array->Add(cfg);
472 }
473 // Decode module name from the line
474 decode = AliAnalysisTaskCfg::DecodeValue(line);
475 cfg = new AliAnalysisTaskCfg(decode);
476 addMacro = 0;
477 addConfig = 0;
478 } else if (cfg && line.BeginsWith("#Module.Libs")) {
479 // Libraries section
480 decode = AliAnalysisTaskCfg::DecodeValue(line);
481 cfg->SetLibraries(decode);
482 } else if (cfg && line.BeginsWith("#Module.Deps")) {
483 // Dependencies section
484 decode = AliAnalysisTaskCfg::DecodeValue(line);
485 cfg->SetDependencies(decode);
486 } else if (cfg && line.BeginsWith("#Module.DataTypes")) {
487 // Data types
488 decode = AliAnalysisTaskCfg::DecodeValue(line);
489 cfg->SetDataTypes(decode);
490 } else if (cfg && line.BeginsWith("#Module.MacroName")) {
491 // Name of the add task macro (including path)
492 decode = AliAnalysisTaskCfg::DecodeValue(line);
493 cfg->SetMacroName(decode);
494 } else if (cfg && line.BeginsWith("#Module.MacroArgs")) {
495 // Arguments for the AddTask macro
496 decode = AliAnalysisTaskCfg::DecodeValue(line);
497 cfg->SetMacroArgs(decode);
498 } else if (cfg && line.BeginsWith("#Module.StartMacro")) {
499 // Marker for start of the AddTask macro
500 addMacro = new TMacro();
501 TString shortName = gSystem->BaseName(cfg->GetMacroName());
502 shortName = shortName(0,shortName.Index("."));
503 addMacro->SetName(shortName);
504 addMacro->SetTitle(cfg->GetMacroName());
505 } else if (cfg && line.BeginsWith("#Module.StartConfig")) {
506 // Marker for start of the configuration macro
507 addConfig = new TMacro();
508 TString shortName = gSystem->BaseName(cfg->GetMacroName());
509 shortName = shortName(0,shortName.Index("."));
510 shortName += "Config";
511 addConfig->SetName(shortName);
512 shortName.Prepend("/");
513 shortName.Prepend(gSystem->DirName(cfg->GetMacroName()));
514 shortName += ".C";
515 addConfig->SetTitle(shortName);
516 } else if (cfg && line.BeginsWith("#Module.EndMacro")) {
517 // Marker for the end of the embedded macro. EndMacro block mandatory.
518 if (cfg && addMacro) {
519 cfg->SetMacro(addMacro);
520 addMacro = 0;
521 } else {
522 ::Error("ExtractModulesFrom", "Canot end un-started macro block");
523 }
524 } else if (cfg && line.BeginsWith("#Module.EndConfig")) {
525 // Marker for the end of the config macro. EndConfig block is mandatory
526 if (cfg && addConfig) {
15f0154e 527 addConfig->GetListOfLines()->AddFirst(new TObjString(Form("%s() {",gSystem->BaseName(addConfig->GetName()))));
528 addConfig->GetListOfLines()->AddLast(new TObjString("}"));
4579e070 529 cfg->SetConfigMacro(addConfig);
530 addConfig = 0;
531 } else {
532 ::Error("ExtractModulesFrom", "Canot end un-started config macro block");
533 }
534 } else {
535 // Reading a block line
536 if (addMacro) addMacro->AddLine(line);
537 else if (addConfig) addConfig->AddLine(line);
538 }
539 }
540 // Add last found object to the list
541 if (cfg) {
15f0154e 542 if (addMacro) ::Error("ExtractModulesFrom", "#Module.EndMacro block not found");
543 if (addConfig) ::Error("ExtractModulesFrom", "#Module.EndConfig block not found");
4579e070 544 if (!array) array = new TObjArray();
545 array->Add(cfg);
546 }
547 return array;
548}