gsystem
Loading...
Searching...
No Matches
gsystem.cc
Go to the documentation of this file.
1
8// gsystem
9#include "gsystem.h"
10#include "gsystemConventions.h"
11#include "gsystem_options.h"
12
13
14// explicit copy constructor
16 : GBase(other.log),
17 dbhost(other.dbhost),
18 name(other.name),
19 path(other.path),
20 factoryName(other.factoryName),
21 experiment(other.experiment),
22 runno(other.runno),
23 variation(other.variation),
24 annotations(other.annotations) {
25 log->debug(CONSTRUCTOR, "GSystem");
26
27 // Deep copy the gvolumesMap
28 for (const auto& [key, volumePtr] : other.gvolumesMap) {
29 if (volumePtr) {
30 gvolumesMap[key] = std::make_shared<GVolume>(*volumePtr); // do not clone, it creates uniqueptr
31 }
32 }
33
34 // Deep copy the gmaterialsMap
35 for (const auto& [key, materialPtr] : other.gmaterialsMap) {
36 if (materialPtr) {
37 gmaterialsMap[key] = std::make_shared<GMaterial>(*materialPtr);
38 }
39 }
40
41 // Deep copy the gmirrorsMap
42 for (const auto& [key, mirrorPtr] : other.gmirrorsMap) {
43 if (mirrorPtr) {
44 gmirrorsMap[key] = std::make_shared<GMirror>(*mirrorPtr);
45 }
46 }
47}
48
49
50// See gsystem.h for API docs.
51GSystem::GSystem(const std::shared_ptr<GOptions>& gopts,
52 const std::string& dbh,
53 const std::string& sname,
54 const std::string& factory,
55 const std::string& exp,
56 int run,
57 const std::string& variation,
58 const std::string& notes)
59 : GBase(gopts, GSYSTEM_LOGGER),
60 dbhost(dbh),
61 name(gutilities::getFileFromPath(sname)),
62 path(gutilities::getDirFromPath(sname)), // Initialize 'path' directly
63 factoryName(factory),
64 experiment(exp),
65 runno(run),
66 variation(variation),
67 annotations(notes) // Use 'notes' directly
68{
69 // If the provided name does not include a directory, set the path to empty.
70 if (name == path || factoryName == GSYSTEMSQLITETFACTORYLABEL) {
71 path = "";
72 log->info(1, "Instantiating GSystem <", name, "> using factory <", factoryName, ">");
73 }
74 else {
75 log->info(1, "Instantiating GSystem <", name, "> using path <", path, "> ",
76 "and factory <", factoryName, ">");
77 }
78}
79
80// See gsystem.h for API docs.
81std::shared_ptr<GSystem> GSystem::descriptorClone(const std::shared_ptr<GOptions>& gopts) const {
82 return std::make_shared<GSystem>(gopts, dbhost, getFilePath(), factoryName, experiment, runno, variation,
83 annotations);
84}
85
86
87// MARK: GVOLUMES
88
89// See gsystem.h for API docs.
90void GSystem::addGVolume(std::vector<std::string> pars) {
91 // Append variation and run number to parameters so the GVolume stores provenance and filtering context.
92 pars.emplace_back(variation);
93 pars.emplace_back(std::to_string(runno));
94
95 std::string volume_name = pars[0];
96
97 // Check if the volume already exists in the map.
98 if (gvolumesMap.find(volume_name) == gvolumesMap.end()) {
99 // Create and add GVolume to the map.
100 gvolumesMap[volume_name] = std::make_shared<GVolume>(log, name, pars);
101 log->info(1, "Adding gVolume <" + volume_name + "> to gvolumesMap.");
102 log->info(2, *gvolumesMap[volume_name]);
103 }
104 else {
106 "gVolume <" + volume_name + "> already exists in gvolumesMap.");
107 }
108}
109
110
111// See gsystem.h for API docs.
112void GSystem::addROOTVolume(const std::string& rootVolumeDefinition) {
113 log->warning("Adding ROOT volume using <" + rootVolumeDefinition + "> to gvolumesMap.");
114 // ROOTWORLDGVOLUMENAME is assumed to be defined in gsystemConventions.h.
115 gvolumesMap[ROOTWORLDGVOLUMENAME] = std::make_shared<GVolume>(rootVolumeDefinition, log);
116}
117
118// add volume from a file (CAD or GDML factories)
119// includes factory and filename in the definitions
120#include <filesystem>
121#include <utility>
122
123namespace fs = std::filesystem;
124
125// See gsystem.h for API docs.
126void GSystem::addVolumeFromFile(const std::string& importType, const std::string& filename) {
127 std::vector<std::string> pars;
128
129 // Extract filename (without the path) and split by delimiter.
130 std::vector<std::string> gvpaths = gutilities::getStringVectorFromStringWithDelimiter(
131 fs::path(filename).filename().string(),
132 ".");
133
134 // Use the first item as the volume name.
135 const std::string& gvolumeName = gvpaths.front();
136
137 // Order is defined in gvolume.cc:
138 // 01: name, 03: type, 04: parameters, 05: material, 02: mother, etc.
139 pars.emplace_back(gvolumeName); // 01 name
140 pars.emplace_back(importType); // 03 type
141 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 04 parameters
142 pars.emplace_back("G4_AIR"); // 05 material: default is air
143 pars.emplace_back(ROOTWORLDGVOLUMENAME); // 02 mother: default is ROOTWORLDGVOLUMENAME
144 pars.emplace_back("0*cm, 0*cm, 0*cm"); // 06 position
145 pars.emplace_back("0*deg, 0*deg, 0*deg"); // 07 rotation
146 pars.emplace_back(DEFAULTG4PLACEMENTTYPE); // 08 Geant4 placement constructor convention
147 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 09 electromagnetic field
148 pars.emplace_back("1"); // 10 visible
149 pars.emplace_back("1"); // 11 style
150 pars.emplace_back("999999"); // 12 color
151 pars.emplace_back("1"); // 13 opacity
152 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 14 digitization
153 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 15 gidentity
154 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 16 copyOf
155 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 17 solidsOpr
156 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 18 mirror
157 pars.emplace_back("1"); // 19 exist flag
158 pars.emplace_back(filename); // 20 description: contains full path
159
160 addGVolume(pars);
161}
162
163
164// See gsystem.h for API docs.
165GVolume* GSystem::getGVolume(const std::string& volumeName) const {
166 auto it = gvolumesMap.find(volumeName);
167 if (it != gvolumesMap.end())
168 return it->second.get();
169 return nullptr;
170}
171
172
173// MARK: GMATERIALS
174
175// See gsystem.h for API docs.
176void GSystem::addGMaterial(std::vector<std::string> pars) {
177 std::string materialName = pars[0];
178
179 if (gmaterialsMap.find(materialName) == gmaterialsMap.end()) {
180 gmaterialsMap[materialName] = std::make_shared<GMaterial>(name, pars, log);
181 log->info(1, "Adding gMaterial <" + materialName + "> to gmaterialsMap.");
182 log->info(2, *gmaterialsMap[materialName]);
183 }
184 else {
186 "gMaterial <" + materialName + "> already exists in gmaterialsMap.");
187 }
188}
189
190
191// MARK: GMIRRORS
192
193// See gsystem.h for API docs.
194void GSystem::addGMirror(std::vector<std::string> pars) {
195 std::string mirrorName = pars[0];
196
197 if (gmirrorsMap.find(mirrorName) == gmirrorsMap.end()) {
198 gmirrorsMap[mirrorName] = std::make_shared<GMirror>(name, pars, log);
199 log->info(1, "Adding gMirror <" + mirrorName + "> to gmirrorsMap.");
200 log->info(2, *gmirrorsMap[mirrorName]);
201 }
202 else {
204 "gMirror <" + mirrorName + "> already exists in gmirrorsMap.");
205 }
206}
207
208
209// See gsystem.h for API docs.
210const GMirror* GSystem::getGMirror(const std::string& mirrorName) const {
211 auto it = gmirrorsMap.find(mirrorName);
212 if (it != gmirrorsMap.end())
213 return it->second.get();
214 return nullptr;
215}
216
217
218// See gsystem.h for API docs.
219const GMaterial* GSystem::getMaterialForGVolume(const std::string& volumeName) const {
220 auto it = gvolumesMap.find(volumeName);
221 if (it != gvolumesMap.end()) {
222 std::string materialName = it->second->getMaterial();
223 auto matIt = gmaterialsMap.find(materialName);
224 if (matIt != gmaterialsMap.end())
225 return matIt->second.get();
226 else {
228 "gMaterial <" + materialName + "> not found for volume <" + volumeName + ">");
229 }
230 }
231 return nullptr;
232}
233
234
235// See gsystem.h for API docs.
236std::string GSystem::getFilePath() const {
237 if (path.empty())
238 return name;
239 return path + "/" + name;
240}
std::shared_ptr< GLogger > log
void warning(Args &&... args) const
void debug(debug_type type, Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
Material definition belonging to a detector system.
Definition gmaterial.h:28
Optical boundary (surface) definition belonging to a detector system.
Definition gmirror.h:28
Represents a single detector system (e.g., calorimeter, tracker).
Definition gsystem.h:33
std::string getFilePath() const
Gets the full file path of the system.
Definition gsystem.cc:236
const GMaterial * getMaterialForGVolume(const std::string &volumeName) const
Retrieve the material associated with a given volume.
Definition gsystem.cc:219
void addROOTVolume(const std::string &rootVolumeDefinition)
Adds the special ROOT/world volume to the system.
Definition gsystem.cc:112
void addGMaterial(std::vector< std::string > pars)
Build and add a material from a serialized parameter list.
Definition gsystem.cc:176
GVolume * getGVolume(const std::string &volumeName) const
Retrieve a volume by name.
Definition gsystem.cc:165
GSystem(const std::shared_ptr< GOptions > &gopts, const std::string &dbhost, const std::string &sname, const std::string &factory, const std::string &experiment, int runno, const std::string &variation, const std::string &annotations="none")
Construct a detector system descriptor.
Definition gsystem.cc:51
std::shared_ptr< GSystem > descriptorClone(const std::shared_ptr< GOptions > &gopts) const
Clone only the system selection metadata.
Definition gsystem.cc:81
const GMirror * getGMirror(const std::string &mirrorName) const
Retrieve a mirror by name.
Definition gsystem.cc:210
void addGVolume(std::vector< std::string > pars)
Build and add a volume from a serialized parameter list.
Definition gsystem.cc:90
void addGMirror(std::vector< std::string > pars)
Build and add a mirror (optical surface) from a serialized parameter list.
Definition gsystem.cc:194
void addVolumeFromFile(const std::string &importType, const std::string &filename)
Add a volume imported from a file (CAD, GDML, etc.).
Definition gsystem.cc:126
Geometry volume record loaded into a GSystem.
Definition gvolume.h:34
run
CONSTRUCTOR
Conventions and shared constants for the detector-system module.
#define GSYSTEMSQLITETFACTORYLABEL
#define ERR_GMIRRORALREADYPRESENT
#define DEFAULTG4PLACEMENTTYPE
#define ERR_GVOLUMEALREADYPRESENT
#define ROOTWORLDGVOLUMENAME
Canonical name for the ROOT/world gvolume entry.
#define ERR_GMATERIALALREADYPRESENT
#define ERR_GMATERIALNOTFOUND
Option definitions and extraction helpers for the gsystem module.
constexpr const char * GSYSTEM_LOGGER
#define UNINITIALIZEDSTRINGQUANTITY
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)
string getDirFromPath(const std::string &path)
string getFileFromPath(const std::string &path)