GGS(GenericGEANT4Simulation)Software  2.7.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 "G4LogicalVolumeStore.hh"
13 #include "G4PhysicalVolumeStore.hh"
14 #include "G4SDManager.hh"
15 #include "G4SolidStore.hh"
16 #ifdef USE_GDML
17 #include "G4GDMLParser.hh"
18 #endif
19 
20 // GGS headers
25 #include "utils/GGSNameDecoder.h"
26 #include "utils/GGSSmartLog.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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
37 
38 GGSDetectorConstruction::GGSDetectorConstruction(const G4String &library, const G4String &,
39  const G4String &configDataCard, bool)
40  : _physicalWorld(NULL), _geometry(NULL), _library(library), _configDataCard(configDataCard), _gdml("") {}
41 
42 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
43 
45  : _physicalWorld(NULL), _geometry(NULL), _library(""), _configDataCard(""), _gdml(gdml) {}
46 
47 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
48 
50 
51 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
52 
53 G4VPhysicalVolume *GGSDetectorConstruction::Construct() {
54 
55  static const std::string routineName("GGSDetectorConstruction::Construct");
56 
57  // 1. Construct the geometry
58  if (_library != "") {
59  // 1.1 Load the concrete GGSVGeometryConstruction from a shared library
60  if (!(GGSGeoPluginManager::GetInstance().LoadGeoPlugin(_library))) {
61  COUT(ERROR) << "Impossible to load geometry library " << _library << ENDL;
62  throw std::runtime_error("Impossible to load geometry library");
63  }
65  if (!_geometry) {
66  COUT(ERROR) << "Can't create the geometry." << ENDL;
67  }
68  COUT(INFO) << "Construct the detector." << ENDL;
69  _geometry->SetGeoDataCard(_configDataCard);
70  _physicalWorld = _geometry->Construct();
71  // Print geometry version
72  const std::string geoVersion = _geometry->GetVersion();
73  if (geoVersion != "") {
74  COUT(INFO) << "Geometry version: " << geoVersion << ENDL;
75  } else {
76  COUT(INFO) << "No geometry version available" << ENDL;
77  }
78  // Print geometry parameters
79  _geometry->ExportParameters(); // Will throw if something goes wrong, so don't check the return value
80  auto intParams = _geometry->GetIntParameters();
81  auto boolParams = _geometry->GetBoolParameters();
82  auto realParams = _geometry->GetRealParameters();
83  auto stringParams = _geometry->GetStringParameters();
84  auto vectIntParams = _geometry->GetVectIntParameters();
85  auto vectBoolParams = _geometry->GetVectBoolParameters();
86  auto vectRealParams = _geometry->GetVectRealParameters();
87  auto vectStringParams = _geometry->GetVectStringParameters();
88  if (intParams.size() + boolParams.size() + realParams.size() + stringParams.size() > 0) {
89  COUT(INFO) << "Geometry parameters:\n";
90  for (auto &par : intParams) {
91  CCOUT(INFO) << par.first << ": " << par.second << "\n";
92  }
93  for (auto &par : boolParams) {
94  CCOUT(INFO) << par.first << ": " << (par.second ? "true" : "false") << "\n";
95  }
96  for (auto &par : realParams) {
97  CCOUT(INFO) << par.first << ": " << par.second << "\n";
98  }
99  for (auto &par : stringParams) {
100  CCOUT(INFO) << par.first << ": " << par.second << "\n";
101  }
102  for (auto &par : vectIntParams) {
103  CCOUT(INFO) << par.first << ": {";
104  for (size_t iVal = 0; iVal < par.second.size() - 1; ++iVal) {
105  std::cout << par.second[iVal] << ", ";
106  }
107  std::cout << par.second.back() << "}\n";
108  }
109  for (auto &par : vectRealParams) {
110  CCOUT(INFO) << par.first << ": {";
111  for (size_t iVal = 0; iVal < par.second.size() - 1; ++iVal) {
112  std::cout << par.second[iVal] << ", ";
113  }
114  std::cout << par.second.back() << "}\n";
115  }
116  for (auto &par : vectBoolParams) {
117  CCOUT(INFO) << par.first << ": {";
118  for (size_t iVal = 0; iVal < par.second.size() - 1; ++iVal) {
119  std::cout << par.second[iVal] << ", ";
120  }
121  std::cout << par.second.back() << "}\n";
122  }
123  for (auto &par : vectStringParams) {
124  CCOUT(INFO) << par.first << ": {";
125  for (size_t iVal = 0; iVal < par.second.size() - 1; ++iVal) {
126  std::cout << par.second[iVal] << ", ";
127  }
128  std::cout << par.second.back() << "}\n";
129  }
130  }
131  }
132 #ifdef USE_GDML
133  else {
134  // 1.2 Build the geometry from a GDML file
135  G4GDMLParser gdmlParser;
136  gdmlParser.SetOverlapCheck(false);
137  try {
138  gdmlParser.Read(_gdml, false);
139  } catch (int &ge) {
140  COUT(ERROR) << "G4GDML: " << ge << ", missing network connection? wrong schema URL?" << ENDL;
141  // G4COUT << "Try to read GDML file _WITHOUT_ schema validation" << G4ENDL;
142  // error, try without schema validation
143  // gdmlParser.ValidateSchema(false);
144  // gdmlParser.Read(_gdml, false);
145  }
146  gdmlParser.StripNamePointers();
147  _physicalWorld = gdmlParser.GetWorldVolume();
148  if (!_physicalWorld) {
149  COUT(ERROR) << "Cannot build the GMDL geometry." << ENDL;
150  throw std::runtime_error("Cannot build the GMDL geometry.");
151  }
152  }
153 #endif
154 
155  // 2. Construct the standard SDs
156  // This feature is removed from GGS, so print an error message and throw an exception.
157 
158  const G4LogicalVolumeStore *logicalVolumeStore = G4LogicalVolumeStore::GetInstance();
159  std::vector<G4LogicalVolume *>::const_iterator i;
161  for (i = logicalVolumeStore->begin(); i != logicalVolumeStore->end(); i++) {
162  std::string logVolName = (*i)->GetName();
163  auto detVolName = logVolName.substr(0, logVolName.find_first_of(' '));
164  auto spacePos = logVolName.find_last_of(' ');
165  std::string scorerName("");
166  if (spacePos != std::string::npos)
167  scorerName = logVolName.substr(spacePos + 1);
168  // check if it is a sensitive volume
169  if (nameDecoder.IsSensitive(detVolName)) {
170  COUT(WARNING) << "This version of GGS does not support the definition of sensitive volumes by name ("
171  << detVolName << ").\n";
172  CCOUT(WARNING) << "Please change the name of the volume and make it active using a datacard command." << ENDL;
173  throw std::runtime_error("Use of removed feature: definition of sensitive volume by name.");
174  }
175  }
176 
177  return _physicalWorld;
178 }
179 
180 void GGSDetectorConstruction::ConstructSDandField() { _geometry->ConstructSDandField(); }
181 
182 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
GGSDetectorConstruction(const G4String &library, const G4String &configDataCard)
Constructor.
const std::map< std::string, std::vector< bool > > & GetVectBoolParameters()
Getter method for vector-of-booleans geometry parameters.
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:105
const std::map< std::string, std::vector< std::string > > & GetVectStringParameters()
Getter method for vector-of-strings geometry parameters.
#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.
const std::map< std::string, std::vector< int > > & GetVectIntParameters()
Getter method for vector-of-integers geometry parameters.
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:100
const std::map< std::string, std::vector< double > > & GetVectRealParameters()
Getter method for vector-of-reals geometry parameters.
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.