BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSPhysicsUtilities.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "G4Version.hh"
20
21#include "BDSDebug.hh"
22#include "BDSException.hh"
23#include "BDSGlobalConstants.hh"
24#include "BDSModularPhysicsList.hh"
25#include "BDSIonDefinition.hh"
26#include "BDSParticleDefinition.hh"
27#if G4VERSION_NUMBER > 1039
28#include "BDSPhysicsChannelling.hh"
29#endif
30#include "BDSPhysicsCutsAndLimits.hh"
31#include "BDSPhysicsEMDissociation.hh"
32#include "BDSPhysicsUtilities.hh"
33#include "BDSUtilities.hh"
34#include "BDSWarning.hh"
35#include "BDSEmStandardPhysicsOp4Channelling.hh" // included with bdsim
36
37#include "FTFP_BERT.hh"
38#include "globals.hh"
39#include "G4AntiNeutrinoE.hh"
40#include "G4AntiNeutrinoMu.hh"
41#include "G4AntiNeutrinoTau.hh"
42#include "G4AntiNeutron.hh"
43#include "G4AntiProton.hh"
44#include "G4Electron.hh"
45#include "G4EmParameters.hh"
46#include "G4EmStandardPhysics_option4.hh"
47#include "G4EmStandardPhysicsSS.hh"
48#include "G4DynamicParticle.hh"
49#include "G4Gamma.hh"
50#include "G4GenericBiasingPhysics.hh"
51#include "G4GenericIon.hh"
52#include "G4IonElasticPhysics.hh"
53#include "G4IonTable.hh"
54#include "G4KaonMinus.hh"
55#include "G4KaonPlus.hh"
56#include "G4MuonMinus.hh"
57#include "G4KaonZeroLong.hh"
58#include "G4LeptonConstructor.hh"
59#include "G4MuonPlus.hh"
60#include "G4NeutrinoE.hh"
61#include "G4NeutrinoMu.hh"
62#include "G4NeutrinoTau.hh"
63#include "G4Neutron.hh"
64#include "G4ParticleTable.hh"
65#include "G4ParticleTableIterator.hh"
66#include "G4PionMinus.hh"
67#include "G4PionPlus.hh"
68#include "G4PionZero.hh"
69#include "G4Positron.hh"
70#include "G4ProductionCutsTable.hh"
71#include "G4Proton.hh"
72#include "G4PhysListFactory.hh"
73#include "G4ProcessManager.hh"
74#include "G4ProcessVector.hh"
75#include "G4Proton.hh"
76#include "G4String.hh"
77#include "G4UImanager.hh"
78#if G4VERSION_NUMBER > 1049
79#include "G4ParticleDefinition.hh"
80#include "G4CoupledTransportation.hh"
81#include "G4Transportation.hh"
82#include <utility>
83#endif
84
85#if G4VERSION_NUMBER > 1069
86#include "G4HadronicParameters.hh"
87#endif
88
89#include "parser/beam.h"
90#include "parser/fastlist.h"
91#include "parser/physicsbiasing.h"
92
93#include <iomanip>
94#include <map>
95#include <regex>
96#include <set>
97#include <stdexcept>
98#include <string> // for stoi
99
100G4bool BDS::IsIon(const G4ParticleDefinition* particle)
101{
102 return G4IonTable::IsIon(particle) && particle!=G4Proton::Definition();
103}
104
105G4bool BDS::IsIon(const G4DynamicParticle* particle)
106{
107 return BDS::IsIon(particle->GetDefinition()) || particle->GetTotalOccupancy()>0;
108}
109
110G4VModularPhysicsList* BDS::BuildPhysics(const G4String& physicsList, G4int verbosity)
111{
112 // this must be done BEFORE any physics processes are constructed
113 // set the upper and lower energy levels applicable for all physics processes
115
116 G4VModularPhysicsList* result = nullptr;
117
119
121 G4String physicsListNameLower = BDS::LowerCase(physicsList);
122 G4bool useGeant4Physics = BDS::StrContains(physicsListNameLower, "g4");
123 G4bool completePhysics = BDS::StrContains(physicsListNameLower, "complete");
124 if (useGeant4Physics)
125 {
126 // strip off G4_ prefix - from original as G4 factory case sensitive
127 G4String geant4PhysicsList = physicsList.substr(2);
128 G4PhysListFactory factory;
129 if (!factory.IsReferencePhysList(geant4PhysicsList))
130 {
131 G4cerr << "Unknown Geant4 physics list \"" << geant4PhysicsList << "\"" << G4endl;
132 G4cout << "Available Geant4 hadronic physics lists:" << G4endl;
133 for (const auto &name : factory.AvailablePhysLists())
134 {G4cout << "\"" << name << "\"" << G4endl;}
135 G4cout << "Available Geant4 EM physics lists:" << G4endl;
136 for (const auto &name : factory.AvailablePhysListsEM())
137 {G4cout << "\"" << name << "\"" << G4endl;}
138 throw BDSException(__METHOD_NAME__, "Unknown Geant4 physics list \"" + geant4PhysicsList + "\"");
139 }
140 else
141 {
142 result = factory.GetReferencePhysList(geant4PhysicsList);
143 if (g->G4PhysicsUseBDSIMRangeCuts())
144 {BDS::SetRangeCuts(result);}
145 if (g->MinimumKineticEnergy() > 0 || g->G4PhysicsUseBDSIMCutsAndLimits())
146 {
147 G4cout << "\nAdding cuts and limits physics process to Geant4 reference physics list" << G4endl;
148 G4cout << "This is to enforce BDSIM range cuts and the minimumKinetic energy option.\n";
149 G4cout << "This is done by default for the functionality of BDSIM tracking and should not affect the physics greatly.\n";
150 G4cout << "See the BDSIM manual about Geant4 reference physics lists for details." << G4endl;
151 result->RegisterPhysics(new BDSPhysicsCutsAndLimits(g->ParticlesToExcludeFromCutsAsSet()));
152 }
153 else if (!g->G4PhysicsUseBDSIMCutsAndLimits() && g->Circular())
154 {
155 G4String message = "g4PhysicsUseBDSIMCutsAndLimits turned off but using a circular machine - circular mechanics will be broken";
156 BDS::Warning(__METHOD_NAME__, message);
157 }
158 }
159 }
160 else if (completePhysics)
161 {// we test one by one for the exact name of very specific physics lists
162 if (BDS::StrContains(physicsListNameLower, "channelling"))
163 {
164 G4cout << "Constructing \"" << physicsListNameLower << "\" complete physics list" << G4endl;
165#if G4VERSION_NUMBER > 1039
166 G4bool useEMD = BDS::StrContains(physicsListNameLower, "emd");
167 G4bool regular = BDS::StrContains(physicsListNameLower, "regular");
168 G4bool em4 = BDS::StrContains(physicsListNameLower, "em4");
169 G4bool emss = BDS::StrContains(physicsListNameLower, "emss");
170 // we don't assign 'result' variable or proceed as that would result in the
171 // range cuts being set for a complete physics list that we wouldn't use
172 auto r = BDS::ChannellingPhysicsComplete(useEMD, regular, em4, emss);
173 r->SetVerboseLevel(verbosity);
174 return r;
175#else
176 throw BDSException(__METHOD_NAME__, "Channel physics is not supported with Geant4 versions less than 10.4");
177#endif
178 }
179 else
180 {throw BDSException(__METHOD_NAME__, "Unknown 'complete' physics list \"" + physicsList + "\"");}
181 }
182 else
183 {
184 result = new BDSModularPhysicsList(physicsList);
185 BDS::SetRangeCuts(result, verbosity); // always set our range cuts for our physics list
186 }
187
188 // force construction of the particles - does no harm and helps with
189 // usage of exotic particle beams
190 result->ConstructParticle();
191 result->SetVerboseLevel(verbosity);
192
193 // apply any user-supplied macro to adjust geant4 physics parameters
194 G4UImanager* UIManager = G4UImanager::GetUIpointer();
195 G4String physicsMacro = BDSGlobalConstants::Instance()->Geant4PhysicsMacroFileName();
196 if (!physicsMacro.empty())
197 {
198 G4bool setInExecOptions = BDSGlobalConstants::Instance()->Geant4PhysicsMacroFileNameFromExecOptions();
199 G4String physicsMacroFull = BDS::GetFullPath(physicsMacro, false, setInExecOptions);
200 G4cout << "Applying geant4 physics macro file: " << physicsMacroFull << G4endl;
201 UIManager->ApplyCommand("/control/execute " + physicsMacroFull);
202 }
203
204 G4VUserPhysicsList* resultAsUserPhysicsList = dynamic_cast<G4VUserPhysicsList*>(result);
205 if (resultAsUserPhysicsList)
206 {// have to cast as they shadow functions and aren't virtual :(
207 resultAsUserPhysicsList->DumpCutValuesTable(verbosity);
208 resultAsUserPhysicsList->SetVerboseLevel(verbosity);
209 }
210 return result;
211}
212
213G4int BDS::NBeamParametersSet(const GMAD::Beam& beamDefinition,
214 const std::set<std::string>& keys)
215{
216 G4int nSet = 0;
217 for (const auto& k : keys)
218 {nSet += beamDefinition.HasBeenSet(k) ? 1 : 0;}
219 return nSet;
220}
221
222void BDS::ConflictingParametersSet(const GMAD::Beam& beamDefinition,
223 const std::set<std::string>& keys,
224 G4int nSet,
225 G4bool warnZeroParamsSet,
226 const G4String& unitString)
227{
228 if (nSet > 1)
229 {
230 G4cerr << "Beam> More than one parameter set - there should only be one" << G4endl;
231 for (const auto& k : keys)
232 {G4cerr << std::setw(14) << std::left << k << ": " << std::setw(7) << std::right << beamDefinition.get_value(k) << " " << unitString << G4endl;}
233 throw BDSException(__METHOD_NAME__, "conflicting parameters set");
234 }
235 else if (nSet == 0 && warnZeroParamsSet)
236 {
237 G4cerr << "Beam> One of the following required to be set" << G4endl;
238 for (const auto &k : keys)
239 {G4cerr << std::setw(14) << std::left << k << ": " << std::setw(7) << std::right << beamDefinition.get_value(k) << " " << unitString << G4endl;}
240 throw BDSException(__METHOD_NAME__, "insufficient parameters set");
241 }
242}
243
245 G4double ffact,
246 BDSParticleDefinition*& designParticle,
247 BDSParticleDefinition*& beamParticle,
248 G4bool& beamDifferentFromDesignParticle)
249{
250 if (beamDefinition.particle.empty())
251 {throw BDSException("Beam> no \"particle\" specified (required).");}
252
253 // check only one of the following has been set - ie no conflicting information
254 std::set<std::string> keysDesign = {"energy", "momentum", "kineticEnergy"};
255 G4int nSetDesign = BDS::NBeamParametersSet(beamDefinition, keysDesign);
256 BDS::ConflictingParametersSet(beamDefinition, keysDesign, nSetDesign);
257 std::set<std::string> keysParticle = {"E0", "P0", "Ek0"};
258 G4int nSetParticle = BDS::NBeamParametersSet(beamDefinition, keysParticle);
259
260 designParticle = BDS::ConstructParticleDefinition(beamDefinition.particle,
261 beamDefinition.beamEnergy * CLHEP::GeV,
262 beamDefinition.beamKineticEnergy * CLHEP::GeV,
263 beamDefinition.beamMomentum * CLHEP::GeV,
264 ffact);
265
266 // ensure a default
267 std::string beamParticleName = beamDefinition.beamParticleName.empty() ? beamDefinition.particle : beamDefinition.beamParticleName;
268 beamDifferentFromDesignParticle = nSetParticle > 0 || beamParticleName != beamDefinition.particle;
269 if (nSetParticle > 0)
270 {// at least one specified so use all of the beam particle ones
271 BDS::ConflictingParametersSet(beamDefinition, keysParticle, nSetParticle, false);
272 beamParticle = BDS::ConstructParticleDefinition(beamParticleName,
273 beamDefinition.E0 * CLHEP::GeV,
274 beamDefinition.Ek0 * CLHEP::GeV,
275 beamDefinition.P0 * CLHEP::GeV,
276 ffact);
277 }
278 else if (beamDefinition.beamParticleName != beamDefinition.particle && !beamDefinition.beamParticleName.empty())
279 {// different particle name but no extra E0, Ek0 or P0 -> therefore use general beam defaults
280 beamParticle = BDS::ConstructParticleDefinition(beamParticleName,
281 beamDefinition.beamEnergy * CLHEP::GeV,
282 beamDefinition.beamKineticEnergy * CLHEP::GeV,
283 beamDefinition.beamMomentum * CLHEP::GeV,
284 ffact);
285 }
286 else
287 {
288 beamParticle = new BDSParticleDefinition(*designParticle);
289 }
290}
291
293 G4double totalEnergyIn,
294 G4double kineticEnergyIn,
295 G4double momentumIn,
296 G4double ffact)
297{
298 BDSParticleDefinition* particleDefB = nullptr; // result
299 G4String particleName = BDS::LowerCase(particleNameIn);
300
301 std::map<G4String, G4String> commonSubstitutions = { {"photon", "gamma"},
302 {"electron", "e-"},
303 {"positron", "e+"},
304 {"pion+", "pi+"},
305 {"pion-", "pi-"},
306 {"pion0", "pi0"} };
307
308 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
309 std::regex ionParticle("(ion\\s).*");
310 if (std::regex_match(particleName, ionParticle))
311 {
312 G4GenericIon::GenericIonDefinition(); // construct general ion particle
313 auto ionDef = new BDSIonDefinition(particleName); // parse the ion definition
314
315 G4IonTable* ionTable = particleTable->GetIonTable();
317 G4int ionPDGID = G4IonTable::GetNucleusEncoding(ionDef->Z(), ionDef->A());
318 G4double mass = ionTable->GetIonMass(ionDef->Z(), ionDef->A());
319 mass += ionDef->NElectrons()*G4Electron::Definition()->GetPDGMass();
320 G4double charge = ionDef->Charge(); // correct even if overridden
321 particleDefB = new BDSParticleDefinition(particleName, mass, charge,
322 totalEnergyIn, kineticEnergyIn, momentumIn, ffact, ionDef, ionPDGID);
323 delete ionDef;
324 }
325 else
326 {
327 // swap out some common name substitutions for Geant4 ones
328 auto searchName = commonSubstitutions.find(particleName);
329 if (searchName != commonSubstitutions.end())
330 {
331 G4cout << "Substituting particle name \"" << particleName << "\" for the Geant4 name: \"" << searchName->second << "\"" << G4endl;
332 particleName = searchName->second;
333 }
334
335 BDS::ConstructBeamParticleG4(particleName); // enforce construction of some basic particles
336 G4ParticleDefinition* particleDef = nullptr;
337
338 // try and see if it's an integer and therefore PDG ID, if not search by string
339 try
340 {
341 // we try this because std::stoi can throw a std::invalid_argument or
342 // std::out_of_range exception, both of which inherit std::logic_error
343 int particleID = std::stoi(particleName);
344 // we don't use the G4ParticleTable->FindParticle(int) because it unnecessarily
345 // checks for physics readiness and throws an exception. here we just inspect
346 // the encoding dictionary ourselves. it's all typedeffed but it's std::map<G4int, G4ParticleDefinition*>
347 G4ParticleTable::G4PTblEncodingDictionary* encoding = G4ParticleTable::fEncodingDictionary;
348 auto search = encoding->find(particleID);
349 if (search != encoding->end())
350 {particleDef = search->second;}
351 else
352 {throw BDSException(__METHOD_NAME__,"PDG ID \"" + particleName + "not found in particle table");}
353 }
354 catch (const std::logic_error&) // else, usual way by string search
355 {particleDef = particleTable->FindParticle(particleName);}
356 if (!particleDef)
357 {
359 throw BDSException(__METHOD_NAME__, "Particle \"" + particleName + "\" not found.");
360 }
361 particleDefB = new BDSParticleDefinition(particleDef, totalEnergyIn, kineticEnergyIn, momentumIn, ffact);
362 }
363 return particleDefB;
364}
365
366void BDS::ConstructBeamParticleG4(const G4String& name)
367{
368 if (name == "proton")
369 {G4Proton::ProtonDefinition();}
370 else if (name == "antiproton")
371 {G4AntiProton::AntiProtonDefinition();}
372 else if (name == "e-")
373 {G4Electron::ElectronDefinition();}
374 else if (name == "e+")
375 {G4Positron::PositronDefinition();}
376 else if (name == "pi-")
377 {G4PionMinus::PionMinusDefinition();}
378 else if (name == "pi+")
379 {G4PionPlus::PionPlusDefinition();}
380 else if (name == "pi0")
381 {G4PionZero::PionZeroDefinition();}
382 else if (name == "neutron")
383 {G4Neutron::NeutronDefinition();}
384 else if (name == "photon" || name == "gamma")
385 {G4Gamma::Gamma();}
386 else if (name == "mu-")
387 {G4MuonMinus::MuonMinusDefinition();}
388 else if (name == "mu+")
389 {G4MuonPlus::MuonPlusDefinition();}
390 else if (name == "kaon-")
391 {G4KaonMinus::KaonMinusDefinition();}
392 else if (name == "kaon+")
393 {G4KaonPlus::KaonPlusDefinition();}
394 else if (name == "kaon0L")
395 {G4KaonZeroLong::KaonZeroLongDefinition();}
396 else if (name == "nu_e")
397 {G4NeutrinoE::NeutrinoEDefinition();}
398 else if (name == "anti_nu_e")
399 {G4AntiNeutrinoE::AntiNeutrinoEDefinition();}
400 else if (name == "nu_mu")
401 {G4NeutrinoMu::NeutrinoMuDefinition();}
402 else if (name == "anti_nu_mu")
403 {G4AntiNeutrinoMu::AntiNeutrinoMuDefinition();}
404 else if (name == "nu_tau")
405 {G4NeutrinoTau::NeutrinoTauDefinition();}
406 else if (name == "anti_nu_tau")
407 {G4AntiNeutrinoTau::AntiNeutrinoTauDefinition();}
408 else
409 {
410 G4String msg = "Unknown common particle type \"" + name;
411 msg += "\" - if it doesn't work, include all \"all_particles\" in the physicsList option.";
412 BDS::Warning(__METHOD_NAME__, msg);
413 }
414}
415
417{
418 // e-, e+, v_e, v_e(bar)
419 G4Electron::ElectronDefinition();
420 G4Positron::PositronDefinition();
421 G4NeutrinoE::NeutrinoEDefinition();
422 G4AntiNeutrinoE::AntiNeutrinoEDefinition();
423
424 // p, pbar, neutron, anti-neutron
425 G4Proton::ProtonDefinition();
426 G4AntiProton::AntiProtonDefinition();
427 G4Neutron::NeutronDefinition();
428 G4AntiNeutron::AntiNeutronDefinition();
429
430 // photon
431 G4Gamma::Gamma();
432}
433
435{
436 G4LeptonConstructor::ConstructParticle();
437 G4PionPlus::PionPlusDefinition();
438 G4PionMinus::PionMinusDefinition();
439 G4KaonPlus::KaonPlusDefinition();
440 G4KaonMinus::KaonMinusDefinition();
441 G4GenericIon::GenericIonDefinition();
442}
443
444void BDS::PrintPrimaryParticleProcesses(const G4String& primaryParticleName)
445{
446 G4cout << "Register physics processes by name for the primary particle \""
447 << primaryParticleName << "\":" << G4endl;
448
449 auto particle = G4ParticleTable::GetParticleTable()->FindParticle(primaryParticleName);
450 if (!particle)
451 {// could be ion that isn't defined
452#ifdef BDSDEBUG
453 G4cout << __METHOD_NAME__ << "primary particle not defined yet - could be ion" << G4endl;
454#endif
455 return;
456 }
457 auto pl = particle->GetProcessManager()->GetProcessList();
458 for (G4int i = 0; i < (G4int)pl->length(); i++)
459 {G4cout << "\"" << (*pl)[i]->GetProcessName() << "\"" << G4endl;}
460}
461
462
463G4GenericBiasingPhysics* BDS::BuildAndAttachBiasWrapper(const GMAD::FastList<GMAD::PhysicsBiasing>& biases)
464{
465 std::set<const G4ParticleDefinition*> particlesToBias;
466 for (const auto& b : biases)
467 {
468 const G4ParticleDefinition* particle = nullptr;
469 G4String particleName = G4String(b.particle);
470 // this works for "GenericIon" too
471 particle = G4ParticleTable::GetParticleTable()->FindParticle(particleName);
472
473 if (particle)
474 {particlesToBias.insert(particle);}
475 else
476 {throw BDSException(__METHOD_NAME__, "Unknown particle type for biasing: \"" + particleName + "\"");}
477 }
478
479 if (particlesToBias.empty()) // nothing valid to bias
480 {return nullptr;}
481
482 G4GenericBiasingPhysics* physBias = new G4GenericBiasingPhysics();
483 for (auto part : particlesToBias)
484 {
485 const G4String& particleName = part->GetParticleName();
486 G4cout << __METHOD_NAME__ << "wrapping \"" << particleName << "\" for biasing" << G4endl;
487 physBias->Bias(particleName);
488 }
489 return physBias;
490}
491
493{
494 G4cout << __METHOD_NAME__ << "Defined particles: " << G4endl;
495 auto it = G4ParticleTable::GetParticleTable()->GetIterator();
496 it->reset(); // because there's only 1 iterator due to geant4 design
497 while ((*it)())
498 {G4cout << it->value()->GetParticleName() << " ";}
499 G4cout << G4endl;
500}
501
502#if G4VERSION_NUMBER > 1039
503G4VModularPhysicsList* BDS::ChannellingPhysicsComplete(G4bool useEMD,
504 G4bool regular,
505 G4bool em4,
506 G4bool emss)
507{
508 G4VModularPhysicsList* physlist = new FTFP_BERT();
509 physlist->RegisterPhysics(new G4IonElasticPhysics()); // not included by default in FTFP_BERT
510 G4GenericBiasingPhysics* biasingPhysics = new G4GenericBiasingPhysics();
511 physlist->RegisterPhysics(new BDSPhysicsChannelling());
512 if (!regular)
513 {
514 // replace the EM physics in the Geant4 provided FTFP_BERT composite physics list
515 physlist->ReplacePhysics(new BDSEmStandardPhysicsOp4Channelling());
516 }
517 else if (em4)
518 {
519 physlist->ReplacePhysics(new G4EmStandardPhysics_option4());
520 }
521 else if (emss)
522 {
523 physlist->ReplacePhysics(new G4EmStandardPhysicsSS());
524 }
525
526 // optional electromagnetic dissociation that isn't in FTFP_BERT by default
527 if (useEMD)
528 {
529 G4cout << "Adding EM Dissocation to crystal channelling physics list" << G4endl;
530 physlist->RegisterPhysics(new BDSPhysicsEMDissociation());
531 }
532
533 biasingPhysics->PhysicsBiasAllCharged();
534 physlist->RegisterPhysics(biasingPhysics);
535 if (BDSGlobalConstants::Instance()->MinimumKineticEnergy() > 0 &&
536 BDSGlobalConstants::Instance()->G4PhysicsUseBDSIMCutsAndLimits())
537 {
538 G4cout << "\nWARNING - adding cuts and limits physics process to \"COMPLETE\" physics list" << G4endl;
539 G4cout << "This is to enforce BDSIM range cuts and the minimumKinetic energy option.\n";
540 G4cout << "This can be turned off by setting option, g4PhysicsUseBDSIMCutsAndLimits=0;\n" << G4endl;
541 physlist->RegisterPhysics(new BDSPhysicsCutsAndLimits(BDSGlobalConstants::Instance()->ParticlesToExcludeFromCutsAsSet()));
542 }
543 return physlist;
544}
545#endif
546
547void BDS::SetRangeCuts(G4VModularPhysicsList* physicsList, G4int verbosity)
548{
550
551 // overwrite when explicitly set in options
552 if (globals->DefaultRangeCutsSet())
553 {
554 G4cout << __METHOD_NAME__ << "Default production range cut " << physicsList->GetDefaultCutValue() << " mm" << G4endl;
555 physicsList->SetDefaultCutValue(globals->DefaultRangeCut());
556 }
557 if (globals->ProdCutPhotonsSet())
558 {
559 G4cout << __METHOD_NAME__ << "Photon production range cut " << physicsList->GetCutValue("gamma") << " mm" << G4endl;
560 physicsList->SetCutValue(globals->ProdCutPhotons(), "gamma");
561 }
562 if (globals->ProdCutElectronsSet())
563 {
564 G4cout << __METHOD_NAME__ << "Electron production range cut " << physicsList->GetCutValue("e-") << " mm" << G4endl;
565 physicsList->SetCutValue(globals->ProdCutElectrons(),"e-");
566 }
567 if (globals->ProdCutPositronsSet())
568 {
569 G4cout << __METHOD_NAME__ << "Positron production range cut " << physicsList->GetCutValue("e+") << " mm" << G4endl;
570 physicsList->SetCutValue(globals->ProdCutPositrons(),"e+");
571 }
572 if (globals->ProdCutProtonsSet())
573 {
574 G4cout << __METHOD_NAME__ << "Proton production range cut " << physicsList->GetCutValue("proton") << " mm" << G4endl;
575 physicsList->SetCutValue(globals->ProdCutProtons(), "proton");
576 }
577
578 // inspection of the physics list doesn't work at this point and seems to always return 0 apart from the default
579 G4cout << __METHOD_NAME__ << "Range cuts from inspection of the physics list" << G4endl;
580 G4cout << __METHOD_NAME__ << "Default production range cut " << physicsList->GetDefaultCutValue() << " mm" << G4endl;
581
582#ifdef BDSDEBUG
583 G4cout << __METHOD_NAME__ << "List of all constructed particles by physics lists" << G4endl;
584 for (auto particle : *G4ParticleTable::fDictionary)
585 {G4cout << particle.second->GetParticleName() << ", ";}
586 G4cout << G4endl;
587#endif
588
589 physicsList->DumpCutValuesTable(verbosity);
590}
591
593{
595 G4double energyLimitLow = globals->PhysicsEnergyLimitLow();
596 G4double energyLimitHigh = globals->PhysicsEnergyLimitHigh();
597 G4bool setEnergyLimitLow = BDS::IsFinite(energyLimitLow);
598 G4bool setEnergyLimitHigh = BDS::IsFinite(energyLimitHigh);
599
600 if (setEnergyLimitLow || setEnergyLimitHigh)
601 {
602 auto table = G4ProductionCutsTable::GetProductionCutsTable();
603 G4double defaultEnergyLimitLow = table->GetLowEdgeEnergy();
604 G4double defaultEnergyLimitHigh = table->GetHighEdgeEnergy();
605 G4double elLow = setEnergyLimitLow ? energyLimitLow : defaultEnergyLimitLow;
606 G4double elHigh = setEnergyLimitHigh ? energyLimitHigh : defaultEnergyLimitHigh;
607 table->SetEnergyRange(elLow, elHigh);
608 if (setEnergyLimitLow)
609 {
610 G4cout << __METHOD_NAME__ << "set EM physics low energy limit: "
611 << elLow/CLHEP::MeV << " MeV" << G4endl;
612 }
613 if (setEnergyLimitHigh)
614 {
615 G4cout << __METHOD_NAME__ << "set high energy limit: "
616 << elHigh/CLHEP::TeV << " TeV" << G4endl;
617 if (elHigh > G4EmParameters::Instance()->MaxKinEnergy())
618 {
619 G4cout << __METHOD_NAME__ << "set EM physics Ek limit to " << elHigh/CLHEP::TeV << " TeV" << G4endl;
620 G4EmParameters::Instance()->SetMaxEnergy(elHigh);
621 }
622#if G4VERSION_NUMBER > 1069
623 // this was in a patch of our own before 10.7 and compensates for ion physics
624 if (elHigh > G4HadronicParameters::Instance()->GetMaxEnergy())
625 {
626 G4cout << __METHOD_NAME__ << "set hadronic physics Ek limit to " << elHigh/CLHEP::TeV << " TeV" << G4endl;
627 G4HadronicParameters::Instance()->SetMaxEnergy(elHigh);
628 }
629#endif
630 }
631 }
632}
633
634#if G4VERSION_NUMBER > 1049
636{
637 G4ParticleDefinition* particleDef = particleDefinition->ParticleDefinition();
639}
640
641void BDS::FixGeant105ThreshholdsForParticle(const G4ParticleDefinition* particleDef)
642{
643 // in the case of ions the particle definition isn't available early on so protect
644 // against this
645 if (!particleDef)
646 {return;}
647 // taken from the Geant4.10.5 field01 example
648 // used to compensate for aggressive killing in Geant4.10.5
649 G4double warningEnergy = 1.0 * CLHEP::kiloelectronvolt; // Arbitrary
650 G4double importantEnergy = 10.0 * CLHEP::kiloelectronvolt; // Arbitrary
651 G4double numberOfTrials = 1500; // Arbitrary
652 auto transportPair = BDS::FindTransportation(particleDef);
653 auto transport = transportPair.first;
654 auto coupledTransport = transportPair.second;
655
656 if (transport)
657 {
658 // Change the values of the looping particle parameters of Transportation
659 transport->SetThresholdWarningEnergy(warningEnergy);
660 transport->SetThresholdImportantEnergy(importantEnergy);
661 transport->SetThresholdTrials(numberOfTrials);
662 }
663 else if(coupledTransport)
664 {
665 // Change the values for Coupled Transport
666 coupledTransport->SetThresholdWarningEnergy(warningEnergy);
667 coupledTransport->SetThresholdImportantEnergy(importantEnergy);
668 coupledTransport->SetThresholdTrials(numberOfTrials);
669 }
670}
671
672std::pair<G4Transportation*, G4CoupledTransportation*> BDS::FindTransportation(const G4ParticleDefinition* particleDef)
673{
674 const auto* partPM = particleDef->GetProcessManager();
675
676 G4VProcess* partTransport = partPM->GetProcess("Transportation");
677 auto transport = dynamic_cast<G4Transportation*>(partTransport);
678
679 partTransport = partPM->GetProcess("CoupledTransportation");
680 auto coupledTransport = dynamic_cast<G4CoupledTransportation*>(partTransport);
681
682 return std::make_pair(transport, coupledTransport);
683}
684#endif
Copy of crystal channelling em physics from Geant4.
General exception with possible name of object and message.
Definition: BDSException.hh:35
A class that holds global options and constants.
static BDSGlobalConstants * Instance()
Access method.
Class to parse an ion particle definition.
Modular physics list based on Geant4 constructors.
Wrapper for particle definition.
G4ParticleDefinition * ParticleDefinition() const
Accessor.
Channelling physics process.
Physics processes required for user tracking limits.
Electromagnetic dissociation for high energy ions.
std::string particle
beam parameters
Definition: beamBase.h:40
double beamKineticEnergy
beam parameters
Definition: beamBase.h:43
double beamEnergy
beam parameters
Definition: beamBase.h:42
double Ek0
initial beam centroid
Definition: beamBase.h:66
std::string beamParticleName
beam parameters
Definition: beamBase.h:41
double P0
initial beam centroid
Definition: beamBase.h:67
double beamMomentum
beam parameters
Definition: beamBase.h:44
double E0
initial beam centroid
Definition: beamBase.h:65
Beam class.
Definition: beam.h:44
double get_value(std::string name) const
get method (only for doubles)
Definition: beam.cc:39
bool HasBeenSet(const std::string &name) const
Whether a parameter has been set using the set_value method or not.
Definition: beam.cc:142
void SetRangeCuts(G4VModularPhysicsList *physicsList, G4int verbosity=1)
void ConstructBeamParticleG4(const G4String &name)
Ensure required beam particle has been constructed for Geant4 purposes.
void PrintDefinedParticles()
G4bool StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
Definition: BDSUtilities.cc:66
std::pair< G4Transportation *, G4CoupledTransportation * > FindTransportation(const G4ParticleDefinition *particleDef)
Taken from Geant4 field01 example. Get the two possible transportation processes.
G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false)
BDSParticleDefinition * ConstructParticleDefinition(const G4String &particleNameIn, G4double totalEnergyIn, G4double kineticEnergyIn, G4double momentumIn, G4double ffact=1)
G4String LowerCase(const G4String &str)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
void ConflictingParametersSet(const GMAD::Beam &beamDefinition, const std::set< std::string > &keys, G4int nSet, G4bool warnZeroParamsSet=true, const G4String &unitString="")
Throw an exception if too few or too many parameters are set for the supplied keys.
void PrintPrimaryParticleProcesses(const G4String &primaryParticleName)
void CheckAndSetEnergyValidityRange()
void FixGeant105ThreshholdsForBeamParticle(const BDSParticleDefinition *particleDefinition)
Apply FixGeant105ThreshholdsForParticle to the beam particle definition.
void FixGeant105ThreshholdsForParticle(const G4ParticleDefinition *particleDefinition)
void ConstructExtendedParticleSet()
void ConstructDesignAndBeamParticle(const GMAD::Beam &beamDefinition, G4double ffact, BDSParticleDefinition *&designParticle, BDSParticleDefinition *&beamParticle, G4bool &beamDifferentFromDesignParticle)
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())
G4int NBeamParametersSet(const GMAD::Beam &beamDefinition, const std::set< std::string > &keys)
Count how many out of the set of keys in a beam instance are set.
G4VModularPhysicsList * BuildPhysics(const G4String &physicsList, G4int verbosity=1)
G4bool IsIon(const G4ParticleDefinition *particle)
Whether a particle is an ion. A proton is counted NOT as an ion.
void ConstructMinimumParticleSet()
G4VModularPhysicsList * ChannellingPhysicsComplete(G4bool useEMD=false, G4bool regular=false, G4bool em4=false, G4bool emss=false)
Build the physics required for channelling to work correctly.