]> git.uio.no Git - u/mrichter/AliRoot.git/blame - ANALYSIS/AliAnalysisTaskCfg.cxx
DQM configure file
[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"
5f254645 22#include "TInterpreter.h"
4579e070 23#include "TSystem.h"
24#include "TObjArray.h"
15f0154e 25#include "TObjString.h"
26#include "TList.h"
4579e070 27
28// Author: Andrei Gheata, 12/08/2011
29
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//==============================================================================
35
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.
46// An example:
47// Content of file: QAsym.cfg
48/*
49# Lines that do not start with #Module are ignored, except those in embedded
50 macro blocks
51#Module.Begin QAsym
2bfe5463 52#Module.Libs PWGPP
4579e070 53#Module.Deps PhysicsSelection
54#Module.DataTypes ESD, AOD, MC
2bfe5463 55#Module.MacroName $ALICE_ROOT/PWGPP/PilotTrain/AddTaskQAsym.C
4579e070 56#Module.MacroArgs 0, AliVEvent::kAnyINT, AliVEvent::kHighMult, AliVEvent::kEMC7, AliVEvent::kMUU7
192ddca2 57#Module.OutputFile
58#Module.TerminateFile
4579e070 59#Module.StartConfig
60__R_ADDTASK__->SelectCollisionCandidates();
61#Module.EndConfig
62*/
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.
71//
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.
74
75
c82bb898 76using std::cout;
77using std::endl;
78using std::ios;
79using std::ofstream;
80using std::ifstream;
4579e070 81ClassImp(AliAnalysisTaskCfg)
82
83//______________________________________________________________________________
84AliAnalysisTaskCfg::AliAnalysisTaskCfg()
85 :TNamed(),
86 fMacroName(),
87 fMacroArgs(),
88 fLibs(),
89 fDeps(),
90 fDataTypes(),
192ddca2 91 fOutputFile(),
92 fTerminateFile(),
4579e070 93 fMacro(0),
15f0154e 94 fConfigDeps(0),
95 fRAddTask(0)
4579e070 96{
97// I/O constructor.
98}
99
100//______________________________________________________________________________
101AliAnalysisTaskCfg::AliAnalysisTaskCfg(const char *name)
102 :TNamed(name,""),
103 fMacroName(),
104 fMacroArgs(),
105 fLibs(),
106 fDeps(),
107 fDataTypes(),
192ddca2 108 fOutputFile(),
109 fTerminateFile(),
4579e070 110 fMacro(0),
15f0154e 111 fConfigDeps(0),
112 fRAddTask(0)
4579e070 113{
114// Constructor. All configuration objects need to be named since they are looked
115// for by name.
116}
117
118//______________________________________________________________________________
119AliAnalysisTaskCfg::AliAnalysisTaskCfg(const AliAnalysisTaskCfg &other)
120 :TNamed(other),
121 fMacroName(other.fMacroName),
122 fMacroArgs(other.fMacroArgs),
123 fLibs(other.fLibs),
124 fDeps(other.fDeps),
125 fDataTypes(other.fDataTypes),
192ddca2 126 fOutputFile(other.fOutputFile),
127 fTerminateFile(other.fTerminateFile),
4579e070 128 fMacro(0),
15f0154e 129 fConfigDeps(0),
130 fRAddTask(0)
4579e070 131{
132// Copy constructor.
133 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
134 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
135}
136
137//______________________________________________________________________________
138AliAnalysisTaskCfg::~AliAnalysisTaskCfg()
139{
140// Destructor.
141 delete fMacro;
142 delete fConfigDeps;
143}
144
145//______________________________________________________________________________
146AliAnalysisTaskCfg& AliAnalysisTaskCfg::operator=(const AliAnalysisTaskCfg &other)
147{
148// Assignment operator.
149 if (&other == this) return *this;
150 TNamed::operator=(other);
151 fMacroName = other.fMacroName;
152 fMacroArgs = other.fMacroArgs;
153 fLibs = other.fLibs;
154 fDeps = other.fDeps;
155 fDataTypes = other.fDataTypes;
192ddca2 156 fOutputFile = other.fOutputFile;
157 fTerminateFile = other.fTerminateFile;
4579e070 158 if (other.fMacro) fMacro = new TMacro(*other.fMacro);
159 if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
15f0154e 160 fRAddTask = other.fRAddTask;
4579e070 161 return *this;
162}
163
164//______________________________________________________________________________
165TMacro *AliAnalysisTaskCfg::OpenMacro(const char *name)
166{
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
170 if (fMacro) {
171 delete fMacro;
172 fMacro = 0;
173 }
174 TString expname;
175 if (strlen(name)) expname = gSystem->ExpandPathName(name);
3e40fd4c 176 else expname = gSystem->ExpandPathName(fMacroName.Data());
4579e070 177 if (expname.IsNull()) {
178 Error("OpenMacro", "Macro name not provided and not previously set");
179 return 0;
180 }
181 if (gSystem->AccessPathName(expname)) {
182 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
183 return 0;
184 }
185 if (strlen(name)) fMacroName = name;
186 fMacro = new TMacro(expname);
187 return fMacro;
188}
189
190//______________________________________________________________________________
191void AliAnalysisTaskCfg::SetMacro(TMacro *macro)
192{
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;
196 fMacro = macro;
197}
198
199//______________________________________________________________________________
200Long64_t AliAnalysisTaskCfg::ExecuteMacro(const char *newargs)
201{
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
77f34eae 204// with stored fMacroArgs unless new arguments are provided. The flag IsLoaded
205// is set once the macro was successfully executed.
21ca8e59 206 if (IsLoaded()) return kTRUE;
4579e070 207 if (!fMacro && !OpenMacro()) {
208 Error("ExecuteMacro", "Cannot execute this macro");
209 return -1;
210 }
211 if (!CheckLoadLibraries()) {
212 Error("ExecuteMacro", "Cannot load requested libraries: %s", fLibs.Data());
213 return -1;
214 }
215
15f0154e 216 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
217 if (!mgr) {
218 Error("ExecuteMacro", "Analysis manager not defined yet");
219 return -1;
220 }
221 Int_t ntasks0 = mgr->GetTasks()->GetEntriesFast();
4579e070 222 TString args = newargs;
223 if (args.IsNull()) args = fMacroArgs;
5f254645 224 Int_t error = 0;
225 Long64_t retval = fMacro->Exec(args, &error);
226 if (error != TInterpreter::kNoError)
227 {
228 Error("ExecuteMacro", "Macro interpretation failed");
229 return -1;
230 }
5e8c7c22 231 Int_t ntasks = mgr->GetTasks()->GetEntriesFast();
5f254645 232 if (ntasks<=ntasks0)
233 {
234 Error("ExecuteMacro", "The macro did not add any tasks to the manager");
235 return -1;
236 }
237 Long64_t ptrTask = (Long64_t)mgr->GetTasks()->At(ntasks0);
5e8c7c22 238 if (retval >= ptrTask) {
15f0154e 239 TObject::SetBit(AliAnalysisTaskCfg::kLoaded, kTRUE);
240 fRAddTask = reinterpret_cast<TObject*>(retval);
5e8c7c22 241 if (fConfigDeps && dynamic_cast<TObject*>(fRAddTask)) {
15f0154e 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__");
246 if (line) {
247 TList *lines = fConfigDeps->GetListOfLines();
248 lines->AddBefore(line, new TObjString(classname));
249 }
250 }
251 }
15f0154e 252 Info("ExecuteMacro", "Macro %s added %d tasks to the manager", fMacro->GetName(), ntasks-ntasks0);
77f34eae 253 return retval;
4579e070 254}
255
256//______________________________________________________________________________
257Int_t AliAnalysisTaskCfg::GetNlibs() const
258{
259// Returns number of requested libraries.
260 if (fLibs.IsNull()) return 0;
5ff9ab50 261 TObjArray *list = fLibs.Tokenize(",");
262 Int_t nlibs = list->GetEntriesFast();
263 delete list;
4579e070 264 return nlibs;
265}
266
267//______________________________________________________________________________
268const char *AliAnalysisTaskCfg::GetLibrary(Int_t i) const
269{
270// Returns library name for the i-th library.
271 Int_t nlibs = GetNlibs();
272 if (i>=nlibs) return 0;
ca1b69aa 273 static TString libname;
4579e070 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);
279 delete list;
280 return libname.Data();
281}
282
283//______________________________________________________________________________
284Bool_t AliAnalysisTaskCfg::CheckLoadLibraries() const
285{
286// Check if all requested libraries were loaded, otherwise load them. If some
287// library cannot be loaded return false.
288 TString library;
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);
295 if (loaded < 0) {
296 Error("CheckLoadLibraries", "Cannot load library %s", library.Data());
297 return kFALSE;
298 }
299 }
300 return kTRUE;
301}
302
303//______________________________________________________________________________
304Bool_t AliAnalysisTaskCfg::NeedsLibrary(const char *lib) const
305{
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);
311}
312
313//______________________________________________________________________________
314Int_t AliAnalysisTaskCfg::GetNdeps() const
315{
316// Returns number of requested libraries.
317 if (fDeps.IsNull()) return 0;
318 Int_t ndeps = fDeps.CountChar(',')+1;
319 return ndeps;
320}
321
322//______________________________________________________________________________
323const char *AliAnalysisTaskCfg::GetDependency(Int_t i) const
324{
325// Returns library name for the i-th library.
326 Int_t ndeps = GetNdeps();
327 if (i>=ndeps) return 0;
ca1b69aa 328 static TString depname;
4579e070 329 TObjArray *list = fDeps.Tokenize(",");
330 depname = list->At(i)->GetName();
331 depname.ReplaceAll(" ","");
332 delete list;
333 return depname.Data();
334}
335
336//______________________________________________________________________________
337Bool_t AliAnalysisTaskCfg::NeedsDependency(const char *dep) const
338{
339// Check if a given library is needed by the module.
340 return fDeps.Contains(dep);
341}
342
343//______________________________________________________________________________
344TMacro *AliAnalysisTaskCfg::OpenConfigMacro(const char *name)
345{
346// Opens the specified macro if name is not empty.
347 if (fConfigDeps) {
348 delete fConfigDeps;
349 fConfigDeps = 0;
350 }
351
352 TString expname = gSystem->ExpandPathName(name);
353 if (expname.IsNull()) {
354 Error("OpenConfigMacro", "Macro name not provided");
355 return 0;
356 }
357 if (gSystem->AccessPathName(expname)) {
358 Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
359 return 0;
360 }
361 fConfigDeps = new TMacro(expname);
362 return fConfigDeps;
363}
364
365//______________________________________________________________________________
366void AliAnalysisTaskCfg::SetConfigMacro(TMacro *macro)
367{
368// Set the macro for configuring deps from outside. This will discard the
369// existing macro if any. The provided macro will be owned by this object.
370 if (fConfigDeps) delete fConfigDeps;
371 fConfigDeps = macro;
372}
373
374//______________________________________________________________________________
375Long64_t AliAnalysisTaskCfg::ExecuteConfigMacro()
376{
377// Execute macro to configure dependencies. No arguments are supported.
378 if (!fConfigDeps) {
379 Error("ExecuteConfigMacro", "Call OpenConfigMacro() first");
380 return -1;
381 }
382 if (!CheckLoadLibraries()) {
383 Error("ExecuteConfigMacro", "Cannot load requested libraries: %s", fLibs.Data());
384 return -1;
385 }
e51ea326 386 Int_t error = 0;
387 Long64_t retval = fConfigDeps->Exec("", &error);
388 if (error != TInterpreter::kNoError)
389 {
390 Error("ExecuteMacro", "Macro interpretation failed");
391 return -1;
392 }
393 return retval;
4579e070 394}
395
396//______________________________________________________________________________
397void AliAnalysisTaskCfg::SetDataTypes(const char *types)
398{
399// Sets the data types supported by the module. Stored in upper case.
400 fDataTypes = types;
401 fDataTypes.ToUpper();
402}
403
404//______________________________________________________________________________
405Bool_t AliAnalysisTaskCfg::SupportsData(const char *type) const
406{
407// Checks if the given data type is supported.
408 TString stype = type;
409 stype.ToUpper();
410 return fDataTypes.Contains(stype);
411}
412
413//______________________________________________________________________________
414void AliAnalysisTaskCfg::Print(Option_t * option) const
415{
416// Print content of the module.
417 TString opt(option);
418 Bool_t full = (opt.Length())?kTRUE:kFALSE;
419 printf("====================================================================\n");
420 printf("# Analysis task: %s\n", GetName());
421 printf("# Supported data types: %s\n", fDataTypes.Data());
422 printf("# Extra libraries: %s\n", fLibs.Data());
423 printf("# Extra dependencies: %s\n", fDeps.Data());
424 if (fConfigDeps) {
425 printf("# Macro to configure deps: %s\n", fConfigDeps->GetTitle());
426 if (full) fConfigDeps->Print();
427 }
428 printf("# Macro connecting this task: %s\n", fMacroName.Data());
429 printf("# Arguments to run the macro: %s\n", fMacroArgs.Data());
430 if (full) {
431 if (fMacro) fMacro->Print();
432 else {
3e40fd4c 433 TMacro macro(gSystem->ExpandPathName(fMacroName.Data()));
4579e070 434 macro.Print();
435 }
436 }
437}
438
439//______________________________________________________________________________
440void AliAnalysisTaskCfg::SaveAs(const char *filename, Option_t *option) const
441{
442// Save the configuration module as text file in the form key:value. The
443// option can be APPEND, otherwise the file gets overwritten.
444 TString opt(option);
445 opt.ToUpper();
446 ios::openmode mode = ios::out;
447 if (opt == "APPEND") mode = ios::app;
448 ofstream out;
449 out.open(filename, mode);
450 if (out.bad()) {
451 Error("SaveAs", "Bad file name: %s", filename);
452 return;
453 }
454 out << "#Module.Begin " << GetName() << endl;
455 out << "#Module.Libs " << fLibs << endl;
456 out << "#Module.Deps " << fDeps << endl;
457 out << "#Module.DataTypes " << fDataTypes << endl;
192ddca2 458 out << "#Module.OutputFile " << fOutputFile << endl;
459 out << "#Module.TerminateFile " << fTerminateFile << endl;
4579e070 460 out << "#Module.MacroName " << fMacroName << endl;
461 out << "#Module.MacroArgs " << fMacroArgs << endl;
462 if (fConfigDeps) {
463 out << "#Config.Deps " << fConfigDeps->GetTitle() << endl;
464 }
465}
466
467
468//______________________________________________________________________________
469const char *AliAnalysisTaskCfg::DecodeValue(TString &line)
470{
471// Decode the value string from the line
ca1b69aa 472 static TString value;
473 value = line(line.Index(' '),line.Length());
4579e070 474 value = value.Strip(TString::kLeading,' ');
475 value = value.Strip(TString::kTrailing,' ');
476 return value.Data();
477}
478
479//______________________________________________________________________________
480TObjArray *AliAnalysisTaskCfg::ExtractModulesFrom(const char *filename)
481{
482// Read all modules from a text file and add them to an object array. The
483// caller must delete the array at the end. Any module must start with a line
484// containing: #Module.Begin
485 TString expname = gSystem->ExpandPathName(filename);
486 if (gSystem->AccessPathName(expname)) {
487 ::Error("ExtractModulesFrom", "Cannot open file %s", filename);
488 return 0;
489 }
490 AliAnalysisTaskCfg *cfg = 0;
491 TObjArray *array = 0;
492 ifstream in;
493 in.open(expname);
494 char cline[1024];
495 TString line;
496 TString decode;
497 TMacro *addMacro = 0;
498 TMacro *addConfig = 0;
499 while (in.good()) {
500 in.getline(cline,1024);
501 line = cline;
502 if (line.BeginsWith("#Module.Begin")) {
503 // New module found, save previous if any
504 if (cfg) {
505 if (addMacro) cfg->SetMacro(addMacro);
506 if (addConfig) cfg->SetConfigMacro(addConfig);
507 if (!array) array = new TObjArray();
508 array->Add(cfg);
509 }
510 // Decode module name from the line
511 decode = AliAnalysisTaskCfg::DecodeValue(line);
512 cfg = new AliAnalysisTaskCfg(decode);
513 addMacro = 0;
514 addConfig = 0;
515 } else if (cfg && line.BeginsWith("#Module.Libs")) {
516 // Libraries section
517 decode = AliAnalysisTaskCfg::DecodeValue(line);
518 cfg->SetLibraries(decode);
519 } else if (cfg && line.BeginsWith("#Module.Deps")) {
520 // Dependencies section
521 decode = AliAnalysisTaskCfg::DecodeValue(line);
522 cfg->SetDependencies(decode);
523 } else if (cfg && line.BeginsWith("#Module.DataTypes")) {
524 // Data types
525 decode = AliAnalysisTaskCfg::DecodeValue(line);
526 cfg->SetDataTypes(decode);
192ddca2 527 } else if (cfg && line.BeginsWith("#Module.OutputFile")) {
528 // Desired output file name (via SetCommonOutput)
529 decode = AliAnalysisTaskCfg::DecodeValue(line);
530 cfg->SetOutputFileName(decode);
531 } else if (cfg && line.BeginsWith("#Module.TerminateFile")) {
532 // Custom file name produced in Terminate if any
533 decode = AliAnalysisTaskCfg::DecodeValue(line);
534 cfg->SetTerminateFileName(decode);
4579e070 535 } else if (cfg && line.BeginsWith("#Module.MacroName")) {
536 // Name of the add task macro (including path)
537 decode = AliAnalysisTaskCfg::DecodeValue(line);
538 cfg->SetMacroName(decode);
539 } else if (cfg && line.BeginsWith("#Module.MacroArgs")) {
540 // Arguments for the AddTask macro
541 decode = AliAnalysisTaskCfg::DecodeValue(line);
542 cfg->SetMacroArgs(decode);
543 } else if (cfg && line.BeginsWith("#Module.StartMacro")) {
544 // Marker for start of the AddTask macro
545 addMacro = new TMacro();
546 TString shortName = gSystem->BaseName(cfg->GetMacroName());
547 shortName = shortName(0,shortName.Index("."));
548 addMacro->SetName(shortName);
549 addMacro->SetTitle(cfg->GetMacroName());
550 } else if (cfg && line.BeginsWith("#Module.StartConfig")) {
551 // Marker for start of the configuration macro
552 addConfig = new TMacro();
553 TString shortName = gSystem->BaseName(cfg->GetMacroName());
554 shortName = shortName(0,shortName.Index("."));
555 shortName += "Config";
556 addConfig->SetName(shortName);
557 shortName.Prepend("/");
558 shortName.Prepend(gSystem->DirName(cfg->GetMacroName()));
559 shortName += ".C";
560 addConfig->SetTitle(shortName);
561 } else if (cfg && line.BeginsWith("#Module.EndMacro")) {
562 // Marker for the end of the embedded macro. EndMacro block mandatory.
563 if (cfg && addMacro) {
564 cfg->SetMacro(addMacro);
565 addMacro = 0;
566 } else {
567 ::Error("ExtractModulesFrom", "Canot end un-started macro block");
568 }
569 } else if (cfg && line.BeginsWith("#Module.EndConfig")) {
570 // Marker for the end of the config macro. EndConfig block is mandatory
571 if (cfg && addConfig) {
15f0154e 572 addConfig->GetListOfLines()->AddFirst(new TObjString(Form("%s() {",gSystem->BaseName(addConfig->GetName()))));
573 addConfig->GetListOfLines()->AddLast(new TObjString("}"));
4579e070 574 cfg->SetConfigMacro(addConfig);
575 addConfig = 0;
576 } else {
577 ::Error("ExtractModulesFrom", "Canot end un-started config macro block");
578 }
579 } else {
580 // Reading a block line
581 if (addMacro) addMacro->AddLine(line);
582 else if (addConfig) addConfig->AddLine(line);
583 }
584 }
585 // Add last found object to the list
586 if (cfg) {
15f0154e 587 if (addMacro) ::Error("ExtractModulesFrom", "#Module.EndMacro block not found");
588 if (addConfig) ::Error("ExtractModulesFrom", "#Module.EndConfig block not found");
4579e070 589 if (!array) array = new TObjArray();
590 array->Add(cfg);
591 }
592 return array;
593}