00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "CondTools/DT/interface/DTCCBConfigHandler.h"
00014
00015
00016
00017
00018 #include "CondTools/DT/interface/DTConfigHandler.h"
00019 #include "CondTools/DT/interface/DTDBSession.h"
00020 #include "CondFormats/DTObjects/interface/DTCCBConfig.h"
00021 #include "CondFormats/DTObjects/interface/DTConfigList.h"
00022 #include "CondCore/DBCommon/interface/AuthenticationMethod.h"
00023 #include "CondCore/DBCommon/interface/DBSession.h"
00024 #include "CondCore/DBCommon/interface/Connection.h"
00025 #include "CondCore/DBCommon/interface/CoralTransaction.h"
00026 #include "CondCore/DBCommon/interface/SessionConfiguration.h"
00027 #include "RelationalAccess/ISessionProxy.h"
00028 #include "RelationalAccess/ISchema.h"
00029 #include "RelationalAccess/ITable.h"
00030 #include "RelationalAccess/ICursor.h"
00031 #include "RelationalAccess/IQuery.h"
00032 #include "CoralBase/AttributeList.h"
00033 #include "CoralBase/AttributeSpecification.h"
00034 #include "CoralBase/Attribute.h"
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 DTCCBConfigHandler::DTCCBConfigHandler( const edm::ParameterSet& ps ):
00050 dataTag( ps.getParameter<std::string> ( "tag" ) ),
00051 onlineConnect( ps.getParameter<std::string> ( "onlineDB" ) ),
00052 onlineAuthentication( ps.getParameter<std::string> (
00053 "onlineAuthentication" ) ),
00054 offlineAuthentication( ps.getParameter<edm::ParameterSet>( "DBParameters" )
00055 .getUntrackedParameter<std::string> (
00056 "authenticationPath" ) ),
00057
00058 listToken( ps.getParameter<std::string> ( "token" ) ) {
00059 std::cout << onlineAuthentication << " "
00060 << offlineAuthentication << std::endl;
00061 }
00062
00063
00064
00065
00066 DTCCBConfigHandler::~DTCCBConfigHandler() {
00067 }
00068
00069
00070
00071
00072 void DTCCBConfigHandler::getNewObjects() {
00073
00074 cond::DBSession* coralSession;
00075 cond::Connection* m_connection;
00076 cond::CoralTransaction* m_coraldb;
00077
00078 coralSession = new cond::DBSession();
00079
00080 coralSession->configuration().setAuthenticationMethod( cond::XML );
00081 coralSession->configuration().setAuthenticationPath( onlineAuthentication );
00082
00083 coralSession->configuration().setMessageLevel( cond::Error );
00084
00085 coralSession->open();
00086
00087 m_connection = new cond::Connection( onlineConnect );
00088 m_connection->connect( coralSession );
00089 m_coraldb = &( m_connection->coralTransaction() );
00090 m_coraldb->start( true );
00091
00092 std::cout << "get session proxy... " << std::endl;
00093 isession = &( m_coraldb->coralSessionProxy() );
00094 std::cout << "session proxy got" << std::endl;
00095 m_coraldb->start( true );
00096
00097
00098
00099
00100
00101
00102 chkConfigList();
00103 std::cout << "get run config..." << std::endl;
00104
00105
00106 cond::TagInfo const & ti = tagInfo();
00107 unsigned int last = ti.lastInterval.first;
00108 std::cout << "last: " << last << std::endl;
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 coral::AttributeList emptyBindVariableList;
00132
00133 unsigned lastRun = last;
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 std::cout << "LAST RUN: " << lastRun << std::endl;
00145 std::map<int,int> cfgMap;
00146
00147 std::cout << "get table handle... " << std::endl;
00148 coral::ITable& runConfigTable =
00149 isession->nominalSchema().tableHandle( "TBRUN" );
00150 std::cout << "table handle got " << std::endl;
00151 std::auto_ptr<coral::IQuery>
00152 runConfigQuery( runConfigTable.newQuery() );
00153 runConfigQuery->addToOutputList( "RUN" );
00154 runConfigQuery->addToOutputList( "CONFKEY" );
00155
00156 coral::ICursor& runConfigCursor = runConfigQuery->execute();
00157 while( runConfigCursor.next() ) {
00158 const coral::AttributeList& row = runConfigCursor.currentRow();
00159 int runConfigId = row["CONFKEY"].data<int>();
00160 int runId = row[ "RUN"].data<int>();
00161 if ( static_cast<unsigned>( runId ) > lastRun )
00162 cfgMap.insert( std::pair<int,int>( runId,
00163 runConfigId ) );
00164 }
00165 std::map<int,int>::const_iterator runIter = cfgMap.begin();
00166 std::map<int,int>::const_iterator runIend = cfgMap.end();
00167 char confKeyString[20];
00168 char brickIdString[20];
00169 while ( runIter != runIend ) {
00170 const std::pair<int,int>& runEntry = *runIter++;
00171 std::cout << "run " << runEntry.first
00172 << " ---> config " << runEntry.second << std::endl;
00173 DTCCBConfig* fullConf = new DTCCBConfig( dataTag );
00174 fullConf->setFullKey( runEntry.second );
00175 fullConf->setStamp( runEntry.first );
00176 std::vector< std::pair<DTCCBId,int> > ccbMap;
00177 std::auto_ptr<coral::IQuery>
00178 ccbDataQuery( isession->nominalSchema().newQuery() );
00179 ccbDataQuery->addToTableList( "CCBRELATIONS" );
00180 ccbDataQuery->addToTableList( "CCBMAP" );
00181 ccbDataQuery->addToOutputList( "CCBMAP.WHEEL" );
00182 ccbDataQuery->addToOutputList( "CCBMAP.SECTOR" );
00183 ccbDataQuery->addToOutputList( "CCBMAP.STATION" );
00184 ccbDataQuery->addToOutputList( "CCBRELATIONS.CONFCCBKEY" );
00185 sprintf( confKeyString, "%i", runEntry.second );
00186 std::string
00187 ccbDataCondition = "CCBRELATIONS.CCBID = CCBMAP.CCBID";
00188 ccbDataCondition += " and CCBRELATIONS.CONFKEY = ";
00189 ccbDataCondition += confKeyString;
00190 ccbDataQuery->setCondition( ccbDataCondition, emptyBindVariableList );
00191 std::cout << ccbDataCondition << std::endl;
00192 coral::ICursor& ccbDataCursor = ccbDataQuery->execute();
00193 while( ccbDataCursor.next() ) {
00194 const coral::AttributeList& row = ccbDataCursor.currentRow();
00195 int wheel = row["CCBMAP.WHEEL" ].data<int>();
00196 int sector = row["CCBMAP.SECTOR" ].data<int>();
00197 int station = row["CCBMAP.STATION"].data<int>();
00198 int ccbConf = row["CCBRELATIONS.CONFCCBKEY"].data<int>();
00199 DTCCBId ccbId;
00200 ccbId. wheelId = wheel;
00201 ccbId.stationId = station;
00202 ccbId. sectorId = sector;
00203 ccbMap.push_back( std::pair<DTCCBId,int>( ccbId, ccbConf ) );
00204 }
00205
00206 std::vector< std::pair<DTCCBId,int> >::const_iterator ccbIter
00207 = ccbMap.begin();
00208 std::vector< std::pair<DTCCBId,int> >::const_iterator ccbIend
00209 = ccbMap.end();
00210 while ( ccbIter != ccbIend ) {
00211 std::pair<DTCCBId,int> ccbEntry = *ccbIter++;
00212 DTCCBId& ccbId = ccbEntry.first;
00213 int ccbConfId = ccbEntry.second;
00214 int wheel = ccbId. wheelId;
00215 int station = ccbId.stationId;
00216 int sector = ccbId. sectorId;
00217 std::vector<int> ccbConf;
00218 coral::ITable& confBrickTable =
00219 isession->nominalSchema().tableHandle( "CFG2BRKREL" );
00220 std::auto_ptr<coral::IQuery>
00221 confBrickQuery( confBrickTable.newQuery() );
00222 confBrickQuery->addToOutputList( "BRKID" );
00223 sprintf( brickIdString, "%i", ccbConfId );
00224 std::string
00225 confBrickCondition = "CONFID = ";
00226 confBrickCondition += brickIdString;
00227 std::cout << confBrickCondition << std::endl;
00228 confBrickQuery->setCondition( confBrickCondition,
00229 emptyBindVariableList );
00230 coral::ICursor& confBrickCursor = confBrickQuery->execute();
00231 while( confBrickCursor.next() ) {
00232 const coral::AttributeList& row = confBrickCursor.currentRow();
00233 int confBrickId = row["BRKID"].data<int>();
00234 ccbConf.push_back( confBrickId );
00235 }
00236 fullConf->setConfigKey( wheel, station, sector, ccbConf );
00237 }
00238
00239
00240
00241
00242
00243
00244 cond::Time_t snc = runEntry.first;
00245 m_to_transfer.push_back( std::make_pair( fullConf, snc ) );
00246 }
00247
00248 delete m_connection;
00249 delete coralSession;
00250
00251 return;
00252 }
00253
00254
00255 void DTCCBConfigHandler::chkConfigList() {
00256 std::cout << "create session: "
00257 << offlineConnect << " , "
00258 << offlineCatalog << " , "
00259 << offlineAuthentication << std::endl;
00260
00261 DTDBSession* session = new DTDBSession( offlineConnect, offlineCatalog,
00262 offlineAuthentication );
00263 session->connect( false );
00264 const DTConfigList* rs;
00265 std::cout << "read full list: " << listToken << std::endl;
00266 DTConfigHandler* ri = DTConfigHandler::create( session, listToken );
00267 rs = ri->getContainer();
00268 DTConfigList::const_iterator iter = rs->begin();
00269 DTConfigList::const_iterator iend = rs->end();
00270 while ( iter != iend ) {
00271 int id = iter->first;
00272 std::cout << "brick " << id
00273 << " -> " << iter->second.ref << std::endl;
00274 std::cout << "========> " << std::endl;
00275 iter++;
00276 }
00277
00278 coral::ITable& fullConfigTable =
00279 isession->nominalSchema().tableHandle( "CONFIGSETS" );
00280 std::auto_ptr<coral::IQuery>
00281 fullConfigQuery( fullConfigTable.newQuery() );
00282 fullConfigQuery->addToOutputList( "CONFKEY" );
00283 fullConfigQuery->addToOutputList( "NAME" );
00284 coral::ICursor& fullConfigCursor = fullConfigQuery->execute();
00285 while( fullConfigCursor.next() ) {
00286 const coral::AttributeList& row = fullConfigCursor.currentRow();
00287 int fullConfigId = row["CONFKEY"].data<int>();
00288 std::string fullConfigName = row["NAME"].data<std::string>();
00289 std::cout << "config " << fullConfigId
00290 << " : " << fullConfigName << std::endl;
00291 }
00292
00293 std::cout << " ===============" << std::endl;
00294
00295 coral::AttributeList emptyBindVariableList;
00296 coral::ITable& brickConfigTable =
00297 isession->nominalSchema().tableHandle( "CFGBRICKS" );
00298 std::auto_ptr<coral::IQuery>
00299 brickConfigQuery( brickConfigTable.newQuery() );
00300 brickConfigQuery->addToOutputList( "BRKID" );
00301 brickConfigQuery->addToOutputList( "BRKNAME" );
00302 coral::ICursor& brickConfigCursor = brickConfigQuery->execute();
00303 DTConfigData* brickData = 0;
00304 char brickIdString[20];
00305 std::vector<int> missingList;
00306 while( brickConfigCursor.next() ) {
00307 const coral::AttributeList& row = brickConfigCursor.currentRow();
00308 int brickConfigId = row["BRKID"].data<int>();
00309 std::string brickConfigName = row["BRKNAME"].data<std::string>();
00310 std::cout << "brick " << brickConfigId
00311 << " : " << brickConfigName << std::endl;
00312 ri->get( brickConfigId, brickData );
00313 if ( brickData == 0 ) {
00314 std::cout << "brick missing, copy request" << std::endl;
00315 missingList.push_back( brickConfigId );
00316
00317 }
00318 }
00319 std::vector<int>::const_iterator brickIter = missingList.begin();
00320 std::vector<int>::const_iterator brickIend = missingList.end();
00321 while ( brickIter != brickIend ) {
00322 int brickConfigId = *brickIter++;
00323 std::auto_ptr<coral::IQuery>
00324 brickDataQuery( isession->nominalSchema().newQuery() );
00325 brickDataQuery->addToTableList( "CFGRELATIONS" );
00326 brickDataQuery->addToTableList( "CONFIGCMDS" );
00327 sprintf( brickIdString, "%i", brickConfigId );
00328 std::string
00329 brickCondition = "CONFIGCMDS.CMDID=CFGRELATIONS.CMDID";
00330 brickCondition += " and CFGRELATIONS.BRKID=";
00331 brickCondition += brickIdString;
00332 std::cout << "+++" << brickCondition << "+++" << std::endl;
00333 brickDataQuery->addToOutputList( "CFGRELATIONS.BRKID" );
00334 brickDataQuery->addToOutputList( "CONFIGCMDS.CONFDATA" );
00335 brickDataQuery->setCondition( brickCondition, emptyBindVariableList );
00336 coral::ICursor& brickDataCursor = brickDataQuery->execute();
00337 brickData = new DTConfigData();
00338 brickData->setId( brickConfigId );
00339 while( brickDataCursor.next() ) {
00340 const coral::AttributeList& row = brickDataCursor.currentRow();
00341 for ( coral::AttributeList::const_iterator iColumn = row.begin();
00342 iColumn != row.end(); ++iColumn ) {
00343 std::cout << iColumn->specification().name() << " : ";
00344 iColumn->toOutputStream( std::cout, false ) << "\t";
00345 std::cout << std::endl;
00346 if ( iColumn->specification().name() == "CONFIGCMDS.CONFDATA" )
00347 std::cout << "add : " << iColumn->data<std::string>()
00348 << std::endl;
00349 }
00350 std::cout << "add : "
00351 << row["CONFIGCMDS.CONFDATA"].data<std::string>()
00352 << std::endl;
00353 brickData->add( row["CONFIGCMDS.CONFDATA"].data<std::string>() );
00354 }
00355 cond::TypedRef<DTConfigData> brickRef( *session->poolDB(), brickData );
00356 brickRef.markWrite( "DTConfigData" );
00357 ri->set( brickConfigId, brickRef.token() );
00358 }
00359
00360 std::cout << "disconnect session..." << std::endl;
00361 session->disconnect();
00362 std::cout << "delete session..." << std::endl;
00363 delete session;
00364 std::cout << "list updated..." << std::endl;
00365
00366 return;
00367
00368 }
00369
00370
00371 std::string DTCCBConfigHandler::id() const {
00372 return dataTag;
00373 }
00374
00375