# How to Set ElectricField in my code using Garfield++

**URL:** <https://root-forum.cern.ch/t/how-to-set-electricfield-in-my-code-using-garfield/62949>\
**Category:** Garfield++\
**Created:** [February 15, 2025, 7:49am UTC](https://root-forum.cern.ch/t/how-to-set-electricfield-in-my-code-using-garfield/62949 "2025-02-15T07:49:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuyang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/yuyang/32/38503_2.png) [@Yuyang](https://root-forum.cern.ch/u/Yuyang)\
**Post date:** [February 15, 2025, 7:49am UTC](https://root-forum.cern.ch/t/how-to-set-electricfield-in-my-code-using-garfield/62949/1 "2025-02-15T07:49:49Z")

</div>

* * *

Hi, I had a problem with my simulation using Garfield++. I have a txt file that records the field distribution, and it’s formatted like this  
z r Er Ez  
0.0000 0.0000 0.0000 0.0000  
0.0000 0.0010000 0.0000 0.0000  
0.0000 0.0020000 0.0000 0.0000  
0.0000 0.0030000 0.0000 0.0000  
0.0000 0.0030000 231.24 -3.3500E-4  
0.0000 0.0040489 231.24 -3.5830E-4  
0.0000 0.0050979 231.23 -4.6605E-5  
0.0000 0.0061468 231.23 -1.3203E-4  
0.0000 0.0073039 231.23 -2.3166E-4  
0.0000 0.0084610 231.23 1.8453E-4  
…  
This file has a lot of data, but its data is not complete, nor is it a regular fetch point. Is there any way I can set up such an electric field in the simulation？  
I guess I’ll use interpolation method, but I don’t know how to implement it  
I sincerely hope there will be a solution.It’s part of my first simulation work by using Garfield++

@hschindl

\_ROOT Version:6.34  
\_Platform:vscode  
\_Compiler:CMake

* * *

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [February 15, 2025, 8:29am UTC](https://root-forum.cern.ch/t/how-to-set-electricfield-in-my-code-using-garfield/62949/2 "2025-02-15T08:29:55Z")

</div>

Hi,

Adding @hschindl in the loop.

Cheers,  
D

---

<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:** [February 15, 2025, 9:37am UTC](https://root-forum.cern.ch/t/how-to-set-electricfield-in-my-code-using-garfield/62949/4 "2025-02-15T09:37:28Z")

</div>

Hi,  
one option is to use `ComponentGrid` which allows you to load a set of electric fields in cylindrical coordinates. This example

> **[Examples/Grid/bfield.C · master · garfield / garfieldpp · GitLab](https://gitlab.cern.ch/garfield/garfieldpp/-/blob/master/Examples/Grid/bfield.C?ref_type=heads)**
>
> http://garfieldpp.web.cern.ch/garfieldpp/

is for loading a magnetic field map on an rz grid but it’s very similar for electric fields.  
`ComponentGrid` only works with a regular grid though, so you would indeed have to interpolate/extrapolate your data to a regular grid.

---

<div class="post-metadata">

**Author:** ![Yuyang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/yuyang/32/38503_2.png) [@Yuyang](https://root-forum.cern.ch/u/Yuyang)\
**Post date:** [February 15, 2025, 12:34pm UTC](https://root-forum.cern.ch/t/how-to-set-electricfield-in-my-code-using-garfield/62949/5 "2025-02-15T12:34:45Z")

</div>

Thank you for your help!  
I have tried the method,but got this feedback in terminal

 ![feed_back](https://root-forum.cern.ch/uploads/default/original/3X/f/4/f483172260f40ff154035d330f0018f129a6829b.png)

And the field image got nothing,

 ![field_image](https://root-forum.cern.ch/uploads/default/original/3X/a/5/a55423c62cc887bfc5a1228ba00d42cccb584cef.png)

What is wrong?I’m confused.@hschindl

(Here’s my code)  
#include \<Garfield/ComponentGrid.hh\>  
#include \<TROOT.h\>  
#include \<TApplication.h\>  
#include \<Garfield/MediumMagboltz.hh\>  
#include \<Garfield/ViewField.hh\>  
#include \<Garfield/GeometrySimple.hh\>  
#include \<Garfield/SolidTube.hh\>  
#include \<Garfield/Sensor.hh\>  
#include \<TH2D.h\>  
#include \<TFile.h\>

using namespace Garfield;

int main() {  
const double rTube = 36;  
const double halflength = 48;

MediumMagboltz gas;  
gas.SetComposition(“CF4”,100.);  
gas.SetPressure(760.0);  
gas.SetTemperature(293.15);  
gas.LoadGasFile(“CF4\_1atm.gas”);

SolidTube tube(0., 0., 0., rTube, halflength);

GeometrySimple geo;  
geo.AddSolid(&tube,&gas);

ComponentGrid cmp;  
cmp.SetMedium(&gas);  
cmp.SetCylindricalCoordinates();  
cmp.LoadElectricField(“field\_data.txt”,“XZ”,false,false);  
cmp.SetGeometry(&geo);

Sensor sensor;  
sensor.AddComponent(&cmp);

ViewField fieldView(&cmp);  
fieldView.SetPlaneXZ();  
// Set the plot limits in the current viewing plane.  
const double xmin = -rTube;  
const double xmax = rTube;  
const double zmin = 0;  
const double zmax = 2 \* halflength;  
fieldView.SetArea(xmin, zmin, xmax, zmax);  
fieldView.SetElectricFieldRange(0.,50000);  
fieldView.GetCanvas()-\>SetLeftMargin(0.16);  
fieldView.Plot(“v”,“COLZ”);

std::vector xf;  
std::vector yf;  
std::vector zf;  
fieldView.PlotFieldLines(xf, yf, zf, true, false);  
fieldView.GetCanvas()-\>SaveAs(“field\_image.png”);  
}

---

<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:** [February 15, 2025, 2:55pm UTC](https://root-forum.cern.ch/t/how-to-set-electricfield-in-my-code-using-garfield/62949/6 "2025-02-15T14:55:39Z")

</div>

Hi,  
thanks for the code. There is one issue that I noticed: you are loading only the electric field (not the potential), but you’re trying to make a contour plot of the potential (`fieldView.Plot(“v”,“COLZ”);`).

---

<div class="post-metadata">

**Author:** ![Yuyang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/yuyang/32/38503_2.png) [@Yuyang](https://root-forum.cern.ch/u/Yuyang)\
**Post date:** [February 16, 2025, 2:35pm UTC](https://root-forum.cern.ch/t/how-to-set-electricfield-in-my-code-using-garfield/62949/7 "2025-02-16T14:35:14Z")

</div>

Hi,thanks for your advice!  
So does that mean I need to put the corresponding potential data in my txt file？  
Because if I just set the bool value of “withpotential” to true, I’ll get an error:

ComponentGrid::ElectricField: Map not available

@hschindl (Sorry to bother you again, 😿

---

<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:** [February 16, 2025, 2:49pm UTC](https://root-forum.cern.ch/t/how-to-set-electricfield-in-my-code-using-garfield/62949/8 "2025-02-16T14:49:14Z")

</div>

Well, yes, if you want to import potentials you need to include them in your txt file.  
Note that you don’t need the potentials for the actual simulations (the charge transport algorithms only use the electric field), but they are of course useful for plotting purposes.

---

<div class="post-metadata">

**Author:** ![Yuyang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/yuyang/32/38503_2.png) [@Yuyang](https://root-forum.cern.ch/u/Yuyang)\
**Post date:** [February 17, 2025, 6:15am UTC](https://root-forum.cern.ch/t/how-to-set-electricfield-in-my-code-using-garfield/62949/9 "2025-02-17T06:15:32Z")

</div>

Thank you for your help!  
The problem solved,and I have now been able to simulate the movement of electrons in such an electric field.  
It is really wonderful because it was the first successful Garfield example I wrote.  
Sincerely,thanks for your help! ❤

---

<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:** [March 3, 2025, 6:16am UTC](https://root-forum.cern.ch/t/how-to-set-electricfield-in-my-code-using-garfield/62949/10 "2025-03-03T06:16:14Z")

</div>

Closed on March 3, 2025, 6:16am UTC

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