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
74// MARK: GVOLUMES
75
76// See gsystem.h for API docs.
77void GSystem::addGVolume(std::vector<std::string> pars) {
78 // Append variation and run number to parameters so the GVolume stores provenance and filtering context.
79 pars.emplace_back(variation);
80 pars.emplace_back(std::to_string(runno));
81
82 std::string volume_name = pars[0];
83
84 // Check if the volume already exists in the map.
85 if (gvolumesMap.find(volume_name) == gvolumesMap.end()) {
86 // Create and add GVolume to the map.
87 gvolumesMap[volume_name] = std::make_shared<GVolume>(log, name, pars);
88 log->info(1, "Adding gVolume <" + volume_name + "> to gvolumesMap.");
89 log->info(2, *gvolumesMap[volume_name]);
90 }
91 else {
93 "gVolume <" + volume_name + "> already exists in gvolumesMap.");
94 }
95}
96
97
98// See gsystem.h for API docs.
99void GSystem::addROOTVolume(const std::string& rootVolumeDefinition) {
100 log->warning("Adding ROOT volume using <" + rootVolumeDefinition + "> to gvolumesMap.");
101 // ROOTWORLDGVOLUMENAME is assumed to be defined in gsystemConventions.h.
102 gvolumesMap[ROOTWORLDGVOLUMENAME] = std::make_shared<GVolume>(rootVolumeDefinition, log);
103}
104
105// add volume from a file (CAD or GDML factories)
106// includes factory and filename in the definitions
107#include <filesystem>
108#include <utility>
109
110namespace fs = std::filesystem;
111
112// See gsystem.h for API docs.
113void GSystem::addVolumeFromFile(const std::string& importType, const std::string& filename) {
114 std::vector<std::string> pars;
115
116 // Extract filename (without the path) and split by delimiter.
117 std::vector<std::string> gvpaths = gutilities::getStringVectorFromStringWithDelimiter(
118 fs::path(filename).filename().string(),
119 ".");
120
121 // Use the first item as the volume name.
122 const std::string& gvolumeName = gvpaths.front();
123
124 // Order is defined in gvolume.cc:
125 // 01: name, 03: type, 04: parameters, 05: material, 02: mother, etc.
126 pars.emplace_back(gvolumeName); // 01 name
127 pars.emplace_back(importType); // 03 type
128 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 04 parameters
129 pars.emplace_back("G4_AIR"); // 05 material: default is air
130 pars.emplace_back(ROOTWORLDGVOLUMENAME); // 02 mother: default is ROOTWORLDGVOLUMENAME
131 pars.emplace_back("0*cm, 0*cm, 0*cm"); // 06 position
132 pars.emplace_back("0*deg, 0*deg, 0*deg"); // 07 rotation
133 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 08 electromagnetic field
134 pars.emplace_back("1"); // 09 visible
135 pars.emplace_back("1"); // 10 style
136 pars.emplace_back("999999"); // 11 color
137 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 12 digitization
138 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 13 gidentity
139 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 14 copyOf
140 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 16 solidsOpr
141 pars.emplace_back(UNINITIALIZEDSTRINGQUANTITY); // 17 mirrot
142 pars.emplace_back("1"); // 18 exist flag
143 pars.emplace_back(filename); // 19 description: contains full path
144
145 addGVolume(pars);
146}
147
148
149// See gsystem.h for API docs.
150GVolume* GSystem::getGVolume(const std::string& volumeName) const {
151 auto it = gvolumesMap.find(volumeName);
152 if (it != gvolumesMap.end())
153 return it->second.get();
154 return nullptr;
155}
156
157
158// MARK: GMATERIALS
159
160// See gsystem.h for API docs.
161void GSystem::addGMaterial(std::vector<std::string> pars) {
162 std::string materialName = pars[0];
163
164 if (gmaterialsMap.find(materialName) == gmaterialsMap.end()) {
165 gmaterialsMap[materialName] = std::make_shared<GMaterial>(name, pars, log);
166 log->info(1, "Adding gMaterial <" + materialName + "> to gmaterialsMap.");
167 log->info(2, *gmaterialsMap[materialName]);
168 }
169 else {
171 "gMaterial <" + materialName + "> already exists in gmaterialsMap.");
172 }
173}
174
175
176// See gsystem.h for API docs.
177const GMaterial* GSystem::getMaterialForGVolume(const std::string& volumeName) const {
178 auto it = gvolumesMap.find(volumeName);
179 if (it != gvolumesMap.end()) {
180 std::string materialName = it->second->getMaterial();
181 auto matIt = gmaterialsMap.find(materialName);
182 if (matIt != gmaterialsMap.end())
183 return matIt->second.get();
184 else {
186 "gMaterial <" + materialName + "> not found for volume <" + volumeName + ">");
187 }
188 }
189 return nullptr;
190}
191
192
193// See gsystem.h for API docs.
194std::string GSystem::getFilePath() {
195 if (path.empty())
196 return name;
197 return path + "/" + name;
198}
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:29
Represents a single detector system (e.g., calorimeter, tracker).
Definition gsystem.h:32
const GMaterial * getMaterialForGVolume(const std::string &volumeName) const
Retrieve the material associated with a given volume.
Definition gsystem.cc:177
void addROOTVolume(const std::string &rootVolumeDefinition)
Adds the special ROOT/world volume to the system.
Definition gsystem.cc:99
void addGMaterial(std::vector< std::string > pars)
Build and add a material from a serialized parameter list.
Definition gsystem.cc:161
GVolume * getGVolume(const std::string &volumeName) const
Retrieve a volume by name.
Definition gsystem.cc:150
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
void addGVolume(std::vector< std::string > pars)
Build and add a volume from a serialized parameter list.
Definition gsystem.cc:77
std::string getFilePath()
Gets the full file path of the system.
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:113
Geometry volume record loaded into a GSystem.
Definition gvolume.h:33
CONSTRUCTOR
Conventions and shared constants for the detector-system module.
#define GSYSTEMSQLITETFACTORYLABEL
#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)