Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hiding subtraction volumes from Geant4 visualization #43

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/g4app.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include <filesystem>

#include "G4BooleanSolid.hh"
#include "G4Event.hh"
plexoos marked this conversation as resolved.
Show resolved Hide resolved
#include "G4GDMLParser.hh"
#include "G4LogicalVolumeStore.hh"
#include "G4OpBoundaryProcess.hh"
#include "G4OpticalPhoton.hh"
#include "G4PhysicalConstants.hh"
#include "G4PrimaryParticle.hh"
#include "G4PrimaryVertex.hh"
#include "G4SubtractionSolid.hh"
#include "G4SystemOfUnits.hh"
#include "G4ThreeVector.hh"
#include "G4Track.hh"
Expand All @@ -30,6 +33,31 @@
#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<G4SubtractionSolid *>(solid))
return true;

// If the solid is a Boolean solid, check its constituent solids
G4BooleanSolid *booleanSolid = dynamic_cast<G4BooleanSolid *>(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
{

Expand All @@ -46,6 +74,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;
}

Expand Down