CMS 3D CMS Logo

OmtfUnpacker.cc
Go to the documentation of this file.
1 // system include files
2 #include <iostream>
3 #include <iomanip>
4 #include <sstream>
5 #include <bitset>
6 #include <string>
7 
8 // user include files
14 
18 
24 
27 
34 
40 
45 
49 
50 namespace omtf {
51 
53  public:
55 
56  ~OmtfUnpacker() override {}
57 
58  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
59 
60  void produce(edm::Event& ev, const edm::EventSetup& es) override;
61 
62  void beginRun(const edm::Run& run, const edm::EventSetup& es) override;
63 
64  private:
65  unsigned long theEventCounter;
66 
68 
71 
76 
79 
81  };
82 
84  theOutputTag = pset.getParameter<std::string>("outputTag");
85 
86  produces<RPCDigiCollection>(theOutputTag);
87  produces<CSCCorrelatedLCTDigiCollection>(theOutputTag);
88  produces<l1t::RegionalMuonCandBxCollection>(theOutputTag);
89  produces<L1MuDTChambPhContainer>(theOutputTag);
90  produces<L1MuDTChambThContainer>(theOutputTag);
91 
92  theSkipDt = pset.getParameter<bool>("skipDt");
93  theSkipRpc = pset.getParameter<bool>("skipRpc");
94  theSkipCsc = pset.getParameter<bool>("skipCsc");
95  theSkipMuon = pset.getParameter<bool>("skipMuon");
96 
97  theFedDataToken = consumes<FEDRawDataCollection>(pset.getParameter<edm::InputTag>("inputLabel"));
98 
99  if (!theSkipRpc) {
100  theRPCEMapToken = esConsumes<edm::Transition::BeginRun>();
101  if (not theConfig.getParameter<bool>("useRpcConnectionFile")) {
102  theAmcMappingToken = esConsumes<edm::Transition::BeginRun>();
103  }
104  }
105  }
106 
109  desc.add<edm::InputTag>("inputLabel", edm::InputTag("rawDataCollector"));
110  desc.add<bool>("skipRpc", false);
111  desc.add<bool>("skipCsc", false);
112  desc.add<bool>("skipDt", false);
113  desc.add<bool>("skipMuon", false);
114  desc.add<bool>("useRpcConnectionFile", false);
115  desc.add<std::string>("rpcConnectionFile", "");
116  desc.add<std::string>("outputTag", "");
117  descriptions.add("omtfUnpacker", desc);
118  }
119 
121  //
122  // rpc unpacker
123  //
124  if (!theSkipRpc) {
126  if (theConfig.getParameter<bool>("useRpcConnectionFile")) {
127  theRpcUnpacker.init(*readoutMapping,
128  edm::FileInPath(theConfig.getParameter<std::string>("rpcConnectionFile")).fullPath());
129  } else {
130  auto const& amcMapping = es.getData(theAmcMappingToken);
131  theRpcUnpacker.init(*readoutMapping, amcMapping);
132  }
133  }
134 
135  //
136  // csc unpacker
137  //
138  if (!theSkipCsc)
140  }
141 
144  theEventCounter++;
145  if (debug)
146  LogDebug("OmtfUnpacker::produce") << "Beginning To Unpack Event: " << theEventCounter;
147 
148  edm::Handle<FEDRawDataCollection> allFEDRawData;
149  event.getByToken(theFedDataToken, allFEDRawData);
150 
151  auto producedRPCDigis = std::make_unique<RPCDigiCollection>();
152  auto producedCscLctDigis = std::make_unique<CSCCorrelatedLCTDigiCollection>();
153  auto producedMuonDigis = std::make_unique<l1t::RegionalMuonCandBxCollection>();
154  producedMuonDigis->setBXRange(-3, 4);
155  auto producedDTPhDigis = std::make_unique<L1MuDTChambPhContainer>();
156  auto producedDTThDigis = std::make_unique<L1MuDTChambThContainer>();
157  std::vector<L1MuDTChambPhDigi> phi_Container;
158  std::vector<L1MuDTChambThDigi> the_Container;
159 
160  for (int fedId = 1380; fedId <= 1381; ++fedId) {
161  const FEDRawData& rawData = allFEDRawData->FEDData(fedId);
162  unsigned int nWords = rawData.size() / sizeof(Word64);
163  LogTrace("") << "FED : " << fedId << " words: " << nWords;
164  if (nWords == 0)
165  continue;
166 
167  //
168  // FED header
169  //
170  const Word64* header = reinterpret_cast<const Word64*>(rawData.data());
171  FEDHeader fedHeader(reinterpret_cast<const unsigned char*>(header));
172  if (!fedHeader.check()) {
173  LogTrace("") << " ** PROBLEM **, header.check() failed, break";
174  break;
175  }
176  if (fedHeader.sourceID() != fedId) {
177  LogTrace("") << " ** PROBLEM **, fedHeader.sourceID() != fedId"
178  << "fedId = " << fedId << " sourceID=" << fedHeader.sourceID();
179  }
180  int triggerBX = fedHeader.bxID();
181  if (debug) {
182  std::ostringstream str;
183  str << " header: " << *reinterpret_cast<const std::bitset<64>*>(header) << std::endl;
184  str << " header triggerType: " << fedHeader.triggerType() << std::endl;
185  str << " header lvl1ID: " << fedHeader.lvl1ID() << std::endl;
186  str << " header bxID: " << fedHeader.bxID() << std::endl;
187  str << " header sourceID: " << fedHeader.sourceID() << std::endl;
188  str << " header version: " << fedHeader.version() << std::endl;
189  str << " header more : " << fedHeader.moreHeaders() << std::endl;
190  str << " triggerBx " << triggerBX << std::endl;
191  LogTrace("") << str.str();
192  }
193 
194  //
195  // FED trailer
196  //
197  const Word64* trailer = reinterpret_cast<const Word64*>(rawData.data()) + (nWords - 1);
198  trailer++;
199  bool moreTrailers = true;
200  while (moreTrailers) {
201  trailer--;
202  FEDTrailer fedTrailer(reinterpret_cast<const unsigned char*>(trailer));
203  if (!fedTrailer.check()) {
204  if (debug)
205  LogTrace("") << " ** PROBLEM **, trailer.check() failed, break";
206  break;
207  }
208  if (fedTrailer.fragmentLength() != nWords) {
209  if (debug)
210  LogTrace("") << " ** PROBLEM **, fedTrailer.fragmentLength()!= nWords, break";
211  break;
212  }
213  moreTrailers = fedTrailer.moreTrailers();
214  if (debug) {
215  std::ostringstream str;
216  str << " trailer: " << *reinterpret_cast<const std::bitset<64>*>(trailer) << std::endl;
217  str << " trailer lenght: " << fedTrailer.fragmentLength() << std::endl;
218  str << " trailer crc: " << fedTrailer.crc() << std::endl;
219  str << " trailer evtStatus: " << fedTrailer.evtStatus() << std::endl;
220  str << " trailer ttsBits: " << fedTrailer.ttsBits() << std::endl;
221  LogTrace("") << str.str();
222  }
223  }
224 
225  //
226  // dump all FED data for debug
227  //
228  if (debug) {
229  std::ostringstream str;
230  for (const Word64* word = header + 1; word != trailer; word++) {
231  str << " data: " << *reinterpret_cast<const std::bitset<64>*>(word) << std::endl;
232  }
233  LogTrace("") << str.str();
234  }
235 
236  //
237  // AMC13 header
238  //
239  const Word64* headerAmc13raw = header + 1;
240  amc13::Header headerAmc13(headerAmc13raw);
241  if (debug) {
242  std::ostringstream str;
243  str << " headerAMC13: " << *reinterpret_cast<const std::bitset<64>*>(headerAmc13raw) << std::endl;
244  str << " amc13 check: " << headerAmc13.check() << std::endl;
245  str << " amc13 format: " << headerAmc13.getFormatVersion() << std::endl;
246  str << " amc13 nAMCs: " << headerAmc13.getNumberOfAMCs() << std::endl;
247  str << " amc13 orbit: " << headerAmc13.getOrbitNumber() << std::endl;
248  LogTrace("") << str.str();
249  }
250  //unsigned int nAMCs = headerAmc13.getNumberOfAMCs();
251  //for (unsigned int iAMC = 0; iAMC <nAMCs; iAMC++) {
252  // const Word64* raw = header+1 +(iAMC+1);
253  //}
254 
255  //
256  // AMC13 trailer
257  //
258  const Word64* trailerAmc13raw = trailer - 1;
259  amc13::Trailer trailerAmc13(trailerAmc13raw);
260  if (debug) {
261  std::ostringstream str;
262  str << " trailerAMC13: " << *reinterpret_cast<const std::bitset<64>*>(trailerAmc13raw) << std::endl;
263  str << " crc: " << trailerAmc13.getCRC() << std::endl;
264  str << " block: " << trailerAmc13.getBlock() << std::endl;
265  str << " LV1ID: " << trailerAmc13.getLV1ID() << std::endl;
266  str << " BX: " << trailerAmc13.getBX() << std::endl;
267  LogTrace("") << str.str();
268  }
269 
270  //
271  // get AMC13 payload (-> AMC's data)
272  //
273  amc13::Packet packetAmc13;
274  if (!packetAmc13.parse(header, header + 1, nWords - 2, fedHeader.lvl1ID(), fedHeader.bxID(), true, false)) {
275  edm::LogError("OMTF") << "Could not extract AMC13 Packet.";
276  return;
277  }
278  //LogTrace("") <<"AMC13 Packet: "<< packetAmc13.blocks() << " size "<<packetAmc13.size() << std::endl;
279 
280  //
281  // loop over AMC's
282  //
283  unsigned int blockNum = 0;
284  for (auto amc : packetAmc13.payload()) {
285  amc::BlockHeader bh = amc.blockHeader();
286  if (debug) {
287  std::ostringstream str;
288  str << " ----------- #" << blockNum++ << std::endl;
289  str << " blockheader: " << std::bitset<64>(bh.raw()) << std::endl;
290  str << " boardID: " << bh.getBoardID() << std::endl;
291  str << " amcNumber: " << bh.getAMCNumber() << std::endl;
292  str << " size: " << bh.getSize(); // << std::endl;
293  LogTrace("") << str.str();
294  }
295 
296  //
297  // AMC header
298  //
299  amc::Header headerAmc = amc.header();
300  if (debug) {
301  std::ostringstream str;
302  str << " AMC header[0]: " << std::bitset<64>(headerAmc.raw()[0]) << std::endl;
303  str << " AMC header[1]: " << std::bitset<64>(headerAmc.raw()[1]) << std::endl;
304  str << " AMC number: " << headerAmc.getAMCNumber(); // << std::endl;
305  LogTrace("") << str.str();
306  }
307 
308  //
309  // AMC trailer
310  //
311  //amc::Trailer trailerAmc = amc.trailer(); //this is the expected way but does not work
312  amc::Trailer trailerAmc(amc.data().get() + amc.size() -
313  1); //FIXME: the above is prefered but this works (CMSSW900)
314  if (debug) {
315  std::ostringstream str;
316  str << " AMC trailer: " << std::bitset<64>(trailerAmc.raw()) << std::endl;
317  str << " getLV1ID: " << trailerAmc.getLV1ID() << std::endl;
318  str << " size: " << trailerAmc.getSize() << std::endl;
319  LogTrace("") << str.str();
320  }
321 
322  //
323  // AMC payload
324  //
325  const auto& payload64 = amc.data();
326  const Word64* word = payload64.get();
327  for (unsigned int iWord = 1; iWord <= amc.size(); iWord++, word++) {
328  if (iWord <= 2)
329  continue; // two header words for each AMC
330  if (iWord == amc.size())
331  continue; // trailer for each AMC
332 
333  LogTrace("") << " payload: " << *reinterpret_cast<const std::bitset<64>*>(word);
334  DataWord64::Type recordType = DataWord64::type(*word);
335 
336  unsigned int fedId = fedHeader.sourceID();
337  unsigned int amcId = bh.getAMCNumber() / 2 + 1; // in OMTF convertsion 1-6
338  //
339  // RPC data
340  //
341  if (DataWord64::rpc == recordType && !theSkipRpc) {
342  theRpcUnpacker.unpack(triggerBX, fedId, amcId, RpcDataWord64(*word), producedRPCDigis.get());
343  }
344 
345  //
346  // CSC data
347  //
348  if (DataWord64::csc == recordType && !theSkipCsc) {
349  theCscUnpacker.unpack(fedId, amcId, CscDataWord64(*word), producedCscLctDigis.get());
350  }
351 
352  //
353  // DT data
354  //
355  if (DataWord64::dt == recordType && !theSkipDt) {
356  theDtUnpacker.unpack(fedId, amcId, DtDataWord64(*word), phi_Container, the_Container);
357  }
358 
359  //
360  // OMTF (muon) data
361  //
362  if (DataWord64::omtf == recordType && !theSkipMuon) {
363  theMuonUnpacker.unpack(fedId, amcId, MuonDataWord64(*word), producedMuonDigis.get());
364  }
365  }
366  }
367  }
368  event.put(std::move(producedRPCDigis), theOutputTag);
369  event.put(std::move(producedCscLctDigis), theOutputTag);
370  event.put(std::move(producedMuonDigis), theOutputTag);
371  producedDTPhDigis->setContainer(phi_Container);
372  event.put(std::move(producedDTPhDigis), theOutputTag);
373  producedDTThDigis->setContainer(the_Container);
374  event.put(std::move(producedDTThDigis), theOutputTag);
375  }
376 
377 } // namespace omtf
378 
379 using namespace omtf;
uint8_t triggerType() const
Event Trigger type identifier.
Definition: FEDHeader.cc:13
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
std::string fullPath() const
Definition: FileInPath.cc:161
bool check() const
Check that the header is OK.
Definition: FEDHeader.cc:44
bool check() const
Definition: AMC13Spec.cc:19
uint16_t sourceID() const
Identifier of the FED.
Definition: FEDHeader.cc:19
uint8_t ttsBits() const
Current value of the Trigger Throttling System bits.
Definition: FEDTrailer.cc:19
void unpack(unsigned int fed, unsigned int amc, const CscDataWord64 &raw, CSCCorrelatedLCTDigiCollection *prod)
static MessageDrop * instance()
Definition: MessageDrop.cc:33
edm::ESGetToken< RPCEMap, RPCEMapRcd > theRPCEMapToken
Definition: OmtfUnpacker.cc:69
unsigned int getLV1ID() const
Definition: AMC13Spec.h:49
void init(const RPCEMap &readoutMapping, const RPCAMCLinkMap &linkMap)
void beginRun(const edm::Run &run, const edm::EventSetup &es) override
unsigned int getBX() const
Definition: AMC13Spec.h:50
unsigned int getAMCNumber() const
Definition: AMCSpec.h:71
Log< level::Error, false > LogError
std::vector< uint64_t > raw() const
Definition: AMCSpec.h:79
#define LogTrace(id)
uint32_t fragmentLength() const
The length of the event fragment counted in 64-bit words including header and trailer.
Definition: FEDTrailer.cc:13
void unpack(int triggerBX, unsigned int fed, unsigned int amc, const RpcDataWord64 &raw, RPCDigiCollection *prod)
MuonUnpacker theMuonUnpacker
Definition: OmtfUnpacker.cc:75
uint64_t word
RpcUnpacker theRpcUnpacker
Definition: OmtfUnpacker.cc:72
OmtfUnpacker(const edm::ParameterSet &pset)
Definition: OmtfUnpacker.cc:83
uint16_t crc() const
Cyclic Redundancy Code of the event fragment including header and trailer.
Definition: FEDTrailer.cc:15
bool moreHeaders() const
Definition: FEDHeader.cc:23
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
unsigned int getBlock() const
Definition: AMC13Spec.h:48
Type type(const T &)
uint8_t evtStatus() const
Event fragment status information.
Definition: FEDTrailer.cc:17
~OmtfUnpacker() override
Definition: OmtfUnpacker.cc:56
unsigned int getAMCNumber() const
Definition: AMCSpec.h:28
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
uint16_t bxID() const
The bunch crossing number.
Definition: FEDHeader.cc:17
uint64_t raw() const
Definition: AMCSpec.h:23
uint64_t Word64
#define debug
Definition: HDRShower.cc:19
unsigned long theEventCounter
Definition: OmtfUnpacker.cc:65
unsigned int getFormatVersion() const
Definition: AMC13Spec.h:24
uint8_t version() const
Version identifier of the FED data format.
Definition: FEDHeader.cc:21
unsigned int getBoardID() const
Definition: AMCSpec.h:29
DtUnpacker theDtUnpacker
Definition: OmtfUnpacker.cc:74
edm::EDGetTokenT< FEDRawDataCollection > theFedDataToken
Definition: OmtfUnpacker.cc:67
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:60
unsigned int getSize() const
Definition: AMCSpec.h:30
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::string theOutputTag
Definition: OmtfUnpacker.cc:78
void unpack(unsigned int fed, unsigned int amc, const DtDataWord64 &raw, std::vector< L1MuDTChambPhDigi > &phi_Container, std::vector< L1MuDTChambThDigi > &the_Container)
std::vector< amc::Packet > payload() const
Definition: AMC13Spec.h:92
unsigned int getOrbitNumber() const
Definition: AMC13Spec.h:26
ESTransientHandle< T > getTransientHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:141
uint32_t lvl1ID() const
Level-1 event number generated by the TTC system.
Definition: FEDHeader.cc:15
edm::ParameterSet theConfig
Definition: OmtfUnpacker.cc:77
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: AMCSpec.h:8
void unpack(unsigned int fed, unsigned int amc, const MuonDataWord64 &raw, l1t::RegionalMuonCandBxCollection *muColl)
#define str(s)
unsigned int getNumberOfAMCs() const
Definition: AMC13Spec.h:25
unsigned int getCRC() const
Definition: AMC13Spec.h:47
bool check() const
Check that the trailer is OK.
Definition: FEDTrailer.cc:45
edm::ESGetToken< RPCAMCLinkMap, RPCOMTFLinkMapRcd > theAmcMappingToken
Definition: OmtfUnpacker.cc:70
void produce(edm::Event &ev, const edm::EventSetup &es) override
def move(src, dest)
Definition: eostools.py:511
bool moreTrailers() const
Definition: FEDTrailer.cc:21
Definition: event.py:1
Definition: Run.h:45
CscUnpacker theCscUnpacker
Definition: OmtfUnpacker.cc:73
#define LogDebug(id)