CMS 3D CMS Logo

L1RPCHwConfigSourceHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/RPC/interface/L1RPCHwConfigSourceHandler.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::L1RPCHwConfigSourceHandler::L1RPCHwConfigSourceHandler(const edm::ParameterSet& ps):
00007   m_name(ps.getUntrackedParameter<std::string>("name","L1RPCHwConfigSourceHandler")),
00008   m_validate(ps.getUntrackedParameter<int>("Validate",0)),
00009   m_connect(ps.getUntrackedParameter<std::string>("OnlineConn","")),
00010   m_authpath(ps.getUntrackedParameter<std::string>("OnlineAuthPath",".")),
00011   m_host(ps.getUntrackedParameter<std::string>("OnlineDBHost","oracms.cern.ch")),
00012   m_sid(ps.getUntrackedParameter<std::string>("OnlineDBSID","omds")),
00013   m_user(ps.getUntrackedParameter<std::string>("OnlineDBUser","RPC_CONFIGURATION")),
00014   m_pass(ps.getUntrackedParameter<std::string>("OnlineDBPass","****")),
00015   m_port(ps.getUntrackedParameter<int>("OnlineDBPort",10121))
00016 {
00017 }
00018 
00019 popcon::L1RPCHwConfigSourceHandler::~L1RPCHwConfigSourceHandler()
00020 {
00021 }
00022 
00023 void popcon::L1RPCHwConfigSourceHandler::getNewObjects()
00024 {
00025 
00026   std::cout << "L1RPCHwConfigSourceHandler: L1RPCHwConfigSourceHandler::getNewObjects begins\n";
00027   edm::Service<cond::service::PoolDBOutputService> mydbservice;
00028 
00029 //  std::cerr << "------- " << m_name 
00030 //           << " - > getNewObjects" << std::endl;
00031 //  std::cerr<<"Got offlineInfo, tag: "<<std::endl;
00032 //  std::cerr << tagInfo().name << " , last object valid from " 
00033 //          << tagInfo().lastInterval.first << " to "
00034 //            << tagInfo().lastInterval.second << " , token is "
00035 //            << tagInfo().token << " and this is the payload "
00036 //            << tagInfo().lastPayloadToken << std::endl;
00037 
00038 // first check what is already there in offline DB
00039   Ref payload;
00040 
00041   if(m_validate==1) {
00042     std::cout<<" Validation was requested, so will check present contents"<<std::endl;
00043     std::cout<<"Name of tag : "<<tagInfo().name << ", tag size : " << tagInfo().size
00044             << ", last object valid since "
00045             << tagInfo().lastInterval.first << std::endl;
00046     payload = lastPayload();
00047   } else {
00048     std::cout << "L1RPCHwConfigSourceHandler: no validation requested"<<std::endl;
00049   }
00050 
00051 // now construct new object from online DB
00052         if (m_connect=="") {
00053           ConnectOnlineDB(m_host,m_sid,m_user,m_pass,m_port);
00054           readHwConfig0();
00055         } else {
00056           ConnectOnlineDB(m_connect,m_authpath);
00057           readHwConfig1();
00058         }
00059         DisconnectOnlineDB();
00060 
00061         cond::Time_t snc=mydbservice->currentTime();
00062 
00063 // look for recent changes
00064         int difference=1;
00065         if (m_validate==1) difference=Compare2Configs(payload,disabledDevs);
00066         if (!difference) cout<<"No changes - will not write anything!!!"<<endl;
00067         if (difference==1) {
00068           cout<<"Will write new object to offline DB!!!"<<endl;
00069           m_to_transfer.push_back(std::make_pair((L1RPCHwConfig*)disabledDevs,snc));
00070         }
00071 
00072         std::cout << "L1RPCHwConfigSourceHandler: L1RPCHwConfigSourceHandler::getNewObjects ends\n";
00073 }
00074 
00075 void popcon::L1RPCHwConfigSourceHandler::ConnectOnlineDB(string host, string sid, string user, string pass, int port=10121)
00076 {
00077   stringstream ss;
00078   ss << "//" << host << ":" << port << "/" << sid;
00079   cout << "L1RPCHwConfigSourceHandler: connecting to " << host << "..." << flush;
00080   env = Environment::createEnvironment(Environment::OBJECT);
00081   conn = env->createConnection(user, pass, ss.str());
00082   cout << "Done." << endl;
00083 }
00084 
00085 void popcon::L1RPCHwConfigSourceHandler::ConnectOnlineDB(string connect, string authPath)
00086 {
00087   cout << "L1RPCHwConfigSourceHandler: connecting to " << connect << "..." << flush;
00088   session = new cond::DBSession();
00089   session->configuration().setAuthenticationMethod(cond::XML);
00090   session->configuration().setAuthenticationPath( authPath ) ;
00091   session->open() ;
00092   connection = new cond::Connection( connect ) ;
00093   connection->connect( session ) ;
00094   coralTr = & (connection->coralTransaction()) ;
00095   cout << "Done." << endl;
00096 }
00097 
00098 void popcon::L1RPCHwConfigSourceHandler::DisconnectOnlineDB()
00099 {
00100   if (m_connect=="") {
00101     env->terminateConnection(conn);
00102     Environment::terminateEnvironment(env);
00103   } else {
00104     connection->disconnect() ;
00105     delete connection ;
00106     delete session ;
00107   }
00108 }
00109 
00110 void popcon::L1RPCHwConfigSourceHandler::readHwConfig0()
00111 {
00112   Statement* stmt = conn->createStatement();
00113   string sqlQuery ="";
00114   cout << endl <<"L1RPCHwConfigSourceHandler: start to build L1RPC Hw Config..." << flush << endl << endl;
00115   disabledDevs =  new L1RPCHwConfig();
00116 
00117 // get disabled crates and translate into towers/sectors/segments
00118   sqlQuery = "select tb.towerto, tb.towerfrom, tb.sector ";
00119   sqlQuery += "from CRATEDISABLED cd, CRATE c, BOARD b, TRIGGERBOARD tb "; 
00120   sqlQuery += "where c.crateid=cd.crate_crateid and b.crate_crateid=c.crateid and b.boardid=tb.triggerboardid and c.type='TRIGGERCRATE' ";
00121   sqlQuery += "order by tb.towerto, tb.sector ";
00122   stmt->setSQL(sqlQuery.c_str());
00123   ResultSet* rset = stmt->executeQuery();
00124   while (rset->next()) {
00125 //    std::cout<<"  found board on disabled crate..."<<std::endl;
00126     int sector=atoi((rset->getString(3)).c_str());
00127     int first=atoi((rset->getString(1)).c_str());
00128     int last=atoi((rset->getString(2)).c_str());
00129     for (int iTower=first; iTower<=last; iTower++) {
00130       for (int jSegment=0; jSegment<12; jSegment++) {
00131         disabledDevs->enablePAC(iTower,sector,jSegment,false);
00132       }
00133     }
00134   }
00135 
00136 // get disabled triggerboards and translate into towers/sectors/segments
00137   sqlQuery = "select tb.towerto, tb.towerfrom, tb.sector ";
00138   sqlQuery += "from BOARDDISABLED bd, BOARD b, TRIGGERBOARD tb ";
00139   sqlQuery += "where b.boardid=bd.board_boardid and b.boardid=tb.triggerboardid ";
00140   sqlQuery += "order by tb.towerto, tb.sector ";
00141   stmt->setSQL(sqlQuery.c_str());
00142   rset = stmt->executeQuery();
00143   while (rset->next()) {
00144 //    std::cout<<"  found disabled board..."<<std::endl;
00145     int sector=atoi((rset->getString(3)).c_str());
00146     int first=atoi((rset->getString(1)).c_str());
00147     int last=atoi((rset->getString(2)).c_str());
00148     for (int iTower=first; iTower<=last; iTower++) {
00149       for (int jSegment=0; jSegment<12; jSegment++) {
00150         disabledDevs->enablePAC(iTower,sector,jSegment,false);
00151       }
00152     }
00153   }
00154 
00155 // get disabled links - this is not usable here
00156 /*
00157   sqlQuery = "select tb.towerto, tb.towerfrom, tb.sector, l.triggerboardinputnum ";
00158   sqlQuery += "from LINKDISABLED ld, LINKCONN l, TRIGGERBOARD tb ";
00159   sqlQuery += " where ld.link_linkconnid=l.linkconnid and l.tb_triggerboardid=tb.triggerboardid ";
00160   sqlQuery += "order by tb.towerto, tb.sector, l.triggerboardinputnum ";
00161   stmt->setSQL(sqlQuery.c_str());
00162   rset = stmt->executeQuery();
00163   while (rset->next()) {
00164     int sector=atoi((rset->getString(3)).c_str());
00165     int first=atoi((rset->getString(1)).c_str());
00166     int last=atoi((rset->getString(2)).c_str());
00167     for (int iTower=first; iTower<=last; iTower++) {
00168       for (int jSegment=0; jSegment<12; jSegment++) {
00169         disabledDevs->enablePAC(iTower,sector,jSegment,false);
00170       }
00171     }
00172   } */
00173 
00174 // get disabled chips and translate into towers/sectors
00175 // for the moment assume that chip position 8 corresponds to lowest tower number
00176 // and so on, ignoring bogus chip at position 11 if given TB operates 3 towers.
00177   sqlQuery = "select tb.towerto, tb.towerfrom, tb.sector, c.position ";
00178   sqlQuery += "from CHIPDISABLED cd, CHIP c, BOARD b, TRIGGERBOARD tb ";
00179   sqlQuery += " where cd.chip_chipid=c.chipid and c.board_boardid=b.boardid and b.boardid=tb.triggerboardid and c.type='PAC' ";
00180   sqlQuery += "order by tb.towerto, tb.sector, c.position ";
00181   stmt->setSQL(sqlQuery.c_str());
00182   rset = stmt->executeQuery();
00183   while (rset->next()) {
00184 //    std::cout<<"  found disabled chip, not sure what to do with it..."<<std::endl;
00185     int sector=atoi((rset->getString(3)).c_str());
00186     int first=atoi((rset->getString(1)).c_str());
00187     int last=atoi((rset->getString(2)).c_str());
00188     int chipPos=rset->getInt(4);
00189     int tower=first+chipPos-8;
00190     if (tower<=last) {
00191       for (int jSegment=0; jSegment<12; jSegment++) {
00192         disabledDevs->enablePAC(tower,sector,jSegment,false);
00193       } 
00194     }
00195   }
00196 }
00197     
00198 void popcon::L1RPCHwConfigSourceHandler::readHwConfig1()
00199 {
00200   coralTr->start( true );
00201   coral::ISchema& schema = coralTr->nominalSchema();
00202   std::string condition="";
00203   coral::AttributeList conditionData;
00204   cout << endl <<"L1RPCHwConfigSourceHandler: start to build L1RPC Hw Config..." << flush << endl << endl;
00205 
00206   disabledDevs =  new L1RPCHwConfig();
00207 
00208 // get disabled crates and translate into towers/sectors/segments
00209   coral::IQuery* query1 = schema.newQuery();
00210   query1->addToTableList( "CRATEDISABLED" );
00211   query1->addToTableList( "CRATE" );
00212   query1->addToTableList( "BOARD" );
00213   query1->addToTableList( "TRIGGERBOARD" );
00214   query1->addToOutputList("TRIGGERBOARD.TOWERTO","TOWERTO");
00215   query1->addToOutputList("TRIGGERBOARD.TOWERFROM","TOWERFROM");
00216   query1->addToOutputList("TRIGGERBOARD.SECTOR","SECTOR");
00217   query1->addToOrderList( "TOWERTO" );
00218   query1->addToOrderList( "SECTOR" );
00219   condition = "CRATE.CRATEID=CRATEDISABLED.CRATE_CRATEID AND BOARD.CRATE_CRATEID=CRATE.CRATEID AND BOARD.BOARDID=TRIGGERBOARD.TRIGGERBOARDID AND CRATE.TYPE='TRIGGERCRATE'";
00220   query1->setCondition( condition, conditionData );
00221   coral::ICursor& cursor1 = query1->execute();
00222   while ( cursor1.next() ) {
00223 //    cursor1.currentRow().toOutputStream( std::cout ) << std::endl;
00224     const coral::AttributeList& row = cursor1.currentRow();
00225     int sector = atoi((row["SECTOR"].data<std::string>()).c_str());
00226     int first = atoi((row["TOWERTO"].data<std::string>()).c_str());
00227     int last = atoi((row["TOWERFROM"].data<std::string>()).c_str());
00228     for (int iTower=first; iTower<=last; iTower++) {
00229       for (int jSegment=0; jSegment<12; jSegment++) {
00230         disabledDevs->enablePAC(iTower,sector,jSegment,false);
00231       }
00232     }
00233   }
00234   delete query1;
00235 
00236 // get disabled triggerboards and translate into towers/sectors/segments
00237   coral::IQuery* query2 = schema.newQuery();
00238   query2->addToTableList( "BOARDDISABLED" );
00239   query2->addToTableList( "BOARD" );
00240   query2->addToTableList( "TRIGGERBOARD" );
00241   query2->addToOutputList("TRIGGERBOARD.TOWERTO","TOWERTO");
00242   query2->addToOutputList("TRIGGERBOARD.TOWERFROM","TOWERFROM");
00243   query2->addToOutputList("TRIGGERBOARD.SECTOR","SECTOR");
00244   query2->addToOrderList( "TOWERTO" );
00245   query2->addToOrderList( "SECTOR" );
00246   condition = "BOARD.BOARDID=BOARDDISABLED.BOARD_BOARDID AND BOARD.BOARDID=TRIGGERBOARD.TRIGGERBOARDID";
00247   query2->setCondition( condition, conditionData );
00248   coral::ICursor& cursor2 = query2->execute();
00249   while ( cursor2.next() ) {
00250 //    cursor2.currentRow().toOutputStream( std::cout ) << std::endl;
00251     const coral::AttributeList& row = cursor2.currentRow();
00252     int sector = atoi((row["SECTOR"].data<std::string>()).c_str());
00253     int first = atoi((row["TOWERTO"].data<std::string>()).c_str());
00254     int last = atoi((row["TOWERFROM"].data<std::string>()).c_str());
00255     for (int iTower=first; iTower<=last; iTower++) {
00256       for (int jSegment=0; jSegment<12; jSegment++) {
00257         disabledDevs->enablePAC(iTower,sector,jSegment,false);
00258       }
00259     }
00260   }
00261   delete query2;
00262 
00263 // get disabled links - this is not usable here
00264 /*
00265   coral::IQuery* query3 = schema.newQuery();
00266   query3->addToTableList( "LINKDISABLED" );
00267   query3->addToTableList( "LINKCONN" );
00268   query3->addToTableList( "TRIGGERBOARD" );
00269   query3->addToOutputList("TRIGGERBOARD.TOWERTO","TOWERTO");
00270   query3->addToOutputList("TRIGGERBOARD.TOWERFROM","TOWERFROM");
00271   query3->addToOutputList("TRIGGERBOARD.SECTOR","SECTOR");
00272   query3->addToOutputList("LINKCONN.TRIGGERBOARDINPUTNUM","TBINPUTNUM");
00273   query3->addToOrderList( "TOWERTO" );
00274   query3->addToOrderList( "SECTOR" );
00275   query3->addToOrderList( "TBINPUTNUM" );
00276   condition = "LINKCONN.LINKCONNID=LINKDISABLED.LINK_LINKCONNID AND LINKCONN.TB_TRIGGERBOARDID=TRIGGERBOARD.TRIGGERBOARDID";
00277   query3->setCondition( condition, conditionData );
00278   coral::ICursor& cursor3 = query3->execute();
00279   while ( cursor3.next() ) {
00280 //    cursor3.currentRow().toOutputStream( std::cout ) << std::endl;
00281     const coral::AttributeList& row = cursor3.currentRow();
00282     int sector = atoi((row["SECTOR"].data<std::string>()).c_str());
00283     int first = atoi((row["TOWERTO"].data<std::string>()).c_str());
00284     int last = atoi((row["TOWERFROM"].data<std::string>()).c_str());
00285     for (int iTower=first; iTower<=last; iTower++) {
00286       for (int jSegment=0; jSegment<12; jSegment++) {
00287         disabledDevs->enablePAC(iTower,sector,jSegment,false);
00288       }
00289     }
00290   }
00291   delete query3;*/
00292 
00293 // get disabled chips and translate into towers/sectors
00294 // for the moment assume that chip position 8 corresponds to lowest tower number
00295 // and so on, ignoring bogus chip at position 11 if given TB operates 3 towers.
00296   coral::IQuery* query4 = schema.newQuery();
00297   query4->addToTableList( "CHIPDISABLED" );
00298   query4->addToTableList( "CHIP" );
00299   query4->addToTableList( "BOARD" );
00300   query4->addToTableList( "TRIGGERBOARD" );
00301   query4->addToOutputList("TRIGGERBOARD.TOWERTO","TOWERTO");
00302   query4->addToOutputList("TRIGGERBOARD.TOWERFROM","TOWERFROM");
00303   query4->addToOutputList("TRIGGERBOARD.SECTOR","SECTOR");
00304   query4->addToOutputList("CHIP.POSITION","POSITION");
00305   query4->addToOrderList( "TOWERTO" );
00306   query4->addToOrderList( "SECTOR" );
00307   query4->addToOrderList( "POSITION" );
00308   condition = "CHIP.CHIPID=CHIPDISABLED.CHIP_CHIPID AND CHIP.BOARD_BOARDID=BOARD.BOARDID AND BOARD.BOARDID=TRIGGERBOARD.TRIGGERBOARDID AND CHIP.TYPE='PAC'";
00309   query4->setCondition( condition, conditionData );
00310   coral::ICursor& cursor4 = query4->execute();
00311   while ( cursor4.next() ) {
00312 //    cursor4.currentRow().toOutputStream( std::cout ) << std::endl;
00313     const coral::AttributeList& row = cursor4.currentRow();
00314     int sector = atoi((row["SECTOR"].data<std::string>()).c_str());
00315     int first = atoi((row["TOWERTO"].data<std::string>()).c_str());
00316     int last = atoi((row["TOWERFROM"].data<std::string>()).c_str());
00317     int chipPos=row["POSITION"].data<short>();
00318     int tower=first+chipPos-8;
00319     if (tower<=last) {
00320       for (int jSegment=0; jSegment<12; jSegment++) {
00321         disabledDevs->enablePAC(tower,sector,jSegment,false);
00322       }
00323     }
00324   }
00325   delete query4;
00326 
00327   coralTr->commit();
00328 }
00329 
00330 int popcon::L1RPCHwConfigSourceHandler::Compare2Configs(Ref set1, L1RPCHwConfig* set2)
00331 {
00332   std::cout<<"Size of new object is : "<<flush;
00333   std::cout<<set2->size()<<std::endl;
00334   std::cout<<"Size of ref object is : "<<flush;
00335   std::cout<<set1->size()<<std::endl;
00336 
00337   if (set1->size() != set2->size()) {
00338     std::cout<<" Number of disabled devices changed "<<std::endl;
00339     return 1;
00340   }
00341   for (int tower=-16; tower<17; tower++) {
00342     for (int sector=0; sector<12; sector++) {
00343       for (int segment=0; segment<12; segment++)
00344       if (set1->isActive(tower,sector,segment) != set2->isActive(tower,sector,segment)) {
00345         std::cout<<" Configuration changed for tower "<<tower<<", sector "<<sector<<", segment "<<segment<<std::endl;
00346         return 1;
00347       }
00348     }
00349   }
00350   return 0;
00351 }

Generated on Tue Jun 9 17:26:56 2009 for CMSSW by  doxygen 1.5.4