From $ROOTSYS/tutorials/proof/finalizeProof.C

//
// Macro to finalize queries run with the macro tutorials/runProof .
// This macro uses an existing PROOF session or starts one at the indicated URL.
// In the case non existing PROOF session is found and no URL is given, the macro
// tries to start a local PROOF session.
//
// To run the macro:
//
//   root[] .L proof/finalizeProof.C+
//   root[] finalizeProof("<analysis>")
//
//   See runProof.C for the analysis currently available.
//
//   The macro looks for the last completed queries for the chosen analysis and
//   asks which one to finalize. If there is only available, it finalizes it
//   without asking.
//   All queries are considered for this, both those run synchronously and those
//   run asynchronously, e.g. runProof("h1(asyn)").
//

#include "Getline.h"
#include "TChain.h"
#include "TEnv.h"
#include "TProof.h"
#include "TString.h"
#include "TDrawFeedback.h"
#include "TList.h"
#include "TQueryResult.h"
#include "TObjArray.h"

#include "getProof.C"

void finalizeProof(const char *what = "simple",
                   const char *url = "proof://localhost:11093",
                   Int_t nwrks = -1)
{

   // Temp dir for PROOF tutorials
   TString tutdir = Form("%s/.proof-tutorial", gSystem->TempDirectory());
   if (gSystem->AccessPathName(tutdir)) {
      Printf("runProof: creating the temporary directory"
                " for the tutorial (%s) ... ", tutdir.Data());
      if (gSystem->mkdir(tutdir, kTRUE) != 0) {
         Printf("runProof: could not assert / create the temporary directory"
                " for the tutorial (%s)", tutdir.Data());
         return;
      }
   }

   // Get / Attach-to the PROOF Session
   TProof *proof = getProof(url, nwrks, tutdir.Data(), "");
   if (!proof) {
      Printf("runProof: could not start/attach a PROOF session");
      return;
   }

   // Get the last session run for the tutorial
   TObjArray *qt = new TObjArray();
   TString lasttag;
   TString proofsessions(Form("%s/sessions",tutdir.Data()));
   // Save tag of the used session
   FILE *fs = fopen(proofsessions.Data(), "r");
   if (!fs) {
      Printf("runProof: could not create files for sessions tags");
   } else {
      char line[1024];
      while (fgets(line, sizeof(line), fs)) {
         int l = strlen(line);
         if (l <= 0) continue;
         if (strncmp(line,"session-",strlen("session-"))) continue;
         if (line[l-1] == '\n') line[l-1] = 0;
         lasttag = line;
         qt->Add(new TObjString(lasttag.Data()));
      }
      fclose(fs);
   }

   // Retrieve the list of available query results
   TList *ql = proof->GetListOfQueries("A");
   if (!ql || ql->GetSize() <= 0) {
      Printf("runProof: no queries to be finalized");
      return;
   }
   ql->Print();

   // Where is the code to run
   char *rootbin = gSystem->Which(gSystem->Getenv("PATH"), "root.exe", kExecutePermission);
   if (!rootbin) {
      Printf("runProof: root.exe not found: please check the environment!");
      return;
   }
   TString rootsys(gSystem->DirName(rootbin));
   rootsys = gSystem->DirName(rootsys);
   TString tutorials(Form("%s/tutorials", rootsys.Data()));
   delete[] rootbin;

   // Create feedback displayer
   TDrawFeedback fb(proof);

   // Parse 'what'; it is in the form 'analysis(arg1,arg2,...)'
   TString args(what);
   args.ReplaceAll("("," ");
   args.ReplaceAll(")"," ");
   args.ReplaceAll(","," ");
   Ssiz_t from = 0;
   TString act, tok;
   if (!args.Tokenize(act, from, " ")) {
      // Cannot continue
      Printf("runProof: action not found: check your arguments (%s)", what);
      return;
   }

   TObjArray *qa = new TObjArray();
   TString sel;
   // Action
   if (act == "simple") {
      sel = "ProofSimple";
   } else if (act == "h1") {
      sel = "h1analysis";
   } else if (act == "pythia8") {
      sel = "ProofPythia";
   } else {
      // Do not know what to run
      Printf("runProof: unknown tutorial: %s", what);
   }

   // Get last completed queries for the chosen analysis
   TString ref;
   Int_t nt = qt->GetEntriesFast();
   while (ref.IsNull() && nt--) {
      lasttag = ((TObjString *)(qt->At(nt)))->GetName();
      if (!lasttag.IsNull())
         Printf("runProof: checking session: %s", lasttag.Data());
      TIter nxq(ql);
      TQueryResult *qr = 0;
      while ((qr = (TQueryResult *)nxq())) {
         if (qr->IsDone() && !lasttag.CompareTo(qr->GetTitle()) &&
                           !sel.CompareTo(qr->GetSelecImp()->GetTitle())) {
            TString r = Form("%s:%s",qr->GetTitle(),qr->GetName());
            qa->Add(new TObjString(r.Data()));
         }
      }
      if (qa->GetEntriesFast() > 0) {
         Int_t qn = 0;
         if (qa->GetEntriesFast() > 1) {
            // Query the client which query to finalize
            Printf("finalizeProof: queries completed for analysis '%s'", act.Data());
            for (Int_t k = 0; k < qa->GetEntriesFast(); k++) {
               Printf(" [%d] %s", k, ((TObjString *)(qa->At(k)))->GetName());
            }
            Bool_t ask = kTRUE;
            while (ask) {
               char *answer = Getline("finalizeProof: enter the one you would like to finalize? [0] ");
               if (answer) {
                  if (answer[0] == 'Q' || answer[0] == 'q') {
                     ask = kFALSE;
                     return;
                  }
                  TString sn(answer);
                  sn.Remove(sn.Length()-1);
                  if (sn.IsDigit()) {
                     qn = sn.Atoi();
                     if (qn >= 0 && qn < qa->GetEntriesFast()) {
                        break;
                     } else {
                        Printf("finalizeProof: choice must be in [0,%d] ('Q' to quit)",
                              qa->GetEntriesFast()-1);
                     }
                  } else {
                     if (sn.IsNull()) {
                        qn = 0;
                        break;
                     } else {
                        Printf("finalizeProof: choice must be a number in [0,%d] ('Q' to quit) (%s)",
                              qa->GetEntriesFast()-1,  sn.Data());
                     }
                  }
               }
            }
         }
         ref = ((TObjString *)(qa->At(qn)))->GetName();
      }
   }
   if (!ref.IsNull()) {
      // Retrieve
      proof->Retrieve(ref);
      // Finalize
      proof->Finalize(ref);
   } else {
      Printf("runProof: no queries to be finalized for analysis '%s'", act.Data());
      return;
   }
}
 finalizeProof.C:1
 finalizeProof.C:2
 finalizeProof.C:3
 finalizeProof.C:4
 finalizeProof.C:5
 finalizeProof.C:6
 finalizeProof.C:7
 finalizeProof.C:8
 finalizeProof.C:9
 finalizeProof.C:10
 finalizeProof.C:11
 finalizeProof.C:12
 finalizeProof.C:13
 finalizeProof.C:14
 finalizeProof.C:15
 finalizeProof.C:16
 finalizeProof.C:17
 finalizeProof.C:18
 finalizeProof.C:19
 finalizeProof.C:20
 finalizeProof.C:21
 finalizeProof.C:22
 finalizeProof.C:23
 finalizeProof.C:24
 finalizeProof.C:25
 finalizeProof.C:26
 finalizeProof.C:27
 finalizeProof.C:28
 finalizeProof.C:29
 finalizeProof.C:30
 finalizeProof.C:31
 finalizeProof.C:32
 finalizeProof.C:33
 finalizeProof.C:34
 finalizeProof.C:35
 finalizeProof.C:36
 finalizeProof.C:37
 finalizeProof.C:38
 finalizeProof.C:39
 finalizeProof.C:40
 finalizeProof.C:41
 finalizeProof.C:42
 finalizeProof.C:43
 finalizeProof.C:44
 finalizeProof.C:45
 finalizeProof.C:46
 finalizeProof.C:47
 finalizeProof.C:48
 finalizeProof.C:49
 finalizeProof.C:50
 finalizeProof.C:51
 finalizeProof.C:52
 finalizeProof.C:53
 finalizeProof.C:54
 finalizeProof.C:55
 finalizeProof.C:56
 finalizeProof.C:57
 finalizeProof.C:58
 finalizeProof.C:59
 finalizeProof.C:60
 finalizeProof.C:61
 finalizeProof.C:62
 finalizeProof.C:63
 finalizeProof.C:64
 finalizeProof.C:65
 finalizeProof.C:66
 finalizeProof.C:67
 finalizeProof.C:68
 finalizeProof.C:69
 finalizeProof.C:70
 finalizeProof.C:71
 finalizeProof.C:72
 finalizeProof.C:73
 finalizeProof.C:74
 finalizeProof.C:75
 finalizeProof.C:76
 finalizeProof.C:77
 finalizeProof.C:78
 finalizeProof.C:79
 finalizeProof.C:80
 finalizeProof.C:81
 finalizeProof.C:82
 finalizeProof.C:83
 finalizeProof.C:84
 finalizeProof.C:85
 finalizeProof.C:86
 finalizeProof.C:87
 finalizeProof.C:88
 finalizeProof.C:89
 finalizeProof.C:90
 finalizeProof.C:91
 finalizeProof.C:92
 finalizeProof.C:93
 finalizeProof.C:94
 finalizeProof.C:95
 finalizeProof.C:96
 finalizeProof.C:97
 finalizeProof.C:98
 finalizeProof.C:99
 finalizeProof.C:100
 finalizeProof.C:101
 finalizeProof.C:102
 finalizeProof.C:103
 finalizeProof.C:104
 finalizeProof.C:105
 finalizeProof.C:106
 finalizeProof.C:107
 finalizeProof.C:108
 finalizeProof.C:109
 finalizeProof.C:110
 finalizeProof.C:111
 finalizeProof.C:112
 finalizeProof.C:113
 finalizeProof.C:114
 finalizeProof.C:115
 finalizeProof.C:116
 finalizeProof.C:117
 finalizeProof.C:118
 finalizeProof.C:119
 finalizeProof.C:120
 finalizeProof.C:121
 finalizeProof.C:122
 finalizeProof.C:123
 finalizeProof.C:124
 finalizeProof.C:125
 finalizeProof.C:126
 finalizeProof.C:127
 finalizeProof.C:128
 finalizeProof.C:129
 finalizeProof.C:130
 finalizeProof.C:131
 finalizeProof.C:132
 finalizeProof.C:133
 finalizeProof.C:134
 finalizeProof.C:135
 finalizeProof.C:136
 finalizeProof.C:137
 finalizeProof.C:138
 finalizeProof.C:139
 finalizeProof.C:140
 finalizeProof.C:141
 finalizeProof.C:142
 finalizeProof.C:143
 finalizeProof.C:144
 finalizeProof.C:145
 finalizeProof.C:146
 finalizeProof.C:147
 finalizeProof.C:148
 finalizeProof.C:149
 finalizeProof.C:150
 finalizeProof.C:151
 finalizeProof.C:152
 finalizeProof.C:153
 finalizeProof.C:154
 finalizeProof.C:155
 finalizeProof.C:156
 finalizeProof.C:157
 finalizeProof.C:158
 finalizeProof.C:159
 finalizeProof.C:160
 finalizeProof.C:161
 finalizeProof.C:162
 finalizeProof.C:163
 finalizeProof.C:164
 finalizeProof.C:165
 finalizeProof.C:166
 finalizeProof.C:167
 finalizeProof.C:168
 finalizeProof.C:169
 finalizeProof.C:170
 finalizeProof.C:171
 finalizeProof.C:172
 finalizeProof.C:173
 finalizeProof.C:174
 finalizeProof.C:175
 finalizeProof.C:176
 finalizeProof.C:177
 finalizeProof.C:178
 finalizeProof.C:179
 finalizeProof.C:180
 finalizeProof.C:181
 finalizeProof.C:182
 finalizeProof.C:183
 finalizeProof.C:184
 finalizeProof.C:185
 finalizeProof.C:186
 finalizeProof.C:187
 finalizeProof.C:188
 finalizeProof.C:189
 finalizeProof.C:190
 finalizeProof.C:191
 finalizeProof.C:192
 finalizeProof.C:193
 finalizeProof.C:194