Geant4/CPPGDML
Binding

We demonstrate here a practical application of CPPGDML for Geant4 geometry construction. The G4VUserDetectorConstruction abstract class is the base class for any Geant4 detector geometry description. In particular, one has to implement its Construct() method which is responsible for the actual geometry construction and returns the pointer to the world volume. In standard Geant4 application its implementation contains direct calls to Geant4 materials, geometry APIs and few others. In case of GDML the actual construction of the Geant4 geometry tree (including the material definitions) is perfomed by the GDML processor. The following code listing demonstrates the implementation of the Geant4 user detector construction using CPPGDML:
#include "G4String.hh"
#include "G4VUserDetectorConstruction.hh"
#include "G4VPhysicalVolume.hh"

#include "SAXProcessor.hh"
#include "ProcessingConfigurator.hh"

class ExN03CalorimeterSD;
class ExN03DetectorMessenger;

class ExN03DetectorConstruction : public G4VUserDetectorConstruction
{
public:
  ExN03DetectorConstruction();
  ~ExN03DetectorConstruction();

public:
  G4VPhysicalVolume*
  Construct();     //The key method one has to implement

... removed some stuff for brevity

private:
  SAXProcessor sxp;                   //XML engine
  ProcessingConfigurator config;      //XML engine configuration
  G4VPhysicalVolume* fWorld;          //World volume
  ExN03CalorimeterSD* calorimeterSD;  //pointer to the sensitive detector
  ExN03DetectorMessenger* detectorMessenger;
};

#include "ExN03DetectorConstruction.hh"
#include "ExN03DetectorMessenger.hh"
#include "ExN03CalorimeterSD.hh"

#include "G4SDManager.hh"
#include "G4VisAttributes.hh"
#include "G4Colour.hh"

#include "GDMLProcessor.hh"
#include "GDMLExpressionEvaluator.hh"

ExN03DetectorConstruction::ExN03DetectorConstruction() : calorimeterSD( 0 ) {
  // create commands for interactive definition of the calorimeter
  detectorMessenger = new ExN03DetectorMessenger(this);
  sxp.Initialize();                // Initialize XML engine
  config.SetURI( "NO3.gdml" );     // Set the GDML data location
  config.SetSetupName( "N03" );    // Set the wanted setup name
  sxp.Configure( &config );        // Apply the configuration settings to XML engine
}
ExN03DetectorConstruction::~ExN03DetectorConstruction() {
  sxp.Finalize();                  // Shut-down XML engine properly
}
G4VPhysicalVolume* ExN03DetectorConstruction::Construct() {
  // Launch XML engine to process GDML data
  sxp.Run();

  // Retrieve the world volume from GDML processor
  fWorld = (G4VPhysicalVolume *)GDMLProcessor::GetInstance()->GetWorldVolume();

  // Check if the world volume seems to be fine
  if( fWorld == 0 ) {
    G4Exception(
       "Invalid world volume, check your setup selection criteria or GDML input!"
               );
  }
  //
  // Sensitive Detectors: Absorber and Gap
  //
  G4SDManager* SDman = G4SDManager::GetSDMpointer();
  if(!calorimeterSD) {
    calorimeterSD = new ExN03CalorimeterSD("CalorSD",this);
    SDman->AddNewDetector( calorimeterSD );
  }
  G4LogicalVolume* lv = 0;
  lv = FindLogicalVolume( "Absorber" );        // FindLogicalVolume makes use of
  if ( lv )                                    // GDMLProcessor which keeps track
    lv->SetSensitiveDetector(calorimeterSD);   // of all Geant4 objects it has created
  lv = FindLogicalVolume( "Gap" );             // during the GDML data processing
  if ( lv )                                    // In the same way one can retrieve
    lv->SetSensitiveDetector(calorimeterSD);   // materials or rotations, etc.
  //
  // Visualization attributes
  //
  lv = FindLogicalVolume( "World" );
  lv->SetVisAttributes (G4VisAttributes::Invisible);
  lv = FindLogicalVolume( "Calorimeter" );
  lv->SetVisAttributes (G4VisAttributes::Invisible);
  G4VisAttributes* simpleBoxVisAtt= new G4VisAttributes(G4Colour(1.0,1.0,1.0));
  simpleBoxVisAtt->SetVisibility(true);
  lv = FindLogicalVolume( "Layer" );
  lv->SetVisAttributes(simpleBoxVisAtt);
  return fWorld;
}

Back to GDML web page.