CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/CondTools/L1Trigger/src/DataWriter.cc

Go to the documentation of this file.
00001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00002 
00003 #include "CondTools/L1Trigger/interface/DataWriter.h"
00004 #include "CondTools/L1Trigger/interface/Exception.h"
00005 #include "CondCore/MetaDataService/interface/MetaData.h"
00006 #include "CondCore/IOVService/interface/IOVProxy.h"
00007 #include "CondCore/DBCommon/interface/Exception.h"
00008 
00009 #include <utility>
00010 
00011 namespace l1t
00012 {
00013   DataWriter::DataWriter(){}
00014   DataWriter::~DataWriter(){}
00015 
00016 
00017 
00018 std::string
00019 DataWriter::writePayload( const edm::EventSetup& setup,
00020                           const std::string& recordType )
00021 {
00022   WriterFactory* factory = WriterFactory::get();
00023   std::auto_ptr<WriterProxy> writer(factory->create( recordType + "@Writer" )) ;
00024   if( writer.get() == 0 )
00025     {
00026       throw cond::Exception( "DataWriter: could not create WriterProxy with name "
00027                              + recordType + "@Writer" ) ;
00028     }
00029 
00030   edm::Service<cond::service::PoolDBOutputService> poolDb;
00031   if (!poolDb.isAvailable())
00032     {
00033       throw cond::Exception( "DataWriter: PoolDBOutputService not available."
00034                              ) ;
00035     }
00036 
00037   // 2010-02-16: Move session and transaction to WriterProxy::save().  Otherwise, if another transaction is
00038   // started while WriterProxy::save() is called (e.g. in a ESProducer like L1ConfigOnlineProdBase), the
00039   // transaction here will become read-only.
00040 //   cond::DbSession session = poolDb->session();
00041 //   cond::DbScopedTransaction tr(session);
00042 //   // if throw transaction will unroll
00043 //   tr.start(false);
00044 
00045   // update key to have new payload registered for record-type pair.
00046   //  std::string payloadToken = writer->save( setup, session ) ;
00047   std::string payloadToken = writer->save( setup ) ;
00048 
00049   edm::LogVerbatim( "L1-O2O" ) << recordType << " PAYLOAD TOKEN "
00050                                << payloadToken ;
00051 
00052 //   tr.commit ();
00053 
00054   return payloadToken ;
00055 }
00056 
00057 void
00058 DataWriter::writeKeyList( L1TriggerKeyList* keyList,
00059                           edm::RunNumber_t sinceRun,
00060                           bool logTransactions )
00061 {
00062   edm::Service<cond::service::PoolDBOutputService> poolDb;
00063   if( !poolDb.isAvailable() )
00064     {
00065       throw cond::Exception( "DataWriter: PoolDBOutputService not available."
00066                              ) ;
00067     }
00068 
00069   cond::DbSession session = poolDb->session();
00070   cond::DbScopedTransaction tr(session);
00071   tr.start(false);
00072 
00073   // Write L1TriggerKeyList payload and save payload token before committing
00074   boost::shared_ptr<L1TriggerKeyList> pointer(keyList);
00075   std::string payloadToken = session.storeObject(pointer.get(),
00076                                                  cond::classNameForTypeId(typeid(L1TriggerKeyList))
00077                                                  );
00078                         
00079   // Commit before calling updateIOV(), otherwise PoolDBOutputService gets
00080   // confused.
00081   tr.commit ();
00082   
00083   // Set L1TriggerKeyList IOV
00084   updateIOV( "L1TriggerKeyListRcd",
00085              payloadToken,
00086              sinceRun,
00087              logTransactions ) ;
00088 }
00089 
00090 bool
00091 DataWriter::updateIOV( const std::string& esRecordName,
00092                        const std::string& payloadToken,
00093                        edm::RunNumber_t sinceRun,
00094                        bool logTransactions )
00095 {
00096   edm::LogVerbatim( "L1-O2O" ) << esRecordName
00097                                << " PAYLOAD TOKEN " << payloadToken ;
00098 
00099   edm::Service<cond::service::PoolDBOutputService> poolDb;
00100   if (!poolDb.isAvailable())
00101     {
00102       throw cond::Exception( "DataWriter: PoolDBOutputService not available."
00103                              ) ;
00104     }
00105 
00106   bool iovUpdated = true ;
00107 
00108   if( poolDb->isNewTagRequest( esRecordName ) )
00109     {
00110       sinceRun = poolDb->beginOfTime() ;
00111       poolDb->createNewIOV( payloadToken,
00112                             sinceRun,
00113                             poolDb->endOfTime(),
00114                             esRecordName,
00115                             logTransactions ) ;
00116     }
00117   else
00118     {   
00119       cond::TagInfo tagInfo ;
00120       poolDb->tagInfo( esRecordName, tagInfo ) ;
00121 
00122       if( sinceRun == 0 ) // find last since and add 1
00123         {
00124           sinceRun = tagInfo.lastInterval.first ;
00125           ++sinceRun ;
00126         }
00127 
00128       if( tagInfo.lastPayloadToken != payloadToken )
00129         {
00130           poolDb->appendSinceTime( payloadToken,
00131                                    sinceRun,
00132                                    esRecordName,
00133                                    logTransactions ) ;
00134         }
00135       else
00136         {
00137           iovUpdated = false ;
00138           edm::LogVerbatim( "L1-O2O" ) << "IOV already up to date." ;
00139         }
00140     }
00141 
00142   if( iovUpdated )
00143     {
00144       edm::LogVerbatim( "L1-O2O" ) << esRecordName << " "
00145                                    << poolDb->tag( esRecordName )
00146                                    << " SINCE " << sinceRun ;
00147     }
00148 
00149   return iovUpdated ;
00150 }
00151 
00152 std::string
00153 DataWriter::payloadToken( const std::string& recordName,
00154                           edm::RunNumber_t runNumber )
00155 {
00156   edm::Service<cond::service::PoolDBOutputService> poolDb;
00157   if( !poolDb.isAvailable() )
00158     {
00159       throw cond::Exception( "DataWriter: PoolDBOutputService not available."
00160                              ) ;
00161     }
00162 
00163   // Get tag corresponding to EventSetup record name.
00164   std::string iovTag = poolDb->tag( recordName ) ;
00165 
00166   // Get IOV token for tag.
00167   cond::DbSession session = poolDb->session();
00168   cond::DbScopedTransaction tr(session);
00169   tr.start(true);
00170   cond::MetaData metadata(session ) ;
00171   std::string iovToken ;
00172   if( metadata.hasTag( iovTag ) )
00173     {
00174       iovToken = metadata.getToken( iovTag ) ;
00175     }
00176   if( iovToken.empty() )
00177     {
00178       return std::string() ;
00179     }
00180 
00181   // Get payload token for run number.
00182 
00183   cond::IOVProxy iov( session );
00184   iov.load(iovToken ) ;
00185   std::string payloadToken("");
00186   cond::IOVProxy::const_iterator iP = iov.find( runNumber );
00187   if( iP != iov.end() ){
00188     payloadToken = iP->token(); 
00189   }
00190   tr.commit() ;
00191   return payloadToken ;
00192 }
00193 
00194 std::string
00195 DataWriter::lastPayloadToken( const std::string& recordName )
00196 {
00197   edm::Service<cond::service::PoolDBOutputService> poolDb;
00198   if( !poolDb.isAvailable() )
00199     {
00200       throw cond::Exception( "DataWriter: PoolDBOutputService not available."
00201                              ) ;
00202     }
00203 
00204   cond::TagInfo tagInfo ;
00205   poolDb->tagInfo( recordName, tagInfo ) ;
00206   return tagInfo.lastPayloadToken ;
00207 }
00208 
00209 bool
00210 DataWriter::fillLastTriggerKeyList( L1TriggerKeyList& output )
00211 {
00212   std::string keyListToken =
00213     lastPayloadToken( "L1TriggerKeyListRcd" ) ;
00214   if( keyListToken.empty() )
00215     {
00216       return false ;
00217     }
00218   else
00219     {
00220       readObject( keyListToken, output ) ;
00221       return true ;
00222     }
00223 }
00224 
00225 } // ns