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 "CondFormats/L1TObjects/interface/L1TriggerKeyExt.h"
0004 #include "CondFormats/DataRecord/interface/L1TriggerKeyExtRcd.h"
0005 #include "CondFormats/L1TObjects/interface/L1TriggerKeyListExt.h"
0006 #include "CondFormats/DataRecord/interface/L1TriggerKeyListExtRcd.h"
0007 
0008 #include <memory>
0009 
0010 #include "FWCore/Framework/interface/Frameworkfwd.h"
0011 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/MakerMacros.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/Utilities/interface/ESGetToken.h"
0016 #include "CondTools/L1TriggerExt/interface/DataWriterExt.h"
0017 
0018 using DataWriterExtPtr = std::unique_ptr<l1t::DataWriterExt>;
0019 using RecordToWriterMap = std::map<std::string, DataWriterExtPtr>;
0020 
0021 class L1CondDBPayloadWriterExt : public edm::one::EDAnalyzer<> {
0022 public:
0023   explicit L1CondDBPayloadWriterExt(const edm::ParameterSet&);
0024   ~L1CondDBPayloadWriterExt() override;
0025 
0026 private:
0027   void beginJob() override;
0028   void analyze(const edm::Event&, const edm::EventSetup&) override;
0029   void endJob() override;
0030 
0031   // ----------member data ---------------------------
0032   RecordToWriterMap m_rcdToWriterMap;
0033   // std::string m_tag ; // tag is known by PoolDBOutputService
0034 
0035   // set to false to write config data without valid TSC key
0036   bool m_writeL1TriggerKeyExt;
0037 
0038   // set to false to write config data only
0039   bool m_writeConfigData;
0040 
0041   // substitute new payload tokens for existing keys in L1TriggerKeyListExt
0042   bool m_overwriteKeys;
0043 
0044   bool m_logTransactions;
0045 
0046   // if true, do not retrieve L1TriggerKeyListExt from EventSetup
0047   bool m_newL1TriggerKeyListExt;
0048 
0049   // Token to access L1TriggerKeyExt data in the event setup
0050   edm::ESGetToken<L1TriggerKeyExt, L1TriggerKeyExtRcd> theL1TriggerKeyExtToken_;
0051 };
0052 
0053 L1CondDBPayloadWriterExt::L1CondDBPayloadWriterExt(const edm::ParameterSet& iConfig)
0054     : m_writeL1TriggerKeyExt(iConfig.getParameter<bool>("writeL1TriggerKeyExt")),
0055       m_writeConfigData(iConfig.getParameter<bool>("writeConfigData")),
0056       m_overwriteKeys(iConfig.getParameter<bool>("overwriteKeys")),
0057       m_logTransactions(iConfig.getParameter<bool>("logTransactions")),
0058       m_newL1TriggerKeyListExt(iConfig.getParameter<bool>("newL1TriggerKeyListExt")),
0059       theL1TriggerKeyExtToken_(esConsumes()) {
0060   auto cc = consumesCollector();
0061   for (const auto& sysWriter : iConfig.getParameter<std::vector<std::string>>("sysWriters")) {
0062     //construct writer
0063     DataWriterExtPtr writer = std::make_unique<l1t::DataWriterExt>(sysWriter);
0064     writer->getWriter()->setToken(cc);
0065     m_rcdToWriterMap[sysWriter] = std::move(writer);  //the sysWriter holds info in 'rcd@prod' format
0066   }
0067 }
0068 
0069 L1CondDBPayloadWriterExt::~L1CondDBPayloadWriterExt() {
0070   // do anything here that needs to be done at desctruction time
0071   // (e.g. close files, deallocate resources etc.)
0072 }
0073 
0074 // ------------ method called to for each event  ------------
0075 void L1CondDBPayloadWriterExt::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0076   using namespace edm;
0077 
0078   // Get L1TriggerKeyListExt and make a copy
0079   L1TriggerKeyListExt oldKeyList;
0080   l1t::DataWriterExt& m_writer = *m_rcdToWriterMap.at("L1TriggerKeyExtRcd@L1TriggerKeyExt");
0081 
0082   if (not(m_newL1TriggerKeyListExt or m_writer.fillLastTriggerKeyList(oldKeyList)))
0083     edm::LogError("L1-O2O") << "Problem getting last L1TriggerKeyListExt";
0084 
0085   L1TriggerKeyListExt* keyList = nullptr;
0086 
0087   // Write L1TriggerKeyExt to ORCON with no IOV
0088   std::string token;
0089   L1TriggerKeyExt key;
0090   // Before calling writePayload(), check if TSC key is already in
0091   // L1TriggerKeyListExt.  writePayload() will not catch this situation in
0092   // the case of dummy configurations.
0093   bool triggerKeyOK = true;
0094   try {
0095     // Get L1TriggerKeyExt
0096     key = iSetup.getData(theL1TriggerKeyExtToken_);
0097     if (!m_overwriteKeys) {
0098       triggerKeyOK = oldKeyList.token(key.tscKey()).empty();
0099     }
0100   } catch (l1t::DataAlreadyPresentException& ex) {
0101     triggerKeyOK = false;
0102     edm::LogVerbatim("L1-O2O") << ex.what();
0103   }
0104 
0105   if (triggerKeyOK and m_writeL1TriggerKeyExt) {
0106     edm::LogVerbatim("L1-O2O") << "Object key for L1TriggerKeyExtRcd@L1TriggerKeyExt: " << key.tscKey()
0107                                << " (about to run writePayload)";
0108     token = m_writer.writePayload(iSetup);
0109   }
0110 
0111   // If L1TriggerKeyExt is invalid (empty), then all configuration data is already in DB
0112   // m_writeL1TriggerKeyExt the naming is misleading,
0113   // the bool is used to say to the module whether it runs or not to update a L1TriggerKeyExtRcd
0114   // (so if no payload for L1TriggerKeyExtRcd AND you run for updating L1TriggerKeyExtRcd ==> you have nothing to update)
0115   if (token.empty() and m_writeL1TriggerKeyExt) {
0116     edm::LogInfo("L1CondDBPayloadWriterExt::analyze") << " token = " << token;
0117     return;
0118   }
0119 
0120   // Record token in L1TriggerKeyListExt
0121   if (m_writeL1TriggerKeyExt) {
0122     keyList = new L1TriggerKeyListExt(oldKeyList);
0123     if (not keyList->addKey(key.tscKey(), token, m_overwriteKeys))
0124       throw cond::Exception("L1CondDBPayloadWriter: TSC key " + key.tscKey() + " already in L1TriggerKeyListExt");
0125   }
0126 
0127   if (not m_writeConfigData) {
0128     // Write L1TriggerKeyListExt to ORCON
0129     if (keyList)
0130       m_writer.writeKeyList(keyList, 0, m_logTransactions);
0131     return;
0132   }
0133 
0134   // Loop over record@type in L1TriggerKeyExt
0135   // (as before make writers, try to write payload and if needed handle exceptions)
0136 
0137   // this is not needed maps have implemented begin and end methods for their iterators
0138   // L1TriggerKeyExt::RecordToKey::const_iterator it = key.recordToKeyMap().begin();
0139   // L1TriggerKeyExt::RecordToKey::const_iterator end = key.recordToKeyMap().end();
0140   // for (; it != end; ++it) {
0141 
0142   bool throwException = false;
0143   for (const auto& it : key.recordToKeyMap()) {
0144     // If there isn't any WriterProxyT constructed for this rcd, continue
0145     // (the missing rcds are left out for a reason - those are static that throw exceptions that cannot be handled in 12_3)
0146     if (m_rcdToWriterMap.find(it.first) == m_rcdToWriterMap.end())
0147       continue;
0148 
0149     // Do nothing if object key is null.
0150     // (Panos) this might not be working as the "empty" keys are L1TriggerKeyExt::kEmptyKey (std::string(""))
0151     if (it.second == L1TriggerKeyExt::kNullKey) {
0152       edm::LogVerbatim("L1-O2O") << "L1CondDBPayloadWriter: null object key for " << it.first
0153                                  << "; skipping this record.";
0154       continue;
0155     }
0156 
0157     // Check key is new before writing
0158     if (oldKeyList.token(it.first, it.second).empty() || m_overwriteKeys) {
0159       // Write data to ORCON with no IOV
0160       if (!oldKeyList.token(it.first, it.second).empty()) {
0161         edm::LogVerbatim("L1-O2O") << "*** Overwriting payload: object key for " << it.first << ": " << it.second;
0162       } else {
0163         edm::LogVerbatim("L1-O2O") << "object key for " << it.first << ": " << it.second;
0164       }
0165 
0166       try {
0167         edm::LogVerbatim("L1-O2O") << "about to run writePayload for " << it.first;
0168         token = m_rcdToWriterMap.at(it.first)->writePayload(iSetup);
0169       } catch (l1t::DataInvalidException& ex) {
0170         edm::LogVerbatim("L1-O2O") << ex.what() << " Skipping to next record.";
0171         throwException = true;
0172         continue;
0173       }
0174 
0175       if (!token.empty()) {
0176         // Record token in L1TriggerKeyListExt
0177         if (!keyList)
0178           keyList = new L1TriggerKeyListExt(oldKeyList);
0179         // The following should never happen because of the check
0180         // above, but just in case....
0181         if (!(keyList->addKey(it.first, it.second, token, m_overwriteKeys)))
0182           throw cond::Exception("L1CondDBPayloadWriter")
0183               << "subsystem key " << it.second << " for " << it.first << " already in L1TriggerKeyListExt";
0184       }
0185 
0186     } else
0187       edm::LogVerbatim("L1-O2O") << "L1CondDBPayloadWriter: object key " << it.second << " for " << it.first
0188                                  << " already in L1TriggerKeyListExt";
0189 
0190   }  // for rcds from keys
0191 
0192   if (keyList)  // Write L1TriggerKeyListExt to ORCON
0193     m_writer.writeKeyList(keyList, 0, m_logTransactions);
0194 
0195   if (throwException)
0196     throw l1t::DataInvalidException("Payload problem found.");
0197 }
0198 
0199 // ------------ method called once each job just before starting event loop  ------------
0200 void L1CondDBPayloadWriterExt::beginJob() {}
0201 
0202 // ------------ method called once each job just after ending the event loop  ------------
0203 void L1CondDBPayloadWriterExt::endJob() {}
0204 
0205 //define this as a plug-in
0206 DEFINE_FWK_MODULE(L1CondDBPayloadWriterExt);