AliPhysics  fe039ad (fe039ad)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExtractProd.C
Go to the documentation of this file.
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 
18 {
28  : fTmp(""), fPath(""), fPass(""), fRuns(""), fDebug(false)
29  {
30  }
40  Bool_t Run(const TString& name, Bool_t mc=false, ULong_t minSize=1000000)
41  {
42  fTmp = "prod";
43  gSystem->TempFileName(fTmp);
44  gSystem->Unlink(fTmp);
45  gSystem->mkdir(fTmp);
46  if (fDebug) Info("", "TMP=%s", fTmp.Data());
47 
48  fPath = "";
49  fPass = "";
50  fRuns = "";
51  TString job;
52  if (!GetJobUrl(name, mc, job)) return false;
53 
54  if (!GetRuns(job, mc, minSize)) return false;
55 
56  if (!fPass.IsNull()) fPass.Append("/");
57  Printf("alien://%s?pattern=%s*/AliESDs.root&runs=%s",
58  fPath.Data(), fPass.Data(), fRuns.Data());
59 
60  gSystem->Unlink(fTmp);
61  return true;
62  }
73  {
74  url = "";
75  TString index("raw.jsp");
76  if (!Download((mc ? "job_details.jsp" : "production/raw.jsp"), index))
77  return false;
78 
79  std::ifstream in(index.Data());
80  TString line;
81  TString tgt(Form("<td class=\"table_row\">%s</td>", name.Data()));
82  do {
83  line.ReadLine(in);
84  if (!line.Contains(tgt)) continue;
85  line.ReadLine(in);
86  Int_t first = line.Index("href=\"");
87  Int_t last = line.Index("\"", first+7);
88  url = line(first+6,last-first-6);
89  break;
90 
91  } while (!in.eof());
92  in.close();
93 
94  if (url.IsNull()) {
95  Error("GetJobUrl", "Production %s not found", name.Data());
96  return false;
97  }
98 
99  return true;
100  }
110  Bool_t GetRuns(const TString& url, Bool_t mc, ULong_t minSize)
111  {
112  TString index("job");
113  if (!Download(Form("%s%s", (mc ? "" : "raw/"), url.Data()), index))
114  return false;
115 
116  std::ifstream in(index.Data());
117  TString tgt1(mc ? "window.open" : "runDetails");
118  TString line = "";
119  do {
120  line.ReadLine(in);
121  if (!line.Contains(tgt1)) continue;
122  Int_t first = -1;
123  Int_t last = -1;
124  if (!mc) {
125  first = line.Index(tgt1);
126  last = line.Index(")", first+tgt1.Length()+1);
127  }
128  else {
129  Int_t tmp = line.Index(">");
130  first = line.Index(">", tmp+1);
131  last = line.Index("<", first);
132  }
133  if (first == kNPOS || last == kNPOS) {
134  Error("GetDir", "Failed to get directory from %s", line.Data());
135  return false;
136  }
137  first += (mc ? 1 : tgt1.Length()+1);
138  last -= first;
139  TString srun = line(first, last);
140  ULong_t runNo = srun.Atoll();
141  if (fDebug) Info("", "Got run %lu (%s)", runNo, srun.Data());
142 
143  if (!GetSize(in, runNo, mc, minSize)) continue;
144  if (!GetDir(in, runNo, mc)) continue;
145 
146  if (!fRuns.IsNull()) fRuns.Append(",");
147  fRuns.Append(Form("%lu", runNo));
148  } while (!in.eof());
149  in.close();
150  if (fRuns.IsNull()) return false;
151 
152  return true;
153  }
164  Bool_t GetSize(std::istream& in, ULong_t runNo,
165  Bool_t mc, ULong_t minSize=100000)
166  {
167  TString line;
168  TString tgt2(mc ? "table_row_right" : "ESDs size");
169  Int_t cnt = 0;
170  do {
171  line.ReadLine(in);
172  if (!line.Contains(tgt2)) continue;
173  cnt++;
174  if (mc && cnt < 3) continue;
175  if (!mc) line.ReadLine(in);
176  if (fDebug) Info("", line);
177 
178  TString ssiz;
179  if (mc) {
180  Int_t first = line.Index(">");
181  Int_t last = line.Index("<",first+1);
182  if (first == kNPOS || last == kNPOS) {
183  Error("GetDir", "Failed to get directory from %s", line.Data());
184  return false;
185  }
186  ssiz = line(first+1, last-first-1);
187  }
188  else {
189  for (Int_t i = 0; i < line.Length(); i++) {
190  if (line[i] == '<') break;
191  if (line[i] == ' ' || line[i] == '\t' || line[i] == ',') continue;
192  ssiz.Append(line[i]);
193  }
194  }
195  Long_t size = ssiz.Atoll();
196  if (fDebug) Info("", "Got run %lu %lu" , runNo, size);
197  if (size < 0) {
198  Error("GetSize", "Failed to extract size for run %lu", runNo);
199  return false;
200  }
201  if (ULong_t(size) < minSize) {
202  Warning("GetSize","Run %lu does not have enough events %lu",runNo,size);
203  return false;
204  }
205  break;
206  } while (!in.eof());
207  return true;
208  }
218  Bool_t GetDir(std::istream& in, ULong_t runNo, Bool_t mc)
219  {
220  TString line;
221  TString tgt3("/catalogue/index.jsp");
222  do {
223  line.ReadLine(in);
224  // Info("", "line=%s", line.Data());
225  if (!line.Contains(tgt3)) continue;
226  if (fDebug) Info("", line);
227  Int_t tmp = mc ? line.Index(">")+1 : 0;
228  Int_t first = line.Index(">", tmp);
229  Int_t last = line.Index("<",first+1);
230  if (first == kNPOS || last == kNPOS) {
231  Error("GetDir", "Failed to get directory from %s", line.Data());
232  return false;
233  }
234 
235  TString dir = line(first+1,last-first-1);
236 
237  if (fDebug) Info("", "Got run %lu %s", runNo, dir.Data());
238  TString path, pass;
239  if (!GetPathPass(dir, runNo, path, pass)) return false;
240 
241  if (fDebug) Info("", "Got run %lu %s %s", runNo,path.Data(),pass.Data());
242 
243  if (fPath.IsNull()) fPath = path;
244  else if (!fPath.EqualTo(path)) {
245  Warning("GetDir", "Run %lu location %s not %s",
246  runNo, path.Data(), fPath.Data());
247  return false;
248  }
249 
250  if (fPass.IsNull()) fPass = pass;
251  else if (!fPass.EqualTo(pass)) {
252  Warning("GetDir", "Run %lu pass %s not %s",
253  runNo, pass.Data(), fPass.Data());
254  return false;
255  }
256  break;
257  } while (!in.eof());
258  return true;
259  }
271  TString& path, TString& pass)
272  {
273  Int_t first = dir.Index(Form("%lu", run));
274  Int_t last = dir.Index("/", first);
275  if (last == kNPOS) last = dir.Length();
276  if (first == kNPOS) {
277  Error("GetPathPass", "Run number %lu not in path %s", run, dir.Data());
278  return false;
279  }
280  while (dir[first-1] == '0') first--;
281  path = dir(0, first);
282  pass = dir(last+1,dir.Length()-last-1);
283  return true;
284  }
294  {
295  const TString base("http://alimonitor.cern.ch");
296  out = Form("%s/%s", fTmp.Data(), out.Data());
297  TString cmd(Form("wget -q \"%s/%s\" -O \"%s\"",
298  base.Data(), url.Data(), out.Data()));
299  if (fDebug) Info("Download", "%s", cmd.Data());
300  Int_t ret = gSystem->Exec(cmd.Data());
301  if (ret != 0) {
302  Error("Download", "Failed to get %s/%s -> %s",
303  base.Data(), url.Data(), out.Data());
304  return false;
305  }
306  return true;
307  }
308 };
309 
310 //
311 // EOF
312 //
Bool_t GetRuns(const TString &url, Bool_t mc, ULong_t minSize)
Definition: ExtractProd.C:110
const char * url
TSystem * gSystem
TString fTmp
Definition: ExtractProd.C:19
Bool_t Run(const TString &name, Bool_t mc=false, ULong_t minSize=1000000)
Definition: ExtractProd.C:40
Bool_t GetSize(std::istream &in, ULong_t runNo, Bool_t mc, ULong_t minSize=100000)
Definition: ExtractProd.C:164
TString fRuns
Definition: ExtractProd.C:22
Bool_t GetJobUrl(const TString &name, Bool_t mc, TString &url)
Definition: ExtractProd.C:72
int Int_t
Definition: External.C:63
TString fPath
Definition: ExtractProd.C:20
unsigned long ULong_t
Definition: External.C:38
TString fPass
Definition: ExtractProd.C:21
Bool_t GetPathPass(const TString &dir, ULong_t run, TString &path, TString &pass)
Definition: ExtractProd.C:270
Bool_t GetDir(std::istream &in, ULong_t runNo, Bool_t mc)
Definition: ExtractProd.C:218
Bool_t Download(const TString &url, TString &out)
Definition: ExtractProd.C:293
bool Bool_t
Definition: External.C:53
Bool_t fDebug
Definition: ExtractProd.C:23
TDirectoryFile * dir