BOSS 7.0.6
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 declareProperty("EmcELecSaturation",m_emcELecSaturation=1);
103 declareProperty("EmcELecSatuDead",m_emcELecSatuDead=0);
104 declareProperty("MucNoiseMode", m_mucNoiseMode=1);
105}
106
107//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
108
110// std::cout<<"G4Svc::~G4Svc()"<<std::endl;
111// if (p_runMgr != 0) {
112// delete p_runMgr;
113// }
114//#ifdef G4VIS_USE
115// if (p_visMgr != 0) {
116// delete p_visMgr;
117// }
118//#endif
119
120}
121
122//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
123G4RunManager* G4Svc::runMgr() {
124 //std::cout<<"G4Svc::runMgr()"<<std::endl;
125 MsgStream log( msgSvc(), name() );
126
127 if (p_runMgr == 0) {
128 p_runMgr = new G4SvcRunManager();
129 p_runMgr->SetLogLevel( m_logLevel );
130 //p_runMgr->SetSaveHits( m_saveHits );
131 //p_runMgr->SetSaveTracks( m_saveTracks );
132 }
133
134 return p_runMgr;
135
136}
137
138//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
139
140StatusCode G4Svc::initialize() {
141
142 MsgStream log( msgSvc(), name() );
143
144 StatusCode status = Service::initialize();
145 log << MSG::INFO << "Service initialized" << endreq;
146
147 //get IRealizationSvc
148 IRealizationSvc *tmpReal;
149 status = service("RealizationSvc", tmpReal);
150 if (!status.isSuccess())
151 {
152 log << MSG::ERROR << " Could not initialize Realization Service" << endreq;
153 return status;
154 } else {
155 log << MSG::INFO << "got the RealizationSvc" << endreq;
156 m_RealizationSvc=dynamic_cast<RealizationSvc*>(tmpReal);
157 }
158
159 m_runID = m_RealizationSvc->getRunId();
160
161 runMgr();
162 return status;
163}
164
165//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
166
167StatusCode G4Svc::finalize() {
168
169 MsgStream log( msgSvc(), name() );
170 StatusCode status = Service::finalize();
171
172 if ( status.isSuccess() )
173 log << MSG::INFO << "Service finalised successfully" << endreq;
174
175 return status;
176}
177
178//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
179StatusCode G4Svc::queryInterface( const InterfaceID& riid,
180 void** ppvInterface ) {
181 StatusCode sc = StatusCode::FAILURE;
182
183 if (riid == IID_IG4Svc) {
184 *ppvInterface = dynamic_cast<IG4Svc*>(this);
185 sc = StatusCode::SUCCESS;
186 } else {
187 sc = Service::queryInterface( riid, ppvInterface );
188 }
189 return sc;
190}
191
192//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
193const G4Event* G4Svc::GetCurrentEvent() const {
194
195 const G4Event *evt = p_runMgr->GetCurrentEvent();
196
197 if (evt == 0) {
198 MsgStream log( msgSvc(), name() );
199 log << MSG::ERROR << "Could not get current G4Event" << endreq;
200 }
201
202 return ( evt );
203
204}
205
206//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
207const G4Run* G4Svc::GetCurrentRun() const {
208
209 return ( p_runMgr->GetCurrentRun() );
210
211}
212
213//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
214G4UImanager* G4Svc::uiMgr() {
215 if (p_uiMgr == 0) {
216 p_uiMgr = G4UImanager::GetUIpointer();
217 }
218 return ( p_uiMgr );
219}
220
221//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
222
223void G4Svc::SetUserInitialization(G4VUserDetectorConstruction* userInit) {
224 if (p_runMgr != 0) {
225 p_runMgr->SetUserInitialization(userInit);
226 }
227}
228
229//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
230
231void G4Svc::SetUserInitialization(G4VUserPhysicsList* userInit) {
232 if (p_runMgr != 0) {
233 p_runMgr->SetUserInitialization(userInit);
234 }
235}
236
237//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
238
239void G4Svc::SetUserAction(G4UserRunAction *action) {
240 if (p_runMgr != 0) {
241 p_runMgr->SetUserAction(action);
242 }
243}
244//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
245
246void G4Svc::SetUserAction(G4UserEventAction *action) {
247 if (p_runMgr != 0) {
248 p_runMgr->SetUserAction(action);
249 }
250}
251//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
252
253void G4Svc::SetUserAction(G4VUserPrimaryGeneratorAction *action) {
254 if (p_runMgr != 0) {
255 if(m_besGenAction)
256 p_runMgr->SetUserAction( action );
257 }
258}
259//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
260
261void G4Svc::SetUserAction(G4UserStackingAction *action) {
262 if (p_runMgr != 0) {
263 p_runMgr->SetUserAction(action);
264 }
265}
266//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
267
268void G4Svc::SetUserAction(G4UserTrackingAction *action) {
269 if (p_runMgr != 0) {
270 p_runMgr->SetUserAction(action);
271 }
272}
273//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
274
275void G4Svc::SetUserAction(G4UserSteppingAction *action) {
276 if (p_runMgr != 0) {
277 p_runMgr->SetUserAction(action);
278 }
279}
280
281//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
282
284
285 MsgStream log( msgSvc(), name() );
286 log << MSG::DEBUG << "Starting G4 terminal" << endreq;
287
288 G4UIsession * ses = new G4UIterminal(new G4UItcsh);
289 ses->SessionStart();
290
291 delete ses;
292}
293
294//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
295
297
298 MsgStream log( msgSvc(), name() );
299
300 log << MSG::VERBOSE << "Attempting G4 Initialization" << endreq;
301
302 if (!_init) {
303 // Tracking
304 //if (m_saveTracks) {
305 // log << MSG::INFO << "turning tracking on" << std::endl;
306 // uiMgr()->ApplyCommand("/tracking/storeTrajectory 1");
307 //}
308
309 // Visualization manager
310#ifdef G4VIS_USE
311 if (m_vis) {
312 log << MSG::INFO << "Initializing G4SvcVisManager" << endreq;
313 //p_visMgr = new G4SvcVisManager;
314 p_visMgr = new G4VisExecutive;
315 p_visMgr->Initialize();
316 }
317#else
318 m_vis = false;
319#endif
320
321 if (!m_vis) {
322 log << MSG::INFO << "Not including Visualization" << endreq;
323 }
324
325 // Verbosity
326 log << MSG::DEBUG << "Setting verbosity levels - "
327 << "run: " << m_runVerb << " event: " << m_eventVerb
328 << " track: " << m_trackVerb << endreq;
329
330 {
331 std::ostrstream ost;
332 ost << "/run/verbose " << m_runVerb << std::ends;
333 uiMgr()->ApplyCommand(ost.str());
334 }
335 {
336 std::ostrstream ost;
337 ost << "/event/verbose " << m_eventVerb << std::ends;
338 uiMgr()->ApplyCommand(ost.str());
339 }
340 {
341 std::ostrstream ost;
342 ost << "/tracking/verbose " << m_trackVerb << std::ends;
343 uiMgr()->ApplyCommand(ost.str());
344 }
345
346 log << MSG::DEBUG << "Initializing G4 kernel" << endreq;
347 p_runMgr->Initialize();
348 _init = true;
349
350 if (m_macroName!="none"){
351 log << MSG::INFO << "Running G4 macro "<<m_macroName<< endreq;
352 uiMgr()->ApplyCommand("/control/execute "+m_macroName);
353 }
354 if (m_interactiveG4) {
356 }
357
358 }else {
359 log << MSG::ERROR << "G4 kernel already initialized" << endreq;
360 }
361}
362
363//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
365{
366 bool cond = p_runMgr->ConfirmBeamOnCondition();
367 if(cond)
368 {
369 std::cout<<"########### initializing the G4Run ############"<<
370 std::endl;
371 p_runMgr->SetRunIDCounter( -std::abs(m_runID) );
372 p_runMgr->RunInitialization();
373 }
374}
375//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
377{
378 p_runMgr->RunTermination();
379}
380//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
382{
383 p_runMgr->SimulateEvent(i);
384}
385
386//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
IMessageSvc * msgSvc()
void SimulateEvent(int i)
virtual StatusCode initialize()
Definition: G4Svc.cpp:140
const G4Event * GetCurrentEvent() const
Definition: G4Svc.cpp:193
void RunTerminate()
Definition: G4Svc.cpp:376
G4UImanager * uiMgr()
Definition: G4Svc.cpp:214
virtual ~G4Svc()
Definition: G4Svc.cpp:109
void RunInitialize()
Definition: G4Svc.cpp:364
G4Svc(const std::string &name, ISvcLocator *svc)
Definition: G4Svc.cpp:37
void StartUISession()
Definition: G4Svc.cpp:283
G4RunManager * runMgr()
Definition: G4Svc.cpp:123
void G4Init()
Definition: G4Svc.cpp:296
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvInterface)
Definition: G4Svc.cpp:179
void SimulateEvents(int)
Definition: G4Svc.cpp:381
void SetUserInitialization(G4VUserDetectorConstruction *userInit)
Definition: G4Svc.cpp:223
virtual StatusCode finalize()
Definition: G4Svc.cpp:167
const G4Run * GetCurrentRun() const
Definition: G4Svc.cpp:207
void SetUserAction(G4UserRunAction *)
Definition: G4Svc.cpp:239
Definition: IG4Svc.h:30