CMS 3D CMS Logo

StreamerInputFile.cc
Go to the documentation of this file.
2 
9 
12 
13 #include <iomanip>
14 #include <iostream>
15 
16 namespace edm {
17 
19 
20  StreamerInputFile::StreamerInputFile(std::string const& name, std::shared_ptr<EventSkipperByID> eventSkipperByID)
21  : startMsg_(),
22  currentEvMsg_(),
23  headerBuf_(1000 * 1000),
24  eventBuf_(1000 * 1000 * 7),
25  currentFile_(0),
30  eventSkipperByID_(eventSkipperByID),
31  currRun_(0),
32  currProto_(0),
34  storage_(),
35  endOfFile_(false) {
36  openStreamerFile(name);
38  }
39 
40  StreamerInputFile::StreamerInputFile(std::vector<std::string> const& names,
41  std::shared_ptr<EventSkipperByID> eventSkipperByID)
42  : startMsg_(),
43  currentEvMsg_(),
44  headerBuf_(1000 * 1000),
45  eventBuf_(1000 * 1000 * 7),
46  currentFile_(0),
47  streamerNames_(names),
51  eventSkipperByID_(eventSkipperByID),
52  currRun_(0),
53  currProto_(0),
55  endOfFile_(false) {
56  openStreamerFile(names.at(0));
57  ++currentFile_;
59  currRun_ = startMsg_->run();
60  currProto_ = startMsg_->protocolVersion();
61  }
62 
65 
67  logFileAction(" Initiating request to open file ");
68 
69  IOOffset size = -1;
70  if (StorageFactory::get()->check(name, &size)) {
71  try {
73  } catch (cms::Exception& e) {
75  ex.addContext("Calling StreamerInputFile::openStreamerFile()");
76  ex.clearMessage();
77  ex << "Error Opening Streamer Input File: " << name << "\n";
78  throw ex;
79  }
80  } else {
81  throw Exception(errors::FileOpenError, "StreamerInputFile::openStreamerFile")
82  << "Error Opening Streamer Input File, file does not exist: " << name << "\n";
83  }
84  currentFileOpen_ = true;
85  logFileAction(" Successfully opened file ");
86  }
87 
89  if (currentFileOpen_ && storage_) {
90  storage_->close();
91  logFileAction(" Closed file ");
92  }
93  currentFileOpen_ = false;
94  }
95 
97  IOSize n = 0;
98  try {
99  n = storage_->read(buf, nBytes);
100  } catch (cms::Exception& ce) {
101  Exception ex(errors::FileReadError, "", ce);
102  ex.addContext("Calling StreamerInputFile::readBytes()");
103  throw ex;
104  }
105  return n;
106  }
107 
109  IOOffset n = 0;
110  try {
111  // We wish to return the number of bytes skipped, not the final offset.
112  n = storage_->position(0, Storage::CURRENT);
113  n = storage_->position(nBytes, Storage::CURRENT) - n;
114  } catch (cms::Exception& ce) {
115  Exception ex(errors::FileReadError, "", ce);
116  ex.addContext("Calling StreamerInputFile::skipBytes()");
117  throw ex;
118  }
119  return n;
120  }
121 
123  IOSize nWant = sizeof(HeaderView);
124  IOSize nGot = readBytes(&headerBuf_[0], nWant);
125  if (nGot != nWant) {
126  throw Exception(errors::FileReadError, "StreamerInputFile::readStartMessage")
127  << "Failed reading streamer file, first read in readStartMessage\n";
128  }
129 
130  HeaderView head(&headerBuf_[0]);
131  uint32 code = head.code();
132  if (code != Header::INIT)
133  {
134  throw Exception(errors::FileReadError, "StreamerInputFile::readStartMessage")
135  << "Expecting an init Message at start of file\n";
136  return;
137  }
138 
139  uint32 headerSize = head.size();
140  if (headerBuf_.size() < headerSize)
141  headerBuf_.resize(headerSize);
142 
143  if (headerSize > sizeof(HeaderView)) {
144  nWant = headerSize - sizeof(HeaderView);
145  nGot = readBytes(&headerBuf_[sizeof(HeaderView)], nWant);
146  if (nGot != nWant) {
147  throw Exception(errors::FileReadError, "StreamerInputFile::readStartMessage")
148  << "Failed reading streamer file, second read in readStartMessage\n";
149  }
150  } else {
151  throw Exception(errors::FileReadError, "StreamerInputFile::readStartMessage")
152  << "Failed reading streamer file, init header size from data too small\n";
153  }
154 
155  startMsg_ = std::make_shared<InitMsgView>(&headerBuf_[0]); // propagate_const<T> has no reset() function
156  }
157 
159  if (this->readEventMessage()) {
160  return true;
161  }
162  if (multiStreams_) {
163  //Try opening next file
164  if (openNextFile()) {
165  endOfFile_ = false;
166  if (this->readEventMessage()) {
167  return true;
168  }
169  }
170  }
171  return false;
172  }
173 
175  if (currentFile_ <= streamerNames_.size() - 1) {
176  FDEBUG(10) << "Opening file " << streamerNames_.at(currentFile_).c_str() << std::endl;
177 
179 
180  // If start message was already there, then compare the
181  // previous and new headers
182  if (startMsg_) {
183  FDEBUG(10) << "Comparing Header" << std::endl;
184  if (!compareHeader()) {
185  return false;
186  }
187  }
188  ++currentFile_;
189  return true;
190  }
191  return false;
192  }
193 
195  //Get the new header
197 
198  //Values from new Header should match up
199  if (currRun_ != startMsg_->run() || currProto_ != startMsg_->protocolVersion()) {
200  throw Exception(errors::MismatchedInputFiles, "StreamerInputFile::compareHeader")
201  << "File " << streamerNames_.at(currentFile_)
202  << "\nhas different run number or protocol version than previous\n";
203  return false;
204  }
205  newHeader_ = true;
206  return true;
207  }
208 
210  if (endOfFile_)
211  return 0;
212 
213  bool eventRead = false;
214  while (!eventRead) {
215  IOSize nWant = sizeof(EventHeader);
216  IOSize nGot = readBytes(&eventBuf_[0], nWant);
217  if (nGot == 0) {
218  // no more data available
219  endOfFile_ = true;
220  return 0;
221  }
222  if (nGot != nWant) {
223  throw edm::Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
224  << "Failed reading streamer file, first read in readEventMessage\n"
225  << "Requested " << nWant << " bytes, read function returned " << nGot << " bytes\n";
226  }
227  HeaderView head(&eventBuf_[0]);
228  uint32 code = head.code();
229 
230  // If it is not an event then something is wrong.
231  if (code != Header::EVENT) {
232  throw Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
233  << "Failed reading streamer file, unknown code in event header\n"
234  << "code = " << code << "\n";
235  }
236  uint32 eventSize = head.size();
237  if (eventSize <= sizeof(EventHeader)) {
238  throw edm::Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
239  << "Failed reading streamer file, event header size from data too small\n";
240  }
241  eventRead = true;
242  if (eventSkipperByID_) {
243  EventHeader* evh = (EventHeader*)(&eventBuf_[0]);
244  if (eventSkipperByID_->skipIt(convert32(evh->run_), convert32(evh->lumi_), convert64(evh->event_))) {
245  eventRead = false;
246  }
247  }
248  nWant = eventSize - sizeof(EventHeader);
249  if (eventRead) {
250  if (eventBuf_.size() < eventSize)
251  eventBuf_.resize(eventSize);
252  nGot = readBytes(&eventBuf_[sizeof(EventHeader)], nWant);
253  if (nGot != nWant) {
254  throw Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
255  << "Failed reading streamer file, second read in readEventMessage\n"
256  << "Requested " << nWant << " bytes, read function returned " << nGot << " bytes\n";
257  }
258  } else {
259  nGot = skipBytes(nWant);
260  if (nGot != nWant) {
261  throw Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
262  << "Failed reading streamer file, skip event in readEventMessage\n"
263  << "Requested " << nWant << " bytes skipped, seek function returned " << nGot << " bytes\n";
264  }
265  }
266  }
267  currentEvMsg_ = std::make_shared<EventMsgView>((void*)&eventBuf_[0]); // propagate_const<T> has no reset() function
268  return 1;
269  }
270 
272  LogAbsolute("fileAction") << std::setprecision(0) << TimeOfDay() << msg << currentFileName_;
273  FlushMessageLog();
274  }
275 } // namespace edm
size
Write out results.
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
uint32 code() const
Definition: MsgHeader.h:43
const std::string names[nVars_]
void closeStreamerFile()
Needs to be public because of forking.
static const StorageFactory * get(void)
IOOffset skipBytes(IOSize nBytes)
std::vector< std::string > streamerNames_
void clearMessage()
Definition: Exception.cc:159
unsigned int uint32
Definition: MsgTools.h:13
char_uint32 run_
Definition: EventMessage.h:64
char_uint64 event_
Definition: EventMessage.h:65
uint32 size() const
Definition: MsgHeader.h:44
void logFileAction(char const *msg)
IOSize readBytes(char *buf, IOSize nBytes)
void addContext(std::string const &context)
Definition: Exception.cc:165
tuple msg
Definition: mps_check.py:279
int64_t IOOffset
Definition: IOTypes.h:19
uint32 convert32(char_uint32 v)
Definition: MsgTools.h:28
std::vector< char > headerBuf_
HLT enums.
edm::propagate_const< std::shared_ptr< EventMsgView > > currentEvMsg_
size_t IOSize
Definition: IOTypes.h:14
#define FDEBUG(lev)
Definition: DebugMacros.h:19
edm::propagate_const< std::shared_ptr< InitMsgView > > startMsg_
StreamerInputFile(std::string const &name, std::shared_ptr< EventSkipperByID > eventSkipperByID=std::shared_ptr< EventSkipperByID >())
void openStreamerFile(std::string const &name)
static void check(T const &p, std::string const &id, SelectedProducts const &iProducts)
edm::propagate_const< std::unique_ptr< Storage > > storage_
std::unique_ptr< Storage > open(const std::string &url, int mode=IOFlags::OpenRead) const