5#include "CLHEP/Units/SystemOfUnits.h"
6#include "CLHEP/Units/PhysicalConstants.h"
8#include "G4ParticleTable.hh"
9#include "G4IonTable.hh"
11#include "BDSLinkTrackerInterface.hh"
12#include "BDSLinkBunch.hh"
13#include "BDSIMLink.hh"
14#include "BDSParticleDefinition.hh"
19 int referenceParticlePDGIn,
20 double referenceKineticEnergyIn,
21 double relativeEnergyCutIn,
23 int referenceIonChargeIn,
32 if(singleton !=
nullptr) {
37 referenceParticlePDGIn,
38 referenceKineticEnergyIn,
52 std::cout <<
"BDSLinkTrackerInterface not initialised" << std::endl;
57BDSLinkTrackerInterface::BDSLinkTrackerInterface(std::string bdsimConfigFileIn,
58 int referenceParticlePDGIn,
59 double referenceKineticEnergyIn,
60 double relativeEnergyCutIn,
62 int referenceIonChargeIn,
64 bdsimConfigFile(bdsimConfigFileIn),
65 referenceParticlePDG(referenceParticlePDGIn),
66 referenceKineticEnergy(referenceKineticEnergyIn),
67 relativeEnergyCut(relativeEnergyCutIn),
69 referenceIonCharge(referenceIonChargeIn),
70 batchMode(batchModeIn)
73 g4particle_table = G4ParticleTable::GetParticleTable();
74 g4ion_table = g4particle_table->GetIonTable();
80 std::vector<std::string> bdsim_args;
81 bdsim_args.push_back(
"bdsim");
82 bdsim_args.push_back(
"--file="+bdsimConfigFile);
83 bdsim_args.push_back(
"--seed="+std::to_string(seed));
84 bdsim_args.push_back(
"--output=None");
88 bdsim_args.push_back(std::string(
"--batch"));
90 bdsim_args.push_back(std::string(
"--vis_mac=vis.mac"));
93 if(relativeEnergyCut < 1e-6)
94 relativeEnergyCut = 1.0;
95 minimumKineticEnergy = relativeEnergyCut * referenceKineticEnergy;
98 std::vector<char*> c_args;
99 for (
auto& arg : bdsim_args) {
100 c_args.push_back(
const_cast<char*
>(arg.c_str()));
104 linkBDSIM->Initialise(c_args.size(),
107 minimumKineticEnergy/CLHEP::GeV,
110 referenceParticleDefinition = prepareBDSParticleDefition(referenceParticlePDG, 0, referenceKineticEnergy,
static_cast<double>(referenceIonCharge));
115 double kineticEnergy,
117 G4ParticleDefinition *particleDefGeant =
nullptr;
121 if (pdg < 1000000000) {
122 particleDefGeant = g4particle_table->FindParticle(pdg);
124 kineticEnergy, momentum, 1,
nullptr);
127 particleDefGeant = g4ion_table->GetIon(pdg);
129 if (ionCharge == 0) {
130 ionCharge = particleDefGeant->GetAtomicNumber();
134 particleDefGeant->GetAtomicNumber(),
137 auto mass = g4ion_table->GetIonMass(ionDef->
Z(), ionDef->
A());
138 auto charge = ionDef->
Charge();
140 auto bdsimPartName =
"ion " + std::to_string(ionDef->
A()) +
141 " " + std::to_string(ionDef->
Z()) +
142 " " + std::to_string(charge);
145 kineticEnergy, momentum, 1, ionDef, pdg);
147 return particleDefinition;
150void BDSLinkTrackerInterface::AddParticle(
double x,
double y,
double px,
double py,
151 double ct,
double deltap,
double chi,
152 double chargeRatio,
double ,
153 int trackid,
int pdgid) {
155 auto q = chargeRatio * referenceParticleDefinition->
Charge();
156 auto mass_ratio = chargeRatio / chi;
157 auto p = referenceParticleDefinition->
Momentum() * (deltap + 1) * mass_ratio;
161 pdg = referenceParticleDefinition->
PDGID();
165 auto partDef = prepareBDSParticleDefition(pdg, p, 0, q);
166 auto t = - ct * CLHEP::m / (referenceParticleDefinition->
Beta() * CLHEP::c_light);
167 auto oneplusdelta = (1 + deltap);
168 auto xp = px / oneplusdelta;
169 auto yp = py / oneplusdelta;
170 auto sqrtarg = 1 - std::pow(xp,2) - std::pow(yp,2);
175 { zp = std::sqrt(sqrtarg); }
185 partDef->TotalEnergy(),
188 linkBunch->
AddParticle(partDef, coords, trackid, trackid);
192void BDSLinkTrackerInterface::AddParticle(
double x,
double y,
193 double px,
double py,
double pz,
195 int trackid,
int pdgid) {
200 pdg = referenceParticleDefinition->
PDGID();
201 q = referenceParticleDefinition->
Charge();
205 q = G4ParticleTable::GetParticleTable()->FindParticle(pdg)->GetPDGCharge();
208 auto p = std::sqrt(std::pow(px,2) +
211 auto partDef = prepareBDSParticleDefition(pdg, p, 0, q);
224 partDef->TotalEnergy(),
227 linkBunch->
AddParticle(partDef, coords, trackid, trackid);
231void BDSLinkTrackerInterface::AddParticles(std::vector<double> x, std::vector<double> y,
232 std::vector<double> px, std::vector<double> py,
233 std::vector<double> ct, std::vector<double> deltap,
234 std::vector<double> chi, std::vector<double> chargeRatio,
235 std::vector<double> s, std::vector<int> trackid,
236 std::vector<int> pdgid) {
237 for(
size_t i=0; i<x.size(); i++) {
238 AddParticle(x[i], y[i], px[i], py[i], ct[i], deltap[i],
239 chi[i], chargeRatio[i], s[i], trackid[i], pdgid[i]);
Interface class to use BDSIM with trackers.
Class to parse an ion particle definition.
G4double Charge() const
Accessor.
A bunch distribution that holds a bunch from a Link.
void AddParticle(BDSParticleDefinition *particleDefinitionIn, const BDSParticleCoordsFull &coordsIn, int externalParticleID, int externalParentID)
Append particle to the bunch for tracking.
A set of particle coordinates including energy and weight.
Wrapper for particle definition.
G4double Momentum() const
Accessor.
G4double Charge() const
Accessor.
G4double Beta() const
Accessor.