00001 #include "RealizationSvc/RealizationSvc.h"
00002 #include "GaudiKernel/Kernel.h"
00003 #include "GaudiKernel/IInterface.h"
00004 #include "GaudiKernel/IIncidentSvc.h"
00005 #include "GaudiKernel/Incident.h"
00006 #include "GaudiKernel/IIncidentListener.h"
00007 #include "GaudiKernel/StatusCode.h"
00008 #include "GaudiKernel/SvcFactory.h"
00009 #include "GaudiKernel/MsgStream.h"
00010 #include "GaudiKernel/IDataProviderSvc.h"
00011 #include "GaudiKernel/SmartDataPtr.h"
00012 #include "GaudiKernel/DataSvc.h"
00013 #include <stdint.h>
00014 #include "CLHEP/Random/RandGauss.h"
00015 #include <vector>
00016
00017 #include "EventModel/EventModel.h"
00018 #include "EventModel/EventHeader.h"
00019
00020 using namespace std;
00021
00022
00023 RealizationSvc::RealizationSvc( const string& name, ISvcLocator* svcloc) :
00024 Service (name, svcloc) {
00025 m_CaliConst = 100;
00026 declareProperty("RunIDList", m_runNoList);
00027 declareProperty("IfUseDB", m_useDB = true);
00028 declareProperty("IfReadBunch", m_ifReadBunch = true);
00029 declareProperty("IfUseTrg", m_ifReadTrg = false);
00030 declareProperty("IfReadRanTrg", m_ifReadRandTrg = false);
00031 declareProperty("IfReadTFEE", m_ifReadTFEE = false);
00032 declareProperty("IfReadRunInfo", m_ifReadRunInfo = false);
00033 declareProperty("BossRelease", m_bossRelease="default");
00034 declareProperty("SftVer", m_SftVer = "default");
00035 declareProperty("ParVer", m_ParVer = "default");
00036 declareProperty("InitEvtID", m_initEvtID = 0);
00037 }
00038
00039 StatusCode RealizationSvc::initialize(){
00040 MsgStream log(messageService(), name());
00041 log << MSG::INFO << "RealizationSvc::initialize()" << endreq;
00042
00043 StatusCode sc = Service::initialize();
00044 if( sc.isFailure() ) return sc;
00045
00046 if(m_SftVer == "NONE") m_SftVer = getenv("BES_RELEASE");
00047
00048 for(unsigned int i = 0; i < m_runNoList.size(); i++) {
00049 if(m_runNoList[i] != 0) {
00050 std::cout <<"run id : " << m_runNoList[i] << std::endl;
00051 if(find(m_runIdList.begin(),m_runIdList.end(),m_runNoList[i]) == m_runIdList.end()) m_runIdList.push_back(m_runNoList[i]);
00052 else log << MSG::ALWAYS <<"RunNo "<<m_runNoList[i]<<" repeated in run list"<<endreq;
00053 }
00054 else {
00055 if(i == 0 || (i+1) >= m_runNoList.size()) log << MSG::ERROR << "Please check your run id list, it is wrong" << endreq;
00056 if(m_runNoList[i+1] < m_runNoList[i-1]) {
00057 for(int j = m_runNoList[i+1]+1; j < m_runNoList[i-1]; j++) {
00058 if(find(m_runIdList.begin(),m_runIdList.end(),j) == m_runIdList.end()) m_runIdList.push_back(j);
00059 else log << MSG::ALWAYS <<"RunNo "<<j<<" repeated in run list"<<endreq;
00060 }
00061 }
00062 else {
00063 for(int j = m_runNoList[i-1]+1; j < m_runNoList[i+1]; j++) {
00064 if(find(m_runIdList.begin(),m_runIdList.end(),j) == m_runIdList.end()) m_runIdList.push_back(j);
00065 else log << MSG::ALWAYS <<"RunNo "<<j<<" repeated in run list"<<endreq;
00066 }
00067 }
00068 }
00069 }
00070 m_runID = m_runIdList[0];
00071
00072 m_connect = new RealDBUtil::ConnectionProvider();
00073 if (!m_connect) {
00074 log << MSG::ERROR << "Could not open connection to database" << endreq;
00075 }
00076
00077 IIncidentSvc* incsvc;
00078 sc = service("IncidentSvc", incsvc);
00079 int priority = 100;
00080 if( sc.isSuccess() ){
00081 incsvc -> addListener(this, "NewRun", priority);
00082 }
00083
00084 sc = serviceLocator()->service("EventDataSvc", m_eventSvc, true);
00085 if (sc.isFailure() ) {
00086 log << MSG::ERROR << "Unable to find EventDataSvc " << endreq;
00087 return sc;
00088 }
00089
00090 return StatusCode::SUCCESS;
00091 }
00092
00093 StatusCode RealizationSvc::finalize(){
00094 MsgStream log(messageService(), name());
00095 log << MSG::INFO << "RealizationSvc::finalize()" << endreq;
00096
00097 return StatusCode::SUCCESS;
00098 }
00099
00100 StatusCode RealizationSvc::queryInterface(const InterfaceID& riid, void** ppvInterface)
00101 {
00102 if ( IRealizationSvc::interfaceID().versionMatch(riid) ) {
00103 *ppvInterface = (IRealizationSvc*)this;
00104 }else{
00105 return Service::queryInterface(riid, ppvInterface);
00106 }
00107 addRef();
00108 return StatusCode::SUCCESS;
00109 }
00110
00111 void RealizationSvc::handle(const Incident& inc){
00112 MsgStream log( messageService(), name() );
00113 log << MSG::DEBUG << "handle: " << inc.type() << endreq;
00114 if ( inc.type() == "NewRun" ){
00115 log << MSG::DEBUG << "Begin New Run" << endreq;
00116 if(m_useDB == true) {
00117 int runNo;
00118 SmartDataPtr<Event::EventHeader> evt(m_eventSvc,"/Event/EventHeader");
00119 if( evt ){
00120 runNo = evt -> runNumber();
00121 log << MSG::INFO <<"The runNumber of current event is "<<runNo<<endreq;
00122 }
00123 else {
00124 log << MSG::ERROR <<"Can not get EventHeader!"<<endreq;
00125 }
00126 readDB(std::abs(runNo));
00127 }
00128 }
00129 }
00130
00131 float RealizationSvc::getLuminosity(int runNo) {
00132 using RealDBUtil::ConnectionProvider;
00133
00134 MsgStream log( messageService(), name() );
00135 std::string sLum;
00136 ConnectionProvider::eRet e = m_connect->getReadLumInfo(sLum,runNo, m_SftVer, m_ParVer, m_bossRelease);
00137 if(e != 0 ) {
00138 log << MSG::ERROR << "Could not find Luminosity infor., exit." << endreq;
00139 exit(1);
00140 }
00141 m_lumi = std::atof(sLum.c_str());
00142 log << MSG::INFO << "Luminosity is " << m_lumi << " in Run " << runNo << endreq;
00143 return m_lumi;
00144 }
00145
00146 std::vector<std::string> RealizationSvc::getBgFileName(std::string query) {
00147 MsgStream log( messageService(), name() );
00148 using RealDBUtil::ConnectionProvider;
00149
00150 std::vector<std::string> Bgfilename;
00151 Bgfilename.clear();
00152
00153 std::vector<std::string> fileInfor;
00154 ConnectionProvider::eRet e = m_connect->getReadBackgroundInfo(fileInfor,query);
00155 if(e != 0 ) {
00156 log << MSG::ERROR << "Could not find background infor., exit." << endreq;
00157 exit(1);
00158 }
00159
00160 for(unsigned int i = 0; i < fileInfor.size(); i+=2)
00161 {
00162 Bgfilename.push_back(fileInfor[i]+"/"+fileInfor[i+1]);
00163 }
00164 fileInfor.clear();
00165
00166 for(unsigned int i = 0; i < Bgfilename.size(); i++) {
00167 log << MSG::INFO << "Background file name: " << Bgfilename[i] << endreq;
00168 }
00169
00170 return Bgfilename;
00171 }
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 void RealizationSvc::readDB(int runNo) {
00243 MsgStream log( messageService(), name() );
00244 log << MSG::DEBUG << "In getCaliConst" << endreq;
00245
00246 using RealDBUtil::ConnectionProvider;
00247
00248 StatusCode sc;
00249
00250
00251 if(m_ifReadBunch == true) {
00252 std::vector<std::string> sbunch;
00253 std::vector<double> bunchInfo;
00254 ConnectionProvider::eRet e = m_connect->getReadBunchInfo(sbunch,runNo,m_SftVer,m_ParVer,m_bossRelease);
00255 if(e == 7 ) {
00256 bool last = true;
00257 for(int id = 1; true; id++) {
00258 e = m_connect->getReadBunchInfo(sbunch,runNo+id,m_SftVer,m_ParVer,m_bossRelease);
00259 last = true;
00260 if(e == 7) {
00261 if((runNo-id) > 0) {
00262 e = m_connect->getReadBunchInfo(sbunch,runNo-id,m_SftVer,m_ParVer,m_bossRelease);
00263 last = false;
00264 }
00265 }
00266 if(e == 0) {
00267 if(last == true) log << MSG::INFO << "Use Bunch infor. of run " << runNo+id << " instead of run " << runNo << endreq;
00268 if(last == false) log << MSG::INFO << "Use Bunch infor. of run " << runNo-id << " instead of run " << runNo << endreq;
00269 break;
00270 }
00271 }
00272 }
00273 if(e != 0 && e != 7) {
00274 log << MSG::ERROR << "Could not find Bunch infor., exit." << endreq;
00275 exit(1);
00276 }
00277 for(unsigned int i = 0; i < sbunch.size(); i++)
00278 {
00279 bunchInfo.push_back(atof(sbunch[i].c_str()));
00280 }
00281 sbunch.clear();
00282
00283 if(bunchInfo.size() == 6)
00284 {
00285
00286 m_bunch_x = 10*bunchInfo[0];
00287 m_bunch_y = 10*bunchInfo[1];
00288 m_bunch_z = 10*bunchInfo[2];
00289 m_sigma_x = 10*bunchInfo[3];
00290 m_sigma_y = 10*bunchInfo[4];
00291 m_sigma_z = 10*bunchInfo[5];
00292 log << MSG::INFO << "BunchPosX: " << m_bunch_x << " BunchPosY: " << m_bunch_y << " BunchPosZ: " << m_bunch_z << " in Run " << runNo << endreq;
00293 log << MSG::INFO << "BunchSigmaX: " << m_sigma_x << " BunchSigmaY: " << m_sigma_y << " BunchSigmaZ: " << m_sigma_z << " in Run " << runNo << endreq;
00294 }
00295 else {
00296 log << MSG::ERROR << "Please check the bunch information, the size is " << bunchInfo.size() << endreq;
00297 }
00298 bunchInfo.clear();
00299 }
00300
00301
00302 if(m_ifReadTrg == true) {
00303 m_trgTable.clear();
00304 std::vector<std::string> strgTable;
00305 ConnectionProvider::eRet e = m_connect->getReadTrgTableInfo(strgTable,runNo);
00306 if(e != 0 ) {
00307 log << MSG::ERROR << "Could not find TrgTable infor., exit." << endreq;
00308 exit(1);
00309 }
00310 for(unsigned int i = 0; i < strgTable.size(); i++)
00311 {
00312 long long value = 0;
00313 for (unsigned int j = 0; j < (strgTable[i]).length(); j++)
00314 {
00315 value = value*10 + (strgTable[i][j] - '0');
00316 }
00317 m_trgTable.push_back(uint32_t(value));
00318 }
00319 strgTable.clear();
00320 if(m_trgTable.size() != 65) log << MSG::ERROR << "Please check the TrgTable information, the size is " << m_trgTable.size() << endreq;
00321
00322 std::vector<double> vtrgGain;
00323 e = m_connect->getEmcGain(vtrgGain,runNo);
00324 if(e != 0 ) {
00325 log << MSG::ERROR << "Could not find emc gain infor., exit." << endreq;
00326 exit(1);
00327 }
00328 for(unsigned int i = 0; i < vtrgGain.size(); i++) {
00329 m_trgGain[i] = vtrgGain[i];
00330 }
00331
00332
00333 e = m_connect->getTrgConfigInfo(runNo);
00334 if(e != 0 ) {
00335 log << MSG::ERROR << "Could not find Trigger config infor., exit." << endreq;
00336 exit(1);
00337 }
00338 m_EtotDataSteps = m_connect->getEtotDataSteps();
00339 m_VthBEtotH = m_connect->getVthBEtotH();
00340 m_VthEEtotH = m_connect->getVthEEtotH();
00341 m_VthEtotL = m_connect->getVthEtotL();
00342 m_VthEtotM = m_connect->getVthEtotM();
00343 m_VthBLZ = m_connect->getVthBLZ();
00344 m_VthDiffB = m_connect->getVthDiffB();
00345 m_VthDiffE = m_connect->getVthDiffE();
00346 m_VthBalBLK = m_connect->getVthBalBLK();
00347 m_VthBalEEMC = m_connect->getVthBalEEMC();
00348 m_VthDiffMin = m_connect->getVthDiffMin();
00349 }
00350
00351
00352 if(m_ifReadRandTrg == true) {
00353 std::vector<std::string> fileInfor;
00354 ConnectionProvider::eRet e = m_connect->getReadBackgroundInfo(fileInfor,runNo);
00355 if(e == 7 ) {
00356 bool last = true;
00357 for(int id = 1; true; id++) {
00358 e = m_connect->getReadBackgroundInfo(fileInfor,runNo+id);
00359 last = true;
00360 if(e == 7) {
00361 if((runNo-id) > 0) {
00362 e = m_connect->getReadBackgroundInfo(fileInfor,runNo-id);
00363 last = false;
00364 }
00365 }
00366 if(e == 0) {
00367 if(last == true) log << MSG::INFO << "Use Bg files of run " << runNo+id << " instead of run " << runNo << endreq;
00368 if(last == false) log << MSG::INFO << "Use Bg files of run " << runNo-id << " instead of run " << runNo << endreq;
00369 break;
00370 }
00371 }
00372 }
00373 if(e != 0 && e != 7) {
00374 log << MSG::ERROR << "Could not find background infor., exit." << endreq;
00375 exit(1);
00376 }
00377 m_bgfilename.clear();
00378 for(unsigned int i = 0; i < fileInfor.size(); i+=2)
00379 {
00380 m_bgfilename.push_back(fileInfor[i]+"/"+fileInfor[i+1]);
00381 }
00382 fileInfor.clear();
00383 for(unsigned int i = 0; i < m_bgfilename.size(); i++) {
00384 log << MSG::INFO << "Background file name: " << m_bgfilename[i] << " in run " << runNo << endreq;
00385 }
00386
00387
00388 std::string srunTime;
00389 std::string stau_value;
00390 e = m_connect->getLumCurvePar(srunTime, stau_value, runNo, m_SftVer, m_ParVer, m_bossRelease);
00391 if(e != 0 ) {
00392 log << MSG::ERROR << "Could not find Luminosity curve parameters, exit." << endreq;
00393 exit(1);
00394 }
00395 m_runTotalTime = std::atof(srunTime.c_str());
00396 m_tauValue = std::atof(stau_value.c_str());
00397 log << MSG::INFO << "Total time is " << m_runTotalTime << ", tau is " << m_tauValue << " in run " << runNo << ". " << endreq;
00398 }
00399
00400
00401 if(m_ifReadTFEE == true) {
00402 m_tfee.clear();
00403 ConnectionProvider::eRet e = m_connect->getReadTofThreshInfo(m_tfee,runNo);
00404 if(e != 0 ) {
00405 log << MSG::ERROR << "Could not find TFEE infor., exit." << endreq;
00406 exit(1);
00407 }
00408
00409 for(unsigned int i = 0; i < m_tfee.size(); i++) {
00410 log << MSG::INFO << "TFEE ----> " << m_tfee[i] << endreq;
00411 }
00412 }
00413
00414
00415 if(m_ifReadRunInfo == true) {
00416 m_runInfo.clear();
00417 std::vector<std::string> srunInfo;
00418 ConnectionProvider::eRet e = m_connect->getRunInfo(srunInfo,runNo);
00419 if(e != 0 ) {
00420 log << MSG::ERROR << "Could not find run infor., exit." << endreq;
00421 exit(1);
00422 }
00423 for(unsigned int i = 0; i < srunInfo.size(); i++)
00424 {
00425 m_runInfo.push_back(atof(srunInfo[i].c_str()));
00426 }
00427 srunInfo.clear();
00428
00429 for(unsigned int i = 0; i < m_runInfo.size(); i++) {
00430 log << MSG::INFO << "runInfo ----> " << m_runInfo[i] << endreq;
00431 }
00432 }
00433
00434 }
00435
00436 void RealizationSvc::updateRealData() {
00437 }