Debian Codegraph @

ncrystal_geant4/include/G4NCrystal/G4NCManager.hh

raw symbols SCIP
1 #ifndef G4NCrystal_Manager_hh
2 #define G4NCrystal_Manager_hh
3
4 ////////////////////////////////////////////////////////////////////////////////
5 // //
6 // This file is part of NCrystal (see https://mctools.github.io/ncrystal/) //
7 // //
8 // Copyright 2015-2022 NCrystal developers //
9 // //
10 // Licensed under the Apache License, Version 2.0 (the "License"); //
11 // you may not use this file except in compliance with the License. //
12 // You may obtain a copy of the License at //
13 // //
14 // http://www.apache.org/licenses/LICENSE-2.0 //
15 // //
16 // Unless required by applicable law or agreed to in writing, software //
17 // distributed under the License is distributed on an "AS IS" BASIS, //
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
19 // See the License for the specific language governing permissions and //
20 // limitations under the License. //
21 // //
22 ////////////////////////////////////////////////////////////////////////////////
23
24 #include <map>
25 #include <vector>
26 #include "G4Material.hh"
27 #include "G4Version.hh"
28 #include "G4MaterialPropertiesTable.hh"
29 #include "NCrystal/NCProcImpl.hh"
30 //Manager class tracking indices of NCrystal::Scatter instances associated to
31 //G4Materials, via entries in the G4MaterialPropertiesTable's on the materials.
32
33 class G4Material;
34 namespace NCrystal {
35 namespace Error {
36 class Exception;
37 }
38 }
39
40 namespace G4NCrystal {
41
42 class Manager {
43 public:
44
45 //Methods needed to add NCrystal::Scatter* properties to G4Materials (via
46 //the property tables):
47 static Manager * getInstance();//Get the singleton
48 void addScatterProperty(G4Material*, NCrystal::ProcImpl::ProcPtr&&);
49
50 //Methods for framework implementers:
51 const NCrystal::ProcImpl::Process* getScatterProperty(G4Material*) const;//returns nullptr when absent.
52 NCrystal::ProcImpl::OptionalProcPtr getScatterPropertyPtr(G4Material*) const;//returns nullptr when absent.
53
54 //Same but with per-thread per-scatter CachePtr:
55 using ProcAndCache = std::pair<const NCrystal::ProcImpl::Process*,NCrystal::CachePtr*>;
56 ProcAndCache getScatterPropertyWithThreadSafeCache(G4Material*) const;
57
58 //Thoroughly clear caches, manager singleton, and possibly NCrystal
59 //factories. It is NOT safe to use the Scatter properties of already created
60 //G4Materials after this.
61 static void cleanup();
62
63 //Safer cleanup which should only remove objects with no current usage:
64 static void clearCaches();
65
66 unsigned nMaterialsWithProperties() const { return m_scatters.size(); }
67
68 //Translate thrown NCrystal exceptions to G4Exception(..) calls (id should
69 //be unique and fixed for each call location):
70 static void handleError( const char*origin, unsigned id,
71 NCrystal::Error::Exception& );
72
73 private:
74 Manager( const Manager & );
75 Manager & operator= ( const Manager & );
76 Manager();
77 ~Manager();
78 static Manager * s_mgr;
79 std::vector<NCrystal::ProcImpl::ProcPtr> m_scatters;
80 std::map<uint64_t,unsigned> m_scat2idx;
81 G4String m_key;
82 NCrystal::CachePtr& getCachePtrForCurrentThreadAndProcess( unsigned scatter_idx ) const;
83 //Returns numeric_limits<unsigned>::max() if not available:
84 unsigned lookupScatterPropertyIndex(G4Material*) const;
85
86 };
87
88 ///////////////////////////////////////////////
89 // Inline for fast access during event loop: //
90 ///////////////////////////////////////////////
91
92 inline std::pair<const NCrystal::ProcImpl::Process*, NCrystal::CachePtr*>
93 Manager::getScatterPropertyWithThreadSafeCache(G4Material* mat) const
94 {
95 //Returns numeric_limits<unsigned>::max() if not available:
96 unsigned scatidx = lookupScatterPropertyIndex(mat);
97 if ( scatidx == std::numeric_limits<unsigned>::max() )
98 return {nullptr,nullptr};
99 assert(scatidx<m_scatters.size());
100 const NCrystal::ProcImpl::Process* sp = m_scatters[scatidx].get();
101 return { sp, &getCachePtrForCurrentThreadAndProcess( scatidx ) };
102 }
103
104 inline unsigned Manager::lookupScatterPropertyIndex(G4Material*mat) const
105 {
106 G4MaterialPropertiesTable* matprop = mat->GetMaterialPropertiesTable();
107 constexpr unsigned not_found = std::numeric_limits<unsigned>::max();
108 if (!matprop)
109 return not_found;
110 #if G4VERSION_NUMBER < 1040
111 //Property maps pre-Geant4 10.4
112 using PropMap_T = std::map< G4String, G4double, std::less<G4String> > ;
113 const PropMap_T* propcmap = matprop->GetPropertiesCMap();
114 PropMap_T::size_type size = propcmap->size();
115 //Access property like this instead of via
116 //ConstPropertyExists+GetConstProperty, to avoid needless string allocations
117 //and double map lookup:
118 if (size<=1) {
119 //Optimise to quickly check in small maps, since we are likely the only
120 //user of constant material properties (this check can be removed if the
121 //situation changes drastically):
122 PropMap_T::const_iterator it;
123 return (size && (it=propcmap->begin())->first == m_key) ? static_cast<unsigned>(it->second) : not_found;
124 } else {
125 //Full map search:
126 PropMap_T::const_iterator it = propcmap->find(m_key);
127 return it==propcmap->end() ? not_found : static_cast<unsigned>(it->second);
128 }
129 #else
130 //Property maps post Geant4 10.4. Here the internal keys are integers, but
131 //we can't be sure it is always the same in different materials. Unfortunately
132 //GetConstProperty triggers a G4 exception in case the key is not present,
133 //so for safety we have to trigger one extra map search via ConstPropertyExists:
134
135 # if G4VERSION_NUMBER >= 1100
136 if ( matprop->GetMaterialConstPropertyNames().empty() || !matprop->ConstPropertyExists(m_key) )
137 return not_found;
138 # else
139 if ( matprop->GetConstPropertyMap()->empty() || !matprop->ConstPropertyExists(m_key) )
140 return not_found;
141 #endif
142 return static_cast<unsigned>(matprop->GetConstProperty(m_key));
143 #endif
144 }
145
146 inline const NCrystal::ProcImpl::Process* Manager::getScatterProperty(G4Material*mat) const
147 {
148 //Returns numeric_limits<unsigned>::max() if not available:
149 unsigned scatidx = lookupScatterPropertyIndex(mat);
150 if ( scatidx == std::numeric_limits<unsigned>::max() )
151 return nullptr;
152 return m_scatters.at(scatidx).get();
153 }
154
155 }
156
157 #endif