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