00001 #include "BesSim.hh"
00002 #include "BesDetectorConstruction.hh"
00003 #include "ReadBoostRoot.hh"
00004 #include "BesMdcGeoParameter.hh"
00005 #include "BesPhysicsList.hh"
00006 #include "BgsPhysicsList.hh"
00007 #include "BesSensitiveManager.hh"
00008 #include "BesPrimaryGeneratorAction.hh"
00009 #include "BesRunAction.hh"
00010 #include "BesEventAction.hh"
00011 #include "BesSteppingAction.hh"
00012 #include "BesTrackingAction.hh"
00013
00014
00015 #include "QGSP.hh"
00016 #include "QGSP_BIC.hh"
00017 #include "QGSP_BERT.hh"
00018 #include "QGSP_BERT_CHIPS.hh"
00019 #include "QGSP_BERT_HP.hh"
00020 #include "QBBC.hh"
00021 #include "CHIPS.hh"
00022 #include "G4Run.hh"
00023 #include "G4RunManager.hh"
00024
00025 #include "GaudiKernel/MsgStream.h"
00026 #include "GaudiKernel/AlgFactory.h"
00027 #include "GaudiKernel/SmartIF.h"
00028 #include "GaudiKernel/Property.h"
00029 #include "G4Svc/IG4Svc.h"
00030 #include "G4Svc/G4Svc.h"
00031 #include "GaudiKernel/NTuple.h"
00032 #include "BesKernel/IBesRndmGenSvc.h"
00033
00034 #include "G4Writer/G4GDMLWriter.h"
00035
00036 #include "CLHEP/Random/Random.h"
00037
00038 #include "DetVerSvc/IDetVerSvc.h"
00039
00040
00041
00042
00043
00044
00045
00047
00048 BesSim::BesSim(const std::string& name, ISvcLocator* pSvcLocator) :
00049 Algorithm(name, pSvcLocator)
00050 {
00051 declareProperty("Mdc",m_mdc=2);
00052 declareProperty("Tof",m_tof=2);
00053 declareProperty("Emc",m_emc=2);
00054 declareProperty("Muc",m_muc=2);
00055 declareProperty("PipeSCM",m_pipeSCM=2);
00056 declareProperty("Field",m_field=1);
00057 declareProperty("PhysicsList",m_physicsList=0);
00058 declareProperty("FormatAR", m_formatAR =0);
00059 declareProperty("Tuning",m_tuning=0);
00060 declareProperty("TuningFile",m_tuningFile);
00061 declareProperty("HitOut",m_hitOut=0);
00062 declareProperty("TDSFlag",m_TDSFlag=false);
00063 declareProperty("AsciiFlag",m_asciiFlag="00000000");
00064 declareProperty("AsciiFile",m_asciiFile="boost.dat");
00065 declareProperty("RootFlag",m_rootFlag=false);
00066 declareProperty("RootFile",m_rootFile="Hit.root");
00067 declareProperty("CutValue", newcut = 0.7);
00068 declareProperty("LogLevel",m_logLevel=5000);
00069
00070 }
00071
00072
00073
00074 StatusCode BesSim::initialize(){
00075
00076 MsgStream log(msgSvc(), name());
00077 log << MSG::INFO << "initialize()" << endreq;
00078 m_nEvent = 0;
00079 m_nRun = 1;
00080
00081
00082 static const bool CREATEIFNOTTHERE(true);
00083 StatusCode RndmStatus = service("BesRndmGenSvc", p_BesRndmGenSvc, CREATEIFNOTTHERE);
00084 if (!RndmStatus.isSuccess() || 0 == p_BesRndmGenSvc)
00085 {
00086 log << MSG::ERROR << " Could not initialize Random Number Service" << endreq;
00087 return RndmStatus;
00088 }
00089
00090 StatusCode status;
00091 IG4Svc *tmpSvc;
00092 status = service("G4Svc",tmpSvc);
00093 if (status.isSuccess()) {
00094 log << MSG::INFO << "got the G4Svc" << endreq;
00095 m_G4Svc=dynamic_cast<G4Svc *>(tmpSvc);
00096 } else {
00097 log << MSG::ERROR << "could not get the G4Svc" << endreq;
00098 return StatusCode::FAILURE;
00099 }
00100
00101 IDetVerSvc* detVerSvc;
00102 StatusCode sc_det = service("DetVerSvc", detVerSvc);
00103 if( sc_det.isFailure() ) {
00104 log << MSG::ERROR << "can't retrieve DetVerSvc instance" << endreq;
00105 return sc_det;
00106 }
00107
00108 int phase = detVerSvc->phase();
00109 log << MSG::INFO << "** ~~~ZZZ~~~ ** : retrieved DetectorStage = " << phase << endreq;
00110
00111
00112 if( phase>2 ) { m_tof = 4; }
00113
00114
00115 if(m_G4Svc->MdcRootFlag())
00116 bookMdcRootFile();
00117
00118 if(m_G4Svc->TofRootFlag())
00119 bookTofRootFile();
00120
00121 if(m_G4Svc->EmcRootFlag())
00122 bookEmcRootFile();
00123
00124 if(m_G4Svc->MucRootFlag())
00125 bookMucRootFile();
00126
00127 log << MSG::INFO << "Four sub-detector construction flags: " <<m_mdc<<" "<<m_tof<<" "<<m_emc<<" "<<m_muc<<endreq;
00128
00129 ReadBoostRoot* readBoost =new ReadBoostRoot(m_mdc, m_tof,m_emc, m_muc, m_field,m_formatAR,m_tuning,m_hitOut);
00130 BesMdcGeoParameter * mdcGeoService;
00131 mdcGeoService = new BesMdcGeoParameter;
00132
00133 log << MSG::INFO << "Instantiating BESIII Detector" << endreq;
00134
00135 BesSensitiveManager* sensitiveManager = new BesSensitiveManager;
00136 sensitiveManager->SetLogLevel(m_G4Svc->LogLevel());
00137
00138 BesDetectorConstruction* detectorConstruction = new BesDetectorConstruction();
00139 detectorConstruction->SetPipeSCM(m_pipeSCM);
00140 m_G4Svc->SetUserInitialization(detectorConstruction);
00141
00142 switch(m_physicsList)
00143 {
00144 case 1:
00145 m_G4Svc->SetUserInitialization(new BesPhysicsList);
00146 break;
00147 case 2:
00148 m_G4Svc->SetUserInitialization(new QGSP);
00149 break;
00150 case 3:
00151 m_G4Svc->SetUserInitialization(new QGSP_BIC);
00152 break;
00153 case 4:
00154 m_G4Svc->SetUserInitialization(new QGSP_BERT);
00155 break;
00156 case 5:
00157 m_G4Svc->SetUserInitialization(new QGSP_BERT_HP);
00158 break;
00159 case 6:
00160 m_G4Svc->SetUserInitialization(new BgsPhysicsList);
00161 break;
00162 case 7:
00163 m_G4Svc->SetUserInitialization(new CHIPS);
00164 break;
00165 case 8:
00166 m_G4Svc->SetUserInitialization(new QBBC);
00167 break;
00168 case 9:
00169 m_G4Svc->SetUserInitialization(new QGSP_BERT_CHIPS);
00170 break;
00171 default:
00172 m_G4Svc->SetUserInitialization(new QGSP_BERT);
00173 break;
00174 }
00175 m_G4Svc->SetUserAction(new BesPrimaryGeneratorAction);
00176 BesRunAction* runAction = new BesRunAction;
00177 runAction->SetTDSFlag(m_TDSFlag);
00178 runAction->SetAsciiFlag(m_asciiFlag);
00179 runAction->SetAsciiFile(m_asciiFile);
00180 runAction->SetRootFlag(m_rootFlag);
00181 runAction->SetRootFile(m_rootFile);
00182 vector<string>::iterator pd;
00183 for (pd = m_tuningFile.begin(); pd != m_tuningFile.end(); pd++){
00184 std::cout << "tuning File: " << *pd << std::endl;
00185 }
00186
00187 runAction->SetTuningFile(m_tuningFile);
00188 m_G4Svc->SetUserAction(runAction);
00189
00190 BesEventAction* evtAction = new BesEventAction(runAction);
00191 m_G4Svc->SetUserAction(evtAction);
00192
00193 m_G4Svc->SetUserAction(new BesSteppingAction);
00194
00195 m_G4Svc->SetUserAction(new BesTrackingAction(runAction) );
00196
00197
00198
00199
00200
00201
00202
00203
00204 return StatusCode::SUCCESS;
00205 }
00206
00207
00208
00209 StatusCode BesSim::beginRun()
00210 {
00211 static int flag = -1;
00212 if(flag<0)
00213 {
00214 MsgStream log(msgSvc(), name());
00215 log << MSG::DEBUG << "\t start initializing G4 "<<endreq;
00216
00217 m_G4Svc->G4Init();
00218
00219
00220 m_G4Svc->RunInitialize();
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 CLHEP::HepRandomEngine* engine = p_BesRndmGenSvc->GetEngine("SIM");
00242 HepRandom::setTheEngine(engine);
00243 HepRandom::showEngineStatus();
00244 }
00245 return StatusCode::SUCCESS;
00246 }
00247
00248 StatusCode BesSim::bookMdcRootFile()
00249 {
00250 MsgStream log(msgSvc(), name());
00251 NTuplePtr nt(ntupleSvc(), "FILE801/n1");
00252 NTuple::Tuple* tupleMdc;
00253 if(nt) tupleMdc = nt;
00254 else {
00255 tupleMdc = ntupleSvc()->book("FILE801/n1",CLID_ColumnWiseTuple,"BesSim");
00256 m_G4Svc->SetTupleMdc(tupleMdc);
00257 }
00258 return StatusCode::SUCCESS;
00259 }
00260
00261
00262
00263 StatusCode BesSim::bookTofRootFile()
00264 {
00265 MsgStream log(msgSvc(), name());
00266 NTuplePtr nt1(ntupleSvc(), "FILE802/n1");
00267 NTuple::Tuple* tupleTof1;
00268 if(nt1) tupleTof1 = nt1;
00269 else {
00270 tupleTof1 = ntupleSvc()->book("FILE802/n1",CLID_ColumnWiseTuple,"BesSim");
00271 m_G4Svc->SetTupleTof1(tupleTof1);
00272 }
00273 NTuplePtr nt2(ntupleSvc(), "FILE802/n2");
00274 NTuple::Tuple* tupleTof2;
00275 if(nt2) tupleTof2 = nt2;
00276 else {
00277 tupleTof2 = ntupleSvc()->book("FILE802/n2",CLID_ColumnWiseTuple,"BesSim");
00278 m_G4Svc->SetTupleTof2(tupleTof2);
00279 }
00280 NTuplePtr nt3(ntupleSvc(), "FILE802/n3");
00281 NTuple::Tuple* tupleTof3;
00282 if(nt3) tupleTof3 = nt3;
00283 else {
00284 tupleTof3 = ntupleSvc()->book("FILE802/n3",CLID_ColumnWiseTuple,"BesSim");
00285 m_G4Svc->SetTupleTof3(tupleTof3);
00286 }
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 return StatusCode::SUCCESS;
00297 }
00298
00299
00300
00301 StatusCode BesSim::bookEmcRootFile()
00302 {
00303 NTuplePtr nt1(ntupleSvc(), "FILE803/n1");
00304 NTuple::Tuple* tupleEmc1;
00305 if(nt1) tupleEmc1 = nt1;
00306 else {
00307 tupleEmc1 = ntupleSvc()->book("FILE803/n1",CLID_ColumnWiseTuple,"BesSim");
00308 m_G4Svc->SetTupleEmc1(tupleEmc1);
00309 }
00310
00311 NTuplePtr nt2(ntupleSvc(), "FILE803/n2");
00312 NTuple::Tuple* tupleEmc2;
00313 if(nt2) tupleEmc2 = nt2;
00314 else {
00315 tupleEmc2 = ntupleSvc()->book("FILE803/n2",CLID_ColumnWiseTuple,"BesSim");
00316 m_G4Svc->SetTupleEmc2(tupleEmc2);
00317 }
00318
00319
00320 return StatusCode::SUCCESS;
00321 }
00322
00323
00324
00325 StatusCode BesSim::bookMucRootFile()
00326 {
00327 MsgStream log(msgSvc(), name());
00328 NTuplePtr nt(ntupleSvc(), "FILE804/n1");
00329 NTuple::Tuple* tupleMuc;
00330 if(nt) tupleMuc = nt;
00331 else {
00332 tupleMuc = ntupleSvc()->book("FILE804/n1",CLID_ColumnWiseTuple,"BesSim");
00333 m_G4Svc->SetTupleMuc(tupleMuc);
00334 }
00335 return StatusCode::SUCCESS;
00336 }
00337
00338
00339
00340 StatusCode BesSim::execute() {
00341
00342 MsgStream log(msgSvc(), name());
00343
00344
00345 if(m_nEvent%m_logLevel == 0)
00346 std::cout<<" BesSim Begin of Event: "<<m_nEvent<<std::endl;
00347
00348 m_G4Svc->SimulateEvents(m_nEvent);
00349 m_nEvent++;
00350
00351 return StatusCode::SUCCESS;
00352 }
00353
00354
00355
00356 StatusCode BesSim::finalize() {
00357
00358 MsgStream log(msgSvc(), name());
00359 log << MSG::INFO << "finalize()" << endreq;
00360 log << MSG::DEBUG << "\t terminating the current G4 run"<<endreq;
00361 m_G4Svc->RunTerminate();
00362
00363 std::cout<<"BesSim::finalize(), total events in this run: "<<m_nEvent<<std::endl;
00364
00365 if(m_G4Svc->runMgr() != 0)
00366 delete m_G4Svc->runMgr();
00367
00368 #ifdef G4VIS_USE
00369 if(m_G4Svc->visMgr() != 0)
00370 delete m_G4Svc->visMgr();
00371 #endif
00372
00373
00374 return StatusCode::SUCCESS;
00375 }