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_dummy(ps.getUntrackedParameter<int>("WriteDummy",0)),
00009 m_validate(ps.getUntrackedParameter<int>("Validate",0)),
00010 m_disableCrates(ps.getUntrackedParameter<std::vector<int> >("DisabledCrates")),
00011 m_disableTowers(ps.getUntrackedParameter<std::vector<int> >("DisabledTowers")),
00012 m_connect(ps.getUntrackedParameter<std::string>("OnlineConn","")),
00013 m_authpath(ps.getUntrackedParameter<std::string>("OnlineAuthPath","."))
00014 {
00015 }
00016
00017 popcon::L1RPCHwConfigSourceHandler::~L1RPCHwConfigSourceHandler()
00018 {
00019 }
00020
00021 void popcon::L1RPCHwConfigSourceHandler::getNewObjects()
00022 {
00023
00024 std::cout << "L1RPCHwConfigSourceHandler: L1RPCHwConfigSourceHandler::getNewObjects begins\n";
00025 edm::Service<cond::service::PoolDBOutputService> mydbservice;
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 Ref payload;
00038
00039 if(m_validate==1) {
00040 std::cout<<" Validation was requested, so will check present contents"<<std::endl;
00041 std::cout<<"Name of tag : "<<tagInfo().name << ", tag size : " << tagInfo().size
00042 << ", last object valid since "
00043 << tagInfo().lastInterval.first << std::endl;
00044 payload = lastPayload();
00045 } else {
00046 std::cout << "L1RPCHwConfigSourceHandler: no validation requested"<<std::endl;
00047 }
00048
00049
00050 disabledDevs = new L1RPCHwConfig();
00051 if (m_dummy==1) {
00052 std::vector<int>::iterator crIt = m_disableCrates.begin();
00053 for (; crIt!=m_disableCrates.end(); ++crIt){
00054 disabledDevs->enableCrate(*crIt,false);
00055 }
00056 std::vector<int>::iterator twIt = m_disableTowers.begin();
00057 for (; twIt!=m_disableTowers.end(); ++twIt){
00058 disabledDevs->enableTower(*twIt,false);
00059 }
00060 } else {
00061 ConnectOnlineDB(m_connect,m_authpath);
00062 readHwConfig1();
00063 DisconnectOnlineDB();
00064 }
00065
00066 cond::Time_t snc=mydbservice->currentTime();
00067
00068
00069 int difference=1;
00070 if (m_validate==1) difference=Compare2Configs(payload,disabledDevs);
00071 if (!difference) std::cout<<"No changes - will not write anything!!!"<<std::endl;
00072 if (difference==1) {
00073 std::cout<<"Will write new object to offline DB!!!"<<std::endl;
00074 m_to_transfer.push_back(std::make_pair((L1RPCHwConfig*)disabledDevs,snc+1));
00075 }
00076
00077 std::cout << "L1RPCHwConfigSourceHandler: L1RPCHwConfigSourceHandler::getNewObjects ends\n";
00078 }
00079
00080 void popcon::L1RPCHwConfigSourceHandler::ConnectOnlineDB(std::string connect, std::string authPath)
00081 {
00082 std::cout << "L1RPCHwConfigSourceHandler: connecting to " << connect << "..." << std::flush;
00083 connection = new cond::DbConnection() ;
00084
00085 connection->configuration().setAuthenticationPath( authPath ) ;
00086 connection->configure();
00087 session = new cond::DbSession(connection->createSession());
00088 session->open(connect,true) ;
00089 std::cout << "Done." << std::endl;
00090 }
00091
00092 void popcon::L1RPCHwConfigSourceHandler::DisconnectOnlineDB()
00093 {
00094 connection->close() ;
00095 delete connection ;
00096 session->close();
00097 delete session ;
00098 }
00099
00100 void popcon::L1RPCHwConfigSourceHandler::readHwConfig1()
00101 {
00102 session->transaction().start( true );
00103 coral::ISchema& schema = session->nominalSchema();
00104 std::string condition="";
00105 coral::AttributeList conditionData;
00106 std::cout << std::endl <<"L1RPCHwConfigSourceHandler: start to build L1RPC Hw Config..." << std::flush << std::endl << std::endl;
00107
00108
00109 coral::IQuery* query1 = schema.newQuery();
00110 query1->addToTableList( "CRATEDISABLED" );
00111 query1->addToTableList( "CRATE" );
00112 query1->addToTableList( "BOARD" );
00113 query1->addToTableList( "TRIGGERBOARD" );
00114 query1->addToOutputList("TRIGGERBOARD.TOWERTO","TOWERTO");
00115 query1->addToOutputList("TRIGGERBOARD.TOWERFROM","TOWERFROM");
00116 query1->addToOutputList("TRIGGERBOARD.SECTOR","SECTOR");
00117 query1->addToOrderList( "TOWERTO" );
00118 query1->addToOrderList( "SECTOR" );
00119 condition = "CRATE.CRATEID=CRATEDISABLED.CRATE_CRATEID AND BOARD.CRATE_CRATEID=CRATE.CRATEID AND BOARD.BOARDID=TRIGGERBOARD.TRIGGERBOARDID AND CRATE.TYPE='TRIGGERCRATE'";
00120 query1->setCondition( condition, conditionData );
00121 coral::ICursor& cursor1 = query1->execute();
00122 while ( cursor1.next() ) {
00123
00124 const coral::AttributeList& row = cursor1.currentRow();
00125 int sector = atoi((row["SECTOR"].data<std::string>()).c_str());
00126 int first = atoi((row["TOWERTO"].data<std::string>()).c_str());
00127 int last = atoi((row["TOWERFROM"].data<std::string>()).c_str());
00128 for (int iTower=first; iTower<=last; iTower++) {
00129 for (int jSegment=0; jSegment<12; jSegment++) {
00130 disabledDevs->enablePAC(iTower,sector,jSegment,false);
00131 }
00132 }
00133 }
00134 delete query1;
00135
00136
00137 coral::IQuery* query2 = schema.newQuery();
00138 query2->addToTableList( "BOARDDISABLED" );
00139 query2->addToTableList( "BOARD" );
00140 query2->addToTableList( "TRIGGERBOARD" );
00141 query2->addToOutputList("TRIGGERBOARD.TOWERTO","TOWERTO");
00142 query2->addToOutputList("TRIGGERBOARD.TOWERFROM","TOWERFROM");
00143 query2->addToOutputList("TRIGGERBOARD.SECTOR","SECTOR");
00144 query2->addToOrderList( "TOWERTO" );
00145 query2->addToOrderList( "SECTOR" );
00146 condition = "BOARD.BOARDID=BOARDDISABLED.BOARD_BOARDID AND BOARD.BOARDID=TRIGGERBOARD.TRIGGERBOARDID";
00147 query2->setCondition( condition, conditionData );
00148 coral::ICursor& cursor2 = query2->execute();
00149 while ( cursor2.next() ) {
00150
00151 const coral::AttributeList& row = cursor2.currentRow();
00152 int sector = atoi((row["SECTOR"].data<std::string>()).c_str());
00153 int first = atoi((row["TOWERTO"].data<std::string>()).c_str());
00154 int last = atoi((row["TOWERFROM"].data<std::string>()).c_str());
00155 for (int iTower=first; iTower<=last; iTower++) {
00156 for (int jSegment=0; jSegment<12; jSegment++) {
00157 disabledDevs->enablePAC(iTower,sector,jSegment,false);
00158 }
00159 }
00160 }
00161 delete query2;
00162
00163
00164
00165
00166
00167
00168
00169
00170
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 coral::IQuery* query4 = schema.newQuery();
00197 query4->addToTableList( "CHIPDISABLED" );
00198 query4->addToTableList( "CHIP" );
00199 query4->addToTableList( "BOARD" );
00200 query4->addToTableList( "TRIGGERBOARD" );
00201 query4->addToOutputList("TRIGGERBOARD.TOWERTO","TOWERTO");
00202 query4->addToOutputList("TRIGGERBOARD.TOWERFROM","TOWERFROM");
00203 query4->addToOutputList("TRIGGERBOARD.SECTOR","SECTOR");
00204 query4->addToOutputList("CHIP.POSITION","POSITION");
00205 query4->addToOrderList( "TOWERTO" );
00206 query4->addToOrderList( "SECTOR" );
00207 query4->addToOrderList( "POSITION" );
00208 condition = "CHIP.CHIPID=CHIPDISABLED.CHIP_CHIPID AND CHIP.BOARD_BOARDID=BOARD.BOARDID AND BOARD.BOARDID=TRIGGERBOARD.TRIGGERBOARDID AND CHIP.TYPE='PAC'";
00209 query4->setCondition( condition, conditionData );
00210 coral::ICursor& cursor4 = query4->execute();
00211 while ( cursor4.next() ) {
00212
00213 const coral::AttributeList& row = cursor4.currentRow();
00214 int sector = atoi((row["SECTOR"].data<std::string>()).c_str());
00215 int first = atoi((row["TOWERTO"].data<std::string>()).c_str());
00216 int last = atoi((row["TOWERFROM"].data<std::string>()).c_str());
00217 int chipPos=row["POSITION"].data<short>();
00218 int tower=first+chipPos-8;
00219 if (tower<=last) {
00220 for (int jSegment=0; jSegment<12; jSegment++) {
00221 disabledDevs->enablePAC(tower,sector,jSegment,false);
00222 }
00223 }
00224 }
00225 delete query4;
00226
00227 }
00228
00229 int popcon::L1RPCHwConfigSourceHandler::Compare2Configs(Ref set1, L1RPCHwConfig* set2)
00230 {
00231 std::cout<<"Size of new object is : "<<std::flush;
00232 std::cout<<set2->size()<<std::endl;
00233 std::cout<<"Size of ref object is : "<<std::flush;
00234 std::cout<<set1->size()<<std::endl;
00235
00236 if (set1->size() != set2->size()) {
00237 std::cout<<" Number of disabled devices changed "<<std::endl;
00238 return 1;
00239 }
00240 for (int tower=-16; tower<17; tower++) {
00241 for (int sector=0; sector<12; sector++) {
00242 for (int segment=0; segment<12; segment++)
00243 if (set1->isActive(tower,sector,segment) != set2->isActive(tower,sector,segment)) {
00244 std::cout<<" Configuration changed for tower "<<tower<<", sector "<<sector<<", segment "<<segment<<std::endl;
00245 return 1;
00246 }
00247 }
00248 }
00249 return 0;
00250 }