Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-04-30 22:24:31

0001 // -*- C++ -*-
0002 //
0003 // Package:     CondLiteIO
0004 // Class  :     FWLiteESSource
0005 //
0006 /**\class FWLiteESSource FWLiteESSource.h PhysicsTools/CondLiteIO/interface/FWLiteESSource.h
0007 
0008  Description: [one line class summary]
0009 
0010  Usage:
0011     <usage>
0012 
0013 */
0014 //
0015 // Original Author:  Chris Jones
0016 //         Created:  Thu Jun 17 15:47:35 CDT 2010
0017 //
0018 
0019 // system include files
0020 #include <iostream>
0021 #include <memory>
0022 #include "TFile.h"
0023 #include <mutex>
0024 
0025 // user include files
0026 #include "FWCore/Framework/interface/ESProductResolverProvider.h"
0027 #include "FWCore/Framework/interface/ESSourceProductResolverNonConcurrentBase.h"
0028 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0029 #include "FWCore/Framework/interface/ESModuleProducesInfo.h"
0030 #include "DataFormats/FWLite/interface/EventSetup.h"
0031 #include "DataFormats/FWLite/interface/Record.h"
0032 #include "FWCore/Framework/interface/HCTypeTag.h"
0033 #include "FWCore/Utilities/interface/TypeIDBase.h"
0034 #include "FWCore/Utilities/interface/Exception.h"
0035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0036 
0037 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0038 
0039 #include "FWCore/Framework/interface/SourceFactory.h"
0040 
0041 #include "FWCore/Concurrency/interface/SerialTaskQueue.h"
0042 #include "FWCore/Concurrency/interface/WaitingTaskList.h"
0043 
0044 // forward declarations
0045 namespace edm {
0046   class EventSetupImpl;
0047 }
0048 
0049 namespace {
0050   struct TypeID : public edm::TypeIDBase {
0051     explicit TypeID(const std::type_info& iInfo) : edm::TypeIDBase(iInfo) {}
0052     TypeID() {}
0053     using TypeIDBase::typeInfo;
0054   };
0055   struct FWLiteESGenericHandle {
0056     FWLiteESGenericHandle(const TypeID& iType) : m_type(iType), m_data(nullptr), m_exception(nullptr) {}
0057 
0058     FWLiteESGenericHandle(const void* iData) : m_type(), m_data(iData), m_exception(nullptr) {}
0059 
0060     FWLiteESGenericHandle(cms::Exception* iException) : m_type(), m_data(nullptr), m_exception(iException) {}
0061 
0062     const std::type_info& typeInfo() const { return m_type.typeInfo(); }
0063 
0064     TypeID m_type;
0065     const void* m_data;
0066     cms::Exception* m_exception;
0067   };
0068 
0069   class FWLiteProductResolver : public edm::eventsetup::ESSourceProductResolverNonConcurrentBase {
0070   public:
0071     FWLiteProductResolver(const TypeID& iTypeID,
0072                           const fwlite::Record* iRecord,
0073                           edm::SerialTaskQueue* iQueue,
0074                           std::mutex* iMutex)
0075         : edm::eventsetup::ESSourceProductResolverNonConcurrentBase(iQueue, iMutex),
0076           m_type(iTypeID),
0077           m_record(iRecord),
0078           m_data{nullptr} {}
0079 
0080     void prefetch(const edm::eventsetup::DataKey& iKey, edm::EventSetupRecordDetails) final {
0081       assert(iKey.type() == m_type);
0082 
0083       FWLiteESGenericHandle h(m_type);
0084       m_record->get(h, iKey.name().value());
0085       m_data = h.m_data;
0086 
0087       if (nullptr != h.m_exception) {
0088         throw *(h.m_exception);
0089       }
0090     }
0091 
0092     void invalidateCache() override {
0093       edm::eventsetup::ESSourceProductResolverNonConcurrentBase::invalidateCache();
0094       m_data = nullptr;
0095     }
0096 
0097     void const* getAfterPrefetchImpl() const final { return m_data; }
0098 
0099   private:
0100     TypeID m_type;
0101     const fwlite::Record* m_record;
0102     void const* m_data;
0103   };
0104 }  // namespace
0105 
0106 class FWLiteESSource : public edm::eventsetup::ESProductResolverProvider, public edm::EventSetupRecordIntervalFinder {
0107 public:
0108   FWLiteESSource(edm::ParameterSet const& iPS);
0109   FWLiteESSource(const FWLiteESSource&) = delete;
0110   const FWLiteESSource& operator=(const FWLiteESSource&) = delete;
0111   ~FWLiteESSource() override;
0112 
0113   using EventSetupRecordKey = edm::eventsetup::EventSetupRecordKey;
0114 
0115 private:
0116   KeyedResolversVector registerResolvers(const EventSetupRecordKey&, unsigned int iovIndex) override;
0117 
0118   std::vector<edm::eventsetup::ESModuleProducesInfo> producesInfo() const override;
0119 
0120   void setIntervalFor(const EventSetupRecordKey&, const edm::IOVSyncValue&, edm::ValidityInterval&) override;
0121 
0122   void delaySettingRecords() override;
0123 
0124   // ---------- member data --------------------------------
0125   std::unique_ptr<TFile> m_file;
0126   fwlite::EventSetup m_es;
0127   std::map<EventSetupRecordKey, fwlite::RecordID> m_keyToID;
0128   edm::SerialTaskQueue m_queue;
0129   std::mutex m_mutex;
0130 };
0131 
0132 FWLiteESSource::FWLiteESSource(edm::ParameterSet const& iPS)
0133     : m_file(TFile::Open(iPS.getParameter<std::string>("fileName").c_str())), m_es(m_file.get()) {}
0134 
0135 FWLiteESSource::~FWLiteESSource() {}
0136 
0137 edm::eventsetup::ESProductResolverProvider::KeyedResolversVector FWLiteESSource::registerResolvers(
0138     const EventSetupRecordKey& iRecordKey, unsigned int iovIndex) {
0139   KeyedResolversVector keyedProxiesVector;
0140   using edm::eventsetup::heterocontainer::HCTypeTag;
0141 
0142   fwlite::RecordID recID = m_keyToID[iRecordKey];
0143   const fwlite::Record& rec = m_es.get(recID);
0144   typedef std::vector<std::pair<std::string, std::string> > TypesAndLabels;
0145   TypesAndLabels typesAndLabels = rec.typeAndLabelOfAvailableData();
0146 
0147   std::cout << "Looking for data in record " << iRecordKey.name() << std::endl;
0148   for (TypesAndLabels::const_iterator it = typesAndLabels.begin(), itEnd = typesAndLabels.end(); it != itEnd; ++it) {
0149     std::cout << " need type " << it->first << std::endl;
0150     HCTypeTag tt = HCTypeTag::findType(it->first);
0151     if (tt != HCTypeTag()) {
0152       edm::eventsetup::DataKey dk(tt, edm::eventsetup::IdTags(it->second.c_str()));
0153       keyedProxiesVector.emplace_back(
0154           dk, std::make_shared<FWLiteProductResolver>(TypeID(tt.value()), &rec, &m_queue, &m_mutex));
0155     } else {
0156       LogDebug("UnknownESType") << "The type '" << it->first << "' is unknown in this job";
0157       std::cout << "    *****FAILED*****" << std::endl;
0158     }
0159   }
0160   return keyedProxiesVector;
0161 }
0162 
0163 std::vector<edm::eventsetup::ESModuleProducesInfo> FWLiteESSource::producesInfo() const {
0164   std::vector<edm::eventsetup::ESModuleProducesInfo> returnValue;
0165   using edm::eventsetup::heterocontainer::HCTypeTag;
0166 
0167   for (auto const& keyID : m_keyToID) {
0168     auto const& rec = m_es.get(keyID.second);
0169     for (auto const& typeLabel : rec.typeAndLabelOfAvailableData()) {
0170       HCTypeTag tt = HCTypeTag::findType(typeLabel.first);
0171       if (tt != HCTypeTag()) {
0172         unsigned int index = returnValue.size();
0173         returnValue.emplace_back(
0174             keyID.first, edm::eventsetup::DataKey(tt, edm::eventsetup::IdTags(typeLabel.second.c_str())), index);
0175       }
0176     }
0177   }
0178 
0179   return returnValue;
0180 }
0181 
0182 void FWLiteESSource::setIntervalFor(const EventSetupRecordKey& iKey,
0183                                     const edm::IOVSyncValue& iSync,
0184                                     edm::ValidityInterval& oIOV) {
0185   m_es.syncTo(iSync.eventID(), iSync.time());
0186 
0187   const fwlite::Record& rec = m_es.get(m_keyToID[iKey]);
0188   edm::IOVSyncValue endSync(rec.endSyncValue().eventID(), rec.endSyncValue().time());
0189   if (rec.endSyncValue().eventID().run() == 0 && rec.endSyncValue().time().value() == 0ULL) {
0190     endSync = edm::IOVSyncValue::endOfTime();
0191   }
0192   oIOV = edm::ValidityInterval(edm::IOVSyncValue(rec.startSyncValue().eventID(), rec.startSyncValue().time()), endSync);
0193 }
0194 
0195 void FWLiteESSource::delaySettingRecords() {
0196   using edm::eventsetup::heterocontainer::HCTypeTag;
0197   std::vector<std::string> recordNames = m_es.namesOfAvailableRecords();
0198 
0199   for (std::vector<std::string>::const_iterator it = recordNames.begin(), itEnd = recordNames.end(); it != itEnd;
0200        ++it) {
0201     HCTypeTag t = HCTypeTag::findType(*it);
0202     if (t != HCTypeTag()) {
0203       EventSetupRecordKey key(t);
0204       findingRecordWithKey(key);
0205       usingRecordWithKey(key);
0206       m_keyToID[key] = m_es.recordID(it->c_str());
0207     }
0208   }
0209 }
0210 
0211 DEFINE_FWK_EVENTSETUP_SOURCE(FWLiteESSource);