00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "GaudiKernel/SvcFactory.h"
00013 #include "GaudiKernel/ISvcLocator.h"
00014 #include "GaudiKernel/MsgStream.h"
00015
00016 #include "G4Svc/G4Svc.h"
00017 #include "G4Svc/G4SvcRunManager.h"
00018 #ifdef G4VIS_USE
00019 #include "G4Svc/G4SvcVisManager.h"
00020 #include "G4VisExecutive.hh"
00021 #endif
00022
00023
00024
00025 #include "G4UIsession.hh"
00026 #include "G4UIterminal.hh"
00027 #include "G4RunManager.hh"
00028 #include "G4UItcsh.hh"
00029
00030 #include <iostream>
00031 #include <cstdlib>
00032 #include <fstream>
00033 #include <strstream>
00034
00035
00036
00037 G4Svc::G4Svc( const std::string& name, ISvcLocator* svc ):
00038 Service( name, svc ), p_runMgr(0), p_uiMgr(0), p_visMgr(0),
00039 _init(false), m_beamTime(0)
00040 {
00041
00042
00043 declareProperty( "Visualize", m_vis=false);
00044
00045
00046 declareProperty( "RunVerbosity", m_runVerb=0);
00047 declareProperty( "EventVerbosity", m_eventVerb=0);
00048 declareProperty( "TrackingVerbosity", m_trackVerb=0);
00049
00050 declareProperty( "FADSmacro",m_macroName="none" );
00051 declareProperty( "MdcNoiseFile", m_mdcNoiseFile="none");
00052
00053
00054
00055 declareProperty( "LogLevel", m_logLevel=6);
00056
00057 declareProperty( "InteractiveG4" , m_interactiveG4=false);
00058
00059 declareProperty("BesGenAction", m_besGenAction=false);
00060
00061 declareProperty("BeamPosX",m_beamPosX=0);
00062 declareProperty("BeamPosY",m_beamPosY=0);
00063 declareProperty("BeamPosZ",m_beamPosZ=0);
00064
00065 declareProperty("BeamSizeX", m_beamSizeX=10);
00066 declareProperty("BeamSizeY", m_beamSizeY=20);
00067 declareProperty("BeamSizeZ", m_beamSizeZ=30);
00068
00069 declareProperty("BeamStartTime",m_beamStartTime=0);
00070 declareProperty("BeamDeltaTime",m_beamDeltaTime=0);
00071 declareProperty("NBunch",m_nBunch=1);
00072
00073 declareProperty("BunchTimeSigma",m_bunchTimeSigma=0);
00074
00075 declareProperty("MdcDataInput",m_mdcDataInput=1);
00076 declareProperty("MdcDedxFlag",m_mdcDedxFlag=1);
00077
00078 declareProperty("BoostLab",m_boostLab=false);
00079 declareProperty("BeamAngle",m_beamAngle=11);
00080
00081 declareProperty("SetBeamShift",m_setBeamShift= false);
00082 declareProperty("BeamShiftPx" ,m_beamShiftPx=39.45);
00083 declareProperty("BeamShiftPy" ,m_beamShiftPy=-0.98);
00084 declareProperty("BeamShiftPz" ,m_beamShiftPz=3.70);
00085
00086 declareProperty("MdcRootFlag",m_mdcRootFlag=false);
00087 declareProperty("TofRootFlag",m_tofRootFlag=false);
00088 declareProperty("EmcRootFlag",m_emcRootFlag=false);
00089 declareProperty("MucRootFlag",m_mucRootFlag=false);
00090
00091
00092 declareProperty("TofSaturationFlag",m_tofSaturationFlag=true);
00093
00094 declareProperty("EmcLightOutput",m_emcLightOutput=true);
00095 declareProperty("EmcIncoherentNoise",m_emcIncoherentNoise=0.2);
00096 declareProperty("EmcCoherentNoise",m_emcCoherentNoise=0.);
00097 declareProperty("EmcNoiseMean",m_emcNoiseMean=1.16);
00098 declareProperty("EmcNoiseSigma",m_emcNoiseSigma=0.23);
00099 declareProperty("EmcNoiseThreshold",m_emcNoiseThreshold=0.5);
00100 declareProperty("EmcNoiseLevel",m_emcNoiseLevel=3);
00101 declareProperty("EmcTime",m_emcTime=20);
00102 declareProperty("EmcELecSaturation",m_emcELecSaturation=1);
00103
00104 declareProperty("MucNoiseMode", m_mucNoiseMode=1);
00105 }
00106
00107
00108
00109 G4Svc::~G4Svc() {
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 }
00121
00122
00123 G4RunManager* G4Svc::runMgr() {
00124
00125 MsgStream log( msgSvc(), name() );
00126
00127 if (p_runMgr == 0) {
00128 p_runMgr = new G4SvcRunManager();
00129 p_runMgr->SetLogLevel( m_logLevel );
00130
00131
00132 }
00133
00134 return p_runMgr;
00135
00136 }
00137
00138
00139
00140 StatusCode G4Svc::initialize() {
00141
00142 MsgStream log( msgSvc(), name() );
00143
00144 StatusCode status = Service::initialize();
00145 log << MSG::INFO << "Service initialized" << endreq;
00146
00147
00148 IRealizationSvc *tmpReal;
00149 status = service("RealizationSvc", tmpReal);
00150 if (!status.isSuccess())
00151 {
00152 log << MSG::ERROR << " Could not initialize Realization Service" << endreq;
00153 return status;
00154 } else {
00155 log << MSG::INFO << "got the RealizationSvc" << endreq;
00156 m_RealizationSvc=dynamic_cast<RealizationSvc*>(tmpReal);
00157 }
00158
00159 m_runID = m_RealizationSvc->getRunId();
00160
00161 runMgr();
00162 return status;
00163 }
00164
00165
00166
00167 StatusCode G4Svc::finalize() {
00168
00169 MsgStream log( msgSvc(), name() );
00170 StatusCode status = Service::finalize();
00171
00172 if ( status.isSuccess() )
00173 log << MSG::INFO << "Service finalised successfully" << endreq;
00174
00175 return status;
00176 }
00177
00178
00179 StatusCode G4Svc::queryInterface( const InterfaceID& riid,
00180 void** ppvInterface ) {
00181 StatusCode sc = StatusCode::FAILURE;
00182
00183 if (riid == IID_IG4Svc) {
00184 *ppvInterface = dynamic_cast<IG4Svc*>(this);
00185 sc = StatusCode::SUCCESS;
00186 } else {
00187 sc = Service::queryInterface( riid, ppvInterface );
00188 }
00189 return sc;
00190 }
00191
00192
00193 const G4Event* G4Svc::GetCurrentEvent() const {
00194
00195 const G4Event *evt = p_runMgr->GetCurrentEvent();
00196
00197 if (evt == 0) {
00198 MsgStream log( msgSvc(), name() );
00199 log << MSG::ERROR << "Could not get current G4Event" << endreq;
00200 }
00201
00202 return ( evt );
00203
00204 }
00205
00206
00207 const G4Run* G4Svc::GetCurrentRun() const {
00208
00209 return ( p_runMgr->GetCurrentRun() );
00210
00211 }
00212
00213
00214 G4UImanager* G4Svc::uiMgr() {
00215 if (p_uiMgr == 0) {
00216 p_uiMgr = G4UImanager::GetUIpointer();
00217 }
00218 return ( p_uiMgr );
00219 }
00220
00221
00222
00223 void G4Svc::SetUserInitialization(G4VUserDetectorConstruction* userInit) {
00224 if (p_runMgr != 0) {
00225 p_runMgr->SetUserInitialization(userInit);
00226 }
00227 }
00228
00229
00230
00231 void G4Svc::SetUserInitialization(G4VUserPhysicsList* userInit) {
00232 if (p_runMgr != 0) {
00233 p_runMgr->SetUserInitialization(userInit);
00234 }
00235 }
00236
00237
00238
00239 void G4Svc::SetUserAction(G4UserRunAction *action) {
00240 if (p_runMgr != 0) {
00241 p_runMgr->SetUserAction(action);
00242 }
00243 }
00244
00245
00246 void G4Svc::SetUserAction(G4UserEventAction *action) {
00247 if (p_runMgr != 0) {
00248 p_runMgr->SetUserAction(action);
00249 }
00250 }
00251
00252
00253 void G4Svc::SetUserAction(G4VUserPrimaryGeneratorAction *action) {
00254 if (p_runMgr != 0) {
00255 if(m_besGenAction)
00256 p_runMgr->SetUserAction( action );
00257 }
00258 }
00259
00260
00261 void G4Svc::SetUserAction(G4UserStackingAction *action) {
00262 if (p_runMgr != 0) {
00263 p_runMgr->SetUserAction(action);
00264 }
00265 }
00266
00267
00268 void G4Svc::SetUserAction(G4UserTrackingAction *action) {
00269 if (p_runMgr != 0) {
00270 p_runMgr->SetUserAction(action);
00271 }
00272 }
00273
00274
00275 void G4Svc::SetUserAction(G4UserSteppingAction *action) {
00276 if (p_runMgr != 0) {
00277 p_runMgr->SetUserAction(action);
00278 }
00279 }
00280
00281
00282
00283 void G4Svc::StartUISession() {
00284
00285 MsgStream log( msgSvc(), name() );
00286 log << MSG::DEBUG << "Starting G4 terminal" << endreq;
00287
00288 G4UIsession * ses = new G4UIterminal(new G4UItcsh);
00289 ses->SessionStart();
00290
00291 delete ses;
00292 }
00293
00294
00295
00296 void G4Svc::G4Init() {
00297
00298 MsgStream log( msgSvc(), name() );
00299
00300 log << MSG::VERBOSE << "Attempting G4 Initialization" << endreq;
00301
00302 if (!_init) {
00303
00304
00305
00306
00307
00308
00309
00310 #ifdef G4VIS_USE
00311 if (m_vis) {
00312 log << MSG::INFO << "Initializing G4SvcVisManager" << endreq;
00313
00314 p_visMgr = new G4VisExecutive;
00315 p_visMgr->Initialize();
00316 }
00317 #else
00318 m_vis = false;
00319 #endif
00320
00321 if (!m_vis) {
00322 log << MSG::INFO << "Not including Visualization" << endreq;
00323 }
00324
00325
00326 log << MSG::DEBUG << "Setting verbosity levels - "
00327 << "run: " << m_runVerb << " event: " << m_eventVerb
00328 << " track: " << m_trackVerb << endreq;
00329
00330 {
00331 std::ostrstream ost;
00332 ost << "/run/verbose " << m_runVerb << std::ends;
00333 uiMgr()->ApplyCommand(ost.str());
00334 }
00335 {
00336 std::ostrstream ost;
00337 ost << "/event/verbose " << m_eventVerb << std::ends;
00338 uiMgr()->ApplyCommand(ost.str());
00339 }
00340 {
00341 std::ostrstream ost;
00342 ost << "/tracking/verbose " << m_trackVerb << std::ends;
00343 uiMgr()->ApplyCommand(ost.str());
00344 }
00345
00346 log << MSG::DEBUG << "Initializing G4 kernel" << endreq;
00347 p_runMgr->Initialize();
00348 _init = true;
00349
00350 if (m_macroName!="none"){
00351 log << MSG::INFO << "Running G4 macro "<<m_macroName<< endreq;
00352 uiMgr()->ApplyCommand("/control/execute "+m_macroName);
00353 }
00354 if (m_interactiveG4) {
00355 StartUISession();
00356 }
00357
00358 }else {
00359 log << MSG::ERROR << "G4 kernel already initialized" << endreq;
00360 }
00361 }
00362
00363
00364 void G4Svc::RunInitialize()
00365 {
00366 bool cond = p_runMgr->ConfirmBeamOnCondition();
00367 if(cond)
00368 {
00369 std::cout<<"########### initializing the G4Run ############"<<
00370 std::endl;
00371 p_runMgr->SetRunIDCounter( -std::abs(m_runID) );
00372 p_runMgr->RunInitialization();
00373 }
00374 }
00375
00376 void G4Svc::RunTerminate()
00377 {
00378 p_runMgr->RunTermination();
00379 }
00380
00381 void G4Svc::SimulateEvents(int i)
00382 {
00383 p_runMgr->SimulateEvent(i);
00384 }
00385
00386