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

JobCRootEnv.cxx

Go to the documentation of this file.
00001 
00002 // $Id: JobCRootEnv.cxx,v 1.35 2007/04/26 14:22:13 west Exp $
00003 //
00004 // A class to manage the ROOT global environment
00005 //
00006 // messier@huhepl.harvard.edu
00008 #include "JobControl/JobCRootEnv.h"
00009 
00010 #include <TSystem.h>
00011 
00012 #include <iostream>
00013 #include <cassert>     // assert
00014 #include <cstring>     // strlen, strncmp
00015 #include <cstdio>      // sprintf
00016 using namespace std;
00017 extern "C" {
00018 #include <sys/types.h> // opendir, closedir
00019 #include <dirent.h>    // opendir, closedir
00020 #ifdef MACOSX
00021   #include <unistd.h>
00022 #else
00023   #include <getopt.h>
00024 #endif
00025 #ifndef GETOPTDONE     // Some getopt.h's have this, some don't...
00026 #define GETOPTDONE (-1)
00027 #endif
00028 }
00029 #include "MessageService/MsgService.h"
00030 // ROOT include files
00031 #include "TROOT.h"
00032 #include "TRint.h"
00033 #include "TSystem.h"
00034 #include "TString.h"
00035 #include "TSysEvtHandler.h"
00036 #include "TInterpreter.h"
00037 
00038 #include "JobControl/IsArgMacroFile.h"
00039 
00040 CVSID("$Id: JobCRootEnv.cxx,v 1.35 2007/04/26 14:22:13 west Exp $");
00041 
00042 //......................................................................
00043 
00044 JobCRootEnv::JobCRootEnv(int argc, char** argv) : 
00045   fHaveBatchOpt(false),
00046   fHaveQuitOpt(false),
00047   fHaveMacroFiles(false),
00048   fHaveInaccessibleFile(false)
00049 {
00050 //======================================================================
00051 // Purpose: Set up the ROOT environment
00052 //
00053 // Inputs:
00054 //  argc - number of command line arguments
00055 //  argv - array of command line arguments
00056 //======================================================================
00057   // Root only understands a subset of the command line arguments we
00058   // might want to allow. This makes a copy of command line arguments
00059   // ROOT understands.
00060   static int   localArgc = 0;   // Copy of command line args.
00061   static char* localArgv[1024]; // Copy of command line args.
00062   if (argc>0) {
00063     localArgv[localArgc++] = argv[0];
00064   }
00065   else {
00066     localArgv[localArgc++] = const_cast<char*>("moot"); // Dummy program name
00067   }
00068   
00069   // Setup the ROOT globals. gROOT from TROOT.h
00070   if (gROOT==0) {
00071     static TROOT root("TROOT",localArgv[0]);
00072   }
00073   assert(gROOT);
00074 
00075   // Setup the application
00076   TApplication* app = gROOT->GetApplication();
00077   if (app==0) {
00078     int c;
00079 
00080 #ifndef MACOSX
00081   optind = 0; // getopt.h: Reset getopt to start of arguments
00082 #else
00083   optind = 1; // skip 0th argument ("loon") for MACOSX
00084 #endif
00085 
00086 #ifdef IRIX6
00087     getoptreset(); // needed by IRIX to reset getopt
00088 #endif
00089     const char* opts = "bs:nqlt:r:x:hH:d:u:p:o:v:";
00090     while ((c=getopt(argc, argv, opts)) != GETOPTDONE) {
00091       MSG("JobC",Msg::kDebug)
00092         << "Processing job command-line ROOT option argument: "
00093         << c << " '" << (char)c << "'" << endl;
00094       switch (c) {
00095         // const_cast because these are string constants but TRint
00096         // ctor needs a char**
00097       case 'l': localArgv[localArgc++] = const_cast<char*>("-l"); break;
00098       case 'b': localArgv[localArgc++] = const_cast<char*>("-b"); fHaveBatchOpt = true; break;
00099       case 'n': localArgv[localArgc++] = const_cast<char*>("-n"); break;
00100       case 'q': localArgv[localArgc++] = const_cast<char*>("-q"); fHaveQuitOpt  = true; break;
00101       case 'h': localArgv[localArgc++] = const_cast<char*>("-h"); break;
00102       default: break;
00103       }
00104     }
00105 
00106     // Add the directory/macro file stuff to the list
00107     int ndir = 0; // Number of directories specified on command line
00108     for (int i=optind; i<argc; ++i) {
00109       // Check if argv[i] is a directory -- if yes add it to the ROOT
00110       // command line
00111       DIR* d = opendir(argv[i]);
00112       if (d) {
00113         localArgv[localArgc++] = argv[i];
00114         closedir(d);
00115         ++ndir;
00116       }
00117 
00118       // Check if argv[i] is a ROOT macro file
00119       // if yes add it to the ROOT command line
00120       TString fname = IsArgMacroFile(argv[i]);
00121       if (fname != "") {
00122         if (gSystem->AccessPathName(fname.Data())) {
00123           cerr << "Skipping inaccessible file: " << argv[i] << endl;
00124           fHaveInaccessibleFile = true;
00125           continue;
00126         }
00127         fHaveMacroFiles = true;
00128         localArgv[localArgc++] = argv[i];
00129       }
00130     }
00131     // If the user has two directories on the command line they've
00132     // made a mistake. Kindly inform them of that mistake...
00133     if (ndir>1) {
00134       MSG("JobC",Msg::kError) << 
00135         "Ambiguous: More than one directory specified on command line.\n";
00136       exit(1);
00137     }
00138 
00139     // Create the ROOT application. This is a little confusing. The
00140     // "new" goes off and puts the TRint application off into ROOT
00141     // global space (gROOT). I can check that's true with the
00142     // assert(). I'm assuming gROOT has taken control of the delete
00143     // for this class. The assert checks that the first part is
00144     // true. I guess I'd have to look at the root code to see if the
00145     // second assumption is also true.
00146     TRint* rint = new TRint("TAPP",&localArgc,localArgv,0,0,kTRUE);
00147     assert(rint == gROOT->GetApplication());
00148     std::string p = gSystem->BaseName(localArgv[0]); p += " [%d] ";
00149     rint->SetPrompt(p.c_str());    
00150 
00151     // Configure the ROOT session
00152     this->SignalConfig();
00153     this->InterpreterConfig();
00154     this->LoadIncludes();
00155     this->LoadClasses();
00156   }
00157 }
00158 
00159 //......................................................................
00160 
00161 void JobCRootEnv::InterpreterConfig()
00162 {
00163 //======================================================================
00164 // Configure the root interpreter
00165 //======================================================================
00166   if (gInterpreter) { // gInterpreter from TInterpreter.h
00167     gInterpreter->SaveContext();
00168     gInterpreter->SaveGlobalsContext();
00169   }
00170 }
00171 
00172 //......................................................................
00173 
00174 void JobCRootEnv::SignalConfig() 
00175 {
00176 //======================================================================
00177 // Configure root's signale handlers
00178 //======================================================================
00179   if (gSystem) { // gSystem from TSystem.h
00180     // Reset ROOT's signal handling to the defaults...
00181     gSystem->ResetSignal(kSigBus,                  kTRUE);
00182     gSystem->ResetSignal(kSigSegmentationViolation,kTRUE);
00183     gSystem->ResetSignal(kSigSystem,               kTRUE);
00184     gSystem->ResetSignal(kSigPipe,                 kTRUE);
00185     gSystem->ResetSignal(kSigIllegalInstruction,   kTRUE);
00186     gSystem->ResetSignal(kSigQuit,                 kTRUE);
00187     if (fHaveBatchOpt)
00188       gSystem->ResetSignal(kSigInterrupt,          kTRUE);
00189     gSystem->ResetSignal(kSigWindowChanged,        kTRUE);
00190     //rwh: 2004-09-07 leave ROOT's version of SigAlarm handling in place
00191     //gSystem->ResetSignal(kSigAlarm,                kTRUE);
00192     gSystem->ResetSignal(kSigChild,                kTRUE);
00193     gSystem->ResetSignal(kSigUrgent,               kTRUE);
00194     gSystem->ResetSignal(kSigFloatingException,    kTRUE);
00195     gSystem->ResetSignal(kSigTermination,          kTRUE);
00196     gSystem->ResetSignal(kSigUser1,                kTRUE);
00197     gSystem->ResetSignal(kSigUser2,                kTRUE);
00198   }
00199 }
00200 
00201 //......................................................................
00202 
00203 void JobCRootEnv::LoadIncludes()
00204 {
00205 //======================================================================
00206 // Load include files to make the root session more covenient
00207 //======================================================================
00208   TApplication* app = gROOT->GetApplication();
00209   if (app) {
00210     // Load a set of useful C++ includes.
00211     // app->ProcessLine("#include <iostream>"); // Root gets this one itself
00212     app->ProcessLine("#include <iomanip>");
00213     app->ProcessLine("#include <string>");
00214 
00215     // Load minos include files
00216     TString mp = gROOT->GetMacroPath();
00217     TString ip;
00218     const char* p;
00219     p = gSystem->Getenv("SRT_PRIVATE_CONTEXT");
00220     if (p) {
00221       mp += ":";
00222       mp += p;
00223       mp += ":";
00224       mp += p;
00225       mp += "/macros";
00226       ip += " -I";
00227       ip += p;
00228     }
00229     p = gSystem->Getenv("SRT_PUBLIC_CONTEXT");
00230     if (p) {
00231       mp += ":";
00232       mp += p;
00233       mp += "/macros";
00234       ip += " -I";
00235       ip += p;
00236     }
00237     
00238     gROOT->SetMacroPath(mp.Data());
00239     gSystem->SetIncludePath(ip);
00240     
00241     TString dip = ".include ";
00242     dip += gSystem->Getenv("SRT_PRIVATE_CONTEXT");
00243     gROOT->ProcessLine(dip.Data());
00244 
00245     dip = ".include ";
00246     dip += gSystem->Getenv("SRT_PUBLIC_CONTEXT");
00247     gROOT->ProcessLine(dip.Data());
00248     dip += "/RDBC/include";
00249     gROOT->ProcessLine(dip.Data());
00250   }
00251 }
00252 
00253 //......................................................................
00254 
00255 void JobCRootEnv::LoadClasses()
00256 {
00257 //======================================================================
00258 // Load classes to make the root session more covenient
00259 //======================================================================
00260   if (gROOT) {
00261     gROOT->LoadClass("TGeometry",   "Graf3d");
00262     gROOT->LoadClass("TTree",       "Tree");
00263     gROOT->LoadClass("TMatrix",     "Matrix");
00264     gROOT->LoadClass("TMinuit",     "Minuit");
00265     gROOT->LoadClass("TPostScript", "Postscript");
00266     gROOT->LoadClass("TCanvas",     "Gpad");
00267     gROOT->LoadClass("THtml",       "Html");
00268   }
00269 }
00270 
00271 //......................................................................
00272 
00273 JobCRootEnv::~JobCRootEnv() 
00274 {
00275 //======================================================================
00276 // Purpose: Shut down the ROOT session
00277 //======================================================================
00278 }
00279 
00280 //......................................................................
00281 
00282 int JobCRootEnv::RunTheApp()
00283 {
00284 //======================================================================
00285 // Purpose: Turn control over to the ROOT application
00286 //======================================================================
00287   TApplication* app = gROOT->GetApplication();
00288   if (app) {
00289     app->Run(kTRUE); // kTRUE == "Return from run" request...
00290     return 1;
00291   }
00292   return 0;
00293 }
00294 

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