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