CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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(0), 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 EventSetupRecord::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,
80 
81  const void* pValue = this->getFromProxy(dataKey,iDesc,iTransientAccessOnly);
82  if(0==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  void 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 = 0;
95  std::shared_ptr<ESHandleExceptionFactory> dummy;
96  this->getImplementation(value, iName.c_str(),desc,true, dummy);
97  iHolder.m_data = t.m_data;
98  iHolder.m_desc = desc;
99  }
100 
101  }
102 }
103 
104 
105 namespace {
106 
107  class RecordHandler {
108  public:
109  RecordHandler(const edm::eventsetup::EventSetupRecordKey& iRec,
110  TFile* iFile,
111  std::vector<DataInfo>& ioInfo):
112  m_key(iRec),
113  m_record(0),
114  m_writer(m_key.name(),iFile),
115  m_cacheID(0) {
116  m_dataInfos.swap(ioInfo);
117  }
118 
119  void update(const edm::EventSetup& iSetup) {
120  if(0==m_record) {
121  m_record = iSetup.find(m_key);
122  assert(0!=m_record);
123  }
124  if(m_cacheID != m_record->cacheIdentifier()) {
125  m_cacheID = m_record->cacheIdentifier();
126 
127  for(std::vector<DataInfo>::const_iterator it = m_dataInfos.begin(),
128  itEnd = m_dataInfos.end();
129  it != itEnd;
130  ++it) {
131  fwliteeswriter::Handle h(&(*it));
132  m_record->get(it->m_label,h);
133  m_writer.update(h.m_data,(it->m_tag.value()),it->m_label.c_str());
134  }
135  edm::ValidityInterval const& iov= m_record->validityInterval();
136  m_writer.fill(edm::ESRecordAuxiliary(iov.first().eventID(),
137  iov.first().time()) );
138  }
139  }
140  private:
142  const edm::eventsetup::EventSetupRecord* m_record;
143  fwlite::RecordWriter m_writer;
144  unsigned long long m_cacheID;
145  std::vector<DataInfo> m_dataInfos;
146  };
147 }
148 
149 
151  public:
154 
155 
156  private:
157  virtual void beginJob() override ;
158  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
159  virtual void endJob() override ;
160  virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
161  virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
162 
163  void update(const edm::EventSetup&);
164 
165  // ----------member data ---------------------------
166  std::vector<boost::shared_ptr<RecordHandler> > m_handlers;
167 
168  std::map<std::string, std::vector<std::pair<std::string,std::string> > > m_recordToDataNames;
169  TFile* m_file;
170 };
171 
172 //
173 // constants, enums and typedefs
174 //
175 
176 //
177 // static data member definitions
178 //
179 
180 //
181 // constructors and destructor
182 //
184 {
185  std::vector<std::string> names = iConfig.getParameterNamesForType<std::vector<edm::ParameterSet> >(false);
186  if (0 == names.size()) {
187  throw edm::Exception(edm::errors::Configuration)<<"No VPSets were given in configuration";
188  }
189  for (std::vector<std::string>::const_iterator it = names.begin(), itEnd=names.end(); it != itEnd; ++it) {
190  const std::vector<edm::ParameterSet>& ps = iConfig.getUntrackedParameter<std::vector<edm::ParameterSet> >(*it);
191  std::vector<std::pair<std::string,std::string> >& data = m_recordToDataNames[*it];
192  for(std::vector<edm::ParameterSet>::const_iterator itPS = ps.begin(),itPSEnd = ps.end();
193  itPS != itPSEnd;
194  ++itPS){
195  std::string type = itPS->getUntrackedParameter<std::string>("type");
196  std::string label = itPS->getUntrackedParameter<std::string>("label",std::string());
197  data.push_back(std::make_pair(type,label) );
198  }
199  }
200 
201  m_file = TFile::Open(iConfig.getUntrackedParameter<std::string>("fileName").c_str(),"NEW");
202 }
203 
204 
206 {
207 
208  // do anything here that needs to be done at desctruction time
209  // (e.g. close files, deallocate resources etc.)
210  m_file->Close();
211  delete m_file;
212 }
213 
214 
215 //
216 // member functions
217 //
218 void
220 {
222  if(m_handlers.empty()) {
223  //now we have access to the EventSetup so we can setup our data structure
224  for(std::map<std::string, std::vector<std::pair<std::string,std::string> > >::iterator it=m_recordToDataNames.begin(),
225  itEnd = m_recordToDataNames.end();
226  it != itEnd;
227  ++it) {
228  HCTypeTag tt = HCTypeTag::findType(it->first);
229  if(tt == HCTypeTag()) {
230  throw cms::Exception("UnknownESRecordType")<<"The name '"<<it->first<<"' is not associated with a known EventSetupRecord.\n"
231  "Please check spelling or load a module known to link with the package which declares that Record.";
232  }
234 
235  const edm::eventsetup::EventSetupRecord* rec = iSetup.find(tt);
236  if(0==rec) {
237  throw cms::Exception("UnknownESRecordType")<<"The name '"<<it->first<<"' is not associated with a type which is not an EventSetupRecord.\n"
238  "Please check your spelling.";
239  }
240 
241  //now figure out what data
242  std::vector<std::pair<std::string,std::string> >& data = it->second;
243  if(data.empty()) {
244  //get everything from the record
245  std::vector<edm::eventsetup::DataKey> keys;
246  rec->fillRegisteredDataKeys(keys);
247  for(std::vector<edm::eventsetup::DataKey>::iterator itKey = keys.begin(), itKeyEnd = keys.end();
248  itKey != itKeyEnd;
249  ++itKey) {
250  data.push_back(std::make_pair(std::string(itKey->type().name()),
251  std::string(itKey->name().value())));
252  }
253  }
254 
255  std::vector<DataInfo> dataInfos;
256  for (std::vector<std::pair<std::string,std::string> >::iterator itData = data.begin(), itDataEnd = data.end();
257  itData != itDataEnd;
258  ++itData) {
259  HCTypeTag tt = HCTypeTag::findType(itData->first);
260  if(tt == HCTypeTag()) {
261  throw cms::Exception("UnknownESDataType")<<"The name '"<<itData->first<<"' is not associated with a known type held in the "<<it->first<<" Record.\n"
262  "Please check spelling or load a module known to link with the package which declares that type.";
263  }
264  if(!bool(edm::TypeWithDict( tt.value() ))) {
265  throw cms::Exception("NoDictionary")<<"The type '"<<itData->first<<"' can not be retrieved from the Record "<<it->first<<" and stored \n"
266  "because no dictionary exists for the type.";
267  }
268  dataInfos.push_back(DataInfo(tt,itData->second));
269  }
270  m_handlers.push_back( boost::shared_ptr<RecordHandler>( new RecordHandler(rKey,m_file,dataInfos) ) );
271  }
272  }
273 
274  for(std::vector<boost::shared_ptr<RecordHandler> >::iterator it = m_handlers.begin(),itEnd = m_handlers.end();
275  it != itEnd;
276  ++it) {
277  (*it)->update(iSetup);
278  }
279 }
280 
281 
282 // ------------ method called to for each event ------------
283 void
285 {
286  update(iSetup);
287 }
288 
289 
290 // ------------ method called once each job just before starting event loop ------------
291 void
293 {
294 }
295 
296 // ------------ method called once each job just after ending the event loop ------------
297 void
299  m_file->Write();
300 }
301 
302 void
304  update(iSetup);
305 }
306 void
308  update(iSetup);
309 }
310 
311 
312 //define this as a plug-in
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:76
virtual void beginRun(edm::Run const &, edm::EventSetup const &) override
void fillRegisteredDataKeys(std::vector< DataKey > &oToFill) const
clears the oToFill vector and then fills it with the keys for all registered data keys ...
static const HistoName names[]
const EventID & eventID() const
Definition: IOVSyncValue.h:42
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
const edm::eventsetup::heterocontainer::HCTypeTag * m_tag
assert(m_qm.get())
virtual void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override
std::vector< boost::shared_ptr< RecordHandler > > m_handlers
const edm::eventsetup::ComponentDescription * m_desc
const eventsetup::EventSetupRecord * find(const eventsetup::EventSetupRecordKey &) const
Definition: EventSetup.cc:91
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:194
tuple iov
Definition: o2o.py:307
FWLiteESRecordWriterAnalyzer(const edm::ParameterSet &)
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
std::map< std::string, std::vector< std::pair< std::string, std::string > > > m_recordToDataNames
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:44
const IOVSyncValue & first() const
Definition: Run.h:43