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