Geant4  10.02.p02
G4GenericBiasingPhysics.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // $Id$
27 //
28 //---------------------------------------------------------------------------
29 //
30 // ClassName: G4GenericBiasingPhysics
31 //
32 // Author: M. Verderi (Sept.10.2013)
33 // Modified:
34 // 07/11/2014, M. Verderi : fix bug of PhysicsBias(...) which was not taking
35 // into account the vector of processes passed, but biasing all.
36 //
37 //----------------------------------------------------------------------------
38 //
39 //
40 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
41 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
42 
44 
45 #include "G4ParticleDefinition.hh"
46 #include "G4ProcessManager.hh"
47 
48 #include "G4BiasingHelper.hh"
50 
51 // factory
53 //
55 
56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
57 
59 : G4VPhysicsConstructor(name),
60  fPhysBiasAllCharged(false), fNonPhysBiasAllCharged(false),
61  fPhysBiasAllChargedISL(false), fNonPhysBiasAllChargedISL(false),
62  fPhysBiasAllNeutral(false), fNonPhysBiasAllNeutral(false),
63  fPhysBiasAllNeutralISL(false), fNonPhysBiasAllNeutralISL(false),
64  fVerbose(false)
65 {;}
66 
67 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
68 
70 {;}
71 
72 
73 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
74 
76 {
77  fBiasedParticles.push_back(particleName);
78  std::vector< G4String > dummy;
79  fBiasedProcesses.push_back(dummy);
80  fBiasAllProcesses.push_back(true);
81 }
82 
83 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
84 
85 void G4GenericBiasingPhysics::PhysicsBias(const G4String& particleName, const std::vector< G4String >& processNames)
86 {
87  fBiasedParticles.push_back(particleName);
88  fBiasedProcesses.push_back(processNames);
89  fBiasAllProcesses.push_back(false);
90 }
91 
92 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
93 
95 {
96  fNonPhysBiasedParticles.push_back(particleName);
97 }
98 
99 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
100 
101 void G4GenericBiasingPhysics::Bias(const G4String& particleName)
102 {
103  PhysicsBias(particleName);
104  NonPhysicsBias(particleName);
105 }
106 
107 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
108 
109 void G4GenericBiasingPhysics::Bias(const G4String& particleName, const std::vector< G4String >& processNames)
110 {
111  PhysicsBias(particleName, processNames);
112  NonPhysicsBias(particleName);
113 }
114 
115 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
116 void G4GenericBiasingPhysics::PhysicsBiasAddPDGRange( G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle )
117 {
118  if ( PDGlow > PDGhigh ) G4cout << " G4GenericBiasingPhysics::PhysicsBiasAddPDGRange(...) : PDGlow > PDGhigh, call ignored." << G4endl;
119  fPhysBiasByPDGRangeLow .push_back( PDGlow );
120  fPhysBiasByPDGRangeHigh.push_back( PDGhigh );
121  if ( includeAntiParticle )
122  {
123  fPhysBiasByPDGRangeLow .push_back( -PDGhigh );
124  fPhysBiasByPDGRangeHigh.push_back( -PDGlow );
125  }
126 }
127 
128 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
129 void G4GenericBiasingPhysics::NonPhysicsBiasAddPDGRange( G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle )
130 {
131  if ( PDGlow > PDGhigh ) G4cout << " G4GenericBiasingPhysics::NonPhysicsBiasAddPDGRange(...) : PDGlow > PDGhigh, call ignored." << G4endl;
132  fNonPhysBiasByPDGRangeLow .push_back( PDGlow );
133  fNonPhysBiasByPDGRangeHigh.push_back( PDGhigh );
134  if ( includeAntiParticle )
135  {
136  fNonPhysBiasByPDGRangeLow .push_back( -PDGhigh );
137  fNonPhysBiasByPDGRangeHigh.push_back( -PDGlow );
138  }
139 }
140 
141 
142 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
143 void G4GenericBiasingPhysics::BiasAddPDGRange( G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle )
144 {
145  if ( PDGlow > PDGhigh ) G4cout << " G4GenericBiasingPhysics::BiasAddPDGRange(...) : PDGlow > PDGhigh, call ignored." << G4endl;
146  PhysicsBiasAddPDGRange ( PDGlow, PDGhigh, includeAntiParticle );
147  NonPhysicsBiasAddPDGRange( PDGlow, PDGhigh, includeAntiParticle );
148 }
149 
151 {
152  fPhysBiasAllCharged = true;
153  fPhysBiasAllChargedISL = includeShortLived;
154 }
156 {
157  fNonPhysBiasAllCharged = true;
158  fNonPhysBiasAllChargedISL = includeShortLived;
159 }
161 {
162  fPhysBiasAllCharged = true;
163  fNonPhysBiasAllCharged = true;
164  fPhysBiasAllChargedISL = includeShortLived;
165  fNonPhysBiasAllChargedISL = includeShortLived;
166 }
168 {
169  fPhysBiasAllNeutral = true;
170  fPhysBiasAllNeutralISL = includeShortLived;
171 }
173 {
174  fNonPhysBiasAllNeutral = true;
175  fNonPhysBiasAllNeutralISL = includeShortLived;
176 }
178 {
179  fPhysBiasAllNeutral = true;
180  fNonPhysBiasAllNeutral = true;
181  fPhysBiasAllNeutralISL = includeShortLived;
182  fNonPhysBiasAllNeutralISL = includeShortLived;
183 }
184 
185 
186 
187 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
188 
190 {;}
191 
192 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
193 
195 {
196 
197  // -- bias setup per individual particle name:
198  aParticleIterator->reset();
199 
200  while( (*aParticleIterator)() )
201  {
202  G4ParticleDefinition* particle = aParticleIterator->value();
203  G4String particleName = particle->GetParticleName();
204  G4ProcessManager* pmanager = particle->GetProcessManager();
205 
206  // -- include non physics process interface for biasing:
207  if ( std::find(fNonPhysBiasedParticles.begin(),
209  particleName ) != fNonPhysBiasedParticles.end() )
210  {
212  }
213 
214  // -- wrap biased physics processes, all processes or only user selected:
215  std::vector< G4String >::const_iterator particleIt =
216  std::find(fBiasedParticles.begin(),
217  fBiasedParticles.end(),
218  particleName );
219  if ( particleIt == fBiasedParticles.end() ) continue;
220 
221  std::vector < G4String >& biasedProcesses = fBiasedProcesses [ particleIt - fBiasedParticles.begin() ];
222  G4bool biasAll = fBiasAllProcesses[ particleIt - fBiasedParticles.begin() ];
223 
224  if ( biasAll )
225  {
226  G4ProcessVector* vprocess = pmanager->GetProcessList();
227  for (G4int ip = 0 ; ip < vprocess->size() ; ip++)
228  {
229  G4VProcess* process = (*vprocess)[ip];
230  biasedProcesses.push_back( process->GetProcessName() );
231  }
232  }
233 
234  G4bool restartLoop(true);
235  while ( restartLoop )
236  {
237  for (std::size_t ip = 0 ; ip < biasedProcesses.size() ; ip++)
238  {
239  G4bool activ = G4BiasingHelper::ActivatePhysicsBiasing(pmanager, biasedProcesses[ip] );
240  restartLoop = activ;
241  if ( restartLoop ) break;
242  }
243  }
244 
245  }
246 
247 
248  // -- bias setup per group:
249  aParticleIterator->reset();
250 
251  while( (*aParticleIterator)() )
252  {
253  G4ParticleDefinition* particle = aParticleIterator->value();
254  G4String particleName = particle->GetParticleName();
255  G4ProcessManager* pmanager = particle->GetProcessManager();
256 
257  // -- exclude particles invidually specified by name:
258  if ( std::find( fNonPhysBiasedParticles.begin(),
260  particleName ) != fNonPhysBiasedParticles.end() ) continue;
261 
262  if ( std::find( fBiasedParticles.begin(),
263  fBiasedParticles.end(),
264  particleName ) != fBiasedParticles.end() ) continue;
265 
266 
267  G4bool physBias(false), nonPhysBias(false);
268 
269  auto PDG = particle->GetPDGEncoding();
270 
271  // -- include particle if in right PDG range:
272  for ( size_t i = 0 ; i < fPhysBiasByPDGRangeLow.size() ; i++ )
273  if ( ( PDG >= fPhysBiasByPDGRangeLow[i] ) && ( PDG <= fPhysBiasByPDGRangeHigh[i] ) )
274  {
275  physBias = true;
276  break;
277  }
278  for ( size_t i = 0 ; i < fNonPhysBiasByPDGRangeLow.size() ; i++ )
279  if ( ( PDG >= fNonPhysBiasByPDGRangeLow[i] ) && ( PDG <= fNonPhysBiasByPDGRangeHigh[i] ) )
280  {
281  nonPhysBias = true;
282  break;
283  }
284 
285  // -- if particle has not yet any biasing, include it on charge criteria:
286  if ( ( physBias == false ) && ( nonPhysBias == false ) )
287  {
288  if ( std::abs( particle->GetPDGCharge() ) > DBL_MIN )
289  {
290  if ( fPhysBiasAllCharged ) if ( fPhysBiasAllChargedISL || !particle->IsShortLived() ) physBias = true;
291  if ( fNonPhysBiasAllCharged ) if ( fNonPhysBiasAllChargedISL || !particle->IsShortLived() ) nonPhysBias = true;
292  }
293  else
294  {
295  if ( fPhysBiasAllNeutral ) if ( fPhysBiasAllNeutralISL || !particle->IsShortLived() ) physBias = true;
296  if ( fNonPhysBiasAllNeutral ) if ( fNonPhysBiasAllNeutralISL || !particle->IsShortLived() ) nonPhysBias = true;
297  }
298  }
299 
300 
301  if ( nonPhysBias ) G4BiasingHelper::ActivateNonPhysicsBiasing(pmanager);
302 
303  if ( physBias )
304  {
305  std::vector < G4String > biasedProcesses;
306  G4ProcessVector* vprocess = pmanager->GetProcessList();
307  for (G4int ip = 0 ; ip < vprocess->size() ; ip++)
308  {
309  G4VProcess* process = (*vprocess)[ip];
310  biasedProcesses.push_back( process->GetProcessName() );
311  }
312 
313  G4bool restartLoop(true);
314  while ( restartLoop )
315  {
316  for (std::size_t ip = 0 ; ip < biasedProcesses.size() ; ip++)
317  {
318  G4bool activ = G4BiasingHelper::ActivatePhysicsBiasing(pmanager, biasedProcesses[ip] );
319  restartLoop = activ;
320  if ( restartLoop ) break;
321  }
322  }
323  }
324 
325  }
326 
327 
328 
329  if ( fVerbose )
330  {
331  // -- print:
332  aParticleIterator->reset();
333 
334  while( (*aParticleIterator)() )
335  {
336  G4ParticleDefinition* particle = aParticleIterator->value();
337  G4String particleName = particle->GetParticleName();
338  G4ProcessManager* pmanager = particle->GetProcessManager();
339 
340  G4bool isBiased(false);
341  G4String processNames;
342  G4int icount(0);
343 
344  G4ProcessVector* vprocess = pmanager->GetProcessList();
345  for (G4int ip = 0 ; ip < vprocess->size() ; ip++)
346  {
347  G4VProcess* process = (*vprocess)[ip];
348  G4BiasingProcessInterface* pb = dynamic_cast< G4BiasingProcessInterface* >(process);
349  if ( pb != nullptr )
350  {
351  isBiased = true;
352  if ( icount < 3 )
353  {
354  processNames += pb->GetProcessName();
355  processNames += " ";
356  }
357  else
358  {
359  processNames += "\n ";
360  processNames += pb->GetProcessName();
361  processNames += " ";
362  icount = 0;
363  }
364  icount++;
365  }
366  }
367  if ( isBiased )
368  {
369  if ( particle->IsShortLived() )
370  G4cout << std::setw(14) << particleName << " **** : " << processNames << G4endl;
371  else
372  G4cout << std::setw(18) << particleName << " : " << processNames << G4endl;
373  }
374  }
375  }
376 }
377 
void BiasAllCharged(G4bool includeShortLived=false)
std::vector< std::vector< G4String > > fBiasedProcesses
void PhysicsBiasAllNeutral(G4bool includeShortLived=false)
G4String name
Definition: TRTMaterials.hh:40
void NonPhysicsBiasAllCharged(G4bool includeShortLived=false)
void PhysicsBias(const G4String &particleName)
static void ActivateNonPhysicsBiasing(G4ProcessManager *pmanager, G4String nonPhysicsProcessName="")
G4ProcessManager * GetProcessManager() const
int G4int
Definition: G4Types.hh:78
std::vector< G4int > fNonPhysBiasByPDGRangeHigh
const G4String & GetParticleName() const
std::vector< G4int > fPhysBiasByPDGRangeHigh
void NonPhysicsBiasAddPDGRange(G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle=true)
void PhysicsBiasAllCharged(G4bool includeShortLived=false)
G4GLOB_DLL std::ostream G4cout
G4GenericBiasingPhysics(const G4String &name="BiasingP")
std::vector< G4int > fNonPhysBiasByPDGRangeLow
bool G4bool
Definition: G4Types.hh:79
#define aParticleIterator
G4_DECLARE_PHYSCONSTR_FACTORY(G4GenericBiasingPhysics)
static G4bool ActivatePhysicsBiasing(G4ProcessManager *pmanager, G4String physicsProcessToBias, G4String wrappedName="")
const G4String & GetProcessName() const
Definition: G4VProcess.hh:408
void PhysicsBiasAddPDGRange(G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle=true)
std::vector< G4int > fPhysBiasByPDGRangeLow
G4int size() const
void BiasAddPDGRange(G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle=true)
#define DBL_MIN
Definition: templates.hh:75
void NonPhysicsBiasAllNeutral(G4bool includeShortLived=false)
std::vector< G4String > fBiasedParticles
#define G4endl
Definition: G4ios.hh:61
void BiasAllNeutral(G4bool includeShortLived=false)
std::vector< G4String > fNonPhysBiasedParticles
std::vector< G4bool > fBiasAllProcesses
G4double GetPDGCharge() const
void NonPhysicsBias(const G4String &particleName)
G4ProcessVector * GetProcessList() const
void Bias(const G4String &particleName)