CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
20 // system include files
21 #include <memory>
22 
23 // user include files
31 
36 
38 
39 #include <fstream>
40 #include <iostream>
41 #include <sstream>
42 #include <string>
43 #include <iomanip>
44 #include <boost/algorithm/string.hpp>
45 
50 //#include "EventFilter/L1TRawToDigi/interface/PackingSetup.h"
51 //
52 // class declaration
53 //
54 
55 namespace l1t {
56 
58 public:
59  explicit MP7BufferDumpToRaw(const edm::ParameterSet&);
61 
62  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
63 
64 
65 private:
66  virtual void beginJob() override;
67  virtual void produce(edm::Event&, const edm::EventSetup&) override;
68  virtual void endJob() override;
69 
70  std::vector<Block> getBlocks(int iAmc);
71 
72  void formatAMC(amc13::Packet& amc13, const std::vector<Block>& blocks, int iAmc);
73 
74  void formatRaw(edm::Event& iEvent, amc13::Packet& amc13, FEDRawData& fed_data);
75 
76  //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
77  //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
78  //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
79  //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
80 
81  // ----------member data ---------------------------
82 
83  // file readers
86  std::vector<unsigned> rxIndex_;
87  std::vector<unsigned> txIndex_;
88 
89  // packet readers
92  // MP7PacketReader::PacketData::const_iter rxItr_;
93  // MP7PacketReader::PacketData txItr_;
94 
95  // formatting parameters
97 
98  // non packetised data parameters
99  unsigned nFramesPerEvent_;
100 
101  // packetised data parameters
102 
103  // hardware params
104  unsigned nBoard_;
105  unsigned iBoard_;
106  std::vector<int> boardId_;
107 
108  // board readout params
109  std::vector< std::vector<int> > rxBlockLength_;
110  std::vector< std::vector<int> > txBlockLength_;
111  bool mux_;
113 
114  // DAQ params
115  int fedId_;
116  int evType_;
117  int fwVer_;
118  int slinkHeaderSize_; // in 8-bit words
120 
121 };
122 
123 //
124 // constants, enums and typedefs
125 //
126 
127 //
128 // static data member definitions
129 //
130 
131 //
132 // constructors and destructor
133 //
135  rxFileReader_(iConfig.getUntrackedParameter<std::string>("rxFile", "rx_summary.txt")),
136  txFileReader_(iConfig.getUntrackedParameter<std::string>("txFile", "tx_summary.txt")),
137  rxPacketReader_(iConfig.getUntrackedParameter<std::string>("rxFile", "rx_summary.txt")),
138  txPacketReader_(iConfig.getUntrackedParameter<std::string>("txFile", "tx_summary.txt")),
139  packetisedData_(iConfig.getUntrackedParameter<bool>("packetisedData", true)),
140  nFramesPerEvent_(iConfig.getUntrackedParameter<int>("nFramesPerEvent", 6)),
141  iBoard_(iConfig.getUntrackedParameter<int>("boardOffset", 0)),
142  boardId_(iConfig.getUntrackedParameter<std::vector<int> >("boardId")),
143  mux_(iConfig.getUntrackedParameter<bool>("mux", false)),
144  fedId_(iConfig.getUntrackedParameter<int>("fedId", 1)),
145  evType_(iConfig.getUntrackedParameter<int>("eventType", 1)),
146  fwVer_(iConfig.getUntrackedParameter<int>("fwVersion", 1)),
147  slinkHeaderSize_(iConfig.getUntrackedParameter<int>("lenSlinkHeader", 8)),
148  slinkTrailerSize_(iConfig.getUntrackedParameter<int>("lenSlinkTrailer", 8))
149 {
150 
151  produces<FEDRawDataCollection>();
152 
153  // check tx/rx file size consistency and number of boards
154  if (rxFileReader_.size() != txFileReader_.size()) {
155  edm::LogError("L1T") << "Different number of boards in Rx and Tx files";
156  }
158  LogDebug("L1T") << "# boards : " << nBoard_;
159 
160  // advance pointers for non packetised data
161  rxIndex_ = iConfig.getUntrackedParameter< std::vector<unsigned> >("nFramesOffset");
162  if (rxIndex_.size() != nBoard_) {
163  edm::LogError("L1T") << "Wrong number of boards in nFramesOffset " << rxIndex_.size();
164  }
165 
166  txIndex_ = iConfig.getUntrackedParameter< std::vector<unsigned> >("nFramesLatency");
167  if (txIndex_.size() != nBoard_) {
168  edm::LogError("L1T") << "Wrong number of boards in nFramesLatency " << txIndex_.size();
169  }
170 
171  // add latency to offset for Tx
172  for (unsigned i=0; i<rxIndex_.size(); ++i) txIndex_.at(i) += rxIndex_.at(i);
173 
174  // check board IDs
175  if (nBoard_ != boardId_.size()) {
176  edm::LogError("L1T") << "Found " << nBoard_ << " boards, but given " << boardId_.size() << " IDs";
177  }
178 
179 
180  // block length PSet
181  std::vector<edm::ParameterSet> vpset = iConfig.getUntrackedParameter< std::vector<edm::ParameterSet> >("blocks");
182 
183  if (vpset.size() != nBoard_) {
184  edm::LogError("L1T") << "Wrong number of block specs " << vpset.size();
185  }
186 
187  for (unsigned i=0; i<nBoard_; ++i) {
188  rxBlockLength_.push_back(vpset.at(i).getUntrackedParameter< std::vector<int> >("rxBlockLength") );
189  txBlockLength_.push_back(vpset.at(i).getUntrackedParameter< std::vector<int> >("rxBlockLength") );
190  }
191 
192 }
193 
194 
196 {
197 
198  // do anything here that needs to be done at desctruction time
199  // (e.g. close files, deallocate resources etc.)
200 
201 }
202 
203 
204 //
205 // member functions
206 //
207 
208 // ------------ method called for each event ------------
209 void
211 {
212  using namespace edm;
213 
214  // AMC 13 packet
215  amc13::Packet amc13;
216 
217  // create AMC formatted data
218  if (mux_) {
219  std::vector<Block> blocks = getBlocks(iBoard_);
220  formatAMC(amc13, blocks, boardId_.at(iBoard_));
221  iBoard_++; //advance to next AMC for next event...
222  iBoard_ = iBoard_ % nBoard_;
223  }
224  else {
225  for (unsigned iBoard=0; iBoard<nBoard_; ++iBoard) {
226  std::vector<Block> blocks = getBlocks(iBoard);
227  formatAMC(amc13, blocks, iBoard);
228  }
229  }
230 
231  LogDebug("L1T") << "AMC13 size " << amc13.size();
232 
233  // prepare the raw data collection
234  std::auto_ptr<FEDRawDataCollection> raw_coll(new FEDRawDataCollection());
235  FEDRawData& fed_data = raw_coll->FEDData(fedId_);
236 
237  formatRaw(iEvent, amc13, fed_data);
238 
239  LogDebug("L1T") << "Packing FED ID " << fedId_ << " size " << fed_data.size();
240 
241  // put the collection in the event
242  iEvent.put(raw_coll);
243 
244 }
245 
246 
247 
248 std::vector<Block>
250 {
251 
252  LogDebug("L1T") << "Getting blocks from board " << iBoard;
253 
254  std::vector<Block> blocks;
255 
256  // Rx blocks first
257  for (unsigned link=0; link<rxBlockLength_.at(iBoard).size(); ++link) {
258 
259  unsigned id = link*2;
260  unsigned size = rxBlockLength_.at(iBoard).at(link);
261 
262  if (size==0) continue;
263 
264  std::vector<uint32_t> data;
265  if (packetisedData_) {
266 
267  const PacketData& p = rxPacketReader_.get(iBoard);
269  for (unsigned i=0; i<rxIndex_.at(iBoard); i++) itr++;
270 
271  LogDebug("L1T") << "Found packet [" << itr->first_ << ", " << itr->last_ << "]";
272  LogDebug("L1T") << "Link " << link << " has " << itr->links_.find(link)->second.size() << " frames";
273 
274  for (unsigned iFrame=0; iFrame<itr->links_.find(link)->second.size(); ++iFrame) {
275  uint64_t d = itr->links_.find(link)->second.at(iFrame);
276  data.push_back(d);
277  }
278  }
279  else {
280  for (unsigned iFrame=rxIndex_.at(iBoard); iFrame<rxIndex_.at(iBoard)+size; ++iFrame) {
281  uint64_t d = rxFileReader_.get(iBoard).link(link).at(iFrame);
282  // LogDebug("L1T") << "Frame " << iFrame << " : " << std::hex << d;
283  if ((d & 0x100000000) > 0) data.push_back( d & 0xffffffff );
284  }
285  rxIndex_.at(iBoard) += nFramesPerEvent_;
286  }
287 
288  LogDebug("L1T") << "AMC " << iBoard << " block " << id << ", size " << data.size();
289 
290  Block block(id, data);
291  blocks.push_back(block);
292 
293  }
294 
295  // then Tx blocks
296  for (unsigned link=0; link<txBlockLength_.at(iBoard).size(); ++link) {
297 
298  unsigned id = (link*2)+1;
299  unsigned size = txBlockLength_.at(iBoard).at(link);
300 
301  if (size==0) continue;
302 
303  std::vector<uint32_t> data;
304  if (packetisedData_) {
305  const PacketData& p = txPacketReader_.get(iBoard);
307  for (unsigned i=0; i<txIndex_.at(iBoard); i++) itr++;
308  for (unsigned iFrame=itr->first_; iFrame<itr->last_; ++iFrame) {
309  uint64_t d = itr->links_.find(link)->second.at(iFrame);
310  data.push_back(d);
311  }
312  }
313  else {
314  for (unsigned iFrame=txIndex_.at(iBoard); iFrame<txIndex_.at(iBoard)+size; ++iFrame) {
315  uint64_t d = txFileReader_.get(iBoard).link(link).at(iFrame);
316  // LogDebug("L1T") << "Frame " << iFrame << " : " << std::hex << d;
317  if ((d & 0x100000000) > 0) data.push_back( d & 0xffffffff );
318  }
319  txIndex_.at(iBoard) += nFramesPerEvent_;
320  }
321 
322  LogDebug("L1T") << "AMC " << iBoard << " block " << id << ", size " << data.size();
323 
324  Block block(id, data);
325 
326  blocks.push_back(block);
327 
328 
329 
330 
331  }
332 
333  LogDebug("L1T") << "AMC " << iBoard << ", read " << blocks.size() << " blocks";
334 
335  return blocks;
336 
337 }
338 
339 
340 void
341 MP7BufferDumpToRaw::formatAMC(amc13::Packet& amc13, const std::vector<Block>& blocks, int iBoard) {
342 
343  LogDebug("L1T") << "Formatting Board " << iBoard;
344 
345  std::vector<uint32_t> load32;
346  // TODO this is an empty word to be replaced with a proper MP7
347  // header containing at least the firmware version
348  load32.push_back(0);
349  for (const auto& block: blocks) {
350  LogDebug("L1T") << "Adding block " << block.header().getID() << " with size " << block.payload().size();
351  auto load = block.payload();
352 
353 #ifdef EDM_ML_DEBUG
354  std::stringstream s("");
355  s << "Block content:" << std::endl << std::hex << std::setfill('0');
356  for (const auto& word: load)
357  s << std::setw(8) << word << std::endl;
358  LogDebug("L1T") << s.str();
359 #endif
360 
361  load32.push_back(block.header().raw());
362  load32.insert(load32.end(), load.begin(), load.end());
363  }
364 
365  LogDebug("L1T") << "Converting payload " << iBoard;
366 
367  std::vector<uint64_t> load64;
368  for (unsigned int i = 0; i < load32.size(); i += 2) {
369  uint64_t word = load32[i];
370  if (i + 1 < load32.size())
371  word |= static_cast<uint64_t>(load32[i + 1]) << 32;
372  load64.push_back(word);
373  }
374 
375  LogDebug("L1T") << "Creating AMC packet " << iBoard;
376 
377  amc13.add(iBoard, boardId_.at(iBoard), load64);
378 
379 }
380 
381 
382 
383 void
385 {
386 
387  unsigned int size = slinkHeaderSize_ + slinkTrailerSize_ + amc13.size() * 8;
388  fed_data.resize(size);
389  unsigned char * payload = fed_data.data();
390  unsigned char * payload_start = payload;
391 
392  auto bxId = iEvent.bunchCrossing();
393  auto evtId = iEvent.id().event();
394 
395  LogDebug("L1T") << "Creating FEDRawData ID " << fedId_ << ", size " << size;
396 
397  FEDHeader header(payload);
398  header.set(payload, evType_, evtId, bxId, fedId_);
399 
400  payload += slinkHeaderSize_;
401 
402  amc13.write(iEvent, payload, size - slinkHeaderSize_ - slinkTrailerSize_);
403 
404  payload += amc13.size() * 8;
405 
406  FEDTrailer trailer(payload);
407  trailer.set(payload, size / 8, evf::compute_crc(payload_start, size), 0, 0);
408 
409 }
410 
411 
412 // ------------ method called once each job just before starting event loop ------------
413 void
415 {
416 
417 
418 
419 }
420 
421 
422 // ------------ method called once each job just after ending the event loop ------------
423 void
425 {
426 
427 
428 }
429 
430 // ------------ method called when starting to processes a run ------------
431 /*
432 void
433 MP7BufferDumpToRaw::beginRun(edm::Run const&, edm::EventSetup const&)
434 {
435 }
436 */
437 
438 // ------------ method called when ending the processing of a run ------------
439 /*
440 void
441 MP7BufferDumpToRaw::endRun(edm::Run const&, edm::EventSetup const&)
442 {
443 }
444 */
445 
446 // ------------ method called when starting to processes a luminosity block ------------
447 /*
448 vvoid
449 MP7BufferDumpToRaw::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
450 {
451 }
452 */
453 
454 // ------------ method called when ending the processing of a luminosity block ------------
455 /*
456 void
457 MP7BufferDumpToRaw::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
458 {
459 }
460 */
461 
462 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
463 void
465  //The following says we do not know what parameters are allowed so do no validation
466  // Please change this to state exactly what you do use, even if it is no parameters
468  desc.setUnknown();
469  descriptions.addDefault(desc);
470 }
471 
472 }
473 
474 using namespace l1t;
475 //define this as a plug-in
#define LogDebug(id)
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
const_iterator begin() const
std::vector< std::vector< int > > rxBlockLength_
int i
Definition: DBlmapReader.cc:9
std::vector< std::vector< int > > txBlockLength_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
unsigned int size() const
Definition: AMCSpec.cc:270
MP7PacketReader txPacketReader_
int bunchCrossing() const
Definition: EventBase.h:66
const FileData & get(size_t k) const
data getter via index
static void set(unsigned char *trailer, int evt_lgth, int crc, int evt_stat, int tts, bool T=false)
Set all fields in the trailer.
Definition: FEDTrailer.cc:42
std::vector< Block > getBlocks(int iAmc)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
virtual void beginJob() override
size_t size() const
number of rawdata objects stored
Definition: MP7FileReader.h:74
const PacketData & get(size_t i)
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
void resize(size_t newsize)
Definition: FEDRawData.cc:32
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
MP7BufferDumpToRaw(const edm::ParameterSet &)
virtual void endJob() override
virtual void produce(edm::Event &, const edm::EventSetup &) override
std::vector< unsigned > rxIndex_
static void set(unsigned char *header, int evt_ty, int lvl1_ID, int bx_ID, int source_ID, int version=0, bool H=false)
Set all fields in the header.
Definition: FEDHeader.cc:40
unsigned short compute_crc(unsigned char *buffer, unsigned int bufSize)
Definition: CRC16.h:67
def load
Definition: svgfig.py:546
bool write(const edm::Event &ev, unsigned char *ptr, unsigned int size) const
Definition: AMCSpec.cc:288
void formatAMC(amc13::Packet &amc13, const std::vector< Block > &blocks, int iAmc)
std::vector< unsigned > txIndex_
unsigned long long uint64_t
Definition: Time.h:15
list blocks
Definition: gather_cfg.py:90
std::vector< Packet >::const_iterator const_iterator
edm::EventID id() const
Definition: EventBase.h:60
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::vector< int > boardId_
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
MP7PacketReader rxPacketReader_
const std::vector< uint64_t > & link(uint32_t i) const
void formatRaw(edm::Event &iEvent, amc13::Packet &amc13, FEDRawData &fed_data)
volatile std::atomic< bool > shutdown_flag false
void add(unsigned int amc_no, unsigned int board, const std::vector< uint64_t > &load)
Definition: AMCSpec.cc:133
tuple size
Write out results.