CMS 3D CMS Logo

L1TRawToDigi.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: EventFilter/L1TRawToDigi
4 // Class: L1TRawToDigi
5 //
13 //
14 // Original Author: Matthias Wolf
15 // Created: Mon, 10 Feb 2014 14:29:40 GMT
16 //
17 //
18 
19 // system include files
20 #include <iostream>
21 #include <iomanip>
22 #include <memory>
23 
24 // user include files
31 
36 
39 
40 #include "PackingSetupFactory.h"
41 
43 
44 namespace l1t {
46  public:
47  explicit L1TRawToDigi(const edm::ParameterSet&);
48  ~L1TRawToDigi();
49 
50  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
51 
52  private:
53  virtual void produce(edm::Event&, const edm::EventSetup&) override;
54 
55  virtual void beginRun(edm::Run const&, edm::EventSetup const&) override {};
56  virtual void endRun(edm::Run const&, edm::EventSetup const&) override {};
57  virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override {};
58  virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override {};
59 
60  // ----------member data ---------------------------
62  std::vector<int> fedIds_;
63  unsigned int minFeds_;
64  unsigned int fwId_;
66 
67  std::unique_ptr<PackingSetup> prov_;
68 
69  // header and trailer sizes in chars
76 
77  bool tmtCheck_;
78 
79  bool ctp7_mode_;
80  bool mtf7_mode_;
81  bool debug_;
82  int warnsa_;
83  int warnsb_;
84  };
85 }
86 
87 std::ostream & operator<<(std::ostream& o, const l1t::BlockHeader& h) {
88  o << "L1T Block Header " << h.getID() << " with size " << h.getSize();
89  return o;
90 };
91 
92 namespace l1t {
94  fedIds_(config.getParameter<std::vector<int>>("FedIds")),
95  minFeds_(config.getParameter<unsigned int>("MinFeds")),
96  fwId_(config.getParameter<unsigned int>("FWId")),
97  fwOverride_(config.getParameter<bool>("FWOverride")),
98  tmtCheck_(config.getParameter<bool>("TMTCheck")),
99  ctp7_mode_(config.getUntrackedParameter<bool>("CTP7")),
100  mtf7_mode_(config.getUntrackedParameter<bool>("MTF7"))
101  {
102  fedData_ = consumes<FEDRawDataCollection>(config.getParameter<edm::InputTag>("InputLabel"));
103 
104  if (ctp7_mode_ and mtf7_mode_) {
105  throw cms::Exception("L1TRawToDigi") << "Can only use one unpacking mode concurrently!";
106  }
107 
109  prov_->registerProducts(*this);
110 
111  slinkHeaderSize_ = config.getUntrackedParameter<int>("lenSlinkHeader");
112  slinkTrailerSize_ = config.getUntrackedParameter<int>("lenSlinkTrailer");
113  amcHeaderSize_ = config.getUntrackedParameter<int>("lenAMCHeader");
114  amcTrailerSize_ = config.getUntrackedParameter<int>("lenAMCTrailer");
115  amc13HeaderSize_ = config.getUntrackedParameter<int>("lenAMC13Header");
116  amc13TrailerSize_ = config.getUntrackedParameter<int>("lenAMC13Trailer");
117 
118  debug_ = config.getUntrackedParameter<bool>("debug");
119  warnsa_ = 0;
120  warnsb_ = 0;
121  }
122 
123 
125  {
126  }
127 
128 
129  //
130  // member functions
131  //
132 
133  // ------------ method called to produce the data ------------
134  void
136  {
137  using namespace edm;
138 
139  std::unique_ptr<UnpackerCollections> coll = prov_->getCollections(event);
140 
142  event.getByToken(fedData_, feds);
143 
144  if (!feds.isValid()) {
145  LogError("L1T") << "Cannot unpack: no FEDRawDataCollection found";
146  return;
147  }
148 
149  unsigned valid_count = 0;
150  for (const auto& fedId: fedIds_) {
151  const FEDRawData& l1tRcd = feds->FEDData(fedId);
152 
153  LogDebug("L1T") << "Found FEDRawDataCollection with ID " << fedId << " and size " << l1tRcd.size();
154 
156  if (l1tRcd.size() > 0) {
157  LogError("L1T") << "Cannot unpack: invalid L1T raw data (size = "
158  << l1tRcd.size() << ") for ID " << fedId << ". Returning empty collections!";
159  } else if (warnsa_ < 5){
160  warnsa_++;
161  LogInfo("L1T") << "During unpacking, encountered empty L1T raw data (size = "
162  << l1tRcd.size() << ") for FED ID " << fedId << ".";
163  }
164  continue;
165  } else {
166  valid_count++;
167  }
168 
169  const unsigned char *data = l1tRcd.data();
170  FEDHeader header(data);
171 
172  if (header.check()) {
173  LogDebug("L1T") << "Found SLink header:"
174  << " Trigger type " << header.triggerType()
175  << " L1 event ID " << header.lvl1ID()
176  << " BX Number " << header.bxID()
177  << " FED source " << header.sourceID()
178  << " FED version " << header.version();
179  } else {
180  LogWarning("L1T") << "Did not find a SLink header!";
181  }
182 
183  FEDTrailer trailer(data + (l1tRcd.size() - slinkTrailerSize_));
184 
185  if (trailer.check()) {
186  LogDebug("L1T") << "Found SLink trailer:"
187  << " Length " << trailer.lenght()
188  << " CRC " << trailer.crc()
189  << " Status " << trailer.evtStatus()
190  << " Throttling bits " << trailer.ttsBits();
191  } else {
192  LogWarning("L1T") << "Did not find a SLink trailer!";
193  }
194 
195  // FIXME Hard-coded firmware version for first 74x MC campaigns.
196  // Will account for differences in the AMC payload, MP7 payload,
197  // and unpacker setup.
198  bool legacy_mc = fwOverride_ && ((fwId_ >> 24) == 0xff);
199 
200  amc13::Packet packet;
201  if (!packet.parse(
202  (const uint64_t*) data,
203  (const uint64_t*) (data + slinkHeaderSize_),
204  (l1tRcd.size() - slinkHeaderSize_ - slinkTrailerSize_) / 8,
205  header.lvl1ID(),
206  header.bxID(),
207  legacy_mc,
208  mtf7_mode_)) {
209  LogError("L1T")
210  << "Could not extract AMC13 Packet.";
211  return;
212  }
213 
214  for (auto& amc: packet.payload()) {
215  if (amc.size() == 0)
216  continue;
217 
218  auto payload64 = amc.data();
219  const uint32_t * start = (const uint32_t*) payload64.get();
220  // Want to have payload size in 32 bit words, but AMC measures
221  // it in 64 bit words → factor 2.
222  const uint32_t * end = start + (amc.size() * 2);
223 
224  std::unique_ptr<Payload> payload;
225  if (ctp7_mode_) {
226  LogDebug("L1T") << "Using CTP7 mode";
227  payload.reset(new CTP7Payload(start, end));
228  } else if (mtf7_mode_) {
229  LogDebug("L1T") << "Using MTF7 mode";
230  payload.reset(new MTF7Payload(start, end));
231  } else {
232  LogDebug("L1T") << "Using MP7 mode";
233  payload.reset(new MP7Payload(start, end, legacy_mc));
234  }
235  unsigned fw = payload->getAlgorithmFWVersion();
236 
237  // Let parameterset value override FW version
238  if (fwOverride_)
239  fw = fwId_;
240 
241  unsigned board = amc.blockHeader().getBoardID();
242  unsigned amc_no = amc.blockHeader().getAMCNumber();
243 
244  auto unpackers = prov_->getUnpackers(fedId, board, amc_no, fw);
245 
246  // getBlock() returns a non-null unique_ptr on success
247  std::unique_ptr<Block> block;
248  while ((block = payload->getBlock()).get()) {
249 
250  // only unpack the Calo Layer 2 MP TMT node if it has processed this BX
251  unsigned tmtId = board - l1t::stage2::layer2::mp::offsetBoardId + 1;
252  unsigned bxId = header.bxID();
253  unsigned unpackTMT = ( !tmtCheck_ || ( ( tmtId - 1 ) == ( ( bxId - 1 + 3 ) % 9 ) ) );
254  unsigned isCaloL2TMT = ( fedId==l1t::stage2::layer2::fedId && ( amc_no != l1t::stage2::layer2::demux::amcSlotNum ) );
255 
256  if( !isCaloL2TMT || unpackTMT ) {
257  if (debug_) {
258  std::cout << ">>> block to unpack <<<" << std::endl
259  << "hdr: " << std::hex << std::setw(8) << std::setfill('0') << block->header().raw() << std::dec
260  << " (ID " << block->header().getID() << ", size " << block->header().getSize()
261  << ", CapID 0x" << std::hex << std::setw(2) << std::setfill('0') << block->header().getCapID()
262  << ")" << std::dec << std::endl;
263  for (const auto& word: block->payload()) {
264  if (debug_)
265  std::cout << "data: " << std::hex << std::setw(8) << std::setfill('0') << word << std::dec << std::endl;
266  }
267  }
268 
269  auto unpacker = unpackers.find(block->header().getID());
270 
271  block->amc(amc.header());
272 
273  if (unpacker == unpackers.end()) {
274  LogDebug("L1T") << "Cannot find an unpacker for"
275  << "\n\tblock: ID " << block->header().getID() << ", size " << block->header().getSize()
276  << "\n\tAMC: # " << amc_no << ", board ID 0x" << std::hex << board << std::dec
277  << "\n\tFED ID " << fedId << ", and FW ID " << fw;
278  // TODO Handle error
279  } else if (!unpacker->second->unpack(*block, coll.get())) {
280  LogDebug("L1T") << "Error unpacking data for block ID "
281  << block->header().getID() << ", AMC # " << amc_no
282  << ", board ID " << board << ", FED ID " << fedId
283  << ", and FW ID " << fw << "!";
284  // TODO Handle error
285  }
286  }
287  }
288  }
289  }
290  if (valid_count < minFeds_){
291  if (warnsb_ < 5){
292  warnsb_++;
293  LogWarning("L1T") << "Unpacked " << valid_count << " non-empty FED IDs but minimum is set to " << minFeds_ << "\n";
294  }
295  }
296  }
297 
298  // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
299  void
302  // These parameters are part of the L1T/HLT interface, avoid changing if possible:
303  desc.add<std::vector<int>>("FedIds", {})->setComment("required parameter: default value is invalid");
304  desc.add<std::string>("Setup", "")->setComment("required parameter: default value is invalid");
305  // These parameters have well defined default values and are not currently
306  // part of the L1T/HLT interface. They can be cleaned up or updated at will:
307  desc.add<unsigned int>("FWId",0)->setComment("Ignored unless FWOverride is true. Calo Stage1: 32 bits: if the first eight bits are 0xff, will read the 74x MC format.\n");
308  desc.add<bool>("FWOverride", false)->setComment("Firmware version should be taken as FWId parameters");
309  desc.add<bool>("TMTCheck", true)->setComment("Flag for turning on/off Calo Layer 2 TMT node check");
310  desc.addUntracked<bool>("CTP7", false);
311  desc.addUntracked<bool>("MTF7", false);
312  desc.add<edm::InputTag>("InputLabel",edm::InputTag("rawDataCollector"));
313  desc.addUntracked<int>("lenSlinkHeader", 8);
314  desc.addUntracked<int>("lenSlinkTrailer", 8);
315  desc.addUntracked<int>("lenAMCHeader", 8);
316  desc.addUntracked<int>("lenAMCTrailer", 0);
317  desc.addUntracked<int>("lenAMC13Header", 8);
318  desc.addUntracked<int>("lenAMC13Trailer", 8);
319  desc.addUntracked<bool>("debug", false)->setComment("turn on verbose output");
320  desc.add<unsigned int>("MinFeds", 0)->setComment("optional parameter: warn if less than MinFeds non-empty FED ids unpacked.");
321  descriptions.add("l1tRawToDigi", desc);
322  }
323 }
324 
325 using namespace l1t;
326 //define this as a plug-in
#define LogDebug(id)
virtual void produce(edm::Event &, const edm::EventSetup &) override
Definition: start.py:1
std::vector< amc::Packet > payload() const
Definition: AMC13Spec.h:80
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
bool check()
Definition: FEDTrailer.cc:64
int version()
Version identifier of the FED data format.
Definition: FEDHeader.cc:32
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
unsigned int getID() const
Definition: Block.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
int evtStatus()
Event fragment status information.
Definition: FEDTrailer.cc:27
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
delete x;
Definition: CaloConfig.h:22
Definition: config.py:1
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
L1TRawToDigi(const edm::ParameterSet &)
Definition: L1TRawToDigi.cc:93
int sourceID()
Identifier of the FED.
Definition: FEDHeader.cc:28
payload
payload postfix for testing
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
std::vector< int > fedIds_
Definition: L1TRawToDigi.cc:62
#define end
Definition: vmac.h:37
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:74
std::unique_ptr< PackingSetup > make(const std::string &) const
int ttsBits()
Current value of the Trigger Throttling System bitsAQ).
Definition: FEDTrailer.cc:32
std::ostream & operator<<(std::ostream &os, const l1t::CaloParamsHelper &p)
bool check()
Check that the header is OK.
Definition: FEDHeader.cc:64
JetCorrectorParametersCollection coll
Definition: classes.h:10
unsigned long long uint64_t
Definition: Time.h:15
int lenght()
The length of the event fragment counted in 64-bit words including header and trailer.
Definition: FEDTrailer.cc:17
bool parse(const uint64_t *start, const uint64_t *data, unsigned int size, unsigned int lv1, unsigned int bx, bool legacy_mc=false, bool mtf7_mode=false)
Definition: AMC13Spec.cc:70
int bxID()
The bunch crossing number.
Definition: FEDHeader.cc:24
virtual void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override
Definition: L1TRawToDigi.cc:57
unsigned int fwId_
Definition: L1TRawToDigi.cc:64
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static const PackingSetupFactory * get()
unsigned int minFeds_
Definition: L1TRawToDigi.cc:63
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::unique_ptr< PackingSetup > prov_
Definition: L1TRawToDigi.cc:67
edm::EDGetTokenT< FEDRawDataCollection > fedData_
Definition: L1TRawToDigi.cc:58
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
unsigned int getSize() const
Definition: Block.h:23
int crc()
Cyclic Redundancy Code of the event fragment including header and trailer.
Definition: FEDTrailer.cc:22
virtual void endRun(edm::Run const &, edm::EventSetup const &) override
Definition: L1TRawToDigi.cc:56
int lvl1ID()
Level-1 event number generated by the TTC system.
Definition: FEDHeader.cc:20
Definition: AMCSpec.h:8
virtual void endLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override
Definition: L1TRawToDigi.cc:58
int triggerType()
Event Trigger type identifier.
Definition: FEDHeader.cc:16
Definition: event.py:1
Definition: Run.h:42
virtual void beginRun(edm::Run const &, edm::EventSetup const &) override
Definition: L1TRawToDigi.cc:55