Simulation compiles but segmentation fault when running

Hello, I’ve to import an old simulation in a new one. In the old simulation, all the geometry was implemented in the DetectorConstruction, this is the old DetectorConstruction.

myDetectorConstruction.hh (9.6 KB)
myDetectorConstruction.cc (152.6 KB)

According to the phylosophy of the new one, the full geometry must NOT be implemented in the detector construction. Then I splitted the Detector construction of the old simulation in two files, one for detectors, one for targets and I call the function of the two files in the detector construction.

The simulation compiles without errors, but when I run it I get a segmentation fault error then the simulation crashes. This is the gdb output

gdb output.txt (8.2 KB)

I commented, in the Clusterspddresda.cc, the full

void Clusterspddresda::TripleClusterGeometry(G4int nbCluster, G4Transform3D positionTripleCluster, G4bool shieldingOption = 0)

except the lines

G4GDMLParser parser3;   
G4RotationMatrix rotationTripleCluster = G4RotationMatrix();
rotationTripleCluster.rotateY(-180.*deg);
//MB1 and MB2 different geometry and crystal ordering
if (strcmp("MB1", nameCluster[nbCluster]) == 0) {
    rotationTripleCluster.rotateZ(-30.*deg);
    //parser3.Read("gdml/TripleCluster/TripleClusterMB1.gdml",false);
    parser3.Read(Mb1fileName,false);
}
else if (strcmp("MB2", nameCluster[nbCluster]) == 0) {
    rotationTripleCluster.rotateZ(30.*deg);
    //parser3.Read("gdml/TripleCluster/TripleClusterMB2.gdml",false);       
    parser3.Read(Mb2fileName,false);                           
}

and I got segmentation fault error.

Then I commented the full void Clusterspddresda::SeptupleClusterHousing(G4int nbCluster, G4Transform3D positionSeptupleCluster, G4bool shieldingOption = 0){

except the lines for importing the gdml files and the simulation crashes

@@@ G4ParticleHPInelastic instantiated for particle He3 data directory variable is G4HE3HPDATA pointing to GEANT4/geant4-v11.0.0-install/share/Geant4-11.0.0/data/G4TENDL1.4/He3/Inelastic
Killed

Then it looked like to me that the problem is in the gdml files that I got from the old simulation. But my collegue (the developer of the new simulation that I’m currently using and modifying) thinks the problem is problem here with the arrays in the TripleClusterGeometry function in Clusterspddresda.cc:

if(CompName.contains("Crystal"))
                {
                        TripleClusterComp_physical[i]->GetLogicalVolume()->SetVisAttributes(vacVisAtt);
                        for(G4int nbCrystal=0; nbCrystal<NbOfCrystals[nbCluster]; nbCrystal++)
            {
                                if(CompName.contains(G4UIcommand::ConvertToString(nbCrystal+1)))
                                {
                                        TripleClusterCrystal_physical[nbCluster][nbCrystal] = TripleClusterComp_physical[i];
                    TripleClusterCrystal_logical[nbCluster][nbCrystal] = TripleClusterCrystal_physical[nbCluster][nbCrystal]->GetLogicalVol\
ume();
                                        CrystalwDeadLayersGeometry(nbCluster, nbCrystal, TripleClusterCrystal_physical[nbCluster][nbCrystal], dDeadLayerCap[nbC\
luster][nbCrystal], hDeadLayerCone[nbCluster][nbCrystal]);
                                }
                        }

But I don’t understand what should be the problem, even because I copied this piece of code from the old simulation (and I guess the old simulation worked, even if I never used it). Anyway, looking the code values of nbCluster is declared but I don’t read its value (or at least I don’t see where it is assigned).

Here the Clusterspddresda , the DetectorConstruction and the gdml files

gdml files WeTransfer - Send Large Files & Share Photos Online - Up to 2GB Free

Clusterspddresda.hh (7.8 KB)
Clusterspddresda.cc (96.6 KB)
DetectorConstruction.cc (3.8 KB)
DetectorConstruction.hh (1.5 KB)

I’d suggest building the application using the RelWithDebInfo or Debug build modes if using CMake. Running bt in gdb after the segfault should then reveal the exact line on which it occurs. That will help trace this, and the state of variables etc at that point can be checked.

Thank you @bmorgan.
Actually I used the bt command, as you can see in the gdb output.txt file that I attached in my previous message

you can see the result here

Thanks

Sure, but that output doesn’t show the line number in the .cc file where the segfault occurred. That’s what I suggested checking that your application is compiled with CMAKE_BUILD_TYPE set to Debug or RelWithDebInfo (or the -g flag is at least used if not using CMake).

Running the application in gdb with it built in one of those modes should give more detail on where, in terms of an actual line of code in a given file, not just a function, where the segfault occurred. That’s far easier than having to dig through a function line by line manually.

Thank you @bmorgan

can you kindly write me the commands to check that the application is compiled with CMAKE_BUILD_TYPE set to Debug or RelWithDebInfo (or the -g flag is at least used if not using CMake).
?

I’m using GEANT4 installed on my laboratory server.

Thanks

To build the application (not Geant4, that’s fine as is) in those modes, run:

$ cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo <otherargs>

or swap “RelWithDebInfo” for “Debug” (former is usually -O2 -g, latter is just -g). Checking that the mode is actually configured/used, run ccmake in the build directory which will open an ncurses GUI showing all the configurable CMake options and their current values. Look for the CMAKE_BUILD_TYPE entry.

The build itself can, assuming use of make, be run with make VERBOSE=1 to show the complete compiler/linker commands used and confirm that the -g flags is present.

Hi @bmorgan, thank you.
I found the cause of the segmentation fault.

It is due to this piece of code

int CrystalCopyNumber[nbCluster][nbCrystal]={{0},{1,2,3},{4,5,6},{7,8,9,10,11,12,13},{14,15,16,17,18,19,20},{21}};
	ActiveCrystal_physical[nbCluster][nbCrystal] = new G4PVPlacement(none,	// position in mother frame
										ActiveCrystal_logical[nbCluster][nbCrystal],		// its logical volume				  
										"ActiveCrystal_phys"+nameCluster[nbCluster]+std::to_string(nbCrystal),	// its name
										motherVolume,	// its mother  volume
										false,				// no boolean operations
										CrystalCopyNumber[nbCluster][nbCrystal]);					// copy number */

In particular it should be due to thid declaration

int CrystalCopyNumber[nbCluster][nbCrystal]={{0},{1,2,3},{4,5,6},{7,8,9,10,11,12,13},{14,15,16,17,18,19,20},{21}};

it doesn’t give errors during compiling it makes the simulation crashing.

I use this code because
-detector 1 has only one crystal then I set copy number 0
-decttor 2 has 3 crystals then I set copy numbers 1-2-3
-detector 3 has 3 crystals then I set copy numbers 4-5-6
etc

Do you know howw to solve?

Can you post what bt in the gdb run is now showing after the segfault please?

Hi @bmorgan, yes.

  1. I cd in the build directory
  2. I launch the command
    cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo /pddresda

being /pddresda the directory in which the main file and Cmake files are into.

  1. After the building I, launched the command
    make VERBOSE=1

this is what DCMAKE and make showed

build-make.txt (253.5 KB)

Then I aunched the gdb debug

gdb output.txt (8.8 KB)

As I said, in clusterpddresda.cc class I’ve six detectors ( see function void Clusterspddresda::InitializeDetectorParameters()).

Can60 has 1 crystal and I’ve to assign copynumber=0 to that crystal
MB1 has 3 crystals and I’ve to assign copy numbers=1,2,3 to these crsytals,
etc.

I have to do that to retrive the copynumber to know which detector has been fired during events.
Thanks

All I can say is that the GDB output:

#0  0x00000000004384e0 in std::operator+<char, std::char_traits<char>, std::allocator<char> > (
    __lhs=__lhs@entry=0x4927b5 "ActiveCrystal_phys", __rhs=...)
    at /afs/lngs.infn.it/software/gcc/10.2.0/include/c++/10.2.0/bits/char_traits.h:357
#1  0x000000000042d4a4 in Clusterspddresda::CrystalwDeadLayersGeometry(int, int, G4VPhysicalVolume*, double, double) ()
    at /afs/lngs.infn.it/software/gcc/10.2.0/include/c++/10.2.0/bits/basic_string.h:6594
#2  0x0000000000434ac3 in Clusterspddresda::TripleClusterGeometry(int, HepGeom::Transform3D, bool) ()
    at  pddresda/shared/src/Clusterspddresda.cc:591

suggests the error is occurring in the block of code starting on line 292 of Clusterspddresda.cc:

	ActiveCrystal_physical[nbCluster][nbCrystal] = new G4PVPlacement(none,	// position in mother frame
										ActiveCrystal_logical[nbCluster][nbCrystal],		// its logical volume				  
										"ActiveCrystal_phys"+nameCluster[nbCluster]+std::to_string(nbCrystal),	// its name
										motherVolume,	// its mother  volume
										false,				// no boolean operations
										CrystalCopyNumber[nbCluster][nbCrystal]);					// copy number 

and thus probably something in the expression:

"ActiveCrystal_phys"+nameCluster[nbCluster]+std::to_string(nbCrystal),	// its name

You’ll have to debug this in more detail to confirm that and determine the fix though.

Hi @bmorgan, yes, I also noticed that the problem was that part!
Thank you

“Segmentation fault” always accours my simulation. This is due to the array size was exceeded when assigned in most cases. Thus spend time on the array, especially if it exceeds its size when assigned.

Hi @FDQin yes, actually the segmentation fault was caused by that array. I had to define it setting the maximum number of crystals and detectors.
But actually, the geometry of the setup is messed up (I got it by an old simulation) and importing it in the new code is very hard. For example, now I solved the problem of the segmentation fault, but I get other problem (still related to the geometry…i.e. it looks like that it can’t import the H3 database)…