CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/PhysicsTools/CondLiteIO/plugins/FWLiteESSource.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     CondLiteIO
00004 // Class  :     FWLiteESSource
00005 // 
00014 //
00015 // Original Author:  Chris Jones
00016 //         Created:  Thu Jun 17 15:47:35 CDT 2010
00017 // $Id: FWLiteESSource.cc,v 1.2 2010/07/20 02:58:33 wmtan Exp $
00018 //
00019 
00020 // system include files
00021 #include <iostream>
00022 #include <memory>
00023 #include "TFile.h"
00024 #include <boost/shared_ptr.hpp>
00025 
00026 // user include files
00027 #include "FWCore/Framework/interface/DataProxyProvider.h"
00028 #include "FWCore/Framework/interface/DataProxy.h"
00029 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
00030 #include "DataFormats/FWLite/interface/EventSetup.h"
00031 #include "DataFormats/FWLite/interface/Record.h"
00032 #include "FWCore/Framework/interface/HCTypeTag.h"
00033 #include "FWCore/Utilities/interface/TypeIDBase.h"
00034 #include "FWCore/Utilities/interface/Exception.h"
00035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00036 
00037 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00038 
00039 #include "FWCore/Framework/interface/SourceFactory.h"
00040 
00041 
00042 // forward declarations
00043 namespace  {
00044    struct TypeID : public edm::TypeIDBase {
00045       explicit TypeID(const type_info& iInfo): edm::TypeIDBase(iInfo) {}
00046       TypeID() {}
00047       using TypeIDBase::typeInfo;
00048    };
00049    struct FWLiteESGenericHandle {
00050       FWLiteESGenericHandle(const TypeID& iType):
00051       m_type(iType),
00052       m_data(0),
00053       m_exception(0){}
00054       
00055       FWLiteESGenericHandle(const void* iData):
00056       m_type(),
00057       m_data(iData),
00058       m_exception(0) {}
00059 
00060       FWLiteESGenericHandle(cms::Exception* iException):
00061       m_type(),
00062       m_data(0),
00063       m_exception(iException){}
00064       
00065       const std::type_info& typeInfo() const {
00066          return m_type.typeInfo();
00067       }
00068       
00069       TypeID m_type;
00070       const void* m_data;
00071       cms::Exception* m_exception;
00072    };
00073    
00074    class FWLiteProxy : public edm::eventsetup::DataProxy {
00075    public:
00076       FWLiteProxy(const TypeID& iTypeID, const fwlite::Record* iRecord):
00077       m_type(iTypeID),
00078       m_record(iRecord){}
00079       
00080       virtual const void* getImpl(const edm::eventsetup::EventSetupRecord&, const edm::eventsetup::DataKey& iKey) {
00081          assert(iKey.type() == m_type);
00082          
00083          FWLiteESGenericHandle h(m_type);
00084          m_record->get(h,iKey.name().value());
00085          
00086          if(0!=h.m_exception) {
00087             throw *(h.m_exception);
00088          }
00089          return h.m_data;
00090       }
00091       
00092       virtual void invalidateCache() {
00093       }
00094       
00095    private:
00096       TypeID m_type;
00097       const fwlite::Record* m_record;
00098    };
00099 }
00100 
00101 class FWLiteESSource : public edm::eventsetup::DataProxyProvider, public edm::EventSetupRecordIntervalFinder {
00102    
00103 public:
00104    FWLiteESSource(edm::ParameterSet const& iPS);
00105    virtual ~FWLiteESSource();
00106    
00107    // ---------- const member functions ---------------------
00108    
00109    // ---------- static member functions --------------------
00110    
00111    // ---------- member functions ---------------------------
00112    virtual void newInterval(const edm::eventsetup::EventSetupRecordKey& iRecordType,
00113                             const edm::ValidityInterval& iInterval);
00114    
00115    
00116 private:
00117    FWLiteESSource(const FWLiteESSource&); // stop default
00118    
00119    const FWLiteESSource& operator=(const FWLiteESSource&); // stop default
00120    
00121    virtual void registerProxies(const edm::eventsetup::EventSetupRecordKey& iRecordKey ,
00122                                 KeyedProxies& aProxyList);
00123    
00124    
00125    virtual void setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
00126                                const edm::IOVSyncValue& , 
00127                                edm::ValidityInterval&);
00128    
00129    virtual void delaySettingRecords();
00130    
00131    // ---------- member data --------------------------------
00132    std::auto_ptr<TFile> m_file;
00133    fwlite::EventSetup m_es;
00134    std::map<edm::eventsetup::EventSetupRecordKey, fwlite::RecordID> m_keyToID;
00135    
00136 };
00137 
00138 
00139 //
00140 // constants, enums and typedefs
00141 //
00142 
00143 //
00144 // static data member definitions
00145 //
00146 
00147 //
00148 // constructors and destructor
00149 //
00150 FWLiteESSource::FWLiteESSource(edm::ParameterSet const& iPS):
00151 m_file( TFile::Open(iPS.getParameter<std::string>("fileName").c_str())),
00152 m_es( m_file.get())
00153 {
00154 }
00155 
00156 // FWLiteESSource::FWLiteESSource(const FWLiteESSource& rhs)
00157 // {
00158 //    // do actual copying here;
00159 // }
00160 
00161 FWLiteESSource::~FWLiteESSource()
00162 {
00163 }
00164 
00165 //
00166 // assignment operators
00167 //
00168 // const FWLiteESSource& FWLiteESSource::operator=(const FWLiteESSource& rhs)
00169 // {
00170 //   //An exception safe implementation is
00171 //   FWLiteESSource temp(rhs);
00172 //   swap(rhs);
00173 //
00174 //   return *this;
00175 // }
00176 
00177 //
00178 // member functions
00179 //
00180 void 
00181 FWLiteESSource::newInterval(const edm::eventsetup::EventSetupRecordKey& iRecordType,
00182                             const edm::ValidityInterval& /*iInterval*/)
00183 {
00184    invalidateProxies(iRecordType);
00185 }
00186 
00187 //
00188 // const member functions
00189 //
00190 void 
00191 FWLiteESSource::registerProxies(const edm::eventsetup::EventSetupRecordKey& iRecordKey ,
00192                                 KeyedProxies& aProxyList)
00193 {
00194    using edm::eventsetup::heterocontainer::HCTypeTag;
00195 
00196    fwlite::RecordID recID =  m_keyToID[iRecordKey];
00197    const fwlite::Record& rec = m_es.get(recID);
00198    typedef std::vector<std::pair<std::string,std::string> > TypesAndLabels;
00199    TypesAndLabels typesAndLabels = rec.typeAndLabelOfAvailableData();
00200 
00201    std::cout <<"Looking for data in record "<<iRecordKey.name()<<std::endl;
00202    for(TypesAndLabels::const_iterator it = typesAndLabels.begin(), itEnd = typesAndLabels.end();
00203        it != itEnd;
00204        ++it) {
00205       std::cout <<" need type "<<it->first<<std::endl;
00206       HCTypeTag tt = HCTypeTag::findType(it->first);
00207       if(tt != HCTypeTag() ) {
00208          edm::eventsetup::DataKey dk(tt,edm::eventsetup::IdTags(it->second.c_str()));
00209          aProxyList.push_back(std::make_pair(dk,
00210                                              boost::shared_ptr<edm::eventsetup::DataProxy>(new FWLiteProxy(TypeID(tt.value()),&rec))));
00211       } else {
00212          LogDebug("UnknownESType")<<"The type '"<<it->first<<"' is unknown in this job";
00213          std::cout <<"    *****FAILED*****"<<std::endl;
00214       }
00215    }
00216    
00217 }
00218 
00219 
00220 void 
00221 FWLiteESSource::setIntervalFor(const edm::eventsetup::EventSetupRecordKey& iKey,
00222                                const edm::IOVSyncValue& iSync, 
00223                                edm::ValidityInterval& oIOV)
00224 {
00225    m_es.syncTo(iSync.eventID(), iSync.time());
00226    
00227    const fwlite::Record& rec = m_es.get(m_keyToID[iKey]);
00228    edm::IOVSyncValue endSync(rec.endSyncValue().eventID(),
00229                              rec.endSyncValue().time());
00230    if(rec.endSyncValue().eventID().run()==0 &&
00231       rec.endSyncValue().time().value() == 0ULL) {
00232       endSync = edm::IOVSyncValue::endOfTime();
00233    }
00234    oIOV = edm::ValidityInterval(edm::IOVSyncValue(rec.startSyncValue().eventID(),
00235                                                   rec.startSyncValue().time()),
00236                                 endSync);
00237 }
00238 
00239 void 
00240 FWLiteESSource::delaySettingRecords()
00241 {
00242    using edm::eventsetup::heterocontainer::HCTypeTag;
00243    std::vector<std::string> recordNames = m_es.namesOfAvailableRecords();
00244    
00245    for(std::vector<std::string>::const_iterator it = recordNames.begin(),
00246        itEnd = recordNames.end();
00247        it != itEnd;
00248        ++it) {
00249       HCTypeTag t = HCTypeTag::findType(*it);
00250       if(t != HCTypeTag() ) {
00251          edm::eventsetup::EventSetupRecordKey key(t);
00252          findingRecordWithKey(key);
00253          usingRecordWithKey(key);
00254          m_keyToID[key]=m_es.recordID(it->c_str());
00255       }
00256    }
00257 }
00258 
00259 
00260 
00261 //
00262 // static member functions
00263 //
00264 
00265 DEFINE_FWK_EVENTSETUP_SOURCE(FWLiteESSource);