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

1 Like

Hi @emillon779,

Thank you for your question.

I have never implemented a quadrupole magnetic field myself, and I do not see an example showing how to do it, so perhaps some experts like @japost may want to elaborate further.

According to the documentation [1], it looks like your code is almost correct. These points are worth cross-checking:

  • The orientation of the field, it may be necessary to either rotate the volume or the field
  • I think new G4FieldManager(); is not the recommended approach; please see how to register the field in the documentation [2] or in the examples under extended/field

A bit later in the same section, it is explained how to speed up your application by using steppers templated on the equation [3].

To ensure long-term stability, you may consider using a symplectic integrator [4]. One of the examples used to show how to use this integrator [5], but the code was removed in later releases.

Regarding how to test that it is working as expected, you may want to run a test with charged geantinos or other charged particles, shooting in directions that are easy to understand and cross-check by eye or hand (e.g., along the center of the field, or slightly offset).

I hope this helps a bit. I also hope other experts may add further insights, as I am happy to learn more about this feature.

Best,
Alvaro

[1] Electromagnetic Field — Book For Application Developers 11.3 documentation

[2] Electromagnetic Field — Book For Application Developers 11.3 documentation

[3] Electromagnetic Field — Book For Application Developers 11.3 documentation

[4] Electromagnetic Field — Book For Application Developers 11.3 documentation

[5] geant4/examples/extended/field/field01/src/F01FieldSetup.cc at f840b5da3a70c2c7be836fdb72a781eab12e0af6 · Geant4/geant4 · GitHub

1 Like

Thank you, Alvaro. Your insights were very helpful. I will review the points you mentioned and take another look at the documentation and extended examples.
Best regards,
Emil

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.