Question: Is My Quadrupole Magnetic Field Definition Correct in DetectorConstruction.cc?

Hi everyone,

I am currently trying to define a quadrupole magnetic field inside a beam pipe using Geant4. In my DetectorConstruction.cc, I implemented the field and the geometry as shown below. However, I am not sure whether this setup is correct or not. Especially, I would like to confirm whether the field is correctly applied only inside the vacuum region (inside the beam pipe).

Here is the relevant part of my code:

#include "G4QuadrupoleMagField.hh"
#include "G4FieldManager.hh"
// ========== BEAM PIPE AND MAGNETIC FIELD ========== //

// 1. Define beam pipe parameters
G4double pipe_innerR = 0.5*cm;
G4double pipe_outerR = 2.0*cm;
G4double pipe_hLen = 2.0*cm;

// 2. Create beam pipe (stainless steel)
G4Material* pipe_mat = nist->FindOrBuildMaterial("G4_STAINLESS-STEEL");
auto solidPipe = new G4Tubs("Pipe", pipe_innerR, pipe_outerR, pipe_hLen, 0., 360.*deg);
auto logicPipe = new G4LogicalVolume(solidPipe, pipe_mat, "PipeLV");

new G4PVPlacement(nullptr, 
                 G4ThreeVector(0, 0, 0), 
                 logicPipe, "Pipe", logicEnv, false, 0, checkOverlaps);

// 3. Create magnetic field volume (vacuum inside pipe)
G4Material* mag_mat = nist->FindOrBuildMaterial("G4_Galactic");  // or use "Vacuum"
auto solidMag = new G4Tubs("MagVol", 0., pipe_innerR, pipe_hLen, 0., 360.*deg);
auto logicMag = new G4LogicalVolume(solidMag, mag_mat, "MagLV");

new G4PVPlacement(nullptr, 
                 G4ThreeVector(),  // centered in pipe
                 logicMag, "MagField", logicPipe, false, 0, checkOverlaps);

// 4. Create quadrupole magnetic field
G4double gradient = 400.0*tesla/meter;  // Adjust strength as needed
G4QuadrupoleMagField* quadField = new G4QuadrupoleMagField(gradient);

// 5. Set up field manager
G4FieldManager* fieldMgr = new G4FieldManager();
fieldMgr->SetDetectorField(quadField);
fieldMgr->CreateChordFinder(quadField);

// 6. Attach field to magnetic volume
logicMag->SetFieldManager(fieldMgr, true);  // true = field is local to this volume

Questions:

  1. Is this the correct way to define a local quadrupole magnetic field inside a cylindrical pipe?
  2. Is the magnetic field only applied inside the logicMag volume (which is vacuum)?
  3. Are there better practices or potential issues I should be aware of?

I’ve also attached a screenshot from the visualization to illustrate the geometry setup.

Thank you very much in advance!

Best regards,
Emil