DLG4::VolumeBuilders
A fluent interface for Geant4 geometry definition.
Loading...
Searching...
No Matches
MaterialsHelpers.hh
Go to the documentation of this file.
1//GeoModulesCopyMaterials. D.S. Leonard 2026 (code is much older, ~ 2017
2// This typically gets included through VolumeBuilders.hh !!
3
4#pragma once
7#include <G4Material.hh>
8#include <G4String.hh>
9#include <G4Element.hh>
10#ifndef GEOMODULE_MATERIALS_HELPERS_HH
11#define GEOMODULE_MATERIALS_HELPERS_HH
12
13
16 G4Material *CopyMaterial(
17 G4MaterialPtrOrString source, const G4String &name, G4double dens, Density unit);
18
20 G4Material *CopyMaterial(G4MaterialPtrOrString source, const G4String &name);
21
27 G4Material *GetMaterial(const G4String &materialName);
28 G4Element *GetElement(const G4String &elementName);
29
34 public:
36 : m_ptr(nullptr) {
37 }
38
39 // Add this constructor to handle "literal strings"
40 G4MaterialPtrOrString(const char *name)
41 : m_ptr(VBHelpers::GetMaterial(G4String(name))) {
42 }
43
44 // Converting constructor from G4Material*
45 G4MaterialPtrOrString(G4Material *mat)
46 : m_ptr(mat) {
47 }
48
49 // Converting constructor from string
50 G4MaterialPtrOrString(const G4String &name)
51 : m_ptr(VBHelpers::GetMaterial(name)) {
52 }
53
54 // Conversion operator to G4Material*
55 operator G4Material *() const { return m_ptr; }
56 // Arrow operator to use it like a pointer directly
57 G4Material *operator->() const { return m_ptr; }
58 // Explicit bool operator prevents accidental math/logic errors
59 // but allows use in 'if' statements.
60 explicit operator bool() const {
61 return m_ptr != nullptr;
62 }
63
64 private:
65 G4Material *m_ptr;
66 };
67}
68#endif
G4Material * CopyMaterial(G4MaterialPtrOrString source, const G4String &name, const G4double dens, const Density unit)
Copy a G4Material to one with a new name, and change its density.
G4Element * GetElement(const G4String &elementName)
G4Material * GetMaterial(const G4String &materialName)
Get material by local or NIST name.
DLG4::Units::Density Density