CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FileReaderDCC.cc
Go to the documentation of this file.
1 #include "FileReaderDCC.h"
2 #include <iostream> // cerr
3 #include <errno.h> // errno
4 #include <string.h> // bzero, memcpy
5 #include <stdlib.h> // exit
6 #include <sys/types.h> // open
7 #include <sys/stat.h> // open
8 #include <fcntl.h> // open
9 #include <unistd.h> // read, close
10 
11 #ifndef O_LARGEFILE //for OSX
12 #define O_LARGEFILE 0
13 #endif
14 
16  if( sizeof(unsigned long long)!=8 || sizeof(unsigned short)!=2 )
17  throw std::runtime_error(std::string("Wrong platform: sizeof(unsigned long long)!=8 || sizeof(unsigned short)!=2"));
18  raw_event = new unsigned short [200000*40];
19  end = (file_buffer_end = file_buffer + sizeof(file_buffer)/sizeof(unsigned long long));
20  bzero(raw_event, sizeof(raw_event) );
21  bzero(file_buffer,sizeof(file_buffer));
22  word_0=0; word_1=0; word_2=0;
23  eventStatus = 0;
26  acceptCriteria = 0x3F; // Everything
27  fd = 0;
28 }
29 
30 FileReaderDCC::~FileReaderDCC(void){ if( fd ) close(fd); }
31 
32 int FileReaderDCC::open(const char *filename) throw (std::runtime_error) {
33  if( fd ) close(fd);
34  fd = ::open(filename,O_RDONLY|O_LARGEFILE);
35  if( fd == -1 ) throw ( std::runtime_error(std::string("Error opening ").append(filename).append(" data file.")) );
36  return fd;
37 }
38 
39 size_t FileReaderDCC::read(const unsigned short* &buf) throw (std::runtime_error) {
40  // Check for ubnormal situation
41  if( end>file_buffer_end || end<file_buffer ) throw ( std::runtime_error("Error of reading") );
42  if( !fd ) throw ( std::runtime_error("Open some file first") );
43 
44  unsigned long long *start = end;
45  unsigned short *event = raw_event;
46 
47  eventStatus = 0;
48  size_t dccWordCount = 0;
49  end = 0;
50 
51  while( !end && dccWordCount<50000*40 ){
52  unsigned long long *dccWord = start;
53  unsigned long long preHeader = 0;
54 
55  // Did we reach end of current buffer and want to read next block?
56  // If it was first time and we don't have file buffer then we won't get inside
57  while( dccWord<file_buffer_end && dccWordCount<50000 ){
58  word_0 = word_1; // delay by 2 DCC words
59  word_1 = word_2; // delay by 1 DCC word
60  word_2 = *dccWord;// current DCC word
61  if( (word_1&0xF0000000000000FFLL)==0x500000000000005FLL && // let's call this a preHeader
62  (word_2&0xFF000000000000FFLL)==0xD900000000000017LL ){ // and this is a header
63  if( eventStatus&Header ){ // Second header
64  word_2 = word_1; // Fall back to get rigth preHeader next time
65  end = dccWord; // Even if we end with preHeader of next evet put it to the end of this event too
66  break;
67  }
68  if( dccWordCount>1 ){ // Extra words between trailer and header
69  if( (word_0&0xFFFFFFFFFFFF0000LL)==0xFFFFFFFFFFFF0000LL ) eventStatus |= FFFF;
70  word_2 = word_1; // Fall back to get rigth preHeader next time
71  end = dccWord;
72  break;
73  }
74  eventStatus |= Header;
75  if( event==raw_event ) preHeader = word_1; // If preHeader not yet in event then put it there
76  start = dccWord;
77  }
78  if( (word_1&0xFF00000000000000LL)==0xEF00000000000000LL &&
79  (word_2&0xFF0000000000000FLL)==0xAF00000000000007LL ){
80  eventStatus |= Trailer;
81  end = ++dccWord;
82  break;
83  }
84  // Increase counters by one DCC word
85  dccWord++;
86  dccWordCount++;
87  }
88 
89  // If have DCC Header
90  if( preHeader ){
91  // Need to account first word of DCC Header
92  memcpy(event,&preHeader,sizeof(preHeader));
93  event += sizeof(preHeader)/sizeof(unsigned short);
94  }
95 
96  // Take care of the rest
97  memcpy(event,start,(dccWord-start)*sizeof(unsigned long long));
98  event += (dccWord-start)*sizeof(unsigned long long)/sizeof(unsigned short);
99 
100  // If reach max length
101  if( dccWordCount==50000*40 ){ end = dccWord; break; }
102 
103  if( !end ){
104  // Need to read next block for the rest of this event
105  ssize_t length = ::read(fd,file_buffer,sizeof(file_buffer));
106  if( length==-1 ) throw ( std::runtime_error("Error of reading") );
107  if( length== 0 ){
108  eventStatus |= EndOfStream;
109  end = (file_buffer_end = file_buffer + sizeof(file_buffer)/sizeof(unsigned long long));
110  break;
111  }
112  file_buffer_end = file_buffer + length/sizeof(unsigned long long);
113 
114  // Will start from the beginning of new buffer next time we read it
115  start = file_buffer;
116  }
117  }
118 
119  if( !end ) eventStatus |= DCCoversize;
120  if( !(eventStatus&Header) && !(eventStatus&Trailer) && !(eventStatus&FFFF) ) eventStatus |= Unknown;
121 
122  buf = (const unsigned short*)raw_event;
123  return (eventStatus&FFFF?event-raw_event-4:event-raw_event);
124 }
125 
126 size_t FileReaderDCC::next(const unsigned short* &buf) throw(std::runtime_error) {
127  size_t size=0;
128  do {
129  if( (size = read(buf)) == 0 ) break;
130  } while( rejectCriteria&eventStatus || !(acceptCriteria&eventStatus) || (selectCriteria?selectCriteria!=eventStatus:0) );
131  return size;
132 }
size_t read(const unsigned short *&buf)
int open(const char *filename)
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
virtual ~FileReaderDCC(void)
unsigned long long word_0
Definition: FileReaderDCC.h:11
unsigned int acceptCriteria
Definition: FileReaderDCC.h:20
unsigned int rejectCriteria
Definition: FileReaderDCC.h:20
#define O_LARGEFILE
unsigned long long * file_buffer_end
Definition: FileReaderDCC.h:14
unsigned long long word_1
Definition: FileReaderDCC.h:11
unsigned long long file_buffer[4000]
Definition: FileReaderDCC.h:12
size_t next(const unsigned short *&buf)
unsigned long long word_2
Definition: FileReaderDCC.h:11
#define end
Definition: vmac.h:38
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
unsigned long long * end
Definition: FileReaderDCC.h:14
unsigned int eventStatus
Definition: FileReaderDCC.h:20
unsigned int selectCriteria
Definition: FileReaderDCC.h:20
tuple filename
Definition: lut2db_cfg.py:20
unsigned short * raw_event
Definition: FileReaderDCC.h:9
tuple size
Write out results.