finalizeProof.C

Go to the documentation of this file.
00001 //
00002 // Macro to finalize queries run with the macro tutorials/runProof .
00003 // This macro uses an existing PROOF session or starts one at the indicated URL.
00004 // In the case non existing PROOF session is found and no URL is given, the macro
00005 // tries to start a local PROOF session.
00006 //
00007 // To run the macro:
00008 //
00009 //   root[] .L proof/finalizeProof.C+
00010 //   root[] finalizeProof("<analysis>")
00011 //
00012 //   See runProof.C for the analysis currently available.
00013 //
00014 //   The macro looks for the last completed queries for the chosen analysis and
00015 //   asks which one to finalize. If there is only available, it finalizes it
00016 //   without asking.
00017 //   All queries are considered for this, both those run synchronously and those
00018 //   run asynchronously, e.g. runProof("h1(asyn)").
00019 //
00020 
00021 #include "Getline.h"
00022 #include "TChain.h"
00023 #include "TEnv.h"
00024 #include "TProof.h"
00025 #include "TString.h"
00026 #include "TDrawFeedback.h"
00027 #include "TList.h"
00028 #include "TQueryResult.h"
00029 #include "TObjArray.h"
00030 
00031 #include "getProof.C"
00032 
00033 void finalizeProof(const char *what = "simple",
00034                    const char *url = "proof://localhost:11093",
00035                    Int_t nwrks = -1)
00036 {
00037 
00038    // Temp dir for PROOF tutorials
00039    TString tutdir = Form("%s/.proof-tutorial", gSystem->TempDirectory());
00040    if (gSystem->AccessPathName(tutdir)) {
00041       Printf("runProof: creating the temporary directory"
00042                 " for the tutorial (%s) ... ", tutdir.Data());
00043       if (gSystem->mkdir(tutdir, kTRUE) != 0) {
00044          Printf("runProof: could not assert / create the temporary directory"
00045                 " for the tutorial (%s)", tutdir.Data());
00046          return;
00047       }
00048    }
00049 
00050    // Get / Attach-to the PROOF Session
00051    TProof *proof = getProof(url, nwrks, tutdir.Data(), "");
00052    if (!proof) {
00053       Printf("runProof: could not start/attach a PROOF session");
00054       return;
00055    }
00056 
00057    // Get the last session run for the tutorial
00058    TObjArray *qt = new TObjArray();
00059    TString lasttag;
00060    TString proofsessions(Form("%s/sessions",tutdir.Data()));
00061    // Save tag of the used session
00062    FILE *fs = fopen(proofsessions.Data(), "r");
00063    if (!fs) {
00064       Printf("runProof: could not create files for sessions tags");
00065    } else {
00066       char line[1024];
00067       while (fgets(line, sizeof(line), fs)) {
00068          int l = strlen(line);
00069          if (l <= 0) continue;
00070          if (strncmp(line,"session-",strlen("session-"))) continue;
00071          if (line[l-1] == '\n') line[l-1] = 0;
00072          lasttag = line;
00073          qt->Add(new TObjString(lasttag.Data()));
00074       }
00075       fclose(fs);
00076    }
00077 
00078    // Retrieve the list of available query results
00079    TList *ql = proof->GetListOfQueries("A");
00080    if (!ql || ql->GetSize() <= 0) {
00081       Printf("runProof: no queries to be finalized");
00082       return;
00083    }
00084    ql->Print();
00085 
00086    // Where is the code to run
00087    char *rootbin = gSystem->Which(gSystem->Getenv("PATH"), "root.exe", kExecutePermission);
00088    if (!rootbin) {
00089       Printf("runProof: root.exe not found: please check the environment!");
00090       return;
00091    }
00092    TString rootsys(gSystem->DirName(rootbin));
00093    rootsys = gSystem->DirName(rootsys);
00094    TString tutorials(Form("%s/tutorials", rootsys.Data()));
00095    delete[] rootbin;
00096 
00097    // Create feedback displayer
00098    TDrawFeedback fb(proof);
00099 
00100    // Parse 'what'; it is in the form 'analysis(arg1,arg2,...)'
00101    TString args(what);
00102    args.ReplaceAll("("," "); 
00103    args.ReplaceAll(")"," "); 
00104    args.ReplaceAll(","," "); 
00105    Ssiz_t from = 0;
00106    TString act, tok;
00107    if (!args.Tokenize(act, from, " ")) {
00108       // Cannot continue
00109       Printf("runProof: action not found: check your arguments (%s)", what);
00110       return;
00111    }
00112 
00113    TObjArray *qa = new TObjArray();
00114    TString sel;
00115    // Action
00116    if (act == "simple") {
00117       sel = "ProofSimple";
00118    } else if (act == "h1") {
00119       sel = "h1analysis";
00120    } else if (act == "pythia8") {
00121       sel = "ProofPythia";
00122    } else {
00123       // Do not know what to run
00124       Printf("runProof: unknown tutorial: %s", what);
00125    }
00126 
00127    // Get last completed queries for the chosen analysis
00128    TString ref;
00129    Int_t nt = qt->GetEntriesFast();
00130    while (ref.IsNull() && nt--) {
00131       lasttag = ((TObjString *)(qt->At(nt)))->GetName();
00132       if (!lasttag.IsNull())
00133          Printf("runProof: checking session: %s", lasttag.Data());
00134       TIter nxq(ql);
00135       TQueryResult *qr = 0;
00136       while ((qr = (TQueryResult *)nxq())) {
00137          if (qr->IsDone() && !lasttag.CompareTo(qr->GetTitle()) &&
00138                            !sel.CompareTo(qr->GetSelecImp()->GetTitle())) {
00139             TString r = Form("%s:%s",qr->GetTitle(),qr->GetName());
00140             qa->Add(new TObjString(r.Data()));
00141          }
00142       }
00143       if (qa->GetEntriesFast() > 0) {
00144          Int_t qn = 0;
00145          if (qa->GetEntriesFast() > 1) {
00146             // Query the client which query to finalize
00147             Printf("finalizeProof: queries completed for analysis '%s'", act.Data());
00148             for (Int_t k = 0; k < qa->GetEntriesFast(); k++) {
00149                Printf(" [%d] %s", k, ((TObjString *)(qa->At(k)))->GetName());
00150             }
00151             Bool_t ask = kTRUE;
00152             while (ask) {
00153                char *answer = Getline("finalizeProof: enter the one you would like to finalize? [0] ");
00154                if (answer) {
00155                   if (answer[0] == 'Q' || answer[0] == 'q') {
00156                      ask = kFALSE;
00157                      return;
00158                   }
00159                   TString sn(answer);
00160                   sn.Remove(sn.Length()-1);
00161                   if (sn.IsDigit()) {
00162                      qn = sn.Atoi();
00163                      if (qn >= 0 && qn < qa->GetEntriesFast()) {
00164                         break;
00165                      } else {
00166                         Printf("finalizeProof: choice must be in [0,%d] ('Q' to quit)",
00167                               qa->GetEntriesFast()-1);
00168                      }
00169                   } else {
00170                      if (sn.IsNull()) {
00171                         qn = 0;
00172                         break;
00173                      } else {
00174                         Printf("finalizeProof: choice must be a number in [0,%d] ('Q' to quit) (%s)",
00175                               qa->GetEntriesFast()-1,  sn.Data());
00176                      }
00177                   }
00178                }
00179             }
00180          }
00181          ref = ((TObjString *)(qa->At(qn)))->GetName();
00182       }
00183    }
00184    if (!ref.IsNull()) {
00185       // Retrieve
00186       proof->Retrieve(ref);
00187       // Finalize
00188       proof->Finalize(ref);
00189    } else {
00190       Printf("runProof: no queries to be finalized for analysis '%s'", act.Data());
00191       return;
00192    }
00193 }

Generated on Tue Jul 5 15:44:52 2011 for ROOT_528-00b_version by  doxygen 1.5.1