]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/qaRec/makeResults.C
extend macro to run on batch
[u/mrichter/AliRoot.git] / TRD / qaRec / makeResults.C
1 #if ! defined (__CINT__) || defined (__MAKECINT__)
2 #include "TError.h"
3 #include <TClass.h>
4 #include <TFileMerger.h>
5 #include <TCanvas.h>
6 #include <TH1.h>
7 #include <TGraph.h>
8 #include <TObjArray.h>
9 #include <TObjString.h>
10 #include <TPython.h>
11 #include <TString.h>
12 #include <TROOT.h>
13 #include <TSystem.h>
14 #include <TStyle.h>
15
16 #include "qaRec/AliTRDrecoTask.h"
17
18 #endif
19
20 #include "run.h"
21
22 Char_t *libs[] = {"libProofPlayer.so", "libANALYSIS.so", "libTRDqaRec.so", "libPyROOT"};
23
24 void makeResults(Char_t *args = "ALL")
25 {
26         // Load Libraries in interactive mode
27   Int_t nlibs = static_cast<Int_t>(sizeof(libs)/sizeof(Char_t *));
28   for(Int_t ilib=0; ilib<nlibs; ilib++){
29     if(gSystem->Load(libs[ilib]) >= 0) continue;
30     printf("Failed to load %s.\n", libs[ilib]);
31     return;
32   }
33
34
35   gStyle->SetOptStat(0);
36   Bool_t mc      = kTRUE;
37   Bool_t friends = kTRUE;
38
39   Char_t *dir = 0x0;
40   TString tasks;
41   TObjArray *argsArray = TString(args).Tokenize("?");
42   switch(argsArray->GetEntriesFast()){
43   case 1:
44     tasks = ((TObjString*)(*argsArray)[0])->String();
45     dir=0x0;
46     break;
47   case 2:
48     tasks = ((TObjString*)(*argsArray)[0])->String();
49     dir = ((TObjString*)(*argsArray)[1])->GetName();
50     break;
51   default:
52     printf("Macro accepts 2 arguments separated by a '?'.\n");
53     printf("arg #1 : list of tasks/options\n");
54     printf("arg #2 : base directory to be processed\n");
55     return;
56   }
57
58   // select tasks to process; we should move it to an 
59   // individual function and move the task identifiers 
60   // outside the const space
61   TObjArray *tasksArray = tasks.Tokenize(" ");
62   Int_t fSteerTask = 0;
63   for(Int_t isel = 0; isel < tasksArray->GetEntriesFast(); isel++){
64     TString s = (dynamic_cast<TObjString *>(tasksArray->UncheckedAt(isel)))->String();
65     if(s.CompareTo("ALL") == 0){
66       for(Int_t itask = 1; itask < fknTasks; itask++) SETBIT(fSteerTask, itask);
67       continue;
68     } else if(s.CompareTo("NOFR") == 0){ 
69       friends = kFALSE;
70     } else if(s.CompareTo("NOMC") == 0){ 
71       mc = kFALSE;
72     } else { 
73       Bool_t foundOpt = kFALSE;  
74       for(Int_t itask = 1; itask < fknTasks; itask++){
75         if(s.CompareTo(fTaskOpt[itask]) != 0) continue;
76         SETBIT(fSteerTask, itask);
77         foundOpt = kTRUE;
78         break;
79       }
80       if(!foundOpt) Info("makeResults.C", Form("Task %s not implemented (yet).", s.Data()));
81     }
82   }
83
84
85   // catch the list of files using the ROOT Python Interface
86   TPython *pyshell = new TPython();
87   pyshell->Exec("import commands");
88
89   // file merger object
90   TFileMerger *fFM = new TFileMerger();
91   TClass *ctask = 0x0;
92   TObject *o = 0x0;
93   TObjArray *fContainer = 0x0;
94   AliTRDrecoTask *task = 0x0;
95
96   if(gSystem->AccessPathName(Form("%s/merge",  gSystem->ExpandPathName("$PWD")))) gSystem->Exec(Form("mkdir -v %s/merge",  gSystem->ExpandPathName("$PWD")));
97
98   printf("\n\tPROCESSING DATA FOR TASKS [%b]:\n", fSteerTask);
99   for(Int_t itask = 1; itask <fknTasks; itask++){
100     if(!TESTBIT(fSteerTask, itask)) continue;
101
102     ctask = new TClass(fTaskClass[itask]);
103     task = (AliTRDrecoTask*)ctask->New();
104     task->SetDebugLevel(0);
105     task->SetMCdata(mc);
106     task->SetFriends(friends);
107     printf("\t%s [%s]\n", task->GetTitle(), task->GetName());
108
109      // setup filelist
110     TString pathname = gSystem->ExpandPathName( dir ? dir : "$PWD");
111     TString filestring((const Char_t*) pyshell->Eval(Form("commands.getstatusoutput(\"find %s | grep TRD.Task%s.root\")[1]", pathname.Data(), task->GetName())));
112     TObjArray *filenames = filestring.Tokenize("\n");
113     Int_t nFiles = filenames->GetEntriesFast();
114     if(!nFiles){
115       printf("No Files found for Task %s\n", task->GetName());
116       delete task;
117       delete ctask;
118       continue;
119     }
120
121     if(nFiles>1){
122       fFM = new(fFM) TFileMerger(kTRUE);
123       fFM->OutputFile(Form("%s/merge/TRD.Task%s.root",  gSystem->ExpandPathName("$PWD"), task->GetName()));
124       for(Int_t ifile = 0; ifile < nFiles; ifile++){
125         TString filename = (dynamic_cast<TObjString *>(filenames->UncheckedAt(ifile)))->String();
126         if(filename.Contains("merge")) continue;
127         //printf("\tProcessing %s ...\n", filename.Data());
128         fFM->AddFile(filename.Data());
129       }
130       fFM->Merge();
131       fFM->~TFileMerger();
132       task->Load(Form("%s/merge/TRD.Task%s.root", gSystem->ExpandPathName("$PWD"), task->GetName()));
133     } else task->Load((dynamic_cast<TObjString *>(filenames->UncheckedAt(0)))->String().Data());
134
135     if(!(fContainer = task->Container())) {
136       delete task;
137       delete ctask;
138       continue;
139     } 
140     
141     task->PostProcess();
142     for(Int_t ipic=0; ipic<task->GetNRefFigures(); ipic++){
143       TCanvas *c = new TCanvas("c", "", 500, 500);
144       Int_t ifirst, ilast; Option_t *opt;
145       TH1 *h = 0x0; TGraph *g = 0x0;
146       task->GetRefFigure(ipic, ifirst, ilast, opt);
147       if(!(o = fContainer->At(ifirst))) continue;
148       
149       if(o->InheritsFrom("TH1")){ 
150         h = dynamic_cast<TH1*>(o);
151         h->Draw(opt);
152       } else if(o->InheritsFrom("TGraph")){ 
153         g = dynamic_cast<TGraph*>(o);
154         g->Draw(Form("a%s", opt));
155       } else{
156         printf("No idea how to plot object of type %s.\n", o->IsA()->GetName());
157         printf("Please teach me.\n");
158         continue;
159       }
160
161       for(Int_t ig=ifirst+1; ig<ilast; ig++){
162         if(!(o = fContainer->At(ig))) continue;
163         if(o->InheritsFrom("TH1")){
164           h = dynamic_cast<TH1*>(o);
165           h->Draw(Form("%ssame", opt));
166         } else if(o->InheritsFrom("TGraph")){
167           g = dynamic_cast<TGraph*>(o);
168           g->Draw(opt);
169         }
170       }
171       c->SaveAs(Form("%s_fig%d.gif", task->GetName(), ipic));
172       delete c;
173     }
174     delete task;
175     delete ctask;
176   }
177   delete pyshell;
178 }
179