How to create a uniform magnetic field inside a cylinder with realistic non-uniform fringing fields outside in Geant4?

Hello Geant4 community,

I’d like to simulate a magnetic field setup where a long cylinder (aligned with the global z-axis) contains a uniform magnetic field in the +z direction inside its volume, but the region outside the cylinder should have the realistic, non-uniform fringing field that results from the coil geometry.

Any hints or code snippets showing how to set up a local but spatially-extended field region would be very helpful.

Also, is it possible to constraint that spatially extended field region? Like, I put this cylinder inside of a box and tell Geant4 to extend the natural spatial field only until the border of the box – inside the box.

Thanks in advance!

Any ideas? I checked Geant4 documentation, but it was not enough for the setup I want.

Just to illustrate my point. I want to design a Panofsky magnet (a yoke structure that has coils on four sides of an iron frame). I want to know, what is the simplest way to do that?

Another issue is that, if I become successful in designing the magnet, I don’t want the fields generated by that magnet extend to all of the world. I intend to put this magnet inside of a box made out of air and tell Geant4 to extend the magnetic field until the end of the magnet’s mother volume.

Is it possible to do that in geant4?

I also came up with another idea. I can design the whole magnet structure in a 2D or 3D FEM solver and extract the confined magnetic field. But the point is, how can I import this confined magnetic field (the field map) in geant4?