BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSBOptrMultiParticleChangeCrossSection.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
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#if G4VERSION_NUMBER > 1009 // consistent with BDSBOptChangeCrossSection
21
22#include "BDSBOptrChangeCrossSection.hh"
23#include "BDSBOptrMultiParticleChangeCrossSection.hh"
24#include "BDSDebug.hh"
25#include "BDSException.hh"
26#include "G4BiasingProcessInterface.hh"
27
28#include "globals.hh"
29#include "G4GenericIon.hh"
30#include "G4IonTable.hh"
31#include "G4ParticleDefinition.hh"
32#include "G4ParticleTable.hh"
33#include "G4Proton.hh"
34
35
36BDSBOptrMultiParticleChangeCrossSection::BDSBOptrMultiParticleChangeCrossSection():
37 G4VBiasingOperator("BDSIM General Biasing"),
38 fCurrentOperator(nullptr),
39 fnInteractions(0)
40{
41#ifdef BDSDEBUG
42 debug = true;
43#else
44 debug = false;
45#endif
46}
47
48BDSBOptrMultiParticleChangeCrossSection::~BDSBOptrMultiParticleChangeCrossSection()
49{}
50
51void BDSBOptrMultiParticleChangeCrossSection::AddParticle(const G4String& particleName)
52{
53 const G4ParticleDefinition* particle = G4ParticleTable::GetParticleTable()->FindParticle(particleName);
54
55 if (!particle)
56 {throw BDSException(__METHOD_NAME__, "Particle \"" + particleName + "\" not found");}
57
58 BDSBOptrChangeCrossSection* optr = new BDSBOptrChangeCrossSection(particleName,particleName);
59 fParticlesToBias.push_back(particle);
60 fBOptrForParticle[particle] = optr;
61 optr->StartRun();
62}
63
64void BDSBOptrMultiParticleChangeCrossSection::SetBias(const G4String& biasObjectName,
65 const G4String& particleName,
66 const G4String& process,
67 G4double dBias,
68 G4int iPrimary)
69{
70 G4String flagString = "";
71 switch (iPrimary)
72 {
73 case 1:
74 {flagString = "all"; break;}
75 case 2:
76 {flagString = "primaries"; break;}
77 case 3:
78 {flagString = "primaries & secondaries"; break;}
79 default:
80 {
81 throw BDSException("Error in biasing object \"" + biasObjectName +
82 "\": invalid particle flag \"" + std::to_string(iPrimary) +
83 "\" for biasing process \"" + process + "\" for particle \"" +
84 particleName + "\": can only be 1,2 or 3)");
85 }
86 }
87 // important feedback for the user
88 G4cout << "Bias> Biasing process \"" << process << "\" for particle \"" << particleName << "\" by factor " << dBias;
89 G4cout << ", for " << flagString << " particles" << G4endl;
90
91 const G4ParticleDefinition* particle = G4ParticleTable::GetParticleTable()->FindParticle(particleName);
92 if (!particle)
93 {throw BDSException(__METHOD_NAME__, "Particle \"" + particleName + "\" not found");}
94
95 try
96 {fBOptrForParticle[particle]->SetBias(process,dBias,iPrimary);}
97 catch (BDSException& e)
98 {
99 e.AppendToMessage("in bias definition \"" + biasObjectName + "\"");
100 throw e;
101 }
102}
103
104G4VBiasingOperation* BDSBOptrMultiParticleChangeCrossSection::ProposeOccurenceBiasingOperation(const G4Track* track,
105 const G4BiasingProcessInterface* callingProcess)
106{
107 // -- examples of limitations imposed to apply the biasing:
108 // -- limit application of biasing to primary particles only:
109 // if ( track->GetParentID() != 0 ) return 0;
110 // -- limit to at most 5 biased interactions:
111 // if ( fnInteractions > 4 ) return 0;
112 // -- and limit to a weight of at least 0.05:
113 // if ( track->GetWeight() < 0.05 ) return 0;
114
115 if (fCurrentOperator)
116 {return fCurrentOperator->GetProposedOccurenceBiasingOperation(track, callingProcess);}
117 else
118 {return nullptr;}
119}
120
121void BDSBOptrMultiParticleChangeCrossSection::StartTracking(const G4Track* track)
122{
123 // -- fetch the underneath biasing operator, if any, for the current particle type:
124 const G4ParticleDefinition* definition = track->GetParticleDefinition();
125 std::map <const G4ParticleDefinition*,BDSBOptrChangeCrossSection*>::iterator it = fBOptrForParticle.find(definition);
126 fCurrentOperator = 0;
127 if (it != fBOptrForParticle.end())
128 {fCurrentOperator = (*it).second;}
129
130 // try again for ions as they have a generic and specific definition
131 // processes are attached to the generic one
132 if (G4IonTable::IsIon(definition) && definition != G4Proton::Definition())
133 {
134 auto search = fBOptrForParticle.find(G4GenericIon::Definition());
135 if (search != fBOptrForParticle.end())
136 {fCurrentOperator = search->second;}
137 }
138 // -- reset count for number of biased interactions:
139 fnInteractions = 0;
140}
141
142void BDSBOptrMultiParticleChangeCrossSection::
143OperationApplied(const G4BiasingProcessInterface* callingProcess,
144 G4BiasingAppliedCase biasingCase,
145 G4VBiasingOperation* occurenceOperationApplied,
146 G4double weightForOccurenceInteraction,
147 G4VBiasingOperation* finalStateOperationApplied,
148 const G4VParticleChange* particleChangeProduced)
149{
150 // -- count number of biased interactions:
152
153 // -- inform the underneath biasing operator that a biased interaction occurred:
154 if (fCurrentOperator)
155 {
156 fCurrentOperator->ReportOperationApplied(callingProcess,
157 biasingCase,
158 occurenceOperationApplied,
159 weightForOccurenceInteraction,
160 finalStateOperationApplied,
161 particleChangeProduced);
162 }
163}
164
165#endif
Operator that changes cross section of a process for a particle.
G4int fnInteractions
Count number of biased interations for current track.
General exception with possible name of object and message.
Definition: BDSException.hh:35