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,
157  std::vector<int> const& numSources,
158  std::string const& runDir) {
159  std::filesystem::path runDirP(runDir);
160  for (auto& baseDir : baseDirs) {
161  std::filesystem::path baseDirP(baseDir);
162  buPaths_.emplace_back(baseDirP / runDirP);
163  }
164 }
165 
167  assert(!events_.empty());
168  std::unique_ptr<FEDRawDataCollection> rawData(new FEDRawDataCollection);
169  bool tcdsInRange;
170  unsigned char* tcds_pointer = nullptr;
171  edm::Timestamp tstamp = fillFRDCollection(*rawData, tcdsInRange, tcds_pointer);
172 
173  auto const& event = events_[0];
174  if (daqSource_->useL1EventID()) {
175  uint32_t L1EventID = event->event();
178  eventID, daqSource_->processGUID(), tstamp, event->isRealData(), edm::EventAuxiliary::PhysicsTrigger);
179  aux.setProcessHistoryID(daqSource_->processHistoryID());
180  daqSource_->makeEventWrapper(eventPrincipal, aux);
181  } else if (tcds_pointer == nullptr) {
182  uint32_t L1EventID = event->event();
183  throw cms::Exception("DAQSource::read") << "No TCDS FED in event with FEDHeader EID -: " << L1EventID;
184  } else {
185  const FEDHeader fedHeader(tcds_pointer);
186  tcds::Raw_v1 const* tcds = reinterpret_cast<tcds::Raw_v1 const*>(tcds_pointer + FEDHeader::length);
191  event->isRealData(),
192  static_cast<edm::EventAuxiliary::ExperimentType>(fedHeader.triggerType()),
195  !tcdsInRange);
196  aux.setProcessHistoryID(daqSource_->processHistoryID());
197  daqSource_->makeEventWrapper(eventPrincipal, aux);
198  }
199  std::unique_ptr<edm::WrapperBase> edp(new edm::Wrapper<FEDRawDataCollection>(std::move(rawData)));
200  eventPrincipal.put(
201  daqProvenanceHelpers_[0]->branchDescription(), std::move(edp), daqProvenanceHelpers_[0]->dummyProvenance());
202  eventCached_ = false;
203 }
204 
206  bool& tcdsInRange,
207  unsigned char*& tcds_pointer) {
209  timeval stv;
210  gettimeofday(&stv, nullptr);
211  time = stv.tv_sec;
212  time = (time << 32) + stv.tv_usec;
213  edm::Timestamp tstamp(time);
214 
215  tcds_pointer = nullptr;
216  tcdsInRange = false;
217  uint16_t selectedTCDSFed = 0;
218  int selectedTCDSFileIndex = -1;
219  for (size_t index = 0; index < events_.size(); index++) {
220  uint32_t eventSize = events_[index]->eventSize();
221  unsigned char* event = (unsigned char*)events_[index]->payload();
222  while (eventSize > 0) {
223  assert(eventSize >= FEDTrailer::length);
224  eventSize -= FEDTrailer::length;
225  const FEDTrailer fedTrailer(event + eventSize);
226  const uint32_t fedSize = fedTrailer.fragmentLength() << 3; //trailer length counts in 8 bytes
227  assert(eventSize >= fedSize - FEDHeader::length);
228  eventSize -= (fedSize - FEDHeader::length);
229  const FEDHeader fedHeader(event + eventSize);
230  const uint16_t fedId = fedHeader.sourceID();
232  throw cms::Exception("DataModeFRDStriped:::fillFRDCollection") << "Out of range FED ID : " << fedId;
233  } else if (fedId >= MINTCDSuTCAFEDID_ && fedId <= MAXTCDSuTCAFEDID_) {
234  if (!selectedTCDSFed) {
235  selectedTCDSFed = fedId;
236  selectedTCDSFileIndex = index;
237  tcds_pointer = event + eventSize;
239  tcdsInRange = true;
240  }
241  } else if (!testing_)
242  throw cms::Exception("DataModeFRDStriped:::fillFRDCollection")
243  << "Second TCDS FED ID " << fedId << " found in file " << selectedTCDSFileIndex
244  << ". First ID: " << selectedTCDSFed << " in file " << index;
245  }
246  FEDRawData& fedData = rawData.FEDData(fedId);
247  fedData.resize(fedSize);
248  memcpy(fedData.data(), event + eventSize, fedSize);
249  }
250  assert(eventSize == 0);
251  }
252 
253  return tstamp;
254 }
255 
256 std::vector<std::shared_ptr<const edm::DaqProvenanceHelper>>& DataModeFRDStriped::makeDaqProvenanceHelpers() {
257  //set FRD data collection
258  daqProvenanceHelpers_.clear();
259  daqProvenanceHelpers_.emplace_back(std::make_shared<const edm::DaqProvenanceHelper>(
260  edm::TypeID(typeid(FEDRawDataCollection)), "FEDRawDataCollection", "FEDRawDataCollection", "DAQSource"));
261  return daqProvenanceHelpers_;
262 }
263 
264 /* TODO: adapt to multi-fils
265 bool DataModeFRD::nextEventView() {
266  if (eventCached_) return true;
267  event_ = std::make_unique<FRDEventMsgView>(dataBlockAddr_);
268  if (event_->size() > dataBlockMax_) {
269  throw cms::Exception("DAQSource::getNextEvent")
270  << " event id:" << event_->event() << " lumi:" << event_->lumi() << " run:" << event_->run()
271  << " of size:" << event_->size() << " bytes does not fit into a chunk of size:" << dataBlockMax_
272  << " bytes";
273  }
274  return true;
275 }
276 */
277 
279  bool status = true;
280  for (size_t i = 0; i < events_.size(); i++) {
281  uint32_t crc = 0;
282  auto const& event = events_[i];
283  if (event->version() >= 5) {
284  crc = crc32c(crc, (const unsigned char*)event->payload(), event->eventSize());
285  if (crc != event->crc32c()) {
286  std::ostringstream ss;
287  ss << "Found a wrong crc32c checksum at readout index " << i << ": expected 0x" << std::hex << event->crc32c()
288  << " but calculated 0x" << crc << ". ";
289  crcMsg_ += ss.str();
290  status = false;
291  }
292  }
293  }
294  return status;
295 }
296 
298 
299 /*
300  read multiple input files for this model
301 */
302 
303 std::pair<bool, std::vector<std::string>> DataModeFRDStriped::defineAdditionalFiles(std::string const& primaryName,
304  bool fileListMode) const {
305  std::vector<std::string> additionalFiles;
306 
307  if (fileListMode) {
308  //for the unit test
309  additionalFiles.push_back(primaryName + "_1");
310  return std::make_pair(true, additionalFiles);
311  }
312 
313  auto fullpath = std::filesystem::path(primaryName);
314  auto fullname = fullpath.filename();
315 
316  for (size_t i = 1; i < buPaths_.size(); i++) {
317  std::filesystem::path newPath = buPaths_[i] / fullname;
318  additionalFiles.push_back(newPath.generic_string());
319  }
320  return std::make_pair(true, additionalFiles);
321 }
322 
324  blockCompleted_ = false;
325  if (eventCached_)
326  return true;
327  for (unsigned int i = 0; i < events_.size(); i++) {
328  //add last event length to each stripe
329  dataBlockAddrs_[i] += events_[i]->size();
330  }
331  return makeEvents();
332 }
333 
335  events_.clear();
337  for (int i = 0; i < numFiles_; i++) {
339  //must be exact
341  blockCompleted_ = true;
342  return false;
343  } else {
344  if (blockCompleted_)
345  throw cms::Exception("DataModeFRDStriped::makeEvents")
346  << "not all striped blocks were completed at the same time";
347  }
348  if (blockCompleted_)
349  continue;
350  events_.emplace_back(std::make_unique<FRDEventMsgView>(dataBlockAddrs_[i]));
352  throw cms::Exception("DAQSource::getNextEvent")
353  << " event id:" << events_[i]->event() << " lumi:" << events_[i]->lumi() << " run:" << events_[i]->run()
354  << " of size:" << events_[i]->size() << " bytes does not fit into the buffer or has corrupted header";
355  }
356  return !blockCompleted_;
357 }
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
std::string const & processGUID() const
Accessor for global process identifier.
Definition: InputSource.h:226
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
void makeDirectoryEntries(std::vector< std::string > const &baseDirs, std::vector< int > const &numSources, std::string const &runDir) override
std::vector< std::shared_ptr< const edm::DaqProvenanceHelper > > daqProvenanceHelpers_
bool fileListLoopMode()
Definition: DAQSource.h:57
void resize(size_t newsize, size_t wordsize=8)
Definition: FEDRawData.cc:28
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