From 58aa69315d420099cc87c2dd86e903585c240b6b Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 7 Dec 2022 16:33:53 +0100 Subject: [PATCH 1/7] add check for duplicated volumes which would lead to errors --- src/DetectorConstruction.cxx | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/DetectorConstruction.cxx b/src/DetectorConstruction.cxx index 09d0566e..6ce0892c 100644 --- a/src/DetectorConstruction.cxx +++ b/src/DetectorConstruction.cxx @@ -63,6 +63,42 @@ G4VPhysicalVolume* DetectorConstruction::Construct() { const auto& geometryInfo = restG4Metadata->GetGeant4GeometryInfo(); geometryInfo.Print(); + // do some checks + { + // Check all physical volume names are unique + G4PhysicalVolumeStore* physicalVolumeStore = G4PhysicalVolumeStore::GetInstance(); + set physicalVolumeNames; + vector::const_iterator physicalVolume; + for (physicalVolume = physicalVolumeStore->begin(); physicalVolume != physicalVolumeStore->end(); + physicalVolume++) { + auto name = (*physicalVolume)->GetName(); + if (physicalVolumeNames.count(name)) { + cerr << "ERROR: physical volume name " << name + << " is not unique. Please double check your geometry files. Be mindful of especial " + "characters such as '0x'" + << endl; + exit(1); + } + physicalVolumeNames.insert(name); + } + + // Check all logical volume names are unique + G4LogicalVolumeStore* logicalVolumeStore = G4LogicalVolumeStore::GetInstance(); + set logicalVolumeNames; + vector::const_iterator logicalVolume; + for (logicalVolume = logicalVolumeStore->begin(); logicalVolume != logicalVolumeStore->end(); + logicalVolume++) { + auto name = (*logicalVolume)->GetName(); + if (logicalVolumeNames.count(name)) { + cerr << "ERROR: logical volume name " << name + << " is not unique. Please double check your geometry files. Be mindful of especial " + "characters such as '0x'" + << endl; + exit(1); + } + logicalVolumeNames.insert(name); + } + } filesystem::current_path(startingPath); auto sensitiveVolume = (string)restG4Metadata->GetSensitiveVolume(); From 1c8219dd4b6578544bb0a6d0b3e216ee499b835e Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 7 Dec 2022 16:34:15 +0100 Subject: [PATCH 2/7] explicit check for null --- src/DetectorConstruction.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DetectorConstruction.cxx b/src/DetectorConstruction.cxx index 6ce0892c..a0c5fc7c 100644 --- a/src/DetectorConstruction.cxx +++ b/src/DetectorConstruction.cxx @@ -103,7 +103,7 @@ G4VPhysicalVolume* DetectorConstruction::Construct() { auto sensitiveVolume = (string)restG4Metadata->GetSensitiveVolume(); G4VPhysicalVolume* physicalVolume = GetPhysicalVolume(sensitiveVolume); - if (!physicalVolume) { + if (physicalVolume == nullptr) { // sensitive volume was not found, perhaps the user specified a logical volume auto physicalVolumes = geometryInfo.GetAllPhysicalVolumesFromLogical(sensitiveVolume); if (physicalVolumes.size() == 1) { @@ -114,7 +114,7 @@ G4VPhysicalVolume* DetectorConstruction::Construct() { } } - if (!physicalVolume) { + if (physicalVolume == nullptr) { G4cout << "ERROR: Sensitive volume '" << sensitiveVolume << "' not found" << G4endl; exit(1); } From 943fb658e703647ee100e986034d925884194cfd Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 7 Dec 2022 16:34:26 +0100 Subject: [PATCH 3/7] comment out unused variable --- src/DetectorConstruction.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DetectorConstruction.cxx b/src/DetectorConstruction.cxx index a0c5fc7c..47663efc 100644 --- a/src/DetectorConstruction.cxx +++ b/src/DetectorConstruction.cxx @@ -124,7 +124,7 @@ G4VPhysicalVolume* DetectorConstruction::Construct() { Double_t mz = restG4Metadata->GetMagneticField().Z() * tesla; G4MagneticField* magField = new G4UniformMagField(G4ThreeVector(mx, my, mz)); - G4FieldManager* localFieldMgr = new G4FieldManager(magField); + // G4FieldManager* localFieldMgr = new G4FieldManager(magField); G4FieldManager* fieldMgr = G4TransportationManager::GetTransportationManager()->GetFieldManager(); fieldMgr->SetDetectorField(magField); fieldMgr->CreateChordFinder(magField); From 1b63bf910badd49b3c62a99625105df49f1b63c0 Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 7 Dec 2022 16:36:18 +0100 Subject: [PATCH 4/7] remove impossible condition --- src/DetectorConstruction.cxx | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/DetectorConstruction.cxx b/src/DetectorConstruction.cxx index 47663efc..884f9153 100644 --- a/src/DetectorConstruction.cxx +++ b/src/DetectorConstruction.cxx @@ -129,17 +129,12 @@ G4VPhysicalVolume* DetectorConstruction::Construct() { fieldMgr->SetDetectorField(magField); fieldMgr->CreateChordFinder(magField); - if (physicalVolume != nullptr) { - G4LogicalVolume* volume = physicalVolume->GetLogicalVolume(); - G4Material* material = volume->GetMaterial(); - G4cout << "Sensitive volume properties:" << G4endl; - G4cout << "\t- Material: " << material->GetName() << G4endl; - G4cout << "\t- Temperature: " << material->GetTemperature() << " K" << G4endl; - G4cout << "\t- Density: " << material->GetDensity() / (g / cm3) << " g/cm3" << G4endl; - } else { - cerr << "Physical volume for sensitive volume '" << sensitiveVolume << "' not found!" << endl; - exit(1); - } + G4LogicalVolume* volume = physicalVolume->GetLogicalVolume(); + G4Material* material = volume->GetMaterial(); + G4cout << "Sensitive volume properties:" << G4endl; + G4cout << "\t- Material: " << material->GetName() << G4endl; + G4cout << "\t- Temperature: " << material->GetTemperature() << " K" << G4endl; + G4cout << "\t- Density: " << material->GetDensity() / (g / cm3) << " g/cm3" << G4endl; const auto& primaryGeneratorInfo = restG4Metadata->GetGeant4PrimaryGeneratorInfo(); // Getting generation volume @@ -159,7 +154,7 @@ G4VPhysicalVolume* DetectorConstruction::Construct() { cout << "ERROR: The generator volume '" << primaryGeneratorInfo.GetSpatialGeneratorFrom() << "'was not found in the geometry" << endl; exit(1); - return worldVolume; + // return worldVolume; } fGeneratorTranslation = pVol->GetTranslation(); From 469825b8b628b609b3bb9932744b003e2b98bcbd Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 7 Dec 2022 16:37:04 +0100 Subject: [PATCH 5/7] normalized variable names --- src/DetectorConstruction.cxx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/DetectorConstruction.cxx b/src/DetectorConstruction.cxx index 884f9153..eb2e8fe2 100644 --- a/src/DetectorConstruction.cxx +++ b/src/DetectorConstruction.cxx @@ -220,17 +220,17 @@ G4VPhysicalVolume* DetectorConstruction::Construct() { return worldVolume; } -G4VPhysicalVolume* DetectorConstruction::GetPhysicalVolume(const G4String& physVolName) const { - G4PhysicalVolumeStore* physVolStore = G4PhysicalVolumeStore::GetInstance(); +G4VPhysicalVolume* DetectorConstruction::GetPhysicalVolume(const G4String& physicalVolumeName) const { + G4PhysicalVolumeStore* physicalVolumeStore = G4PhysicalVolumeStore::GetInstance(); TRestGeant4Metadata* restG4Metadata = fSimulationManager->GetRestMetadata(); const auto& geometryInfo = restG4Metadata->GetGeant4GeometryInfo(); - vector::const_iterator physVol; - for (physVol = physVolStore->begin(); physVol != physVolStore->end(); physVol++) { - auto name = (*physVol)->GetName(); + vector::const_iterator physicalVolume; + for (physicalVolume = physicalVolumeStore->begin(); physicalVolume != physicalVolumeStore->end(); + physicalVolume++) { + auto name = (*physicalVolume)->GetName(); auto alternativeName = (G4String)geometryInfo.GetAlternativeNameFromGeant4PhysicalName(name); - - if (name == physVolName || alternativeName == physVolName) { - return *physVol; + if (name == physicalVolumeName || alternativeName == physicalVolumeName) { + return *physicalVolume; } } From 1817f12e3fc11c664bdd7f33d57620ba24523978 Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 7 Dec 2022 18:21:38 +0100 Subject: [PATCH 6/7] update reference range for example --- examples/04.MuonScan/ValidateCosmicMuonsFromWall.C | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/04.MuonScan/ValidateCosmicMuonsFromWall.C b/examples/04.MuonScan/ValidateCosmicMuonsFromWall.C index 94a34f4e..5a25eb4a 100644 --- a/examples/04.MuonScan/ValidateCosmicMuonsFromWall.C +++ b/examples/04.MuonScan/ValidateCosmicMuonsFromWall.C @@ -29,8 +29,8 @@ Int_t ValidateCosmicMuonsFromWall(const char* filename) { } cout << "Run entries: " << run.GetEntries() << endl; - if (run.GetEntries() < 950 || run.GetEntries() > 1000) { - cout << "The number of entries is not between 950 and 1000!" << endl; + if (run.GetEntries() < 900 || run.GetEntries() > 1000) { + cout << "The number of entries is not in the required range!" << endl; cout << "Number of entries : " << run.GetEntries() << endl; return 3; } From b89b0af47720a3d0c610ed672f89fe304def98dc Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 7 Dec 2022 22:38:53 +0100 Subject: [PATCH 7/7] addressed https://github.com/rest-for-physics/restG4/pull/88#discussion_r1042528152 --- examples/04.MuonScan/ValidateCosmicMuonsFromWall.C | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/04.MuonScan/ValidateCosmicMuonsFromWall.C b/examples/04.MuonScan/ValidateCosmicMuonsFromWall.C index 5a25eb4a..a82caedc 100644 --- a/examples/04.MuonScan/ValidateCosmicMuonsFromWall.C +++ b/examples/04.MuonScan/ValidateCosmicMuonsFromWall.C @@ -29,14 +29,15 @@ Int_t ValidateCosmicMuonsFromWall(const char* filename) { } cout << "Run entries: " << run.GetEntries() << endl; - if (run.GetEntries() < 900 || run.GetEntries() > 1000) { - cout << "The number of entries is not in the required range!" << endl; - cout << "Number of entries : " << run.GetEntries() << endl; + std::pair nEntriesRange = {900., 1000.}; + const int entries = run.GetEntries(); + if (entries < nEntriesRange.first || entries > nEntriesRange.second) { + cout << "The number of entries (" << entries << ") is out of range: [" << nEntriesRange.first << " - " + << nEntriesRange.second << "]" << endl; return 3; } cout << "All tests passed! [\033[32mOK\033[0m]\n"; - // Other tests like opening other metadata classes. Detector TGeoManager, etc. return 0; }