CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/CondTools/DT/src/DTUserKeyedConfigHandler.cc

Go to the documentation of this file.
00001 /*
00002  *  See header file for a description of this class.
00003  *
00004  *  $Date: 2012/02/15 16:04:16 $
00005  *  $Revision: 1.8 $
00006  *  \author Paolo Ronchese INFN Padova
00007  *
00008  */
00009 
00010 //-----------------------
00011 // This Class' Header --
00012 //-----------------------
00013 #include "CondTools/DT/interface/DTUserKeyedConfigHandler.h"
00014 
00015 //-------------------------------
00016 // Collaborating Class Headers --
00017 //-------------------------------
00018 #include "CondFormats/DTObjects/interface/DTCCBConfig.h"
00019 #include "CondFormats/DTObjects/interface/DTKeyedConfig.h"
00020 
00021 
00022 #include "CondCore/DBCommon/interface/DbTransaction.h"
00023 
00024 #include "FWCore/ServiceRegistry/interface/Service.h"
00025 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00026 #include "CondCore/DBOutputService/interface/KeyedElement.h"
00027 #include "CondCore/IOVService/interface/KeyList.h"
00028 
00029 #include "RelationalAccess/ISchema.h"
00030 #include "RelationalAccess/ITable.h"
00031 #include "RelationalAccess/ICursor.h"
00032 #include "RelationalAccess/IQuery.h"
00033 #include "CoralBase/AttributeList.h"
00034 #include "CoralBase/AttributeSpecification.h"
00035 #include "CoralBase/Attribute.h"
00036 
00037 //---------------
00038 // C++ Headers --
00039 //---------------
00040 
00041 
00042 //-------------------
00043 // Initializations --
00044 //-------------------
00045 cond::KeyList* DTUserKeyedConfigHandler::keyList = 0;
00046 
00047 //----------------
00048 // Constructors --
00049 //----------------
00050 DTUserKeyedConfigHandler::DTUserKeyedConfigHandler( const edm::ParameterSet& ps ):
00051  dataRun(               ps.getParameter<int>         ( "run" ) ),
00052  dataTag(               ps.getParameter<std::string> ( "tag" ) ),
00053  onlineConnect(         ps.getParameter<std::string> ( "onlineDB" ) ),
00054  onlineAuthentication(  ps.getParameter<std::string> ( 
00055                         "onlineAuthentication" ) ),
00056  brickContainer(        ps.getParameter<std::string> ( "container" ) ),
00057  writeKeys(             ps.getParameter<bool>        ( "writeKeys" ) ),
00058  writeData(             ps.getParameter<bool>        ( "writeData" ) ),
00059  connection(),
00060  isession() {
00061   std::cout << " PopCon application for DT configuration export "
00062             <<  onlineAuthentication << std::endl;
00063 
00064   std::vector<edm::ParameterSet> dtConfigKeys (
00065         ps.getParameter< std::vector<edm::ParameterSet> >( "DTConfigKeys" )
00066   );
00067   std::vector<edm::ParameterSet>::const_iterator iter = dtConfigKeys.begin();
00068   std::vector<edm::ParameterSet>::const_iterator iend = dtConfigKeys.end();
00069   while ( iter != iend ) {
00070     const edm::ParameterSet& cp = *iter++;
00071     int configType = cp.getUntrackedParameter<int>( "configType" );
00072     int configKey  = cp.getUntrackedParameter<int>( "configKey"  );
00073     std::cout << "config: "
00074               << configType << " -> "
00075               << configKey << std::endl;
00076     DTConfigKey userKey;
00077     userKey.confType = configType;
00078     userKey.confKey  = configKey;
00079     userConf.push_back( userKey );
00080   }
00081 }
00082 
00083 //--------------
00084 // Destructor --
00085 //--------------
00086 DTUserKeyedConfigHandler::~DTUserKeyedConfigHandler() {
00087 }
00088 
00089 //--------------
00090 // Operations --
00091 //--------------
00092 void DTUserKeyedConfigHandler::getNewObjects() {
00093 
00094   //to access the information on the tag inside the offline database:
00095   cond::TagInfo const & ti = tagInfo();
00096   unsigned int last = ti.lastInterval.first;
00097   std::cout << "last configuration key already copied for run: "
00098             << last << std::endl;
00099 
00100   std::vector<DTConfigKey> lastKey;
00101 //  std::cout << "check for last " << std::endl;
00102   if ( last == 0 ) {
00103     DTCCBConfig* dummyConf = new DTCCBConfig( dataTag );
00104     dummyConf->setStamp( 0 );
00105     dummyConf->setFullKey( lastKey );
00106     cond::Time_t snc = 1;
00107     if ( writeKeys && ( dataRun > 1 ) )
00108          m_to_transfer.push_back( std::make_pair( dummyConf, snc ) );
00109   }
00110   else {
00111     std::cout << "get last payload" << std::endl;
00112     Ref payload = lastPayload();
00113     std::cout << "get last full key" << std::endl;
00114     lastKey = payload->fullKey();
00115     std::cout << "last key: " << std::endl;
00116     std::vector<DTConfigKey>::const_iterator keyIter = lastKey.begin();
00117     std::vector<DTConfigKey>::const_iterator keyIend = lastKey.end();
00118     while ( keyIter != keyIend ) {
00119       const DTConfigKey& keyList = *keyIter++;
00120       std::cout << keyList.confType << " : " << keyList.confKey << std::endl;
00121     }
00122   }
00123 
00124   std::cout << "configure DbConnection" << std::endl;
00125   //  conn->configure( cond::CmsDefaults );
00126   connection.configuration().setAuthenticationPath( onlineAuthentication );
00127   connection.configure();
00128   std::cout << "create DbSession" << std::endl;
00129   isession = connection.createSession();
00130   std::cout << "open session" << std::endl;
00131   isession.open( onlineConnect );
00132   std::cout << "start transaction" << std::endl;
00133   isession.transaction().start();
00134   
00135   // get ccb identifiers map
00136   std::cout << "retrieve CCB map" << std::endl;
00137   std::map<int,DTCCBId> ccbMap;
00138   coral::ITable& ccbMapTable =
00139     isession.nominalSchema().tableHandle( "CCBMAP" );
00140   std::auto_ptr<coral::IQuery>
00141     ccbMapQuery( ccbMapTable.newQuery() );
00142   ccbMapQuery->addToOutputList( "CCBID" );
00143   ccbMapQuery->addToOutputList( "WHEEL" );
00144   ccbMapQuery->addToOutputList( "SECTOR" );
00145   ccbMapQuery->addToOutputList( "STATION" );
00146   coral::ICursor& ccbMapCursor = ccbMapQuery->execute();
00147   while( ccbMapCursor.next() ) {
00148     const coral::AttributeList& row = ccbMapCursor.currentRow();
00149     int ccb     = row["CCBID"  ].data<int>();
00150     int wheel   = row["WHEEL"  ].data<int>();
00151     int sector  = row["SECTOR" ].data<int>();
00152     int station = row["STATION"].data<int>();
00153     DTCCBId ccbId;
00154     ccbId.  wheelId =   wheel;
00155     ccbId.stationId = station;
00156     ccbId. sectorId =  sector;
00157     ccbMap.insert( std::pair<int,DTCCBId>( ccb, ccbId ) );
00158   }
00159 
00160   // get brick types
00161   std::cout << "retrieve brick types" << std::endl;
00162   std::map<int,int> bktMap;
00163   coral::AttributeList emptyBindVariableList;
00164   std::auto_ptr<coral::IQuery>
00165          brickTypeQuery( isession.nominalSchema().newQuery() );
00166   brickTypeQuery->addToTableList( "CFGBRICKS" );
00167   brickTypeQuery->addToTableList( "BRKT2CSETT" );
00168   std::string bTypeCondition = "CFGBRICKS.BRKTYPE=BRKT2CSETT.BRKTYPE";
00169   brickTypeQuery->addToOutputList( "CFGBRICKS.BRKID" );
00170   brickTypeQuery->addToOutputList( "BRKT2CSETT.CSETTYPE" );
00171   brickTypeQuery->setCondition( bTypeCondition, emptyBindVariableList );
00172   coral::ICursor& brickTypeCursor = brickTypeQuery->execute();
00173   while( brickTypeCursor.next() ) {
00174     const coral::AttributeList& row = brickTypeCursor.currentRow();
00175     int id = row["CFGBRICKS.BRKID"    ].data<int>();
00176     int bt = row["BRKT2CSETT.CSETTYPE"].data<short>();
00177 //    std::cout << "brick " << id << " type " << bt << std::endl;
00178 // @@FIX - TEMPORARY PATCH
00179 //    if ( bt > 3 ) bt = 3;
00180     bktMap.insert( std::pair<int,int>( id, bt ) );
00181   }
00182 
00183   // get ccb config keys
00184   std::cout << "retrieve CCB configuration keys" << std::endl;
00185   std::map<int,std::map<int,int>*> keyMap;
00186   std::map<int,int> cckMap;
00187   coral::ITable& ccbRelTable =
00188     isession.nominalSchema().tableHandle( "CCBRELATIONS" );
00189   std::auto_ptr<coral::IQuery>
00190     ccbRelQuery( ccbRelTable.newQuery() );
00191   ccbRelQuery->addToOutputList( "CONFKEY" );
00192   ccbRelQuery->addToOutputList( "CCBID" );
00193   ccbRelQuery->addToOutputList( "CONFCCBKEY" );
00194   coral::ICursor& ccbRelCursor = ccbRelQuery->execute();
00195   // loop over all full configurations
00196   while( ccbRelCursor.next() ) {
00197     const coral::AttributeList& row = ccbRelCursor.currentRow();
00198     int cfg     = row["CONFKEY"   ].data<int>();
00199     int ccb     = row["CCBID"     ].data<int>();
00200     int key     = row["CONFCCBKEY"].data<int>();
00201     // check for used configurations
00202 //    if ( cfgMap.find( cfg ) == cfgMap.end() ) continue;
00203     if ( userDiscardedKey( cfg ) ) continue;
00204     std::map<int,std::map<int,int>*>::const_iterator keyIter =
00205                                                      keyMap.find( cfg );
00206     std::map<int,std::map<int,int>*>::const_iterator keyIend =
00207                                                      keyMap.end();
00208     std::map<int,int>* mapPtr = 0;
00209     // check for new full configuration
00210     if ( keyIter != keyIend ) mapPtr = keyIter->second;
00211     else                      keyMap.insert(
00212                               std::pair<int,std::map<int,int>*>( cfg,
00213                               mapPtr = new std::map<int,int> ) );
00214     // store ccb config key
00215     std::map<int,int>& mapRef( *mapPtr );
00216     mapRef.insert( std::pair<int,int>( ccb, key ) );
00217     // check for new ccb config key
00218     if ( cckMap.find( key ) == cckMap.end() )
00219          cckMap.insert( std::pair<int,int>( key, ccb ) );
00220   }
00221 
00222   // get brick keys
00223   std::cout << "retrieve CCB configuration bricks" << std::endl;
00224   std::map<int,std::vector<int>*> brkMap;
00225   coral::ITable& confBrickTable =
00226     isession.nominalSchema().tableHandle( "CFG2BRKREL" );
00227   std::auto_ptr<coral::IQuery>
00228     confBrickQuery( confBrickTable.newQuery() );
00229   confBrickQuery->addToOutputList( "CONFID" );
00230   confBrickQuery->addToOutputList( "BRKID"  );
00231   coral::ICursor& confBrickCursor = confBrickQuery->execute();
00232   // loop over all brick keys
00233   while( confBrickCursor.next() ) {
00234     const coral::AttributeList& row = confBrickCursor.currentRow();
00235     int key = row["CONFID"].data<int>();
00236     int brk = row["BRKID" ].data<int>();
00237     // check for used ccb config key
00238     if ( cckMap.find( key ) == cckMap.end() ) continue;
00239     std::map<int,std::vector<int>*>::const_iterator brkIter =
00240                                                     brkMap.find( key );
00241     std::map<int,std::vector<int>*>::const_iterator brkIend =
00242                                                     brkMap.end();
00243     // check for new ccb config key
00244     std::vector<int>* brkPtr = 0;
00245     if ( brkIter != brkIend ) brkPtr = brkIter->second;
00246     else                      brkMap.insert(
00247                               std::pair<int,std::vector<int>*>( key,
00248                               brkPtr = new std::vector<int> ) );
00249     // store brick key
00250     brkPtr->push_back( brk );
00251   }
00252 
00253   // set run and full configuration in payload
00254   DTCCBConfig* fullConf = new DTCCBConfig( dataTag );
00255   fullConf->setStamp( 1 );
00256   fullConf->setFullKey( userConf );
00257   std::map<int,bool> userBricks;
00258   std::map<int,bool>::const_iterator uBrkIter = userBricks.begin();
00259   std::map<int,bool>::const_iterator uBrkIend = userBricks.end();
00260   std::vector<DTConfigKey>::const_iterator cfgIter = userConf.begin();
00261   std::vector<DTConfigKey>::const_iterator cfgIend = userConf.end();
00262   while ( cfgIter != cfgIend ) {
00263     const DTConfigKey& cfgEntry = *cfgIter++;
00264     int cft = cfgEntry.confType;
00265     int cfg = cfgEntry.confKey;
00266     // retrieve ccb config map
00267     std::map<int,std::map<int,int>*>::const_iterator keyIter =
00268                                                      keyMap.find( cfg );
00269     std::map<int,std::map<int,int>*>::const_iterator keyIend =
00270                                                      keyMap.end();
00271     std::map<int,int>* mapPtr = 0;
00272     if ( keyIter != keyIend ) mapPtr = keyIter->second;
00273     if ( mapPtr == 0 ) continue;
00274     std::map<int,int>::const_iterator ccbIter = mapPtr->begin();
00275     std::map<int,int>::const_iterator ccbIend = mapPtr->end();
00276     while ( ccbIter != ccbIend ) {
00277       const std::pair<int,int>& ccbEntry = *ccbIter++;
00278       // get ccb config key
00279       int ccb = ccbEntry.first;
00280       int key = ccbEntry.second;
00281       // retrieve chamber id
00282       std::map<int,DTCCBId>::const_iterator ccbIter = ccbMap.find( ccb );
00283       std::map<int,DTCCBId>::const_iterator ccbIend = ccbMap.end();
00284       if ( ccbIter == ccbIend ) continue;
00285       const DTCCBId& chaId = ccbIter->second;
00286       // retrieve brick id list
00287       std::map<int,std::vector<int>*>::const_iterator brkIter =
00288                                                       brkMap.find( key );
00289       std::map<int,std::vector<int>*>::const_iterator brkIend =
00290                                                       brkMap.end();
00291       if ( brkIter == brkIend ) continue;
00292       std::vector<int>* brkPtr = brkIter->second;
00293       if ( brkPtr == 0 ) continue;
00294       // brick id lists in payload
00295       std::vector<int> bkList;
00296       bkList.reserve( 20 );
00297       std::map<int,int>::const_iterator bktIter = bktMap.begin();
00298       std::map<int,int>::const_iterator bktIend = bktMap.end();
00299       std::vector<int>::const_iterator bkiIter = brkPtr->begin();
00300       std::vector<int>::const_iterator bkiIend = brkPtr->end();
00301       while ( bkiIter != bkiIend ) {
00302         int brickId = *bkiIter++;
00303         bktIter = bktMap.find( brickId );
00304         if ( bktIter == bktIend ) continue;
00305         if ( bktIter->second == cft ) {
00306           bkList.push_back( brickId );
00307           uBrkIter = userBricks.find( brickId );
00308           if ( uBrkIter == uBrkIend ) 
00309               userBricks.insert( std::pair<int,bool>( brickId, true ) );
00310         }
00311       }
00312       fullConf->appendConfigKey( chaId.wheelId,
00313                                  chaId.stationId,
00314                                  chaId.sectorId,
00315                                  bkList );
00316     }
00317   }
00318   cond::Time_t snc = dataRun;
00319   if ( writeKeys ) m_to_transfer.push_back( std::make_pair( fullConf, snc ) );
00320   std::cout << "writing payload : " << sizeof( *fullConf ) 
00321             << " ( " << ( fullConf->end() - fullConf->begin() )
00322             << " ) " << std::endl;
00323   if ( writeData ) chkConfigList( userBricks );
00324 
00325   isession.transaction().commit();
00326   isession.close();
00327 
00328   return;
00329 
00330 }
00331 
00332 
00333 void DTUserKeyedConfigHandler::chkConfigList(
00334                                const std::map<int,bool>& userBricks ) {
00335 
00336   std::cout << "open POOL out db " << std::endl;
00337   edm::Service<cond::service::PoolDBOutputService> outdb;
00338 
00339   std::map<int,bool>::const_iterator uBrkIter = userBricks.begin();
00340   std::map<int,bool>::const_iterator uBrkIend = userBricks.end();
00341 
00342   coral::ITable& brickConfigTable =
00343     isession.nominalSchema().tableHandle( "CFGBRICKS" );
00344   std::auto_ptr<coral::IQuery>
00345     brickConfigQuery( brickConfigTable.newQuery() );
00346   brickConfigQuery->addToOutputList( "BRKID" );
00347   brickConfigQuery->addToOutputList( "BRKNAME" );
00348   coral::ICursor& brickConfigCursor = brickConfigQuery->execute();
00349   DTKeyedConfig* brickData = 0;
00350   std::vector<int> missingList;
00351   std::vector<unsigned long long> checkedKeys;
00352   while( brickConfigCursor.next() ) {
00353     const coral::AttributeList& row = brickConfigCursor.currentRow();
00354     int brickConfigId = row["BRKID"].data<int>();
00355     uBrkIter = userBricks.find( brickConfigId );
00356     if ( uBrkIter == uBrkIend ) continue;
00357     if ( !( uBrkIter->second ) ) continue;
00358     std::string brickConfigName = row["BRKNAME"].data<std::string>();
00359     std::cout << "brick " << brickConfigId
00360               << " : "    << brickConfigName << std::endl;
00361     checkedKeys.push_back( brickConfigId );
00362     bool brickFound = false;
00363     try {
00364       std::cout << "load brick " << checkedKeys[0] << std::endl;
00365       std::cout << "key list " <<  keyList << std::endl;
00366       keyList->load( checkedKeys );
00367       std::cout << "get brick..." << std::endl;
00368       const DTKeyedConfig* brickCheck =
00369                            keyList->get<DTKeyedConfig>( 0 );
00370       if ( brickCheck != 0 ) {
00371         brickFound = ( brickCheck->getId() == brickConfigId );
00372       }
00373     }
00374     catch ( std::exception e ) {
00375     }
00376     if ( !brickFound ) {
00377       std::cout << "brick " << brickConfigId << " missing, copy request"
00378                 << std::endl;
00379       missingList.push_back( brickConfigId );
00380     }
00381     checkedKeys.clear();
00382   }
00383   keyList->load( checkedKeys );
00384 
00385   std::vector<int>::const_iterator brickIter = missingList.begin();
00386   std::vector<int>::const_iterator brickIend = missingList.end();
00387   while ( brickIter != brickIend ) {
00388     int brickConfigId = *brickIter++;
00389     coral::AttributeList bindVariableList;
00390     bindVariableList.extend( "brickId", typeid(int) );
00391     bindVariableList["brickId"].data<int>() = brickConfigId;
00392     std::auto_ptr<coral::IQuery>
00393            brickDataQuery( isession.nominalSchema().newQuery() );
00394     brickDataQuery->addToTableList( "CFGRELATIONS" );
00395     brickDataQuery->addToTableList( "CONFIGCMDS" );
00396     std::string
00397     brickCondition  =      "CONFIGCMDS.CMDID=CFGRELATIONS.CMDID";
00398     brickCondition += " and CFGRELATIONS.BRKID=:brickId";
00399     brickDataQuery->addToOutputList( "CFGRELATIONS.BRKID" );
00400     brickDataQuery->addToOutputList( "CONFIGCMDS.CONFDATA" );
00401     brickDataQuery->setCondition( brickCondition, bindVariableList );
00402     coral::ICursor& brickDataCursor = brickDataQuery->execute();
00403     brickData = new DTKeyedConfig();
00404     brickData->setId( brickConfigId );
00405     while( brickDataCursor.next() ) {
00406       const coral::AttributeList& row = brickDataCursor.currentRow();
00407       brickData->add( row["CONFIGCMDS.CONFDATA"].data<std::string>() );
00408     }
00409     cond::KeyedElement k( brickData, brickConfigId );
00410     std::cout << "now writing brick: " << brickConfigId << std::endl;
00411     outdb->writeOne( k.m_obj, k.m_key, brickContainer );
00412   }
00413 
00414   return;
00415 
00416 }
00417 
00418 
00419 std::string DTUserKeyedConfigHandler::id() const {
00420   return dataTag;
00421 }
00422 
00423 
00424 bool DTUserKeyedConfigHandler::sameConfigList(
00425                          const std::vector<DTConfigKey>& cfgl,
00426                          const std::vector<DTConfigKey>& cfgr ) {
00427   if ( cfgl.size() != cfgr.size() ) return false;
00428   std::map<int,int> lmap;
00429   std::vector<DTConfigKey>::const_iterator lIter = cfgl.begin();
00430   std::vector<DTConfigKey>::const_iterator lIend = cfgl.end();
00431   while ( lIter != lIend ) {
00432     const DTConfigKey& entry = *lIter++;
00433     lmap.insert( std::pair<int,int>( entry.confType, entry.confKey ) );
00434   }
00435   std::map<int,int> rmap;
00436   std::vector<DTConfigKey>::const_iterator rIter = cfgr.begin();
00437   std::vector<DTConfigKey>::const_iterator rIend = cfgr.end();
00438   while ( rIter != rIend ) {
00439     const DTConfigKey& entry = *rIter++;
00440     rmap.insert( std::pair<int,int>( entry.confType, entry.confKey ) );
00441   }
00442   std::map<int,int>::const_iterator lmIter = lmap.begin();
00443   std::map<int,int>::const_iterator lmIend = lmap.end();
00444   std::map<int,int>::const_iterator rmIter = rmap.begin();
00445   std::map<int,int>::const_iterator rmIend = rmap.end();
00446   while ( ( lmIter != lmIend ) &&
00447           ( rmIter != rmIend ) ) {
00448     const std::pair<int,int>& lEntry = *lmIter++;
00449     const std::pair<int,int>& rEntry = *rmIter++;
00450     if ( lEntry.first  != rEntry.first  ) return false;
00451     if ( lEntry.second != rEntry.second ) return false;
00452   }
00453   return true;
00454 }
00455 
00456 
00457 bool DTUserKeyedConfigHandler::userDiscardedKey( int key ) {
00458   std::vector<DTConfigKey>::const_iterator iter = userConf.begin();
00459   std::vector<DTConfigKey>::const_iterator iend = userConf.end();
00460   while ( iter != iend ) {
00461     const DTConfigKey& entry = *iter++;
00462     if ( entry.confKey == key ) return false;
00463   }
00464   return true;
00465 }
00466 
00467 void DTUserKeyedConfigHandler::setList( cond::KeyList* list ) {
00468   keyList = list;
00469 }
00470