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