CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CastorUnpacker.cc
Go to the documentation of this file.
9 #include <iostream>
11 
12 namespace CastorUnpacker_impl {
13  template <class DigiClass>
14  const HcalQIESample* unpack(const HcalQIESample* startPoint, const HcalQIESample* limit, DigiClass& digi, int presamples, const CastorElectronicsId& eid, int startSample, int endSample, int expectedTime, const HcalHTRData& hhd) {
15  // set parameters
16  digi.setPresamples(presamples);
17  int fiber=startPoint->fiber();
18  int fiberchan=startPoint->fiberChan();
19  uint32_t zsmask=hhd.zsBunchMask()>>startSample;
20  digi.setZSInfo(hhd.isUnsuppressed(),hhd.wasMarkAndPassZS(fiber,fiberchan),zsmask);
21 
22  // digi.setReadoutIds(eid);
23  // setReadoutIds is missing in CastorDataFrame class digi.setReadoutIds(eid);
24  if (expectedTime>=0 && !hhd.isUnsuppressed()) {
25  // std::cout << hhd.getFibOrbMsgBCN(fiber) << " " << expectedTime << " fiber="<<fiber<< std::endl;
26  digi.setFiberIdleOffset(hhd.getFibOrbMsgBCN(fiber)-expectedTime);
27  }
28  // what is my sample number?
29  int myFiberChan=startPoint->fiberAndChan();
30  int ncurr=0,ntaken=0;
31  const HcalQIESample* qie_work=startPoint;
32  while (qie_work!=limit && qie_work->fiberAndChan()==myFiberChan) {
33  if (ncurr>=startSample && ncurr<=endSample) {
34  digi.setSample(ntaken,*qie_work);
35  ++ntaken;
36  }
37  ncurr++;
38  qie_work++;
39  }
40  digi.setSize(ntaken);
41  return qie_work;
42  }
43 }
44 
45 namespace { inline bool isTPGSOI(const HcalTriggerPrimitiveSample& s) {
46  return (s.raw()&0x200)!=0;
47 }
48 }
49 
50 
51 CastorUnpacker::CastorUnpacker(int sourceIdOffset, int beg, int end) : sourceIdOffset_(sourceIdOffset) , expectedOrbitMessageTime_(-1)
52 {
53  if ( beg >= 0 && beg <= CastorDataFrame::MAXSAMPLES -1 ) {
54  startSample_ = beg;
55  } else {
56  startSample_ = 0;
57  }
58  if ( end >= 0 && end <= CastorDataFrame::MAXSAMPLES -1 && end >= beg ) {
59  endSample_ = end;
60  } else {
62  }
63 }
64 
65 
67  CastorRawCollections& colls, HcalUnpackerReport& report, bool silent) {
68 
69  if (raw.size()<16) {
70  if (!silent) edm::LogWarning("Invalid Data") << "Empty/invalid DCC data, size = " << raw.size();
71  return;
72  }
73 
74  // get the DCC header
75  const HcalDCCHeader* dccHeader=(const HcalDCCHeader*)(raw.data());
76  int dccid=dccHeader->getSourceId()-sourceIdOffset_;
77 
78  // check the summary status
79 
80  // walk through the HTR data...
81  HcalHTRData htr;
82  const unsigned short* daq_first, *daq_last, *tp_first, *tp_last;
83  const HcalQIESample* qie_begin, *qie_end, *qie_work;
84  const HcalTriggerPrimitiveSample *tp_begin, *tp_end, *tp_work;
85  for (int spigot=0; spigot<HcalDCCHeader::SPIGOT_COUNT; spigot++) {
86  if (!dccHeader->getSpigotPresent(spigot)) continue;
87 
88  int retval=dccHeader->getSpigotData(spigot,htr,raw.size());
89  if (retval!=0) {
90  if (retval==-1) {
91  if (!silent) edm::LogWarning("Invalid Data") << "Invalid HTR data (data beyond payload size) observed on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
92  report.countSpigotFormatError();
93  }
94  continue;
95  }
96  // check
97  if (dccHeader->getSpigotCRCError(spigot)) {
98  if (!silent)
99  edm::LogWarning("Invalid Data") << "CRC Error on HTR data observed on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
100  report.countSpigotFormatError();
101  continue;
102  }
103  if (!htr.check()) {
104  if (!silent)
105  edm::LogWarning("Invalid Data") << "Invalid HTR data observed on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
106  report.countSpigotFormatError();
107  continue;
108  }
109  if (htr.isHistogramEvent()) {
110  if (!silent)
111  edm::LogWarning("Invalid Data") << "Histogram data passed to non-histogram unpacker on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
112  continue;
113 
114  }
115  if ((htr.getFirmwareFlavor()&0xE0)==0x80) { // some kind of TTP data
116  if (colls.ttp!=0) {
117  HcalTTPUnpacker ttpUnpack;
118  colls.ttp->push_back(HcalTTPDigi());
119  ttpUnpack.unpack(htr,colls.ttp->back());
120  } else {
121  LogDebug("CastorUnpackerHcalTechTrigProcessor") << "Skipping data on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId() << " which is from the TechTrigProcessor (use separate unpacker!)";
122  }
123  continue;
124  }
125  if (htr.getFirmwareFlavor()>=0x80) {
126  if (!silent) edm::LogWarning("CastorUnpacker") << "Skipping data on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId() << " which is of unknown flavor " << htr.getFirmwareFlavor();
127  continue;
128  }
129  // calculate "real" number of presamples
130  int nps=htr.getNPS()-startSample_;
131 
132  // get pointers
133  htr.dataPointers(&daq_first,&daq_last,&tp_first,&tp_last);
134  unsigned int smid=htr.getSubmodule();
135  int htr_tb=smid&0x1;
136  int htr_slot=(smid>>1)&0x1F;
137  int htr_cr=(smid>>6)&0x1F;
138 
139  tp_begin=(HcalTriggerPrimitiveSample*)tp_first;
140  tp_end=(HcalTriggerPrimitiveSample*)(tp_last+1); // one beyond last..
141 
143  int currFiberChan=0x3F; // invalid fiber+channel...
144  int ncurr=0;
145  bool valid=false;
147  bool tpgSOIbitInUse=htr.getFormatVersion()>=3; // version 3 and later
148  // bool isHOtpg=htr.getFormatVersion()>=3 && htr.getFirmwareFlavor()==0; // HO is flavor zero
149  int npre=0;
150  /*
151  Unpack the trigger primitives
152  */
153  // lookup the right channel
154  bool dotp = true;
155  CastorElectronicsId eid(0,1,spigot,dccid);
156  eid.setHTR(htr_cr,htr_slot,htr_tb);
157  DetId did=emap.lookup(eid);
158  if ( did.null() ) dotp = false;
159  HcalCastorDetId id1(did);
160  HcalCastorDetId id((id1.zside()==0),id1.sector(),1);
161  if ( id1.module() > 12 ) dotp = false;
162  if ( dotp ) {
163  // std::cout << " tp_first="<< tp_first << " tp_last="<< tp_last<< " tb="<<htr_tb<<" slot="<<htr_slot<<" crate="<<htr_cr<<" dccid="<< dccid<< std::endl;
164  // regular TPs (not HO)
165  for (tp_work=tp_begin; tp_work!=tp_end; tp_work++) {
166  // std::cout << "raw=0x"<<std::hex<< tp_work->raw()<<std::dec <<std::endl;
167  if (tp_work->raw()==0xFFFF) continue; // filler word
168  if (tp_work->slbAndChan()!=currFiberChan) { // start new set
169  npre=0;
170  currFiberChan=tp_work->slbAndChan();
171 
172  // std::cout<< " NEW SET "<<std::endl;
173  //HcalElectronicsId eid(tp_work->slbChan(),tp_work->slb(),spigot,dccid,htr_cr,htr_slot,htr_tb);
174  //DetId did=emap.lookupTrigger(eid);
175  //if (did.null()) {
176  //report.countUnmappedTPDigi(eid);
177  //if (unknownIdsTrig_.find(eid)==unknownIdsTrig_.end()) {
178  //if (!silent) edm::LogWarning("HCAL") << "HcalUnpacker: No trigger primitive match found for electronics id :" << eid;
179  //unknownIdsTrig_.insert(eid);
180  //}
181  //valid=false;
182  //continue;
183  //} else if (did==HcalTrigTowerDetId::Undefined ||
184  //(did.det()==DetId::Hcal && did.subdetId()==0)) {
186  //valid=false;
187  //continue;
188  //}
189 
190  colls.tpCont->push_back(CastorTriggerPrimitiveDigi(id));
191  // set the various bits
192  if (!tpgSOIbitInUse) colls.tpCont->back().setPresamples(nps);
193  colls.tpCont->back().setZSInfo(htr.isUnsuppressed(),htr.wasMarkAndPassZSTP(tp_work->slb(),tp_work->slbChan()));
194 
195  // no hits recorded for current
196  ncurr=0;
197  valid=true;
198  }
199  // add the word (if within settings or recent firmware [recent firmware ignores startSample/endSample])
200  if (valid && ((tpgSOIbitInUse && ncurr<10) || (ncurr>=startSample_ && ncurr<=endSample_))) {
201  colls.tpCont->back().setSample(colls.tpCont->back().size(),*tp_work);
202  colls.tpCont->back().setSize(colls.tpCont->back().size()+1);
203  }
204  // set presamples,if SOI
205  if (valid && tpgSOIbitInUse && isTPGSOI(*tp_work)) {
206  colls.tpCont->back().setPresamples(ncurr);
207  }
208  ncurr++;
209  npre++;
210  }
211  }
212 
214  qie_begin=(HcalQIESample*)daq_first;
215  qie_end=(HcalQIESample*)(daq_last+1); // one beyond last..
216 
218  currFiberChan=0x3F; // invalid fiber+channel...
219  ncurr=0;
220  valid=false;
221 
222  for (qie_work=qie_begin; qie_work!=qie_end; ) {
223  if (qie_work->raw()==0xFFFF) {
224  qie_work++;
225  continue; // filler word
226  }
227  // always at the beginning ...
228  currFiberChan=qie_work->fiberAndChan();
229 
230  // lookup the right channel
231  CastorElectronicsId eid(qie_work->fiberChan(),qie_work->fiber(),spigot,dccid);
232  eid.setHTR(htr_cr,htr_slot,htr_tb);
233  DetId did=emap.lookup(eid);
234 
235  if (!did.null()) {
236  colls.castorCont->push_back(CastorDataFrame(HcalCastorDetId(did)));
237  qie_work=CastorUnpacker_impl::unpack<CastorDataFrame>(qie_work, qie_end, colls.castorCont->back(), nps, eid, startSample_, endSample_, expectedOrbitMessageTime_, htr);
238  } else {
239  report.countUnmappedDigi();
240  if (unknownIds_.find(eid)==unknownIds_.end()) {
241  if (!silent)
242  edm::LogWarning("CASTOR") << "CastorUnpacker: No match found for electronics id :" << eid;
243  unknownIds_.insert(eid);
244  }
245  for (int fiberC=qie_work->fiberAndChan();
246  qie_work!=qie_end && qie_work->fiberAndChan()==fiberC;
247  qie_work++);
248  }
249  }
250  }
251 }
252 
#define LogDebug(id)
uint16_t raw() const
get the raw word
Definition: HcalQIESample.h:22
bool check() const
Check for a good event Requires a minimum length, matching wordcount and length, not an empty event...
Definition: HcalHTRData.cc:64
int sector() const
get the sector (1-16)
int fiberAndChan() const
get the id channel
Definition: HcalQIESample.h:38
int fiberChan() const
get the fiber channel number
Definition: HcalQIESample.h:36
bool getSpigotCRCError(unsigned int nspigot) const
Read the &quot;CRC-Mismatch&quot; bit for this spigot.
void setHTR(int crate, int slot, int tb)
static bool isTPGSOI(const HcalTriggerPrimitiveSample &s)
Definition: HcalUnpacker.cc:93
int module() const
get the module (1-2 for EM, 1-12 for HAD)
bool wasMarkAndPassZS(int fiber, int fiberchan) const
Was this channel passed as part of Mark&amp;Pass ZS?
Definition: HcalHTRData.cc:358
unsigned int getFibOrbMsgBCN(int fiber) const
Get the BCN of the Fiber Orbit Messages.
Definition: HcalHTRData.h:179
int getSpigotData(int nspigot, HcalHTRData &decodeTool, int validSize) const
int slbChan() const
get the slb channel number
int getFormatVersion() const
Get the version number of this event.
Definition: HcalHTRData.h:35
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:49
std::set< CastorElectronicsId > unknownIds_
void unpack(const FEDRawData &raw, const CastorElectronicsMap &emap, CastorRawCollections &conts, HcalUnpackerReport &report, bool silent=false)
For histograms, no begin and end.
bool isUnsuppressed() const
Is this event an unsuppresed event?
Definition: HcalHTRData.cc:355
tuple report
Definition: zeeHLT_cff.py:9
const DetId lookup(CastorElectronicsId fId) const
lookup the logical detid associated with the given electronics id
int slbAndChan() const
get the id channel
CastorUnpacker(int sourceIdOffset, int beg, int end)
for normal data
int zside() const
get the z-side of the cell (1/-1)
uint16_t raw() const
get the raw word
int fiber() const
get the fiber number
Definition: HcalQIESample.h:34
bool getSpigotPresent(unsigned int nspigot) const
Read the &quot;PRESENT&quot; bit for this spigot.
#define end
Definition: vmac.h:38
std::vector< CastorDataFrame > * castorCont
std::vector< CastorTriggerPrimitiveDigi > * tpCont
int getSourceId() const
Definition: HcalDCCHeader.h:34
Definition: DetId.h:20
int sourceIdOffset_
number to subtract from the source id to get the dcc id
int expectedOrbitMessageTime_
Expected orbit bunch time (needed to evaluate time differences)
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:155
bool unpack(const HcalHTRData &data, HcalTTPDigi &digi)
bool null() const
is this a null id ?
Definition: DetId.h:47
int getNPS() const
Get the number of presamples in daq data.
Definition: HcalHTRData.cc:402
int getFirmwareFlavor() const
Get the HTR firmware flavor.
Definition: HcalHTRData.cc:411
int slb() const
get the slb site number
static const int SPIGOT_COUNT
Definition: HcalDCCHeader.h:21
bool wasMarkAndPassZSTP(int slb, int slbchan) const
Was this channel passed as part of Mark&amp;Pass ZS?
Definition: HcalHTRData.cc:366
const HcalQIESample * unpack(const HcalQIESample *startPoint, const HcalQIESample *limit, DigiClass &digi, int presamples, const CastorElectronicsId &eid, int startSample, int endSample, int expectedTime, const HcalHTRData &hhd)
uint32_t zsBunchMask() const
ZS Bunch Mask (if available)
Definition: HcalHTRData.cc:375
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:29
Readout chain identification for Castor Bits for the readout chain : some names need change! [31:26] ...
unsigned int getSubmodule() const
Get the HTR submodule number.
Definition: HcalHTRData.cc:333
static const int MAXSAMPLES
bool isHistogramEvent() const
Is this event a histogram event? (do not call standard unpack in this case!!!!!)
Definition: HcalHTRData.cc:387
int startSample_
first sample from fed raw data to copy
int endSample_
last sample from fed raw data to copy (if present)
std::vector< HcalTTPDigi > * ttp