# Program run error

**URL:** <https://root-forum.cern.ch/t/program-run-error/54516>\
**Category:** Garfield++\
**Created:** [April 17, 2023, 7:01am UTC](https://root-forum.cern.ch/t/program-run-error/54516 "2023-04-17T07:01:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![changjh12](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/cc9497/32.png) [@changjh12](https://root-forum.cern.ch/u/changjh12)\
**Post date:** [April 17, 2023, 7:01am UTC](https://root-forum.cern.ch/t/program-run-error/54516/1 "2023-04-17T07:01:57Z")

</div>

Hello, I was modifying the program when the error below occurred.I tried to modify it in other ways, but it didn’t work. Thank you!

I added a line of code

> DriftElectron(x0, y0, z0, t0)

in this zone.

```auto
  constexpr unsigned int nEvents = 50000;//离子数与电子数
  for (unsigned int i = 0; i <= nEvents; ++i) { 
    std::cout << i << "/" << nEvents << "\n";
    // Randomize the initial position. 距离单位cm
    const double x0 = 3.0* RndmUniform()-1.5;//-15-15mm
    const double y0 = 0.025+0.4*RndmUniform();//
    const double z0 = -0.6+1.2*RndmUniform(); //-5-5mm
    const double t0 = 0 ;
    drift.DriftIon(x0, y0, z0, t0); 
    drift.DriftElectron(x0, y0, z0, t0);
  }

```

and I got this error.

> \*\*\* Break \*\*\* segmentation violation  
> Generating stack trace…  
> 0x00007f8d8d9d4083 in \_\_libc\_start\_main + 0xf3 from /lib/x86\_64-linux-gnu/libc.so.6  
> 0x00007f8d90f4caee in from ./sw

Here is my program.

* * *

```auto

#include <iostream>
#include <fstream>
#include <cstdlib>

#include <omp.h>
#include <TCanvas.h>
#include <TROOT.h>
#include <TApplication.h>
#include <TH1F.h>

#include "Garfield/ViewDrift.hh"
#include "Garfield/ViewSignal.hh"
#include "Garfield/ViewField.hh"
#include "Garfield/ViewFEMesh.hh"
#include "Garfield/ViewMedium.hh"

#include "Garfield/DriftLineRKF.hh"
#include "Garfield/ComponentComsol.hh"
#include "Garfield/MediumMagboltz.hh"
#include "Garfield/Sensor.hh"
#include "Garfield/AvalancheMC.hh"
#include "Garfield/AvalancheMicroscopic.hh"
#include "Garfield/SolidBox.hh"
#include "Garfield/GeometrySimple.hh"
#include "Garfield/ComponentConstant.hh"
#include "Garfield/TrackSrim.hh"
#include "Garfield/Random.hh"
#include "Garfield/Plotting.hh"
#include "Garfield/TrackHeed.hh"

using namespace Garfield;

//原始charge信号打印到txt文件函数
void printcharge(Sensor& sensor,const std::string label,const int nTimebins,const double tstep,const double tmin,const double index){     
  char title[60];
  sprintf(title, "charge_%s.txt",label.c_str()); 
  std::ofstream outfile;
  outfile.open(title,std::ios::out);
  outfile << "time(ns) "<< "total charge "<< "electron charge " << "ion charge\n";
  for(unsigned int i=0;i<nTimebins;++i){
    const double t=tstep/2+tmin+i*tstep;
    const double f=sensor.GetSignal(label,i);
    const double fe=sensor.GetElectronSignal(label,i);
    const double fion=sensor.GetIonSignal(label,i);
    outfile << t << " " << f << " " <<fe << " " << fion << "\n";
  }
}

//原始电流信号打印到txt文件函数
void printsignaltotxt(Sensor& sensor,const std::string label,const int nTimebins,const double tstep,const double tmin){     
  char title[60];
  sprintf(title,"current_%s.txt",label.c_str()); 
  std::ofstream outfile;
  outfile.open(title,std::ios::out);
  outfile << "time（ns） "<< "total current "<< "electron current " << "ion current\n";
  for(unsigned int i=0;i<nTimebins;++i){
    const double t=tstep/2+tmin+i*tstep;
    const double f=sensor.GetSignal(label,i);
    const double fe=sensor.GetElectronSignal(label,i);
    const double fion=sensor.GetIonSignal(label,i);
    outfile << t << " " << f << " " <<fe << " " << fion << "\n";
  }
}

int main(int argc, char * argv[]) {

  TApplication app("app", &argc, argv);

  //导入电场文件COSMOL、材料关联
  ComponentComsol fm;
  fm.Initialise("mesh1.mphtxt", "dielectrics.dat", "field1.txt", "mm");
  fm.SetWeightingPotential("weight_sj.txt", "sj");
  fm.SetWeightingPotential("weight_sw.txt", "sw");
  fm.PrintRange();

  // Setup the gas.
  MediumMagboltz gas;
  gas.SetComposition("xe", 100.); //100%Xe
  gas.SetTemperature(293.15);
  gas.SetPressure(40*760.);
  gas.LoadGasFile("xe_40atm.gas"); 
  // Load the ion mobilities.
  const std::string path = std::getenv("GARFIELD_HOME");
  gas.LoadIonMobility(path + "/Data/IonMobility_Xe+_P12_Xe.txt");//Xe离子种类需要改进，看论文
  // Associate the gas with the corresponding field map material. 
  const unsigned int nMaterials = fm.GetNumberOfMaterials();
  for (unsigned int i = 0; i < nMaterials; ++i) {
    const double eps = fm.GetPermittivity(i);
    if (eps == 1.) fm.SetMedium(i, &gas);//金属的介电常数需要查询，不能为1 ，可以设置为1e10
  }
  fm.PrintMaterials();

  // Create the sensor.
  Sensor sensor;
  sensor.AddComponent(&fm);
  sensor.AddElectrode(&fm,"sj");     
  sensor.AddElectrode(&fm,"sw"); 
  sensor.SetArea(-4.0, 0, -0.8, 4.0, 0.6, 0.8);

  //设置信号计算的时间下时间限，步长及时间步长个数，单位ns
  const double tstep = 100; // ********** //信号采样步长，0.1微秒 （ns,s=1e9ns）
  const double tmin = 0; // ********** //信号采样时间下限
  const double tmax=20000000; // ***** 上限，根据结果调整 1ms   
  const unsigned int nbins = (tmax-tmin)/tstep; //信号采样时间间隔数 ,随机取样
  sensor.SetTimeWindow(tmin, tstep, nbins);
  sensor.ClearSignal();

  DriftLineRKF drift;//用以计算初电离离子漂移
  drift.SetSensor(&sensor); //链接Sensor
  drift.UseWeightingPotential(true);    
  drift.EnableSignalCalculation(); //开启电流信号计算

  ViewDrift driftView;
  constexpr bool plotDrift = true;
  if (plotDrift) {
    drift.EnablePlotting(&driftView);//离子漂移线
  }
  
  constexpr unsigned int nEvents = 50000;//离子数与电子数
  for (unsigned int i = 0; i <= nEvents; ++i) { 
    std::cout << i << "/" << nEvents << "\n";
    // Randomize the initial position. 距离单位cm
    const double x0 = 3.0* RndmUniform()-1.5;//-15-15mm
    const double y0 = 0.025+0.4*RndmUniform();//
    const double z0 = -0.6+1.2*RndmUniform(); //-5-5mm
    const double t0 = 0 ;
    drift.DriftIon(x0, y0, z0, t0); 
    drift.DriftElectron(x0, y0, z0, t0);
  }

  //driftline_plot
  if (plotDrift) {
    TCanvas* cd = new TCanvas("cd","drift line",600,600);
    ViewFEMesh* meshView = new ViewFEMesh();
    meshView->SetArea(-4.0, -0.2, -1.0, 4.0, 0.6, 1.0);
    meshView->SetCanvas(cd);
    meshView->SetComponent(&fm);
    // y-z projection.
    meshView->SetPlane(1, 0, 0, 0, 0, 0);
    meshView->SetFillMesh(true);
    // Set the color of the 304.map ID,不锈钢上色
    driftView.SetColourElectrons(kRed);
    driftView.SetColourTracks(kGreen + 3); 
    driftView.SetColourIons(kYellow);
    //
    meshView->SetColor(1, kRed);
    meshView->EnableAxes();
    meshView->SetViewDrift(&driftView);
    meshView->Plot();
    cd->SaveAs("dirftline.jpg");//输出漂移线图片
  }   

  // ---- 收集极输出信号 ----

  // Plot the induced current.
  ViewSignal signalView;
  signalView.SetSensor(&sensor);
  TCanvas* c1 = new TCanvas("signal_sj_current", "", 800, 600);
  signalView.SetCanvas(c1);
  signalView.SetLabelY("Induced Current-sj [fc/ns]");
  signalView.PlotSignal("sj");
  //打印原始总电流，电子电流，离子电流信号到txt文件
  printsignaltotxt(sensor,"sj",nbins,tstep,tmin);   
  
  // 阳极上电荷计算
  TCanvas c2("signal_sj_charge", "", 600, 600);
  sensor.IntegrateSignal("sj");
  signalView.SetCanvas(&c2);
  signalView.SetLabelY("Induced Charge-sj [fC]");
  signalView.PlotSignal("sj");
  // 电荷信号文件保存
  printcharge(sensor,"sj",nbins,tstep,tmin,0);  

  // ----- 栅极输出信号 -----

  // Plot the induced current.
  ViewSignal signalView_sw;
  signalView_sw.SetSensor(&sensor);
  TCanvas* c3 = new TCanvas("signal_sw_current", "", 600, 600);
  signalView_sw.SetCanvas(c3);
  signalView_sw.SetLabelY("Induced Current-sw [fc/ns]");
  signalView_sw.PlotSignal("sw");
  //打印原始总电流，电子电流，离子电流信号到txt文件
  printsignaltotxt(sensor,"sw",nbins,tstep,tmin);   
  // 阳极上电荷计算
  TCanvas c4("signal_sj_charge", "", 800, 600);
  sensor.IntegrateSignal("sw");
  signalView_sw.SetCanvas(&c4);
  signalView_sw.SetLabelY("Induced Charge-sw [fC]");
  signalView_sw.PlotSignal("sw");
  // 电荷信号文件保存
  printcharge(sensor,"sw",nbins,tstep,tmin,0);  

  //计算完成标志
  std::cout << "=======================" <<std::endl;
  std::cout << "Calculation is finished.\n";    
  app.Run(true);

}

```

* * *

---

<div class="post-metadata">

**Author:** ![changjh12](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/cc9497/32.png) [@changjh12](https://root-forum.cern.ch/u/changjh12)\
**Post date:** [April 17, 2023, 9:41am UTC](https://root-forum.cern.ch/t/program-run-error/54516/2 "2023-04-17T09:41:34Z")

</div>

Hi, hschindl @hschindl

---

<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:** [April 17, 2023, 10:06am UTC](https://root-forum.cern.ch/t/program-run-error/54516/3 "2023-04-17T10:06:58Z")

</div>

The line that you added looks quite innocent.  
Does this always happen or only for specific starting points (x0, y0, z0)?

---

<div class="post-metadata">

**Author:** ![changjh12](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/cc9497/32.png) [@changjh12](https://root-forum.cern.ch/u/changjh12)\
**Post date:** [April 17, 2023, 10:30am UTC](https://root-forum.cern.ch/t/program-run-error/54516/4 "2023-04-17T10:30:46Z")

</div>

It always happens. It feels strange.

---

<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:** [April 17, 2023, 1:12pm UTC](https://root-forum.cern.ch/t/program-run-error/54516/5 "2023-04-17T13:12:14Z")

</div>

Indeed, that’s strange. Can you try it on a fresh installation of Garfield++? If you still have the same issue, can you provide a _ **minimal** _ version of the program that reproduces the segmentation violation, together with the input files needed to run it?

---

<div class="post-metadata">

**Author:** ![changjh12](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/cc9497/32.png) [@changjh12](https://root-forum.cern.ch/u/changjh12)\
**Post date:** [April 19, 2023, 12:06pm UTC](https://root-forum.cern.ch/t/program-run-error/54516/6 "2023-04-19T12:06:13Z")

</div>

I updated Garfield++ and the problem was solved. Thank you very much. 😀

---

<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:** [May 3, 2023, 12:06pm UTC](https://root-forum.cern.ch/t/program-run-error/54516/7 "2023-05-03T12:06:15Z")

</div>

Closed on May 3, 2023, 12:06pm UTC

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