Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <sstream>
0002 
0003 #include "CondTools/L1TriggerExt/plugins/L1CondDBIOVWriterExt.h"
0004 #include "CondTools/L1TriggerExt/interface/DataWriterExt.h"
0005 
0006 #include "CondFormats/L1TObjects/interface/L1TriggerKeyExt.h"
0007 #include "CondFormats/DataRecord/interface/L1TriggerKeyExtRcd.h"
0008 #include "CondFormats/L1TObjects/interface/L1TriggerKeyListExt.h"
0009 #include "CondFormats/DataRecord/interface/L1TriggerKeyListExtRcd.h"
0010 
0011 #include "CondCore/CondDB/interface/Serialization.h"
0012 
0013 L1CondDBIOVWriterExt::L1CondDBIOVWriterExt(const edm::ParameterSet& iConfig)
0014     : m_tscKey(iConfig.getParameter<std::string>("tscKey")),
0015       m_rsKey(iConfig.getParameter<std::string>("rsKey")),
0016       m_ignoreTriggerKey(iConfig.getParameter<bool>("ignoreTriggerKey")),
0017       m_logKeys(iConfig.getParameter<bool>("logKeys")),
0018       m_logTransactions(iConfig.getParameter<bool>("logTransactions")),
0019       m_forceUpdate(iConfig.getParameter<bool>("forceUpdate")) {
0020   //now do what ever initialization is needed
0021   typedef std::vector<edm::ParameterSet> ToSave;
0022   ToSave toSave = iConfig.getParameter<ToSave>("toPut");
0023   for (ToSave::const_iterator it = toSave.begin(); it != toSave.end(); it++) {
0024     std::string record = it->getParameter<std::string>("record");
0025     std::string type = it->getParameter<std::string>("type");
0026     m_recordTypes.push_back(record + "@" + type);
0027   }
0028   l1TriggerKeyExtToken_ = esConsumes();
0029 }
0030 
0031 L1CondDBIOVWriterExt::~L1CondDBIOVWriterExt() {
0032   // do anything here that needs to be done at desctruction time
0033   // (e.g. close files, deallocate resources etc.)
0034 }
0035 
0036 // ------------ method called to for each event  ------------
0037 void L1CondDBIOVWriterExt::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0038   using namespace edm;
0039 
0040   // Get L1TriggerKeyListExt
0041   L1TriggerKeyListExt keyList;
0042   l1t::DataWriterExt dataWriter;
0043   if (!dataWriter.fillLastTriggerKeyList(keyList)) {
0044     edm::LogError("L1-O2O") << "Problem getting last L1TriggerKeyListExt";
0045   }
0046 
0047   unsigned long long run = iEvent.id().run();
0048 
0049   L1TriggerKeyExt::RecordToKey recordTypeToKeyMap;
0050 
0051   bool triggerKeyIOVUpdated = true;
0052 
0053   // Start log string, convert run number into string
0054   std::stringstream ss;
0055   ss << run;
0056   std::string log = "KEYLOG runNumber=" + ss.str();
0057   bool logRecords = true;
0058 
0059   std::string m_Key = m_tscKey + ":" + m_rsKey;
0060 
0061   if (!m_ignoreTriggerKey) {
0062     if (!m_tscKey.empty() && !m_rsKey.empty()) {
0063       edm::LogVerbatim("L1-O2O") << "Object key for L1TriggerKeyExt@L1TriggerKeyExtRcd: " << m_tscKey << " : "
0064                                  << m_rsKey;
0065 
0066       // Use TSC key and L1TriggerKeyListExt to find next run's
0067       // L1TriggerKey token
0068       std::string keyToken = keyList.token(m_Key);
0069 
0070       // Update IOV sequence for this token with since-time = new run
0071       triggerKeyIOVUpdated = m_writer.updateIOV("L1TriggerKeyExtRcd", keyToken, run, m_logTransactions);
0072 
0073       // Read current L1TriggerKeyExt directly from ORCON using token
0074       L1TriggerKeyExt key;
0075       m_writer.readObject(keyToken, key);
0076 
0077       recordTypeToKeyMap = key.recordToKeyMap();
0078 
0079       // Replace spaces in key with ?s.  Do reverse substitution when
0080       // making L1TriggerKeyExt.
0081       std::string tmpKey = m_Key;
0082       replace(tmpKey.begin(), tmpKey.end(), ' ', '?');
0083       log += " tscKey:rsKey=" + tmpKey;
0084       logRecords = false;
0085     } else {
0086       // For use with Run Settings, no corresponding L1TrigerKey in
0087       // ORCON.
0088 
0089       // Get L1TriggerKeyExt from EventSetup
0090       auto esKey = iSetup.getHandle(l1TriggerKeyExtToken_);
0091 
0092       recordTypeToKeyMap = esKey->recordToKeyMap();
0093     }
0094   } else {
0095     std::vector<std::string>::const_iterator recordTypeItr = m_recordTypes.begin();
0096     std::vector<std::string>::const_iterator recordTypeEnd = m_recordTypes.end();
0097 
0098     for (; recordTypeItr != recordTypeEnd; ++recordTypeItr) {
0099       recordTypeToKeyMap.insert(std::make_pair(*recordTypeItr, m_Key));
0100     }
0101   }
0102 
0103   // If L1TriggerKeyExt IOV was already up to date, then so are all its
0104   // sub-records.
0105   bool throwException = false;
0106 
0107   if (triggerKeyIOVUpdated || m_forceUpdate) {
0108     // Loop over record@type in L1TriggerKeyExt
0109     L1TriggerKeyExt::RecordToKey::const_iterator itr = recordTypeToKeyMap.begin();
0110     L1TriggerKeyExt::RecordToKey::const_iterator end = recordTypeToKeyMap.end();
0111 
0112     for (; itr != end; ++itr) {
0113       std::string recordType = itr->first;
0114       std::string objectKey = itr->second;
0115 
0116       std::string recordName(recordType, 0, recordType.find_first_of('@'));
0117 
0118       if (logRecords) {
0119         // Replace spaces in key with ?s.  Do reverse substitution when
0120         // making L1TriggerKeyExt.
0121         std::string tmpKey = objectKey;
0122         replace(tmpKey.begin(), tmpKey.end(), ' ', '?');
0123         log += " " + recordName + "Key=" + tmpKey;
0124       }
0125 
0126       // Do nothing if object key is null.
0127       if (objectKey == L1TriggerKeyExt::kNullKey) {
0128         edm::LogVerbatim("L1-O2O") << "L1CondDBIOVWriterExt: null object key for " << recordType
0129                                    << "; skipping this record.";
0130       } else {
0131         // Find payload token
0132         edm::LogVerbatim("L1-O2O") << "Object key for " << recordType << ": " << objectKey;
0133 
0134         std::string payloadToken = keyList.token(recordType, objectKey);
0135         if (payloadToken.empty()) {
0136           edm::LogVerbatim("L1-O2O") << "L1CondDBIOVWriterExt: empty payload token for " + recordType + ", key " +
0137                                             objectKey;
0138 
0139           throwException = true;
0140         } else {
0141           m_writer.updateIOV(recordName, payloadToken, run, m_logTransactions);
0142         }
0143       }
0144     }
0145   }
0146 
0147   if (m_logKeys) {
0148     edm::LogVerbatim("L1-O2O") << log;
0149   }
0150 
0151   if (throwException) {
0152     throw cond::Exception("L1CondDBIOVWriterExt: empty payload tokens");
0153   }
0154 }
0155 
0156 // ------------ method called once each job just before starting event loop  ------------
0157 void L1CondDBIOVWriterExt::beginJob() {}
0158 
0159 // ------------ method called once each job just after ending the event loop  ------------
0160 void L1CondDBIOVWriterExt::endJob() {}
0161 
0162 //define this as a plug-in
0163 //DEFINE_FWK_MODULE(L1CondDBIOVWriterExt);