Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

JobCRootModule.cxx

Go to the documentation of this file.
00001 
00002 // $Id: JobCRootModule.cxx,v 1.8 2002/06/13 15:45:49 rhatcher Exp $
00004 #include "JobControl/JobCRootModule.h"
00005 
00006 #include <cassert>
00007 
00008 // ROOT include files
00009 #include "TROOT.h"
00010 #include "TSystem.h"
00011 #include "TObjectTable.h"
00012 #include "TRint.h"
00013 #include "TEnv.h"
00014 #include "TInterpreter.h"
00015 // for function: char* Form(const char *fmt, ...)
00016 #include "TString.h"
00017 
00018 #include "JobControl/JobCModuleRegistry.h"
00019 #include "JobControl/JobCommand.h"
00020 
00021 #include "MessageService/MsgService.h"
00022 
00023 CVSID("$Id: JobCRootModule.cxx,v 1.8 2002/06/13 15:45:49 rhatcher Exp $");
00024 JOBMODULE(JobCRootModule, 
00025           "Root", "Directly handle ROOT commands passed to JobC");
00026 
00027 //......................................................................
00028 
00029 JobCRootModule::JobCRootModule() 
00030 {
00031    MSG("Root",Msg::kVerbose) << "JobCRootModule::ctor" << endl;
00032 }
00033 
00034 //......................................................................
00035 
00036 JobCRootModule::~JobCRootModule() 
00037 {
00038    MSG("Root",Msg::kVerbose) << "JobCRootModule::dtor" << endl;
00039 }
00040 
00041 //......................................................................
00042 
00043 void JobCRootModule::Report() 
00044 {
00045    MSG("Root",Msg::kWarning) << "JobCRootModule::Report" << endl;
00046 }
00047 
00048 //......................................................................
00049 
00050 void JobCRootModule::Reset() 
00051 {
00052    MSG("Root",Msg::kWarning) << "JobCRootModule::Reset" << endl;
00053 }
00054 
00055 //......................................................................
00056 
00057 void JobCRootModule::InitRoot() 
00058 {
00059    // Perform any necessary initialization
00060    // including putting TApplication into TRint-like mode
00061    MSG("Root",Msg::kVerbose) << "JobCRootModule::InitRoot" << endl;
00062 
00063    static bool first = true;
00064    if (!first) return;
00065 
00066    first = false;
00067    // Make sure there is a TApp running
00068    // if we plan on using CINT facilities
00069    TApplication* app = gROOT->GetApplication();
00070    if (app) {
00071       TRint* trint = dynamic_cast<TRint*>(app);
00072       if (!trint) {
00073          MSG("Root",Msg::kDebug) 
00074             << "JobCRootModule::InitRoot not a TRint" << endl;
00075          // initialize Root's CINT to be more like it would be
00076          // if this were a TRint, while still not running
00077          // the user's 
00078          app->ProcessLine("#include <iostream.h>");
00079          
00080          // TRint would do the following:
00081 #ifdef TRINTLIKE
00082          // The following libs are also useful to have,
00083          // make sure they are loaded...
00084          gROOT->LoadClass("TGeometry",   "Graf3d");
00085          gROOT->LoadClass("TTree",       "Tree");
00086          gROOT->LoadClass("TMatrix",     "Matrix");
00087          gROOT->LoadClass("TMinuit",     "Minuit");
00088          gROOT->LoadClass("TPostScript", "Postscript");
00089          gROOT->LoadClass("TCanvas",     "Gpad");
00090          gROOT->LoadClass("THtml",       "Html");
00091 #endif
00092       } else {
00093          MSG("Root",Msg::kDebug)
00094             << "JobCRootModule::InitRoot was a TRint" << endl;
00095       }
00096       app->ProcessLine("#include <iomanip.h>");
00097       app->ProcessLine("#include <string>");
00098       gInterpreter->SaveContext();
00099       gInterpreter->SaveGlobalsContext();
00100    } else {
00101       MSG("Root",Msg::kFatal)
00102          << "JobCRootModule::InitRoot no TApplication" << endl;
00103    }
00104 
00105 }
00106 
00107 //......................................................................
00108 void JobCRootModule::PassPtrToRoot(string className, 
00109                                    string instanceName,
00110                                    const void* ptr, bool isConst)
00111 {
00112    // Pass a pointer to some class object to ROOT so
00113    // ProcessLine has access to the object
00114    // any "const"ness of "ptr" is *NOT* preserved if you lie via "isConst"
00115 
00116    // make sure TApp is running so that CINT with <iostream.h> is defined
00117    InitRoot();
00118    TApplication* app = gROOT->GetApplication();
00119 
00120    const char*  strConst = "";
00121    if (isConst) strConst = "const";
00122 
00123    // thanks Brett Viren for this "dirty trick".
00124    string theline = Form("%s %s* %s = (%s %s *)0x%lx",
00125                          strConst,
00126                          className.c_str(),
00127                          instanceName.c_str(),
00128                          strConst,
00129                          className.c_str(),
00130                          reinterpret_cast<unsigned long>(ptr));
00131 
00132    MSG("Root",Msg::kDebug) 
00133       << "JobCRootCommandModule::PassPtrToRoot " 
00134       << endl << theline << endl;
00135 
00136    app->ProcessLine(theline.c_str());
00137 }
00138 
00139 //......................................................................
00140 
00141 void JobCRootModule::HandleCommand(JobCommand* cmd) 
00142 {
00143 
00144    static int ncmd = 0;
00145    ncmd++;
00146 
00147    class BadJobCRootCmd { };
00148 
00149    // make sure TApp is running so that CINT with <iostream.h> is defined
00150    InitRoot();
00151 
00152    try {
00153       TApplication* app = gROOT->GetApplication();
00154       const char* c = cmd->PopCmd();
00155       if (c) {
00156          string sc(c);
00157          if ((sc == "RunMacro")            || 
00158              (sc == "LoadMacro")           || 
00159              (sc == "CompileMacro")        || 
00160              (sc == "ForceCompileMacro")   || 
00161              (sc == "ProcessLine")            ) {
00162             string wholeLine;
00163             // eat up rest of line
00164             while (const char* opt = cmd->PopOpt()) {
00165                wholeLine += " ";  // put back in the spaces
00166                wholeLine += opt;
00167             }
00168             if (sc == "RunMacro")  wholeLine.insert(0,".x ");
00169             if (sc == "LoadMacro" || sc == "CompileMacro") 
00170                wholeLine.insert(0,".L ");
00171             if (sc == "CompileMacro") wholeLine += "+";
00172             if (sc == "ForceCompileMacro") wholeLine += "++";
00173             app->ProcessLine(wholeLine.c_str());
00174          }
00175          else if (sc == "StartBrowser") {
00176             static int nbrowser = 0; 
00177             nbrowser++;
00178             string bstart("TBrowser RootModuleBrowser");
00179             bstart += Form("%d",nbrowser);
00180             app->ProcessLine(bstart.c_str());
00181          }
00182          else if (sc == "LoadLibrary") {
00183             const char* libraryName = cmd->PopOpt();
00184             Int_t status = gSystem->Load(libraryName);
00185             MSG("Root",Msg::kInfo) 
00186                << Form("(Int_t) %d = gSystem->Load(%s)",
00187                        status,libraryName)
00188                << endl;
00189          }
00190          else if (sc == "LoadClass") {
00191             const char* className   = cmd->PopOpt();
00192             const char* libraryName = cmd->PopOpt();
00193             Int_t status = gROOT->LoadClass(className,libraryName);
00194             MSG("Root",Msg::kInfo) 
00195                << Form("(Int_t) %d = gROOT->LoadClass(%s,%s)",
00196                        status,className,libraryName)
00197                << endl;
00198          }
00199          else if (sc == "MacroPath") {
00200             const char* c2 = cmd->PopCmd();
00201             // if no further part of the command: assume user meant "Reset"
00202             if (!c2) c2 = "Reset";
00203             string sc2(c2);
00204             const char* opt = cmd->PopOpt();
00205             if (!opt) opt = "";
00206             if (sc2 == "Add") {
00207                // add first opt to from of macropath
00208                TString tsopt(opt);
00209                tsopt += ":";
00210                tsopt += TROOT::GetMacroPath();
00211                TROOT::SetMacroPath(tsopt);
00212             }
00213             else if (sc2 == "Reset") {
00214                // reset the path
00215                // if user supplied an option then full path is that
00216                // if not then the path is cleared and will be refetched
00217                // from the gEnv "database".
00218                TROOT::SetMacroPath(opt);
00219             }
00220             else if (sc2 == "Show") {
00221                // do nothing we'll always show the value
00222             }
00223             else {
00224                MSG("Root",Msg::kWarning) 
00225                   << "\"" << sc2 << "\": "
00226                   << "Not a valid sub-comand of " << sc << endl;
00227                throw BadJobCRootCmd();
00228             }
00229             MSG("Root",Msg::kInfo)
00230                << "MacroPath is \""
00231                << TROOT::GetMacroPath()
00232                << "\"" << endl;
00233          }
00234          else if (sc == "ObjectStat") {
00235             const char* c2 = cmd->PopCmd();
00236             // if no further part of the command: assume user meant "Print"
00237             if (!c2) c2 = "Print";
00238             string sc2(c2);
00239             if (sc2 == "Print") {
00240                if (gObjectTable) {
00241                   gObjectTable->Print();
00242                } else {
00243                   MSG("Root",Msg::kInfo) 
00244                      << "Sorry no gObjectTable available" << endl;
00245                }
00246             }
00247             else if (sc2 == "Set") {
00248                bool toggle = true;
00249                const char* val = cmd->PopOpt();
00250                if (val) {
00251                   // there was an actual value, use that
00252                   cmd->PushOpt();
00253                   toggle = (bool) cmd->PopIntOpt();
00254                }
00255                TObject::SetObjectStat(toggle);
00256             }
00257             else {
00258                MSG("Root",Msg::kWarning) 
00259                   << "\"" << sc2 << "\": "
00260                   << "Not a valid sub-comand of " << sc<< endl;
00261                throw BadJobCRootCmd();
00262             }
00263          }
00264          else if (sc == "Reset") {
00265             gROOT->Reset(cmd->PopOpt());
00266          }
00267          else if (sc == "SaveContext") {
00268             gROOT->SaveContext();
00269          }
00270          else if (sc == "SetStyle") {
00271             gROOT->SetStyle(cmd->PopOpt());
00272          }
00273          else {
00274             MSG("Root",Msg::kWarning) 
00275                << "\"" << sc << "\":"
00276                << " Not a valid command" << endl;
00277             throw BadJobCRootCmd();
00278          }
00279       }
00280       else {
00281          throw BadJobCRootCmd();
00282       }
00283    }
00284    catch ( BadJobCRootCmd ) {
00285       static bool firsthelp = true;
00286       if (firsthelp) Help();
00287       firsthelp = false;
00288    }
00289 }
00290 
00291 //......................................................................
00292 
00293 void JobCRootModule::Help()
00294 {
00295    //
00296    // Print help for this module
00297    //
00298 
00299    const char *name = this->GetName();
00300 
00301    MSG("JobC", Msg::kInfo)
00302       << "Help for '" << name << "': " 
00303       << name 
00304       << " is a JobCModule interfacing to ROOT from JobControl" << endl
00305       << "Commands implemented:" 
00306       << endl << endl
00307 //--------------
00308       << "  /" << name << "/RunMacro   <macro.C>    // root [] .x <macro.C>"
00309       << endl
00310       << "  /" << name << "/LoadMacro  <macro.C>    // root [] .L <macro.C>"
00311       << endl
00312       << "  /" << name << "/CompileMacro  <macro.C> // root [] .L <macro.C>+"
00313       << endl
00314       << "  /" << name << "/ForceCompileMacro  <macro.C> // root [] .L <macro.C>++"
00315       << endl
00316       << "     Run or Load a macro file, like .x or .L at ROOT prompt"
00317       << endl << endl
00318 //--------------
00319       << "  /" << name << "/ProcessLine <arbitrary text>"
00320       << endl
00321       << "     Process line just like at ROOT prompt"
00322       << endl << endl
00323 //--------------
00324       << "  /" << name << "/StartBrowser"
00325       << endl
00326       << "     Start a TBrowser"
00327       << endl << endl
00328 //--------------
00329       << "  /" << name << "/LoadLibrary <libraryName> "
00330       << endl
00331       << "     Make a dynamic library available (via gSystem)"
00332       << endl << endl
00333 //--------------
00334       << "  /" << name << "/LoadClass <className> <libraryName> "
00335       << endl
00336       << "     Make a class available out of a dynamic library (via gROOT)"
00337       << endl << endl
00338 //--------------
00339       << "  /" << name << "/MacroPath/Add <location(s)>"
00340       << endl
00341       << "  /" << name << "/MacroPath/Reset [<full list>]"
00342       << endl
00343       << "  /" << name << "/MacroPath/Show"
00344       << endl
00345       << "     Manipulate the locations where ROOT looks for macros;"
00346       << endl
00347       << "     path is a colon separated list:"
00348       << endl
00349       << "        Add to the front of the list"
00350       << endl
00351       << "        Reset to fixed value (default: initial ROOT defined value)"
00352       << endl
00353       << "        Show the current setting"
00354       << endl << endl
00355 //--------------
00356       << "  /" << name << "/ObjectStat/Set [1|0]"
00357       << endl
00358       << "  /" << name << "/ObjectStat/Print"
00359       << endl
00360       << "     TObject statistics collection (via gObjectTable):"
00361       << endl
00362       << "        Turn [on|off] the statistics collection (default: on)"
00363       << endl
00364       << "        Print the gObjectTable"
00365       << endl << endl
00366 //--------------
00367       << "  /" << name << "/Reset "
00368       << endl
00369       << "     Reset ROOT"
00370       << endl << endl
00371 //--------------
00372       << "  /" << name << "/SaveContext"
00373       << endl
00374       << "     Have the interpreter save the current context"
00375       << endl << endl
00376 //--------------
00377       << "  /" << name << "/SetStyle <styleName>"
00378       << endl
00379       << "     Set the ROOT Style"
00380       << endl << endl
00381 //--------------
00382       << endl;
00383 }
00384 

Generated on Mon Feb 15 11:06:49 2010 for loon by  doxygen 1.3.9.1