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