LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
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 // Define static variable which holds the one-and-only instance
47 
49 {
50  defName = "QGSP_BERT"; // default list to use if no PHYSLIST defined
51  fgTheInstance = this; // record created self in static pointer
52 }
53 
55 {
56  fgTheInstance = 0;
57 }
58 
60 {
61  // Cleaner dtor calls G4PhysListFactorySingleton dtor at job end
62  static Cleaner cleaner;
63 
64  if ( ! fgTheInstance ) {
65  // need to create one
66  cleaner.UseMe(); // dummy call to quiet compiler warnings
68 
69  // special pre-register some physics overrides (common EM overrides)
70  fgTheInstance->RegisterPhysicsReplacement("_EMV","G4EmStandardPhysics_option1");
71  fgTheInstance->RegisterPhysicsReplacement("_EMX","G4EmStandardPhysics_option2");
72  fgTheInstance->RegisterPhysicsReplacement("_EMY","G4EmStandardPhysics_option3");
73  fgTheInstance->RegisterPhysicsReplacement("_LIV","G4EmLivermorePhysics");
74  fgTheInstance->RegisterPhysicsReplacement("_PEN","G4EmPenelopePhysics");
75 
76 
77  }
78 
79  return *fgTheInstance;
80 }
81 
83 {
84  // instantiate PhysList by environment variable "PHYSLIST"
85  G4String name = "";
86  char* path = getenv("PHYSLIST");
87  if (path) {
88  name = G4String(path);
89  } else {
90  name = defName;
91  G4cout << "### G4PhysListFactorySingleton WARNING: "
92  << " environment variable PHYSLIST is not defined"
93  << G4endl
94  << " Default Physics Lists " << name
95  << " is instantiated"
96  << G4endl;
97  }
98  return GetReferencePhysList(name);
99 }
100 
101 G4VModularPhysicsList*
103 {
104  G4VModularPhysicsList* p = 0;
105 
106  // Replacing the very fixed list of if/else with a std::map lookup
107  /*
108  if(name == "CHIPS") {p = new CHIPS();}
109  else if(name == "FTFP_BERT") {p = new FTFP_BERT();}
110  else if(name == "FTFP_BERT_EMV") {p = new FTFP_BERT_EMV();}
111  else if(name == "FTFP_BERT_EMX") {p = new FTFP_BERT_EMX();}
112  else if(name == "FTFP_BERT_TRV") {p = new FTFP_BERT_TRV();}
113  else if(name == "FTF_BIC") {p = new FTF_BIC();}
114  else if(name == "LBE") {p = new LBE();}
115  else if(name == "LHEP") {p = new LHEP();}
116  else if(name == "LHEP_EMV") {p = new LHEP_EMV();}
117  else if(name == "QBBC") {p = new QBBC();}
118  else if(name == "QGSC_BERT") {p = new QGSC_BERT();}
119  else if(name == "QGSP") {p = new QGSP();}
120  else if(name == "QGSP_BERT") {p = new QGSP_BERT();}
121  else if(name == "QGSP_BERT_EMV") {p = new QGSP_BERT_EMV();}
122  else if(name == "QGSP_BERT_EMX") {p = new QGSP_BERT_EMX();}
123  else if(name == "QGSP_BERT_HP") {p = new QGSP_BERT_HP();}
124  else if(name == "QGSP_BERT_NOLEP") {p = new QGSP_BERT_NOLEP();}
125  else if(name == "QGSP_BERT_TRV") {p = new QGSP_BERT_TRV();}
126  else if(name == "QGSP_BERT_CHIPS") {p = new QGSP_BERT_CHIPS();}
127  else if(name == "QGSP_BIC") {p = new QGSP_BIC();}
128  else if(name == "QGSP_BIC_EMY") {p = new QGSP_BIC_EMY();}
129  else if(name == "QGSP_BIC_HP") {p = new QGSP_BIC_HP();}
130  else if(name == "QGSP_FTFP_BERT"){p = new QGSP_FTFP_BERT();}
131  else if(name == "QGS_BIC") {p = new QGS_BIC();}
132  else if(name == "QGSP_INCL_ABLA"){p = new QGSP_INCL_ABLA();}
133  else if(name == "Shielding") {p = new Shielding();}
134  */
135 
136  // look for key bits that indicate physics to be replaced in a
137  // base physics list; remove those keys from the physics list name
138  std::vector<G4String> physicsReplacements;
139  G4bool allKnown;
140  G4String nameNoReplace = GetBaseName(name,physicsReplacements,allKnown);
141 
142  // we don't want map creating an entry if it doesn't exist
143  // so use map::find() not map::operator[]
145  = fFunctionMap.find(nameNoReplace);
146  if ( fFunctionMap.end() != itr ) {
147  // found an appropriate entry in the list
148  PhysListCtorFuncPtr_t foo = itr->second; // this is the function
149  p = (*foo)(); // use function to create the physics list
150  }
151  if ( ! p ) {
152  G4cout << "### G4PhysListFactorySingleton WARNING: "
153  << "PhysicsList " << nameNoReplace
154  << "(originally=\"" << name << "\")"
155  << " is not known"
156  << G4endl;
157  } else if ( physicsReplacements.size() != 0 ) {
158  //
159  // if there is physics to be replaced, do so now
160  //
161  G4PhysicsProcessFactorySingleton& procFactory =
163  for (size_t k=0; k<physicsReplacements.size(); ++k) {
164  G4String procName = physicsReplacements[k];
165  if ( ! procFactory.IsKnownPhysicsProcess(procName) ) {
166  G4cout << "### G4PhysListFactorySingleton WARNING: "
167  << "G4PhysicsProcesFactorySingleton had no process \""
168  << procName << "\" registered" << G4endl;
169  } else {
170  G4cout << "### G4PhysListFactorySingleton: ReplacePhysics("
171  << procName << ")" << G4endl;
172  G4VPhysicsConstructor* pctor = procFactory.GetPhysicsProcess(procName);
173  p->ReplacePhysics(pctor);
174  }
175  }
176  }
177 
178  return p;
179 }
180 
182 {
183  // check if we know the name (after stripping off replacement keys)
184  std::vector<G4String> physicsReplacements;
185  G4bool allKnown;
186  G4String nameNoReplace = GetBaseName(name,physicsReplacements,allKnown);
187 
188  return allKnown;
189 }
190 
191 const std::vector<G4String>&
193 {
194  // list of names might be out of date due to new registrations
195  // rescan the std::map on each call (which won't be frequent)
196  listnames.clear();
197 
198  // scan map for registered names
200  for ( itr = fFunctionMap.begin(); itr != fFunctionMap.end(); ++itr )
201  listnames.push_back(itr->first);
202 
203  return listnames;
204 }
205 
207 {
208  const std::vector<G4String>& list = AvailablePhysLists();
209  G4cout << "G4VModularPhysicsLists in "
210  << "G4PhysicsProcessFactorySingleton are: "
211  << G4endl;
212  if ( list.empty() ) G4cout << " ... no registered lists" << G4endl;
213  else {
214  for (size_t indx=0; indx < list.size(); ++indx ) {
215  G4cout << " [" << std::setw(2) << indx << "] "
216  << "\"" << list[indx] << "\"" << G4endl;
217  }
218  }
219  G4cout << "G4PhysicsProcessFactorySingleton supports variants of the above"
220  << G4endl << "with physics process replacements:" << G4endl;
221  if ( fPhysicsReplaceList.empty() ) {
222  G4cout << " ... no registered replacements" << G4endl;
223  } else {
224  G4bool printPhysicsProcesses = false;
225  G4PhysicsProcessFactorySingleton& procFactory =
228  fPhysicsReplaceList.begin();
229  for ( ; repitr != fPhysicsReplaceList.end(); ++repitr ) {
230  G4String key = repitr->first;
231  G4String procName = repitr->second;
232  G4bool known = procFactory.IsKnownPhysicsProcess(procName);
233  if ( ! known ) printPhysicsProcesses = true;
234  G4cout << " " << std::setw(10) << key << " ==> "
235  << std::setw(30) << procName << " "
236  << ( (known)?"known":"*** unknown ***" ) << G4endl;
237  }
238  if ( printPhysicsProcesses ) procFactory.PrintAvailablePhysicsProcesses();
239  }
240 }
241 
244  G4bool* boolptr)
245 {
246  // record new functions for creating lists
247  fFunctionMap[name] = foo;
248  fBoolPtrMap[name] = boolptr;
249  return true;
250 }
251 
253  G4String physics)
254 {
255  fPhysicsReplaceList[key] = physics;
256  return true;
257 }
258 
259 G4String
261  std::vector<G4String>& physicsReplace,
262  G4bool& allKnown)
263 {
264  allKnown = true;
265  G4String nameNoReplace = name;
266  physicsReplace.clear();
267  G4PhysicsProcessFactorySingleton& procFactory =
269 
271  for ( ; repitr != fPhysicsReplaceList.end(); ++repitr ) {
272  G4String key = repitr->first;
273  size_t i = nameNoReplace.find(key);
274  if ( i != std::string::npos ) {
275  // remove key from base physics list name
276  nameNoReplace.erase(i,key.size());
277 
278  // add to list of things needing replacement
279  G4String procName = repitr->second;
280  physicsReplace.push_back(procName);
281  if ( ! procFactory.IsKnownPhysicsProcess(procName) ) {
282  G4cout << "G4PhysListFactorySingleton::GetBaseName "
283  << "\"" << key << "\" ==> \"" << procName << "\" not found"
284  << G4endl;
285  //procFactory.PrintAvailablePhysicsProcesses();
286  allKnown = false;
287  }
288  }
289  }
290 
292  = fFunctionMap.find(nameNoReplace);
293  if ( fFunctionMap.end() == itr ) allKnown = false;
294 
295  return nameNoReplace;
296 }
297 
static G4PhysListFactorySingleton * fgTheInstance
intermediate_table::iterator iterator
std::map< G4String, PhysListCtorFuncPtr_t > fFunctionMap
std::map< G4String, G4String > fPhysicsReplaceList
G4String GetBaseName(G4String name, std::vector< G4String > &physicsReplacements, G4bool &allKnown)
const std::vector< G4String > & AvailablePhysLists() const
G4VModularPhysicsList *(* PhysListCtorFuncPtr_t)()
intermediate_table::const_iterator const_iterator
static G4PhysicsProcessFactorySingleton & Instance()
G4bool RegisterPhysicsReplacement(G4String key, G4String physics)
G4bool IsReferencePhysList(const G4String &)
G4bool RegisterCreator(G4String name, PhysListCtorFuncPtr_t ctorptr, G4bool *ptr)
G4VPhysicsConstructor * GetPhysicsProcess(const G4String &)
static G4PhysListFactorySingleton & Instance()
G4VModularPhysicsList * ReferencePhysList()
G4VModularPhysicsList * GetReferencePhysList(const G4String &)
std::map< G4String, G4bool * > fBoolPtrMap