Skip to content

Commit

Permalink
Hiding subtraction volumes from Geant4 visualization (#43)
Browse files Browse the repository at this point in the history
Added code to check if any solids are subtraction solids. Hide these
volumes from visualization (NOT deleting anything)

This is needed as QT and OGL fails to plot these properly.

---------

Co-authored-by: Dmitri Smirnov <dmixsmi@gmail.com>
  • Loading branch information
ggalgoczi and plexoos authored Oct 16, 2024
1 parent daa3e05 commit 666cc0f
Showing 1 changed file with 52 additions and 0 deletions.
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"
#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

0 comments on commit 666cc0f

Please sign in to comment.