Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:57:44

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 "DataFormats/FWLite/interface/EventSetup.h"
0030 #include "DataFormats/FWLite/interface/Record.h"
0031 #include "FWCore/Framework/interface/HCTypeTag.h"
0032 #include "FWCore/Utilities/interface/TypeIDBase.h"
0033 #include "FWCore/Utilities/interface/Exception.h"
0034 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0035 
0036 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0037 
0038 #include "FWCore/Framework/interface/SourceFactory.h"
0039 
0040 #include "FWCore/Concurrency/interface/SerialTaskQueue.h"
0041 #include "FWCore/Concurrency/interface/WaitingTaskList.h"
0042 
0043 // forward declarations
0044 namespace edm {
0045   class EventSetupImpl;
0046 }
0047 
0048 namespace {
0049   struct TypeID : public edm::TypeIDBase {
0050     explicit TypeID(const std::type_info& iInfo) : edm::TypeIDBase(iInfo) {}
0051     TypeID() {}
0052     using TypeIDBase::typeInfo;
0053   };
0054   struct FWLiteESGenericHandle {
0055     FWLiteESGenericHandle(const TypeID& iType) : m_type(iType), m_data(nullptr), m_exception(nullptr) {}
0056 
0057     FWLiteESGenericHandle(const void* iData) : m_type(), m_data(iData), m_exception(nullptr) {}
0058 
0059     FWLiteESGenericHandle(cms::Exception* iException) : m_type(), m_data(nullptr), m_exception(iException) {}
0060 
0061     const std::type_info& typeInfo() const { return m_type.typeInfo(); }
0062 
0063     TypeID m_type;
0064     const void* m_data;
0065     cms::Exception* m_exception;
0066   };
0067 
0068   class FWLiteProductResolver : public edm::eventsetup::ESSourceProductResolverNonConcurrentBase {
0069   public:
0070     FWLiteProductResolver(const TypeID& iTypeID,
0071                           const fwlite::Record* iRecord,
0072                           edm::SerialTaskQueue* iQueue,
0073                           std::mutex* iMutex)
0074         : edm::eventsetup::ESSourceProductResolverNonConcurrentBase(iQueue, iMutex),
0075           m_type(iTypeID),
0076           m_record(iRecord),
0077           m_data{nullptr} {}
0078 
0079     void prefetch(const edm::eventsetup::DataKey& iKey, edm::EventSetupRecordDetails) final {
0080       assert(iKey.type() == m_type);
0081 
0082       FWLiteESGenericHandle h(m_type);
0083       m_record->get(h, iKey.name().value());
0084       m_data = h.m_data;
0085 
0086       if (nullptr != h.m_exception) {
0087         throw *(h.m_exception);
0088       }
0089     }
0090 
0091     void invalidateCache() override {
0092       edm::eventsetup::ESSourceProductResolverNonConcurrentBase::invalidateCache();
0093       m_data = nullptr;
0094     }
0095 
0096     void const* getAfterPrefetchImpl() const final { return m_data; }
0097 
0098   private:
0099     TypeID m_type;
0100     const fwlite::Record* m_record;
0101     void const* m_data;
0102   };
0103 }  // namespace
0104 
0105 class FWLiteESSource : public edm::eventsetup::ESProductResolverProvider, public edm::EventSetupRecordIntervalFinder {
0106 public:
0107   FWLiteESSource(edm::ParameterSet const& iPS);
0108   FWLiteESSource(const FWLiteESSource&) = delete;
0109   const FWLiteESSource& operator=(const FWLiteESSource&) = delete;
0110   ~FWLiteESSource() override;
0111 
0112   using EventSetupRecordKey = edm::eventsetup::EventSetupRecordKey;
0113 
0114 private:
0115   KeyedResolversVector registerResolvers(const EventSetupRecordKey&, unsigned int iovIndex) override;
0116 
0117   void setIntervalFor(const EventSetupRecordKey&, const edm::IOVSyncValue&, edm::ValidityInterval&) override;
0118 
0119   void delaySettingRecords() override;
0120 
0121   // ---------- member data --------------------------------
0122   std::unique_ptr<TFile> m_file;
0123   fwlite::EventSetup m_es;
0124   std::map<EventSetupRecordKey, fwlite::RecordID> m_keyToID;
0125   edm::SerialTaskQueue m_queue;
0126   std::mutex m_mutex;
0127 };
0128 
0129 FWLiteESSource::FWLiteESSource(edm::ParameterSet const& iPS)
0130     : m_file(TFile::Open(iPS.getParameter<std::string>("fileName").c_str())), m_es(m_file.get()) {}
0131 
0132 FWLiteESSource::~FWLiteESSource() {}
0133 
0134 edm::eventsetup::ESProductResolverProvider::KeyedResolversVector FWLiteESSource::registerResolvers(
0135     const EventSetupRecordKey& iRecordKey, unsigned int iovIndex) {
0136   KeyedResolversVector keyedProxiesVector;
0137   using edm::eventsetup::heterocontainer::HCTypeTag;
0138 
0139   fwlite::RecordID recID = m_keyToID[iRecordKey];
0140   const fwlite::Record& rec = m_es.get(recID);
0141   typedef std::vector<std::pair<std::string, std::string> > TypesAndLabels;
0142   TypesAndLabels typesAndLabels = rec.typeAndLabelOfAvailableData();
0143 
0144   std::cout << "Looking for data in record " << iRecordKey.name() << std::endl;
0145   for (TypesAndLabels::const_iterator it = typesAndLabels.begin(), itEnd = typesAndLabels.end(); it != itEnd; ++it) {
0146     std::cout << " need type " << it->first << std::endl;
0147     HCTypeTag tt = HCTypeTag::findType(it->first);
0148     if (tt != HCTypeTag()) {
0149       edm::eventsetup::DataKey dk(tt, edm::eventsetup::IdTags(it->second.c_str()));
0150       keyedProxiesVector.emplace_back(
0151           dk, std::make_shared<FWLiteProductResolver>(TypeID(tt.value()), &rec, &m_queue, &m_mutex));
0152     } else {
0153       LogDebug("UnknownESType") << "The type '" << it->first << "' is unknown in this job";
0154       std::cout << "    *****FAILED*****" << std::endl;
0155     }
0156   }
0157   return keyedProxiesVector;
0158 }
0159 
0160 void FWLiteESSource::setIntervalFor(const EventSetupRecordKey& iKey,
0161                                     const edm::IOVSyncValue& iSync,
0162                                     edm::ValidityInterval& oIOV) {
0163   m_es.syncTo(iSync.eventID(), iSync.time());
0164 
0165   const fwlite::Record& rec = m_es.get(m_keyToID[iKey]);
0166   edm::IOVSyncValue endSync(rec.endSyncValue().eventID(), rec.endSyncValue().time());
0167   if (rec.endSyncValue().eventID().run() == 0 && rec.endSyncValue().time().value() == 0ULL) {
0168     endSync = edm::IOVSyncValue::endOfTime();
0169   }
0170   oIOV = edm::ValidityInterval(edm::IOVSyncValue(rec.startSyncValue().eventID(), rec.startSyncValue().time()), endSync);
0171 }
0172 
0173 void FWLiteESSource::delaySettingRecords() {
0174   using edm::eventsetup::heterocontainer::HCTypeTag;
0175   std::vector<std::string> recordNames = m_es.namesOfAvailableRecords();
0176 
0177   for (std::vector<std::string>::const_iterator it = recordNames.begin(), itEnd = recordNames.end(); it != itEnd;
0178        ++it) {
0179     HCTypeTag t = HCTypeTag::findType(*it);
0180     if (t != HCTypeTag()) {
0181       EventSetupRecordKey key(t);
0182       findingRecordWithKey(key);
0183       usingRecordWithKey(key);
0184       m_keyToID[key] = m_es.recordID(it->c_str());
0185     }
0186   }
0187 }
0188 
0189 DEFINE_FWK_EVENTSETUP_SOURCE(FWLiteESSource);