CMS 3D CMS Logo

MP7BufferDumpToRaw.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: EventFilter/L1TRawToDigi
4 // Class: MP7BufferDumpToRaw
5 //
13 //
14 // Original Author: James Brooke
15 // Created: Tue, 11 Mar 2014 14:55:45 GMT
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
30 
35 
37 
38 #include <fstream>
39 #include <iostream>
40 #include <sstream>
41 #include <string>
42 #include <iomanip>
43 #include <boost/algorithm/string.hpp>
44 
49 //#include "EventFilter/L1TRawToDigi/interface/PackingSetup.h"
50 //
51 // class declaration
52 //
53 
54 namespace l1t {
55 
57  public:
58  explicit MP7BufferDumpToRaw(const edm::ParameterSet&);
59  ~MP7BufferDumpToRaw() override;
60 
61  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
62 
63  private:
64  void produce(edm::Event&, const edm::EventSetup&) override;
65 
66  std::vector<Block> getBlocks(int iAmc);
67 
68  void formatAMC(amc13::Packet& amc13, const std::vector<Block>& blocks, int iAmc);
69 
71 
72  // ----------member data ---------------------------
73 
74  // file readers
77  std::vector<unsigned> rxIndex_;
78  std::vector<unsigned> txIndex_;
79 
80  // packet readers
83  // MP7PacketReader::PacketData::const_iter rxItr_;
84  // MP7PacketReader::PacketData txItr_;
85 
86  // formatting parameters
88 
89  // non packetised data parameters
90  unsigned nFramesPerEvent_;
91 
92  // packetised data parameters
93 
94  // hardware params
95  unsigned nBoard_;
96  unsigned iBoard_;
97  std::vector<int> boardId_;
98 
99  // board readout params
100  std::vector<std::vector<int> > rxBlockLength_;
101  std::vector<std::vector<int> > txBlockLength_;
102  bool mux_;
104 
105  // DAQ params
106  int fedId_;
107  int evType_;
108  int fwVer_;
109  int slinkHeaderSize_; // in 8-bit words
111  };
112 
113  //
114  // constants, enums and typedefs
115  //
116 
117  //
118  // static data member definitions
119  //
120 
121  //
122  // constructors and destructor
123  //
125  : rxFileReader_(iConfig.getUntrackedParameter<std::string>("rxFile", "rx_summary.txt")),
126  txFileReader_(iConfig.getUntrackedParameter<std::string>("txFile", "tx_summary.txt")),
127  rxPacketReader_(iConfig.getUntrackedParameter<std::string>("rxFile", "rx_summary.txt"),
128  iConfig.getUntrackedParameter<int>("rxHeaderFrames", 1),
129  0,
130  iConfig.getUntrackedParameter<int>("rxKeyLink", 0)),
131  txPacketReader_(iConfig.getUntrackedParameter<std::string>("txFile", "tx_summary.txt"),
132  iConfig.getUntrackedParameter<int>("txHeaderFrames", 1),
133  0,
134  iConfig.getUntrackedParameter<int>("txKeyLink", 0)),
135  packetisedData_(iConfig.getUntrackedParameter<bool>("packetisedData", true)),
136  nFramesPerEvent_(iConfig.getUntrackedParameter<int>("nFramesPerEvent", 6)),
137  iBoard_(iConfig.getUntrackedParameter<int>("boardOffset", 0)),
138  boardId_(iConfig.getUntrackedParameter<std::vector<int> >("boardId")),
139  mux_(iConfig.getUntrackedParameter<bool>("mux", false)),
140  fedId_(iConfig.getUntrackedParameter<int>("fedId", 1)),
141  evType_(iConfig.getUntrackedParameter<int>("eventType", 1)),
142  fwVer_(iConfig.getUntrackedParameter<int>("fwVersion", 1)),
143  slinkHeaderSize_(iConfig.getUntrackedParameter<int>("lenSlinkHeader", 8)),
144  slinkTrailerSize_(iConfig.getUntrackedParameter<int>("lenSlinkTrailer", 8)) {
145  produces<FEDRawDataCollection>();
146 
147  // check tx/rx file size consistency and number of boards
148  if (rxFileReader_.size() != txFileReader_.size()) {
149  edm::LogError("L1T") << "Different number of boards in Rx and Tx files";
150  }
152  LogDebug("L1T") << "# boards : " << nBoard_;
153 
154  // advance pointers for non packetised data
155  rxIndex_ = iConfig.getUntrackedParameter<std::vector<unsigned> >("nFramesOffset");
156  if (rxIndex_.size() != nBoard_) {
157  edm::LogError("L1T") << "Wrong number of boards in nFramesOffset " << rxIndex_.size();
158  }
159 
160  txIndex_ = iConfig.getUntrackedParameter<std::vector<unsigned> >("nFramesLatency");
161  if (txIndex_.size() != nBoard_) {
162  edm::LogError("L1T") << "Wrong number of boards in nFramesLatency " << txIndex_.size();
163  }
164 
165  // add latency to offset for Tx
166  for (unsigned i = 0; i < rxIndex_.size(); ++i)
167  txIndex_.at(i) += rxIndex_.at(i);
168 
169  // check board IDs
170  if (nBoard_ != boardId_.size()) {
171  edm::LogError("L1T") << "Found " << nBoard_ << " boards, but given " << boardId_.size() << " IDs";
172  }
173 
174  // block length PSet
175  std::vector<edm::ParameterSet> vpset = iConfig.getUntrackedParameter<std::vector<edm::ParameterSet> >("blocks");
176 
177  if (vpset.size() != nBoard_) {
178  edm::LogError("L1T") << "Wrong number of block specs " << vpset.size();
179  }
180 
181  rxBlockLength_.resize(nBoard_);
182  txBlockLength_.resize(nBoard_);
183 
184  for (unsigned i = 0; i < nBoard_; ++i) {
185  std::vector<int> rx = vpset.at(i).getUntrackedParameter<std::vector<int> >("rxBlockLength");
186 
187  rxBlockLength_.at(i).resize(rx.size());
188 
189  for (unsigned j = 0; j < rx.size(); ++j) {
190  rxBlockLength_.at(i).at(j) = rx.at(j);
191 
192  if (rx.at(j) != 0) {
193  // LogDebug("L1T") << "Block readout : board " << i << " Rx link " << j << " size " << rx.at(j);
194  }
195  }
196 
197  std::vector<int> tx = vpset.at(i).getUntrackedParameter<std::vector<int> >("txBlockLength");
198  txBlockLength_.at(i).resize(tx.size());
199 
200  for (unsigned j = 0; j < tx.size(); ++j) {
201  txBlockLength_.at(i).at(j) = tx.at(j);
202 
203  if (tx.at(j) != 0) {
204  // LogDebug("L1T") << "Block readout : board " << i << " Tx link " << j << " size " << tx.at(j);
205  }
206  }
207  }
208 
209  LogDebug("L1T") << "Board ID size " << boardId_.size();
210 
211  LogDebug("L1T") << "Frames per event " << nFramesPerEvent_;
212  }
213 
215  // do anything here that needs to be done at desctruction time
216  // (e.g. close files, deallocate resources etc.)
217  }
218 
219  //
220  // member functions
221  //
222 
223  // ------------ method called for each event ------------
225  using namespace edm;
226 
227  // AMC 13 packet
229 
230  // create AMC formatted data
231  if (mux_) {
232  std::vector<Block> blocks = getBlocks(iBoard_);
234  } else {
235  for (unsigned iBoard = 0; iBoard < nBoard_; ++iBoard) {
236  std::vector<Block> blocks = getBlocks(iBoard);
237  formatAMC(amc13, blocks, iBoard);
238  }
239  }
240 
241  LogDebug("L1T") << "AMC13 size " << amc13.size();
242 
243  // prepare the raw data collection
244  std::unique_ptr<FEDRawDataCollection> raw_coll(new FEDRawDataCollection());
245  FEDRawData& fed_data = raw_coll->FEDData(fedId_);
246 
247  formatRaw(iEvent, amc13, fed_data);
248 
249  LogDebug("L1T") << "Packing FED ID " << fedId_ << " size " << fed_data.size();
250 
251  // put the collection in the event
252  iEvent.put(std::move(raw_coll));
253 
254  //advance to next AMC for next event, if required...
255  if (mux_) {
256  iBoard_++;
257  iBoard_ = iBoard_ % nBoard_;
258  }
259  }
260 
261  std::vector<Block> MP7BufferDumpToRaw::getBlocks(int iBoard) {
262  LogDebug("L1T") << "Getting blocks from board " << iBoard << ", " << rxBlockLength_.at(iBoard).size()
263  << " Rx links, " << txBlockLength_.at(iBoard).size() << " Tx links";
264 
265  std::vector<Block> blocks;
266 
267  // Rx blocks first
268  for (unsigned link = 0; link < rxBlockLength_.at(iBoard).size(); ++link) {
269  unsigned id = link * 2;
270  unsigned size = rxBlockLength_.at(iBoard).at(link);
271 
272  if (size == 0)
273  continue;
274 
275  std::vector<uint32_t> data;
276  if (packetisedData_) {
277  const PacketData& p = rxPacketReader_.get(iBoard);
278  PacketData::const_iterator itr = p.begin();
279  for (unsigned i = 0; i < rxIndex_.at(iBoard); i++)
280  itr++;
281 
282  LogDebug("L1T") << "Found packet [" << itr->first_ << ", " << itr->last_ << "]";
283  LogDebug("L1T") << "Link " << link << " has " << itr->links_.find(link)->second.size() << " frames";
284 
285  for (unsigned iFrame = 0; iFrame < itr->links_.find(link)->second.size(); ++iFrame) {
286  uint64_t d = itr->links_.find(link)->second.at(iFrame);
287  data.push_back(d);
288  }
289  } else {
290  for (unsigned iFrame = rxIndex_.at(iBoard); iFrame < rxIndex_.at(iBoard) + size; ++iFrame) {
291  uint64_t d = rxFileReader_.get(iBoard).link(link).at(iFrame);
292  LogDebug("L1T") << "Frame " << iFrame << " : " << std::hex << d;
293  if ((d & 0x100000000) > 0)
294  data.push_back(d & 0xffffffff);
295  }
296  }
297 
298  LogDebug("L1T") << "Board " << iBoard << " block " << id << ", size " << data.size();
299 
300  Block block(id, data);
301  blocks.push_back(block);
302  }
303 
304  // then Tx blocks
305  for (unsigned link = 0; link < txBlockLength_.at(iBoard).size(); ++link) {
306  unsigned id = (link * 2) + 1;
307  unsigned size = txBlockLength_.at(iBoard).at(link);
308 
309  if (size == 0)
310  continue;
311 
312  LogDebug("L1T") << "Block " << id << " expecting size " << size;
313 
314  std::vector<uint32_t> data;
315  if (packetisedData_) {
316  const PacketData& p = txPacketReader_.get(iBoard);
317  PacketData::const_iterator itr = p.begin();
318  for (unsigned i = 0; i < txIndex_.at(iBoard); i++)
319  itr++;
320 
321  LogDebug("L1T") << "Found packet [" << itr->first_ << ", " << itr->last_ << "]";
322  LogDebug("L1T") << "Link " << link << " has " << itr->links_.find(link)->second.size() << " frames";
323 
324  for (unsigned iFrame = 0; iFrame < itr->links_.find(link)->second.size(); ++iFrame) {
325  uint64_t d = itr->links_.find(link)->second.at(iFrame);
326  data.push_back(d);
327  }
328 
329  } else {
330  for (unsigned iFrame = txIndex_.at(iBoard); iFrame < txIndex_.at(iBoard) + size; ++iFrame) {
331  uint64_t d = txFileReader_.get(iBoard).link(link).at(iFrame);
332  LogDebug("L1T") << "Frame " << iFrame << " : " << std::hex << d;
333  if ((d & 0x100000000) > 0)
334  data.push_back(d & 0xffffffff);
335  }
336  }
337 
338  LogDebug("L1T") << "Board " << iBoard << " block " << id << ", size " << data.size();
339 
340  Block block(id, data);
341 
342  blocks.push_back(block);
343  }
344 
345  if (packetisedData_) {
346  rxIndex_.at(iBoard)++;
347  txIndex_.at(iBoard)++;
348  } else {
349  rxIndex_.at(iBoard) += nFramesPerEvent_;
350  txIndex_.at(iBoard) += nFramesPerEvent_;
351  }
352 
353  LogDebug("L1T") << "Board " << iBoard << ", read " << blocks.size() << " blocks";
354 
355  return blocks;
356  }
357 
358  void MP7BufferDumpToRaw::formatAMC(amc13::Packet& amc13, const std::vector<Block>& blocks, int iBoard) {
359  LogDebug("L1T") << "Formatting Board " << iBoard;
360 
361  std::vector<uint32_t> load32;
362  // TODO this is an empty word to be replaced with a proper MP7
363  // header containing at least the firmware version
364  load32.push_back(0);
365  load32.push_back(fwVer_);
366 
367  for (const auto& block : blocks) {
368  LogDebug("L1T") << "Adding block " << block.header().getID() << " with size " << block.payload().size();
369  auto load = block.payload();
370 
371 #ifdef EDM_ML_DEBUG
372  std::stringstream s("");
373  s << "Block content:" << std::endl << std::hex << std::setfill('0');
374  for (const auto& word : load)
375  s << std::setw(8) << word << std::endl;
376  LogDebug("L1T") << s.str();
377 #endif
378 
379  load32.push_back(block.header().raw());
380  load32.insert(load32.end(), load.begin(), load.end());
381  }
382 
383  LogDebug("L1T") << "Converting payload " << iBoard;
384 
385  std::vector<uint64_t> load64;
386  for (unsigned int i = 0; i < load32.size(); i += 2) {
387  uint64_t word = load32[i];
388  if (i + 1 < load32.size())
389  word |= static_cast<uint64_t>(load32[i + 1]) << 32;
390  load64.push_back(word);
391  }
392 
393  LogDebug("L1T") << "Creating AMC packet " << iBoard;
394  // LogDebug("L1T") << iBoard << ", " << boardId_.at(iBoard) << ", " << load64.size();
395 
396  amc13.add(iBoard, boardId_.at(iBoard), 0, 0, 0, load64);
397  }
398 
400  unsigned int size = slinkHeaderSize_ + slinkTrailerSize_ + amc13.size() * 8;
401  fed_data.resize(size);
402  unsigned char* payload = fed_data.data();
403  unsigned char* payload_start = payload;
404 
405  auto bxId = iEvent.bunchCrossing();
406  auto evtId = iEvent.id().event();
407 
408  LogDebug("L1T") << "Creating FEDRawData ID " << fedId_ << ", size " << size;
409 
411  header.set(payload, evType_, evtId, bxId, fedId_);
412 
414 
416  payload += amc13.size() * 8;
417 
418  FEDTrailer trailer(payload);
419  trailer.set(payload, size / 8, evf::compute_crc(payload_start, size), 0, 0);
420  }
421 
422  // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
424  //The following says we do not know what parameters are allowed so do no validation
425  // Please change this to state exactly what you do use, even if it is no parameters
427  desc.setUnknown();
428  descriptions.addDefault(desc);
429  }
430 
431 } // namespace l1t
432 
433 using namespace l1t;
434 //define this as a plug-in
size
Write out results.
const std::vector< uint64_t > & link(uint32_t i) const
MP7PacketReader txPacketReader_
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:45
delete x;
Definition: CaloConfig.h:22
Log< level::Error, false > LogError
std::vector< Block > getBlocks(int iAmc)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
T getUntrackedParameter(std::string const &, T const &) const
const PacketData & get(size_t i)
uint64_t word
int iEvent
Definition: GenABIO.cc:224
void addDefault(ParameterSetDescription const &psetDescription)
static void set(unsigned char *trailer, uint32_t lenght, uint16_t crc, uint8_t evt_stat, uint8_t tts, bool moreTrailers=false)
Set all fields in the trailer.
Definition: FEDTrailer.cc:31
void resize(size_t newsize)
Definition: FEDRawData.cc:28
MP7BufferDumpToRaw(const edm::ParameterSet &)
void produce(edm::Event &, const edm::EventSetup &) override
std::vector< unsigned > rxIndex_
unsigned short compute_crc(unsigned char *buffer, unsigned int bufSize)
Definition: CRC16.h:46
std::vector< std::vector< int > > rxBlockLength_
void formatAMC(amc13::Packet &amc13, const std::vector< Block > &blocks, int iAmc)
std::vector< std::vector< int > > txBlockLength_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
d
Definition: ztail.py:151
std::vector< unsigned > txIndex_
unsigned long long uint64_t
Definition: Time.h:13
def load(fileName)
Definition: svgfig.py:547
size_t size() const
number of rawdata objects stored
Definition: MP7FileReader.h:71
std::vector< Packet >::const_iterator const_iterator
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
std::vector< int > boardId_
const FileData & get(size_t k) const
data getter via index
MP7PacketReader rxPacketReader_
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
void formatRaw(edm::Event &iEvent, amc13::Packet &amc13, FEDRawData &fed_data)
def move(src, dest)
Definition: eostools.py:511
#define LogDebug(id)