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