00001
00002
00003
00004
00005
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <memory>
00023 #include "TFile.h"
00024 #include "Reflex/Type.h"
00025
00026
00027 #include "FWCore/Framework/interface/Frameworkfwd.h"
00028 #include "FWCore/Framework/interface/EDAnalyzer.h"
00029
00030 #include "FWCore/Framework/interface/EventSetup.h"
00031 #include "FWCore/Framework/interface/EventSetupRecord.h"
00032 #include "FWCore/Framework/interface/MakerMacros.h"
00033
00034 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00035
00036 #include "PhysicsTools/CondLiteIO/interface/RecordWriter.h"
00037 #include "FWCore/Utilities/interface/EDMException.h"
00038 #include "FWCore/Framework/interface/HCTypeTag.h"
00039
00040
00041
00042
00043
00044 namespace {
00045
00046 struct DataInfo {
00047 DataInfo( const edm::eventsetup::heterocontainer::HCTypeTag& iTag,
00048 const std::string& iLabel) :
00049 m_tag(iTag), m_label(iLabel) {}
00050 edm::eventsetup::heterocontainer::HCTypeTag m_tag;
00051 std::string m_label;
00052 };
00053 }
00054
00055 namespace fwliteeswriter {
00056 struct DummyType {
00057 const edm::eventsetup::heterocontainer::HCTypeTag* m_tag;
00058 mutable const void* m_data;
00059 };
00060 struct Handle {
00061 Handle(const DataInfo* iInfo): m_data(0), m_info(iInfo) {}
00062 const void* m_data;
00063 const DataInfo* m_info;
00064 const edm::eventsetup::ComponentDescription* m_desc;
00065 };
00066 }
00067
00068
00069 namespace edm {
00070 namespace eventsetup {
00071
00072 template <>
00073 void EventSetupRecord::getImplementation<fwliteeswriter::DummyType>(fwliteeswriter::DummyType const *& iData ,
00074 const char* iName,
00075 const ComponentDescription*& iDesc,
00076 bool iTransientAccessOnly) const {
00077 DataKey dataKey(*(iData->m_tag),
00078 iName,
00079 DataKey::kDoNotCopyMemory);
00080
00081 const void* pValue = this->getFromProxy(dataKey,iDesc,iTransientAccessOnly);
00082 if(0==pValue) {
00083 throw cms::Exception("NoProxyException");
00084 }
00085 iData->m_data = pValue;
00086 }
00087
00088 template<>
00089 void EventSetupRecord::get<fwliteeswriter::Handle>(const std::string& iName, fwliteeswriter::Handle& iHolder) const {
00090 fwliteeswriter::DummyType t;
00091 t.m_tag = &(iHolder.m_info->m_tag);
00092 const fwliteeswriter::DummyType* value = &t;
00093 const ComponentDescription* desc = 0;
00094 this->getImplementation(value, iName.c_str(),desc,true);
00095 iHolder.m_data = t.m_data;
00096 iHolder.m_desc = desc;
00097 }
00098
00099 }
00100 }
00101
00102
00103 namespace {
00104
00105 class RecordHandler {
00106 public:
00107 RecordHandler(const edm::eventsetup::EventSetupRecordKey& iRec,
00108 TFile* iFile,
00109 std::vector<DataInfo>& ioInfo):
00110 m_key(iRec),
00111 m_record(0),
00112 m_writer(m_key.name(),iFile),
00113 m_cacheID(0) {
00114 m_dataInfos.swap(ioInfo);
00115 }
00116
00117 void update(const edm::EventSetup& iSetup) {
00118 if(0==m_record) {
00119 m_record = iSetup.find(m_key);
00120 assert(0!=m_record);
00121 }
00122 if(m_cacheID != m_record->cacheIdentifier()) {
00123 m_cacheID = m_record->cacheIdentifier();
00124
00125 for(std::vector<DataInfo>::const_iterator it = m_dataInfos.begin(),
00126 itEnd = m_dataInfos.end();
00127 it != itEnd;
00128 ++it) {
00129 fwliteeswriter::Handle h(&(*it));
00130 m_record->get(it->m_label,h);
00131 m_writer.update(h.m_data,(it->m_tag.value()),it->m_label.c_str());
00132 }
00133 edm::ValidityInterval const& iov= m_record->validityInterval();
00134 m_writer.fill(edm::ESRecordAuxiliary(iov.first().eventID(),
00135 iov.first().time()) );
00136 }
00137 }
00138 private:
00139 edm::eventsetup::EventSetupRecordKey m_key;
00140 const edm::eventsetup::EventSetupRecord* m_record;
00141 fwlite::RecordWriter m_writer;
00142 unsigned long long m_cacheID;
00143 std::vector<DataInfo> m_dataInfos;
00144 };
00145 }
00146
00147
00148 class FWLiteESRecordWriterAnalyzer : public edm::EDAnalyzer {
00149 public:
00150 explicit FWLiteESRecordWriterAnalyzer(const edm::ParameterSet&);
00151 ~FWLiteESRecordWriterAnalyzer();
00152
00153
00154 private:
00155 virtual void beginJob() ;
00156 virtual void analyze(const edm::Event&, const edm::EventSetup&);
00157 virtual void endJob() ;
00158 virtual void beginRun(edm::Run const&, edm::EventSetup const&);
00159 virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&);
00160
00161 void update(const edm::EventSetup&);
00162
00163
00164 std::vector<boost::shared_ptr<RecordHandler> > m_handlers;
00165
00166 std::map<std::string, std::vector<std::pair<std::string,std::string> > > m_recordToDataNames;
00167 TFile* m_file;
00168 };
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 FWLiteESRecordWriterAnalyzer::FWLiteESRecordWriterAnalyzer(const edm::ParameterSet& iConfig)
00182 {
00183 std::vector<std::string> names = iConfig.getParameterNamesForType<std::vector<edm::ParameterSet> >(false);
00184 if (0 == names.size()) {
00185 throw edm::Exception(edm::errors::Configuration)<<"No VPSets were given in configuration";
00186 }
00187 for (std::vector<std::string>::const_iterator it = names.begin(), itEnd=names.end(); it != itEnd; ++it) {
00188 const std::vector<edm::ParameterSet>& ps = iConfig.getUntrackedParameter<std::vector<edm::ParameterSet> >(*it);
00189 std::vector<std::pair<std::string,std::string> >& data = m_recordToDataNames[*it];
00190 for(std::vector<edm::ParameterSet>::const_iterator itPS = ps.begin(),itPSEnd = ps.end();
00191 itPS != itPSEnd;
00192 ++itPS){
00193 std::string type = itPS->getUntrackedParameter<std::string>("type");
00194 std::string label = itPS->getUntrackedParameter<std::string>("label",std::string());
00195 data.push_back(std::make_pair(type,label) );
00196 }
00197 }
00198
00199 m_file = TFile::Open(iConfig.getUntrackedParameter<std::string>("fileName").c_str(),"NEW");
00200 }
00201
00202
00203 FWLiteESRecordWriterAnalyzer::~FWLiteESRecordWriterAnalyzer()
00204 {
00205
00206
00207
00208 m_file->Close();
00209 delete m_file;
00210 }
00211
00212
00213
00214
00215
00216 void
00217 FWLiteESRecordWriterAnalyzer::update(const edm::EventSetup& iSetup)
00218 {
00219 using edm::eventsetup::heterocontainer::HCTypeTag;
00220 if(m_handlers.empty()) {
00221
00222 for(std::map<std::string, std::vector<std::pair<std::string,std::string> > >::iterator it=m_recordToDataNames.begin(),
00223 itEnd = m_recordToDataNames.end();
00224 it != itEnd;
00225 ++it) {
00226 HCTypeTag tt = HCTypeTag::findType(it->first);
00227 if(tt == HCTypeTag()) {
00228 throw cms::Exception("UnknownESRecordType")<<"The name '"<<it->first<<"' is not associated with a known EventSetupRecord.\n"
00229 "Please check spelling or load a module known to link with the package which declares that Record.";
00230 }
00231 edm::eventsetup::EventSetupRecordKey rKey(tt);
00232
00233 const edm::eventsetup::EventSetupRecord* rec = iSetup.find(tt);
00234 if(0==rec) {
00235 throw cms::Exception("UnknownESRecordType")<<"The name '"<<it->first<<"' is not associated with a type which is not an EventSetupRecord.\n"
00236 "Please check your spelling.";
00237 }
00238
00239
00240 std::vector<std::pair<std::string,std::string> >& data = it->second;
00241 if(data.empty()) {
00242
00243 std::vector<edm::eventsetup::DataKey> keys;
00244 rec->fillRegisteredDataKeys(keys);
00245 for(std::vector<edm::eventsetup::DataKey>::iterator itKey = keys.begin(), itKeyEnd = keys.end();
00246 itKey != itKeyEnd;
00247 ++itKey) {
00248 data.push_back(std::make_pair(std::string(itKey->type().name()),
00249 std::string(itKey->name().value())));
00250 }
00251 }
00252
00253 std::vector<DataInfo> dataInfos;
00254 for (std::vector<std::pair<std::string,std::string> >::iterator itData = data.begin(), itDataEnd = data.end();
00255 itData != itDataEnd;
00256 ++itData) {
00257 HCTypeTag tt = HCTypeTag::findType(itData->first);
00258 if(tt == HCTypeTag()) {
00259 throw cms::Exception("UnknownESDataType")<<"The name '"<<itData->first<<"' is not associated with a known type held in the "<<it->first<<" Record.\n"
00260 "Please check spelling or load a module known to link with the package which declares that type.";
00261 }
00262 if(ROOT::Reflex::Type::ByTypeInfo( tt.value() ) == ROOT::Reflex::Type()) {
00263 throw cms::Exception("NoDictionary")<<"The type '"<<itData->first<<"' can not be retrieved from the Record "<<it->first<<" and stored \n"
00264 "because no Reflex dictionary exists for the type.";
00265 }
00266 dataInfos.push_back(DataInfo(tt,itData->second));
00267 }
00268 m_handlers.push_back( boost::shared_ptr<RecordHandler>( new RecordHandler(rKey,m_file,dataInfos) ) );
00269 }
00270 }
00271
00272 for(std::vector<boost::shared_ptr<RecordHandler> >::iterator it = m_handlers.begin(),itEnd = m_handlers.end();
00273 it != itEnd;
00274 ++it) {
00275 (*it)->update(iSetup);
00276 }
00277 }
00278
00279
00280
00281 void
00282 FWLiteESRecordWriterAnalyzer::analyze(const edm::Event& , const edm::EventSetup& iSetup)
00283 {
00284 update(iSetup);
00285 }
00286
00287
00288
00289 void
00290 FWLiteESRecordWriterAnalyzer::beginJob()
00291 {
00292 }
00293
00294
00295 void
00296 FWLiteESRecordWriterAnalyzer::endJob() {
00297 m_file->Write();
00298 }
00299
00300 void
00301 FWLiteESRecordWriterAnalyzer::beginRun(edm::Run const&, edm::EventSetup const& iSetup){
00302 update(iSetup);
00303 }
00304 void
00305 FWLiteESRecordWriterAnalyzer::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const& iSetup){
00306 update(iSetup);
00307 }
00308
00309
00310
00311 DEFINE_FWK_MODULE(FWLiteESRecordWriterAnalyzer);