CMS 3D CMS Logo

StreamerInputFile.cc
Go to the documentation of this file.
2 
10 
13 
14 #include <iomanip>
15 #include <iostream>
16 
17 namespace edm {
18 
20 
22  std::string const& LFN,
23  std::shared_ptr<EventSkipperByID> eventSkipperByID,
24  unsigned int prefetchMBytes)
25  : startMsg_(),
26  currentEvMsg_(),
27  headerBuf_(1000 * 1000),
28  eventBuf_(1000 * 1000 * 7),
29  tempBuf_(1024 * 1024 * prefetchMBytes),
30  currentFile_(0),
31  streamerNames_(),
32  multiStreams_(false),
33  currentFileName_(),
34  currentFileOpen_(false),
35  eventSkipperByID_(eventSkipperByID),
36  currRun_(0),
37  currProto_(0),
38  newHeader_(false),
39  storage_(),
40  endOfFile_(false) {
41  openStreamerFile(name, LFN);
43  }
44 
46  std::shared_ptr<EventSkipperByID> eventSkipperByID,
47  unsigned int prefetchMBytes)
48  : StreamerInputFile(name, name, eventSkipperByID, prefetchMBytes) {}
49 
50  StreamerInputFile::StreamerInputFile(std::vector<FileCatalogItem> const& names,
51  std::shared_ptr<EventSkipperByID> eventSkipperByID,
52  unsigned int prefetchMBytes)
53  : startMsg_(),
54  currentEvMsg_(),
55  headerBuf_(1000 * 1000),
56  eventBuf_(1000 * 1000 * 7),
57  tempBuf_(1024 * 1024 * prefetchMBytes),
58  currentFile_(0),
59  streamerNames_(names),
60  multiStreams_(true),
61  currentFileName_(),
62  currentFileOpen_(false),
63  eventSkipperByID_(eventSkipperByID),
64  currRun_(0),
65  currProto_(0),
66  newHeader_(false),
67  endOfFile_(false) {
68  openStreamerFile(names.at(0).fileName(0), names.at(0).logicalFileName());
69  ++currentFile_;
71  currRun_ = startMsg_->run();
72  currProto_ = startMsg_->protocolVersion();
73  }
74 
77 
79 
80  // Check if the logical file name was found.
81  if (currentFileName_.empty()) {
82  // LFN not found in catalog.
83  throw cms::Exception("LogicalFileNameNotFound", "StreamerInputFile::openStreamerFile()\n")
84  << "Logical file name '" << LFN << "' was not found in the file catalog.\n"
85  << "If you wanted a local file, you forgot the 'file:' prefix\n"
86  << "before the file name in your configuration file.\n";
87  return;
88  }
89 
90  logFileAction(" Initiating request to open file ");
91 
92  using namespace edm::storage;
93  IOOffset size = -1;
94  if (StorageFactory::get()->check(name, &size)) {
95  try {
97  } catch (cms::Exception& e) {
99  ex.addContext("Calling StreamerInputFile::openStreamerFile()");
100  ex.clearMessage();
101  ex << "Error Opening Streamer Input File: " << name << "\n";
102  throw ex;
103  }
104  } else {
105  throw Exception(errors::FileOpenError, "StreamerInputFile::openStreamerFile")
106  << "Error Opening Streamer Input File, file does not exist: " << name << "\n";
107  }
108  currentFileOpen_ = true;
109  logFileAction(" Successfully opened file ");
110  }
111 
113  if (currentFileOpen_ && storage_) {
114  storage_->close();
115  logFileAction(" Closed file ");
116  }
117  currentFileOpen_ = false;
118  }
119 
120  std::pair<storage::IOSize, char*> StreamerInputFile::readBytes(char* buf,
121  storage::IOSize nBytes,
122  bool zeroCopy,
123  unsigned int skippedHdr) {
124  storage::IOSize n = 0;
125  //returned pointer should point to the beginning of the header
126  //even if we read event payload that comes afterwards
127  char* ptr = buf - skippedHdr;
128  try {
129  if (!tempBuf_.empty()) {
130  if (tempPos_ == tempLen_) {
131  n = storage_->read(&tempBuf_[0], tempBuf_.size());
132  tempPos_ = 0;
133  tempLen_ = n;
134  if (n == 0)
135  return std::pair<storage::IOSize, char*>(0, ptr);
136  }
137  if (nBytes <= tempLen_ - tempPos_) {
138  //zero-copy can't done when header start address is in the previous buffer
139  if (!zeroCopy || skippedHdr > tempPos_) {
140  memcpy(buf, &tempBuf_[0] + tempPos_, nBytes);
141  tempPos_ += nBytes;
142  } else {
143  //pass pointer to the prebuffer address (zero copy) at the beginning of the header
144  ptr = &tempBuf_[0] + tempPos_ - skippedHdr;
145  tempPos_ += nBytes;
146  }
147  n = nBytes;
148  } else {
149  //crossing buffer boundary
150  auto len = tempLen_ - tempPos_;
151  memcpy(buf, &tempBuf_[0] + tempPos_, len);
152  tempPos_ += len;
153  char* tmpPtr = buf + len;
154  n = len + readBytes(tmpPtr, nBytes - len, false).first;
155  }
156  } else
157  n = storage_->read(buf, nBytes);
158  } catch (cms::Exception& ce) {
159  Exception ex(errors::FileReadError, "", ce);
160  ex.addContext("Calling StreamerInputFile::readBytes()");
161  throw ex;
162  }
163  return std::pair<storage::IOSize, char*>(n, ptr);
164  }
165 
167  storage::IOOffset n = 0;
168  try {
169  // We wish to return the number of bytes skipped, not the final offset.
170  n = storage_->position(0, storage::Storage::CURRENT);
171  n = storage_->position(nBytes, storage::Storage::CURRENT) - n;
172  } catch (cms::Exception& ce) {
173  Exception ex(errors::FileReadError, "", ce);
174  ex.addContext("Calling StreamerInputFile::skipBytes()");
175  throw ex;
176  }
177  return n;
178  }
179 
181  using namespace edm::storage;
182  IOSize nWant = sizeof(HeaderView);
183  IOSize nGot = readBytes(&headerBuf_[0], nWant, false).first;
184  if (nGot != nWant) {
185  throw Exception(errors::FileReadError, "StreamerInputFile::readStartMessage")
186  << "Failed reading streamer file, first read in readStartMessage\n";
187  }
188 
189  uint32 headerSize;
190  {
191  HeaderView head(&headerBuf_[0]);
192  uint32 code = head.code();
193  if (code != Header::INIT)
194  {
195  throw Exception(errors::FileReadError, "StreamerInputFile::readStartMessage")
196  << "Expecting an init Message at start of file\n";
197  return;
198  }
199  headerSize = head.size();
200  }
201 
202  if (headerBuf_.size() < headerSize)
203  headerBuf_.resize(headerSize);
204 
205  if (headerSize > sizeof(HeaderView)) {
206  nWant = headerSize - sizeof(HeaderView);
207  auto res = readBytes(&headerBuf_[sizeof(HeaderView)], nWant, true, sizeof(HeaderView));
208  if (res.first != nWant) {
209  throw Exception(errors::FileReadError, "StreamerInputFile::readStartMessage")
210  << "Failed reading streamer file, second read in readStartMessage\n";
211  }
212  startMsg_ = std::make_shared<InitMsgView>(res.second); // propagate_const<T> has no reset() function
213  } else {
214  throw Exception(errors::FileReadError, "StreamerInputFile::readStartMessage")
215  << "Failed reading streamer file, init header size from data too small\n";
216  }
217  }
218 
220  if (this->readEventMessage()) {
221  return Next::kEvent;
222  }
223  if (multiStreams_) {
224  //Try opening next file
225  if (currentFile_ <= streamerNames_.size() - 1) {
226  newHeader_ = true;
227  return Next::kFile;
228  }
229  }
230  return Next::kStop;
231  }
232 
234  if (currentFile_ <= streamerNames_.size() - 1) {
235  FDEBUG(10) << "Opening file " << streamerNames_.at(currentFile_).fileNames()[0].c_str() << std::endl;
236 
237  openStreamerFile(streamerNames_.at(currentFile_).fileNames()[0],
238  streamerNames_.at(currentFile_).logicalFileName());
239 
240  // If start message was already there, then compare the
241  // previous and new headers
242  if (startMsg_) {
243  FDEBUG(10) << "Comparing Header" << std::endl;
244  compareHeader();
245  }
246  ++currentFile_;
247  endOfFile_ = false;
248  return true;
249  }
250  return false;
251  }
252 
254  //Get the new header
256 
257  //Values from new Header should match up
258  if (currRun_ != startMsg_->run() || currProto_ != startMsg_->protocolVersion()) {
259  throw Exception(errors::MismatchedInputFiles, "StreamerInputFile::compareHeader")
260  << "File " << streamerNames_.at(currentFile_).fileNames()[0]
261  << "\nhas different run number or protocol version than previous\n";
262  return false;
263  }
264  return true;
265  }
266 
268  if (endOfFile_)
269  return 0;
270 
271  using namespace edm::storage;
272  bool eventRead = false;
273  while (!eventRead) {
274  IOSize nWant = sizeof(EventHeader);
275  IOSize nGot = readBytes(&eventBuf_[0], nWant, false).first;
276  if (nGot == 0) {
277  // no more data available
278  endOfFile_ = true;
279  return 0;
280  }
281  if (nGot != nWant) {
282  throw edm::Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
283  << "Failed reading streamer file, first read in readEventMessage\n"
284  << "Requested " << nWant << " bytes, read function returned " << nGot << " bytes\n";
285  }
286  uint32 eventSize;
287  {
288  HeaderView head(&eventBuf_[0]);
289  uint32 code = head.code();
290 
291  // If it is not an event then something is wrong.
292  if (code != Header::EVENT) {
293  throw Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
294  << "Failed reading streamer file, unknown code in event header\n"
295  << "code = " << code << "\n";
296  }
297  eventSize = head.size();
298  }
299  if (eventSize <= sizeof(EventHeader)) {
300  throw edm::Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
301  << "Failed reading streamer file, event header size from data too small\n";
302  }
303  eventRead = true;
304  if (eventSkipperByID_) {
305  EventHeader* evh = (EventHeader*)(&eventBuf_[0]);
306  if (eventSkipperByID_->skipIt(convert32(evh->run_), convert32(evh->lumi_), convert64(evh->event_))) {
307  eventRead = false;
308  }
309  }
310  nWant = eventSize - sizeof(EventHeader);
311  if (eventRead) {
312  if (eventBuf_.size() < eventSize)
313  eventBuf_.resize(eventSize);
314 
315  auto res = readBytes(&eventBuf_[sizeof(EventHeader)], nWant, true, sizeof(EventHeader));
316  if (res.first != nWant) {
317  throw Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
318  << "Failed reading streamer file, second read in readEventMessage\n"
319  << "Requested " << nWant << " bytes, read function returned " << res.first << " bytes\n";
320  }
321  currentEvMsg_ =
322  std::make_shared<EventMsgView>((void*)res.second); // propagate_const<T> has no reset() function
323  } else {
324  nGot = skipBytes(nWant);
325  if (nGot != nWant) {
326  throw Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
327  << "Failed reading streamer file, skip event in readEventMessage\n"
328  << "Requested " << nWant << " bytes skipped, seek function returned " << nGot << " bytes\n";
329  }
330  }
331  }
332  return 1;
333  }
334 
336  LogAbsolute("fileAction") << std::setprecision(0) << TimeOfDay() << msg << currentFileName_;
337  FlushMessageLog();
338  }
339 } // namespace edm
size
Write out results.
int64_t IOOffset
Definition: IOTypes.h:20
uint64 convert64(char_uint64 v)
Definition: MsgTools.h:20
std::vector< char > eventBuf_
void FlushMessageLog()
edm::propagate_const< std::shared_ptr< EventSkipperByID > > eventSkipperByID_
char_uint32 lumi_
Definition: EventMessage.h:66
#define FDEBUG(lev)
Definition: DebugMacros.h:19
const std::string names[nVars_]
Definition: Electron.h:6
std::pair< storage::IOSize, char * > readBytes(char *buf, storage::IOSize nBytes, bool zeroCopy, unsigned int skippedHdr=0)
uint32 size() const
Definition: MsgHeader.h:44
storage::IOOffset skipBytes(storage::IOSize nBytes)
StreamerInputFile(std::string const &name, std::string const &LFN, std::shared_ptr< EventSkipperByID > eventSkipperByID=std::shared_ptr< EventSkipperByID >(), unsigned int prefetchMBytes=0)
void clearMessage()
Definition: Exception.cc:159
void openStreamerFile(std::string const &name, std::string const &LFN)
unsigned int uint32
Definition: MsgTools.h:13
uint32 code() const
Definition: MsgHeader.h:43
char_uint32 run_
Definition: EventMessage.h:64
size_t IOSize
Definition: IOTypes.h:15
char_uint64 event_
Definition: EventMessage.h:65
void logFileAction(char const *msg)
void addContext(std::string const &context)
Definition: Exception.cc:165
tuple msg
Definition: mps_check.py:286
static void check(T const &p, std::string const &id, SelectedProducts const &iProducts, bool iVerbose)
uint32 convert32(char_uint32 v)
Definition: MsgTools.h:28
std::vector< char > headerBuf_
HLT enums.
edm::propagate_const< std::shared_ptr< EventMsgView > > currentEvMsg_
std::vector< FileCatalogItem > streamerNames_
Log< level::System, true > LogAbsolute
#define get
std::vector< char > tempBuf_
edm::propagate_const< std::shared_ptr< InitMsgView > > startMsg_
edm::propagate_const< std::unique_ptr< edm::storage::Storage > > storage_