Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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"
51 
52 
53 // factory
55 //
57 
58 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
59 
61 : G4VPhysicsConstructor(name),
62  fPhysBiasAllCharged(false), fNonPhysBiasAllCharged(false),
63  fPhysBiasAllChargedISL(false), fNonPhysBiasAllChargedISL(false),
64  fPhysBiasAllNeutral(false), fNonPhysBiasAllNeutral(false),
65  fPhysBiasAllNeutralISL(false), fNonPhysBiasAllNeutralISL(false),
66  fVerbose(false)
67 {;}
68 
69 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
70 
72 {;}
73 
74 
75 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
76 
78 {
79  fBiasedParticles.push_back(particleName);
80  std::vector< G4String > dummy;
81  fBiasedProcesses.push_back(dummy);
82  fBiasAllProcesses.push_back(true);
83 }
84 
85 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
86 
87 void G4GenericBiasingPhysics::PhysicsBias(const G4String& particleName, const std::vector< G4String >& processNames)
88 {
89  fBiasedParticles.push_back(particleName);
90  fBiasedProcesses.push_back(processNames);
91  fBiasAllProcesses.push_back(false);
92 }
93 
94 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
95 
97 {
98  fNonPhysBiasedParticles.push_back(particleName);
99 }
100 
101 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
102 
103 void G4GenericBiasingPhysics::Bias(const G4String& particleName)
104 {
105  PhysicsBias(particleName);
106  NonPhysicsBias(particleName);
107 }
108 
109 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
110 
111 void G4GenericBiasingPhysics::Bias(const G4String& particleName, const std::vector< G4String >& processNames)
112 {
113  PhysicsBias(particleName, processNames);
114  NonPhysicsBias(particleName);
115 }
116 
117 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
118 void G4GenericBiasingPhysics::PhysicsBiasAddPDGRange( G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle )
119 {
120  if ( PDGlow > PDGhigh ) G4cout << " G4GenericBiasingPhysics::PhysicsBiasAddPDGRange(...) : PDGlow > PDGhigh, call ignored." << G4endl;
121  fPhysBiasByPDGRangeLow .push_back( PDGlow );
122  fPhysBiasByPDGRangeHigh.push_back( PDGhigh );
123  if ( includeAntiParticle )
124  {
125  fPhysBiasByPDGRangeLow .push_back( -PDGhigh );
126  fPhysBiasByPDGRangeHigh.push_back( -PDGlow );
127  }
128 }
129 
130 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
131 void G4GenericBiasingPhysics::NonPhysicsBiasAddPDGRange( G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle )
132 {
133  if ( PDGlow > PDGhigh ) G4cout << " G4GenericBiasingPhysics::NonPhysicsBiasAddPDGRange(...) : PDGlow > PDGhigh, call ignored." << G4endl;
134  fNonPhysBiasByPDGRangeLow .push_back( PDGlow );
135  fNonPhysBiasByPDGRangeHigh.push_back( PDGhigh );
136  if ( includeAntiParticle )
137  {
138  fNonPhysBiasByPDGRangeLow .push_back( -PDGhigh );
139  fNonPhysBiasByPDGRangeHigh.push_back( -PDGlow );
140  }
141 }
142 
143 
144 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
145 void G4GenericBiasingPhysics::BiasAddPDGRange( G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle )
146 {
147  if ( PDGlow > PDGhigh ) G4cout << " G4GenericBiasingPhysics::BiasAddPDGRange(...) : PDGlow > PDGhigh, call ignored." << G4endl;
148  PhysicsBiasAddPDGRange ( PDGlow, PDGhigh, includeAntiParticle );
149  NonPhysicsBiasAddPDGRange( PDGlow, PDGhigh, includeAntiParticle );
150 }
151 
153 {
154  fPhysBiasAllCharged = true;
155  fPhysBiasAllChargedISL = includeShortLived;
156 }
158 {
159  fNonPhysBiasAllCharged = true;
160  fNonPhysBiasAllChargedISL = includeShortLived;
161 }
163 {
164  fPhysBiasAllCharged = true;
165  fNonPhysBiasAllCharged = true;
166  fPhysBiasAllChargedISL = includeShortLived;
167  fNonPhysBiasAllChargedISL = includeShortLived;
168 }
170 {
171  fPhysBiasAllNeutral = true;
172  fPhysBiasAllNeutralISL = includeShortLived;
173 }
175 {
176  fNonPhysBiasAllNeutral = true;
177  fNonPhysBiasAllNeutralISL = includeShortLived;
178 }
180 {
181  fPhysBiasAllNeutral = true;
182  fNonPhysBiasAllNeutral = true;
183  fPhysBiasAllNeutralISL = includeShortLived;
184  fNonPhysBiasAllNeutralISL = includeShortLived;
185 }
186 
187 
188 void G4GenericBiasingPhysics::AddParallelGeometry( const G4String& particleName, const G4String& parallelGeometryName )
189 {
190  // -- add particle, caring of possible duplication:
191  G4bool isKnown = false;
192  for ( G4String knownParticle : fParticlesWithParallelGeometries )
193  {
194  if ( knownParticle == particleName )
195  {
196  isKnown = true;
197  break;
198  }
199  }
200 
201  // -- add the geometry, caring for possible duplication of this geometry, for this particle:
202  if ( !isKnown ) fParticlesWithParallelGeometries.push_back( particleName );
203  std::vector< G4String >& geometries = fParallelGeometriesForParticle[particleName];
204 
205  isKnown = false;
206  for ( G4String knownGeometry : geometries )
207  {
208  if ( knownGeometry == parallelGeometryName )
209  {
210  isKnown = true;
211  break;
212  }
213  }
214  if ( !isKnown ) geometries.push_back( parallelGeometryName );
215 
216 }
217 
218 void G4GenericBiasingPhysics::AddParallelGeometry( const G4String& particleName, const std::vector< G4String >& parallelGeometryNames )
219 {
220  for ( G4String geometry : parallelGeometryNames ) AddParallelGeometry( particleName, geometry );
221 }
222 
223 void G4GenericBiasingPhysics::AddParallelGeometry( G4int PDGlow, G4int PDGhigh, const G4String& parallelGeometryName , G4bool includeAntiParticle )
224 {
225  if ( PDGlow > PDGhigh )
226  {
227  G4cout << "G4GenericBiasingPhysics::AddParallelGeometry( G4int PDGlow, G4int PDGhigh, const G4String& parallelGeometryName , G4bool includeAntiParticle = true ), PDGlow > PDGhigh : call ignored" << G4endl;
228  return;
229  }
230 
231  fPDGlowParallelGeometries .push_back( PDGlow );
232  fPDGhighParallelGeometries.push_back( PDGhigh );
233  G4int rangeIndex = fPDGlowParallelGeometries.size() - 1;
234  fPDGrangeParallelGeometries[rangeIndex].push_back( parallelGeometryName );
235 
236  if ( includeAntiParticle )
237  {
238  fPDGlowParallelGeometries .push_back( -PDGhigh );
239  fPDGhighParallelGeometries.push_back( -PDGlow );
240  rangeIndex = fPDGlowParallelGeometries.size() - 1;
241  fPDGrangeParallelGeometries[rangeIndex].push_back( parallelGeometryName );
242  }
243 
244 }
245 
246 void G4GenericBiasingPhysics::AddParallelGeometry( G4int PDGlow, G4int PDGhigh, const std::vector< G4String >& parallelGeometryNames, G4bool includeAntiParticle )
247 {
248  if ( PDGlow > PDGhigh )
249  {
250  G4cout << "G4GenericBiasingPhysics::AddParallelGeometry( G4int PDGlow, G4int PDGhigh, const std::vector< G4String >& parallelGeometryNames, G4bool includeAntiParticle = true ), PDGlow > PDGhigh : call ignored" << G4endl;
251  return;
252  }
253 
254  for ( G4String geometry : parallelGeometryNames ) AddParallelGeometry( PDGlow, PDGhigh, geometry, includeAntiParticle );
255 }
256 
257 void G4GenericBiasingPhysics::AddParallelGeometryAllCharged( const G4String& parallelGeometryName , G4bool includeShortLived )
258 {
259  G4bool isKnown = false;
260  for ( G4String geometry : fParallelGeometriesForCharged )
261  {
262  if ( geometry == parallelGeometryName )
263  {
264  isKnown = true;
265  break;
266  }
267  }
268  if ( !isKnown )
269  {
270  fParallelGeometriesForCharged .push_back( parallelGeometryName );
271  fAllChargedParallelGeometriesISL.push_back( includeShortLived );
272  }
273 }
274 
275 void G4GenericBiasingPhysics::AddParallelGeometryAllCharged( const std::vector< G4String >& parallelGeometryNames, G4bool includeShortLived )
276 {
277  for ( G4String geometry : parallelGeometryNames ) AddParallelGeometryAllCharged( geometry, includeShortLived );
278 }
279 
280 void G4GenericBiasingPhysics::AddParallelGeometryAllNeutral( const G4String& parallelGeometryName , G4bool includeShortLived )
281 {
282  G4bool isKnown = false;
283  for ( G4String geometry : fParallelGeometriesForNeutral )
284  {
285  if ( geometry == parallelGeometryName )
286  {
287  isKnown = true;
288  break;
289  }
290  }
291  if ( !isKnown )
292  {
293  fParallelGeometriesForNeutral .push_back( parallelGeometryName );
294  fAllNeutralParallelGeometriesISL.push_back( includeShortLived );
295  }
296 }
297 
298 void G4GenericBiasingPhysics::AddParallelGeometryAllNeutral( const std::vector< G4String >& parallelGeometryNames, G4bool includeShortLived )
299 {
300  for ( G4String geometry : parallelGeometryNames ) AddParallelGeometryAllNeutral( geometry, includeShortLived );
301 }
302 
303 
304 
305 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
306 
308 {;}
309 
310 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
311 
313 {
314 
315  // -- bias setup per individual particle name:
317  particleIterator->reset();
318 
319  while( (*particleIterator)() )
320  {
321  G4ParticleDefinition* particle = particleIterator->value();
322  G4String particleName = particle->GetParticleName();
323  G4ProcessManager* pmanager = particle->GetProcessManager();
324 
325  // -- include non physics process interface for biasing:
326  if ( std::find(fNonPhysBiasedParticles.begin(),
327  fNonPhysBiasedParticles.end(),
328  particleName ) != fNonPhysBiasedParticles.end() )
329  {
331  }
332 
333  // -- wrap biased physics processes, all processes or only user selected:
334  std::vector< G4String >::const_iterator particleIt =
335  std::find(fBiasedParticles.begin(),
336  fBiasedParticles.end(),
337  particleName );
338  if ( particleIt == fBiasedParticles.end() ) continue;
339 
340  std::vector < G4String >& biasedProcesses = fBiasedProcesses [ particleIt - fBiasedParticles.begin() ];
341  G4bool biasAll = fBiasAllProcesses[ particleIt - fBiasedParticles.begin() ];
342 
343  if ( biasAll )
344  {
345  G4ProcessVector* vprocess = pmanager->GetProcessList();
346  for (G4int ip = 0 ; ip < vprocess->size() ; ip++)
347  {
348  G4VProcess* process = (*vprocess)[ip];
349  biasedProcesses.push_back( process->GetProcessName() );
350  }
351  }
352 
353  G4bool restartLoop(true);
354  while ( restartLoop )
355  {
356  for (std::size_t ip = 0 ; ip < biasedProcesses.size() ; ip++)
357  {
358  G4bool activ = G4BiasingHelper::ActivatePhysicsBiasing(pmanager, biasedProcesses[ip] );
359  restartLoop = activ;
360  if ( restartLoop ) break;
361  }
362  }
363 
364  }
365 
366 
367  // -- bias setup per group:
368  particleIterator->reset();
369 
370  while( (*particleIterator)() )
371  {
372  G4ParticleDefinition* particle = particleIterator->value();
373  G4String particleName = particle->GetParticleName();
374  G4ProcessManager* pmanager = particle->GetProcessManager();
375 
376  // -- exclude particles invidually specified by name:
377  if ( std::find( fNonPhysBiasedParticles.begin(),
378  fNonPhysBiasedParticles.end(),
379  particleName ) != fNonPhysBiasedParticles.end() ) continue;
380 
381  if ( std::find( fBiasedParticles.begin(),
382  fBiasedParticles.end(),
383  particleName ) != fBiasedParticles.end() ) continue;
384 
385 
386  G4bool physBias(false), nonPhysBias(false);
387 
388  auto PDG = particle->GetPDGEncoding();
389 
390  // -- include particle if in right PDG range:
391  for ( size_t i = 0 ; i < fPhysBiasByPDGRangeLow.size() ; i++ )
392  if ( ( PDG >= fPhysBiasByPDGRangeLow[i] ) && ( PDG <= fPhysBiasByPDGRangeHigh[i] ) )
393  {
394  physBias = true;
395  break;
396  }
397  for ( size_t i = 0 ; i < fNonPhysBiasByPDGRangeLow.size() ; i++ )
398  if ( ( PDG >= fNonPhysBiasByPDGRangeLow[i] ) && ( PDG <= fNonPhysBiasByPDGRangeHigh[i] ) )
399  {
400  nonPhysBias = true;
401  break;
402  }
403 
404  // -- if particle has not yet any biasing, include it on charge criteria:
405  if ( ( physBias == false ) && ( nonPhysBias == false ) )
406  {
407  if ( std::abs( particle->GetPDGCharge() ) > DBL_MIN )
408  {
409  if ( fPhysBiasAllCharged ) if ( fPhysBiasAllChargedISL || !particle->IsShortLived() ) physBias = true;
410  if ( fNonPhysBiasAllCharged ) if ( fNonPhysBiasAllChargedISL || !particle->IsShortLived() ) nonPhysBias = true;
411  }
412  else
413  {
414  if ( fPhysBiasAllNeutral ) if ( fPhysBiasAllNeutralISL || !particle->IsShortLived() ) physBias = true;
415  if ( fNonPhysBiasAllNeutral ) if ( fNonPhysBiasAllNeutralISL || !particle->IsShortLived() ) nonPhysBias = true;
416  }
417  }
418 
419 
420  if ( nonPhysBias ) G4BiasingHelper::ActivateNonPhysicsBiasing(pmanager);
421 
422  if ( physBias )
423  {
424  std::vector < G4String > biasedProcesses;
425  G4ProcessVector* vprocess = pmanager->GetProcessList();
426  for (G4int ip = 0 ; ip < vprocess->size() ; ip++)
427  {
428  G4VProcess* process = (*vprocess)[ip];
429  biasedProcesses.push_back( process->GetProcessName() );
430  }
431 
432  G4bool restartLoop(true);
433  while ( restartLoop )
434  {
435  for (std::size_t ip = 0 ; ip < biasedProcesses.size() ; ip++)
436  {
437  G4bool activ = G4BiasingHelper::ActivatePhysicsBiasing(pmanager, biasedProcesses[ip] );
438  restartLoop = activ;
439  if ( restartLoop ) break;
440  }
441  }
442  }
443 
444  }
445 
446 
447 
448  // -- Associate parallel geometries:
449  AssociateParallelGeometries();
450 
451 
452  // -- tells what is done:
453  if ( fVerbose )
454  {
455  // -- print:
456  particleIterator->reset();
457 
458  while( (*particleIterator)() )
459  {
460  G4ParticleDefinition* particle = particleIterator->value();
461  G4String particleName = particle->GetParticleName();
462  G4ProcessManager* pmanager = particle->GetProcessManager();
463 
464  G4bool isBiased(false);
465  G4String processNames;
466  G4int icount(0);
467 
468  G4ProcessVector* vprocess = pmanager->GetProcessList();
469  for (G4int ip = 0 ; ip < vprocess->size() ; ip++)
470  {
471  G4VProcess* process = (*vprocess)[ip];
472  G4BiasingProcessInterface* pb = dynamic_cast< G4BiasingProcessInterface* >(process);
473  if ( pb != nullptr )
474  {
475  isBiased = true;
476  if ( icount < 3 )
477  {
478  processNames += pb->GetProcessName();
479  processNames += " ";
480  }
481  else
482  {
483  processNames += "\n ";
484  processNames += pb->GetProcessName();
485  processNames += " ";
486  icount = 0;
487  }
488  icount++;
489  }
490  }
491  if ( isBiased )
492  {
493  if ( particle->IsShortLived() )
494  G4cout << std::setw(14) << particleName << " **** : " << processNames << G4endl;
495  else
496  G4cout << std::setw(18) << particleName << " : " << processNames << G4endl;
497  }
498  }
499  }
500 }
501 
502 
503 
504 void G4GenericBiasingPhysics::AssociateParallelGeometries()
505 {
506 
507  // -- parallel geometries for individual particles:
509  particleIterator->reset();
510 
511  while( (*particleIterator)() )
512  {
513  G4ParticleDefinition* particle = particleIterator->value();
514  G4String particleName = particle->GetParticleName();
515  G4ProcessManager* pmanager = particle->GetProcessManager();
516 
517  G4bool requested = false;
518  for ( G4String requestedParticles : fParticlesWithParallelGeometries )
519  {
520  if ( requestedParticles == particleName )
521  {
522  requested = true;
523  break;
524  }
525  }
526  if ( requested )
527  {
528  // -- insert biasing process for handling parallel geometries:
530 
531  // -- attach the requested worlds to this process:
532  std::vector< G4String >& parallelWorlds = fParallelGeometriesForParticle[ particleName ];
533  for ( G4String world : parallelWorlds ) limiter->AddParallelWorld( world );
534  }
535 
536  }
537 
538 
539  // -- parallel geometries for particles in PDG ranges:
540  G4int i = 0; // -- index for PDG range
541  for ( G4int PDGlow : fPDGlowParallelGeometries )
542  {
543  G4int PDGhigh = fPDGhighParallelGeometries[i];
544  auto & geometries = fPDGrangeParallelGeometries[i];
545 
546  particleIterator->reset();
547 
548  while( (*particleIterator)() )
549  {
550  G4ParticleDefinition* particle = particleIterator->value();
551  G4int particlePDG = particle->GetPDGEncoding();
552  G4ProcessManager* pmanager = particle->GetProcessManager();
553 
554  if ( ( particlePDG >= PDGlow ) && ( particlePDG <= PDGhigh ) )
555  {
556  // -- ยงยง exclude particles from individual list ?
557  // -- insert biasing process for handling parallel geometries:
559 
560  // -- attached the requested worlds to this process:
561  for ( auto geometry : geometries ) limiter->AddParallelWorld( geometry );
562  }
563  }
564  // -- increment index for next PDG range:
565  i++;
566  }
567 
568 
569  // -- parallel geometries for all charged particles:
570 
571 
572 
573 
574 }
const XML_Char * name
Definition: expat.h:151
void BiasAllCharged(G4bool includeShortLived=false)
void PhysicsBiasAllNeutral(G4bool includeShortLived=false)
void NonPhysicsBiasAllCharged(G4bool includeShortLived=false)
void PhysicsBias(const G4String &particleName)
void AddParallelGeometry(const G4String &particleName, const G4String &parallelGeometryName)
static void ActivateNonPhysicsBiasing(G4ProcessManager *pmanager, G4String nonPhysicsProcessName="")
int G4int
Definition: G4Types.hh:78
const G4String & GetParticleName() const
void AddParallelWorld(const G4String &parallelWorldName)
void NonPhysicsBiasAddPDGRange(G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle=true)
void PhysicsBiasAllCharged(G4bool includeShortLived=false)
G4ParticleTable::G4PTblDicIterator * GetParticleIterator() const
G4GLOB_DLL std::ostream G4cout
G4GenericBiasingPhysics(const G4String &name="BiasingP")
bool G4bool
Definition: G4Types.hh:79
static G4bool ActivatePhysicsBiasing(G4ProcessManager *pmanager, G4String physicsProcessToBias, G4String wrappedName="")
static G4ParallelGeometriesLimiterProcess * AddLimiterProcess(G4ProcessManager *pmanager, const G4String &processName="biasLimiter")
const G4String & GetProcessName() const
Definition: G4VProcess.hh:408
void PhysicsBiasAddPDGRange(G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle=true)
void AddParallelGeometryAllCharged(const G4String &parallelGeometryName, G4bool includeShortLived=false)
G4int size() const
void BiasAddPDGRange(G4int PDGlow, G4int PDGhigh, G4bool includeAntiParticle=true)
G4ProcessManager * GetProcessManager() const
#define DBL_MIN
Definition: templates.hh:75
void NonPhysicsBiasAllNeutral(G4bool includeShortLived=false)
std::vector< G4InuclElementaryParticle >::iterator particleIterator
Definition: G4BigBanger.cc:65
#define G4endl
Definition: G4ios.hh:61
void BiasAllNeutral(G4bool includeShortLived=false)
G4double GetPDGCharge() const
void NonPhysicsBias(const G4String &particleName)
G4ProcessVector * GetProcessList() const
void AddParallelGeometryAllNeutral(const G4String &parallelGeometryName, G4bool includeShortLived=false)
#define G4_DECLARE_PHYSCONSTR_FACTORY(physics_constructor)
void Bias(const G4String &particleName)