00001 #include "DaqRunInfoSvc.h"
00002 #include "DatabaseSvc/IDatabaseSvc.h"
00003 #include "DbiRawDataFileSvc/DaqRawDataFileInfo.h"
00004 #include "DaqRunInfoSvc/DaqRunInfo.h"
00005 #include "DaqRunInfoSvc/DaqCalibRunInfo.h"
00006
00007 #include "GaudiKernel/MsgStream.h"
00008 #include "GaudiKernel/Service.h"
00009
00010 #include "Conventions/Site.h"
00011 #include "Conventions/SimFlag.h"
00012 #include "Context/TimeStamp.h"
00013 #include "Context/Context.h"
00014 #include "DatabaseInterface/DbiResultPtr.h"
00015 #include "DatabaseInterface/DbiSqlContext.h"
00016
00017 #include <string>
00018 #include <sstream>
00019 #include <cstdlib>
00020
00021 DaqRunInfoSvc::DaqRunInfoSvc(const string& name, ISvcLocator *svc)
00022 :Service(name, svc), m_dbSvc(0)
00023 {
00024 declareProperty("DatabaseSvcName", m_dbSvcName="DatabaseSvc",
00025 "Name of service which provides the interface to mysql");
00026 }
00027
00028 StatusCode DaqRunInfoSvc::initialize()
00029 {
00030 this->Service::initialize();
00031
00032 MsgStream log( msgSvc(), name() );
00033
00034 StatusCode sc = service(m_dbSvcName, m_dbSvc, true);
00035 if( sc != StatusCode::SUCCESS ){
00036 log << MSG::ERROR << "Failed to access Database service: "
00037 << m_dbSvcName <<endreq;
00038 return sc;
00039 }
00040
00041 m_dbName = "testdb";
00042 char* name = getenv("DYB_DB_NAME");
00043 if (name){
00044 m_dbName = string(name);
00045 }
00046 else{
00047 log << MSG::WARNING << "Enviroment variable ${DYB_DB_NAME} is not set."<<endreq;
00048 log << MSG::WARNING << "Use the default database name: "<< m_dbName << endreq;
00049 }
00050 return StatusCode::SUCCESS;
00051 }
00052
00053 StatusCode DaqRunInfoSvc::finalize()
00054 {
00055 return this->Service::finalize();
00056 }
00057
00058 StatusCode DaqRunInfoSvc::queryInterface(const InterfaceID& riid,
00059 void** ppvInterface)
00060 {
00061 StatusCode sc = StatusCode::FAILURE;
00062 if (ppvInterface) {
00063 *ppvInterface = 0;
00064
00065 if (IDaqRunInfoSvc::interfaceID().versionMatch(riid)) {
00066 *ppvInterface = static_cast<IDaqRunInfoSvc*>(this);
00067 sc = StatusCode::SUCCESS;
00068 addRef();
00069 }
00070 else sc = Service::queryInterface( riid, ppvInterface );
00071 }
00072 return sc;
00073 }
00074
00075 const DaqRawDataFileInfo* DaqRunInfoSvc::GetFileInfo(const string fileName)
00076 {
00077 MsgStream log( msgSvc(), name() );
00078
00079
00080 DbiSqlContext vc("1=1");
00081 DbiResultPtr<DaqRawDataFileInfo> rp("DaqRawDataFileInfo", vc);
00082
00083
00084 unsigned int numRows = rp.GetNumRows();
00085 if (numRows < 1){
00086 log << MSG::WARNING <<"==> No records is found in table DaqRawDataFileInfo."<<endreq;
00087 return 0;
00088 }
00089 const DaqRawDataFileInfo* row;
00090 for (unsigned int idx=0; idx < numRows; idx++){
00091 row = rp.GetRow(idx);
00092 if(fileName == row->GetFileName())
00093 return row;
00094 }
00095
00096 log << MSG::WARNING <<fileName <<" is not found in table DaqRawDataFileInfo."<<endreq;
00097 return 0;
00098 }
00099
00100 const DaqRunInfo* DaqRunInfoSvc::GetRunInfo(const int runNo)
00101 {
00102 MsgStream log( msgSvc(), name() );
00103
00104
00105 DbiSqlContext vc("1=1");
00106 DbiResultPtr<DaqRunInfo> rp("DaqRunInfo", vc);
00107
00108
00109 unsigned int numRows = rp.GetNumRows();
00110 if (numRows < 1){
00111 log << MSG::WARNING <<"No records is found in table DaqRunInfo."<<endreq;
00112 return 0;
00113 }
00114 const DaqRunInfo* row;
00115 for (unsigned int idx=0; idx < numRows; idx++){
00116 row = rp.GetRow(idx);
00117 if(runNo == row->GetRunNo())
00118 return row;
00119 }
00120
00121 log << MSG::WARNING <<"runNo: "<<runNo<<" is not found in table DaqRunInfo."<<endreq;
00122 return 0;
00123 }
00124
00125 int DaqRunInfoSvc::runNumber(const string fileName)
00126 {
00127 MsgStream log( msgSvc(), name() );
00128 const DaqRawDataFileInfo* file = GetFileInfo(fileName);
00129 if(!file){
00130 log << MSG::WARNING <<"Can not find "<<fileName<<" in table DaqRawDataFileInfo."<<endreq;
00131 return 0;
00132 }
00133
00134 return file->GetRunNo();
00135 }
00136
00137 const DaqCalibRunInfo* DaqRunInfoSvc::GetCalibRunInfo(const int runNo)
00138 {
00139 MsgStream log( msgSvc(), name() );
00140
00141
00142 DbiSqlContext vc("1=1");
00143 DbiResultPtr<DaqCalibRunInfo> rp("DaqCalibRunInfo", vc);
00144
00145
00146 unsigned int numRows = rp.GetNumRows();
00147 if (numRows < 1){
00148 log << MSG::WARNING <<"No records is found in table DaqCalibRunInfo."<<endreq;
00149 return 0;
00150 }
00151 const DaqCalibRunInfo* row;
00152 for (unsigned int idx=0; idx < numRows; idx++){
00153 row = rp.GetRowByIndex(idx);
00154 if(runNo == row->GetRunNo()){
00155 return row;
00156 }
00157 }
00158
00159 log << MSG::WARNING <<"runNo: "<<runNo<<" is not found in table DaqCalibRunInfo."<<endreq;
00160 return 0;
00161 }
00162
00163 int DaqRunInfoSvc::dbRecordVector(const int runNo, DatabaseRecordVector& vec)
00164 {
00165 stringstream ss_runNo;
00166 ss_runNo<<runNo;
00167
00168 string sql;
00169 sql = "SELECT t1.*";
00170 sql += " FROM DaqRunConfig as t1, DaqRunInfo as t2";
00171 sql += " WHERE t2.runNo = " + ss_runNo.str();
00172 sql += " AND t1.schemaVersion = t2.schemaVersion";
00173 sql += " AND ( t1.dataVersion = t2.dataVersion OR t1.dataVersion = t2.baseVersion AND t1.creationVersion <= t2.dataVersion )";
00174 sql += " ORDER BY t1.creationVersion DESC, className, objectId, name";
00175
00176
00177 DatabaseRecordVector pre_vec;
00178 m_dbSvc->query(m_dbName, sql, pre_vec);
00179
00180
00181 DatabaseRecordVector::iterator itr;
00182 DatabaseRecordVector::iterator it;
00183 for( itr = pre_vec.begin(); itr != pre_vec.end(); itr++){
00184 bool flag = true;
00185 if(vec.size() == 0) {
00186 vec.push_back((*itr));
00187 continue;
00188 }
00189 for( it = vec.begin(); it != vec.end(); it++){
00190 if((*it)["className"] == (*itr)["className"]
00191 && (*it)["objectId"] == (*itr)["objectId"]
00192 && (*it)["name"] == (*itr)["name"]
00193 && (*it)["parentPosition"] == (*itr)["parentPosition"]){
00194 flag = false;
00195 break;
00196 }
00197 }
00198 if(flag) vec.push_back((*itr));
00199 }
00200 return 1;
00201 }
00202
00203 int DaqRunInfoSvc::fullRunConfigList(const int runNo, vector<DayaBay::DaqRunConfig>& vec)
00204 {
00205 DatabaseRecordVector dbRV;
00206 dbRecordVector(runNo, dbRV);
00207
00208 MsgStream log( msgSvc(), name() );
00209 log << MSG::DEBUG <<dbRV.size()<<" rows are selected for runNo="<<runNo<<endreq;
00210
00211 DatabaseRecordVector::iterator it ;
00212 for( it=dbRV.begin(); it != dbRV.end(); it++){
00213 DayaBay::DaqRunConfig config;
00214 config.m_SchemaVersion = atoi((*it)["schemaVersion"].c_str());
00215 config.m_DataVersion = atoi((*it)["dataVersion"].c_str());
00216 config.m_CreationVersion = atoi((*it)["creationVersion"].c_str());
00217 config.m_ClassName = (*it)["className"];
00218 config.m_ObjectId = (*it)["objectId"];
00219 config.m_Name = (*it)["name"];
00220 config.m_ParentPosition = atoi((*it)["parentPosition"].c_str());
00221 config.m_IntValue = atoi((*it)["intValue"].c_str());
00222 config.m_FloatValue = atof((*it)["floatValue"].c_str());
00223 config.m_StringValue = (*it)["stringValue"];
00224
00225 vec.push_back(config);
00226
00227 }
00228
00229 return 1;
00230 }
00231
00232 int DaqRunInfoSvc::simpRunConfigList(const int runNo, vector<DayaBay::DaqRunConfig>& vec)
00233 {
00234 MsgStream log( msgSvc(), name() );
00235
00236 string runType = GetRunType(runNo);
00237 DatabaseRecordVector dbRV;
00238 dbRecordVector(runNo, dbRV);
00239 DatabaseRecordVector::iterator itr;
00240
00241
00242 vector<string> dets;
00243 vector<string> crates;
00244 vector<string> ltbs;
00245 vector<string> fees;
00246 findHWList(dbRV, dets, crates, ltbs, fees);
00247
00248
00249 vector<string> thrs;
00250 for( itr = dbRV.begin(); itr != dbRV.end(); itr++){
00251 if( (*itr)["className"] == "FEE" && stringInVector((*itr)["objectId"], fees) && (*itr)["name"] == "childObjectId_FEEDACThreshold" && (*itr)["stringValue"].find(runType) != string::npos)
00252 thrs.push_back((*itr)["stringValue"]);
00253 }
00254
00255 vector<bool> uniform;
00256 for( itr = dbRV.begin(); itr != dbRV.end(); itr++){
00257 if( (*itr)["className"] == "FEEDACThreshold" && stringInVector((*itr)["objectId"], thrs) && (*itr)["name"] == "DACSelect" ){
00258 if( (*itr)["stringValue"] == "UniformVal" )
00259 uniform.push_back(true);
00260 else
00261 uniform.push_back(false);
00262 }
00263 }
00264
00265 for( itr=dbRV.begin(); itr != dbRV.end(); itr++){
00266 DayaBay::DaqRunConfig config;
00267 config.m_SchemaVersion = atoi((*itr)["schemaVersion"].c_str());
00268 config.m_DataVersion = atoi((*itr)["dataVersion"].c_str());
00269 config.m_CreationVersion = atoi((*itr)["creationVersion"].c_str());
00270 config.m_ClassName = (*itr)["className"];
00271 if(config.m_ClassName == "Partition"
00272 || config.m_ClassName == "ROSApplication"
00273 || config.m_ClassName == "Segment" )
00274 continue;
00275 config.m_ObjectId = (*itr)["objectId"];
00276 if(config.m_ClassName == "FEEDACThreshold" || config.m_ClassName == "LTB_variableReg"){
00277 if(config.m_ObjectId.find(runType) == string::npos)
00278 continue;
00279 }
00280 if(config.m_ClassName == "Detector"){
00281 if(stringInVector(config.m_ObjectId, dets) == 0)
00282 continue;
00283 }
00284 if(config.m_ClassName == "Crate"){
00285 if(stringInVector(config.m_ObjectId, crates) == 0)
00286 continue;
00287 }
00288 if(config.m_ClassName == "LTB"){
00289 if(stringInVector(config.m_ObjectId, ltbs) == 0)
00290 continue;
00291 }
00292 if(config.m_ClassName == "FEE"){
00293 if(stringInVector(config.m_ObjectId, fees) == 0)
00294 continue;
00295 }
00296 if(config.m_ClassName == "FEEDACThreshold"){
00297 if(stringInVector(config.m_ObjectId, thrs) == 0 )
00298 continue;
00299
00300
00301 }
00302
00303 config.m_Name = (*itr)["name"];
00304 if(config.m_Name.find("childClassName") != string::npos)
00305 continue;
00306 config.m_ParentPosition = atoi((*itr)["parentPosition"].c_str());
00307 config.m_IntValue = atoi((*itr)["intValue"].c_str());
00308 config.m_FloatValue = atof((*itr)["floatValue"].c_str());
00309 config.m_StringValue = (*itr)["stringValue"];
00310 bool flag = true;
00311
00312
00313 if(flag) vec.push_back(config);
00314
00315 }
00316
00317 return 1;
00318 }
00319 int DaqRunInfoSvc::findHWList(vector<DayaBay::DaqRunConfig> cv, vector<string>& dets, vector<string>& crates, vector<string>& ltbs, vector<string>& fees)
00320 {
00321 vector<DayaBay::DaqRunConfig>::iterator it;
00322 for ( it = cv.begin(); it != cv.end(); it++){
00323 if(it->m_ClassName == "ROSConfiguration" && it->m_Name == "childObjectId_Detectors")
00324 dets.push_back(it->m_StringValue);
00325 }
00326 for ( it = cv.begin(); it != cv.end(); it++){
00327 if(it->m_ClassName == "Detector" && it->m_Name == "childObjectId_HW_Objects" && stringInVector(it->m_ObjectId, dets))
00328 crates.push_back(it->m_StringValue);
00329 }
00330 for ( it = cv.begin(); it != cv.end(); it++){
00331 if(it->m_ClassName == "Crate" && it->m_Name == "childObjectId_LTB" && stringInVector(it->m_ObjectId, crates))
00332 ltbs.push_back(it->m_StringValue);
00333 if(it->m_ClassName == "Crate" && it->m_Name == "childObjectId_Modules" && stringInVector(it->m_ObjectId, crates))
00334 fees.push_back(it->m_StringValue);
00335 }
00336 return 1;
00337 }
00338
00339 int DaqRunInfoSvc::findFEEThreshold(string runType, vector<DayaBay::DaqRunConfig> cv, vector<string> fees, map<string, string>& th_obj, map<string, bool>& isuni, map<string, int>& thres_uni, map<string, map<string, int> >& thres)
00340 {
00341 vector<DayaBay::DaqRunConfig>::iterator it;
00342 for ( it = cv.begin(); it != cv.end(); it++){
00343 if(it->m_ClassName == "FEE" && stringInVector(it->m_ObjectId, fees) && it->m_Name == "childObjectId_FEEDACThreshold" && it->m_StringValue.find(runType) != string::npos)
00344 th_obj[it->m_ObjectId] = it->m_StringValue;
00345 }
00346
00347 map<string, string>::iterator itr;
00348 for(itr = th_obj.begin(); itr != th_obj.end(); itr++){
00349 for ( it = cv.begin(); it != cv.end(); it++){
00350 if(it->m_ClassName == "FEEDACThreshold" && it->m_ObjectId == itr->second && it->m_Name == "DACSelect"){
00351 if(it->m_StringValue == "UniformVal")
00352 isuni[itr->first] = true;
00353 else
00354 isuni[itr->first] = false;
00355 }
00356 }
00357 map<string, int> amap;
00358 for ( it = cv.begin(); it != cv.end(); it++){
00359 if(it->m_ClassName == "FEEDACThreshold" && it->m_ObjectId == itr->second && it->m_Name == "DACUniformVal" && isuni[itr->first])
00360 thres_uni[itr->first] = it->m_IntValue;
00361 if(it->m_ClassName == "FEEDACThreshold" && it->m_ObjectId == itr->second && it->m_Name.find("DACVal_Chanel") != string::npos){
00362 amap[it->m_Name] = it->m_IntValue;
00363 }
00364 }
00365 thres[itr->first] = amap;
00366 }
00367 return 1;
00368 }
00369
00370 int DaqRunInfoSvc::findLtbTrigger(string runType, vector<DayaBay::DaqRunConfig> cv, vector<string> ltbs, map<string, string>& mode, map<string, int>& source, map<string, string>& trig_name, map<string, map<string, int> >& trig_thres )
00371 {
00372 vector<DayaBay::DaqRunConfig>::iterator it;
00373 for ( it = cv.begin(); it != cv.end(); it++){
00374 if(it->m_ClassName == "LTB" && stringInVector(it->m_ObjectId, ltbs) && it->m_Name == "childObjectId_LTB_variableReg" && it->m_StringValue.find(runType) != string::npos)
00375 mode[it->m_ObjectId] = it->m_StringValue;
00376 }
00377
00378 const int kMANUAL = 1;
00379 const int kCROSS = 2;
00380 const int kPERIODIC = 4;
00381 const int kMULTIPLICITY = 256;
00382 const int kESUM_ADC = 512;
00383 const int kHIGH_ESUM_COMP = 1024;
00384 const int kLOW_ESUM_COMP = 2048;
00385 const int kALL_ESUM_COMP = 4096;
00386
00387 map<string, string>::iterator itr;
00388 for(itr = mode.begin(); itr != mode.end(); itr++){
00389 std::string name = "";
00390 map<string, int> threshold;
00391 for ( it = cv.begin(); it != cv.end(); it++){
00392 if(it->m_ClassName == "LTB_variableReg" && it->m_Name == "LTB_triggerSource" && it->m_ObjectId == itr->second){
00393 source[itr->first] = it->m_IntValue;
00394 if(kMANUAL & it->m_IntValue)
00395 name += "MANUAL_TRIGGER[1] ";
00396 if(kCROSS & it->m_IntValue)
00397 name += "CROSS_TRIGGER_IN[2] ";
00398 if(kPERIODIC & it->m_IntValue)
00399 name += "PERIODIC_TRIGGER[4] ";
00400 if(kMULTIPLICITY & it->m_IntValue)
00401 name += "MULTIPLICITY_TRIGGER[256] ";
00402 if(kESUM_ADC & it->m_IntValue)
00403 name += "ESUM_ADC_TRG[512] ";
00404 if(kHIGH_ESUM_COMP & it->m_IntValue)
00405 name += "HIGH_ESUM_COMP_TRG[1024] ";
00406 if(kLOW_ESUM_COMP & it->m_IntValue)
00407 name += "LOW_ESUM_COMP_TRG[2048] ";
00408 if(kALL_ESUM_COMP & it->m_IntValue)
00409 name += "ALL_ESUM_COMP_TRG[4096] ";
00410 }
00411 }
00412 trig_name[itr->first] = name;
00413
00414 for (it = cv.begin(); it != cv.end(); it++){
00415 if(it->m_ClassName == "LTB_variableReg" && it->m_ObjectId == itr->second){
00416 if(trig_name[itr->first].find("MULTIPLICITY_TRIGGER") != string::npos && it->m_Name == "HSUM_trigger_threshold")
00417 threshold["MULTIPLICITY_TRIGGER"] = it->m_IntValue;
00418 if(trig_name[itr->first].find("ESUM_ADC_TRG") != string::npos && it->m_Name == "ESUM_adc_trigger_threshold")
00419 threshold["ESUM_ADC_TRG"] = it->m_IntValue;
00420 if(trig_name[itr->first].find("HIGH_ESUM_COMP_TRG") != string::npos && it->m_Name == "DAC_half_value")
00421 threshold["HIGH_ESUM_COMP_TRG"] = it->m_IntValue;
00422 if(trig_name[itr->first].find("LOW_ESUM_COMP_TRG") != string::npos && it->m_Name == "DAC_half_value")
00423 threshold["LOW_ESUM_COMP_TRG"] = it->m_IntValue;
00424 if(trig_name[itr->first].find("ALL_ESUM_COMP_TRG") != string::npos && it->m_Name == "DAC_total_value")
00425 threshold["ALL_ESUM_COMP_TRG"] = it->m_IntValue;
00426 }
00427 }
00428 trig_thres[itr->first] = threshold;
00429 }
00430 return 1;
00431 }
00432
00433 int DaqRunInfoSvc::findHWList(DatabaseRecordVector dbRV, vector<string>& dets, vector<string>& crates, vector<string>& ltbs, vector<string>& fees)
00434 {
00435 DatabaseRecordVector::iterator itr;
00436 for( itr = dbRV.begin(); itr != dbRV.end(); itr++){
00437 if( (*itr)["className"] == "ROSConfiguration" && (*itr)["name"] == "childObjectId_Detectors" )
00438 dets.push_back((*itr)["stringValue"]);
00439 }
00440 for( itr = dbRV.begin(); itr != dbRV.end(); itr++){
00441 if( (*itr)["className"] == "Detector" && (*itr)["name"] == "childObjectId_HW_Objects" && stringInVector((*itr)["objectId"], dets))
00442 crates.push_back((*itr)["stringValue"]);
00443 }
00444 for( itr = dbRV.begin(); itr != dbRV.end(); itr++){
00445 if( (*itr)["className"] == "Crate" && (*itr)["name"] == "childObjectId_LTB" && stringInVector((*itr)["objectId"], crates))
00446 ltbs.push_back((*itr)["stringValue"]);
00447 if( (*itr)["className"] == "Crate" && (*itr)["name"] == "childObjectId_Modules" && stringInVector((*itr)["objectId"], crates))
00448 fees.push_back((*itr)["stringValue"]);
00449 }
00450 return 1;
00451 }
00452
00453 string DaqRunInfoSvc::GetRunType(const int runNo)
00454 {
00455
00456 stringstream ss_runNo;
00457 ss_runNo<<runNo;
00458 string sql = "select runType from DaqRunInfo where runNo = " + ss_runNo.str();
00459 DatabaseRecordVector runVec;
00460 m_dbSvc->query(m_dbName, sql, runVec);
00461 DatabaseRecord runRecord = runVec[0];
00462 string runType = runRecord["runType"];
00463
00464 return runType;
00465 }
00466
00467 int DaqRunInfoSvc::stringInVector(string str, vector<string> vec)
00468 {
00469
00470 int index = 0;
00471 int i = 0;
00472 vector<string>::iterator it;
00473 for(it = vec.begin(); it!= vec.end(); it++){
00474 i++;
00475 if((*it) == str){
00476 index = i;
00477 break;
00478 }
00479 }
00480 return index;
00481 }
00482