Geant4  10.01.p01
ChargeExchangeMC.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 /*
27  * =============================================================================
28  *
29  * Filename: cexmc.cc
30  *
31  * Description: main
32  *
33  * Version: 1.0
34  * Created: 10.10.2009 23:24:39
35  * Revision: none
36  * Compiler: gcc
37  *
38  * Author: Alexey Radkov (),
39  * Company: PNPI
40  *
41  * =============================================================================
42  */
43 
44 #include <set>
45 #ifdef CEXMC_USE_PERSISTENCY
46 #include <boost/algorithm/string.hpp>
47 #include <boost/archive/archive_exception.hpp>
48 #ifdef CEXMC_USE_CUSTOM_FILTER
49 #include <boost/variant/get.hpp>
50 #endif
51 #endif
52 #include <G4UImanager.hh>
53 #include <G4String.hh>
54 #ifdef G4UI_USE
55 #include <G4UIsession.hh>
56 #include <G4UIterminal.hh>
57 #ifdef G4UI_USE_TCSH
58 #include <G4UItcsh.hh>
59 #endif
60 #ifdef G4UI_USE_QT
61 #include <G4UIQt.hh>
62 #endif
63 #endif
64 #ifdef G4VIS_USE
65 #include <G4VisExecutive.hh>
66 #endif
67 #include "CexmcRunManager.hh"
68 #include "CexmcHistoManager.hh"
69 #include "CexmcSetup.hh"
70 #include "CexmcPhysicsList.hh"
71 #include "CexmcPhysicsManager.hh"
73 #include "CexmcTrackingAction.hh"
74 #include "CexmcSteppingAction.hh"
75 #include "CexmcEventAction.hh"
76 #include "CexmcRunAction.hh"
77 #include "CexmcMessenger.hh"
78 #include "CexmcException.hh"
80 #include "CexmcCommon.hh"
81 
82 
83 namespace
84 {
85  const G4String CexmcVisManagerVerboseLevel( "errors" );
86 }
87 
88 
90 {
92  preinitMacro( "" ), initMacro( "" ), rProject( "" ),
93  wProject( "" ), overrideExistingProject( false ),
94  customFilter( "" )
95  {}
96 
106 };
107 
108 
109 void printUsage( void )
110 {
111 #ifdef CEXMC_PROG_NAME
112  const char * progName( CEXMC_PROG_NAME );
113 #else
114  const char * progName( "cexmc" );
115 #endif
116 
117  G4cout << "Usage: " << progName << " [-i] "
118 #ifdef G4UI_USE_QT
119  "[-g] "
120 #endif
121  "[-p preinit_macro] [-m init_macro] "
122 #ifdef CEXMC_USE_PERSISTENCY
123  "[[-y] -w project]" << G4endl <<
124  " [-r project "
125 #ifdef CEXMC_USE_CUSTOM_FILTER
126  "[-f filter_script] "
127 #endif
128  "[-o list]]"
129 #endif
130  << G4endl;
131  G4cout << "or " << progName << " [--help | -h]" << G4endl;
132  G4cout << " -i - run in interactive mode" << G4endl;
133 #ifdef G4UI_USE_QT
134  G4cout << " -g - start graphical interface (Qt), implies "
135  "interactive mode " << G4endl;
136 #endif
137  G4cout << " -p - use specified preinit macro file " << G4endl;
138  G4cout << " -m - use specified init macro file " << G4endl;
139 #ifdef CEXMC_USE_PERSISTENCY
140  G4cout << " -w - save data in specified project files" << G4endl;
141  G4cout << " -r - read data from specified project files" <<
142  G4endl;
143 #ifdef CEXMC_USE_CUSTOM_FILTER
144  G4cout << " -f - use specified custom filter script" << G4endl;
145 #endif
146  G4cout << " -o - comma-separated list of data to output, "
147  "possible values:" << G4endl <<
148  " run, geom, events" << G4endl;
149  G4cout << " -y - force project override" << G4endl;
150 #endif
151  G4cout << " --help | -h - print this message and exit " << G4endl;
152 }
153 
154 
155 G4bool parseArgs( int argc, char ** argv, CexmcCmdLineData & cmdLineData )
156 {
157  if ( argc < 2 )
158  return false;
159 
160  for ( G4int i( 1 ); i < argc; ++i )
161  {
162  do
163  {
164  if ( G4String( argv[ i ] ) == "--help" )
165  {
166  return false;
167  }
168  if ( G4String( argv[ i ] ) == "-h" )
169  {
170  return false;
171  }
172  if ( G4String( argv[ i ], 2 ) == "-i" )
173  {
174  cmdLineData.isInteractive = true;
175  break;
176  }
177 #ifdef G4UI_USE_QT
178  if ( G4String( argv[ i ], 2 ) == "-g" )
179  {
180  cmdLineData.isInteractive = true;
181  cmdLineData.startQtSession = true;
182  break;
183  }
184 #endif
185  if ( G4String( argv[ i ], 2 ) == "-p" )
186  {
187  cmdLineData.preinitMacro = argv[ i ] + 2;
188  if ( cmdLineData.preinitMacro == "" )
189  {
190  if ( ++i >= argc )
192  cmdLineData.preinitMacro = argv[ i ];
193  }
194  break;
195  }
196  if ( G4String( argv[ i ], 2 ) == "-m" )
197  {
198  cmdLineData.initMacro = argv[ i ] + 2;
199  if ( cmdLineData.initMacro == "" )
200  {
201  if ( ++i >= argc )
203  cmdLineData.initMacro = argv[ i ];
204  }
205  break;
206  }
207 #ifdef CEXMC_USE_PERSISTENCY
208  if ( G4String( argv[ i ], 2 ) == "-w" )
209  {
210  cmdLineData.wProject = argv[ i ] + 2;
211  if ( cmdLineData.wProject == "" )
212  {
213  if ( ++i >= argc )
215  cmdLineData.wProject = argv[ i ];
216  }
217  break;
218  }
219  if ( G4String( argv[ i ], 2 ) == "-r" )
220  {
221  cmdLineData.rProject = argv[ i ] + 2;
222  if ( cmdLineData.rProject == "" )
223  {
224  if ( ++i >= argc )
226  cmdLineData.rProject = argv[ i ];
227  }
228  break;
229  }
230  if ( G4String( argv[ i ], 2 ) == "-y" )
231  {
232  cmdLineData.overrideExistingProject = true;
233  break;
234  }
235  if ( G4String( argv[ i ], 2 ) == "-o" )
236  {
237  std::string outputData( argv[ i ] + 2 );
238  if ( outputData == "" )
239  {
240  if ( ++i >= argc )
242  outputData = argv[ i ];
243  }
244  std::set< std::string > tokens;
245  boost::split( tokens, outputData, boost::is_any_of( "," ) );
246  for ( std::set< std::string >::iterator k( tokens.begin() );
247  k != tokens.end(); ++k )
248  {
249  do
250  {
251  if ( *k == "run" )
252  {
253  cmdLineData.outputData.insert( CexmcOutputRun );
254  break;
255  }
256  if ( *k == "geom" )
257  {
258  cmdLineData.outputData.insert(
260  break;
261  }
262  if ( *k == "events" )
263  {
264  cmdLineData.outputData.insert( CexmcOutputEvents );
265  break;
266  }
268  } while ( false );
269  }
270  break;
271  }
272 #ifdef CEXMC_USE_CUSTOM_FILTER
273  if ( G4String( argv[ i ], 2 ) == "-f" )
274  {
275  cmdLineData.customFilter = argv[ i ] + 2;
276  if ( cmdLineData.customFilter == "" )
277  {
278  if ( ++i >= argc )
280  cmdLineData.customFilter = argv[ i ];
281  }
282  break;
283  }
284 #endif
285 #endif
286 
288 
289  } while ( false );
290  }
291 
292  return true;
293 }
294 
295 
296 int main( int argc, char ** argv )
297 {
298 #ifdef G4UI_USE
299  G4UIsession * session( NULL );
300 #endif
301 
302  CexmcCmdLineData cmdLineData;
303 #ifdef CEXMC_USE_PERSISTENCY
304  G4bool outputDataOnly( false );
305 #endif
306 
307  try
308  {
309  if ( ! parseArgs( argc, argv, cmdLineData ) )
310  {
311  printUsage();
312  return 0;
313  }
314 #ifdef CEXMC_USE_PERSISTENCY
315  if ( cmdLineData.rProject != "" &&
316  cmdLineData.rProject == cmdLineData.wProject )
318  if ( cmdLineData.rProject == "" && ! cmdLineData.outputData.empty() )
320 #ifdef CEXMC_USE_CUSTOM_FILTER
321  if ( cmdLineData.rProject == "" && ! cmdLineData.customFilter.empty() )
323 #endif
324  if ( cmdLineData.wProject != "" && ! cmdLineData.outputData.empty() )
326  outputDataOnly = ! cmdLineData.outputData.empty();
327 #endif
328  }
329  catch ( CexmcException & e )
330  {
331  G4cout << e.what() << G4endl;
332  return 1;
333  }
334  catch ( ... )
335  {
336  G4cout << "Unknown exception caught when parsing args" << G4endl;
337  return 1;
338  }
339 
340  CexmcRunManager * runManager( NULL );
342 #ifdef G4VIS_USE
343  G4VisManager * visManager( NULL );
344 #endif
345 #ifdef CEXMC_USE_ROOT
346  CexmcHistoManager::Instance();
347 #endif
348 
349  try
350  {
351  runManager = new CexmcRunManager( cmdLineData.wProject,
352  cmdLineData.rProject,
353  cmdLineData.overrideExistingProject );
354 #ifdef CEXMC_USE_PERSISTENCY
355 #ifdef CEXMC_USE_CUSTOM_FILTER
356  runManager->SetCustomFilter( cmdLineData.customFilter );
357 #endif
358 
359  if ( outputDataOnly )
360  {
361  /* we will need an arbitrary physics list to get access to particle
362  * table if events output was ordered */
363  CexmcOutputDataTypeSet::const_iterator found(
364  cmdLineData.outputData.find( CexmcOutputEvents ) );
365  if ( found != cmdLineData.outputData.end() )
366  runManager->SetUserInitialization(
368  Create( CexmcPionZeroProduction ) );
369  runManager->PrintReadData( cmdLineData.outputData );
370  delete runManager;
371  return 0;
372  }
373 #endif
374 
375  G4UImanager * uiManager( G4UImanager::GetUIpointer() );
376 
377  if ( cmdLineData.preinitMacro != "" )
378  uiManager->ApplyCommand( "/control/execute " +
379  cmdLineData.preinitMacro );
380 
381  CexmcProductionModelType productionModelType(
382  runManager->GetProductionModelType() );
383 
384  if ( productionModelType == CexmcUnknownProductionModel )
386 
388  Create( productionModelType ) );
389  CexmcPhysicsManager * physicsManager(
390  dynamic_cast< CexmcPhysicsManager * >( physicsList ) );
391  CexmcProductionModel * productionModel(
392  physicsManager->GetProductionModel() );
393 
394  if ( ! productionModel )
396 
397  G4cout << CEXMC_LINE_START << "Production model '" <<
398  productionModel->GetName() << "' instantiated" << G4endl;
399 
400  runManager->SetUserInitialization( physicsList );
401 
402  CexmcSetup * setup( new CexmcSetup( runManager->GetGdmlFileName(),
403  runManager->ShouldGdmlFileBeValidated() ) );
404 
405  runManager->SetUserInitialization( setup );
406 
407  runManager->Initialize();
408 
409  runManager->SetPhysicsManager( physicsManager );
410 
412  physicsManager ) );
413 
414  runManager->SetUserAction( new CexmcEventAction( physicsManager ) );
415 
416  runManager->SetUserAction( new CexmcRunAction( physicsManager ) );
417 
418  runManager->SetUserAction( new CexmcTrackingAction( physicsManager ) );
419 
420  runManager->SetUserAction( new CexmcSteppingAction( physicsManager ) );
421 
422 #ifdef CEXMC_USE_ROOT
423  CexmcHistoManager::Instance()->Initialize();
424 #endif
425 
426 #ifdef G4VIS_USE
427  if ( cmdLineData.isInteractive )
428  {
429  visManager = new G4VisExecutive( CexmcVisManagerVerboseLevel );
430  visManager->Initialize();
431  }
432 #endif
433 
434 #ifdef CEXMC_USE_PERSISTENCY
435  if ( runManager->ProjectIsRead() )
436  {
437  runManager->ReadProject();
438  runManager->PrintReadRunData();
439  }
440 #endif
441 
442  if ( cmdLineData.initMacro != "" )
443  uiManager->ApplyCommand( "/control/execute " +
444  cmdLineData.initMacro );
445 
446  if ( cmdLineData.isInteractive )
447  productionModel->PrintInitialData();
448 
449 #ifdef G4UI_USE
450  if ( cmdLineData.isInteractive )
451  {
452  if ( cmdLineData.startQtSession )
453  {
454 #ifdef G4UI_USE_QT
455  /* no need to pass all command line options to QApplication */
456  session = new G4UIQt( 1, argv );
457  const G4String & guiMacroName( runManager->GetGuiMacroName() );
458  if ( guiMacroName != "" )
459  uiManager->ApplyCommand( "/control/execute " +
460  guiMacroName );
461 #ifdef CEXMC_USE_ROOTQT
462  CexmcHistoManager::Instance()->EnableLiveHistograms( session );
463 #endif
464 #endif
465  }
466  else
467  {
468 #ifdef G4UI_USE_TCSH
469  session = new G4UIterminal( new G4UItcsh );
470 #else
471  session = new G4UIterminal;
472 #endif
473  }
474  if ( session )
475  session->SessionStart();
476  }
477 #endif
478 
479 #ifdef CEXMC_USE_PERSISTENCY
480  if ( runManager->ProjectIsSaved() )
481  runManager->SaveProject();
482 #endif
483  }
484  catch ( CexmcException & e )
485  {
486  G4cout << e.what() << G4endl;
487  }
488 #ifdef CEXMC_USE_PERSISTENCY
489  catch ( boost::archive::archive_exception & e )
490  {
491  G4cout << CEXMC_LINE_START << "Serialization error: " << e.what() <<
492  G4endl;
493  }
494 #ifdef CEXMC_USE_CUSTOM_FILTER
495  catch ( boost::bad_get & e )
496  {
497  G4cout << CEXMC_LINE_START << "Custom filter error: " << e.what() <<
498  G4endl;
499  }
500 #endif
501 #endif
502  catch ( ... )
503  {
504  G4cout << "Unknown exception caught" << G4endl;
505  }
506 
507 #ifdef CEXMC_USE_ROOT
508  CexmcHistoManager::Destroy();
509 #endif
510 #ifdef G4VIS_USE
511  delete visManager;
512 #endif
514  delete runManager;
515 #ifdef G4UI_USE
516  delete session;
517 #endif
518 
519  return 0;
520 }
521 
virtual void SetUserInitialization(G4VUserDetectorConstruction *userInit)
G4bool ShouldGdmlFileBeValidated(void) const
void SetPhysicsManager(CexmcPhysicsManager *physicsManager_)
static void Destroy(void)
CexmcOutputDataTypeSet outputData
virtual G4UIsession * SessionStart()
Definition: G4UIsession.cc:38
int G4int
Definition: G4Types.hh:78
CexmcProductionModelType
Definition: CexmcCommon.hh:91
G4String GetGdmlFileName(void) const
G4bool ProjectIsRead(void) const
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:58
G4GLOB_DLL std::ostream G4cout
G4String GetGuiMacroName(void) const
#define CEXMC_LINE_START
Definition: CexmcCommon.hh:52
bool G4bool
Definition: G4Types.hh:79
int main(int argc, char **argv)
static G4UIterminal * session
void Initialize()
const char * what(void) const
G4bool parseArgs(int argc, char **argv, CexmcCmdLineData &cmdLineData)
CexmcProductionModelType GetProductionModelType(void) const
virtual void Initialize()
#define G4endl
Definition: G4ios.hh:61
void printUsage(void)
std::set< CexmcOutputDataType > CexmcOutputDataTypeSet
G4bool ProjectIsSaved(void) const
virtual CexmcProductionModel * GetProductionModel(void)=0
static CexmcMessenger * Instance(void)
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:432
virtual void SetUserAction(G4UserRunAction *userAction)