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