]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliAnalysisTaskCfg.cxx
EMCAL triggers added in AliTriggerAnalysis + code clean-up
[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 using std::cout;
77 using std::endl;
78 using std::ios;
79 using std::ofstream;
80 using std::ifstream;
81 ClassImp(AliAnalysisTaskCfg)
82
83 //______________________________________________________________________________
84 AliAnalysisTaskCfg::AliAnalysisTaskCfg()
85                    :TNamed(),
86                     fMacroName(),
87                     fMacroArgs(),
88                     fLibs(),
89                     fDeps(),
90                     fDataTypes(),
91                     fOutputFile(),
92                     fTerminateFile(),
93                     fMacro(0),
94                     fConfigDeps(0),
95                     fRAddTask(0)
96 {
97 // I/O constructor.
98 }
99
100 //______________________________________________________________________________
101 AliAnalysisTaskCfg::AliAnalysisTaskCfg(const char *name)
102                    :TNamed(name,""),
103                     fMacroName(),
104                     fMacroArgs(),
105                     fLibs(),
106                     fDeps(),
107                     fDataTypes(),
108                     fOutputFile(),
109                     fTerminateFile(),
110                     fMacro(0),
111                     fConfigDeps(0),
112                     fRAddTask(0)
113 {
114 // Constructor. All configuration objects need to be named since they are looked
115 // for by name.
116 }
117
118 //______________________________________________________________________________
119 AliAnalysisTaskCfg::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),
126                     fOutputFile(other.fOutputFile),
127                     fTerminateFile(other.fTerminateFile),
128                     fMacro(0),
129                     fConfigDeps(0),
130                     fRAddTask(0)
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 //______________________________________________________________________________
138 AliAnalysisTaskCfg::~AliAnalysisTaskCfg()
139 {
140 // Destructor.
141    delete fMacro;
142    delete fConfigDeps;
143 }
144
145 //______________________________________________________________________________
146 AliAnalysisTaskCfg& 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;
156    fOutputFile = other.fOutputFile;
157    fTerminateFile = other.fTerminateFile;
158    if (other.fMacro) fMacro = new TMacro(*other.fMacro);
159    if (other.fConfigDeps) fConfigDeps = new TMacro(*other.fConfigDeps);
160    fRAddTask  = other.fRAddTask;
161    return *this;
162 }
163    
164 //______________________________________________________________________________
165 TMacro *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);
176    else              expname = gSystem->ExpandPathName(fMacroName.Data());
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 //______________________________________________________________________________
191 void 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 //______________________________________________________________________________
200 Long64_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 
204 // with stored fMacroArgs unless new arguments are provided. The flag IsLoaded
205 // is set once the macro was successfully executed.
206    if (IsLoaded()) return kTRUE;
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    
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();
222    TString args = newargs;
223    if (args.IsNull()) args = fMacroArgs;
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    }
231    Int_t ntasks = mgr->GetTasks()->GetEntriesFast();
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);
238    if (retval) {
239       TObject::SetBit(AliAnalysisTaskCfg::kLoaded, kTRUE);
240       fRAddTask = reinterpret_cast<TObject*>(retval);
241       if (fConfigDeps && dynamic_cast<TObject*>(fRAddTask)) {
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    }
252    Info("ExecuteMacro", "Macro %s added %d tasks to the manager", fMacro->GetName(), ntasks-ntasks0);
253    return retval;
254 }
255
256 //______________________________________________________________________________
257 Int_t AliAnalysisTaskCfg::GetNlibs() const
258 {
259 // Returns number of requested libraries.
260    if (fLibs.IsNull()) return 0;
261    TObjArray *list  = fLibs.Tokenize(",");
262    Int_t nlibs = list->GetEntriesFast();
263    delete list;
264    return nlibs;
265 }
266
267 //______________________________________________________________________________
268 const 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;
273    static TString libname;
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 //______________________________________________________________________________
284 Bool_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       TString libext = library;
294       libext.Append(".");
295       Int_t loaded = strlen(gSystem->GetLibraries(libext,"",kFALSE));
296       if (!loaded) loaded = gSystem->Load(library);
297       if (loaded < 0) {
298          Error("CheckLoadLibraries", "Cannot load library %s", library.Data());
299          return kFALSE;
300       }
301    }
302    return kTRUE;
303 }
304
305 //______________________________________________________________________________
306 Bool_t AliAnalysisTaskCfg::NeedsLibrary(const char *lib) const
307 {
308 // Check if a given library is needed by the module.
309    TString libname = lib;
310    libname.ReplaceAll(".so","");
311    if (libname.BeginsWith("lib")) libname.Remove(0, 3);
312    return fLibs.Contains(libname);
313 }
314
315 //______________________________________________________________________________
316 Int_t AliAnalysisTaskCfg::GetNdeps() const
317 {
318 // Returns number of requested libraries.
319    if (fDeps.IsNull()) return 0;
320    Int_t ndeps = fDeps.CountChar(',')+1;
321    return ndeps;
322 }
323
324 //______________________________________________________________________________
325 const char *AliAnalysisTaskCfg::GetDependency(Int_t i) const
326 {
327 // Returns library name for the i-th library.
328    Int_t ndeps = GetNdeps();
329    if (i>=ndeps) return 0;
330    static TString depname;
331    TObjArray *list  = fDeps.Tokenize(",");
332    depname = list->At(i)->GetName();
333    depname.ReplaceAll(" ","");
334    delete list;
335    return depname.Data();
336 }
337
338 //______________________________________________________________________________
339 Bool_t AliAnalysisTaskCfg::NeedsDependency(const char *dep) const
340 {
341 // Check if a given library is needed by the module.
342    Int_t indmin = 0;
343    Int_t indmax = 0;
344    Int_t len = fDeps.Length();
345    TString crt;
346    while (indmax<len) {
347       indmax = fDeps.Index(",",indmin);
348       if (indmax < 0) indmax = len;
349       // indmin points to the beginning of the string while indmax to the end+1
350       crt = fDeps(indmin, indmax-indmin);
351       if (crt==dep) return kTRUE;
352       indmin = indmax+1;
353    }
354    return kFALSE;
355 }
356
357 //______________________________________________________________________________
358 TMacro *AliAnalysisTaskCfg::OpenConfigMacro(const char *name)
359 {
360 // Opens the specified macro if name is not empty.
361    if (fConfigDeps) {
362       delete fConfigDeps;
363       fConfigDeps = 0;
364    }
365    
366    TString expname = gSystem->ExpandPathName(name);
367    if (expname.IsNull()) {
368       Error("OpenConfigMacro", "Macro name not provided");
369       return 0;
370    }   
371    if (gSystem->AccessPathName(expname)) {
372       Error("OpenMacro", "Macro: %s cannot be opened.", expname.Data());
373       return 0;
374    }
375    fConfigDeps = new TMacro(expname);
376    return fConfigDeps;
377 }   
378
379 //______________________________________________________________________________
380 void AliAnalysisTaskCfg::SetConfigMacro(TMacro *macro)
381 {
382 // Set the macro for configuring deps from outside. This will discard the 
383 // existing macro if any. The provided macro will be owned by this object.
384    if (fConfigDeps) delete fConfigDeps;
385    fConfigDeps = macro;
386 }   
387   
388 //______________________________________________________________________________
389 Long64_t AliAnalysisTaskCfg::ExecuteConfigMacro()
390 {
391 // Execute macro to configure dependencies. No arguments are supported.
392    if (!fConfigDeps) {
393       Error("ExecuteConfigMacro", "Call OpenConfigMacro() first");
394       return -1;
395    }
396    if (!CheckLoadLibraries()) {
397       Error("ExecuteConfigMacro", "Cannot load requested libraries: %s", fLibs.Data());
398       return -1;
399    }
400    Int_t error = 0;
401    Long64_t retval = fConfigDeps->Exec("", &error);
402    if (error != TInterpreter::kNoError)
403    {
404       Error("ExecuteMacro", "Macro interpretation failed");
405       return -1;
406    }
407    return retval;
408 }
409
410 //______________________________________________________________________________
411 void AliAnalysisTaskCfg::SetDataTypes(const char *types)
412 {
413 // Sets the data types supported by the module. Stored in upper case.
414    fDataTypes = types;
415    fDataTypes.ToUpper();
416 }
417
418 //______________________________________________________________________________
419 Bool_t AliAnalysisTaskCfg::SupportsData(const char *type) const
420 {
421 // Checks if the given data type is supported.
422    TString stype = type;
423    stype.ToUpper();
424    return fDataTypes.Contains(stype);
425 }
426
427 //______________________________________________________________________________
428 void AliAnalysisTaskCfg::Print(Option_t * option) const
429 {
430 // Print content of the module.
431    TString opt(option);
432    Bool_t full = (opt.Length())?kTRUE:kFALSE;
433    printf("====================================================================\n");
434    printf("# Analysis task:                %s\n", GetName());
435    printf("# Supported data types:         %s\n", fDataTypes.Data());
436    printf("# Extra libraries:              %s\n", fLibs.Data());
437    printf("# Extra dependencies:           %s\n", fDeps.Data());
438    if (fConfigDeps) {
439       printf("# Macro to configure deps:      %s\n", fConfigDeps->GetTitle());
440       if (full) fConfigDeps->Print();
441    }   
442    printf("# Macro connecting this task:   %s\n", fMacroName.Data());
443    printf("# Arguments to run the macro:   %s\n", fMacroArgs.Data());
444    if (full) {
445       if (fMacro) fMacro->Print();
446       else {
447          TMacro macro(gSystem->ExpandPathName(fMacroName.Data()));
448          macro.Print();
449       }   
450    }   
451 }
452    
453 //______________________________________________________________________________
454 void AliAnalysisTaskCfg::SaveAs(const char *filename, Option_t *option) const
455 {
456 // Save the configuration module as text file in the form key:value. The
457 // option can be APPEND, otherwise the file gets overwritten.
458    TString opt(option);
459    opt.ToUpper();
460    ios::openmode mode = ios::out;
461    if (opt == "APPEND") mode = ios::app;
462    ofstream out;
463    out.open(filename, mode);
464    if (out.bad()) {
465       Error("SaveAs", "Bad file name: %s", filename);
466       return;
467    }
468    out << "#Module.Begin " << GetName() << endl;
469    out << "#Module.Libs " << fLibs << endl;
470    out << "#Module.Deps " << fDeps << endl;
471    out << "#Module.DataTypes " << fDataTypes << endl;
472    out << "#Module.OutputFile " << fOutputFile << endl;
473    out << "#Module.TerminateFile " << fTerminateFile << endl;
474    out << "#Module.MacroName " << fMacroName << endl;
475    out << "#Module.MacroArgs " << fMacroArgs << endl;
476    if (fConfigDeps) {
477       out << "#Config.Deps " << fConfigDeps->GetTitle() << endl;
478    }      
479 }
480
481
482 //______________________________________________________________________________
483 const char *AliAnalysisTaskCfg::DecodeValue(TString &line)
484 {
485 // Decode the value string from the line
486    static TString value;
487    value = line(line.Index(' '),line.Length());
488    value = value.Strip(TString::kLeading,' ');
489    value = value.Strip(TString::kTrailing,' ');
490    return value.Data();
491 }
492    
493 //______________________________________________________________________________
494 TObjArray *AliAnalysisTaskCfg::ExtractModulesFrom(const char *filename)
495 {
496 // Read all modules from a text file and add them to an object array. The
497 // caller must delete the array at the end. Any module must start with a line
498 // containing: #Module.Begin
499    TString expname = gSystem->ExpandPathName(filename);
500    if (gSystem->AccessPathName(expname)) {
501       ::Error("ExtractModulesFrom", "Cannot open file %s", filename);
502       return 0;
503    }   
504    AliAnalysisTaskCfg *cfg = 0;
505    TObjArray *array = 0;
506    ifstream in;
507    in.open(expname);
508    char cline[1024];
509    TString line;
510    TString decode;
511    TMacro *addMacro = 0;
512    TMacro *addConfig = 0;
513    while (in.good()) {
514       in.getline(cline,1024);
515       line = cline;
516       if (line.BeginsWith("#Module.Begin")) {
517       // New module found, save previous if any
518          if (cfg) {
519             if (addMacro) cfg->SetMacro(addMacro);
520             if (addConfig) cfg->SetConfigMacro(addConfig);
521             if (!array) array = new TObjArray();
522             array->Add(cfg);
523          }
524          // Decode module name from the line
525          decode = AliAnalysisTaskCfg::DecodeValue(line);
526          cfg = new AliAnalysisTaskCfg(decode);
527          addMacro = 0;
528          addConfig = 0;
529       } else if (cfg && line.BeginsWith("#Module.Libs")) {
530          // Libraries section
531          decode = AliAnalysisTaskCfg::DecodeValue(line);
532          cfg->SetLibraries(decode);
533       } else if (cfg && line.BeginsWith("#Module.Deps")) {
534          // Dependencies section
535          decode = AliAnalysisTaskCfg::DecodeValue(line);
536          cfg->SetDependencies(decode);
537       } else if (cfg && line.BeginsWith("#Module.DataTypes")) {
538          // Data types
539          decode = AliAnalysisTaskCfg::DecodeValue(line);
540          cfg->SetDataTypes(decode);
541       } else if (cfg && line.BeginsWith("#Module.OutputFile")) {
542          // Desired output file name (via SetCommonOutput)
543          decode = AliAnalysisTaskCfg::DecodeValue(line);
544          cfg->SetOutputFileName(decode);
545       } else if (cfg && line.BeginsWith("#Module.TerminateFile")) {
546          // Custom file name produced in Terminate if any
547          decode = AliAnalysisTaskCfg::DecodeValue(line);
548          cfg->SetTerminateFileName(decode);
549       } else if (cfg && line.BeginsWith("#Module.MacroName")) {
550          // Name of the add task macro (including path)
551          decode = AliAnalysisTaskCfg::DecodeValue(line);
552          cfg->SetMacroName(decode);
553       } else if (cfg && line.BeginsWith("#Module.MacroArgs")) {
554          // Arguments for the AddTask macro
555          decode = AliAnalysisTaskCfg::DecodeValue(line);
556          cfg->SetMacroArgs(decode);
557       } else if (cfg && line.BeginsWith("#Module.StartMacro")) {
558          // Marker for start of the AddTask macro
559          addMacro = new TMacro();
560          TString shortName = gSystem->BaseName(cfg->GetMacroName());
561          shortName = shortName(0,shortName.Index("."));
562          addMacro->SetName(shortName);
563          addMacro->SetTitle(cfg->GetMacroName());
564       } else if (cfg && line.BeginsWith("#Module.StartConfig")) {
565          // Marker for start of the configuration macro
566          addConfig = new TMacro();
567 //         TString shortName = gSystem->BaseName(cfg->GetMacroName());
568 //         shortName = shortName(0,shortName.Index("."));
569          TString shortName = cfg->GetName();
570          shortName += "_Config";
571          addConfig->SetName(shortName);
572 //         shortName.Prepend("/");
573 //         shortName.Prepend(gSystem->DirName(cfg->GetMacroName()));
574          shortName += ".C";
575          addConfig->SetTitle(shortName);
576       } else if (cfg && line.BeginsWith("#Module.EndMacro")) {
577          // Marker for the end of the embedded macro. EndMacro block mandatory.
578          if (cfg && addMacro) {
579             cfg->SetMacro(addMacro);
580             addMacro = 0;
581          } else {
582             ::Error("ExtractModulesFrom", "Canot end un-started macro block");
583          }
584       } else if (cfg && line.BeginsWith("#Module.EndConfig")) {
585          // Marker for the end of the config macro. EndConfig block is mandatory
586          if (cfg && addConfig) {
587             addConfig->GetListOfLines()->AddFirst(new TObjString(Form("%s() {",gSystem->BaseName(addConfig->GetName()))));
588             addConfig->GetListOfLines()->AddLast(new TObjString("}"));
589             cfg->SetConfigMacro(addConfig);
590             addConfig = 0;
591          } else {
592             ::Error("ExtractModulesFrom", "Canot end un-started config macro block");
593          }
594       } else {   
595          // Reading a block line
596          if (addMacro) addMacro->AddLine(line);
597          else if (addConfig) addConfig->AddLine(line);
598       }   
599    }
600    // Add last found object to the list
601    if (cfg) {
602       if (addMacro) ::Error("ExtractModulesFrom", "#Module.EndMacro block not found");         
603       if (addConfig) ::Error("ExtractModulesFrom", "#Module.EndConfig block not found");
604       if (!array) array = new TObjArray();
605       array->Add(cfg);
606    }
607    return array;  
608 }