Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 // $Id: G4SDStructure.cc 94771 2015-12-09 09:44:05Z 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  for(auto st : structure) delete st;
51  structure.clear();
52  for(auto dt : detector) delete dt;
53  detector.clear();
54 }
55 
57 {
58  return (this==&right);
59 }
60 
62  G4String treeStructure)
63 {
64  G4String remainingPath = treeStructure;
65  remainingPath.remove(0,pathName.length());
66  if( ! remainingPath.isNull() )
67  { // The detector should be kept in subdirectory.
68  // First, check if the subdirectoy exists.
69  G4String subD = ExtractDirName( remainingPath );
70  G4SDStructure* tgtSDS = FindSubDirectory(subD);
71  if( tgtSDS == nullptr )
72  { // Subdirectory not found. Create a new directory.
73  subD.prepend(pathName);
74  tgtSDS = new G4SDStructure(subD);
75  structure.push_back( tgtSDS );
76  }
77  tgtSDS->AddNewDetector(aSD,treeStructure);
78  }
79  else
80  { // The sensitive detector should be kept in this directory.
81  G4VSensitiveDetector* tgtSD = GetSD( aSD->GetName() );
82  if(!tgtSD)
83  { detector.push_back( aSD ); }
84  else if( tgtSD != aSD )
85  {
86 #ifdef G4VERBOSE
88  ed << aSD->GetName() << " had already been stored in "
89  << pathName << ". Object pointer is overwitten.\n";
90  ed << "It's users' responsibility to delete the old sensitive detector object.";
91  G4Exception("G4SDStructure::AddNewDetector()","DET1010",JustWarning,ed);
92 #endif
93  RemoveSD( tgtSD );
94  detector.push_back( aSD );
95  }
96  }
97 }
98 
99 G4SDStructure* G4SDStructure::FindSubDirectory(G4String subD)
100 {
101  for(auto st : structure)
102  { if( subD == st->dirName ) return st; }
103  return nullptr;
104 }
105 
107 {
108  for(auto det : detector)
109  { if(aSDName == det->GetName()) return det; }
110  return nullptr;
111 }
112 
113 void G4SDStructure::RemoveSD(G4VSensitiveDetector* sd)
114 {
115  auto det = std::find(detector.begin(), detector.end(), sd);
116  if(det!=detector.end()) detector.erase(det);
117 }
118 
119 G4String G4SDStructure::ExtractDirName(G4String aName)
120 {
121  G4String subD = aName;
122  G4int i = aName.first('/');
123  if( i != G4int(std::string::npos) ) subD.remove(i+1);
124  return subD;
125 }
126 
127 void G4SDStructure::Activate(G4String aName, G4bool sensitiveFlag)
128 {
129  G4String aPath = aName;
130  aPath.remove(0,pathName.length());
131  if( aPath.first('/') != G4int(std::string::npos) )
132  { // Command is ordered for a subdirectory.
133  G4String subD = ExtractDirName(aPath);
134  G4SDStructure* tgtSDS = FindSubDirectory(subD);
135  if( tgtSDS == nullptr )
136  { // The subdirectory is not found
137  G4cout << subD << " is not found in " << pathName << G4endl;
138  }
139  else
140  {
141  tgtSDS->Activate(aName,sensitiveFlag);
142  }
143  }
144  else if( aPath.isNull() )
145  { // Command is ordered for all detectors in this directory.
146  for(auto det : detector) det->Activate(sensitiveFlag);
147  for(auto st : structure) st->Activate(G4String("/"),sensitiveFlag);
148  }
149  else
150  { // Command is ordered to a particular detector.
151  G4VSensitiveDetector* tgtSD = GetSD(aPath);
152  if( tgtSD == nullptr )
153  { // The detector is not found.
154  G4cout << aPath << " is not found in " << pathName << G4endl;
155  }
156  else
157  {
158  tgtSD->Activate(sensitiveFlag);
159  }
160  }
161 }
162 
164 {
165  G4String aPath = aName;
166  aPath.remove(0,pathName.length());
167  if( aPath.first('/') != G4int(std::string::npos) )
168  { // SD exists in sub-directory
169  G4String subD = ExtractDirName(aPath);
170  G4SDStructure* tgtSDS = FindSubDirectory(subD);
171  if( tgtSDS == nullptr )
172  { // The subdirectory is not found
173  if (warning)
174  G4cout << subD << " is not found in " << pathName << G4endl;
175  return nullptr;
176  }
177  else
178  {
179  return tgtSDS->FindSensitiveDetector(aName);
180  }
181  }
182  else
183  { // SD must exist in this directory
184  G4VSensitiveDetector* tgtSD = GetSD(aPath);
185  if( tgtSD == nullptr )
186  { // The detector is not found.
187  if (warning)
188  G4cout << aPath << " is not found in " << pathName << G4endl;
189  }
190  return tgtSD;
191  }
192 }
193 
195 {
196  // Broadcast to subdirectories.
197  for(auto st : structure)
198  { st->Initialize(HCE); }
199  // Initialize all detectors in this directory.
200  for(auto dt : detector)
201  { if(dt->isActive()) dt->Initialize(HCE); }
202 }
203 
205 {
206  // Broadcast to subdirectories.
207  for(auto st : structure)
208  { st->Terminate(HCE); }
209  // Terminate all detectors in this directory.
210  for(auto dt : detector)
211  { if(dt->isActive()) dt->EndOfEvent(HCE); }
212 }
213 
215 {
216  G4cout << pathName << G4endl;
217  for(auto sd : detector)
218  {
219  G4cout << pathName << sd->GetName();
220  if( sd->isActive() )
221  { G4cout << " *** Active "; }
222  else
223  { G4cout << " XXX Inactive "; }
224  G4cout << G4endl;
225  }
226  for(auto st : structure) st->ListTree();
227 }
228 
229 
void AddNewDetector(G4VSensitiveDetector *aSD, G4String treeStructure)
void Activate(G4bool activeFlag)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String & remove(str_size)
G4int first(char) const
void Initialize(G4HCofThisEvent *HCE)
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
G4VSensitiveDetector * GetSD(G4String aName)
G4SDStructure(G4String aPath)
G4int last(char) const
#define G4endl
Definition: G4ios.hh:61
G4bool isNull() const
G4VSensitiveDetector * FindSensitiveDetector(G4String aName, G4bool warning=true)