CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CSCTMBData.cc
Go to the documentation of this file.
1 
8 
9 #include <iomanip> // dump for JK
10 #include <iostream>
11 #include <cstdio>
14 
15 bool CSCTMBData::debug =false;
16 
18  : theOriginalBuffer(0),
19  theB0CLine( 0 ),
20  theE0FLine( 0 ),
21  theTMBHeader(2007, 0x50c3),
22  theCLCTData(&theTMBHeader),
23  theTMBScopeIsPresent(false),
24  theTMBScope(0),
25  theTMBMiniScopeIsPresent(false),
26  theTMBMiniScope(0),
27  theBlockedCFEBIsPresent(false),
28  theTMBBlockedCFEB(0),
29  theTMBTrailer(theTMBHeader.sizeInWords()+theCLCTData.sizeInWords(), 2007),
30  size_( 0 ),
31  cWordCnt( 0 ),
32  theRPCDataIsPresent(false)
33 {
34 
35 
36 }
37 
38 CSCTMBData::CSCTMBData(int firmwareVersion, int firmwareRevision, int cfebs)
39  : theOriginalBuffer(0),
40  theB0CLine( 0 ),
41  theE0FLine( 0 ),
42  theTMBHeader(firmwareVersion, firmwareRevision),
43  theCLCTData(&theTMBHeader),
44  theTMBScopeIsPresent(false),
45  theTMBScope(0),
46  theTMBMiniScopeIsPresent(false),
47  theTMBMiniScope(0),
48  theBlockedCFEBIsPresent(false),
49  theTMBBlockedCFEB(0),
50  theTMBTrailer(theTMBHeader.sizeInWords()+theCLCTData.sizeInWords(), firmwareVersion),
51  size_( 0 ),
52  cWordCnt( 0 ),
53  theRPCDataIsPresent(false)
54 {
55 
56  theTMBHeader.setNCFEBs(cfebs);
59 
60 
61 }
62 
63 CSCTMBData::CSCTMBData(unsigned short *buf)
64  : theOriginalBuffer(buf),
65  theTMBHeader(2007, 0x50c3),
66  theCLCTData(&theTMBHeader),
67  theTMBScopeIsPresent(false),
68  theTMBScope(0),
69  theTMBMiniScopeIsPresent(false),
70  theTMBMiniScope(0),
71  theBlockedCFEBIsPresent(false),
72  theTMBBlockedCFEB(0),
73  theTMBTrailer(theTMBHeader.sizeInWords()+theCLCTData.sizeInWords(), 2007),
74  theRPCDataIsPresent(false){
75  size_ = UnpackTMB(buf);
76 }
77 
78 // Explicitly-defined copy constructor is needed when the scope data is
79 // present, to prevent the same pointer from being deleted twice. -SV.
81  theOriginalBuffer(data.theOriginalBuffer),
82  theB0CLine(data.theB0CLine), theE0FLine(data.theE0FLine),
83  theTMBHeader(data.theTMBHeader),
84  theCLCTData(data.theCLCTData), theRPCData(data.theRPCData),
85  theTMBScopeIsPresent(data.theTMBScopeIsPresent),
86  theTMBMiniScopeIsPresent(data.theTMBMiniScopeIsPresent),
87  theBlockedCFEBIsPresent(data.theBlockedCFEBIsPresent),
88  theTMBTrailer(data.theTMBTrailer),
89  size_(data.size_), cWordCnt(data.cWordCnt),
90  theRPCDataIsPresent(data.theRPCDataIsPresent)
91 {
93  theTMBScope = new CSCTMBScope(*(data.theTMBScope));
94  }
95  else {
96  theTMBScope = 0;
97  }
98 
101  }
102  else {
103  theTMBMiniScope = 0;
104  }
105 
108  }
109  else {
110  theTMBBlockedCFEB = 0;
111  }
112 
113 }
114 
116  if (theTMBScopeIsPresent) {
117  delete theTMBScope;
118  theTMBScopeIsPresent = false;
119  }
120 
122  delete theTMBMiniScope;
123  theTMBMiniScopeIsPresent = false;
124  }
125 
127  delete theTMBBlockedCFEB;
128  theBlockedCFEBIsPresent = false;
129  }
130 }
131 
134 int findLine(unsigned short *buf, unsigned short marker,int first, int maxToDo) {
135  for(int i = first; i < maxToDo; ++i) {
136  if(buf[i] == marker) {
137  return i;
138  }
139  }
140  return -1;
141 }
142 
144  std::vector<std::bitset<16> > theTotalTMBData(theE0FLine+1-theB0CLine);
145  unsigned i = 0;
146  for (unsigned int line=theB0CLine; line<theE0FLine+1;++line) {
147  theTotalTMBData[i] = std::bitset<16>(theOriginalBuffer[line]);
148  ++i;
149  }
150  if ( theTotalTMBData.size() > 0 ) {
151  std::bitset<22> CRC=calCRC22(theTotalTMBData);
152  LogTrace("CSCTMBData|CSCRawToDigi") << " Test here " << CRC.to_ulong();
153  return CRC.to_ulong();
154  }
155  else {
156  LogTrace("CSCTMBData|CSCRawToDigi") << "theTotalTMBData doesn't exist";
157  return 0;
158  }
159 }
160 
161 int CSCTMBData::UnpackTMB(unsigned short *buf) {
163  unsigned short int firmwareVersion=0;
164  int Ntbins = 0 ;
165  int NHeaderFrames = 0; //WARNING in 5_0_X
166  int NRPCtbins = 0; // =VB= number of RPC tbins
167 
168  int b0cLine=0;
169 
172  NHeaderFrames++; NHeaderFrames--;
173 
174  if (buf[b0cLine]==0xdb0c) {
175  firmwareVersion=2007;
176  Ntbins = buf[b0cLine+19]&0xF8;
177  NRPCtbins = (buf[b0cLine+36]>>5)&0x1F; // =VB= get RPC tbins
178  NHeaderFrames = buf[b0cLine+5]&0x3F; //WARNING in 5_0_X
179  }
180  else if (buf[b0cLine]==0x6b0c) {
181  firmwareVersion=2006;
182  Ntbins = buf[b0cLine+1]&0x1f ;
183  NRPCtbins = Ntbins;
184  NHeaderFrames = buf[b0cLine+4]&0x1f; //WARNING in 5_0_X
185  }
186  else {
187  LogTrace("CSCTMBData|CSCRawToDigi") << "+++ Can't find b0C flag";
188  }
189 
190  if ((firmwareVersion==2007)&&(!(((buf[b0cLine]&0xFFFF)==0xDB0C)&&((buf[b0cLine+1]&0xf000)==0xD000)
191  &&((buf[b0cLine+2]&0xf000)==0xD000)&&((buf[b0cLine+3]&0xf000)==0xD000)))){
192  LogTrace("CSCTMBData|CSCRawToDigi") << "+++ CSCTMBData warning: error in header in 2007 format!";
193  }
194 
195  int MaxSizeRPC = 1+NRPCtbins*2*4+1;
196  //int MaxSizeScope = 5;
197  int e0bLine =-1;
198  switch (firmwareVersion) {
199  case 2007:
200  e0bLine = 42; //last word of header2007
201  break;
202  case 2006:
203  e0bLine = 26; //last word of header in 2006 format
204  break;
205  default:
206  edm::LogError("CSCTMBData|CSCRawToDigi") << "+++ undetermined firmware format - cant find e0bLine";
207  }
208 
210 
211  if(!theTMBHeader.check()) {
212  LogTrace("CSCTMBData|CSCRawToDigi") << "+++ CSCTMBData warning: Bad TMB header e0bLine=" << std::hex << buf[e0bLine];
213  return 0;
214  }
215 
216  int currentPosition = theTMBHeader.sizeInWords();
217  int theFirmwareVersion = theTMBHeader.FirmwareVersion();
218 
219  theCLCTData = CSCCLCTData(theTMBHeader.NCFEBs(), theTMBHeader.NTBins(), buf+e0bLine+1, theFirmwareVersion);
220 
221  if(!theCLCTData.check()) {
222  LogTrace("CSCTMBData|CSCRawToDigi") << "+++ CSCTMBData warning: Bad CLCT data";
223  }
224  else {
225  currentPosition+=theCLCTData.sizeInWords();
226  }
227 
228  //int i = currentPosition-1;
229  //printf ( "%04x %04x %04x %04x\n",buf[i+3],buf[i+2],buf[i+1],buf[i] ) ;
230 
231 
232  // look for RPC
233  int b04Line = currentPosition;
234 
235  if(buf[b04Line]==0x6b04) {
236  // we need an e04 line to calculate the size
237  int e04Line = findLine(buf, 0x6e04, currentPosition, currentPosition+MaxSizeRPC);
238  if(e04Line != -1) {
239  theRPCDataIsPresent = true;
240  theRPCData = CSCRPCData(buf+b04Line, e04Line-b04Line+1);
241  currentPosition+=theRPCData.sizeInWords();
242  }
243  else {
244  LogTrace("CSCTMBData|CSCRawToDigi") << "CSCTMBData::corrupt RPC data! Failed to find end! ";
245  return 0;
246  }
247  }
248 
249  int TotTMBReadout=0;
250  switch (firmwareVersion) {
251  case 2007:
252  TotTMBReadout= 43+Ntbins*6*5+1+NRPCtbins*2*4+2+8*256+8;
253  break;
254  case 2006:
255  TotTMBReadout= 27+Ntbins*6*5+1+NRPCtbins*2*4+2+8*256+8; //see tmb2004 manual (version v2p06) page54.
256  break;
257  default:
258  edm::LogError("CSCTMBData|CSCRawToDigi") << "can't find TotTMBReadout - unknown firmware version!";
259  break;
260  }
261 
262 //std::cout << " !!!TMB Scope!!! " << std::endl;
263  if (buf[currentPosition]==0x6b05) {
264  int b05Line = currentPosition;
265  LogTrace("CSCTMBData|CSCRawToDigi") << "found scope!";
266  int e05Line = findLine(buf, 0x6e05, currentPosition, TotTMBReadout-currentPosition);
267  if(e05Line != -1){
268  theTMBScopeIsPresent = true;
269  theTMBScope = new CSCTMBScope(buf,b05Line, e05Line);
270  // The size of the TMB scope data can vary, and I see no good reasons
271  // not to determine it dynamically. -SV, 5 Nov 2008.
272  //currentPosition+=theTMBScope->sizeInWords();
273  currentPosition+=(e05Line-b05Line+1);
274  }
275  else {
276  LogTrace("CSCTMBData|CSCRawToDigi")
277  << "+++ CSCTMBData warning: found 0x6b05 line, but not 0x6e05! +++";
278  }
279  }
280 
282  if (buf[currentPosition]==0x6b07){
283  int Line6b07 = currentPosition;
284  LogTrace("CSCTMBData") << " TMBData ---> Begin of MiniScope found " ;
285  int Line6E07 = findLine(buf, 0x6E07, currentPosition, TotTMBReadout-currentPosition);
286  if(Line6E07 !=-1){
287  LogTrace("CSCTMBData") << " TMBData --> End of MiniScope found " << Line6E07-Line6b07+1 << " words ";
289  theTMBMiniScope = new CSCTMBMiniScope(buf, Line6b07, Line6E07);
290  currentPosition += (Line6E07-Line6b07+1);
291  }
292  else {
293  LogTrace("CSCTMBData")
294  << "+++ CSCTMBData warning MiniScope!: found 0x6b07 line, but not 0x6e07! +++";
295  }
296  }
298 
300  if (buf[currentPosition]==0x6BCB){
301  int Line6BCB = currentPosition;
302  LogTrace("CSCTMBData") << " TMBData ---> Begin of Blocked CFEB found " ;
303  int Line6ECB = findLine(buf, 0x6ECB, currentPosition, TotTMBReadout-currentPosition);
304  if(Line6ECB !=-1){
305  LogTrace("CSCTMBData") << " TMBData --> End of Blocked CFEB found " << Line6ECB-Line6BCB+1 << " words ";
307  theTMBBlockedCFEB = new CSCTMBBlockedCFEB(buf, Line6BCB, Line6ECB);
308  currentPosition += (Line6ECB-Line6BCB+1);
309  }
310  else {
311  LogTrace("CSCTMBData")
312  << "+++ CSCTMBData warning Blocked CFEB!: found 0x6BCB line, but not 0x6ECB! +++";
313  }
314  }
316 
317  int maxLine = findLine(buf, 0xde0f, currentPosition, TotTMBReadout-currentPosition);
318  if(maxLine == -1)
319  {
320  LogTrace("CSCTMBData|CSCRawToDigi") << "+++ CSCTMBData warning: No e0f line!";
321  return 0;
322  }
323 
324  //Now for CRC check put this information into bitset
325 
326  theB0CLine = b0cLine;
327  theE0FLine = maxLine;
328 
329  // finally, the trailer
330  int e0cLine = findLine(buf, 0x6e0c, currentPosition, maxLine);
331  if (e0cLine == -1)
332  {
333  LogTrace("CSCTMBData|CSCRawToDigi") << "+++ CSCTMBData warning: No e0c line!";
334  }
335  else
336  {
337  theTMBTrailer = CSCTMBTrailer(buf+e0cLine, firmwareVersion);
338  LogTrace("CSCTMBData|CSCRawToDigi")
339  << "TMB trailer size: " << theTMBTrailer.sizeInWords();
340  }
341 
342  checkSize();
343 
344  // Dump of TMB; format proposed by JK.
345 #ifdef TMBDUMP
346  LogTrace("CSCTMBData") << "Dump of TMB data:";
347  for (int line = b0cLine; line <= maxLine+3; line++) {
348  LogTrace("CSCTMBData")
349  << "Adr= " << std::setw(4) << line
350  << " Data= " << std::setfill('0') << std::setw(5)
351  << std::uppercase << std::hex << buf[line] << std::dec << std::endl;
352  }
353 #endif
354 
355  // size, since we count from 0 and have one more trailer word
356  // there are sometimes multiple "de0f" lines in trailer, so key on "6e0c"
357  return e0cLine-b0cLine+theTMBTrailer.sizeInWords();
358 } //UnpackTMB
359 
361 {
362  // sum up all the components and see if they have the size indicated in the TMBTrailer
363  return true;
364 }
365 
366 std::bitset<22> CSCTMBData::calCRC22(const std::vector< std::bitset<16> >& datain)
367 {
368  std::bitset<22> CRC;
369  CRC.reset();
370  for(unsigned int i=0;i<datain.size()-3;++i)
371  {
372  CRC=nextCRC22_D16(datain[i],CRC);
373  }
374  return CRC;
375 }
376 
378 {
379  if (!theTMBScopeIsPresent) throw("No TMBScope in this chamber");
380  return * theTMBScope;
381 }
382 
384 {
385  if (!theTMBMiniScopeIsPresent) throw("No TMBScope in this chamber");
386  return * theTMBMiniScope;
387 }
388 
389 
391 {
392  if (!theBlockedCFEBIsPresent) throw("No TMB Blocked CFEB in this chamber");
393  return * theTMBBlockedCFEB;
394 }
395 
396 
397 std::bitset<22> CSCTMBData::nextCRC22_D16(const std::bitset<16>& D,
398  const std::bitset<22>& C)
399 {
400  std::bitset<22> NewCRC;
401 
402  NewCRC[ 0] = D[ 0] ^ C[ 6];
403  NewCRC[ 1] = D[ 1] ^ D[ 0] ^ C[ 6] ^ C[ 7];
404  NewCRC[ 2] = D[ 2] ^ D[ 1] ^ C[ 7] ^ C[ 8];
405  NewCRC[ 3] = D[ 3] ^ D[ 2] ^ C[ 8] ^ C[ 9];
406  NewCRC[ 4] = D[ 4] ^ D[ 3] ^ C[ 9] ^ C[10];
407  NewCRC[ 5] = D[ 5] ^ D[ 4] ^ C[10] ^ C[11];
408  NewCRC[ 6] = D[ 6] ^ D[ 5] ^ C[11] ^ C[12];
409  NewCRC[ 7] = D[ 7] ^ D[ 6] ^ C[12] ^ C[13];
410  NewCRC[ 8] = D[ 8] ^ D[ 7] ^ C[13] ^ C[14];
411  NewCRC[ 9] = D[ 9] ^ D[ 8] ^ C[14] ^ C[15];
412  NewCRC[10] = D[10] ^ D[ 9] ^ C[15] ^ C[16];
413  NewCRC[11] = D[11] ^ D[10] ^ C[16] ^ C[17];
414  NewCRC[12] = D[12] ^ D[11] ^ C[17] ^ C[18];
415  NewCRC[13] = D[13] ^ D[12] ^ C[18] ^ C[19];
416  NewCRC[14] = D[14] ^ D[13] ^ C[19] ^ C[20];
417  NewCRC[15] = D[15] ^ D[14] ^ C[20] ^ C[21];
418  NewCRC[16] = D[15] ^ C[ 0] ^ C[21];
419  NewCRC[17] = C[ 1];
420  NewCRC[18] = C[ 2];
421  NewCRC[19] = C[ 3];
422  NewCRC[20] = C[ 4];
423  NewCRC[21] = C[ 5];
424 
425  return NewCRC;
426 }
427 
428 
429 boost::dynamic_bitset<> CSCTMBData::pack()
430 {
431  boost::dynamic_bitset<> result = bitset_utilities::ushortToBitset(theTMBHeader.sizeInWords()*16,
432  theTMBHeader.data());
433  boost::dynamic_bitset<> clctData = bitset_utilities::ushortToBitset(theCLCTData.sizeInWords()*16,
434  theCLCTData.data());
435  result = bitset_utilities::append(result,clctData);
436  boost::dynamic_bitset<> newResult = result;
437 // theTMBTrailer.setCRC(TMBCRCcalc());
438 
440  theTMBTrailer.data());
441  result = bitset_utilities::append(result,tmbTrailer);
442 
443  // now convert to a vector<bitset<16>>, so we can calculate the crc
444  std::vector<std::bitset<16> > wordVector;
445  // try to tune so it stops before the e0f line
446  for(unsigned pos = 0; pos < result.size()-16; pos += 16)
447  {
448  std::bitset<16> word;
449  for(int i = 0; i < 16; ++i)
450  {
451  word[i] = result[pos+i];
452  }
453  wordVector.push_back(word);
454  }
455  theTMBTrailer.setCRC(calCRC22(wordVector).to_ulong());
457  theTMBTrailer.data());
458  newResult = bitset_utilities::append(newResult, tmbTrailer);
459 
460  return newResult;
461 }
462 
463 
465 {
466  CSCTMBData tmbData;
467  cscClassPackerCompare(tmbData);
468 }
469 
int sizeInWords() const
in 16-bit words
Definition: CSCCLCTData.h:46
CSCTMBTrailer * tmbTrailer()
Definition: CSCTMBData.h:50
unsigned theE0FLine
Definition: CSCTMBData.h:76
CSCCLCTData * clctData()
Definition: CSCTMBData.h:40
int i
Definition: DBlmapReader.cc:9
bool theRPCDataIsPresent
Definition: CSCTMBData.h:97
uint16_t NTBins() const
Definition: CSCTMBHeader.h:74
CSCTMBTrailer theTMBTrailer
Definition: CSCTMBData.h:93
static void selfTest()
tests packing
Definition: CSCTMBData.cc:464
int TMBCRCcalc()
Definition: CSCTMBData.cc:143
unsigned short * theOriginalBuffer
Definition: CSCTMBData.h:72
boost::dynamic_bitset pack()
not const because it sets size int TMBTrailer
Definition: CSCTMBData.cc:429
bool theBlockedCFEBIsPresent
Definition: CSCTMBData.h:90
boost::dynamic_bitset append(const boost::dynamic_bitset<> &bs1, const boost::dynamic_bitset<> &bs2)
this method takes two bitsets bs1 and bs2 and returns result of bs2 appended to the end of bs1 ...
std::bitset< 22 > nextCRC22_D16(const std::bitset< 16 > &D, const std::bitset< 22 > &C)
Definition: CSCTMBData.cc:397
unsigned short size_
Definition: CSCTMBData.h:95
bool theTMBMiniScopeIsPresent
Definition: CSCTMBData.h:87
int sizeInWords() const
in 16-bit frames
Definition: CSCTMBTrailer.h:47
unsigned theB0CLine
Definition: CSCTMBData.h:75
CSCRPCData theRPCData
Definition: CSCTMBData.h:80
CSCTMBBlockedCFEB & tmbBlockedCFEB() const
Definition: CSCTMBData.cc:390
unsigned short * data()
Definition: CSCTMBTrailer.h:48
bool cscClassPackerCompare(T &t)
uint16_t NCFEBs() const
Definition: CSCTMBHeader.h:77
std::bitset< 22 > calCRC22(const std::vector< std::bitset< 16 > > &datain)
Definition: CSCTMBData.cc:366
static bool debug
Definition: CSCTMBData.h:94
void setNCFEBs(uint16_t ncfebs)
Definition: CSCTMBHeader.h:81
CSCTMBHeader theTMBHeader
Definition: CSCTMBData.h:78
CSCTMBBlockedCFEB * theTMBBlockedCFEB
Definition: CSCTMBData.h:91
unsigned short * data()
Definition: CSCCLCTData.h:44
tuple result
Definition: query.py:137
unsigned short * data()
Definition: CSCTMBHeader.h:106
CSCTMBScope & tmbScope() const
Definition: CSCTMBData.cc:377
void setCRC(int crc)
int sizeInWords()
Definition: CSCRPCData.h:19
bool first
Definition: L1TdeRCT.cc:75
#define LogTrace(id)
unsigned short int sizeInWords() const
Definition: CSCTMBHeader.h:100
CSCTMBMiniScope * theTMBMiniScope
Definition: CSCTMBData.h:88
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:150
bool theTMBScopeIsPresent
The TMB scope is not present in most of data hence its dynamic.
Definition: CSCTMBData.h:82
bool check() const
Definition: CSCCLCTData.cc:359
bool check() const
Definition: CSCTMBHeader.h:113
CSCTMBScope * theTMBScope
Definition: CSCTMBData.h:83
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
int FirmwareVersion() const
Definition: CSCTMBHeader.h:34
size_(0)
Definition: OwnArray.h:181
bool checkSize() const
sees if the size adds up to the word count
Definition: CSCTMBData.cc:360
boost::dynamic_bitset ushortToBitset(const unsigned int numberOfBits, unsigned short *buf)
this method takes numberOfBits bits from unsigned short * array and returns them in the bitset obj...
volatile std::atomic< bool > shutdown_flag false
CSCCLCTData theCLCTData
Definition: CSCTMBData.h:79
int UnpackTMB(unsigned short *buf)
Definition: CSCTMBData.cc:161
CSCTMBMiniScope & tmbMiniScope() const
Definition: CSCTMBData.cc:383
int findLine(unsigned short *buf, unsigned short marker, int first, int maxToDo)
Definition: CSCTMBData.cc:134