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) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "G4Version.hh"
19#if G4VERSION_NUMBER > 1009 // consistent with BDSBOptChangeCrossSection
20
21#include "BDSBOptrChangeCrossSection.hh"
22#include "BDSBOptrMultiParticleChangeCrossSection.hh"
23#include "BDSDebug.hh"
24#include "BDSException.hh"
25#include "G4BiasingProcessInterface.hh"
26
27#include "globals.hh"
28#include "G4GenericIon.hh"
29#include "G4IonTable.hh"
30#include "G4ParticleDefinition.hh"
31#include "G4ParticleTable.hh"
32#include "G4Proton.hh"
33
34
35BDSBOptrMultiParticleChangeCrossSection::BDSBOptrMultiParticleChangeCrossSection():
36 G4VBiasingOperator("BDSIM General Biasing"),
37 fCurrentOperator(nullptr),
38 fnInteractions(0)
39{
40#ifdef BDSDEBUG
41 debug = true;
42#else
43 debug = false;
44#endif
45}
46
47BDSBOptrMultiParticleChangeCrossSection::~BDSBOptrMultiParticleChangeCrossSection()
48{}
49
50void BDSBOptrMultiParticleChangeCrossSection::AddParticle(const G4String& particleName)
51{
52 const G4ParticleDefinition* particle = G4ParticleTable::GetParticleTable()->FindParticle(particleName);
53
54 if (!particle)
55 {throw BDSException(__METHOD_NAME__, "Particle \"" + particleName + "\" not found");}
56
57 BDSBOptrChangeCrossSection* optr = new BDSBOptrChangeCrossSection(particleName,particleName);
58 fParticlesToBias.push_back(particle);
59 fBOptrForParticle[particle] = optr;
60 optr->StartRun();
61}
62
63void BDSBOptrMultiParticleChangeCrossSection::SetBias(const G4String& biasObjectName,
64 const G4String& particleName,
65 const G4String& process,
66 G4double dBias,
67 G4int iPrimary)
68{
69 G4String flagString = "";
70 switch (iPrimary)
71 {
72 case 1:
73 {flagString = "all"; break;}
74 case 2:
75 {flagString = "primaries"; break;}
76 case 3:
77 {flagString = "primaries & secondaries"; break;}
78 default:
79 {
80 throw BDSException("Error in biasing object \"" + biasObjectName +
81 "\": invalid particle flag \"" + std::to_string(iPrimary) +
82 "\" for biasing process \"" + process + "\" for particle \"" +
83 particleName + "\": can only be 1,2 or 3)");
84 }
85 }
86 // important feedback for the user
87 G4cout << "Bias> Biasing process \"" << process << "\" for particle \"" << particleName << "\" by factor " << dBias;
88 G4cout << ", for " << flagString << " particles" << G4endl;
89
90 const G4ParticleDefinition* particle = G4ParticleTable::GetParticleTable()->FindParticle(particleName);
91 if (!particle)
92 {throw BDSException(__METHOD_NAME__, "Particle \"" + particleName + "\" not found");}
93
94 try
95 {fBOptrForParticle[particle]->SetBias(process,dBias,iPrimary);}
96 catch (BDSException& e)
97 {
98 e.AppendToMessage("in bias definition \"" + biasObjectName + "\"");
99 throw e;
100 }
101}
102
103G4VBiasingOperation* BDSBOptrMultiParticleChangeCrossSection::ProposeOccurenceBiasingOperation(const G4Track* track,
104 const G4BiasingProcessInterface* callingProcess)
105{
106 // -- examples of limitations imposed to apply the biasing:
107 // -- limit application of biasing to primary particles only:
108 // if ( track->GetParentID() != 0 ) return 0;
109 // -- limit to at most 5 biased interactions:
110 // if ( fnInteractions > 4 ) return 0;
111 // -- and limit to a weight of at least 0.05:
112 // if ( track->GetWeight() < 0.05 ) return 0;
113
114 if (fCurrentOperator)
115 {return fCurrentOperator->GetProposedOccurenceBiasingOperation(track, callingProcess);}
116 else
117 {return nullptr;}
118}
119
120void BDSBOptrMultiParticleChangeCrossSection::StartTracking(const G4Track* track)
121{
122 // -- fetch the underneath biasing operator, if any, for the current particle type:
123 const G4ParticleDefinition* definition = track->GetParticleDefinition();
124 std::map <const G4ParticleDefinition*,BDSBOptrChangeCrossSection*>::iterator it = fBOptrForParticle.find(definition);
125 fCurrentOperator = 0;
126 if (it != fBOptrForParticle.end())
127 {fCurrentOperator = (*it).second;}
128
129 // try again for ions as they have a generic and specific definition
130 // processes are attached to the generic one
131 if (G4IonTable::IsIon(definition) && definition != G4Proton::Definition())
132 {
133 auto search = fBOptrForParticle.find(G4GenericIon::Definition());
134 if (search != fBOptrForParticle.end())
135 {fCurrentOperator = search->second;}
136 }
137 // -- reset count for number of biased interactions:
138 fnInteractions = 0;
139}
140
141void BDSBOptrMultiParticleChangeCrossSection::
142OperationApplied(const G4BiasingProcessInterface* callingProcess,
143 G4BiasingAppliedCase biasingCase,
144 G4VBiasingOperation* occurenceOperationApplied,
145 G4double weightForOccurenceInteraction,
146 G4VBiasingOperation* finalStateOperationApplied,
147 const G4VParticleChange* particleChangeProduced)
148{
149 // -- count number of biased interactions:
151
152 // -- inform the underneath biasing operator that a biased interaction occurred:
153 if (fCurrentOperator)
154 {
155 fCurrentOperator->ReportOperationApplied(callingProcess,
156 biasingCase,
157 occurenceOperationApplied,
158 weightForOccurenceInteraction,
159 finalStateOperationApplied,
160 particleChangeProduced);
161 }
162}
163
164#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.