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