BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
G4Svc.cpp
Go to the documentation of this file.
1//------------------------------------------------------------------
2//
3// ClassName: G4Svc
4//
5// Description: This service provides access to Geant 4
6//
7// Based on Athena software
8//
9//------------------------------------------------------------------
10
11// Include files
12#include "GaudiKernel/ISvcLocator.h"
13#include "GaudiKernel/MsgStream.h"
14
15#include "G4Svc.h"
16#include "G4SvcRunManager.h"
17#ifdef G4VIS_USE
18# include "G4Svc/G4SvcVisManager.h"
19# include "G4VisExecutive.hh"
20#endif
21
22// GEANT
23#include "G4RunManager.hh"
24#include "G4UIsession.hh"
25#include "G4UItcsh.hh"
26#include "G4UIterminal.hh"
27
28#include <cstdlib>
29#include <fstream>
30#include <iostream>
31#include <strstream>
32
33//*************************************************************************//
35
36G4Svc::G4Svc( const std::string& name, ISvcLocator* svc )
37 : base_class( name, svc )
38 , p_runMgr( 0 )
39 , p_uiMgr( 0 )
40#ifdef G4VIS_USE
41 , p_visMgr( 0 )
42#endif
43 , _init( false )
44 , m_beamTime( 0 ) {
45 // declareProperty( "PhysicsList", m_PhysicsList="none");
46 // declareProperty( "DefaultPhysicsCut", m_PhysicsCut=-1.);
47 declareProperty( "Visualize", m_vis = false );
48 // declareProperty( "VisType", m_visType="VRML1FILE");
49
50 declareProperty( "RunVerbosity", m_runVerb = 0 );
51 declareProperty( "EventVerbosity", m_eventVerb = 0 );
52 declareProperty( "TrackingVerbosity", m_trackVerb = 0 );
53
54 declareProperty( "FADSmacro", m_macroName = "none" );
55 declareProperty( "MdcNoiseFile", m_mdcNoiseFile = "none" );
56
57 // declareProperty( "SaveHits", m_saveHits=true);
58 // declareProperty( "SaveTracks", m_saveTracks=false);
59 declareProperty( "LogLevel", m_logLevel = 6 );
60
61 declareProperty( "InteractiveG4", m_interactiveG4 = false );
62
63 declareProperty( "BesGenAction", m_besGenAction = false );
64
65 declareProperty( "BeamPosX", m_beamPosX = 0 );
66 declareProperty( "BeamPosY", m_beamPosY = 0 );
67 declareProperty( "BeamPosZ", m_beamPosZ = 0 );
68
69 declareProperty( "BeamSizeX", m_beamSizeX = 10 );
70 declareProperty( "BeamSizeY", m_beamSizeY = 20 );
71 declareProperty( "BeamSizeZ", m_beamSizeZ = 30 );
72
73 declareProperty( "BeamStartTime", m_beamStartTime = 0 );
74 declareProperty( "BeamDeltaTime", m_beamDeltaTime = 0 );
75 declareProperty( "NBunch", m_nBunch = 1 );
76
77 declareProperty( "BunchTimeSigma", m_bunchTimeSigma = 0 );
78
79 declareProperty( "MdcDataInput", m_mdcDataInput = 1 );
80 declareProperty( "MdcDedxFlag", m_mdcDedxFlag = 1 );
81 declareProperty( "MdcNewSimOpt",
82 m_mdcNewSimOpt = 1 ); // (0:Old MdcSim, 1:New) liaoyp -- 2023.10.30
83
84 declareProperty( "BoostLab", m_boostLab = false );
85 declareProperty( "BeamAngle", m_beamAngle = 11 );
86
87 declareProperty( "SetBeamShift", m_setBeamShift = false );
88 declareProperty( "BeamShiftPx", m_beamShiftPx = 39.45 ); // in unit MeV
89 declareProperty( "BeamShiftPy", m_beamShiftPy = -0.98 );
90 declareProperty( "BeamShiftPz", m_beamShiftPz = 3.70 );
91
92 declareProperty( "MdcRootFlag", m_mdcRootFlag = false );
93 declareProperty( "TofRootFlag", m_tofRootFlag = false );
94 declareProperty( "EmcRootFlag", m_emcRootFlag = false );
95 declareProperty( "MucRootFlag", m_mucRootFlag = false );
96
97 // Switch for ADC Saturation in Bhabha events
98 declareProperty( "TofSaturationFlag", m_tofSaturationFlag = true );
99
100 declareProperty( "EmcLightOutput", m_emcLightOutput = true );
101 declareProperty( "EmcIncoherentNoise", m_emcIncoherentNoise = 0.2 );
102 declareProperty( "EmcCoherentNoise", m_emcCoherentNoise = 0. );
103 declareProperty( "EmcNoiseMean", m_emcNoiseMean = 1.16 );
104 declareProperty( "EmcNoiseSigma", m_emcNoiseSigma = 0.23 );
105 declareProperty( "EmcNoiseThreshold", m_emcNoiseThreshold = 0.5 );
106 declareProperty( "EmcNoiseLevel", m_emcNoiseLevel = 3 );
107 declareProperty( "EmcTime", m_emcTime = 20 );
108 declareProperty( "EmcELecSaturation", m_emcELecSaturation = 1 );
109 declareProperty( "EmcELecSatuDead", m_emcELecSatuDead = 0 );
110 declareProperty( "MucNoiseMode", m_mucNoiseMode = 1 );
111}
112
113//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
114
116 // std::cout<<"G4Svc::~G4Svc()"<<std::endl;
117 // if (p_runMgr != 0) {
118 // delete p_runMgr;
119 // }
120 // #ifdef G4VIS_USE
121 // if (p_visMgr != 0) {
122 // delete p_visMgr;
123 // }
124 // #endif
125}
126
127//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
128G4RunManager* G4Svc::runMgr() {
129 // std::cout<<"G4Svc::runMgr()"<<std::endl;
130 MsgStream log( msgSvc(), name() );
131
132 if ( p_runMgr == 0 )
133 {
134 p_runMgr = new G4SvcRunManager();
135 p_runMgr->SetLogLevel( m_logLevel );
136 // p_runMgr->SetSaveHits( m_saveHits );
137 // p_runMgr->SetSaveTracks( m_saveTracks );
138 }
139
140 return p_runMgr;
141}
142
143//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
144
145StatusCode G4Svc::initialize() {
146
147 MsgStream log( msgSvc(), name() );
148
149 StatusCode status = Service::initialize();
150 log << MSG::INFO << "Service initialized" << endmsg;
151
152 // get IRealizationSvc
153 status = service( "RealizationSvc", m_RealizationSvc );
154 if ( !status.isSuccess() )
155 {
156 log << MSG::ERROR << " Could not initialize Realization Service" << endmsg;
157 return status;
158 }
159 else { log << MSG::INFO << "got the RealizationSvc" << endmsg; }
160
161 m_runID = m_RealizationSvc->getRunId();
162
163 runMgr();
164 return status;
165}
166
167//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
168
169StatusCode G4Svc::finalize() {
170
171 MsgStream log( msgSvc(), name() );
172 StatusCode status = Service::finalize();
173
174 if ( status.isSuccess() ) log << MSG::INFO << "Service finalised successfully" << endmsg;
175
176 return status;
177}
178
179//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
180/*StatusCode G4Svc::queryInterface( const InterfaceID& riid,
181 void** ppvInterface ) {
182 StatusCode sc = StatusCode::FAILURE;
183
184 if (riid == IID_IG4Svc) {
185 *ppvInterface = dynamic_cast<IG4Svc*>(this);
186 sc = StatusCode::SUCCESS;
187 } else {
188 sc = Service::queryInterface( riid, ppvInterface );
189 }
190 return sc;
191}*/
192
193//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
194const G4Event* G4Svc::GetCurrentEvent() const {
195
196 const G4Event* evt = p_runMgr->GetCurrentEvent();
197
198 if ( evt == 0 )
199 {
200 MsgStream log( msgSvc(), name() );
201 log << MSG::ERROR << "Could not get current G4Event" << endmsg;
202 }
203
204 return ( evt );
205}
206
207//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
208const G4Run* G4Svc::GetCurrentRun() const { return ( p_runMgr->GetCurrentRun() ); }
209
210//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
211G4UImanager* G4Svc::uiMgr() {
212 if ( p_uiMgr == 0 ) { p_uiMgr = G4UImanager::GetUIpointer(); }
213 return ( p_uiMgr );
214}
215
216//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
217
218void G4Svc::SetUserInitialization( G4VUserDetectorConstruction* userInit ) {
219 if ( p_runMgr != 0 ) { p_runMgr->SetUserInitialization( userInit ); }
220}
221
222//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
223
224void G4Svc::SetUserInitialization( G4VUserPhysicsList* userInit ) {
225 if ( p_runMgr != 0 ) { p_runMgr->SetUserInitialization( userInit ); }
226}
227
228void G4Svc::SetUserInitialization( G4VUserActionInitialization* userInit ) {
229 if ( p_runMgr != 0 ) { p_runMgr->SetUserInitialization( userInit ); }
230}
231
232//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
233
234/*void G4Svc::SetUserAction(G4UserRunAction *action) {
235 if (p_runMgr != 0) {
236 p_runMgr->SetUserAction(action);
237 }
238}*/
239//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
240
241/*void G4Svc::SetUserAction(G4UserEventAction *action) {
242 if (p_runMgr != 0) {
243 p_runMgr->SetUserAction(action);
244 }
245}*/
246//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
247/*
248void G4Svc::SetUserAction(G4VUserPrimaryGeneratorAction *action) {
249 if (p_runMgr != 0) {
250 if(m_besGenAction)
251 p_runMgr->SetUserAction( action );
252 }
253}*/
254//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
255
256/*void G4Svc::SetUserAction(G4UserStackingAction *action) {
257 if (p_runMgr != 0) {
258 p_runMgr->SetUserAction(action);
259 }
260}*/
261//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
262
263/*void G4Svc::SetUserAction(G4UserTrackingAction *action) {
264 if (p_runMgr != 0) {
265 p_runMgr->SetUserAction(action);
266 }
267}*/
268//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
269
270/*void G4Svc::SetUserAction(G4UserSteppingAction *action) {
271 if (p_runMgr != 0) {
272 p_runMgr->SetUserAction(action);
273 }
274}*/
275
276//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
277
279
280 MsgStream log( msgSvc(), name() );
281 log << MSG::DEBUG << "Starting G4 terminal" << endmsg;
282
283 G4UIsession* ses = new G4UIterminal( new G4UItcsh );
284 ses->SessionStart();
285
286 delete ses;
287}
288
289//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
290
292
293 MsgStream log( msgSvc(), name() );
294
295 log << MSG::VERBOSE << "Attempting G4 Initialization" << endmsg;
296
297 if ( !_init )
298 {
299 // Tracking
300 // if (m_saveTracks) {
301 // log << MSG::INFO << "turning tracking on" << std::endl;
302 // uiMgr()->ApplyCommand("/tracking/storeTrajectory 1");
303 //}
304
305 // Visualization manager
306#ifdef G4VIS_USE
307 if ( m_vis )
308 {
309 log << MSG::INFO << "Initializing G4SvcVisManager" << endmsg;
310 // p_visMgr = new G4SvcVisManager;
311 p_visMgr = new G4VisExecutive;
312 p_visMgr->Initialize();
313 }
314#else
315 m_vis = false;
316#endif
317
318 if ( !m_vis ) { log << MSG::INFO << "Not including Visualization" << endmsg; }
319
320 // Verbosity
321 log << MSG::DEBUG << "Setting verbosity levels - "
322 << "run: " << m_runVerb << " event: " << m_eventVerb << " track: " << m_trackVerb
323 << endmsg;
324
325 {
326 std::ostrstream ost;
327 ost << "/run/verbose " << m_runVerb << std::ends;
328 uiMgr()->ApplyCommand( ost.str() );
329 }
330 {
331 std::ostrstream ost;
332 ost << "/event/verbose " << m_eventVerb << std::ends;
333 uiMgr()->ApplyCommand( ost.str() );
334 }
335 {
336 std::ostrstream ost;
337 ost << "/tracking/verbose " << m_trackVerb << std::ends;
338 uiMgr()->ApplyCommand( ost.str() );
339 }
340
341 log << MSG::DEBUG << "Initializing G4 kernel" << endmsg;
342 p_runMgr->Initialize();
343 _init = true;
344
345 if ( m_macroName != "none" )
346 {
347 log << MSG::INFO << "Running G4 macro " << m_macroName << endmsg;
348 uiMgr()->ApplyCommand( "/control/execute " + m_macroName );
349 }
350 if ( m_interactiveG4 ) { StartUISession(); }
351 }
352 else { log << MSG::ERROR << "G4 kernel already initialized" << endmsg; }
353}
354
355//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
357 bool cond = p_runMgr->ConfirmBeamOnCondition();
358 if ( cond )
359 {
360 std::cout << "########### initializing the G4Run ############" << std::endl;
361 p_runMgr->SetRunIDCounter( -std::abs( m_runID ) );
362 p_runMgr->RunInitialization();
363 }
364}
365//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
366void G4Svc::RunTerminate() { p_runMgr->RunTermination(); }
367//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
368void G4Svc::SimulateEvents( int i ) { p_runMgr->SimulateEvent( i ); }
369
370//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
DECLARE_COMPONENT(BesBdkRc)
IMessageSvc * msgSvc()
Definition G4Svc.h:31
virtual StatusCode initialize()
Definition G4Svc.cpp:145
const G4Event * GetCurrentEvent() const
Definition G4Svc.cpp:194
void RunTerminate()
Definition G4Svc.cpp:366
G4UImanager * uiMgr()
Definition G4Svc.cpp:211
virtual ~G4Svc()
Definition G4Svc.cpp:115
void RunInitialize()
Definition G4Svc.cpp:356
G4Svc(const std::string &name, ISvcLocator *svc)
Definition G4Svc.cpp:36
void StartUISession()
Definition G4Svc.cpp:278
G4RunManager * runMgr()
Definition G4Svc.cpp:128
void G4Init()
Definition G4Svc.cpp:291
void SimulateEvents(int)
Definition G4Svc.cpp:368
void SetUserInitialization(G4VUserDetectorConstruction *userInit)
Definition G4Svc.cpp:218
virtual StatusCode finalize()
Definition G4Svc.cpp:169
const G4Run * GetCurrentRun() const
Definition G4Svc.cpp:208