29 #include "Geant4/G4GDMLParser.hh" 70 = G4GDML_DEFAULT_SCHEMALOCATION;
74 std::cout <<
"Configuration:" 76 "\n output file: '" <<
destPath <<
"'" 79 "\n validate: " << std::boolalpha <<
validate <<
80 "\n only read: " << std::boolalpha <<
dontWrite <<
81 "\n overwrite: " << std::boolalpha <<
overwrite <<
82 "\n print help: " << std::boolalpha <<
help <<
92 static const unsigned int DefaultDebugLevel = 1;
106 static void printHelp
107 (
int exitCode = 0,
const char* progName =
"simplifyGDML");
116 (
int exitCode ,
const char* progName )
119 "Asks GEANT4 to process and rewrite a GDML file." 120 " This effectively simplifies the complexity of constructs in the GDML" 123 "\nUsage: " << progName <<
" [options] [--] inputPath [outputPath]" 125 "\nThe output file will contain the world volume of the specified setup." 126 "\nIt must be different from the input file:" 127 " GEANT4 will refuse to overwrite." 129 "\nNOTE: the path to the GDML schema in the output may need to be fixed by" 131 "\nTo allow validation, the GDML schema must be present as described in the" 132 " header of the GDML file." 133 "\nLArSoft does not necessarily distributes GDML schema, so these lines" 134 " are aimed to load them from network:" 136 "\n<gdml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" 137 "\n xsi:noNamespaceSchemaLocation=\"http://service-spi.web.cern.ch/service-spi/app/releases/GDML/schema/gdml.xsd\"" 142 "\n ask Geant4 to validated the GDML while reading (validation output" 143 "\n will be on screen, with no effect to the rest of the program)" 145 "\n not implemented yet" 147 "\n only read (and validate as requested), do not write a new GDML file" 148 "\n--setup=SETUPNAME , -s SETUPNAME" 149 "\n name of path or URL to the schema to use for writing." 151 <<
"' setup is chosen." 152 "\n--schema=SCHEMAURL , -S SCHEMAURL" 153 "\n path or URL to the schema to use for writing." 155 <<
"' schema is used." 157 "\n print these usage instructions and exit" 160 if (exitCode >= 0) std::exit(exitCode);
165 static constexpr
const char Validate =
'v';
166 static constexpr
const char Schema =
'S';
167 static constexpr
const char Setup =
's';
168 static constexpr
const char NoWrite =
'r';
169 static constexpr
const char Overwrite =
'f';
170 static constexpr
const char Debug =
'd';
171 static constexpr
const char Help =
'h';
177 static const option longopts[] = {
178 {
"validate", no_argument, NULL, opt::Validate },
179 {
"schema", required_argument, NULL, opt::Schema },
180 {
"setup", required_argument, NULL, opt::Setup },
181 {
"overwrite", no_argument, NULL, opt::Overwrite },
182 {
"nowrite", no_argument, NULL, opt::NoWrite },
183 {
"debug", optional_argument, NULL, opt::Debug },
184 {
"help", no_argument, NULL, opt::Help },
192 for (
auto const& longopt: longopts) {
193 if (!longopt.name)
break;
194 if (longopt.val == 0)
continue;
195 shortopts += (char) longopt.val;
196 if (longopt.has_arg != no_argument) shortopts +=
':';
197 if (longopt.has_arg == optional_argument) shortopts +=
':';
202 error res = error::Success;
205 bool forgiving =
false;
207 ((ch = getopt_long(argc, argv, shortopts.c_str(), longopts, NULL)) != -1)
227 std::istringstream sstr(optarg);
230 std::cerr <<
"Invalid debug level: '" << optarg <<
"'" <<
std::endl;
241 <<
"Invalid option: '" << argv[optind-1] <<
"'" <<
std::endl;
243 res = error::InvalidOption;
252 std::cerr <<
"Internal error: option '" << ch <<
"' not supported yet." 261 if (optind >= (
int) argc) {
262 if (!forgiving) std::cerr <<
"Source file name is required!" <<
std::endl;
263 return error::MissingArgument;
266 if (optind < (
int) argc) params.
destPath = argv[optind++];
268 if (optind < (
int) argc) {
270 std::cerr <<
"Spurious arguments: '" << argv[optind] <<
"'";
271 if (optind + 1 < (
int) argc)
272 std::cerr <<
" and " << (argc - optind - 1) <<
" more";
275 return error::ExtraArguments;
284 auto const iExt = name.find(
".gdml");
285 if (iExt == std::string::npos) name.append(suffix);
286 else name.insert(iExt, suffix);
291 return access(path.c_str(), F_OK);
298 decltype(
auto) worldVolume = parser.GetWorldVolume(params.
setupName);
300 std::cerr <<
"Failed to find the world volume for setup '" 309 <<
"\nFetching the world volume for setup '" << params.
setupName <<
"'..." 313 std::cerr <<
"Failed to find the world volume for setup '" 320 std::cout <<
"Overwriting the destination file..." <<
std::endl;
324 std::cerr <<
"Destination file '" << params.
destPath 333 (params.
destPath, worldVolume,
false );
345 int main(
int argc,
char** argv) {
376 <<
"\nFetching the world volume for setup '" << params.
setupName <<
"'..." 384 std::cerr <<
"Output path ignored since we don't write anything." 390 if (res != 0)
return res;
std::string setupName
Name of the chosen setup in the GDML source.
static std::string const DefaultSetupName
int main(int argc, char **argv)
bool overwrite
Overwrite the output file if already present.
bool exists(std::string path)
bool help
Print usage instructions and exit.
std::string destPath
GDML output file name.
bool validate
Ask Geant4 to validate the source.
static std::string const DefaultSchemaURL
std::string sourcePath
GDML input file name.
static void printHelp(int exitCode=0, const char *progName="simplifyGDML")
static error parse(ConfigurationParameters ¶ms, unsigned int argc, char **argv)
G4VPhysicalVolume * readWorldVolume(G4GDMLParser &parser, ConfigurationParameters const ¶ms)
std::string addNameSuffix(std::string name, std::string suffix)
std::string schemaPath
URL of the schema used while writing GDML.
bool dontWrite
Do not write the output file.
unsigned int debugLevel
Debug level.
static const unsigned int StartingDebugLevel
int writeWorld(G4GDMLParser &parser, ConfigurationParameters const ¶ms)
QTextStream & endl(QTextStream &s)