LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
RootGraphicsEnabler.cxx
Go to the documentation of this file.
1 
18 // ROOT libraries
19 #include "TROOT.h"
20 #include "TApplication.h"
21 #include "TGClient.h"
22 #include "TSystem.h"
23 #include "TVirtualX.h"
24 #include "TGX11.h" // this header currently triggers a compiler error,
25  // that we had to disable via compiler flag -Wno-variadic-macros
26 
27 // C/C++ standard libraries
28 #include <iostream>
29 #include <string>
30 #include <stdexcept> // std::runtime_error
31 #include <cstdlib> // getenv()
32 
33 
34 namespace { // local namespace
35 
51  struct RootGraphicsEnablerClass {
52 
54  RootGraphicsEnablerClass() { EnableRootGraphics(); }
55 
57  RootGraphicsEnablerClass(std::ostream& out) { EnableRootGraphics(&out); }
58 
61  static void EnableRootGraphics(std::ostream* out = nullptr);
62 
63  }; // RootGraphicEnablerClass
64 
65  // static instance that is here only to ensure the initialization function is called;
66  // this is currently verbose
67  RootGraphicsEnablerClass RootGraphicsEnabler{
68 #if not defined(NDEBUG) // output only if debug is not suppressed
69  std::cout
70 #endif
71  };
72 
73 
74  void RootGraphicsEnablerClass::EnableRootGraphics
75  (std::ostream* out /* = nullptr */)
76  {
77 
78  if (out) (*out) << "RootGraphicsEnablerClass hacking its way forth." << std::endl;
79 
80  //======================================================================
81  // Setup the root environment for a program started with no arguments
82  //======================================================================
83 
84  if (out) (*out) << " ==> get the current TApplication (and make sure gROOT is valid)" << std::endl;
85  TApplication* app = ROOT::GetROOT()->GetApplication();
86 
87  // ROOT::GetROOT() should initialize gROOT.
88  if (!gROOT)
89  throw std::runtime_error("RootGraphicsEnabler: no ROOT global pointer");
90 
91  if (!app) {
92  if (out) (*out) << " ==> create a TApplication" << std::endl;
93  int argc = 0;
94  char** argv = nullptr;
95  new TApplication("TApplicationFromRootGraphicsEnabler", &argc, argv);
96  } // if no application
97 
98  if (out) (*out) << " ==> set batch mode off (now it is " << (gROOT->IsBatch()? "on": "already off") << std::endl;
99  gROOT->SetBatch(kFALSE);
100 
101  if (!gClient) {
102  if (out) (*out) << " ==> creating a TGClient" << std::endl;
103 
104  if (out) (*out) << " ==> loading graphics library (X11)" << std::endl;
105  int res = gSystem->Load("libGX11.so");
106  if (out) {
107  switch (res) {
108  case 0: break; // successfully loaded
109  case 1: (*out) << " (it was already)" << std::endl; break;
110  case -1: (*out) << " ERROR: not found!" << std::endl; break;
111  case -2: (*out) << " ERROR: version mismatch!" << std::endl; break;
112  default: (*out) << " ERROR: undocumented (code=" << res << ")" << std::endl; break;
113  } // switch
114  }
115 
116  if (out) (*out) << " ==> creating TVirtualX" << std::endl;
117  gVirtualX = new TGX11("X11", "X11 session");
118 
119  std::string const DISPLAY = getenv("DISPLAY");
120 
121  if (out) (*out) << " ==> creating the TGClient (DISPLAY='" << DISPLAY << "')" << std::endl;
122  new TGClient(DISPLAY.c_str());
123  } // if no graphics client
124 
125  if (out) (*out) << "RootGraphicsEnablerClass hacking compleled." << std::endl;
126 
127  } // RootGraphicsEnabler::EnableRootGraphics()
128 
129 } // local namespace
130