gphysics
Loading...
Searching...
No Matches
gphysics.cc
Go to the documentation of this file.
1// gphysics
2#include "gphysics.h"
3#include "gphysics_options.h"
5#include "gphysicsRuntime.h"
6
7// gemc
8#include "gutilities.h"
9
10// geant4 version
11#include "G4Version.hh"
12
13// geant4
14
15// From examples/extended/physicslists/extensibleFactory:
16// The extensible factory also allows for the extension of lists by adding
17// (using "+" as a separator) or replacing (using "_" as a separator)
18// specific physics constructors. These can be specified by
19// pre-defining a short name (e.g. RADIO for G4RadioactiveDecayPhysics) or
20// providing the full class name.
21#include "G4PhysListFactoryAlt.hh"
22
23// allow ourselves to give the user extra info about available physics constructors (ctors)
24#include "G4PhysicsConstructorFactory.hh"
25
26
27// Implementation note (non-Doxygen):
28// The public API and semantics are documented in gphysics.h. This translation unit focuses on:
29// - Reading options
30// - Instantiating the requested reference physics list using the Geant4 extensible factory
31// - Registering default add-on constructors
32GPhysics::GPhysics(const std::shared_ptr<GOptions>& gopts) :
33 GBase(gopts, GPHYSICS_LOGGER),
34 physList(nullptr) {
35 log->debug(CONSTRUCTOR, "GPhysics");
36
37 bool showPhys = gopts->getSwitch("showPhysics");
38 std::string gphysList = gopts->getRequiredScalarString("phys_list");
39
40 if (showPhys) {
41 // Diagnostic mode: print what Geant4 advertises and return without building a list.
42 printAvailable();
43 return;
44 }
45
46 // g4alt::G4PhysListFactoryAlt is the extensible factory
47 // including the G4PhysListFactoryAlt.hh header and the line:
48 // using namespace g4alt;
49 // would make this a drop-in replacement, but we'll list the explicit
50 // namespace here just for clarity
51 // The Geant4 factory syntax is whitespace-sensitive; normalize user input.
52 std::string g4physList = gutilities::removeAllSpacesFromString(gphysList);
53
54 try {
55 physList = new gphysics::RuntimePhysicsList(g4physList);
56 } catch (const std::exception& error) {
57 log->error(gphysics::ERR_PHYSLISTERROR, "%1", error.what());
58 }
59 if (const auto cut = gopts->getOptionalScalarDouble("production_cut")) {
60 std::string error;
61 if (!static_cast<gphysics::RuntimePhysicsList*>(physList)->setProductionCut(*cut, error)) {
62 log->error(gphysics::ERR_PHYSLISTERROR, "%1", error);
63 }
64 }
65
66 if (!physList) {
67 log->error(gphysics::ERR_PHYSLISTERROR, "physics list <%1> could not be loaded.", gphysList);
68 }
69
70 log->info(2, "G4PhysListFactory: <%1> loaded.", g4physList);
71}
72
73
74// Implementation note (non-Doxygen):
75// Prints Geant4 version metadata and queries Geant4 registries for the available lists/constructors.
76void GPhysics::printAvailable() const {
77 std::string g4ver = gutilities::replaceCharInStringWithChars(G4Version, "$", "");
78
79 log->info(0, "Geant4 Version %1 %2", g4ver, G4Date);;
80
81 g4alt::G4PhysListFactory factory;
82 factory.PrintAvailablePhysLists();
83
84 log->info(0, "Available Geant4 Physics Lists:");
85
86 G4PhysicsConstructorRegistry* g4pctorFactory = G4PhysicsConstructorRegistry::Instance();
87 g4pctorFactory->PrintAvailablePhysicsConstructors();
88}
GBase(const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
std::shared_ptr< GLogger > log
GPhysics(const std::shared_ptr< GOptions > &gopts)
Constructs the physics list builder and (unless requested otherwise) instantiates the physics list.
Definition gphysics.cc:32
CONSTRUCTOR
constexpr const char * GPHYSICS_LOGGER
constexpr int ERR_PHYSLISTERROR
Error code used when the requested physics list cannot be loaded.
string removeAllSpacesFromString(const std::string &str)
string replaceCharInStringWithChars(const std::string &input, const std::string &toReplace, const std::string &replacement)