Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
finalizeProof.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_proof
3 ///
4 /// Macro to finalize queries run with the macro tutorials/runProof .
5 /// This macro uses an existing PROOF session or starts one at the indicated URL.
6 /// In the case non existing PROOF session is found and no URL is given, the macro
7 /// tries to start a local PROOF session.
8 ///
9 /// To run the macro:
10 ///
11 /// root[] .L proof/finalizeProof.C+
12 /// root[] finalizeProof("<analysis>")
13 ///
14 /// See runProof.C for the analysis currently available.
15 ///
16 /// The macro looks for the last completed queries for the chosen analysis and
17 /// asks which one to finalize. If there is only available, it finalizes it
18 /// without asking.
19 /// All queries are considered for this, both those run synchronously and those
20 /// run asynchronously, e.g. runProof("h1(asyn)").
21 ///
22 ///
23 /// \macro_code
24 ///
25 /// \author Gerardo Ganis
26 
27 #include "Getline.h"
28 #include "TChain.h"
29 #include "TEnv.h"
30 #include "TProof.h"
31 #include "TString.h"
32 #include "TDrawFeedback.h"
33 #include "TList.h"
34 #include "TQueryResult.h"
35 #include "TObjArray.h"
36 
37 #include "getProof.C"
38 
39 void finalizeProof(const char *what = "simple",
40  const char *url = "proof://localhost:11093",
41  Int_t nwrks = -1)
42 {
43 
44  // Temp dir for PROOF tutorials
45  TString tutdir = Form("%s/.proof-tutorial", gSystem->TempDirectory());
46  if (gSystem->AccessPathName(tutdir)) {
47  Printf("runProof: creating the temporary directory"
48  " for the tutorial (%s) ... ", tutdir.Data());
49  if (gSystem->mkdir(tutdir, kTRUE) != 0) {
50  Printf("runProof: could not assert / create the temporary directory"
51  " for the tutorial (%s)", tutdir.Data());
52  return;
53  }
54  }
55 
56  // Get / Attach-to the PROOF Session
57  TProof *proof = getProof(url, nwrks, tutdir.Data(), "");
58  if (!proof) {
59  Printf("runProof: could not start/attach a PROOF session");
60  return;
61  }
62 
63  // Get the last session run for the tutorial
64  TObjArray *qt = new TObjArray();
65  TString lasttag;
66  TString proofsessions(Form("%s/sessions",tutdir.Data()));
67  // Save tag of the used session
68  FILE *fs = fopen(proofsessions.Data(), "r");
69  if (!fs) {
70  Printf("runProof: could not create files for sessions tags");
71  } else {
72  char line[1024];
73  while (fgets(line, sizeof(line), fs)) {
74  int l = strlen(line);
75  if (l <= 0) continue;
76  if (strncmp(line,"session-",strlen("session-"))) continue;
77  if (line[l-1] == '\n') line[l-1] = 0;
78  lasttag = line;
79  qt->Add(new TObjString(lasttag.Data()));
80  }
81  fclose(fs);
82  }
83 
84  // Retrieve the list of available query results
85  TList *ql = proof->GetListOfQueries("A");
86  if (!ql || ql->GetSize() <= 0) {
87  Printf("runProof: no queries to be finalized");
88  return;
89  }
90  ql->Print();
91 
92  // Where is the code to run
93  char *rootbin = gSystem->Which(gSystem->Getenv("PATH"), "root.exe", kExecutePermission);
94  if (!rootbin) {
95  Printf("runProof: root.exe not found: please check the environment!");
96  return;
97  }
98  TString rootsys(gSystem->DirName(rootbin));
99  rootsys = gSystem->DirName(rootsys);
100  TString tutorials(Form("%s/tutorials", rootsys.Data()));
101  delete[] rootbin;
102 
103  // Create feedback displayer
104  TDrawFeedback fb(proof);
105 
106  // Parse 'what'; it is in the form 'analysis(arg1,arg2,...)'
107  TString args(what);
108  args.ReplaceAll("("," ");
109  args.ReplaceAll(")"," ");
110  args.ReplaceAll(","," ");
111  Ssiz_t from = 0;
112  TString act, tok;
113  if (!args.Tokenize(act, from, " ")) {
114  // Cannot continue
115  Printf("runProof: action not found: check your arguments (%s)", what);
116  return;
117  }
118 
119  TObjArray *qa = new TObjArray();
120  TString sel;
121  // Action
122  if (act == "simple") {
123  sel = "ProofSimple";
124  } else if (act == "h1") {
125  sel = "h1analysis";
126  } else if (act == "pythia8") {
127  sel = "ProofPythia";
128  } else {
129  // Do not know what to run
130  Printf("runProof: unknown tutorial: %s", what);
131  }
132 
133  // Get last completed queries for the chosen analysis
134  TString ref;
135  Int_t nt = qt->GetEntriesFast();
136  while (ref.IsNull() && nt--) {
137  lasttag = ((TObjString *)(qt->At(nt)))->GetName();
138  if (!lasttag.IsNull())
139  Printf("runProof: checking session: %s", lasttag.Data());
140  TIter nxq(ql);
141  TQueryResult *qr = 0;
142  while ((qr = (TQueryResult *)nxq())) {
143  if (qr->IsDone() && !lasttag.CompareTo(qr->GetTitle()) &&
144  !sel.CompareTo(qr->GetSelecImp()->GetTitle())) {
145  TString r = Form("%s:%s",qr->GetTitle(),qr->GetName());
146  qa->Add(new TObjString(r.Data()));
147  }
148  }
149  if (qa->GetEntriesFast() > 0) {
150  Int_t qn = 0;
151  if (qa->GetEntriesFast() > 1) {
152  // Query the client which query to finalize
153  Printf("finalizeProof: queries completed for analysis '%s'", act.Data());
154  for (Int_t k = 0; k < qa->GetEntriesFast(); k++) {
155  Printf(" [%d] %s", k, ((TObjString *)(qa->At(k)))->GetName());
156  }
157  Bool_t ask = kTRUE;
158  while (ask) {
159  char *answer = Getline("finalizeProof: enter the one you would like to finalize? [0] ");
160  if (answer) {
161  if (answer[0] == 'Q' || answer[0] == 'q') {
162  ask = kFALSE;
163  return;
164  }
165  TString sn(answer);
166  sn.Remove(sn.Length()-1);
167  if (sn.IsDigit()) {
168  qn = sn.Atoi();
169  if (qn >= 0 && qn < qa->GetEntriesFast()) {
170  break;
171  } else {
172  Printf("finalizeProof: choice must be in [0,%d] ('Q' to quit)",
173  qa->GetEntriesFast()-1);
174  }
175  } else {
176  if (sn.IsNull()) {
177  qn = 0;
178  break;
179  } else {
180  Printf("finalizeProof: choice must be a number in [0,%d] ('Q' to quit) (%s)",
181  qa->GetEntriesFast()-1, sn.Data());
182  }
183  }
184  }
185  }
186  }
187  ref = ((TObjString *)(qa->At(qn)))->GetName();
188  }
189  }
190  if (!ref.IsNull()) {
191  // Retrieve
192  proof->Retrieve(ref);
193  // Finalize
194  proof->Finalize(ref);
195  } else {
196  Printf("runProof: no queries to be finalized for analysis '%s'", act.Data());
197  return;
198  }
199 }