Here’s my working code.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <TCanvas.h>
#include <TMath.h>
#include <TROOT.h>
#include <TTree.h>
#include <TApplication.h>
#include <TH1F.h>
#include <TH2F.h>
#include <TBuffer3D.h>
#include <TBuffer3DTypes.h>
#include <TVirtualViewer3D.h>
#include <TFile.h>
#include <TApplication.h>
#include "Garfield/MediumMagboltz.hh"
#include "Garfield/ComponentAnsys123.hh"
#include "Garfield/Sensor.hh"
#include "Garfield/DriftLineRKF.hh"
#include "Garfield/AvalancheMicroscopic.hh"
#include "Garfield/AvalancheMC.hh"
#include "Garfield/ViewGeometry.hh"
#include "Garfield/Plotting.hh"
#include "Garfield/ViewField.hh"
#include "Garfield/ViewFEMesh.hh"
#include "Garfield/ViewSignal.hh"
#include "Garfield/TrackHeed.hh"
#include "Garfield/TrackSrim.hh"
#include "Garfield/SolidBox.hh"
#include "Garfield/GeometrySimple.hh"
#include "Garfield/ComponentAnalyticField.hh"
#include <time.h>
using namespace Garfield;
using namespace std;
using namespace ROOT;
int main() {
bool debugging = true;
double trackMom = 100.;
int nTrk = 100;
//save cluster information
double xc = 0., yc = 0., zc = 0., tc = 0.;
int nCluster;
double trkion;
//int Clus_posi;
TFile *file = new TFile("ar_90_co2_10_0T_1GeV.root","RECREATE");
TTree* c_position = new TTree("c","c Position");
c_position->Branch("zc",&zc,"zc/D");
c_position->Branch("ncl",&nCluster,"nCluster/I");
c_position->Branch("trkion",&trkion,"trkion/D");
//set gas information
MediumMagboltz* gas = new MediumMagboltz();
gas->SetComposition("ar",95.,"isobutane",2.,"CF4",3.);
gas->SetTemperature(293.15);
gas->SetPressure(760.);
gas->SetMaxElectronEnergy(200.);
//gas->EnablePenningTransfer(0.44, 0.0, "He");
gas->Initialise(true);
// geo
double zlength = 50.;
SolidBox* box = new SolidBox(0., 0., 0., 20., 20, zlength); // center and half length
GeometrySimple* geo = new GeometrySimple();
geo->AddSolid(box, gas);
ComponentAnalyticField* comp = new ComponentAnalyticField();
if(debugging) comp->EnableDebugging();
else comp->DisableDebugging();
comp->SetGeometry(geo);
comp->AddPlaneY(-20, 0., "pad_plane");
comp->AddPlaneY(20, 10000., "HV");
comp->SetMagneticField(0, 1, 0);
Sensor* sensor = new Sensor();
if(debugging) sensor->EnableDebugging();
sensor->AddComponent(comp);
sensor->AddElectrode(comp,"pad_plane");
//setup a track
double x0 = 0, y0 =0 , z0 = -1*zlength, t0 = 0.;
//double dx0 = 0., dy0 = 1., dz0 = dy0/tan(theta);
double dx0 = 0., dy0 = 0., dz0 = 1.;
//double dy0 = sin(3.1415926*3/180) , dx0 = 0, dz0 = cos(3.1415926*3/180);
TrackHeed* track = new TrackHeed();
track->SetSensor(sensor);
track->SetParticle("muon");
track->SetMomentum(trackMom*1.e9);
// track->EnableElectricField();
// track->EnableMagneticField();
// double maxrange = 0., rforstraight = 0., stepstraight = 0., stepcurved = 0.;
// track->GetSteppingLimits(maxrange, rforstraight, stepstraight, stepcurved);
// stepcurved = 0.02;
// track->SetSteppingLimits(maxrange, rforstraight, stepstraight, stepcurved);
for(int iTrk=0; iTrk<nTrk; iTrk++){
track->NewTrack(x0, y0, z0, t0, dx0, dy0, dz0);
// Cluster coordinates
//double xc = 0., yc = 0., zc = 0., tc = 0.;
// Number of electrons produced in a collision
int nc = 0;
// Energy loss in a collision
double ec = 0.;
// Dummy variable (not used at present)
double extra = 0.;
//reset cluster
nCluster = 0;
//loop clusters
while(track->GetCluster(xc, yc, zc, tc, nc, ec, extra)){
if((zc>= -50.) && (zc< 50.)){
nCluster++;
//Clus_posi = zc;
c_position->Fill();
//cout << "cluster position : "<< zc << " deposited energy : "<< ec << endl;
}
}
//cout<<"Cluster number: " << nCluster/100. <<endl;
//trkion = nCluster/100.0;
}
c_position->Write();
delete track;
cout<<"end of the program"<<endl;
file->Close();
}