Geant4 9.6.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// $Id$
28//
29
30// G4SDStructure
31#include "G4SDStructure.hh"
32#include "G4ios.hh"
33
35{
36 pathName = aPath;
37 dirName = aPath;
38 G4int i = dirName.length();
39 if( i > 1 )
40 {
41 dirName.remove(i-1);
42 G4int isl = dirName.last('/');
43 dirName.remove(0,isl+1);
44 dirName += "/";
45 }
46}
47
49{
50 size_t nTree = structure.size();
51 for(size_t iTree=0;iTree<nTree;iTree++)
52 { delete structure[iTree]; }
53 size_t nDet = detector.size();
54 for(size_t iDet=0;iDet<nDet;iDet++)
55 { delete detector[iDet]; }
56}
57
59{
60 return (this==&right);
61}
62
64 G4String treeStructure)
65{
66 G4String remainingPath = treeStructure;
67 remainingPath.remove(0,pathName.length());
68 if( ! remainingPath.isNull() )
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 == 0 )
74 { // Subdirectory not found. Create a new directory.
75 subD.prepend(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 != 0 )
85 {
86 G4cout << aSD->GetName() << " had already stored in "
87 << pathName << G4endl;
88 }
89 else
90 {
91 detector.push_back( aSD );
92 }
93 }
94}
95
96G4SDStructure* G4SDStructure::FindSubDirectory(G4String subD)
97{
98 for( size_t i=0; i<structure.size(); i++ )
99 {
100 if( subD == structure[i]->dirName ) return structure[i];
101 }
102 return 0;
103}
104
106{
107 for( size_t i=0; i<detector.size(); i++ )
108 {
109 G4VSensitiveDetector* tgtSD = detector[i];
110 if( aSDName == tgtSD->GetName() ) return tgtSD;
111 }
112 return 0;
113}
114
115G4String G4SDStructure::ExtractDirName(G4String aName)
116{
117 G4String subD = aName;
118 G4int i = aName.first('/');
119 if( i != G4int(std::string::npos) ) subD.remove(i+1);
120 return subD;
121}
122
123void G4SDStructure::Activate(G4String aName, G4bool sensitiveFlag)
124{
125 G4String aPath = aName;
126 aPath.remove(0,pathName.length());
127 if( aPath.first('/') != G4int(std::string::npos) )
128 { // Command is ordered for a subdirectory.
129 G4String subD = ExtractDirName(aPath);
130 G4SDStructure* tgtSDS = FindSubDirectory(subD);
131 if( tgtSDS == 0 )
132 { // The subdirectory is not found
133 G4cout << subD << " is not found in " << pathName << G4endl;
134 }
135 else
136 {
137 tgtSDS->Activate(aName,sensitiveFlag);
138 }
139 }
140 else if( aPath.isNull() )
141 { // Command is ordered for all detectors in this directory.
142 for( size_t i=0; i<detector.size(); i++)
143 {
144 detector[i]->Activate(sensitiveFlag);
145 }
146 for( size_t j=0;j<structure.size(); j++)
147 {
148 structure[j]->Activate(G4String("/"),sensitiveFlag);
149 }
150 }
151 else
152 { // Command is ordered to a particular detector.
153 G4VSensitiveDetector* tgtSD = GetSD(aPath);
154 if( tgtSD == 0 )
155 { // The detector is not found.
156 G4cout << aPath << " is not found in " << pathName << G4endl;
157 }
158 else
159 {
160 tgtSD->Activate(sensitiveFlag);
161 }
162 }
163}
164
166{
167 G4String aPath = aName;
168 aPath.remove(0,pathName.length());
169 if( aPath.first('/') != G4int(std::string::npos) )
170 { // SD exists in sub-directory
171 G4String subD = ExtractDirName(aPath);
172 G4SDStructure* tgtSDS = FindSubDirectory(subD);
173 if( tgtSDS == 0 )
174 { // The subdirectory is not found
175 if (warning)
176 G4cout << subD << " is not found in " << pathName << G4endl;
177 return 0;
178 }
179 else
180 {
181 return tgtSDS->FindSensitiveDetector(aName);
182 }
183 }
184 else
185 { // SD must exist in this directory
186 G4VSensitiveDetector* tgtSD = GetSD(aPath);
187 if( tgtSD == 0 )
188 { // The detector is not found.
189 if (warning)
190 G4cout << aPath << " is not found in " << pathName << G4endl;
191 }
192 return tgtSD;
193 }
194}
195
197{
198 size_t i;
199 // Broadcast to subdirectories.
200 for( i=0; i<structure.size(); i++ )
201 {
202 structure[i]->Initialize(HCE);
203 }
204 // Initialize all detectors in this directory.
205 for( i=0; i<detector.size(); i++ )
206 {
207 if(detector[i]->isActive()) detector[i]->Initialize(HCE);
208 }
209}
210
212{
213 size_t i;
214 // Broadcast to subdirectories.
215 for( i=0; i<structure.size(); i++ )
216 {
217 structure[i]->Terminate(HCE);
218 }
219 // Initialize all detectors in this directory.
220 for( i=0; i<detector.size(); i++ )
221 {
222 if(detector[i]->isActive()) detector[i]->EndOfEvent(HCE);
223 }
224}
225
227{
228 G4cout << pathName << G4endl;
229 for(size_t i=0; i<detector.size(); i++)
230 {
231 G4VSensitiveDetector* sd = detector[i];
232 G4cout << pathName << sd->GetName();
233 if( sd->isActive() )
234 { G4cout << " *** Active "; }
235 else
236 { G4cout << " XXX Inactive "; }
237 G4cout << G4endl;
238 }
239 for(size_t j=0; j<structure.size(); j++)
240 { structure[j]->ListTree(); }
241}
242
243
int G4int
Definition: G4Types.hh:66
bool G4bool
Definition: G4Types.hh:67
#define G4endl
Definition: G4ios.hh:52
G4DLLIMPORT std::ostream G4cout
void Initialize(G4HCofThisEvent *HCE)
void Terminate(G4HCofThisEvent *HCE)
void Activate(G4String aName, G4bool sensitiveFlag)
void AddNewDetector(G4VSensitiveDetector *aSD, G4String treeStructure)
G4int operator==(const G4SDStructure &right) const
G4VSensitiveDetector * GetSD(G4String aName)
G4VSensitiveDetector * FindSensitiveDetector(G4String aName, G4bool warning=true)
G4SDStructure(G4String aPath)
G4String & remove(str_size)
G4String & prepend(const char *)
G4bool isNull() const
G4int first(char) const
G4int last(char) const
void Activate(G4bool activeFlag)