CMS 3D CMS Logo

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