Let’s start the learning of Geant4 Simulation code with a very basic problem “How to make a solid box in Geant4?”
In Geant4, which is a toolkit for the simulation of the passage of particles through matter, creating a solid box involves defining a geometric shape using the appropriate classes provided by the toolkit. Geant4 uses a hierarchical structure for geometry, and you typically create your geometry by combining basic shapes to form more complex ones.
To create a simple Geant4 simulation that models a solid box, you’ll need at least the following source code and header files:
- Main Source Code File (Simulation.cc):
- Detector Construction Header File (DetectorConstruction.hh):
- Detector Construction Source Code File (DetectorConstruction.cc):
- Physics List Header File (PhysicsList.hh):
- Physics List Source Code File (PhysicsList.cc):
- Action Initialization Header File (ActionInitialization.hh):
- Action Initialization Source Code File (ActionInitialization.cc):
- Primary Generator Action Header File (PrimaryGeneratorAction.hh):
- Primary Generator Action Source Code File (PrimaryGeneratorAction.cc):
So, let’s discuss all the necessary files one by one.
Main Source Code File (Simulation.cc):
In Geant4, the main source code file typically serves as the entry point for your simulation. This file contains the main() function, which is the starting point for your Geant4 application. In this file, you set up the Geant4 run manager, define the geometry, physics processes, and other simulation components, and then initiate the run.
Here’s the necessary Simulation.cc file for our purpose:
This code assumes you have various user-defined classes like DetectorConstruction, PhysicsList and PrimaryGeneratorAction, RunAction, EventAction, and SteppingAction. These classes define the geometry, physics processes, particle generation, and user actions for your simulation.
In interactive mode, you can run the simulation by executing the ./Simulation command in your terminal.
Detector Construction Header File (DetectorConstruction.hh):
In Geant4, the DetectorConstruction header file is part of the user-defined code that describes the geometry of the detector used in the simulation. The DetectorConstruction class is responsible for defining the physical geometry of your experimental setup, specifying the materials used, and configuring sensitive detectors.
Here is the necessary DetectorConstruction.hh file for our purpose:
In the above code:
- DetectorConstruction is derived from G4VUserDetectorConstruction, which is a Geant4 base class that users can extend to define their own detector geometry.
- The constructor and destructor (DetectorConstruction and ~DetectorConstruction) can be used for any necessary setup or cleanup.
Your corresponding .cc file will implement the details of the detector construction. This may include defining materials, constructing solid shapes (like boxes, cylinders, etc.), creating logical volumes, and arranging them in a hierarchy to represent the geometry of your experimental setup.
Detector Construction Source Code File (DetectorConstruction.cc):
The Detector Construction source code file is part of the user-defined code that implements the construction of the detector geometry. This file works in conjunction with the DetectorConstruction.hh header file. The DetectorConstruction class, derived from G4VUserDetectorConstruction, is responsible for defining the physical geometry of the detector, specifying materials, and configuring sensitive detectors.
Here’s the necessary DetectorConstruction.cc file for our purpose:
This example demonstrates a simple case where the detector is a solid box made of air. It defines the materials, constructs a box shape using G4Box, creates a logical volume with the specified material, and places the logical volume in the world volume.
Physics List Header File (PhysicsList.hh):
The Physics List Header File is part of the user-defined code and typically contains the declaration of a class that inherits from G4VModularPhysicsList. The physics list defines which particle physics processes will be included in the simulation, such as electromagnetic interactions, hadronic processes, decay processes, etc.
Here’s the necessary what a PhysicsList.hh file for our purpose:
In the above code:
- PhysicsList is derived from G4VModularPhysicsList, which is a Geant4 base class for modular physics lists. Modular physics lists allow you to customize and combine different physics models for various processes.
- The constructor and destructor (PhysicsList and ~PhysicsList) can be used for any necessary setup or cleanup.
- You may need to include additional header files depending on the specific physics processes and models you intend to use in your simulation. Geant4 provides a variety of physics lists that cover a wide range of applications, and you can customize or extend them based on your needs.
The corresponding .cc file (e.g., PhysicsList.cc) will implement the details of the physics processes you want to include.
Physics List Source Code File (PhysicsList.cc):
This file works in conjunction with the PhysicsList.hh header file. The PhysicsList class, derived from G4VModularPhysicsList, is responsible for specifying which particle physics processes will be included in the simulation, such as electromagnetic interactions, hadronic processes, decay processes, and more.
Here’s the necessary PhysicsList.cc file for our purpose:
In the above code:
- PhysicsList is derived from G4VModularPhysicsList, which is a Geant4 base class for modular physics lists.
- The constructor (PhysicsList) registers various physics processes by creating instances of corresponding physics classes and passing them to the RegisterPhysics method.
Action Initialization Header File (ActionInitialization.hh):
It is part of the user-defined code and typically contains the declaration of a class responsible for initializing the user actions for the simulation. User actions include actions related to the initialization, tracking, and termination of events, as well as defining the behavior of primary particles.
Here’s the necessary ActionInitialization.hh file for our purpose:
In the above code:
- ActionInitialization is derived from G4VUserActionInitialization, which is a Geant4 base class for user action initialization.
- The constructor and destructor (ActionInitialization and ~ActionInitialization) can be used for any necessary setup or cleanup.
- The BuildForMaster() and Build() methods are virtual functions that you must implement in your corresponding .cc file. These methods are responsible for constructing the user actions for the master thread and worker threads, respectively.
The corresponding .cc file will implement the details of constructing the user actions.
Action Initialization Source Code File (ActionInitialization.cc):
This file works in conjunction with the ActionInitialization.hh header file. The ActionInitialization class, derived from G4VUserActionInitialization, is responsible for initializing and configuring various user actions, such as primary particle generation, run actions, event actions, and stepping actions.
Here’s the necessary ActionInitialization.cc file for our purpose:
In the above code:
- The constructor (ActionInitialization) and destructor (~ActionInitialization) do any necessary setup or cleanup.
- The Build() method is responsible for constructing actions for worker threads, such as primary particle generation, event actions, and stepping actions.
Primary Generator Action Header File (PrimaryGeneratorAction.hh):
The Primary Generator Action Header File is part of the user-defined code and typically contains the declaration of a class responsible for generating primary particles in the simulation. Primary particles are the initial particles created in the simulation that undergo interactions in the detector.
Here’s the necessary PrimaryGeneratorAction.hh file for our purpose:
In the above code:
- PrimaryGeneratorAction is derived from G4VUserPrimaryGeneratorAction, which is a Geant4 base class for user primary generator actions.
- The constructor and destructor (PrimaryGeneratorAction and ~PrimaryGeneratorAction) can be used for any necessary setup or cleanup.
- The GeneratePrimaries method is a virtual function that you must implement in your corresponding .cc file. This method is responsible for generating primary particles in the event.
The corresponding .cc file will implement the details of the primary particle generation.
Primary Generator Action Source Code File (PrimaryGeneratorAction.cc):
This file works in conjunction with the PrimaryGeneratorAction.hh header file. The PrimaryGeneratorAction class, derived from G4VUserPrimaryGeneratorAction, is responsible for defining and generating the initial primary particles in the simulation.
Here’s the necessary PrimaryGeneratorAction.cc file for our purpose:
In the above code:
- The constructor (PrimaryGeneratorAction) and destructor (~PrimaryGeneratorAction) do any necessary setup or cleanup.
Apart from these source codes and header files, you need one more file It is a Cmake List file.
Cmake List file (CMakeLists.txt)
In Geant4 simulations (and many other C++ projects), the Cmake List file is a file used by the CMake build system to configure, build, and install the software. CMake is a cross-platform build system that generates native build files for various platforms and build environments.
Here’s the CMakeLists.txt file for a Geant4 simulation for our purpose:
This complete code will create a cubic box made of water material with a size of 4\times 3\times 2\rm \ m^3. For this
- make a folder named “build” in the directory where you saved all these files
- open a terminal in the “build” folder and run the command
cmake ..
make
./sim
And finally, you will get the output like this.