]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/trains/ExtractProd.C
Merge branch 'feature-movesplit'
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / trains / ExtractProd.C
1 #ifndef __CINT__
2 # include <TFile.h>
3 # include <TString.h>
4 # include <TError.h>
5 # include <TSystem.h>
6 # include <fstream>
7 #else
8 class TFile;
9 class TString;
10 #endif
11
12
13 struct ExtractProd
14 {
15   TString fTmp;
16   TString fPath;
17   TString fPass;
18   TString fRuns;
19   Bool_t  fDebug;
20
21   ExtractProd() 
22     : fTmp(""), fPath(""), fPass(""), fRuns(""), fDebug(false)
23   {
24   }
25   
26   Bool_t Run(const TString& name, Bool_t mc=false, ULong_t minSize=1000000)
27   {
28     fTmp = "prod";
29     gSystem->TempFileName(fTmp);
30     gSystem->Unlink(fTmp);
31     gSystem->mkdir(fTmp);
32     if (fDebug) Info("", "TMP=%s", fTmp.Data());
33
34     fPath = "";
35     fPass = "";
36     fRuns = "";
37     TString job;
38     if (!GetJobUrl(name, mc, job)) return false;
39     
40     if (!GetRuns(job, mc, minSize)) return false;
41
42     if (!fPass.IsNull()) fPass.Append("/");
43     Printf("alien://%s?pattern=%s*/AliESDs.root&runs=%s", 
44            fPath.Data(), fPass.Data(), fRuns.Data());
45
46     gSystem->Unlink(fTmp);
47     return true;
48   }
49   Bool_t GetJobUrl(const TString& name, Bool_t mc, TString& url)
50   {
51     url = "";
52     TString index("raw.jsp");
53     if (!Download((mc ? "job_details.jsp" : "production/raw.jsp"), index))
54         return false;
55     
56     std::ifstream in(index.Data());
57     TString line;
58     TString tgt(Form("<td class=\"table_row\">%s</td>", name.Data()));
59     do { 
60       line.ReadLine(in);
61       if (!line.Contains(tgt)) continue;
62       line.ReadLine(in);
63       Int_t first = line.Index("href=\"");
64       Int_t last  = line.Index("\"", first+7);
65       url = line(first+6,last-first-6);
66       break;
67       
68     } while (!in.eof());
69     in.close();
70     
71     if (url.IsNull()) { 
72       Error("GetJobUrl", "Production %s not found", name.Data());
73       return false;
74     }
75
76     return true;
77   }
78   Bool_t GetRuns(const TString& url, Bool_t mc, ULong_t minSize)
79   {
80     TString index("job");
81     if (!Download(Form("%s%s", (mc ? "" : "raw/"), url.Data()), index)) 
82       return false;
83
84     std::ifstream in(index.Data());
85     TString tgt1(mc ? "window.open" : "runDetails");
86     TString line  = "";
87     do { 
88       line.ReadLine(in);
89       if (!line.Contains(tgt1)) continue;
90       Int_t   first = -1;
91       Int_t   last  = -1;
92       if (!mc) { 
93         first = line.Index(tgt1);
94         last  = line.Index(")", first+tgt1.Length()+1);
95       }
96       else { 
97         Int_t tmp = line.Index(">");
98         first = line.Index(">", tmp+1);
99         last  = line.Index("<", first);
100       }
101       if (first == kNPOS || last == kNPOS) { 
102         Error("GetDir", "Failed to get directory from %s", line.Data());
103         return false;
104       }
105       first += (mc ? 1 : tgt1.Length()+1);
106       last  -= first;
107       TString srun  = line(first, last);
108       ULong_t runNo = srun.Atoll();
109       if (fDebug) Info("", "Got run %lu (%s)", runNo, srun.Data());
110
111       if (!GetSize(in, runNo, mc, minSize)) continue;
112       if (!GetDir(in, runNo, mc))           continue;
113
114       if (!fRuns.IsNull()) fRuns.Append(",");
115       fRuns.Append(Form("%lu", runNo));
116     } while (!in.eof());
117     in.close();
118     if (fRuns.IsNull()) return false;
119
120     return true;
121   }
122   Bool_t GetSize(std::istream& in, ULong_t runNo, 
123                  Bool_t mc, ULong_t minSize=100000)
124   {
125     TString line;
126     TString tgt2(mc ? "table_row_right" : "ESDs size");
127     Int_t   cnt = 0;
128     do {
129       line.ReadLine(in);
130       if (!line.Contains(tgt2)) continue;
131       cnt++;
132       if (mc && cnt < 3) continue;
133       if (!mc) line.ReadLine(in);
134       if (fDebug) Info("", line);
135
136       TString ssiz;
137       if (mc) { 
138         Int_t first       = line.Index(">");
139         Int_t last        = line.Index("<",first+1);
140         if (first == kNPOS || last == kNPOS) { 
141           Error("GetDir", "Failed to get directory from %s", line.Data());
142           return false;
143         }
144         ssiz = line(first+1, last-first-1);
145       }
146       else {
147         for (Int_t i = 0; i < line.Length(); i++) { 
148           if (line[i] == '<') break;
149           if (line[i] == ' ' || line[i] == '\t' || line[i] == ',') continue;
150           ssiz.Append(line[i]);
151         }
152       }
153       Long_t size = ssiz.Atoll();
154       if (fDebug) Info("", "Got run %lu %lu" , runNo, size);
155       if (size < 0) {
156         Error("GetSize", "Failed to extract size for run %lu", runNo);
157         return false;
158       }
159       if (ULong_t(size) < minSize) {
160         Warning("GetSize","Run %lu does not have enough events %lu",runNo,size);
161         return false;
162       }
163       break;
164     } while (!in.eof());
165     return true;
166   }
167   Bool_t GetDir(std::istream& in, ULong_t runNo, Bool_t mc)
168   {
169     TString line;
170     TString tgt3("/catalogue/index.jsp");
171     do { 
172       line.ReadLine(in);
173       // Info("", "line=%s", line.Data());
174       if (!line.Contains(tgt3)) continue;
175       if (fDebug) Info("", line);
176       Int_t tmp         = mc ? line.Index(">")+1 : 0;
177       Int_t first       = line.Index(">", tmp);
178       Int_t last        = line.Index("<",first+1);
179       if (first == kNPOS || last == kNPOS) { 
180         Error("GetDir", "Failed to get directory from %s", line.Data());
181         return false;
182       }
183       
184       TString dir = line(first+1,last-first-1);
185         
186       if (fDebug) Info("", "Got run %lu %s", runNo, dir.Data());
187       TString path, pass;
188       if (!GetPathPass(dir, runNo, path, pass)) return false;
189       
190       if (fDebug) Info("", "Got run %lu %s %s", runNo,path.Data(),pass.Data());
191
192       if      (fPath.IsNull()) fPath = path;
193       else if (!fPath.EqualTo(path)) { 
194         Warning("GetDir", "Run %lu location %s not %s", 
195               runNo, path.Data(), fPath.Data());
196         return false;
197       }
198
199       if      (fPass.IsNull()) fPass = pass;
200       else if (!fPass.EqualTo(pass)) { 
201         Warning("GetDir", "Run %lu pass %s not %s", 
202               runNo, pass.Data(), fPass.Data());
203         return false;
204       }
205       break;
206     } while (!in.eof());
207     return true;
208   }
209   Bool_t GetPathPass(const TString& dir, ULong_t run, 
210                      TString& path, TString& pass) 
211   {
212     Int_t first = dir.Index(Form("%lu", run));
213     Int_t last  = dir.Index("/", first);
214     if (last == kNPOS) last = dir.Length();
215     if (first == kNPOS) { 
216       Error("GetPathPass", "Run number %lu not in path %s", run, dir.Data());
217       return false;
218     }
219     while (dir[first-1] == '0') first--;
220     path = dir(0, first);
221     pass = dir(last+1,dir.Length()-last-1);
222     return true;
223   }
224   Bool_t Download(const TString& url, TString& out)
225   {
226     const TString base("http://alimonitor.cern.ch");
227     out = Form("%s/%s", fTmp.Data(), out.Data());
228     TString cmd(Form("wget -q \"%s/%s\" -O \"%s\"", 
229                      base.Data(), url.Data(), out.Data()));
230     if (fDebug) Info("Download", "%s", cmd.Data());
231     Int_t ret = gSystem->Exec(cmd.Data());
232     if (ret != 0)  {
233       Error("Download", "Failed to get %s/%s -> %s", 
234             base.Data(), url.Data(), out.Data());
235       return false;
236     }
237     return true;
238   }
239 };
240