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
42
43// See gsystem.h for API docs.
44GSystem::GSystem(const std::shared_ptr<GOptions>& gopts,
45 const std::string& dbh,
46 const std::string& sname,
47 const std::string& factory,
48 const std::string& exp,
49 int run,
50 const std::string& variation,
51 const std::string& notes)
52 : GBase(gopts, GSYSTEM_LOGGER),
53 dbhost(dbh),
54 name(gutilities::getFileFromPath(sname)),
55 path(gutilities::getDirFromPath(sname)), // Initialize 'path' directly
56 factoryName(factory),
57 experiment(exp),
58 runno(run),
59 variation(variation),
60 annotations(notes) // Use 'notes' directly
61{
62 // If the provided name does not include a directory, set the path to empty.
63 if (name == path || factoryName == GSYSTEMSQLITETFACTORYLABEL) {
64 path = "";
65 log->info(1, "Instantiating GSystem <", name, "> using factory <", factoryName, ">");
66 }
67 else {
68 log->info(1, "Instantiating GSystem <", name, "> using path <", path, "> ",
69 "and factory <", factoryName, ">");
70 }
71}
72
73// See gsystem.h for API docs.
74std::shared_ptr<GSystem> GSystem::descriptorClone(const std::shared_ptr<GOptions>& gopts) const {
75 return std::make_shared<GSystem>(gopts, dbhost, getFilePath(), factoryName, experiment, runno, variation,
76 annotations);
77}
78
79
80// MARK: GVOLUMES
81
82// See gsystem.h for API docs.
83void GSystem::addGVolume(std::vector<std::string> pars) {
84 // Append variation and run number to parameters so the GVolume stores provenance and filtering context.
85 pars.emplace_back(variation);
86 pars.emplace_back(std::to_string(runno));
87
88 std::string volume_name = pars[0];
89
90 // Check if the volume already exists in the map.
91 if (gvolumesMap.find(volume_name) == gvolumesMap.end()) {
92 // Create and add GVolume to the map.
93 gvolumesMap[volume_name] = std::make_shared<GVolume>(log, name, pars);
94 log->info(1, "Adding gVolume <" + volume_name + "> to gvolumesMap.");
95 log->info(2, *gvolumesMap[volume_name]);
96 }
97 else {
99 "gVolume <" + volume_name + "> already exists in gvolumesMap.");
100 }
101}
102
103
104// See gsystem.h for API docs.
105void GSystem::addROOTVolume(const std::string& rootVolumeDefinition) {
106 log->warning("Adding ROOT volume using <" + rootVolumeDefinition + "> to gvolumesMap.");
107 // ROOTWORLDGVOLUMENAME is assumed to be defined in gsystemConventions.h.
108 gvolumesMap[ROOTWORLDGVOLUMENAME] = std::make_shared<GVolume>(rootVolumeDefinition, log);
109}
110
111// add volume from a file (CAD or GDML factories)
112// includes factory and filename in the definitions
113#include <filesystem>
114#include <utility>
115
116namespace fs = std::filesystem;
117
118// See gsystem.h for API docs.
119void GSystem::addVolumeFromFile(const std::string& importType, const std::string& filename) {
120 std::vector<std::string> pars;
121
122 // Extract filename (without the path) and split by delimiter.
123 std::vector<std::string> gvpaths = gutilities::getStringVectorFromStringWithDelimiter(
124 fs::path(filename).filename().string(),
125 ".");
126
127 // Use the first item as the volume name.
128 const std::string& gvolumeName = gvpaths.front();
129
130 // Order is defined in gvolume.cc:
131 // 01: name, 03: type, 04: parameters, 05: material, 02: mother, etc.
132 pars.emplace_back(gvolumeName); // 01 name
133 pars.emplace_back(importType); // 03 type
134 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 04 parameters
135 pars.emplace_back("G4_AIR"); // 05 material: default is air
136 pars.emplace_back(ROOTWORLDGVOLUMENAME); // 02 mother: default is ROOTWORLDGVOLUMENAME
137 pars.emplace_back("0*cm, 0*cm, 0*cm"); // 06 position
138 pars.emplace_back("0*deg, 0*deg, 0*deg"); // 07 rotation
139 pars.emplace_back(DEFAULTG4PLACEMENTTYPE); // 08 Geant4 placement constructor convention
140 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 09 electromagnetic field
141 pars.emplace_back("1"); // 10 visible
142 pars.emplace_back("1"); // 11 style
143 pars.emplace_back("999999"); // 12 color
144 pars.emplace_back("1"); // 13 opacity
145 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 14 digitization
146 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 15 gidentity
147 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 16 copyOf
148 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 17 solidsOpr
149 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 18 mirror
150 pars.emplace_back("1"); // 19 exist flag
151 pars.emplace_back(filename); // 20 description: contains full path
152
153 addGVolume(pars);
154}
155
156
157// See gsystem.h for API docs.
158GVolume* GSystem::getGVolume(const std::string& volumeName) const {
159 auto it = gvolumesMap.find(volumeName);
160 if (it != gvolumesMap.end())
161 return it->second.get();
162 return nullptr;
163}
164
165
166// MARK: GMATERIALS
167
168// See gsystem.h for API docs.
169void GSystem::addGMaterial(std::vector<std::string> pars) {
170 std::string materialName = pars[0];
171
172 if (gmaterialsMap.find(materialName) == gmaterialsMap.end()) {
173 gmaterialsMap[materialName] = std::make_shared<GMaterial>(name, pars, log);
174 log->info(1, "Adding gMaterial <" + materialName + "> to gmaterialsMap.");
175 log->info(2, *gmaterialsMap[materialName]);
176 }
177 else {
179 "gMaterial <" + materialName + "> already exists in gmaterialsMap.");
180 }
181}
182
183
184// See gsystem.h for API docs.
185const GMaterial* GSystem::getMaterialForGVolume(const std::string& volumeName) const {
186 auto it = gvolumesMap.find(volumeName);
187 if (it != gvolumesMap.end()) {
188 std::string materialName = it->second->getMaterial();
189 auto matIt = gmaterialsMap.find(materialName);
190 if (matIt != gmaterialsMap.end())
191 return matIt->second.get();
192 else {
194 "gMaterial <" + materialName + "> not found for volume <" + volumeName + ">");
195 }
196 }
197 return nullptr;
198}
199
200
201// See gsystem.h for API docs.
202std::string GSystem::getFilePath() const {
203 if (path.empty())
204 return name;
205 return path + "/" + name;
206}
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
Represents a single detector system (e.g., calorimeter, tracker).
Definition gsystem.h:32
std::string getFilePath() const
Gets the full file path of the system.
Definition gsystem.cc:202
const GMaterial * getMaterialForGVolume(const std::string &volumeName) const
Retrieve the material associated with a given volume.
Definition gsystem.cc:185
void addROOTVolume(const std::string &rootVolumeDefinition)
Adds the special ROOT/world volume to the system.
Definition gsystem.cc:105
void addGMaterial(std::vector< std::string > pars)
Build and add a material from a serialized parameter list.
Definition gsystem.cc:169
GVolume * getGVolume(const std::string &volumeName) const
Retrieve a volume by name.
Definition gsystem.cc:158
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:44
std::shared_ptr< GSystem > descriptorClone(const std::shared_ptr< GOptions > &gopts) const
Clone only the system selection metadata.
Definition gsystem.cc:74
void addGVolume(std::vector< std::string > pars)
Build and add a volume from a serialized parameter list.
Definition gsystem.cc:83
void addVolumeFromFile(const std::string &importType, const std::string &filename)
Add a volume imported from a file (CAD, GDML, etc.).
Definition gsystem.cc:119
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 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)