diff --git a/src/g4app.h b/src/g4app.h index 1afc6d5..4f2db12 100644 --- a/src/g4app.h +++ b/src/g4app.h @@ -1,5 +1,8 @@ #include +#include "G4BooleanSolid.hh" +#include "G4SubtractionSolid.hh" +#include "G4LogicalVolumeStore.hh" #include "G4Event.hh" #include "G4GDMLParser.hh" #include "G4OpBoundaryProcess.hh" @@ -30,6 +33,32 @@ #include "U4/U4Touchable.h" #include "U4/U4Track.h" + +bool IsSubtractionSolid(G4VSolid* solid) +{ + if (!solid) + return false; + + // Check if the solid is directly a G4SubtractionSolid + if (dynamic_cast(solid)) + return true; + + // If the solid is a Boolean solid, check its constituent solids + G4BooleanSolid* booleanSolid = dynamic_cast(solid); + if (booleanSolid) + { + G4VSolid* solidA = booleanSolid->GetConstituentSolid(0); + G4VSolid* solidB = booleanSolid->GetConstituentSolid(1); + + // Recursively check the constituent solids + if (IsSubtractionSolid(solidA) || IsSubtractionSolid(solidB)) + return true; + } + + // For other solid types, return false + return false; +} + struct DetectorConstruction : G4VUserDetectorConstruction { @@ -46,6 +75,30 @@ struct DetectorConstruction : G4VUserDetectorConstruction G4CXOpticks::SetGeometry(world); + G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance(); + + static G4VisAttributes invisibleVisAttr(false); + + // Check if the store is not empty + if (lvStore && !lvStore->empty()) + { + // Iterate over all logical volumes in the store + for (auto& logicalVolume : *lvStore) + { + G4VSolid* solid = logicalVolume->GetSolid(); + + // Check if the solid uses subtraction + if (IsSubtractionSolid(solid)) + { + // Assign the invisible visual attributes to the logical volume + logicalVolume->SetVisAttributes(&invisibleVisAttr); + + // Optionally, print out the name of the logical volume + G4cout << "Hiding logical volume: " << logicalVolume->GetName() << G4endl; + } + } + } + return world; }