Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-22 01:53:41

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/DataProxyProvider.h"
0027 #include "FWCore/Framework/interface/ESSourceDataProxyNonConcurrentBase.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 FWLiteProxy : public edm::eventsetup::ESSourceDataProxyNonConcurrentBase {
0069   public:
0070     FWLiteProxy(const TypeID& iTypeID, const fwlite::Record* iRecord, edm::SerialTaskQueue* iQueue, std::mutex* iMutex)
0071         : edm::eventsetup::ESSourceDataProxyNonConcurrentBase(iQueue, iMutex),
0072           m_type(iTypeID),
0073           m_record(iRecord),
0074           m_data{nullptr} {}
0075 
0076     void prefetch(const edm::eventsetup::DataKey& iKey, edm::EventSetupRecordDetails) final {
0077       assert(iKey.type() == m_type);
0078 
0079       FWLiteESGenericHandle h(m_type);
0080       m_record->get(h, iKey.name().value());
0081       m_data = h.m_data;
0082 
0083       if (nullptr != h.m_exception) {
0084         throw *(h.m_exception);
0085       }
0086     }
0087 
0088     void invalidateCache() override {
0089       edm::eventsetup::ESSourceDataProxyNonConcurrentBase::invalidateCache();
0090       m_data = nullptr;
0091     }
0092 
0093     void const* getAfterPrefetchImpl() const final { return m_data; }
0094 
0095   private:
0096     TypeID m_type;
0097     const fwlite::Record* m_record;
0098     void const* m_data;
0099   };
0100 }  // namespace
0101 
0102 class FWLiteESSource : public edm::eventsetup::DataProxyProvider, public edm::EventSetupRecordIntervalFinder {
0103 public:
0104   FWLiteESSource(edm::ParameterSet const& iPS);
0105   FWLiteESSource(const FWLiteESSource&) = delete;
0106   const FWLiteESSource& operator=(const FWLiteESSource&) = delete;
0107   ~FWLiteESSource() override;
0108 
0109   using EventSetupRecordKey = edm::eventsetup::EventSetupRecordKey;
0110 
0111 private:
0112   KeyedProxiesVector registerProxies(const EventSetupRecordKey&, unsigned int iovIndex) override;
0113 
0114   void setIntervalFor(const EventSetupRecordKey&, const edm::IOVSyncValue&, edm::ValidityInterval&) override;
0115 
0116   void delaySettingRecords() override;
0117 
0118   // ---------- member data --------------------------------
0119   std::unique_ptr<TFile> m_file;
0120   fwlite::EventSetup m_es;
0121   std::map<EventSetupRecordKey, fwlite::RecordID> m_keyToID;
0122   edm::SerialTaskQueue m_queue;
0123   std::mutex m_mutex;
0124 };
0125 
0126 FWLiteESSource::FWLiteESSource(edm::ParameterSet const& iPS)
0127     : m_file(TFile::Open(iPS.getParameter<std::string>("fileName").c_str())), m_es(m_file.get()) {}
0128 
0129 FWLiteESSource::~FWLiteESSource() {}
0130 
0131 edm::eventsetup::DataProxyProvider::KeyedProxiesVector FWLiteESSource::registerProxies(
0132     const EventSetupRecordKey& iRecordKey, unsigned int iovIndex) {
0133   KeyedProxiesVector keyedProxiesVector;
0134   using edm::eventsetup::heterocontainer::HCTypeTag;
0135 
0136   fwlite::RecordID recID = m_keyToID[iRecordKey];
0137   const fwlite::Record& rec = m_es.get(recID);
0138   typedef std::vector<std::pair<std::string, std::string> > TypesAndLabels;
0139   TypesAndLabels typesAndLabels = rec.typeAndLabelOfAvailableData();
0140 
0141   std::cout << "Looking for data in record " << iRecordKey.name() << std::endl;
0142   for (TypesAndLabels::const_iterator it = typesAndLabels.begin(), itEnd = typesAndLabels.end(); it != itEnd; ++it) {
0143     std::cout << " need type " << it->first << std::endl;
0144     HCTypeTag tt = HCTypeTag::findType(it->first);
0145     if (tt != HCTypeTag()) {
0146       edm::eventsetup::DataKey dk(tt, edm::eventsetup::IdTags(it->second.c_str()));
0147       keyedProxiesVector.emplace_back(dk, std::make_shared<FWLiteProxy>(TypeID(tt.value()), &rec, &m_queue, &m_mutex));
0148     } else {
0149       LogDebug("UnknownESType") << "The type '" << it->first << "' is unknown in this job";
0150       std::cout << "    *****FAILED*****" << std::endl;
0151     }
0152   }
0153   return keyedProxiesVector;
0154 }
0155 
0156 void FWLiteESSource::setIntervalFor(const EventSetupRecordKey& iKey,
0157                                     const edm::IOVSyncValue& iSync,
0158                                     edm::ValidityInterval& oIOV) {
0159   m_es.syncTo(iSync.eventID(), iSync.time());
0160 
0161   const fwlite::Record& rec = m_es.get(m_keyToID[iKey]);
0162   edm::IOVSyncValue endSync(rec.endSyncValue().eventID(), rec.endSyncValue().time());
0163   if (rec.endSyncValue().eventID().run() == 0 && rec.endSyncValue().time().value() == 0ULL) {
0164     endSync = edm::IOVSyncValue::endOfTime();
0165   }
0166   oIOV = edm::ValidityInterval(edm::IOVSyncValue(rec.startSyncValue().eventID(), rec.startSyncValue().time()), endSync);
0167 }
0168 
0169 void FWLiteESSource::delaySettingRecords() {
0170   using edm::eventsetup::heterocontainer::HCTypeTag;
0171   std::vector<std::string> recordNames = m_es.namesOfAvailableRecords();
0172 
0173   for (std::vector<std::string>::const_iterator it = recordNames.begin(), itEnd = recordNames.end(); it != itEnd;
0174        ++it) {
0175     HCTypeTag t = HCTypeTag::findType(*it);
0176     if (t != HCTypeTag()) {
0177       EventSetupRecordKey key(t);
0178       findingRecordWithKey(key);
0179       usingRecordWithKey(key);
0180       m_keyToID[key] = m_es.recordID(it->c_str());
0181     }
0182   }
0183 }
0184 
0185 DEFINE_FWK_EVENTSETUP_SOURCE(FWLiteESSource);