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 
34 
35 #include <fstream>
36 #include <iostream>
37 #include <sstream>
38 #include <string>
39 #include <iomanip>
40 #include <boost/algorithm/string.hpp>
41 
45 
46 //
47 // class declaration
48 //
49 
50 namespace l1t {
51 
53 public:
54  explicit MP7BufferDumpToRaw(const edm::ParameterSet&);
56 
57  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
58 
59 
60 private:
61  virtual void beginJob() override;
62  virtual void produce(edm::Event&, const edm::EventSetup&) override;
63  virtual void endJob() override;
64 
65  void fillBlocks(int iAmc);
66 
67  void formatRaw(FEDRawData& feddata, int bx, int evtId, int orbit);
68 
69  //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
70  //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
71  //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
72  //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
73 
74  // ----------member data ---------------------------
75 
76  // file readers
79  unsigned rxIndex_;
80  unsigned txIndex_;
81 
82 
83  // packet reader (if needed)
84  // MP7PacketReader rxPacketReader_;
85  // MP7PacketReader txPacketReader_;
86 
87  // formatting parameters
89 
90  // non packetised data parameters
91  unsigned nFramesPerEvent_;
92 
93  // packetised data parameters
94 
95 
96  // DAQ parameters
97  int fedId_;
98  int evType_;
99  int fwVer_;
100  int lenSlinkHeader_; // in 8-bit words
106  std::vector<int> rxBlockLength_;
107  std::vector<int> txBlockLength_;
108 
109 
110  // the data
111  std::vector<Block> blocks_;
112 
113 };
114 
115 //
116 // constants, enums and typedefs
117 //
118 
119 //
120 // static data member definitions
121 //
122 
123 //
124 // constructors and destructor
125 //
127  rxFileReader_(iConfig.getUntrackedParameter<std::string>("rxFile", "rx_summar\
128 y.txt")),
129  txFileReader_(iConfig.getUntrackedParameter<std::string>("txFile", "tx_summar\
130 y.txt")),
131  rxIndex_(0),
132  txIndex_(0),
133  packetisedData_(iConfig.getUntrackedParameter<bool>("packetisedData", true)),
134  nFramesPerEvent_(iConfig.getUntrackedParameter<unsigned>("nFramesPerEvent", 6)),
135  fedId_(iConfig.getUntrackedParameter<int>("fedId", 1)),
136  evType_(iConfig.getUntrackedParameter<int>("eventType", 1)),
137  fwVer_(iConfig.getUntrackedParameter<int>("fwVersion", 1)),
138  lenSlinkHeader_(iConfig.getUntrackedParameter<int>("lenSlinkHeader", 16)),
139  lenSlinkTrailer_(iConfig.getUntrackedParameter<int>("lenSlinkTrailer", 16)),
140  lenAMC13Header_(iConfig.getUntrackedParameter<int>("lenAMC13Header", 0)),
141  lenAMC13Trailer_(iConfig.getUntrackedParameter<int>("lenAMC13Trailer", 0)),
142  lenAMCHeader_(iConfig.getUntrackedParameter<int>("lenAMCHeader", 12)),
143  lenAMCTrailer_(iConfig.getUntrackedParameter<int>("lenAMCTrailer", 8)),
144  rxBlockLength_(iConfig.getUntrackedParameter< std::vector<int> >("rxBlockLength")),
145  txBlockLength_(iConfig.getUntrackedParameter< std::vector<int> >("txBlockLength"))
146 {
147 
148  produces<FEDRawDataCollection>();
149 
150  // advance pointers for non packetised data
151  if (!packetisedData_) {
152  rxIndex_ += iConfig.getUntrackedParameter<int>("nFramesOffset", 0);
153  txIndex_ += rxIndex_;
154  txIndex_ += iConfig.getUntrackedParameter<int>("nFramesLatency", 0);
155  }
156 
157 }
158 
159 
161 {
162 
163  // do anything here that needs to be done at desctruction time
164  // (e.g. close files, deallocate resources etc.)
165 
166 }
167 
168 
169 //
170 // member functions
171 //
172 
173 // ------------ method called for each event ------------
174 void
176 {
177  using namespace edm;
178 
179  // fill the block structure
180  blocks_.clear();
181 
182  int iAmc=0;
183  fillBlocks(iAmc);
184 
185  // create the collection
186  std::auto_ptr<FEDRawDataCollection> rawColl(new FEDRawDataCollection());
187 
188  // retrieve the target buffer
189  FEDRawData& feddata=rawColl->FEDData(fedId_);
190 
191  // fill the RAW data
192  int bx = iEvent.bunchCrossing();
193  int evtId = iEvent.id().event();
194  long int orbit = iEvent.orbitNumber();
195 
196  formatRaw(feddata, bx, evtId, orbit);
197 
198  // put the collection in the event
199  iEvent.put(rawColl);
200 
201 }
202 
203 
204 
205 void
207 {
208 
209  // Rx blocks first
210  for (unsigned i=0; i<rxBlockLength_.size(); ++i) {
211 
212  unsigned id = i*2;
213  unsigned size = rxBlockLength_.at(i);
214 
215  std::vector<uint32_t> data;
216  for (unsigned iFrame=rxIndex_; iFrame<size; ++iFrame) {
217  if (!packetisedData_) {
218  data.push_back( rxFileReader_.get(iAmc).link(i).at(iFrame) );
219  }
220  }
221 
222  Block block(id, data);
223  blocks_.push_back(block);
224 
225  }
226 
227  // then Tx blocks
228  for (unsigned i=0; i<txBlockLength_.size(); ++i) {
229 
230  unsigned id = (i*2)+1;
231  unsigned size = txBlockLength_.at(i);
232 
233  std::vector<uint32_t> data(size);
234  for (unsigned iFrame=txIndex_; iFrame<size; ++iFrame) {
235  if (!packetisedData_) {
236  data.push_back( txFileReader_.get(iAmc).link(i).at(iFrame) );
237  }
238  }
239 
240  Block block(id, data);
241 
242  blocks_.push_back(block);
243 
244  }
245 
246  // advance pointers to next event
247  if (!packetisedData_) {
250  }
251 
252 }
253 
254 
255 void
256 MP7BufferDumpToRaw::formatRaw(FEDRawData& feddata, int bx, int evtId, int orbit)
257 {
258 
259  // now create the raw data array
260  int capEvtSize = 0;
261  for (std::vector<Block>::const_iterator itr=blocks_.begin(); itr!=blocks_.end(); ++itr) {
262  capEvtSize += itr->getSize() + 1;
263  }
264 
265  int amcSize = 0;
266  for (unsigned i=0; i<rxBlockLength_.size(); ++i) amcSize += 4 * (rxBlockLength_.at(i) + 1);
267  for (unsigned i=0; i<txBlockLength_.size(); ++i) amcSize += 4 * (txBlockLength_.at(i) + 1);
268 
269  int fedSize = amcSize;
270  fedSize += lenSlinkHeader_;
271  fedSize += lenSlinkTrailer_;
272  fedSize += lenAMC13Header_;
273  fedSize += lenAMC13Trailer_;
274  fedSize += lenAMCHeader_;
275  fedSize += lenAMCTrailer_;
276  fedSize = (fedSize+7) & ~0x7;// round up to multiple of 8
277 
278  edm::LogInfo("mp7") << "Captured event size=" << capEvtSize << ", AMCsize=" << amcSize << ", FED size=" << fedSize << std::endl;
279 
280  // event info for headers
281  edm::LogInfo("mp7") << "Event : " << evtId << " orbit=" << orbit << " bx=" << bx << std::endl;
282 
283 
284  // Allocate space for header+trailer+payload
285  feddata.resize(fedSize);
286 
287  int iWord = 0;
288 
289  // write SLINK header
290  feddata.data()[iWord+1] |= fedId_ & 0xff;
291  feddata.data()[iWord+2] |= (fedId_>>8) & 0xf;
292 
293  feddata.data()[iWord+2] |= (bx<<4) & 0xff;
294  feddata.data()[iWord+3] |= (bx>>4) & 0xff;
295 
296  feddata.data()[iWord+4] = evtId & 0xff;
297  feddata.data()[iWord+5] = (evtId>>8) & 0xff;
298  feddata.data()[iWord+6] = (evtId>>16) & 0xff;
299 
300  feddata.data()[iWord+7] |= evType_ & 0xf;
301  feddata.data()[iWord+7] |= 0x50;
302 
303 
304  // write AMC13 header
305  iWord += lenSlinkHeader_;
306 
307  // do nothing for now
308 
309  // write AMC header
310  iWord += lenAMC13Header_;
311 
312  feddata.data()[iWord+0] = evtId & 0xff;
313  feddata.data()[iWord+1] = (evtId>>8) & 0xff;
314  feddata.data()[iWord+2] = (evtId>>16) & 0xff;
315 
316  feddata.data()[iWord+4] |= (orbit<<4) & 0xff;
317  feddata.data()[iWord+5] |= (orbit>>4) & 0xff;
318  feddata.data()[iWord+6] |= (orbit>>12) & 0xff;
319 
320  feddata.data()[iWord+6] |= (bx>>4) & 0xff;
321  feddata.data()[iWord+7] |= (bx>>12) & 0xff;
322 
323  feddata.data()[iWord+8] = evType_ & 0xff;
324 
325  feddata.data()[iWord+9] = (amcSize/4) & 0xff;
326  feddata.data()[iWord+10] = ((amcSize/4) >> 8) & 0xff;
327 
328  feddata.data()[iWord+11] = fwVer_ & 0xff;
329 
330 
331  // now add payload
332 
333  iWord += lenAMCHeader_;
334  std::ostringstream payloadInfo;
335 
336  // for (int iBlock=0; iBlock<nRxLinks_ && iWord<fedSize; ++iBlock) {
337 
338  // int blockId = 2*iBlock;
339  // int blockLength = rxBlockLength_.at(iBlock);
340 
341  // write block header
342  // feddata.data()[iWord+2] = blockLength & 0xff;
343  // feddata.data()[iWord+3] = blockId & 0xff;
344  // iWord+=4;
345 
346  // if (blockLength!=0) {
347  // payloadInfo << ", " << (blockId&0xff) << " (" << (blockLength&0xff) << ")";
348  // }
349 
350  // if (blockLength>(int)rxData.at(iBlock).size()) {
351  // edm::LogError("mp7") << "Read insufficient data for block " << blockId <<". Expected " << rxBlockLength_.at(iBlock) << " read " << rxData.at(iBlock).size() << " from Rx link " << iBlock << std::endl;
352  // continue;
353  // }
354 
355  // for (int i=0; i<blockLength; ++i) {
356  // if(i < (int)rxData.at(iBlock).size()) {
357  // feddata.data()[iWord] = rxData.at(iBlock).at(i) & 0xff;
358  // feddata.data()[iWord+1] = (rxData.at(iBlock).at(i) >> 8) & 0xff;
359  // feddata.data()[iWord+2] = (rxData.at(iBlock).at(i) >> 16) & 0xff;
360  // feddata.data()[iWord+3] = (rxData.at(iBlock).at(i) >> 24) & 0xff;
361  // iWord+=4;
362  // }
363  // else {
364  // feddata.data()[iWord] = 0;
365  // feddata.data()[iWord+1] = 0;
366  // feddata.data()[iWord+2] = 0;
367  // feddata.data()[iWord+3] = 0;
368  // iWord+=4;
369  // }
370  // }
371 
372  // }
373 
374  // edm::LogInfo("mp7") << "Rx blocks : " << payloadInfo.str() << std::endl;
375 
376  // now do Tx links
377  // strictly these will appear in the wrong place
378  // they should be interspersed with Rx channels, not appended
379 
380  // payloadInfo.str("");
381 
382  // for (int iBlock=0; iBlock<nTxLinks_ && iWord<fedSize; ++iBlock) {
383 
384  // int blockId = (2*iBlock)+1;
385  // int blockLength = txBlockLength_.at(iBlock);
386 
387  // // write block header
388  // feddata.data()[iWord+2] = blockLength & 0xff;
389  // feddata.data()[iWord+3] = blockId & 0xff;
390  // iWord+=4;
391 
392  // payloadInfo << ", " << (blockId&0xff) << "(" << (blockLength&0xff) << ")";
393 
394  // if (blockLength>(int)txData.at(iBlock).size()) {
395  // edm::LogError("mp7") << "Read insufficient data for block " << blockId <<". Expected " << blockLength << " read " << txData.at(iBlock).size() << " from Tx link " << iBlock << std::endl;
396  // continue;
397  // }
398 
399  // for (int i=0; i<blockLength; ++i) {
400  // if (i<(int)txData.at(iBlock).size()) {
401  // feddata.data()[iWord] = txData.at(iBlock).at(i) & 0xff;
402  // feddata.data()[iWord+1] = (txData.at(iBlock).at(i) >> 8) & 0xff;
403  // feddata.data()[iWord+2] = (txData.at(iBlock).at(i) >> 16) & 0xff;
404  // feddata.data()[iWord+3] = (txData.at(iBlock).at(i) >> 24) & 0xff;
405  // iWord+=4;
406  // }
407  // else {
408  // feddata.data()[iWord] = 0;
409  // feddata.data()[iWord+1] = 0;
410  // feddata.data()[iWord+2] = 0;
411  // feddata.data()[iWord+3] = 0;
412  // iWord+=4;
413  // }
414  // }
415 
416  // }
417 
418  // edm::LogInfo("mp7") << "Tx blocks : " << payloadInfo.str() << std::endl;
419 
420  // write AMC trailer
421  feddata.data()[iWord] = evtId & 0xff;
422  feddata.data()[iWord+1] = (evtId>>8) & 0xff;
423 
424  // write AMC13 trailer
425  iWord += lenAMCTrailer_;
426 
427  // write SLINK trailer
428  iWord += lenAMC13Trailer_;
429  iWord = (iWord+7) & ~0x7; // move to next 64 bit boundary
430  feddata.data()[iWord+4] = (fedSize/8) & 0xff;
431  feddata.data()[iWord+5] = ((fedSize/8)>>8) & 0xff;
432  feddata.data()[iWord+6] = ((fedSize/8)>>16) & 0xff;
433 
434  feddata.data()[iWord+7] |= 0xa0;
435 
436  edm::LogInfo("mp7") << "End of packet after " << iWord+8 << " bytes of " << fedSize << std::endl;
437 
438 
439 }
440 
441 
442 // ------------ method called once each job just before starting event loop ------------
443 void
445 {
446 
447 
448 
449 }
450 
451 
452 // ------------ method called once each job just after ending the event loop ------------
453 void
455 {
456 
457 
458 }
459 
460 // ------------ method called when starting to processes a run ------------
461 /*
462 void
463 MP7BufferDumpToRaw::beginRun(edm::Run const&, edm::EventSetup const&)
464 {
465 }
466 */
467 
468 // ------------ method called when ending the processing of a run ------------
469 /*
470 void
471 MP7BufferDumpToRaw::endRun(edm::Run const&, edm::EventSetup const&)
472 {
473 }
474 */
475 
476 // ------------ method called when starting to processes a luminosity block ------------
477 /*
478 vvoid
479 MP7BufferDumpToRaw::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
480 {
481 }
482 */
483 
484 // ------------ method called when ending the processing of a luminosity block ------------
485 /*
486 void
487 MP7BufferDumpToRaw::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
488 {
489 }
490 */
491 
492 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
493 void
495  //The following says we do not know what parameters are allowed so do no validation
496  // Please change this to state exactly what you do use, even if it is no parameters
498  desc.setUnknown();
499  descriptions.addDefault(desc);
500 }
501 
502 }
503 
504 using namespace l1t;
505 //define this as a plug-in
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
void formatRaw(FEDRawData &feddata, int bx, int evtId, int orbit)
std::vector< int > rxBlockLength_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
int bunchCrossing() const
Definition: EventBase.h:62
const FileData & get(size_t k) const
data getter via index
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
virtual void beginJob() override
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
int orbitNumber() const
Definition: EventBase.h:63
std::vector< Block > blocks_
std::vector< int > txBlockLength_
edm::EventID id() const
Definition: EventBase.h:56
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
const std::vector< uint64_t > & link(uint32_t i) const
tuple size
Write out results.