CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/CondCore/ESSources/plugins/CondDBESSource.cc

Go to the documentation of this file.
00001 //
00002 // Package:     CondCore/ESSources
00003 // Module:      CondDBESSource
00004 //
00005 // Description: <one line class summary>
00006 //
00007 // Implementation:
00008 //     <Notes on implementation>
00009 //
00010 // Author:      Zhen Xie
00011 //
00012 #include "CondDBESSource.h"
00013 
00014 #include "boost/shared_ptr.hpp"
00015 #include "CondCore/DBCommon/interface/Exception.h"
00016 #include "CondCore/DBCommon/interface/Auth.h"
00017 #include "CondFormats/Common/interface/Time.h"
00018 #include "CondCore/DBCommon/interface/DbTransaction.h"
00019 #include "CondCore/DBCommon/interface/ConvertIOVSyncValue.h"
00020 
00021 #include "CondCore/ESSources/interface/ProxyFactory.h"
00022 #include "CondCore/ESSources/interface/DataProxy.h"
00023 
00024 #include "CondCore/IOVService/interface/PayloadProxy.h"
00025 #include "CondCore/MetaDataService/interface/MetaData.h"
00026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00027 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00028 #include "CondCore/TagCollection/interface/TagCollectionRetriever.h"
00029 #include <exception>
00030 //#include <cstdlib>
00031 //#include <iostream>
00032 
00033 
00034 #include "CondFormats/Common/interface/TimeConversions.h"
00035 #include <iomanip>
00036 
00037 namespace {
00038   /* utility ot build the name of the plugin corresponding to a given record
00039      se ESSources
00040    */
00041   std::string
00042   buildName( std::string const & iRecordName ) {
00043     return iRecordName + std::string( "@NewProxy" );
00044   }
00045   
00046   std::string joinRecordAndLabel( std::string const & iRecordName, std::string const & iLabelName ) {
00047     return iRecordName + std::string( "@" ) + iLabelName;
00048   }
00049 
00050   /* utility class to return a IOVs associated to a given "name"
00051      This implementation return the IOV associated to a record...
00052      It is essentialy a workaround to get the full IOV out of the tag colector
00053      that is not accessible as hidden in the ESSource
00054      FIXME: need to support label??
00055    */
00056   class CondGetterFromESSource : public cond::CondGetter {
00057   public:
00058     CondGetterFromESSource(CondDBESSource::ProxyMap const & ip) : m_proxies(ip){}
00059     virtual ~CondGetterFromESSource(){}
00060 
00061     cond::IOVProxy get(std::string name) const {
00062       CondDBESSource::ProxyMap::const_iterator p = m_proxies.find(name);
00063       if ( p != m_proxies.end())
00064         return (*p).second->proxy()->iov();
00065       return cond::IOVProxy();
00066     }
00067 
00068     CondDBESSource::ProxyMap const & m_proxies;
00069   };
00070 
00071   // dump the state of a DataProxy
00072   void dumpInfo(std::ostream & out, std::string const & recName, cond::DataProxyWrapperBase const & proxy) {
00073     cond::SequenceState state(proxy.proxy()->iov().state());
00074     out << recName << " / " << proxy.label() << ": " 
00075         << proxy.connString() << ", " << proxy.tag()   << "\n  "
00076         << state.size() << ", " << state.revision()  << ", "
00077         << cond::time::to_boost(state.timestamp())     << "\n  "
00078         << state.comment()
00079         << "\n  "
00080         << "refresh " << proxy.proxy()->stats.nRefresh
00081         << "/" << proxy.proxy()->stats.nArefresh
00082         << ", reconnect " << proxy.proxy()->stats.nReconnect
00083         << "/" << proxy.proxy()->stats.nAreconnect
00084         << ", make " << proxy.proxy()->stats.nMake
00085         << ", load " << proxy.proxy()->stats.nLoad
00086       ;
00087     if ( proxy.proxy()->stats.nLoad>0) {
00088       out << "\n oids,sinces:";
00089       cond::BasePayloadProxy::ObjIds const & ids =  proxy.proxy()->stats.ids;
00090       for (cond::BasePayloadProxy::ObjIds::const_iterator id=ids.begin(); id!=ids.end(); ++id)
00091         out << " "
00092             // << std::ios::hex 
00093             << (*id).oid1 <<"-"<< (*id).oid2 <<"," 
00094             // << std::ios::dec 
00095             <<  (*id).since;
00096     }
00097   }
00098 
00099 }
00100 
00101 
00102 /*
00103  *  config Param
00104  *  RefreshEachRun: if true will refresh the IOV at each new run (or lumiSection)
00105  *  DumpStat: if true dump the statistics of all DataProxy (currently on cout)
00106  *  DBParameters: configuration set of the connection
00107  *  globaltag: The GlobalTag
00108  *  toGet: list of record label tag connection-string to add/overwrite the content of the global-tag
00109  */
00110 CondDBESSource::CondDBESSource( const edm::ParameterSet& iConfig ) :
00111   m_connection(), 
00112   m_lastRun(0),  // for the stat
00113   m_lastLumi(0),  // for the stat
00114   m_policy( NOREFRESH ),
00115   m_doDump( iConfig.getUntrackedParameter<bool>( "DumpStat", false ) )
00116 {
00117   if( iConfig.getUntrackedParameter<bool>( "RefreshAlways", false ) ) {
00118     m_policy = REFRESH_ALWAYS;
00119   }
00120   if( iConfig.getUntrackedParameter<bool>( "RefreshOpenIOVs", false ) ) {
00121     m_policy = REFRESH_OPEN_IOVS;
00122   }
00123   if( iConfig.getUntrackedParameter<bool>( "RefreshEachRun", false ) ) {
00124     m_policy = REFRESH_EACH_RUN;
00125   }
00126   if( iConfig.getUntrackedParameter<bool>( "ReconnectEachRun", false ) ) {
00127     m_policy = RECONNECT_EACH_RUN;
00128   }
00129 
00130   Stats s = {0,0,0,0,0,0,0,0};
00131   m_stats = s;  
00132   //std::cout<<"CondDBESSource::CondDBESSource"<<std::endl;
00133   /*parameter set parsing and pool environment setting
00134    */
00135   
00136   // default connection string
00137   // inproduction used for the global tag
00138   std::string userconnect= iConfig.getParameter<std::string>("connect");
00139   
00140 
00141   // connection configuration
00142   edm::ParameterSet connectionPset = iConfig.getParameter<edm::ParameterSet>( "DBParameters" );
00143   m_connection.configuration().setParameters( connectionPset );
00144   m_connection.configure();
00145   
00146 
00147   // load additional record/tag info it will overwrite the global tag
00148   std::map<std::string,cond::TagMetadata> replacement;
00149   if( iConfig.exists( "toGet" ) ) {
00150     typedef std::vector< edm::ParameterSet > Parameters;
00151     Parameters toGet = iConfig.getParameter<Parameters>( "toGet" );
00152     for( Parameters::iterator itToGet = toGet.begin(); itToGet != toGet.end(); ++itToGet ) {
00153       cond::TagMetadata nm;
00154       nm.recordname = itToGet->getParameter<std::string>( "record" );
00155       nm.labelname = itToGet->getUntrackedParameter<std::string>( "label", "" );
00156       nm.tag = itToGet->getParameter<std::string>( "tag" );
00157       nm.pfn = itToGet->getUntrackedParameter<std::string>( "connect", userconnect );
00158       //        nm.objectname=itFound->second;
00159       std::string recordLabelKey = joinRecordAndLabel( nm.recordname, nm.labelname );
00160       replacement.insert( std::pair<std::string,cond::TagMetadata>( recordLabelKey, nm ) );
00161     }
00162   }
00163   
00164   // get the global tag, merge with "replacement" store in "tagCollection"
00165   std::string globaltag;
00166   if( iConfig.exists( "globaltag" ) ) 
00167     globaltag = iConfig.getParameter<std::string>( "globaltag" );
00168   
00169   fillTagCollectionFromDB(userconnect,
00170                           iConfig.getUntrackedParameter<std::string>( "pfnPrefix", "" ),
00171                           iConfig.getUntrackedParameter<std::string>( "pfnPostfix", "" ),
00172                           globaltag,
00173                           replacement);
00174   
00175   
00176   TagCollection::iterator it;
00177   TagCollection::iterator itBeg = m_tagCollection.begin();
00178   TagCollection::iterator itEnd = m_tagCollection.end();
00179  
00180   std::map<std::string, cond::DbSession> sessions;
00181 
00182   /* load DataProxy Plugin (it is strongly typed due to EventSetup ideosyncrasis)
00183    * construct proxy
00184    * contrary to EventSetup the "object-name" is not used as identifier: multiple entries in a record are
00185    * dinstinguished only by their label...
00186    * done in two step: first create ProxyWrapper loading ALL required dictionaries
00187    * this will allow to initialize POOL in one go for each "database"
00188    * The real initialization of the Data-Proxies is done in the second loop 
00189    */
00190   std::vector<cond::DataProxyWrapperBase *> proxyWrappers(m_tagCollection.size());
00191   size_t ipb=0;
00192   for(it=itBeg;it!=itEnd;++it){
00193     proxyWrappers[ipb++] =  
00194       cond::ProxyFactory::get()->create(buildName(it->second.recordname));
00195   }
00196   // now all required libraries have been loaded
00197   // init sessions and DataProxies
00198   ipb=0;
00199   for(it=itBeg;it!=itEnd;++it){
00200     std::map<std::string, cond::DbSession>::iterator p = sessions.find( it->second.pfn );
00201     cond::DbSession nsess;
00202     if (p==sessions.end()) {
00203       //open db get tag info (i.e. the IOV token...)
00204       nsess = m_connection.createSession();
00205       nsess.openReadOnly( it->second.pfn, "" );
00206       sessions.insert(std::make_pair(it->second.pfn,nsess));
00207     } else nsess = (*p).second;
00208     //cond::MetaData metadata(nsess);
00209     //nsess.transaction().start(true);
00210     //std::string iovtoken = metadata.getToken(it->tag);
00211     // owenship...
00212     ProxyP proxy(proxyWrappers[ipb++]);
00213    //  instert in the map
00214     m_proxies.insert(std::make_pair(it->second.recordname, proxy));
00215     // initialize
00216     //proxy->lateInit(nsess,iovtoken, 
00217     //              it->labelname, it->pfn, it->tag
00218     //      );
00219     proxy->lateInit(nsess,it->second.tag, 
00220                     it->second.labelname, it->second.pfn);
00221     //nsess.transaction().commit();
00222   }
00223 
00224   // one loaded expose all other tags to the Proxy! 
00225   CondGetterFromESSource visitor( m_proxies );
00226   ProxyMap::iterator b = m_proxies.begin();
00227   ProxyMap::iterator e = m_proxies.end();
00228   for ( ;b != e; b++ ) {
00229 
00230     (*b).second->proxy()->loadMore( visitor );
00231 
00233     edm::eventsetup::EventSetupRecordKey recordKey(edm::eventsetup::EventSetupRecordKey::TypeTag::findType( (*b).first ) );
00234     if( recordKey.type() != edm::eventsetup::EventSetupRecordKey::TypeTag() ) {
00235       findingRecordWithKey( recordKey );
00236       usingRecordWithKey( recordKey );
00237     }
00238   }
00239 
00240   m_stats.nData=m_proxies.size();
00241 
00242 }
00243 
00244 
00245 CondDBESSource::~CondDBESSource() {
00246   //dump info FIXME: find a more suitable place...
00247   if (m_doDump) {
00248     std::cout << "CondDBESSource Statistics" << std::endl
00249               << "DataProxy " << m_stats.nData
00250               << " setInterval " << m_stats.nSet
00251               << " Runs " << m_stats.nRun
00252               << " Lumis " << m_stats.nLumi
00253               << " Refresh " << m_stats.nRefresh
00254               << " Actual Refresh " << m_stats.nActualRefresh
00255               << " Reconnect " << m_stats.nReconnect
00256               << " Actual Reconnect " << m_stats.nActualReconnect;
00257     std::cout << std::endl;
00258     std::cout << "Global Proxy Statistics" << std::endl
00259               << "proxy " << cond::BasePayloadProxy::gstats.nProxy
00260               << " make " << cond::BasePayloadProxy::gstats.nMake
00261               << " load " << cond::BasePayloadProxy::gstats.nLoad;
00262     std::cout << std::endl;
00263 
00264 
00265     ProxyMap::iterator b= m_proxies.begin();
00266     ProxyMap::iterator e= m_proxies.end();
00267     for ( ;b != e; b++ ) {
00268       dumpInfo( std::cout, (*b).first, *(*b).second );
00269       std::cout << "\n" << std::endl;
00270     }
00271 
00272     // FIXME
00273     // We shall eventually close transaction and session...
00274   }
00275 }
00276 
00277 
00278 //
00279 // invoked by EventSetUp: for a given record return the smallest IOV for which iTime is valid
00280 // limit to next run/lumisection of Refresh is required
00281 //
00282 void 
00283 CondDBESSource::setIntervalFor( const edm::eventsetup::EventSetupRecordKey& iKey, const edm::IOVSyncValue& iTime, edm::ValidityInterval& oInterval ){
00284 
00285   std::string recordname=iKey.name();
00286   
00287   edm::LogInfo( "CondDBESSource" ) << "Getting data for record \""<< recordname
00288                                    << "\" to be consumed by "<< iTime.eventID() << ", timestamp: " << iTime.time().value()
00289                                    << "; from CondDBESSource::setIntervalFor";
00290   
00291   m_stats.nSet++;
00292   //{
00293     // not really required, keep here for the time being
00294     if(iTime.eventID().run()!=m_lastRun) {
00295       m_lastRun=iTime.eventID().run();
00296       m_stats.nRun++;
00297     }
00298     if(iTime.luminosityBlockNumber()!=m_lastLumi) {
00299       m_lastLumi=iTime.luminosityBlockNumber();
00300       m_stats.nLumi++;
00301     }
00302     //}
00303  
00304   bool doRefresh = false;
00305   if( m_policy == REFRESH_EACH_RUN || m_policy == RECONNECT_EACH_RUN ) {
00306     // find out the last run number for the proxy of the specified record
00307     std::map<std::string,unsigned int>::iterator iRec = m_lastRecordRuns.find( recordname );
00308     if( iRec != m_lastRecordRuns.end() ){
00309       unsigned int lastRecordRun = iRec->second;
00310       if( lastRecordRun != m_lastRun ){
00311         // a refresh is required!
00312         doRefresh = true;
00313         iRec->second = m_lastRun;
00314         edm::LogInfo( "CondDBESSource" ) << "Preparing refresh for record \"" << recordname 
00315                                          << "\" since there has been a transition from run "
00316                                          << lastRecordRun << " to run " << m_lastRun
00317                                          << "; from CondDBESSource::setIntervalFor";
00318       }
00319     } else {
00320       doRefresh = true;
00321       m_lastRecordRuns.insert( std::make_pair( recordname, m_lastRun ) );
00322       edm::LogInfo( "CondDBESSource" ) << "Preparing refresh for record \"" << recordname 
00323                                        << "\" for " << iTime.eventID() << ", timestamp: " << iTime.time().value()
00324                                        << "; from CondDBESSource::setIntervalFor";
00325     }
00326     if ( !doRefresh )
00327       edm::LogInfo( "CondDBESSource" ) << "Though enabled, refresh not needed for record \"" << recordname 
00328                                        << "\" for " << iTime.eventID() << ", timestamp: " << iTime.time().value()
00329                                        << "; from CondDBESSource::setIntervalFor";
00330   } else if( m_policy == REFRESH_ALWAYS || m_policy == REFRESH_OPEN_IOVS ) {
00331     doRefresh = true;
00332     edm::LogInfo( "CondDBESSource" ) << "Forcing refresh for record \"" << recordname 
00333                                      << "\" for " << iTime.eventID() << ", timestamp: " << iTime.time().value()
00334                                      << "; from CondDBESSource::setIntervalFor";
00335   }
00336 
00337   oInterval = edm::ValidityInterval::invalidInterval();
00338 
00339   // compute the smallest interval (assume all objects have the same timetype....)                                                                                                          
00340   cond::ValidityInterval recordValidity(1,cond::TIMELIMIT);
00341   cond::TimeType timetype;
00342   bool userTime=true;
00343 
00344  //FIXME use equal_range
00345   ProxyMap::const_iterator pmBegin = m_proxies.lower_bound(recordname);
00346   ProxyMap::const_iterator pmEnd = m_proxies.upper_bound(recordname);
00347   if ( pmBegin == pmEnd ) {
00348     edm::LogInfo( "CondDBESSource" ) << "No DataProxy (Pluging) found for record \""<< recordname
00349                                      << "\"; from CondDBESSource::setIntervalFor";
00350     return;
00351   }
00352   
00353   for ( ProxyMap::const_iterator pmIter = pmBegin; pmIter != pmEnd; ++pmIter ) {
00354 
00355     edm::LogInfo( "CondDBESSource" ) << "Processing record \"" << recordname
00356                                      << "\" and label \""<< pmIter->second->label()
00357                                      << "\" for " << iTime.eventID() << ", timestamp: " << iTime.time().value()
00358                                      << "; from CondDBESSource::setIntervalFor";
00359 
00360     timetype = (*pmIter).second->proxy()->timetype();
00361     
00362     cond::Time_t abtime = cond::fromIOVSyncValue( iTime, timetype );
00363     userTime = ( 0 == abtime );
00364     
00365     //std::cout<<"abtime "<<abtime<<std::endl;
00366 
00367     if (userTime) return; //  oInterval invalid to avoid that make is called...
00368 
00369 
00370     
00371     if( doRefresh ) {
00372 
00373       std::string recKey = joinRecordAndLabel( recordname, pmIter->second->label() );
00374       TagCollection::const_iterator tcIter = m_tagCollection.find( recKey ); 
00375       if ( tcIter == m_tagCollection.end() ) {
00376         edm::LogInfo( "CondDBESSource" ) << "No Tag found for record \""<< recordname
00377                                          << "\" and label \""<< pmIter->second->label()
00378                                          << "\"; from CondDBESSource::setIntervalFor";
00379         return;
00380       }
00381 
00382       // first reconnect if required
00383       if( m_policy == RECONNECT_EACH_RUN ) {
00384         edm::LogInfo( "CondDBESSource" ) << "Checking if the session must be closed and re-opened for getting correct conditions"
00385                                          << "; from CondDBESSource::setIntervalFor";
00386         std::stringstream transId;
00387         //transId << "long" << m_lastRun;
00388         transId << m_lastRun;
00389         std::map<std::string,std::pair<cond::DbSession,std::string> >::iterator iSess = m_sessionPool.find( tcIter->second.pfn );
00390         cond::DbSession theSession;
00391         bool reopen = false;
00392         if( iSess != m_sessionPool.end() ){
00393           if( iSess->second.second != transId.str() ) {
00394             // the available session is open for a different run: reopen
00395             reopen = true;
00396             iSess->second.second = transId.str();
00397           }
00398           theSession = iSess->second.first;
00399         } else {
00400           // no available session: probably first run analysed... 
00401           theSession = m_connection.createSession(); 
00402           m_sessionPool.insert(std::make_pair( tcIter->second.pfn,std::make_pair(theSession,transId.str()) )); 
00403           reopen = true;
00404         } 
00405         if( reopen ){
00406           theSession.openReadOnly( tcIter->second.pfn, transId.str() );
00407           edm::LogInfo( "CondDBESSource" ) << "Re-opening the session with connection string " << tcIter->second.pfn
00408                                            << " and new transaction Id " <<  transId.str()
00409                                            << "; from CondDBESSource::setIntervalFor";
00410         }
00411         
00412         edm::LogInfo( "CondDBESSource" ) << "Reconnecting to \"" << tcIter->second.pfn
00413                                          << "\" for getting new payload for record \"" << recordname 
00414                                          << "\" and label \""<< pmIter->second->label()
00415                                          << "\" from IOV tag \"" << tcIter->second.tag
00416                                          << "\" to be consumed by " << iTime.eventID() << ", timestamp: " << iTime.time().value()
00417                                          << "; from CondDBESSource::setIntervalFor";
00418         bool isSizeIncreased = pmIter->second->proxy()->refresh( theSession );
00419         if( isSizeIncreased )
00420           edm::LogInfo( "CondDBESSource" ) << "After reconnecting, an increased size of the IOV sequence labeled by tag \"" << tcIter->second.tag
00421                                            << "\" was found; from CondDBESSource::setIntervalFor";
00422         m_stats.nActualReconnect += isSizeIncreased;
00423         m_stats.nReconnect++;
00424       } else {
00425         edm::LogInfo( "CondDBESSource" ) << "Refreshing IOV sequence labeled by tag \"" << tcIter->second.tag
00426                                          << "\" for getting new payload for record \"" << recordname
00427                                          << "\" and label \""<< pmIter->second->label()
00428                                          << "\" to be consumed by " << iTime.eventID() << ", timestamp: " << iTime.time().value()
00429                                          << "; from CondDBESSource::setIntervalFor";
00430         bool isSizeIncreased = pmIter->second->proxy()->refresh();
00431         if( isSizeIncreased )
00432           edm::LogInfo( "CondDBESSource" ) << "After refreshing, an increased size of the IOV sequence labeled by tag \"" << tcIter->second.tag
00433                                            << "\" was found; from CondDBESSource::setIntervalFor";
00434         m_stats.nActualRefresh += isSizeIncreased;
00435         m_stats.nRefresh++;
00436       }
00437 
00438     }
00439 
00440     /*
00441       // make oInterval valid For Ever
00442     {
00443      oInterval = edm::ValidityInterval(cond::toIOVSyncValue(recordValidity.first,  cond::runnumber, true), 
00444                                        cond::toIOVSyncValue(recordValidity.second, cond::runnumber, false));
00445      return;
00446     }    
00447     */
00448 
00449     //query the IOVSequence
00450     cond::ValidityInterval validity = (*pmIter).second->proxy()->setIntervalFor( abtime );
00451     
00452     edm::LogInfo( "CondDBESSource" ) << "Validity coming from IOV sequence for record \"" << recordname
00453                                      << "\" and label \""<< pmIter->second->label()
00454                                      << "\": (" << validity.first << ", " << validity.second
00455                                      << ") for time (type: "<< cond::timeTypeNames( timetype ) << ") " << abtime
00456                                      << "; from CondDBESSource::setIntervalFor";
00457     
00458     recordValidity.first = std::max(recordValidity.first,validity.first);
00459     recordValidity.second = std::min(recordValidity.second,validity.second);
00460   }      
00461   
00462   if( m_policy == REFRESH_OPEN_IOVS ) {
00463     doRefresh = ( recordValidity.second == cond::timeTypeSpecs[timetype].endValue );
00464     edm::LogInfo( "CondDBESSource" ) << "Validity for record \"" << recordname
00465                                      << "\" and the corresponding label(s) coming from Condition DB: (" << recordValidity.first 
00466                                      << ", "<< recordValidity.first 
00467                                      << ") as the last IOV element in the IOV sequence is infinity"
00468                                      << "; from CondDBESSource::setIntervalFor";
00469   }
00470   
00471   // to force refresh we set end-value to the minimum such an IOV can extend to: current run or lumiblock
00472     
00473   if ( (!userTime) && recordValidity.second !=0 ) {
00474     edm::IOVSyncValue start = cond::toIOVSyncValue(recordValidity.first, timetype, true);
00475     edm::IOVSyncValue stop = doRefresh  ? cond::limitedIOVSyncValue (iTime, timetype)
00476       : cond::toIOVSyncValue(recordValidity.second, timetype, false);
00477        
00478     oInterval = edm::ValidityInterval( start, stop );
00479    }
00480   
00481   edm::LogInfo( "CondDBESSource" ) << "Setting validity for record \"" << recordname 
00482                                    << "\" and corresponding label(s): starting at " << oInterval.first().eventID() << ", timestamp: " << oInterval.first().time().value()
00483                                    << ", ending at "<< oInterval.last().eventID() << ", timestamp: " << oInterval.last().time().value()
00484                                    << ", for "<< iTime.eventID() << ", timestamp: " << iTime.time().value()
00485                                    << "; from CondDBESSource::setIntervalFor";
00486 }
00487   
00488 
00489 //required by EventSetup System
00490 void 
00491 CondDBESSource::registerProxies(const edm::eventsetup::EventSetupRecordKey& iRecordKey , KeyedProxies& aProxyList) {
00492   std::string recordname=iRecordKey.name();
00493 
00494   ProxyMap::const_iterator b = m_proxies.lower_bound(recordname);
00495   ProxyMap::const_iterator e = m_proxies.upper_bound(recordname);
00496   if ( b == e) {
00497     edm::LogInfo( "CondDBESSource" ) << "No DataProxy (Pluging) found for record \""<< recordname
00498                                      << "\"; from CondDBESSource::registerProxies";
00499     return;
00500   }
00501 
00502   for (ProxyMap::const_iterator p=b;p!=e;++p) {  
00503     if(0 != (*p).second.get()) {
00504       edm::eventsetup::TypeTag type =  (*p).second->type(); 
00505       edm::eventsetup::DataKey key( type, edm::eventsetup::IdTags((*p).second->label().c_str()) );
00506       aProxyList.push_back(KeyedProxies::value_type(key,(*p).second->edmProxy()));
00507     }
00508   }
00509 }
00510 
00511 // required by the EventSetup System
00512 void 
00513 CondDBESSource::newInterval(const edm::eventsetup::EventSetupRecordKey& iRecordType,
00514                             const edm::ValidityInterval&) 
00515 {
00516   //LogDebug ("CondDBESSource")<<"newInterval";
00517   invalidateProxies(iRecordType);
00518 }
00519 
00520 
00521 // fills tagcollection merging with replacement
00522 void 
00523 CondDBESSource::fillTagCollectionFromDB( const std::string & coraldb, 
00524                                          const std::string & prefix,
00525                                          const std::string & postfix,
00526                                          const std::string & roottag,
00527                                          std::map<std::string,cond::TagMetadata>& replacement ) {
00528 
00529   std::set< cond::TagMetadata > tagcoll;
00530  
00531  if ( !roottag.empty() ) {
00532    if ( coraldb.empty() ) 
00533      throw cond::Exception( std::string( "ESSource: requested global tag ") + roottag + std::string( " but not connection string given" ) );
00534    cond::DbSession session = m_connection.createSession();
00535    session.open( coraldb, cond::Auth::COND_READER_ROLE, true );
00536    session.transaction().start( true );
00537    cond::TagCollectionRetriever tagRetriever( session, prefix, postfix );
00538    tagRetriever.getTagCollection( roottag,tagcoll );
00539    session.transaction().commit();
00540   } 
00541 
00542   std::set<cond::TagMetadata>::iterator tagCollIter;
00543   std::set<cond::TagMetadata>::iterator tagCollBegin = tagcoll.begin();
00544   std::set<cond::TagMetadata>::iterator tagCollEnd = tagcoll.end();
00545 
00546   // FIXME the logic is a bit perverse: can be surely linearized (at least simplified!) ....
00547   for( tagCollIter = tagCollBegin; tagCollIter != tagCollEnd; ++tagCollIter ) {
00548     std::string recordLabelKey = joinRecordAndLabel( tagCollIter->recordname, tagCollIter->labelname );
00549     std::map<std::string,cond::TagMetadata>::iterator fid = replacement.find( recordLabelKey );
00550     if( fid != replacement.end() ) {
00551       cond::TagMetadata tagMetadata;
00552       tagMetadata.recordname = tagCollIter->recordname;
00553       tagMetadata.labelname = tagCollIter->labelname;
00554       tagMetadata.pfn = fid->second.pfn;
00555       tagMetadata.tag = fid->second.tag;
00556       tagMetadata.objectname = tagCollIter->objectname;
00557       m_tagCollection.insert( std::make_pair( recordLabelKey, tagMetadata ) );
00558       replacement.erase( fid );
00559       edm::LogInfo( "CondDBESSource" ) << "Replacing connection string \"" << tagCollIter->pfn
00560                                        << "\" and tag \"" << tagCollIter->tag
00561                                        << "\" for record \"" << tagMetadata.recordname
00562                                        << "\" and label \"" << tagMetadata.labelname
00563                                        << "\" with connection string \"" << tagMetadata.pfn
00564                                        << "\" and tag " << tagMetadata.tag
00565                                        << "\"; from CondDBESSource::fillTagCollectionFromDB";
00566     } else {
00567       m_tagCollection.insert( std::make_pair( recordLabelKey, *tagCollIter) );
00568     }
00569   }
00570   std::map<std::string,cond::TagMetadata>::iterator replacementIter;
00571   std::map<std::string,cond::TagMetadata>::iterator replacementBegin = replacement.begin();
00572   std::map<std::string,cond::TagMetadata>::iterator replacementEnd = replacement.end();
00573   for( replacementIter = replacementBegin; replacementIter != replacementEnd; ++replacementIter ){
00574     //std::cout<<"appending"<<std::endl;
00575     //std::cout<<"pfn "<<replacementIter->second.pfn<<std::endl;
00576     //std::cout<<"objectname "<<replacementIter->second.objectname<<std::endl;
00577     //std::cout<<"tag "<<replacementIter->second.tag<<std::endl;
00578     //std::cout<<"recordname "<<replacementIter->second.recordname<<std::endl;
00579     m_tagCollection.insert( *replacementIter );
00580   }
00581 }
00582 
00583 
00584 // backward compatibility for configuration files
00585 class PoolDBESSource : public CondDBESSource {
00586 public:
00587   explicit  PoolDBESSource( const edm::ParameterSet& ps) :
00588     CondDBESSource(ps){}
00589 };
00590 
00591 #include "FWCore/Framework/interface/SourceFactory.h"
00592 //define this as a plug-in
00593 DEFINE_FWK_EVENTSETUP_SOURCE(PoolDBESSource);
00594