00001 #include "CondTools/RPC/interface/RPCReadOutMappingSourceHandler.h"
00002 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00003 #include "FWCore/ServiceRegistry/interface/Service.h"
00004 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00005
00006 popcon::RPCReadOutMappingSourceHandler::RPCReadOutMappingSourceHandler(const edm::ParameterSet& ps):
00007 m_name(ps.getUntrackedParameter<std::string>("name","RPCReadOutMappingSourceHandler")),
00008 m_validate(ps.getUntrackedParameter<int>("Validate",0)),
00009 m_host(ps.getUntrackedParameter<std::string>("OnlineDBHost","lxplus.cern.ch")),
00010 m_sid(ps.getUntrackedParameter<std::string>("OnlineDBSID","blah")),
00011 m_user(ps.getUntrackedParameter<std::string>("OnlineDBUser","blaah")),
00012 m_pass(ps.getUntrackedParameter<std::string>("OnlineDBPass","blaaah")),
00013 m_port(ps.getUntrackedParameter<int>("OnlineDBPort",1521))
00014 {
00015 }
00016
00017 popcon::RPCReadOutMappingSourceHandler::~RPCReadOutMappingSourceHandler()
00018 {
00019 }
00020
00021 void popcon::RPCReadOutMappingSourceHandler::getNewObjects()
00022 {
00023
00024
00025
00026 edm::Service<cond::service::PoolDBOutputService> mydbservice;
00027
00028
00029 const RPCReadOutMapping* cabling_prev;
00030 if(m_validate==1) {
00031
00032 std::cout<<" Sorry, validation not available for the moment..."<<std::endl;
00033 }
00034
00035
00036 ConnectOnlineDB(m_host,m_sid,m_user,m_pass,m_port);
00037 readCablingMap();
00038 DisconnectOnlineDB();
00039
00040 cond::Time_t snc=mydbservice->currentTime();
00041
00042
00043 int difference=1;
00044 if (m_validate==1) difference=Compare2Cablings(cabling_prev,cabling);
00045 if (!difference) cout<<"No changes - will not write anything!!!"<<endl;
00046 if (difference==1) {
00047 cout<<"Will write new object to offline DB!!!"<<endl;
00048 m_to_transfer.push_back(std::make_pair((RPCReadOutMapping*)cabling,snc));
00049 }
00050
00051 std::cout << "RPCReadOutMappingSourceHandler: RPCReadOutMappingSourceHandler::getNewObjects ends\n";
00052 }
00053
00054 void popcon::RPCReadOutMappingSourceHandler::ConnectOnlineDB(string host, string sid, string user, string pass, int port=1521)
00055 {
00056 stringstream ss;
00057 ss << "//" << host << ":" << port << "/" << sid;
00058
00059 cout << "RPCReadOutMappingSourceHandler: connecting to " << host << "..." << flush;
00060 env = Environment::createEnvironment(Environment::OBJECT);
00061 conn = env->createConnection(user, pass, ss.str());
00062 cout << "Done." << endl;
00063 }
00064
00065 void popcon::RPCReadOutMappingSourceHandler::DisconnectOnlineDB()
00066 {
00067 env->terminateConnection(conn);
00068 Environment::terminateEnvironment(env);
00069 }
00070
00071 void popcon::RPCReadOutMappingSourceHandler::readCablingMap()
00072 {
00073
00074
00075 time_t rawtime;
00076 time(&rawtime);
00077 tm * ptm = gmtime(&rawtime);
00078 char buffer[20];
00079 strftime(buffer,20,"%d/%m/%Y_%H:%M:%S",ptm);
00080 string cabling_version=(string)buffer;
00081
00082 Statement* stmt = conn->createStatement();
00083 string sqlQuery ="";
00084
00085 cout << endl <<"RPCReadOutMappingSourceHandler: start to build RPC cabling..." << flush << endl << endl;
00086 cabling = new RPCReadOutMapping(cabling_version);
00087
00088
00089 sqlQuery=" SELECT DCCBoardId, FEDNumber FROM DCCBoard WHERE DCCBoardId>0 ORDER BY FEDNumber ";
00090 stmt->setSQL(sqlQuery.c_str());
00091 ResultSet* rset = stmt->executeQuery();
00092 std::pair<int,int> tmp_tbl;
00093 std::vector< std::pair<int,int> > theDAQ;
00094 while (rset->next()) {
00095 tmp_tbl.first=rset->getInt(1);
00096 tmp_tbl.second=rset->getInt(2);
00097 theDAQ.push_back(tmp_tbl);
00098 }
00099 for(unsigned int iFED=0;iFED<theDAQ.size();iFED++) {
00100 std::vector<std::pair<int,int> > theTB;
00101 DccSpec dcc(theDAQ[iFED].second);
00102 sqlQuery = " SELECT TriggerBoardId, DCCInputChannelNum FROM TriggerBoard ";
00103 sqlQuery += " WHERE DCCBoard_DCCBoardId= ";
00104 sqlQuery += IntToString(theDAQ[iFED].first);
00105 sqlQuery += " ORDER BY DCCInputChannelNum ";
00106 stmt->setSQL(sqlQuery.c_str());
00107 rset = stmt->executeQuery();
00108 while (rset->next()) {
00109 tmp_tbl.first=rset->getInt(1);
00110 tmp_tbl.second=rset->getInt(2);
00111 theTB.push_back(tmp_tbl);
00112 }
00113 for(unsigned int iTB=0;iTB<theTB.size();iTB++) {
00114 std::vector<std::pair<int,int> > theLink;
00115 TriggerBoardSpec tb(theTB[iTB].second);
00116 sqlQuery = " SELECT Board_BoardId, TriggerBoardInputNum FROM LinkConn ";
00117 sqlQuery += " WHERE TB_TriggerBoardId= ";
00118 sqlQuery += IntToString(theTB[iTB].first);
00119 sqlQuery += " ORDER BY TriggerBoardInputNum ";
00120 stmt->setSQL(sqlQuery.c_str());
00121 rset = stmt->executeQuery();
00122 while (rset->next()) {
00123 tmp_tbl.first=rset->getInt(1);
00124 tmp_tbl.second=rset->getInt(2);
00125 theLink.push_back(tmp_tbl);
00126 }
00127 for(unsigned int iLink=0;iLink<theLink.size();iLink++) {
00128 std::vector<std::pair<int,string> > theLB;
00129 std::pair<int,string> tmpLB;
00130
00131 sqlQuery = " SELECT Name ";
00132 sqlQuery += " FROM Board ";
00133 sqlQuery += " WHERE BoardId= ";
00134 sqlQuery += IntToString(theLink[iLink].first);
00135 stmt->setSQL(sqlQuery.c_str());
00136 rset = stmt->executeQuery();
00137 while (rset->next()) {
00138 tmpLB.first=theLink[iLink].first;
00139 tmpLB.second=rset->getString(1);
00140 theLB.push_back(tmpLB);
00141 }
00142
00143 sqlQuery = " SELECT LinkBoard.LinkBoardId, Board.Name ";
00144 sqlQuery += " FROM LinkBoard, Board ";
00145 sqlQuery += " WHERE LinkBoard.MasterId= ";
00146 sqlQuery += IntToString(theLink[iLink].first);
00147 sqlQuery += " AND Board.BoardId=LinkBoard.LinkBoardId";
00148 sqlQuery += " AND LinkBoard.MasterId<>LinkBoard.LinkBoardId";
00149 sqlQuery += " ORDER BY LinkBoard.LinkBoardId ";
00150 stmt->setSQL(sqlQuery.c_str());
00151 rset = stmt->executeQuery();
00152 while (rset->next()) {
00153 tmpLB.first=rset->getInt(1);
00154 tmpLB.second=rset->getString(2);
00155 theLB.push_back(tmpLB);
00156 }
00157 LinkConnSpec lc(theLink[iLink].second);
00158 int linkChannel;
00159 for(unsigned int iLB=0; iLB<theLB.size(); iLB++) {
00160 linkChannel=atoi(((theLB[iLB].second).substr((theLB[iLB].second).length()-1,1)).c_str());
00161 bool master = (theLB[iLB].first==theLink[iLink].first);
00162 LinkBoardSpec lb(master,linkChannel);
00163 FEBStruct tmpFEB;
00164 std::vector<FEBStruct> theFEB;
00165 sqlQuery = " SELECT FEBLocation.FEBLocationId,";
00166 sqlQuery += " FEBLocation.CL_ChamberLocationId,";
00167 sqlQuery += " FEBConnector.FEBConnectorId,";
00168 sqlQuery += " FEBLocation.FEBLocalEtaPartition,";
00169 sqlQuery += " FEBLocation.PosInLocalEtaPartition,";
00170 sqlQuery += " FEBLocation.FEBCMSEtaPartition,";
00171 sqlQuery += " FEBLocation.PosInCMSEtaPartition,";
00172 sqlQuery += " FEBConnector.LinkBoardInputNum ";
00173 sqlQuery += " FROM FEBLocation, FEBConnector ";
00174 sqlQuery += " WHERE FEBLocation.LB_LinkBoardId= ";
00175 sqlQuery += IntToString(theLB[iLB].first);
00176 sqlQuery += " AND FEBLocation.FEBLocationId=FEBConnector.FL_FEBLocationId";
00177 sqlQuery += " ORDER BY FEBLocation.FEBLocationId, FEBConnector.FEBConnectorId";
00178 stmt->setSQL(sqlQuery.c_str());
00179 rset = stmt->executeQuery();
00180 while (rset->next()) {
00181 tmpFEB.febId=rset->getInt(1);
00182 tmpFEB.chamberId=rset->getInt(2);
00183 tmpFEB.connectorId=rset->getInt(3);
00184 tmpFEB.localEtaPart=rset->getString(4);
00185 tmpFEB.posInLocalEtaPart=rset->getInt(5);
00186 tmpFEB.cmsEtaPart=rset->getString(6);
00187 tmpFEB.posInCmsEtaPart=rset->getInt(7);
00188 tmpFEB.lbInputNum=rset->getInt(8);
00189 theFEB.push_back(tmpFEB);
00190 }
00191 for(unsigned int iFEB=0; iFEB<theFEB.size(); iFEB++) {
00192 FebLocationSpec febLocation = {theFEB[iFEB].cmsEtaPart,theFEB[iFEB].posInCmsEtaPart,theFEB[iFEB].localEtaPart,theFEB[iFEB].posInLocalEtaPart};
00193
00194 ChamberLocationSpec chamber;
00195 sqlQuery = "SELECT DiskOrWheel, Layer, Sector, Subsector,";
00196 sqlQuery += " ChamberLocationName,";
00197 sqlQuery += " FEBZOrnt, FEBRadOrnt, BarrelOrEndcap";
00198 sqlQuery += " FROM ChamberLocation ";
00199 sqlQuery += " WHERE ChamberLocationId= ";
00200 sqlQuery += IntToString(theFEB[iFEB].chamberId);
00201 stmt->setSQL(sqlQuery.c_str());
00202 rset = stmt->executeQuery();
00203 while (rset->next()) {
00204 chamber.diskOrWheel=rset->getInt(1);
00205 chamber.layer=rset->getInt(2);
00206 chamber.sector=rset->getInt(3);
00207 chamber.subsector=rset->getString(4);
00208 if (chamber.subsector=="") chamber.subsector="0";
00209 chamber.chamberLocationName=rset->getString(5);
00210 chamber.febZOrnt=rset->getString(6);
00211 chamber.febZRadOrnt=rset->getString(7);
00212 if (chamber.febZRadOrnt=="") chamber.febZRadOrnt="N/A";
00213 chamber.barrelOrEndcap=rset->getString(8);
00214 }
00215 FebConnectorSpec febConnector(theFEB[iFEB].lbInputNum,chamber,febLocation);
00216
00217 sqlQuery = "SELECT CableChannelNum, ChamberStripNumber, CmsStripNumber";
00218 sqlQuery += " FROM ChamberStrip ";
00219 sqlQuery += " WHERE FC_FEBConnectorId= ";
00220 sqlQuery += IntToString(theFEB[iFEB].connectorId);
00221 sqlQuery += " ORDER BY CableChannelNum";
00222 stmt->setSQL(sqlQuery.c_str());
00223 rset = stmt->executeQuery();
00224 unsigned int iStripEntry=0;
00225 while (rset->next()) {
00226 ChamberStripSpec strip = {rset->getInt(1),rset->getInt(2),rset->getInt(3)};
00227 febConnector.add(strip);
00228 iStripEntry++;
00229 }
00230 lb.add(febConnector);
00231 }
00232 lc.add(lb);
00233 }
00234 tb.add(lc);
00235 }
00236 dcc.add(tb);
00237 }
00238 std::cout<<"--> Adding DCC"<<std::endl;
00239 cabling->add(dcc);
00240 }
00241 cout << endl <<"Building RPC Cabling done!" << flush << endl << endl;
00242 }
00243
00244 int popcon::RPCReadOutMappingSourceHandler::Compare2Cablings(const RPCReadOutMapping* map1, RPCReadOutMapping* map2) {
00245 vector<const DccSpec *> dccs1 = map1->dccList();
00246 vector<const DccSpec *> dccs2 = map2->dccList();
00247 if(dccs1.size()!=dccs2.size()) {
00248
00249 return 1;
00250 }
00251 pair<int,int> dccRange1 = map1->dccNumberRange();
00252 pair<int,int> dccRange2 = map2->dccNumberRange();
00253 if(dccRange1.first!=dccRange2.first) {
00254
00255 return 1;
00256 }
00257 if(dccRange1.second!=dccRange2.second) {
00258
00259 return 1;
00260 }
00261 typedef vector<const DccSpec *>::const_iterator IDCC;
00262 IDCC idcc2 = dccs2.begin();
00263 for (IDCC idcc1 = dccs1.begin(); idcc1 != dccs1.end(); idcc1++) {
00264 int dccNo = (**idcc1).id();
00265 std::string dccContents = (**idcc1).print(4);
00266 if ((**idcc2).id()!=dccNo) {
00267
00268 return 1;
00269 }
00270 if ((**idcc2).print(4)!=dccContents) {
00271
00272 return 1;
00273 }
00274 idcc2++;
00275 }
00276 return 0;
00277 }