Non costant magnetic field

Dear allpix-squared developers,
is there any plan to implement non uniform magnetic field in the software?
I have in mind the case of a detector in a solenoid.

Thanks and regards,
Marco Bomben

Hi Marco,

a solenoid field, this would be handy :slight_smile:

Indeed there is currently no plan from our side to implement anything new there. We would be very happy to assist in developing this, though.

You can see in the code for the MagneticFieldReader, that the implementation is rather short. From what I see in this presentation, it should be rather straight forward to implement, if you have an analytic expression for the magnetic field on hand. Maybe this would be sufficient for the moment?

A tricky part would come up, if it’s not sufficient for the charge carrier propagation to assume a constant magnetic field throughout one sensor’s volume, as we currently only pass one value for the magnetic field to the detector object. If this should become relevant, there’s certainly something we can do about that.

Would you be interested in looking into this?


Dear @pschutze ,
I reached this post because i am in the process of making some tracking studies varying a magnetic field while accounting for variation of level arm of a detector fully pixel based making the B field more intense.

In this context, i found the allpix-squared to have an excellent API to customize geometry, location of detectors and (still have to go through all tutorial) Easy to use interfacce for students which i Plan to make work on It soon.

I Need to place several layers of pixel detectors (timepix or velopix) in a dipole field (a la LHCb) with Fringe tails, but at First order i just care about studying momentum resolution, while accounting for multiple scattering. I have a dipole magnetic field expressed in some steps and grid values , and any guidance on where to look at for implementing It would be great. (Also i would be interested to understand if i can use and add raw G4 cherenkov radiators to have a better modeling of what i expect to contribute mostly to multiple scattering).


Dear @rquaglia

implementing this sounds interesting, and I can give you a few pointers where to start looking.

  • The magnetic field is provided, in one form or the other, by the MagneticFieldReader module, so any new way of defining the field would have to go there. Module code here

  • An example of how to parse and create a field function for a custom field forumla can be found in the ElectricFieldReader her. Beware that the magnetic field has to be provided in global coordinates!

  • The propagation modules are already prepared for propagating charge carriers within the field, since they query the Detector object for every step on what the local magnetic field is

  • That means the last part open to implement is a way of storing the magnetic field and retrieving its value at a position given in local coordinates. This has to happen in the Detector class here and they best way probably depends a bit on what you are targeting.

Does this give you a good starting point to dive into the code a bit?

Please let me know if you have further questions, we can also have a chat and discuss implementation details. We’d love to get your contribution on this into the main repository!

Best regards,

Dear @simonspa , thanks a lot for the pointers.

My main goal is to emulate a tracker fully pixel based under different magnetic field level-arm/intensity ratio. In practice I don’t even need to simulate the readout of the system and the impact of a B field inside a sensor, so infact i am planning to use the framework only to create the Energy deposits and plug them into a custom root file i can use to make studies and post-process with smearing parameters to emulate a given resolution effect. The main aspect i need to include in my simulation is the effect of multiple scattering from ‘air’ in the world and from the pixel sensors and eventually add a ‘radiator’ detector in the middle to just include the effect of multiple scattering and bremmsthralung emission for electrons.

I guess this is not too difficult to achieve in the framework, from what i have seen so far, given the very nice flexible and user-friendly way to locate a given pixeled detector. I give a bit a try those days and come back to you in case i might need a 1-1 meeting.

My first approach would be to have 3-TH3D input histogram and retrieve the B field via tri-linear interpolation , since my mag-field dipole map is essentially a look-up table , and via the interpolation i can basically be independent on the grid-step size over which the map i start from is defined.


Hi @rquaglia

that sounds like a good start, you could just store this meshed field as a linearized array (x0 y0 z0 x1 y1 z1 etc) with known bins in all dimensions, then jumping around in it is very easy (have a look at the DetectorField class that does this for the electric field in the sensor.

By implementing this, you essentially automatically get this for the “in sensor” part as well. Depending on how strong your field is, there might be an effect on the resolution you have on your sensors - but have a look later.

I forgot one point: you of course would have to pass that field also to Geant4, this happens here: src/modules/DepositionGeant4/DepositionGeant4Module.cpp · master · Allpix Squared / Allpix Squared · GitLab



Hi @simonspa , i did something very quick and dirty and it seems like i can get along with that, i had to edit the geometry manager and some other things ( if you want a branch on the repository with the code / example to have a look let me know…)
See attached figure for a LHCb dipole field which i took from 3 TH3D files with interpolation read from a csv file.
I might open a new thread continuing this, since i probably have more questions:

  • how can i tell that the world material is G4_Air ? ( is it configurable?)
    [Solved, but maybe any other passive material i Need to customize It with a simplified material budget]
  • how can i profile/save to ROOT the B field map i am reading -stepping and the computed integrated Bdl field of a trajectory? ( if i did it right, it should be 4 T*m)
  • how can i edit the particle gun input for each event ? So far i guess i can only set a single particle/energy , not something varying event by event. I was thinking to read a file where the origin/direction/particle type/momentum is stored and use it as input for the particle gun ( to mimic the typical kinematics of a track in the LHCb detector)

Let me know if it’s better to open a new thread.

Hi @rquaglia

  • The world material is configurable as parameter of the GeometryBuilderGeant4
  • I fear that this is something you would have to implement in the MagneticFieldReader directly, the central library does not provide a facility to write out field data again
  • For your particle gun there are several methods. Either you have e.g. a LHCb MC file of primaries (from Pythia or similar), then using the DepositionGenerator module would be an option. Otherwise you can use Geant4 macro files to define your very own General Particle Source with the properties you need.


1 Like