CMS 3D CMS Logo

RctRawToDigi.cc
Go to the documentation of this file.
2 
3 // System headers
4 #include <vector>
5 #include <sstream>
6 #include <iostream>
7 
8 // Framework headers
13 
14 // Raw data collection headers
19 
23 
24 // Namespace resolution
25 using edm::LogError;
26 using edm::LogWarning;
27 using std::cout;
28 using std::dec;
29 using std::endl;
30 using std::hex;
31 using std::string;
32 using std::vector;
33 
35  : inputLabel_(iConfig.getParameter<edm::InputTag>("inputLabel")),
36  fedId_(iConfig.getUntrackedParameter<int>("rctFedId", FEDNumbering::MINRCTFEDID)),
37  verbose_(iConfig.getUntrackedParameter<bool>("verbose", false)) {
38  LogDebug("RCT") << "RctRawToDigi will unpack FED Id " << fedId_;
40  // RCT collections
41  produces<L1CaloEmCollection>();
42  produces<L1CaloRegionCollection>();
43 
44  // Error collection
45  consumes<FEDRawDataCollection>(inputLabel_);
46 }
47 
49  // do anything here that needs to be done at destruction time
50  // (e.g. close files, deallocate resources etc.)
51  //delete formatTranslator_;
52 }
53 
54 // ------------ method called to produce the data ------------
56  using namespace edm;
57 
58  // Instantiate all the collections the unpacker needs; puts them in event when this object goes out of scope.
59  std::unique_ptr<RctUnpackCollections> colls(new RctUnpackCollections(iEvent));
60 
61  // get raw data collection
63  iEvent.getByLabel(inputLabel_, feds);
64 
65  // if raw data collection is present, check the headers and do the unpacking
66  if (feds.isValid()) {
67  const FEDRawData& rctRcd = feds->FEDData(fedId_);
68 
69  //Check FED size
70  LogDebug("RCT") << "Upacking FEDRawData of size " << std::dec << rctRcd.size();
71 
72  //check header size
74  if (rctRcd.size() > 0) {
75  LogError("L1T") << "Cannot unpack: empty/invalid L1T raw data (size = " << rctRcd.size() << ") for ID "
76  << fedId_ << ". Returning empty collections!";
77  } else {
78  // there's no MC packer for this payload, so totally expected that sometimes it will be absent... no warning issued.
79  }
80  //continue;
81  return;
82  }
83 
84  // do the unpacking
85  unpack(rctRcd, iEvent, colls.get());
86 
87  } else {
88  LogError("L1T") << "Cannot unpack: no collection found";
89 
90  return;
91  }
92 }
93 
95  const unsigned char* data = d.data(); // The 8-bit wide raw-data array.
96  // Data offset - starts at 16 as there is a 64-bit S-Link header followed
97  // by a 64-bit software-controlled header (for pipeline format version
98  // info that is not yet used).
99 
101 
102  if (header.check()) {
103  LogDebug("L1T") << "Found SLink header:"
104  << " Trigger type " << header.triggerType() << " L1 event ID " << header.lvl1ID() << " BX Number "
105  << header.bxID() << " FED source " << header.sourceID() << " FED version " << header.version();
106  } else {
107  LogWarning("L1T") << "Did not find a valid SLink header!";
108  }
109 
110  FEDTrailer trailer(data + (d.size() - sLinkTrailerSize_));
111 
112  if (trailer.check()) {
113  LogDebug("L1T") << "Found SLink trailer:"
114  << " Length " << trailer.fragmentLength() << " CRC " << trailer.crc() << " Status "
115  << trailer.evtStatus() << " Throttling bits " << trailer.ttsBits();
116  } else {
117  LogWarning("L1T") << "Did not find a SLink trailer!";
118  }
119 
120  unpackCTP7(reinterpret_cast<const uint32_t*>(data), 0, sizeof(data), colls);
121 }
122 
123 void RctRawToDigi::unpackCTP7(const uint32_t* data,
124  const unsigned block_id,
125  const unsigned size,
126  RctUnpackCollections* const colls) {
127  //offset from 6 link header words
128  uint32_t of = 6;
129  LogDebug("L1T") << "Block ID = " << block_id << " size = " << size;
130 
131  CTP7Format ctp7Format;
132  RctDataDecoder rctDataDecoder;
133  uint32_t nBXTemp = 0;
134  uint32_t ctp7FWVersion;
135  uint32_t L1ID, L1aBCID;
136  std::vector<RCTInfo> allCrateRCTInfo[5];
137 
138  L1ID = data[1 + of]; // extract the L1 ID number
139  L1aBCID = data[5 + of] & 0x00000FFF; // extract the BCID number of L1A
140  nBXTemp = (data[5 + of] & 0x00FF0000) >> 16; // extract number of BXs readout per L1A
141  ctp7FWVersion = data[4 + of];
142 
143  if (nBXTemp != 1 && nBXTemp != 3 && nBXTemp != 5)
144  nBXTemp = 1;
145 
146  const uint32_t nBX = nBXTemp;
147 
148  LogDebug("L1T") << "CTP7 L1ID = " << L1ID << " L1A BCID = " << L1aBCID << " BXs in capture = " << nBX
149  << " CTP7 DAQ FW = " << ctp7FWVersion;
150 
151  struct link_data {
152  bool even;
153  unsigned int crateID;
154  unsigned int ctp7LinkNumber;
155  std::vector<unsigned int> uint;
156  };
157 
158  //nBX max 5, nLinks max 36 [nBX][nLinks]
159  link_data allLinks[5][36];
160  const uint32_t NLinks = ctp7Format.NLINKS;
161  assert(NLinks <= 36);
162 
163  //change this implementation
164  uint32_t iDAQBuffer = 0;
165 
166  //Step 1: Grab all data from ctp7 buffer and put into link format
167  for (unsigned int iLink = 0; iLink < NLinks; iLink++) {
168  iDAQBuffer = of + ctp7Format.EVENT_HEADER_WORDS +
169  iLink * (ctp7Format.CHANNEL_HEADER_WORDS + nBX * ctp7Format.CHANNEL_DATA_WORDS_PER_BX);
170 
171  //first decode linkID
172  uint32_t linkID = data[iDAQBuffer++];
173  uint32_t tmp = data[iDAQBuffer++];
174  uint32_t CRCErrCnt = tmp & 0x0000FFFF;
175  //uint32_t linkStatus = (tmp & 0xFFFF0000) >> 16;
176 
177  uint32_t crateID = 0;
178  uint32_t expectedCrateID = 0;
179  bool even = false;
180  bool expectedEven = false;
181 
182  //getExpected Link ID
183  rctDataDecoder.getExpectedLinkID(iLink, expectedCrateID, expectedEven);
184  //getDecodedLink ID
185  rctDataDecoder.decodeLinkID(linkID, crateID, even);
186 
187  //Perform a check to see if the link ID is as expected, if not then report an error but continue unpacking
188  if (expectedCrateID != crateID || even != expectedEven) {
189  LogError("L1T") << "Expected Crate ID " << expectedCrateID << " expectedEven " << expectedEven
190  << "does not match actual Crate ID " << crateID << " even " << even;
191  }
192 
193  if (CRCErrCnt != 0)
194  LogError("L1T") << "WARNING CRC ErrorFound linkID " << linkID << " expected crateID " << expectedCrateID;
195 
196  // Loop over multiple BX
197  for (uint32_t iBX = 0; iBX < nBX; iBX++) {
198  allLinks[iBX][iLink].uint.reserve(6);
199  allLinks[iBX][iLink].ctp7LinkNumber = iLink;
200  allLinks[iBX][iLink].crateID = expectedCrateID;
201  allLinks[iBX][iLink].even = expectedEven;
202 
203  //Notice 6 words per BX
204  for (unsigned int iWord = 0; iWord < 6; iWord++) {
205  allLinks[iBX][iLink].uint.push_back(data[iDAQBuffer + iWord + iBX * 6]);
206  }
207  }
208  }
209 
210  //Step 2: Dynamically match links and create RCTInfo Objects
211  uint32_t nCratesFound = 0;
212  for (unsigned int iCrate = 0; iCrate < 18; iCrate++) {
213  bool foundEven = false, foundOdd = false;
214  link_data even[5];
215  link_data odd[5];
216 
217  for (unsigned int iLink = 0; iLink < NLinks; iLink++) {
218  if ((allLinks[0][iLink].crateID == iCrate) && (allLinks[0][iLink].even == true)) {
219  foundEven = true;
220  for (unsigned int iBX = 0; iBX < nBX; iBX++)
221  even[iBX] = allLinks[iBX][iLink];
222  } else if ((allLinks[0][iLink].crateID == iCrate) && (allLinks[0][iLink].even == false)) {
223  foundOdd = true;
224  for (unsigned int iBX = 0; iBX < nBX; iBX++)
225  odd[iBX] = allLinks[iBX][iLink];
226  }
227 
228  //if success then create RCTInfoVector and fill output object
229  if (foundEven && foundOdd) {
230  nCratesFound++;
231 
232  //fill rctInfoVector for all BX read out
233  for (unsigned int iBX = 0; iBX < nBX; iBX++) {
234  //RCTInfoFactory rctInfoFactory;
235  std::vector<RCTInfo> rctInfoData;
236  rctDataDecoder.decodeLinks(even[iBX].uint, odd[iBX].uint, rctInfoData);
237  rctDataDecoder.setRCTInfoCrateID(rctInfoData, iCrate);
238  allCrateRCTInfo[iBX].push_back(rctInfoData.at(0));
239  }
240  break;
241  }
242  }
243  }
244 
245  if (nCratesFound != 18)
246  LogError("L1T") << "Warning -- only found " << nCratesFound << " valid crates";
247 
248  //start assuming 1 BX readout
249  int32_t startBX = 0;
250  if (nBX == 1)
251  startBX = 0;
252  else if (nBX == 3)
253  startBX = -1;
254  else if (nBX == 5)
255  startBX = -2;
256 
257  //Step 3: Create Collections from RCTInfo Objects
258  //Notice, iBX used for grabbing data from array, startBX used for storing in Collection
259  for (uint32_t iBX = 0; iBX < nBX; iBX++, startBX++) {
260  for (unsigned int iCrate = 0; iCrate < nCratesFound; iCrate++) {
261  RCTInfo rctInfo = allCrateRCTInfo[iBX].at(iCrate);
262  //Use Crate ID to identify eta/phi of candidate
263  for (int j = 0; j < 4; j++) {
264  L1CaloEmCand em = L1CaloEmCand(rctInfo.neRank[j], rctInfo.neRegn[j], rctInfo.neCard[j], rctInfo.crateID, false);
265  em.setBx(startBX);
266  colls->rctEm()->push_back(em);
267  }
268 
269  for (int j = 0; j < 4; j++) {
270  L1CaloEmCand em = L1CaloEmCand(rctInfo.ieRank[j], rctInfo.ieRegn[j], rctInfo.ieCard[j], rctInfo.crateID, true);
271  em.setBx(startBX);
272  colls->rctEm()->push_back(em);
273  }
274 
275  for (int j = 0; j < 7; j++) {
276  for (int k = 0; k < 2; k++) {
277  bool o = (((rctInfo.oBits >> (j * 2 + k)) & 0x1) == 0x1);
278  bool t = (((rctInfo.tBits >> (j * 2 + k)) & 0x1) == 0x1);
279  bool m = (((rctInfo.mBits >> (j * 2 + k)) & 0x1) == 0x1);
280  bool q = (((rctInfo.qBits >> (j * 2 + k)) & 0x1) == 0x1);
281  L1CaloRegion rgn = L1CaloRegion(rctInfo.rgnEt[j][k], o, t, m, q, rctInfo.crateID, j, k);
282  rgn.setBx(startBX);
283  colls->rctCalo()->push_back(rgn);
284  }
285  }
286 
287  for (int k = 0; k < 4; k++) {
288  for (int j = 0; j < 2; j++) {
289  // 0 1 4 5 2 3 6 7
290  uint32_t offset = j * 2 + k % 2 + (k / 2) * 4;
291  bool fg = (((rctInfo.hfQBits >> offset) & 0x1) == 0x1);
292  L1CaloRegion rgn = L1CaloRegion(rctInfo.hfEt[j][k], fg, rctInfo.crateID, (j * 4 + k));
293  rgn.setBx(startBX);
294  colls->rctCalo()->push_back(rgn);
295  }
296  }
297  }
298  }
299 }
300 
301 bool RctRawToDigi::printAll(const unsigned char* data, const unsigned size) {
302  for (unsigned i = 0; i < size; i++) {
303  std::cout << data[i] << " ";
304  if (i % 6 == 5)
305  std::cout << std::endl;
306  }
307  return true;
308 }
309 
310 // ------------ method called once each job just after ending the event loop ------------
312  unsigned total = 0;
313  std::ostringstream os;
314 
315  for (unsigned i = 0; i <= MAX_ERR_CODE; ++i) {
316  total += errorCounters_.at(i);
317  os << "Error " << i << " (" << errorCounters_.at(i) << ")";
318  if (i < MAX_ERR_CODE) {
319  os << ", ";
320  }
321  }
322 
323  if (total > 0 && verbose_) {
324  edm::LogError("RCT") << "Encountered " << total << " unpacking errors: " << os.str();
325  }
326 }
327 
size
Write out results.
std::vector< unsigned > errorCounters_
Counts number of errors for each code (index)
Definition: RctRawToDigi.h:104
static const unsigned sLinkTrailerSize_
Definition: RctRawToDigi.h:72
static const unsigned sLinkHeaderSize_
Definition: RctRawToDigi.h:69
virtual void endJob()
method called at job end - use to print summary report
static const unsigned amc13TrailerSize_
Definition: RctRawToDigi.h:78
L1CaloRegionCollection *const rctCalo() const
Input calo regions from the RCT to the RCT.
uint8_t ttsBits() const
Current value of the Trigger Throttling System bits.
Definition: FEDTrailer.cc:19
void setBx(int16_t bx)
set bx
void setBx(int16_t bx)
set BX
Definition: L1CaloEmCand.cc:49
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:45
Level-1 Region Calorimeter Trigger EM candidate.
Definition: L1CaloEmCand.h:17
static const unsigned MIN_DATA
The minimum number of blocks we will try to unpack before thinking something is wrong (really this sh...
Definition: RctRawToDigi.h:90
Log< level::Error, false > LogError
assert(be >=bs)
uint32_t fragmentLength() const
The length of the event fragment counted in 64-bit words including header and trailer.
Definition: FEDTrailer.cc:13
int iEvent
Definition: GenABIO.cc:224
void unpackCTP7(const uint32_t *data, const unsigned block_id, const unsigned size, RctUnpackCollections *const colls)
~RctRawToDigi() override
Definition: RctRawToDigi.cc:48
uint16_t crc() const
Cyclic Redundancy Code of the event fragment including header and trailer.
Definition: FEDTrailer.cc:15
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
uint8_t evtStatus() const
Event fragment status information.
Definition: FEDTrailer.cc:17
d
Definition: ztail.py:151
static const unsigned MAX_ERR_CODE
Definition: RctRawToDigi.h:102
RctRawToDigi(const edm::ParameterSet &)
Definition: RctRawToDigi.cc:34
edm::InputTag inputLabel_
FED collection label.
Definition: RctRawToDigi.h:93
L1CaloEmCollection *const rctEm() const
Input electrons from the RCT to the RCT.
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
void produce(edm::Event &, const edm::EventSetup &) override
Definition: RctRawToDigi.cc:55
A calorimeter trigger region (sum of 4x4 trigger towers)
Definition: L1CaloRegion.h:21
void unpack(const FEDRawData &d, edm::Event &e, RctUnpackCollections *const colls)
Unpacks the raw data.
Definition: RctRawToDigi.cc:94
Log< level::Warning, false > LogWarning
bool printAll(const unsigned char *data, const unsigned size)
tmp
align.sh
Definition: createJobs.py:716
bool check() const
Check that the trailer is OK.
Definition: FEDTrailer.cc:45
int fedId_
RCT FED ID.
Definition: RctRawToDigi.h:94
const bool verbose_
If true, then debug print out for each event.
Definition: RctRawToDigi.h:96
static const unsigned amc13HeaderSize_
Definition: RctRawToDigi.h:75
#define LogDebug(id)