Geant4 VMC Version 6.8
Loading...
Searching...
No Matches
TG4MediumMap.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Geant4 Virtual Monte Carlo package
3// Copyright (C) 2007 - 2014 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 "TG4MediumMap.h"
16#include "TG4GeometryServices.h"
17#include "TG4Globals.h"
18#include "TG4Medium.h"
19
20#include <G4LogicalVolume.hh>
21#include <G4Material.hh>
22
23#include <iomanip>
24
25//_____________________________________________________________________________
30
31//_____________________________________________________________________________
33{
35
36 // Delete medium objects
37 std::map<G4int, TG4Medium*>::const_iterator it;
38 for (it = fIdMap.begin(); it != fIdMap.end(); ++it) delete it->second;
39}
40
41//
42// public methods
43//
44
45//_____________________________________________________________________________
46TG4Medium* TG4MediumMap::AddMedium(G4int mediumID, G4bool warn)
47{
50
51 TG4Medium* medium = GetMedium(mediumID, false);
52
53 if (medium) {
54 if (warn) {
55 TString text = "Medium with given ID=";
56 text += mediumID;
57 text += " already exists.";
58 TG4Globals::Warning("TG4MediumMap", "AddMedium", text);
59 }
60 return medium;
61 }
62
63 medium = new TG4Medium(mediumID);
64 fIdMap[mediumID] = medium;
65 return medium;
66}
67
68//_____________________________________________________________________________
69void TG4MediumMap::MapMedium(G4LogicalVolume* lv, G4int mediumID)
70{
73
74 TG4Medium* medium = GetMedium(mediumID);
75
76 if (!medium) {
77 TString text = "Medium with given ID=";
78 text += mediumID;
79 text += " not defined.";
80 TG4Globals::Warning("TG4MediumMap", "MapMedium", text);
81 return;
82 }
83
84 fLVMap[lv] = medium;
85
86 if (fMaterialMap.find(lv->GetMaterial()) == fMaterialMap.end()) {
87 fMaterialMap[lv->GetMaterial()] = medium;
88 }
89}
90
91//_____________________________________________________________________________
92void TG4MediumMap::MapMedium(const G4String& lvName, G4int mediumID)
93{
96
97 auto volumes = TG4GeometryServices::Instance()->FindLogicalVolumes(lvName);
98
99 if (volumes.empty()) {
100 TG4Globals::Warning("TG4MediumMap", "MapMedium",
101 "Logical volume " + TString(lvName) + " not defined.");
102 return;
103 }
104
105 for (auto lv : volumes) MapMedium(lv, mediumID);
106}
107
108//_____________________________________________________________________________
110{
112
113 if (fIdMap.size()) {
114 G4cout << "Dump of TG4MediumMap - " << fIdMap.size()
115 << " entries:" << G4endl;
116 G4int counter = 0;
117 std::map<G4int, TG4Medium*>::const_iterator it;
118 for (it = fIdMap.begin(); it != fIdMap.end(); ++it) {
119 G4cout << "Map element " << std::setw(3) << counter++ << " ";
120 it->second->Print();
121 G4cout << G4endl;
122 }
123 }
124}
125
126//_____________________________________________________________________________
128{
130
131 return fIdMap.size();
132}
133
134//_____________________________________________________________________________
135TG4Medium* TG4MediumMap::GetMedium(G4int mediumID, G4bool warn) const
136{
138
139 std::map<G4int, TG4Medium*>::const_iterator it = fIdMap.find(mediumID);
140
141 if (it == fIdMap.end()) {
142 if (warn) {
143 TString text = "Medium with given ID=";
144 text += mediumID;
145 text += " not defined.";
146 TG4Globals::Warning("TG4MediumMap", "GetMedium", text);
147 }
148 return 0;
149 }
150
151 return it->second;
152}
153
154//_____________________________________________________________________________
155TG4Medium* TG4MediumMap::GetMedium(const G4String& name, G4bool warn) const
156{
158
159 std::map<G4int, TG4Medium*>::const_iterator it;
160 for (it = fIdMap.begin(); it != fIdMap.end(); ++it)
161 if (it->second->GetName() == name) return it->second;
162
163 // Give warning if not found
164 if (warn) {
165 TG4Globals::Warning("TG4MediumMap", "GetMedium",
166 "Medium " + TString(name) + " is not found.");
167 }
168 return 0;
169}
170
171//_____________________________________________________________________________
172TG4Medium* TG4MediumMap::GetMedium(G4LogicalVolume* lv, G4bool warn) const
173{
175
176 std::map<G4LogicalVolume*, TG4Medium*>::const_iterator it = fLVMap.find(lv);
177
178 if (it == fLVMap.end()) {
179 if (warn) {
180 TG4Globals::Warning("TG4MediumMap", "GetMedium",
181 "Medium for LV " + TString(lv->GetName()) + " is not found.");
182 }
183 return 0;
184 }
185
186 return it->second;
187}
188
189//_____________________________________________________________________________
191 const G4Material* material, G4bool warn) const
192{
194
195 std::map<const G4Material*, TG4Medium*>::const_iterator it =
196 fMaterialMap.find(material);
197
198 if (it == fMaterialMap.end()) {
199 if (warn) {
200 TG4Globals::Warning("TG4MediumMap", "GetMedium",
201 "Medium for material " + TString(material->GetName()) +
202 " is not found.");
203 }
204 return 0;
205 }
206
207 return it->second;
208}
209
210//_____________________________________________________________________________
211void TG4MediumMap::GetMedia(const G4String& namePattern,
212 std::vector<TG4Medium*>& media, G4bool warn) const
213{
216
217 // Strip pattern
218 std::string pattern = namePattern;
219 if (pattern.find("*") != std::string::npos) {
220 pattern.erase(pattern.find("*"));
221 }
222
223 G4bool found = false;
224 std::map<G4int, TG4Medium*>::const_iterator it;
225 for (it = fIdMap.begin(); it != fIdMap.end(); ++it) {
226 if (it->second->GetName().find(pattern) != std::string::npos) {
227 media.push_back(it->second);
228 found = true;
229 }
230 }
231
232 // Give warning if not found
233 if (warn && (!found)) {
234 TG4Globals::Warning("TG4MediumMap", "GetMedia",
235 "No medium with name pattern " + TString(namePattern) + " was found.");
236 }
237}
Definition of the TG4GeometryServices class.
Definition of the TG4Globals class and basic container types.
Definition of the TG4MediumMap class.
Definition of the TG4Medium class.
static TG4GeometryServices * Instance()
std::vector< G4LogicalVolume * > FindLogicalVolumes(const G4String &name, G4bool silent=false) const
static void Warning(const TString &className, const TString &methodName, const TString &text)
void MapMedium(G4LogicalVolume *lv, G4int mediumID)
TG4Medium * AddMedium(G4int mediumID, G4bool warn=true)
std::map< G4int, TG4Medium * > fIdMap
map of medias to their IDs
G4int GetNofMedia() const
TG4Medium * GetMedium(G4int mediumID, G4bool warn=true) const
void Print() const
std::map< const G4Material *, TG4Medium * > fMaterialMap
map of materials to medias
std::map< G4LogicalVolume *, TG4Medium * > fLVMap
map of medias to the logical volumes
void GetMedia(const G4String &namePattern, std::vector< TG4Medium * > &media, G4bool warn=true) const
Helper class to keep medium data.
Definition TG4Medium.h:29