CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DTDDUFileReader.cc
Go to the documentation of this file.
1 
10 
14 
19 
22 
23 #include <string>
24 #include <iosfwd>
25 #include <iostream>
26 #include <algorithm>
27 
28 using namespace std;
29 using namespace edm;
30 
31 
33  runNumber(1), eventNumber(1) {
34 
35  const string & filename = pset.getUntrackedParameter<string>("fileName");
36 
37  readFromDMA = pset.getUntrackedParameter<bool>("readFromDMA",true);
38  numberOfHeaderWords = pset.getUntrackedParameter<int>("numberOfHeaderWords",10);
39  skipEvents = pset.getUntrackedParameter<int>("skipEvents",0);
40 
41  inputFile.open(filename.c_str());
42  if( inputFile.fail() ) {
43  throw cms::Exception("InputFileMissing")
44  << "[DTDDUFileReader]: the input file: " << filename <<" is not present";
45  } else {
46  cout << "DTDDUFileReader: DaqSource file '" << filename << "' was succesfully opened" << endl;
47  }
48 
49  uint32_t runNumber_tmp;
50  inputFile.read(dataPointer<uint32_t>( &runNumber_tmp ), 4);
51  runNumber = runNumber_tmp;
52 
53  inputFile.ignore(4*(numberOfHeaderWords-1));
54 
55  if (skipEvents) {
56  cout<<""<<endl;
57  cout<<" Dear user, pleas be patient, "<<skipEvents<<" are being skipped .."<<endl;
58  cout<<""<<endl;
59  }
60 
61 }
62 
63 
65  inputFile.close();
66 }
67 
69  Timestamp& tstamp,
71  data = new FEDRawDataCollection();
72 
73  vector<uint64_t> eventData;
74  size_t estimatedEventDimension = 102400; // dimensione hardcoded
75  eventData.reserve(estimatedEventDimension);
76  uint64_t word = 0;
77 
78  bool haederTag = false;
79  bool dataTag = true;
80  bool headerAlreadyFound = false;
81 
82  int wordCount = 0;
83 
84  // getting the data word by word from the file
85  // do it until you get the DDU trailer
86  while ( !isTrailer(word, dataTag, wordCount) ) {
87  //while ( !isTrailer(word) ) {
88 
89  if (readFromDMA) {
90  int nread;
91  word = dmaUnpack(dataTag,nread);
92  if ( nread<=0 ) {
93  cout<<"[DTDDUFileReader]: ERROR! no more words and failed to get the trailer"<<endl;
94  delete data; data=0;
95  return false;
96  }
97  }
98 
99  else {
100  int nread = inputFile.read(dataPointer<uint64_t>( &word ), dduWordLength);
101  dataTag = false;
102  if ( nread<=0 ) {
103  cout<<"[DTDDUFileReader]: ERROR! failed to get the trailer"<<endl;
104  delete data; data=0;
105  return false;
106  }
107  }
108 
109  // get the DDU header
110  if (!headerAlreadyFound)
111  if ( isHeader(word,dataTag)) {
112  headerAlreadyFound=true;
113  haederTag=true;
114  }
115 
116  // from now on fill the eventData with the ROS data
117  if (haederTag) {
118 
119  if (readFromDMA) {
120  // swapping only the 8 byte words
121  if (dataTag) {
122  swap(word);
123  } // WARNING also the ddu status words have been swapped!
124  // Control the correct interpretation in DDUUnpacker
125  }
126 
127  eventData.push_back(word);
128  wordCount++;
129  }
130 
131  }
132 
133  // FEDTrailer candidate(reinterpret_cast<const unsigned char*>(&word));
134  // cout<<"EventSize: pushed back "<<eventData.size()
135  // <<"; From trailer "<<candidate.lenght()<<endl;
136 
137  // next event reading will start with meaningless trailer+header from DTLocalDAQ
138  // those will be skipped automatically when seeking for the DDU header
139  //if (eventData.size() > estimatedEventDimension) throw 2;
140 
141  // Eventually skipping events
142  if ((int)eventNumber >= skipEvents) {
143 
144  // Setting the Event ID
145  eID = EventID( runNumber, 1U, eventNumber);
146 
147  // eventDataSize = (Number Of Words)* (Word Size)
148  int eventDataSize = eventData.size()*dduWordLength;
149 
150 
151  if ( dduID<770 || dduID > 775 ) {
152  cout<<"[DTDDUFileReader]: ERROR. DDU ID out of range. DDU id="<<dduID<<endl;
153  // The FED ID is always the first in the DT range
155  }
156  FEDRawData& fedRawData = data->FEDData( dduID );
157  fedRawData.resize(eventDataSize);
158 
159  copy(reinterpret_cast<unsigned char*>(&eventData[0]),
160  reinterpret_cast<unsigned char*>(&eventData[0]) + eventDataSize, fedRawData.data());
161 
162  }
163 
164  return true;
165 
166 }
167 
169 
170  twoNibble64* newWorld = reinterpret_cast<twoNibble64*>(&word);
171 
172  uint32_t msBits_tmp = newWorld->msBits;
173  newWorld->msBits = newWorld->lsBits;
174  newWorld->lsBits = msBits_tmp;
175 }
176 
177 
178 uint64_t DTDDUFileReader::dmaUnpack(bool & isData, int & nread) {
179 
180  uint64_t dduWord = 0;
181 
182  uint32_t td[4];
183  // read 4 32-bits word from the file;
184  nread = inputFile.read(dataPointer<uint32_t>( &td[0] ), 4);
185  nread += inputFile.read(dataPointer<uint32_t>( &td[1] ), 4);
186  nread += inputFile.read(dataPointer<uint32_t>( &td[2] ), 4);
187  nread += inputFile.read(dataPointer<uint32_t>( &td[3] ), 4);
188 
189  uint32_t data[2] = {0, 0};
190  // adjust 4 32-bits words into 2 32-bits words
191  data[0] |= td[3] & 0x3ffff;
192  data[0] |= (td[2] << 18 ) & 0xfffc0000;
193  data[1] |= (td[2] >> 14 ) & 0x0f;
194  data[1] |= (td[1] << 4 ) & 0x3ffff0;
195  data[1] |= (td[0] << 22 ) & 0xffc00000;
196 
197  isData = ( td[0] >> 10 ) & 0x01;
198 
199  // push_back to a 64 word
200  dduWord = (uint64_t(data[1]) << 32) | data[0];
201 
202  return dduWord;
203 }
204 
205 bool DTDDUFileReader::isHeader(uint64_t word, bool dataTag) {
206 
207  bool it_is = false;
208  FEDHeader candidate(reinterpret_cast<const unsigned char*>(&word));
209  if ( candidate.check() ) {
210  // if ( candidate.check() && !dataTag) {
211  it_is = true;
212  dduID = candidate.sourceID();
213  eventNumber++;
214  }
215 
216  return it_is;
217 }
218 
219 
220 bool DTDDUFileReader::isTrailer(uint64_t word, bool dataTag, int wordCount) {
221 
222  bool it_is = false;
223  FEDTrailer candidate(reinterpret_cast<const unsigned char*>(&word));
224  if ( candidate.check() ) {
225  // if ( candidate.check() && !dataTag) {
226  //cout<<"[DTDDUFileReader] "<<wordCount<<" - "<<candidate.lenght()<<endl;
227  if ( wordCount == candidate.lenght())
228  it_is = true;
229  }
230 
231  return it_is;
232 }
233 
234 
236 
237  bool retval=false;
238  if ( inputFile.eof() ) retval=true;
239  return retval;
240 
241 }
242 
243 
244 
T getUntrackedParameter(std::string const &, T const &) const
bool check()
Definition: FEDTrailer.cc:66
virtual bool checkEndOfFile()
DTDDUFileReader(const edm::ParameterSet &pset)
Constructor.
RawFile * open(const char *path)
Open file.
Definition: RawFile.cc:27
bool isHeader(uint64_t word, bool dataTag)
check for a 64 bits word to be a DDU header
bool fail()
It is not OK.
Definition: RawFile.cc:78
int read(void *data, size_t nbytes)
Read from file.
Definition: RawFile.cc:82
int sourceID()
Identifier of the FED.
Definition: FEDHeader.cc:30
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
int close()
Close file if necessary.
Definition: RawFile.cc:60
void resize(size_t newsize)
Definition: FEDRawData.cc:33
void swap(uint64_t &word)
swapping the lsBits with the msBits
edm::RunNumber_t runNumber
bool check()
Check that the header is OK.
Definition: FEDHeader.cc:66
edm::EventNumber_t eventNumber
int eof()
Check end of file.
Definition: RawFile.cc:100
unsigned long long uint64_t
Definition: Time.h:15
int lenght()
The length of the event fragment counted in 64-bit words including header and trailer.
Definition: FEDTrailer.cc:19
int ignore(long offset)
Ignore some bytes.
Definition: RawFile.cc:98
virtual int fillRawData(edm::EventID &eID, edm::Timestamp &tstamp, FEDRawDataCollection *&data)
Generate and fill FED raw data for a full event.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:29
tuple filename
Definition: lut2db_cfg.py:20
uint64_t dmaUnpack(bool &isData, int &nread)
pre-unpack the data if read via DMA
tuple cout
Definition: gather_cfg.py:121
bool isTrailer(uint64_t word, bool dataTag, int wordCount)
check for a 64 bits word to be a DDU trailer
static const int dduWordLength
virtual ~DTDDUFileReader()
Destructor.