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  if (rctRcd.size() > 0) {
84  LogError("L1T") << "Cannot unpack: empty/invalid L1T raw data (size = "
85  << rctRcd.size() << ") for ID " << fedId_ << ". Returning empty collections!";
86  } else {
87  // there's no MC packer for this payload, so totally expected that sometimes it will be absent... no warning issued.
88  }
89  //continue;
90  return;
91  }
92 
93  // do the unpacking
94  unpack(rctRcd, iEvent, colls.get());
95 
96  }
97  else{
98 
99  LogError("L1T") << "Cannot unpack: no collection found";
100 
101  return;
102  }
103 
104 }
105 
107 {
108 
109  const unsigned char * data = d.data(); // The 8-bit wide raw-data array.
110  // Data offset - starts at 16 as there is a 64-bit S-Link header followed
111  // by a 64-bit software-controlled header (for pipeline format version
112  // info that is not yet used).
113 
114 
115  FEDHeader header(data);
116 
117  if (header.check()) {
118  LogDebug("L1T") << "Found SLink header:"
119  << " Trigger type " << header.triggerType()
120  << " L1 event ID " << header.lvl1ID()
121  << " BX Number " << header.bxID()
122  << " FED source " << header.sourceID()
123  << " FED version " << header.version();
124  } else {
125  LogWarning("L1T") << "Did not find a valid SLink header!";
126  }
127 
128  FEDTrailer trailer(data + (d.size() - sLinkTrailerSize_));
129 
130  if (trailer.check()) {
131  LogDebug("L1T") << "Found SLink trailer:"
132  << " Length " << trailer.lenght()
133  << " CRC " << trailer.crc()
134  << " Status " << trailer.evtStatus()
135  << " Throttling bits " << trailer.ttsBits();
136  } else {
137  LogWarning("L1T") << "Did not find a SLink trailer!";
138  }
139 
140  unpackCTP7((uint32_t*)data, 0, sizeof(data), colls);
141 
142 }
143 
144 
145 void
146 RctRawToDigi::unpackCTP7(const uint32_t *data, const unsigned block_id, const unsigned size, RctUnpackCollections * const colls)
147 {
148  //offset from 6 link header words
149  uint32_t of = 6;
150  LogDebug("L1T") << "Block ID = " << block_id << " size = " << size;
151 
152  CTP7Format ctp7Format;
153  RctDataDecoder rctDataDecoder;
154  uint32_t nBXTemp = 0;
155  uint32_t ctp7FWVersion;
156  uint32_t L1ID, L1aBCID;
157  std::vector<RCTInfo> allCrateRCTInfo[5];
158 
159  L1ID = data[1+of]; // extract the L1 ID number
160  L1aBCID = data[5+of] & 0x00000FFF; // extract the BCID number of L1A
161  nBXTemp = (data[5+of] & 0x00FF0000) >> 16; // extract number of BXs readout per L1A
162  ctp7FWVersion = data[4+of];
163 
164  if(nBXTemp != 1 && nBXTemp != 3 && nBXTemp != 5)
165  nBXTemp = 1;
166 
167  const uint32_t nBX = nBXTemp;
168 
169  LogDebug("L1T") << "CTP7 L1ID = " << L1ID << " L1A BCID = " << L1aBCID << " BXs in capture = " << nBX << " CTP7 DAQ FW = " << ctp7FWVersion;
170 
171  struct link_data{
172  bool even;
173  unsigned int crateID;
174  unsigned int ctp7LinkNumber;
175  std::vector <unsigned int> uint;
176  };
177 
178  //nBX max 5, nLinks max 36 [nBX][nLinks]
179  link_data allLinks[5][36];
180  const uint32_t NLinks = ctp7Format.NLINKS;
181  assert(NLinks <= 36);
182 
183  //change this implementation
184  uint32_t iDAQBuffer = 0;
185 
186  //Step 1: Grab all data from ctp7 buffer and put into link format
187  for(unsigned int iLink = 0; iLink < NLinks; iLink++ ){
188  iDAQBuffer = of +
189  ctp7Format.EVENT_HEADER_WORDS + iLink * (ctp7Format.CHANNEL_HEADER_WORDS +
190  nBX * ctp7Format.CHANNEL_DATA_WORDS_PER_BX);
191 
192  //first decode linkID
193  uint32_t linkID = data[iDAQBuffer++];
194  uint32_t tmp = data[iDAQBuffer++];
195  uint32_t CRCErrCnt = tmp & 0x0000FFFF;
196  //uint32_t linkStatus = (tmp & 0xFFFF0000) >> 16;
197 
198  uint32_t crateID = 0; uint32_t expectedCrateID = 0;
199  bool even = false; bool expectedEven = false;
200 
201  //getExpected Link ID
202  rctDataDecoder.getExpectedLinkID(iLink, expectedCrateID, expectedEven);
203  //getDecodedLink ID
204  rctDataDecoder.decodeLinkID(linkID, crateID, even);
205 
206  //Perform a check to see if the link ID is as expected, if not then report an error but continue unpacking
207  if(expectedCrateID!=crateID || even!=expectedEven ){
208  LogError("L1T") <<"Expected Crate ID "<< expectedCrateID <<" expectedEven "<< expectedEven
209  <<"does not match actual Crate ID "<<crateID<<" even "<<even;
210  }
211 
212  if(CRCErrCnt!=0)
213  LogError("L1T")<<"WARNING CRC ErrorFound linkID "<< linkID<<" expected crateID "<< expectedCrateID;
214 
215  // Loop over multiple BX
216  for (uint32_t iBX=0; iBX<nBX; iBX++){
217  allLinks[iBX][iLink].uint.reserve(6);
218  allLinks[iBX][iLink].ctp7LinkNumber = iLink;
219  allLinks[iBX][iLink].crateID = expectedCrateID;
220  allLinks[iBX][iLink].even = expectedEven;
221 
222  //Notice 6 words per BX
223  for(unsigned int iWord = 0; iWord < 6 ; iWord++ ){
224  allLinks[iBX][iLink].uint.push_back(data[iDAQBuffer+iWord+iBX*6]);
225  }
226  }
227  }
228 
229  //Step 2: Dynamically match links and create RCTInfo Objects
230  uint32_t nCratesFound = 0;
231  for(unsigned int iCrate = 0; iCrate < 18 ; iCrate++){
232 
233  bool foundEven = false, foundOdd = false;
234  link_data even[5];
235  link_data odd[5];
236 
237  for(unsigned int iLink = 0; iLink < NLinks; iLink++){
238 
239  if( (allLinks[0][iLink].crateID==iCrate) && (allLinks[0][iLink].even == true) ){
240  foundEven = true;
241  for (unsigned int iBX=0; iBX<nBX; iBX++)
242  even[iBX] = allLinks[iBX][iLink];
243  }
244  else if( (allLinks[0][iLink].crateID==iCrate) && (allLinks[0][iLink].even == false) ){
245  foundOdd = true;
246  for (unsigned int iBX=0; iBX<nBX; iBX++)
247  odd[iBX] = allLinks[iBX][iLink];
248  }
249 
250  //if success then create RCTInfoVector and fill output object
251  if(foundEven && foundOdd){
252  nCratesFound++;
253 
254  //fill rctInfoVector for all BX read out
255  for (unsigned int iBX=0; iBX<nBX; iBX++){
256  //RCTInfoFactory rctInfoFactory;
257  std::vector <RCTInfo> rctInfoData;
258  rctDataDecoder.decodeLinks(even[iBX].uint, odd[iBX].uint, rctInfoData);
259  rctDataDecoder.setRCTInfoCrateID(rctInfoData, iCrate);
260  allCrateRCTInfo[iBX].push_back(rctInfoData.at(0));
261  }
262  break;
263  }
264  }
265  }
266 
267  if(nCratesFound != 18)
268  LogError("L1T") << "Warning -- only found "<< nCratesFound << " valid crates";
269 
270  //start assuming 1 BX readout
271  int32_t startBX = 0;
272  if(nBX == 1)
273  startBX = 0;
274  else if(nBX == 3)
275  startBX = -1;
276  else if(nBX == 5)
277  startBX = -2;
278 
279  //Step 3: Create Collections from RCTInfo Objects
280  //Notice, iBX used for grabbing data from array, startBX used for storing in Collection
281  for (uint32_t iBX=0; iBX<nBX; iBX++, startBX++){
282 
283  for(unsigned int iCrate = 0; iCrate < nCratesFound; iCrate++ ){
284 
285  RCTInfo rctInfo = allCrateRCTInfo[iBX].at(iCrate);
286  //Use Crate ID to identify eta/phi of candidate
287  for(int j = 0; j < 4; j++) {
288  L1CaloEmCand em = L1CaloEmCand(rctInfo.neRank[j],
289  rctInfo.neRegn[j],
290  rctInfo.neCard[j],
291  rctInfo.crateID,
292  false);
293  em.setBx(startBX);
294  colls->rctEm()->push_back(em);
295  }
296 
297  for(int j = 0; j < 4; j++) {
298  L1CaloEmCand em = L1CaloEmCand(rctInfo.ieRank[j],
299  rctInfo.ieRegn[j],
300  rctInfo.ieCard[j],
301  rctInfo.crateID,
302  true);
303  em.setBx(startBX);
304  colls->rctEm()->push_back(em);
305  }
306 
307  for(int j = 0; j < 7; j++) {
308  for(int k = 0; k < 2; k++) {
309  bool o = (((rctInfo.oBits >> (j * 2 + k)) & 0x1) == 0x1);
310  bool t = (((rctInfo.tBits >> (j * 2 + k)) & 0x1) == 0x1);
311  bool m = (((rctInfo.mBits >> (j * 2 + k)) & 0x1) == 0x1);
312  bool q = (((rctInfo.qBits >> (j * 2 + k)) & 0x1) == 0x1);
313  L1CaloRegion rgn = L1CaloRegion(rctInfo.rgnEt[j][k], o, t, m, q, rctInfo.crateID , j, k);
314  rgn.setBx(startBX);
315  colls->rctCalo()->push_back(rgn);
316  }
317  }
318 
319  for(int k = 0; k < 4; k++) {
320  for(int j = 0; j < 2; j++) {
321  // 0 1 4 5 2 3 6 7
322  uint32_t offset = j*2 + k%2 + (k/2)*4;
323  bool fg=(((rctInfo.hfQBits >> offset) & 0x1) == 0x1);
324  L1CaloRegion rgn = L1CaloRegion(rctInfo.hfEt[j][k], fg, rctInfo.crateID , (j * 4 + k));
325  rgn.setBx(startBX);
326  colls->rctCalo()->push_back(rgn);
327  }
328  }
329  }
330  }
331 }
332 
333 
334 
335 bool
336 RctRawToDigi::printAll(const unsigned char *data, const unsigned size)
337 {
338  for(unsigned i = 0; i < size; i ++){
339  std::cout << data[i] << " ";
340  if(i%6==5)
341  std::cout<<std::endl;
342  }
343  return true;
344 }
345 
346 
347 // ------------ method called once each job just after ending the event loop ------------
349 {
350  unsigned total=0;
351  std::ostringstream os;
352 
353  for (unsigned i=0 ; i <= MAX_ERR_CODE ; ++i) {
354  total+=errorCounters_.at(i);
355  os << "Error " << i << " (" << errorCounters_.at(i) << ")";
356  if(i < MAX_ERR_CODE) { os << ", "; }
357  }
358 
359  if (total>0 && verbose_) {
360  edm::LogError("RCT") << "Encountered " << total << " unpacking errors: " << os.str();
361  }
362 }
363 
#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:331
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:418
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
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:145
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