CMS 3D CMS Logo

LmfSource.cc
Go to the documentation of this file.
1 /*
2  * \author Philippe Gras CEA/Saclay
3  */
4 
5 #include <iostream>
6 #include <iomanip>
15 
16 using namespace edm;
17 using namespace std;
18 
19 const unsigned char LmfSource::minDataFormatVersion_ = 4;
20 const unsigned char LmfSource::maxDataFormatVersion_ = 5;
21 const unsigned LmfSource::fileHeaderSize = 2;
22 
25  fileNames_(pset.getParameter<vector<string> >("fileNames")),
26  iFile_(-1),
27  fedId_(-1),
28  fileHeader_(fileHeaderSize),
29  dataFormatVers_(5),
30  rcRead_(false),
31  preScale_(pset.getParameter<unsigned>("preScale")),
32  iEvent_(0),
33  iEventInFile_(0),
34  indexTablePos_(0),
35  orderedRead_(pset.getParameter<bool>("orderedRead")),
36  watchFileList_(pset.getParameter<bool>("watchFileList")),
37  fileListName_(pset.getParameter<std::string>("fileListName")),
38  inputDir_(pset.getParameter<std::string>("inputDir")),
39  nSecondsToSleep_(pset.getParameter<int>("nSecondsToSleep")),
40  verbosity_(pset.getUntrackedParameter<int>("verbosity")) {
41  if (preScale_ == 0)
42  preScale_ = 1;
43  produces<FEDRawDataCollection>();
44  // open fileListName
45  if (watchFileList_) {
46  fileList_.open(fileListName_.c_str());
47  if (fileList_.fail()) {
48  throw cms::Exception("FileListOpenError") << "Failed to open input file " << fileListName_ << "\n";
49  }
50  } else {
51  //throws a cms exception if error in fileNames parameter
53  }
54 }
55 
57  if (iFile_ == -1)
58  return false; //no file open
59 
60  if (verbosity_)
61  cout << "[LmfSource]"
62  << "Opening file #" << (iFile_ + 1) << " '" << currentFileName_ << "'\n";
63 
64  in_.read((char*)&fileHeader_[0], fileHeaderSize * sizeof(uint32_t));
65 
66  if (in_.eof())
67  return false;
68 
69  if (verbosity_) {
70  cout << "[LmfSource]"
71  << "File header (in hex):" << hex;
72  for (unsigned i = 0; i < fileHeaderSize; ++i) {
73  if (i % 8 == 0)
74  cout << "\n";
75  cout << setw(8) << fileHeader_[i] << " ";
76  }
77  cout << dec << "\n";
78  }
79 
80  char id[4];
81 
82  id[0] = fileHeader_[0] & 0xFF;
83  id[1] = (fileHeader_[0] >> 8) & 0xFF;
84  id[2] = (fileHeader_[0] >> 16) & 0xFF;
85  id[3] = (fileHeader_[0] >> 24) & 0xFF;
86 
87  if (!(id[0] == 'L' && id[1] == 'M' && id[2] == 'F')) {
88  throw cms::Exception("FileReadError") << currentFileName_ << " is not a file in LMF format!";
89  }
90  dataFormatVers_ = id[3];
91  if (verbosity_)
92  cout << "[LmfSource]"
93  << "LMF format: " << (int)dataFormatVers_ << "\n";
94 
96  throw cms::Exception("FileReadError") << currentFileName_ << ": LMF format version " << (int)dataFormatVers_
97  << " is not supported by this release of LmfSource module";
98  }
99 
101 
102  if (verbosity_)
103  cout << "[LmfSource] File position of index table: 0x" << setfill('0') << hex << setw(8) << indexTablePos_
104  << setfill(' ') << dec << "\n";
105 
106  if (dataFormatVers_ < 5) {
107  in_.ignore(4);
108  }
109 
110  return true;
111 }
112 
114  // bool rc;
115  // while(!((rc = readFileHeader()) & readEventPayload())){
116  // if(openFile(++iFile_)==false){//no more files
117  // if(verbosity_) cout << "[LmfSource]"
118  // << "No more input file";
119  // return false;
120  // }
121  // }
122  auto coll = std::make_unique<FEDRawDataCollection>();
123  coll->swap(fedColl_);
124  if (verbosity_)
125  cout << "[LmfSource] Putting FEDRawDataCollection in event\n";
126  evt.put(std::move(coll));
127 }
128 
129 bool LmfSource::openFile(int iFile) {
130  iEventInFile_ = 0;
131  if (watchFileList_) {
132  for (;;) {
133  // read the first field of the line, which must be the filename
136  if (!fileList_.fail()) {
137  // skip the rest of the line
138  std::string tmp_buffer;
139  std::getline(fileList_, tmp_buffer);
140  if (verbosity_)
141  cout << "[LmfSource]"
142  << "Opening file " << currentFileName_ << "\n";
143  in_.open(currentFileName_.c_str());
144  if (!in_.fail()) {
145  // file was successfully open
146  return true;
147  } else {
148  // skip file
149  edm::LogError("FileOpenError") << "Failed to open input file " << currentFileName_ << ". Skipping file\n";
150  in_.close();
151  in_.clear();
152  }
153  }
154  // if here, no new file is available: sleep and retry later
155  if (verbosity_)
156  std::cout << "[LmfSource]"
157  << " going to sleep 5 seconds\n";
158  sleep(nSecondsToSleep_);
159  fileList_.clear();
160  }
161  } else {
162  if (iFile > (int)fileNames_.size() - 1)
163  return false;
164  currentFileName_ = fileNames_[iFile];
165  if (verbosity_)
166  cout << "[LmfSource]"
167  << "Opening file " << currentFileName_ << "\n";
168  in_.open(currentFileName_.c_str());
169  if (in_.fail()) {
170  throw cms::Exception("FileOpenError") << "Failed to open input file " << currentFileName_ << "\n";
171  }
172  }
173  return true;
174 }
175 
177  if (iFile_ < 0)
178  return false; //no file opened.
179  if (orderedRead_) {
180  if (iEventInFile_ >= indexTable_.size())
181  return false;
182  if (verbosity_) {
183  cout << "[LmfSource] move to event with orbit Id " << indexTable_[iEventInFile_].orbit << " at file position 0x"
184  << hex << setfill('0') << setw(8) << indexTable_[iEventInFile_].filePos << setfill(' ') << dec << "\n";
185  }
186  const streampos pos = indexTable_[iEventInFile_].filePos;
187  in_.clear();
188  in_.seekg(pos);
189  if (in_.bad()) {
190  cout << "[LmfSource] Problem while reading file " << currentFileName_ << ". Problem with event index table?\n";
191  return false;
192  }
193  ++iEventInFile_;
194  return true;
195  } else {
196  return true;
197  }
198 }
199 
200 bool LmfSource::readEvent(bool doSkip) {
201  while (!(nextEventWithinFile() && readEventWithinFile(doSkip))) {
202  //failed to read event. Let's look for next file:
203  in_.close();
204  in_.clear();
205  bool rcOpen = openFile(++iFile_);
206  if (rcOpen == false) { //no more files
207  if (verbosity_)
208  cout << "[LmfSource]"
209  << "No more input file";
210  rcRead_ = false;
211  return rcRead_;
212  }
214  if (verbosity_)
215  cout << "File header readout " << (rcRead_ ? "succeeded" : "failed") << "\n";
216  if (rcRead_ && orderedRead_)
217  readIndexTable();
218  }
219  return rcRead_;
220 }
221 
223  //empties collection:
224  if (fedId_ > 0) {
226  }
227  if (verbosity_)
228  cout << "[LmfSource]"
229  << "About to read event...\n";
230 
231  bool rc;
232  for (;;) {
233  if (filter()) { //event to read
234  rc = readEvent();
235  break; //either event is read or no more event
236  } else { //event to skip
237  rc = readEvent(true);
238  if (rc == false) { //no more events
239  break;
240  }
241  }
242  }
243 
244  if (!rc)
245  return false; //event readout failed
246 
247  if (verbosity_)
248  cout << "[LmfSource]"
249  << "Setting event time to " << /*toString(*/ timeStamp_ /*)*/ << ", "
250  << "Run number to " << runNum_ << ","
251  << "Event number to " << eventNum_ << "\n";
252 
253  time = timeStamp_;
255  return true;
256 }
257 
259  if (iFile_ == -1 || !rcRead_)
260  return false; //no file open
261  // or header reading failed
262  //number of 32-bit word to read first to get the event size
263  //field
264  const int timeStamp32[] = {0, 0}; //timestamp is 64-bit long
265  const int lumiBlock32[] = {2, 2};
266  const int runNum32[] = {3, 3};
267  const int orbitNum32[] = {4, 4};
268  const int bx32[] = {5, 5};
269  const int eventNum32[] = {6, 6};
270  const int activeFedId32[] = {7, -1};
271  const int calibTrig32[] = {-1, 7};
272  const int nFeds32[] = {-1, 8};
273  // const int reserved32[] = {-1,9};
274  const int evtHeadSize32[] = {8, 10};
275 
276  const unsigned char iv = dataFormatVers_ - minDataFormatVersion_;
277  assert(iv <= sizeof(timeStamp32) / sizeof(timeStamp32[0]));
278 
279  if ((int)header_.size() < evtHeadSize32[iv])
280  header_.resize(evtHeadSize32[iv]);
281 
282  if (verbosity_)
283  cout << "[LmfSource]"
284  << "Reading event header\n";
285 
286  in_.read((char*)&header_[0], evtHeadSize32[iv] * 4);
287  if (in_.bad()) { //reading error other than eof
288  throw cms::Exception("FileReadError") << "Error while reading from file " << currentFileName_;
289  }
290  if (in_.eof())
291  return false;
292 
293  if (verbosity_) {
294  cout << "[LmfSource]"
295  << "Event header (in hex):" << hex << setfill('0');
296  for (int i = 0; i < evtHeadSize32[iv]; ++i) {
297  if (i % 8 == 0)
298  cout << "\n";
299  cout << setw(8) << header_[i] << " ";
300  }
301  cout << dec << setfill(' ') << "\n";
302  }
303 
304  timeStamp_ = *(uint64_t*)&header_[timeStamp32[iv]];
305  lumiBlock_ = header_[lumiBlock32[iv]];
306  runNum_ = header_[runNum32[iv]];
307  orbitNum_ = header_[orbitNum32[iv]];
308  eventNum_ = header_[eventNum32[iv]];
309  bx_ = header_[bx32[iv]];
310  calibTrig_ = calibTrig32[iv] >= 0 ? header_[calibTrig32[iv]] : 0;
311  int activeFedId = activeFedId32[iv] >= 0 ? header_[activeFedId32[iv]] : ((calibTrig_ & 0x3F) + 600);
312  nFeds_ = nFeds32[iv] < 0 ? 1 : header_[nFeds32[iv]];
313 
314  if (verbosity_) {
315  time_t t = time_t(timeStamp_ >> 32);
316  div_t t_ms_us = div(timeStamp_ & 0xFFFFFFFF, 1000);
317  char tbuf[256];
318  strftime(tbuf, sizeof(tbuf), "%F %T", localtime(&t));
319  tbuf[sizeof(tbuf) - 1] = 0;
320  cout << "[LmfSource] "
321  << "timeStamp: " << /*toString(timeStamp_)*/ timeStamp_ << " (" << tbuf << " " << t_ms_us.quot
322  << " ms " << t_ms_us.rem << " us)\n"
323  << "lumiBlock: " << lumiBlock_ << "\n"
324  << "runNum: " << runNum_ << "\n"
325  << "orbitNum: " << orbitNum_ << "\n"
326  << "eventNum: " << eventNum_ << "\n"
327  << "bx: " << bx_ << "\n"
328  << "activeFedId: " << activeFedId << "\n"
329  << "Calib trigger type: " << ((calibTrig_ >> 8) & 0x3) << "\n"
330  << "Color: " << ((calibTrig_ >> 6) & 0x3) << "\n"
331  << "Side: " << ((calibTrig_ >> 11) & 0x1) << "\n"
332  << "nFeds: " << nFeds_ << "\n";
333  }
334 
335  const int dccLenOffset32 = 2;
336  const int fedIdOffset32 = 0;
337  const int nPreRead32 = 3;
338  vector<int32_t> buf(nPreRead32);
339  for (int iFed = 0; iFed < nFeds_; ++iFed) {
340  in_.read((char*)&buf[0], nPreRead32 * sizeof(uint32_t));
341 
342  if (verbosity_) {
343  cout << "[LmfSource] " << nPreRead32 << " first 32-bit words of "
344  << "FED block: " << hex << setfill('0');
345  for (unsigned i = 0; i < buf.size(); ++i) {
346  cout << "0x" << setw(8) << buf[i] << " ";
347  }
348  cout << dec << setfill(' ');
349  }
350 
351  if (in_.bad())
352  return false;
353 
354  const unsigned eventSize64 = buf[dccLenOffset32] & 0x00FFFFFF;
355  const unsigned eventSize32 = eventSize64 * 2;
356  const unsigned eventSize8 = eventSize64 * 8;
357  const unsigned fedId_ = (buf[fedIdOffset32] >> 8) & 0xFFF;
358 
359  if (eventSize8 > maxEventSize_) {
360  throw cms::Exception("FileReadError")
361  << "Size of event fragment (FED block) read from "
362  << " data of file " << currentFileName_ << "is unexpctively large (" << (eventSize8 >> 10) << " kByte). "
363  << "This must be an error (corrupted file?)\n";
364  }
365 
367  throw cms::Exception("FileReadError") << "Invalid FED number read from data file.";
368  }
369 
370  int32_t toRead8 = (eventSize32 - nPreRead32) * sizeof(int32_t);
371 
372  if (toRead8 < 0) {
373  throw cms::Exception("FileReadError")
374  << "Event size error while reading an event from file " << currentFileName_ << "\n";
375  }
376 
377  if (doSkip) { //event to skip
378  if (verbosity_)
379  cout << "[LmfSource] "
380  << "Skipping on event. Move file pointer " << toRead8 << " ahead.\n";
381  in_.seekg(toRead8, ios::cur);
382  if (in_.bad()) { //reading error other than eof
383  throw cms::Exception("FileReadError") << "Error while reading from file " << currentFileName_;
384  }
385  } else {
386  //reads FED data:
387  FEDRawData& data_ = fedColl_.FEDData(fedId_);
388  data_.resize(eventSize8);
389 
390  //copy already read data:
391  copy(buf.begin(), buf.end(), (int32_t*)data_.data());
392 
393  in_.read((char*)(data_.data()) + nPreRead32 * 4, toRead8);
394 
395  if (in_.bad()) { //reading error other than eof
396  throw cms::Exception("FileReadError") << "Error while reading from file " << currentFileName_;
397  }
398 
399  if (verbosity_ && data_.size() > 16) {
400  cout << "[LmfSource]"
401  << "Head of DCC data (in hex):" << hex;
402  for (int i = 0; i < 16; ++i) {
403  if (i % 8 == 0)
404  cout << "\n";
405  cout << setw(8) << ((uint32_t*)data_.data())[i] << " ";
406  }
407  cout << dec << "\n";
408  }
409 
410  if (dataFormatVers_ <= 4) { //calib trigger in not in event header.
411  // gets it from DCC block
412  calibTrig_ = (((uint32_t*)data_.data())[5] & 0xFC0) | ((activeFedId - 600) & 0x3F);
413  if (verbosity_) {
414  cout << "[LmfSource] Old data format. "
415  "Uses information read from FED block to retrieve calibration "
416  "trigger type. Value is: 0x"
417  << hex << setfill('0') << setw(3) << calibTrig_ << setfill(' ') << dec << "\n";
418  }
419  }
420  }
421  if (in_.eof())
422  return false;
423  }
424  ++iEvent_;
425  return true;
426 }
427 
428 bool LmfSource::filter() const { return (iEvent_ % preScale_ == 0); }
429 
431  char buf[256];
432  const int secTousec = 1000 * 1000;
433  time_t tsec = t / secTousec;
434  uint32_t tusec = (uint32_t)(t - tsec);
435  strftime(buf, sizeof(buf), "%F %R %S s", localtime(&tsec));
436  buf[sizeof(buf) - 1] = 0;
437  stringstream buf2;
438  buf2 << (tusec + 500) / 1000;
439  return string(buf) + " " + buf2.str() + " ms";
440 }
441 
443  for (unsigned i = 0; i < fileNames_.size(); ++i) {
445  const char s[] = "file:";
446  if (fileName.compare(0, sizeof(s) - 1, s) == 0) { //file: prefix => to strip
447  fileName.erase(fileName.begin(), fileName.begin() + sizeof(s) - 1);
448  }
449  if (fileName.find_first_of(':') != string::npos) {
450  throw cms::Exception("LmfSource") << "Character ':' is not allowed in paths specified fileNames "
451  << "parameter. Please note only local file (or NFS, AFS)"
452  << " is supported (no rfio, no /store)";
453  }
454  const char s1[] = "/store";
455  if (fileName.compare(0, sizeof(s1) - 1, s1) == 0) {
456  throw cms::Exception("LmfSource") << "CMSSW /store not supported by LmfSource. Only local file "
457  << "(or NFS/AFS) allowed. Path starting with /store not permitted";
458  }
459  }
460 }
461 
463  stringstream errMsg;
464  errMsg << "Error while reading event index table of file " << currentFileName_ << ". Try to read it with "
465  << "option orderedRead disabled.\n";
466 
467  if (indexTablePos_ == 0)
468  throw cms::Exception("LmfSource") << errMsg.str();
469 
470  in_.clear();
471  in_.seekg(indexTablePos_);
472 
473  uint32_t nevts = 0;
474  in_.read((char*)&nevts, sizeof(nevts));
475  in_.ignore(4);
476  if (nevts > maxEvents_) {
477  throw cms::Exception("LmfSource") << "Number of events indicated in event index of file " << currentFileName_
478  << " is unexpectively large. File cannot be "
479  << "read in time-ordered event mode. See orderedRead parmater of "
480  << "LmfSource module.\n";
481  }
482  //if(in_.bad()) throw cms::Exception("LmfSource") << errMsg.str();
483  if (in_.bad())
484  throw cms::Exception("LmfSource") << errMsg.str();
485  indexTable_.resize(nevts);
486  in_.read((char*)&indexTable_[0], nevts * sizeof(IndexRecord));
487 }
int iFile_
Definition: LmfSource.h:64
int nSecondsToSleep_
Definition: LmfSource.h:197
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
uint32_t orbitNum_
Definition: LmfSource.h:130
bool openFile(int iFile)
Definition: LmfSource.cc:129
std::ifstream in_
Definition: LmfSource.h:134
int32_t *__restrict__ iv
static const unsigned fileHeaderSize
Definition: LmfSource.h:83
uint32_t runNum_
Definition: LmfSource.h:127
bool filter() const
Definition: LmfSource.cc:428
bool readEventWithinFile(bool doSkip)
Definition: LmfSource.cc:258
uint32_t indexTablePos_
Definition: LmfSource.h:150
bool rcRead_
Definition: LmfSource.h:138
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:45
std::vector< uint32_t > header_
Definition: LmfSource.h:81
std::string fileListName_
Definition: LmfSource.h:183
Log< level::Error, false > LogError
assert(be >=bs)
void checkFileNames()
Definition: LmfSource.cc:442
uint32_t iEventInFile_
Definition: LmfSource.h:148
uint32_t iEvent_
Definition: LmfSource.h:144
LmfSource(const edm::ParameterSet &pset, const edm::InputSourceDescription &isd)
Definition: LmfSource.cc:23
unsigned preScale_
Definition: LmfSource.h:140
int nFeds_
Definition: LmfSource.h:154
void resize(size_t newsize)
Definition: FEDRawData.cc:28
static const unsigned char minDataFormatVersion_
Definition: LmfSource.h:91
static const unsigned maxEvents_
Definition: LmfSource.h:164
uint32_t bx_
Definition: LmfSource.h:128
FEDRawDataCollection fedColl_
Definition: LmfSource.h:68
std::string inputDir_
Definition: LmfSource.h:187
int verbosity_
Definition: LmfSource.h:201
static const unsigned maxEventSize_
Definition: LmfSource.h:169
int calibTrig_
Definition: LmfSource.h:152
unsigned long long TimeValue_t
Definition: Timestamp.h:21
uint32_t lumiBlock_
Definition: LmfSource.h:126
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
bool setRunAndEventInfo(edm::EventID &id, edm::TimeValue_t &time, edm::EventAuxiliary::ExperimentType &eType) override
Definition: LmfSource.cc:222
void produce(edm::Event &e) override
Definition: LmfSource.cc:113
unsigned long long uint64_t
Definition: Time.h:13
unsigned char dataFormatVers_
Definition: LmfSource.h:132
int fedId_
Definition: LmfSource.h:77
bool watchFileList_
Definition: LmfSource.h:179
static bool inRange(int)
HLT enums.
std::vector< IndexRecord > indexTable_
Definition: LmfSource.h:159
bool orderedRead_
Definition: LmfSource.h:174
void readIndexTable()
Definition: LmfSource.cc:462
std::ifstream fileList_
Definition: LmfSource.h:193
bool nextEventWithinFile()
Definition: LmfSource.cc:176
std::string currentFileName_
Definition: LmfSource.h:191
uint64_t timeStamp_
Definition: LmfSource.h:125
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
uint32_t eventNum_
Definition: LmfSource.h:129
bool readFileHeader()
Definition: LmfSource.cc:56
bool readEvent(bool doSkip=false)
Definition: LmfSource.cc:200
std::vector< uint32_t > fileHeader_
Definition: LmfSource.h:87
std::string toString(edm::TimeValue_t &t) const
Definition: LmfSource.cc:430
def move(src, dest)
Definition: eostools.py:511
std::vector< std::string > fileNames_
Definition: LmfSource.h:60
static const unsigned char maxDataFormatVersion_
Definition: LmfSource.h:95