CMS 3D CMS Logo

HcalUnpacker.cc
Go to the documentation of this file.
12 
13 //#define DebugLog
14 
15 namespace HcalUnpacker_impl {
16  template <class DigiClass>
17  const HcalQIESample* unpack(const HcalQIESample* startPoint, const HcalQIESample* limit, DigiClass& digi, int presamples, const HcalElectronicsId& eid, int startSample, int endSample, int expectedTime, const HcalHTRData& hhd) {
18  // set parameters
19  digi.setPresamples(presamples);
20  digi.setReadoutIds(eid);
21 
22  int fiber=startPoint->fiber();
23  int fiberchan=startPoint->fiberChan();
24  uint32_t zsmask=hhd.zsBunchMask()>>startSample;
25  digi.setZSInfo(hhd.isUnsuppressed(),hhd.wasMarkAndPassZS(fiber,fiberchan),zsmask);
26 
27  if (expectedTime>=0 && !hhd.isUnsuppressed()) {
28 #ifdef DebugLog
29  std::cout << hhd.getFibOrbMsgBCN(fiber) << " " << expectedTime << std::endl;
30 #endif
31  digi.setFiberIdleOffset(hhd.getFibOrbMsgBCN(fiber)-expectedTime);
32  }
33 
34  // what is my sample number?
35  int myFiberChan=startPoint->fiberAndChan();
36  int ncurr=0,ntaken=0;
37  const HcalQIESample* qie_work=startPoint;
38  while (qie_work!=limit && qie_work->fiberAndChan()==myFiberChan) {
39  if (ncurr>=startSample && ncurr<=endSample) {
40  digi.setSample(ntaken,*qie_work);
41  ++ntaken;
42  }
43  ncurr++;
44  qie_work++;
45  }
46  digi.setSize(ntaken);
47  return qie_work;
48  }
49 
50 
51  template <class DigiClass>
52  const unsigned short* unpack_compact(const unsigned short* startPoint, const unsigned short* limit, DigiClass& digi,
53  int presamples, const HcalElectronicsId& eid, int startSample, int endSample,
54  int expectedTime, const HcalHTRData& hhd) {
55  // set parameters
56  digi.setPresamples(presamples);
57  digi.setReadoutIds(eid);
58  int flavor, error_flags, capid0, channelid;
59 
60  HcalHTRData::unpack_per_channel_header(*startPoint,flavor,error_flags,capid0,channelid);
61  bool isCapRotating=!(error_flags&0x1);
62  bool fiberErr=(error_flags&0x2);
63  bool dataValid=!(error_flags&0x2);
64  int fiberchan=channelid&0x3;
65  int fiber=((channelid>>2)&0x7)+1;
66 
67  uint32_t zsmask=hhd.zsBunchMask()>>startSample;
68  digi.setZSInfo(hhd.isUnsuppressed(),hhd.wasMarkAndPassZS(fiber,fiberchan),zsmask);
69 
70  if (expectedTime>=0 && !hhd.isUnsuppressed()) {
71 #ifdef DebugLog
72  std::cout << hhd.getFibOrbMsgBCN(fiber) << " " << expectedTime << std::endl;
73 #endif
74  digi.setFiberIdleOffset(hhd.getFibOrbMsgBCN(fiber)-expectedTime);
75  }
76 
77  // what is my sample number?
78  int ncurr=0,ntaken=0;
79  const unsigned short* qie_work=startPoint;
80  // we branch here between normal (flavor=5) and error mode (flavor=6)
81  if (flavor==5) {
82  for (qie_work++; qie_work!=limit && !HcalHTRData::is_channel_header(*qie_work); qie_work++) {
83  int capidn=(isCapRotating)?((capid0+ncurr)%4):(capid0);
84  int capidn1=(isCapRotating)?((capid0+ncurr+1)%4):(capid0);
85  // two samples in one...
86  HcalQIESample s0((*qie_work)&0x7F,capidn,fiber,fiberchan,dataValid,fiberErr);
87  HcalQIESample s1(((*qie_work)>>8)&0x7F,capidn1,fiber,fiberchan,dataValid,fiberErr);
88 
89  if (ncurr>=startSample && ncurr<=endSample) {
90  digi.setSample(ntaken,s0);
91  ++ntaken;
92  }
93  ncurr++;
94  if (ncurr>=startSample && ncurr<=endSample) {
95  digi.setSample(ntaken,s1);
96  ++ntaken;
97  }
98  ncurr++;
99  }
100  digi.setSize(ntaken);
101  } else if (flavor==6) {
102  for (qie_work++; qie_work!=limit && !HcalHTRData::is_channel_header(*qie_work); qie_work++) {
103  if (ncurr>=startSample && ncurr<=endSample) {
104  HcalQIESample sample((*qie_work)&0x7F,((*qie_work)>>8)&0x3,fiber,fiberchan,((*qie_work)>>10)&0x1,((*qie_work)>>11)&0x1);
105  digi.setSample(ntaken,sample);
106  ++ntaken;
107  }
108  ncurr++;
109  }
110  digi.setSize(ntaken);
111  } else {
112  edm::LogWarning("Bad Data") << "Invalid flavor " << flavor;
113  qie_work=limit;
114  }
115  return qie_work;
116  }
117 
118  template <class DigiClass>
120  int presamples, const HcalElectronicsId& eid, int startSample, int endSample) {
121  // set parameters
122  digi.setPresamples(presamples-startSample);
123  digi.setReadoutIds(eid);
124  int error_flags=i.errFlags();
125  int capid0=i.capid0();
126  int flavor = i.flavor();
127 
128  bool isCapRotating=!(error_flags&0x1);
129  bool fiberErr=(error_flags&0x2);
130  bool dataValid=!(error_flags&0x2);
131  int fiberchan=i.channelid()&0x3;
132  int fiber=((i.channelid()>>2)&0x7)+1;
133 
134  // digi.setZSInfo(hhd.isUnsuppressed(),hhd.wasMarkAndPassZS(fiber,fiberchan),zsmask);
135 
136  // what is my sample number?
137  int ncurr=0,ntaken=0;
138  if(flavor==5){
139  for (++i; i!=iend && !i.isHeader(); ++i) {
140  int capidn=(isCapRotating)?((capid0+ncurr)%4):(capid0);
141 
142  HcalQIESample s(i.adc(),capidn,fiber,fiberchan,dataValid,fiberErr);
143 
144  if (ncurr>=startSample && ncurr<=endSample) {
145  digi.setSample(ntaken,s);
146  ++ntaken;
147  }
148  ncurr++;
149  }
150  digi.setSize(ntaken);
151  }
152  else if(flavor==7){ //similar to VME flavor 6, used for premix in MC
153  for (++i; i!=iend && !i.isHeader(); ++i) {
154  if (ncurr>=startSample && ncurr<=endSample) {
155  HcalQIESample sample(i.adc(),i.capid(),fiber,fiberchan,i.dataValid(),i.errFlags());
156  digi.setSample(ntaken,sample);
157  ++ntaken;
158  }
159  ncurr++;
160  }
161  digi.setSize(ntaken);
162  }
163  }
164 
165 }
166 
167 
168 static inline bool isTPGSOI(const HcalTriggerPrimitiveSample& s) {
169  return (s.raw()&0x200)!=0;
170 }
171 
172 
173 struct HOUnrolledTP { // parts of an HO trigger primitive, unpacked
174  bool valid, checked;
175  int ieta, iphi, samples, soi;
176  unsigned int databits;
178  valid=false;
179  checked=false;
180  ieta=0;
181  iphi=0;
182  samples=0;
183  soi=0;
184  databits=0;
185  }
186  void setbit(int i) { databits|=(1<<i); }
187 };
188 
190  Collections& colls, HcalUnpackerReport& report, bool silent) {
191 
192  if (raw.size()<16) {
193  if (!silent) edm::LogWarning("Invalid Data") << "Empty/invalid data, size = " << raw.size();
194  return;
195  }
196 
197  // get the DCC header
198  const HcalDCCHeader* dccHeader=(const HcalDCCHeader*)(raw.data());
199 
200  if (dccHeader->BOEshouldBeZeroAlways()==0) // also includes uTCA before the common AMC13XG firmware
201  unpackVME(raw,emap,colls,report,silent);
202  else unpackUTCA(raw,emap,colls,report,silent);
203 }
204 
205 static int slb(uint16_t theSample) { return ((theSample>>13)&0x7); }
206 static int slbChan(uint16_t theSample) { return (theSample>>11)&0x3; }
207 static int slbAndChan(uint16_t theSample) { return (theSample>>11)&0x1F; }
208 
209 
211  Collections& colls, HcalUnpackerReport& report, bool silent) {
212 
213  // get the DCC header
214  const HcalDCCHeader* dccHeader=(const HcalDCCHeader*)(raw.data());
215  const HcalDTCHeader* dtcHeader=(const HcalDTCHeader*)(raw.data());
216  bool is_VME_DCC=(dccHeader->getDCCDataFormatVersion()<0x10) || ((mode_&0x1)==0);
217 
218  int dccid=(is_VME_DCC)?(dccHeader->getSourceId()-sourceIdOffset_):(dtcHeader->getSourceId()-sourceIdOffset_);
219 
220  // check the summary status
221 
222  // walk through the HTR data. For the uTCA, use spigot=slot+1
223  HcalHTRData htr;
224  const unsigned short* daq_first, *daq_last, *tp_first, *tp_last;
225  const HcalQIESample* qie_begin, *qie_end, *qie_work;
226  const HcalTriggerPrimitiveSample *tp_begin, *tp_end, *tp_work;
227  for (int spigot=0; spigot<HcalDCCHeader::SPIGOT_COUNT; spigot++) {
228 
229  if (is_VME_DCC) {
230  if (!dccHeader->getSpigotPresent(spigot)) continue;
231 
232  int retval=dccHeader->getSpigotData(spigot,htr,raw.size());
233  if (retval!=0) {
234  if (retval==-1) {
235  if (!silent) edm::LogWarning("Invalid Data") << "Invalid HTR data (data beyond payload size) observed on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
236  report.countSpigotFormatError();
237  }
238  continue;
239  }
240  // check
241  if (dccHeader->getSpigotCRCError(spigot)) {
242  if (!silent)
243  edm::LogWarning("Invalid Data") << "CRC Error on HTR data observed on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
244  report.countSpigotFormatError();
245  continue;
246  }
247  } else { // is_uTCA (!is_VME_DCC)
248  int slot=spigot+1;
249  if (slot>HcalDTCHeader::MAXIMUM_SLOT) continue;
250 
251  if (!dtcHeader->getSlotPresent(slot)) continue;
252 
253  int retval=dtcHeader->getSlotData(slot,htr,raw.size());
254  if (retval!=0) {
255  if (retval==-1) {
256  if (!silent) edm::LogWarning("Invalid Data") << "Invalid uHTR data (data beyond payload size) observed on slot " << slot << " of DTC with source id " << dtcHeader->getSourceId();
257  report.countSpigotFormatError();
258  }
259  continue;
260  }
261  // check
262  if (dtcHeader->getSlotCRCError(slot)) {
263  if (!silent)
264  edm::LogWarning("Invalid Data") << "CRC Error on uHTR data observed on slot " << slot << " of DTC with source id " << dtcHeader->getSourceId();
265  report.countSpigotFormatError();
266  continue;
267  }
268  }
269 
270 
271  // check for EE
272  if (htr.isEmptyEvent()) {
273  report.countEmptyEventSpigot();
274  }
275  if (htr.isOverflowWarning()) {
276  report.countOFWSpigot();
277  }
278  if (htr.isBusy()) {
279  report.countBusySpigot();
280  }
281  if (!htr.check()) {
282  if (!silent)
283  edm::LogWarning("Invalid Data") << "Invalid HTR data observed on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
284  report.countSpigotFormatError();
285  continue;
286  }
287  if (htr.isHistogramEvent()) {
288  if (!silent) edm::LogWarning("Invalid Data") << "Histogram data passed to non-histogram unpacker on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
289  continue;
290  }
291  if ((htr.getFirmwareFlavor()&0xE0)==0x80) { // some kind of TTP data
292  if (colls.ttp!=nullptr) {
293  HcalTTPUnpacker ttpUnpack;
294  colls.ttp->push_back(HcalTTPDigi());
295  ttpUnpack.unpack(htr,colls.ttp->back());
296  } else {
297  LogDebug("HcalTechTrigProcessor") << "Skipping data on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId() << " which is from the TechTrigProcessor (use separate unpacker!)";
298  }
299  continue;
300  }
301  if (htr.getFirmwareFlavor()>=0x80) {
302  if (!silent) edm::LogWarning("HcalUnpacker") << "Skipping data on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId() << " which is of unknown flavor " << htr.getFirmwareFlavor();
303  continue;
304  }
305 
306  // calculate "real" number of presamples
307  int nps=htr.getNPS()-startSample_;
308 
309  // get pointers
310  htr.dataPointers(&daq_first,&daq_last,&tp_first,&tp_last);
311  unsigned int smid=htr.getSubmodule();
312  int htr_tb=smid&0x1;
313  int htr_slot=(smid>>1)&0x1F;
314  int htr_cr=(smid>>6)&0x1F;
315 
316  tp_begin=(const HcalTriggerPrimitiveSample*)tp_first;
317  tp_end=(const HcalTriggerPrimitiveSample*)(tp_last+1); // one beyond last..
318 
320  int currFiberChan=0x3F; // invalid fiber+channel...
321  int ncurr=0;
322  bool valid=false;
323 
324  bool tpgSOIbitInUse=htr.getFormatVersion()>=3; // version 3 and later
325  bool isHOtpg=htr.getFormatVersion()>=3 && htr.getFirmwareFlavor()==0; // HO is flavor zero
326  int npre=0;
327  /*
328  Unpack the trigger primitives
329  */
330  if (isHOtpg) {
331  HOUnrolledTP unrolled[24];
332  for (tp_work=tp_begin; tp_work!=tp_end; tp_work++) {
333  if (tp_work->raw()==0xFFFF) continue; // filler word
334  int sector=slbChan(tp_work->raw());
335  if (sector>2) continue;
336 
337  for (int ibit=0; ibit<8; ibit++) {
338  int linear=sector*8+ibit;
339  if (!unrolled[linear].checked) {
340  unrolled[linear].checked=true;
341  int fiber=(linear/3)+1;
342  int fc=(linear%3);
343  // electronics id (use precision match for HO TP)
344  HcalElectronicsId eid(fc,fiber,spigot,dccid);
345  eid.setHTR(htr_cr,htr_slot,htr_tb);
346  DetId did=emap.lookup(eid);
347  if (!did.null()) {
348  if (did.det()==DetId::Hcal && ((HcalSubdetector)did.subdetId())==HcalOuter ) {
349  HcalDetId hid(did);
350  unrolled[linear].valid=true;
351  unrolled[linear].ieta=hid.ieta();
352  unrolled[linear].iphi=hid.iphi();
353  }
354  } else {
355  report.countUnmappedTPDigi(eid);
356  }
357  }
358  if (unrolled[linear].valid) {
359  if (isTPGSOI(*tp_work)) unrolled[linear].soi=unrolled[linear].samples;
360  if (tp_work->raw()&(1<<ibit)) unrolled[linear].setbit(unrolled[linear].samples);
361  unrolled[linear].samples++;
362  }
363  }
364  }
365  for (int i=0; i<24; i++) {
366  if (unrolled[i].valid)
367  colls.tphoCont->push_back(HOTriggerPrimitiveDigi(
368  unrolled[i].ieta,
369  unrolled[i].iphi,
370  unrolled[i].samples,
371  unrolled[i].soi,
372  unrolled[i].databits));
373  }
374  } else { // regular TPs (not HO)
375  for (tp_work=tp_begin; tp_work!=tp_end; tp_work++) {
376  if (tp_work->raw()==0xFFFF) continue; // filler word
377  if (slbAndChan(tp_work->raw())!=currFiberChan) { // start new set
378  npre=0;
379  currFiberChan=slbAndChan(tp_work->raw());
380  // lookup the right channel
381  HcalElectronicsId eid(slbChan(tp_work->raw()),slb(tp_work->raw()),spigot,dccid,htr_cr,htr_slot,htr_tb);
382  DetId did=emap.lookupTrigger(eid);
383  if (did.null()) {
384  report.countUnmappedTPDigi(eid);
385  if (unknownIdsTrig_.find(eid)==unknownIdsTrig_.end()) {
386  if (!silent) edm::LogWarning("HCAL") << "HcalUnpacker: No trigger primitive match found for electronics id :" << eid;
387  unknownIdsTrig_.insert(eid);
388  }
389  valid=false;
390  continue;
391  } else if (did==HcalTrigTowerDetId::Undefined ||
392  (did.det()==DetId::Hcal && did.subdetId()==0)) {
393  // known to be unmapped
394  valid=false;
395  continue;
396  }
397  HcalTrigTowerDetId id(did);
398  colls.tpCont->push_back(HcalTriggerPrimitiveDigi(id));
399  // set the various bits
400  if (!tpgSOIbitInUse) colls.tpCont->back().setPresamples(nps);
401  colls.tpCont->back().setZSInfo(htr.isUnsuppressed(),htr.wasMarkAndPassZSTP(slb(tp_work->raw()),slbChan(tp_work->raw())));
402 
403  // no hits recorded for current
404  ncurr=0;
405  valid=true;
406  }
407  // add the word (if within settings or recent firmware [recent firmware ignores startSample/endSample])
408  if (valid && ((tpgSOIbitInUse && ncurr<10) || (ncurr>=startSample_ && ncurr<=endSample_))) {
409  colls.tpCont->back().setSample(colls.tpCont->back().size(),*tp_work);
410  colls.tpCont->back().setSize(colls.tpCont->back().size()+1);
411  }
412  // set presamples,if SOI
413  if (valid && tpgSOIbitInUse && isTPGSOI(*tp_work)) {
414  colls.tpCont->back().setPresamples(ncurr);
415  }
416  ncurr++;
417  npre++;
418  }
419  }
420 
423 
424  qie_begin=(const HcalQIESample*)daq_first;
425  qie_end=(const HcalQIESample*)(daq_last+1); // one beyond last..
426 
428 
429 
430  for (qie_work=qie_begin; qie_work!=qie_end; ) {
431  if (qie_work->raw()==0xFFFF) {
432  qie_work++;
433  continue; // filler word
434  }
435  // always at the beginning ...
436 
437  // lookup the right channel
438  HcalElectronicsId eid(qie_work->fiberChan(),qie_work->fiber(),spigot,dccid);
439  eid.setHTR(htr_cr,htr_slot,htr_tb);
440  DetId did=emap.lookup(eid);
441 
442  if (!did.null()) {
443  if (did.det()==DetId::Calo && did.subdetId()==HcalZDCDetId::SubdetectorId) {
444  colls.zdcCont->push_back(ZDCDataFrame(HcalZDCDetId(did)));
445  qie_work=HcalUnpacker_impl::unpack<ZDCDataFrame>(qie_work, qie_end, colls.zdcCont->back(), nps, eid, startSample_, endSample_, expectedOrbitMessageTime_, htr);
446  } else if (did.det()==DetId::Hcal) {
447  switch (((HcalSubdetector)did.subdetId())) {
448  case (HcalBarrel):
449  case (HcalEndcap): {
450  colls.hbheCont->push_back(HBHEDataFrame(HcalDetId(did)));
451  qie_work=HcalUnpacker_impl::unpack<HBHEDataFrame>(qie_work, qie_end, colls.hbheCont->back(), nps, eid, startSample_, endSample_, expectedOrbitMessageTime_, htr);
452  } break;
453  case (HcalOuter): {
454  colls.hoCont->push_back(HODataFrame(HcalDetId(did)));
455  qie_work=HcalUnpacker_impl::unpack<HODataFrame>(qie_work, qie_end, colls.hoCont->back(), nps, eid, startSample_, endSample_, expectedOrbitMessageTime_, htr);
456  } break;
457  case (HcalForward): {
458  colls.hfCont->push_back(HFDataFrame(HcalDetId(did)));
459  qie_work=HcalUnpacker_impl::unpack<HFDataFrame>(qie_work, qie_end, colls.hfCont->back(), nps, eid, startSample_, endSample_, expectedOrbitMessageTime_, htr);
460  } break;
461  case (HcalOther) : {
462  HcalOtherDetId odid(did);
463  if (odid.subdet()==HcalCalibration) {
464  colls.calibCont->push_back(HcalCalibDataFrame(HcalCalibDetId(did)));
465  qie_work=HcalUnpacker_impl::unpack<HcalCalibDataFrame>(qie_work, qie_end, colls.calibCont->back(), nps, eid, startSample_, endSample_, expectedOrbitMessageTime_, htr);
466  }
467  } break;
468  case (HcalEmpty):
469  default: {
470  for (int fiberC=qie_work->fiberAndChan();
471  qie_work!=qie_end && qie_work->fiberAndChan()==fiberC;
472  qie_work++);
473  }
474  break;
475  }
476  }
477  } else {
478  report.countUnmappedDigi(eid);
479  if (unknownIds_.find(eid)==unknownIds_.end()) {
480  if (!silent) edm::LogWarning("HCAL") << "HcalUnpacker: No match found for electronics id :" << eid;
481  unknownIds_.insert(eid);
482  }
483  for (int fiberC=qie_work->fiberAndChan();
484  qie_work!=qie_end && qie_work->fiberAndChan()==fiberC;
485  qie_work++);
486  }
487  }
488  } else {
489  // this is the branch for unpacking the compact data format with per-channel headers
490  const unsigned short* ptr_header=daq_first;
491  const unsigned short* ptr_end=daq_last+1;
492  int flavor, error_flags, capid0, channelid;
493 
494  while (ptr_header!=ptr_end) {
495  if (*ptr_header==0xFFFF) { // impossible filler word
496  ptr_header++;
497  continue;
498  }
499  // unpack the header word
500  bool isheader=HcalHTRData::unpack_per_channel_header(*ptr_header,flavor,error_flags,capid0,channelid);
501  if (!isheader) {
502  ptr_header++;
503  continue;
504  }
505 
506  int fiberchan=channelid&0x3;
507  int fiber=((channelid>>2)&0x7)+1;
508 
509  // lookup the right channel
510  HcalElectronicsId eid(fiberchan,fiber,spigot,dccid);
511  eid.setHTR(htr_cr,htr_slot,htr_tb);
512  DetId did=emap.lookup(eid);
513 
514  if (!did.null()) {
515  if (did.det()==DetId::Calo && did.subdetId()==HcalZDCDetId::SubdetectorId) {
516  colls.zdcCont->push_back(ZDCDataFrame(HcalZDCDetId(did)));
517  ptr_header=HcalUnpacker_impl::unpack_compact<ZDCDataFrame>(ptr_header, ptr_end, colls.zdcCont->back(), nps, eid, startSample_, endSample_, expectedOrbitMessageTime_, htr);
518  } else if (did.det()==DetId::Hcal) {
519  switch (((HcalSubdetector)did.subdetId())) {
520  case (HcalBarrel):
521  case (HcalEndcap): {
522  colls.hbheCont->push_back(HBHEDataFrame(HcalDetId(did)));
523  ptr_header=HcalUnpacker_impl::unpack_compact<HBHEDataFrame>(ptr_header, ptr_end, colls.hbheCont->back(), nps, eid, startSample_, endSample_, expectedOrbitMessageTime_, htr);
524  } break;
525  case (HcalOuter): {
526  colls.hoCont->push_back(HODataFrame(HcalDetId(did)));
527  ptr_header=HcalUnpacker_impl::unpack_compact<HODataFrame>(ptr_header, ptr_end, colls.hoCont->back(), nps, eid, startSample_, endSample_, expectedOrbitMessageTime_, htr);
528  } break;
529  case (HcalForward): {
530  colls.hfCont->push_back(HFDataFrame(HcalDetId(did)));
531  ptr_header=HcalUnpacker_impl::unpack_compact<HFDataFrame>(ptr_header, ptr_end, colls.hfCont->back(), nps, eid, startSample_, endSample_, expectedOrbitMessageTime_, htr);
532  } break;
533  case (HcalOther) : {
534  HcalOtherDetId odid(did);
535  if (odid.subdet()==HcalCalibration) {
536  colls.calibCont->push_back(HcalCalibDataFrame(HcalCalibDetId(did)));
537  ptr_header=HcalUnpacker_impl::unpack_compact<HcalCalibDataFrame>(ptr_header, ptr_end, colls.calibCont->back(), nps, eid, startSample_, endSample_, expectedOrbitMessageTime_, htr);
538  }
539  } break;
540  case (HcalEmpty):
541  default: {
542  for (ptr_header++;
543  ptr_header!=ptr_end && !HcalHTRData::is_channel_header(*ptr_header);
544  ptr_header++);
545  }
546  break;
547  }
548  }
549  } else {
550  report.countUnmappedDigi(eid);
551  if (unknownIds_.find(eid)==unknownIds_.end()) {
552  if (!silent) edm::LogWarning("HCAL") << "HcalUnpacker: No match found for electronics id :" << eid;
553  unknownIds_.insert(eid);
554  }
555  for (ptr_header++;
556  ptr_header!=ptr_end && !HcalHTRData::is_channel_header(*ptr_header);
557  ptr_header++);
558  }
559  }
560 
561  }
562  }
563 }
564 
566  Collections& colls, HcalUnpackerReport& report, bool silent) {
567 
568  const hcal::AMC13Header* amc13=(const hcal::AMC13Header*)(raw.data());
569 
570  // how many AMC in this packet
571  int namc=amc13->NAMC();
572  for (int iamc=0; iamc<namc; iamc++) {
573  // if not enabled, ignore
574  if (!amc13->AMCEnabled(iamc)) continue;
575 
576  if (!amc13->AMCDataPresent(iamc)) {
577  if (!silent)
578  edm::LogWarning("Invalid Data") << "Missing data observed on iamc " << iamc << " of AMC13 with source id " << amc13->sourceId();
579  report.countSpigotFormatError();
580  continue;
581  }
582  if (!amc13->AMCCRCOk(iamc)) {
583  if (!silent)
584  edm::LogWarning("Invalid Data") << "CRC Error on uHTR data observed on iamc " << iamc << " of AMC13 with source id " << amc13->sourceId();
585  report.countSpigotFormatError();
586  // continue;
587  }
588  // this unpacker cannot handle segmented data!
589  if (amc13->AMCSegmented(iamc)) {
590  if (!silent)
591  edm::LogWarning("Invalid Data") << "Unpacker cannot handle segmented data observed on iamc " << iamc << " of AMC13 with source id " << amc13->sourceId();
592  report.countSpigotFormatError();
593  continue;
594  }
595 
596  // ok, now we're work-able
597  int slot=amc13->AMCSlot(iamc);
598  int crate=amc13->AMCId(iamc)&0xFF;
599  // this is used only for the 1.6 Gbps link data
600  int nps=(amc13->AMCId(iamc)>>12)&0xF;
601 
602  HcalUHTRData uhtr(amc13->AMCPayload(iamc),amc13->AMCSize(iamc));
603  //Check to make sure uMNio is not unpacked here
604  if(uhtr.getFormatVersion() != 1) {
605  unpackUMNio(raw, slot, colls);
606  continue;
607  }
608 #ifdef DebugLog
609  //debug printouts
610  int nwords=uhtr.getRawLengthBytes()/2;
611  for (int iw=0; iw<nwords; iw++)
612  printf("%04d %04x\n",iw,uhtr.getRawData16()[iw]);
613 #endif
614 
615  //use uhtr presamples since amc header not properly packed in simulation
616  nps = uhtr.presamples();
617  HcalUHTRData::const_iterator i=uhtr.begin(), iend=uhtr.end();
618  while (i!=iend) {
619 #ifdef DebugLog
620  std::cout << "This data is flavored:" << i.flavor() << std::endl;
621 #endif
622 
623  if (!i.isHeader()) {
624  ++i;
625 #ifdef DebugLog
626  std::cout << "its not a header" << std::endl;
627 #endif
628  continue;
629  }
631  if (i.flavor() == 1 || i.flavor() == 0) {
632  int ifiber=((i.channelid()>>3)&0x1F);
633  int ichan=(i.channelid()&0x7);
634  HcalElectronicsId eid(crate,slot,ifiber,ichan, false);
635  DetId did=emap.lookup(eid);
636  // Count from current position to next header, or equal to end
637  const uint16_t* head_pos = i.raw();
638  int ns = 0;
639  for (++i; i != iend && !i.isHeader(); ++i) {
640  ns++;
641  }
642  // Check QEI11 container exists
643  if (colls.qie11 == nullptr) {
644  colls.qie11 = new QIE11DigiCollection(ns);
645  }
646  else if (colls.qie11->samples() != ns) {
647  // This is horrible
648  edm::LogError("Invalid Data") << "QIE11 Collection has " << colls.qie11->samples() << " samples per digi, raw data has " << ns << "!";
649  return;
650  }
651 
652  // Insert data
654  if (!did.null()) { // unpack and store...
655  colls.qie11->addDataFrame(did, head_pos);
656  } else {
657  report.countUnmappedDigi(eid);
658  if (unknownIds_.find(eid)==unknownIds_.end()) {
659  if (!silent) edm::LogWarning("HCAL") << "HcalUnpacker: No match found for electronics id :" << eid;
660  unknownIds_.insert(eid);
661 #ifdef DebugLog
662  std::cout << "HcalUnpacker: No match found for electronics id :" << eid << std::endl;
663 #endif
664  }
665 #ifdef DebugLog
666  std::cout << "OH NO! detector id is null!" << std::endl;
667 #endif
668  }
669  } else if (i.flavor() == 2){
671 
672  int ifiber=((i.channelid()>>3)&0x1F);
673  int ichan=(i.channelid()&0x7);
674  HcalElectronicsId eid(crate,slot,ifiber,ichan, false);
675  DetId did=emap.lookup(eid);
676 
677  // Count from current position to next header, or equal to end
678  const uint16_t* head_pos = i.raw();
679  int ns = 0;
680  for (++i; i != iend && !i.isHeader(); ++i) {
681  ns++;
682  }
683 
684  // Check QEI10 container exists
685  if (colls.qie10ZDC == nullptr) {
686  colls.qie10ZDC = new QIE10DigiCollection(ns);
687  }
688  else if (colls.qie10ZDC->samples() != ns) {
689  // This is horrible
690  edm::LogError("Invalid Data") << "QIE10ZDC Collection has " << colls.qie10ZDC->samples() << " samples per digi, raw data has " << ns << "!";
691  return;
692  }
693 
694  if (colls.qie10 == nullptr) {
695  colls.qie10 = new QIE10DigiCollection(ns);
696  }
697  else if (colls.qie10->samples() != ns) {
698  // This is horrible
699  edm::LogError("Invalid Data") << "QIE10 Collection has " << colls.qie10->samples() << " samples per digi, raw data has " << ns << "!";
700  return;
701  }
702 
703  // Insert data
705  if (!did.null() && did.det()==DetId::Calo && did.subdetId()==HcalZDCDetId::SubdetectorId) { // unpack and store...
706  colls.qie10ZDC->addDataFrame(did, head_pos);
707  }
708  else if (!did.null()) { // unpack and store...
709  colls.qie10->addDataFrame(did, head_pos);
710  } else {
711  report.countUnmappedDigi(eid);
712  if (unknownIds_.find(eid)==unknownIds_.end()) {
713  if (!silent) edm::LogWarning("HCAL") << "HcalUnpacker: No match found for electronics id :" << eid;
714  unknownIds_.insert(eid);
715 #ifdef DebugLog
716  std::cout << "HcalUnpacker: No match found for electronics id :" << eid << std::endl;
717 #endif
718  }
719 #ifdef DebugLog
720  std::cout << "OH NO! HcalUnpacker: No match found for electronics id :" << eid << std::endl;
721 #endif
722  }
723  }
724  else if (i.flavor()==5 || (i.flavor()==7 && i.technicalDataType()==15)) { // Old-style digis
725  int ifiber=((i.channelid()>>2)&0x1F);
726  int ichan=(i.channelid()&0x3);
727  HcalElectronicsId eid(crate,slot,ifiber,ichan, false);
728  DetId did=emap.lookup(eid);
729 
730  if (!did.null()) { // unpack and store...
731  if (did.det()==DetId::Calo && did.subdetId()==HcalZDCDetId::SubdetectorId) {
732  colls.zdcCont->push_back(ZDCDataFrame(HcalZDCDetId(did)));
733  HcalUnpacker_impl::unpack_compact<ZDCDataFrame>(i,iend, colls.zdcCont->back(), nps, eid, startSample_, endSample_);
734  } else if (did.det()==DetId::Hcal) {
735  switch (((HcalSubdetector)did.subdetId())) {
736  case (HcalBarrel):
737  case (HcalEndcap): {
738  colls.hbheCont->push_back(HBHEDataFrame(HcalDetId(did)));
739  HcalUnpacker_impl::unpack_compact<HBHEDataFrame>(i, iend, colls.hbheCont->back(), nps, eid, startSample_, endSample_);
740  } break;
741  case (HcalOuter): {
742  colls.hoCont->push_back(HODataFrame(HcalDetId(did)));
743  HcalUnpacker_impl::unpack_compact<HODataFrame>(i, iend, colls.hoCont->back(), nps, eid, startSample_, endSample_);
744  } break;
745  case (HcalForward): {
746  colls.hfCont->push_back(HFDataFrame(HcalDetId(did)));
747  HcalUnpacker_impl::unpack_compact<HFDataFrame>(i, iend, colls.hfCont->back(), nps, eid, startSample_, endSample_);
748  } break;
749  case (HcalOther) : {
750  HcalOtherDetId odid(did);
751  if (odid.subdet()==HcalCalibration) {
752  colls.calibCont->push_back(HcalCalibDataFrame(HcalCalibDetId(did)));
753  HcalUnpacker_impl::unpack_compact<HcalCalibDataFrame>(i, iend, colls.calibCont->back(), nps, eid, startSample_, endSample_);
754  }
755  } break;
756  case (HcalEmpty):
757  default: {
758  for (++i;
759  i!=iend && !i.isHeader();
760  ++i);
761  }
762  break;
763  }
764  }
765  } else {
766  report.countUnmappedDigi(eid);
767  if (unknownIds_.find(eid)==unknownIds_.end()) {
768  if (!silent) edm::LogWarning("HCAL") << "HcalUnpacker: No match found for electronics id :" << eid;
769  unknownIds_.insert(eid);
770  }
771  for (++i;
772  i!=iend && !i.isHeader();
773  ++i);
774  }
775  } else if (i.flavor()==0x4) { // TP digis
776  int ilink=((i.channelid()>>4)&0xF);
777  int itower=(i.channelid()&0xF);
778  HcalElectronicsId eid(crate,slot,ilink,itower,true);
779  DetId did=emap.lookupTrigger(eid);
780 #ifdef DebugLog
781  std::cout << "Unpacking " << eid << " " << i.channelid() << std::endl;
782 #endif
783  if (did.null()) {
784  report.countUnmappedTPDigi(eid);
785  if (unknownIdsTrig_.find(eid)==unknownIdsTrig_.end()) {
786  if (!silent) edm::LogWarning("HCAL") << "HcalUnpacker: No trigger primitive match found for electronics id :" << eid;
787  unknownIdsTrig_.insert(eid);
788  }
789  // Skip it
790  for (++i; i!=iend && !i.isHeader(); ++i);
791  } else if (did==HcalTrigTowerDetId::Undefined ||
792  (did.det()==DetId::Hcal && did.subdetId()==0)) {
793  for (++i; i!=iend && !i.isHeader(); ++i);
794  } else {
795  HcalTrigTowerDetId id(did);
796 #ifdef DebugLog
797  std::cout << "Unpacking " << id << std::endl;
798 #endif
799  colls.tpCont->push_back(HcalTriggerPrimitiveDigi(id));
800  int j=0;
801  for (++i; i!=iend && !i.isHeader(); ++i) {
802  colls.tpCont->back().setSample(j,i.value());
803  if (i.soi()) colls.tpCont->back().setPresamples(j);
804  j++;
805  }
806  colls.tpCont->back().setSize(j);
807  }
808  } else {
809  // consume any not-understood channel data
810  for (++i;
811  i!=iend && !i.isHeader();
812  ++i);
813  }
814  }
815  }
816 }
817 
819  hbheCont=nullptr;
820  hoCont=nullptr;
821  hfCont=nullptr;
822  tpCont=nullptr;
823  zdcCont=nullptr;
824  calibCont=nullptr;
825  ttp=nullptr;
826  qie10=nullptr;
827  qie10ZDC=nullptr;
828  qie11=nullptr;
829  umnio=nullptr;
830 }
831 
832 void HcalUnpacker::unpack(const FEDRawData& raw, const HcalElectronicsMap& emap, std::vector<HcalHistogramDigi>& histoDigis) {
833 
834  // get the DCC header
835  const HcalDCCHeader* dccHeader=(const HcalDCCHeader*)(raw.data());
836  int dccid=dccHeader->getSourceId()-sourceIdOffset_;
837 
838  // check the summary status
839 
840  // walk through the HTR data...
841  HcalHTRData htr;
842  for (int spigot=0; spigot<HcalDCCHeader::SPIGOT_COUNT; spigot++) {
843  if (!dccHeader->getSpigotPresent(spigot)) continue;
844 
845  int retval=dccHeader->getSpigotData(spigot,htr,raw.size());
846  // check
847  if (retval || !htr.check()) {
848  edm::LogWarning("Invalid Data") << "Invalid HTR data observed on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
849  continue;
850  }
851  if (!htr.isHistogramEvent()) {
852  edm::LogWarning("Invalid Data") << "Non-histogram data passed to histogram unpacker on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
853  continue;
854  }
855 
856  unsigned int smid=htr.getSubmodule();
857  int htr_tb=smid&0x1;
858  int htr_slot=(smid>>1)&0x1F;
859  int htr_cr=(smid>>6)&0x1F;
860 
861  // find out the fibers
862  int f[2],fc;
863  htr.getHistogramFibers(f[0],f[1]);
864 
865  for (int nf=0; nf<2; nf++) {
866  if (f[nf]<0 || (nf==1 && f[0]==f[1])) continue; // skip if invalid or the same
867  for (fc=0; fc<=2; fc++) {
868  HcalElectronicsId eid(fc,f[nf],spigot,dccid);
869  eid.setHTR(htr_cr,htr_slot,htr_tb);
870  DetId did=emap.lookup(eid);
871 
872  if (did.null() || did.det()!=DetId::Hcal || did.subdetId()==0) {
873  if (unknownIds_.find(eid)==unknownIds_.end()) {
874  edm::LogWarning("HCAL") << "HcalHistogramUnpacker: No match found for electronics id :" << eid;
875  unknownIds_.insert(eid);
876  }
877  continue;
878  }
879  histoDigis.push_back(HcalHistogramDigi(HcalDetId(did))); // add it!
880  HcalHistogramDigi& digi=histoDigis.back();
881 
882  // unpack the four capids
883  for (int capid=0; capid<4; capid++)
884  htr.unpackHistogram(f[nf],fc,capid,digi.getArray(capid));
885 
886  }
887  }
888  }
889 }
890 // Method to unpack uMNio data
891 void HcalUnpacker::unpackUMNio(const FEDRawData& raw, int slot, Collections& colls) {
892  const hcal::AMC13Header* amc13=(const hcal::AMC13Header*)(raw.data());
893  int namc=amc13->NAMC();
894  //Find AMC corresponding to uMNio slot
895  for (int iamc=0; iamc<namc; iamc++) {
896  if (amc13->AMCSlot(iamc) == slot) namc = iamc;
897  }
898  if (namc==amc13->NAMC()) {
899  return;
900  }
901  const uint16_t* data = (const uint16_t*)(amc13->AMCPayload(namc));
902  size_t nwords = amc13->AMCSize(namc) * ( sizeof(uint64_t) / sizeof(uint16_t) );
903  *(colls.umnio) = HcalUMNioDigi(data, nwords);
904 
905 }
#define LogDebug(id)
int getSourceId() const
Definition: HcalDTCHeader.h:35
uint16_t raw() const
get the raw word
Definition: HcalQIESample.h:20
bool check() const
Check for a good event Requires a minimum length, matching wordcount and length, not an empty event...
Definition: HcalHTRData.cc:62
static int slbAndChan(uint16_t theSample)
void unpackUTCA(const FEDRawData &raw, const HcalElectronicsMap &emap, Collections &conts, HcalUnpackerReport &report, bool silent=false)
std::vector< HcalTTPDigi > * ttp
Definition: HcalUnpacker.h:33
std::vector< HFDataFrame > * hfCont
Definition: HcalUnpacker.h:28
int fiberAndChan() const
get the id channel
Definition: HcalQIESample.h:36
HcalUMNioDigi * umnio
Definition: HcalUnpacker.h:37
int fiberChan() const
get the fiber channel number
Definition: HcalQIESample.h:34
bool getSpigotCRCError(unsigned int nspigot) const
Read the "CRC-Mismatch" bit for this spigot.
void addDataFrame(DetId detid, const uint16_t *data)
uint16_t value() const
Definition: HcalUHTRData.h:52
static bool isTPGSOI(const HcalTriggerPrimitiveSample &s)
const unsigned short * unpack_compact(const unsigned short *startPoint, const unsigned short *limit, DigiClass &digi, int presamples, const HcalElectronicsId &eid, int startSample, int endSample, int expectedTime, const HcalHTRData &hhd)
Definition: HcalUnpacker.cc:52
std::vector< HBHEDataFrame > * hbheCont
Definition: HcalUnpacker.h:26
std::vector< HOTriggerPrimitiveDigi > * tphoCont
Definition: HcalUnpacker.h:32
bool wasMarkAndPassZS(int fiber, int fiberchan) const
Was this channel passed as part of Mark&Pass ZS?
Definition: HcalHTRData.cc:356
unsigned int getFibOrbMsgBCN(int fiber) const
Get the BCN of the Fiber Orbit Messages.
Definition: HcalHTRData.h:178
static bool unpack_per_channel_header(unsigned short, int &flav, int &error_flags, int &capid0, int &channelid)
Unpack a per-channel header word (compact format)
Definition: HcalHTRData.cc:433
void getHistogramFibers(int &a, int &b) const
Get the fiber numbers for the data present in this event (only in histogram mode!) ...
Definition: HcalHTRData.cc:413
int getSpigotData(int nspigot, HcalHTRData &decodeTool, int validSize) const
int getFormatVersion() const
Get the version number of this event.
Definition: HcalHTRData.h:33
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
int AMCSize(int i) const
Definition: AMC13Header.h:37
HcalDataFrameContainer< QIE10DataFrame > QIE10DigiCollection
bool isUnsuppressed() const
Is this event an unsuppresed event?
Definition: HcalHTRData.cc:353
bool isOverflowWarning() const
Definition: HcalHTRData.cc:109
bool unpackHistogram(int fiber, int fiberchan, int capid, unsigned short *histogram) const
Unpack special histogramming mode data.
Definition: HcalHTRData.cc:441
static bool is_channel_header(unsigned short value)
check top bit to see if this is a compact format channel header word
Definition: HcalHTRData.h:90
QIE10DigiCollection * qie10ZDC
Definition: HcalUnpacker.h:35
bool isEmptyEvent() const
Definition: HcalHTRData.cc:100
bool AMCDataPresent(int i) const
Definition: AMC13Header.h:47
int ieta() const
get the cell ieta
Definition: HcalDetId.h:56
HcalSubdetector
Definition: HcalAssistant.h:31
uint16_t raw() const
get the raw word
void unpackVME(const FEDRawData &raw, const HcalElectronicsMap &emap, Collections &conts, HcalUnpackerReport &report, bool silent=false)
double f[11][100]
std::vector< HcalTriggerPrimitiveDigi > * tpCont
Definition: HcalUnpacker.h:31
int fiber() const
get the fiber number
Definition: HcalQIESample.h:32
bool getSpigotPresent(unsigned int nspigot) const
Read the "PRESENT" bit for this spigot.
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
bool getSlotPresent(unsigned int nslot) const
Read the "PRESENT" bit for this slot.
Definition: HcalDTCHeader.h:80
HcalDataFrameContainer< QIE11DataFrame > QIE11DigiCollection
int getSourceId() const
Definition: HcalDCCHeader.h:32
int iphi() const
get the cell iphi
Definition: HcalDetId.cc:103
unsigned int databits
Definition: DetId.h:18
void setHTR(int crate, int slot, int tb)
void unpack(const FEDRawData &raw, const HcalElectronicsMap &emap, std::vector< HcalHistogramDigi > &histoDigis)
static const int SubdetectorId
Definition: HcalZDCDetId.h:25
std::vector< HcalCalibDataFrame > * calibCont
Definition: HcalUnpacker.h:29
static int slb(uint16_t theSample)
unsigned long long uint64_t
Definition: Time.h:15
HcalOtherSubdetector subdet() const
get the category
void dataPointers(const unsigned short **daq_first, const unsigned short **daq_last, const unsigned short **tp_first, const unsigned short **tp_last) const
Obtain the starting and ending pointers for external unpacking of the data.
Definition: HcalHTRData.cc:153
bool unpack(const HcalHTRData &data, HcalTTPDigi &digi)
int sourceId() const
Definition: AMC13Header.h:15
bool null() const
is this a null id ?
Definition: DetId.h:45
std::vector< HODataFrame > * hoCont
Definition: HcalUnpacker.h:27
QIE11DigiCollection * qie11
Definition: HcalUnpacker.h:36
void setbit(int i)
short BOEshouldBeZeroAlways() const
Definition: HcalDCCHeader.h:50
int getNPS() const
Get the number of presamples in daq data.
Definition: HcalHTRData.cc:400
static const HcalTrigTowerDetId Undefined
bool AMCSegmented(int i) const
Definition: AMC13Header.h:41
static const int FORMAT_VERSION_COMPACT_DATA
Definition: HcalHTRData.h:20
int getFirmwareFlavor() const
Get the HTR firmware flavor.
Definition: HcalHTRData.cc:409
int NAMC() const
Definition: AMC13Header.h:25
static const int SPIGOT_COUNT
Definition: HcalDCCHeader.h:19
int AMCSlot(int i) const
Definition: AMC13Header.h:33
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
const uint64_t * AMCPayload(int i) const
Definition: AMC13Header.cc:4
int getSlotData(int nslot, HcalHTRData &decodeTool, int validSize) const
bool wasMarkAndPassZSTP(int slb, int slbchan) const
Was this channel passed as part of Mark&Pass ZS?
Definition: HcalHTRData.cc:364
uint16_t * getArray(int capid)
get the array for the specified capid
uint32_t zsBunchMask() const
ZS Bunch Mask (if available)
Definition: HcalHTRData.cc:373
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
QIE10DigiCollection * qie10
Definition: HcalUnpacker.h:34
std::vector< ZDCDataFrame > * zdcCont
Definition: HcalUnpacker.h:30
bool AMCCRCOk(int i) const
Definition: AMC13Header.h:45
const DetId lookupTrigger(HcalElectronicsId fId) const
brief lookup the trigger logical detid associated with the given electronics id
short getDCCDataFormatVersion() const
Definition: HcalDCCHeader.h:57
unsigned int getSubmodule() const
Get the HTR submodule number.
Definition: HcalHTRData.cc:331
bool AMCEnabled(int i) const
Definition: AMC13Header.h:51
bool isBusy() const
Definition: HcalHTRData.cc:117
float linear(float x)
void unpackUMNio(const FEDRawData &raw, int slot, Collections &colls)
uint16_t AMCId(int i) const
Definition: AMC13Header.h:31
Detector det() const
get the detector field from this detid
Definition: DetId.h:35
const uint16_t * raw() const
Definition: HcalUHTRData.h:68
Readout chain identification for Hcal.
const DetId lookup(HcalElectronicsId fId) const
lookup the logical detid associated with the given electronics id
bool isHistogramEvent() const
Is this event a histogram event? (do not call standard unpack in this case!!!!!)
Definition: HcalHTRData.cc:385
static const int MAXIMUM_SLOT
Definition: HcalDTCHeader.h:22
const HcalQIESample * unpack(const HcalQIESample *startPoint, const HcalQIESample *limit, DigiClass &digi, int presamples, const HcalElectronicsId &eid, int startSample, int endSample, int expectedTime, const HcalHTRData &hhd)
Definition: HcalUnpacker.cc:17
bool getSlotCRCError(unsigned int nslot) const
Read the "CRC-Mismatch" bit for this slot.
Definition: HcalDTCHeader.h:84
static int slbChan(uint16_t theSample)