Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:13

0001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0002 
0003 #include "CondTools/L1Trigger/interface/DataWriter.h"
0004 #include "CondTools/L1Trigger/interface/Exception.h"
0005 #include "CondCore/CondDB/interface/Exception.h"
0006 
0007 #include "CondCore/CondDB/interface/Serialization.h"
0008 
0009 #include <utility>
0010 
0011 namespace l1t {
0012   DataWriter::DataWriter() {}
0013   DataWriter::~DataWriter() {}
0014 
0015   std::string DataWriter::writePayload(const edm::EventSetup& setup, const std::string& recordType) {
0016     WriterFactory* factory = WriterFactory::get();
0017     std::unique_ptr<WriterProxy> writer(factory->create(recordType + "@Writer"));
0018     if (writer.get() == nullptr) {
0019       throw cond::Exception("DataWriter: could not create WriterProxy with name " + recordType + "@Writer");
0020     }
0021 
0022     edm::Service<cond::service::PoolDBOutputService> poolDb;
0023     if (!poolDb.isAvailable()) {
0024       throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0025     }
0026 
0027     // 2010-02-16: Move session and transaction to WriterProxy::save().  Otherwise, if another transaction is
0028     // started while WriterProxy::save() is called (e.g. in a ESProducer like L1ConfigOnlineProdBase), the
0029     // transaction here will become read-only.
0030     //   cond::DbSession session = poolDb->session();
0031     //   cond::DbScopedTransaction tr(session);
0032 
0033     cond::persistency::TransactionScope tr(poolDb->session().transaction());
0034     //   // if throw transaction will unroll
0035     //   tr.start(false);
0036 
0037     // update key to have new payload registered for record-type pair.
0038     //  std::string payloadToken = writer->save( setup, session ) ;
0039     std::string payloadToken = writer->save(setup);
0040 
0041     edm::LogVerbatim("L1-O2O") << recordType << " PAYLOAD TOKEN " << payloadToken;
0042 
0043     tr.close();
0044     //   tr.commit ();
0045 
0046     return payloadToken;
0047   }
0048 
0049   void DataWriter::writeKeyList(L1TriggerKeyList* keyList, edm::RunNumber_t sinceRun, bool logTransactions) {
0050     edm::Service<cond::service::PoolDBOutputService> poolDb;
0051     if (!poolDb.isAvailable()) {
0052       throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0053     }
0054 
0055     cond::persistency::Session session = poolDb->session();
0056     cond::persistency::TransactionScope tr(session.transaction());
0057     ///  tr.start( false );
0058 
0059     // Write L1TriggerKeyList payload and save payload token before committing
0060     std::shared_ptr<L1TriggerKeyList> pointer(keyList);
0061     std::string payloadToken = session.storePayload(*pointer);
0062 
0063     // Commit before calling updateIOV(), otherwise PoolDBOutputService gets
0064     // confused.
0065     //tr.commit ();
0066     tr.close();
0067 
0068     // Set L1TriggerKeyList IOV
0069     updateIOV("L1TriggerKeyListRcd", payloadToken, sinceRun, logTransactions);
0070   }
0071 
0072   bool DataWriter::updateIOV(const std::string& esRecordName,
0073                              const std::string& payloadToken,
0074                              edm::RunNumber_t sinceRun,
0075                              bool logTransactions) {
0076     edm::LogVerbatim("L1-O2O") << esRecordName << " PAYLOAD TOKEN " << payloadToken;
0077 
0078     edm::Service<cond::service::PoolDBOutputService> poolDb;
0079     if (!poolDb.isAvailable()) {
0080       throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0081     }
0082 
0083     bool iovUpdated = true;
0084 
0085     if (poolDb->isNewTagRequest(esRecordName)) {
0086       sinceRun = poolDb->beginOfTime();
0087       poolDb->createNewIOV(payloadToken, sinceRun, esRecordName);
0088     } else {
0089       cond::TagInfo_t tagInfo;
0090       poolDb->tagInfo(esRecordName, tagInfo);
0091 
0092       if (sinceRun == 0)  // find last since and add 1
0093       {
0094         sinceRun = tagInfo.lastInterval.since;
0095         ++sinceRun;
0096       }
0097 
0098       if (tagInfo.lastInterval.payloadId != payloadToken) {
0099         poolDb->appendSinceTime(payloadToken, sinceRun, esRecordName);
0100       } else {
0101         iovUpdated = false;
0102         edm::LogVerbatim("L1-O2O") << "IOV already up to date.";
0103       }
0104     }
0105 
0106     if (iovUpdated) {
0107       edm::LogVerbatim("L1-O2O") << esRecordName << " " << poolDb->tag(esRecordName) << " SINCE " << sinceRun;
0108     }
0109 
0110     return iovUpdated;
0111   }
0112 
0113   std::string DataWriter::payloadToken(const std::string& recordName, edm::RunNumber_t runNumber) {
0114     edm::Service<cond::service::PoolDBOutputService> poolDb;
0115     if (!poolDb.isAvailable()) {
0116       throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0117     }
0118 
0119     // Get tag corresponding to EventSetup record name.
0120     std::string iovTag = poolDb->tag(recordName);
0121 
0122     // Get IOV token for tag.
0123     cond::persistency::Session session = poolDb->session();
0124     cond::persistency::IOVProxy iov = session.readIov(iovTag);
0125     session.transaction().start();
0126     auto iovs = iov.selectAll();
0127     std::string payloadToken("");
0128     auto iP = iovs.find(runNumber);
0129     if (iP != iovs.end()) {
0130       payloadToken = (*iP).payloadId;
0131     }
0132     session.transaction().commit();
0133     return payloadToken;
0134   }
0135 
0136   std::string DataWriter::lastPayloadToken(const std::string& recordName) {
0137     edm::Service<cond::service::PoolDBOutputService> poolDb;
0138     if (!poolDb.isAvailable()) {
0139       throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0140     }
0141 
0142     cond::TagInfo_t tagInfo;
0143     poolDb->tagInfo(recordName, tagInfo);
0144     return tagInfo.lastInterval.payloadId;
0145   }
0146 
0147   bool DataWriter::fillLastTriggerKeyList(L1TriggerKeyList& output) {
0148     std::string keyListToken = lastPayloadToken("L1TriggerKeyListRcd");
0149     if (keyListToken.empty()) {
0150       return false;
0151     } else {
0152       readObject(keyListToken, output);
0153       return true;
0154     }
0155   }
0156 
0157 }  // namespace l1t