GGS(GenericGEANT4Simulation)Software  2.6.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSDetectorConstruction.cpp
Go to the documentation of this file.
1 /*
2  * GGSDetectorConstruction.cpp
3  *
4  * Created on: 2010-09-29
5  * Authors: Emiliano Mocchiutti and Cecilia Pizzolotto
6  */
7 
10 // Geant4 headers
11 #include "G4GeometryManager.hh"
12 #include "G4PhysicalVolumeStore.hh"
13 #include "G4LogicalVolumeStore.hh"
14 #include "G4SolidStore.hh"
15 #include "G4SDManager.hh"
16 #ifdef USE_GDML
17 #include "G4GDMLParser.hh"
18 #endif
19 
20 // GGS headers
21 #include "utils/GGSSmartLog.h"
22 #include "utils/GGSNameDecoder.h"
27 
28 // C++ headers
29 #include <stdexcept>
30 
31 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
32 
33 GGSDetectorConstruction::GGSDetectorConstruction(const G4String &library, const G4String &configDataCard) :
34  _physicalWorld(NULL), _geometry(NULL), _library(library), _configDataCard(configDataCard), _gdml("") {
35 }
36 
37 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
38 
39 GGSDetectorConstruction::GGSDetectorConstruction(const G4String &library, const G4String &,
40  const G4String &configDataCard, bool) :
41  _physicalWorld(NULL), _geometry(NULL), _library(library), _configDataCard(configDataCard), _gdml("") {
42 
43 }
44 
45 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
46 
48  _physicalWorld(NULL), _geometry(NULL), _library(""), _configDataCard(""), _gdml(gdml) {
49 
50 }
51 
52 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
53 
55 }
56 
57 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
58 
59 G4VPhysicalVolume* GGSDetectorConstruction::Construct() {
60 
61  static const std::string routineName("GGSDetectorConstruction::Construct");
62 
63  // 1. Construct the geometry
64  if (_library != "") {
65  // 1.1 Load the concrete GGSVGeometryConstruction from a shared library
66  if (!(GGSGeoPluginManager::GetInstance().LoadGeoPlugin(_library))) {
67  COUT(ERROR) << "Impossible to load geometry library " << _library << ENDL;
68  throw std::runtime_error("Impossible to load geometry library");
69  }
71  if (!_geometry) {
72  COUT(ERROR) << "Can't create the geometry." << ENDL;
73 
74  }
75  COUT(INFO) << "Construct the detector." << ENDL;
76  _geometry->SetGeoDataCard(_configDataCard);
77  _physicalWorld = _geometry->Construct();
78  // Print geometry version
79  const std::string geoVersion = _geometry->GetVersion();
80  if (geoVersion != "") {
81  COUT(INFO) << "Geometry version: " << geoVersion << ENDL;
82  }
83  else {
84  COUT(INFO) << "No geometry version available" << ENDL;
85  }
86  // Print geometry parameters
87  if(!_geometry->ExportParameters()) {
88  throw(std::runtime_error("Error while exporting geometry parameters"));
89  }
90  auto intParams = _geometry->GetIntParameters();
91  auto boolParams = _geometry->GetBoolParameters();
92  auto realParams = _geometry->GetRealParameters();
93  auto stringParams = _geometry->GetStringParameters();
94  if(intParams.size() + boolParams.size() + realParams.size() + stringParams.size() > 0) {
95  COUT(INFO) << "Geometry parameters:\n";
96  for(auto &par : intParams) {
97  CCOUT(INFO) << par.first <<": " << par.second << "\n";
98  }
99  for(auto &par : boolParams) {
100  CCOUT(INFO) << par.first <<": " << (par.second ? "true" : "false") << "\n";
101  }
102  for(auto &par : realParams) {
103  CCOUT(INFO) << par.first <<": " << par.second << "\n";
104  }
105  for(auto &par : stringParams) {
106  CCOUT(INFO) << par.first <<": " << par.second << "\n";
107  }
108  }
109  }
110 #ifdef USE_GDML
111  else {
112  // 1.2 Build the geometry from a GDML file
113  G4GDMLParser gdmlParser;
114  gdmlParser.SetOverlapCheck(false);
115  try {
116  gdmlParser.Read(_gdml, false);
117  }
118  catch (int &ge) {
119  COUT(ERROR) << "G4GDML: " << ge << ", missing network connection? wrong schema URL?"
120  << ENDL;
121  //G4COUT << "Try to read GDML file _WITHOUT_ schema validation" << G4ENDL;
122  // error, try without schema validation
123  //gdmlParser.ValidateSchema(false);
124  //gdmlParser.Read(_gdml, false);
125  }
126  gdmlParser.StripNamePointers();
127  _physicalWorld = gdmlParser.GetWorldVolume();
128  if (!_physicalWorld) {
129  COUT(ERROR) << "Cannot build the GMDL geometry." << ENDL;
130  throw std::runtime_error("Cannot build the GMDL geometry.");
131  }
132  }
133 #endif
134 
135  // 2. Construct the standard SDs
136  // This feature is removed from GGS, so print an error message and throw an exception.
137 
138  const G4LogicalVolumeStore* logicalVolumeStore = G4LogicalVolumeStore::GetInstance();
139  std::vector<G4LogicalVolume*>::const_iterator i;
141  for (i = logicalVolumeStore->begin(); i != logicalVolumeStore->end(); i++) {
142  std::string logVolName = (*i)->GetName();
143  auto detVolName = logVolName.substr(0, logVolName.find_first_of(' '));
144  auto spacePos = logVolName.find_last_of(' ');
145  std::string scorerName("");
146  if (spacePos != std::string::npos)
147  scorerName = logVolName.substr(spacePos + 1);
148  // check if it is a sensitive volume
149  if (nameDecoder.IsSensitive(detVolName)) {
150  COUT(WARNING) << "This version of GGS does not support the definition of sensitive volumes by name ("
151  << detVolName << ").\n";
152  CCOUT(WARNING) << "Please change the name of the volume and make it active using a datacard command." << ENDL;
153  throw std::runtime_error("Use of removed feature: definition of sensitive volume by name.");
154  }
155  }
156 
157  return _physicalWorld;
158 
159 }
160 
161 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
GGSDetectorConstruction(const G4String &library, const G4String &configDataCard)
Constructor.
const std::map< std::string, int > & GetIntParameters()
Getter method for integer geometry parameters.
const std::map< std::string, double > & GetRealParameters()
Getter method for real geometry parameters.
static GGSGeoPluginManager & GetInstance()
Get the singleton instance.
G4VPhysicalVolume * Construct()
Override of the Construct method.
#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.
const std::map< std::string, std::string > & GetStringParameters()
Getter method for string geometry parameters.
#define CCOUT(level)
Smart log utility which prints no header at the beginning of the line.
Definition: GGSSmartLog.h:88
virtual const std::string GetVersion()
Getter method for geometry version.
const std::map< std::string, bool > & GetBoolParameters()
Getter method for boolean geometry parameters.
virtual bool ExportParameters()
Function for exporting the geometry parameters.
GGSVGeometryConstruction * GetGeoConstruction()
Returns the geometry construction object.
static GGSNameDecoder & GetInstance()
Get instance of the singleton.
bool IsSensitive(const std::string &volumeName)
Check if the logical volume is sensitive.
Class needed to decode sensitive volume names.
void SetGeoDataCard(const G4String &dataCard)
Sets the geometry configuration datacard.