Deprecated/UpgradeASCIITests/G4TankSimulatorASCII/G4TankSimulator.cc
Go to the documentation of this file.
1 #include "G4TankSimulator.h"
3 #include "G4TankPhysicsList.h"
4 #include "G4TankConstruction.h"
6 #include "G4TankTrackingAction.h"
7 #include "G4TankStackingAction.h"
8 #include "G4TankSteppingAction.h"
9 #include "G4TankEventAction.h"
10 #include "G4TankRunAction.h"
11 #include "G4TankVisManager.h"
12 #include "G4TankFastCerenkov.h"
13 #include "G4TankPMT.h"
14 #include "G4OutputHandler.h"
15 
16 #include "ParticleCases.h"
17 
18 #include <G4RunManager.hh>
19 #include <G4UImanager.hh>
20 #include <G4VisManager.hh>
21 
22 #include <fwk/CentralConfig.h>
23 #include <fwk/RandomEngineRegistry.h>
24 
25 #include <det/Detector.h>
26 
27 #include <evt/Event.h>
28 #include <evt/ShowerSimData.h>
29 
30 #include <sdet/SDetector.h>
31 #include <sdet/Station.h>
32 
33 #include <sevt/PMT.h>
34 #include <sevt/PMTSimData.h>
35 #include <sevt/SEvent.h>
36 #include <sevt/Station.h>
37 #include <sevt/StationSimData.h>
38 
39 #include <utl/ErrorLogger.h>
40 #include <utl/Reader.h>
41 #include <utl/Particle.h>
42 #include <utl/ShowerParticleIterator.h>
43 #include <utl/TimeDistribution.h>
44 #include <utl/TimeDistributionAlgorithm.h>
45 
46 #include <cstddef>
47 #include <iostream>
48 #include <sstream>
49 
50 #include <CLHEP/Random/Random.h>
51 
52 #include <tls/Geant4Manager.h>
53 
54 using namespace utl;
55 using namespace fwk;
56 using namespace std;
57 using namespace sevt;
58 using namespace sdet;
59 using namespace evt;
60 using namespace det;
61 using namespace G4TankSimulatorASCII;
62 
63 G4TankPMT* G4TankSimulator::fgPMTAction = 0;
64 const sdet::Station* G4TankSimulator::fCurrentDetectorStation = 0;
65 SEvent::StationIterator G4TankSimulator::fCurrentEventStationIt;
66 StationSimData::ParticleIterator G4TankSimulator::fCurrentParticleIt;
67 
68 bool G4TankSimulator::fgMuCapture;
69 
70 
71 G4TankSimulator::G4TankSimulator() :
72  fRunManager(0),
73  fUImanager(0),
74  fVisManager(0),
75  fGeoVisOn(0),
76  fTrajVisOn(0),
77  fUseGlobalPhysicsList(false),
78  fDetectorConstructed(0),
79  fFastMode(0),
80  fEventId("")
81 {
82 }
83 
84 
86 {
87 }
88 
89 
90 
91 
92 
93 
96 {
97  Branch topB =
98  CentralConfig::GetInstance()->GetTopBranch("G4TankSimulatorASCII");
99  Branch visB = topB.GetChild("visualization");
100 
101  visB.GetChild("geometry").GetData(fGeoVisOn);
102  visB.GetChild("trajectories").GetData(fTrajVisOn);
103 
104  Branch fastModeB = topB.GetChild("fastMode");
105  fastModeB.GetData(fFastMode);
106 
107  Branch muCaptureB = topB.GetChild("muCapture");
108  muCaptureB.GetData(fgMuCapture);
109 
110  Branch globalPhysicsListB = topB.GetChild("globalPhysicsList");
111  globalPhysicsListB.GetData(fUseGlobalPhysicsList);
112 
113  if ( (fGeoVisOn || fTrajVisOn) && !fVisManager) {
115  }
116  if (!fRunManager) {
118  fRunManager = tls::Geant4Manager::GetInstance().GetRunManager();
119  else
120  fRunManager = new G4RunManager;
121  }
122 
123  fUImanager = G4UImanager::GetUIpointer();
124 
125  G4OutputHandler* logger = new G4OutputHandler();
126  fUImanager->SetCoutDestination(logger);
127 
128  if (fUseGlobalPhysicsList) {
129  INFO("using global PhysicsList from Geant4Manager");
130  tls::Geant4Manager::GetInstance().AddVisManager(fVisManager);
131 
132  G4TankPhysicsListCustomization* physicsListCustomization =
134 
135  INFO("Constructing G4Tank");
136  const SDetector& sDetector = Detector::GetInstance().GetSDetector();
137  const SDetector::StationIterator sIt = sDetector.AllStationsBegin();
138  fCurrentDetectorStation = &(*sIt);
139  fgTankConstruction = new G4TankConstruction(); //reads fCurrentDetectorStation
140 
141  tls::Geant4Customization custom("G4TankSimulatorASCII", fgTankConstruction, physicsListCustomization);
147  custom.SetEventAction(new G4TankEventAction);
148  custom.SetRunAction(new G4TankRunAction);
149 
150  tls::Geant4Manager::GetInstance().AddCustomization(custom);
151  }
152  else {
153  INFO("using PhysicsList from G4TankPhysicsList");
154  fRunManager->SetUserInitialization(new G4TankPhysicsList(fFastMode));
155 
156  fRunManager->SetUserAction(new G4TankPrimaryGenerator);
157 
159  fRunManager->SetUserAction(fStackingAction);
160  fRunManager->SetUserAction(new G4TankTrackingAction);
161  fRunManager->SetUserAction(new G4TankSteppingAction);
162  fRunManager->SetUserAction(new G4TankEventAction);
163  fRunManager->SetUserAction(new G4TankRunAction);
164 
165  fUImanager->ApplyCommand("/run/verbose 0");
166  fUImanager->ApplyCommand("/event/verbose 0");
167  fUImanager->ApplyCommand("/tracking/verbose 0");
168  }
169 
170  fDetectorConstructed = false;
171 
172  return eSuccess;
173 }
174 
175 
178 {
179  if (!event.HasSEvent()) {
180  ERROR("SEvent does not exist.");
181  return eFailure;
182  }
183 
184  CLHEP::HepRandom::setTheEngine(&RandomEngineRegistry::GetInstance().
185  Get(RandomEngineRegistry::eDetector).
186  GetEngine());
187 
188  SEvent& sEvent = event.GetSEvent();
189 
190  if (!fDetectorConstructed && sEvent.GetNumberOfStations() > 0) {
191 
192  if (!fUseGlobalPhysicsList) {
193  INFO("Constructing G4Tank");
194 
195  const SEvent::ConstStationIterator sIt = sEvent.StationsBegin();
196  if (sIt == sEvent.StationsEnd()) {
197  ERROR("No stations!");
198  return eFailure;
199  }
200 
201  const SDetector& sDetector = Detector::GetInstance().GetSDetector();
202  fCurrentDetectorStation = &sDetector.GetStation(sIt->GetId());
203 
204  // Construct detector geometry and register w/ RunManager
205  fRunManager->SetUserInitialization(new G4TankConstruction);
206 
207  fRunManager->Initialize();
208  }
209 
210  fDetectorConstructed = true;
211 
212  if (fGeoVisOn || fTrajVisOn) {
213  fVisManager->Initialize();
214  fUImanager->ApplyCommand("/vis/open VRML2FILE");
215  fUImanager->ApplyCommand("/vis/scene/create");
216  fUImanager->ApplyCommand("/vis/sceneHandler/attach");
217  fUImanager->ApplyCommand("/vis/scene/add/volume");
218  fUImanager->ApplyCommand("/vis/viewero/set/style/wireframe");
219  fUImanager->ApplyCommand("/vis/drawVolume");
220  fUImanager->ApplyCommand("/vis/scene/notifyHandlers");
221  fUImanager->ApplyCommand("/vis/viewer/update");
222  }
223  if (fTrajVisOn) {
224  fUImanager->ApplyCommand("/tracking/storeTrajectory 1");
225  fUImanager->ApplyCommand("/vis/scene/add/trajectories");
226  }
227 
228  }
229 
231  tls::Geant4Manager::GetInstance().Customize("G4TankSimulatorASCII");
232 
233  if ( fFastMode == 1 ) return RunFast(event);
234  else return RunFull(event);
235 }
236 
237 
240 {
241  // Get the SEvent
242  SEvent& sEvent = event.GetSEvent();
243 
244  // Get the SDetector
245  const SDetector& sDetector = Detector::GetInstance().GetSDetector();
246 
247  for (SEvent::StationIterator sIt = sEvent.StationsBegin();
248  sIt != sEvent.StationsEnd(); ++sIt) {
249 
250  if (!fDetectorConstructed) {
251  ERROR("The detector hasn't been initialized when looping over stations");
252  return eFailure;
253  }
254 
255  if (sIt->HasSimData()) {
256 
257  StationSimData& simData = sIt->GetSimData();
258 
259  // full-blown tank sim (no optimization of photon tracking)
260  simData.SetSimulatorSignature("G4TankSimulatorFullASCII");
261 
262  const int stationId = sIt->GetId();
263 
264  const unsigned long numParticles =
265  simData.ParticlesEnd() - simData.ParticlesBegin();
266 
267  if (numParticles)
268  G4cerr << stationId << ':' << numParticles << ' ' << flush;
269  else
270  continue;
271 
273  fCurrentDetectorStation = &sDetector.GetStation(stationId);
275 
276  ConstructTraces(*sIt);
277 
279  pIt != simData.ParticlesEnd(); ++pIt) {
280 
281  fCurrentParticleIt = pIt;
282 
283  fRunManager->BeamOn(1);
284 
285  }
286  }
287  }
288 
289  return eSuccess;
290 }
291 
292 
295 {
296  SEvent& sEvent = event.GetSEvent();
297  const SDetector& sDetector = Detector::GetInstance().GetSDetector();
298 
299  // find first station with SimData
300  for (fCurrentEventStationIt = sEvent.StationsBegin();
301  fCurrentEventStationIt != sEvent.StationsEnd() &&
303 
304  if (fCurrentEventStationIt == sEvent.StationsEnd()) {
305  INFO("No stations with SimData found.");
306  return eContinueLoop;
307  }
308 
309  const int id = fCurrentEventStationIt->GetId();
310  fCurrentDetectorStation = &sDetector.GetStation(id);
311  if (!fDetectorConstructed) {
313  fRunManager->SetUserInitialization(fgTankConstruction);
314  fRunManager->Initialize();
315  fDetectorConstructed = true;
316  }
317  fgPMTAction = new G4TankPMT();
318 
319  for (SEvent::StationIterator sIt = sEvent.StationsBegin();
320  sIt != sEvent.StationsEnd(); ++sIt) {
321 
322  if (!sIt->HasSimData()) continue;
323 
324  StationSimData& simData = sIt->GetSimData();
325 
326  simData.SetSimulatorSignature("G4TankSimulatorASCII");
327 
328  const unsigned long numParticles =
329  simData.ParticlesEnd() - simData.ParticlesBegin();
330 
331  const int stationId = sIt->GetId();
332 
333  if (numParticles)
334  G4cerr << stationId << ':' << numParticles << ' ' << flush;
335  else
336  continue;
337 
339  fCurrentDetectorStation = &sDetector.GetStation(stationId);
340 
341  ConstructTraces(*sIt);
342 
343  // prod the Cerenkov process update it's tank description data
345 
347  pIt != simData.ParticlesEnd(); ++pIt) {
348 
349  fCurrentParticleIt = pIt;
352 
353  fRunManager->BeamOn(1);
354 
355  } // particles
356 
357  } // stations
358 
359  G4cerr << endl;
360 
361  delete fgPMTAction;
362 
363  return eSuccess;
364 }
365 
366 
369 {
370  if (fUseGlobalPhysicsList) {
371  tls::Geant4Manager::GetInstance().NotifyDelete();
372  }
373  else {
374  if (fRunManager) {
375  delete fRunManager;
376  fRunManager = 0;
377  }
378  }
379  return eSuccess;
380 }
381 
382 
383 void
385  const
386 {
387  const StationSimData& sSim = station.GetSimData();
388 
389  // Add pmt sim data only if the station contains particles
390  if (sSim.ParticlesEnd() == sSim.ParticlesBegin())
391  return;
392 
394  pmtIt != station.PMTsEnd(sdet::PMTConstants::eAnyType); ++pmtIt)
395  if (!pmtIt->HasSimData()) {
396  pmtIt->MakeSimData();
397  pmtIt->GetSimData().MakePETimeDistribution();
398  }
399 }
void SetRunAction(G4UserRunAction *const action)
fwk::VModule::ResultFlag Run(evt::Event &theEvent)
Run: invoked once per event.
Branch GetTopBranch() const
Definition: Branch.cc:63
fwk::VModule::ResultFlag Finish()
Finish: invoked at end of the run (NOT end of the event)
Station Level Simulated Data
StationIterator StationsEnd()
End of all stations.
Definition: SEvent.h:59
void SetPrimaryGenerator(G4VUserPrimaryGeneratorAction *const action)
Detector description interface for Station-related data.
Report success to RunController.
Definition: VModule.h:62
fwk::VModule::ResultFlag Init()
Initialize: invoked at beginning of run (NOT beginning of event)
int GetNumberOfStations() const
Get total number of stations in the event.
Definition: SEvent.h:124
Interface class to access to the SD part of an event.
Definition: SEvent.h:39
PMTIterator PMTsBegin(const sdet::PMTConstants::PMTType type=sdet::PMTConstants::eWaterCherenkovLarge)
begin PMT iterator for read/write
Skip remaining modules in the current loop and continue with next iteration of the loop...
Definition: VModule.h:68
void SetStackingAction(G4UserStackingAction *const action)
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
boost::filter_iterator< PMTFilter, InternalPMTIterator > PMTIterator
Iterator over station for read/write.
void SetSimulatorSignature(const std::string &name)
Set name of the tank simulator module used to simulate this station.
void SetCurrentTank(const sevt::SEvent::StationIterator sIt)
Definition: G4TankPMT.cc:39
StationIterator AllStationsBegin() const
Beginning of the collection of pointers to all stations in the history of the array.
Definition: SDetector.h:121
ParticleVector::iterator ParticleIterator
Class representing a document branch.
Definition: Branch.h:107
class to hold data at Station level
void SetEventAction(G4UserEventAction *const action)
PMTIterator PMTsEnd(const sdet::PMTConstants::PMTType type=sdet::PMTConstants::eWaterCherenkovLarge)
end PMT iterator for read/write
Data structure to hold the different Geant4 global objects required to run a single module...
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
ParticleIterator ParticlesBegin()
Beginning of simulated particles entering the station.
void SetTrackingAction(G4UserTrackingAction *const action)
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
StationIterator StationsBegin()
Beginning of all stations.
Definition: SEvent.h:57
void SetParticle(const utl::Particle &particle)
Definition: G4TankPMT.cc:9
Detector description interface for SDetector-related data.
Definition: SDetector.h:42
Report failure to RunController, causing RunController to terminate execution.
Definition: VModule.h:64
boost::indirect_iterator< InternalStationIterator, Station & > StationIterator
Iterator over all stations.
Definition: SEvent.h:52
sevt::StationSimData & GetSimData()
Get simulated data at station level.
utl::CoordinateSystemPtr Get(const std::string &id)
Get a well-known Coordinate System.
const Station & GetStation(const int stationId) const
Get station by Station Id.
Definition: SDetector.cc:192
boost::indirect_iterator< InternalConstStationIterator, const Station & > ConstStationIterator
Definition: SEvent.h:54
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
ParticleIterator ParticlesEnd()
End of simulated particles entering the station.
bool HasSEvent() const
void SetSteppingAction(G4UserSteppingAction *const action)
allow customization of the standard global PhysicsList that are handled by the Geant4Manager ...

, generated on Tue Sep 26 2023.