CMS 3D CMS Logo

StreamerOutputMsgBuilders.cc
Go to the documentation of this file.
2 
16 
17 #include <iostream>
18 #include <memory>
19 #include <string>
20 #include <sys/time.h>
21 #include <unistd.h>
22 #include <vector>
23 #include <zlib.h>
24 
25 namespace edm::streamer {
28  ret.hltTriggerSelections = EventSelector::getEventSelectionVString(ps);
29  ret.compressionAlgoStr = ps.getUntrackedParameter<std::string>("compression_algorithm");
30  ret.compressionLevel = ps.getUntrackedParameter<int>("compression_level");
31  ret.lumiSectionInterval = ps.getUntrackedParameter<int>("lumiSection_interval");
32  ret.useCompression = ps.getUntrackedParameter<bool>("use_compression");
33  return ret;
34  }
35 
38  std::string const& moduleLabel)
39  :
40 
41  serializer_(selections),
42  useCompression_(p.useCompression),
43  compressionAlgoStr_(p.compressionAlgoStr),
44  compressionLevel_(p.compressionLevel),
45  lumiSectionInterval_(p.lumiSectionInterval),
46  hltsize_(0),
47  host_name_(),
48  hltTriggerSelections_(),
49  outputModuleId_(0) {
50  //limits initially set for default ZLIB option
51  int minCompressionLevel = 1;
52  int maxCompressionLevel = 9;
53 
54  // test luminosity sections
55  struct timeval now;
56  struct timezone dummyTZ;
57  gettimeofday(&now, &dummyTZ);
58  timeInSecSinceUTC = static_cast<double>(now.tv_sec) + (static_cast<double>(now.tv_usec) / 1000000.0);
59 
60  if (useCompression_ == true) {
61  if (compressionAlgoStr_ == "ZLIB") {
63  } else if (compressionAlgoStr_ == "LZMA") {
65  minCompressionLevel = 0;
66  } else if (compressionAlgoStr_ == "ZSTD") {
68  maxCompressionLevel = 20;
69  } else if (compressionAlgoStr_ == "UNCOMPRESSED") {
71  useCompression_ = false;
73  } else
74  throw cms::Exception("StreamerOutputMsgBuilders", "Compression type unknown")
75  << "Unknown compression algorithm " << compressionAlgoStr_;
76 
77  if (compressionLevel_ < minCompressionLevel) {
78  FDEBUG(9) << "Compression Level = " << compressionLevel_ << " no compression" << std::endl;
80  useCompression_ = false;
82  } else if (compressionLevel_ > maxCompressionLevel) {
83  FDEBUG(9) << "Compression Level = " << compressionLevel_ << " using max compression level "
84  << maxCompressionLevel << std::endl;
85  compressionLevel_ = maxCompressionLevel;
87  }
88  } else
90 
91  int got_host = gethostname(host_name_, 255);
92  if (got_host != 0)
93  strncpy(host_name_, "noHostNameFoundOrTooLong", sizeof(host_name_));
94  //loadExtraClasses();
95 
96  // 25-Jan-2008, KAB - pull out the trigger selection request
97  // which we need for the INIT message
98  hltTriggerSelections_ = p.hltTriggerSelections;
99 
100  Strings const& hltTriggerNames = edm::getAllTriggerNames();
101  hltsize_ = hltTriggerNames.size();
102 
103  //Checksum of the module label
104  uLong crc = crc32(0L, Z_NULL, 0);
105  Bytef const* buf = (Bytef const*)(moduleLabel.data());
106  crc = crc32(crc, buf, moduleLabel.length());
107  outputModuleId_ = static_cast<uint32>(crc);
108  }
109 
111 
112  std::unique_ptr<InitMsgBuilder> StreamerOutputMsgBuilders::serializeRegistry(
113  SerializeDataBuffer& sbuf,
114  std::string const& processName,
115  std::string const& moduleLabel,
116  ParameterSetID const& toplevel,
117  SendJobHeader::ParameterSetMap const* psetMap) const {
118  if (psetMap) {
119  serializer_.serializeRegistry(sbuf, *psetMap);
120  } else {
122  }
123  // resize header_buf_ to reflect space used in serializer_ + header
124  // I just added an overhead for header of 50000 for now
125  unsigned int src_size = sbuf.currentSpaceUsed();
126  unsigned int new_size = src_size + 50000;
127  if (sbuf.header_buf_.size() < new_size)
128  sbuf.header_buf_.resize(new_size);
129 
130  //Build the INIT Message
131  //Following values are strictly DUMMY and will be replaced
132  // once available with Utility function etc.
133  uint32 run = 1;
134 
135  //Get the Process PSet ID
136 
137  //In case we need to print it
138  // cms::Digest dig(toplevel.compactForm());
139  // cms::MD5Result r1 = dig.digest();
140  // std::string hexy = r1.toString();
141  // std::cout << "HEX Representation of Process PSetID: " << hexy << std::endl;
142 
143  //L1 stays dummy as of today
144  Strings l1_names; //3
145  l1_names.push_back("t1");
146  l1_names.push_back("t10");
147  l1_names.push_back("t2");
148 
149  Strings const& hltTriggerNames = edm::getAllTriggerNames();
150 
151  auto init_message = std::make_unique<InitMsgBuilder>(&sbuf.header_buf_[0],
152  sbuf.header_buf_.size(),
153  run,
154  Version((uint8 const*)toplevel.compactForm().c_str()),
155  getReleaseVersion().c_str(),
156  processName.c_str(),
157  moduleLabel.c_str(),
159  hltTriggerNames,
161  l1_names,
162  (uint32)sbuf.adler32_chksum());
163 
164  // copy data into the destination message
165  unsigned char* src = sbuf.bufferPointer();
166  std::copy(src, src + src_size, init_message->dataAddress());
167  init_message->setDataLength(src_size);
168  return init_message;
169  }
170 
173  std::vector<unsigned char>& hltbits) const {
174  hltbits.clear();
175 
176  std::vector<unsigned char> vHltState;
177 
178  if (triggerResults.isValid()) {
180  vHltState.push_back(((triggerResults->at(i)).state()));
181  }
182  } else {
183  // We fill all Trigger bits to valid state.
185  vHltState.push_back(hlt::Pass);
186  }
187  }
188 
189  //Pack into member hltbits
190  if (!vHltState.empty()) {
191  unsigned int packInOneByte = 4;
192  unsigned int sizeOfPackage = 1 + ((vHltState.size() - 1) / packInOneByte); //Two bits per HLT
193 
194  hltbits.resize(sizeOfPackage);
195  std::fill(hltbits.begin(), hltbits.end(), 0);
196 
197  for (std::vector<unsigned char>::size_type i = 0; i != vHltState.size(); ++i) {
198  unsigned int whichByte = i / packInOneByte;
199  unsigned int indxWithinByte = i % packInOneByte;
200  hltbits[whichByte] = hltbits[whichByte] | (vHltState[i] << (indxWithinByte * 2));
201  }
202  }
203 
204  //This is Just a printing code.
205  //std::cout << "Size of hltbits:" << hltbits_.size() << std::endl;
206  //for(unsigned int i=0; i != hltbits_.size() ; ++i) {
207  // printBits(hltbits_[i]);
208  //}
209  //std::cout << "\n";
210  }
211 
212  std::unique_ptr<EventMsgBuilder> StreamerOutputMsgBuilders::serializeEvent(
213  SerializeDataBuffer& sbuf,
214  EventForOutput const& e,
216  ParameterSetID const& selectorCfg,
217  uint32_t eventMetaDataChecksum) const {
218  constexpr unsigned int reserve_size = SerializeDataBuffer::reserve_size;
219  //Lets Build the Event Message first
220 
221  std::vector<unsigned char> hltbits;
222  setHltMask(e, triggerResults, hltbits);
223 
224  uint32 lumi = e.luminosityBlock();
225  if (lumiSectionInterval_ != 0) {
226  struct timeval now;
227  struct timezone dummyTZ;
228  gettimeofday(&now, &dummyTZ);
229  double timeInSec =
230  static_cast<double>(now.tv_sec) + (static_cast<double>(now.tv_usec) / 1000000.0) - timeInSecSinceUTC;
231  // what about overflows?
232  lumi = static_cast<uint32>(timeInSec / std::abs(lumiSectionInterval_)) + 1;
233  }
235  sbuf, e, selectorCfg, eventMetaDataChecksum, compressionAlgo_, compressionLevel_, reserve_size);
236 
237  return serializeEventCommon(e.id().run(), lumi, e.id().event(), hltbits, hltsize_, sbuf);
238  }
239 
240  std::pair<std::unique_ptr<EventMsgBuilder>, uint32_t> StreamerOutputMsgBuilders::serializeEventMetaData(
241  SerializeDataBuffer& sbuf, BranchIDLists const& branchLists, ThinnedAssociationsHelper const& helper) const {
242  constexpr unsigned int reserve_size = SerializeDataBuffer::reserve_size;
243  //Lets Build the Event Message first
244 
245  std::vector<unsigned char> hltbits;
246  serializer_.serializeEventMetaData(sbuf, branchLists, helper, compressionAlgo_, compressionLevel_, reserve_size);
247  auto eventMetaDataChecksum = sbuf.adler32_chksum_;
248 
249  return std::make_pair(serializeEventCommon(0, 0, 0, hltbits, 0, sbuf), eventMetaDataChecksum);
250  }
251 
253  uint32 lumi,
254  uint64 event,
255  std::vector<unsigned char> hltbits,
256  unsigned int hltsize,
257  SerializeDataBuffer& sbuf) const {
258  // resize header_buf_ to reserved size on first written event
259  constexpr unsigned int reserve_size = SerializeDataBuffer::reserve_size;
260  if (sbuf.header_buf_.size() < reserve_size)
261  sbuf.header_buf_.resize(reserve_size);
262 
263  //Following is strictly DUMMY Data for L! Trig and will be replaced with actual
264  // once figured out, there is no logic involved here.
265  std::vector<bool> l1bit = {true, true, false};
266  //End of dummy data
267 
268  auto msg = std::make_unique<EventMsgBuilder>(&sbuf.header_buf_[0],
269  sbuf.comp_buf_.size(),
270  run,
271  event,
272  lumi,
274  0,
275  l1bit,
276  (uint8*)&hltbits[0],
277  hltsize,
278  (uint32)sbuf.adler32_chksum(),
279  host_name_);
280 
281  // 50000 bytes is reserved for header as has been the case with previous version which did one extra copy of event data
282  uint32 headerSize = msg->headerSize();
283  if (headerSize > reserve_size)
284  throw cms::Exception("StreamerOutputMsgBuilders", "Header Overflow")
285  << " header of size " << headerSize << "bytes is too big to fit into the reserved buffer space";
286 
287  //set addresses to other buffer and copy constructed header there
288  msg->setBufAddr(&sbuf.comp_buf_[reserve_size - headerSize]);
289  msg->setEventAddr(sbuf.bufferPointer());
290  std::copy(&sbuf.header_buf_[0], &sbuf.header_buf_[headerSize], (char*)(&sbuf.comp_buf_[reserve_size - headerSize]));
291 
292  unsigned int src_size = sbuf.currentSpaceUsed();
293  msg->setEventLength(src_size); //compressed size
294  if (useCompression_)
295  msg->setOrigDataSize(
296  sbuf.currentEventSize()); //uncompressed size (or 0 if no compression -> streamer input source requires this)
297  else
298  msg->setOrigDataSize(0);
299 
300  return msg;
301  }
302 
304  desc.addUntracked<int>("max_event_size", 7000000)->setComment("Obsolete parameter.");
305  desc.addUntracked<bool>("use_compression", true)
306  ->setComment("If True, compression will be used to write streamer file.");
307  desc.addUntracked<std::string>("compression_algorithm", "ZLIB")
308  ->setComment("Compression algorithm to use: UNCOMPRESSED, ZLIB, LZMA or ZSTD");
309  desc.addUntracked<int>("compression_level", 1)->setComment("Compression level to use on serialized ROOT events");
310  desc.addUntracked<int>("lumiSection_interval", 0)
311  ->setComment(
312  "If 0, use lumi section number from event.\n"
313  "If not 0, the interval in seconds between fake lumi sections.");
314  }
315 } // namespace edm::streamer
std::vector< unsigned char > comp_buf_
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
Definition: helper.py:1
int serializeEvent(SerializeDataBuffer &data_buffer, EventForOutput const &event, ParameterSetID const &selectorConfig, uint32_t metaDataChecksum, StreamerCompressionAlgo compressionAlgo, int compression_level, unsigned int reserveSize) const
std::vector< std::string > const & getAllTriggerNames()
ret
prodAgent to be discontinued
std::map< ParameterSetID, ParameterSetBlob > ParameterSetMap
int serializeEventMetaData(SerializeDataBuffer &data_buffer, const BranchIDLists &branchIDLists, ThinnedAssociationsHelper const &thinnedAssociationsHelper, StreamerCompressionAlgo compressionAlgo, int compression_level, unsigned int reserveSize) const
data_buffer.adler32_chksum_ is the meta data checksum to pass to subsequent events ...
unsigned int currentEventSize() const
std::unique_ptr< EventMsgBuilder > serializeEventCommon(uint32 run, uint32 lumi, uint64 event, std::vector< unsigned char > hltbits, unsigned int hltsize, SerializeDataBuffer &sbuf) const
static constexpr unsigned int reserve_size
void setHltMask(EventForOutput const &e, Handle< TriggerResults > const &triggerResults, std::vector< unsigned char > &hltbits) const
int serializeRegistry(SerializeDataBuffer &data_buffer) const
#define FDEBUG(lev)
Definition: DebugMacros.h:19
uint16_t size_type
std::vector< std::pair< BranchDescription const *, EDGetToken > > SelectedProducts
T getUntrackedParameter(std::string const &, T const &) const
static void fillDescription(ParameterSetDescription &desc)
accept
Definition: HLTenums.h:18
static std::vector< std::string > getEventSelectionVString(edm::ParameterSet const &pset)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
unsigned char uint8
Definition: MsgTools.h:12
std::unique_ptr< EventMsgBuilder > serializeEvent(SerializeDataBuffer &sbuf, EventForOutput const &e, Handle< TriggerResults > const &triggerResults, ParameterSetID const &selectorCfg, uint32_t eventMetaDataChecksum) const
value_type compactForm() const
Definition: Hash.h:186
unsigned char const * bufferPointer() const
StreamerOutputMsgBuilders(Parameters const &p, SelectedProducts const *selections, std::string const &moduleLabel)
std::string getReleaseVersion()
static Parameters parameters(ParameterSet const &ps)
static std::string const triggerResults("TriggerResults")
tuple msg
Definition: mps_check.py:286
unsigned long long uint64
Definition: MsgTools.h:15
std::vector< std::string > Strings
Definition: MsgTools.h:19
unsigned int currentSpaceUsed() const
unsigned int uint32
Definition: MsgTools.h:14
Definition: event.py:1
std::pair< std::unique_ptr< EventMsgBuilder >, uint32_t > serializeEventMetaData(SerializeDataBuffer &sbuf, BranchIDLists const &branchLists, ThinnedAssociationsHelper const &helper) const
std::unique_ptr< InitMsgBuilder > serializeRegistry(SerializeDataBuffer &sbuf, std::string const &processName, std::string const &moduleLabel, ParameterSetID const &toplevel, SendJobHeader::ParameterSetMap const *psetMap) const