]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliAnalysisTaskCfg.cxx
bugfix: correct range of DDL for specified detector
[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    Int_t error = 0;
382    Long64_t retval = fConfigDeps->Exec("", &error);
383    if (error != TInterpreter::kNoError)
384    {
385       Error("ExecuteMacro", "Macro interpretation failed");
386       return -1;
387    }
388    return retval;
389 }
390
391 //______________________________________________________________________________
392 void AliAnalysisTaskCfg::SetDataTypes(const char *types)
393 {
394 // Sets the data types supported by the module. Stored in upper case.
395    fDataTypes = types;
396    fDataTypes.ToUpper();
397 }
398
399 //______________________________________________________________________________
400 Bool_t AliAnalysisTaskCfg::SupportsData(const char *type) const
401 {
402 // Checks if the given data type is supported.
403    TString stype = type;
404    stype.ToUpper();
405    return fDataTypes.Contains(stype);
406 }
407
408 //______________________________________________________________________________
409 void AliAnalysisTaskCfg::Print(Option_t * option) const
410 {
411 // Print content of the module.
412    TString opt(option);
413    Bool_t full = (opt.Length())?kTRUE:kFALSE;
414    printf("====================================================================\n");
415    printf("# Analysis task:                %s\n", GetName());
416    printf("# Supported data types:         %s\n", fDataTypes.Data());
417    printf("# Extra libraries:              %s\n", fLibs.Data());
418    printf("# Extra dependencies:           %s\n", fDeps.Data());
419    if (fConfigDeps) {
420       printf("# Macro to configure deps:      %s\n", fConfigDeps->GetTitle());
421       if (full) fConfigDeps->Print();
422    }   
423    printf("# Macro connecting this task:   %s\n", fMacroName.Data());
424    printf("# Arguments to run the macro:   %s\n", fMacroArgs.Data());
425    if (full) {
426       if (fMacro) fMacro->Print();
427       else {
428          TMacro macro(gSystem->ExpandPathName(fMacroName.Data()));
429          macro.Print();
430       }   
431    }   
432 }
433    
434 //______________________________________________________________________________
435 void AliAnalysisTaskCfg::SaveAs(const char *filename, Option_t *option) const
436 {
437 // Save the configuration module as text file in the form key:value. The
438 // option can be APPEND, otherwise the file gets overwritten.
439    TString opt(option);
440    opt.ToUpper();
441    ios::openmode mode = ios::out;
442    if (opt == "APPEND") mode = ios::app;
443    ofstream out;
444    out.open(filename, mode);
445    if (out.bad()) {
446       Error("SaveAs", "Bad file name: %s", filename);
447       return;
448    }
449    out << "#Module.Begin " << GetName() << endl;
450    out << "#Module.Libs " << fLibs << endl;
451    out << "#Module.Deps " << fDeps << endl;
452    out << "#Module.DataTypes " << fDataTypes << endl;
453    out << "#Module.OutputFile " << fOutputFile << endl;
454    out << "#Module.TerminateFile " << fTerminateFile << endl;
455    out << "#Module.MacroName " << fMacroName << endl;
456    out << "#Module.MacroArgs " << fMacroArgs << endl;
457    if (fConfigDeps) {
458       out << "#Config.Deps " << fConfigDeps->GetTitle() << endl;
459    }      
460 }
461
462
463 //______________________________________________________________________________
464 const char *AliAnalysisTaskCfg::DecodeValue(TString &line)
465 {
466 // Decode the value string from the line
467    static TString value;
468    value = line(line.Index(' '),line.Length());
469    value = value.Strip(TString::kLeading,' ');
470    value = value.Strip(TString::kTrailing,' ');
471    return value.Data();
472 }
473    
474 //______________________________________________________________________________
475 TObjArray *AliAnalysisTaskCfg::ExtractModulesFrom(const char *filename)
476 {
477 // Read all modules from a text file and add them to an object array. The
478 // caller must delete the array at the end. Any module must start with a line
479 // containing: #Module.Begin
480    TString expname = gSystem->ExpandPathName(filename);
481    if (gSystem->AccessPathName(expname)) {
482       ::Error("ExtractModulesFrom", "Cannot open file %s", filename);
483       return 0;
484    }   
485    AliAnalysisTaskCfg *cfg = 0;
486    TObjArray *array = 0;
487    ifstream in;
488    in.open(expname);
489    char cline[1024];
490    TString line;
491    TString decode;
492    TMacro *addMacro = 0;
493    TMacro *addConfig = 0;
494    while (in.good()) {
495       in.getline(cline,1024);
496       line = cline;
497       if (line.BeginsWith("#Module.Begin")) {
498       // New module found, save previous if any
499          if (cfg) {
500             if (addMacro) cfg->SetMacro(addMacro);
501             if (addConfig) cfg->SetConfigMacro(addConfig);
502             if (!array) array = new TObjArray();
503             array->Add(cfg);
504          }
505          // Decode module name from the line
506          decode = AliAnalysisTaskCfg::DecodeValue(line);
507          cfg = new AliAnalysisTaskCfg(decode);
508          addMacro = 0;
509          addConfig = 0;
510       } else if (cfg && line.BeginsWith("#Module.Libs")) {
511          // Libraries section
512          decode = AliAnalysisTaskCfg::DecodeValue(line);
513          cfg->SetLibraries(decode);
514       } else if (cfg && line.BeginsWith("#Module.Deps")) {
515          // Dependencies section
516          decode = AliAnalysisTaskCfg::DecodeValue(line);
517          cfg->SetDependencies(decode);
518       } else if (cfg && line.BeginsWith("#Module.DataTypes")) {
519          // Data types
520          decode = AliAnalysisTaskCfg::DecodeValue(line);
521          cfg->SetDataTypes(decode);
522       } else if (cfg && line.BeginsWith("#Module.OutputFile")) {
523          // Desired output file name (via SetCommonOutput)
524          decode = AliAnalysisTaskCfg::DecodeValue(line);
525          cfg->SetOutputFileName(decode);
526       } else if (cfg && line.BeginsWith("#Module.TerminateFile")) {
527          // Custom file name produced in Terminate if any
528          decode = AliAnalysisTaskCfg::DecodeValue(line);
529          cfg->SetTerminateFileName(decode);
530       } else if (cfg && line.BeginsWith("#Module.MacroName")) {
531          // Name of the add task macro (including path)
532          decode = AliAnalysisTaskCfg::DecodeValue(line);
533          cfg->SetMacroName(decode);
534       } else if (cfg && line.BeginsWith("#Module.MacroArgs")) {
535          // Arguments for the AddTask macro
536          decode = AliAnalysisTaskCfg::DecodeValue(line);
537          cfg->SetMacroArgs(decode);
538       } else if (cfg && line.BeginsWith("#Module.StartMacro")) {
539          // Marker for start of the AddTask macro
540          addMacro = new TMacro();
541          TString shortName = gSystem->BaseName(cfg->GetMacroName());
542          shortName = shortName(0,shortName.Index("."));
543          addMacro->SetName(shortName);
544          addMacro->SetTitle(cfg->GetMacroName());
545       } else if (cfg && line.BeginsWith("#Module.StartConfig")) {
546          // Marker for start of the configuration macro
547          addConfig = new TMacro();
548          TString shortName = gSystem->BaseName(cfg->GetMacroName());
549          shortName = shortName(0,shortName.Index("."));
550          shortName += "Config";
551          addConfig->SetName(shortName);
552          shortName.Prepend("/");
553          shortName.Prepend(gSystem->DirName(cfg->GetMacroName()));
554          shortName += ".C";
555          addConfig->SetTitle(shortName);
556       } else if (cfg && line.BeginsWith("#Module.EndMacro")) {
557          // Marker for the end of the embedded macro. EndMacro block mandatory.
558          if (cfg && addMacro) {
559             cfg->SetMacro(addMacro);
560             addMacro = 0;
561          } else {
562             ::Error("ExtractModulesFrom", "Canot end un-started macro block");
563          }
564       } else if (cfg && line.BeginsWith("#Module.EndConfig")) {
565          // Marker for the end of the config macro. EndConfig block is mandatory
566          if (cfg && addConfig) {
567             addConfig->GetListOfLines()->AddFirst(new TObjString(Form("%s() {",gSystem->BaseName(addConfig->GetName()))));
568             addConfig->GetListOfLines()->AddLast(new TObjString("}"));
569             cfg->SetConfigMacro(addConfig);
570             addConfig = 0;
571          } else {
572             ::Error("ExtractModulesFrom", "Canot end un-started config macro block");
573          }
574       } else {   
575          // Reading a block line
576          if (addMacro) addMacro->AddLine(line);
577          else if (addConfig) addConfig->AddLine(line);
578       }   
579    }
580    // Add last found object to the list
581    if (cfg) {
582       if (addMacro) ::Error("ExtractModulesFrom", "#Module.EndMacro block not found");         
583       if (addConfig) ::Error("ExtractModulesFrom", "#Module.EndConfig block not found");
584       if (!array) array = new TObjArray();
585       array->Add(cfg);
586    }
587    return array;  
588 }