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