Skip to content

Commit

Permalink
Merge pull request #88 from rest-for-physics/lobis-check-geometry
Browse files Browse the repository at this point in the history
check possible issues in geometry and raise exception
  • Loading branch information
lobis authored Dec 8, 2022
2 parents 973a3f9 + b89b0af commit 126bebc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
9 changes: 5 additions & 4 deletions examples/04.MuonScan/ValidateCosmicMuonsFromWall.C
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ 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;
cout << "Number of entries : " << run.GetEntries() << endl;
std::pair<double, double> 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;
}
77 changes: 54 additions & 23 deletions src/DetectorConstruction.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,47 @@ 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<string> physicalVolumeNames;
vector<G4VPhysicalVolume*>::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<string> logicalVolumeNames;
vector<G4LogicalVolume*>::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();
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) {
Expand All @@ -78,7 +114,7 @@ G4VPhysicalVolume* DetectorConstruction::Construct() {
}
}

if (!physicalVolume) {
if (physicalVolume == nullptr) {
G4cout << "ERROR: Sensitive volume '" << sensitiveVolume << "' not found" << G4endl;
exit(1);
}
Expand All @@ -88,22 +124,17 @@ 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);

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
Expand All @@ -123,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();
Expand Down Expand Up @@ -189,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<G4VPhysicalVolume*>::const_iterator physVol;
for (physVol = physVolStore->begin(); physVol != physVolStore->end(); physVol++) {
auto name = (*physVol)->GetName();
vector<G4VPhysicalVolume*>::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;
}
}

Expand Down

0 comments on commit 126bebc

Please sign in to comment.