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 std::string& fileExt,
13  const size_t framesPerBX,
14  const size_t tmux,
15  const size_t maxFramesPerFile,
16  const ChannelMap_t& channelSpecs)
17  : fileFormat_(format),
18  boardDataFileID_("CMSSW"),
19  filePathGen_([=](const size_t i) { return path + "_" + std::to_string(i) + "." + fileExt; }),
20  framesPerBX_(framesPerBX),
21  boardTMUX_(tmux),
22  maxFramesPerFile_(maxFramesPerFile),
23  maxEventsPerFile_(maxFramesPerFile_),
24  eventIndex_(0),
25  pendingEvents_(0),
26  channelMap_(channelSpecs) {
27  if (channelMap_.empty())
28  throw std::runtime_error("BoardDataWriter channel map cannnot be empty");
29  if (fileExt != "txt" && fileExt != "txt.gz" && fileExt != "txt.xz")
30  throw std::runtime_error("BoardDataWriter fileExt must be one of txt, txt.gz, txt.xz");
31 
32  for (const auto& [id, value] : channelMap_) {
33  const auto& [spec, indices] = value;
34  for (const auto i : indices)
35  boardData_.add(i);
36 
37  if ((spec.tmux % boardTMUX_) != 0)
38  throw std::runtime_error("BoardDataWriter, link [" + id.interface + ", " + std::to_string(id.channel) +
39  "]: Specified TMUX period, " + std::to_string(spec.tmux) +
40  ", is not a multiple of the board TMUX, " + std::to_string(boardTMUX_));
41 
42  const size_t tmuxRatio(spec.tmux / boardTMUX_);
43  if (indices.size() != tmuxRatio)
44  throw std::runtime_error("BoardDataWriter, link [" + id.interface + ", " + std::to_string(id.channel) +
45  "]: Number of channel indices specified, " + std::to_string(indices.size()) +
46  ", does not match link:board TMUX ratio, " + std::to_string(tmuxRatio));
47 
48  maxEventsPerFile_ = std::min(maxEventsPerFile_,
49  ((maxFramesPerFile_ - spec.offset) / (framesPerBX_ * boardTMUX_)) - (tmuxRatio - 1));
50  }
51 
52  resetBoardData();
53  }
54 
56  const std::string& path,
57  const std::string& fileExt,
58  const size_t framesPerBX,
59  const size_t tmux,
60  const size_t maxFramesPerFile,
61  const std::map<LinkId, std::vector<size_t>>& channelMap,
62  const std::map<std::string, ChannelSpec>& channelSpecs)
64  format, path, fileExt, framesPerBX, tmux, maxFramesPerFile, mergeMaps(channelMap, channelSpecs)) {}
65 
67 
68  void BoardDataWriter::addEvent(const EventData& eventData) {
69  // Check that data is supplied for each channel
70  for (const auto& [id, info] : channelMap_) {
71  if (not eventData.has(id))
72  throw std::runtime_error("Event data for link [" + id.interface + ", " + std::to_string(id.channel) +
73  "] is missing.");
74  }
75 
76  for (const auto& [id, channelData] : eventData) {
77  // Check that each channel was declared to constructor
78  if (channelMap_.count(id) == 0)
79  throw std::runtime_error("Event data for link [" + id.interface + ", " + std::to_string(id.channel) +
80  "] was given to BoardDataWriter, but its structure was not defined");
81 
82  const auto& [spec, indices] = channelMap_.at(id);
83  const size_t chanIndex(indices.at(pendingEvents_ % (spec.tmux / boardTMUX_)));
84 
85  // Check that that expected amount of data has been provided
86  if (channelData.size() > (spec.tmux * framesPerBX_ - spec.interpacketGap))
87  throw std::runtime_error("Event data for link [" + id.interface + ", " + std::to_string(id.channel) +
88  "] (TMUX " + std::to_string(spec.tmux) + ", " + std::to_string(spec.interpacketGap) +
89  " cycles between packets) is too long (" + std::to_string(channelData.size()) +
90  " 64-bit words)");
91 
92  if (channelData.empty())
93  throw std::runtime_error("Event data for link [" + id.interface + ", " + std::to_string(id.channel) +
94  "] is empty");
95 
96  // Copy event data for this channel to board data object
97  boardData_.at(chanIndex).insert(boardData_.at(chanIndex).end(), channelData.begin(), channelData.end());
98 
99  // Override flags for start & end of event
100  BoardData::Channel::iterator it(boardData_.at(chanIndex).end() - 1);
101  it->endOfPacket = true;
102  it -= (channelData.size() - 1);
103  it->startOfPacket = true;
104 
105  // Pad link with non-valid frames
106  boardData_.at(chanIndex).insert(
107  boardData_.at(chanIndex).end(), spec.tmux * framesPerBX_ - channelData.size(), Frame());
108  }
109 
110  eventIndex_++;
111  pendingEvents_++;
112 
114  flush();
115  }
116 
118  if (pendingEvents_ == 0)
119  return;
120 
121  // Pad any channels that aren't full with invalid frames
122  for (auto& x : boardData_)
123  x.second.resize(maxFramesPerFile_);
124 
125  // For each channel: Assert start_of_orbit for first clock cycle that start is asserted
126  for (auto& x : boardData_) {
127  for (auto& frame : x.second) {
128  if (frame.startOfPacket) {
129  frame.startOfOrbit = true;
130  break;
131  }
132  }
133  }
134 
135  // Set ID field for board data files
137 
138  // Write board data object to file
141  fileNames_.push_back(filePath);
142 
143  // Clear board data to be ready for next event
144  resetBoardData();
145  }
146 
148  const std::map<std::string, ChannelSpec>& specMap) {
149  ChannelMap_t channelMap;
150  for (const auto& x : indexMap)
151  channelMap[x.first] = {specMap.at(x.first.interface), x.second};
152  return channelMap;
153  }
154 
156  for (auto& x : boardData_)
157  x.second.clear();
158 
159  for (const auto& [id, value] : channelMap_) {
160  const auto& [spec, indices] = value;
161  for (size_t tmuxIndex = 0; tmuxIndex < indices.size(); tmuxIndex++)
162  boardData_.at(indices.at(tmuxIndex)).resize(tmuxIndex * boardTMUX_ * framesPerBX_ + spec.offset);
163  }
164 
165  pendingEvents_ = 0;
166  }
167 
168 } // 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 std::string &fileExt, const size_t framesPerBX, const size_t tmux, const size_t maxFramesPerFile, const ChannelMap_t &)
void addEvent(const EventData &data)
static std::string to_string(const XMLCh *ch)
void write(const BoardData &, const std::string &filePath, const FileFormat)
Definition: utilities.cc:372
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
const std::string & name() const
Definition: BoardData.cc:15
std::vector< std::string > fileNames_
float x
void setBoardDataFileID(const std::string &)
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:37