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 // G4VUserDetectorConstruction implementation << 27 // 26 // 28 // Original author: M.Asai, 1999 << 27 // $Id: G4VUserDetectorConstruction.cc 101679 2016-11-21 09:30:00Z gcosmo $ 29 // ------------------------------------------- << 28 // 30 29 31 #include "G4VUserDetectorConstruction.hh" 30 #include "G4VUserDetectorConstruction.hh" 32 << 31 #include "G4VPhysicalVolume.hh" 33 #include "G4FieldManager.hh" << 32 #include "G4VUserParallelWorld.hh" 34 #include "G4LogicalVolume.hh" 33 #include "G4LogicalVolume.hh" 35 #include "G4LogicalVolumeStore.hh" 34 #include "G4LogicalVolumeStore.hh" 36 #include "G4MultiSensitiveDetector.hh" << 37 #include "G4RunManager.hh" << 38 #include "G4SDManager.hh" << 39 #include "G4VPhysicalVolume.hh" << 40 #include "G4VSensitiveDetector.hh" 35 #include "G4VSensitiveDetector.hh" 41 #include "G4VUserParallelWorld.hh" << 36 #include "G4FieldManager.hh" 42 << 37 #include "G4SDManager.hh" 43 #include <cassert> << 38 #include "G4MultiSensitiveDetector.hh" 44 #include <map> << 39 #include <assert.h> 45 #include <sstream> 40 #include <sstream> 46 41 47 // ------------------------------------------- << 42 G4VUserDetectorConstruction::G4VUserDetectorConstruction() >> 43 {;} >> 44 >> 45 G4VUserDetectorConstruction::~G4VUserDetectorConstruction() >> 46 {;} >> 47 48 void G4VUserDetectorConstruction::RegisterPara 48 void G4VUserDetectorConstruction::RegisterParallelWorld(G4VUserParallelWorld* aPW) 49 { 49 { 50 auto pwItr = std::find(parallelWorld.cbegin( << 50 std::vector<G4VUserParallelWorld*>::iterator pwItr; 51 if (pwItr != parallelWorld.cend()) { << 51 for(pwItr=parallelWorld.begin();pwItr!=parallelWorld.end();pwItr++) 52 G4String eM = "A parallel world <"; << 52 { 53 eM += aPW->GetName(); << 53 if((*pwItr)->GetName()==aPW->GetName()) 54 eM += "> is already registered to the user << 54 { 55 G4Exception("G4VUserDetectorConstruction:: << 55 G4String eM = "A parallel world <"; 56 FatalErrorInArgument, eM); << 56 eM += aPW->GetName(); >> 57 eM += "> is already registered to the user detector construction."; >> 58 G4Exception("G4VUserDetectorConstruction::RegisterParallelWorld", >> 59 "Run0051",FatalErrorInArgument,eM); >> 60 } 57 } 61 } 58 parallelWorld.push_back(aPW); 62 parallelWorld.push_back(aPW); 59 } 63 } 60 64 61 // ------------------------------------------- << 62 G4int G4VUserDetectorConstruction::ConstructPa 65 G4int G4VUserDetectorConstruction::ConstructParallelGeometries() 63 { 66 { 64 G4int nP = 0; 67 G4int nP = 0; 65 for (const auto& pwItr : parallelWorld) { << 68 std::vector<G4VUserParallelWorld*>::iterator pwItr; 66 pwItr->Construct(); << 69 for(pwItr=parallelWorld.begin();pwItr!=parallelWorld.end();pwItr++) 67 ++nP; << 70 { >> 71 (*pwItr)->Construct(); >> 72 nP++; 68 } 73 } 69 return nP; 74 return nP; 70 } 75 } 71 76 72 // ------------------------------------------- << 73 void G4VUserDetectorConstruction::ConstructPar 77 void G4VUserDetectorConstruction::ConstructParallelSD() 74 { 78 { 75 for (const auto& pwItr : parallelWorld) { << 79 std::vector<G4VUserParallelWorld*>::iterator pwItr; 76 pwItr->ConstructSD(); << 80 for(pwItr=parallelWorld.begin();pwItr!=parallelWorld.end();pwItr++) 77 } << 81 { (*pwItr)->ConstructSD(); } 78 } 82 } 79 83 80 // ------------------------------------------- << 81 G4int G4VUserDetectorConstruction::GetNumberOf 84 G4int G4VUserDetectorConstruction::GetNumberOfParallelWorld() const 82 { << 85 { return parallelWorld.size(); } 83 return (G4int)parallelWorld.size(); << 84 } << 85 86 86 // ------------------------------------------- << 87 G4VUserParallelWorld* G4VUserDetectorConstruct 87 G4VUserParallelWorld* G4VUserDetectorConstruction::GetParallelWorld(G4int i) const 88 { 88 { 89 if (i < 0 || i >= GetNumberOfParallelWorld() << 89 if(i<0||i>=GetNumberOfParallelWorld()) return 0; 90 return parallelWorld[i]; 90 return parallelWorld[i]; 91 } 91 } 92 92 93 // ------------------------------------------- << 93 #include "G4RunManager.hh" 94 void G4VUserDetectorConstruction::ConstructSDa << 95 94 96 // ------------------------------------------- << 95 void G4VUserDetectorConstruction::ConstructSDandField() 97 void G4VUserDetectorConstruction::CloneF() << 98 { 96 { 99 using FMtoFMmap = std::map<G4FieldManager*, << 97 // G4RunManager::RMType rmtype = G4RunManager::GetRunManager()->GetRunManagerType(); 100 using FMpair = std::pair<G4FieldManager*, G4 << 98 // if(rmtype != G4RunManager::sequentialRM) >> 99 // { >> 100 // G4cout >> 101 // << "User-derived detector construction class does not implement \n" >> 102 // << "ConstructSDandFiled method: i.e. workers will not have SD and fields!\n" >> 103 // << "The user can safely ignore this message if (s)he has no sensitive\n" >> 104 // << "detector or field in her/his application." << G4endl; >> 105 // } >> 106 } 101 107 102 FMtoFMmap masterToWorker; << 108 #include <map> 103 G4LogicalVolumeStore* const logVolStore = G4 << 109 void G4VUserDetectorConstruction::CloneF() 104 for (const auto& g4LogicalVolume : *logVolSt << 110 { 105 // Use shadow of master to get instance of << 111 typedef std::map<G4FieldManager*,G4FieldManager*> FMtoFMmap; 106 G4FieldManager* masterFM = nullptr; // g4 << 112 typedef std::pair<G4FieldManager*,G4FieldManager*> FMpair; 107 G4FieldManager* clonedFM = nullptr; << 113 FMtoFMmap masterToWorker; 108 if (masterFM != nullptr) { << 114 G4LogicalVolumeStore* const logVolStore = G4LogicalVolumeStore::GetInstance(); 109 auto fmFound = masterToWorker.find(maste << 115 assert( logVolStore != NULL ); 110 if (fmFound == masterToWorker.cend()) { << 116 for ( G4LogicalVolumeStore::const_iterator it = logVolStore->begin() ; it != logVolStore->end() ; ++it ) 111 // First time we see this SD, let's cl << 117 { 112 try { << 118 G4LogicalVolume *g4LogicalVolume = *it; 113 auto insertedEl = masterToWorker.ins << 119 //Use shadow of master to get instance of FM 114 clonedFM = (insertedEl.first)->secon << 120 G4FieldManager* masterFM = 0;//g4LogicalVolume->fFieldManager; 115 } << 121 G4FieldManager* clonedFM = 0; 116 catch (...) { << 122 if ( masterFM ) 117 G4ExceptionDescription msg; << 123 { 118 msg << "Cloning of G4FieldManager fa << 124 FMtoFMmap::iterator fmFound = masterToWorker.find(masterFM); 119 << " But derived class does not << 125 if ( fmFound == masterToWorker.end() ) 120 "continue."; << 126 { 121 G4Exception("G4VUserDetectorConstruc << 127 //First time we see this SD, let's clone and remember... 122 } << 128 try { 123 } << 129 std::pair<FMtoFMmap::iterator,bool> insertedEl = masterToWorker.insert( FMpair(masterFM, masterFM->Clone()) ); 124 else { << 130 clonedFM = (insertedEl.first)->second; 125 // We have already seen this SD attach << 131 } 126 // let's re-use previous clone << 132 catch (...) 127 clonedFM = (*fmFound).second; << 133 { 128 } << 134 G4ExceptionDescription msg; 129 } // masterFM != 0 << 135 msg << "Cloning of G4FieldManager failed." 130 // Note that we do not push FM to daughter << 136 << " But derived class does not implement cloning. Cannot continue."; 131 // we area looping on all logical volumes << 137 G4Exception("G4VUserDetectorConstruction::CloneSD", "Run0053", FatalException,msg); 132 // the map master<->cloned the final effec << 138 133 // correct Boolean flag: log-volumes that << 139 } 134 // FM they will have cloned ones << 140 } 135 g4LogicalVolume->SetFieldManager(clonedFM, << 141 else 136 } << 142 { >> 143 // We have already seen this SD attached to a fifferent LogicalVolume, let's re-use previous clone >> 144 clonedFM = (*fmFound).second; >> 145 } >> 146 }// masterFM != 0 >> 147 //Note that we do not push FM to doughters (false argument), however, since we area looping on all >> 148 //logical volumes and we implemented the "trick" of the map master<->cloned the final >> 149 //effect is the same as using here the correct boolean flag: log-volumes that originally were sharing >> 150 //the same FM they will have cloned ones >> 151 g4LogicalVolume->SetFieldManager(clonedFM, false); >> 152 } 137 } 153 } 138 154 139 // ------------------------------------------- << 140 void G4VUserDetectorConstruction::CloneSD() 155 void G4VUserDetectorConstruction::CloneSD() 141 { 156 { 142 // Loop on ALL logial volumes to search for << 157 //Loop on ALL logial volumes to search for attached SD 143 G4LogicalVolumeStore* const logVolStore = G4 << 158 G4LogicalVolumeStore* const logVolStore = G4LogicalVolumeStore::GetInstance(); 144 << 159 assert( logVolStore != NULL ); 145 using SDtoSDmap = std::map<G4VSensitiveDetec << 160 146 using SDpair = std::pair<G4VSensitiveDetecto << 161 typedef std::map<G4VSensitiveDetector*,G4VSensitiveDetector*> SDtoSDmap; 147 SDtoSDmap masterToWorker; << 162 typedef std::pair<G4VSensitiveDetector*,G4VSensitiveDetector*> SDpair; 148 << 163 SDtoSDmap masterToWorker; 149 for (const auto& g4LogicalVolume : *logVolSt << 164 150 // Use shadow of master to get the instanc << 165 for ( G4LogicalVolumeStore::const_iterator it = logVolStore->begin() ; it != logVolStore->end() ; ++it ) 151 G4VSensitiveDetector* masterSD = nullptr; << 166 { 152 G4VSensitiveDetector* clonedSD = nullptr; << 167 G4LogicalVolume *g4LogicalVolume = *it; 153 if (masterSD != nullptr) { << 168 //Use shadow of master to get the instance of SD 154 auto sdFound = masterToWorker.find(maste << 169 G4VSensitiveDetector* masterSD = 0;//g4LogicalVolume->fSensitiveDetector; 155 if (sdFound == masterToWorker.cend()) { << 170 G4VSensitiveDetector* clonedSD = 0; 156 // First time we see this SD, let's cl << 171 if ( masterSD ) 157 try { << 172 { 158 auto insertedEl = masterToWorker.ins << 173 SDtoSDmap::iterator sdFound = masterToWorker.find(masterSD); 159 clonedSD = (insertedEl.first)->secon << 174 if ( sdFound == masterToWorker.end() ) 160 } << 175 { 161 catch (...) { << 176 //First time we see this SD, let's clone and remember... 162 G4ExceptionDescription msg; << 177 try { 163 msg << "Cloning of G4VSensitiveDetec << 178 std::pair<SDtoSDmap::iterator,bool> insertedEl = masterToWorker.insert( SDpair(masterSD,masterSD->Clone()) ); >> 179 clonedSD = (insertedEl.first)->second; >> 180 } >> 181 catch (...) >> 182 { >> 183 G4ExceptionDescription msg; >> 184 msg << "Cloning of G4VSensitiveDetector requested for:" << masterSD->GetName() << "\n" 164 #ifndef WIN32 185 #ifndef WIN32 165 << " (full path name: " << maste << 186 << " (full path name: " << masterSD->GetFullPathName() << ").\n" 166 #endif 187 #endif 167 << " But derived class does not << 188 << " But derived class does not implement cloning. Cannot continue."; 168 "continue."; << 189 G4Exception("G4VUserDetectorConstruction::CloneSD", "Run0053", FatalException,msg); 169 G4Exception("G4VUserDetectorConstruc << 190 } 170 } << 191 171 } << 192 } 172 else { << 193 else 173 // We have already seen this SD attach << 194 { 174 // let's re-use previous clone << 195 // We have already seen this SD attached to a fifferent LogicalVolume, let's re-use previous clone 175 clonedSD = (*sdFound).second; << 196 clonedSD = (*sdFound).second; 176 } << 197 177 } // masterSD!=0 << 198 } 178 g4LogicalVolume->SetSensitiveDetector(clon << 199 }// masterSD!=0 179 } << 200 g4LogicalVolume->SetSensitiveDetector(clonedSD); >> 201 >> 202 } 180 } 203 } 181 204 182 // ------------------------------------------- << 205 void G4VUserDetectorConstruction::SetSensitiveDetector 183 void G4VUserDetectorConstruction::SetSensitive << 206 (const G4String& logVolName, G4VSensitiveDetector* aSD, G4bool multi) 184 << 207 { 185 { << 186 G4bool found = false; 208 G4bool found = false; 187 G4LogicalVolumeStore* store = G4LogicalVolum 209 G4LogicalVolumeStore* store = G4LogicalVolumeStore::GetInstance(); 188 auto volmap = store->GetMap(); << 210 for(G4LogicalVolumeStore::iterator pos=store->begin(); pos!=store->end(); pos++) 189 auto pos = volmap.find(logVolName); << 211 { 190 if (pos != volmap.cend()) { << 212 if((*pos)->GetName()==logVolName) 191 if ((pos->second.size() > 1) && !multi) { << 213 { 192 G4String eM = "More than one logical vol << 214 if(found && !multi) 193 eM += pos->first; << 215 { 194 eM += "> are found and thus the sensitiv << 216 G4String eM = "More than one logical volumes of the name <"; 195 eM += aSD->GetName(); << 217 eM += (*pos)->GetName(); 196 eM += "> cannot be uniquely assigned."; << 218 eM += "> are found and thus the sensitive detector <"; 197 G4Exception("G4VUserDetectorConstruction << 219 eM += aSD->GetName(); 198 FatalErrorInArgument, eM); << 220 eM += "> cannot be uniquely assigned."; 199 } << 221 G4Exception("G4VUserDetectorConstruction::SetSensitiveDetector", 200 found = true; << 222 "Run0052",FatalErrorInArgument,eM); 201 for (auto& i : pos->second) { << 223 } 202 SetSensitiveDetector(i, aSD); << 224 found = true; >> 225 SetSensitiveDetector(*pos,aSD); 203 } 226 } 204 } << 227 } 205 if (!found) { << 228 if(!found) 206 G4String eM2 = "No logical volume of name << 229 { >> 230 G4String eM2 = "No logical volume of the name <"; 207 eM2 += logVolName; 231 eM2 += logVolName; 208 eM2 += "> is found. The specified sensitiv 232 eM2 += "> is found. The specified sensitive detector <"; 209 eM2 += aSD->GetName(); 233 eM2 += aSD->GetName(); 210 eM2 += "> couldn't be assigned to any volu 234 eM2 += "> couldn't be assigned to any volume."; 211 G4Exception("G4VUserDetectorConstruction:: << 235 G4Exception("G4VUserDetectorConstruction::SetSensitiveDetector", 212 FatalErrorInArgument, eM2); << 236 "Run0053",FatalErrorInArgument,eM2); 213 } 237 } 214 } << 238 } 215 239 216 // ------------------------------------------- << 240 void G4VUserDetectorConstruction::SetSensitiveDetector 217 void G4VUserDetectorConstruction::SetSensitive << 241 (G4LogicalVolume* logVol, G4VSensitiveDetector* aSD) 218 << 242 { 219 { << 243 assert(logVol!=nullptr&&aSD!=nullptr); 220 assert(logVol != nullptr && aSD != nullptr); << 221 244 222 // The aSD has already been added by user to << 245 //The aSD has already been added by user to the manager if needed 223 // G4SDManager::GetSDMpointer()->AddNewDetec << 246 //G4SDManager::GetSDMpointer()->AddNewDetector(aSD); 224 247 225 // New Logic: allow for "multiple" SDs being << 248 //New Logic: allow for "multiple" SDs being attached to a single LV. 226 // To do that we use a special proxy SD call << 249 //To do that we use a special proxy SD called G4MultiSensitiveDetector 227 250 228 // Get existing SD if already set and check << 251 //Get existing SD if already set and check if it is of the special type 229 G4VSensitiveDetector* originalSD = logVol->G 252 G4VSensitiveDetector* originalSD = logVol->GetSensitiveDetector(); 230 if (originalSD == aSD) { << 253 if ( originalSD == nullptr ) { 231 G4ExceptionDescription msg; << 254 logVol->SetSensitiveDetector(aSD); 232 msg << "Attempting to add multiple times t << 255 } else { 233 msg << originalSD->GetName() << "\") is no << 256 G4MultiSensitiveDetector* msd = dynamic_cast<G4MultiSensitiveDetector*>(originalSD); 234 G4Exception("G4VUserDetectorConstruction:: << 257 if ( msd != nullptr ) { 235 return; << 258 msd->AddSD(aSD); 236 } << 259 } else { 237 if (originalSD == nullptr) { << 260 std::ostringstream mn; 238 logVol->SetSensitiveDetector(aSD); << 261 mn<<"/MultiSD_"<<logVol->GetName()<<"_"<<logVol; 239 } << 262 const G4String msdname = mn.str(); 240 else { << 263 msd = new G4MultiSensitiveDetector(msdname); 241 auto msd = dynamic_cast<G4MultiSensitiveDe << 264 //We need to register the proxy to have correct handling of IDs 242 if (msd != nullptr) { << 265 G4SDManager::GetSDMpointer()->AddNewDetector(msd); 243 msd->AddSD(aSD); << 266 msd->AddSD(originalSD); 244 } << 267 msd->AddSD(aSD); 245 else { << 268 logVol->SetSensitiveDetector(msd); 246 std::ostringstream mn; << 269 } 247 mn << "/MultiSD_" << logVol->GetName() < << 248 const G4String msdname = mn.str(); << 249 msd = new G4MultiSensitiveDetector(msdna << 250 // We need to register the proxy to have << 251 G4SDManager::GetSDMpointer()->AddNewDete << 252 msd->AddSD(originalSD); << 253 msd->AddSD(aSD); << 254 logVol->SetSensitiveDetector(msd); << 255 } << 256 } 270 } 257 } 271 } 258 272