source: JEM-EUSO/esaf_lal/tags/v1_r0/esaf/doc/g4libs.C @ 117

Last change on this file since 117 was 117, checked in by moretto, 11 years ago

ESAF version compilable on mac OS

File size: 6.8 KB
Line 
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
41void loadg4libs();
42
43void g4libs()
44{
45/// Function for loading all libraries for running VMC with Geant4
46//   gROOT->LoadMacro("basiclibs.C");
47//   basiclibs();
48  loadg4libs();
49
50  gSystem->SetFPEMask(0); 
51}   
52
53string NoSpaces(string s)
54{
55/// ???
56
57  std::stringstream str(s);
58  std::string token;
59  std::getline(str, token, '\n');
60  return token;
61}
62
63Bool_t isLibrary(const char* libName)
64{
65/// Helper function which testes the existence of the given library
66/// \param libName  The library name
67
68  if (TString(gSystem->DynamicPathName(libName, kTRUE)) != TString(""))
69    return kTRUE;
70  else 
71    return kFALSE;
72}   
73
74Bool_t isBatch()
75{
76/// Helper function which testes if Root was started in batch mode
77
78  for ( Int_t i=0; i<gApplication->Argc(); ++i ) 
79    if ( TString(gROOT->GetApplication()->Argv(i)) == "-b" ) return true;
80 
81  return false;
82}   
83
84Bool_t isSet(const char* variable)
85{
86/// Helper function which checks if the specified environment variable
87/// is set.
88/// \param variable  The environment variable name
89
90  TString value = gSystem->Getenv(variable);
91  if ( value != "") return true;
92 
93  return false;
94} 
95
96void vgmlibs()
97{ 
98/// Function for loading VGM libraries.
99
100  if ( isSet("USE_VGM") ) { 
101    cout << "Loading VGM libraries ... " << endl;
102    gSystem->Load("libClhepVGM");
103    gSystem->Load("libBaseVGM");
104    gSystem->Load("libGeant4GM");
105    gSystem->Load("libRootGM");
106    gSystem->Load("libXmlVGM");
107  } 
108}
109
110void GetLinkLine(string& all_lines) 
111{
112/// Build the string with the list of libraries using liblist
113
114  // Geant4 lib directory
115  TString g4lib  = gSystem->Getenv("G4LIB");
116  if ( g4lib.Length() == 0 ) 
117    g4lib  = gSystem->Getenv("G4INSTALL") + TString("/lib");
118  g4lib += "/"+TString(gSystem->Getenv("G4SYSTEM"));
119 
120  // Build the string with the list of libraries using liblist
121  TString command
122    = "echo -L"+g4lib+" `" + g4lib+"/liblist -m "+g4lib +" <  " + g4lib+"/libname.map`";
123  FILE* pipe = gSystem->OpenPipe(command, "r");
124  char line[100];
125  while ( fgets(line, sizeof(line), pipe ) != NULL ) {
126    all_lines += line;
127  }
128} 
129
130void HandleLinkLine(const char* str, const char* what)
131{
132/// Tokenize the input string and load/unload the libraries
133/// from the list.
134/// \param str  The string output from Geant4 liblist
135/// \param what The option specifying whether we want to load ('l') or
136///             unload ('u') libraries
137
138  // Fill the libs names in the vector
139  std::vector<std::string> libs;
140  std::stringstream sstream(str);
141  unsigned int w = 0;
142  while ( ! sstream.eof() ) {
143    // Read one string
144    std::string token;
145    std::getline(sstream, token, ' ');
146
147    // Check stream status
148    if ( sstream.bad() ) break;
149
150    // Check that we got a meaningful tokenonent
151    if ( token.empty() || std::isspace(token[0]) ) continue;
152   
153    if ( token[0] != '-' ) {
154      Warning("LoadLibraryList", "Unknown element %s", token.c_str());
155      continue;
156    }
157       
158    std::string dir_or_file = token.substr(2,token.size()-2);
159    if ( token[1] == 'L' ) { 
160      std::stringstream path;
161      path << gSystem->GetDynamicPath() << ":" 
162           << dir_or_file;
163      gSystem->SetDynamicPath(path.str().c_str());
164    }
165    else if ( token[1] == 'l' ) {
166      std::stringstream ln;
167      ln << "lib" << NoSpaces(dir_or_file) << '.' << gSystem->GetSoExt();
168      std::string lib(ln.str());
169      libs.push_back(lib);
170      if ( lib.length() > w ) w = lib.length();
171    }
172    else {
173      Warning("LoadLibraryList", "Unknown option %s in", 
174              token.c_str(), str);
175      continue;
176     }
177  }
178 
179  // Process the vector with libs names and load libraries
180  size_t n = libs.size();
181  TString sWhat(what);
182  Bool_t load = sWhat.Contains("l");
183  if (!load && !sWhat.Contains("u")) {
184     std::cerr << "  Unknown load action " << what << std::endl;
185     return;
186  }
187
188  for ( size_t i = 0; i < n; ++i ) {
189    size_t idx = n - i - 1;
190   
191    // Uncomment to debug
192    // size_t m = TMath::Log10(n)+1;
193    // string say="   Loading ";
194    // if ( TString(what).Contains("u") ) say = "   Unloading ";
195    // std::cout << say         << std::setw(m) << (i+1)
196    //        << "/"            << std::setw(m) << n
197    //        << ": "           << std::setw(w) << libs[idx] // << std::endl;
198    //        << std::flush;
199   
200    int result = 0; 
201    if ( libs[idx].c_str() ) {
202       if  (load) 
203          result = gSystem->Load(libs[idx].c_str());
204       else
205          gSystem->Unload(libs[idx].c_str());
206    } 
207    // Uncomment to debug
208    // if ( TString(what).Contains("l")  )
209    //   std::cout << ( result < 0 ? " failed" : " ok" ) << "\r";
210  }
211  // std::cout << "\n   Done" << std::endl;
212}
213
214void loadg4libs() 
215{
216/// The function to unload Geant4 libraries
217
218  // CLHEP
219  gSystem->Load("libCLHEP");
220 
221  // xerces-c library if GDML is activated
222  if ( isSet("G4LIB_BUILD_GDML") ) {
223    gSystem->Load("libxerces-c");
224  } 
225
226  // Get the string with the list of libraries
227  string all_lines;
228  GetLinkLine(all_lines);
229 
230  // Load Geant4 libraries
231//   cout << "Loading Geant4 libraries ..." << endl;
232  HandleLinkLine(all_lines.c_str(),"l");
233
234  // VGM librares
235//   vgmlibs();
236 
237  // G4Root library (if available)
238  if ( isLibrary("libg4root") ) {
239    cout << "Loading g4root library ..." << endl;
240    gSystem->Load("libg4root");
241  }
242   
243  // Geant4 VMC library
244//   cout << "Loading geant4vmc library ..." << endl;
245//   gSystem->Load("libgeant4vmc");
246
247  // Geant4 VMC GUI library
248  // (if available and Root is not running in batch mode)
249//   if ( isLibrary("libgeant4vmc_gui") && ! isBatch() ) {
250//     cout << "Loading geant4vmc_gui library ... " << endl;
251//     gSystem->Load("libgeant4vmc_gui");
252//   } 
253}
254
255void unloadg4libs() 
256{
257/// The function to unload Geant4 libraries
258
259  // Get the string with the list of libraries
260  string all_lines;
261  GetLinkLine(all_lines);
262 
263  // Load Geant4 libraries
264  cout << "Unloading Geant4 libraries ..." << endl;
265  HandleLinkLine(all_lines.c_str(),"u");
266}
267
Note: See TracBrowser for help on using the repository browser.