CMS 3D CMS Logo

Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes

DTUserKeyedConfigHandler Class Reference

#include <DTUserKeyedConfigHandler.h>

Inheritance diagram for DTUserKeyedConfigHandler:
popcon::PopConSourceHandler< DTCCBConfig >

List of all members.

Public Member Functions

 DTUserKeyedConfigHandler (const edm::ParameterSet &ps)
void getNewObjects ()
std::string id () const
virtual ~DTUserKeyedConfigHandler ()

Static Public Member Functions

static void setList (cond::KeyList *list)

Private Member Functions

void chkConfigList (const std::map< int, bool > &userBricks)
bool userDiscardedKey (int key)

Static Private Member Functions

static bool sameConfigList (const std::vector< DTConfigKey > &cfgl, const std::vector< DTConfigKey > &cfgr)

Private Attributes

std::string brickContainer
DTCCBConfigccbConfig
cond::DbConnection connection
int dataRun
std::string dataTag
cond::DbSession isession
std::string onlineAuthentication
std::string onlineConnect
std::vector< DTConfigKeyuserConf
bool writeData
bool writeKeys

Static Private Attributes

static cond::KeyListkeyList = 0

Detailed Description

Description:

Date:
2010/07/21 16:06:53
Revision:
1.4
Author:
Paolo Ronchese INFN Padova

Definition at line 40 of file DTUserKeyedConfigHandler.h.


Constructor & Destructor Documentation

DTUserKeyedConfigHandler::DTUserKeyedConfigHandler ( const edm::ParameterSet ps)

Constructor

Definition at line 50 of file DTUserKeyedConfigHandler.cc.

References DTConfigKey::confKey, DTConfigKey::confType, gather_cfg::cout, CommonMethods::cp(), edm::ParameterSet::getParameter(), edm::ParameterSet::getUntrackedParameter(), onlineAuthentication, and userConf.

                                                                             :
 dataRun(               ps.getParameter<int>         ( "run" ) ),
 dataTag(               ps.getParameter<std::string> ( "tag" ) ),
 onlineConnect(         ps.getParameter<std::string> ( "onlineDB" ) ),
 onlineAuthentication(  ps.getParameter<std::string> ( 
                        "onlineAuthentication" ) ),
 brickContainer(        ps.getParameter<std::string> ( "container" ) ),
 writeKeys(             ps.getParameter<bool>        ( "writeKeys" ) ),
 writeData(             ps.getParameter<bool>        ( "writeData" ) ),
 connection(),
 isession() {
  std::cout << " PopCon application for DT configuration export "
            <<  onlineAuthentication << std::endl;

  std::vector<edm::ParameterSet> dtConfigKeys (
        ps.getParameter< std::vector<edm::ParameterSet> >( "DTConfigKeys" )
  );
  std::vector<edm::ParameterSet>::const_iterator iter = dtConfigKeys.begin();
  std::vector<edm::ParameterSet>::const_iterator iend = dtConfigKeys.end();
  while ( iter != iend ) {
    const edm::ParameterSet& cp = *iter++;
    int configType = cp.getUntrackedParameter<int>( "configType" );
    int configKey  = cp.getUntrackedParameter<int>( "configKey"  );
    std::cout << "config: "
              << configType << " -> "
              << configKey << std::endl;
    DTConfigKey userKey;
    userKey.confType = configType;
    userKey.confKey  = configKey;
    userConf.push_back( userKey );
  }
}
DTUserKeyedConfigHandler::~DTUserKeyedConfigHandler ( ) [virtual]

Destructor

Definition at line 86 of file DTUserKeyedConfigHandler.cc.

                                                    {
}

Member Function Documentation

void DTUserKeyedConfigHandler::chkConfigList ( const std::map< int, bool > &  userBricks) [private]

Definition at line 333 of file DTUserKeyedConfigHandler.cc.

References DTKeyedConfig::add(), brickContainer, gather_cfg::cout, alignCSCRings::e, exception, cond::KeyList::get(), DTKeyedConfig::getId(), isession, gen::k, keyList, cond::KeyList::load(), cond::KeyedElement::m_key, cond::KeyedElement::m_obj, cond::DbSession::nominalSchema(), and DTKeyedConfig::setId().

Referenced by getNewObjects().

                                                                    {

  std::cout << "open POOL out db " << std::endl;
  edm::Service<cond::service::PoolDBOutputService> outdb;

  std::map<int,bool>::const_iterator uBrkIter = userBricks.begin();
  std::map<int,bool>::const_iterator uBrkIend = userBricks.end();

  coral::ITable& brickConfigTable =
    isession.nominalSchema().tableHandle( "CFGBRICKS" );
  std::auto_ptr<coral::IQuery>
    brickConfigQuery( brickConfigTable.newQuery() );
  brickConfigQuery->addToOutputList( "BRKID" );
  brickConfigQuery->addToOutputList( "BRKNAME" );
  coral::ICursor& brickConfigCursor = brickConfigQuery->execute();
  DTKeyedConfig* brickData = 0;
  std::vector<int> missingList;
  std::vector<unsigned long long> checkedKeys;
  while( brickConfigCursor.next() ) {
    const coral::AttributeList& row = brickConfigCursor.currentRow();
    int brickConfigId = row["BRKID"].data<int>();
    uBrkIter = userBricks.find( brickConfigId );
    if ( uBrkIter == uBrkIend ) continue;
    if ( !( uBrkIter->second ) ) continue;
    std::string brickConfigName = row["BRKNAME"].data<std::string>();
    std::cout << "brick " << brickConfigId
              << " : "    << brickConfigName << std::endl;
    checkedKeys.push_back( brickConfigId );
    bool brickFound = false;
    try {
      std::cout << "load brick " << checkedKeys[0] << std::endl;
      std::cout << "key list " <<  keyList << std::endl;
      keyList->load( checkedKeys );
      std::cout << "get brick..." << std::endl;
      const DTKeyedConfig* brickCheck =
                           keyList->get<DTKeyedConfig>( 0 );
      if ( brickCheck != 0 ) {
        brickFound = ( brickCheck->getId() == brickConfigId );
      }
    }
    catch ( std::exception e ) {
    }
    if ( !brickFound ) {
      std::cout << "brick " << brickConfigId << " missing, copy request"
                << std::endl;
      missingList.push_back( brickConfigId );
    }
    checkedKeys.clear();
  }
  keyList->load( checkedKeys );

  std::vector<int>::const_iterator brickIter = missingList.begin();
  std::vector<int>::const_iterator brickIend = missingList.end();
  while ( brickIter != brickIend ) {
    int brickConfigId = *brickIter++;
    coral::AttributeList bindVariableList;
    bindVariableList.extend( "brickId", typeid(int) );
    bindVariableList["brickId"].data<int>() = brickConfigId;
    std::auto_ptr<coral::IQuery>
           brickDataQuery( isession.nominalSchema().newQuery() );
    brickDataQuery->addToTableList( "CFGRELATIONS" );
    brickDataQuery->addToTableList( "CONFIGCMDS" );
    std::string
    brickCondition  =      "CONFIGCMDS.CMDID=CFGRELATIONS.CMDID";
    brickCondition += " and CFGRELATIONS.BRKID=:brickId";
    brickDataQuery->addToOutputList( "CFGRELATIONS.BRKID" );
    brickDataQuery->addToOutputList( "CONFIGCMDS.CONFDATA" );
    brickDataQuery->setCondition( brickCondition, bindVariableList );
    coral::ICursor& brickDataCursor = brickDataQuery->execute();
    brickData = new DTKeyedConfig();
    brickData->setId( brickConfigId );
    while( brickDataCursor.next() ) {
      const coral::AttributeList& row = brickDataCursor.currentRow();
      brickData->add( row["CONFIGCMDS.CONFDATA"].data<std::string>() );
    }
    cond::KeyedElement k( brickData, brickConfigId );
    std::cout << "now writing brick: " << brickConfigId << std::endl;
    outdb->writeOne( k.m_obj, k.m_key, brickContainer );
  }

  return;

}
void DTUserKeyedConfigHandler::getNewObjects ( ) [virtual]

Operations

Implements popcon::PopConSourceHandler< DTCCBConfig >.

Definition at line 92 of file DTUserKeyedConfigHandler.cc.

References DTCCBConfig::appendConfigKey(), DTCCBConfig::begin(), chkConfigList(), cond::DbSession::close(), cond::DbConnection::configuration(), cond::DbConnection::configure(), DTConfigKey::confKey, DTConfigKey::confType, connection, gather_cfg::cout, cond::DbConnection::createSession(), dataRun, dataTag, DTCCBConfig::end(), isession, combine::key, keyList, prof2calltree::last, cond::TagInfo::lastInterval, popcon::PopConSourceHandler< DTCCBConfig >::lastPayload(), popcon::PopConSourceHandler< DTCCBConfig >::m_to_transfer, cond::DbSession::nominalSchema(), onlineAuthentication, onlineConnect, cond::DbSession::open(), DTCCBId::sectorId, DTCCBConfig::setFullKey(), DTCCBConfig::setStamp(), relativeConstraints::station, DTCCBId::stationId, popcon::PopConSourceHandler< DTCCBConfig >::tagInfo(), cond::DbSession::transaction(), userConf, userDiscardedKey(), DTCCBId::wheelId, writeData, and writeKeys.

                                             {

  //to access the information on the tag inside the offline database:
  cond::TagInfo const & ti = tagInfo();
  unsigned int last = ti.lastInterval.first;
  std::cout << "last configuration key already copied for run: "
            << last << std::endl;

  std::vector<DTConfigKey> lastKey;
//  std::cout << "check for last " << std::endl;
  if ( last == 0 ) {
    DTCCBConfig* dummyConf = new DTCCBConfig( dataTag );
    dummyConf->setStamp( 0 );
    dummyConf->setFullKey( lastKey );
    cond::Time_t snc = 1;
    if ( writeKeys && ( dataRun > 1 ) )
         m_to_transfer.push_back( std::make_pair( dummyConf, snc ) );
  }
  else {
    std::cout << "get last payload" << std::endl;
    Ref payload = lastPayload();
    std::cout << "get last full key" << std::endl;
    lastKey = payload->fullKey();
    std::cout << "last key: " << std::endl;
    std::vector<DTConfigKey>::const_iterator keyIter = lastKey.begin();
    std::vector<DTConfigKey>::const_iterator keyIend = lastKey.end();
    while ( keyIter != keyIend ) {
      const DTConfigKey& keyList = *keyIter++;
      std::cout << keyList.confType << " : " << keyList.confKey << std::endl;
    }
  }

  std::cout << "configure DbConnection" << std::endl;
  //  conn->configure( cond::CmsDefaults );
  connection.configuration().setAuthenticationPath( onlineAuthentication );
  connection.configure();
  std::cout << "create DbSession" << std::endl;
  isession = connection.createSession();
  std::cout << "open session" << std::endl;
  isession.open( onlineConnect );
  std::cout << "start transaction" << std::endl;
  isession.transaction().start();
  
  // get ccb identifiers map
  std::cout << "retrieve CCB map" << std::endl;
  std::map<int,DTCCBId> ccbMap;
  coral::ITable& ccbMapTable =
    isession.nominalSchema().tableHandle( "CCBMAP" );
  std::auto_ptr<coral::IQuery>
    ccbMapQuery( ccbMapTable.newQuery() );
  ccbMapQuery->addToOutputList( "CCBID" );
  ccbMapQuery->addToOutputList( "WHEEL" );
  ccbMapQuery->addToOutputList( "SECTOR" );
  ccbMapQuery->addToOutputList( "STATION" );
  coral::ICursor& ccbMapCursor = ccbMapQuery->execute();
  while( ccbMapCursor.next() ) {
    const coral::AttributeList& row = ccbMapCursor.currentRow();
    int ccb     = row["CCBID"  ].data<int>();
    int wheel   = row["WHEEL"  ].data<int>();
    int sector  = row["SECTOR" ].data<int>();
    int station = row["STATION"].data<int>();
    DTCCBId ccbId;
    ccbId.  wheelId =   wheel;
    ccbId.stationId = station;
    ccbId. sectorId =  sector;
    ccbMap.insert( std::pair<int,DTCCBId>( ccb, ccbId ) );
  }

  // get brick types
  std::cout << "retrieve brick types" << std::endl;
  std::map<int,int> bktMap;
  coral::AttributeList emptyBindVariableList;
  std::auto_ptr<coral::IQuery>
         brickTypeQuery( isession.nominalSchema().newQuery() );
  brickTypeQuery->addToTableList( "CFGBRICKS" );
  brickTypeQuery->addToTableList( "BRKT2CSETT" );
  std::string bTypeCondition = "CFGBRICKS.BRKTYPE=BRKT2CSETT.BRKTYPE";
  brickTypeQuery->addToOutputList( "CFGBRICKS.BRKID" );
  brickTypeQuery->addToOutputList( "BRKT2CSETT.CSETTYPE" );
  brickTypeQuery->setCondition( bTypeCondition, emptyBindVariableList );
  coral::ICursor& brickTypeCursor = brickTypeQuery->execute();
  while( brickTypeCursor.next() ) {
    const coral::AttributeList& row = brickTypeCursor.currentRow();
    int id = row["CFGBRICKS.BRKID"    ].data<int>();
    int bt = row["BRKT2CSETT.CSETTYPE"].data<short>();
//    std::cout << "brick " << id << " type " << bt << std::endl;
// @@FIX - TEMPORARY PATCH
//    if ( bt > 3 ) bt = 3;
    bktMap.insert( std::pair<int,int>( id, bt ) );
  }

  // get ccb config keys
  std::cout << "retrieve CCB configuration keys" << std::endl;
  std::map<int,std::map<int,int>*> keyMap;
  std::map<int,int> cckMap;
  coral::ITable& ccbRelTable =
    isession.nominalSchema().tableHandle( "CCBRELATIONS" );
  std::auto_ptr<coral::IQuery>
    ccbRelQuery( ccbRelTable.newQuery() );
  ccbRelQuery->addToOutputList( "CONFKEY" );
  ccbRelQuery->addToOutputList( "CCBID" );
  ccbRelQuery->addToOutputList( "CONFCCBKEY" );
  coral::ICursor& ccbRelCursor = ccbRelQuery->execute();
  // loop over all full configurations
  while( ccbRelCursor.next() ) {
    const coral::AttributeList& row = ccbRelCursor.currentRow();
    int cfg     = row["CONFKEY"   ].data<int>();
    int ccb     = row["CCBID"     ].data<int>();
    int key     = row["CONFCCBKEY"].data<int>();
    // check for used configurations
//    if ( cfgMap.find( cfg ) == cfgMap.end() ) continue;
    if ( userDiscardedKey( cfg ) ) continue;
    std::map<int,std::map<int,int>*>::const_iterator keyIter =
                                                     keyMap.find( cfg );
    std::map<int,std::map<int,int>*>::const_iterator keyIend =
                                                     keyMap.end();
    std::map<int,int>* mapPtr = 0;
    // check for new full configuration
    if ( keyIter != keyIend ) mapPtr = keyIter->second;
    else                      keyMap.insert(
                              std::pair<int,std::map<int,int>*>( cfg,
                              mapPtr = new std::map<int,int> ) );
    // store ccb config key
    std::map<int,int>& mapRef( *mapPtr );
    mapRef.insert( std::pair<int,int>( ccb, key ) );
    // check for new ccb config key
    if ( cckMap.find( key ) == cckMap.end() )
         cckMap.insert( std::pair<int,int>( key, ccb ) );
  }

  // get brick keys
  std::cout << "retrieve CCB configuration bricks" << std::endl;
  std::map<int,std::vector<int>*> brkMap;
  coral::ITable& confBrickTable =
    isession.nominalSchema().tableHandle( "CFG2BRKREL" );
  std::auto_ptr<coral::IQuery>
    confBrickQuery( confBrickTable.newQuery() );
  confBrickQuery->addToOutputList( "CONFID" );
  confBrickQuery->addToOutputList( "BRKID"  );
  coral::ICursor& confBrickCursor = confBrickQuery->execute();
  // loop over all brick keys
  while( confBrickCursor.next() ) {
    const coral::AttributeList& row = confBrickCursor.currentRow();
    int key = row["CONFID"].data<int>();
    int brk = row["BRKID" ].data<int>();
    // check for used ccb config key
    if ( cckMap.find( key ) == cckMap.end() ) continue;
    std::map<int,std::vector<int>*>::const_iterator brkIter =
                                                    brkMap.find( key );
    std::map<int,std::vector<int>*>::const_iterator brkIend =
                                                    brkMap.end();
    // check for new ccb config key
    std::vector<int>* brkPtr = 0;
    if ( brkIter != brkIend ) brkPtr = brkIter->second;
    else                      brkMap.insert(
                              std::pair<int,std::vector<int>*>( key,
                              brkPtr = new std::vector<int> ) );
    // store brick key
    brkPtr->push_back( brk );
  }

  // set run and full configuration in payload
  DTCCBConfig* fullConf = new DTCCBConfig( dataTag );
  fullConf->setStamp( 1 );
  fullConf->setFullKey( userConf );
  std::map<int,bool> userBricks;
  std::map<int,bool>::const_iterator uBrkIter = userBricks.begin();
  std::map<int,bool>::const_iterator uBrkIend = userBricks.end();
  std::vector<DTConfigKey>::const_iterator cfgIter = userConf.begin();
  std::vector<DTConfigKey>::const_iterator cfgIend = userConf.end();
  while ( cfgIter != cfgIend ) {
    const DTConfigKey& cfgEntry = *cfgIter++;
    int cft = cfgEntry.confType;
    int cfg = cfgEntry.confKey;
    // retrieve ccb config map
    std::map<int,std::map<int,int>*>::const_iterator keyIter =
                                                     keyMap.find( cfg );
    std::map<int,std::map<int,int>*>::const_iterator keyIend =
                                                     keyMap.end();
    std::map<int,int>* mapPtr = 0;
    if ( keyIter != keyIend ) mapPtr = keyIter->second;
    if ( mapPtr == 0 ) continue;
    std::map<int,int>::const_iterator ccbIter = mapPtr->begin();
    std::map<int,int>::const_iterator ccbIend = mapPtr->end();
    while ( ccbIter != ccbIend ) {
      const std::pair<int,int>& ccbEntry = *ccbIter++;
      // get ccb config key
      int ccb = ccbEntry.first;
      int key = ccbEntry.second;
      // retrieve chamber id
      std::map<int,DTCCBId>::const_iterator ccbIter = ccbMap.find( ccb );
      std::map<int,DTCCBId>::const_iterator ccbIend = ccbMap.end();
      if ( ccbIter == ccbIend ) continue;
      const DTCCBId& chaId = ccbIter->second;
      // retrieve brick id list
      std::map<int,std::vector<int>*>::const_iterator brkIter =
                                                      brkMap.find( key );
      std::map<int,std::vector<int>*>::const_iterator brkIend =
                                                      brkMap.end();
      if ( brkIter == brkIend ) continue;
      std::vector<int>* brkPtr = brkIter->second;
      if ( brkPtr == 0 ) continue;
      // brick id lists in payload
      std::vector<int> bkList;
      bkList.reserve( 20 );
      std::map<int,int>::const_iterator bktIter = bktMap.begin();
      std::map<int,int>::const_iterator bktIend = bktMap.end();
      std::vector<int>::const_iterator bkiIter = brkPtr->begin();
      std::vector<int>::const_iterator bkiIend = brkPtr->end();
      while ( bkiIter != bkiIend ) {
        int brickId = *bkiIter++;
        bktIter = bktMap.find( brickId );
        if ( bktIter == bktIend ) continue;
        if ( bktIter->second == cft ) {
          bkList.push_back( brickId );
          uBrkIter = userBricks.find( brickId );
          if ( uBrkIter == uBrkIend ) 
              userBricks.insert( std::pair<int,bool>( brickId, true ) );
        }
      }
      fullConf->appendConfigKey( chaId.wheelId,
                                 chaId.stationId,
                                 chaId.sectorId,
                                 bkList );
    }
  }
  cond::Time_t snc = dataRun;
  if ( writeKeys ) m_to_transfer.push_back( std::make_pair( fullConf, snc ) );
  std::cout << "writing payload : " << sizeof( *fullConf ) 
            << " ( " << ( fullConf->end() - fullConf->begin() )
            << " ) " << std::endl;
  if ( writeData ) chkConfigList( userBricks );

  isession.transaction().commit();
  isession.close();

  return;

}
std::string DTUserKeyedConfigHandler::id ( void  ) const [virtual]

Implements popcon::PopConSourceHandler< DTCCBConfig >.

Definition at line 419 of file DTUserKeyedConfigHandler.cc.

References dataTag.

                                             {
  return dataTag;
}
bool DTUserKeyedConfigHandler::sameConfigList ( const std::vector< DTConfigKey > &  cfgl,
const std::vector< DTConfigKey > &  cfgr 
) [static, private]

Definition at line 424 of file DTUserKeyedConfigHandler.cc.

References DTConfigKey::confKey, and DTConfigKey::confType.

                                                              {
  if ( cfgl.size() != cfgr.size() ) return false;
  std::map<int,int> lmap;
  std::vector<DTConfigKey>::const_iterator lIter = cfgl.begin();
  std::vector<DTConfigKey>::const_iterator lIend = cfgl.end();
  while ( lIter != lIend ) {
    const DTConfigKey& entry = *lIter++;
    lmap.insert( std::pair<int,int>( entry.confType, entry.confKey ) );
  }
  std::map<int,int> rmap;
  std::vector<DTConfigKey>::const_iterator rIter = cfgr.begin();
  std::vector<DTConfigKey>::const_iterator rIend = cfgr.end();
  while ( rIter != rIend ) {
    const DTConfigKey& entry = *rIter++;
    rmap.insert( std::pair<int,int>( entry.confType, entry.confKey ) );
  }
  std::map<int,int>::const_iterator lmIter = lmap.begin();
  std::map<int,int>::const_iterator lmIend = lmap.end();
  std::map<int,int>::const_iterator rmIter = rmap.begin();
  std::map<int,int>::const_iterator rmIend = rmap.end();
  while ( ( lmIter != lmIend ) &&
          ( rmIter != rmIend ) ) {
    const std::pair<int,int>& lEntry = *lmIter++;
    const std::pair<int,int>& rEntry = *rmIter++;
    if ( lEntry.first  != rEntry.first  ) return false;
    if ( lEntry.second != rEntry.second ) return false;
  }
  return true;
}
void DTUserKeyedConfigHandler::setList ( cond::KeyList list) [static]

Definition at line 467 of file DTUserKeyedConfigHandler.cc.

References keyList, and list().

Referenced by DTUserKeyedConfigPopConAnalyzer::analyze().

                                                          {
  keyList = list;
}
bool DTUserKeyedConfigHandler::userDiscardedKey ( int  key) [private]

Definition at line 457 of file DTUserKeyedConfigHandler.cc.

References DTConfigKey::confKey, and userConf.

Referenced by getNewObjects().

                                                         {
  std::vector<DTConfigKey>::const_iterator iter = userConf.begin();
  std::vector<DTConfigKey>::const_iterator iend = userConf.end();
  while ( iter != iend ) {
    const DTConfigKey& entry = *iter++;
    if ( entry.confKey == key ) return false;
  }
  return true;
}

Member Data Documentation

Definition at line 66 of file DTUserKeyedConfigHandler.h.

Referenced by chkConfigList().

Definition at line 70 of file DTUserKeyedConfigHandler.h.

Definition at line 72 of file DTUserKeyedConfigHandler.h.

Referenced by getNewObjects().

Definition at line 62 of file DTUserKeyedConfigHandler.h.

Referenced by getNewObjects().

std::string DTUserKeyedConfigHandler::dataTag [private]

Definition at line 63 of file DTUserKeyedConfigHandler.h.

Referenced by getNewObjects(), and id().

Definition at line 73 of file DTUserKeyedConfigHandler.h.

Referenced by chkConfigList(), and getNewObjects().

Definition at line 79 of file DTUserKeyedConfigHandler.h.

Referenced by chkConfigList(), getNewObjects(), and setList().

Definition at line 65 of file DTUserKeyedConfigHandler.h.

Referenced by DTUserKeyedConfigHandler(), and getNewObjects().

Definition at line 64 of file DTUserKeyedConfigHandler.h.

Referenced by getNewObjects().

Definition at line 69 of file DTUserKeyedConfigHandler.h.

Referenced by getNewObjects().

Definition at line 68 of file DTUserKeyedConfigHandler.h.

Referenced by getNewObjects().