GGS(GenericGEANT4Simulation)Software  2.7.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSWolowitz.cpp
1 
14 // GEANT4 headers
15 #include "G4Box.hh"
16 #include "G4Colour.hh"
17 #include "G4GeometryManager.hh"
18 #include "G4LogicalVolume.hh"
19 #include "G4LogicalVolumeStore.hh"
20 #include "G4Material.hh"
21 #include "G4PVPlacement.hh"
22 #include "G4PVReplica.hh"
23 #include "G4PhysicalVolumeStore.hh"
24 #include "G4SolidStore.hh"
25 #include "G4VisAttributes.hh"
26 #ifdef USE_GDML
27 #include "G4GDMLParser.hh"
28 #endif
29 #include "G4RunManager.hh"
30 #include "G4TransportationManager.hh"
31 
32 // VGM headers
33 #ifdef USE_VGM
34 #include "Geant4GM/volumes/Factory.h"
35 #include "RootGM/volumes/Factory.h"
36 #include "TGeoManager.h"
37 #endif
38 
39 // GGS headers
40 #include "GGSVersion.h" //Automatically generated by the CMake configuration system
43 #include "utils/GGSInputParser.h"
44 #include "utils/GGSSmartLog.h"
45 
46 namespace GGSWolowitz {
47 G4String outputFile = "GGSGeometry";
48 G4String geoDataCard = "";
49 G4String geometryFile = "";
50 #ifdef USE_GDML
51 G4String outputType = "gdml";
52 #else
53 G4String outputType = "vgm";
54 #endif
55 G4String inputFile = "";
56 G4String schemaLocation = "http://service-spi.web.cern.ch/service-spi/app/releases/GDML/schema/gdml.xsd";
57 G4String detectorFlag = "";
58 } // namespace GGSWolowitz
59 
60 void usage() {
61  std::cout << "This program is used to convert GGS detector geometry into other geometry formats.\n";
62  std::cout << "Built with support for "
63 #ifdef USE_GDML
64  "GDML"
65 #ifdef USE_VGM
66  " and "
67 #endif
68 #endif
69 #ifdef USE_VGM
70  "VGM"
71 #endif
72  ".\n";
73  std::cout << "\nUsage: GGSWolowitz [options] \n";
74  std::cout << "\nOptions:\n";
75  std::cout << " -o, --output-file FILE output filename [default: output.gdml]\n";
76  std::cout << " -g, --geometry FILE full path and name of the geometry .so file\n";
77  std::cout
78  << " -gd, --geo-data-card FILE data card filename for geometry configuration [default: not set]\n";
79  std::cout << " -t, --output-type TYPE output type (possible values: "
80 #ifdef USE_GDML
81  "gdml, "
82 #endif
83 #ifdef USE_VGM
84  "vgm, "
85 #endif
86  "default: "
87  << GGSWolowitz::outputType << ")\n";
88  std::cout << " -i, --input-file FILE file containing input arguments [default: not set]\n";
89  std::cout << " -u, --gdml-schema-location url of schema location [default: "
90  "http://service-spi.web.cern.ch/service-spi/app/releases/GDML/schema/gdml.xsd]\n";
91  std::cout << " -v, --verbose be verbose [default: false]\n";
92  std::cout << " -V, --version displays software version and exit\n";
93  std::cout << " -h, --help display this help and exit\n";
94  std::cout << "\nFor bug reporting instructions, please see for example:\n";
95  std::cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
96  std::cout << "Report bugs to: mori@fi.infn.it" << std::endl;
97 }
98 
99 void version() {
100  std::cout << "GGSWolowitz geometry software\n";
101  std::cout << "Using GGS " << GGSVERSION << std::endl;
102 }
103 
104 using namespace GGSWolowitz;
105 
106 int HandleInputPar(int argc, const char **argv) {
107  static const std::string routineName("HandleInputPar");
108  // Set verbose level to print warnings about unrecognized options
109  GGSSmartLog::verboseLevel = GGSSmartLog::WARNING;
110  int vLevel = GGSSmartLog::INFO;
111  if (argc > 1) {
112  int i = 1;
113  while (i < argc) {
114  if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
115  usage();
116  return 1;
117  }
118  // -----------------------------------------------------//
119  if (!strcmp(argv[i], "-V") || !strcmp(argv[i], "--version")) {
120  version();
121  return 1;
122  }
123  // -----------------------------------------------------//
124  else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose")) {
125  std::string vLevStr(argv[++i]);
126  if (vLevStr == "error")
127  vLevel = GGSSmartLog::ERROR;
128  else if (vLevStr == "warning")
129  vLevel = GGSSmartLog::WARNING;
130  else if (vLevStr == "normal" || vLevStr == "info")
131  vLevel = GGSSmartLog::INFO;
132  else if (vLevStr == "debug")
133  vLevel = GGSSmartLog::DEBUG;
134  else if (vLevStr == "deepdeb")
135  vLevel = GGSSmartLog::DEEPDEB;
136  else {
137  COUT(WARNING) << "Unrecognized verbosity level: " << vLevStr << ". Ignoring." << ENDL;
138  }
139  }
140  // -----------------------------------------------------//
141  else if (!strcmp(argv[i], "-o") || !strcmp(argv[i], "--output-file")) {
142  if (++i >= argc) {
143  usage();
144  return 1;
145  }
146  outputFile = argv[i];
147  }
148  // -----------------------------------------------------//
149  else if (!strcmp(argv[i], "-t") || !strcmp(argv[i], "--output-type")) {
150  if (++i >= argc) {
151  usage();
152  return 1;
153  }
154  if (strcmp(argv[i], "gdml") && strcmp(argv[i], "vgm")) {
155  COUT(ERROR) << "Unknown output type: " << argv[i] << ENDL;
156  return 1;
157  }
158 #ifndef USE_GDML
159  if (!strcmp(argv[i], "gdml")) {
160  COUT(ERROR) << "Converting to gdml requires Geant4 version compiled with Xerces-C" << ENDL;
161  return 1;
162  }
163 #endif
164 #ifndef USE_VGM
165  if (!strcmp(argv[i], "vgm")) {
166  COUT(ERROR) << "Converting to vgm requires Virtual Geometry Model" << ENDL;
167  return 1;
168  }
169 #endif
170 
171  outputType = argv[i];
172  }
173  // -----------------------------------------------------//
174  else if (!strcmp(argv[i], "-g") || !strcmp(argv[i], "--geometry")) {
175  if (++i >= argc) {
176  usage();
177  return 1;
178  }
179  geometryFile = argv[i];
180  }
181  // -----------------------------------------------------//
182  else if (!strcmp(argv[i], "-gd") || !strcmp(argv[i], "--geo-data-card")) {
183  if (++i >= argc) {
184  usage();
185  return 1;
186  }
187  geoDataCard = argv[i];
188  }
189  // -----------------------------------------------------//
190  else if (!strcmp(argv[i], "-i") || !strcmp(argv[i], "--input-file")) {
191  if (++i >= argc) {
192  usage();
193  return 1;
194  }
195  inputFile = argv[i];
196  }
197  // -----------------------------------------------------//
198  else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--gdml-schema-location")) {
199  if (++i >= argc) {
200  usage();
201  return 1;
202  }
203  schemaLocation = argv[i];
204  }
205  // -----------------------------------------------------//
206  else {
207  COUT(WARNING) << "Unidentified input argument \"" << argv[i] << "\". Ignoring." << ENDL;
208  }
209  i++;
210  }
211  }
212 
213  GGSSmartLog::verboseLevel = vLevel;
214  return 0;
215 }
216 
217 int main(int argc, const char **argv) {
218  static const std::string routineName("GGSWolowitz");
219 
220  if (HandleInputPar(argc, argv))
221  return 0;
222 
223  version();
224 
225  if (strcmp(inputFile.c_str(), "")) {
226  int nPar = 0;
227  std::string mych[1000];
228  GGSInputParser::GetInstance().ReadInput(inputFile, &nPar, (std::string *)mych);
229  if (nPar > 1) {
230  const char **fileArgv;
231  fileArgv = new const char *[nPar];
232  int i = 0;
233  while (i < nPar) {
234  fileArgv[i] = (mych[i]).c_str();
235  i++;
236  }
237  if (HandleInputPar(nPar, fileArgv))
238  return 0;
239  }
240  }
241 
242  COUT(DEBUG) << "Start run manager " << ENDL;
243  G4RunManager *runManager = new G4RunManager();
244 
245  if (!(GGSGeoPluginManager::GetInstance().LoadGeoPlugin(geometryFile))) {
246  COUT(ERROR) << "Impossible to load geometry library " << geometryFile << ". Exit." << ENDL;
247  delete runManager;
248  return 1;
249  }
250 
252  if (!geometry) {
253  COUT(ERROR) << "Can't create the geometry. Exit." << ENDL;
254  delete runManager;
255  return 1;
256  }
257  // runManager->SetUserInitialization(geometry);
258 
259  COUT(DEBUG) << "Construct the geometry." << ENDL;
260 
261  geometry->SetGeoDataCard(geoDataCard);
262  G4VPhysicalVolume *physVolume = geometry->Construct();
263 
264 #ifdef USE_GDML
265  if (GGSWolowitz::outputType == "gdml") {
266  COUT(DEBUG) << "Write the GDML file." << ENDL;
267  G4GDMLParser parser;
268  parser.SetAddPointerToName(true);
269  if (outputFile.length() < 6 || outputFile.substr(outputFile.length() - 5) != ".gdml") {
270  COUT(DEBUG) << "Append .gdml extension to output file name" << ENDL;
271  outputFile.append(".gdml");
272  }
273  parser.Write(outputFile, physVolume, true, schemaLocation);
274  }
275 #endif
276 #ifdef USE_VGM
277  if (GGSWolowitz::outputType == "vgm") {
278  COUT(DEBUG) << "Write the VGM file." << ENDL;
279  Geant4GM::Factory *g4Factory = new Geant4GM::Factory();
280  if (GGSSmartLog::verboseLevel >= GGSSmartLog::DEBUG)
281  g4Factory->SetDebug(1);
282  g4Factory->Import(physVolume);
283 
284  // Export VGM geometry to Root
285  RootGM::Factory *rtFactory = new RootGM::Factory();
286  if (GGSSmartLog::verboseLevel >= GGSSmartLog::DEBUG)
287  rtFactory->SetDebug(1);
288  g4Factory->Export(rtFactory);
289  gGeoManager->CloseGeometry();
290  if (outputFile.length() < 6 || outputFile.substr(outputFile.length() - 5) != ".root") {
291  COUT(DEBUG) << "Append .root extension to output file name" << ENDL;
292  outputFile.append(".root");
293  }
294  gGeoManager->Export(outputFile.data());
295  }
296 #endif
297 
298  return 0;
299 }
static GGSInputParser & GetInstance()
Get instance of the singleton.
Abstract class needed to load GGS geometry.
static GGSGeoPluginManager & GetInstance()
Get the singleton instance.
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76
virtual G4VPhysicalVolume * Construct()=0
Construct the detector - virtual method.
GGSVGeometryConstruction * GetGeoConstruction()
Returns the geometry construction object.
void ReadInput(std::string fileName, int *argc, std::string *argv)
Reads configuration file.
void SetGeoDataCard(const G4String &dataCard)
Sets the geometry configuration datacard.