CMS 3D CMS Logo

DAQSourceModelsFRD.cc
Go to the documentation of this file.
3 
4 #include <iostream>
5 #include <sstream>
6 #include <sys/types.h>
7 #include <sys/file.h>
8 #include <sys/time.h>
9 #include <unistd.h>
10 #include <vector>
11 
13 
17 
19 
24 
26 
31 
33  std::unique_ptr<FEDRawDataCollection> rawData(new FEDRawDataCollection);
34  bool tcdsInRange;
35  unsigned char* tcds_pointer = nullptr;
36  edm::Timestamp tstamp = fillFEDRawDataCollection(*rawData, tcdsInRange, tcds_pointer);
37 
38  if (daqSource_->useL1EventID()) {
39  uint32_t L1EventID = event_->event();
42  eventID, daqSource_->processGUID(), tstamp, event_->isRealData(), edm::EventAuxiliary::PhysicsTrigger);
43  aux.setProcessHistoryID(daqSource_->processHistoryID());
44  daqSource_->makeEventWrapper(eventPrincipal, aux);
45  } else if (tcds_pointer == nullptr) {
46  uint32_t L1EventID = event_->event();
47  throw cms::Exception("DAQSource::read") << "No TCDS FED in event with FEDHeader EID -: " << L1EventID;
48  } else {
49  const FEDHeader fedHeader(tcds_pointer);
50  tcds::Raw_v1 const* tcds = reinterpret_cast<tcds::Raw_v1 const*>(tcds_pointer + FEDHeader::length);
53  daqSource_->eventRunNumber(), //TODO_ eventRunNumber_
54  daqSource_->currentLumiSection(), //currentLumiSection_
55  event_->isRealData(),
56  static_cast<edm::EventAuxiliary::ExperimentType>(fedHeader.triggerType()),
59  !tcdsInRange);
60  aux.setProcessHistoryID(daqSource_->processHistoryID());
61  daqSource_->makeEventWrapper(eventPrincipal, aux);
62  }
63 
64  std::unique_ptr<edm::WrapperBase> edp(new edm::Wrapper<FEDRawDataCollection>(std::move(rawData)));
65  eventPrincipal.put(
66  daqProvenanceHelpers_[0]->branchDescription(), std::move(edp), daqProvenanceHelpers_[0]->dummyProvenance());
67 }
68 
70  bool& tcdsInRange,
71  unsigned char*& tcds_pointer) {
73  timeval stv;
74  gettimeofday(&stv, nullptr);
75  time = stv.tv_sec;
76  time = (time << 32) + stv.tv_usec;
77  edm::Timestamp tstamp(time);
78 
79  uint32_t eventSize = event_->eventSize();
80  unsigned char* event = (unsigned char*)event_->payload();
81  tcds_pointer = nullptr;
82  tcdsInRange = false;
83  uint16_t selectedTCDSFed = 0;
84  while (eventSize > 0) {
85  assert(eventSize >= FEDTrailer::length);
86  eventSize -= FEDTrailer::length;
87  const FEDTrailer fedTrailer(event + eventSize);
88  const uint32_t fedSize = fedTrailer.fragmentLength() << 3; //trailer length counts in 8 bytes
89  assert(eventSize >= fedSize - FEDHeader::length);
90  eventSize -= (fedSize - FEDHeader::length);
91  const FEDHeader fedHeader(event + eventSize);
92  const uint16_t fedId = fedHeader.sourceID();
94  throw cms::Exception("DAQSource::fillFEDRawDataCollection") << "Out of range FED ID : " << fedId;
95  } else if (fedId >= MINTCDSuTCAFEDID_ && fedId <= MAXTCDSuTCAFEDID_) {
96  if (!selectedTCDSFed) {
97  selectedTCDSFed = fedId;
98  tcds_pointer = event + eventSize;
100  tcdsInRange = true;
101  }
102  } else
103  throw cms::Exception("DAQSource::fillFEDRawDataCollection")
104  << "Second TCDS FED ID " << fedId << " found. First ID: " << selectedTCDSFed;
105  }
106  //take event ID from GTPE FED
107  FEDRawData& fedData = rawData.FEDData(fedId);
108  fedData.resize(fedSize);
109  memcpy(fedData.data(), event + eventSize, fedSize);
110  }
111  assert(eventSize == 0);
112 
113  return tstamp;
114 }
115 
116 std::vector<std::shared_ptr<const edm::DaqProvenanceHelper>>& DataModeFRD::makeDaqProvenanceHelpers() {
117  //set FRD data collection
118  daqProvenanceHelpers_.clear();
119  daqProvenanceHelpers_.emplace_back(std::make_shared<const edm::DaqProvenanceHelper>(
120  edm::TypeID(typeid(FEDRawDataCollection)), "FEDRawDataCollection", "FEDRawDataCollection", "DAQSource"));
121  return daqProvenanceHelpers_;
122 }
123 
125  if (eventCached_)
126  return true;
127  event_ = std::make_unique<FRDEventMsgView>(dataBlockAddr_);
128  if (event_->size() > dataBlockMax_) {
129  throw cms::Exception("DAQSource::getNextEvent")
130  << " event id:" << event_->event() << " lumi:" << event_->lumi() << " run:" << event_->run()
131  << " of size:" << event_->size() << " bytes does not fit into a chunk of size:" << dataBlockMax_ << " bytes";
132  }
133  return true;
134 }
135 
137  crc_ = 0;
138  if (event_->version() >= 5) {
139  crc_ = crc32c(crc_, (const unsigned char*)event_->payload(), event_->eventSize());
140  if (crc_ != event_->crc32c())
141  return false;
142  }
143  return true;
144 }
145 
147  std::stringstream ss;
148  ss << "Found a wrong crc32c checksum: expected 0x" << std::hex << event_->crc32c() << " but calculated 0x" << crc_;
149  return ss.str();
150 }
151 
152 /*
153  * FRD Multi Test
154  */
155 
156 void DataModeFRDStriped::makeDirectoryEntries(std::vector<std::string> const& baseDirs, std::string const& runDir) {
157  std::filesystem::path runDirP(runDir);
158  for (auto& baseDir : baseDirs) {
159  std::filesystem::path baseDirP(baseDir);
160  buPaths_.emplace_back(baseDirP / runDirP);
161  }
162 }
163 
165  assert(!events_.empty());
166  std::unique_ptr<FEDRawDataCollection> rawData(new FEDRawDataCollection);
167  bool tcdsInRange;
168  unsigned char* tcds_pointer = nullptr;
169  edm::Timestamp tstamp = fillFRDCollection(*rawData, tcdsInRange, tcds_pointer);
170 
171  auto const& event = events_[0];
172  if (daqSource_->useL1EventID()) {
173  uint32_t L1EventID = event->event();
176  eventID, daqSource_->processGUID(), tstamp, event->isRealData(), edm::EventAuxiliary::PhysicsTrigger);
177  aux.setProcessHistoryID(daqSource_->processHistoryID());
178  daqSource_->makeEventWrapper(eventPrincipal, aux);
179  } else if (tcds_pointer == nullptr) {
180  uint32_t L1EventID = event->event();
181  throw cms::Exception("DAQSource::read") << "No TCDS FED in event with FEDHeader EID -: " << L1EventID;
182  } else {
183  const FEDHeader fedHeader(tcds_pointer);
184  tcds::Raw_v1 const* tcds = reinterpret_cast<tcds::Raw_v1 const*>(tcds_pointer + FEDHeader::length);
189  event->isRealData(),
190  static_cast<edm::EventAuxiliary::ExperimentType>(fedHeader.triggerType()),
193  !tcdsInRange);
194  aux.setProcessHistoryID(daqSource_->processHistoryID());
195  daqSource_->makeEventWrapper(eventPrincipal, aux);
196  }
197  std::unique_ptr<edm::WrapperBase> edp(new edm::Wrapper<FEDRawDataCollection>(std::move(rawData)));
198  eventPrincipal.put(
199  daqProvenanceHelpers_[0]->branchDescription(), std::move(edp), daqProvenanceHelpers_[0]->dummyProvenance());
200  eventCached_ = false;
201 }
202 
204  bool& tcdsInRange,
205  unsigned char*& tcds_pointer) {
207  timeval stv;
208  gettimeofday(&stv, nullptr);
209  time = stv.tv_sec;
210  time = (time << 32) + stv.tv_usec;
211  edm::Timestamp tstamp(time);
212 
213  tcds_pointer = nullptr;
214  tcdsInRange = false;
215  uint16_t selectedTCDSFed = 0;
216  int selectedTCDSFileIndex = -1;
217  for (size_t index = 0; index < events_.size(); index++) {
218  uint32_t eventSize = events_[index]->eventSize();
219  unsigned char* event = (unsigned char*)events_[index]->payload();
220  while (eventSize > 0) {
221  assert(eventSize >= FEDTrailer::length);
222  eventSize -= FEDTrailer::length;
223  const FEDTrailer fedTrailer(event + eventSize);
224  const uint32_t fedSize = fedTrailer.fragmentLength() << 3; //trailer length counts in 8 bytes
225  assert(eventSize >= fedSize - FEDHeader::length);
226  eventSize -= (fedSize - FEDHeader::length);
227  const FEDHeader fedHeader(event + eventSize);
228  const uint16_t fedId = fedHeader.sourceID();
230  throw cms::Exception("DataModeFRDStriped:::fillFRDCollection") << "Out of range FED ID : " << fedId;
231  } else if (fedId >= MINTCDSuTCAFEDID_ && fedId <= MAXTCDSuTCAFEDID_) {
232  if (!selectedTCDSFed) {
233  selectedTCDSFed = fedId;
234  selectedTCDSFileIndex = index;
235  tcds_pointer = event + eventSize;
237  tcdsInRange = true;
238  }
239  } else if (!testing_)
240  throw cms::Exception("DataModeFRDStriped:::fillFRDCollection")
241  << "Second TCDS FED ID " << fedId << " found in file " << selectedTCDSFileIndex
242  << ". First ID: " << selectedTCDSFed << " in file " << index;
243  }
244  FEDRawData& fedData = rawData.FEDData(fedId);
245  fedData.resize(fedSize);
246  memcpy(fedData.data(), event + eventSize, fedSize);
247  }
248  assert(eventSize == 0);
249  }
250 
251  return tstamp;
252 }
253 
254 std::vector<std::shared_ptr<const edm::DaqProvenanceHelper>>& DataModeFRDStriped::makeDaqProvenanceHelpers() {
255  //set FRD data collection
256  daqProvenanceHelpers_.clear();
257  daqProvenanceHelpers_.emplace_back(std::make_shared<const edm::DaqProvenanceHelper>(
258  edm::TypeID(typeid(FEDRawDataCollection)), "FEDRawDataCollection", "FEDRawDataCollection", "DAQSource"));
259  return daqProvenanceHelpers_;
260 }
261 
262 /* TODO: adapt to multi-fils
263 bool DataModeFRD::nextEventView() {
264  if (eventCached_) return true;
265  event_ = std::make_unique<FRDEventMsgView>(dataBlockAddr_);
266  if (event_->size() > dataBlockMax_) {
267  throw cms::Exception("DAQSource::getNextEvent")
268  << " event id:" << event_->event() << " lumi:" << event_->lumi() << " run:" << event_->run()
269  << " of size:" << event_->size() << " bytes does not fit into a chunk of size:" << dataBlockMax_
270  << " bytes";
271  }
272  return true;
273 }
274 */
275 
277  bool status = true;
278  for (size_t i = 0; i < events_.size(); i++) {
279  uint32_t crc = 0;
280  auto const& event = events_[i];
281  if (event->version() >= 5) {
282  crc = crc32c(crc, (const unsigned char*)event->payload(), event->eventSize());
283  if (crc != event->crc32c()) {
284  std::ostringstream ss;
285  ss << "Found a wrong crc32c checksum at readout index " << i << ": expected 0x" << std::hex << event->crc32c()
286  << " but calculated 0x" << crc << ". ";
287  crcMsg_ += ss.str();
288  status = false;
289  }
290  }
291  }
292  return status;
293 }
294 
296 
297 /*
298  read multiple input files for this model
299 */
300 
301 std::pair<bool, std::vector<std::string>> DataModeFRDStriped::defineAdditionalFiles(std::string const& primaryName,
302  bool fileListMode) const {
303  std::vector<std::string> additionalFiles;
304 
305  if (fileListMode) {
306  //for the unit test
307  additionalFiles.push_back(primaryName + "_1");
308  return std::make_pair(true, additionalFiles);
309  }
310 
311  auto fullpath = std::filesystem::path(primaryName);
312  auto fullname = fullpath.filename();
313 
314  for (size_t i = 1; i < buPaths_.size(); i++) {
315  std::filesystem::path newPath = buPaths_[i] / fullname;
316  additionalFiles.push_back(newPath.generic_string());
317  }
318  return std::make_pair(true, additionalFiles);
319 }
320 
322  blockCompleted_ = false;
323  if (eventCached_)
324  return true;
325  for (unsigned int i = 0; i < events_.size(); i++) {
326  //add last event length to each stripe
327  dataBlockAddrs_[i] += events_[i]->size();
328  }
329  return makeEvents();
330 }
331 
333  events_.clear();
335  for (int i = 0; i < numFiles_; i++) {
337  //must be exact
339  blockCompleted_ = true;
340  return false;
341  } else {
342  if (blockCompleted_)
343  throw cms::Exception("DataModeFRDStriped::makeEvents")
344  << "not all striped blocks were completed at the same time";
345  }
346  if (blockCompleted_)
347  continue;
348  events_.emplace_back(std::make_unique<FRDEventMsgView>(dataBlockAddrs_[i]));
350  throw cms::Exception("DAQSource::getNextEvent")
351  << " event id:" << events_[i]->event() << " lumi:" << events_[i]->lumi() << " run:" << events_[i]->run()
352  << " of size:" << events_[i]->size() << " bytes does not fit into the buffer or has corrupted header";
353  }
354  return !blockCompleted_;
355 }
size
Write out results.
uint8_t triggerType() const
Event Trigger type identifier.
Definition: FEDHeader.cc:13
std::string getChecksumError() const override
std::string getChecksumError() const override
std::pair< bool, std::vector< std::string > > defineAdditionalFiles(std::string const &primaryName, bool fileListMode) const override
bool checksumValid() override
bool checksumValid() override
edm::Timestamp fillFRDCollection(FEDRawDataCollection &rawData, bool &tcdsInRange, unsigned char *&tcds_pointer)
static const uint32_t length
Definition: FEDTrailer.h:57
static const uint32_t length
Definition: FEDHeader.h:54
uint16_t sourceID() const
Identifier of the FED.
Definition: FEDHeader.cc:19
edm::Timestamp fillFEDRawDataCollection(FEDRawDataCollection &rawData, bool &tcdsInRange, unsigned char *&tcds_pointer)
std::vector< std::unique_ptr< FRDEventMsgView > > events_
std::vector< std::shared_ptr< const edm::DaqProvenanceHelper > > daqProvenanceHelpers_
bool nextEventView() override
assert(be >=bs)
void readEvent(edm::EventPrincipal &eventPrincipal) override
std::unique_ptr< FRDEventMsgView > event_
edm::EventAuxiliary makeEventAuxiliary(const tcds::Raw_v1 *, unsigned int runNumber, unsigned int lumiSection, bool isRealData, const edm::EventAuxiliary::ExperimentType &, const std::string &processGUID, bool verifyLumiSection, bool suppressWarning)
uint32_t fragmentLength() const
The length of the event fragment counted in 64-bit words including header and trailer.
Definition: FEDTrailer.cc:13
std::vector< unsigned char * > dataBlockMaxAddrs_
int currentLumiSection() const
Definition: DAQSource.h:52
void put(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, ProductProvenance const &productProvenance) const
Definition: TCDSRaw.h:16
void makeDirectoryEntries(std::vector< std::string > const &baseDirs, std::string const &runDir) override
std::string const & processGUID() const
Accessor for global process identifier.
Definition: InputSource.h:197
void resize(size_t newsize)
Definition: FEDRawData.cc:28
std::vector< std::shared_ptr< const edm::DaqProvenanceHelper > > & makeDaqProvenanceHelpers() override
std::vector< std::filesystem::path > buPaths_
bool useL1EventID() const
Definition: DAQSource.h:51
void makeEventWrapper(edm::EventPrincipal &eventPrincipal, edm::EventAuxiliary &aux)
Definition: DAQSource.h:54
edm::ProcessHistoryID & processHistoryID()
Definition: DAQSource.h:59
unsigned long long TimeValue_t
Definition: Timestamp.h:21
bool nextEventView() override
void readEvent(edm::EventPrincipal &eventPrincipal) override
uint32_t crc32c(uint32_t crc, const unsigned char *buf, size_t len)
Definition: crc32c.cc:340
std::vector< std::shared_ptr< const edm::DaqProvenanceHelper > > daqProvenanceHelpers_
bool fileListLoopMode()
Definition: DAQSource.h:57
unsigned char * dataBlockAddr_
DAQSource * daqSource_
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
uint16_t MINTCDSuTCAFEDID_
uint16_t MAXTCDSuTCAFEDID_
int eventRunNumber() const
Definition: DAQSource.h:53
std::vector< unsigned char * > dataBlockAddrs_
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1
std::vector< std::shared_ptr< const edm::DaqProvenanceHelper > > & makeDaqProvenanceHelpers() override