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