LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
extensibleFactory.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 //
28 //
29 //
30 //
31 // -------------------------------------------------------------
32 // extensibleFactory
33 //
34 // Application demonstrating the extensible physics list factory
35 //
36 // Author of hadronic/Hadr00/Hadr00.cc
37 // V.Ivanchenko, 20 June 2008 (as hadronic/Hadr00/Hadr00.cc)
38 // Author of examples/extended/physicslists/factory/factory.cc
39 // I. Hrivnacova, 2017-09-26
40 
41 // Modified from factory.cc
42 // R.Hatcher 2017-10-31
43 // copied from examples/extended/physicslists/factory
44 // modified to use alternative extensible physics list factory
45 //
46 // -------------------------------------------------------------
47 //
48 //
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
51 
52 #include "DetectorConstruction.hh"
53 #include "ActionInitialization.hh"
55 
56 #ifdef G4MULTITHREADED
57 #include "G4MTRunManager.hh"
58 #else
59 #include "G4RunManager.hh"
60 #endif
61 
63 // The following change is the _only_ required changed to move from
64 // the non-extensible factory to the exensible factory. All other changes
65 // relative to the "factory" example are there to demonstrate new features.
67 //non-extensible: #include "G4PhysListFactory.hh"
68 #include "G4PhysListFactoryAlt.hh"
69 //use this for drop-in replacement: using namespace g4alt;
70 
72 // headers needed to demonstrate new featues
74 
75 // allow ourselves to extend the short names for physics ctor addition/replace
76 // along the same lines as EMX, EMY, etc
77 #include "G4PhysListRegistry.hh"
78 
79 // allow ourselves to give the user extra info about available physics ctors
80 #include "G4PhysicsConstructorFactory.hh"
81 
82 // pull in a user defined physics list definition into the main program
83 // and register it with the factory (doesn't have to be the main program
84 // but the .o containing the declaration _must_ get linked/loaded)
85 #include "G4PhysListStamper.hh" // defines macro for factory registration
86 #include "MySpecialPhysList.hh"
87 G4_DECLARE_PHYSLIST_FACTORY(MySpecialPhysList);
88 
90 
91 #include "G4VModularPhysicsList.hh"
92 #include "G4UImanager.hh"
93 #include "Randomize.hh"
94 #include "G4VisExecutive.hh"
95 #include "G4UIExecutive.hh"
96 
97 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
98 
99 namespace {
100 
101  void PrintAvailable(G4int verbosity) {
102  G4cout << G4endl;
103  G4cout << "extensibleFactory: here are the available physics lists:"
104  << G4endl;
105  g4alt::G4PhysListFactory factory;
106  factory.PrintAvailablePhysLists();
107 
108  // if user asked for extra verbosity then print physics ctors as well
109  if ( verbosity > 1 ) {
110  G4cout << G4endl;
111  G4cout << "extensibleFactory: "
112  << "here are the available physics ctors that can be added:"
113  << G4endl;
114  G4PhysicsConstructorRegistry* g4pctorFactory =
115  G4PhysicsConstructorRegistry::Instance();
116  g4pctorFactory->PrintAvailablePhysicsConstructors();
117  }
118  }
119 
120  void PrintUsage(G4int verbosity) {
121  G4cerr << " Usage: " << G4endl;
122  G4cerr << " extensibleFactory [-m macro ] [-p physList ]"
123  << " [-u UIsession] [-t nThreads]" << G4endl
124  << " [-v | --verbose] [-h | --help]" << G4endl;
125  G4cerr << " note: -t option is available only for multi-threaded mode."
126  << G4endl;
127  G4cerr << " note: -v can be repeated to increase verbosity." << G4endl;
128  G4cerr << G4endl;
129 
130  if (verbosity>0) PrintAvailable(verbosity);
131  }
132 
133 }
134 
135 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
136 
137 int main(int argc,char** argv)
138 {
139  // Evaluate arguments
140  //
141  if ( argc > 13 ) {
142  PrintUsage(0);
143  return 1;
144  }
145 
146  G4String macro;
148  G4String physListName;
149  char* physListNameEnv = 0;
150  G4String gdmlFileName;
151 #ifdef G4MULTITHREADED
152  G4int nofThreads = 0;
153 #endif
154  G4int verbosity = 0;
155 
156  for ( G4int i=1; i<argc; i=i+2 ) {
157  G4String g4argv(argv[i]); // convert only once
158  if ( g4argv == "-m" ) macro = argv[i+1];
159  else if ( g4argv == "-u" ) session = argv[i+1];
160  else if ( g4argv == "-p" ) physListName = argv[i+1];
161 #ifdef G4MULTITHREADED
162  else if ( g4argv == "-t" ) {
163  nofThreads = G4UIcommand::ConvertToInt(argv[i+1]);
164  }
165 #endif
166  else if ( g4argv == "-v" || g4argv == "--verbose" ) {
167  ++verbosity; // verbose flag doesn't take an argument
168  --i ; // don't increment argc by two, just the one
169  }
170  else if ( g4argv == "-h" || g4argv == "--help" ) {
171  PrintUsage(verbosity+1);
172  return 1;
173  }
174  else {
175  PrintUsage(0);
176  return 1;
177  }
178  }
179 
180  // Detect interactive mode (if no arguments) and define UI session
181  //
182  G4UIExecutive* ui = 0;
183  if ( ! macro.size() ) {
184  ui = new G4UIExecutive(argc, argv, session);
185  }
186 
187  // Choose the Random engine //choose the Random engine
188  G4Random::setTheEngine(new CLHEP::RanecuEngine());
189 
190  // Construct the run manager
191 #ifdef G4MULTITHREADED
192  G4MTRunManager * runManager = new G4MTRunManager();
193  if ( nofThreads > 0 ) {
194  runManager->SetNumberOfThreads(nofThreads);
195  }
196 #else
197  G4RunManager * runManager = new G4RunManager();
198 #endif
199 
200  // g4alt::G4PhysListFactoryAlt is the extensible factory
201  // including the G4PhysListFactoryAlt.hh header and the line:
202  // using namespace g4alt;
203  // would make this a drop-in replacement, but we'll list the explicit
204  // namespace here just for clarity
205  g4alt::G4PhysListFactory factory;
206  G4VModularPhysicsList* physList = nullptr;
207 
208  // Show how an alternative default list could be set
209  // but set the default to the normal default FTFP_BERT.
210  // This is what is used when no -p flag is given and $PHYSLIST
211  // is not defined in the environment.
212  G4String defaultPhysListName = "FTFP_BERT";
213  if ( verbosity > 0 ) {
214  G4cout << "extensibleFactory: SetDefaultReferencePhysList to '"
215  << defaultPhysListName << "' ('' = system default)"
216  << G4endl << G4endl;
217  }
218  factory.SetDefaultReferencePhysList(defaultPhysListName);
219 
220  // set a short name for G4RadioactiveDecayPhysics
221  G4PhysListRegistry* plreg = G4PhysListRegistry::Instance();
222  plreg->AddPhysicsExtension("RADIO","G4RadioactiveDecayPhysics");
223  plreg->AddPhysicsExtension("MYPHYSICS","MyG4PhysicsPhysics");
224  if ( verbosity > 0 ) {
225  G4cout << "extensibleFactory: adding extensions" << G4endl
226  << " RADIO ===> G4RadioactiveDecayPhysics" << G4endl
227  << " MYPHYSICS ===> MyG4PhysicsPhysics" << G4endl
228  << G4endl;
229  }
230 
231  // Get Reference PhysicsList via its name, or if none given
232  // from environment varialb e$PHYSLIST, with fall back to a default
233  if ( physListName.size() ) {
234  if ( verbosity > 0 ) {
235  G4cout << "extensibleFactory: explicitly using '"
236  << physListName << "'" << G4endl;
237  }
238  physList = factory.GetReferencePhysList(physListName);
239  } else {
240  if ( verbosity > 0 ) {
241  G4cout << "extensibleFactory: no -p flag;"
242  << " using ReferencePhysList() ($PHYSLIST or default)" << G4endl;
243  }
244  physList = factory.ReferencePhysList();
245 
246  if ( ! physList ) {
247  // failed? get what the user set, but we couldn't find
248  physListNameEnv = std::getenv("PHYSLIST");
249  if ( physListNameEnv ) {
250  G4cout << "extensibleFactory: $PHYSLIST="
251  << physListNameEnv << G4endl;
252  }
253  }
254 
255  }
256 
257  // deal with failure to get what the user wanted
258  // print what they _could_ use
259  if ( ! physList ) {
260  G4cerr << "extensibleFactory: PhysicsList '"
261  << ( physListNameEnv ? physListNameEnv : physListName )
262  << "' was not available in g4alt::PhysListFactory." << G4endl;
263  PrintAvailable(verbosity);
264 
265  // if we can't get what the user asked for...
266  // don't go on to use something else, that's confusing
267  G4ExceptionDescription ED;
268  ED << "The factory for the physicslist ["
269  << ( physListNameEnv ? physListNameEnv : physListName )
270  << "] does not exist!"
271  << G4endl;
272  G4Exception("extensibleFactory",
273  "extensibleFactory001", FatalException, ED);
274  exit(42);
275  }
276 
277  // Set mandatory initialization classes
278  runManager->SetUserInitialization(new DetectorConstruction());
279  runManager->SetUserInitialization(physList);
280 
281  // set user action classes
282  ActionInitialization* actinit =
283  new ActionInitialization("extensibleFactory");
284  runManager->SetUserInitialization(actinit);
285 
286 // Initialize visualization
287  G4VisManager* visManager = new G4VisExecutive;
288  // G4VisExecutive can take a verbosity argument - see /vis/verbose guidance.
289  // G4VisManager* visManager = new G4VisExecutive("Quiet");
290  visManager->Initialize();
291 
292  // Get the pointer to the User Interface manager
293  G4UImanager* UImanager = G4UImanager::GetUIpointer();
294 
295  if ( macro.size() ) {
296  // batch mode
297  G4String command = "/control/execute ";
298  UImanager->ApplyCommand(command+macro);
299  }
300  else {
301  // interactive mode : define UI session
302  UImanager->ApplyCommand("/control/execute init_vis.mac");
303  ui->SessionStart();
304  delete ui;
305  }
306 
307  // Job termination
308  // Free the store: user actions, physics_list and detector_description are
309  // owned and deleted by the run manager, so they should not be deleted
310  // in the main() program !
311 
312  delete visManager;
313  delete runManager;
314 }
315 
316 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
static G4UIterminal * session
int main(int argc, char **argv)
void PrintUsage()
G4_DECLARE_PHYSLIST_FACTORY(MySpecialPhysList)