CMS 3D CMS Logo

FWLiteESRecordWriterAnalyzer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWLiteESRecordWriterAnalyzer
4 // Class: FWLiteESRecordWriterAnalyzer
5 //
13 //
14 // Original Author: Chris Jones
15 // Created: Fri Jun 18 14:23:07 CDT 2010
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 #include "TFile.h"
24 
25 // user include files
28 
32 
34 
38 
39 //
40 // class declaration
41 //
42 
43 namespace {
44 
45  struct DataInfo {
46  DataInfo( const edm::eventsetup::heterocontainer::HCTypeTag& iTag,
47  const std::string& iLabel) :
48  m_tag(iTag), m_label(iLabel) {}
50  std::string m_label;
51  };
52 }
53 
54 namespace fwliteeswriter {
55  struct DummyType {
57  mutable const void* m_data;
58  };
59  struct Handle {
60  Handle(const DataInfo* iInfo): m_data(nullptr), m_info(iInfo) {}
61  const void* m_data;
62  const DataInfo* m_info;
64  };
65 }
66 
67 
68 namespace edm {
69  namespace eventsetup {
70 
71  template <>
72  void EventSetupRecordImpl::getImplementation<fwliteeswriter::DummyType>(fwliteeswriter::DummyType const *& iData ,
73  const char* iName,
74  const ComponentDescription*& iDesc,
75  bool iTransientAccessOnly,
76  std::shared_ptr<ESHandleExceptionFactory>& whyFailedFactory) const {
77  DataKey dataKey(*(iData->m_tag),
78  iName,
79  DataKey::kDoNotCopyMemory);
80 
81  const void* pValue = this->getFromProxy(dataKey,iDesc,iTransientAccessOnly);
82  if(nullptr==pValue) {
83  throw cms::Exception("NoProxyException")<<"No data of type \""<<iData->m_tag->name()<<"\" with label \""<<
84  iName<<"\" in record \""<<this->key().name()<<"\"";
85  }
86  iData->m_data = pValue;
87  }
88 
89  template<>
90  bool EventSetupRecord::get<fwliteeswriter::Handle>(const std::string& iName, fwliteeswriter::Handle& iHolder) const {
92  t.m_tag = &(iHolder.m_info->m_tag);
94  const ComponentDescription* desc = nullptr;
95  std::shared_ptr<ESHandleExceptionFactory> dummy;
96  impl_->getImplementation(value, iName.c_str(),desc,true, dummy);
97  iHolder.m_data = t.m_data;
98  iHolder.m_desc = desc;
99  return true;
100  }
101 
102  }
103 }
104 
105 
106 namespace {
107 
108  class RecordHandler {
109  public:
110  RecordHandler(const edm::eventsetup::EventSetupRecordKey& iRec,
111  TFile* iFile,
112  std::vector<DataInfo>& ioInfo):
113  m_key(iRec),
114  m_record(),
115  m_writer(m_key.name(),iFile),
116  m_cacheID(0) {
117  m_dataInfos.swap(ioInfo);
118  }
119 
120  void update(const edm::EventSetup& iSetup) {
121  if(not m_record) {
122  m_record = iSetup.find(m_key);
123  assert(m_record);
124  }
125  if(m_cacheID != m_record->cacheIdentifier()) {
126  m_cacheID = m_record->cacheIdentifier();
127 
128  for(std::vector<DataInfo>::const_iterator it = m_dataInfos.begin(),
129  itEnd = m_dataInfos.end();
130  it != itEnd;
131  ++it) {
132  fwliteeswriter::Handle h(&(*it));
133  m_record->get(it->m_label,h);
134  m_writer.update(h.m_data,(it->m_tag.value()),it->m_label.c_str());
135  }
136  edm::ValidityInterval const& iov= m_record->validityInterval();
137  m_writer.fill(edm::ESRecordAuxiliary(iov.first().eventID(),
138  iov.first().time()) );
139  }
140  }
141  private:
143  std::optional<edm::eventsetup::EventSetupRecordGeneric> m_record;
144  fwlite::RecordWriter m_writer;
145  unsigned long long m_cacheID;
146  std::vector<DataInfo> m_dataInfos;
147  };
148 }
149 
150 
152  public:
154  ~FWLiteESRecordWriterAnalyzer() override;
155 
156 
157  private:
158  void beginJob() override ;
159  void analyze(const edm::Event&, const edm::EventSetup&) override;
160  void endJob() override ;
161  void beginRun(edm::Run const&, edm::EventSetup const&) override;
162  void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
163 
164  void update(const edm::EventSetup&);
165 
166  // ----------member data ---------------------------
167  std::vector<std::shared_ptr<RecordHandler> > m_handlers;
168 
169  std::map<std::string, std::vector<std::pair<std::string,std::string> > > m_recordToDataNames;
170  TFile* m_file;
171 };
172 
173 //
174 // constants, enums and typedefs
175 //
176 
177 //
178 // static data member definitions
179 //
180 
181 //
182 // constructors and destructor
183 //
185 {
186  std::vector<std::string> names = iConfig.getParameterNamesForType<std::vector<edm::ParameterSet> >(false);
187  if (names.empty()) {
188  throw edm::Exception(edm::errors::Configuration)<<"No VPSets were given in configuration";
189  }
190  for (std::vector<std::string>::const_iterator it = names.begin(), itEnd=names.end(); it != itEnd; ++it) {
191  const std::vector<edm::ParameterSet>& ps = iConfig.getUntrackedParameter<std::vector<edm::ParameterSet> >(*it);
192  std::vector<std::pair<std::string,std::string> >& data = m_recordToDataNames[*it];
193  for(std::vector<edm::ParameterSet>::const_iterator itPS = ps.begin(),itPSEnd = ps.end();
194  itPS != itPSEnd;
195  ++itPS){
196  std::string type = itPS->getUntrackedParameter<std::string>("type");
197  std::string label = itPS->getUntrackedParameter<std::string>("label",std::string());
198  data.push_back(std::make_pair(type,label) );
199  }
200  }
201 
202  m_file = TFile::Open(iConfig.getUntrackedParameter<std::string>("fileName").c_str(),"NEW");
203 }
204 
205 
207 {
208 
209  // do anything here that needs to be done at desctruction time
210  // (e.g. close files, deallocate resources etc.)
211  m_file->Close();
212  delete m_file;
213 }
214 
215 
216 //
217 // member functions
218 //
219 void
221 {
223  if(m_handlers.empty()) {
224  //now we have access to the EventSetup so we can setup our data structure
225  for(std::map<std::string, std::vector<std::pair<std::string,std::string> > >::iterator it=m_recordToDataNames.begin(),
226  itEnd = m_recordToDataNames.end();
227  it != itEnd;
228  ++it) {
229  HCTypeTag tt = HCTypeTag::findType(it->first);
230  if(tt == HCTypeTag()) {
231  throw cms::Exception("UnknownESRecordType")<<"The name '"<<it->first<<"' is not associated with a known EventSetupRecord.\n"
232  "Please check spelling or load a module known to link with the package which declares that Record.";
233  }
235 
236  auto rec = iSetup.find(tt);
237  if(not rec) {
238  throw cms::Exception("UnknownESRecordType")<<"The name '"<<it->first<<"' is not associated with a type which is not an EventSetupRecord.\n"
239  "Please check your spelling.";
240  }
241 
242  //now figure out what data
243  std::vector<std::pair<std::string,std::string> >& data = it->second;
244  if(data.empty()) {
245  //get everything from the record
246  std::vector<edm::eventsetup::DataKey> keys;
247  rec->fillRegisteredDataKeys(keys);
248  for(std::vector<edm::eventsetup::DataKey>::iterator itKey = keys.begin(), itKeyEnd = keys.end();
249  itKey != itKeyEnd;
250  ++itKey) {
251  data.push_back(std::make_pair(std::string(itKey->type().name()),
252  std::string(itKey->name().value())));
253  }
254  }
255 
256  std::vector<DataInfo> dataInfos;
257  for (std::vector<std::pair<std::string,std::string> >::iterator itData = data.begin(), itDataEnd = data.end();
258  itData != itDataEnd;
259  ++itData) {
260  HCTypeTag tt = HCTypeTag::findType(itData->first);
261  if(tt == HCTypeTag()) {
262  throw cms::Exception("UnknownESDataType")<<"The name '"<<itData->first<<"' is not associated with a known type held in the "<<it->first<<" Record.\n"
263  "Please check spelling or load a module known to link with the package which declares that type.";
264  }
265  if(!bool(edm::TypeWithDict( tt.value() ))) {
266  throw cms::Exception("NoDictionary")<<"The type '"<<itData->first<<"' can not be retrieved from the Record "<<it->first<<" and stored \n"
267  "because no dictionary exists for the type.";
268  }
269  dataInfos.push_back(DataInfo(tt,itData->second));
270  }
271  m_handlers.push_back( std::make_shared<RecordHandler>(rKey,m_file,dataInfos) );
272  }
273  }
274 
275  for(std::vector<std::shared_ptr<RecordHandler> >::iterator it = m_handlers.begin(),itEnd = m_handlers.end();
276  it != itEnd;
277  ++it) {
278  (*it)->update(iSetup);
279  }
280 }
281 
282 
283 // ------------ method called to for each event ------------
284 void
286 {
287  update(iSetup);
288 }
289 
290 
291 // ------------ method called once each job just before starting event loop ------------
292 void
294 {
295 }
296 
297 // ------------ method called once each job just after ending the event loop ------------
298 void
300  m_file->Write();
301 }
302 
303 void
305  update(iSetup);
306 }
307 void
309  update(iSetup);
310 }
311 
312 
313 //define this as a plug-in
std::optional< eventsetup::EventSetupRecordGeneric > find(const eventsetup::EventSetupRecordKey &iKey) const
Definition: EventSetup.h:160
type
Definition: HCALResponse.h:21
T getUntrackedParameter(std::string const &, T const &) const
std::pair< const char *, const std::type_info * > findType(const char *iClassName)
Definition: typelookup.cc:69
void beginRun(edm::Run const &, edm::EventSetup const &) override
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
const EventID & eventID() const
Definition: IOVSyncValue.h:40
void analyze(const edm::Event &, const edm::EventSetup &) override
const edm::eventsetup::heterocontainer::HCTypeTag * m_tag
void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override
#define nullptr
std::vector< std::shared_ptr< RecordHandler > > m_handlers
const edm::eventsetup::ComponentDescription * m_desc
const std::string names[nVars_]
example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
void beginJob()
Definition: Breakpoints.cc:14
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:169
char const * label
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
FWLiteESRecordWriterAnalyzer(const edm::ParameterSet &)
Definition: value.py:1
std::map< std::string, std::vector< std::pair< std::string, std::string > > > m_recordToDataNames
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
#define update(a, b)
void update(const edm::EventSetup &)
const Timestamp & time() const
Definition: IOVSyncValue.h:42
const IOVSyncValue & first() const
Definition: Run.h:45