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