1 |
// $Id: g4libs.C 496 2010-05-19 15:55:27Z ivana $ |
2 |
|
3 |
//------------------------------------------------ |
4 |
// The Virtual Monte Carlo examples |
5 |
// Copyright (C) 2007, Ivana Hrivnacova |
6 |
// All rights reserved. |
7 |
// |
8 |
// For the licensing terms see geant4_vmc/LICENSE. |
9 |
// Contact: vmc@pcroot.cern.ch |
10 |
//------------------------------------------------- |
11 |
|
12 |
/// \file g4libs.C |
13 |
/// \brief Macro for loading Geant4 and Geant4 VMC libraries |
14 |
/// |
15 |
/// A new macro for loading Geant4 and Geant4 VMC libraries |
16 |
/// with using liblist utility provided in Geant4 |
17 |
/// |
18 |
/// \author Christian Holm Christensen, NBI; |
19 |
/// Dmitry Naumov, JINR |
20 |
// |
21 |
// Macro for loading Geant4 and Geant4 VMC libraries |
22 |
|
23 |
#if !defined(__CINT__) || defined(__MAKECINT__) |
24 |
|
25 |
#include <vector> |
26 |
#include <string> |
27 |
#include <sstream> |
28 |
#include <iostream> |
29 |
#include <iomanip> |
30 |
|
31 |
#include <TSystem.h> |
32 |
#include <Riostream.h> |
33 |
#include <TCint.h> |
34 |
#include <TError.h> |
35 |
#include <TMath.h> |
36 |
#include <TApplication.h> |
37 |
#include <TROOT.h> |
38 |
|
39 |
#endif |
40 |
|
41 |
void loadg4libs(); |
42 |
|
43 |
void PamG4Libs() |
44 |
{ |
45 |
/// Function for loading all libraries for running VMC with Geant4 |
46 |
loadg4libs(); |
47 |
|
48 |
gSystem->SetFPEMask(0); |
49 |
} |
50 |
|
51 |
string NoSpaces(string s) |
52 |
{ |
53 |
/// ??? |
54 |
|
55 |
std::stringstream str(s); |
56 |
std::string token; |
57 |
std::getline(str, token, '\n'); |
58 |
return token; |
59 |
} |
60 |
|
61 |
Bool_t isLibrary(const char* libName) |
62 |
{ |
63 |
/// Helper function which testes the existence of the given library |
64 |
/// \param libName The library name |
65 |
|
66 |
if (TString(gSystem->DynamicPathName(libName, kTRUE)) != TString("")) |
67 |
return kTRUE; |
68 |
else |
69 |
return kFALSE; |
70 |
} |
71 |
|
72 |
Bool_t isBatch() |
73 |
{ |
74 |
/// Helper function which testes if Root was started in batch mode |
75 |
|
76 |
for ( Int_t i=0; i<gApplication->Argc(); ++i ) |
77 |
if ( TString(gROOT->GetApplication()->Argv(i)) == "-b" ) return true; |
78 |
|
79 |
return false; |
80 |
} |
81 |
|
82 |
Bool_t isSet(const char* variable) |
83 |
{ |
84 |
/// Helper function which checks if the specified environment variable |
85 |
/// is set. |
86 |
/// \param variable The environment variable name |
87 |
|
88 |
TString value = gSystem->Getenv(variable); |
89 |
if ( value != "") return true; |
90 |
|
91 |
return false; |
92 |
} |
93 |
|
94 |
void vgmlibs() |
95 |
{ |
96 |
/// Function for loading VGM libraries. |
97 |
|
98 |
if ( isSet("USE_VGM") ) { |
99 |
cout << "Loading VGM libraries ... " << endl; |
100 |
gSystem->Load("libClhepVGM"); |
101 |
gSystem->Load("libBaseVGM"); |
102 |
gSystem->Load("libGeant4GM"); |
103 |
gSystem->Load("libRootGM"); |
104 |
gSystem->Load("libXmlVGM"); |
105 |
} |
106 |
} |
107 |
|
108 |
void GetLinkLine(string& all_lines) |
109 |
{ |
110 |
/// Build the string with the list of libraries using liblist |
111 |
|
112 |
// Geant4 lib directory |
113 |
TString g4lib = gSystem->Getenv("G4LIB"); |
114 |
if ( g4lib.Length() == 0 ) |
115 |
g4lib = gSystem->Getenv("G4INSTALL") + TString("/lib"); |
116 |
g4lib += "/"+TString(gSystem->Getenv("G4SYSTEM")); |
117 |
|
118 |
// Build the string with the list of libraries using liblist |
119 |
TString command |
120 |
// = "echo -L"+g4lib+" `" + g4lib+"/liblist -m "+g4lib +" < " + g4lib+"/libname.map`"; |
121 |
= "echo -L"+g4lib+" `cd " +g4lib+"; for file in \\`ls -L *.so | sed s/ib// | sed s/.so//\\`; do echo -$file; done`"; |
122 |
|
123 |
std::cout << "Executing: " << command << std::endl; |
124 |
|
125 |
FILE* pipe = gSystem->OpenPipe(command, "r"); |
126 |
char line[100]; |
127 |
while ( fgets(line, sizeof(line), pipe ) != NULL ) { |
128 |
all_lines += line; |
129 |
} |
130 |
|
131 |
std::cout << all_lines << std::endl; |
132 |
|
133 |
} |
134 |
|
135 |
void HandleLinkLine(const char* str, const char* what) |
136 |
{ |
137 |
/// Tokenize the input string and load/unload the libraries |
138 |
/// from the list. |
139 |
/// \param str The string output from Geant4 liblist |
140 |
/// \param what The option specifying whether we want to load ('l') or |
141 |
/// unload ('u') libraries |
142 |
|
143 |
// Fill the libs names in the vector |
144 |
std::vector<std::string> libs; |
145 |
std::stringstream sstream(str); |
146 |
unsigned int w = 0; |
147 |
while ( ! sstream.eof() ) { |
148 |
// Read one string |
149 |
std::string token; |
150 |
std::getline(sstream, token, ' '); |
151 |
|
152 |
// Check stream status |
153 |
if ( sstream.bad() ) break; |
154 |
|
155 |
// Check that we got a meaningful tokenonent |
156 |
if ( token.empty() || std::isspace(token[0]) ) continue; |
157 |
|
158 |
if ( token[0] != '-' ) { |
159 |
Warning("LoadLibraryList", "Unknown element %s", token.c_str()); |
160 |
continue; |
161 |
} |
162 |
|
163 |
std::string dir_or_file = token.substr(2,token.size()-2); |
164 |
if ( token[1] == 'L' ) { |
165 |
std::stringstream path; |
166 |
path << gSystem->GetDynamicPath() << ":" |
167 |
<< dir_or_file; |
168 |
gSystem->SetDynamicPath(path.str().c_str()); |
169 |
} |
170 |
else if ( token[1] == 'l' ) { |
171 |
std::stringstream ln; |
172 |
ln << "lib" << NoSpaces(dir_or_file) << '.' << gSystem->GetSoExt(); |
173 |
std::string lib(ln.str()); |
174 |
libs.push_back(lib); |
175 |
if ( lib.length() > w ) w = lib.length(); |
176 |
} |
177 |
else { |
178 |
Warning("LoadLibraryList", "Unknown option %s in", |
179 |
token.c_str(), str); |
180 |
continue; |
181 |
} |
182 |
} |
183 |
|
184 |
// Process the vector with libs names and load libraries |
185 |
size_t n = libs.size(); |
186 |
TString sWhat(what); |
187 |
Bool_t load = sWhat.Contains("l"); |
188 |
if (!load && !sWhat.Contains("u")) { |
189 |
std::cerr << " Unknown load action " << what << std::endl; |
190 |
return; |
191 |
} |
192 |
|
193 |
for ( size_t i = 0; i < n; ++i ) { |
194 |
size_t idx = n - i - 1; |
195 |
|
196 |
// Uncomment to debug |
197 |
// size_t m = TMath::Log10(n)+1; |
198 |
// string say=" Loading "; |
199 |
// if ( TString(what).Contains("u") ) say = " Unloading "; |
200 |
// std::cout << say << std::setw(m) << (i+1) |
201 |
// << "/" << std::setw(m) << n |
202 |
// << ": " << std::setw(w) << libs[idx] // << std::endl; |
203 |
// << std::flush; |
204 |
|
205 |
int result = 0; |
206 |
if ( libs[idx].c_str() ) { |
207 |
if (load) |
208 |
result = gSystem->Load(libs[idx].c_str()); |
209 |
else |
210 |
gSystem->Unload(libs[idx].c_str()); |
211 |
} |
212 |
// Uncomment to debug |
213 |
// if ( TString(what).Contains("l") ) |
214 |
// std::cout << ( result < 0 ? " failed" : " ok" ) << "\r"; |
215 |
} |
216 |
// std::cout << "\n Done" << std::endl; |
217 |
} |
218 |
|
219 |
void loadg4libs() |
220 |
{ |
221 |
/// The function to unload Geant4 libraries |
222 |
|
223 |
// CLHEP |
224 |
gSystem->Load("libCLHEP"); |
225 |
|
226 |
// xerces-c library if GDML is activated |
227 |
if ( isSet("G4LIB_BUILD_GDML") ) { |
228 |
gSystem->Load("libxerces-c"); |
229 |
} |
230 |
|
231 |
// Get the string with the list of libraries |
232 |
string all_lines; |
233 |
GetLinkLine(all_lines); |
234 |
|
235 |
// Load Geant4 libraries |
236 |
cout << "Loading Geant4 libraries ..." << endl; |
237 |
HandleLinkLine(all_lines.c_str(),"l"); |
238 |
|
239 |
// VGM librares |
240 |
vgmlibs(); |
241 |
|
242 |
// G4Root library (if available) |
243 |
if ( isLibrary("libg4root") ) { |
244 |
cout << "Loading g4root library ..." << endl; |
245 |
gSystem->Load("libg4root"); |
246 |
} |
247 |
|
248 |
// Geant4 VMC library |
249 |
cout << "Loading geant4vmc library ..." << endl; |
250 |
gSystem->Load("libgeant4vmc"); |
251 |
|
252 |
// Geant4 VMC GUI library |
253 |
// (if available and Root is not running in batch mode) |
254 |
if ( isLibrary("libgeant4vmc_gui") && ! isBatch() ) { |
255 |
cout << "Loading geant4vmc_gui library ... " << endl; |
256 |
gSystem->Load("libgeant4vmc_gui"); |
257 |
} |
258 |
} |
259 |
|
260 |
void unloadg4libs() |
261 |
{ |
262 |
/// The function to unload Geant4 libraries |
263 |
|
264 |
// Get the string with the list of libraries |
265 |
string all_lines; |
266 |
GetLinkLine(all_lines); |
267 |
|
268 |
// Load Geant4 libraries |
269 |
cout << "Unloading Geant4 libraries ..." << endl; |
270 |
HandleLinkLine(all_lines.c_str(),"u"); |
271 |
} |
272 |
|