Geant4 Cross Reference |
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 // 28 // 29 //--------------------------------------------------------------- 30 // 31 // G4GlobalFastSimulationManager.cc 32 // 33 // Description: 34 // A singleton class which manages the Fast Simulation managers 35 // attached to envelopes. Implementation. 36 // 37 // History: 38 // June 98: Verderi && MoraDeFreitas - "G4ParallelWorld" becomes 39 // "G4FlavoredParallelWorld"; some method name changes; 40 // GetFlavoredWorldForThis now returns a 41 // G4FlavoredParallelWorld pointer. 42 // Feb 98: Verderi && MoraDeFreitas - First Implementation. 43 // March 98: correction to instanciate dynamically the manager 44 // May 07: Move to parallel world scheme 45 // 46 //--------------------------------------------------------------- 47 48 #include "G4GlobalFastSimulationManager.hh" 49 50 #include "G4FastSimulationMessenger.hh" 51 #include "G4Material.hh" 52 #include "G4PVPlacement.hh" 53 #include "G4ParticleDefinition.hh" 54 #include "G4ParticleTable.hh" 55 #include "G4PhysicalVolumeStore.hh" 56 #include "G4ProcessManager.hh" 57 #include "G4ProcessVector.hh" 58 #include "G4RegionStore.hh" 59 #include "G4ThreadLocalSingleton.hh" 60 #include "G4ThreeVector.hh" 61 #include "G4TransportationManager.hh" 62 63 // -------------------------------------------------- 64 // -- static methods to retrieve the manager pointer: 65 // -------------------------------------------------- 66 G4GlobalFastSimulationManager* G4GlobalFastSimulationManager::GetGlobalFastSimulationManager() 67 { 68 static G4ThreadLocalSingleton<G4GlobalFastSimulationManager> instance; 69 return instance.Instance(); 70 } 71 72 G4GlobalFastSimulationManager* G4GlobalFastSimulationManager::GetInstance() 73 { 74 return G4GlobalFastSimulationManager::GetGlobalFastSimulationManager(); 75 } 76 77 // --------------- 78 // -- constructor 79 // --------------- 80 G4GlobalFastSimulationManager::G4GlobalFastSimulationManager() 81 { 82 fTheFastSimulationMessenger = new G4FastSimulationMessenger(this); 83 } 84 85 // ------------- 86 // -- destructor 87 // ------------- 88 G4GlobalFastSimulationManager::~G4GlobalFastSimulationManager() 89 { 90 delete fTheFastSimulationMessenger; 91 } 92 93 // ---------------------- 94 // -- management methods: 95 // ---------------------- 96 void G4GlobalFastSimulationManager::AddFastSimulationManager(G4FastSimulationManager* fsmanager) 97 { 98 ManagedManagers.push_back(fsmanager); 99 } 100 101 void G4GlobalFastSimulationManager::RemoveFastSimulationManager(G4FastSimulationManager* fsmanager) 102 { 103 ManagedManagers.remove(fsmanager); 104 } 105 106 void G4GlobalFastSimulationManager::AddFSMP(G4FastSimulationManagerProcess* fp) 107 { 108 fFSMPVector.push_back(fp); 109 } 110 111 void G4GlobalFastSimulationManager::RemoveFSMP(G4FastSimulationManagerProcess* fp) 112 { 113 fFSMPVector.remove(fp); 114 } 115 116 void G4GlobalFastSimulationManager::ActivateFastSimulationModel(const G4String& aName) 117 { 118 G4bool result = false; 119 for (auto& ManagedManager : ManagedManagers) 120 result = result || ManagedManager->ActivateFastSimulationModel(aName); 121 122 G4cout << "Model " << aName << (result ? " activated." : " not found.") << G4endl; 123 } 124 125 void G4GlobalFastSimulationManager::InActivateFastSimulationModel(const G4String& aName) 126 { 127 G4bool result = false; 128 for (auto& ManagedManager : ManagedManagers) 129 result = result || ManagedManager->InActivateFastSimulationModel(aName); 130 131 G4cout << "Model " << aName << (result ? " inactivated." : " not found.") << G4endl; 132 } 133 134 void G4GlobalFastSimulationManager::Flush() 135 { 136 // loop over all models (that need flushing?) and flush 137 for (auto& ManagedManager : ManagedManagers) 138 ManagedManager->FlushModels(); 139 } 140 141 // --------------------------------- 142 // -- display fast simulation setup: 143 // --------------------------------- 144 void G4GlobalFastSimulationManager::ShowSetup() 145 { 146 std::vector<G4VPhysicalVolume*> worldDone; 147 G4VPhysicalVolume* world; 148 G4RegionStore* regions = G4RegionStore::GetInstance(); 149 // ---------------------------------------------------- 150 // -- loop on regions to get the list of world volumes: 151 // ---------------------------------------------------- 152 G4cout << "\nFast simulation setup:" << G4endl; 153 for (auto& region : *regions) { 154 world = region->GetWorldPhysical(); 155 if (world == nullptr) // region does not belong to any (existing) world 156 { 157 continue; 158 } 159 G4bool newWorld = true; 160 for (auto& ii : worldDone) 161 if (ii == world) { 162 newWorld = false; 163 break; 164 } 165 if (newWorld) { 166 worldDone.push_back(world); 167 G4Region* worldRegion = world->GetLogicalVolume()->GetRegion(); 168 // -- preambule: print physical volume and region names... 169 if (world 170 == G4TransportationManager::GetTransportationManager() 171 ->GetNavigatorForTracking() 172 ->GetWorldVolume()) 173 G4cout << "\n * Mass Geometry with "; 174 else 175 G4cout << "\n * Parallel Geometry with "; 176 G4cout << "world volume: `" << world->GetName() << "' [region : `" << worldRegion->GetName() 177 << "']" << G4endl; 178 // -- ... and print G4FSMP(s) attached to this world volume: 179 G4bool findG4FSMP(false); 180 // -- show to what particles this G4FSMP is attached to: 181 std::vector<G4ParticleDefinition*> particlesKnown; 182 for (auto& ip : fFSMPVector) 183 if (ip->GetWorldVolume() == world) { 184 G4cout << " o G4FastSimulationProcess: '" << ip->GetProcessName() << "'" << G4endl; 185 G4cout << " Attached to:"; 186 G4ParticleTable* particles = G4ParticleTable::GetParticleTable(); 187 for (G4int iParticle = 0; iParticle < particles->entries(); iParticle++) { 188 G4ParticleDefinition* particle = particles->GetParticle(iParticle); 189 G4ProcessVector* processes = particle->GetProcessManager()->GetProcessList(); 190 if (processes->contains(ip)) { 191 G4cout << " " << particle->GetParticleName(); 192 findG4FSMP = true; 193 particlesKnown.push_back(particle); 194 } 195 } 196 G4cout << G4endl; 197 } 198 if (!findG4FSMP) G4cout << " o G4FastSimulationProcess: (none)" << G4endl; 199 // -- now display the regions in this world volume, with mother<->daughter link shown by 200 // indentation: 201 G4cout << " o Region(s) and model(s) setup:" << G4endl; 202 DisplayRegion(worldRegion, 1, particlesKnown); 203 } 204 } 205 } 206 207 void G4GlobalFastSimulationManager::DisplayRegion( 208 G4Region* region, G4int depth, std::vector<G4ParticleDefinition*>& particlesKnown) const 209 { 210 G4String indent = " "; 211 for (G4int I = 0; I < depth; I++) 212 indent += " "; 213 G4cout << indent << "Region: `" << region->GetName() << "'" << G4endl; 214 G4FastSimulationManager* fastSimManager = region->GetFastSimulationManager(); 215 if (fastSimManager != nullptr) { 216 indent += " "; 217 G4cout << indent << "Model(s):" << G4endl; 218 indent += " "; 219 for (auto im : fastSimManager->GetFastSimulationModelList()) { 220 G4cout << indent << "`" << im->GetName() << "'"; 221 G4cout << " ; applicable to:"; 222 G4ParticleTable* particles = G4ParticleTable::GetParticleTable(); 223 for (G4int iParticle = 0; iParticle < particles->entries(); iParticle++) { 224 if (im->IsApplicable(*(particles->GetParticle(iParticle)))) { 225 G4cout << " " << particles->GetParticle(iParticle)->GetParticleName(); 226 G4bool known(false); 227 for (auto& l : particlesKnown) 228 if (l == particles->GetParticle(iParticle)) { 229 known = true; 230 break; 231 } 232 if (!known) G4cout << "[!!]"; 233 } 234 } 235 G4cout << G4endl; 236 } 237 } 238 239 // -- all that to check mothership of "region" 240 G4PhysicalVolumeStore* physVolStore = G4PhysicalVolumeStore::GetInstance(); 241 for (auto physVol : *physVolStore) { 242 if (physVol->GetLogicalVolume()->IsRootRegion()) 243 if (physVol->GetMotherLogical() != nullptr) { 244 G4Region* thisVolMotherRegion = physVol->GetMotherLogical()->GetRegion(); 245 if (thisVolMotherRegion == region) 246 DisplayRegion(physVol->GetLogicalVolume()->GetRegion(), depth + 1, particlesKnown); 247 } 248 } 249 } 250 251 // ---------------------------- 252 // -- management methods : list 253 // ---------------------------- 254 255 void G4GlobalFastSimulationManager::ListEnvelopes(const G4String& aName, listType theType) 256 { 257 if (theType == ISAPPLICABLE) { 258 for (auto& ManagedManager : ManagedManagers) 259 ManagedManager->ListModels(aName); 260 return; 261 } 262 263 if (aName == "all") { 264 G4int titled = 0; 265 for (auto& ManagedManager : ManagedManagers) { 266 if (theType == NAMES_ONLY) { 267 if ((titled++) == 0) G4cout << "Current Envelopes for Fast Simulation:\n"; 268 G4cout << " "; 269 ManagedManager->ListTitle(); 270 G4cout << G4endl; 271 } 272 else 273 ManagedManager->ListModels(); 274 } 275 } 276 else { 277 for (auto& ManagedManager : ManagedManagers) 278 if (aName == ManagedManager->GetEnvelope()->GetName()) { 279 ManagedManager->ListModels(); 280 break; 281 } 282 } 283 } 284 285 void G4GlobalFastSimulationManager::ListEnvelopes(const G4ParticleDefinition* aPD) 286 { 287 for (auto& ManagedManager : ManagedManagers) 288 ManagedManager->ListModels(aPD); 289 } 290 291 G4VFastSimulationModel* G4GlobalFastSimulationManager::GetFastSimulationModel( 292 const G4String& modelName, const G4VFastSimulationModel* previousFound) const 293 { 294 G4VFastSimulationModel* model = nullptr; 295 // -- flag used to navigate accross the various managers; 296 bool foundPrevious(false); 297 for (auto ManagedManager : ManagedManagers) { 298 model = ManagedManager->GetFastSimulationModel(modelName, previousFound, foundPrevious); 299 if (model != nullptr) break; 300 } 301 return model; 302 } 303