LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
exampleB2b.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 //
29 
30 #include "B2bDetectorConstruction.hh"
31 #include "B2ActionInitialization.hh"
32 
33 //G4-TBB interfaces
34 #include "tbbUserWorkerInitialization.hh"
35 #include "SimpleTbbMasterRunManager.hh"
36 #include "G4Threading.hh"
37 
38 #include "G4UImanager.hh"
39 #include "FTFP_BERT.hh"
40 #include "G4StepLimiterPhysics.hh"
41 
42 #include "Randomize.hh"
43 
44 #include "G4VisExecutive.hh"
45 #include "G4UIExecutive.hh"
46 
47 //TBB includes
48 #include <tbb/task_scheduler_init.h>
49 #include <tbb/task.h>
50 
51 
52 //This function is very simple: it just start tbb work.
53 //This is done in a seperate thread, because for G4 the
54 //master cannot live in the same thread where workers are
55 //Starting tbb work in a thread guarantees that no workers
56 //are created where the master lives (the main thread)
57 //Clearly a separate solution is to create and configure master
58 //in a separate thread. But this is much simpler.
59 G4ThreadFunReturnType startWork(G4ThreadFunArgType arg)
60 {
61  tbb::task_list* tasks = static_cast<tbb::task_list*>(arg);
62  //We assume at least one /run/beamOn was executed, thus the tasklist is now filled,
63  //lets start TBB
64  try {
65  std::cout<<"Now calling 'tbb::task::spawn_work_and_wait' "<<std::endl;
66  tbb::task::spawn_root_and_wait( *tasks );
67  } catch(std::exception& e) {
68  std::cerr<<"Error occurred. Error info is:\""<<e.what()<<"\""<<std::endl;
69  }
70  return static_cast<G4ThreadFunReturnType>(0);
71 }
72 
73 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
74 
75 int main(int argc,char** argv)
76 {
77  // Instantiate G4UIExecutive if there are no arguments (interactive mode)
78  G4UIExecutive* ui = 0;
79  if ( argc == 1 ) {
80  ui = new G4UIExecutive(argc, argv);
81  }
82 
83  // Choose the Random engine
84 
85  G4Random::setTheEngine(new CLHEP::RanecuEngine);
86 
87  unsigned int numCoresAvailable= G4Threading::G4GetNumberOfCores();
88  unsigned int numberOfCoresToUse= (numCoresAvailable > 1 ) ? 2 : 1 ;
89  //=== TBB engine initialization
90  tbb::task_scheduler_init init( numberOfCoresToUse );
91  tbb::task_list tasks;
92 
93  SimpleTbbMasterRunManager* runManager = new SimpleTbbMasterRunManager;
94 
95  //Set TBB specific data to run-manager, 1 event per tbb::task (e.g. Nevents == N tasks)
96  //Note that a /run/beamOn command will just create tasks and add them to tasks
97  runManager->SetNumberEventsPerTask(1); //Not needed since 1 is however default
98  runManager->SetTaskList(&tasks);
99  //Set user-initialization that specify threading model, in this case TBB.
100  //This overwrites default that uses pthreads
101  runManager->SetUserInitialization(new tbbUserWorkerInitialization );
102 
103  //==== Geant4 specific stuff, from now up to END-G4 comment is copy from MT example
104  // Set mandatory initialization classes
105 
106  runManager->SetUserInitialization(new B2bDetectorConstruction());
107 
108  G4VModularPhysicsList* physicsList = new FTFP_BERT;
109  physicsList->RegisterPhysics(new G4StepLimiterPhysics());
110  runManager->SetUserInitialization(physicsList);
111 
112  // Set user action classes
113 
114  runManager->SetUserInitialization(new B2ActionInitialization());
115 
116  // Initialize G4 kernel
117 
118  runManager->Initialize();
119 
120  // Get the pointer to the User Interface manager
121  G4UImanager* UImanager = G4UImanager::GetUIpointer();
122 
123  if (!ui) // batch mode
124  {
125  G4String command = "/control/execute ";
126  G4String fileName = argv[1];
127  UImanager->ApplyCommand(command+fileName);
128  }
129  else
130  { // interactive mode : define UI session
131 #if 1
132  G4int nEvents= 50;
133  runManager->BeamOn(nEvents);
134 #else
135  UImanager->ApplyCommand("/control/execute init.mac");
136  if (ui->IsGUI())
137  UImanager->ApplyCommand("/control/execute gui.mac");
138  ui->SessionStart();
139  delete ui;
140 #endif
141  }
142  //END-G4
143  G4Thread* aThread = new G4Thread;
144  G4THREADCREATE(aThread, startWork, static_cast<G4ThreadFunArgType>(&tasks));
145 
146  //Wait for work to be finised
147  if(aThread)
148  aThread->join();
149 
150  delete runManager;
151 
152  return 0;
153 }
154 
155 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
int main(int argc, char **argv)
Definition: exampleB2b.cc:50
G4ThreadFunReturnType startWork(G4ThreadFunArgType arg)
Definition: exampleB2b.cc:59
Float_t e
Definition: plot.C:35
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33