G4PhysListFactorySingleton.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: G4PhysListFactorySingleton.cc,v 1.4 2012-09-20 21:43:53 greenc Exp $
27 // GEANT4 tag $Name: not supported by cvs2svn $
28 //
29 //---------------------------------------------------------------------------
30 //
31 // ClassName: G4PhysListFactorySingleton
32 //
33 // Author: 2011-10-06 R. Hatcher
34 //
35 // Modified:
36 //
37 //----------------------------------------------------------------------------
38 //
39 
41 
42 #include <iomanip>
44 
45 namespace g4nu {
46 
47 // Define static variable which holds the one-and-only instance
49 
51 {
52  defName = "QGSP_BERT"; // default list to use if no PHYSLIST defined
53  fgTheInstance = this; // record created self in static pointer
54 }
55 
57 {
58  fgTheInstance = 0;
59 }
60 
62 {
63  // Cleaner dtor calls G4PhysListFactorySingleton dtor at job end
64  static Cleaner cleaner;
65 
66  if ( ! fgTheInstance ) {
67  // need to create one
68  cleaner.UseMe(); // dummy call to quiet compiler warnings
70 
71  // special pre-register some physics overrides (common EM overrides)
72  fgTheInstance->RegisterPhysicsReplacement("_EMV","G4EmStandardPhysics_option1");
73  fgTheInstance->RegisterPhysicsReplacement("_EMX","G4EmStandardPhysics_option2");
74  fgTheInstance->RegisterPhysicsReplacement("_EMY","G4EmStandardPhysics_option3");
75  fgTheInstance->RegisterPhysicsReplacement("_LIV","G4EmLivermorePhysics");
76  fgTheInstance->RegisterPhysicsReplacement("_PEN","G4EmPenelopePhysics");
77 
78 
79  }
80 
81  return *fgTheInstance;
82 }
83 
85 {
86  // instantiate PhysList by environment variable "PHYSLIST"
87  G4String name = "";
88  char* path = getenv("PHYSLIST");
89  if (path) {
90  name = G4String(path);
91  } else {
92  name = defName;
93  G4cout << "### G4PhysListFactorySingleton WARNING: "
94  << " environment variable PHYSLIST is not defined"
95  << G4endl
96  << " Default Physics Lists " << name
97  << " is instantiated"
98  << G4endl;
99  }
100  return GetReferencePhysList(name);
101 }
102 
103 G4VModularPhysicsList*
105 {
106  G4VModularPhysicsList* p = 0;
107 
108  // Replacing the very fixed list of if/else with a std::map lookup
109  /*
110  if(name == "CHIPS") {p = new CHIPS();}
111  else if(name == "FTFP_BERT") {p = new FTFP_BERT();}
112  else if(name == "FTFP_BERT_EMV") {p = new FTFP_BERT_EMV();}
113  else if(name == "FTFP_BERT_EMX") {p = new FTFP_BERT_EMX();}
114  else if(name == "FTFP_BERT_TRV") {p = new FTFP_BERT_TRV();}
115  else if(name == "FTF_BIC") {p = new FTF_BIC();}
116  else if(name == "LBE") {p = new LBE();}
117  else if(name == "LHEP") {p = new LHEP();}
118  else if(name == "LHEP_EMV") {p = new LHEP_EMV();}
119  else if(name == "QBBC") {p = new QBBC();}
120  else if(name == "QGSC_BERT") {p = new QGSC_BERT();}
121  else if(name == "QGSP") {p = new QGSP();}
122  else if(name == "QGSP_BERT") {p = new QGSP_BERT();}
123  else if(name == "QGSP_BERT_EMV") {p = new QGSP_BERT_EMV();}
124  else if(name == "QGSP_BERT_EMX") {p = new QGSP_BERT_EMX();}
125  else if(name == "QGSP_BERT_HP") {p = new QGSP_BERT_HP();}
126  else if(name == "QGSP_BERT_NOLEP") {p = new QGSP_BERT_NOLEP();}
127  else if(name == "QGSP_BERT_TRV") {p = new QGSP_BERT_TRV();}
128  else if(name == "QGSP_BERT_CHIPS") {p = new QGSP_BERT_CHIPS();}
129  else if(name == "QGSP_BIC") {p = new QGSP_BIC();}
130  else if(name == "QGSP_BIC_EMY") {p = new QGSP_BIC_EMY();}
131  else if(name == "QGSP_BIC_HP") {p = new QGSP_BIC_HP();}
132  else if(name == "QGSP_FTFP_BERT"){p = new QGSP_FTFP_BERT();}
133  else if(name == "QGS_BIC") {p = new QGS_BIC();}
134  else if(name == "QGSP_INCL_ABLA"){p = new QGSP_INCL_ABLA();}
135  else if(name == "Shielding") {p = new Shielding();}
136  */
137 
138  // look for key bits that indicate physics to be replaced in a
139  // base physics list; remove those keys from the physics list name
140  std::vector<G4String> physicsReplacements;
141  G4bool allKnown;
142  G4String nameNoReplace = GetBaseName(name,physicsReplacements,allKnown);
143 
144  // we don't want map creating an entry if it doesn't exist
145  // so use map::find() not map::operator[]
147  = fFunctionMap.find(nameNoReplace);
148  if ( fFunctionMap.end() != itr ) {
149  // found an appropriate entry in the list
150  PhysListCtorFuncPtr_t foo = itr->second; // this is the function
151  p = (*foo)(); // use function to create the physics list
152  }
153  if ( ! p ) {
154  G4cout << "### G4PhysListFactorySingleton WARNING: "
155  << "PhysicsList " << nameNoReplace
156  << "(originally=\"" << name << "\")"
157  << " is not known"
158  << G4endl;
159  } else if ( physicsReplacements.size() != 0 ) {
160  //
161  // if there is physics to be replaced, do so now
162  //
163  G4PhysicsProcessFactorySingleton& procFactory =
165  for (size_t k=0; k<physicsReplacements.size(); ++k) {
166  G4String procName = physicsReplacements[k];
167  if ( ! procFactory.IsKnownPhysicsProcess(procName) ) {
168  G4cout << "### G4PhysListFactorySingleton WARNING: "
169  << "G4PhysicsProcesFactorySingleton had no process \""
170  << procName << "\" registered" << G4endl;
171  } else {
172  G4cout << "### G4PhysListFactorySingleton: ReplacePhysics("
173  << procName << ")" << G4endl;
174  G4VPhysicsConstructor* pctor = procFactory.GetPhysicsProcess(procName);
175  p->ReplacePhysics(pctor);
176  }
177  }
178  }
179 
180  return p;
181 }
182 
184 {
185  // check if we know the name (after stripping off replacement keys)
186  std::vector<G4String> physicsReplacements;
187  G4bool allKnown;
188  G4String nameNoReplace = GetBaseName(name,physicsReplacements,allKnown);
189 
190  return allKnown;
191 }
192 
193 const std::vector<G4String>&
195 {
196  // list of names might be out of date due to new registrations
197  // rescan the std::map on each call (which won't be frequent)
198  listnames.clear();
199 
200  // scan map for registered names
202  for ( itr = fFunctionMap.begin(); itr != fFunctionMap.end(); ++itr )
203  listnames.push_back(itr->first);
204 
205  return listnames;
206 }
207 
209 {
210  const std::vector<G4String>& list = AvailablePhysLists();
211  G4cout << "G4VModularPhysicsLists in "
212  << "G4PhysicsProcessFactorySingleton are: "
213  << G4endl;
214  if ( list.empty() ) G4cout << " ... no registered lists" << G4endl;
215  else {
216  for (size_t indx=0; indx < list.size(); ++indx ) {
217  G4cout << " [" << std::setw(2) << indx << "] "
218  << "\"" << list[indx] << "\"" << G4endl;
219  }
220  }
221  G4cout << "G4PhysicsProcessFactorySingleton supports variants of the above"
222  << G4endl << "with physics process replacements:" << G4endl;
223  if ( fPhysicsReplaceList.empty() ) {
224  G4cout << " ... no registered replacements" << G4endl;
225  } else {
226  G4bool printPhysicsProcesses = false;
227  G4PhysicsProcessFactorySingleton& procFactory =
230  fPhysicsReplaceList.begin();
231  for ( ; repitr != fPhysicsReplaceList.end(); ++repitr ) {
232  G4String key = repitr->first;
233  G4String procName = repitr->second;
234  G4bool known = procFactory.IsKnownPhysicsProcess(procName);
235  if ( ! known ) printPhysicsProcesses = true;
236  G4cout << " " << std::setw(10) << key << " ==> "
237  << std::setw(30) << procName << " "
238  << ( (known)?"known":"*** unknown ***" ) << G4endl;
239  }
240  if ( printPhysicsProcesses ) procFactory.PrintAvailablePhysicsProcesses();
241  }
242 }
243 
246  G4bool* boolptr)
247 {
248  // record new functions for creating lists
249  fFunctionMap[name] = foo;
250  fBoolPtrMap[name] = boolptr;
251  return true;
252 }
253 
255  G4String physics)
256 {
257  fPhysicsReplaceList[key] = physics;
258  return true;
259 }
260 
261 G4String
263  std::vector<G4String>& physicsReplace,
264  G4bool& allKnown)
265 {
266  allKnown = true;
267  G4String nameNoReplace = name;
268  physicsReplace.clear();
269  G4PhysicsProcessFactorySingleton& procFactory =
271 
273  for ( ; repitr != fPhysicsReplaceList.end(); ++repitr ) {
274  G4String key = repitr->first;
275  size_t i = nameNoReplace.find(key);
276  if ( i != std::string::npos ) {
277  // remove key from base physics list name
278  nameNoReplace.erase(i,key.size());
279 
280  // add to list of things needing replacement
281  G4String procName = repitr->second;
282  physicsReplace.push_back(procName);
283  if ( ! procFactory.IsKnownPhysicsProcess(procName) ) {
284  G4cout << "G4PhysListFactorySingleton::GetBaseName "
285  << "\"" << key << "\" ==> \"" << procName << "\" not found"
286  << G4endl;
287  //procFactory.PrintAvailablePhysicsProcesses();
288  allKnown = false;
289  }
290  }
291  }
292 
294  = fFunctionMap.find(nameNoReplace);
295  if ( fFunctionMap.end() == itr ) allKnown = false;
296 
297  return nameNoReplace;
298 }
299 
300 } // end-of-namespace g4nu
301 
302 /// !!!!!! register existing classes without disturbing their .cc files (yet)
303 #include "G4NuPhysicsLists/G4PhysListRegisterOld.icc"
304 /// !!!!!! register some existing G4PhysicsConstructor objects w/ G4PhysicsProcessFactorySingleton
305 /// !!!!!! so that one can do the "normal" EM overrides
306 #include "G4NuPhysicsLists/G4PhysicsProcessRegisterOld.icc"
static QCString name
Definition: declinfo.cpp:673
intermediate_table::iterator iterator
static G4PhysListFactorySingleton & Instance()
const std::vector< G4String > & AvailablePhysLists() const
std::map< G4String, PhysListCtorFuncPtr_t > fFunctionMap
G4String GetBaseName(G4String name, std::vector< G4String > &physicsReplacements, G4bool &allKnown)
intermediate_table::const_iterator const_iterator
G4VModularPhysicsList * GetReferencePhysList(const G4String &)
def key(type, name=None)
Definition: graph.py:13
std::string getenv(std::string const &name)
Definition: getenv.cc:15
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
p
Definition: test.py:223
static G4PhysicsProcessFactorySingleton & Instance()
G4VModularPhysicsList *(* PhysListCtorFuncPtr_t)()
G4bool RegisterCreator(G4String name, PhysListCtorFuncPtr_t ctorptr, G4bool *ptr)
std::map< G4String, G4bool * > fBoolPtrMap
static G4PhysListFactorySingleton * fgTheInstance
G4bool RegisterPhysicsReplacement(G4String key, G4String physics)
G4VPhysicsConstructor * GetPhysicsProcess(const G4String &)
std::map< G4String, G4String > fPhysicsReplaceList