CMS 3D CMS Logo

Classes | Public Types | Public Member Functions | Protected Member Functions | Private Types | Private Member Functions | Private Attributes

CondDBESSource Class Reference

#include <CondDBESSource.h>

Inheritance diagram for CondDBESSource:
edm::eventsetup::DataProxyProvider edm::EventSetupRecordIntervalFinder PoolDBESSource

List of all members.

Classes

struct  Stats

Public Types

typedef std::multimap
< std::string, ProxyP
ProxyMap
typedef boost::shared_ptr
< cond::DataProxyWrapperBase
ProxyP

Public Member Functions

 CondDBESSource (const edm::ParameterSet &)
 ~CondDBESSource ()

Protected Member Functions

virtual void newInterval (const edm::eventsetup::EventSetupRecordKey &iRecordType, const edm::ValidityInterval &iInterval)
 called when a new interval of validity occurs for iRecordType
virtual void registerProxies (const edm::eventsetup::EventSetupRecordKey &iRecordKey, KeyedProxies &aProxyList)
virtual void setIntervalFor (const edm::eventsetup::EventSetupRecordKey &, const edm::IOVSyncValue &, edm::ValidityInterval &)

Private Types

typedef std::set
< cond::TagMetadata
TagCollection

Private Member Functions

void fillTagCollectionFromDB (const std::string &coraldb, const std::string &prefix, const std::string &postfix, const std::string &roottag, std::map< std::string, cond::TagMetadata > &replacement)

Private Attributes

bool doDump
bool doRefresh
unsigned int lastLumi
unsigned int lastRun
cond::DbConnection m_connection
ProxyMap m_proxies
TagCollection m_tagCollection
Stats stats

Detailed Description

Definition at line 34 of file CondDBESSource.h.


Member Typedef Documentation

typedef std::multimap< std::string, ProxyP> CondDBESSource::ProxyMap

Definition at line 38 of file CondDBESSource.h.

Definition at line 37 of file CondDBESSource.h.

typedef std::set< cond::TagMetadata > CondDBESSource::TagCollection [private]

Definition at line 63 of file CondDBESSource.h.


Constructor & Destructor Documentation

CondDBESSource::CondDBESSource ( const edm::ParameterSet iConfig) [explicit]

required by eventsetup

Definition at line 105 of file CondDBESSource.cc.

References b, cond::Auth::COND_READER_ROLE, cond::DbConnection::configuration(), cond::DbConnection::configure(), cond::DbConnection::createSession(), alignCSCRings::e, edm::ParameterSet::exists(), fillTagCollectionFromDB(), edm::EventSetupRecordIntervalFinder::findingRecordWithKey(), edm::eventsetup::heterocontainer::HCTypeTag::findType(), reco::get(), edm::ParameterSet::getParameter(), edm::ParameterSet::getUntrackedParameter(), align_cfg::globaltag, gen::k, cond::TagMetadata::labelname, m_connection, m_proxies, m_tagCollection, CondDBESSource::Stats::nData, cond::DbSession::open(), cond::TagMetadata::pfn, cond::TagMetadata::recordname, alignCSCRings::s, stats, cond::TagMetadata::tag, align_tpl::toGet, edm::eventsetup::EventSetupRecordKey::type(), and edm::eventsetup::DataProxyProvider::usingRecordWithKey().

                                                               :
  m_connection(), 
  lastRun(0),  // for the stat
  lastLumi(0),  // for the stat
  doRefresh(iConfig.getUntrackedParameter<bool>("RefreshEachRun",false)),
  doDump(iConfig.getUntrackedParameter<bool>("DumpStat",false))
{
  Stats s = {0,0,0,0,0,0};
  stats=s;      
  //std::cout<<"CondDBESSource::CondDBESSource"<<std::endl;
  /*parameter set parsing and pool environment setting
   */
  
  // default connection string
  // inproduction used for the global tag
  std::string userconnect= iConfig.getParameter<std::string>("connect");
  

  // connection configuration
  edm::ParameterSet connectionPset = iConfig.getParameter<edm::ParameterSet>("DBParameters");
  m_connection.configuration().setParameters( connectionPset );
  m_connection.configure();
  

  // load additional record/tag info it will overwrite the global tag
  std::map<std::string,cond::TagMetadata> replacement;
  if( iConfig.exists("toGet") ){
    typedef std::vector< edm::ParameterSet > Parameters;
    Parameters toGet = iConfig.getParameter<Parameters>("toGet");
    for(Parameters::iterator itToGet = toGet.begin(); itToGet != toGet.end(); ++itToGet ) {
      cond::TagMetadata nm;
      nm.recordname=itToGet->getParameter<std::string>("record");
      nm.labelname=itToGet->getUntrackedParameter<std::string>("label","");
      nm.tag=itToGet->getParameter<std::string>("tag");
      nm.pfn=itToGet->getUntrackedParameter<std::string>("connect",userconnect);
      //        nm.objectname=itFound->second;
      std::string k=nm.recordname+"@"+nm.labelname;
      replacement.insert(std::pair<std::string,cond::TagMetadata>(k,nm));
    }
  }
  
  // get the global tag, merge with "replacement" store in "tagCollection"
  std::string globaltag;
  if( iConfig.exists("globaltag")) globaltag=iConfig.getParameter<std::string>("globaltag");
  
  fillTagCollectionFromDB(userconnect,
                          iConfig.getUntrackedParameter<std::string>("pfnPrefix",""),
                          iConfig.getUntrackedParameter<std::string>("pfnPostfix",""),
                          globaltag,
                          replacement);
  
  
  TagCollection::iterator it;
  TagCollection::iterator itBeg=m_tagCollection.begin();
  TagCollection::iterator itEnd=m_tagCollection.end();
 
  typedef std::map<std::string, cond::DbSession> Sessions;
  Sessions sessions;

  /* load DataProxy Plugin (it is strongly typed due to EventSetup ideosyncrasis)
   * construct proxy
   * contrary to EventSetup the "object-name" is not used as identifier: multiple entries in a record are
   * dinstinguished only by their label...
   * done in two step: first create ProxyWrapper loading ALL required dictionaries
   * this will allow to initialize POOL in one go for each "database"
   * The real initialization of the Data-Proxies is done in the second loop 
   */
  std::vector<cond::DataProxyWrapperBase *> proxyWrappers(m_tagCollection.size());
  size_t ipb=0;
  for(it=itBeg;it!=itEnd;++it){
    proxyWrappers[ipb++] =  
      cond::ProxyFactory::get()->create(buildName(it->recordname));
  }
  // now all required libraries have been loaded
  // init sessions and DataProxies
  ipb=0;
  for(it=itBeg;it!=itEnd;++it){
    Sessions::iterator p = sessions.find( it->pfn);
    cond::DbSession nsess;
    if (p==sessions.end()) {
      //open db get tag info (i.e. the IOV token...)
      nsess = m_connection.createSession();
      nsess.open( it->pfn, cond::Auth::COND_READER_ROLE, true );
      sessions.insert(std::make_pair(it->pfn,nsess));
    } else nsess = (*p).second;
    //cond::MetaData metadata(nsess);
    //nsess.transaction().start(true);
    //std::string iovtoken = metadata.getToken(it->tag);
    // owenship...
    ProxyP proxy(proxyWrappers[ipb++]);
   //  instert in the map
    m_proxies.insert(std::make_pair(it->recordname, proxy));
    // initialize
    //proxy->lateInit(nsess,iovtoken, 
    //              it->labelname, it->pfn, it->tag
    //      );
    proxy->lateInit(nsess,it->tag, 
                    it->labelname, it->pfn);
    //nsess.transaction().commit();
  }

  // one loaded expose all other tags to the Proxy! 
  CondGetterFromESSource visitor(m_proxies);
  ProxyMap::iterator b= m_proxies.begin();
  ProxyMap::iterator e= m_proxies.end();
  for (;b!=e;b++) {
    (*b).second->proxy()->loadMore(visitor);

    edm::eventsetup::EventSetupRecordKey recordKey(edm::eventsetup::EventSetupRecordKey::TypeTag::findType( (*b).first ) );
    if( recordKey.type() != edm::eventsetup::EventSetupRecordKey::TypeTag() ) {
      findingRecordWithKey( recordKey );
      usingRecordWithKey( recordKey );
    }
  }

  stats.nData=m_proxies.size();
}
CondDBESSource::~CondDBESSource ( )

Definition at line 225 of file CondDBESSource.cc.

References b, gather_cfg::cout, doDump, alignCSCRings::e, cond::BasePayloadProxy::gstats, m_proxies, CondDBESSource::Stats::nActualRefresh, CondDBESSource::Stats::nData, CondDBESSource::Stats::nLumi, CondDBESSource::Stats::nRefresh, CondDBESSource::Stats::nRun, CondDBESSource::Stats::nSet, and stats.

                                {
  //dump info FIXME: find a more suitable place...
  if (doDump) {
    std::cout << "CondDBESSource Statistics" << std::endl
              << "DataProxy " << stats.nData
              <<" setInterval " << stats.nSet
              <<" Runs " << stats.nRun
              <<" Lumis " << stats.nLumi
              <<" Refresh " << stats.nRefresh
              <<" Actual Refresh " << stats.nActualRefresh;
    std::cout << std::endl;
    std::cout << "Global Proxy Statistics" << std::endl
              << "proxy " << cond::BasePayloadProxy::gstats.nProxy
              << " make " << cond::BasePayloadProxy::gstats.nMake
              << " load " << cond::BasePayloadProxy::gstats.nLoad;
    std::cout << std::endl;


    ProxyMap::iterator b= m_proxies.begin();
    ProxyMap::iterator e= m_proxies.end();
    for (;b!=e;b++) {
      dumpInfo(std::cout,(*b).first,*(*b).second);
      std::cout << "\n" << std::endl;
    }

    // FIXME
    // We shall eventually close transaction and session...
  }
}

Member Function Documentation

void CondDBESSource::fillTagCollectionFromDB ( const std::string &  coraldb,
const std::string &  prefix,
const std::string &  postfix,
const std::string &  roottag,
std::map< std::string, cond::TagMetadata > &  replacement 
) [private]

Definition at line 379 of file CondDBESSource.cc.

References cond::Auth::COND_READER_ROLE, cond::DbConnection::createSession(), NewTree::fid, cond::TagCollectionRetriever::getTagCollection(), gen::k, cond::TagMetadata::labelname, m, m_connection, m_tagCollection, cond::TagMetadata::objectname, cond::DbSession::open(), cond::TagMetadata::pfn, cond::TagMetadata::recordname, python::CommonUtils::session, cond::TagMetadata::tag, and cond::DbSession::transaction().

Referenced by CondDBESSource().

                                                                                        {
  //  std::cout<<"fillTagCollectionFromDB"<<std::endl;


  std::set< cond::TagMetadata > tagcoll;
 
 if (!roottag.empty()) {
   if (coraldb.empty()) 
     throw cond::Exception(std::string("ESSource: requested global tag ")+roottag+" but not connection string given");
   cond::DbSession session = m_connection.createSession();
   session.open( coraldb, cond::Auth::COND_READER_ROLE, true );
   session.transaction().start(true);
   cond::TagCollectionRetriever tagRetriever( session, prefix, postfix );
   tagRetriever.getTagCollection(roottag,tagcoll);
   session.transaction().commit();
  } 

  std::set<cond::TagMetadata>::iterator it;
  std::set<cond::TagMetadata>::iterator itBeg=tagcoll.begin();
  std::set<cond::TagMetadata>::iterator itEnd=tagcoll.end();

  // FIXME the logic is a bit perverse: can be surely linearized (at least simplified!) ....
  for(it=itBeg; it!=itEnd; ++it){
    std::string k=it->recordname+"@"+it->labelname;
    std::map<std::string,cond::TagMetadata>::iterator fid=replacement.find(k);
    if(fid != replacement.end()){
      cond::TagMetadata m;
      m.recordname=it->recordname;
      m.labelname=it->labelname;
      m.pfn=fid->second.pfn;
      m.tag=fid->second.tag;
      m.objectname=it->objectname;
      m_tagCollection.insert(m);
      replacement.erase(fid);
    }else{
      m_tagCollection.insert(*it);
    }
  }
  std::map<std::string,cond::TagMetadata>::iterator itrep;
  std::map<std::string,cond::TagMetadata>::iterator itrepBeg=replacement.begin();
  std::map<std::string,cond::TagMetadata>::iterator itrepEnd=replacement.end();
  for(itrep=itrepBeg; itrep!=itrepEnd; ++itrep){
    //std::cout<<"appending"<<std::endl;
    //std::cout<<"pfn "<<itrep->second.pfn<<std::endl;
    //std::cout<<"objectname "<<itrep->second.objectname<<std::endl;
    //std::cout<<"tag "<<itrep->second.tag<<std::endl;
    //std::cout<<"recordname "<<itrep->second.recordname<<std::endl;
    m_tagCollection.insert(itrep->second);
  }
}
void CondDBESSource::newInterval ( const edm::eventsetup::EventSetupRecordKey iRecordType,
const edm::ValidityInterval iInterval 
) [protected, virtual]

called when a new interval of validity occurs for iRecordType

Implements edm::eventsetup::DataProxyProvider.

Definition at line 369 of file CondDBESSource.cc.

References edm::eventsetup::DataProxyProvider::invalidateProxies().

{
  //LogDebug ("CondDBESSource")<<"newInterval";
  invalidateProxies(iRecordType);
}
void CondDBESSource::registerProxies ( const edm::eventsetup::EventSetupRecordKey iRecordKey,
KeyedProxies aProxyList 
) [protected, virtual]

Implements edm::eventsetup::DataProxyProvider.

Definition at line 348 of file CondDBESSource.cc.

References b, alignCSCRings::e, combine::key, LogDebug, m_proxies, edm::eventsetup::EventSetupRecordKey::name(), and AlCaHLTBitMon_ParallelJobs::p.

                                                                                                             {
  std::string recordname=iRecordKey.name();

  ProxyMap::const_iterator b = m_proxies.lower_bound(recordname);
  ProxyMap::const_iterator e = m_proxies.upper_bound(recordname);
  if ( b == e) {
    LogDebug ("CondDBESSource")<<"no DataProxy (Pluging) found for record "<<recordname;
    return;
  }

  for (ProxyMap::const_iterator p=b;p!=e;++p) {  
    if(0 != (*p).second.get()) {
      edm::eventsetup::TypeTag type =  (*p).second->type(); 
      edm::eventsetup::DataKey key( type, edm::eventsetup::IdTags((*p).second->label().c_str()) );
      aProxyList.push_back(KeyedProxies::value_type(key,(*p).second->edmProxy()));
    }
  }
}
void CondDBESSource::setIntervalFor ( const edm::eventsetup::EventSetupRecordKey iKey,
const edm::IOVSyncValue iTime,
edm::ValidityInterval oInterval 
) [protected, virtual]

Implements edm::EventSetupRecordIntervalFinder.

Definition at line 261 of file CondDBESSource.cc.

References b, doRefresh, alignCSCRings::e, edm::IOVSyncValue::eventID(), cond::fromIOVSyncValue(), edm::ValidityInterval::invalidInterval(), lastLumi, lastRun, cond::limitedIOVSyncValue(), LogDebug, edm::IOVSyncValue::luminosityBlockNumber(), m_proxies, max(), min, CondDBESSource::Stats::nActualRefresh, edm::eventsetup::EventSetupRecordKey::name(), CondDBESSource::Stats::nLumi, CondDBESSource::Stats::nRefresh, CondDBESSource::Stats::nRun, CondDBESSource::Stats::nSet, AlCaHLTBitMon_ParallelJobs::p, stats, cond::TIMELIMIT(), upload_serial_tpl::timetype, and cond::toIOVSyncValue().

                                                                                                                                          {

  stats.nSet++;
  {
    // not really required, keep here for the time being
    if(iTime.eventID().run()!=lastRun) {
      lastRun=iTime.eventID().run();
      stats.nRun++;
    }
    if(iTime.luminosityBlockNumber()!=lastLumi) {
      lastLumi=iTime.luminosityBlockNumber();
      stats.nLumi++;
    }
  }
 

  std::string recordname=iKey.name();
  oInterval = edm::ValidityInterval::invalidInterval();
  
  //FIXME use equal_range
  ProxyMap::const_iterator b = m_proxies.lower_bound(recordname);
  ProxyMap::const_iterator e = m_proxies.upper_bound(recordname);
  if ( b == e) {
    LogDebug ("CondDBESSource")<<"no DataProxy (Pluging) found for record "<<recordname;
    return;
  }

  // compute the smallest interval (assume all objects have the same timetype....)
  cond::ValidityInterval recordValidity(1,cond::TIMELIMIT);
  cond::TimeType timetype;
  bool userTime=true;
  for (ProxyMap::const_iterator p=b;p!=e;++p) {

    timetype = (*p).second->proxy()->timetype();
    
    cond::Time_t abtime = cond::fromIOVSyncValue(iTime,timetype);
    userTime = (0==abtime);
    
    //std::cout<<"abtime "<<abtime<<std::endl;

    if (userTime) return; //  oInterval invalid to avoid that make is called...
    /*
      // make oInterval valid For Ever
    {
     oInterval = edm::ValidityInterval(cond::toIOVSyncValue(recordValidity.first,  cond::runnumber, true), 
                                       cond::toIOVSyncValue(recordValidity.second, cond::runnumber, false));
     return;
    }    
    */


    // refresh if required...
    if (doRefresh)  {
      LogDebug ("CondDBESSource") << "Refresh " << recordname << " " << iTime.eventID() << std::endl; 
      stats.nActualRefresh += (*p).second->proxy()->refresh(); 
      stats.nRefresh++;
    }
    
   
    


    //query the IOVSequence
    cond::ValidityInterval validity = (*p).second->proxy()->setIntervalFor(abtime);
    
    recordValidity.first = std::max(recordValidity.first,validity.first);
    recordValidity.second = std::min(recordValidity.second,validity.second);
 
    LogDebug ("CondDBESSource") <<"setting validity " << recordname << " "
                                <<recordValidity.first<<" "<<recordValidity.second<<" for ibtime "<<abtime<< std::endl;
 
  }      
   
  // to force refresh we set end-value to the minimum such an IOV can exend to: current run or lumiblock
    
  if ( (!userTime) && recordValidity.second!=0) {
    edm::IOVSyncValue start = cond::toIOVSyncValue(recordValidity.first, timetype, true);
    edm::IOVSyncValue stop = doRefresh ? cond::limitedIOVSyncValue (iTime, timetype)
      : cond::toIOVSyncValue(recordValidity.second, timetype, false);
       
    oInterval = edm::ValidityInterval( start, stop );
   }
}

Member Data Documentation

bool CondDBESSource::doDump [private]

Definition at line 82 of file CondDBESSource.h.

Referenced by ~CondDBESSource().

bool CondDBESSource::doRefresh [private]

Definition at line 80 of file CondDBESSource.h.

Referenced by setIntervalFor().

unsigned int CondDBESSource::lastLumi [private]

Definition at line 79 of file CondDBESSource.h.

Referenced by setIntervalFor().

unsigned int CondDBESSource::lastRun [private]

Definition at line 78 of file CondDBESSource.h.

Referenced by setIntervalFor().

Definition at line 57 of file CondDBESSource.h.

Referenced by CondDBESSource(), and fillTagCollectionFromDB().

Definition at line 60 of file CondDBESSource.h.

Referenced by CondDBESSource(), registerProxies(), setIntervalFor(), and ~CondDBESSource().

Definition at line 65 of file CondDBESSource.h.

Referenced by CondDBESSource(), and fillTagCollectionFromDB().

Definition at line 76 of file CondDBESSource.h.

Referenced by CondDBESSource(), setIntervalFor(), and ~CondDBESSource().