gfields
Loading...
Searching...
No Matches
gmagneto.h
Go to the documentation of this file.
1#pragma once
2
3// gemc
4#include "gfield.h"
5#include "gfield_options.h"
6#include "gfieldConventions.h"
7
8// geant4
9#include "G4ThreeVector.hh"
10
11// c++
12#include <memory>
13#include <set>
14#include <string>
15
16class GLogger;
17
47class GMagneto : public GBase<GMagneto> {
48public:
62 explicit GMagneto(const std::shared_ptr<GOptions>& gopts,
63 const std::set<std::string>& required_fields = {});
64
75 static std::shared_ptr<GField> initialize_magnetic_field(
76 const std::shared_ptr<GOptions>& gopts, double& field_polarity,
77 std::shared_ptr<GLogger> caller_log = nullptr);
78
86 const std::shared_ptr<GField>& magnetic_field, const G4ThreeVector& position);
87
88private:
89 using gFieldMap = std::unordered_map<std::string, std::shared_ptr<GField>>;
90 using gFieldMgrMap = std::unordered_map<std::string, std::shared_ptr<G4FieldManager>>;
91
93 std::shared_ptr<gFieldMap> fields_map;
94
96 std::shared_ptr<gFieldMgrMap> fields_manager;
97
98public:
104 bool isField(const std::string& name) const { return fields_map->find(name) != fields_map->end(); }
105
110 std::vector<std::string> getFieldNames() const;
111
119 std::shared_ptr<GField> getField(std::string name) {
120 bool not_found = (fields_map->find(name) == fields_map->end());
121
122 if (not_found) { log->error(ERR_WRONG_FIELD_NOT_FOUND, "GField >", name, "< not found. Exiting."); }
123 return fields_map->at(name);
124 }
125
133 std::shared_ptr<G4FieldManager> getFieldMgr(std::string name) {
134
135 // Debug visibility: print out the full map of registered field managers.
136 for (const auto& [key, value] : *fields_manager) {
137 log->info(2, "GFieldManager >", key, "< >", value);
138 }
139
140 if (fields_manager->find(name) == fields_manager->end()) {
141 log->error(ERR_WRONG_FIELD_NOT_FOUND, "GField >", name, "< not found. Exiting.");
142 }
143
144 return fields_manager->at(name);
145 }
146};
std::shared_ptr< GLogger > log
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
Manager for magnetic fields and their associated G4FieldManager objects.
Definition gmagneto.h:47
GMagneto(const std::shared_ptr< GOptions > &gopts, const std::set< std::string > &required_fields={})
Construct and initialize the magnetic field registry.
Definition gmagneto.cc:37
std::shared_ptr< G4FieldManager > getFieldMgr(std::string name)
Retrieve the G4FieldManager associated with a given field name.
Definition gmagneto.h:133
std::shared_ptr< GField > getField(std::string name)
Retrieve a field object by name.
Definition gmagneto.h:119
bool isField(const std::string &name) const
Check whether a field with the given name exists.
Definition gmagneto.h:104
static double magnetic_field_magnitude_tesla(const std::shared_ptr< GField > &magnetic_field, const G4ThreeVector &position)
Return the magnetic-field magnitude at a position in Tesla.
Definition gmagneto.cc:140
std::vector< std::string > getFieldNames() const
Return the configured field names.
Definition gmagneto.cc:153
static std::shared_ptr< GField > initialize_magnetic_field(const std::shared_ptr< GOptions > &gopts, double &field_polarity, std::shared_ptr< GLogger > caller_log=nullptr)
Load the configured global magnetic field for code that needs direct field probes.
Definition gmagneto.cc:85
double position[3]
#define ERR_WRONG_FIELD_NOT_FOUND