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 static int slb(const HcalTriggerPrimitiveSample& theSample) { return ((theSample.raw()>>13)&0x7); }
66 static int slbChan(const HcalTriggerPrimitiveSample& theSample) { return (theSample.raw()>>11)&0x3; }
67 static int slbAndChan(const HcalTriggerPrimitiveSample& theSample) { return (theSample.raw()>>11)&0x1F; }
68 
69 
70 
73 
74  if (raw.size()<16) {
75  if (!silent) edm::LogWarning("Invalid Data") << "Empty/invalid DCC data, size = " << raw.size();
76  return;
77  }
78 
79  // get the DCC header
80  const HcalDCCHeader* dccHeader=(const HcalDCCHeader*)(raw.data());
81  int dccid=dccHeader->getSourceId()-sourceIdOffset_;
82 
83  // check the summary status
84 
85  // walk through the HTR data...
86  HcalHTRData htr;
87  const unsigned short* daq_first, *daq_last, *tp_first, *tp_last;
88  const HcalQIESample* qie_begin, *qie_end, *qie_work;
89  const HcalTriggerPrimitiveSample *tp_begin, *tp_end, *tp_work;
90  for (int spigot=0; spigot<HcalDCCHeader::SPIGOT_COUNT; spigot++) {
91  if (!dccHeader->getSpigotPresent(spigot)) continue;
92 
93  int retval=dccHeader->getSpigotData(spigot,htr,raw.size());
94  if (retval!=0) {
95  if (retval==-1) {
96  if (!silent) edm::LogWarning("Invalid Data") << "Invalid HTR data (data beyond payload size) observed on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
97  report.countSpigotFormatError();
98  }
99  continue;
100  }
101  // check
102  if (dccHeader->getSpigotCRCError(spigot)) {
103  if (!silent)
104  edm::LogWarning("Invalid Data") << "CRC Error on HTR data observed on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
105  report.countSpigotFormatError();
106  continue;
107  }
108  if (!htr.check()) {
109  if (!silent)
110  edm::LogWarning("Invalid Data") << "Invalid HTR data observed on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
111  report.countSpigotFormatError();
112  continue;
113  }
114  if (htr.isHistogramEvent()) {
115  if (!silent)
116  edm::LogWarning("Invalid Data") << "Histogram data passed to non-histogram unpacker on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId();
117  continue;
118 
119  }
120  if ((htr.getFirmwareFlavor()&0xE0)==0x80) { // some kind of TTP data
121  if (colls.ttp!=0) {
122  HcalTTPUnpacker ttpUnpack;
123  colls.ttp->push_back(HcalTTPDigi());
124  ttpUnpack.unpack(htr,colls.ttp->back());
125  } else {
126  LogDebug("CastorUnpackerHcalTechTrigProcessor") << "Skipping data on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId() << " which is from the TechTrigProcessor (use separate unpacker!)";
127  }
128  continue;
129  }
130  if (htr.getFirmwareFlavor()>=0x80) {
131  if (!silent) edm::LogWarning("CastorUnpacker") << "Skipping data on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId() << " which is of unknown flavor " << htr.getFirmwareFlavor();
132  continue;
133  }
134  // calculate "real" number of presamples
135  int nps=htr.getNPS()-startSample_;
136 
137  // get pointers
138  htr.dataPointers(&daq_first,&daq_last,&tp_first,&tp_last);
139  unsigned int smid=htr.getSubmodule();
140  int htr_tb=smid&0x1;
141  int htr_slot=(smid>>1)&0x1F;
142  int htr_cr=(smid>>6)&0x1F;
143 
144  tp_begin=(HcalTriggerPrimitiveSample*)tp_first;
145  tp_end=(HcalTriggerPrimitiveSample*)(tp_last+1); // one beyond last..
146 
148  int currFiberChan=0x3F; // invalid fiber+channel...
149  int ncurr=0;
150  bool valid=false;
152  bool tpgSOIbitInUse=htr.getFormatVersion()>=3; // version 3 and later
153  // bool isHOtpg=htr.getFormatVersion()>=3 && htr.getFirmwareFlavor()==0; // HO is flavor zero
154  int npre=0;
155  /*
156  Unpack the trigger primitives
157  */
158  // lookup the right channel
159  bool dotp = true;
160  CastorElectronicsId eid(0,1,spigot,dccid);
161  eid.setHTR(htr_cr,htr_slot,htr_tb);
162  DetId did=emap.lookup(eid);
163  if ( did.null() ) dotp = false;
164  HcalCastorDetId id1(did);
165  HcalCastorDetId id((id1.zside()==0),id1.sector(),1);
166  if ( id1.module() > 12 ) dotp = false;
167  if ( dotp ) {
168  // std::cout << " tp_first="<< tp_first << " tp_last="<< tp_last<< " tb="<<htr_tb<<" slot="<<htr_slot<<" crate="<<htr_cr<<" dccid="<< dccid<< std::endl;
169  // regular TPs (not HO)
170  for (tp_work=tp_begin; tp_work!=tp_end; tp_work++) {
171  // std::cout << "raw=0x"<<std::hex<< tp_work->raw()<<std::dec <<std::endl;
172  if (tp_work->raw()==0xFFFF) continue; // filler word
173  if (slbAndChan(*tp_work)!=currFiberChan) { // start new set
174  npre=0;
175  currFiberChan=slbAndChan(*tp_work);
176 
177  // std::cout<< " NEW SET "<<std::endl;
178  //HcalElectronicsId eid(tp_work->slbChan(),tp_work->slb(),spigot,dccid,htr_cr,htr_slot,htr_tb);
179  //DetId did=emap.lookupTrigger(eid);
180  //if (did.null()) {
181  //report.countUnmappedTPDigi(eid);
182  //if (unknownIdsTrig_.find(eid)==unknownIdsTrig_.end()) {
183  //if (!silent) edm::LogWarning("HCAL") << "HcalUnpacker: No trigger primitive match found for electronics id :" << eid;
184  //unknownIdsTrig_.insert(eid);
185  //}
186  //valid=false;
187  //continue;
188  //} else if (did==HcalTrigTowerDetId::Undefined ||
189  //(did.det()==DetId::Hcal && did.subdetId()==0)) {
191  //valid=false;
192  //continue;
193  //}
194 
195  colls.tpCont->push_back(CastorTriggerPrimitiveDigi(id));
196  // set the various bits
197  if (!tpgSOIbitInUse) colls.tpCont->back().setPresamples(nps);
198  colls.tpCont->back().setZSInfo(htr.isUnsuppressed(),htr.wasMarkAndPassZSTP(slb(*tp_work),slbChan(*tp_work)));
199 
200  // no hits recorded for current
201  ncurr=0;
202  valid=true;
203  }
204  // add the word (if within settings or recent firmware [recent firmware ignores startSample/endSample])
205  if (valid && ((tpgSOIbitInUse && ncurr<10) || (ncurr>=startSample_ && ncurr<=endSample_))) {
206  colls.tpCont->back().setSample(colls.tpCont->back().size(),*tp_work);
207  colls.tpCont->back().setSize(colls.tpCont->back().size()+1);
208  }
209  // set presamples,if SOI
210  if (valid && tpgSOIbitInUse && isTPGSOI(*tp_work)) {
211  colls.tpCont->back().setPresamples(ncurr);
212  }
213  ncurr++;
214  npre++;
215  }
216  }
217 
219  qie_begin=(HcalQIESample*)daq_first;
220  qie_end=(HcalQIESample*)(daq_last+1); // one beyond last..
221 
223  currFiberChan=0x3F; // invalid fiber+channel...
224  ncurr=0;
225  valid=false;
226 
227  for (qie_work=qie_begin; qie_work!=qie_end; ) {
228  if (qie_work->raw()==0xFFFF) {
229  qie_work++;
230  continue; // filler word
231  }
232  // always at the beginning ...
233  currFiberChan=qie_work->fiberAndChan();
234 
235  // lookup the right channel
236  CastorElectronicsId eid(qie_work->fiberChan(),qie_work->fiber(),spigot,dccid);
237  eid.setHTR(htr_cr,htr_slot,htr_tb);
238  DetId did=emap.lookup(eid);
239 
240  if (!did.null()) {
241  colls.castorCont->push_back(CastorDataFrame(HcalCastorDetId(did)));
242  qie_work=CastorUnpacker_impl::unpack<CastorDataFrame>(qie_work, qie_end, colls.castorCont->back(), nps, eid, startSample_, endSample_, expectedOrbitMessageTime_, htr);
243  } else {
244  report.countUnmappedDigi();
245  if (unknownIds_.find(eid)==unknownIds_.end()) {
246  if (!silent)
247  edm::LogWarning("CASTOR") << "CastorUnpacker: No match found for electronics id :" << eid;
248  unknownIds_.insert(eid);
249  }
250  for (int fiberC=qie_work->fiberAndChan();
251  qie_work!=qie_end && qie_work->fiberAndChan()==fiberC;
252  qie_work++);
253  }
254  }
255  }
256 }
257 
#define LogDebug(id)
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 slb(const HcalTriggerPrimitiveSample &theSample)
int sector() const
get the sector (1-16)
int fiberAndChan() const
get the id channel
Definition: HcalQIESample.h:36
int fiberChan() const
get the fiber channel number
Definition: HcalQIESample.h:34
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)
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:356
unsigned int getFibOrbMsgBCN(int fiber) const
Get the BCN of the Fiber Orbit Messages.
Definition: HcalHTRData.h:177
static int slbChan(const HcalTriggerPrimitiveSample &theSample)
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
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:353
tuple report
Definition: zeeHLT_cff.py:9
const DetId lookup(CastorElectronicsId fId) const
lookup the logical detid associated with the given electronics id
CastorUnpacker(int sourceIdOffset, int beg, int end)
for normal data
static int slbAndChan(const HcalTriggerPrimitiveSample &theSample)
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:32
bool getSpigotPresent(unsigned int nspigot) const
Read the &quot;PRESENT&quot; bit for this spigot.
#define end
Definition: vmac.h:37
std::vector< CastorDataFrame > * castorCont
std::vector< CastorTriggerPrimitiveDigi > * tpCont
int getSourceId() const
Definition: HcalDCCHeader.h:32
Definition: DetId.h:18
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:153
bool unpack(const HcalHTRData &data, HcalTTPDigi &digi)
bool null() const
is this a null id ?
Definition: DetId.h:45
int getNPS() const
Get the number of presamples in daq data.
Definition: HcalHTRData.cc:400
int getFirmwareFlavor() const
Get the HTR firmware flavor.
Definition: HcalHTRData.cc:409
static const int SPIGOT_COUNT
Definition: HcalDCCHeader.h:19
bool wasMarkAndPassZSTP(int slb, int slbchan) const
Was this channel passed as part of Mark&amp;Pass ZS?
Definition: HcalHTRData.cc:364
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:373
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
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:331
static const int MAXSAMPLES
bool isHistogramEvent() const
Is this event a histogram event? (do not call standard unpack in this case!!!!!)
Definition: HcalHTRData.cc:385
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