CMS 3D CMS Logo

BoardDataWriter.cc
Go to the documentation of this file.
2 
3 #include <fstream>
4 
7 
8 namespace l1t::demo {
9 
11  const std::string& path,
12  const size_t framesPerBX,
13  const size_t tmux,
14  const size_t maxFramesPerFile,
15  const ChannelMap_t& channelSpecs)
16  : fileFormat_(format),
17  filePathGen_([=](const size_t i) { return path + "_" + std::to_string(i) + ".txt"; }),
18  framesPerBX_(framesPerBX),
19  boardTMUX_(tmux),
20  maxFramesPerFile_(maxFramesPerFile),
21  maxEventsPerFile_(maxFramesPerFile_),
22  eventIndex_(0),
23  pendingEvents_(0),
24  channelMap_(channelSpecs) {
25  if (channelMap_.empty())
26  throw std::runtime_error("BoardDataWriter channel map cannnot be empty");
27 
28  for (const auto& [id, value] : channelMap_) {
29  const auto& [spec, indices] = value;
30  for (const auto i : indices)
31  boardData_.add(i);
32 
33  if ((spec.tmux % boardTMUX_) != 0)
34  throw std::runtime_error("BoardDataWriter, link [" + id.interface + ", " + std::to_string(id.channel) +
35  "]: Specified TMUX period, " + std::to_string(spec.tmux) +
36  ", is not a multiple of the board TMUX, " + std::to_string(boardTMUX_));
37 
38  const size_t tmuxRatio(spec.tmux / boardTMUX_);
39  if (indices.size() != tmuxRatio)
40  throw std::runtime_error("BoardDataWriter, link [" + id.interface + ", " + std::to_string(id.channel) +
41  "]: Number of channel indices specified, " + std::to_string(indices.size()) +
42  ", does not match link:board TMUX ratio, " + std::to_string(tmuxRatio));
43 
44  maxEventsPerFile_ = std::min(maxEventsPerFile_,
45  ((maxFramesPerFile_ - spec.offset) / (framesPerBX_ * boardTMUX_)) - (tmuxRatio - 1));
46  }
47 
48  resetBoardData();
49  }
50 
52  const std::string& path,
53  const size_t framesPerBX,
54  const size_t tmux,
55  const size_t maxFramesPerFile,
56  const std::map<LinkId, std::vector<size_t>>& channelMap,
57  const std::map<std::string, ChannelSpec>& channelSpecs)
58  : BoardDataWriter(format, path, framesPerBX, tmux, maxFramesPerFile, mergeMaps(channelMap, channelSpecs)) {}
59 
60  void BoardDataWriter::addEvent(const EventData& eventData) {
61  // Check that data is supplied for each channel
62  for (const auto& [id, info] : channelMap_) {
63  if (not eventData.has(id))
64  throw std::runtime_error("Event data for link [" + id.interface + ", " + std::to_string(id.channel) +
65  "] is missing.");
66  }
67 
68  for (const auto& [id, channelData] : eventData) {
69  // Check that each channel was declared to constructor
70  if (channelMap_.count(id) == 0)
71  throw std::runtime_error("Event data for link [" + id.interface + ", " + std::to_string(id.channel) +
72  "] was given to BoardDataWriter, but its structure was not defined");
73 
74  const auto& [spec, indices] = channelMap_.at(id);
75  const size_t chanIndex(indices.at(pendingEvents_ % (spec.tmux / boardTMUX_)));
76 
77  // Check that that expected amount of data has been provided
78  if (channelData.size() > (spec.tmux * framesPerBX_ - spec.interpacketGap))
79  throw std::runtime_error("Event data for link [" + id.interface + ", " + std::to_string(id.channel) +
80  "] (TMUX " + std::to_string(spec.tmux) + ", " + std::to_string(spec.interpacketGap) +
81  " cycles between packets) is too long (" + std::to_string(channelData.size()) +
82  " 64-bit words)");
83 
84  if (channelData.empty())
85  throw std::runtime_error("Event data for link [" + id.interface + ", " + std::to_string(id.channel) +
86  "] is empty");
87 
88  // Copy event data for this channel to board data object
89  boardData_.at(chanIndex).insert(boardData_.at(chanIndex).end(), channelData.begin(), channelData.end());
90 
91  // Override flags for start & end of event
92  BoardData::Channel::iterator it(boardData_.at(chanIndex).end() - 1);
93  it->end = true;
94  it -= (channelData.size() - 1);
95  it->start = true;
96 
97  // Pad link with non-valid frames
98  boardData_.at(chanIndex).insert(
99  boardData_.at(chanIndex).end(), spec.tmux * framesPerBX_ - channelData.size(), Frame());
100  }
101 
102  eventIndex_++;
103  pendingEvents_++;
104 
106  flush();
107  }
108 
110  if (pendingEvents_ == 0)
111  return;
112 
113  // Pad any channels that aren't full with invalid frames
114  for (auto& x : boardData_)
115  x.second.resize(maxFramesPerFile_);
116 
117  // Write board data object to file
120  fileNames_.push_back(filePath);
121 
122  // Clear board data to be ready for next event
123  resetBoardData();
124  }
125 
127  const std::map<std::string, ChannelSpec>& specMap) {
128  ChannelMap_t channelMap;
129  for (const auto& x : indexMap)
130  channelMap[x.first] = {specMap.at(x.first.interface), x.second};
131  return channelMap;
132  }
133 
135  for (auto& x : boardData_)
136  x.second.clear();
137 
138  for (const auto& [id, value] : channelMap_) {
139  const auto& [spec, indices] = value;
140  for (size_t tmuxIndex = 0; tmuxIndex < indices.size(); tmuxIndex++)
141  boardData_.at(indices.at(tmuxIndex)).resize(tmuxIndex * boardTMUX_ * framesPerBX_ + spec.offset);
142  }
143 
144  pendingEvents_ = 0;
145  }
146 
147 } // namespace l1t::demo
std::bitset< TTBV::S_ > Frame
Definition: TTTypes.h:58
static const TGPicture * info(bool iBackgroundIsBlack)
Logical ID for link within any given time slice (e.g. ["tracks", 0] -> ["tracks", 17] for links from ...
Definition: LinkId.h:10
BoardDataWriter(FileFormat, const std::string &filePath, const size_t framesPerBX, const size_t tmux, const size_t maxFramesPerFile, const ChannelMap_t &)
void addEvent(const EventData &data)
std::string to_string(const V &value)
Definition: OMSAccess.h:71
void write(const BoardData &, const std::string &filePath, const FileFormat)
Definition: utilities.cc:270
std::function< std::string(const size_t)> filePathGen_
static ChannelMap_t mergeMaps(const std::map< LinkId, std::vector< size_t >> &, const std::map< std::string, ChannelSpec > &)
Definition: value.py:1
std::map< LinkId, std::pair< ChannelSpec, std::vector< size_t > > > ChannelMap_t
std::vector< std::string > fileNames_
float x
Class representing information phase-2 ATCA I/O data corresponding to a single event, with logical channel IDs (essentially string-uint pairs, e.g. tracks-0 to tracks-17).
Definition: EventData.h:28
bool has(const LinkId &) const
Definition: EventData.cc:23
Channel & at(size_t)
Definition: BoardData.cc:35