00001 #include "StaticCalibDataSvc.h"
00002 #include "GaudiKernel/IMessageSvc.h"
00003 #include "GaudiKernel/MsgStream.h"
00004 #include "GaudiKernel/SystemOfUnits.h"
00005 #include <sstream>
00006 #include <fstream>
00007 #include <stdlib.h>
00008
00009 using namespace Gaudi;
00010 using namespace std;
00011
00012 StaticCalibDataSvc::StaticCalibDataSvc(const std::string& name, ISvcLocator *svc)
00013 : Service(name,svc)
00014 {
00015
00016 declareProperty("PmtDataFile",m_pmtDataFileName="",
00017 "Filename for loading pmt simulation input data");
00018 declareProperty("RpcDataFile",m_rpcDataFileName="",
00019 "Filename for loading rpc simulation input data");
00020 declareProperty("FeeDataFile",m_feeDataFileName="",
00021 "Filename for loading FEE simulation input data");
00022 declareProperty("FecDataFile",m_fecDataFileName="",
00023 "Filename for loading FEC simulation input data");
00024
00025 m_masterPmtDataFileName = "Master.pmtCalibMap.txt";
00026 m_masterFeeDataFileName = "Master.feeCalibMap.txt";
00027
00028 m_pmtDataMap.clear();
00029 m_rpcDataMap.clear();
00030 m_feeDataMap.clear();
00031 m_fecDataMap.clear();
00032
00033 m_pmtDataBySensorMap.clear();
00034 m_rpcDataBySensorMap.clear();
00035
00036 m_feeDataByChannelMap.clear();
00037 m_fecDataByChannelMap.clear();
00038 }
00039
00040 StaticCalibDataSvc::~StaticCalibDataSvc()
00041 {
00042 }
00043
00044 StatusCode StaticCalibDataSvc::initialize()
00045 {
00046 this->Service::initialize();
00047
00048 IMessageSvc* msg = msgSvc();
00049 if( !msg ){
00050 return StatusCode::FAILURE;
00051 }
00052 MsgStream log(msg,"StaticCalibSvc");
00053
00054
00056 if(m_pmtDataFileName!="") {
00057 msg->reportMessage( "StaticCalibSvc", MSG::INFO, ("Loading " + m_pmtDataFileName).c_str() );
00058 readPmtDataFile( m_pmtDataFileName,
00059 m_pmtDataUser,
00060 m_pmtDataBySensorUser );
00061 }
00062
00063 if(m_feeDataFileName!=""){
00064 msg->reportMessage( "StaticCalibSvc", MSG::INFO, ("Loading " + m_feeDataFileName).c_str() );
00065 readFeeDataFile( m_feeDataFileName,
00066 m_feeDataUser,
00067 m_feeDataByChannelUser );
00068 }
00069
00070 if(m_rpcDataFileName!=""){
00071
00072 std::ostringstream msgStr;
00073 msgStr << "RPC calibration data file not yet implemented!";
00074 msg->reportMessage("StaticCalibDataSvc",MSG::ERROR,msgStr.str());
00075 return StatusCode::FAILURE;
00076 }
00077
00078 if(m_fecDataFileName!=""){
00079
00080 std::ostringstream msgStr;
00081 msgStr << "FEC calibration data file not yet implemented!";
00082 msg->reportMessage("StaticCalibDataSvc",MSG::ERROR,msgStr.str());
00083 return StatusCode::FAILURE;
00084 }
00085
00086
00087 FileNameList::iterator it, itend;
00088
00090 msg->reportMessage( "StaticCalibSvc", MSG::INFO, ("Loading " + m_masterPmtDataFileName).c_str() );
00091 FileNameList PmtDataFileList;
00092 readMasterFile( m_masterPmtDataFileName, PmtDataFileList );
00093
00094 itend = PmtDataFileList.end();
00095 for( it=PmtDataFileList.begin(); it!=itend; it++ ) {
00096 PmtData aPmtData;
00097 PmtDataBySensor aPmtDataBySensor;
00098 readPmtDataFile( it->second,
00099 aPmtData,
00100 aPmtDataBySensor );
00101
00102 m_pmtDataMap[ it->first ] = aPmtData;
00103 m_pmtDataBySensorMap[ it->first ] = aPmtDataBySensor;
00104 }
00105
00106
00107 m_currentPmtContextRange = PmtDataFileList.begin()->first;
00108 m_currentPmtData = &(m_pmtDataMap.begin()->second);
00109 m_currentPmtDataBySensor = &(m_pmtDataBySensorMap.begin()->second);
00110
00112 msg->reportMessage( "StaticCalibSvc", MSG::INFO, ("Loading " + m_masterFeeDataFileName).c_str() );
00113 FileNameList FeeDataFileList;
00114 readMasterFile( m_masterFeeDataFileName, FeeDataFileList );
00115
00116 itend = FeeDataFileList.end();
00117 for( it=FeeDataFileList.begin(); it!=itend; it++ ) {
00118 FeeData aFeeData;
00119 FeeDataByChannel aFeeDataByChannel;
00120 readFeeDataFile( it->second,
00121 aFeeData,
00122 aFeeDataByChannel );
00123
00124 m_feeDataMap[ it->first ] = aFeeData;
00125 m_feeDataByChannelMap[ it->first ] = aFeeDataByChannel;
00126 }
00127
00128
00129 m_currentFeeContextRange = FeeDataFileList.begin()->first;
00130 m_currentFeeData = &(m_feeDataMap.begin()->second);
00131 m_currentFeeDataByChannel = &(m_feeDataByChannelMap.begin()->second);
00132
00137
00138 return StatusCode::SUCCESS;
00139 }
00140
00141 StatusCode StaticCalibDataSvc::finalize()
00142 {
00143 return this->Service::finalize();
00144 }
00145
00146 StatusCode StaticCalibDataSvc::queryInterface(const InterfaceID& riid,
00147 void** ppvInterface)
00148 {
00149 StatusCode sc = StatusCode::FAILURE;
00150 if (ppvInterface) {
00151 *ppvInterface = 0;
00152
00153 if (ICalibDataSvc::interfaceID().versionMatch(riid)) {
00154 *ppvInterface = static_cast<ICalibDataSvc*>(this);
00155 sc = StatusCode::SUCCESS;
00156 addRef();
00157 }
00158 else sc = Service::queryInterface( riid, ppvInterface );
00159 }
00160 return sc;
00161 }
00162
00173 const DayaBay::PmtCalibData* StaticCalibDataSvc::pmtCalibData(
00174 const DayaBay::DetectorSensor& pmtId,
00175 const ServiceMode& svcMode)
00176 {
00177
00178
00179
00180
00181
00182 PmtDataBySensor* pPmtDataBySensor = 0;
00183
00184 if (m_pmtDataFileName.size() != 0) {
00185 pPmtDataBySensor = &m_pmtDataBySensorUser;
00186 } else {
00187 const Context &cont = svcMode.context();
00188 if ( m_currentPmtContextRange.IsCompatible( cont ) ) {
00189 pPmtDataBySensor = m_currentPmtDataBySensor;
00190 } else {
00193 PmtDataBySensorMap::iterator it, itend=m_pmtDataBySensorMap.end();
00194 for( it = m_pmtDataBySensorMap.begin(); it!=itend; it++ ) {
00195 if( it->first.IsCompatible( cont ) ) {
00196 m_currentPmtContextRange = it->first;
00197 m_currentPmtDataBySensor = &(it->second);
00198 pPmtDataBySensor = m_currentPmtDataBySensor;
00200 m_currentPmtData = &(m_pmtDataMap[m_currentPmtContextRange]);
00201 break;
00202 }
00203 }
00204 }
00205 }
00206
00207 if( pPmtDataBySensor!=0 ) {
00208 std::map<DayaBay::DetectorSensor, DayaBay::PmtCalibData>::iterator result =
00209 pPmtDataBySensor->find(pmtId);
00210 if(result != pPmtDataBySensor->end()){
00211 return &(result->second);
00212 }
00214 return 0;
00215 }
00216
00218 return 0;
00219 }
00220
00221 const DayaBay::RpcCalibData* StaticCalibDataSvc::rpcCalibData(
00222 const DayaBay::RpcSensor& rpcId,
00223 const ServiceMode& )
00224 {
00225 std::map<DayaBay::RpcSensor, DayaBay::RpcCalibData>::iterator result =
00226 m_rpcDataBySensorUser.find(rpcId);
00227 if(result != m_rpcDataBySensorUser.end()){
00228 return &(result->second);
00229 }
00230 return 0;
00231 }
00232
00233 const DayaBay::FeeCalibData* StaticCalibDataSvc::feeCalibData(
00234 const DayaBay::FeeChannelId& channelId,
00235 const ServiceMode& svcMode)
00236 {
00237 FeeDataByChannel * pFeeDataByChannel = 0;
00238
00239 if (m_feeDataFileName.size() != 0) {
00240 pFeeDataByChannel = &m_feeDataByChannelUser;
00241 } else {
00242 const Context &cont = svcMode.context();
00243 if ( m_currentFeeContextRange.IsCompatible( cont ) ) {
00244 pFeeDataByChannel = m_currentFeeDataByChannel;
00245 } else {
00248 FeeDataByChannelMap::iterator it, itend=m_feeDataByChannelMap.end();
00249 for( it = m_feeDataByChannelMap.begin(); it!=itend; it++ ) {
00250 if( it->first.IsCompatible( cont ) ) {
00251 m_currentFeeContextRange = it->first;
00252 m_currentFeeDataByChannel = &(it->second);
00253 pFeeDataByChannel = m_currentFeeDataByChannel;
00255 m_currentFeeData = &(m_feeDataMap[m_currentFeeContextRange]);
00256 break;
00257 }
00258 }
00259 }
00260 }
00261
00262 if( pFeeDataByChannel!=0 ) {
00263 std::map<DayaBay::FeeChannelId, DayaBay::FeeCalibData>::iterator result =
00264 pFeeDataByChannel->find(channelId);
00265 if(result != pFeeDataByChannel->end()){
00266 return &(result->second);
00267 }
00269 return 0;
00270 }
00271
00273 return 0;
00274 }
00275
00276 const DayaBay::FecCalibData* StaticCalibDataSvc::fecCalibData(
00277 const DayaBay::FecChannelId& channelId,
00278 const ServiceMode& )
00279 {
00280 std::map<DayaBay::FecChannelId, DayaBay::FecCalibData>::iterator result =
00281 m_fecDataByChannelUser.find(channelId);
00282 if(result != m_fecDataByChannelUser.end()){
00283 return &(result->second);
00284 }
00285 return 0;
00286 }
00287
00288 const std::vector<DayaBay::PmtCalibData>& StaticCalibDataSvc::pmtCalibList(
00289 const DayaBay::Detector& detectorId,
00290 const ServiceMode& svcMode)
00291 {
00292 PmtData* pPmtData = 0;
00293
00294 if (m_pmtDataFileName.size() != 0) {
00295 pPmtData = &m_pmtDataUser;
00296 } else {
00297 const Context &cont = svcMode.context();
00298 if ( m_currentPmtContextRange.IsCompatible( cont ) ) {
00299 pPmtData = m_currentPmtData;
00300 } else {
00303 PmtDataMap::iterator it, itend=m_pmtDataMap.end();
00304 for( it = m_pmtDataMap.begin(); it!=itend; it++ ) {
00305 if( it->first.IsCompatible( cont ) ) {
00306 m_currentPmtContextRange = it->first;
00307 m_currentPmtData = &(it->second);
00308 pPmtData = m_currentPmtData;
00310 m_currentPmtDataBySensor = &(m_pmtDataBySensorMap[m_currentPmtContextRange]);
00311 break;
00312 }
00313 }
00314 }
00315 }
00316
00317 if( pPmtData!=0 ) {
00318 return (*pPmtData)[detectorId];
00319 }
00320
00322 cout<<"No available PmtCalibData found"<<endl;
00323 cout<<"No CalibData file specified, or wrong time range in Master.pmtCalibMap.txt, or no such data file at all"<<endl;
00324 abort();
00325 }
00326
00327 const std::vector<DayaBay::RpcCalibData>& StaticCalibDataSvc::rpcCalibList(
00328 const DayaBay::Detector& detectorId,
00329 const ServiceMode& )
00330 {
00331 return m_rpcDataUser[detectorId];
00332 }
00333
00334 const std::vector<DayaBay::FeeCalibData>& StaticCalibDataSvc::feeCalibList(
00335 const DayaBay::Detector& detectorId,
00336 const ServiceMode& svcMode)
00337 {
00338 FeeData * pFeeData = 0;
00339
00340 if (m_feeDataFileName.size() != 0) {
00341 pFeeData = &m_feeDataUser;
00342 } else {
00343 const Context &cont = svcMode.context();
00344 if ( m_currentFeeContextRange.IsCompatible( cont ) ) {
00345 pFeeData = m_currentFeeData;
00346 } else {
00349 FeeDataMap::iterator it, itend=m_feeDataMap.end();
00350 for( it = m_feeDataMap.begin(); it!=itend; it++ ) {
00351 if( it->first.IsCompatible( cont ) ) {
00352 m_currentFeeContextRange = it->first;
00353 m_currentFeeData = &(it->second);
00354 pFeeData = m_currentFeeData;
00356 m_currentFeeDataByChannel = &(m_feeDataByChannelMap[m_currentFeeContextRange]);
00357 break;
00358 }
00359 }
00360 }
00361 }
00362
00363 if( pFeeData!=0 ) {
00364 return (*pFeeData)[detectorId];
00365 }
00366
00368 cout<<"No available FeeCalibData found"<<endl;
00369 cout<<"No CalibData file specified, or wrong time range in Master.feeCalibMap.txt, or no such data file at all"<<endl;
00370 abort();
00371 }
00372
00373 const std::vector<DayaBay::FecCalibData>& StaticCalibDataSvc::fecCalibList(
00374 const DayaBay::Detector& detectorId,
00375 const ServiceMode& )
00376 {
00377 return m_fecDataUser[detectorId];
00378 }
00379
00380
00381
00382 StatusCode StaticCalibDataSvc::readPmtDataFile( std::string pmtDataFileName, PmtData &pmtData, PmtDataBySensor &pmtDataBySensor )
00383 {
00384 IMessageSvc* msg = msgSvc();
00385 if( !msg ){
00386 return StatusCode::FAILURE;
00387 }
00388 MsgStream log(msg,"StaticCalibSvc");
00389
00390 std::ifstream input( pmtDataFileName.c_str() );
00391 std::ostringstream prtStr;
00392 prtStr<<"PmtCalibMap "<<pmtDataFileName;
00393 msg->reportMessage("StaticCalibDataSvc",MSG::DEBUG,prtStr.str());
00394 if( !input.is_open() ){
00395 std::ostringstream msgStr;
00396 msgStr << "Failed to open input file: " << pmtDataFileName;
00397 msg->reportMessage("StaticCalibDataSvc",MSG::ERROR,msgStr.str());
00398 return StatusCode::FAILURE;
00399 }
00400 int sensorId;
00401 std::string description;
00402 int status;
00403 double speHigh, sigmaSpeHigh, speLow, timeOffset, timeSpread, efficiency;
00404 double prePulseProb, afterPulseProb, darkRate;
00405 std::string line;
00406 while( std::getline(input,line) ){
00407 std::ostringstream msgStr;
00408 msgStr << "Got line: " << line;
00409 msg->reportMessage("StaticCalibDataSvc",MSG::VERBOSE,msgStr.str());
00410 msgStr.str("");
00411 std::stringstream streamLine(line);
00412 if( streamLine.peek() == '#' ){
00413 continue;
00414 }
00415 streamLine >> sensorId >> description
00416 >> status >> speHigh >> sigmaSpeHigh >> speLow
00417 >> timeOffset >> timeSpread >> efficiency >> prePulseProb
00418 >> afterPulseProb >> darkRate;
00419 msgStr << "Adding PMT: " << description;
00420 msg->reportMessage("StaticCalibDataSvc",MSG::VERBOSE,msgStr.str());
00421
00422 DayaBay::DetectorSensor pmtId(sensorId);
00423 DayaBay::Detector detector( pmtId.site(), pmtId.detectorId() );
00424 DayaBay::PmtCalibData pmt;
00425 pmt.m_pmtId = pmtId;
00426 pmt.m_status = (DayaBay::PmtCalibData::Status_t)status;
00427 pmt.m_speHigh = speHigh;
00428 pmt.m_sigmaSpeHigh = sigmaSpeHigh;
00429 pmt.m_speLow = speLow;
00430 pmt.m_timeOffset = timeOffset * Units::ns;
00431 pmt.m_timeSpread = timeSpread * Units::ns;
00432 pmt.m_efficiency = efficiency;
00433 pmt.m_prePulseProb = prePulseProb;
00434 pmt.m_afterPulseProb = afterPulseProb;
00435 pmt.m_darkRate = darkRate * Units::hertz;
00436 std::vector<DayaBay::PmtCalibData>& pmtList = pmtData[detector];
00437 pmtList.push_back(pmt);
00438
00439 pmtDataBySensor[pmtId] = pmtList[pmtList.size()-1];
00440 }
00441
00442 return StatusCode::SUCCESS;
00443
00444 }
00445
00446
00447 StatusCode StaticCalibDataSvc::readFeeDataFile( std::string feeDataFileName, FeeData &feeData, FeeDataByChannel &feeDataByChannel )
00448 {
00449 IMessageSvc* msg = msgSvc();
00450 if( !msg ){
00451 return StatusCode::FAILURE;
00452 }
00453 MsgStream log(msg,"StaticCalibSvc");
00454
00455 std::ifstream input( feeDataFileName.c_str() );
00456 if( !input.is_open() ){
00457 std::ostringstream msgStr;
00458 msgStr << "Failed to open input file: " << feeDataFileName;
00459 msg->reportMessage("StaticCalibDataSvc",MSG::ERROR,msgStr.str());
00460 return StatusCode::FAILURE;
00461 }
00462 int channelId;
00463 std::string description;
00464 int status;
00465 double adcThresholdHigh, adcThresholdLow, adcBaselineHigh, adcBaselineLow;
00466 std::string line;
00467 while( std::getline(input,line) ){
00468 std::ostringstream msgStr;
00469 msgStr << "Got line: " << line;
00470 msg->reportMessage("StaticCalibDataSvc",MSG::VERBOSE,msgStr.str());
00471 msgStr.str("");
00472 std::stringstream streamLine(line);
00473 if( streamLine.peek() == '#' ){
00474 continue;
00475 }
00476 streamLine >> channelId >> description
00477 >> status >> adcThresholdHigh >> adcThresholdLow
00478 >> adcBaselineHigh >> adcBaselineLow;
00479 msgStr << "Adding PMT: " << description;
00480 msg->reportMessage("StaticCalibDataSvc",MSG::VERBOSE,msgStr.str());
00481
00482 DayaBay::FeeChannelId feeChannelId(channelId);
00483 DayaBay::Detector detector(feeChannelId.site(),feeChannelId.detectorId());
00484 DayaBay::FeeCalibData fee;
00485 fee.m_channelId = feeChannelId;
00486 fee.m_status = (DayaBay::FeeCalibData::Status_t)status;
00487 fee.m_adcThresholdHigh = adcThresholdHigh;
00488 fee.m_adcThresholdLow = adcThresholdLow;
00489 fee.m_adcBaselineHigh = adcBaselineHigh;
00490 fee.m_adcBaselineLow = adcBaselineLow;
00491 std::vector<DayaBay::FeeCalibData>& feeList = feeData[detector];
00492 feeList.push_back(fee);
00493
00494 feeDataByChannel[feeChannelId] = feeList[feeList.size()-1];
00495 }
00496
00497 return StatusCode::SUCCESS;
00498
00499 }
00500
00501
00502 StatusCode StaticCalibDataSvc::readMasterFile( std::string masterFileName, FileNameList &subFileNameList )
00503 {
00504 IMessageSvc* msg = msgSvc();
00505 if( !msg ){
00506 return StatusCode::FAILURE;
00507 }
00508 MsgStream log(msg,"StaticCalibSvc");
00509
00510 std::string Path = getenv ("DATASVCROOT");
00511 std::ifstream masterFileStream( (Path+"/share/"+masterFileName).c_str() );
00512
00513 if( !masterFileStream.is_open() ){
00514 msg->reportMessage( "StaticCalibSvc", MSG::ERROR, ("Fail to open" + masterFileName).c_str() );
00515 return StatusCode::FAILURE;
00516 }
00517
00518 int StartRun,EndRun;
00519 int Sites;
00520 int SimFlag, StartTimeSec, StartTimeNanoSec, EndTimeSec, EndTimeNanoSec;
00521 std::string MapFileName;
00522
00523 std::string line;
00524
00525 while( std::getline( masterFileStream, line ) ){
00526 std::stringstream streamLine(line);
00527 if( streamLine.peek() == '#' ){
00528 continue;
00529 }
00530 streamLine >>StartRun >>EndRun >>Sites
00531 >>SimFlag >>StartTimeSec>> StartTimeNanoSec >>EndTimeSec >>EndTimeNanoSec
00532 >>MapFileName;
00533
00534 ContextRange CRange( Sites, SimFlag,
00535 TimeStamp( StartTimeSec, StartTimeNanoSec ),
00536 TimeStamp( EndTimeSec, EndTimeNanoSec ) );
00537
00538
00539
00540
00541 MapFileName = Path + "/share/" + MapFileName;
00542 subFileNameList[ CRange ] = MapFileName ;
00543 }
00544
00545 return StatusCode::SUCCESS;
00546 }
00547