Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4SDStructure.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27//
28
29// G4SDStructure
30#include "G4SDStructure.hh"
31#include "G4ios.hh"
32
34 : verboseLevel(0)
35{
36 pathName = aPath;
37 dirName = aPath;
38 auto i = dirName.length();
39 if(i > 1)
40 {
41 dirName.erase(i - 1);
42 auto isl = dirName.rfind('/');
43 dirName.erase(0, isl + 1);
44 dirName += "/";
45 }
46}
47
49{
50 for(auto st : structure)
51 delete st;
52 structure.clear();
53 for(auto dt : detector)
54 delete dt;
55 detector.clear();
56}
57
59{
60 return (this == &right);
61}
62
64 const G4String& treeStructure)
65{
66 G4String remainingPath = treeStructure;
67 remainingPath.erase(0, pathName.length());
68 if(!remainingPath.empty())
69 { // The detector should be kept in subdirectory.
70 // First, check if the subdirectoy exists.
71 G4String subD = ExtractDirName(remainingPath);
72 G4SDStructure* tgtSDS = FindSubDirectory(subD);
73 if(tgtSDS == nullptr)
74 { // Subdirectory not found. Create a new directory.
75 subD.insert(0, pathName);
76 tgtSDS = new G4SDStructure(subD);
77 structure.push_back(tgtSDS);
78 }
79 tgtSDS->AddNewDetector(aSD, treeStructure);
80 }
81 else
82 { // The sensitive detector should be kept in this directory.
83 G4VSensitiveDetector* tgtSD = GetSD(aSD->GetName());
84 if(!tgtSD)
85 {
86 detector.push_back(aSD);
87 }
88 else if(tgtSD != aSD)
89 {
90#ifdef G4VERBOSE
92 ed << aSD->GetName() << " had already been stored in " << pathName
93 << ". Object pointer is overwritten.\n";
94 ed << "It's users' responsibility to delete the old sensitive detector "
95 "object.";
96 G4Exception("G4SDStructure::AddNewDetector()", "DET1010", JustWarning,
97 ed);
98#endif
99 RemoveSD(tgtSD);
100 detector.push_back(aSD);
101 }
102 }
103}
104
105G4SDStructure* G4SDStructure::FindSubDirectory(const G4String& subD)
106{
107 for(auto st : structure)
108 {
109 if(subD == st->dirName)
110 return st;
111 }
112 return nullptr;
113}
114
116{
117 for(auto det : detector)
118 {
119 if(aSDName == det->GetName())
120 return det;
121 }
122 return nullptr;
123}
124
125void G4SDStructure::RemoveSD(G4VSensitiveDetector* sd)
126{
127 auto det = std::find(detector.begin(), detector.end(), sd);
128 if(det != detector.end())
129 detector.erase(det);
130}
131
132G4String G4SDStructure::ExtractDirName(const G4String& aName)
133{
134 G4String subD = aName;
135 auto i = aName.find('/');
136 if(i != G4String::npos)
137 subD.erase(i + 1);
138 return subD;
139}
140
141void G4SDStructure::Activate(const G4String& aName, G4bool sensitiveFlag)
142{
143 G4String aPath = aName;
144 aPath.erase(0, pathName.length());
145 if(aPath.find('/') != std::string::npos)
146 { // Command is ordered for a subdirectory.
147 G4String subD = ExtractDirName(aPath);
148 G4SDStructure* tgtSDS = FindSubDirectory(subD);
149 if(tgtSDS == nullptr)
150 { // The subdirectory is not found
151 G4cout << subD << " is not found in " << pathName << G4endl;
152 }
153 else
154 {
155 tgtSDS->Activate(aName, sensitiveFlag);
156 }
157 }
158 else if(aPath.empty())
159 { // Command is ordered for all detectors in this directory.
160 for(auto det : detector)
161 det->Activate(sensitiveFlag);
162 for(auto st : structure)
163 st->Activate(G4String("/"), sensitiveFlag);
164 }
165 else
166 { // Command is ordered to a particular detector.
167 G4VSensitiveDetector* tgtSD = GetSD(aPath);
168 if(tgtSD == nullptr)
169 { // The detector is not found.
170 G4cout << aPath << " is not found in " << pathName << G4endl;
171 }
172 else
173 {
174 tgtSD->Activate(sensitiveFlag);
175 }
176 }
177}
178
180 const G4String& aName, G4bool warning)
181{
182 G4String aPath = aName;
183 aPath.erase(0, pathName.length());
184 if(aPath.find('/') != std::string::npos)
185 { // SD exists in sub-directory
186 G4String subD = ExtractDirName(aPath);
187 G4SDStructure* tgtSDS = FindSubDirectory(subD);
188 if(tgtSDS == nullptr)
189 { // The subdirectory is not found
190 if(warning)
191 G4cout << subD << " is not found in " << pathName << G4endl;
192 return nullptr;
193 }
194 else
195 {
196 return tgtSDS->FindSensitiveDetector(aName, warning);
197 }
198 }
199 else
200 { // SD must exist in this directory
201 G4VSensitiveDetector* tgtSD = GetSD(aPath);
202 if(tgtSD == nullptr)
203 { // The detector is not found.
204 if(warning)
205 G4cout << aPath << " is not found in " << pathName << G4endl;
206 }
207 return tgtSD;
208 }
209}
210
212{
213 // Broadcast to subdirectories.
214 for(auto st : structure)
215 {
216 st->Initialize(HCE);
217 }
218 // Initialize all detectors in this directory.
219 for(auto dt : detector)
220 {
221 if(dt->isActive())
222 dt->Initialize(HCE);
223 }
224}
225
227{
228 // Broadcast to subdirectories.
229 for(auto st : structure)
230 {
231 st->Terminate(HCE);
232 }
233 // Terminate all detectors in this directory.
234 for(auto dt : detector)
235 {
236 if(dt->isActive())
237 dt->EndOfEvent(HCE);
238 }
239}
240
242{
243 G4cout << pathName << G4endl;
244 for(auto sd : detector)
245 {
246 G4cout << pathName << sd->GetName();
247 if(sd->isActive())
248 {
249 G4cout << " *** Active ";
250 }
251 else
252 {
253 G4cout << " XXX Inactive ";
254 }
255 G4cout << G4endl;
256 }
257 for(auto st : structure)
258 st->ListTree();
259}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:59
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
bool G4bool
Definition: G4Types.hh:86
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
void Initialize(G4HCofThisEvent *HCE)
G4bool operator==(const G4SDStructure &right) const
G4VSensitiveDetector * GetSD(const G4String &aName)
void Activate(const G4String &aName, G4bool sensitiveFlag)
void Terminate(G4HCofThisEvent *HCE)
G4VSensitiveDetector * FindSensitiveDetector(const G4String &aName, G4bool warning=true)
G4SDStructure(const G4String &aPath)
void AddNewDetector(G4VSensitiveDetector *aSD, const G4String &treeStructure)
void Activate(G4bool activeFlag)