Geant4  10.01.p02
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: G4SDStructure.cc 74576 2013-10-15 07:20:11Z gcosmo $
28 //
29 
30 // G4SDStructure
31 #include "G4SDStructure.hh"
32 #include "G4ios.hh"
33 
34 G4SDStructure::G4SDStructure(G4String aPath):verboseLevel(0)
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)
85  { detector.push_back( aSD ); }
86  else if( tgtSD != aSD )
87  {
88 #ifdef G4VERBOSE
90  ed << aSD->GetName() << " had already been stored in "
91  << pathName << ". Object pointer is overwitten.\n";
92  ed << "It's users' responsibility to delete the old sensitive detector object.";
93  G4Exception("G4SDStructure::AddNewDetector()","DET1010",JustWarning,ed);
94 #endif
95  RemoveSD( tgtSD );
96  detector.push_back( aSD );
97  }
98  }
99 }
100 
102 {
103  for( size_t i=0; i<structure.size(); i++ )
104  {
105  if( subD == structure[i]->dirName ) return structure[i];
106  }
107  return 0;
108 }
109 
111 {
112  for( size_t i=0; i<detector.size(); i++ )
113  {
114  G4VSensitiveDetector* tgtSD = detector[i];
115  if( aSDName == tgtSD->GetName() ) return tgtSD;
116  }
117  return 0;
118 }
119 
121 {
122  std::vector<G4VSensitiveDetector*>::iterator itr = detector.begin();
123  for(;itr!=detector.end();itr++)
124  {
125  if((*itr)==sd) {
126  detector.erase(itr);
127  break;
128  }
129  }
130 }
131 
133 {
134  G4String subD = aName;
135  G4int i = aName.first('/');
136  if( i != G4int(std::string::npos) ) subD.remove(i+1);
137  return subD;
138 }
139 
140 void G4SDStructure::Activate(G4String aName, G4bool sensitiveFlag)
141 {
142  G4String aPath = aName;
143  aPath.remove(0,pathName.length());
144  if( aPath.first('/') != G4int(std::string::npos) )
145  { // Command is ordered for a subdirectory.
146  G4String subD = ExtractDirName(aPath);
147  G4SDStructure* tgtSDS = FindSubDirectory(subD);
148  if( tgtSDS == 0 )
149  { // The subdirectory is not found
150  G4cout << subD << " is not found in " << pathName << G4endl;
151  }
152  else
153  {
154  tgtSDS->Activate(aName,sensitiveFlag);
155  }
156  }
157  else if( aPath.isNull() )
158  { // Command is ordered for all detectors in this directory.
159  for( size_t i=0; i<detector.size(); i++)
160  {
161  detector[i]->Activate(sensitiveFlag);
162  }
163  for( size_t j=0;j<structure.size(); j++)
164  {
165  structure[j]->Activate(G4String("/"),sensitiveFlag);
166  }
167  }
168  else
169  { // Command is ordered to a particular detector.
170  G4VSensitiveDetector* tgtSD = GetSD(aPath);
171  if( tgtSD == 0 )
172  { // The detector is not found.
173  G4cout << aPath << " is not found in " << pathName << G4endl;
174  }
175  else
176  {
177  tgtSD->Activate(sensitiveFlag);
178  }
179  }
180 }
181 
183 {
184  G4String aPath = aName;
185  aPath.remove(0,pathName.length());
186  if( aPath.first('/') != G4int(std::string::npos) )
187  { // SD exists in sub-directory
188  G4String subD = ExtractDirName(aPath);
189  G4SDStructure* tgtSDS = FindSubDirectory(subD);
190  if( tgtSDS == 0 )
191  { // The subdirectory is not found
192  if (warning)
193  G4cout << subD << " is not found in " << pathName << G4endl;
194  return 0;
195  }
196  else
197  {
198  return tgtSDS->FindSensitiveDetector(aName);
199  }
200  }
201  else
202  { // SD must exist in this directory
203  G4VSensitiveDetector* tgtSD = GetSD(aPath);
204  if( tgtSD == 0 )
205  { // The detector is not found.
206  if (warning)
207  G4cout << aPath << " is not found in " << pathName << G4endl;
208  }
209  return tgtSD;
210  }
211 }
212 
214 {
215  size_t i;
216  // Broadcast to subdirectories.
217  for( i=0; i<structure.size(); i++ )
218  {
219  structure[i]->Initialize(HCE);
220  }
221  // Initialize all detectors in this directory.
222  for( i=0; i<detector.size(); i++ )
223  {
224  if(detector[i]->isActive()) detector[i]->Initialize(HCE);
225  }
226 }
227 
229 {
230  size_t i;
231  // Broadcast to subdirectories.
232  for( i=0; i<structure.size(); i++ )
233  {
234  structure[i]->Terminate(HCE);
235  }
236  // Initialize all detectors in this directory.
237  for( i=0; i<detector.size(); i++ )
238  {
239  if(detector[i]->isActive()) detector[i]->EndOfEvent(HCE);
240  }
241 }
242 
244 {
245  G4cout << pathName << G4endl;
246  for(size_t i=0; i<detector.size(); i++)
247  {
249  G4cout << pathName << sd->GetName();
250  if( sd->isActive() )
251  { G4cout << " *** Active "; }
252  else
253  { G4cout << " XXX Inactive "; }
254  G4cout << G4endl;
255  }
256  for(size_t j=0; j<structure.size(); j++)
257  { structure[j]->ListTree(); }
258 }
259 
260 
void AddNewDetector(G4VSensitiveDetector *aSD, G4String treeStructure)
void Activate(G4bool activeFlag)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String & remove(str_size)
G4String pathName
G4int first(char) const
void RemoveSD(G4VSensitiveDetector *)
void Initialize(G4HCofThisEvent *HCE)
std::vector< G4VSensitiveDetector * > detector
void Activate(G4String aName, G4bool sensitiveFlag)
int G4int
Definition: G4Types.hh:78
G4String & prepend(const char *)
G4GLOB_DLL std::ostream G4cout
void Terminate(G4HCofThisEvent *HCE)
bool G4bool
Definition: G4Types.hh:79
G4int operator==(const G4SDStructure &right) const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4SDStructure * FindSubDirectory(G4String subD)
G4VSensitiveDetector * GetSD(G4String aName)
G4SDStructure(G4String aPath)
G4String dirName
G4int last(char) const
G4String ExtractDirName(G4String aPath)
#define G4endl
Definition: G4ios.hh:61
G4bool isNull() const
std::vector< G4SDStructure * > structure
G4VSensitiveDetector * FindSensitiveDetector(G4String aName, G4bool warning=true)