Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // 26 // 27 // 27 // 28 28 29 // G4SDStructure 29 // G4SDStructure 30 #include "G4SDStructure.hh" 30 #include "G4SDStructure.hh" 31 31 32 #include "G4ios.hh" 32 #include "G4ios.hh" 33 33 34 G4SDStructure::G4SDStructure(const G4String& a 34 G4SDStructure::G4SDStructure(const G4String& aPath) 35 { 35 { 36 pathName = aPath; 36 pathName = aPath; 37 dirName = aPath; 37 dirName = aPath; 38 auto i = dirName.length(); 38 auto i = dirName.length(); 39 if (i > 1) { 39 if (i > 1) { 40 dirName.erase(i - 1); 40 dirName.erase(i - 1); 41 auto isl = dirName.rfind('/'); 41 auto isl = dirName.rfind('/'); 42 dirName.erase(0, isl + 1); 42 dirName.erase(0, isl + 1); 43 dirName += "/"; 43 dirName += "/"; 44 } 44 } 45 } 45 } 46 46 47 G4SDStructure::~G4SDStructure() 47 G4SDStructure::~G4SDStructure() 48 { 48 { 49 for (auto st : structure) 49 for (auto st : structure) 50 delete st; 50 delete st; 51 structure.clear(); 51 structure.clear(); 52 for (auto dt : detector) 52 for (auto dt : detector) 53 delete dt; 53 delete dt; 54 detector.clear(); 54 detector.clear(); 55 } 55 } 56 56 57 G4bool G4SDStructure::operator==(const G4SDStr 57 G4bool G4SDStructure::operator==(const G4SDStructure& right) const { return (this == &right); } 58 58 59 void G4SDStructure::AddNewDetector(G4VSensitiv 59 void G4SDStructure::AddNewDetector(G4VSensitiveDetector* aSD, const G4String& treeStructure) 60 { 60 { 61 G4String remainingPath = treeStructure; 61 G4String remainingPath = treeStructure; 62 remainingPath.erase(0, pathName.length()); 62 remainingPath.erase(0, pathName.length()); 63 if (! remainingPath.empty()) { // The detec 63 if (! remainingPath.empty()) { // The detector should be kept in subdirectory. 64 // First, ch 64 // First, check if the subdirectoy exists. 65 G4String subD = ExtractDirName(remainingPa 65 G4String subD = ExtractDirName(remainingPath); 66 G4SDStructure* tgtSDS = FindSubDirectory(s 66 G4SDStructure* tgtSDS = FindSubDirectory(subD); 67 if (tgtSDS == nullptr) { // Subdirectory 67 if (tgtSDS == nullptr) { // Subdirectory not found. Create a new directory. 68 subD.insert(0, pathName); 68 subD.insert(0, pathName); 69 tgtSDS = new G4SDStructure(subD); 69 tgtSDS = new G4SDStructure(subD); 70 structure.push_back(tgtSDS); 70 structure.push_back(tgtSDS); 71 } 71 } 72 tgtSDS->AddNewDetector(aSD, treeStructure) 72 tgtSDS->AddNewDetector(aSD, treeStructure); 73 } 73 } 74 else { // The sensitive detector should be 74 else { // The sensitive detector should be kept in this directory. 75 G4VSensitiveDetector* tgtSD = GetSD(aSD->G 75 G4VSensitiveDetector* tgtSD = GetSD(aSD->GetName()); 76 if (tgtSD == nullptr) { 76 if (tgtSD == nullptr) { 77 detector.push_back(aSD); 77 detector.push_back(aSD); 78 } 78 } 79 else if (tgtSD != aSD) { 79 else if (tgtSD != aSD) { 80 #ifdef G4VERBOSE 80 #ifdef G4VERBOSE 81 G4ExceptionDescription ed; 81 G4ExceptionDescription ed; 82 ed << aSD->GetName() << " had already be 82 ed << aSD->GetName() << " had already been stored in " << pathName 83 << ". Object pointer is overwritten.\ 83 << ". Object pointer is overwritten.\n"; 84 ed << "It's users' responsibility to del 84 ed << "It's users' responsibility to delete the old sensitive detector " 85 "object."; 85 "object."; 86 G4Exception("G4SDStructure::AddNewDetect 86 G4Exception("G4SDStructure::AddNewDetector()", "DET1010", JustWarning, ed); 87 #endif 87 #endif 88 RemoveSD(tgtSD); 88 RemoveSD(tgtSD); 89 detector.push_back(aSD); 89 detector.push_back(aSD); 90 } 90 } 91 } 91 } 92 } 92 } 93 93 94 G4SDStructure* G4SDStructure::FindSubDirectory 94 G4SDStructure* G4SDStructure::FindSubDirectory(const G4String& subD) 95 { 95 { 96 for (auto st : structure) { 96 for (auto st : structure) { 97 if (subD == st->dirName) return st; 97 if (subD == st->dirName) return st; 98 } 98 } 99 return nullptr; 99 return nullptr; 100 } 100 } 101 101 102 G4VSensitiveDetector* G4SDStructure::GetSD(con 102 G4VSensitiveDetector* G4SDStructure::GetSD(const G4String& aSDName) 103 { 103 { 104 for (auto det : detector) { 104 for (auto det : detector) { 105 if (aSDName == det->GetName()) return det; 105 if (aSDName == det->GetName()) return det; 106 } 106 } 107 return nullptr; 107 return nullptr; 108 } 108 } 109 109 110 void G4SDStructure::RemoveSD(G4VSensitiveDetec 110 void G4SDStructure::RemoveSD(G4VSensitiveDetector* sd) 111 { 111 { 112 auto det = std::find(detector.begin(), detec 112 auto det = std::find(detector.begin(), detector.end(), sd); 113 if (det != detector.end()) detector.erase(de 113 if (det != detector.end()) detector.erase(det); 114 } 114 } 115 115 116 G4String G4SDStructure::ExtractDirName(const G 116 G4String G4SDStructure::ExtractDirName(const G4String& aName) 117 { 117 { 118 G4String subD = aName; 118 G4String subD = aName; 119 auto i = aName.find('/'); 119 auto i = aName.find('/'); 120 if (i != G4String::npos) subD.erase(i + 1); 120 if (i != G4String::npos) subD.erase(i + 1); 121 return subD; 121 return subD; 122 } 122 } 123 123 124 void G4SDStructure::Activate(const G4String& a 124 void G4SDStructure::Activate(const G4String& aName, G4bool sensitiveFlag) 125 { 125 { 126 G4String aPath = aName; 126 G4String aPath = aName; 127 aPath.erase(0, pathName.length()); 127 aPath.erase(0, pathName.length()); 128 if (aPath.find('/') != std::string::npos) { 128 if (aPath.find('/') != std::string::npos) { // Command is ordered for a subdirectory. 129 G4String subD = ExtractDirName(aPath); 129 G4String subD = ExtractDirName(aPath); 130 G4SDStructure* tgtSDS = FindSubDirectory(s 130 G4SDStructure* tgtSDS = FindSubDirectory(subD); 131 if (tgtSDS == nullptr) { // The subdirect 131 if (tgtSDS == nullptr) { // The subdirectory is not found 132 G4cout << subD << " is not found in " << 132 G4cout << subD << " is not found in " << pathName << G4endl; 133 } 133 } 134 else { 134 else { 135 tgtSDS->Activate(aName, sensitiveFlag); 135 tgtSDS->Activate(aName, sensitiveFlag); 136 } 136 } 137 } 137 } 138 else if (aPath.empty()) { // Command is ord 138 else if (aPath.empty()) { // Command is ordered for all detectors in this directory. 139 for (auto det : detector) 139 for (auto det : detector) 140 det->Activate(sensitiveFlag); 140 det->Activate(sensitiveFlag); 141 for (auto st : structure) 141 for (auto st : structure) 142 st->Activate(G4String("/"), sensitiveFla 142 st->Activate(G4String("/"), sensitiveFlag); 143 } 143 } 144 else { // Command is ordered to a particula 144 else { // Command is ordered to a particular detector. 145 G4VSensitiveDetector* tgtSD = GetSD(aPath) 145 G4VSensitiveDetector* tgtSD = GetSD(aPath); 146 if (tgtSD == nullptr) { // The detector i 146 if (tgtSD == nullptr) { // The detector is not found. 147 G4cout << aPath << " is not found in " < 147 G4cout << aPath << " is not found in " << pathName << G4endl; 148 } 148 } 149 else { 149 else { 150 tgtSD->Activate(sensitiveFlag); 150 tgtSD->Activate(sensitiveFlag); 151 } 151 } 152 } 152 } 153 } 153 } 154 154 155 G4VSensitiveDetector* G4SDStructure::FindSensi 155 G4VSensitiveDetector* G4SDStructure::FindSensitiveDetector(const G4String& aName, G4bool warning) 156 { 156 { 157 G4String aPath = aName; 157 G4String aPath = aName; 158 aPath.erase(0, pathName.length()); 158 aPath.erase(0, pathName.length()); 159 if (aPath.find('/') != std::string::npos) { 159 if (aPath.find('/') != std::string::npos) { // SD exists in sub-directory 160 G4String subD = ExtractDirName(aPath); 160 G4String subD = ExtractDirName(aPath); 161 G4SDStructure* tgtSDS = FindSubDirectory(s 161 G4SDStructure* tgtSDS = FindSubDirectory(subD); 162 if (tgtSDS == nullptr) { // The subdirect 162 if (tgtSDS == nullptr) { // The subdirectory is not found 163 if (warning) G4cout << subD << " is not 163 if (warning) G4cout << subD << " is not found in " << pathName << G4endl; 164 return nullptr; 164 return nullptr; 165 } 165 } 166 166 167 return tgtSDS->FindSensitiveDetector(aName 167 return tgtSDS->FindSensitiveDetector(aName, warning); 168 } 168 } 169 169 170 // SD must exist in this directory 170 // SD must exist in this directory 171 G4VSensitiveDetector* tgtSD = GetSD(aPath); 171 G4VSensitiveDetector* tgtSD = GetSD(aPath); 172 if (tgtSD == nullptr) { // The detector is 172 if (tgtSD == nullptr) { // The detector is not found. 173 if (warning) G4cout << aPath << " is not f 173 if (warning) G4cout << aPath << " is not found in " << pathName << G4endl; 174 } 174 } 175 return tgtSD; 175 return tgtSD; 176 } 176 } 177 177 178 void G4SDStructure::Initialize(G4HCofThisEvent 178 void G4SDStructure::Initialize(G4HCofThisEvent* HCE) 179 { 179 { 180 // Broadcast to subdirectories. 180 // Broadcast to subdirectories. 181 for (auto st : structure) { 181 for (auto st : structure) { 182 st->Initialize(HCE); 182 st->Initialize(HCE); 183 } 183 } 184 // Initialize all detectors in this director 184 // Initialize all detectors in this directory. 185 for (auto dt : detector) { 185 for (auto dt : detector) { 186 if (dt->isActive()) dt->Initialize(HCE); 186 if (dt->isActive()) dt->Initialize(HCE); 187 } 187 } 188 } 188 } 189 189 190 void G4SDStructure::Terminate(G4HCofThisEvent* 190 void G4SDStructure::Terminate(G4HCofThisEvent* HCE) 191 { 191 { 192 // Broadcast to subdirectories. 192 // Broadcast to subdirectories. 193 for (auto st : structure) { 193 for (auto st : structure) { 194 st->Terminate(HCE); 194 st->Terminate(HCE); 195 } 195 } 196 // Terminate all detectors in this directory 196 // Terminate all detectors in this directory. 197 for (auto dt : detector) { 197 for (auto dt : detector) { 198 if (dt->isActive()) dt->EndOfEvent(HCE); 198 if (dt->isActive()) dt->EndOfEvent(HCE); 199 } 199 } 200 } 200 } 201 201 202 void G4SDStructure::ListTree() 202 void G4SDStructure::ListTree() 203 { 203 { 204 G4cout << pathName << G4endl; 204 G4cout << pathName << G4endl; 205 for (auto sd : detector) { 205 for (auto sd : detector) { 206 G4cout << pathName << sd->GetName(); 206 G4cout << pathName << sd->GetName(); 207 if (sd->isActive()) { 207 if (sd->isActive()) { 208 G4cout << " *** Active "; 208 G4cout << " *** Active "; 209 } 209 } 210 else { 210 else { 211 G4cout << " XXX Inactive "; 211 G4cout << " XXX Inactive "; 212 } 212 } 213 G4cout << G4endl; 213 G4cout << G4endl; 214 } 214 } 215 for (auto st : structure) 215 for (auto st : structure) 216 st->ListTree(); 216 st->ListTree(); 217 } 217 } 218 218