Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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
33G4SDStructure::G4SDStructure(const G4String &aPath):verboseLevel(0)
34{
35 pathName = aPath;
36 dirName = aPath;
37 G4int i = dirName.length();
38 if( i > 1 )
39 {
40 dirName.remove(i-1);
41 G4int isl = dirName.last('/');
42 dirName.remove(0,isl+1);
43 dirName += "/";
44 }
45}
46
48{
49 for(auto st : structure) delete st;
50 structure.clear();
51 for(auto dt : detector) delete dt;
52 detector.clear();
53}
54
56{
57 return (this==&right);
58}
59
61 const G4String &treeStructure)
62{
63 G4String remainingPath = treeStructure;
64 remainingPath.remove(0,pathName.length());
65 if( ! remainingPath.isNull() )
66 { // The detector should be kept in subdirectory.
67 // First, check if the subdirectoy exists.
68 G4String subD = ExtractDirName( remainingPath );
69 G4SDStructure* tgtSDS = FindSubDirectory(subD);
70 if( tgtSDS == nullptr )
71 { // Subdirectory not found. Create a new directory.
72 subD.prepend(pathName);
73 tgtSDS = new G4SDStructure(subD);
74 structure.push_back( tgtSDS );
75 }
76 tgtSDS->AddNewDetector(aSD,treeStructure);
77 }
78 else
79 { // The sensitive detector should be kept in this directory.
80 G4VSensitiveDetector* tgtSD = GetSD( aSD->GetName() );
81 if(!tgtSD)
82 { detector.push_back( aSD ); }
83 else if( tgtSD != aSD )
84 {
85#ifdef G4VERBOSE
87 ed << aSD->GetName() << " had already been stored in "
88 << pathName << ". Object pointer is overwritten.\n";
89 ed << "It's users' responsibility to delete the old sensitive detector object.";
90 G4Exception("G4SDStructure::AddNewDetector()","DET1010",JustWarning,ed);
91#endif
92 RemoveSD( tgtSD );
93 detector.push_back( aSD );
94 }
95 }
96}
97
98G4SDStructure* G4SDStructure::FindSubDirectory(const G4String &subD)
99{
100 for(auto st : structure)
101 { if( subD == st->dirName ) return st; }
102 return nullptr;
103}
104
106{
107 for(auto det : detector)
108 { if(aSDName == det->GetName()) return det; }
109 return nullptr;
110}
111
112void G4SDStructure::RemoveSD(G4VSensitiveDetector* sd)
113{
114 auto det = std::find(detector.begin(), detector.end(), sd);
115 if(det!=detector.end()) detector.erase(det);
116}
117
118G4String G4SDStructure::ExtractDirName(const G4String &aName)
119{
120 G4String subD = aName;
121 G4int i = aName.first('/');
122 if( i != G4int(std::string::npos) ) subD.remove(i+1);
123 return subD;
124}
125
126void G4SDStructure::Activate(const G4String &aName, G4bool sensitiveFlag)
127{
128 G4String aPath = aName;
129 aPath.remove(0,pathName.length());
130 if( aPath.first('/') != std::string::npos )
131 { // Command is ordered for a subdirectory.
132 G4String subD = ExtractDirName(aPath);
133 G4SDStructure* tgtSDS = FindSubDirectory(subD);
134 if( tgtSDS == nullptr )
135 { // The subdirectory is not found
136 G4cout << subD << " is not found in " << pathName << G4endl;
137 }
138 else
139 {
140 tgtSDS->Activate(aName,sensitiveFlag);
141 }
142 }
143 else if( aPath.isNull() )
144 { // Command is ordered for all detectors in this directory.
145 for(auto det : detector) det->Activate(sensitiveFlag);
146 for(auto st : structure) st->Activate(G4String("/"),sensitiveFlag);
147 }
148 else
149 { // Command is ordered to a particular detector.
150 G4VSensitiveDetector* tgtSD = GetSD(aPath);
151 if( tgtSD == nullptr )
152 { // The detector is not found.
153 G4cout << aPath << " is not found in " << pathName << G4endl;
154 }
155 else
156 {
157 tgtSD->Activate(sensitiveFlag);
158 }
159 }
160}
161
163{
164 G4String aPath = aName;
165 aPath.remove(0,pathName.length());
166 if( aPath.first('/') != std::string::npos )
167 { // SD exists in sub-directory
168 G4String subD = ExtractDirName(aPath);
169 G4SDStructure* tgtSDS = FindSubDirectory(subD);
170 if( tgtSDS == nullptr )
171 { // The subdirectory is not found
172 if (warning)
173 G4cout << subD << " is not found in " << pathName << G4endl;
174 return nullptr;
175 }
176 else
177 {
178 return tgtSDS->FindSensitiveDetector(aName,warning);
179 }
180 }
181 else
182 { // SD must exist in this directory
183 G4VSensitiveDetector* tgtSD = GetSD(aPath);
184 if( tgtSD == nullptr )
185 { // The detector is not found.
186 if (warning)
187 G4cout << aPath << " is not found in " << pathName << G4endl;
188 }
189 return tgtSD;
190 }
191}
192
194{
195 // Broadcast to subdirectories.
196 for(auto st : structure)
197 { st->Initialize(HCE); }
198 // Initialize all detectors in this directory.
199 for(auto dt : detector)
200 { if(dt->isActive()) dt->Initialize(HCE); }
201}
202
204{
205 // Broadcast to subdirectories.
206 for(auto st : structure)
207 { st->Terminate(HCE); }
208 // Terminate all detectors in this directory.
209 for(auto dt : detector)
210 { if(dt->isActive()) dt->EndOfEvent(HCE); }
211}
212
214{
215 G4cout << pathName << G4endl;
216 for(auto sd : detector)
217 {
218 G4cout << pathName << sd->GetName();
219 if( sd->isActive() )
220 { G4cout << " *** Active "; }
221 else
222 { G4cout << " XXX Inactive "; }
223 G4cout << G4endl;
224 }
225 for(auto st : structure) st->ListTree();
226}
227
228
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#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)
G4String & remove(str_size)
G4String & prepend(const char *)
G4bool isNull() const
std::size_t last(char) const
std::size_t first(char) const
void Activate(G4bool activeFlag)