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