ExampleB1 eventaction.cc

Please fill out the following information to help in answering your question, and also see tips for posting code snippets. If you don’t provide this information it will take more time to help with your problem!

Geant4 Version: 11.4.2
Operating System:Windows
*Compiler/Version:MVSC 2026
*
CMake Version:4.3.0


Hello everyone,

I just started learning geant4, and I started with modifying exampleB1 to use geaneral particle source instead of the particle gun.

The problem is that i keep getting energy deposited in my scoring volume as zero each time I run the simulation.

Can anyone help with identifying the probable cause of this problem?

Hi Francisca,

There’s not really enough information here to help. Could you post your modifications to exampleB1 to use the GPS (see How to post code snippets for posting code), and what macro commands you’re using to set up the GPS for running?

so this is my PrimaryGeneratorAction.cc


```
namespace myfirst
{


    PrimaryGeneratorAction::PrimaryGeneratorAction()
    {
        
        fParticleSource = new G4GeneralParticleSource();
    }


    PrimaryGeneratorAction::~PrimaryGeneratorAction()
    {
        delete fParticleSource;
    }

   

    void PrimaryGeneratorAction::GeneratePrimaries(G4Event* event)
    {

        fParticleSource->GeneratePrimaryVertex(event);
    }

}  

```




macro commands  for the source definition are;

```
/run/initialize

/gps/particle gamma
/gps/energy 140 keV
/gps/pos/centre -0.1 0 0 m
/gps/pos/type Point

/gps/ang/type iso

```




and the EventAction.cc


```
namespace myfirst
{


	EventAction::EventAction(RunAction* runAction) : fRunAction(runAction) {}


	void EventAction::BeginOfEventAction(const G4Event*)
	{
		fEdep = 0.;
	}


	void EventAction::EndOfEventAction(const G4Event*)
	{
		// accumulate statistics in run action
		fRunAction->AddEdep(fEdep);
	}


} 
``` 






Also this is for my detectorconstruction.cc


```
namespace myfirst
{


    G4VPhysicalVolume* DetectorConstruction::Construct()
    {
        // Get nist material manager
        G4NistManager* nist = G4NistManager::Instance();

       
        //initial variables to be used throughout
        G4bool checkOverlaps = true;


        // Building my World
        //
        G4double world_sizeX = 1.0 * m;
        G4double world_sizeY = 1.5 * m;
        G4double world_sizeZ = 0.8 * m;

        G4Material* world_mat = nist->FindOrBuildMaterial("G4_AIR");

        auto solidWorld =
            new G4Box("World",  // its name
                0.5 * world_sizeX, 0.5 * world_sizeY, 0.5 * world_sizeZ);  // its size

        auto logicWorld = new G4LogicalVolume(solidWorld,  // its solid
            world_mat,  // its material
            "World");  // its name

        auto physWorld = new G4PVPlacement(nullptr,  // no rotation
            G4ThreeVector(),  // at (0,0,0)
            logicWorld,  // its logical volume
            "World",  // its name
            nullptr,  // its mother  volume
            false,  // no boolean operation
            0,  // copy number
            checkOverlaps);  // overlaps checking

        


        // Constructing my /working area
        
        
        G4double env_sizeX = 0.7 * m;
        G4double env_sizeY = 0.6 * m;
        G4double env_sizeZ = 0.5 * m;

        auto solidOuter = new G4Box("OuterWall",  // its name
            0.5 * env_sizeX, 0.5 * env_sizeY, 0.5 * env_sizeZ);  // its size


        G4double wall = 10 * cm;
        //G4double clearance = 0.1 * mm;

        //dimensions of the inner wall

        G4double in_sizeX = 0.7 * m - wall;
        G4double in_sizeY = 0.6 * m - wall;
        G4double in_sizeZ = 0.5 * m - wall;


        auto solidInner = new G4Box("Cabinet",  // its name
            0.5 * in_sizeX, 0.5 * in_sizeY, 0.5 * in_sizeZ);  // its size


        // for the actual working area

        auto solidWall = new G4SubtractionSolid(
            "Wall",
            solidOuter,
            solidInner);

        // creating the logic volumes for both the wall (Outer) and the inner working area (Cabinet)

        // first creating stainless steel

        auto Fe = nist->FindOrBuildElement("Fe");
        auto Cr = nist->FindOrBuildElement("Cr");
        auto Ni = nist->FindOrBuildElement("Ni");

        G4double density = 8.0 * g / cm3;

        auto stainlessSteel =
            new G4Material("StainlessSteel",
                density,
                3);

        stainlessSteel->AddElement(Fe, 70. * perCent);
        stainlessSteel->AddElement(Cr, 18. * perCent);
        stainlessSteel->AddElement(Ni, 12. * perCent);

        auto logicOuter = new G4LogicalVolume(solidWall,  // its solid
            stainlessSteel,  // its material
            "Wall");  // its name

        
        new G4PVPlacement(nullptr,  // no rotation
            G4ThreeVector(),  // at (0,0,0)
            logicOuter,  // its logical volume
            "Wall",  // its name
            logicWorld,  // its mother  volume
            false,  // no boolean operation
            0,  // copy number
            checkOverlaps);  // overlaps checking

        //
        // now for the working area
        G4Material* cabinet_air = nist->FindOrBuildMaterial("G4_AIR");
        auto logicCabinet = new G4LogicalVolume(solidInner,  // its solid
            cabinet_air,  // its material
            "Cabinet");  // its name

        new G4PVPlacement(nullptr,  // no rotation
            G4ThreeVector(),  // at (0,0,0)
            logicCabinet,  // its logical volume
            "Cabinet",  // its name
            logicOuter,  // its mother  volume
            false,  // no boolean operation
            0,  // copy number
            checkOverlaps);  // overlaps checking
     
        
        // 
        // Shape 1 (calibrator)
        //
        G4Material* calib_mat = nist->FindOrBuildMaterial("G4_AIR");
        G4ThreeVector pos1 = G4ThreeVector(-0.1 *m , 0*m, 0 *m);

        // Outer dimensions of the calibrator section shape
        G4double pRMin_1 = 0. * cm, pRMax_1 = 8.6 * cm;
        G4double pDz_1 = 0.5 * 43.8 * cm;
        G4double pSPhi_1 = 0. * deg;
        G4double pDPhi_1 = 360. * deg;
        
        auto solidCalibOuter = new G4Tubs("Calib_Outer", pRMin_1, pRMax_1, pDz_1, pSPhi_1, pDPhi_1);


        // Inner dimensions of the calibrator
        G4double pRMin_2 = 0. * cm, pRMax_2 = 3.05 * cm;
        G4double pDz_2 = 0.5 * 25.3 * cm;
        G4double pSPhi_2 = 0. * deg;
        G4double pDPhi_2 = 360. * deg;

        auto solidCalibInner = new G4Tubs("Calib_Inner", pRMin_2, pRMax_2, pDz_2, pSPhi_1, pDPhi_1);


        auto solidCalib = new G4SubtractionSolid(
            "Calibrator",
            solidCalibOuter,
            solidCalibInner);


        auto logicCalib = new G4LogicalVolume(solidCalib,  // its solid
            calib_mat,  // its material
            "Calibrator");  // its name
           // nullptr,
            //G4ThreeVector(0,0, 9.25*cm)); // offset(halflength of outer -halflength of inner +/- depending on where the hole should start


        auto rotX = new G4RotationMatrix();
        rotX->rotateX(-90. * deg);


        new G4PVPlacement(rotX,  // no rotation
            pos1,  // at position
            logicCalib,  // its logical volume
            "Calibrator",  // its name
            logicCabinet,  // its mother  volume
            false,  // no boolean operation
            0,  // copy number
            checkOverlaps);  // overlaps checking

        
        // Shape 2

        G4Material* shape2_mat = nist->FindOrBuildMaterial("G4_SKIN_ICRP");
        G4ThreeVector pos2 = G4ThreeVector(0.2 *m, 0 * m, 0* m);

        // boxshape for scoring volume
        G4double shape2_dxa = 10 * cm;
        G4double shape2_dya = 10 * cm;
        G4double shape2_dza = 10 * cm;

        auto solidShape2 =
            new G4Box("Shape2",  // its name
                0.5 * shape2_dxa, 0.5 * shape2_dya, 0.5 * shape2_dza);  // its size

        auto logicShape2 = new G4LogicalVolume(solidShape2,  // its solid
            shape2_mat,  // its material
            "Shape2");  // its name

        new G4PVPlacement(nullptr,  // no rotation
            pos2,  // at position
            logicShape2,  // its logical volume
            "Shape2",  // its name
            logicCabinet,  // its mother  volume
            false,  // no boolean operation
            0,  // copy number
            checkOverlaps);  // overlaps checking


        // now setting visibility
        //for wall

        auto wallVis = new G4VisAttributes(G4Colour(0.7, 0.7, 0.7,0.05));
        wallVis->SetVisibility(true);
        wallVis->SetForceSolid(true);

        logicOuter->SetVisAttributes(wallVis);

        //for working area or cabinet

        //logicCabinet->SetVisAttributes(G4VisAttributes::GetInvisible());
        auto cabVis = new G4VisAttributes(G4Colour(0.0, 0.5, 0.5, 0.5));
        cabVis->SetVisibility(true);
        cabVis->SetForceSolid(true);

        logicCabinet->SetVisAttributes(cabVis);

        // for calibrator

        auto calibVis = new G4VisAttributes(G4Colour::Blue());

        calibVis->SetVisibility(true);
        calibVis->SetForceSolid(true);

        logicCalib->SetVisAttributes(calibVis);


        // for scoring volume or shape 2 

        auto shape2Vis = new G4VisAttributes(G4Colour::Red());

        shape2Vis->SetVisibility(true);
        shape2Vis->SetForceSolid(true);

        logicShape2->SetVisAttributes(shape2Vis);


        // Set  scoring volume
        //
        fScoringVolume = logicShape2;

        //
        // always return the physical World
        //
        return physWorld;
    }

    

} ```



Hi Francisca,

I ran your code and I get 0 energy too. I think the problem is in your geometry, not in GPS or scoring.

Looking at your placements: you put Shape2 inside Cabinet, and you put Cabinet inside Wall :

new G4PVPlacement(..., logicCabinet, "Cabinet", logicOuter, ...);  // logicOuter = Wall

But is that what you want? Wall is a subtraction solid (solidOuter - solidInner), so it is only the thin steel shell with an empty hole in the middle. You are placing Cabinet into that hole - which is outside the wall’s material. In geant4 a daughter must sit inside its mother’s solid, so the navigator never really enters Cabinet, and never enters Shape2…

Try placing Cabinet in the World instead of in the Wall:

new G4PVPlacement(..., logicCabinet, "Cabinet", logicWorld, ...);

After this change I get a normal, non-zero energy. Wall and Cabinet then sit side by side in the world, and Shape2 becomes reachable.

@dkonst thank you so much, it worked now. And I think I am starting to understand what you meant with the placement.

Much appreciated.

Hi Francisca,

Great to hear it works now…

Since you are just getting going with Geant4, I’d strongly recommend the “First steps with Geant4” beginner course given by Mihály Novák:

It’s aimed exactly at newcomers and the full lecture slides and video recordings are attached to the timetable, so you can follow it at your own pace…

@dkonst ,

Thank you once again for this information.

Will be be sure to follow it.

Much appreciated once again.