CMS 3D CMS Logo

StreamerInputSource.cc
Go to the documentation of this file.
2 
6 
18 
19 #include "zlib.h"
20 
29 
33 
34 #include <string>
35 #include <iostream>
36 #include <set>
37 
38 namespace edm {
39  namespace {
40  int const init_size = 1024 * 1024;
41  }
42 
44  : RawInputSource(pset, desc),
45  tc_(getTClass(typeid(SendEvent))),
46  dest_(init_size),
47  xbuf_(TBuffer::kRead, init_size),
48  sendEvent_(),
49  eventPrincipalHolder_(),
50  adjustEventToNewProductRegistry_(false),
51  processName_(),
52  protocolVersion_(0U) {}
53 
55 
56  // ---------------------------------------
57  std::unique_ptr<FileBlock> StreamerInputSource::readFile_() { return std::make_unique<FileBlock>(); }
58 
60  ProductRegistry& reg,
62  ThinnedAssociationsHelper& thinnedHelper,
63  bool subsequent) {
64  SendDescs const& descs = header.descs();
65 
66  FDEBUG(6) << "mergeIntoRegistry: Product List: " << std::endl;
67 
68  if (subsequent) {
69  ProductRegistry pReg;
70  pReg.updateFromInput(descs);
72  if (!mergeInfo.empty()) {
73  throw cms::Exception("MismatchedInput", "RootInputFileSequence::previousEvent()") << mergeInfo;
74  }
75  branchIDListHelper.updateFromInput(header.branchIDLists());
76  thinnedHelper.updateFromPrimaryInput(header.thinnedAssociationsHelper());
77  } else {
78  declareStreamers(descs);
79  buildClassCache(descs);
81  if (!reg.frozen()) {
82  reg.updateFromInput(descs);
83  }
84  branchIDListHelper.updateFromInput(header.branchIDLists());
85  thinnedHelper.updateFromPrimaryInput(header.thinnedAssociationsHelper());
86  }
87  }
88 
90  std::vector<std::string> missingDictionaries;
91  std::vector<std::string> branchNamesForMissing;
92  std::vector<std::string> producedTypes;
93  for (auto const& item : descs) {
94  //pi->init();
95  std::string const real_name = wrappedClassName(item.className());
96  FDEBUG(6) << "declare: " << real_name << std::endl;
97  if (!loadCap(real_name, missingDictionaries)) {
98  branchNamesForMissing.emplace_back(item.branchName());
99  producedTypes.emplace_back(item.className() + std::string(" (read from input)"));
100  }
101  }
102  if (!missingDictionaries.empty()) {
103  std::string context("Calling StreamerInputSource::declareStreamers, checking dictionaries for input types");
104  throwMissingDictionariesException(missingDictionaries, context, producedTypes, branchNamesForMissing, true);
105  }
106  }
107 
109  for (auto const& item : descs) {
110  //pi->init();
111  std::string const real_name = wrappedClassName(item.className());
112  FDEBUG(6) << "BuildReadData: " << real_name << std::endl;
113  doBuildRealData(real_name);
114  }
115  }
116 
121  std::unique_ptr<SendJobHeader> StreamerInputSource::deserializeRegistry(InitMsgView const& initView) {
122  if (initView.code() != Header::INIT)
123  throw cms::Exception("StreamTranslation", "Registry deserialization error")
124  << "received wrong message type: expected INIT, got " << initView.code() << "\n";
125 
126  //Get the process name and store if for Protocol version 4 and above.
127  if (initView.protocolVersion() > 3) {
128  processName_ = initView.processName();
129  protocolVersion_ = initView.protocolVersion();
130 
131  FDEBUG(10) << "StreamerInputSource::deserializeRegistry processName = " << processName_ << std::endl;
132  FDEBUG(10) << "StreamerInputSource::deserializeRegistry protocolVersion_= " << protocolVersion_ << std::endl;
133  }
134 
135  // calculate the adler32 checksum
136  uint32_t adler32_chksum = cms::Adler32((char const*)initView.descData(), initView.descLength());
137  //std::cout << "Adler32 checksum of init message = " << adler32_chksum << std::endl;
138  //std::cout << "Adler32 checksum of init messsage from header = " << initView.adler32_chksum() << " "
139  // << "host name = " << initView.hostName() << " len = " << initView.hostName_len() << std::endl;
140  if ((uint32)adler32_chksum != initView.adler32_chksum()) {
141  // skip event (based on option?) or throw exception?
142  throw cms::Exception("StreamDeserialization", "Checksum error")
143  << " chksum from registry data = " << adler32_chksum << " from header = " << initView.adler32_chksum()
144  << " host name = " << initView.hostName() << std::endl;
145  }
146 
147  TClass* desc = getTClass(typeid(SendJobHeader));
148 
149  TBufferFile xbuf(
150  TBuffer::kRead, initView.descLength(), const_cast<char*>((char const*)initView.descData()), kFALSE);
151  RootDebug tracer(10, 10);
152  std::unique_ptr<SendJobHeader> sd((SendJobHeader*)xbuf.ReadObjectAny(desc));
153 
154  if (sd.get() == nullptr) {
155  throw cms::Exception("StreamTranslation", "Registry deserialization error")
156  << "Could not read the initial product registry list\n";
157  }
158 
159  sd->initializeTransients();
160  return sd;
161  }
162 
167  void StreamerInputSource::deserializeAndMergeWithRegistry(InitMsgView const& initView, bool subsequent) {
168  std::unique_ptr<SendJobHeader> sd = deserializeRegistry(initView);
170  if (subsequent) {
172  }
173  SendJobHeader::ParameterSetMap const& psetMap = sd->processParameterSet();
174  pset::Registry& psetRegistry = *pset::Registry::instance();
175  for (auto const& item : psetMap) {
176  ParameterSet pset(item.second.pset());
177  pset.setID(item.first);
178  psetRegistry.insertMapped(pset);
179  }
180  }
181 
186  if (eventView.code() != Header::EVENT)
187  throw cms::Exception("StreamTranslation", "Event deserialization error")
188  << "received wrong message type: expected EVENT, got " << eventView.code() << "\n";
189  FDEBUG(9) << "Decode event: " << eventView.event() << " " << eventView.run() << " " << eventView.size() << " "
190  << eventView.adler32_chksum() << " " << eventView.eventLength() << " " << eventView.eventData()
191  << std::endl;
192  // uncompress if we need to
193  // 78 was a dummy value (for no uncompressed) - should be 0 for uncompressed
194  // need to get rid of this when 090 MTCC streamers are gotten rid of
195  unsigned long origsize = eventView.origDataSize();
196  unsigned long dest_size; //(should be >= eventView.origDataSize())
197 
198  uint32_t adler32_chksum = cms::Adler32((char const*)eventView.eventData(), eventView.eventLength());
199  //std::cout << "Adler32 checksum of event = " << adler32_chksum << std::endl;
200  //std::cout << "Adler32 checksum from header = " << eventView.adler32_chksum() << " "
201  // << "host name = " << eventView.hostName() << " len = " << eventView.hostName_len() << std::endl;
202  if ((uint32)adler32_chksum != eventView.adler32_chksum()) {
203  // skip event (based on option?) or throw exception?
204  throw cms::Exception("StreamDeserialization", "Checksum error")
205  << " chksum from event = " << adler32_chksum << " from header = " << eventView.adler32_chksum()
206  << " host name = " << eventView.hostName() << std::endl;
207  }
208  if (origsize != 78 && origsize != 0) {
209  // compressed
210  dest_size = uncompressBuffer(const_cast<unsigned char*>((unsigned char const*)eventView.eventData()),
211  eventView.eventLength(),
212  dest_,
213  origsize);
214  } else { // not compressed
215  // we need to copy anyway the buffer as we are using dest in xbuf
216  dest_size = eventView.eventLength();
217  dest_.resize(dest_size);
218  unsigned char* pos = (unsigned char*)&dest_[0];
219  unsigned char const* from = (unsigned char const*)eventView.eventData();
220  std::copy(from, from + dest_size, pos);
221  }
222  //TBuffer xbuf(TBuffer::kRead, dest_size,
223  // (char const*) &dest[0],kFALSE);
224  //TBuffer xbuf(TBuffer::kRead, eventView.eventLength(),
225  // (char const*) eventView.eventData(),kFALSE);
226  xbuf_.Reset();
227  xbuf_.SetBuffer(&dest_[0], dest_size, kFALSE);
228  RootDebug tracer(10, 10);
229 
230  //We do not yet know which EventPrincipal we will use, therefore
231  // we are using a new EventPrincipalHolder as a proxy. We need to
232  // make a new one instead of reusing the same one becuase when running
233  // multi-threaded there will be multiple EventPrincipals being used
234  // simultaneously.
235  eventPrincipalHolder_ = std::make_unique<EventPrincipalHolder>(); // propagate_const<T> has no reset() function
237  {
238  std::shared_ptr<void> refCoreStreamerGuard(nullptr, [](void*) {
240  ;
241  });
242  sendEvent_ = std::unique_ptr<SendEvent>((SendEvent*)xbuf_.ReadObjectAny(tc_));
243  }
244 
245  if (sendEvent_.get() == nullptr) {
246  throw cms::Exception("StreamTranslation", "Event deserialization error")
247  << "got a null event from input stream\n";
248  }
250 
251  FDEBUG(5) << "Got event: " << sendEvent_->aux().id() << " " << sendEvent_->products().size() << std::endl;
252  if (runAuxiliary().get() == nullptr || runAuxiliary()->run() != sendEvent_->aux().run()) {
254  new RunAuxiliary(sendEvent_->aux().run(), sendEvent_->aux().time(), Timestamp::invalidTimestamp());
255  runAuxiliary->setProcessHistoryID(sendEvent_->processHistory().id());
256  setRunAuxiliary(runAuxiliary);
258  }
261  runAuxiliary()->run(), eventView.lumi(), sendEvent_->aux().time(), Timestamp::invalidTimestamp());
262  luminosityBlockAuxiliary->setProcessHistoryID(sendEvent_->processHistory().id());
263  setLuminosityBlockAuxiliary(luminosityBlockAuxiliary);
264  }
265  setEventCached();
266  }
267 
271  bool eventOK = eventPrincipal.adjustToNewProductRegistry(*productRegistry());
272  assert(eventOK);
274  }
275  EventSelectionIDVector ids(sendEvent_->eventSelectionIDs());
276  BranchListIndexes indexes(sendEvent_->branchListIndexes());
277  branchIDListHelper()->fixBranchListIndexes(indexes);
278  eventPrincipal.fillEventPrincipal(sendEvent_->aux(), processHistoryRegistry(), std::move(ids), std::move(indexes));
279 
280  //We now know which eventPrincipal to use and we can reuse the slot in
281  // streamToEventPrincipalHolders to own the memory
282  eventPrincipalHolder_->setEventPrincipal(&eventPrincipal);
283  if (streamToEventPrincipalHolders_.size() < eventPrincipal.streamID().value() + 1) {
284  streamToEventPrincipalHolders_.resize(eventPrincipal.streamID().value() + 1);
285  }
287 
288  // no process name list handling
289 
290  SendProds& sps = sendEvent_->products();
291  for (auto& spitem : sps) {
292  FDEBUG(10) << "check prodpair" << std::endl;
293  if (spitem.desc() == nullptr)
294  throw cms::Exception("StreamTranslation", "Empty Provenance");
295  FDEBUG(5) << "Prov:"
296  << " " << spitem.desc()->className() << " " << spitem.desc()->productInstanceName() << " "
297  << spitem.desc()->branchID() << std::endl;
298 
299  BranchDescription const branchDesc(*spitem.desc());
300  // This ProductProvenance constructor inserts into the entry description registry
301  if (spitem.parents()) {
302  ProductProvenance productProvenance(spitem.branchID(), *spitem.parents());
303  if (spitem.prod() != nullptr) {
304  FDEBUG(10) << "addproduct next " << spitem.branchID() << std::endl;
305  eventPrincipal.putOnRead(
306  branchDesc, std::unique_ptr<WrapperBase>(const_cast<WrapperBase*>(spitem.prod())), &productProvenance);
307  FDEBUG(10) << "addproduct done" << std::endl;
308  } else {
309  FDEBUG(10) << "addproduct empty next " << spitem.branchID() << std::endl;
310  eventPrincipal.putOnRead(branchDesc, std::unique_ptr<WrapperBase>(), &productProvenance);
311  FDEBUG(10) << "addproduct empty done" << std::endl;
312  }
313  } else {
314  ProductProvenance const* productProvenance = nullptr;
315  if (spitem.prod() != nullptr) {
316  FDEBUG(10) << "addproduct next " << spitem.branchID() << std::endl;
317  eventPrincipal.putOnRead(
318  branchDesc, std::unique_ptr<WrapperBase>(const_cast<WrapperBase*>(spitem.prod())), productProvenance);
319  FDEBUG(10) << "addproduct done" << std::endl;
320  } else {
321  FDEBUG(10) << "addproduct empty next " << spitem.branchID() << std::endl;
322  eventPrincipal.putOnRead(branchDesc, std::unique_ptr<WrapperBase>(), productProvenance);
323  FDEBUG(10) << "addproduct empty done" << std::endl;
324  }
325  }
326  spitem.clear();
327  }
328 
329  FDEBUG(10) << "Size = " << eventPrincipal.size() << std::endl;
330  }
331 
340  unsigned int StreamerInputSource::uncompressBuffer(unsigned char* inputBuffer,
341  unsigned int inputSize,
342  std::vector<unsigned char>& outputBuffer,
343  unsigned int expectedFullSize) {
344  unsigned long origSize = expectedFullSize;
345  unsigned long uncompressedSize = expectedFullSize * 1.1;
346  FDEBUG(1) << "Uncompress: original size = " << origSize << ", compressed size = " << inputSize << std::endl;
347  outputBuffer.resize(uncompressedSize);
348  int ret = uncompress(&outputBuffer[0], &uncompressedSize, inputBuffer, inputSize); // do not need compression level
349  //std::cout << "unCompress Return value: " << ret << " Okay = " << Z_OK << std::endl;
350  if (ret == Z_OK) {
351  // check the length against original uncompressed length
352  FDEBUG(10) << " original size = " << origSize << " final size = " << uncompressedSize << std::endl;
353  if (origSize != uncompressedSize) {
354  // we throw an error and return without event! null pointer
355  throw cms::Exception("StreamDeserialization", "Uncompression error")
356  << "mismatch event lengths should be" << origSize << " got " << uncompressedSize << "\n";
357  }
358  } else {
359  // we throw an error and return without event! null pointer
360  throw cms::Exception("StreamDeserialization", "Uncompression error") << "Error code = " << ret << "\n ";
361  }
362  return (unsigned int)uncompressedSize;
363  }
364 
366  // called from an online streamer source to reset after a stop command
367  // so an enable command will work
370  assert(!eventCached());
371  reset();
372  }
373 
375  // Need to define a dummy setRun here or else the InputSource::setRun is called
376  // if we have a source inheriting from this and wants to define a setRun method
377  throw Exception(errors::LogicError) << "StreamerInputSource::setRun()\n"
378  << "Run number cannot be modified for this type of Input Source\n"
379  << "Contact a Storage Manager Developer\n";
380  }
381 
383 
385 
387  return eventPrincipal_ ? eventPrincipal_->getIt(id) : nullptr;
388  }
389 
391  unsigned int& index) const {
392  return eventPrincipal_ ? eventPrincipal_->getThinnedProduct(id, index) : nullptr;
393  }
394 
396  std::vector<WrapperBase const*>& wrappers,
397  std::vector<unsigned int>& keys) const {
398  if (eventPrincipal_)
399  eventPrincipal_->getThinnedProducts(pid, wrappers, keys);
400  }
401 
403  assert(eventPrincipal_ != nullptr);
405  }
406 
408 
410 } // namespace edm
void read(EventPrincipal &eventPrincipal) override
ProcessHistoryRegistry const & processHistoryRegistry() const
Accessors for process history registry.
Definition: InputSource.h:155
static void fillDescription(ParameterSetDescription &description)
size_t size() const
Definition: Principal.cc:299
uint32 lumi() const
Definition: EventMessage.cc:81
void throwMissingDictionariesException(std::vector< std::string > &missingDictionaries, std::string const &context)
WrapperBase const * getThinnedProduct(ProductID const &pid, unsigned int &key) const override
ProductRegistry & productRegistryUpdate()
Definition: InputSource.h:326
static Timestamp invalidTimestamp()
Definition: Timestamp.h:101
const uint8 * eventData() const
Definition: EventMessage.h:79
def copy(args, dbName)
const uint8 * descData() const
Definition: InitMessage.h:86
static void mergeIntoRegistry(SendJobHeader const &header, ProductRegistry &, BranchIDListHelper &, ThinnedAssociationsHelper &, bool subsequent)
std::string hostName() const
std::vector< BranchDescription > SendDescs
void doBuildRealData(const std::string &name)
std::map< ParameterSetID, ParameterSetBlob > ParameterSetMap
#define nullptr
bool registerProcessHistory(ProcessHistory const &processHistory)
void resetRunAuxiliary(bool isNewRun=true) const
Definition: InputSource.h:337
std::unique_ptr< SendJobHeader > deserializeRegistry(InitMsgView const &initView)
void setRefCoreStreamer(bool resetAll=false)
RunNumber_t run() const
Accessor for current run number.
Definition: InputSource.cc:436
StreamerInputSource(ParameterSet const &pset, InputSourceDescription const &desc)
void setRun(RunNumber_t r) override
void updateFromPrimaryInput(ThinnedAssociationsHelper const &)
std::vector< EventSelectionID > EventSelectionIDVector
uint32 run() const
Definition: EventMessage.cc:71
void getThinnedProducts(ProductID const &pid, std::vector< WrapperBase const * > &foundContainers, std::vector< unsigned int > &keys) const override
uint32 adler32_chksum() const
Definition: EventMessage.h:96
uint32 eventLength() const
Definition: EventMessage.h:81
std::unique_ptr< FileBlock > readFile_() override
static void declareStreamers(SendDescs const &descs)
uint32 adler32_chksum() const
Definition: InitMessage.h:89
std::vector< BranchListIndex > BranchListIndexes
static void buildClassCache(SendDescs const &descs)
edm::propagate_const< std::unique_ptr< EventPrincipalHolder > > eventPrincipalHolder_
void setLuminosityBlockAuxiliary(LuminosityBlockAuxiliary *lbp)
Definition: InputSource.h:333
uint32 code() const
Definition: EventMessage.h:76
StreamID streamID() const
void deserializeEvent(EventMsgView const &eventView)
std::string hostName() const
Definition: InitMessage.cc:184
void getThinnedProducts(ProductID const &pid, std::vector< WrapperBase const * > &wrappers, std::vector< unsigned int > &keys) const override
std::string merge(ProductRegistry const &other, std::string const &fileName, BranchDescription::MatchMode branchesMustMatch=BranchDescription::Permissive)
bool insertMapped(value_type const &v, bool forceUpdate=false)
Definition: Registry.cc:32
void setProcessHistoryID(ProcessHistoryID const &phid)
std::vector< StreamedProduct > SendProds
unsigned int uint32
Definition: MsgTools.h:13
void setEventCached()
Called by the framework to merge or ached() const {return eventCached_;}.
Definition: InputSource.h:358
void Adler32(char const *data, size_t len, uint32_t &a, uint32_t &b)
ThinnedAssociationsHelper const & thinnedAssociationsHelper() const
LuminosityBlockNumber_t luminosityBlock() const
Accessor for current luminosity block number.
Definition: InputSource.cc:441
uint32 protocolVersion() const
Definition: InitMessage.cc:109
std::shared_ptr< BranchIDListHelper const > branchIDListHelper() const
Accessors for branchIDListHelper.
Definition: InputSource.h:159
unsigned int value() const
Definition: StreamID.h:42
double sd
std::string wrappedClassName(std::string const &iFullName)
edm::propagate_const< std::unique_ptr< SendEvent > > sendEvent_
void putOnRead(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, ProductProvenance const *productProvenance) const
bool loadCap(const std::string &name, std::vector< std::string > &missingDictionaries)
uint32 size() const
Definition: EventMessage.h:77
BranchIDLists const & branchIDLists() const
TClass * getTClass(const std::type_info &ti)
uint32 origDataSize() const
Definition: EventMessage.cc:86
unsigned int transitionIndex() const
WrapperBase const * getThinnedProduct(ProductID const &, unsigned int &) const override
bool updateFromInput(BranchIDLists const &bidlists)
WrapperBase const * getIt(ProductID const &id) const override
std::shared_ptr< RunAuxiliary > runAuxiliary() const
Called by the framework to merge or insert run in principal cache.
Definition: InputSource.h:239
edm::propagate_const< TClass * > tc_
void resetLuminosityBlockAuxiliary(bool isNewLumi=true) const
Definition: InputSource.h:341
ProcessHistoryRegistry & processHistoryRegistryForUpdate()
Definition: InputSource.h:327
void fillEventPrincipal(EventAuxiliary const &aux, ProcessHistoryRegistry const &processHistoryRegistry, DelayedReader *reader=0)
HLT enums.
void reset() const
Definition: InputSource.h:345
uint32 code() const
Definition: InitMessage.h:65
void setRunAuxiliary(RunAuxiliary *rp)
Definition: InputSource.h:329
std::shared_ptr< ProductRegistry const > productRegistry() const
Accessors for product registry.
Definition: InputSource.h:151
bool adjustToNewProductRegistry(ProductRegistry const &reg)
Definition: Principal.cc:311
std::vector< edm::propagate_const< std::unique_ptr< EventPrincipalHolder > > > streamToEventPrincipalHolders_
std::shared_ptr< ThinnedAssociationsHelper const > thinnedAssociationsHelper() const
Accessors for thinnedAssociationsHelper.
Definition: InputSource.h:165
std::string processName() const
Definition: InitMessage.cc:123
void updateFromInput(ProductList const &other)
const int init_size
void setProcessHistoryID(ProcessHistoryID const &phid)
Definition: RunAuxiliary.h:36
unsigned int RunNumber_t
void adjustIndexesAfterProductRegistryAddition()
Definition: Principal.cc:884
uint32 descLength() const
Definition: InitMessage.h:85
uint64 event() const
Definition: EventMessage.cc:76
#define FDEBUG(lev)
Definition: DebugMacros.h:19
void deserializeAndMergeWithRegistry(InitMsgView const &initView, bool subsequent=false)
std::shared_ptr< LuminosityBlockAuxiliary > luminosityBlockAuxiliary() const
Called by the framework to merge or insert lumi in principal cache.
Definition: InputSource.h:242
SendDescs const & descs() const
static void fillDescription(ParameterSetDescription &description)
def move(src, dest)
Definition: eostools.py:511
static unsigned int uncompressBuffer(unsigned char *inputBuffer, unsigned int inputSize, std::vector< unsigned char > &outputBuffer, unsigned int expectedFullSize)
WrapperBase const * getIt(ProductID const &pid) const override
static Registry * instance()
Definition: Registry.cc:12
void loadExtraClasses()
Definition: ClassFiller.cc:33
bool eventCached() const
Definition: InputSource.h:356
std::vector< unsigned char > dest_