Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0002 
0003 #include "CondTools/L1TriggerExt/interface/DataWriterExt.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 #include <typeinfo>
0011 
0012 namespace l1t {
0013 
0014   // Default Constructor
0015   DataWriterExt::DataWriterExt() {}
0016 
0017   // Constructor Needed for Writers with Rcds dependednt on Online Producers
0018   // they need to be constructed at "construction time" to register what they produce
0019   DataWriterExt::DataWriterExt(const std::string& recordType) {
0020     WriterFactory* factory = WriterFactory::get();
0021     writer_ = factory->create(recordType + "@Writer");
0022     if (writer_.get() == nullptr) {
0023       throw cond::Exception("DataWriter: could not create WriterProxy with name " + recordType + "@Writer");
0024     }
0025     edm::LogVerbatim("L1-O2O DataWriterExt::DataWriterExt")
0026         << "Created new " << typeid(writer_).name() << "  | address " << writer_.get() << "  | rcd " << recordType;
0027   }
0028 
0029   DataWriterExt::~DataWriterExt() {}
0030 
0031   std::string DataWriterExt::writePayload(const edm::EventSetup& setup) {
0032     edm::LogVerbatim("L1-O2O DataWriterExt::writePayload") << "Will use stored writer at " << writer_.get();
0033 
0034     edm::Service<cond::service::PoolDBOutputService> poolDb;
0035     if (!poolDb.isAvailable()) {
0036       throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0037     }
0038 
0039     // 2010-02-16: Move session and transaction to WriterProxy::save().  Otherwise, if another transaction is
0040     // started while WriterProxy::save() is called (e.g. in a ESProducer like L1ConfigOnlineProdBase), the
0041     // transaction here will become read-only.
0042     std::string payloadToken = writer_->save(setup);
0043     auto& writerRef = *writer_.get();
0044     edm::LogVerbatim("L1-O2O") << typeid(writerRef).name() << " PAYLOAD TOKEN " << payloadToken;
0045 
0046     return payloadToken;
0047   }
0048 
0049   std::string DataWriterExt::writePayload(const edm::EventSetup& setup, const std::string& recordType) {
0050     WriterFactory* factory = WriterFactory::get();
0051     WriterProxyPtr writer(factory->create(recordType + "@Writer"));
0052     if (writer.get() == nullptr) {
0053       throw cond::Exception("DataWriter: could not create WriterProxy with name " + recordType + "@Writer");
0054     }
0055     writer_ = std::move(writer);
0056 
0057     edm::Service<cond::service::PoolDBOutputService> poolDb;
0058     if (!poolDb.isAvailable()) {
0059       throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0060     }
0061 
0062     // 2010-02-16: Move session and transaction to WriterProxy::save().  Otherwise, if another transaction is
0063     // started while WriterProxy::save() is called (e.g. in a ESProducer like L1ConfigOnlineProdBase), the
0064     // transaction here will become read-only.
0065     //   cond::DbSession session = poolDb->session();
0066     //   cond::DbScopedTransaction tr(session);
0067 
0068     ///  cond::persistency::TransactionScope tr(poolDb->session().transaction());
0069     //   // if throw transaction will unroll
0070     //   tr.start(false);
0071 
0072     // update key to have new payload registered for record-type pair.
0073     //  std::string payloadToken = writer->save( setup, session ) ;
0074     std::string payloadToken = writer_->save(setup);
0075     auto& writerRef = *writer_.get();
0076     edm::LogVerbatim("L1-O2O") << typeid(writerRef).name() << " PAYLOAD TOKEN " << payloadToken;
0077 
0078     ////  tr.close();
0079     //   tr.commit ();
0080 
0081     return payloadToken;
0082   }
0083 
0084   void DataWriterExt::writeKeyList(L1TriggerKeyListExt* keyList, edm::RunNumber_t sinceRun, bool logTransactions) {
0085     edm::Service<cond::service::PoolDBOutputService> poolDb;
0086     if (!poolDb.isAvailable()) {
0087       throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0088     }
0089 
0090     poolDb->forceInit();
0091     cond::persistency::Session session = poolDb->session();
0092     if (not session.transaction().isActive())
0093       session.transaction().start(false);
0094 
0095     // Write L1TriggerKeyListExt payload and save payload token before committing
0096     std::shared_ptr<L1TriggerKeyListExt> pointer(keyList);
0097     std::string payloadToken = session.storePayload(*pointer);
0098 
0099     // Commit before calling updateIOV(), otherwise PoolDBOutputService gets
0100     // confused. ??? why?
0101 
0102     // Set L1TriggerKeyListExt IOV
0103     session.transaction().commit();
0104     updateIOV("L1TriggerKeyListExtRcd", payloadToken, sinceRun, logTransactions);
0105   }
0106 
0107   bool DataWriterExt::updateIOV(const std::string& esRecordName,
0108                                 const std::string& payloadToken,
0109                                 edm::RunNumber_t sinceRun,
0110                                 bool logTransactions) {
0111     edm::LogVerbatim("L1-O2O") << esRecordName << " PAYLOAD TOKEN " << payloadToken;
0112 
0113     edm::Service<cond::service::PoolDBOutputService> poolDb;
0114     if (!poolDb.isAvailable()) {
0115       throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0116     }
0117 
0118     bool iovUpdated = true;
0119 
0120     if (poolDb->isNewTagRequest(esRecordName)) {
0121       sinceRun = poolDb->beginOfTime();
0122       poolDb->createNewIOV(payloadToken, sinceRun, esRecordName);
0123     } else {
0124       cond::TagInfo_t tagInfo;
0125       poolDb->tagInfo(esRecordName, tagInfo);
0126 
0127       if (sinceRun == 0)  // find last since and add 1
0128       {
0129         sinceRun = tagInfo.lastInterval.since;
0130         ++sinceRun;
0131       }
0132 
0133       if (tagInfo.lastInterval.payloadId != payloadToken) {
0134         poolDb->appendSinceTime(payloadToken, sinceRun, esRecordName);
0135       } else {
0136         iovUpdated = false;
0137         edm::LogVerbatim("L1-O2O") << "IOV already up to date.";
0138       }
0139     }
0140 
0141     if (iovUpdated) {
0142       edm::LogVerbatim("L1-O2O") << esRecordName << " " << poolDb->tag(esRecordName) << " SINCE " << sinceRun;
0143     }
0144 
0145     return iovUpdated;
0146   }
0147 
0148   std::string DataWriterExt::payloadToken(const std::string& recordName, edm::RunNumber_t runNumber) {
0149     edm::Service<cond::service::PoolDBOutputService> poolDb;
0150     if (!poolDb.isAvailable()) {
0151       throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0152     }
0153 
0154     // Get tag corresponding to EventSetup record name.
0155     std::string iovTag = poolDb->tag(recordName);
0156 
0157     // Get IOV token for tag.
0158     cond::persistency::Session session = poolDb->session();
0159     cond::persistency::IOVProxy iov = session.readIov(iovTag);
0160     session.transaction().start();
0161     auto iovs = iov.selectAll();
0162     std::string payloadToken("");
0163     auto iP = iovs.find(runNumber);
0164     if (iP != iovs.end()) {
0165       payloadToken = (*iP).payloadId;
0166     }
0167     session.transaction().commit();
0168     return payloadToken;
0169   }
0170 
0171   std::string DataWriterExt::lastPayloadToken(const std::string& recordName) {
0172     edm::Service<cond::service::PoolDBOutputService> poolDb;
0173     if (!poolDb.isAvailable()) {
0174       throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0175     }
0176 
0177     cond::TagInfo_t tagInfo;
0178     poolDb->tagInfo(recordName, tagInfo);
0179     return tagInfo.lastInterval.payloadId;
0180   }
0181 
0182   bool DataWriterExt::fillLastTriggerKeyList(L1TriggerKeyListExt& output) {
0183     std::string keyListToken = lastPayloadToken("L1TriggerKeyListExtRcd");
0184     if (keyListToken.empty()) {
0185       return false;
0186     } else {
0187       readObject(keyListToken, output);
0188       return true;
0189     }
0190   }
0191 
0192 }  // namespace l1t