HOWTO: Check for overlaps / extrusions in a geometry using TGeoChecker

Root's TGeoChecker class provides some useful functions to check for errors in geometries. A simple example (in pseudocode) would look like:

# Create a new manager
TGeoManager *manager = new TGeoManager()

# Create the checker code
TGeoChecker *checker = new TGeoChecker( manager );

# Check for overlap between top-level and daughters to a tolerance
# of 0.001 cm. And provide a "f"ull check of the volume.
TGeoVolume *top = manager->GetTopVolume();
checker->CheckOverlaps( top, 0.001, "f" )

# Finally print a list of the overlaps and extrusions

A much more sophisticated (and working) example is included below. It takes as input the name of the volume you want to examine, then recursively follows the daughter volumes to examine the full geometry. It should be noted that many of the overlaps which this code will find will likely be intended in the geometry... one typically uses two or more overlapping volumes to construct shapes more complicated than what root/geant/etc... provides by default. Extrusions are a different matter. When a volume extrudes it's mother volume, the offending volume will never be seen by particles traversing the geometry.

TGeoChecker *gGeoChecker = 0;
Double_t gTolerance = 0.0001;
Int_t gRecurseLevel = 0;

void checker( const Char_t *toplevel = "TPCE" )


// Set the top volume we are interested in checking, and
// punt if it wasn't found in the geometry
TGeoVolume *top = gGeoManager->GetVolume( toplevel );
if ( !top )
std::cout << Form("%s was not found",toplevel) << std::endl;

// Instance of the handy but little known TGeoChecker
gGeoChecker = new TGeoChecker( gGeoManager );

// Check for overlaping / extruding volumes in the specified
// volume, and recursively descend into all sub volumes
checkForOverlaps( top );
std::cout << std::endl;
std::cout << std::endl;

// Print out any overlaps found

void checkForOverlaps( TGeoVolume *volume )

if ( gRecurseLevel < 2 )
std::cout << Form("\nExamining: %s ",volume->GetName());

gRecurseLevel++; // increment recursion level on call

// Check for overlaps in this volume
gGeoChecker->CheckOverlaps( volume, gTolerance, "f" );

TObjArray *nodes = volume->GetNodes();
TIter next(nodes);
TGeoNode *node;

// Iterate over all nodes in this volume, calling checkForOverlaps
// on each corresponding daughter volume. This may take a while,
// so the top-level daughters get a printout.
while ( node = (TGeoNode*)next() )
volume = node->GetVolume();

// std::cout << Form(".");

checkForOverlaps( volume );

gRecurseLevel--; // decrement recursion level on exit