CMS 3D CMS Logo

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