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