CMS 3D CMS Logo

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