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