Introducing a Uniform Magnetic Field in the simulation


I am a beginner and currently I am trying to introduce a uniform magnetic field in my simulation by following the instructions in this doccument.

I tried to introduce the field within a G4Box made of vacuum. Here is a part of my code:

void MyDetectorConstruction::ConstructSDandField()
    G4MagneticField *magField = new G4UniformMagField(G4ThreeVector(0.5*tesla, 0., 0.));
    G4FieldManager *globalFieldMgr = G4TransportationManager::GetTransportationManager()->GetFieldManager();

    auto localFieldMgr = new G4FieldManager(magField);
    logicVacuum->SetFieldManager(localFieldMgr, true);

My problem is, when trying to open the simulation, I always receive: Segmentation fault (core dumped). What could be the reason for that?

Thank you.

Are both Region and logicVacuum data members of your class? Are you sure that they are both set to non-null values when this function is called (i.e., after you’ve built your geometry)?

Thank you for the response.

My program is compiling normally, after a while I managed to make it work by copying the same geometry from example B5, that uses G4Tubs, and creating my own MagneticField class. However, I am still having difficulties in realizing why in my program the magnetic field only works along with this specific geometry and how do I adapt to make it work with other types of geometry that I wish to choose.


As Michael wrote in the code segment which you showed it was not clear whether ‘Region’ and ‘logicVacuum’ were valid pointers: that they are NOT null, and that they point to valid objects of each type.

So, to check this, you can add a couple of lines to your program, such as

G4cout << " Value of Region= " << Region << G4endl;
G4cout << " and its name is " << Region->GetName() << G4endl;
G4cout << " Value of logicVacuum= " << logicVacuum << G4endl;
G4cout << " and its name is " << logicVacuum->GetName() << G4endl;

This will likely help you to identify whether one of these variables does not hold the value which you expect, and to correct it.

Best regards,