Geant4 VMC Version 6.6
Loading...
Searching...
No Matches
TG4VRegionsManager.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Geant4 Virtual Monte Carlo package
3// Copyright (C) 2007 - 2023 Ivana Hrivnacova
4// All rights reserved.
5//
6// For the licensing terms see geant4_vmc/LICENSE.
7// Contact: root-vmc@cern.ch
8//-------------------------------------------------
9
14
15#include "TG4VRegionsManager.h"
16#include "TG4G3CutVector.h"
17#include "TG4G3PhysicsManager.h"
18#include "TG4G3Units.h"
19#include "TG4GeometryServices.h"
20#include "TG4Globals.h"
21#include "TG4Limits.h"
22#include "TG4Medium.h"
23#include "TG4RegionsMessenger.h"
24
25#include <G4LogicalVolumeStore.hh>
26#include <G4ProductionCuts.hh>
27#include <G4Region.hh>
28#include <G4RegionStore.hh>
29#include <G4RunManager.hh>
30#include <G4SystemOfUnits.hh>
31#include <G4UnitsTable.hh>
32#include <G4VUserPhysicsList.hh>
33#include <G4Version.hh>
34
35#include <map>
36#include <set>
37#include <fstream>
38
40
42 "RegionWithDefaultCuts";
43const G4String TG4VRegionsManager::fgkDefaultFileName = "regions.dat";
44
45//_____________________________________________________________________________
47 : TG4Verbose("regionsManager")
48{
50
51 fgInstance = this;
52}
53
54//_____________________________________________________________________________
61
62//
63// protected methods
64//
65
66//_____________________________________________________________________________
68{
71
72 TG4boolVector* isCutVector =
75
76 G4double cutValue = 0.;
77 if ((*isCutVector)[cutType] && (*cutVector)[cutType] > 0.) {
78 cutValue = (*cutVector)[cutType];
79 }
80 else {
81 // get g3default and issue a warning
82 TG4Globals::Warning("TG4VRegionsManager", "GetGlobalEnergyCut",
83 "The default cut " + TString(TG4G3CutVector::GetCutName(cutType)) +
84 " is not defined." + TG4Globals::Endl() + "The default Geant3 value will be used"
85 " for tracking media that do not define this cut." + TG4Globals::Endl());
86
87 cutValue = TG4G3Defaults::Instance()->CutValue(cutType);
88 }
89
90 return cutValue;
91}
92
93//_____________________________________________________________________________
95 TG4Limits* limits, TG4G3Cut cutType, G4double globalCutValue) const
96{
99
100 G4double cut = 0.;
101 if (limits->GetCutVector() && (*limits->GetCutVector())[cutType] > 0.) {
102 cut = (*limits->GetCutVector())[cutType];
103 }
104 else {
105 cut = globalCutValue;
106 }
107
108 return cut;
109}
110
111//_____________________________________________________________________________
113 const G4MaterialCutsCouple* couple, const G4Region* region) const
114{
117
118 G4ProductionCuts* productionCuts = region->GetProductionCuts();
119 std::vector<G4Material*>::const_iterator itm = region->GetMaterialIterator();
120 size_t nofMaterials = region->GetNumberOfMaterials();
121 for (size_t i = 0; i < nofMaterials; i++, itm++) {
122 if (couple->GetMaterial() == (*itm) &&
123 couple->GetProductionCuts() == productionCuts) {
124 return true;
125 }
126 }
127
128 return false;
129}
130
133{
136
137 if (VerboseLevel() > 0) {
138 G4cout << ".. Checking regions materials " << G4endl;
139 }
140
141 Bool_t good = true;
142 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
143 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
144
145 G4LogicalVolume* lv = (*lvStore)[i];
146
147 // skip world volume
148 if (lv == TG4GeometryServices::Instance()->GetWorld()->GetLogicalVolume())
149 continue;
150
151 // skip volume without medium
152 TG4Medium* medium =
154 if (!medium) continue;
155
156 if (lv->GetRegion()->GetName() != lv->GetMaterial()->GetName() &&
157 lv->GetRegion()->GetName() != fgkDefaultRegionName) {
158
159 G4cout << "The region name " << lv->GetRegion()->GetName()
160 << " for LV = " << lv->GetName()
161 << " does not match its material name "
162 << lv->GetMaterial()->GetName() << G4endl;
163
164 good = false;
165 }
166 }
167 if (good) {
168 G4cout << ".. Regions are consistent with materials." << G4endl;
169 }
170 else {
171 G4cout << ".. Found inconsistencies between regions and materials."
172 << G4endl;
173 }
174}
175
176//_____________________________________________________________________________
177void TG4VRegionsManager::PrintLegend(std::ostream& output) const
178{
180
181 //clang-format off
182 output << std::setw(30) << std::left << "# material name"
183 << " rangeGam[mm]"
184 << " rangeEle[mm]"
185 << " cutGam[GeV]"
186 << " cutEle[GeV]"
187 << " vmcCutGam[GeV]"
188 << " vmcCutEle[GeV]" << G4endl;
189 //clang-format on
190}
191
192//_____________________________________________________________________________
193void TG4VRegionsManager::PrintRegionData(std::ostream& output,
194 const G4String& matName, const TG4RegionData& values) const
195{
200
201 auto name = "\'" + matName + "\';";
202
203 // Print all data
204 //clang-format off
205 output << std::setw(30) << std::left << name << " "
206 << std::scientific << values[fgkRangeGamIdx] << " "
207 << std::scientific << values[fgkRangeEleIdx] << " "
208 << std::scientific << values[fgkCutGamIdx] * TG4G3Units::InverseEnergy() << " "
209 << std::scientific << values[fgkCutEleIdx] * TG4G3Units::InverseEnergy() << " "
210 << std::scientific << values[fgkVmcCutGamIdx] * TG4G3Units::InverseEnergy() << " "
211 << std::scientific << values[fgkVmcCutEleIdx] * TG4G3Units::InverseEnergy() << G4endl;
212 //clang-format on
213}
214
215//_____________________________________________________________________________
216void TG4VRegionsManager::PrintFromG4Table(std::ostream& output) const
217{
221
222 G4ProductionCutsTable* productionCutsTable =
223 G4ProductionCutsTable::GetProductionCutsTable();
224
225 if (productionCutsTable->GetTableSize() == 0) {
226 G4cout << "No production cuts defined." << G4endl;
227 return;
228 }
229
230 PrintLegend(output);
231
232 for (G4int i = 0; i < G4int(productionCutsTable->GetTableSize()); i++) {
233 const G4MaterialCutsCouple* couple =
234 productionCutsTable->GetMaterialCutsCouple(i);
235
236 const G4Material* material = couple->GetMaterial();
237 G4ProductionCuts* cuts = couple->GetProductionCuts();
238
239 G4double rangeGam = cuts->GetProductionCut(0);
240 G4double rangeEle = cuts->GetProductionCut(1);
241 // if ( couple->IsRecalcNeeded() ) {
242 // TG4Globals::Warning("TG4VRegionsManager", "PrintProductionCuts",
243 // "Recalculation is needed - the energy cuts may be wrong");
244 // }
245
246 const std::vector<G4double>* energyCutsGam =
247 productionCutsTable->GetEnergyCutsVector(0);
248 const std::vector<G4double>* energyCutsEle =
249 productionCutsTable->GetEnergyCutsVector(1);
250
251 G4double cutGam = (*energyCutsGam)[couple->GetIndex()];
252 G4double cutEle = (*energyCutsEle)[couple->GetIndex()];
253
254 // Get limits via material
255 TG4Limits* limits =
256 TG4GeometryServices::Instance()->FindLimits(material, true);
257
258 G4double cutGamLimits = DBL_MAX;
259 G4double cutEleLimits = DBL_MAX;
260 if (!limits) {
261 TG4Globals::Warning("TG4VRegionsManager", "CheckRegions",
262 "Limits for material " + TString(material->GetName()) + " not found. " +
264 }
265 else {
266 cutGamLimits = GetEnergyCut(limits, kCUTGAM, DBL_MAX);
267 cutEleLimits = GetEnergyCut(limits, kCUTELE, DBL_MAX);
268 }
269
270 G4String matName = material->GetName();
271 TG4RegionData values =
272 {rangeGam, rangeEle, cutGam, cutEle, cutGamLimits, cutEleLimits};
273
274 PrintRegionData(output, matName, values);
275 }
276}
277
278//
279// public methods
280//
281
282//_____________________________________________________________________________
283void TG4VRegionsManager::PrintRegions(std::ostream& output) const
284{
286
287 PrintFromG4Table(output);
288}
289
290//_____________________________________________________________________________
297
298//_____________________________________________________________________________
300{
302
303 // Open file
304 auto fileName = fFileName.empty() ? fgkDefaultFileName : fFileName;
305 std::ofstream fileOutput;
306 fileOutput.open(fileName, std::ios::out);
307 if (! fileOutput.is_open()) {
308 TG4Globals::Warning("TG4VRegionsManager", "SaveRegions",
309 "Saving regions in file " + TString(fileName.data()) + " has failed.");
310 return;
311 }
312
313 if (VerboseLevel() > 0) {
314 G4cout << "Saving regions from production cuts table in file: " << fileName << G4endl;
315 }
316
317 PrintRegions(fileOutput);
318 fileOutput.close();
319}
320
321//_____________________________________________________________________________
323{
326
327 auto regionStore = G4RegionStore::GetInstance();
328
329 G4cout << "========= Region Store Dump ======================================"
330 << G4endl;
331
332 for (auto region : *regionStore ) {
333 G4cout << region->GetName() << ":" << G4endl;
334
335 auto cuts = region->GetProductionCuts();
336 if (cuts != nullptr) {
337 auto rangeGam = cuts->GetProductionCut(0);
338 auto rangeEle = cuts->GetProductionCut(1);
339 G4cout << " ProductionCuts: rangeGam=" << rangeGam << " rangeEle=" << rangeEle << G4endl;
340 }
341
342 size_t lvCounter = 0;
343 auto lvIt = region->GetRootLogicalVolumeIterator();
344 G4cout << " RootVolumes: ";
345 while (lvCounter < region->GetNumberOfRootVolumes()) {
346 G4cout << " " << (*lvIt++)->GetName() << "; ";
347 ++lvCounter;
348 }
349 G4cout << G4endl;;
350
351 size_t matCounter = 0;
352 auto matIt = region->GetMaterialIterator();
353 G4cout << " Materials: ";
354 while (matCounter < region->GetNumberOfMaterials()) {
355 G4cout << " " << (*matIt)->GetName() << "; ";;
356 ++matCounter;
357 }
358 G4cout << G4endl;;
359 }
360
361 G4cout << "========= End Region Store Dump =================================="
362 << G4endl;
363}
Definition of the TG4G3CutVector class.
Definition of the TG4G3PhysicsManager class.
Definition of the TG4G3Units class.
Definition of the TG4GeometryServices class.
Definition of the TG4Globals class and basic container types.
Definition of the TG4Limits class.
Definition of the TG4Medium class.
Definition of the TG4RegionsMessenger class.
Definition of the TG4VRegionsManager class.
Vector of kinetic energy cut values with convenient set/get methods.
static const G4String & GetCutName(TG4G3Cut cut)
static TG4G3Defaults * Instance()
G4double CutValue(G4int cut) const
static TG4G3PhysicsManager * Instance()
TG4boolVector * GetIsCutVector() const
TG4G3CutVector * GetCutVector() const
static G4double InverseEnergy()
Definition TG4G3Units.h:157
TG4Limits * FindLimits(const G4String &name, G4bool silent=false) const
static TG4GeometryServices * Instance()
TG4MediumMap * GetMediumMap() const
static void Warning(const TString &className, const TString &methodName, const TString &text)
static TString Endl()
Definition TG4Globals.h:100
Extended G4UserLimits class.
Definition TG4Limits.h:38
G4String GetName() const
Definition TG4Limits.h:129
const TG4G3CutVector * GetCutVector() const
Definition TG4Limits.h:141
TG4Medium * GetMedium(G4int mediumID, G4bool warn=true) const
Helper class to keep medium data.
Definition TG4Medium.h:29
Base class for mangers for converting VMC cuts in energy in G4 regions.
static constexpr size_t fgkRangeGamIdx
void PrintLegend(std::ostream &output) const
void PrintRegionData(std::ostream &output, const G4String &matName, const TG4RegionData &values) const
static const G4String fgkDefaultRegionName
the name of the region with default cuts
static const G4String fgkDefaultFileName
the name of the region with default cuts
static TG4VRegionsManager * fgInstance
the singleton instance
static constexpr size_t fgkCutGamIdx
G4double GetEnergyCut(TG4Limits *limits, TG4G3Cut cutType, G4double globalCutValue) const
G4bool IsCoupleUsedInTheRegion(const G4MaterialCutsCouple *couple, const G4Region *region) const
std::array< G4double, fgkValuesSize > TG4RegionData
static constexpr size_t fgkCutEleIdx
static constexpr size_t fgkVmcCutGamIdx
void PrintFromG4Table(std::ostream &output) const
void CheckRegionsInGeometry() const
G4double GetGlobalEnergyCut(TG4G3Cut cutType) const
virtual void DumpRegionStore() const
static constexpr size_t fgkRangeEleIdx
G4String fFileName
file name for regions output
virtual void CheckRegions() const
virtual void PrintRegions(std::ostream &output) const
static constexpr size_t fgkVmcCutEleIdx
Base class for defining the verbose level and a common messenger.
Definition TG4Verbose.h:36
virtual G4int VerboseLevel() const
Definition TG4Verbose.h:78
TG4G3Cut
Enumeration for G3 types of kinetic energy cuts.
Definition TG4G3Cut.h:22
std::vector< G4bool > TG4boolVector
Definition TG4Globals.h:37
@ kCUTGAM
Definition TG4G3Cut.h:27
@ kCUTELE
Definition TG4G3Cut.h:32