Trying to read geometry from another file

Hi, I am working on building beam lines with Geant4 and I would like to build the magnets in separate files and be able to read them from DetectorConstruction and place them in the logicalWorld as per the beam line. But I get a segmentation fault when I try to place the logical volume within the world. I am including some snippets of the code which concerns this part:

DetectorConstruction.cc

DetectorConstruction::DetectorConstruction()
:solidWorld(0),logicWorld(0),physiWorld(0), fMagneticBoxMat(NULL), fMagneticCoilMat(NULL), fCorePipeMat(NULL), fDefaultMat(NULL)
{
mbp = new MagnetMBP();
}

//…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…

DetectorConstruction::~DetectorConstruction()
{
delete mbp;
delete fMagneticBoxMat;
delete fMagneticCoilMat;
delete fCorePipeMat;
delete fDefaultMat;

}

//…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…

G4VPhysicalVolume* DetectorConstruction::Construct()
{

double WorldSize = 10.*m;
double WorldSizeX = 10.*m;
double WorldSizeY = 10.*m;

solidWorld = new G4Box(“World”, //its name
WorldSizeX/2,WorldSizeY/2,WorldSize/2); //its size

logicWorld = new G4LogicalVolume(solidWorld, //its solid
fDefaultMat, //its material
“World”); //its name

physiWorld = new G4PVPlacement(0, //no rotation
G4ThreeVector(), //at (0,0,0)
logicWorld, //its logical volume
“World”, //its name
0, //its mother volume
false, //no boolean operation
0); //copy number

mbp->ConstructMagnet(logical_MBP,2000,200);

MagnetMBP.hh

#ifndef MagnetMBP_h
#define MagnetMBP_h 1

#include “G4VUserDetectorConstruction.hh”
#include “globals.hh”

#include “G4MagneticField.hh”
#include “G4UniformMagField.hh”
#include “G4FieldManager.hh”
#include “G4TransportationManager.hh”
#include “G4Mag_UsualEqRhs.hh”
#include “G4MagIntegratorStepper.hh”
#include “G4ChordFinder.hh”

#include “G4ExplicitEuler.hh”
#include “G4ImplicitEuler.hh”
#include “G4SimpleRunge.hh”
#include “G4SimpleHeum.hh”
#include “G4ClassicalRK4.hh”
#include “G4UserLimits.hh”

#include “G4Tubs.hh”
#include “G4Box.hh”

class G4Tubs;
class G4Box;
class G4LogicalVolume;
class G4VPhysicalVolume;
class G4Material;
class G4UniformMagField;
class G4VPVParameterisation;
class G4UserLimits;
class DetectorMessenger;

class MagnetMBP
{

public: MagnetMBP();
~MagnetMBP();

public:
void ConstructMagnet(G4LogicalVolume*, G4double, G4double);
void DefineMBPMat();

private:

G4Box* MBP_geom; //pointer to the MBPL Geom
G4LogicalVolume* logic_MBP_geom; //pointer to the logical MBPL

G4Material* fMBPBoxMat;
G4Material* fMBPCoilMat;
G4Material* fMBPCorePipeMat;
G4Material* fMBPDefaultMat;

};

#endif

MagnetMBP.cc

#include “MagnetMBP.hh”
#include “DetectorConstruction.hh”
#include “DetectorMessenger.hh”

#include “G4Material.hh”
#include “G4NistManager.hh”

#include “G4Box.hh”
#include “G4Tubs.hh”
#include “G4LogicalVolume.hh”
#include “G4PVPlacement.hh”
#include “G4PVReplica.hh”

#include “G4MagneticField.hh”
#include “G4UniformMagField.hh”
#include “G4FieldManager.hh”
#include “G4TransportationManager.hh”
#include “G4Mag_UsualEqRhs.hh”
#include “G4MagIntegratorStepper.hh”
#include “G4ChordFinder.hh”

#include “G4ExplicitEuler.hh”
#include “G4ImplicitEuler.hh”
#include “G4SimpleRunge.hh”
#include “G4SimpleHeum.hh”
//#include “G4ClassicalRK4.hh”
#include “G4HelixExplicitEuler.hh”
#include “G4HelixImplicitEuler.hh”
#include “G4HelixSimpleRunge.hh”
#include “G4CashKarpRKF45.hh”
#include “G4RKG3_Stepper.hh”
#include <G4SubtractionSolid.hh>
#include <G4UnionSolid.hh>

#include “G4GeometryManager.hh”
#include “G4PhysicalVolumeStore.hh”
#include “G4LogicalVolumeStore.hh”
#include “G4SolidStore.hh”

#include “G4VisAttributes.hh”
#include “G4Colour.hh”
#include “G4PhysicalConstants.hh”
#include “G4SystemOfUnits.hh”

#include “G4GDMLParser.hh”

MagnetMBP::MagnetMBP()
{;}

MagnetMBP::~MagnetMBP()
{;}

void MagnetMBP::ConstructMagnet(G4LogicalVolume* box_logical, G4double l, G4double a)
{
DefineMBPMat();

//MBP
G4double H = a-110.*mm;
G4double MBP_x = 1800.*mm;
G4double MBP_y = H+1230.*mm;
G4double MBP_z = l+670.*mm;

MBP_geom = new G4Box(“MBP_geom”,MBP_x/2,MBP_y/2,MBP_z/2);
logic_MBP_geom = new G4LogicalVolume(MBP_geom,fMBPDefaultMat,“MBP_geom”);

   //MBP Outer box without copper coils and outer laminations

G4double box_x = 1740.*mm;
G4double box_y = H+1130.*mm;
G4double box_z = l;


box_logical = logic_MBP_geom;

}

void MagnetMBP::DefineMBPMat()
{

//This function illustrates the possible ways to define materials

G4String name, symbol ; // a=mass of a mole;
G4double a, z, density ; // z=mean number of protons;
G4int nel, natoms ;
G4int ncomponents;
G4double fractionmass, pressure, temperature;

//
// define Elements
//

a = 1.01g/mole;
G4Element
elH = new G4Element(name=“Hydrogen”,symbol=“H” , z= 1., a);

a = 12.01g/mole;
G4Element


fMBPBoxMat = Iron;
fMBPCoilMat = Copper;
fMBPCorePipeMat = Stain_Steel;
fMBPDefaultMat = Air;
//G4cout << *(G4Material::GetMaterialTable()) << G4endl;

}

The segmentation fault message I receive is:

*** Break *** segmentation violation

===========================================================

There was a crash.

This is the entire stack trace of all threads:

===========================================================

#0 0x00007fe4288c141c in waitpid () from /lib64/libc.so.6

#1 0x00007fe42883ef12 in do_system () from /lib64/libc.so.6

#2 0x00007fe42e91203d in TUnixSystem::StackTrace() () from /cvmfs/sft.cern.ch/lcg/views/LCG_94/x86_64-centos7-gcc7-opt/lib/libCore.so

#3 0x00007fe42e9148d4 in TUnixSystem::DispatchSignals(ESignals) () from /cvmfs/sft.cern.ch/lcg/views/LCG_94/x86_64-centos7-gcc7-opt/lib/libCore.so

#4

#5 0x00007fe42ef96e31 in G4LogicalVolume::GetFieldManager() const () from /cvmfs/sft.cern.ch/lcg/views/LCG_94/x86_64-centos7-gcc7-opt/lib64/libG4geometry.so

#6 0x00007fe42ef9824e in G4LogicalVolume::AddDaughter(G4VPhysicalVolume*) () from /cvmfs/sft.cern.ch/lcg/views/LCG_94/x86_64-centos7-gcc7-opt/lib64/libG4geometry.so

#7 0x00007fe42f1aea9f in G4PVPlacement::G4PVPlacement(CLHEP::HepRotation*, CLHEP::Hep3Vector const&, G4LogicalVolume*, G4String const&, G4LogicalVolume*, bool, int, bool) () from /cvmfs/sft.cern.ch/lcg/views/LCG_94/x86_64-centos7-gcc7-opt/lib64/libG4geometry.so

#8 0x000000000041341e in DetectorConstruction::Construct() ()

#9 0x00007fe4325fd7ba in G4RunManager::InitializeGeometry() () from /cvmfs/sft.cern.ch/lcg/views/LCG_94/x86_64-centos7-gcc7-opt/lib64/libG4run.so

#10 0x00007fe4325fd6aa in G4RunManager::Initialize() () from /cvmfs/sft.cern.ch/lcg/views/LCG_94/x86_64-centos7-gcc7-opt/lib64/libG4run.so

#11 0x000000000040e994 in main ()

===========================================================

The lines below might hint at the cause of the crash.

You may get help by asking at the ROOT forum http://root.cern.ch/forum

Only if you are really convinced it is a bug in ROOT then please submit a

report at http://root.cern.ch/bugs Please post the ENTIRE stack trace

from above as an attachment in addition to anything else

that might help us fixing this issue.

===========================================================

#5 0x00007fe42ef96e31 in G4LogicalVolume::GetFieldManager() const () from /cvmfs/sft.cern.ch/lcg/views/LCG_94/x86_64-centos7-gcc7-opt/lib64/libG4geometry.so

#6 0x00007fe42ef9824e in G4LogicalVolume::AddDaughter(G4VPhysicalVolume*) () from /cvmfs/sft.cern.ch/lcg/views/LCG_94/x86_64-centos7-gcc7-opt/lib64/libG4geometry.so

#7 0x00007fe42f1aea9f in G4PVPlacement::G4PVPlacement(CLHEP::HepRotation*, CLHEP::Hep3Vector const&, G4LogicalVolume*, G4String const&, G4LogicalVolume*, bool, int, bool) () from /cvmfs/sft.cern.ch/lcg/views/LCG_94/x86_64-centos7-gcc7-opt/lib64/libG4geometry.so

#8 0x000000000041341e in DetectorConstruction::Construct() ()

#9 0x00007fe4325fd7ba in G4RunManager::InitializeGeometry() () from /cvmfs/sft.cern.ch/lcg/views/LCG_94/x86_64-centos7-gcc7-opt/lib64/libG4run.so

#10 0x00007fe4325fd6aa in G4RunManager::Initialize() () from /cvmfs/sft.cern.ch/lcg/views/LCG_94/x86_64-centos7-gcc7-opt/lib64/libG4run.so

#11 0x000000000040e994 in main ()

===========================================================

Please let me know what I am doing wrong… Thank you very much in advance…

Best Regards,
Dipanwita

I fixed the issue by returning the logical volume from the MagnetMBP.cc. So it works fine now :slight_smile: