Logo ROOT   6.12/07
Reference Guide
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 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1276
An array of TObjects.
Definition: TObjArray.h:37
TLine * line
Attaches to a PROOF session, possibly at the indicated URL.
Collectable string class.
Definition: TObjString.h:28
Basic string class.
Definition: TString.h:125
int Int_t
Definition: RtypesCore.h:41
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1004
bool Bool_t
Definition: RtypesCore.h:59
virtual char * Which(const char *search, const char *file, EAccessMode mode=kFileExists)
Find location of file in a search path.
Definition: TSystem.cxx:1522
TObject * At(Int_t idx) const
Definition: TObjArray.h:165
virtual Bool_t IsDone() const
Definition: TQueryResult.h:143
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
Definition: TSystem.cxx:904
virtual const char * Getenv(const char *env)
Get environment variable.
Definition: TSystem.cxx:1638
virtual const char * TempDirectory() const
Return a user configured or systemwide directory to create temporary files in.
Definition: TSystem.cxx:1458
A container class for query results.
Definition: TQueryResult.h:36
Int_t getProof(const char *where, Int_t verbose=1)
Open a PROOF session at gUrl.
virtual TList * GetListOfQueries(Option_t *opt="")
Ask the master for the list of queries.
Definition: TProof.cxx:2066
A doubly linked list.
Definition: TList.h:44
ROOT::R::TRInterface & r
Definition: Object.C:4
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
Int_t GetEntriesFast() const
Definition: TObjArray.h:64
char * Form(const char *fmt,...)
#define Printf
Definition: TGeoToOCC.h:18
const Bool_t kFALSE
Definition: RtypesCore.h:88
int Ssiz_t
Definition: RtypesCore.h:63
TMacro * GetSelecImp() const
Definition: TQueryResult.h:128
This class controls a Parallel ROOT Facility, PROOF, cluster.
Definition: TProof.h:316
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:396
Long64_t Finalize(Int_t query=-1, Bool_t force=kFALSE)
Finalize the qry-th query in fQueries.
Definition: TProof.cxx:5855
Bool_t IsNull() const
Definition: TString.h:383
auto * l
Definition: textangle.C:4
Utility class to draw objects in the feedback list during queries.
Definition: TDrawFeedback.h:35
void Add(TObject *obj)
Definition: TObjArray.h:73
Int_t Retrieve(Int_t query, const char *path=0)
Send retrieve request for the qry-th query in fQueries.
Definition: TProof.cxx:5920
virtual void Print(Option_t *option="") const
Default print for collections, calls Print(option, 1).
virtual Int_t GetSize() const
Definition: TCollection.h:180
const Bool_t kTRUE
Definition: RtypesCore.h:87
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
const char * Data() const
Definition: TString.h:345