GGS(GenericGEANT4Simulation)Software  2.6.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSWolowitz.cpp
1 
14 // GEANT4 headers
15 #include "G4Material.hh"
16 #include "G4Box.hh"
17 #include "G4LogicalVolume.hh"
18 #include "G4PVPlacement.hh"
19 #include "G4PVReplica.hh"
20 #include "G4GeometryManager.hh"
21 #include "G4PhysicalVolumeStore.hh"
22 #include "G4LogicalVolumeStore.hh"
23 #include "G4SolidStore.hh"
24 #include "G4VisAttributes.hh"
25 #include "G4Colour.hh"
26 #ifdef USE_GDML
27 #include "G4GDMLParser.hh"
28 #endif
29 #include "G4TransportationManager.hh"
30 #include "G4RunManager.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
41 #include "utils/GGSSmartLog.h"
42 #include "utils/GGSInputParser.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 }
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 << " -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_GDML
80  "gdml, "
81 #endif
82 #ifdef USE_VGM
83  "vgm, "
84 #endif
85  "default: " << GGSWolowitz::outputType << ")\n";
86  std::cout << " -i, --input-file FILE file containing input arguments [default: not set]\n";
87  std::cout
88  << " -u, --gdml-schema-location url of schema location [default: http://service-spi.web.cern.ch/service-spi/app/releases/GDML/schema/gdml.xsd]\n";
89  std::cout << " -v, --verbose be verbose [default: false]\n";
90  std::cout << " -V, --version displays software version and exit\n";
91  std::cout << " -h, --help display this help and exit\n";
92  std::cout << "\nFor bug reporting instructions, please see for example:\n";
93  std::cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
94  std::cout << "Report bugs to: mori@fi.infn.it" << std::endl;
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  COUT(WARNING) << "Unrecognized verbosity level: " << vLevStr << ". Ignoring." << ENDL;
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],"gdml") && strcmp(argv[i],"vgm")) {
153  COUT(ERROR) << "Unknown output type: " << argv[i] << ENDL;
154  return 1;
155  }
156 #ifndef USE_GDML
157  if (!strcmp(argv[i],"gdml")) {
158  COUT(ERROR) << "Converting to gdml requires Geant4 version compiled with Xerces-C" << ENDL;
159  return 1;
160  }
161 #endif
162 #ifndef USE_VGM
163  if (!strcmp(argv[i],"vgm")) {
164  COUT(ERROR) << "Converting to vgm requires Virtual Geometry Model" <<ENDL;
165  return 1;
166  }
167 #endif
168 
169  outputType = argv[i];
170  }
171  // -----------------------------------------------------//
172  else if (!strcmp(argv[i], "-g") || !strcmp(argv[i], "--geometry")) {
173  if (++i >= argc) {
174  usage();
175  return 1;
176  }
177  geometryFile = argv[i];
178  }
179  // -----------------------------------------------------//
180  else if (!strcmp(argv[i], "-gd") || !strcmp(argv[i], "--geo-data-card")) {
181  if (++i >= argc) {
182  usage();
183  return 1;
184  }
185  geoDataCard = argv[i];
186  }
187  // -----------------------------------------------------//
188  else if (!strcmp(argv[i], "-i") || !strcmp(argv[i], "--input-file")) {
189  if (++i >= argc) {
190  usage();
191  return 1;
192  }
193  inputFile = argv[i];
194  }
195  // -----------------------------------------------------//
196  else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--gdml-schema-location")) {
197  if (++i >= argc) {
198  usage();
199  return 1;
200  }
201  schemaLocation = argv[i];
202  }
203  // -----------------------------------------------------//
204  else {
205  COUT(WARNING) << "Unidentified input argument \"" << argv[i] << "\". Ignoring." << ENDL;
206  }
207  i++;
208  }
209  }
210 
211  GGSSmartLog::verboseLevel = vLevel;
212  return 0;
213 }
214 
215 int main(int argc, const char** argv) {
216  static const std::string routineName("GGSWolowitz");
217 
218  if (HandleInputPar(argc, argv))
219  return 0;
220 
221  version();
222 
223  if (strcmp(inputFile.c_str(), "")) {
224  int nPar = 0;
225  std::string mych[1000];
226  GGSInputParser::GetInstance().ReadInput(inputFile, &nPar, (std::string*) mych);
227  if (nPar > 1) {
228  const char **fileArgv;
229  fileArgv = new const char*[nPar];
230  int i = 0;
231  while (i < nPar) {
232  fileArgv[i] = (mych[i]).c_str();
233  i++;
234  }
235  if (HandleInputPar(nPar, fileArgv))
236  return 0;
237  }
238  }
239 
240  COUT(DEBUG) << "Start run manager " << ENDL;
241  G4RunManager* runManager = new G4RunManager();
242 
243  if (!(GGSGeoPluginManager::GetInstance().LoadGeoPlugin(geometryFile))) {
244  COUT(ERROR) << "Impossible to load geometry library " << geometryFile << ". Exit." << ENDL;
245  delete runManager;
246  return 1;
247  }
248 
250  if (!geometry) {
251  COUT(ERROR) << "Can't create the geometry. Exit." << ENDL;
252  delete runManager;
253  return 1;
254  }
255  //runManager->SetUserInitialization(geometry);
256 
257  COUT(DEBUG) << "Construct the geometry." << ENDL;
258 
259  geometry->SetGeoDataCard(geoDataCard);
260  G4VPhysicalVolume *physVolume = geometry->Construct();
261 
262 #ifdef USE_GDML
263  if (GGSWolowitz::outputType == "gdml") {
264  COUT(DEBUG) << "Write the GDML file." << ENDL;
265  G4GDMLParser parser;
266  parser.SetAddPointerToName(true);
267  if (outputFile.length() < 6 || outputFile.substr(outputFile.length()-5) != ".gdml") {
268  COUT(DEBUG) << "Append .gdml extension to output file name" << ENDL;
269  outputFile.append(".gdml");
270  }
271  parser.Write(outputFile, physVolume, true, schemaLocation);
272  }
273 #endif
274 #ifdef USE_VGM
275  if (GGSWolowitz::outputType == "vgm") {
276  COUT(DEBUG) << "Write the VGM file." << ENDL;
277  Geant4GM::Factory* g4Factory = new Geant4GM::Factory();
278  if(GGSSmartLog::verboseLevel >= GGSSmartLog::DEBUG)
279  g4Factory->SetDebug(1);
280  g4Factory->Import(physVolume);
281 
282  // Export VGM geometry to Root
283  RootGM::Factory* rtFactory = new RootGM::Factory();
284  if(GGSSmartLog::verboseLevel >= GGSSmartLog::DEBUG)
285  rtFactory->SetDebug(1);
286  g4Factory->Export(rtFactory);
287  gGeoManager->CloseGeometry();
288  if (outputFile.length() < 6 || outputFile.substr(outputFile.length()-5) != ".root") {
289  COUT(DEBUG) << "Append .root extension to output file name" << ENDL;
290  outputFile.append(".root");
291  }
292  gGeoManager->Export(outputFile.data());
293  }
294 #endif
295 
296  return 0;
297 }
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:93
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:66
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.