# Importing (and utilizing correctly) Magnetic field maps

**URL:** <https://root-forum.cern.ch/t/importing-and-utilizing-correctly-magnetic-field-maps/64066>\
**Category:** Garfield++\
**Created:** [August 22, 2025, 2:04pm UTC](https://root-forum.cern.ch/t/importing-and-utilizing-correctly-magnetic-field-maps/64066 "2025-08-22T14:04:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![keithwm422](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/96bed5/32.png) [@keithwm422](https://root-forum.cern.ch/u/keithwm422)\
**Post date:** [August 22, 2025, 2:04pm UTC](https://root-forum.cern.ch/t/importing-and-utilizing-correctly-magnetic-field-maps/64066/1 "2025-08-22T14:04:20Z")

</div>

I have setup successfully and ran Garfield++ simulations using ComponentAnalyticFields with the geometry and Voltages of a wire drift chamber I work with. This has enabled me to build time-to-space mappings of my chamber for analysis, with lorentz angles when Magnetic field is applied in our chamber. The Garfield++ software works and is written well, I like working with it! I am trying to understand if the software supports importing a Magnetic field map on top of the Drift Chamber Electric Fields. The magnetic field map within my chamber is not constant; I have a pretty good measurement of its mapping and could work on importing that into a simulation. But I believe the Magbolz gas file will be rather large if I allow many steps in both Electric Field and Magnetic Field. The gas files I use currently have 1 magnetic field value and many Electric Field values to cover the range calculated by the component analytic fields of my chamber. In addition, I am unsure if AvalanchMC supports multiple lorentz angle and drift velocity table lookups for a single electron drifting. Maybe I need to switch to DriftLineRKF for my time-to-space simulations and that would be more straightforward integrating the velocity vector with changing lorentz angle along a drift line. Is there already support or examples in the software for this type of calculation? I am sorry if I missed it but couldn’t easily find the answer myself. Thanks for your time!

---

<div class="post-metadata">

**Author:** ![devajith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/devajith/32/37165_2.png) [@devajith](https://root-forum.cern.ch/u/devajith)\
**Post date:** [August 22, 2025, 2:24pm UTC](https://root-forum.cern.ch/t/importing-and-utilizing-correctly-magnetic-field-maps/64066/2 "2025-08-22T14:24:28Z")

</div>

Hi @keithwm422,

Welcome to the ROOT Forum! Let me add [@hschindl](https://root-forum.cern.ch/u/hschindl) in the loop.

Cheers,

Dev

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [August 25, 2025, 8:49am UTC](https://root-forum.cern.ch/t/importing-and-utilizing-correctly-magnetic-field-maps/64066/3 "2025-08-25T08:49:14Z")

</div>

Hi,

many thanks for your message!

- Yes, it’s possible to use a magnetic field map on top of the electric field. If you have the values of the B field on a regular (Cartesian or cylindrical) grid, you can use the class `ComponentGrid`, see [this example](https://gitlab.cern.ch/garfield/garfieldpp/-/blob/master/Examples/Grid/bfield.C?ref_type=heads).
- The size of the gas file is not necessarily an issue, but the time for running Magboltz might become very long if you include too many points, so it’s indeed a good idea to stick to a reasonable (“just enough”) number of electric fields, magnetic fields and angles. How many electric field values do you have at the moment? Perhaps you can reduce the number a bit if the Magboltz run time becomes unmanageable?
- `AvalancheMC`and `DriftLineRKF` both use the same methods to compute the drift velocity vector at a given electric and magnetic field, there is no difference between the two classes in that respect.

---

<div class="post-metadata">

**Author:** ![keithwm422](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/96bed5/32.png) [@keithwm422](https://root-forum.cern.ch/u/keithwm422)\
**Post date:** [September 6, 2025, 2:10am UTC](https://root-forum.cern.ch/t/importing-and-utilizing-correctly-magnetic-field-maps/64066/4 "2025-09-06T02:10:44Z")

</div>

Thanks for the prompt reply. I have investigated the use of `ComponentGrid` . I already utilize `CompenentAnalyticField` with many wires and 2 planes, all at various voltages. So I went ahead and tried to add both together in my simulation like this:

```auto
Sensor * sensor = new Sensor;

```

```auto
sensor->AddComponent(cmp);

```

```auto
sensor->AddComponent(cmpB); 

```

The first component (ComponentAnalyticField) is where I add wires and planes and the second component (ComponentGrid) I utilize the LoadMagneticField function in the example you linked me to. **Currently, I am running DriftLineRKF and getting status code -10 which I don’t see in the documentation**. I do get many errors or complaints about Electric Fields:  
ComponentGrid::ElectricField: Map not available.

This is fine with me since I haven’t tried to add any Electric Fields into the cmpB object.

On the Magbolz/gasfile topic, I did generate a gas table successfully with both B and E fields that cover the range I need in the field setup I configure. I previously had been generating gas files separately and then loading them into my DriftLineRKF simulation (to prevent the need to run Magbolz every time I adjust the DriftLineRKF or decide to include more starting points, etc). However, my gas files will not load after successfully generating with multiple B and E fields. I attach both my gas file generator and my load gas file code here for feedback, guidance, and criticism. Thanks for your help!

[MakeGasFile\_flight.C](https://root-forum.cern.ch/uploads/short-url/lsfMgGgjnqZ9xsMI8jUU8bI72Wf.C) (2.1 KB)

[LoadGasFile.C](https://root-forum.cern.ch/uploads/short-url/xGvmfAepr3IwSfnTJ0PDilkWiat.C) (1.5 KB)

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [September 6, 2025, 7:12am UTC](https://root-forum.cern.ch/t/importing-and-utilizing-correctly-magnetic-field-maps/64066/5 "2025-09-06T07:12:07Z")

</div>

Hi,  
I don’t see anything particularly suspicious in your code to generate and load gas files. Can you post your .gas file?  
As for the `DriftLineRKF` error, can you post a minimal reproducible example?  
Thanks!

---

<div class="post-metadata">

**Author:** ![keithwm422](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/96bed5/32.png) [@keithwm422](https://root-forum.cern.ch/u/keithwm422)\
**Post date:** [September 8, 2025, 5:49pm UTC](https://root-forum.cern.ch/t/importing-and-utilizing-correctly-magnetic-field-maps/64066/6 "2025-09-08T17:49:20Z")

</div>

Here is the gas file I generated which doesn’t seem to load with the LoadGasFile code I posted above. I renamed the file extension when uploaded here because it wouldn’t let me upload with the `.gas` one. I am working on the minimum reproducible example of the DriftLineRKF code. Thanks!

[Flight2024\_Bon\_P\_755.865\_T\_303.15\_10Ar\_90CO2\_multiE.txt](https://root-forum.cern.ch/uploads/short-url/AtFVxabY2iNxroczvNpo9ffBF3b.txt) (28.0 KB)

---

<div class="post-metadata">

**Author:** ![keithwm422](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/96bed5/32.png) [@keithwm422](https://root-forum.cern.ch/u/keithwm422)\
**Post date:** [September 16, 2025, 3:33am UTC](https://root-forum.cern.ch/t/importing-and-utilizing-correctly-magnetic-field-maps/64066/7 "2025-09-16T03:33:45Z")

</div>

I was able to narrow down my problem with status codes and reproduce it in this simplified example. Takes ~5 mins to run because I have the Magbolz gas file being generated during the run, but the status values are all -10. However, when flipping the order of the components added to my sensor (lines 81 and 82), the status code returns +1 instead. It appears that the ElectricField method of Sensor class (line 117 of `Sensor.cc`) will return the status corresponding to the last component it found with a non-zero status. And since my ComponentGrid doesn’t have an Electric Field added to it, I get -10, until I flip the order in which case I get the +1 for the only wire I added into my example.  
Anyways, is this the intended way of utilizing both `ComponentGrid` and `ComponentAnalyticField` in a setup for `DriftLineRKF`? Or should I be doing extra steps with these components such that my status code would return appropriately when I drift electrons?

Thanks again for your help!

[garfield\_randomB.txt](https://root-forum.cern.ch/uploads/short-url/rCNOI8Zh3vhJGOgNjIErSNH6N0X.txt) (3.5 KB)

[StatusCodesMRE.C](https://root-forum.cern.ch/uploads/short-url/os95aJpdnZCLFMqQEfSAsjAvYt4.C) (5.2 KB)

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [September 16, 2025, 5:53am UTC](https://root-forum.cern.ch/t/importing-and-utilizing-correctly-magnetic-field-maps/64066/8 "2025-09-16T05:53:12Z")

</div>

Hi,  
thanks a lot for the extra debugging! I will commit a fix such that invalid/undefined electric fields are ignored if there is another component providing an electric field.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [September 30, 2025, 5:54am UTC](https://root-forum.cern.ch/t/importing-and-utilizing-correctly-magnetic-field-maps/64066/9 "2025-09-30T05:54:04Z")

</div>

Closed on September 30, 2025, 5:54am UTC

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