CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalHTRData.cc
Go to the documentation of this file.
1 //#include "Utilities/Configuration/interface/Architecture.h"
2 /*
3  * $Date: 2010/05/27 13:33:19 $
4  * $Revision: 1.16 $
5  * \author J. Mans -- UMD
6  */
7 #ifndef HTBDAQ_DATA_STANDALONE
9 #else
10 #include "HcalHTRData.h"
13 #endif
14 #include <string.h>
15 #include <stdio.h>
16 
17 HcalHTRData::HcalHTRData() : m_formatVersion(-2), m_rawLength(0), m_rawConst(0), m_ownData(0) { }
18 HcalHTRData::HcalHTRData(const unsigned short* data, int length) {
19  adoptData(data,length);
20  m_ownData=0;
21 }
22 HcalHTRData::HcalHTRData(const HcalHTRData& hd) : m_formatVersion(hd.m_formatVersion), m_rawLength(hd.m_rawLength), m_rawConst(hd.m_rawConst), m_ownData(0) { }
23 
24 HcalHTRData::HcalHTRData(int version_to_create) : m_formatVersion(version_to_create) {
25  allocate(version_to_create);
26 }
27 
28 void HcalHTRData::allocate(int version_to_create) {
29  m_formatVersion=version_to_create;
30  // the needed space is for the biggest possible event...
31  const int needed=0x200;
32  // create a buffer big enough...
33  m_ownData=new unsigned short[needed];
34  m_rawLength=0;
36 }
37 
39  if (m_ownData==0) {
43  }
44  return (*this);
45 }
46 
47 void HcalHTRData::adoptData(const unsigned short* data, int length) {
48  m_rawLength=length;
50  if (m_rawLength<5) {
51  m_formatVersion=-2; // invalid!
52  } else {
53  // determine format version
54  if ((m_rawConst[2]&0x8000)==0) m_formatVersion=-1; // original format before versions
55  else m_formatVersion=(m_rawConst[4]>>12)&0xF;
56  }
57 }
58 
59 // check :: not EE, length is reasonable, length matches wordcount
60 // length required for tp+daq is correct
61 
62 bool HcalHTRData::check() const {
63  if (m_formatVersion==-1) {
64  // length checks
65  // minimum length
66  if (m_rawLength<6+12) return false;
67  // matches wordcount
68  if (m_rawLength!=m_rawConst[m_rawLength-3]) return false;
69  // empty event check
70  if (m_rawConst[2]&0x20) return false;
71  } else {
72  // length checks
73  // minimum length
74  if (m_rawLength<8+4) return false;
75  if (m_formatVersion<=3) {
76  // matches wordcount
78  if (isHistogramEvent() && m_rawConst[m_rawLength-3]==786) {
79  // known bug!
80  } else
81  return false;
82  }
83  } else {
84  // eventually add CRC check
85  }
86  // empty event check (redundant...)
87  if (m_rawConst[2]&0x4) return false;
88  }
89 
90  if (!isHistogramEvent()) {
91  // daq/tp length check
92  int tp, daq, header, trailer;
93  determineSectionLengths(tp,daq,header,trailer);
94  if (tp+daq+header+trailer>m_rawLength) return false;
95  }
96 
97  return true;
98 }
99 
100 void HcalHTRData::determineSectionLengths(int& tpWords, int& daqWords, int& headerWords, int& trailerWords) const {
101  if (m_formatVersion==-1) {
102  tpWords=m_rawConst[5]>>8;
103  daqWords=CHANNELS_PER_SPIGOT*(m_rawConst[m_rawLength-4]>>8); // always 24 channels, no zero suppresion
104  headerWords=6;
105  trailerWords=12;
106  } else {
107  tpWords=m_rawConst[5]>>8;
108  if (m_rawLength>4)
109  daqWords=m_rawConst[m_rawLength-4]&0x7FF; // zero suppression supported
110  headerWords=8;
111  trailerWords=4; // minimum, may be more...
112  }
113 }
114 
115 void HcalHTRData::determineStaticLengths(int& headerWords, int& trailerWords) const {
116  if (m_formatVersion==-1) {
117  headerWords=6;
118  trailerWords=12;
119  } else if (m_formatVersion<5) {
120  headerWords=8;
121  trailerWords=4; // minimum, may be more...
122  } else {
123  headerWords=8;
124  trailerWords=12; // minimum, may be more...
125  }
126 }
127 
128 void HcalHTRData::dataPointers(const unsigned short** daq_first,
129  const unsigned short** daq_last,
130  const unsigned short** tp_first,
131  const unsigned short** tp_last) const {
132  int tp_words_total, daq_words_total, headerLen, trailerLen;
133  determineSectionLengths(tp_words_total,daq_words_total,headerLen,trailerLen);
134 
135  *tp_first=m_rawConst+headerLen;
136  *tp_last=*tp_first+(tp_words_total-1);
137  *daq_first=*tp_last+1;
138  *daq_last=*daq_first+(daq_words_total-1);
139 }
140 
141 /* using FiberAd[2:0] ChanId[1:0] */
142 static const int channelDecoder[32] = { 0, 1, 2, 99, 3, 4, 5, 99,
143  6, 7, 8, 99, 9,10,11, 99,
144  12,13,14,99,15,16,17, 99,
145  18,19,20,99,21,22,23, 99};
146 
147 void HcalHTRData::unpack(unsigned char* daq_lengths, unsigned short* daq_samples,
148  unsigned char* tp_lengths, unsigned short* tp_samples) const {
149 
150  if (daq_lengths!=0) memset(daq_lengths,0,CHANNELS_PER_SPIGOT);
151  if (tp_lengths!=0) memset(tp_lengths,0,CHANNELS_PER_SPIGOT);
152 
153  // currently, the major differences between the versions are
154  // -1 : 6 word header, no zero suppression, trailer setup
155  // 0 : 8 word header, zero suppression,
156 
157  int tp_words_total, daq_words_total, headerLen, trailerLen;
158  determineSectionLengths(tp_words_total,daq_words_total,headerLen,trailerLen);
159 
160  // printf("%d %d %d %d\n",tp_words_total,daq_words_total,headerLen,trailerLen);
161  int wordPtr;
162  const unsigned short* tpBase=m_rawConst+headerLen;
163  // process the trigger primitive words
164  if (tp_lengths!=0) {
165  for (wordPtr=0; wordPtr<tp_words_total; wordPtr++) {
166  int ichan=channelDecoder[tpBase[wordPtr]>>11];
167  if (ichan>=24) continue;
168  tp_samples[ichan*MAXIMUM_SAMPLES_PER_CHANNEL+tp_lengths[ichan]]=tpBase[wordPtr]&0x3ff;
169  tp_lengths[ichan]++;
170  }
171  }
172 
173  const unsigned short* daqBase=m_rawConst+headerLen+tp_words_total;
174  // process the DAQ words [ assumes that data from one channel will always be together ]
175  int lastChan=-1;
176  int lastCapid=0;
177  if (daq_lengths!=0) {
178  for (wordPtr=0; wordPtr<daq_words_total; wordPtr++) {
179  int ichan=channelDecoder[daqBase[wordPtr]>>11];
180  if (ichan>=24) continue;
181  int capid=(daqBase[wordPtr]&0x180)>>7;
182  int erdv=(daqBase[wordPtr]&0x600)>>9;
183  if (erdv!=0x1 ||
184  (lastChan==ichan && (capid!=((lastCapid+1)%4)))) {
185  daq_lengths[ichan]|=0x80;
186  }
187  lastChan=ichan;
188  lastCapid=capid;
189 
190  int useLength=daq_lengths[ichan]&0x1F;
191  // printf("%d %d\n",ichan,useLength);
192  daq_samples[ichan*MAXIMUM_SAMPLES_PER_CHANNEL+useLength]=daqBase[wordPtr]&0x3ff;
193  daq_lengths[ichan]=(useLength+1)|(daq_lengths[ichan]&0xE0); // keep the error bits
194  }
195  }
196 
197 }
198 
199 void HcalHTRData::pack(unsigned char* daq_lengths, unsigned short* daq_samples,
200  unsigned char* tp_lengths, unsigned short* tp_samples, bool do_capid) {
201 
202  int tp_words_total=0, daq_words_total=0, headerLen, trailerLen;
203  determineStaticLengths(headerLen,trailerLen);
204 
205  tp_words_total=0;
206  daq_words_total=0;
207  int ichan,isample;
208 
209  // trigger primitive words
210  unsigned short* ptr=m_ownData+headerLen;
211  if (tp_samples!=0 && tp_lengths!=0) {
212  for (ichan=0; ichan<24; ichan++) {
213  unsigned short chanid=((ichan%3)+((ichan/3)<<2))<<11;
214  for (isample=0; isample<tp_lengths[ichan] && isample<MAXIMUM_SAMPLES_PER_CHANNEL; isample++) {
215  ptr[tp_words_total]=chanid|(tp_samples[ichan*MAXIMUM_SAMPLES_PER_CHANNEL+isample]&0x3FF);
216  tp_words_total++;
217  }
218  }
219  }
220 
221  // daq words
222  ptr=m_ownData+headerLen+tp_words_total;
223  for (ichan=0; ichan<24; ichan++) {
224  unsigned short chanid=((ichan%3)+((ichan/3)<<2))<<11;
225  for (isample=0; isample<daq_lengths[ichan] && isample<MAXIMUM_SAMPLES_PER_CHANNEL; isample++) {
226  unsigned short basedata=daq_samples[ichan*MAXIMUM_SAMPLES_PER_CHANNEL+isample]&0x3FF;
227  if (do_capid) basedata=(basedata&0x7F)|(0x200)|((isample%4)<<7);
228  ptr[daq_words_total]=chanid|basedata;
229  daq_words_total++;
230  }
231  }
232  unsigned short totalLen;
233  if (m_formatVersion==-1) {
234  m_ownData[5]=(tp_words_total<<8)|0x1;
235  totalLen=headerLen+tp_words_total+daq_words_total+trailerLen;
236  m_rawLength=totalLen;
237  m_ownData[totalLen-3]=totalLen;
238  m_ownData[totalLen-4]=(tp_words_total/CHANNELS_PER_SPIGOT)|((daq_words_total/CHANNELS_PER_SPIGOT)<<8);
239  } else {
240  m_ownData[5]=(tp_words_total<<8)|0x1;
241  totalLen=headerLen+tp_words_total+daq_words_total+trailerLen;
242  if ((totalLen%2)==1) {
243  m_ownData[totalLen-4]=0xFFFF; // parity word
244  totalLen++; // round to even number of 16-bit words
245  }
246  m_rawLength=totalLen;
247  m_ownData[totalLen-2]=totalLen/2; // 32-bit words
248  m_ownData[totalLen-3]=totalLen;
249  m_ownData[totalLen-4]=daq_words_total;
250  }
251  if (trailerLen==12) { // initialize extra trailer words if present
252  for (int i=12; i>4; i--)
253  m_ownData[totalLen-i]=0;
254  }
255 
256 }
257 
258 void HcalHTRData::packHeaderTrailer(int L1Anumber, int bcn, int submodule, int orbitn, int pipeline, int ndd, int nps, int firmwareRev) {
259  m_ownData[0]=L1Anumber&0xFF;
260  m_ownData[1]=(L1Anumber&0xFFFF00)>>8;
261  if (m_formatVersion==-1) {
262  m_ownData[2]=((pipeline&0x7F)<<8); // no error bits
263  m_ownData[3]=((orbitn&0xFF)<<8)|(submodule&0xFF);
264  m_ownData[4]=bcn&0xFFF;
265  // m_ownData[5]&=0xFF01;
266  } else {
267  m_ownData[2]=0x8000; // Version is valid, no error bits
268  if (m_formatVersion==0)
269  m_ownData[3]=((orbitn&0x3F)<<10)|(submodule&0x3FF);
270  else
271  m_ownData[3]=((orbitn&0x1F)<<11)|(submodule&0x7FF);
272  m_ownData[4]=((m_formatVersion&0xF)<<12)|(bcn&0xFFF);
273  m_ownData[5]|=((nps&0x1F)<<3)|0x1;
274  m_ownData[6]=((firmwareRev&0x70000)>>3)|(firmwareRev&0x1FFF);
275  m_ownData[7]=pipeline&0xFF;
276  m_ownData[m_rawLength-4]&=0x7FF;
277  m_ownData[m_rawLength-4]|=(ndd&0x1F)<<11;
278  }
279  m_ownData[m_rawLength-2]=m_rawLength/2; // 32-bit words
280  m_ownData[m_rawLength-1]=(L1Anumber&0xFF)<<8;
281 }
282 
283 void HcalHTRData::packUnsuppressed(const bool* mp) {
284  if (m_formatVersion<4) return;
285 
286  for (int fiber=1; fiber<=8; fiber++) {
287  for (int fiberchan=0; fiberchan<=2; fiberchan++) {
288  int linchan=(fiber-1)*3+fiberchan;
289 
290  unsigned short& val=m_ownData[m_rawLength-12+(linchan/8)];
291  if (mp[linchan]) val|=1<<(linchan%8);
292  }
293  }
294 
295  // set the unsupressed bit
296  m_ownData[6]|=0x8000;
297 }
298 
299 unsigned int HcalHTRData::getOrbitNumber() const {
300  switch (m_formatVersion) {
301  case (-1) : return (m_rawConst[3]>>8);
302  case (0) : return (m_rawConst[3]>>10);
303  default : return (m_rawConst[3]>>11);
304  }
305 }
306 unsigned int HcalHTRData::getSubmodule() const {
307  switch (m_formatVersion) {
308  case (-1) : return (m_rawConst[3]&0xFF);
309  case (0) : return (m_rawConst[3]&0x3FF);
310  default : return (m_rawConst[3]&0x7FF);
311  }
312 }
313 unsigned int HcalHTRData::htrSlot() const{
314  const unsigned int smid = getSubmodule();
315  return ((smid>>1)&0x1F);
316 }
317 unsigned int HcalHTRData::htrTopBottom() const{
318  const unsigned int smid = getSubmodule();
319  return (smid&0x01);
320 }
321 unsigned int HcalHTRData::readoutVMECrateId() const{
322  const unsigned int smid = getSubmodule();
323  return ((smid>>6)&0x1F);
324 }
326  return (m_formatVersion==-1)?(false):(m_rawConst[2]&0x4000);
327 }
329  return (m_formatVersion<4)?(false):(m_rawConst[6]&0x8000);
330 }
331 bool HcalHTRData::wasMarkAndPassZS(int fiber, int fiberchan) const {
332  if (fiber<1 || fiber>8 || fiberchan<0 || fiberchan>2) return false;
333  if (!isUnsuppressed() || m_formatVersion<5) return false;
334  int linchan=(fiber-1)*3+fiberchan;
335 
336  unsigned short val=m_rawConst[m_rawLength-12+(linchan/8)];
337  return ((val>>(linchan%8))&0x1)!=0;
338 }
339 bool HcalHTRData::wasMarkAndPassZSTP(int slb, int slbchan) const {
340  if (slb<1 || slb>6 || slbchan<0 || slbchan>3) return false;
341  if (!isUnsuppressed() || m_formatVersion<5) return false;
342  int linchan=(slb-1)*4+slbchan;
343 
344  unsigned short val=m_rawConst[m_rawLength-12+(linchan/8)];
345  return ((val>>(linchan%8))&0x100)!=0;
346 }
347 
348 uint32_t HcalHTRData::zsBunchMask() const {
349  uint32_t mask=0;
350  if (isUnsuppressed() && m_formatVersion>=5) {
351  mask=m_rawConst[m_rawLength-5]|
352  ((m_rawConst[m_rawLength-6]&0xF000)<<4);
353  }
354  return mask;
355 }
356 
358  return (m_formatVersion==-1)?(false):(m_rawConst[2]&0x1000);
359 }
361  return (m_formatVersion==-1)?(m_rawConst[2]&0x2):(m_rawConst[2]&0x2000);
362 }
363 int HcalHTRData::getNDD() const {
364  return (m_formatVersion==-1)?(m_rawConst[m_rawLength-4]>>8):(m_rawConst[m_rawLength-4]>>11);
365 }
366 int HcalHTRData::getNTP() const {
367  int retval=-1;
368  if (m_formatVersion==-1) retval=m_rawConst[m_rawLength-4]&0xFF;
369  else if (m_formatVersion<3) retval=m_rawConst[m_rawLength-4]>>11;
370  return retval;
371 }
373  return (m_formatVersion==-1)?(m_rawConst[m_rawLength-4]&0xFF):(m_rawConst[m_rawLength-4]&0x7FF);
374 }
375 int HcalHTRData::getNPS() const {
376  return (m_formatVersion==-1)?(0):((m_rawConst[5]>>3)&0x1F);
377 }
378 unsigned int HcalHTRData::getPipelineLength() const {
379  return (m_formatVersion==-1)?(m_rawConst[2]>>8):(m_rawConst[7]&0xFF);
380 }
381 unsigned int HcalHTRData::getFirmwareRevision() const {
382  return (m_formatVersion==-1)?(0):((m_rawConst[6]&0x1FFF)+((m_rawConst[6]&0xE000)<<3));
383 }
385  return (m_formatVersion<2)?(-1):((m_rawConst[7]>>8)&0xFF);
386 }
387 
388 void HcalHTRData::getHistogramFibers(int& a, int& b) const {
389  a=-1;
390  b=-1;
391  if (m_formatVersion==-1) {
392  a=((m_rawConst[2]&0x0F00)>>8);
393  b=((m_rawConst[2]&0xF000)>>12);
394  } else {
395  a=((m_rawConst[5]&0x0F00)>>8);
396  b=((m_rawConst[5]&0xF000)>>12);
397  }
398 }
399 
400 bool HcalHTRData::wasHistogramError(int ifiber) const {
401  bool retval=!isHistogramEvent();
402  if (!retval) {
403  retval=((m_rawConst[7])&(1<<ifiber))!=0;
404  }
405  return retval;
406 }
407 
408 bool HcalHTRData::unpackHistogram(int myfiber, int mysc, int capid, unsigned short* histogram) const {
409  // check for histogram mode
410  if (!isHistogramEvent()) return false;
411 
412  int fiber1, fiber2;
413  getHistogramFibers(fiber1,fiber2);
414  if (fiber1!=myfiber && fiber2!=myfiber) return false;
415 
416  if (m_formatVersion==-1) {
417  int offset=6+mysc*4*32+capid*32;
418  if (myfiber==fiber2) offset+=3*4*32; // skip to the second half...
419  for (int i=0; i<32; i++)
420  histogram[i]=m_rawConst[offset+i];
421  return true;
422  } else {
423  int offset=8+mysc*4*32+capid*32;
424  if (myfiber==fiber2) offset+=3*4*32; // skip to the second half...
425  for (int i=0; i<32; i++)
426  histogram[i]=m_rawConst[offset+i];
427  return true;
428  }
429 }
unsigned short * m_ownData
Definition: HcalHTRData.h:242
int i
Definition: DBlmapReader.cc:9
int getNDD() const
Get the number of daq data samples per channel when not zero-suppressed.
Definition: HcalHTRData.cc:363
bool check() const
Check for a good event Requires a minimum length, matching wordcount and length, not an empty event...
Definition: HcalHTRData.cc:62
int m_formatVersion
Definition: HcalHTRData.h:239
unsigned int htrTopBottom() const
HcalElectronicsId-style HTR top/bottom (1=top/0=bottom)
Definition: HcalHTRData.cc:317
void packUnsuppressed(const bool *mp)
pack trailer with Mark and Pass bits
Definition: HcalHTRData.cc:283
bool isPatternRAMEvent() const
Is this event a pattern-ram event?
Definition: HcalHTRData.cc:357
const unsigned short * m_rawConst
Definition: HcalHTRData.h:241
void determineStaticLengths(int &headerWords, int &trailerWords) const
Definition: HcalHTRData.cc:115
bool wasMarkAndPassZS(int fiber, int fiberchan) const
Was this channel passed as part of Mark&amp;Pass ZS?
Definition: HcalHTRData.cc:331
bool wasHistogramError(int ifiber) const
Was there an error on the given fiber for this event (only in histogram mode!)
Definition: HcalHTRData.cc:400
static const int CHANNELS_PER_SPIGOT
Definition: HcalHTRData.h:20
static const int MAXIMUM_SAMPLES_PER_CHANNEL
Definition: HcalHTRData.h:21
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:388
unsigned int getOrbitNumber() const
Get the HTR orbit number.
Definition: HcalHTRData.cc:299
HcalHTRData & operator=(const HcalHTRData &)
Definition: HcalHTRData.cc:38
int getNPrecisionWords() const
Get the total number of precision data 16-bit words.
Definition: HcalHTRData.cc:372
bool isCalibrationStream() const
Is this event a calibration-stream event?
Definition: HcalHTRData.cc:325
bool isUnsuppressed() const
Is this event an unsuppresed event?
Definition: HcalHTRData.cc:328
void allocate(int version_to_create=0)
Definition: HcalHTRData.cc:28
unsigned int getPipelineLength() const
Get the pipeline length used for this event.
Definition: HcalHTRData.cc:378
unsigned int htrSlot() const
HcalElectronicsId-style HTR slot.
Definition: HcalHTRData.cc:313
void unpack(unsigned char *daq_lengths, unsigned short *daq_samples, unsigned char *tp_lengths, unsigned short *tp_samples) const
Unpack the HTR data into TP and DAQ data sorted by channel.
Definition: HcalHTRData.cc:147
bool unpackHistogram(int fiber, int fiberchan, int capid, unsigned short *histogram) const
Unpack special histogramming mode data.
Definition: HcalHTRData.cc:408
int getNTP() const
Get the number of trigger data samples when not zero-suppressed (not available after FW 4) ...
Definition: HcalHTRData.cc:366
unsigned int offset(bool)
void pack(unsigned char *daq_lengths, unsigned short *daq_samples, unsigned char *tp_lengths, unsigned short *tp_samples, bool do_capid=false)
Unpack the HTR data into TP and DAQ data sorted by channel.
Definition: HcalHTRData.cc:199
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:128
static const int channelDecoder[32]
Definition: HcalHTRData.cc:142
double b
Definition: hdecay.h:120
int getNPS() const
Get the number of presamples in daq data.
Definition: HcalHTRData.cc:375
int getFirmwareFlavor() const
Get the HTR firmware flavor.
Definition: HcalHTRData.cc:384
unsigned int readoutVMECrateId() const
HcalElectronicsId-style VME crate number.
Definition: HcalHTRData.cc:321
void adoptData(const unsigned short *data, int length)
Definition: HcalHTRData.cc:47
bool wasMarkAndPassZSTP(int slb, int slbchan) const
Was this channel passed as part of Mark&amp;Pass ZS?
Definition: HcalHTRData.cc:339
void packHeaderTrailer(int L1Anumber, int bcn, int submodule, int orbitn, int pipeline, int ndd, int nps, int firmwareRev=0)
pack header and trailer (call after pack)
Definition: HcalHTRData.cc:258
double a
Definition: hdecay.h:121
uint32_t zsBunchMask() const
ZS Bunch Mask (if available)
Definition: HcalHTRData.cc:348
unsigned int getSubmodule() const
Get the HTR submodule number.
Definition: HcalHTRData.cc:306
unsigned int getFirmwareRevision() const
Get the HTR firmware version.
Definition: HcalHTRData.cc:381
bool isHistogramEvent() const
Is this event a histogram event? (do not call standard unpack in this case!!!!!)
Definition: HcalHTRData.cc:360
void determineSectionLengths(int &tpWords, int &daqWords, int &headerWords, int &trailerWords) const
Definition: HcalHTRData.cc:100