Classes | Functions
simplifyGDML.cc File Reference

Reprocesses a GDML file via GEANT4. More...

#include "Geant4/G4GDMLParser.hh"
#include <getopt.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <cstdio>
#include <cstdlib>

Go to the source code of this file.

Classes

struct  ConfigurationParameters
 
struct  ConfigurationParser
 
struct  ConfigurationParser::opt
 

Functions

std::string addNameSuffix (std::string name, std::string suffix)
 
bool exists (std::string path)
 
G4VPhysicalVolume * readWorldVolume (G4GDMLParser &parser, ConfigurationParameters const &params)
 
int writeWorld (G4GDMLParser &parser, ConfigurationParameters const &params)
 
int main (int argc, char **argv)
 

Detailed Description

Reprocesses a GDML file via GEANT4.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.fnal..nosp@m.gov)
Date
June 13, 2017

Run with --help argument for usage instructions.

This program needs to be linked to:

Example of build command in UPS environment:

g++ -Wall -pedantic -std=c++14 \
-I"${GEANT4_FQ_DIR}/include" -I"$CLHEP_INC" -I"$XERCES_C_INC" \
-L"${GEANT4_FQ_DIR}/lib64" -lG4persistency -lG4geometry \
-L"$XERCES_C_LIB" -lxerces-c \
-o simplifyGDML.exe simplifyGDML.cc

Note that this program is a glorified 3-line code: parse XML file, get the world volume, write it.

Definition in file simplifyGDML.cc.

Function Documentation

std::string addNameSuffix ( std::string  name,
std::string  suffix 
)

Definition at line 283 of file simplifyGDML.cc.

283  {
284  auto const iExt = name.find(".gdml");
285  if (iExt == std::string::npos) name.append(suffix);
286  else name.insert(iExt, suffix);
287  return name;
288 } // addNameSuffix()
static QCString name
Definition: declinfo.cpp:673
int find(char c, int index=0, bool cs=TRUE) const
Definition: qcstring.cpp:41
QCString & insert(uint index, const char *s)
Definition: qcstring.cpp:355
QCString & append(const char *s)
Definition: qcstring.cpp:383
bool exists ( std::string  path)

Definition at line 290 of file simplifyGDML.cc.

290  {
291  return access(path.c_str(), F_OK);
292 } // exists()
def access(path, mode)
int main ( int  argc,
char **  argv 
)

Definition at line 345 of file simplifyGDML.cc.

345  {
346 
347  //
348  // argument parsing
349  //
351 
352  auto parseRes = ConfigurationParser::parse(params, argc, argv);
353 
354  if (params.destPath.empty() && !params.dontWrite)
355  params.destPath = addNameSuffix(params.sourcePath, "-simplified");
356 
357  if (params.debugLevel > 0) params.print();
358 
359  // print usage instructions before exiting
360  if (params.help) {
362  return 0;
363  }
364  if (parseRes != ConfigurationParser::Success) return (int) parseRes;
365 
366  //
367  // the magic
368  //
369  G4GDMLParser parser;
370 
371  std::cout << std::string(80, '-')
372  << "\nReading '" << params.sourcePath << "'..." << std::endl;
373  parser.Read(params.sourcePath, params.validate);
374 
375  std::cout << std::string(80, '-')
376  << "\nFetching the world volume for setup '" << params.setupName << "'..."
377  << std::endl;
378  // for c2: worldVolume is unused here
379  //decltype(auto) worldVolume = parser.GetWorldVolume(params.setupName);
380  parser.GetWorldVolume(params.setupName);
381 
382  if (params.dontWrite) {
383  if (!params.destPath.empty()) {
384  std::cerr << "Output path ignored since we don't write anything."
385  << std::endl;
386  }
387  }
388  else {
389  int res = writeWorld(parser, params);
390  if (res != 0) return res;
391  }
392 
393  //
394  // the missing error code
395  //
396  return 0;
397 } // main()
std::string setupName
Name of the chosen setup in the GDML source.
Definition: simplifyGDML.cc:53
std::string string
Definition: nybbler.cc:12
bool help
Print usage instructions and exit.
Definition: simplifyGDML.cc:61
std::string destPath
GDML output file name.
Definition: simplifyGDML.cc:50
bool validate
Ask Geant4 to validate the source.
Definition: simplifyGDML.cc:58
std::string sourcePath
GDML input file name.
Definition: simplifyGDML.cc:49
static void printHelp(int exitCode=0, const char *progName="simplifyGDML")
static error parse(ConfigurationParameters &params, unsigned int argc, char **argv)
std::string addNameSuffix(std::string name, std::string suffix)
bool dontWrite
Do not write the output file.
Definition: simplifyGDML.cc:60
unsigned int debugLevel
Debug level.
Definition: simplifyGDML.cc:62
int writeWorld(G4GDMLParser &parser, ConfigurationParameters const &params)
QTextStream & endl(QTextStream &s)
G4VPhysicalVolume* readWorldVolume ( G4GDMLParser &  parser,
ConfigurationParameters const &  params 
)

Definition at line 296 of file simplifyGDML.cc.

297 {
298  decltype(auto) worldVolume = parser.GetWorldVolume(params.setupName);
299  if (!worldVolume) {
300  std::cerr << "Failed to find the world volume for setup '"
301  << params.setupName << "'" << std::endl;
302  }
303  return worldVolume;
304 } // readWorldVolume()
if(!yymsg) yymsg
QTextStream & endl(QTextStream &s)
int writeWorld ( G4GDMLParser &  parser,
ConfigurationParameters const &  params 
)

Definition at line 306 of file simplifyGDML.cc.

306  {
307 
308  std::cout << std::string(80, '-')
309  << "\nFetching the world volume for setup '" << params.setupName << "'..."
310  << std::endl;
311  decltype(auto) worldVolume = readWorldVolume(parser, params);
312  if (!worldVolume) {
313  std::cerr << "Failed to find the world volume for setup '"
314  << params.setupName << "'" << std::endl;
315  return 1;
316  }
317 
318  if (exists(params.destPath)) {
319  if (params.overwrite) {
320  std::cout << "Overwriting the destination file..." << std::endl;
321  std::remove(params.destPath.c_str());
322  }
323  else {
324  std::cerr << "Destination file '" << params.destPath
325  << "' already exists." << std::endl;
326  return 2;
327  }
328  }
329 
330  std::cout << std::string(80, '-')
331  << "\nWriting '" << params.destPath << "'..." << std::endl;
332  parser.Write
333  (params.destPath, worldVolume, false /* do not add references to names*/);
334 
335  std::cout << std::string(80, '-')
336  << "\n'" << params.sourcePath << "' => [" << params.setupName << "] => '"
337  << params.destPath << "': done."
338  << std::endl;
339 
340  return 0;
341 } // writeVolume()
std::string string
Definition: nybbler.cc:12
bool exists(std::string path)
G4VPhysicalVolume * readWorldVolume(G4GDMLParser &parser, ConfigurationParameters const &params)
if(!yymsg) yymsg
QTextStream & endl(QTextStream &s)