Geant4  10.02.p03
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:
199  particleIterator->reset();
200 
201  while( (*particleIterator)() )
202  {
203  G4ParticleDefinition* particle = particleIterator->value();
204  G4String particleName = particle->GetParticleName();
205  G4ProcessManager* pmanager = particle->GetProcessManager();
206 
207  // -- include non physics process interface for biasing:
208  if ( std::find(fNonPhysBiasedParticles.begin(),
210  particleName ) != fNonPhysBiasedParticles.end() )
211  {
213  }
214 
215  // -- wrap biased physics processes, all processes or only user selected:
216  std::vector< G4String >::const_iterator particleIt =
217  std::find(fBiasedParticles.begin(),
218  fBiasedParticles.end(),
219  particleName );
220  if ( particleIt == fBiasedParticles.end() ) continue;
221 
222  std::vector < G4String >& biasedProcesses = fBiasedProcesses [ particleIt - fBiasedParticles.begin() ];
223  G4bool biasAll = fBiasAllProcesses[ particleIt - fBiasedParticles.begin() ];
224 
225  if ( biasAll )
226  {
227  G4ProcessVector* vprocess = pmanager->GetProcessList();
228  for (G4int ip = 0 ; ip < vprocess->size() ; ip++)
229  {
230  G4VProcess* process = (*vprocess)[ip];
231  biasedProcesses.push_back( process->GetProcessName() );
232  }
233  }
234 
235  G4bool restartLoop(true);
236  while ( restartLoop )
237  {
238  for (std::size_t ip = 0 ; ip < biasedProcesses.size() ; ip++)
239  {
240  G4bool activ = G4BiasingHelper::ActivatePhysicsBiasing(pmanager, biasedProcesses[ip] );
241  restartLoop = activ;
242  if ( restartLoop ) break;
243  }
244  }
245 
246  }
247 
248 
249  // -- bias setup per group:
250  particleIterator->reset();
251 
252  while( (*particleIterator)() )
253  {
254  G4ParticleDefinition* particle = particleIterator->value();
255  G4String particleName = particle->GetParticleName();
256  G4ProcessManager* pmanager = particle->GetProcessManager();
257 
258  // -- exclude particles invidually specified by name:
259  if ( std::find( fNonPhysBiasedParticles.begin(),
261  particleName ) != fNonPhysBiasedParticles.end() ) continue;
262 
263  if ( std::find( fBiasedParticles.begin(),
264  fBiasedParticles.end(),
265  particleName ) != fBiasedParticles.end() ) continue;
266 
267 
268  G4bool physBias(false), nonPhysBias(false);
269 
270  auto PDG = particle->GetPDGEncoding();
271 
272  // -- include particle if in right PDG range:
273  for ( size_t i = 0 ; i < fPhysBiasByPDGRangeLow.size() ; i++ )
274  if ( ( PDG >= fPhysBiasByPDGRangeLow[i] ) && ( PDG <= fPhysBiasByPDGRangeHigh[i] ) )
275  {
276  physBias = true;
277  break;
278  }
279  for ( size_t i = 0 ; i < fNonPhysBiasByPDGRangeLow.size() ; i++ )
280  if ( ( PDG >= fNonPhysBiasByPDGRangeLow[i] ) && ( PDG <= fNonPhysBiasByPDGRangeHigh[i] ) )
281  {
282  nonPhysBias = true;
283  break;
284  }
285 
286  // -- if particle has not yet any biasing, include it on charge criteria:
287  if ( ( physBias == false ) && ( nonPhysBias == false ) )
288  {
289  if ( std::abs( particle->GetPDGCharge() ) > DBL_MIN )
290  {
291  if ( fPhysBiasAllCharged ) if ( fPhysBiasAllChargedISL || !particle->IsShortLived() ) physBias = true;
292  if ( fNonPhysBiasAllCharged ) if ( fNonPhysBiasAllChargedISL || !particle->IsShortLived() ) nonPhysBias = true;
293  }
294  else
295  {
296  if ( fPhysBiasAllNeutral ) if ( fPhysBiasAllNeutralISL || !particle->IsShortLived() ) physBias = true;
297  if ( fNonPhysBiasAllNeutral ) if ( fNonPhysBiasAllNeutralISL || !particle->IsShortLived() ) nonPhysBias = true;
298  }
299  }
300 
301 
302  if ( nonPhysBias ) G4BiasingHelper::ActivateNonPhysicsBiasing(pmanager);
303 
304  if ( physBias )
305  {
306  std::vector < G4String > biasedProcesses;
307  G4ProcessVector* vprocess = pmanager->GetProcessList();
308  for (G4int ip = 0 ; ip < vprocess->size() ; ip++)
309  {
310  G4VProcess* process = (*vprocess)[ip];
311  biasedProcesses.push_back( process->GetProcessName() );
312  }
313 
314  G4bool restartLoop(true);
315  while ( restartLoop )
316  {
317  for (std::size_t ip = 0 ; ip < biasedProcesses.size() ; ip++)
318  {
319  G4bool activ = G4BiasingHelper::ActivatePhysicsBiasing(pmanager, biasedProcesses[ip] );
320  restartLoop = activ;
321  if ( restartLoop ) break;
322  }
323  }
324  }
325 
326  }
327 
328 
329 
330  if ( fVerbose )
331  {
332  // -- print:
333  particleIterator->reset();
334 
335  while( (*particleIterator)() )
336  {
337  G4ParticleDefinition* particle = particleIterator->value();
338  G4String particleName = particle->GetParticleName();
339  G4ProcessManager* pmanager = particle->GetProcessManager();
340 
341  G4bool isBiased(false);
342  G4String processNames;
343  G4int icount(0);
344 
345  G4ProcessVector* vprocess = pmanager->GetProcessList();
346  for (G4int ip = 0 ; ip < vprocess->size() ; ip++)
347  {
348  G4VProcess* process = (*vprocess)[ip];
349  G4BiasingProcessInterface* pb = dynamic_cast< G4BiasingProcessInterface* >(process);
350  if ( pb != nullptr )
351  {
352  isBiased = true;
353  if ( icount < 3 )
354  {
355  processNames += pb->GetProcessName();
356  processNames += " ";
357  }
358  else
359  {
360  processNames += "\n ";
361  processNames += pb->GetProcessName();
362  processNames += " ";
363  icount = 0;
364  }
365  icount++;
366  }
367  }
368  if ( isBiased )
369  {
370  if ( particle->IsShortLived() )
371  G4cout << std::setw(14) << particleName << " **** : " << processNames << G4endl;
372  else
373  G4cout << std::setw(18) << particleName << " : " << processNames << G4endl;
374  }
375  }
376  }
377 }
378 
G4ProcessVector * GetProcessList() const
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)
G4ProcessManager * GetProcessManager() const
static void ActivateNonPhysicsBiasing(G4ProcessManager *pmanager, G4String nonPhysicsProcessName="")
int G4int
Definition: G4Types.hh:78
std::vector< G4int > fNonPhysBiasByPDGRangeHigh
std::vector< G4int > fPhysBiasByPDGRangeHigh
void NonPhysicsBiasAddPDGRange(G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle=true)
void PhysicsBiasAllCharged(G4bool includeShortLived=false)
const G4String & GetProcessName() const
Definition: G4VProcess.hh:408
const G4String & GetParticleName() const
G4GLOB_DLL std::ostream G4cout
G4GenericBiasingPhysics(const G4String &name="BiasingP")
std::vector< G4int > fNonPhysBiasByPDGRangeLow
bool G4bool
Definition: G4Types.hh:79
G4_DECLARE_PHYSCONSTR_FACTORY(G4GenericBiasingPhysics)
static G4bool ActivatePhysicsBiasing(G4ProcessManager *pmanager, G4String physicsProcessToBias, G4String wrappedName="")
G4int size() const
void PhysicsBiasAddPDGRange(G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle=true)
std::vector< G4int > fPhysBiasByPDGRangeLow
void BiasAddPDGRange(G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle=true)
#define DBL_MIN
Definition: templates.hh:75
void NonPhysicsBiasAllNeutral(G4bool includeShortLived=false)
std::vector< G4InuclElementaryParticle >::iterator particleIterator
Definition: G4BigBanger.cc:65
std::vector< G4String > fBiasedParticles
#define G4endl
Definition: G4ios.hh:61
void BiasAllNeutral(G4bool includeShortLived=false)
std::vector< G4String > fNonPhysBiasedParticles
std::vector< G4bool > fBiasAllProcesses
G4ParticleTable::G4PTblDicIterator * GetParticleIterator() const
G4double GetPDGCharge() const
void NonPhysicsBias(const G4String &particleName)
void Bias(const G4String &particleName)