CMS 3D CMS Logo

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